1use std::path::{Path, PathBuf};
13
14use serde::{Deserialize, Serialize};
15use sha2::{Digest, Sha256};
16
17use super::receipt::{SessionReceipt, RECEIPT_TYPE};
18use crate::statements::{
19 ApprovalRevocation, ApprovalUse, JournalCheckpoint,
20 ReplayCheck, ReplayCheckLevel,
21 approval_revocation_record_digest, approval_use_record_digest,
22 journal_checkpoint_record_digest,
23};
24
25#[derive(Debug)]
27pub enum PackageError {
28 Io(std::io::Error),
29 Json(serde_json::Error),
30 InvalidPackage(String),
31}
32
33impl std::fmt::Display for PackageError {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 match self {
36 Self::Io(e) => write!(f, "package io: {e}"),
37 Self::Json(e) => write!(f, "package json: {e}"),
38 Self::InvalidPackage(msg) => write!(f, "invalid package: {msg}"),
39 }
40 }
41}
42
43impl std::error::Error for PackageError {}
44impl From<std::io::Error> for PackageError {
45 fn from(e: std::io::Error) -> Self { Self::Io(e) }
46}
47impl From<serde_json::Error> for PackageError {
48 fn from(e: serde_json::Error) -> Self { Self::Json(e) }
49}
50
51const RECEIPT_FILE: &str = "receipt.json";
53const MERKLE_FILE: &str = "merkle.json";
54const RENDER_FILE: &str = "render.json";
55const ARTIFACTS_DIR: &str = "artifacts";
56const PROOFS_DIR: &str = "proofs";
57const PREVIEW_FILE: &str = "preview.html";
58
59const APPROVALS_DIR: &str = "approvals";
72const APPROVALS_GRANTS: &str = "approvals/grants";
73const APPROVALS_USES: &str = "approvals/uses";
74const APPROVALS_CHECKPOINTS:&str = "approvals/checkpoints";
75const APPROVALS_INDEX_FILE: &str = "approvals/index.json";
76
77#[derive(Debug, Clone, Default)]
87pub struct ApprovalsBundle {
88 pub grants: Vec<(String, Vec<u8>)>,
93 pub uses: Vec<ApprovalUse>,
97 pub checkpoints: Vec<JournalCheckpoint>,
100 pub revocations: Vec<ApprovalRevocation>,
105
106 pub action_envelopes: Vec<(String, Vec<u8>)>,
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct ApprovalsIndex {
121 #[serde(rename = "type")]
123 pub type_: String,
124 pub schema_version: u32,
125 pub grants: Vec<String>,
128 pub uses: Vec<String>,
130 pub checkpoints: Vec<String>,
131 pub revocations: Vec<String>,
132}
133
134impl ApprovalsIndex {
135 pub fn type_string() -> &'static str { "treeship/approvals-index/v1" }
136}
137
138pub struct PackageOutput {
140 pub path: PathBuf,
142 pub receipt_digest: String,
144 pub merkle_root: Option<String>,
146 pub file_count: usize,
148}
149
150pub fn build_package(
160 receipt: &SessionReceipt,
161 output_dir: &Path,
162) -> Result<PackageOutput, PackageError> {
163 build_package_with_approvals(receipt, output_dir, None)
164}
165
166pub fn build_package_with_approvals(
170 receipt: &SessionReceipt,
171 output_dir: &Path,
172 bundle: Option<&ApprovalsBundle>,
173) -> Result<PackageOutput, PackageError> {
174 let session_id = &receipt.session.id;
175 let pkg_dir = output_dir.join(format!("{session_id}.treeship"));
176
177 std::fs::create_dir_all(&pkg_dir)?;
178 std::fs::create_dir_all(pkg_dir.join(ARTIFACTS_DIR))?;
179 std::fs::create_dir_all(pkg_dir.join(PROOFS_DIR))?;
180
181 let mut file_count = 0usize;
182
183 let receipt_bytes = serde_json::to_vec_pretty(receipt)?;
185 std::fs::write(pkg_dir.join(RECEIPT_FILE), &receipt_bytes)?;
186 file_count += 1;
187
188 let receipt_hash = Sha256::digest(&receipt_bytes);
189 let receipt_digest = format!("sha256:{}", hex::encode(receipt_hash));
190
191 let merkle_bytes = serde_json::to_vec_pretty(&receipt.merkle)?;
193 std::fs::write(pkg_dir.join(MERKLE_FILE), &merkle_bytes)?;
194 file_count += 1;
195
196 let render_bytes = serde_json::to_vec_pretty(&receipt.render)?;
198 std::fs::write(pkg_dir.join(RENDER_FILE), &render_bytes)?;
199 file_count += 1;
200
201 for proof_entry in &receipt.merkle.inclusion_proofs {
203 let proof_bytes = serde_json::to_vec_pretty(proof_entry)?;
204 let filename = format!("{}.proof.json", proof_entry.artifact_id);
205 std::fs::write(pkg_dir.join(PROOFS_DIR).join(filename), &proof_bytes)?;
206 file_count += 1;
207 }
208
209 if receipt.render.generate_preview {
211 let preview = generate_preview_html(receipt);
212 std::fs::write(pkg_dir.join(PREVIEW_FILE), preview.as_bytes())?;
213 file_count += 1;
214 }
215
216 if let Some(b) = bundle {
221 if !b.grants.is_empty() || !b.uses.is_empty() || !b.checkpoints.is_empty() || !b.revocations.is_empty() || !b.action_envelopes.is_empty() {
222 std::fs::create_dir_all(pkg_dir.join(APPROVALS_GRANTS))?;
223 std::fs::create_dir_all(pkg_dir.join(APPROVALS_USES))?;
224 std::fs::create_dir_all(pkg_dir.join(APPROVALS_CHECKPOINTS))?;
225 std::fs::create_dir_all(pkg_dir.join(ARTIFACTS_DIR))?;
231 for (artifact_id, envelope_bytes) in &b.action_envelopes {
232 let safe = sanitize_filename(artifact_id);
233 std::fs::write(
234 pkg_dir.join(ARTIFACTS_DIR).join(format!("{safe}.json")),
235 envelope_bytes,
236 )?;
237 file_count += 1;
238 }
239
240 let mut grant_ids = Vec::with_capacity(b.grants.len());
241 for (grant_id, envelope_bytes) in &b.grants {
242 let safe = sanitize_filename(grant_id);
243 std::fs::write(
244 pkg_dir.join(APPROVALS_GRANTS).join(format!("{safe}.json")),
245 envelope_bytes,
246 )?;
247 grant_ids.push(grant_id.clone());
248 file_count += 1;
249 }
250
251 let mut use_ids = Vec::with_capacity(b.uses.len());
252 for u in &b.uses {
253 let safe = sanitize_filename(&u.use_id);
254 let bytes = serde_json::to_vec_pretty(u)?;
255 std::fs::write(
256 pkg_dir.join(APPROVALS_USES).join(format!("{safe}.json")),
257 &bytes,
258 )?;
259 use_ids.push(u.use_id.clone());
260 file_count += 1;
261 }
262
263 let mut checkpoint_ids = Vec::with_capacity(b.checkpoints.len());
264 for cp in &b.checkpoints {
265 let safe = sanitize_filename(&cp.checkpoint_id);
266 let bytes = serde_json::to_vec_pretty(cp)?;
267 std::fs::write(
268 pkg_dir.join(APPROVALS_CHECKPOINTS).join(format!("{safe}.json")),
269 &bytes,
270 )?;
271 checkpoint_ids.push(cp.checkpoint_id.clone());
272 file_count += 1;
273 }
274
275 let mut revocation_ids = Vec::with_capacity(b.revocations.len());
276 for rev in &b.revocations {
277 let safe = sanitize_filename(&rev.revocation_id);
278 let bytes = serde_json::to_vec_pretty(rev)?;
279 std::fs::write(
280 pkg_dir.join(APPROVALS_DIR).join(format!("revocations-{safe}.json")),
281 &bytes,
282 )?;
283 revocation_ids.push(rev.revocation_id.clone());
284 file_count += 1;
285 }
286
287 let index = ApprovalsIndex {
288 type_: ApprovalsIndex::type_string().into(),
289 schema_version: 1,
290 grants: grant_ids,
291 uses: use_ids,
292 checkpoints: checkpoint_ids,
293 revocations: revocation_ids,
294 };
295 let index_bytes = serde_json::to_vec_pretty(&index)?;
296 std::fs::write(pkg_dir.join(APPROVALS_INDEX_FILE), &index_bytes)?;
297 file_count += 1;
298 }
299 }
300
301 Ok(PackageOutput {
302 path: pkg_dir,
303 receipt_digest,
304 merkle_root: receipt.merkle.root.clone(),
305 file_count,
306 })
307}
308
309fn sanitize_filename(s: &str) -> String {
313 s.chars()
314 .map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '.' || c == '_' { c } else { '_' })
315 .collect()
316}
317
318pub fn read_approvals_bundle(pkg_dir: &Path) -> Result<ApprovalsBundle, PackageError> {
328 let approvals_dir = pkg_dir.join(APPROVALS_DIR);
329 if !approvals_dir.is_dir() {
330 return Ok(ApprovalsBundle::default());
331 }
332
333 let mut bundle = ApprovalsBundle::default();
334
335 let grants_dir = pkg_dir.join(APPROVALS_GRANTS);
338 if grants_dir.is_dir() {
339 for entry in std::fs::read_dir(&grants_dir)? {
340 let entry = entry?;
341 let path = entry.path();
342 if path.extension().and_then(|s| s.to_str()) != Some("json") { continue; }
343 let id = path.file_stem().and_then(|s| s.to_str()).unwrap_or("").to_string();
344 let bytes = std::fs::read(&path)?;
345 bundle.grants.push((id, bytes));
346 }
347 }
348
349 let uses_dir = pkg_dir.join(APPROVALS_USES);
350 if uses_dir.is_dir() {
351 for entry in std::fs::read_dir(&uses_dir)? {
352 let entry = entry?;
353 let path = entry.path();
354 if path.extension().and_then(|s| s.to_str()) != Some("json") { continue; }
355 let bytes = std::fs::read(&path)?;
356 let u: ApprovalUse = serde_json::from_slice(&bytes)?;
357 bundle.uses.push(u);
358 }
359 }
360
361 let cps_dir = pkg_dir.join(APPROVALS_CHECKPOINTS);
362 if cps_dir.is_dir() {
363 for entry in std::fs::read_dir(&cps_dir)? {
364 let entry = entry?;
365 let path = entry.path();
366 if path.extension().and_then(|s| s.to_str()) != Some("json") { continue; }
367 let bytes = std::fs::read(&path)?;
368 let cp: JournalCheckpoint = serde_json::from_slice(&bytes)?;
369 bundle.checkpoints.push(cp);
370 }
371 }
372
373 let arts_dir = pkg_dir.join(ARTIFACTS_DIR);
380 if arts_dir.is_dir() {
381 for entry in std::fs::read_dir(&arts_dir)? {
382 let entry = entry?;
383 let path = entry.path();
384 if path.extension().and_then(|s| s.to_str()) != Some("json") { continue; }
385 let id = path.file_stem().and_then(|s| s.to_str()).unwrap_or("").to_string();
386 let bytes = std::fs::read(&path)?;
387 bundle.action_envelopes.push((id, bytes));
388 }
389 }
390
391 Ok(bundle)
392}
393
394pub fn read_package(pkg_dir: &Path) -> Result<SessionReceipt, PackageError> {
396 let receipt_path = pkg_dir.join(RECEIPT_FILE);
397 if !receipt_path.exists() {
398 return Err(PackageError::InvalidPackage(
399 format!("missing {RECEIPT_FILE} in {}", pkg_dir.display()),
400 ));
401 }
402 let bytes = std::fs::read(&receipt_path)?;
403 let receipt: SessionReceipt = serde_json::from_slice(&bytes)?;
404
405 if receipt.type_ != RECEIPT_TYPE {
406 return Err(PackageError::InvalidPackage(
407 format!("unexpected type: {} (expected {RECEIPT_TYPE})", receipt.type_),
408 ));
409 }
410
411 Ok(receipt)
412}
413
414pub fn verify_package(pkg_dir: &Path) -> Result<Vec<VerifyCheck>, PackageError> {
418 let mut checks = Vec::new();
419
420 let receipt = match read_package(pkg_dir) {
422 Ok(r) => {
423 checks.push(VerifyCheck::pass("receipt.json", "Parses as valid Session Receipt"));
424 r
425 }
426 Err(e) => {
427 checks.push(VerifyCheck::fail("receipt.json", &format!("Failed to parse: {e}")));
428 return Ok(checks);
429 }
430 };
431
432 if receipt.type_ == RECEIPT_TYPE {
434 checks.push(VerifyCheck::pass("type", "Correct receipt type"));
435 } else {
436 checks.push(VerifyCheck::fail("type", &format!("Expected {RECEIPT_TYPE}, got {}", receipt.type_)));
437 }
438
439 let receipt_path = pkg_dir.join(RECEIPT_FILE);
441 let on_disk = std::fs::read(&receipt_path)?;
442 let re_serialized = serde_json::to_vec_pretty(&receipt)?;
443 if on_disk == re_serialized {
444 checks.push(VerifyCheck::pass("determinism", "receipt.json round-trips identically"));
445 } else {
446 checks.push(VerifyCheck::warn("determinism", "receipt.json does not byte-match after re-serialization"));
448 }
449
450 if !receipt.artifacts.is_empty() {
452 let mut tree = crate::merkle::MerkleTree::new();
453 for art in &receipt.artifacts {
454 tree.append(&art.artifact_id);
455 }
456 let root_bytes = tree.root();
457 let recomputed_root = root_bytes
458 .map(|r| format!("mroot_{}", hex::encode(r)));
459 let root_hex = root_bytes
460 .map(|r| hex::encode(r))
461 .unwrap_or_default();
462
463 if recomputed_root == receipt.merkle.root {
464 checks.push(VerifyCheck::pass("merkle_root", "Merkle root matches recomputed value"));
465 } else {
466 checks.push(VerifyCheck::fail(
467 "merkle_root",
468 &format!(
469 "Mismatch: on-disk {:?} vs recomputed {:?}",
470 receipt.merkle.root, recomputed_root
471 ),
472 ));
473 }
474
475 for proof_entry in &receipt.merkle.inclusion_proofs {
477 let verified = crate::merkle::MerkleTree::verify_proof(
478 &root_hex,
479 &proof_entry.artifact_id,
480 &proof_entry.proof,
481 );
482 if verified {
483 checks.push(VerifyCheck::pass(
484 &format!("inclusion:{}", proof_entry.artifact_id),
485 "Inclusion proof valid",
486 ));
487 } else {
488 checks.push(VerifyCheck::fail(
489 &format!("inclusion:{}", proof_entry.artifact_id),
490 "Inclusion proof failed verification",
491 ));
492 }
493 }
494 } else {
495 checks.push(VerifyCheck::warn("merkle_root", "No artifacts to verify"));
496 }
497
498 if receipt.merkle.leaf_count == receipt.artifacts.len() {
500 checks.push(VerifyCheck::pass("leaf_count", "Leaf count matches artifact count"));
501 } else {
502 checks.push(VerifyCheck::fail(
503 "leaf_count",
504 &format!("leaf_count {} != artifact count {}", receipt.merkle.leaf_count, receipt.artifacts.len()),
505 ));
506 }
507
508 let ordered = receipt.timeline.windows(2).all(|w| {
510 (&w[0].timestamp, w[0].sequence_no, &w[0].event_id)
511 <= (&w[1].timestamp, w[1].sequence_no, &w[1].event_id)
512 });
513 if ordered {
514 checks.push(VerifyCheck::pass("timeline_order", "Timeline is correctly ordered"));
515 } else {
516 checks.push(VerifyCheck::fail("timeline_order", "Timeline entries are not in deterministic order"));
517 }
518
519 if receipt.proofs.event_log_skipped > 0 {
527 checks.push(VerifyCheck::warn(
528 "event_log_completeness",
529 &format!(
530 "{} event(s) skipped during close (malformed lines in events.jsonl). \
531 Receipt is cryptographically valid but does not represent the full event stream. \
532 Inspect close-time stderr or the events.jsonl directly to investigate.",
533 receipt.proofs.event_log_skipped,
534 ),
535 ));
536 }
537
538 let bundle = read_approvals_bundle(pkg_dir).unwrap_or_default();
550 add_approval_evidence_checks(&mut checks, &bundle);
551
552 Ok(checks)
553}
554
555pub(crate) fn add_approval_evidence_checks(
566 checks: &mut Vec<VerifyCheck>,
567 bundle: &ApprovalsBundle,
568) {
569 if bundle.uses.is_empty() && bundle.checkpoints.is_empty() {
570 return;
574 }
575
576 use std::collections::HashMap;
585 let mut by_nonce: HashMap<(String, String), Vec<&ApprovalUse>> = HashMap::new();
586 let mut by_use_id: HashMap<&str, Vec<&ApprovalUse>> = HashMap::new();
587 for u in &bundle.uses {
588 by_nonce
589 .entry((u.grant_id.clone(), u.nonce_digest.clone()))
590 .or_default()
591 .push(u);
592 by_use_id.entry(&u.use_id).or_default().push(u);
593 }
594 let over_max: Vec<((String, String), Vec<&ApprovalUse>, u32)> = by_nonce
595 .iter()
596 .filter_map(|(key, uses)| {
597 let max = uses.iter().filter_map(|u| u.max_uses).next()?;
598 if (uses.len() as u32) > max {
599 Some((key.clone(), uses.iter().map(|u| *u).collect(), max))
600 } else {
601 None
602 }
603 })
604 .collect();
605 let dup_use_ids: Vec<(&&str, &Vec<&ApprovalUse>)> =
606 by_use_id.iter().filter(|(_, v)| v.len() > 1).collect();
607
608 if over_max.is_empty() && dup_use_ids.is_empty() {
609 checks.push(VerifyCheck::pass(
610 "replay-package-local",
611 &format!("no duplicate approval use inside package ({} uses scanned)", bundle.uses.len()),
612 ));
613 } else {
614 let mut detail = String::from("package-local replay violation:");
615 for ((grant_id, _nd), uses, max) in &over_max {
616 detail.push_str(&format!(
617 " grant {grant_id} consumed {} times in this package (max_uses={max});",
618 uses.len(),
619 ));
620 }
621 for (uid, uses) in &dup_use_ids {
622 detail.push_str(&format!(" use_id {uid} appears {} times;", uses.len()));
623 }
624 checks.push(VerifyCheck::fail("replay-package-local", &detail));
625 }
626
627 if !bundle.checkpoints.is_empty() {
632 let mut tampered = Vec::new();
633 for cp in &bundle.checkpoints {
634 let recomputed = journal_checkpoint_record_digest(cp);
635 if recomputed != cp.record_digest {
636 tampered.push((cp.checkpoint_id.clone(), cp.record_digest.clone(), recomputed));
637 }
638 }
639 if tampered.is_empty() {
640 checks.push(VerifyCheck::pass(
641 "replay-included-checkpoint",
642 &format!("{} included journal checkpoint(s) verify offline", bundle.checkpoints.len()),
643 ));
644 } else {
645 let detail = tampered.iter()
646 .map(|(id, expected, actual)| {
647 format!("checkpoint {id} tampered (stored {expected}, recomputed {actual})")
648 })
649 .collect::<Vec<_>>()
650 .join("; ");
651 checks.push(VerifyCheck::fail("replay-included-checkpoint", &detail));
652 }
653 }
654
655 let mut tampered_uses = Vec::new();
665 for u in &bundle.uses {
666 let recomputed = approval_use_record_digest(u);
667 if recomputed != u.record_digest {
668 tampered_uses.push((u.use_id.clone(), u.record_digest.clone(), recomputed));
669 }
670 }
671 if !bundle.uses.is_empty() {
672 if tampered_uses.is_empty() {
673 checks.push(VerifyCheck::pass(
674 "approval-use-record-digest",
675 &format!("{} use record(s) recompute identically", bundle.uses.len()),
676 ));
677 } else {
678 let detail = tampered_uses.iter()
679 .map(|(id, expected, actual)| {
680 format!("use {id} tampered (stored {expected}, recomputed {actual})")
681 })
682 .collect::<Vec<_>>()
683 .join("; ");
684 checks.push(VerifyCheck::fail("approval-use-record-digest", &detail));
685 }
686 }
687
688 if !bundle.uses.is_empty() {
706 use crate::attestation::envelope::Envelope;
707 use crate::attestation::{pae, artifact_id_from_pae};
708 use crate::statements::{nonce_digest, ApprovalStatement};
709 let mut grant_nonce_digest: std::collections::HashMap<String, String> = std::collections::HashMap::new();
710 let mut tampered_grants: Vec<String> = Vec::new();
711 for (grant_id, env_bytes) in &bundle.grants {
712 let env = match Envelope::from_json(env_bytes) {
713 Ok(e) => e,
714 Err(_) => {
715 tampered_grants.push(format!("grant {grant_id} envelope unparseable"));
716 continue;
717 }
718 };
719 let derived = match env.payload_bytes() {
724 Ok(p) => artifact_id_from_pae(&pae(&env.payload_type, &p)),
725 Err(_) => {
726 tampered_grants.push(format!("grant {grant_id} envelope payload undecodable"));
727 continue;
728 }
729 };
730 if &derived != grant_id {
731 tampered_grants.push(format!(
732 "grant {grant_id} envelope content derives to {derived} -- envelope substituted or tampered",
733 ));
734 continue;
735 }
736 let approval: ApprovalStatement = match env.unmarshal_statement() {
737 Ok(a) => a,
738 Err(_) => {
739 tampered_grants.push(format!("grant {grant_id} payload not an ApprovalStatement"));
740 continue;
741 }
742 };
743 grant_nonce_digest.insert(grant_id.clone(), nonce_digest(&approval.nonce));
744 }
745 let mut mismatches: Vec<String> = Vec::new();
746 let mut missing_grants: Vec<String> = Vec::new();
747 for u in &bundle.uses {
748 match grant_nonce_digest.get(&u.grant_id) {
749 Some(expected) => {
750 if expected != &u.nonce_digest {
751 mismatches.push(format!(
752 "use {} claims nonce_digest {} but grant {} signed nonce hashes to {}",
753 u.use_id, u.nonce_digest, u.grant_id, expected,
754 ));
755 }
756 }
757 None => {
758 missing_grants.push(format!(
759 "use {} references grant {} but no usable grant envelope is in the package",
760 u.use_id, u.grant_id,
761 ));
762 }
763 }
764 }
765 if mismatches.is_empty() && missing_grants.is_empty() && tampered_grants.is_empty() {
766 checks.push(VerifyCheck::pass(
767 "approval-use-nonce-binding",
768 &format!(
769 "{} use record(s) bind to content-addressed grant signed nonces",
770 bundle.uses.len(),
771 ),
772 ));
773 } else {
774 let mut parts: Vec<String> = Vec::new();
775 if !tampered_grants.is_empty() { parts.push(tampered_grants.join("; ")); }
776 if !mismatches.is_empty() { parts.push(mismatches.join("; ")); }
777 if !missing_grants.is_empty() { parts.push(missing_grants.join("; ")); }
778 checks.push(VerifyCheck::fail("approval-use-nonce-binding", &parts.join("; ")));
779 }
780 }
781
782 if !bundle.uses.is_empty() {
797 use crate::attestation::envelope::Envelope;
798 use crate::attestation::{pae, artifact_id_from_pae};
799 use crate::statements::{nonce_digest, ActionStatement};
800 if bundle.action_envelopes.is_empty() {
801 checks.push(VerifyCheck::warn(
802 "approval-use-action-binding",
803 "no action envelopes embedded -- action↔use binding not asserted by package (pre-v0.9.10)",
804 ));
805 } else {
806 let use_ids: std::collections::HashSet<&str> = bundle.uses.iter().map(|u| u.use_id.as_str()).collect();
807 let mut violations: Vec<String> = Vec::new();
808 let mut bound_count = 0usize;
809 for (artifact_id, env_bytes) in &bundle.action_envelopes {
810 let env = match Envelope::from_json(env_bytes) {
811 Ok(e) => e,
812 Err(_) => {
813 violations.push(format!("action {artifact_id} envelope unparseable"));
814 continue;
815 }
816 };
817 let derived = match env.payload_bytes() {
825 Ok(p) => artifact_id_from_pae(&pae(&env.payload_type, &p)),
826 Err(_) => {
827 violations.push(format!("action {artifact_id} envelope payload undecodable"));
828 continue;
829 }
830 };
831 if &derived != artifact_id {
832 violations.push(format!(
833 "action {artifact_id} envelope content derives to {derived} -- envelope substituted or tampered",
834 ));
835 continue;
836 }
837 let action: ActionStatement = match env.unmarshal_statement() {
838 Ok(a) => a,
839 Err(_) => {
840 violations.push(format!("action {artifact_id} not an ActionStatement"));
841 continue;
842 }
843 };
844 let raw_nonce = match action.approval_nonce.as_deref() {
845 Some(n) => n,
846 None => continue,
847 };
848 let claimed_use_id = action
849 .meta
850 .as_ref()
851 .and_then(|m| m.get("approval_use_id"))
852 .and_then(|v| v.as_str());
853 let Some(claimed_use_id) = claimed_use_id else {
854 violations.push(format!(
855 "action {artifact_id} consumed an approval but its meta has no approval_use_id"
856 ));
857 continue;
858 };
859 if !use_ids.contains(claimed_use_id) {
860 violations.push(format!(
861 "action {artifact_id} claims approval_use_id={} but no such use is embedded",
862 claimed_use_id,
863 ));
864 continue;
865 }
866 let expected = nonce_digest(raw_nonce);
867 let matched_use = bundle.uses.iter().find(|u| u.use_id == claimed_use_id);
868 if let Some(u) = matched_use {
869 if u.nonce_digest != expected {
870 violations.push(format!(
871 "action {artifact_id} approval_nonce hashes to {} but use {} stores nonce_digest {}",
872 expected, claimed_use_id, u.nonce_digest,
873 ));
874 continue;
875 }
876 }
877 bound_count += 1;
878 }
879 if violations.is_empty() {
880 checks.push(VerifyCheck::pass(
881 "approval-use-action-binding",
882 &format!(
883 "{bound_count} consuming action(s) bind cleanly to content-addressed envelope(s)",
884 ),
885 ));
886 } else {
887 checks.push(VerifyCheck::fail(
888 "approval-use-action-binding",
889 &violations.join("; "),
890 ));
891 }
892 }
893 }
894
895 if !bundle.uses.is_empty() || !bundle.checkpoints.is_empty() {
922 use std::collections::{HashMap, HashSet};
923 struct Node<'a> { label: String, digest: &'a str, prev: &'a str }
926 let mut nodes: Vec<Node> = Vec::new();
927 for u in &bundle.uses {
928 nodes.push(Node {
929 label: format!("use {}", u.use_id),
930 digest: u.record_digest.as_str(),
931 prev: u.previous_record_digest.as_str(),
932 });
933 }
934 for cp in &bundle.checkpoints {
935 nodes.push(Node {
936 label: format!("checkpoint {}", cp.checkpoint_id),
937 digest: cp.record_digest.as_str(),
938 prev: cp.previous_record_digest.as_str(),
939 });
940 }
941
942 let owned: HashSet<&str> = std::iter::once("")
943 .chain(nodes.iter().map(|n| n.digest))
944 .collect();
945
946 let mut violations: Vec<String> = Vec::new();
947 for n in &nodes {
949 if !owned.contains(n.prev) {
950 violations.push(format!(
951 "{} previous_record_digest {} not anchored in package",
952 n.label, n.prev,
953 ));
954 }
955 }
956 let genesis: Vec<&Node> = nodes.iter().filter(|n| n.prev.is_empty()).collect();
958 if genesis.len() > 1 {
959 violations.push(format!(
960 "{} records claim previous_record_digest='' (genesis): {}",
961 genesis.len(),
962 genesis.iter().map(|n| n.label.clone()).collect::<Vec<_>>().join(", "),
963 ));
964 }
965 let mut by_prev: HashMap<&str, Vec<&Node>> = HashMap::new();
967 for n in &nodes {
968 by_prev.entry(n.prev).or_default().push(n);
969 }
970 for (prev, group) in &by_prev {
971 if group.len() > 1 && !prev.is_empty() {
972 violations.push(format!(
973 "fork: {} records share previous_record_digest {}: {}",
974 group.len(),
975 prev,
976 group.iter().map(|n| n.label.clone()).collect::<Vec<_>>().join(", "),
977 ));
978 }
979 }
980
981 if violations.is_empty() {
984 let by_digest: HashMap<&str, &Node> = nodes.iter().map(|n| (n.digest, n)).collect();
985 let next_of: HashMap<&str, &Node> = nodes
986 .iter()
987 .filter(|n| !n.prev.is_empty())
988 .map(|n| (n.prev, *(&n)))
989 .collect();
990 let start = match genesis.first() {
991 Some(g) => Some(*g),
992 None => None,
993 };
994 let mut visited: HashSet<&str> = HashSet::new();
995 let mut current = start;
996 while let Some(node) = current {
997 if !visited.insert(node.digest) {
998 violations.push(format!(
999 "cycle detected at {} (record_digest {})",
1000 node.label, node.digest,
1001 ));
1002 break;
1003 }
1004 current = next_of.get(node.digest).copied();
1005 }
1006 if violations.is_empty() && visited.len() != nodes.len() {
1008 let unreached: Vec<String> = nodes.iter()
1009 .filter(|n| !visited.contains(n.digest))
1010 .map(|n| n.label.clone())
1011 .collect();
1012 if !unreached.is_empty() {
1013 violations.push(format!(
1014 "disconnected subchain: {} record(s) not reachable from genesis: {}",
1015 unreached.len(),
1016 unreached.join(", "),
1017 ));
1018 }
1019 }
1020 let _ = by_digest; }
1022
1023 if violations.is_empty() {
1024 checks.push(VerifyCheck::pass(
1025 "approval-use-chain-continuity",
1026 &format!(
1027 "{} record(s) form a single connected linked list from one genesis with no cycles or forks",
1028 nodes.len(),
1029 ),
1030 ));
1031 } else {
1032 checks.push(VerifyCheck::fail(
1033 "approval-use-chain-continuity",
1034 &violations.join("; "),
1035 ));
1036 }
1037 }
1038
1039 let hub_checkpoints: Vec<&JournalCheckpoint> = bundle
1053 .checkpoints
1054 .iter()
1055 .filter(|cp| cp.checkpoint_kind == crate::statements::CheckpointKind::HubOrg)
1056 .collect();
1057 if !hub_checkpoints.is_empty() {
1058 let mut all_ok = true;
1059 let mut details: Vec<String> = Vec::new();
1060 let mut have_valid_signature = false;
1061
1062 for cp in &hub_checkpoints {
1063 match crate::statements::verify_hub_checkpoint_signature(cp) {
1064 crate::statements::HubCheckpointVerification::Valid => {
1065 have_valid_signature = true;
1066 let covered: std::collections::HashSet<&String> =
1071 cp.covered_use_ids.iter().collect();
1072 let missing: Vec<String> = bundle
1073 .uses
1074 .iter()
1075 .filter(|u| !covered.contains(&u.use_id))
1076 .map(|u| u.use_id.clone())
1077 .collect();
1078 if missing.is_empty() {
1079 details.push(format!(
1080 "{} signed by {} verifies; covers {} use(s)",
1081 cp.checkpoint_id,
1082 cp.hub_id,
1083 cp.covered_use_ids.len(),
1084 ));
1085 } else {
1086 all_ok = false;
1087 details.push(format!(
1088 "{} verifies but does not cover {} use(s): {}",
1089 cp.checkpoint_id,
1090 missing.len(),
1091 missing.join(", "),
1092 ));
1093 }
1094 }
1095 crate::statements::HubCheckpointVerification::MissingFields(field) => {
1096 all_ok = false;
1097 details.push(format!(
1098 "{} declares kind=hub-org but field `{}` is missing",
1099 cp.checkpoint_id, field,
1100 ));
1101 }
1102 crate::statements::HubCheckpointVerification::Tampered => {
1103 all_ok = false;
1104 details.push(format!(
1105 "{} hub signature failed verification (tampered or wrong key)",
1106 cp.checkpoint_id,
1107 ));
1108 }
1109 crate::statements::HubCheckpointVerification::NotHubKind => {
1110 all_ok = false;
1114 details.push(format!(
1115 "{} kind toggled out of hub-org during verify",
1116 cp.checkpoint_id,
1117 ));
1118 }
1119 }
1120 }
1121 if all_ok && have_valid_signature {
1122 checks.push(VerifyCheck::pass(
1123 "replay-hub-org",
1124 &details.join("; "),
1125 ));
1126 } else {
1127 checks.push(VerifyCheck::warn(
1131 "replay-hub-org",
1132 &details.join("; "),
1133 ));
1134 }
1135 }
1136 let _ = ReplayCheckLevel::HubOrg;
1140 let _ = approval_revocation_record_digest as fn(&ApprovalRevocation) -> String;
1141 let _ = ReplayCheck::not_performed;
1142}
1143
1144#[derive(Debug, Clone)]
1146pub struct VerifyCheck {
1147 pub name: String,
1148 pub status: VerifyStatus,
1149 pub detail: String,
1150}
1151
1152#[derive(Debug, Clone, PartialEq, Eq)]
1154pub enum VerifyStatus {
1155 Pass,
1156 Fail,
1157 Warn,
1158}
1159
1160impl VerifyCheck {
1161 pub fn pass(name: &str, detail: &str) -> Self {
1162 Self { name: name.into(), status: VerifyStatus::Pass, detail: detail.into() }
1163 }
1164 pub fn fail(name: &str, detail: &str) -> Self {
1165 Self { name: name.into(), status: VerifyStatus::Fail, detail: detail.into() }
1166 }
1167 pub fn warn(name: &str, detail: &str) -> Self {
1168 Self { name: name.into(), status: VerifyStatus::Warn, detail: detail.into() }
1169 }
1170}
1171
1172impl VerifyCheck {
1173 pub fn passed(&self) -> bool {
1174 self.status == VerifyStatus::Pass
1175 }
1176}
1177
1178const PREVIEW_TEMPLATE: &str = include_str!("preview_template.html");
1181
1182fn generate_preview_html(receipt: &SessionReceipt) -> String {
1189 let receipt_json = serde_json::to_string_pretty(receipt)
1190 .unwrap_or_else(|_| "{}".to_string());
1191 let safe_json = receipt_json.replace('<', r"\u003c");
1199
1200 PREVIEW_TEMPLATE
1204 .replace("__RECEIPT_JSON__", &safe_json)
1205}
1206
1207#[cfg(test)]
1208mod tests {
1209 use super::*;
1210 use crate::session::event::*;
1211 use crate::session::manifest::SessionManifest;
1212 use crate::session::receipt::{ArtifactEntry, ReceiptComposer};
1213
1214 fn make_receipt() -> SessionReceipt {
1215 let manifest = SessionManifest::new(
1216 "ssn_pkg_test".into(),
1217 "agent://test".into(),
1218 "2026-04-05T08:00:00Z".into(),
1219 1743843600000,
1220 );
1221
1222 let mk = |seq: u64, inst: &str, et: EventType| -> SessionEvent {
1223 SessionEvent {
1224 session_id: "ssn_pkg_test".into(),
1225 event_id: format!("evt_{:016x}", seq),
1226 timestamp: format!("2026-04-05T08:{:02}:00Z", seq),
1227 sequence_no: seq,
1228 trace_id: "trace_1".into(),
1229 span_id: format!("span_{seq}"),
1230 parent_span_id: None,
1231 agent_id: format!("agent://{inst}"),
1232 agent_instance_id: inst.into(),
1233 agent_name: inst.into(),
1234 agent_role: None,
1235 host_id: "host_1".into(),
1236 tool_runtime_id: None,
1237 event_type: et,
1238 artifact_ref: None,
1239 meta: None,
1240 }
1241 };
1242
1243 let events = vec![
1244 mk(0, "root", EventType::SessionStarted),
1245 mk(1, "root", EventType::AgentStarted { parent_agent_instance_id: None }),
1246 mk(2, "root", EventType::AgentCalledTool {
1247 tool_name: "read_file".into(),
1248 tool_input_digest: None,
1249 tool_output_digest: None,
1250 duration_ms: Some(10),
1251 }),
1252 mk(3, "root", EventType::AgentCompleted { termination_reason: None }),
1253 mk(4, "root", EventType::SessionClosed { summary: Some("Done".into()), duration_ms: Some(60000) }),
1254 ];
1255
1256 let artifacts = vec![
1257 ArtifactEntry { artifact_id: "art_001".into(), payload_type: "action".into(), digest: None, signed_at: None },
1258 ];
1259
1260 ReceiptComposer::compose(&manifest, &events, artifacts)
1261 }
1262
1263 #[test]
1264 fn build_and_read_package() {
1265 let receipt = make_receipt();
1266 let tmp = std::env::temp_dir().join(format!("treeship-pkg-test-{}", rand::random::<u32>()));
1267
1268 let output = build_package(&receipt, &tmp).unwrap();
1269 assert!(output.path.exists());
1270 assert!(output.path.join("receipt.json").exists());
1271 assert!(output.path.join("merkle.json").exists());
1272 assert!(output.path.join("render.json").exists());
1273 assert!(output.path.join("preview.html").exists());
1274 assert!(output.receipt_digest.starts_with("sha256:"));
1275 assert!(output.file_count >= 4);
1276
1277 let read_back = read_package(&output.path).unwrap();
1279 assert_eq!(read_back.session.id, "ssn_pkg_test");
1280 assert_eq!(read_back.type_, RECEIPT_TYPE);
1281
1282 let _ = std::fs::remove_dir_all(&tmp);
1283 }
1284
1285 #[test]
1286 fn verify_valid_package() {
1287 let receipt = make_receipt();
1288 let tmp = std::env::temp_dir().join(format!("treeship-pkg-verify-{}", rand::random::<u32>()));
1289
1290 let output = build_package(&receipt, &tmp).unwrap();
1291 let checks = verify_package(&output.path).unwrap();
1292
1293 let fails: Vec<_> = checks.iter().filter(|c| c.status == VerifyStatus::Fail).collect();
1294 assert!(fails.is_empty(), "unexpected failures: {fails:?}");
1295
1296 let passes: Vec<_> = checks.iter().filter(|c| c.status == VerifyStatus::Pass).collect();
1297 assert!(passes.len() >= 5, "expected at least 5 pass checks, got {}", passes.len());
1298
1299 let _ = std::fs::remove_dir_all(&tmp);
1300 }
1301
1302 #[test]
1303 fn verify_detects_missing_receipt() {
1304 let tmp = std::env::temp_dir().join(format!("treeship-pkg-empty-{}", rand::random::<u32>()));
1305 std::fs::create_dir_all(&tmp).unwrap();
1306
1307 let err = read_package(&tmp);
1308 assert!(err.is_err());
1309
1310 let _ = std::fs::remove_dir_all(&tmp);
1311 }
1312
1313 #[test]
1314 fn preview_html_contains_session_info() {
1315 let receipt = make_receipt();
1316 let html = generate_preview_html(&receipt);
1317 assert!(html.contains("ssn_pkg_test"));
1318 assert!(html.contains("treeship.dev"));
1319 assert!(html.contains("Timeline"));
1320 }
1321}