You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maka.apache.org by wy...@apache.org on 2026/09/23 18:58:55 UTC

(maka) 03/03: fix(event-log): preserve bundle material and copy provenance

This is an automated email from the ASF dual-hosted git repository.

M4n5ter pushed a commit to branch feat/runtime-host-rust
in repository https://gitbox.apache.org/repos/asf/maka.git

commit a74f8765f8370b665c8f7011ecafe38272dc9495
Author: Wang <m4...@gmail.com>
AuthorDate: Thu Sep 24 02:51:23 2026 +0800

    fix(event-log): preserve bundle material and copy provenance
    
    Generated-by: OpenAI Codex
---
 crates/event-log/src/artifacts.rs                  |  34 ++-
 crates/event-log/src/artifacts/history.rs          | 104 +--------
 crates/event-log/src/artifacts/references.rs       | 113 +++++++++
 crates/event-log/src/bundle.rs                     |   1 +
 crates/event-log/src/bundle/accounting.rs          | 122 ++++++++++
 crates/event-log/src/bundle/blobs.rs               |  23 ++
 crates/event-log/src/bundle/closure.rs             |  33 ++-
 crates/event-log/src/bundle/closure/members.rs     |  77 ++++++
 crates/event-log/src/bundle/export.rs              |  20 ++
 .../src/bundle/migrations/0001_staging.sql         |   7 +
 crates/event-log/src/bundle/validation.rs          |  10 +-
 .../event-log/src/bundle/validation/artifacts.rs   | 260 +++++++++++++++++++++
 crates/event-log/src/bundle/validation/catalog.rs  | 159 +++++++++++++
 crates/event-log/src/bundle/validation/copies.rs   |  30 ++-
 crates/event-log/src/bundle/validation/material.rs |  25 +-
 crates/event-log/src/tool_payloads.rs              |  28 +++
 crates/event-log/tests/bundle/export.rs            |   4 +
 crates/event-log/tests/bundle/fixtures.rs          |  40 ++--
 crates/event-log/tests/bundle/frames.rs            |  50 ++--
 crates/event-log/tests/bundle/materials.rs         | 218 +++++++++++++++++
 crates/event-log/tests/bundle/tamper.rs            | 103 ++++++++
 .../tests/integration/large_outputs.rs             |   1 +
 crates/runtime/src/artifact.rs                     |   9 +
 crates/runtime/src/event.rs                        |   4 +
 crates/runtime/src/event_write.rs                  |  12 +
 crates/runtime/tests/archive.rs                    |   1 +
 crates/runtime/tests/tool_image_success.rs         |   3 +-
 27 files changed, 1330 insertions(+), 161 deletions(-)

diff --git a/crates/event-log/src/artifacts.rs b/crates/event-log/src/artifacts.rs
index ccf7f9232d..0e4887c02c 100644
--- a/crates/event-log/src/artifacts.rs
+++ b/crates/event-log/src/artifacts.rs
@@ -21,6 +21,7 @@ mod copy;
 pub(crate) mod history;
 mod read;
 mod records;
