From dce2544920e15c9567749b959ce3778d21b4285f Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 01:43:32 -0600 Subject: [PATCH 1/2] fix(id): move replaced metadata to archive --- pkgs/id/src/fileops.rs | 254 +++++++++++++++++++++++++++++++++++++++++ pkgs/id/src/tags.rs | 21 +++- 2 files changed, 274 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/fileops.rs b/pkgs/id/src/fileops.rs index eccecd0552..8eef816933 100644 --- a/pkgs/id/src/fileops.rs +++ b/pkgs/id/src/fileops.rs @@ -28,6 +28,8 @@ use thiserror::Error; use crate::tags::{TagStore, now_unix}; +const ARCHIVE_IDENTITY_TAG_KEYS: [&[u8]; 3] = [b"name", b"file", b"path"]; + /// Errors from file operations, distinguishable so callers can map them to /// protocol errors or HTTP status codes. #[derive(Debug, Error)] @@ -233,6 +235,19 @@ impl<'a> FileOps<'a> { let archive = claim_archive_name(self.store, name, ts, existing.hash) .await .with_context(|| format!("archiving replaced file {name}"))?; + self.tags + .transfer_all_tags_except( + &self.tags.global, + name.as_bytes(), + archive.as_bytes(), + &ARCHIVE_IDENTITY_TAG_KEYS, + ) + .await + .with_context(|| format!("moving metadata for archived file {name}"))?; + self.tags + .refresh_identity_tags(&self.tags.global, archive.as_bytes()) + .await + .with_context(|| format!("refreshing identity for archive {archive}"))?; Ok(Some(archive)) } @@ -596,3 +611,242 @@ mod archive_name_tests { ); } } + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] +mod replacement_metadata_tests { + use crate::local::LocalNode; + use crate::tags::Tag; + + use super::*; + + async fn put(node: &LocalNode, name: &str, content: &[u8]) { + let guard = node + .blobs() + .add_bytes(content.to_vec()) + .temp_tag() + .await + .unwrap(); + node.ops().put(name, guard.hash()).await.unwrap(); + } + + async fn set_tag(node: &LocalNode, subject: &str, key: &str, value: &str) { + node.tags() + .set_tag( + &node.tags().global, + subject.as_bytes(), + key.as_bytes(), + Some(value.as_bytes()), + b"", + ) + .await + .unwrap(); + } + + async fn set_singleton(node: &LocalNode, subject: &str, key: &str, value: &str) { + node.tags() + .set_singleton( + &node.tags().global, + subject.as_bytes(), + key.as_bytes(), + Some(value.as_bytes()), + b"", + ) + .await + .unwrap(); + } + + async fn tags_of(node: &LocalNode, subject: &str) -> Vec { + node.tags() + .get_tags(&node.tags().global, subject.as_bytes()) + .await + .unwrap() + } + + fn values(tags: &[Tag], key: &str) -> Vec> { + tags.iter() + .filter(|tag| tag.key.as_bytes() == key.as_bytes()) + .filter_map(|tag| tag.value.as_ref().map(|value| value.as_bytes().to_vec())) + .collect() + } + + fn has_tag(tags: &[Tag], key: &str, value: &str) -> bool { + values(tags, key) + .iter() + .any(|found| found == value.as_bytes()) + } + + fn assert_single_value(tags: &[Tag], key: &str, value: &str) { + assert_eq!( + values(tags, key), + vec![value.as_bytes().to_vec()], + "{tags:?}" + ); + } + + fn assert_archive_identity(tags: &[Tag], archive: &str) { + assert_single_value(tags, "name", archive); + assert_single_value(tags, "file", archive.rsplit('/').next().unwrap_or(archive)); + assert!( + values(tags, "path").is_empty(), + "archive inherited path: {tags:?}" + ); + } + + async fn seed_replacement_pair(node: &LocalNode) { + put(node, "source.txt", b"new content").await; + put(node, "destination.txt", b"replaced content").await; + + set_tag(node, "source.txt", "shared", "source-value").await; + set_tag(node, "source.txt", "source-only", "source-metadata").await; + set_singleton(node, "source.txt", "created", "source-created").await; + set_singleton(node, "source.txt", "modified", "source-modified").await; + + set_tag(node, "destination.txt", "shared", "replaced-value").await; + set_tag( + node, + "destination.txt", + "replaced-only", + "replaced-metadata", + ) + .await; + set_tag( + node, + "destination.txt", + "archive.previous", + "earlier.archive.7", + ) + .await; + set_tag(node, "destination.txt", "path", "old/source/path.txt").await; + set_singleton(node, "destination.txt", "created", "replaced-created").await; + set_singleton(node, "destination.txt", "modified", "replaced-modified").await; + } + + fn assert_replaced_metadata_on_archive(tags: &[Tag], archive: &str) { + assert!(has_tag(tags, "shared", "replaced-value"), "{tags:?}"); + assert!( + has_tag(tags, "replaced-only", "replaced-metadata"), + "{tags:?}" + ); + assert!( + has_tag(tags, "archive.previous", "earlier.archive.7"), + "{tags:?}" + ); + assert_single_value(tags, "created", "replaced-created"); + assert_single_value(tags, "modified", "replaced-modified"); + assert_archive_identity(tags, archive); + assert!(!has_tag(tags, "name", "destination.txt"), "{tags:?}"); + assert!(!has_tag(tags, "file", "destination.txt"), "{tags:?}"); + } + + #[tokio::test] + async fn rename_onto_existing_moves_replaced_metadata_and_refreshes_archive_identity() { + let node = LocalNode::open(true).await.unwrap(); + seed_replacement_pair(&node).await; + + let outcome = node + .ops() + .rename("source.txt", "destination.txt", false) + .await + .unwrap(); + let archive = outcome.archived_replaced.expect("replaced file archived"); + + let archived = tags_of(&node, &archive).await; + assert_replaced_metadata_on_archive(&archived, &archive); + + let destination = tags_of(&node, "destination.txt").await; + assert!( + has_tag(&destination, "shared", "source-value"), + "{destination:?}" + ); + assert!( + has_tag(&destination, "source-only", "source-metadata"), + "{destination:?}" + ); + assert!( + !has_tag(&destination, "shared", "replaced-value"), + "{destination:?}" + ); + assert!( + !has_tag(&destination, "replaced-only", "replaced-metadata"), + "{destination:?}" + ); + assert!( + !has_tag(&destination, "archive.previous", "earlier.archive.7"), + "{destination:?}" + ); + assert!( + !has_tag(&destination, "path", "old/source/path.txt"), + "{destination:?}" + ); + assert_single_value(&destination, "created", "source-created"); + assert_single_value(&destination, "modified", "source-modified"); + assert_single_value(&destination, "name", "destination.txt"); + assert_single_value(&destination, "file", "destination.txt"); + assert!( + has_tag(&destination, "archive.replace", &archive), + "{destination:?}" + ); + + node.shutdown().await.unwrap(); + } + + #[tokio::test] + async fn copy_onto_existing_moves_replaced_metadata_and_keeps_single_fresh_stamps() { + let node = LocalNode::open(true).await.unwrap(); + seed_replacement_pair(&node).await; + + let outcome = node + .ops() + .copy("source.txt", "destination.txt") + .await + .unwrap(); + let archive = outcome.archived_replaced.expect("replaced file archived"); + + let archived = tags_of(&node, &archive).await; + assert_replaced_metadata_on_archive(&archived, &archive); + + let destination = tags_of(&node, "destination.txt").await; + assert!( + has_tag(&destination, "shared", "source-value"), + "{destination:?}" + ); + assert!( + has_tag(&destination, "source-only", "source-metadata"), + "{destination:?}" + ); + assert!( + !has_tag(&destination, "shared", "replaced-value"), + "{destination:?}" + ); + assert!( + !has_tag(&destination, "replaced-only", "replaced-metadata"), + "{destination:?}" + ); + assert!( + !has_tag(&destination, "archive.previous", "earlier.archive.7"), + "{destination:?}" + ); + assert!( + !has_tag(&destination, "path", "old/source/path.txt"), + "{destination:?}" + ); + assert_eq!(values(&destination, "created").len(), 1, "{destination:?}"); + assert_eq!(values(&destination, "modified").len(), 1, "{destination:?}"); + assert_single_value(&destination, "name", "destination.txt"); + assert_single_value(&destination, "file", "destination.txt"); + assert!( + has_tag(&destination, "archive.replace", &archive), + "{destination:?}" + ); + + let source = tags_of(&node, "source.txt").await; + assert!(has_tag(&source, "shared", "source-value"), "{source:?}"); + assert!( + has_tag(&source, "source-only", "source-metadata"), + "{source:?}" + ); + + node.shutdown().await.unwrap(); + } +} diff --git a/pkgs/id/src/tags.rs b/pkgs/id/src/tags.rs index 59444cdbfe..72b1770157 100644 --- a/pkgs/id/src/tags.rs +++ b/pkgs/id/src/tags.rs @@ -985,10 +985,28 @@ impl TagStore { ns: &NamespacePair, from: &[u8], to: &[u8], + ) -> Result { + self.transfer_all_tags_except(ns, from, to, &[]).await + } + + /// Transfer tags to another subject, dropping tags whose keys are excluded. + /// + /// Each non-excluded tag is copied to `to`, then all tags are deleted from + /// `from`. Excluded tags are discarded rather than left on the old subject. + /// Returns the number of tags copied. + pub async fn transfer_all_tags_except( + &self, + ns: &NamespacePair, + from: &[u8], + to: &[u8], + excluded_keys: &[&[u8]], ) -> Result { let tags = self.get_tags(ns, from).await?; - let count = tags.len(); + let mut count = 0; for tag in &tags { + if excluded_keys.iter().any(|key| *key == tag.key.as_bytes()) { + continue; + } self.set_tag( ns, to, @@ -997,6 +1015,7 @@ impl TagStore { b"", ) .await?; + count += 1; } self.del_all_tags(ns, from).await?; From 83a488442f91310e4c86daed119a0c111af7a452 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 02:58:24 -0600 Subject: [PATCH 2/2] fix(id): preserve tag payloads on transfer; delete archived metadata with its archive Codex review on PR #4266, two P2 findings: 1. transfer_all_tags_except (and copy_all_tags) recreated tags with empty entry data, silently destroying payloads such as legacy link targets written by migrate_legacy_meta. Tags now move by content hash: Tag carries the entry content length, and the new TagStore::set_tag_hash writes both index entries against the original hash, so payloads survive rename/copy archiving exactly. Regression test: rename_onto_existing_preserves_replaced_tag_payloads. 2. FileOps::delete removed archive blob tags but not the metadata archive_existing now moves onto archive subjects, leaving it searchable after hard deletion. delete now removes each archive subject metadata as well. Regression test: delete_removes_archived_replaced_metadata. --- pkgs/id/src/fileops.rs | 82 ++++++++++++++++++++++++++++++++++++++++++ pkgs/id/src/tags.rs | 57 +++++++++++++++++++++++++---- 2 files changed, 133 insertions(+), 6 deletions(-) diff --git a/pkgs/id/src/fileops.rs b/pkgs/id/src/fileops.rs index 8eef816933..cf5e3e031e 100644 --- a/pkgs/id/src/fileops.rs +++ b/pkgs/id/src/fileops.rs @@ -496,6 +496,17 @@ impl<'a> FileOps<'a> { Ok(_) => archives_removed += 1, Err(e) => tracing::warn!("failed to delete archive tag {tag}: {e}"), } + // Archives created by rename/copy carry the replaced file's + // metadata (archive_existing moves it onto the archive subject); + // remove it too, or it stays searchable after the archive blob + // tag is gone. + if let Err(e) = self + .tags + .del_all_tags(&self.tags.global, tag.as_bytes()) + .await + { + tracing::warn!("failed to delete metadata for archive {tag}: {e:#}"); + } } Ok(DeleteOutcome { @@ -849,4 +860,75 @@ mod replacement_metadata_tests { node.shutdown().await.unwrap(); } + + #[tokio::test] + async fn rename_onto_existing_preserves_replaced_tag_payloads() { + let node = LocalNode::open(true).await.unwrap(); + put(&node, "source.txt", b"new content").await; + put(&node, "destination.txt", b"replaced content").await; + + // A tag with a non-empty entry payload, as written for legacy + // links by migrate_legacy_meta (payload carries the link target). + let payload: &[u8] = b"hash:0123abcd"; + node.tags() + .set_tag( + &node.tags().global, + b"destination.txt", + b"link", + Some(b"target"), + payload, + ) + .await + .unwrap(); + let before = tags_of(&node, "destination.txt").await; + let before = before + .iter() + .find(|tag| tag.key.as_bytes() == b"link") + .expect("link tag seeded"); + assert_eq!(before.len as usize, payload.len(), "{before:?}"); + let before_hash = before.hash; + + let outcome = node + .ops() + .rename("source.txt", "destination.txt", false) + .await + .unwrap(); + let archive = outcome.archived_replaced.expect("replaced file archived"); + + let archived = tags_of(&node, &archive).await; + let moved = archived + .iter() + .find(|tag| tag.key.as_bytes() == b"link") + .expect("link tag moved to archive"); + assert_eq!(moved.hash, before_hash, "{moved:?}"); + assert_eq!(moved.len as usize, payload.len(), "{moved:?}"); + + node.shutdown().await.unwrap(); + } + + #[tokio::test] + async fn delete_removes_archived_replaced_metadata() { + let node = LocalNode::open(true).await.unwrap(); + seed_replacement_pair(&node).await; + + let outcome = node + .ops() + .rename("source.txt", "destination.txt", false) + .await + .unwrap(); + let archive = outcome.archived_replaced.expect("replaced file archived"); + assert!( + !tags_of(&node, &archive).await.is_empty(), + "archive carries the replaced file's metadata before delete" + ); + + let deleted = node.ops().delete("destination.txt").await.unwrap(); + assert_eq!(deleted.archives_removed, 1); + assert!( + tags_of(&node, &archive).await.is_empty(), + "archive metadata removed with the archive" + ); + + node.shutdown().await.unwrap(); + } } diff --git a/pkgs/id/src/tags.rs b/pkgs/id/src/tags.rs index 72b1770157..789d4e3cb9 100644 --- a/pkgs/id/src/tags.rs +++ b/pkgs/id/src/tags.rs @@ -335,6 +335,8 @@ pub struct Tag { pub value: Option, /// Content hash of the entry data. pub hash: Hash, + /// Length in bytes of the entry data referenced by `hash`. + pub len: u64, /// Entry timestamp (microseconds since epoch, from iroh-docs). pub timestamp: u64, /// Author who wrote this entry. @@ -845,6 +847,44 @@ impl TagStore { Ok(()) } + /// Set a tag whose entry payload already exists in the blob store, + /// referencing it by content hash instead of writing new bytes. + /// + /// Both α and Ω entries will reference the given content hash, so the + /// payload is preserved exactly. Used when moving or copying tags + /// between subjects, where recreating the tag with new data would + /// silently drop the original payload. + pub async fn set_tag_hash( + &self, + ns: &NamespacePair, + subject: &[u8], + key: &[u8], + value: Option<&[u8]>, + hash: Hash, + size: u64, + ) -> Result<()> { + let alpha_key = encode_alpha_key(subject, key, value); + let omega_key = encode_omega_key(subject, key, value); + + ns.alpha + .set_hash(self.author, alpha_key, hash, size) + .await + .context("writing α entry")?; + ns.omega + .set_hash(self.author, omega_key, hash, size) + .await + .context("writing Ω entry")?; + + self.emit(TagEvent::Set { + ns: self.ns_name(ns), + subject: String::from_utf8_lossy(subject).into_owned(), + key: String::from_utf8_lossy(key).into_owned(), + value: value.map(|v| String::from_utf8_lossy(v).into_owned()), + }); + + Ok(()) + } + /// Delete a specific tag from both α and Ω. /// /// Deletes entries matching the exact `(subject, key, value|null)` tuple. @@ -991,8 +1031,9 @@ impl TagStore { /// Transfer tags to another subject, dropping tags whose keys are excluded. /// - /// Each non-excluded tag is copied to `to`, then all tags are deleted from - /// `from`. Excluded tags are discarded rather than left on the old subject. + /// Each non-excluded tag is copied to `to` by content hash, so its entry + /// payload is preserved exactly, then all tags are deleted from `from`. + /// Excluded tags are discarded rather than left on the old subject. /// Returns the number of tags copied. pub async fn transfer_all_tags_except( &self, @@ -1007,12 +1048,13 @@ impl TagStore { if excluded_keys.iter().any(|key| *key == tag.key.as_bytes()) { continue; } - self.set_tag( + self.set_tag_hash( ns, to, &tag.key, tag.value.as_ref().map(TagValue::as_bytes), - b"", + tag.hash, + tag.len, ) .await?; count += 1; @@ -1038,12 +1080,13 @@ impl TagStore { let tags = self.get_tags(ns, from).await?; let count = tags.len(); for tag in &tags { - self.set_tag( + self.set_tag_hash( ns, to, &tag.key, tag.value.as_ref().map(TagValue::as_bytes), - b"", + tag.hash, + tag.len, ) .await?; } @@ -1424,6 +1467,7 @@ fn entry_to_tag_alpha(entry: &iroh_docs::Entry) -> Result { key, value, hash: entry.content_hash(), + len: entry.content_len(), timestamp: entry.timestamp(), author: entry.author(), }) @@ -1437,6 +1481,7 @@ fn entry_to_tag_omega(entry: &iroh_docs::Entry) -> Result { key, value, hash: entry.content_hash(), + len: entry.content_len(), timestamp: entry.timestamp(), author: entry.author(), })