From 734580193e1a2e711310488b9d4e980b68faa070 Mon Sep 17 00:00:00 2001 From: nnshah1 Date: Thu, 4 Jun 2026 23:42:38 -0700 Subject: [PATCH 1/9] feat(self-host): auto-clean metadata-registry on detach (gh-8749) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `MetadataArtifactRegistry` accumulated entries on every register but had no caller-reachable cleanup path: `clear_self_hosted_artifacts` needed a `LocalModel` handle, and the static `detach_from_endpoint` helper that the Python `unregister_model` binding calls doesn't have one. Long-running workers leaked entries on every LoRA detach or model reload — the documented blocker on flipping `DYN_SELF_HOST_METADATA` default-on. Fix: tag each registration with `Owner = (connection_id, lora_slug)`, the pair `detach_from_endpoint` already has. The registry keeps a secondary `owners -> (slug, suffix)` map; `unregister_for_owner` reads it and calls the existing `unregister` so there is no duplicated cleanup logic. HTTP `get` is unchanged — no new locks on the read path. The previous `attached_self_host_suffix` field and the never-called `clear_self_hosted_artifacts` method are deleted. Tests: existing 2 metadata_registry tests updated to the new `register` signature; new `unregister_for_owner_clears_only_that_owner` asserts LoRA detach leaves the base intact and is idempotent. Signed-off-by: nnshah1 --- lib/llm/src/local_model.rs | 30 ++++------ lib/runtime/src/metadata_registry.rs | 85 +++++++++++++++++++++++----- 2 files changed, 82 insertions(+), 33 deletions(-) diff --git a/lib/llm/src/local_model.rs b/lib/llm/src/local_model.rs index 4cf09b04dfd7..50c0e7b01fb2 100644 --- a/lib/llm/src/local_model.rs +++ b/lib/llm/src/local_model.rs @@ -301,7 +301,6 @@ impl LocalModelBuilder { migration_limit: self.migration_limit, migration_max_seq_len: self.migration_max_seq_len, self_host_metadata: self.self_host_metadata, - attached_self_host_suffix: None, }); } @@ -353,7 +352,6 @@ impl LocalModelBuilder { migration_limit: self.migration_limit, migration_max_seq_len: self.migration_max_seq_len, self_host_metadata: self.self_host_metadata, - attached_self_host_suffix: None, }) } } @@ -376,9 +374,6 @@ pub struct LocalModel { migration_limit: u32, migration_max_seq_len: Option, self_host_metadata: bool, - /// Set by `move_to_self_host` so `clear_self_hosted_artifacts` - /// scopes its unregister to this model's `(slug, suffix)` only. - attached_self_host_suffix: Option, } impl LocalModel { @@ -512,7 +507,7 @@ impl LocalModel { ); if self.self_host_metadata { - self.move_to_self_host(endpoint.drt(), model_suffix.as_deref()) + self.move_to_self_host(endpoint, model_suffix.as_deref()) .context("move_to_self_host")?; } @@ -555,9 +550,10 @@ impl LocalModel { /// (recorded as `BASE_SUFFIX` in the registry). fn move_to_self_host( &mut self, - drt: &dynamo_runtime::DistributedRuntime, + endpoint: &Endpoint, model_suffix: Option<&str>, ) -> anyhow::Result<()> { + let drt = endpoint.drt(); let Some(base_url) = self_host_base_url(drt)? else { tracing::warn!( model_slug = %self.card.slug(), @@ -570,6 +566,7 @@ impl LocalModel { let model_slug = self.card.slug().to_string(); let suffix = model_suffix.unwrap_or(dynamo_runtime::metadata_registry::BASE_SUFFIX); let registry = drt.metadata_artifacts(); + let owner = (drt.connection_id(), model_suffix.map(str::to_string)); // Advertise non-typed siblings (preprocessor_config.json, // special_tokens_map.json, …) so external preprocessors that load @@ -621,12 +618,11 @@ impl LocalModel { let url = url::Url::parse(&format!( "{base_url}/v1/metadata/{model_slug}/{suffix}/{filename}" ))?; - registry.register(&model_slug, suffix, &filename, absolute); + registry.register(owner.clone(), &model_slug, suffix, &filename, absolute); cf.move_to_url(url); rewritten += 1; } - self.attached_self_host_suffix = Some(suffix.to_string()); tracing::debug!( model_slug, suffix, @@ -637,15 +633,6 @@ impl LocalModel { Ok(()) } - /// Idempotent. Call before detach/hot-reload; otherwise the - /// runtime drops the registry entries with the worker process. - pub fn clear_self_hosted_artifacts(&self, drt: &dynamo_runtime::DistributedRuntime) { - if let Some(suffix) = self.attached_self_host_suffix.as_deref() { - drt.metadata_artifacts() - .unregister(self.card.slug().as_ref(), suffix); - } - } - /// Helper associated function to detach a model from an endpoint /// /// For base models, pass `lora_name = None`. @@ -661,6 +648,13 @@ impl LocalModel { // Compute model_suffix from lora_name if present let model_suffix = lora_name.map(|name| Slug::slugify(name).to_string()); + // Drop any self-host metadata-registry entries this attach owned. + // No-op when self-host wasn't enabled or when DYN_SYSTEM_PORT was + // unset; matches `move_to_self_host`'s `(connection_id, lora_slug)` + // owner key. + drt.metadata_artifacts() + .unregister_for_owner(&(instance_id, model_suffix.clone())); + let instance = DiscoveryInstance::Model { namespace: endpoint_id.namespace, component: endpoint_id.component, diff --git a/lib/runtime/src/metadata_registry.rs b/lib/runtime/src/metadata_registry.rs index 21a8b82834ce..c0dbe6c0c502 100644 --- a/lib/runtime/src/metadata_registry.rs +++ b/lib/runtime/src/metadata_registry.rs @@ -12,6 +12,12 @@ //! `suffix` is the LoRA slug (or `"_base"` for non-LoRA). It scopes //! each registration so detaching a LoRA doesn't unregister the base //! model's files (or vice versa). +//! +//! Detach uses [`MetadataArtifactRegistry::unregister_for_owner`]: each +//! registration tags itself with an `Owner = (connection_id, lora_slug)` +//! so the static `LocalModel::detach_from_endpoint` (which has those +//! two values but not the model slug) can clean up without an extra +//! parameter on the Python `unregister_model` API. use std::collections::HashMap; use std::path::PathBuf; @@ -27,39 +33,55 @@ pub const BASE_SUFFIX: &str = "_base"; /// `(slug, suffix, filename)`. type Key = (String, String, String); -/// Cloning shares the underlying map. +/// `(connection_id, lora_slug)` — identifies what `detach_from_endpoint` +/// has on hand. `None` lora_slug = base model. +pub type Owner = (u64, Option); + +/// Cloning shares the underlying maps. #[derive(Clone, Debug, Default)] pub struct MetadataArtifactRegistry { entries: Arc>>, + owners: Arc>>, } impl MetadataArtifactRegistry { pub fn new() -> Self { - Self { - entries: Arc::new(RwLock::new(HashMap::new())), - } + Self::default() } - pub fn register(&self, slug: &str, suffix: &str, filename: &str, path: PathBuf) { - let mut entries = self.entries.write(); - entries.insert( + /// Register one file and tag the `(slug, suffix)` pair with `owner` + /// so `unregister_for_owner` can find it on detach. + pub fn register(&self, owner: Owner, slug: &str, suffix: &str, filename: &str, path: PathBuf) { + self.entries.write().insert( (slug.to_string(), suffix.to_string(), filename.to_string()), path, ); + self.owners + .write() + .insert(owner, (slug.to_string(), suffix.to_string())); tracing::debug!(slug, suffix, filename, "registered metadata artifact"); } pub fn get(&self, slug: &str, suffix: &str, filename: &str) -> Option { - let entries = self.entries.read(); - entries + self.entries + .read() .get(&(slug.to_string(), suffix.to_string(), filename.to_string())) .cloned() } /// Drop entries for a single registration scoped by `(slug, suffix)`. pub fn unregister(&self, slug: &str, suffix: &str) { - let mut entries = self.entries.write(); - entries.retain(|(s, sx, _), _| !(s == slug && sx == suffix)); + self.entries + .write() + .retain(|(s, sx, _), _| !(s == slug && sx == suffix)); + } + + /// Drop every entry registered by `owner`. No-op if `owner` never + /// registered (e.g. self-host was disabled or skipped). + pub fn unregister_for_owner(&self, owner: &Owner) { + if let Some((slug, suffix)) = self.owners.write().remove(owner) { + self.unregister(&slug, &suffix); + } } pub fn len(&self) -> usize { @@ -75,11 +97,13 @@ impl MetadataArtifactRegistry { mod tests { use super::*; + const BASE: Owner = (1, None); + #[test] fn register_get_roundtrip() { let reg = MetadataArtifactRegistry::new(); let p = PathBuf::from("/tmp/tokenizer.json"); - reg.register("llama-3-8b", "_base", "tokenizer.json", p.clone()); + reg.register(BASE, "llama-3-8b", "_base", "tokenizer.json", p.clone()); assert_eq!(reg.get("llama-3-8b", "_base", "tokenizer.json"), Some(p)); assert!(reg.get("llama-3-8b", "_base", "missing.json").is_none()); @@ -89,9 +113,10 @@ mod tests { #[test] fn unregister_only_removes_matching_suffix() { let reg = MetadataArtifactRegistry::new(); - reg.register("m", "_base", "config.json", PathBuf::from("/m/c")); - reg.register("m", "_base", "tokenizer.json", PathBuf::from("/m/t")); - reg.register("m", "lora-v1", "config.json", PathBuf::from("/m/c")); + let lora = (1, Some("lora-v1".to_string())); + reg.register(BASE, "m", "_base", "config.json", PathBuf::from("/m/c")); + reg.register(BASE, "m", "_base", "tokenizer.json", PathBuf::from("/m/t")); + reg.register(lora, "m", "lora-v1", "config.json", PathBuf::from("/m/c")); reg.unregister("m", "_base"); @@ -104,4 +129,34 @@ mod tests { ); assert_eq!(reg.len(), 1); } + + #[test] + fn unregister_for_owner_clears_only_that_owner() { + // Two attaches in the same process (same connection_id): one base + // plus one LoRA. Detaching the LoRA must leave the base intact — + // the auto-cleanup hook on `LocalModel::detach_from_endpoint` + // depends on this scoping. + let reg = MetadataArtifactRegistry::new(); + let lora_owner = (1, Some("lora-v1".to_string())); + reg.register(BASE, "m", "_base", "config.json", PathBuf::from("/m/c")); + reg.register(BASE, "m", "_base", "tokenizer.json", PathBuf::from("/m/t")); + reg.register( + lora_owner.clone(), + "m", + "lora-v1", + "adapter.json", + PathBuf::from("/m/a"), + ); + + reg.unregister_for_owner(&lora_owner); + + assert!(reg.get("m", "lora-v1", "adapter.json").is_none()); + assert_eq!( + reg.get("m", "_base", "config.json"), + Some(PathBuf::from("/m/c")) + ); + // Idempotent — second call is a no-op. + reg.unregister_for_owner(&lora_owner); + assert_eq!(reg.len(), 2); + } } From b502a81b60cf0696d615aa502645c81deb5f8242 Mon Sep 17 00:00:00 2001 From: nnshah1 Date: Wed, 10 Jun 2026 15:43:07 -0700 Subject: [PATCH 2/9] refactor(metadata-registry): single-map Owner storage + collision panic Address graham-code-review feedback on PR #10351: - Drop the secondary `owners` map; store `Owner = (instance_id, lora_slug)` inline with each entry value. One lock, one source of truth, no nested-write-lock hazard, no two-map sync risk. - `register` takes `&Owner` (one clone inside, not per-file). - Panic on collision: re-registering the same (slug, suffix, filename) with a different owner is a programming error (two attaches of the same model+suffix in one process would let detach-#1 wipe files detach-#2 still needs). Same-owner re-register is fine and just updates the path. - Doc + local var naming aligned on `instance_id` to match `local_model.rs`'s existing usage (the value populates `DiscoveryInstance::Model.instance_id`). - Tests: collision panic + same-owner update path coverage. Signed-off-by: nnshah1 --- lib/llm/src/local_model.rs | 10 +-- lib/runtime/src/metadata_registry.rs | 121 ++++++++++++++++++--------- 2 files changed, 85 insertions(+), 46 deletions(-) diff --git a/lib/llm/src/local_model.rs b/lib/llm/src/local_model.rs index 50c0e7b01fb2..1fa5f10a2481 100644 --- a/lib/llm/src/local_model.rs +++ b/lib/llm/src/local_model.rs @@ -566,7 +566,8 @@ impl LocalModel { let model_slug = self.card.slug().to_string(); let suffix = model_suffix.unwrap_or(dynamo_runtime::metadata_registry::BASE_SUFFIX); let registry = drt.metadata_artifacts(); - let owner = (drt.connection_id(), model_suffix.map(str::to_string)); + let instance_id = drt.connection_id(); + let owner = (instance_id, model_suffix.map(str::to_string)); // Advertise non-typed siblings (preprocessor_config.json, // special_tokens_map.json, …) so external preprocessors that load @@ -618,7 +619,7 @@ impl LocalModel { let url = url::Url::parse(&format!( "{base_url}/v1/metadata/{model_slug}/{suffix}/{filename}" ))?; - registry.register(owner.clone(), &model_slug, suffix, &filename, absolute); + registry.register(&owner, &model_slug, suffix, &filename, absolute); cf.move_to_url(url); rewritten += 1; } @@ -645,13 +646,8 @@ impl LocalModel { let instance_id = drt.connection_id(); let endpoint_id = endpoint.id(); - // Compute model_suffix from lora_name if present let model_suffix = lora_name.map(|name| Slug::slugify(name).to_string()); - // Drop any self-host metadata-registry entries this attach owned. - // No-op when self-host wasn't enabled or when DYN_SYSTEM_PORT was - // unset; matches `move_to_self_host`'s `(connection_id, lora_slug)` - // owner key. drt.metadata_artifacts() .unregister_for_owner(&(instance_id, model_suffix.clone())); diff --git a/lib/runtime/src/metadata_registry.rs b/lib/runtime/src/metadata_registry.rs index c0dbe6c0c502..5f9ad0602eb2 100644 --- a/lib/runtime/src/metadata_registry.rs +++ b/lib/runtime/src/metadata_registry.rs @@ -13,11 +13,11 @@ //! each registration so detaching a LoRA doesn't unregister the base //! model's files (or vice versa). //! -//! Detach uses [`MetadataArtifactRegistry::unregister_for_owner`]: each -//! registration tags itself with an `Owner = (connection_id, lora_slug)` -//! so the static `LocalModel::detach_from_endpoint` (which has those -//! two values but not the model slug) can clean up without an extra -//! parameter on the Python `unregister_model` API. +//! Each entry also stores its `Owner = (instance_id, lora_slug)` so +//! `unregister_for_owner` can clean up on detach without the caller +//! threading the model slug. Each `(slug, suffix, filename)` key must +//! have at most one owner — `register` panics on collision with a +//! different owner. use std::collections::HashMap; use std::path::PathBuf; @@ -33,15 +33,13 @@ pub const BASE_SUFFIX: &str = "_base"; /// `(slug, suffix, filename)`. type Key = (String, String, String); -/// `(connection_id, lora_slug)` — identifies what `detach_from_endpoint` -/// has on hand. `None` lora_slug = base model. +/// `(instance_id, lora_slug)`. `None` lora_slug = base model. pub type Owner = (u64, Option); -/// Cloning shares the underlying maps. +/// Cloning shares the underlying map. #[derive(Clone, Debug, Default)] pub struct MetadataArtifactRegistry { - entries: Arc>>, - owners: Arc>>, + entries: Arc>>, } impl MetadataArtifactRegistry { @@ -49,16 +47,24 @@ impl MetadataArtifactRegistry { Self::default() } - /// Register one file and tag the `(slug, suffix)` pair with `owner` - /// so `unregister_for_owner` can find it on detach. - pub fn register(&self, owner: Owner, slug: &str, suffix: &str, filename: &str, path: PathBuf) { - self.entries.write().insert( - (slug.to_string(), suffix.to_string(), filename.to_string()), - path, - ); - self.owners - .write() - .insert(owner, (slug.to_string(), suffix.to_string())); + /// Panics if `(slug, suffix, filename)` is already registered by a + /// different owner — two `LocalModel` instances attaching the same + /// model + LoRA suffix in one process would let detach-#1 wipe + /// files that detach-#2 still needs. Re-registering the same + /// (slug, suffix, filename) by the *same* owner just updates the + /// path and is fine. + pub fn register(&self, owner: &Owner, slug: &str, suffix: &str, filename: &str, path: PathBuf) { + let key = (slug.to_string(), suffix.to_string(), filename.to_string()); + let mut entries = self.entries.write(); + if let Some((_, prior)) = entries.get(&key) { + assert_eq!( + prior, owner, + "metadata-registry collision on {key:?}: prior owner {prior:?} \ + differs from new owner {owner:?}; two attaches of the same \ + model+suffix in one process are not supported", + ); + } + entries.insert(key, (path, owner.clone())); tracing::debug!(slug, suffix, filename, "registered metadata artifact"); } @@ -66,7 +72,7 @@ impl MetadataArtifactRegistry { self.entries .read() .get(&(slug.to_string(), suffix.to_string(), filename.to_string())) - .cloned() + .map(|(p, _)| p.clone()) } /// Drop entries for a single registration scoped by `(slug, suffix)`. @@ -79,9 +85,7 @@ impl MetadataArtifactRegistry { /// Drop every entry registered by `owner`. No-op if `owner` never /// registered (e.g. self-host was disabled or skipped). pub fn unregister_for_owner(&self, owner: &Owner) { - if let Some((slug, suffix)) = self.owners.write().remove(owner) { - self.unregister(&slug, &suffix); - } + self.entries.write().retain(|_, (_, o)| o != owner); } pub fn len(&self) -> usize { @@ -97,13 +101,19 @@ impl MetadataArtifactRegistry { mod tests { use super::*; - const BASE: Owner = (1, None); + fn base() -> Owner { + (1, None) + } + + fn lora(slug: &str) -> Owner { + (1, Some(slug.to_string())) + } #[test] fn register_get_roundtrip() { let reg = MetadataArtifactRegistry::new(); let p = PathBuf::from("/tmp/tokenizer.json"); - reg.register(BASE, "llama-3-8b", "_base", "tokenizer.json", p.clone()); + reg.register(&base(), "llama-3-8b", "_base", "tokenizer.json", p.clone()); assert_eq!(reg.get("llama-3-8b", "_base", "tokenizer.json"), Some(p)); assert!(reg.get("llama-3-8b", "_base", "missing.json").is_none()); @@ -113,16 +123,26 @@ mod tests { #[test] fn unregister_only_removes_matching_suffix() { let reg = MetadataArtifactRegistry::new(); - let lora = (1, Some("lora-v1".to_string())); - reg.register(BASE, "m", "_base", "config.json", PathBuf::from("/m/c")); - reg.register(BASE, "m", "_base", "tokenizer.json", PathBuf::from("/m/t")); - reg.register(lora, "m", "lora-v1", "config.json", PathBuf::from("/m/c")); + reg.register(&base(), "m", "_base", "config.json", PathBuf::from("/m/c")); + reg.register( + &base(), + "m", + "_base", + "tokenizer.json", + PathBuf::from("/m/t"), + ); + reg.register( + &lora("lora-v1"), + "m", + "lora-v1", + "config.json", + PathBuf::from("/m/c"), + ); reg.unregister("m", "_base"); assert!(reg.get("m", "_base", "config.json").is_none()); assert!(reg.get("m", "_base", "tokenizer.json").is_none()); - // LoRA entry on the same slug survives detach of the base. assert_eq!( reg.get("m", "lora-v1", "config.json"), Some(PathBuf::from("/m/c")) @@ -132,16 +152,18 @@ mod tests { #[test] fn unregister_for_owner_clears_only_that_owner() { - // Two attaches in the same process (same connection_id): one base - // plus one LoRA. Detaching the LoRA must leave the base intact — - // the auto-cleanup hook on `LocalModel::detach_from_endpoint` - // depends on this scoping. let reg = MetadataArtifactRegistry::new(); - let lora_owner = (1, Some("lora-v1".to_string())); - reg.register(BASE, "m", "_base", "config.json", PathBuf::from("/m/c")); - reg.register(BASE, "m", "_base", "tokenizer.json", PathBuf::from("/m/t")); + let lora_owner = lora("lora-v1"); + reg.register(&base(), "m", "_base", "config.json", PathBuf::from("/m/c")); reg.register( - lora_owner.clone(), + &base(), + "m", + "_base", + "tokenizer.json", + PathBuf::from("/m/t"), + ); + reg.register( + &lora_owner, "m", "lora-v1", "adapter.json", @@ -159,4 +181,25 @@ mod tests { reg.unregister_for_owner(&lora_owner); assert_eq!(reg.len(), 2); } + + #[test] + #[should_panic(expected = "metadata-registry collision")] + fn register_panics_on_owner_collision() { + let reg = MetadataArtifactRegistry::new(); + let owner_a = (1, None); + let owner_b = (2, None); + reg.register(&owner_a, "m", "_base", "config.json", PathBuf::from("/a")); + reg.register(&owner_b, "m", "_base", "config.json", PathBuf::from("/b")); + } + + #[test] + fn register_same_owner_updates_path() { + let reg = MetadataArtifactRegistry::new(); + reg.register(&base(), "m", "_base", "config.json", PathBuf::from("/a")); + reg.register(&base(), "m", "_base", "config.json", PathBuf::from("/b")); + assert_eq!( + reg.get("m", "_base", "config.json"), + Some(PathBuf::from("/b")) + ); + } } From 64c25a062f31211b4515f744e1645222a327b69f Mon Sep 17 00:00:00 2001 From: nnshah1 Date: Tue, 23 Jun 2026 15:14:22 -0700 Subject: [PATCH 3/9] fix(self-host): unregister metadata only after discovery.unregister succeeds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If discovery.unregister failed transiently, the model card stayed in discovery while the local metadata was already gone — frontend would fetch /v1/metadata URLs that 404'd. Defer the registry cleanup until after discovery.unregister returns Ok. Capture the owner key before the move into DiscoveryInstance. Signed-off-by: nnshah1 --- lib/llm/src/local_model.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/llm/src/local_model.rs b/lib/llm/src/local_model.rs index 1fa5f10a2481..5d6c1381d360 100644 --- a/lib/llm/src/local_model.rs +++ b/lib/llm/src/local_model.rs @@ -647,9 +647,7 @@ impl LocalModel { let endpoint_id = endpoint.id(); let model_suffix = lora_name.map(|name| Slug::slugify(name).to_string()); - - drt.metadata_artifacts() - .unregister_for_owner(&(instance_id, model_suffix.clone())); + let registry_owner = (instance_id, model_suffix.clone()); let instance = DiscoveryInstance::Model { namespace: endpoint_id.namespace, @@ -662,6 +660,7 @@ impl LocalModel { let discovery = drt.discovery(); discovery.unregister(instance).await?; + drt.metadata_artifacts().unregister_for_owner(®istry_owner); if let Some(lora_name) = lora_name { tracing::info!( From 28d5aefca2319f1f1800bdddf6aa1563dd5b1f85 Mon Sep 17 00:00:00 2001 From: nnshah1 Date: Tue, 23 Jun 2026 21:41:47 -0700 Subject: [PATCH 4/9] refactor(metadata-registry): drop dead unregister + concat key (CodeRabbit feedback) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove MetadataArtifactRegistry::unregister (slug+suffix variant) and its test. Post-PR, the only production caller is unregister_for_owner which filters by Owner stored in the value — the slug+suffix method had no remaining callers and risked drifting out of sync with ownership semantics. - Switch Key from (String, String, String) tuple to a single \0-separated String. One allocation per lookup instead of three. The HTTP route handler (system_status_server) is the only get() caller and already has owned String parts from the URL parse; this just removes the duplicate work. - Trim the collision-panic message per review style (no AI-scaffolded prose, just the colliding owners). Signed-off-by: nnshah1 --- lib/llm/src/local_model.rs | 3 +- lib/runtime/src/metadata_registry.rs | 72 ++++++++-------------------- 2 files changed, 22 insertions(+), 53 deletions(-) diff --git a/lib/llm/src/local_model.rs b/lib/llm/src/local_model.rs index 5d6c1381d360..6c6f3a82b4ce 100644 --- a/lib/llm/src/local_model.rs +++ b/lib/llm/src/local_model.rs @@ -660,7 +660,8 @@ impl LocalModel { let discovery = drt.discovery(); discovery.unregister(instance).await?; - drt.metadata_artifacts().unregister_for_owner(®istry_owner); + drt.metadata_artifacts() + .unregister_for_owner(®istry_owner); if let Some(lora_name) = lora_name { tracing::info!( diff --git a/lib/runtime/src/metadata_registry.rs b/lib/runtime/src/metadata_registry.rs index 5f9ad0602eb2..20e43176dbb0 100644 --- a/lib/runtime/src/metadata_registry.rs +++ b/lib/runtime/src/metadata_registry.rs @@ -30,16 +30,25 @@ use parking_lot::RwLock; /// `_base` would collide with this sentinel and is not supported. pub const BASE_SUFFIX: &str = "_base"; -/// `(slug, suffix, filename)`. -type Key = (String, String, String); - /// `(instance_id, lora_slug)`. `None` lora_slug = base model. pub type Owner = (u64, Option); /// Cloning shares the underlying map. #[derive(Clone, Debug, Default)] pub struct MetadataArtifactRegistry { - entries: Arc>>, + // Key is a single concatenated `{slug}\0{suffix}\0{filename}` string — + // one allocation per lookup instead of a 3-`String` tuple. + entries: Arc>>, +} + +fn make_key(slug: &str, suffix: &str, filename: &str) -> String { + let mut s = String::with_capacity(slug.len() + suffix.len() + filename.len() + 2); + s.push_str(slug); + s.push('\0'); + s.push_str(suffix); + s.push('\0'); + s.push_str(filename); + s } impl MetadataArtifactRegistry { @@ -47,21 +56,17 @@ impl MetadataArtifactRegistry { Self::default() } - /// Panics if `(slug, suffix, filename)` is already registered by a - /// different owner — two `LocalModel` instances attaching the same - /// model + LoRA suffix in one process would let detach-#1 wipe - /// files that detach-#2 still needs. Re-registering the same - /// (slug, suffix, filename) by the *same* owner just updates the - /// path and is fine. + /// Panics on owner collision — two `LocalModel` instances attaching the + /// same (slug, suffix, filename) in one process would let detach-#1 + /// wipe files detach-#2 still needs. Same-owner re-register is fine + /// (path update). pub fn register(&self, owner: &Owner, slug: &str, suffix: &str, filename: &str, path: PathBuf) { - let key = (slug.to_string(), suffix.to_string(), filename.to_string()); + let key = make_key(slug, suffix, filename); let mut entries = self.entries.write(); if let Some((_, prior)) = entries.get(&key) { assert_eq!( prior, owner, - "metadata-registry collision on {key:?}: prior owner {prior:?} \ - differs from new owner {owner:?}; two attaches of the same \ - model+suffix in one process are not supported", + "metadata-registry collision on {key:?}: prior={prior:?}, new={owner:?}", ); } entries.insert(key, (path, owner.clone())); @@ -71,17 +76,10 @@ impl MetadataArtifactRegistry { pub fn get(&self, slug: &str, suffix: &str, filename: &str) -> Option { self.entries .read() - .get(&(slug.to_string(), suffix.to_string(), filename.to_string())) + .get(&make_key(slug, suffix, filename)) .map(|(p, _)| p.clone()) } - /// Drop entries for a single registration scoped by `(slug, suffix)`. - pub fn unregister(&self, slug: &str, suffix: &str) { - self.entries - .write() - .retain(|(s, sx, _), _| !(s == slug && sx == suffix)); - } - /// Drop every entry registered by `owner`. No-op if `owner` never /// registered (e.g. self-host was disabled or skipped). pub fn unregister_for_owner(&self, owner: &Owner) { @@ -120,36 +118,6 @@ mod tests { assert!(reg.get("llama-3-8b", "lora-v1", "tokenizer.json").is_none()); } - #[test] - fn unregister_only_removes_matching_suffix() { - let reg = MetadataArtifactRegistry::new(); - reg.register(&base(), "m", "_base", "config.json", PathBuf::from("/m/c")); - reg.register( - &base(), - "m", - "_base", - "tokenizer.json", - PathBuf::from("/m/t"), - ); - reg.register( - &lora("lora-v1"), - "m", - "lora-v1", - "config.json", - PathBuf::from("/m/c"), - ); - - reg.unregister("m", "_base"); - - assert!(reg.get("m", "_base", "config.json").is_none()); - assert!(reg.get("m", "_base", "tokenizer.json").is_none()); - assert_eq!( - reg.get("m", "lora-v1", "config.json"), - Some(PathBuf::from("/m/c")) - ); - assert_eq!(reg.len(), 1); - } - #[test] fn unregister_for_owner_clears_only_that_owner() { let reg = MetadataArtifactRegistry::new(); From 6bd73aa4f5a5944daa81f32f3e57ca2f565b887e Mon Sep 17 00:00:00 2001 From: nnshah1 Date: Thu, 25 Jun 2026 14:16:19 -0700 Subject: [PATCH 5/9] refactor: address GuanLuo review (revert concat key; simplify move_to_self_host signature) - metadata_registry: revert key to (String, String, String) tuple per GuanLuo - local_model: move_to_self_host takes &DistributedRuntime directly Signed-off-by: nnshah1 --- lib/llm/src/local_model.rs | 5 ++--- lib/runtime/src/metadata_registry.rs | 24 ++++++------------------ 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/lib/llm/src/local_model.rs b/lib/llm/src/local_model.rs index 6c6f3a82b4ce..f0d0db41388b 100644 --- a/lib/llm/src/local_model.rs +++ b/lib/llm/src/local_model.rs @@ -507,7 +507,7 @@ impl LocalModel { ); if self.self_host_metadata { - self.move_to_self_host(endpoint, model_suffix.as_deref()) + self.move_to_self_host(endpoint.drt(), model_suffix.as_deref()) .context("move_to_self_host")?; } @@ -550,10 +550,9 @@ impl LocalModel { /// (recorded as `BASE_SUFFIX` in the registry). fn move_to_self_host( &mut self, - endpoint: &Endpoint, + drt: &dynamo_runtime::DistributedRuntime, model_suffix: Option<&str>, ) -> anyhow::Result<()> { - let drt = endpoint.drt(); let Some(base_url) = self_host_base_url(drt)? else { tracing::warn!( model_slug = %self.card.slug(), diff --git a/lib/runtime/src/metadata_registry.rs b/lib/runtime/src/metadata_registry.rs index 20e43176dbb0..87d4daecaa68 100644 --- a/lib/runtime/src/metadata_registry.rs +++ b/lib/runtime/src/metadata_registry.rs @@ -33,22 +33,12 @@ pub const BASE_SUFFIX: &str = "_base"; /// `(instance_id, lora_slug)`. `None` lora_slug = base model. pub type Owner = (u64, Option); +type Key = (String, String, String); + /// Cloning shares the underlying map. #[derive(Clone, Debug, Default)] pub struct MetadataArtifactRegistry { - // Key is a single concatenated `{slug}\0{suffix}\0{filename}` string — - // one allocation per lookup instead of a 3-`String` tuple. - entries: Arc>>, -} - -fn make_key(slug: &str, suffix: &str, filename: &str) -> String { - let mut s = String::with_capacity(slug.len() + suffix.len() + filename.len() + 2); - s.push_str(slug); - s.push('\0'); - s.push_str(suffix); - s.push('\0'); - s.push_str(filename); - s + entries: Arc>>, } impl MetadataArtifactRegistry { @@ -61,7 +51,7 @@ impl MetadataArtifactRegistry { /// wipe files detach-#2 still needs. Same-owner re-register is fine /// (path update). pub fn register(&self, owner: &Owner, slug: &str, suffix: &str, filename: &str, path: PathBuf) { - let key = make_key(slug, suffix, filename); + let key = (slug.to_string(), suffix.to_string(), filename.to_string()); let mut entries = self.entries.write(); if let Some((_, prior)) = entries.get(&key) { assert_eq!( @@ -74,10 +64,8 @@ impl MetadataArtifactRegistry { } pub fn get(&self, slug: &str, suffix: &str, filename: &str) -> Option { - self.entries - .read() - .get(&make_key(slug, suffix, filename)) - .map(|(p, _)| p.clone()) + let key = (slug.to_string(), suffix.to_string(), filename.to_string()); + self.entries.read().get(&key).map(|(p, _)| p.clone()) } /// Drop every entry registered by `owner`. No-op if `owner` never From 4a74346c0dd7dd7da699c0adb3cbdb84215eb57b Mon Sep 17 00:00:00 2001 From: nnshah1 Date: Thu, 25 Jun 2026 14:47:32 -0700 Subject: [PATCH 6/9] refactor(metadata-registry): endpoint-triple key + Result on collision MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Key is now (namespace, component, endpoint, slug, suffix, filename). The endpoint triple disambiguates multi-LocalModel-per-DRT — two roles attaching the same base model on one worker no longer collide. - register() returns Err(CollisionError) instead of panicking; caller propagates with ? so the worker fails to start cleanly on programmer error rather than aborting. - URL becomes /v1/metadata/{ns}/{comp}/{ep}/{slug}/{suffix}/{filename}. Worker-internal — frontend treats CheckedFile.path as opaque. - move_to_self_host takes &Endpoint again (it now genuinely needs the endpoint triple, not just &DistributedRuntime). Signed-off-by: nnshah1 --- lib/llm/src/local_model.rs | 23 ++- lib/runtime/src/metadata_registry.rs | 257 ++++++++++++++++++++---- lib/runtime/src/system_status_server.rs | 39 +++- 3 files changed, 265 insertions(+), 54 deletions(-) diff --git a/lib/llm/src/local_model.rs b/lib/llm/src/local_model.rs index f0d0db41388b..310aa5e0affb 100644 --- a/lib/llm/src/local_model.rs +++ b/lib/llm/src/local_model.rs @@ -507,7 +507,7 @@ impl LocalModel { ); if self.self_host_metadata { - self.move_to_self_host(endpoint.drt(), model_suffix.as_deref()) + self.move_to_self_host(endpoint, model_suffix.as_deref()) .context("move_to_self_host")?; } @@ -550,9 +550,13 @@ impl LocalModel { /// (recorded as `BASE_SUFFIX` in the registry). fn move_to_self_host( &mut self, - drt: &dynamo_runtime::DistributedRuntime, + endpoint: &Endpoint, model_suffix: Option<&str>, ) -> anyhow::Result<()> { + let drt = endpoint.drt(); + let namespace = endpoint.component().namespace().name().to_string(); + let component = endpoint.component().name().to_string(); + let endpoint_name = endpoint.name().to_string(); let Some(base_url) = self_host_base_url(drt)? else { tracing::warn!( model_slug = %self.card.slug(), @@ -616,9 +620,20 @@ impl LocalModel { }; let url = url::Url::parse(&format!( - "{base_url}/v1/metadata/{model_slug}/{suffix}/{filename}" + "{base_url}/v1/metadata/{namespace}/{component}/{endpoint_name}/{model_slug}/{suffix}/{filename}" ))?; - registry.register(&owner, &model_slug, suffix, &filename, absolute); + registry + .register( + &owner, + &namespace, + &component, + &endpoint_name, + &model_slug, + suffix, + &filename, + absolute, + ) + .context("registering metadata artifact")?; cf.move_to_url(url); rewritten += 1; } diff --git a/lib/runtime/src/metadata_registry.rs b/lib/runtime/src/metadata_registry.rs index 87d4daecaa68..29c21e6dd9d6 100644 --- a/lib/runtime/src/metadata_registry.rs +++ b/lib/runtime/src/metadata_registry.rs @@ -4,26 +4,29 @@ //! Worker-side index from a model metadata file's identity to its //! on-disk path. When a worker self-hosts metadata, it registers each //! file here and rewrites the MDC's `CheckedFile.path` to a -//! `/v1/metadata/{slug}/{suffix}/{filename}` URL on its own -//! `system_status_server`. The route handler reads paths back out by -//! the same key and streams the bytes to the frontend, which +//! `/v1/metadata/{namespace}/{component}/{endpoint}/{slug}/{suffix}/{filename}` +//! URL on its own `system_status_server`. The route handler reads paths +//! back out by the same key and streams the bytes to the frontend, which //! blake3-verifies them against the MDC. //! -//! `suffix` is the LoRA slug (or `"_base"` for non-LoRA). It scopes -//! each registration so detaching a LoRA doesn't unregister the base -//! model's files (or vice versa). +//! The endpoint triple in the key disambiguates multiple `LocalModel` +//! instances coexisting on one DRT (e.g. different roles attaching the +//! same base model). `suffix` is the LoRA slug (or `"_base"` for +//! non-LoRA), scoping each registration so detaching a LoRA doesn't +//! unregister the base model's files. //! //! Each entry also stores its `Owner = (instance_id, lora_slug)` so //! `unregister_for_owner` can clean up on detach without the caller -//! threading the model slug. Each `(slug, suffix, filename)` key must -//! have at most one owner — `register` panics on collision with a -//! different owner. +//! threading the model slug. `register` returns +//! `Err(CollisionError)` on conflict — the caller propagates and the +//! worker fails to start. use std::collections::HashMap; use std::path::PathBuf; use std::sync::Arc; use parking_lot::RwLock; +use thiserror::Error; /// Sentinel `suffix` for non-LoRA registrations. LoRA suffixes are /// `Slug::slugify` outputs (`[a-z0-9_-]+`); a name that slugifies to @@ -33,7 +36,19 @@ pub const BASE_SUFFIX: &str = "_base"; /// `(instance_id, lora_slug)`. `None` lora_slug = base model. pub type Owner = (u64, Option); -type Key = (String, String, String); +/// `(namespace, component, endpoint, slug, suffix, filename)`. +type Key = (String, String, String, String, String, String); + +/// Registration collided with a different owner — programmer error. +#[derive(Debug, Error)] +#[error( + "metadata-registry collision on key {key:?}: prior_owner={prior:?}, new_owner={new:?}" +)] +pub struct CollisionError { + pub key: Key, + pub prior: Owner, + pub new: Owner, +} /// Cloning shares the underlying map. #[derive(Clone, Debug, Default)] @@ -46,25 +61,69 @@ impl MetadataArtifactRegistry { Self::default() } - /// Panics on owner collision — two `LocalModel` instances attaching the - /// same (slug, suffix, filename) in one process would let detach-#1 - /// wipe files detach-#2 still needs. Same-owner re-register is fine - /// (path update). - pub fn register(&self, owner: &Owner, slug: &str, suffix: &str, filename: &str, path: PathBuf) { - let key = (slug.to_string(), suffix.to_string(), filename.to_string()); + /// Returns `Err(CollisionError)` if a different owner already + /// registered this key. Same-owner re-register updates the path. + #[allow(clippy::too_many_arguments)] + pub fn register( + &self, + owner: &Owner, + namespace: &str, + component: &str, + endpoint: &str, + slug: &str, + suffix: &str, + filename: &str, + path: PathBuf, + ) -> Result<(), CollisionError> { + let key = ( + namespace.to_string(), + component.to_string(), + endpoint.to_string(), + slug.to_string(), + suffix.to_string(), + filename.to_string(), + ); let mut entries = self.entries.write(); if let Some((_, prior)) = entries.get(&key) { - assert_eq!( - prior, owner, - "metadata-registry collision on {key:?}: prior={prior:?}, new={owner:?}", - ); + if prior != owner { + return Err(CollisionError { + key, + prior: prior.clone(), + new: owner.clone(), + }); + } } entries.insert(key, (path, owner.clone())); - tracing::debug!(slug, suffix, filename, "registered metadata artifact"); + tracing::debug!( + namespace, + component, + endpoint, + slug, + suffix, + filename, + "registered metadata artifact", + ); + Ok(()) } - pub fn get(&self, slug: &str, suffix: &str, filename: &str) -> Option { - let key = (slug.to_string(), suffix.to_string(), filename.to_string()); + #[allow(clippy::too_many_arguments)] + pub fn get( + &self, + namespace: &str, + component: &str, + endpoint: &str, + slug: &str, + suffix: &str, + filename: &str, + ) -> Option { + let key = ( + namespace.to_string(), + component.to_string(), + endpoint.to_string(), + slug.to_string(), + suffix.to_string(), + filename.to_string(), + ); self.entries.read().get(&key).map(|(p, _)| p.clone()) } @@ -99,38 +158,78 @@ mod tests { fn register_get_roundtrip() { let reg = MetadataArtifactRegistry::new(); let p = PathBuf::from("/tmp/tokenizer.json"); - reg.register(&base(), "llama-3-8b", "_base", "tokenizer.json", p.clone()); + reg.register( + &base(), + "ns", + "comp", + "ep", + "llama-3-8b", + "_base", + "tokenizer.json", + p.clone(), + ) + .unwrap(); - assert_eq!(reg.get("llama-3-8b", "_base", "tokenizer.json"), Some(p)); - assert!(reg.get("llama-3-8b", "_base", "missing.json").is_none()); - assert!(reg.get("llama-3-8b", "lora-v1", "tokenizer.json").is_none()); + assert_eq!( + reg.get("ns", "comp", "ep", "llama-3-8b", "_base", "tokenizer.json"), + Some(p) + ); + assert!( + reg.get("ns", "comp", "ep", "llama-3-8b", "_base", "missing.json") + .is_none() + ); + assert!( + reg.get("ns", "comp", "ep", "llama-3-8b", "lora-v1", "tokenizer.json") + .is_none() + ); } #[test] fn unregister_for_owner_clears_only_that_owner() { let reg = MetadataArtifactRegistry::new(); let lora_owner = lora("lora-v1"); - reg.register(&base(), "m", "_base", "config.json", PathBuf::from("/m/c")); reg.register( &base(), + "ns", + "comp", + "ep", + "m", + "_base", + "config.json", + PathBuf::from("/m/c"), + ) + .unwrap(); + reg.register( + &base(), + "ns", + "comp", + "ep", "m", "_base", "tokenizer.json", PathBuf::from("/m/t"), - ); + ) + .unwrap(); reg.register( &lora_owner, + "ns", + "comp", + "ep", "m", "lora-v1", "adapter.json", PathBuf::from("/m/a"), - ); + ) + .unwrap(); reg.unregister_for_owner(&lora_owner); - assert!(reg.get("m", "lora-v1", "adapter.json").is_none()); + assert!( + reg.get("ns", "comp", "ep", "m", "lora-v1", "adapter.json") + .is_none() + ); assert_eq!( - reg.get("m", "_base", "config.json"), + reg.get("ns", "comp", "ep", "m", "_base", "config.json"), Some(PathBuf::from("/m/c")) ); // Idempotent — second call is a no-op. @@ -139,22 +238,102 @@ mod tests { } #[test] - #[should_panic(expected = "metadata-registry collision")] - fn register_panics_on_owner_collision() { + fn register_returns_err_on_owner_collision() { let reg = MetadataArtifactRegistry::new(); let owner_a = (1, None); let owner_b = (2, None); - reg.register(&owner_a, "m", "_base", "config.json", PathBuf::from("/a")); - reg.register(&owner_b, "m", "_base", "config.json", PathBuf::from("/b")); + reg.register( + &owner_a, + "ns", + "comp", + "ep", + "m", + "_base", + "config.json", + PathBuf::from("/a"), + ) + .unwrap(); + let err = reg + .register( + &owner_b, + "ns", + "comp", + "ep", + "m", + "_base", + "config.json", + PathBuf::from("/b"), + ) + .unwrap_err(); + assert_eq!(err.prior, owner_a); + assert_eq!(err.new, owner_b); } #[test] fn register_same_owner_updates_path() { let reg = MetadataArtifactRegistry::new(); - reg.register(&base(), "m", "_base", "config.json", PathBuf::from("/a")); - reg.register(&base(), "m", "_base", "config.json", PathBuf::from("/b")); + reg.register( + &base(), + "ns", + "comp", + "ep", + "m", + "_base", + "config.json", + PathBuf::from("/a"), + ) + .unwrap(); + reg.register( + &base(), + "ns", + "comp", + "ep", + "m", + "_base", + "config.json", + PathBuf::from("/b"), + ) + .unwrap(); + assert_eq!( + reg.get("ns", "comp", "ep", "m", "_base", "config.json"), + Some(PathBuf::from("/b")) + ); + } + + #[test] + fn different_endpoints_coexist() { + let reg = MetadataArtifactRegistry::new(); + let owner_a = (1, None); + let owner_b = (2, None); + reg.register( + &owner_a, + "ns", + "comp", + "ep-a", + "m", + "_base", + "config.json", + PathBuf::from("/a"), + ) + .unwrap(); + // Same (slug, suffix, filename) but different endpoint → no collision. + reg.register( + &owner_b, + "ns", + "comp", + "ep-b", + "m", + "_base", + "config.json", + PathBuf::from("/b"), + ) + .unwrap(); + assert_eq!( + reg.get("ns", "comp", "ep-a", "m", "_base", "config.json"), + Some(PathBuf::from("/a")) + ); assert_eq!( - reg.get("m", "_base", "config.json"), + reg.get("ns", "comp", "ep-b", "m", "_base", "config.json"), Some(PathBuf::from("/b")) ); } diff --git a/lib/runtime/src/system_status_server.rs b/lib/runtime/src/system_status_server.rs index f0c6db7dcc66..6692700d70e8 100644 --- a/lib/runtime/src/system_status_server.rs +++ b/lib/runtime/src/system_status_server.rs @@ -217,10 +217,11 @@ pub async fn spawn_system_status_server( } // Self-hosted MDC files. Always mounted; empty registry → 404. - // Suffix segment scopes per-registration (LoRA slug, or `_base`) - // so detaching one registration doesn't wipe another's entries. + // The endpoint triple disambiguates multi-LocalModel-per-DRT; the + // suffix segment (LoRA slug or `_base`) scopes per-registration so + // detaching one doesn't wipe another's entries. app = app.route( - "/v1/metadata/{model_slug}/{model_suffix}/{*filename}", + "/v1/metadata/{namespace}/{component}/{endpoint}/{model_slug}/{model_suffix}/{*filename}", get({ let state = Arc::clone(&server_state); move |path| metadata_file_handler(State(state), path) @@ -487,20 +488,33 @@ async fn list_loras_handler(State(state): State>) -> impl } } -/// `GET /v1/metadata/{slug}/{suffix}/{filename}` — 404 on miss, -/// 500 on read error, raw bytes on hit. Consumer blake3-verifies. +/// `GET /v1/metadata/{namespace}/{component}/{endpoint}/{slug}/{suffix}/{filename}` +/// — 404 on miss, 500 on read error, raw bytes on hit. Consumer blake3-verifies. async fn metadata_file_handler( State(state): State>, - Path((model_slug, model_suffix, filename)): Path<(String, String, String)>, + Path((namespace, component, endpoint, model_slug, model_suffix, filename)): Path<( + String, + String, + String, + String, + String, + String, + )>, ) -> impl IntoResponse { - let path = match state - .drt() - .metadata_artifacts() - .get(&model_slug, &model_suffix, &filename) - { + let path = match state.drt().metadata_artifacts().get( + &namespace, + &component, + &endpoint, + &model_slug, + &model_suffix, + &filename, + ) { Some(p) => p, None => { tracing::debug!( + namespace, + component, + endpoint, model_slug, model_suffix, filename, @@ -514,6 +528,9 @@ async fn metadata_file_handler( Ok(bytes) => (StatusCode::OK, bytes).into_response(), Err(err) => { tracing::error!( + namespace, + component, + endpoint, model_slug, model_suffix, filename, From 534bdcbe2a8d8c6640ec3330fb7b8fcd72beb36e Mon Sep 17 00:00:00 2001 From: nnshah1 Date: Thu, 25 Jun 2026 15:05:09 -0700 Subject: [PATCH 7/9] fix(metadata-registry): clippy warnings + e2e test URL for new endpoint-triple key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CI on 4a74346c0d surfaced: 1. `clippy::result_large_err` — CollisionError is 208 bytes; wrap in Box so the Err variant fits the lint's size threshold. Tests still work because Box derefs to T transparently for .prior/.new field access, and Box impls Error for anyhow's .context()? chain. 2. `clippy::collapsible_if` — combine the nested if-let with && via the let-chains syntax (stable on the toolchain CI uses). 3. CodeRabbit caught: tests/frontend/test_self_host_metadata.py still used the old /v1/metadata/{slug}/{suffix}/{filename} URL. Updated to the 6-tuple shape /v1/metadata/{ns}/{comp}/{ep}/{slug}/{suffix}/{filename} matching MockerWorkerProcess defaults (ns=dynamo, comp=sample, ep=generate). Verified locally: cargo clippy clean, all 5 metadata_registry tests pass. Signed-off-by: nnshah1 --- lib/runtime/src/metadata_registry.rs | 33 +++++++++++++---------- tests/frontend/test_self_host_metadata.py | 16 ++++++----- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/lib/runtime/src/metadata_registry.rs b/lib/runtime/src/metadata_registry.rs index 29c21e6dd9d6..41bba76f7475 100644 --- a/lib/runtime/src/metadata_registry.rs +++ b/lib/runtime/src/metadata_registry.rs @@ -41,9 +41,7 @@ type Key = (String, String, String, String, String, String); /// Registration collided with a different owner — programmer error. #[derive(Debug, Error)] -#[error( - "metadata-registry collision on key {key:?}: prior_owner={prior:?}, new_owner={new:?}" -)] +#[error("metadata-registry collision on key {key:?}: prior_owner={prior:?}, new_owner={new:?}")] pub struct CollisionError { pub key: Key, pub prior: Owner, @@ -74,7 +72,7 @@ impl MetadataArtifactRegistry { suffix: &str, filename: &str, path: PathBuf, - ) -> Result<(), CollisionError> { + ) -> Result<(), Box> { let key = ( namespace.to_string(), component.to_string(), @@ -84,14 +82,14 @@ impl MetadataArtifactRegistry { filename.to_string(), ); let mut entries = self.entries.write(); - if let Some((_, prior)) = entries.get(&key) { - if prior != owner { - return Err(CollisionError { - key, - prior: prior.clone(), - new: owner.clone(), - }); - } + if let Some((_, prior)) = entries.get(&key) + && prior != owner + { + return Err(Box::new(CollisionError { + key, + prior: prior.clone(), + new: owner.clone(), + })); } entries.insert(key, (path, owner.clone())); tracing::debug!( @@ -179,8 +177,15 @@ mod tests { .is_none() ); assert!( - reg.get("ns", "comp", "ep", "llama-3-8b", "lora-v1", "tokenizer.json") - .is_none() + reg.get( + "ns", + "comp", + "ep", + "llama-3-8b", + "lora-v1", + "tokenizer.json" + ) + .is_none() ); } diff --git a/tests/frontend/test_self_host_metadata.py b/tests/frontend/test_self_host_metadata.py index f391af144434..d5535977e08d 100644 --- a/tests/frontend/test_self_host_metadata.py +++ b/tests/frontend/test_self_host_metadata.py @@ -5,7 +5,8 @@ Single-worker check: opt the mocker into self-host via `DYN_SELF_HOST_METADATA=true`, then `requests.get` the -`/v1/metadata/{slug}/{suffix}/{filename}` route on the worker's own +`/v1/metadata/{namespace}/{component}/{endpoint}/{slug}/{suffix}/{filename}` +route on the worker's own `system_status_server`. No frontend resolution involved — this only exercises the worker producer + HTTP route shipped in PR1 (#8855). @@ -72,8 +73,13 @@ def test_worker_serves_metadata_via_http( slug = _slugify(TEST_MODEL) suffix = "_base" # BASE_SUFFIX in lib/runtime/src/metadata_registry.rs + # MockerWorkerProcess defaults: --component=sample, the rest pick up + # WorkerConfig defaults from lib/runtime — namespace=dynamo, + # endpoint=generate. The route is now keyed on the endpoint triple + # so the URL includes (namespace, component, endpoint). + base = f"http://localhost:{system_port}/v1/metadata/dynamo/sample/generate" - url = f"http://localhost:{system_port}/v1/metadata/{slug}/{suffix}/config.json" + url = f"{base}/{slug}/{suffix}/config.json" response = requests.get(url, timeout=10) assert ( response.status_code == 200 @@ -87,7 +93,7 @@ def test_worker_serves_metadata_via_http( # Unknown filename under the same (slug, suffix) → 404. miss = requests.get( - f"http://localhost:{system_port}/v1/metadata/{slug}/{suffix}/missing.json", + f"{base}/{slug}/{suffix}/missing.json", timeout=5, ) assert miss.status_code == 404, miss.text @@ -96,9 +102,7 @@ def test_worker_serves_metadata_via_http( # Qwen3 HF snapshot (`vocab.json`) must also be served. Doubles # as a symlink-follow check — HF snapshot entries are symlinks # into `blobs/`, so a non-following stat would miss this file. - extra_url = ( - f"http://localhost:{system_port}/v1/metadata/{slug}/{suffix}/vocab.json" - ) + extra_url = f"{base}/{slug}/{suffix}/vocab.json" extra_response = requests.get(extra_url, timeout=10) assert ( extra_response.status_code == 200 From 55b3fef371d34a7633ddb46a1d7f6ecf80b2aa2f Mon Sep 17 00:00:00 2001 From: nnshah1 Date: Thu, 25 Jun 2026 15:12:17 -0700 Subject: [PATCH 8/9] refactor(metadata-registry): extract make_key helper for the 6-tuple key Per graham-code-review pass on 534bdcbe2a: the 6-tuple key is now constructed by hand in two places (`register` and `get`), 7 lines each. A `make_key` helper compresses both call sites to one line, makes the key shape a single source of truth, and removes the visual ceremony that pushed the user to suggest it. Tradeoff revisited from the earlier 3-tuple revert: at 3 fields the helper was indirection; at 6 fields the per-call construction is genuinely noise. `#[allow(clippy::too_many_arguments)]` retained on the helper itself since the field set is the contract. Verified: cargo clippy clean, 5 metadata_registry tests pass, fmt OK. Signed-off-by: nnshah1 --- lib/runtime/src/metadata_registry.rs | 37 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/runtime/src/metadata_registry.rs b/lib/runtime/src/metadata_registry.rs index 41bba76f7475..051a41d59cde 100644 --- a/lib/runtime/src/metadata_registry.rs +++ b/lib/runtime/src/metadata_registry.rs @@ -39,6 +39,25 @@ pub type Owner = (u64, Option); /// `(namespace, component, endpoint, slug, suffix, filename)`. type Key = (String, String, String, String, String, String); +#[allow(clippy::too_many_arguments)] +fn make_key( + namespace: &str, + component: &str, + endpoint: &str, + slug: &str, + suffix: &str, + filename: &str, +) -> Key { + ( + namespace.to_string(), + component.to_string(), + endpoint.to_string(), + slug.to_string(), + suffix.to_string(), + filename.to_string(), + ) +} + /// Registration collided with a different owner — programmer error. #[derive(Debug, Error)] #[error("metadata-registry collision on key {key:?}: prior_owner={prior:?}, new_owner={new:?}")] @@ -73,14 +92,7 @@ impl MetadataArtifactRegistry { filename: &str, path: PathBuf, ) -> Result<(), Box> { - let key = ( - namespace.to_string(), - component.to_string(), - endpoint.to_string(), - slug.to_string(), - suffix.to_string(), - filename.to_string(), - ); + let key = make_key(namespace, component, endpoint, slug, suffix, filename); let mut entries = self.entries.write(); if let Some((_, prior)) = entries.get(&key) && prior != owner @@ -114,14 +126,7 @@ impl MetadataArtifactRegistry { suffix: &str, filename: &str, ) -> Option { - let key = ( - namespace.to_string(), - component.to_string(), - endpoint.to_string(), - slug.to_string(), - suffix.to_string(), - filename.to_string(), - ); + let key = make_key(namespace, component, endpoint, slug, suffix, filename); self.entries.read().get(&key).map(|(p, _)| p.clone()) } From 169b22413db0768e331b70baaee1936ceadda682 Mon Sep 17 00:00:00 2001 From: nnshah1 Date: Thu, 25 Jun 2026 23:52:34 -0700 Subject: [PATCH 9/9] chore: kick DCO app Empty commit to nudge the DCO app to re-check; the previous check appears stuck in 'not started' on `55b3fef371` despite all required signoffs being present. Signed-off-by: nnshah1