+pub(crate) mod references;
 use crate::{EventLog, StoreError};
 use maka_runtime::{
     artifact::{Artifact, ArtifactSource, content_digest},
@@ -125,18 +126,8 @@ pub(crate) async fn commit_in_transaction(
     mut artifact: Artifact,
     bytes: &[u8],
 ) -> Result<Artifact, StoreError> {
-    artifact.validate().map_err(invalid)?;
-    if artifact.size_bytes != bytes.len() as u64 || artifact.size_bytes > MAX_ATTACHMENT_BYTES {
-        return Err(invalid("Artifact payload size mismatch or limit exceeded"));
-    }
-    let encoded = serde_json::to_string(&artifact)?;
-    if encoded.len() > MAX_RECORD_BYTES {
-        return Err(invalid("Artifact metadata exceeds limit"));
-    }
     let digest = content_digest(bytes);
-    if artifact.source == ArtifactSource::UserUpload && artifact.summary.as_ref() != Some(&digest) {
-        return Err(StoreError::ArtifactConflict);
-    }
+    let encoded = validate_material(&artifact, bytes.len() as u64, &digest)?;
     require_session(connection, &artifact.session_id).await?;
     if let Some((existing, stored_digest)) =
         read_record(connection, &artifact.session_id, &artifact.id).await?
@@ -169,6 +160,27 @@ pub(crate) async fn commit_in_transaction(
     Ok(artifact)
 }
 
+/// Validate immutable metadata against already checked payload length and digest.
+pub(crate) fn validate_material(
+    artifact: &Artifact,
+    bytes: u64,
+    digest: &str,
+) -> Result<String, StoreError> {
+    artifact.validate().map_err(invalid)?;
+    if artifact.size_bytes != bytes || artifact.size_bytes > MAX_ATTACHMENT_BYTES {
+        return Err(invalid("Artifact payload size mismatch or limit exceeded"));
+    }
+    let encoded = serde_json::to_string(artifact)?;
+    if encoded.len() > MAX_RECORD_BYTES {
+        return Err(invalid("Artifact metadata exceeds limit"));
+    }
+    if artifact.source == ArtifactSource::UserUpload && artifact.summary.as_deref() != Some(digest)
+    {
+        return Err(StoreError::ArtifactConflict);
+    }
+    Ok(encoded)
+}
+
 pub(crate) async fn verify_projection_replay(
     connection: &mut sqlx::SqliteConnection,
     artifact: &Artifact,
diff --git a/crates/event-log/src/artifacts/history.rs b/crates/event-log/src/artifacts/history.rs
index aa0f6c4978..701a182de5 100644
--- a/crates/event-log/src/artifacts/history.rs
+++ b/crates/event-log/src/artifacts/history.rs
@@ -21,8 +21,7 @@ use super::records::{advance, invalid, read_record};
 use crate::StoreError;
 use maka_runtime::{
     attachment::{AttachmentRef, StorageRef},
-    event::{Fact, InvocationInput, RuntimeEvent, ToolOutcome},
-    tool_output::{DurableToolProjection, ProjectionPart},
+    event::RuntimeEvent,
 };
 use sqlx::SqliteConnection;
 
@@ -96,70 +95,7 @@ pub(crate) async fn retain(
         ).bind(target).bind(after).fetch_optional(&mut *tx).await?;
         let Some((sequence, json)) = row else { break };
         let event: RuntimeEvent = serde_json::from_str(&json)?;
-        let mut references: Vec<(&StorageRef, Option<&AttachmentRef>)> = Vec::new();
-        match &event.fact {
-            Fact::InvocationOpened {
-                input:
-                    InvocationInput::Message {
-                        content,
-                        source_messages,
-                        ..
-                    },
-                ..
-            } => {
-                references.extend(
-                    content
-                        .attachments
-                        .iter()
-                        .flatten()
-                        .map(|a| (&a.storage_ref, Some(a))),
-                );
-                for message in source_messages {
-                    references.extend(
-                        message
-                            .unprepared_content
-                            .attachments
-                            .iter()
-                            .flatten()
-                            .map(|a| (&a.storage_ref, Some(a))),
-                    );
-                    references.extend(
-                        message
-                            .message
-                            .content
-                            .attachments
-                            .iter()
-                            .flatten()
-                            .map(|a| (&a.storage_ref, Some(a))),
-                    );
-                }
-            }
-            Fact::MessageSteered { message } => {
-                references.extend(
-                    message
-                        .content
-                        .attachments
-                        .iter()
-                        .flatten()
-                        .map(|a| (&a.storage_ref, Some(a))),
-                );
-            }
-            Fact::ToolSettled {
-                outcome:
-                    ToolOutcome::Succeeded {
-                        model_projection: DurableToolProjection::Content { parts },
-                        ..
-                    },
-                ..
-            } => {
-                references.extend(parts.iter().filter_map(|part| match part {
-                    ProjectionPart::Artifact { image } => Some((&image.reference, None)),
-                    ProjectionPart::Text { .. } => None,
-                }));
-            }
-            _ => {}
-        }
-        for (reference, attachment) in references {
+        for (reference, attachment) in super::references::from_event(&event) {
             retain_one(tx, source, target, reference, attachment, now).await?;
         }
         after = sequence;
@@ -186,32 +122,16 @@ pub(crate) async fn retain_revision(
         .await?;
         let Some((sequence, json)) = row else { break };
         let event: RuntimeEvent = serde_json::from_str(&json)?;
-        let Fact::InvocationOpened {
-            input: InvocationInput::Message {
-                source_messages, ..
-            },
-            ..
-        } = event.fact
-        else {
-            return Err(invalid("revision source is not a message opening"));
-        };
-        for source_message in source_messages {
-            for attachment in source_message
-                .unprepared_content
-                .attachments
-                .iter()
-                .flatten()
-            {
-                retain_one(
-                    tx,
-                    source,
-                    target,
-                    &attachment.storage_ref,
-                    Some(attachment),
-                    now,
-                )
-                .await?;
-            }
+        for attachment in super::references::revision(&event)? {
+            retain_one(
+                tx,
+                source,
+                target,
+                &attachment.storage_ref,
+                Some(attachment),
+                now,
+            )
+            .await?;
         }
         after = sequence;
     }
diff --git a/crates/event-log/src/artifacts/references.rs b/crates/event-log/src/artifacts/references.rs
new file mode 100644
index 0000000000..fbf191f63d
--- /dev/null
+++ b/crates/event-log/src/artifacts/references.rs
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use crate::StoreError;
+use maka_runtime::{
+    attachment::{AttachmentRef, StorageRef},
+    event::{Fact, InvocationInput, RuntimeEvent, ToolOutcome},
+    tool_output::{DurableToolProjection, ProjectionPart},
+};
+
+/// Only typed canonical references convey ownership; arbitrary plugin JSON does not.
+pub(crate) fn from_event(event: &RuntimeEvent) -> Vec<(&StorageRef, Option<&AttachmentRef>)> {
+    let mut references: Vec<(&StorageRef, Option<&AttachmentRef>)> = Vec::new();
+    match &event.fact {
+        Fact::InvocationOpened {
+            input:
+                InvocationInput::Message {
+                    content,
+                    source_messages,
+                    ..
+                },
+            ..
+        } => {
+            references.extend(
+                content
+                    .attachments
+                    .iter()
+                    .flatten()
+                    .map(|a| (&a.storage_ref, Some(a))),
+            );
+            for message in source_messages {
+                references.extend(
+                    message
+                        .unprepared_content
+                        .attachments
+                        .iter()
+                        .flatten()
+                        .map(|a| (&a.storage_ref, Some(a))),
+                );
+                references.extend(
+                    message
+                        .message
+                        .content
+                        .attachments
+                        .iter()
+                        .flatten()
+                        .map(|a| (&a.storage_ref, Some(a))),
+                );
+            }
+        }
+        Fact::MessageSteered { message } => {
+            references.extend(
+                message
+                    .content
+                    .attachments
+                    .iter()
+                    .flatten()
+                    .map(|a| (&a.storage_ref, Some(a))),
+            );
+        }
+        Fact::ToolSettled {
+            outcome:
+                ToolOutcome::Succeeded {
+                    model_projection: DurableToolProjection::Content { parts },
+                    ..
+                },
+            ..
+        } => {
+            references.extend(parts.iter().filter_map(|part| match part {
+                ProjectionPart::Artifact { image } => Some((&image.reference, None)),
+                ProjectionPart::Text { .. } => None,
+            }));
+        }
+        _ => {}
+    }
+
+    references
+}
+
+/// Only original editable inputs belong to Revision evidence.
+pub(crate) fn revision(event: &RuntimeEvent) -> Result<Vec<&AttachmentRef>, StoreError> {
+    let Fact::InvocationOpened {
+        input: InvocationInput::Message {
+            source_messages, ..
+        },
+        ..
+    } = &event.fact
+    else {
+        return Err(StoreError::InvalidTransition(
+            "revision source is not a message opening".into(),
+        ));
+    };
+    Ok(source_messages
+        .iter()
+        .flat_map(|message| message.unprepared_content.attachments.iter().flatten())
+        .collect())
+}
diff --git a/crates/event-log/src/bundle.rs b/crates/event-log/src/bundle.rs
index 6e5706c2f5..cc358fccc7 100644
--- a/crates/event-log/src/bundle.rs
+++ b/crates/event-log/src/bundle.rs
@@ -26,6 +26,7 @@ use sha2::{Digest, Sha256};
 use sqlx::{Connection, SqliteConnection};
 use std::collections::HashSet;
 
+mod accounting;
 mod blobs;
 mod closure;
 mod export;
diff --git a/crates/event-log/src/bundle/accounting.rs b/crates/event-log/src/bundle/accounting.rs
new file mode 100644
index 0000000000..434c277bdf
--- /dev/null
+++ b/crates/event-log/src/bundle/accounting.rs
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use super::format::Record;
+use crate::StoreError;
+use maka_runtime::{
+    event::{Fact, RuntimeEvent},
+    model::{ModelEvent, ModelUsage},
+};
+use sqlx::SqliteConnection;
+
+/// The first provider usage fact fixes a request's valuation, if it was quoted.
+pub(super) async fn witness(
+    db: &mut SqliteConnection,
+    request: &str,
+) -> Result<Option<u64>, StoreError> {
+    let row: Option<i64> = sqlx::query_scalar(
+        "SELECT MIN(e.sequence) FROM runtime_events r JOIN runtime_events e ON e.invocation_id=r.invocation_id
+         AND json_extract(e.event_json,'$.fact.step_id')=json_extract(r.event_json,'$.fact.step_id')
+         WHERE r.event_id=? AND (e.kind='model_completed' OR
+          (e.kind='model_observed' AND json_extract(e.event_json,'$.fact.event.kind')='finished'))"
+    ).bind(request).fetch_one(db).await?;
+    row.map(crate::sequence_number).transpose()
+}
+
+pub(super) async fn validate(
+    staged: &mut SqliteConnection,
+    original: &mut SqliteConnection,
+) -> Result<(), StoreError> {
+    let mut after = 0i64;
+    loop {
+        let row: Option<(i64, String)> = sqlx::query_as(
+            "SELECT number,record_json FROM frames WHERE kind='accounting' AND number>? ORDER BY number LIMIT 1"
+        ).bind(after).fetch_optional(&mut *staged).await?;
+        let Some((number, json)) = row else { break };
+        let Record::Accounting {
+            event_id,
+            quote,
+            valuation,
+        } = serde_json::from_str(&json)?
+        else {
+            unreachable!()
+        };
+        let request: Option<String> = sqlx::query_scalar(
+            "SELECT event_json FROM runtime_events WHERE event_id=? AND kind='model_requested'",
+        )
+        .bind(&event_id)
+        .fetch_optional(&mut *original)
+        .await?;
+        let event: RuntimeEvent =
+            serde_json::from_str(&request.ok_or_else(|| invalid("unbound bundle model quote"))?)?;
+        let Fact::ModelRequested {
+            model_id, step_id, ..
+        } = &event.fact
+        else {
+            unreachable!()
+        };
+        quote.validate(model_id).map_err(invalid)?;
+        crate::usage::valuation::capture(original, &event_id, &quote).await?;
+        let mut observed: Option<ModelUsage> = None;
+        let mut sequence = 0i64;
+        loop {
+            let row: Option<(i64, String)> = sqlx::query_as(
+                "SELECT sequence,event_json FROM runtime_events WHERE invocation_id=?1 AND sequence>?2
+                 AND json_extract(event_json,'$.fact.step_id')=?3 AND (kind='model_completed' OR
+                  (kind='model_observed' AND json_extract(event_json,'$.fact.event.kind')='finished'))
+                 ORDER BY sequence LIMIT 1"
+            ).bind(&event.invocation.invocation_id).bind(sequence).bind(step_id).fetch_optional(&mut *original).await?;
+            let Some((next, json)) = row else { break };
+            let usage_event: RuntimeEvent = serde_json::from_str(&json)?;
+            let usage = match usage_event.fact {
+                Fact::ModelCompleted { output, .. } => output.usage,
+                Fact::ModelObserved {
+                    event: ModelEvent::Finished { usage, .. },
+                    ..
+                } => usage,
+                _ => unreachable!(),
+            };
+            if observed.as_ref().is_some_and(|prior| prior != &usage) {
+                return Err(invalid("bundle request has contradictory usage evidence"));
+            }
+            observed = Some(usage);
+            sequence = next;
+        }
+        match (observed, valuation) {
+            (None, None) => {}
+            (Some(usage), Some(valuation))
+                if usage == valuation.usage
+                    && quote.estimate(&usage).map_err(invalid)? == valuation.usd =>
+            {
+                crate::usage::valuation::value(original, &event_id, &usage).await?;
+            }
+            _ => {
+                return Err(invalid(
+                    "bundle valuation differs from its selected usage evidence",
+                ));
+            }
+        }
+        after = number;
+    }
+    Ok(())
+}
+
+fn invalid(message: &str) -> StoreError {
+    StoreError::InvalidTransition(message.into())
+}
diff --git a/crates/event-log/src/bundle/blobs.rs b/crates/event-log/src/bundle/blobs.rs
index df48214273..927d0c782d 100644
--- a/crates/event-log/src/bundle/blobs.rs
+++ b/crates/event-log/src/bundle/blobs.rs
@@ -72,6 +72,7 @@ pub(super) async fn export<W: AsyncWrite + Unpin>(
         }
     }
     for session in &inventory.sessions {
+        required(db, &session.id).await?;
         let mut after = None::<String>;
         loop {
             let next: Option<(String, String, String, i64)> = sqlx::query_as(
@@ -121,6 +122,28 @@ pub(super) async fn export<W: AsyncWrite + Unpin>(
     Ok(())
 }
 
+async fn required(db: &mut SqliteConnection, session: &str) -> Result<(), StoreError> {
+    // Copied evidence resolves through destination-owned material, never through
+    // a live lookup into an ancestor's artifact namespace.
+    let broken: bool = sqlx::query_scalar(
+        "SELECT EXISTS(SELECT 1 FROM session_history_events e
+         JOIN json_each(e.event_json,'$.fact.outcome.artifacts') proof
+         LEFT JOIN session_history_artifacts h ON h.session_id=e.owner_session_id
+           AND h.source_session_id=e.event_session AND h.source_artifact_id=json_extract(proof.value,'$.id')
+         LEFT JOIN artifacts a ON a.session_id=e.owner_session_id AND
+           a.id=CASE WHEN e.inherited=1 THEN h.artifact_id ELSE json_extract(proof.value,'$.id') END
+         WHERE e.owner_session_id=? AND e.kind='tool_settled' AND
+           (a.id IS NULL OR length(a.payload)!=json_extract(proof.value,'$.bytes')
+            OR a.content_sha256!=json_extract(proof.value,'$.digest')
+            OR json_extract(a.record_json,'$.source')!='tool_result_projection'
+            OR json_extract(a.record_json,'$.turnId')!=json_extract(e.event_json,'$.invocation.turn_id')))"
+    ).bind(session).fetch_one(db).await?;
+    if broken {
+        return Err(invalid("bundle lacks generated tool material"));
+    }
+    Ok(())
+}
+
 async fn transfer<W: AsyncWrite + Unpin>(
     db: &mut SqliteConnection,
     blob: Blob,
diff --git a/crates/event-log/src/bundle/closure.rs b/crates/event-log/src/bundle/closure.rs
index 778e14bd49..23f3d2aebd 100644
--- a/crates/event-log/src/bundle/closure.rs
+++ b/crates/event-log/src/bundle/closure.rs
@@ -26,6 +26,8 @@ use maka_runtime::{
 use sqlx::SqliteConnection;
 use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
 
+mod members;
+
 /// Event identities and exact copied memberships, not additional catalog Sessions.
 pub(super) struct Closure {
     pub events: BTreeSet<u64>,
@@ -55,15 +57,6 @@ impl Closure {
             bytes: 0,
         };
         for session in &inventory.sessions {
-            let busy: bool = sqlx::query_scalar(
-                "SELECT EXISTS(SELECT 1 FROM local_runtime_events o WHERE o.kind='invocation_opened' AND o.event_session=?1
-                 AND NOT EXISTS(SELECT 1 FROM runtime_events t WHERE t.invocation_id=o.invocation_id AND t.kind='invocation_ended'))
-                 OR EXISTS(SELECT 1 FROM message_admissions WHERE session_id=?1)
-                 OR EXISTS(SELECT 1 FROM session_processes WHERE session_id=?1 AND cleaned=0)"
-            ).bind(&session.id).fetch_one(&mut *db).await?;
-            if busy {
-                return Err(StoreError::SessionBusy);
-            }
             selected
                 .scope(
                     db,
@@ -80,6 +73,13 @@ impl Closure {
                 let sequence = sequence_number(sequence)?;
                 selected.add(sequence)?;
                 selected.revisions.insert((session.id.clone(), sequence));
+                let source: String = sqlx::query_scalar(
+                    "SELECT source_session_id FROM session_history_copies WHERE session_id=?",
+                )
+                .bind(&session.id)
+                .fetch_one(&mut *db)
+                .await?;
+                selected.member(db, &source, sequence).await?;
                 if selected.revisions.len() > MAX_EVENTS {
                     return Err(StoreError::PrefixTooLarge);
                 }
@@ -243,8 +243,8 @@ impl Closure {
             return Err(StoreError::MaterialCollected);
         }
         let filter = Selection::predicate("e", "?4");
-        let rows: Vec<(i64, bool, Option<i64>)> = sqlx::query_as(sqlx::AssertSqlSafe(format!(
-            "SELECT e.sequence,e.inherited,e.archive_sequence FROM session_history_events e
+        let rows: Vec<(i64, bool)> = sqlx::query_as(sqlx::AssertSqlSafe(format!(
+            "SELECT e.sequence,e.inherited FROM session_history_events e
              WHERE e.owner_session_id=?1 AND e.sequence>?2 AND e.sequence<=?3 AND {filter}
              ORDER BY e.sequence LIMIT ?5"
         )))
@@ -255,18 +255,11 @@ impl Closure {
         .bind((MAX_EVENTS + 1) as i64)
         .fetch_all(&mut *db)
         .await?;
-        for (sequence, inherited, archive) in rows {
+        for (sequence, inherited) in rows {
             let sequence = sequence_number(sequence)?;
             self.add(sequence)?;
             if inherited {
-                let archive = archive.map(sequence_number).transpose()?;
-                if let Some(archive) = archive {
-                    self.add(archive)?;
-                }
-                self.members.insert((session.clone(), sequence), archive);
-                if self.members.len() > MAX_EVENTS {
-                    return Err(StoreError::PrefixTooLarge);
-                }
+                self.member(db, session, sequence).await?;
             }
         }
         self.scopes.insert(key, through);
diff --git a/crates/event-log/src/bundle/closure/members.rs b/crates/event-log/src/bundle/closure/members.rs
new file mode 100644
index 0000000000..eee26669cf
--- /dev/null
+++ b/crates/event-log/src/bundle/closure/members.rs
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use super::Closure;
+use crate::{
+    StoreError,
+    bundle::{MAX_SESSIONS, format::MAX_EVENTS},
+    sequence_number,
+};
+use sqlx::SqliteConnection;
+use std::collections::BTreeSet;
+
+impl Closure {
+    /// Follow only this member's flattened ownership chain. Do not expose a
+    /// proof-only parent's complete conversation, catalog or private artifacts.
+    pub(super) async fn member(
+        &mut self,
+        db: &mut SqliteConnection,
+        session: &str,
+        sequence: u64,
+    ) -> Result<(), StoreError> {
+        let owner: String =
+            sqlx::query_scalar("SELECT event_session FROM runtime_events WHERE sequence=?")
+                .bind(sequence as i64)
+                .fetch_one(&mut *db)
+                .await?;
+        let mut current = session.to_owned();
+        let mut seen = BTreeSet::new();
+        while current != owner {
+            if !seen.insert(current.clone()) || seen.len() > MAX_SESSIONS {
+                return Err(invalid("bundle member ownership contains a cycle"));
+            }
+            if self.members.contains_key(&(current.clone(), sequence)) {
+                return Ok(());
+            }
+            let row: Option<(String, Option<i64>)> = sqlx::query_as(
+                "SELECT c.source_session_id,h.archive_sequence FROM session_history_copies c
+                 JOIN session_history_members h ON h.session_id=c.session_id WHERE c.session_id=? AND h.sequence=?"
+            ).bind(&current).bind(sequence as i64).fetch_optional(&mut *db).await?;
+            let (source, archive) =
+                row.ok_or_else(|| invalid("bundle member is outside its declared source history"))?;
+            self.copies.insert(current.clone());
+            self.members.insert(
+                (current, sequence),
+                archive.map(sequence_number).transpose()?,
+            );
+            if self.copies.len() > MAX_SESSIONS || self.members.len() > MAX_EVENTS {
+                return Err(StoreError::PrefixTooLarge);
+            }
+            self.add(sequence)?;
+            if let Some(archive) = archive {
+                self.add(sequence_number(archive)?)?;
+            }
+            current = source;
+        }
+        Ok(())
+    }
+}
+fn invalid(message: &str) -> StoreError {
+    StoreError::InvalidTransition(message.into())
+}
diff --git a/crates/event-log/src/bundle/export.rs b/crates/event-log/src/bundle/export.rs
index ec4faddea8..550c8e26e1 100644
--- a/crates/event-log/src/bundle/export.rs
+++ b/crates/event-log/src/bundle/export.rs
@@ -50,6 +50,7 @@ impl EventLog {
             let result = async {
                 let inventory = super::inventory(&mut tx, &root).await?;
                 inventory.verify_confirmation(&expected)?;
+                require_idle(&mut tx, &inventory).await?;
                 let through = sequence_number(sqlx::query_scalar::<_, i64>("SELECT COALESCE(MAX(sequence),0) FROM event_log")
                     .fetch_one(&mut *tx).await.map_err(StoreError::from)?)?;
                 let closure = Closure::capture(&mut tx, &inventory, through).await?;
@@ -74,6 +75,21 @@ impl EventLog {
     }
 }
 
+async fn require_idle(db: &mut SqliteConnection, inventory: &Inventory) -> Result<(), StoreError> {
+    for session in &inventory.sessions {
+        let busy: bool = sqlx::query_scalar(
+            "SELECT EXISTS(SELECT 1 FROM local_runtime_events o WHERE o.kind='invocation_opened' AND o.event_session=?1
+             AND NOT EXISTS(SELECT 1 FROM runtime_events t WHERE t.invocation_id=o.invocation_id AND t.kind='invocation_ended'))
+             OR EXISTS(SELECT 1 FROM message_admissions WHERE session_id=?1)
+             OR EXISTS(SELECT 1 FROM session_processes WHERE session_id=?1 AND cleaned=0)"
+        ).bind(&session.id).fetch_one(&mut *db).await?;
+        if busy {
+            return Err(StoreError::SessionBusy);
+        }
+    }
+    Ok(())
+}
+
 async fn catalog<W: AsyncWrite + Unpin>(
     db: &mut SqliteConnection,
     inventory: &Inventory,
@@ -164,6 +180,10 @@ async fn accounting<W: AsyncWrite + Unpin>(
         .fetch_optional(&mut *db)
         .await?;
         if let Some((event_id, quote, usage, usd)) = row {
+            // A proof-only request need not include its later completion. Its
+            // future usage is not evidence belonging to this transfer.
+            let witness = super::accounting::witness(db, &event_id).await?;
+            let usage = usage.filter(|_| witness.is_some_and(|n| closure.events.contains(&n)));
             writer
                 .record(&Record::Accounting {
                     event_id,
diff --git a/crates/event-log/src/bundle/migrations/0001_staging.sql b/crates/event-log/src/bundle/migrations/0001_staging.sql
index f535a8f0f4..e8ca7543f7 100644
--- a/crates/event-log/src/bundle/migrations/0001_staging.sql
+++ b/crates/event-log/src/bundle/migrations/0001_staging.sql
@@ -35,6 +35,13 @@ CREATE UNIQUE INDEX event_blobs ON frames(json_extract(record_json, '$.resource'
     WHERE kind = 'blob' AND json_extract(record_json, '$.resource') IN ('tool_result', 'composition');
 CREATE UNIQUE INDEX artifact_blobs ON frames(json_extract(record_json, '$.metadata.sessionId'), json_extract(record_json, '$.metadata.id'))
     WHERE kind = 'blob' AND json_extract(record_json, '$.resource') = 'artifact';
+CREATE UNIQUE INDEX history_artifacts ON frames(json_extract(record_json, '$.session'), json_extract(record_json, '$.source_session'), json_extract(record_json, '$.source_artifact'))
+    WHERE kind = 'history_artifact';
+
+-- Derived during validation, never supplied by the transfer.
+CREATE TABLE referenced_mappings (
+    frame INTEGER PRIMARY KEY REFERENCES frames(number)
+);
 
 -- Keep binary payloads in bounded pieces; metadata never becomes file paths.
 CREATE TABLE chunks (
diff --git a/crates/event-log/src/bundle/validation.rs b/crates/event-log/src/bundle/validation.rs
index c4389001c2..1e3b6e8015 100644
--- a/crates/event-log/src/bundle/validation.rs
+++ b/crates/event-log/src/bundle/validation.rs
@@ -28,6 +28,8 @@ use maka_runtime::{
 };
 use sqlx::{Connection, SqliteConnection, sqlite::SqliteConnectOptions};
 
+mod artifacts;
+mod catalog;
 mod copies;
 mod material;
 
@@ -62,7 +64,8 @@ async fn replay(
         .fetch_one(&mut *staged)
         .await?;
     let Record::Header {
-        source_high_water, ..
+        source_high_water,
+        inventory,
     } = serde_json::from_str(&header)?
     else {
         return Err(invalid("missing staged header"));
@@ -161,7 +164,10 @@ async fn replay(
         }
         after = sequence;
     }
-    material::check_bindings(staged, original).await
+    material::check_bindings(staged, original).await?;
+    catalog::validate(staged, original, &inventory, source_high_water).await?;
+    artifacts::validate(staged, original, &inventory).await?;
+    super::accounting::validate(staged, original).await
 }
 
 async fn request(
diff --git a/crates/event-log/src/bundle/validation/artifacts.rs b/crates/event-log/src/bundle/validation/artifacts.rs
new file mode 100644
index 0000000000..fc9af14784
--- /dev/null
+++ b/crates/event-log/src/bundle/validation/artifacts.rs
@@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use super::invalid;
+use crate::{
+    StoreError,
+    artifacts::references,
+    bundle::{
+        Inventory,
+        format::{Blob, Record},
+    },
+};
+use maka_runtime::{
+    artifact::Artifact,
+    attachment::{AttachmentRef, StorageRef},
+    event::RuntimeEvent,
+};
+use sqlx::SqliteConnection;
+
+pub(super) async fn validate(
+    staged: &mut SqliteConnection,
+    original: &mut SqliteConnection,
+    inventory: &Inventory,
+) -> Result<(), StoreError> {
+    sqlx::query("DELETE FROM referenced_mappings")
+        .execute(&mut *staged)
+        .await?;
+    let mut after = 0i64;
+    loop {
+        let row: Option<(i64, String)> = sqlx::query_as(
+            "SELECT number,record_json FROM frames WHERE number>? AND
+             ((kind='blob' AND json_extract(record_json,'$.resource')='artifact') OR kind='history_artifact')
+             ORDER BY number LIMIT 1"
+        ).bind(after).fetch_optional(&mut *staged).await?;
+        let Some((number, json)) = row else { break };
+        match serde_json::from_str(&json)? {
+            Record::Blob(Blob::Artifact { metadata, digest }) => {
+                selected(inventory, &metadata.session_id)?;
+                crate::artifacts::validate_material(&metadata, metadata.size_bytes, &digest)?;
+            }
+            Record::HistoryArtifact {
+                session,
+                source_session,
+                source_artifact,
+                artifact,
+            } => {
+                selected(inventory, &session)?;
+                crate::sessions::validate_id(&source_session)?;
+                maka_runtime::interaction::entity_id(&source_artifact).map_err(invalid)?;
+                let identity = maka_runtime::artifact::content_digest(&serde_json::to_vec(&(
+                    &source_session,
+                    &source_artifact,
+                ))?);
+                if artifact != format!("history-{}", identity.trim_start_matches("sha256:")) {
+                    return Err(invalid(
+                        "bundle history Artifact identity differs from its provenance",
+                    ));
+                }
+                if descriptor(staged, &session, &artifact).await?.is_none() {
+                    return Err(invalid("bundle history mapping lacks its owned Artifact"));
+                }
+            }
+            _ => unreachable!(),
+        }
+        after = number;
+    }
+    for session in &inventory.sessions {
+        let mut after = (0i64, false);
+        loop {
+            let row: Option<(i64, bool, bool, String)> = sqlx::query_as(
+                "SELECT e.sequence,0 AS revision,e.inherited,e.event_json FROM session_history_events e
+                 WHERE e.owner_session_id=?1 AND (e.sequence,0)>(?2,?3)
+                 UNION ALL
+                 SELECT e.sequence,1 AS revision,1 AS inherited,e.event_json FROM session_revision_sources r
+                 JOIN runtime_events e ON e.sequence=r.sequence WHERE r.session_id=?1 AND (e.sequence,1)>(?2,?3)
+                 ORDER BY sequence,revision LIMIT 1"
+            ).bind(&session.id).bind(after.0).bind(after.1).fetch_optional(&mut *original).await?;
+            let Some((sequence, revision, inherited, json)) = row else {
+                break;
+            };
+            let event: RuntimeEvent = serde_json::from_str(&json)?;
+            let refs = if revision {
+                references::revision(&event)?
+                    .into_iter()
+                    .map(|a| (&a.storage_ref, Some(a)))
+                    .collect()
+            } else {
+                references::from_event(&event)
+            };
+            for (reference, attachment) in refs {
+                reference_material(staged, &session.id, inherited, reference, attachment).await?;
+            }
+            generated(staged, &session.id, inherited, &event).await?;
+            after = (sequence, revision);
+        }
+    }
+    let unbound: bool = sqlx::query_scalar(
+        "SELECT EXISTS(SELECT 1 FROM frames f WHERE f.kind='history_artifact'
+         AND NOT EXISTS(SELECT 1 FROM referenced_mappings r WHERE r.frame=f.number))",
+    )
+    .fetch_one(staged)
+    .await?;
+    if unbound {
+        return Err(invalid(
+            "bundle history mapping lacks a typed copied reference",
+        ));
+    }
+    Ok(())
+}
+
+async fn reference_material(
+    staged: &mut SqliteConnection,
+    owner: &str,
+    inherited: bool,
+    reference: &StorageRef,
+    attachment: Option<&AttachmentRef>,
+) -> Result<(), StoreError> {
+    let StorageRef::SessionFile {
+        session_id,
+        relative_path,
+    } = reference
+    else {
+        // Workspace and external files are not transferred, and Session context
+        // is canonical evidence rather than an Artifact payload.
+        return Ok(());
+    };
+    let artifact = if inherited {
+        let mapping: Option<(i64, String)> = sqlx::query_as(
+            "SELECT number,json_extract(record_json,'$.artifact') FROM frames WHERE kind='history_artifact'
+             AND json_extract(record_json,'$.session')=? AND json_extract(record_json,'$.source_session')=?
+             AND json_extract(record_json,'$.source_artifact')=?"
+        ).bind(owner).bind(session_id).bind(relative_path).fetch_optional(&mut *staged).await?;
+        let (frame, id) = mapping
+            .ok_or_else(|| invalid("bundle copied reference lacks its retained Artifact"))?;
+        sqlx::query("INSERT INTO referenced_mappings(frame) VALUES(?) ON CONFLICT DO NOTHING")
+            .bind(frame)
+            .execute(&mut *staged)
+            .await?;
+        descriptor(staged, owner, &id)
+            .await?
+            .ok_or_else(|| invalid("bundle history mapping lacks its owned Artifact"))?
+            .0
+    } else if session_id == owner {
+        // Users may delete ordinary uploads while their immutable references
+        // remain in history. Preserve that absence instead of inventing bytes.
+        let Some((artifact, _)) = descriptor(staged, owner, relative_path).await? else {
+            return Ok(());
+        };
+        artifact
+    } else {
+        return Ok(());
+    };
+    if let Some(attachment) = attachment {
+        artifact.validate_attachment(attachment).map_err(invalid)?;
+    }
+    Ok(())
+}
+
+async fn descriptor(
+    staged: &mut SqliteConnection,
+    session: &str,
+    id: &str,
+) -> Result<Option<(Artifact, String)>, StoreError> {
+    let row: Option<String> = sqlx::query_scalar(
+        "SELECT record_json FROM frames WHERE kind='blob' AND json_extract(record_json,'$.resource')='artifact'
+         AND json_extract(record_json,'$.metadata.sessionId')=? AND json_extract(record_json,'$.metadata.id')=?"
+    ).bind(session).bind(id).fetch_optional(staged).await?;
+    row.map(|json| {
+        let Record::Blob(Blob::Artifact { metadata, digest }) = serde_json::from_str(&json)? else {
+            unreachable!()
+        };
+        Ok((metadata, digest))
+    })
+    .transpose()
+}
+
+async fn generated(
+    staged: &mut SqliteConnection,
+    owner: &str,
+    inherited: bool,
+    event: &RuntimeEvent,
+) -> Result<(), StoreError> {
+    use maka_runtime::{
+        artifact::{ArtifactKind, ArtifactSource},
+        event::{Fact, ToolOutcome},
+        tool_output::{DurableToolProjection, ProjectionPart},
+    };
+    let Fact::ToolSettled {
+        outcome:
+            ToolOutcome::Succeeded {
+                artifacts,
+                model_projection,
+                ..
+            },
+        ..
+    } = &event.fact
+    else {
+        return Ok(());
+    };
+    for evidence in artifacts {
+        let id = if inherited {
+            sqlx::query_scalar(
+                "SELECT json_extract(record_json,'$.artifact') FROM frames WHERE kind='history_artifact'
+                 AND json_extract(record_json,'$.session')=? AND json_extract(record_json,'$.source_session')=?
+                 AND json_extract(record_json,'$.source_artifact')=?"
+            ).bind(owner).bind(&event.invocation.session_id).bind(&evidence.id)
+                .fetch_optional(&mut *staged).await?
+                .ok_or_else(|| invalid("bundle generated Artifact has no copied ownership"))?
+        } else {
+            evidence.id.clone()
+        };
+        let (artifact, digest) = descriptor(staged, owner, &id)
+            .await?
+            .ok_or_else(|| invalid("bundle lacks generated tool material"))?;
+        let mime_matches = matches!(model_projection, DurableToolProjection::Content { parts } if parts.iter().any(|part|
+            matches!(part, ProjectionPart::Artifact { image } if
+                matches!(&image.reference, StorageRef::SessionFile { relative_path, .. } if relative_path == &evidence.id)
+                && artifact.mime_type.as_deref() == Some(image.mime_type.as_str()))
+        ));
+        if artifact.source != ArtifactSource::ToolResultProjection
+            || artifact.kind != ArtifactKind::Image
+            || artifact.turn_id != event.invocation.turn_id
+            || artifact.size_bytes != evidence.bytes
+            || digest != evidence.digest
+            || !mime_matches
+        {
+            return Err(invalid(
+                "bundle generated tool material differs from its canonical evidence",
+            ));
+        }
+    }
+    Ok(())
+}
+
+fn selected(inventory: &Inventory, id: &str) -> Result<(), StoreError> {
+    if inventory
+        .sessions
+        .binary_search_by(|s| s.id.as_str().cmp(id))
+        .is_err()
+    {
+        return Err(invalid("bundle Artifact is outside its selected catalog"));
+    }
+    Ok(())
+}
diff --git a/crates/event-log/src/bundle/validation/catalog.rs b/crates/event-log/src/bundle/validation/catalog.rs
new file mode 100644
index 0000000000..43d643d821
--- /dev/null
+++ b/crates/event-log/src/bundle/validation/catalog.rs
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use super::invalid;
+use crate::{
+    StoreError,
+    bundle::{Inventory, closure::Closure, format::Record},
+};
+use sqlx::SqliteConnection;
+use std::collections::{BTreeMap, BTreeSet, VecDeque};
+
+pub(super) async fn validate(
+    staged: &mut SqliteConnection,
+    original: &mut SqliteConnection,
+    inventory: &Inventory,
+    fence: u64,
+) -> Result<(), StoreError> {
+    let mut parents: BTreeMap<&str, BTreeSet<String>> = inventory
+        .sessions
+        .iter()
+        .map(|s| (s.id.as_str(), BTreeSet::new()))
+        .collect();
+    let mut seen = BTreeSet::new();
+    let mut after = 0i64;
+    loop {
+        let row: Option<(i64, String)> = sqlx::query_as(
+            "SELECT number,record_json FROM frames WHERE kind='session' AND number>? ORDER BY number LIMIT 1"
+        ).bind(after).fetch_optional(&mut *staged).await?;
+        let Some((number, json)) = row else { break };
+        let Record::Session {
+            id,
+            parent,
+            created_at,
+            updated_at,
+            configuration,
+            ..
+        } = serde_json::from_str(&json)?
+        else {
+            unreachable!()
+        };
+        crate::sessions::validate_id(&id)?;
+        crate::sessions::validate_time(created_at)?;
+        crate::sessions::validate_time(updated_at)?;
+        if serde_json::to_vec(&configuration)?.len() > crate::sessions::MAX_CONFIGURATION_BYTES {
+            return Err(invalid("bundle Session configuration exceeds its limit"));
+        }
+        let edges = parents
+            .get_mut(id.as_str())
+            .ok_or_else(|| invalid("bundle catalog differs from its inventory"))?;
+        if let Some(parent) = parent {
+            crate::sessions::validate_id(&parent)?;
+            edges.insert(parent);
+        }
+        seen.insert(id);
+        after = number;
+    }
+    if seen.len() != inventory.sessions.len() {
+        return Err(invalid("bundle catalog differs from its inventory"));
+    }
+    for (id, edges) in &mut parents {
+        let missing_input: bool = sqlx::query_scalar(
+            "SELECT EXISTS(SELECT 1 FROM session_history_copies WHERE session_id=?1
+             AND json_extract(request_json,'$.purpose.kind')='revision'
+             AND NOT EXISTS(SELECT 1 FROM session_revision_sources WHERE session_id=?1))",
+        )
+        .bind(id)
+        .fetch_one(&mut *original)
+        .await?;
+        if missing_input {
+            return Err(invalid("bundle Revision lacks its editable source input"));
+        }
+        let parent: Option<String> = sqlx::query_scalar(
+            "SELECT source_session_id FROM session_history_copies WHERE session_id=?",
+        )
+        .bind(id)
+        .fetch_optional(&mut *original)
+        .await?;
+        if let Some(parent) = parent.filter(|p| seen.contains(p)) {
+            edges.insert(parent);
+        }
+    }
+    // Two distinct ancestry edges may coexist. Check both without recursively
+    // following an input-controlled graph, or adopting any parent authority.
+    let mut children: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
+    let mut pending = BTreeMap::new();
+    for (id, edges) in &parents {
+        pending.insert(*id, edges.len());
+        for parent in edges {
+            if !seen.contains(parent) {
+                return Err(invalid("bundle catalog parent is outside its inventory"));
+            }
+            children.entry(parent).or_default().push(id);
+        }
+    }
+    let roots: Vec<_> = pending
+        .iter()
+        .filter(|(_, n)| **n == 0)
+        .map(|(id, _)| *id)
+        .collect();
+    if roots != [inventory.root_session_id.as_str()] {
+        return Err(invalid("bundle catalog is not the selected rooted subtree"));
+    }
+    let mut ready = VecDeque::from(roots);
+    let mut visited = 0;
+    while let Some(id) = ready.pop_front() {
+        visited += 1;
+        for child in children.get(id).into_iter().flatten() {
+            let count = pending.get_mut(child).expect("catalog child");
+            *count -= 1;
+            if *count == 0 {
+                ready.push_back(child);
+            }
+        }
+    }
+    if visited != seen.len() {
+        return Err(invalid("bundle catalog contains an ancestry cycle"));
+    }
+
+    // Reuse the export dependency definition. Closure only visits staged rows,
+    // so matching counts proves no unrelated original facts or copy links remain.
+    let closure = Closure::capture(original, inventory, fence).await?;
+    let actual: (i64, i64, i64, i64) = sqlx::query_as(
+        "SELECT (SELECT COUNT(*) FROM runtime_events),
+                (SELECT COUNT(*) FROM session_history_copies),
+                (SELECT COUNT(*) FROM session_history_members),
+                (SELECT COUNT(*) FROM session_revision_sources)",
+    )
+    .fetch_one(original)
+    .await?;
+    if actual
+        != (
+            closure.events.len() as i64,
+            closure.copies.len() as i64,
+            closure.members.len() as i64,
+            closure.revisions.len() as i64,
+        )
+    {
+        return Err(invalid(
+            "bundle contains history outside its selected closure",
+        ));
+    }
+    Ok(())
+}
diff --git a/crates/event-log/src/bundle/validation/copies.rs b/crates/event-log/src/bundle/validation/copies.rs
index 6231047e8e..3c6e13b6e0 100644
--- a/crates/event-log/src/bundle/validation/copies.rs
+++ b/crates/event-log/src/bundle/validation/copies.rs
@@ -25,9 +25,9 @@ use crate::{
         format::{Copy, Record},
     },
 };
-use maka_runtime::session::{CopyPurpose, Lineage};
+use maka_runtime::session::{CopyPurpose, CopyState, Lineage};
 use sqlx::SqliteConnection;
-use std::collections::VecDeque;
+use std::collections::{BTreeMap, VecDeque};
 
 pub(super) struct Copies(VecDeque<Copy>);
 
@@ -52,6 +52,31 @@ impl Copies {
             copies.push(copy);
             after = number;
         }
+        let indexes: BTreeMap<_, _> = copies
+            .iter()
+            .enumerate()
+            .map(|(index, copy)| (copy.request.target_session_id.as_str(), index))
+            .collect();
+        let mut children = vec![Vec::new(); copies.len()];
+        let mut ready = VecDeque::new();
+        for (index, copy) in copies.iter().enumerate() {
+            if let Some(parent) = indexes.get(copy.request.source_session_id.as_str()) {
+                if copies[*parent].observed_through > copy.observed_through {
+                    return Err(invalid("copy source did not exist at its observed fence"));
+                }
+                children[*parent].push(index);
+            } else {
+                ready.push_back(index);
+            }
+        }
+        let mut visited = 0;
+        while let Some(index) = ready.pop_front() {
+            visited += 1;
+            ready.extend(children[index].iter().copied());
+        }
+        if visited != copies.len() {
+            return Err(invalid("bundle copy ancestry contains a cycle"));
+        }
         copies.sort_by_key(|copy| copy.observed_through);
         Ok(Self(copies.into()))
     }
@@ -146,6 +171,7 @@ fn shape(copy: &Copy, fence: u64) -> Result<(), StoreError> {
         crate::sessions::validate_id(id)?;
     }
     if r.source_session_id == r.target_session_id
+        || copy.state == CopyState::Abandoned
         || r.expected_source_revision == 0
         || r.expected_source_revision > 9_007_199_254_740_991
         || copy.through > copy.observed_through
diff --git a/crates/event-log/src/bundle/validation/material.rs b/crates/event-log/src/bundle/validation/material.rs
index 3439cbfe8b..5e4d125fdd 100644
--- a/crates/event-log/src/bundle/validation/material.rs
+++ b/crates/event-log/src/bundle/validation/material.rs
@@ -153,9 +153,32 @@ pub(super) async fn check_bindings(
          WHERE t.event_session=h.session_id OR (h.archive_sequence IS NOT NULL AND
            (a.kind IS NOT 'tool_result_archived' OR a.sequence<=t.sequence OR
             json_extract(a.event_json,'$.fact.placeholder.identity.runtime_event_id') IS NOT t.event_id)))"
-    ).fetch_one(db).await?;
+    ).fetch_one(&mut *db).await?;
     if invalid_pin {
         return Err(invalid("copy has a self member or mismatched archive pin"));
     }
+    let changed_projection: bool = sqlx::query_scalar(
+        "SELECT EXISTS(SELECT 1 FROM session_history_members h
+         JOIN session_history_copies c ON c.session_id=h.session_id
+         JOIN runtime_events target ON target.sequence=h.sequence
+         LEFT JOIN session_history_members parent ON parent.session_id=c.source_session_id AND parent.sequence=h.sequence
+         WHERE h.archive_sequence IS NOT COALESCE(
+            (SELECT a.sequence FROM runtime_events a WHERE a.kind='tool_result_archived'
+             AND a.event_session=c.source_session_id AND a.sequence<=c.observed_through
+             AND json_extract(a.event_json,'$.fact.placeholder.identity.runtime_event_id')=target.event_id),
+            parent.archive_sequence))"
+    ).fetch_one(&mut *db).await?;
+    if changed_projection {
+        return Err(invalid(
+            "copy archive pin differs from its source projection",
+        ));
+    }
+    let premature: bool = sqlx::query_scalar(
+        "SELECT EXISTS(SELECT 1 FROM session_history_copies c JOIN runtime_events e ON e.event_session=c.session_id
+         WHERE e.sequence<=c.observed_through OR c.state!='committed')"
+    ).fetch_one(db).await?;
+    if premature {
+        return Err(invalid("copy has work before its creation or retention"));
+    }
     Ok(())
 }
diff --git a/crates/event-log/src/tool_payloads.rs b/crates/event-log/src/tool_payloads.rs
index 6d24da75ef..48862c007d 100644
--- a/crates/event-log/src/tool_payloads.rs
+++ b/crates/event-log/src/tool_payloads.rs
@@ -38,6 +38,7 @@ pub(crate) fn verify_binding(event: &RuntimeEvent, bytes: Option<i64>) -> Result
                 ToolOutcome::Succeeded {
                     raw,
                     model_projection,
+                    artifacts,
                 },
             ..
         } => {
@@ -53,6 +54,20 @@ pub(crate) fn verify_binding(event: &RuntimeEvent, bytes: Option<i64>) -> Result
             model_projection
                 .validate(&event.invocation.session_id)
                 .map_err(|_| invalid())?;
+            let mut ids = std::collections::HashSet::new();
+            let references = crate::artifacts::references::from_event(event);
+            for artifact in artifacts {
+                if artifacts.len() > 64 || !ids.insert(&artifact.id)
+                    || maka_runtime::interaction::entity_id(&artifact.id).is_err()
+                    || artifact.bytes > maka_runtime::attachment::MAX_ATTACHMENT_BYTES
+                    || !maka_runtime::archive::valid_projection_digest(&artifact.digest)
+                    || !references.iter().any(|(reference, _)| {
+                        matches!(reference, maka_runtime::attachment::StorageRef::SessionFile { session_id, relative_path }
+                            if session_id == &event.invocation.session_id && relative_path == &artifact.id)
+                    }) {
+                    return Err(invalid());
+                }
+            }
         }
         _ if bytes.is_some() => return Err(invalid()),
         _ => {}
@@ -82,6 +97,19 @@ pub(crate) async fn insert(
     connection: &mut SqliteConnection,
     write: &EventWrite,
 ) -> Result<(), StoreError> {
+    if let Fact::ToolSettled {
+        outcome: ToolOutcome::Succeeded { artifacts, .. },
+        ..
+    } = &write.event().fact
+        && *artifacts
+            != write
+                .projection_artifacts()
+                .iter()
+                .map(|a| a.evidence())
+                .collect::<Vec<_>>()
+    {
+        return Err(invalid());
+    }
     if let Some(payload) = write.raw_payload() {
         sqlx::query("INSERT INTO tool_result_payloads (event_id, payload) VALUES (?, ?)")
             .bind(&write.event().id)
diff --git a/crates/event-log/tests/bundle/export.rs b/crates/event-log/tests/bundle/export.rs
index b9b794e72e..0e2c061e19 100644
--- a/crates/event-log/tests/bundle/export.rs
+++ b/crates/event-log/tests/bundle/export.rs
@@ -30,6 +30,8 @@ use serde_json::{Value, json};
 mod fixtures;
 #[path = "frames.rs"]
 mod frames;
+#[path = "materials.rs"]
+mod materials;
 #[path = "tamper.rs"]
 mod tamper;
 use frames::records;
@@ -287,5 +289,7 @@ async fn branch_export_keeps_archive_proofs_and_owned_bytes_without_parent_futur
         Err(BundleError::Store(StoreError::SessionBusy))
     ));
     end(&log, "busy").await;
+    materials::verify_nested_copy(&log).await;
+    tamper::verify_deleted_upload(&log).await;
     log.close().await.unwrap();
 }
diff --git a/crates/event-log/tests/bundle/fixtures.rs b/crates/event-log/tests/bundle/fixtures.rs
index 8c1bde11dd..51a78266c9 100644
--- a/crates/event-log/tests/bundle/fixtures.rs
+++ b/crates/event-log/tests/bundle/fixtures.rs
@@ -43,19 +43,33 @@ pub(super) async fn tool(
         .await
         .unwrap();
     let event = |fact| EventWrite::plain(RuntimeEvent::new(invocation.clone(), fact)).unwrap();
-    log.append(&event(Fact::ModelRequested {
-        step_id: step.into(),
-        model_id: "test".into(),
-        purpose: ModelPurpose::Main,
-        context: None,
-        source_scope: source.source_evidence.scope,
-        source_high_water: source.source_evidence.high_water,
-        source_digest: source.source_evidence.digest,
-        input_digest: "fixture".into(),
-        route_identity: format!("sha256:{}", "a".repeat(64)),
-        checkpoint_event_id: source.baseline.map(|baseline| baseline.event_id),
-        effective_source_digest: Some(source.effective_source_digest),
-    }))
+    log.append(
+        &event(Fact::ModelRequested {
+            step_id: step.into(),
+            model_id: "test".into(),
+            purpose: ModelPurpose::Main,
+            context: None,
+            source_scope: source.source_evidence.scope,
+            source_high_water: source.source_evidence.high_water,
+            source_digest: source.source_evidence.digest,
+            input_digest: "fixture".into(),
+            route_identity: format!("sha256:{}", "a".repeat(64)),
+            checkpoint_event_id: source.baseline.map(|baseline| baseline.event_id),
+            effective_source_digest: Some(source.effective_source_digest),
+        })
+        .with_quote(maka_runtime::pricing::Quote {
+            provider_id: "fixture".into(),
+            revision: 1,
+            pricing: Some(maka_runtime::pricing::Pricing {
+                model_key: "fixture:test".into(),
+                input_usd_per_million: 1.0,
+                output_usd_per_million: 2.0,
+                cache_read_usd_per_million: None,
+                cache_write_usd_per_million: None,
+            }),
+        })
+        .unwrap(),
+    )
     .await
     .unwrap();
     log.append(&event(Fact::ModelCompleted {
diff --git a/crates/event-log/tests/bundle/frames.rs b/crates/event-log/tests/bundle/frames.rs
index 26a6c278f3..edb9aea7ca 100644
--- a/crates/event-log/tests/bundle/frames.rs
+++ b/crates/event-log/tests/bundle/frames.rs
@@ -65,6 +65,23 @@ pub(super) fn records(bytes: &[u8], expected: &str) -> Vec<Value> {
 
 /// Rewrite selected canonical facts and recompute only the transport checksum.
 pub(super) fn rewrite_events(bytes: &[u8], mut change: impl FnMut(&mut Value) -> bool) -> Vec<u8> {
+    rewrite_records(bytes, |record| {
+        if record["kind"] != "event" {
+            return true;
+        }
+        let mut event: Value = serde_json::from_str(record["json"].as_str().unwrap()).unwrap();
+        let before = event.clone();
+        if !change(&mut event) {
+            return false;
+        }
+        if event != before {
+            record["json"] = Value::String(serde_json::to_string(&event).unwrap());
+        }
+        true
+    })
+}
+
+pub(super) fn rewrite_records(bytes: &[u8], mut change: impl FnMut(&mut Value) -> bool) -> Vec<u8> {
     use std::io::{Cursor, Read};
     let magic = b"MAKA-SESSION\0\x01";
     let mut cursor = Cursor::new(bytes);
@@ -77,19 +94,23 @@ pub(super) fn rewrite_events(bytes: &[u8], mut change: impl FnMut(&mut Value) ->
         let mut json = vec![0; u32::from_be_bytes(length) as usize];
         cursor.read_exact(&mut json).unwrap();
         let mut record: Value = serde_json::from_slice(&json).unwrap();
+        let payload_len = if record["kind"] == "blob" {
+            if record["resource"] == "artifact" {
+                record["metadata"]["sizeBytes"].as_u64().unwrap()
+            } else {
+                record["bytes"].as_u64().unwrap()
+            }
+        } else {
+            0
+        };
+        let start = cursor.position() as usize;
+        cursor.set_position(cursor.position() + payload_len);
         let end = record["kind"] == "end";
         if end {
             record["digest"] = Value::String(format!("sha256:{:x}", Sha256::digest(&output)));
             record["frames"] = Value::from(count);
-        } else if record["kind"] == "event" {
-            let mut event: Value = serde_json::from_str(record["json"].as_str().unwrap()).unwrap();
-            let before = event.clone();
-            if !change(&mut event) {
-                continue;
-            }
-            if event != before {
-                record["json"] = Value::String(serde_json::to_string(&event).unwrap());
-            }
+        } else if !change(&mut record) {
+            continue;
         }
         let json = serde_json::to_vec(&record).unwrap();
         output.extend((json.len() as u32).to_be_bytes());
@@ -98,15 +119,6 @@ pub(super) fn rewrite_events(bytes: &[u8], mut change: impl FnMut(&mut Value) ->
             return output;
         }
         count += 1;
-        if record["kind"] == "blob" {
-            let length = if record["resource"] == "artifact" {
-                record["metadata"]["sizeBytes"].as_u64().unwrap()
-            } else {
-                record["bytes"].as_u64().unwrap()
-            };
-            let start = cursor.position() as usize;
-            output.extend(&bytes[start..start + length as usize]);
-            cursor.set_position(cursor.position() + length);
-        }
+        output.extend(&bytes[start..start + payload_len as usize]);
     }
 }
diff --git a/crates/event-log/tests/bundle/materials.rs b/crates/event-log/tests/bundle/materials.rs
new file mode 100644
index 0000000000..57ce52c16c
--- /dev/null
+++ b/crates/event-log/tests/bundle/materials.rs
@@ -0,0 +1,218 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use maka_event_log::{
+    EventLog, StoreError,
+    bundle::{self, BundleError, StagedBundle},
+    sessions::SessionCopy,
+};
+use maka_runtime::{
+    capability::{CallResult, ContentBlock},
+    event::{EventWrite, Fact, Invocation, InvocationInput, InvocationOutcome, RuntimeEvent},
+    session::CopyPurpose,
+    tool_call::ToolCallIdentity,
+    tool_output::{ToolOutput, ToolSuccess},
+};
+use serde_json::{Value, json};
+use sqlx::Connection;
+
+pub(super) async fn verify_nested_copy(log: &EventLog) {
+    let revision = log
+        .get_session::<Value>("branch")
+        .await
+        .unwrap()
+        .unwrap()
+        .revision;
+    log.copy_session(
+        SessionCopy {
+            source_session_id: "branch".into(),
+            target_session_id: "nested".into(),
+            expected_source_revision: revision,
+            purpose: CopyPurpose::Branch {
+                turn_id: None,
+                side_conversation: false,
+            },
+        },
+        &json!({}),
+        3,
+    )
+    .await
+    .unwrap();
+    let inventory = log.preview_bundle("nested").await.unwrap();
+    let (bytes, report) = log
+        .export_bundle("nested", &inventory.subtree_digest, Vec::new())
+        .await
+        .unwrap();
+    let records = super::frames::records(&bytes, &report.digest);
+    assert_eq!(records.iter().filter(|r| r["kind"] == "copy").count(), 2);
+    assert_eq!(records.iter().filter(|r| r["kind"] == "session").count(), 1);
+    let mut staged = StagedBundle::read(bytes.as_slice()).await.unwrap();
+    staged.validate_history().await.unwrap();
+    staged.close().await.unwrap();
+    let mut changed = false;
+    let unpinned = super::frames::rewrite_records(&bytes, |record| {
+        if record["kind"] == "member"
+            && record["session"] == "nested"
+            && !record["archive_sequence"].is_null()
+        {
+            record["archive_sequence"] = Value::Null;
+            changed = true;
+        }
+        true
+    });
+    assert!(changed);
+    bundle::inspect(unpinned.as_slice()).await.unwrap();
+    let mut staged = StagedBundle::read(unpinned.as_slice()).await.unwrap();
+    let error = staged.validate_history().await;
+    assert!(
+        matches!(error, Err(BundleError::Store(StoreError::InvalidTransition(ref message)))
+        if message == "copy archive pin differs from its source projection"),
+        "{error:?}"
+    );
+    staged.close().await.unwrap();
+    let forged = super::frames::rewrite_records(&bytes, |record| {
+        if record["kind"] == "copy" && record["request"]["targetSessionId"] == "nested" {
+            record["request"]["sourceSessionId"] = json!("unrelated-parent");
+            record["lineage"]["origin"]["parent_session_id"] = json!("unrelated-parent");
+        }
+        true
+    });
+    bundle::inspect(forged.as_slice()).await.unwrap();
+    let mut staged = StagedBundle::read(forged.as_slice()).await.unwrap();
+    let error = staged.validate_history().await;
+    assert!(
+        matches!(error, Err(BundleError::Store(StoreError::InvalidTransition(ref message)))
+        if message == "bundle member is outside its declared source history"
+            || message == "copy archive pin differs from its source projection"),
+        "{error:?}"
+    );
+    staged.close().await.unwrap();
+}
+
+#[tokio::test]
+async fn generated_images_remain_required_material_not_deletable_upload_references() {
+    let dir = tempfile::tempdir().unwrap();
+    let log = EventLog::open(&dir.path().join("events.sqlite"))
+        .await
+        .unwrap();
+    log.create_session("images", "images", &json!({}), 1)
+        .await
+        .unwrap();
+    let invocation = Invocation {
+        session_id: "images".into(),
+        turn_id: "turn".into(),
+        run_id: "run".into(),
+        invocation_id: "invocation".into(),
+    };
+    let event = |fact| EventWrite::plain(RuntimeEvent::new(invocation.clone(), fact)).unwrap();
+    log.append(&event(Fact::InvocationOpened {
+        input: InvocationInput::Code {
+            source: "images".into(),
+        },
+        configuration: None,
+    }))
+    .await
+    .unwrap();
+    let outputs = [
+        ToolSuccess::image(b"\x89PNG\r\n\x1a\n".to_vec(), "image/png".into()).unwrap(),
+        ToolOutput::Mcp(CallResult {
+            content: vec![ContentBlock::Image {
+                data: "iVBORw0KGgo=".into(),
+                mime_type: "image/png".into(),
+            }],
+            structured_content: None,
+        })
+        .into(),
+    ];
+    for (index, output) in outputs.into_iter().enumerate() {
+        let operation = format!("image-{index}");
+        log.append(&event(Fact::ToolDispatched {
+            operation_id: operation.clone(),
+            call: ToolCallIdentity::standalone(operation.clone()),
+            name: "Read".into(),
+            input: json!({}),
+        }))
+        .await
+        .unwrap();
+        let (write, _) = EventWrite::tool_success(
+            format!("result-{index}"),
+            std::time::SystemTime::now(),
+            invocation.clone(),
+            operation,
+            output,
+        )
+        .unwrap();
+        log.append(&write).await.unwrap();
+    }
+    log.append(&event(Fact::InvocationEnded {
+        outcome: InvocationOutcome::Completed,
+    }))
+    .await
+    .unwrap();
+    let inventory = log.preview_bundle("images").await.unwrap();
+    let (bytes, _) = log
+        .export_bundle("images", &inventory.subtree_digest, Vec::new())
+        .await
+        .unwrap();
+    let mut staged = StagedBundle::read(bytes.as_slice()).await.unwrap();
+    staged.validate_history().await.unwrap();
+    staged.close().await.unwrap();
+    for remove in [true, false] {
+        let damaged = super::frames::rewrite_records(&bytes, |record| {
+            if record["kind"] == "blob" && record["resource"] == "artifact" {
+                if remove {
+                    return false;
+                }
+                record["metadata"]["kind"] = json!("file");
+            }
+            true
+        });
+        bundle::inspect(damaged.as_slice()).await.unwrap();
+        let mut staged = StagedBundle::read(damaged.as_slice()).await.unwrap();
+        let error = staged.validate_history().await;
+        let expected = if remove {
+            "bundle lacks generated tool material"
+        } else {
+            "bundle generated tool material differs from its canonical evidence"
+        };
+        assert!(
+            matches!(error, Err(BundleError::Store(StoreError::InvalidTransition(ref message))) if message == expected),
+            "{error:?}"
+        );
+        staged.close().await.unwrap();
+    }
+    let mut corrupt = sqlx::SqliteConnection::connect_with(
+        &sqlx::sqlite::SqliteConnectOptions::new().filename(dir.path().join("events.sqlite")),
+    )
+    .await
+    .unwrap();
+    sqlx::query("DELETE FROM artifacts")
+        .execute(&mut corrupt)
+        .await
+        .unwrap();
+    corrupt.close().await.unwrap();
+    let error = log
+        .export_bundle("images", &inventory.subtree_digest, Vec::new())
+        .await;
+    assert!(
+        matches!(error, Err(BundleError::Store(StoreError::InvalidTransition(ref message))) if message == "bundle lacks generated tool material"),
+        "{error:?}"
+    );
+    log.close().await.unwrap();
+}
diff --git a/crates/event-log/tests/bundle/tamper.rs b/crates/event-log/tests/bundle/tamper.rs
index 740995dbda..636ee9871f 100644
--- a/crates/event-log/tests/bundle/tamper.rs
+++ b/crates/event-log/tests/bundle/tamper.rs
@@ -93,6 +93,91 @@ pub(super) async fn verify_original_proofs(
         json!({"kind":"event","sequence":events.last().unwrap().0+1,"json":serde_json::to_string(&imported).unwrap()}),
     );
     rejected(&mixed, "imported messages cannot admit an invocation").await;
+    imported["invocation"]["session_id"] = json!("unrelated-session");
+    imported["invocation"]["run_id"] = json!("unrelated-run");
+    let unrelated = append_record(
+        bytes,
+        json!({"kind":"event","sequence":events.last().unwrap().0+1,"json":serde_json::to_string(&imported).unwrap()}),
+    );
+    rejected(
+        &unrelated,
+        "bundle contains history outside its selected closure",
+    )
+    .await;
+    for (kind, field, value, expected) in [
+        (
+            "session",
+            "id",
+            json!("not-selected"),
+            "bundle catalog differs from its inventory",
+        ),
+        (
+            "session",
+            "parent",
+            json!("branch"),
+            "bundle catalog is not the selected rooted subtree",
+        ),
+        (
+            "accounting",
+            "event_id",
+            json!("not-a-request"),
+            "unbound bundle model quote",
+        ),
+        (
+            "accounting",
+            "valuation",
+            json!({"usage":{"input_tokens":1},"usd":0.0}),
+            "bundle valuation differs from its selected usage evidence",
+        ),
+    ] {
+        let mut changed = false;
+        let tampered = super::frames::rewrite_records(bytes, |record| {
+            if !changed && record["kind"] == kind {
+                record[field] = value.clone();
+                changed = true;
+            }
+            true
+        });
+        assert!(changed, "missing {kind} fixture");
+        rejected(&tampered, expected).await;
+    }
+    let omitted = super::frames::rewrite_records(bytes, |record| record["kind"] != "session");
+    rejected(&omitted, "bundle catalog differs from its inventory").await;
+    for (omit, expected) in [
+        (
+            "history_artifact",
+            "bundle copied reference lacks its retained Artifact",
+        ),
+        (
+            "artifact",
+            "bundle history mapping lacks its owned Artifact",
+        ),
+    ] {
+        let omitted = super::frames::rewrite_records(bytes, |record| {
+            !(record["kind"] == omit || (record["kind"] == "blob" && record["resource"] == omit))
+        });
+        rejected(&omitted, expected).await;
+    }
+    for (field, value, expected) in [
+        (
+            "sessionId",
+            "not-selected",
+            "bundle Artifact is outside its selected catalog",
+        ),
+        (
+            "name",
+            "wrong.txt",
+            "Attachment metadata does not match its canonical Artifact",
+        ),
+    ] {
+        let tampered = super::frames::rewrite_records(bytes, |record| {
+            if record["kind"] == "blob" && record["resource"] == "artifact" {
+                record["metadata"][field] = json!(value);
+            }
+            true
+        });
+        rejected(&tampered, expected).await;
+    }
     // Reject attacker-declared allocation before waiting for the absent body.
     let mut oversized = b"MAKA-SESSION\0\x01".to_vec();
     oversized.extend(u32::MAX.to_be_bytes());
@@ -102,6 +187,24 @@ pub(super) async fn verify_original_proofs(
     ));
 }
 
+pub(super) async fn verify_deleted_upload(log: &maka_event_log::EventLog) {
+    assert_eq!(
+        log.delete_user_artifact("source", "input").await.unwrap(),
+        maka_event_log::artifacts::ArtifactDeletion::Deleted
+    );
+    let inventory = log.preview_bundle("source").await.unwrap();
+    let (bytes, _) = log
+        .export_bundle("source", &inventory.subtree_digest, Vec::new())
+        .await
+        .unwrap();
+    let mut staged = StagedBundle::read(bytes.as_slice()).await.unwrap();
+    // The branch still owns its retained copy; the original reference now
+    // reports a missing upload without making the entire bundle invalid.
+    staged.validate_history().await.unwrap();
+    staged.validate_history().await.unwrap();
+    staged.close().await.unwrap();
+}
+
 async fn rejected(bytes: &[u8], expected: &str) {
     bundle::inspect(bytes).await.unwrap();
     let mut staged = StagedBundle::read(bytes).await.unwrap();
diff --git a/crates/runtime-host/tests/integration/large_outputs.rs b/crates/runtime-host/tests/integration/large_outputs.rs
index b08f297476..c5e342c136 100644
--- a/crates/runtime-host/tests/integration/large_outputs.rs
+++ b/crates/runtime-host/tests/integration/large_outputs.rs
@@ -77,6 +77,7 @@ async fn large_attachment_results_preserve_raw_without_poisoning_model_history()
                     ToolOutcome::Succeeded {
                         raw,
                         model_projection,
+                        ..
                     },
                 ..
             } = &stored.event.fact
diff --git a/crates/runtime/src/artifact.rs b/crates/runtime/src/artifact.rs
index bb4def852a..9abe984073 100644
--- a/crates/runtime/src/artifact.rs
+++ b/crates/runtime/src/artifact.rs
@@ -20,6 +20,15 @@
 use serde::{Deserialize, Serialize};
 use sha2::{Digest, Sha256};
 
+/// Material committed atomically with a canonical fact, not a best-effort file reference.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
+pub struct ArtifactEvidence {
+    pub id: String,
+    pub bytes: u64,
+    pub digest: String,
+}
+
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
 #[serde(rename_all = "snake_case")]
 pub enum ArtifactKind {
diff --git a/crates/runtime/src/event.rs b/crates/runtime/src/event.rs
index b7630cfcdc..0403d9d49c 100644
--- a/crates/runtime/src/event.rs
+++ b/crates/runtime/src/event.rs
@@ -78,6 +78,10 @@ pub enum ToolOutcome {
     Succeeded {
         raw: crate::tool_output::RawToolResultRef,
         model_projection: crate::tool_output::DurableToolProjection,
+        /// Generated material must survive with this fact; ordinary references
+        /// may instead point to user-deletable uploads.
+        #[serde(default, skip_serializing_if = "Vec::is_empty")]
+        artifacts: Vec<crate::artifact::ArtifactEvidence>,
     },
     Failed {
         message: String,
diff --git a/crates/runtime/src/event_write.rs b/crates/runtime/src/event_write.rs
index 9d826b75a9..db7c1380e8 100644
--- a/crates/runtime/src/event_write.rs
+++ b/crates/runtime/src/event_write.rs
@@ -51,6 +51,13 @@ impl ProjectionArtifactWrite {
     pub fn bytes(&self) -> &[u8] {
         &self.bytes
     }
+    pub fn evidence(&self) -> crate::artifact::ArtifactEvidence {
+        crate::artifact::ArtifactEvidence {
+            id: self.artifact.id.clone(),
+            bytes: self.bytes.len() as u64,
+            digest: content_digest(&self.bytes),
+        }
+    }
 }
 
 impl EventWrite {
@@ -212,6 +219,11 @@ impl EventWrite {
                         outcome: ToolOutcome::Succeeded {
                             raw,
                             model_projection: success.projection,
+                            artifacts: success
+                                .artifacts
+                                .iter()
+                                .map(ProjectionArtifactWrite::evidence)
+                                .collect(),
                         },
                     },
                 },
diff --git a/crates/runtime/tests/archive.rs b/crates/runtime/tests/archive.rs
index 855ce1973f..bb128e49c1 100644
--- a/crates/runtime/tests/archive.rs
+++ b/crates/runtime/tests/archive.rs
@@ -154,6 +154,7 @@ fn execution_failure_and_projection_failure_do_not_reveal_raw_or_collapse_catego
             digest: content_digest(b"unavailable raw"),
         },
         model_projection: Projection::Failure,
+        artifacts: Vec::new(),
     };
     assert_eq!(
         encode_projection(&outcome_projection(&succeeded)).unwrap(),
diff --git a/crates/runtime/tests/tool_image_success.rs b/crates/runtime/tests/tool_image_success.rs
index 1cdf3695a8..85bfd54da3 100644
--- a/crates/runtime/tests/tool_image_success.rs
+++ b/crates/runtime/tests/tool_image_success.rs
@@ -96,12 +96,13 @@ async fn pending_image_is_normalized_once_and_delivered_only_after_commit_ack()
         );
         assert_eq!(artifact.artifact().session_id, "session");
         let Fact::ToolSettled {
-            outcome: ToolOutcome::Succeeded { raw, .. },
+            outcome: ToolOutcome::Succeeded { raw, artifacts, .. },
             ..
         } = &write.event().fact
         else {
             panic!()
         };
+        assert_eq!(artifacts, &[artifact.evidence()]);
         let output = decode_raw_tool_result(write.raw_payload().unwrap(), raw).unwrap();
         assert!(matches!(&output, ToolOutput::Image(image) if image.mime_type == "image/gif"));
         let delivered = output.into_json();


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org