From a86f681298c3d0a062a086880de1e3a8176a7776 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Wed, 4 Nov 2026 16:00:26 -0600 Subject: [PATCH 1/4] docs(id): record dynamic publication design for #2147 --- ...6Z_feature_dynamic_publications_runtime.md | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 doc/2026-11-04T21-50-26Z_feature_dynamic_publications_runtime/2026-11-04T21-50-26Z_feature_dynamic_publications_runtime.md diff --git a/doc/2026-11-04T21-50-26Z_feature_dynamic_publications_runtime/2026-11-04T21-50-26Z_feature_dynamic_publications_runtime.md b/doc/2026-11-04T21-50-26Z_feature_dynamic_publications_runtime/2026-11-04T21-50-26Z_feature_dynamic_publications_runtime.md new file mode 100644 index 0000000000..cf51239958 --- /dev/null +++ b/doc/2026-11-04T21-50-26Z_feature_dynamic_publications_runtime/2026-11-04T21-50-26Z_feature_dynamic_publications_runtime.md @@ -0,0 +1,57 @@ +# Dynamic publication runtime apply and operator CLI + +**Status:** initial design; settle the open decisions below against the existing serve and protocol architecture before source implementation. + +## Request and intent + +Issue [#2147](https://github.com/developing-today/code/issues/2147) requires operators to publish and unpublish programs while `id serve` is running, without restarting it. A publication may use a dedicated listener (port/interface) or a path on an Explorer listener. The set must persist across restarts and be inspectable through the CLI. An acknowledged path mount may override an Explorer route; an unacknowledged shadow must be rejected or warned about as specified by the issue. + +The acceptance walk is: publish a program on a new port and reach it; publish a path mount that shadows an Explorer route and observe the warning and override; unpublish both and verify the listener closes and the Explorer route returns, all while the same serve process remains alive. + +## Baseline and scope + +PR [#5114](https://github.com/developing-today/code/pull/5114) merged slice 1: the persisted `Registry`, validation, boot restore, and shadow warnings. The remaining work is runtime application of that registry and CLI publish/unpublish/list operations. Do not redesign the slice-1 registry model beyond fields needed for runtime application; do not add unrelated refactors. + +`Registry` is intentionally socket-free. `cmd_serve` currently owns configured listener tasks and routers, while CLI clients use the meta protocol and `MetaClient::open()` can fall back to local stores when no serve process is running. A publication command must not report success via that fallback without changing the running server. + +## Initial design direction + +1. Keep the persisted registry as the desired publication set. A serve-owned runtime component should own live listener handles and the dynamic path-routing state for the lifetime of `cmd_serve`. +2. Apply the persisted set during startup and apply mutations through the same runtime path. Preserve each listener's existing `WebSecurity` boundary. A failed validation, bind, or persistence operation must not leave the CLI reporting success with live and durable state disagreeing. +3. Make publish, unpublish, and list CLI commands address a running serve control surface. They should use the same registry and apply operations as startup, rather than editing the JSON file independently. If no running server is available, fail with an actionable error rather than falling back to a local-store operation. +4. Keep the existing shadow acknowledgement semantics: reject unacknowledged shadowing, surface the warning for acknowledged shadowing, and restore the Explorer route when the path mount is removed. + +These are design constraints, not yet a settled transport or transaction protocol. The next step is to choose those details based on the repository's listener, router, and meta-protocol lifecycle. + +## Decisions to settle before source changes + +- **Control channel and authorization:** The existing meta protocol has authenticated peer identities and `AccessPolicy`, but `--open-writes` can authorize every peer to mutate the store. Publication control changes network listeners and routes, so decide whether it needs a narrower operator-only authorization or a local-only control channel. Do not silently treat ordinary store write access as sufficient without recording the security rationale. +- **Explorer target for path mounts:** `PathMount` stores only a path and calls the target the primary Explorer listener, but serve can have multiple Explorer listeners. Define which listener receives a mount and how ambiguity is reported. +- **Configured/dynamic listener conflicts:** Define duplicate-address behavior and how port-0 publications report the actual OS-assigned address in `list` and after restart. +- **Apply/persistence ordering:** Define how publish/unpublish stage runtime changes and atomic registry writes so a bind or save failure leaves a recoverable, non-success state. +- **Target semantics:** Confirm that “programs and pages” in #2147 means the world-backed program surface already represented by `Publication::world`, not arbitrary static pages. + +## Verification plan + +- Unit tests for desired-to-live apply behavior, duplicate/conflicting placements, rollback on failed bind/save, route shadowing and restoration, and actual listener address reporting. +- Protocol/CLI tests for request parsing, authorization, no-server behavior, and any append-only wire-format compatibility requirement. +- Integration acceptance using the existing serve harness: add a listener and fetch the program; mount an acknowledged shadow and observe the warning/override; unpublish both and verify route restoration and listener closure without restarting the process; restart and verify persisted publications restore. +- Run focused `pkgs/id` tests locally and inspect the PR's actual basic-tier checks before deciding merge readiness. + +## 2026-11-04T21-56-00Z Decision: control and apply boundaries + +The implementation will use the existing meta-protocol connection for CLI-to-serve control, but publication requests are **operator-only**, not ordinary store writes. The server will authorize these requests only when the authenticated caller identity is the serving node's own identity; this remains true with `--open-writes` and `--allow-node`. Local publication commands will explicitly connect to the running serve process using its local identity and will fail if no serve process is available. They will not use `MetaClient::open()`'s local-store fallback. This keeps remotely authorized file writers from opening network listeners or changing routes. + +The serve process will serialize publication mutations through one manager that owns the registry and active dynamic resources. A listener publish will validate and bind a staged listener, atomically save the candidate registry, then activate the staged listener. A save failure closes the staged listener without changing the active set. Unpublish will atomically save the candidate set before removing the live route or stopping the listener; those in-process removals are non-failing, and a process crash after the save naturally restarts from the new set. The CLI responds success only after persistence and live application both finish. + +Because `PathMount` has no listener identity, it targets the first effective Explorer binding (the primary Explorer listener); it fails if no Explorer listener exists. With port `0`, the registry retains the requested ephemeral address so restart may choose a new port; `list` reports the manager's actual bound address while the process is running. “Programs and pages” means the world-backed program surface already represented by `Publication::world`, not arbitrary static content. + +These choices fit the current `MetaProtocol`/`AccessPolicy`, `cmd_serve` listener ownership, and the slice-1 `Registry` model. The key trade-off is that commands require local access to the serving identity key; this is preferred over widening the existing remote writer policy. The remaining source changes must preserve each listener's `WebSecurity` and use the same shadow-warning validation for runtime and boot application. + +## References + +- [Issue #2147](https://github.com/developing-today/code/issues/2147) +- [Slice 1 PR #5114](https://github.com/developing-today/code/pull/5114) +- `pkgs/id/src/publications.rs` +- `pkgs/id/src/commands/serve.rs` +- `pkgs/id/src/protocol.rs` and `pkgs/id/src/meta_client.rs` From 94a3edb443f8ff17abe91ec9f8f5013e7c6903fb Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 18:58:58 -0600 Subject: [PATCH 2/4] id(serve): runtime apply manager for dynamic publications (#2147 slices 2-3 core) --- pkgs/id/src/lib.rs | 1 + pkgs/id/src/publications_runtime.rs | 636 ++++++++++++++++++++++++++++ 2 files changed, 637 insertions(+) create mode 100644 pkgs/id/src/publications_runtime.rs diff --git a/pkgs/id/src/lib.rs b/pkgs/id/src/lib.rs index f9368ba5ef..fb4593c735 100644 --- a/pkgs/id/src/lib.rs +++ b/pkgs/id/src/lib.rs @@ -221,6 +221,7 @@ pub mod notes; pub use id_core::petname; pub mod protocol; pub mod publications; +pub mod publications_runtime; pub mod pull_policy; pub use id_core::residency; pub mod references; diff --git a/pkgs/id/src/publications_runtime.rs b/pkgs/id/src/publications_runtime.rs new file mode 100644 index 0000000000..4faebda595 --- /dev/null +++ b/pkgs/id/src/publications_runtime.rs @@ -0,0 +1,636 @@ +//! Runtime apply for dynamic publications (#2147, slices 2–3: the +//! serve-owned manager core). +//! +//! Slice 1 ([`crate::publications`]) is the persisted registry — the +//! desired set, deliberately socket-free. This module is the runtime +//! half the design record for #2147 specifies +//! (`doc/2026-11-04T21-50-26Z_feature_dynamic_publications_runtime`): +//! ONE manager owns the registry and the live resources it maps to, +//! serializes every mutation through the same apply path startup +//! uses, and never lets the durable set and the live set disagree +//! silently. +//! +//! The transactional ordering is the design's 2026-11-04 decision: +//! +//! - **Publish (listener):** validate against a candidate registry, +//! bind a *staged* listener, atomically save the candidate +//! registry, then activate the staged listener. A bind failure +//! changes nothing; a save failure closes the staged listener +//! without changing the active set. +//! - **Publish (path mount):** validate, save the candidate, then +//! mount on the primary Explorer listener. Mounts target the +//! first effective Explorer binding; with no Explorer listener +//! the publish fails before anything is saved. +//! - **Unpublish:** atomically save the candidate set *before* +//! removing the live route or stopping the listener — those +//! in-process removals are non-failing, and a crash after the +//! save restarts from the new set, so the order is safe. +//! - **Restore (boot):** apply the persisted set through the same +//! resource path; a failure part-way rolls back what was applied +//! and fails loudly, matching slice 1's fail-loud load contract. +//! +//! The manager is generic over the two resource seams +//! ([`ListenerFactory`] and [`MountSink`]) so the apply logic is +//! testable without sockets: `cmd_serve` supplies the real factory +//! (its listener tasks, preserving each listener's `WebSecurity`) +//! and the real Explorer mount sink. Wiring those adapters, the +//! operator-only meta-protocol control surface, and the CLI +//! publish/unpublish/list commands is the remaining slice-3 work; +//! the CLI must drive THIS manager (never edit the registry file +//! independently, and never fall back to a local-store operation +//! when no serve process is running — the design's control +//! decision). + +use std::collections::BTreeMap; +use std::net::SocketAddr; +use std::path::PathBuf; + +use anyhow::{Context, Result, ensure}; + +use crate::publications::{Placement, Publication, Registry}; + +/// A live listener opened for a [`Placement::Listener`] publication. +pub trait LiveListener: Send { + /// The address the listener is actually bound to. For a + /// requested port of 0 this is the OS-assigned address, which + /// is what `list` reports while the process runs (the registry + /// keeps the requested address, per the design decision). + fn actual_addr(&self) -> SocketAddr; + + /// Stop the listener and release the address. + fn close(self: Box); +} + +/// Opens live listeners for listener placements. Implemented by +/// `cmd_serve` over its real listener tasks. +pub trait ListenerFactory: Send { + /// Bind a listener serving `world` at `addr`, or fail without + /// side effects. + fn bind(&mut self, world: &str, addr: SocketAddr) -> Result>; +} + +/// Applies path mounts to the primary Explorer listener. +pub trait MountSink: Send { + /// Whether an Explorer listener exists to mount on at all. + fn has_explorer(&self) -> bool; + + /// Mount `world` at `path` on the Explorer listener, so the + /// publication overrides the Explorer there. + fn mount(&mut self, world: &str, path: &str) -> Result<()>; + + /// Remove the mount at `path`, restoring the Explorer route. + fn unmount(&mut self, path: &str) -> Result<()>; +} + +/// One publication as `list` reports it: the persisted record plus +/// the live address for listener placements. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PublicationStatus { + /// The persisted publication. + pub publication: Publication, + /// The live bound address for a listener placement (the + /// requested address is in the publication; for port 0 the two + /// differ). `None` for path mounts and for a listener that is + /// not currently live. + pub actual_addr: Option, +} + +/// The serve-owned publication manager: the persisted registry +/// plus every live resource it maps to. +pub struct PublicationManager { + registry: Registry, + registry_path: PathBuf, + explorer_routes: Vec, + factory: F, + mounts: M, + listeners: BTreeMap>, + mounted: Vec, +} + +impl PublicationManager { + /// A manager over an already-loaded `registry` (slice 1's boot + /// load). No live resources exist until [`Self::restore`] (or a + /// publish) applies them. + pub fn new( + registry: Registry, + registry_path: PathBuf, + explorer_routes: Vec, + factory: F, + mounts: M, + ) -> Self { + Self { + registry, + registry_path, + explorer_routes, + factory, + mounts, + listeners: BTreeMap::new(), + mounted: Vec::new(), + } + } + + /// The persisted (desired) set. + #[must_use] + pub fn registry(&self) -> &Registry { + &self.registry + } + + fn routes(&self) -> Vec<&str> { + self.explorer_routes.iter().map(String::as_str).collect() + } + + /// Apply the persisted set at startup, through the same + /// resource path as runtime publishes. Returns the boot shadow + /// warnings (slice 1's [`Registry::boot_warnings`]). + /// + /// # Errors + /// + /// Fails when any persisted publication cannot be applied (a + /// listener that will not bind, a path mount with no Explorer + /// listener); everything applied so far is rolled back first, + /// so a failed restore leaves no half-applied set behind. + pub fn restore(&mut self) -> Result> { + let warnings = self.registry.boot_warnings(&self.routes()); + let persisted = self.registry.publications.clone(); + for publication in &persisted { + if let Err(error) = self.activate(publication) { + self.rollback_all(); + return Err(error).context(format!( + "restoring publication of world '{}' at {}", + publication.world, + publication.placement.describe() + )); + } + } + Ok(warnings) + } + + /// Publish `publication`: validate, stage, persist, activate — + /// in that order (see the module docs). Returns the loud shadow + /// warning for an acknowledged shadowing mount. + /// + /// # Errors + /// + /// Fails on any validation refusal from the registry (taken + /// placement, unacknowledged shadow), a bind failure, a mount + /// with no Explorer listener, or a persistence failure. On + /// every failure the live and persisted sets are unchanged. + pub fn publish(&mut self, publication: Publication) -> Result> { + let mut candidate = self.registry.clone(); + let warning = candidate.publish(publication.clone(), &self.routes())?; + + // Stage the live resource before persisting, so a bind + // failure changes nothing at all. + let staged = match &publication.placement { + Placement::Listener { addr } => Some(self.factory.bind(&publication.world, *addr)?), + Placement::PathMount { .. } => { + ensure!( + self.mounts.has_explorer(), + "cannot publish a path mount: no Explorer listener is running \ + (path mounts target the primary Explorer listener)" + ); + None + } + }; + + if let Err(error) = candidate.save(&self.registry_path) { + // Save failed: close the staged listener without + // changing the active set. + if let Some(listener) = staged { + listener.close(); + } + return Err(error).context("persisting the publications registry"); + } + self.registry = candidate; + + match &publication.placement { + Placement::Listener { addr } => { + let listener = staged.expect("listener placements stage a listener"); + self.listeners.insert(*addr, listener); + } + Placement::PathMount { path } => { + if let Err(error) = self.mounts.mount(&publication.world, path) { + // The registry is already persisted with the + // mount in it; revert the persisted set so the + // two never silently disagree, and say so. + let mut reverted = self.registry.clone(); + let _ = reverted.unpublish(&publication.placement); + let _ = reverted.save(&self.registry_path); + self.registry = reverted; + return Err(error).context("applying the path mount after persisting it"); + } + self.mounted.push(path.clone()); + } + } + Ok(warning) + } + + /// Unpublish the publication at `placement`: persist the + /// candidate set first, then remove the live resource. + /// + /// # Errors + /// + /// Fails when nothing is published at `placement` or the + /// candidate set cannot be persisted (the live set is then + /// untouched). + pub fn unpublish(&mut self, placement: &Placement) -> Result { + let mut candidate = self.registry.clone(); + let removed = candidate.unpublish(placement)?; + candidate + .save(&self.registry_path) + .context("persisting the publications registry")?; + self.registry = candidate; + + match placement { + Placement::Listener { addr } => { + if let Some(listener) = self.listeners.remove(addr) { + listener.close(); + } + } + Placement::PathMount { path } => { + let canonical = crate::publications::canonicalize_mount_path(path)?; + if self.mounted.iter().any(|mounted| mounted == &canonical) { + self.mounts.unmount(&canonical)?; + self.mounted.retain(|mounted| mounted != &canonical); + } + } + } + Ok(removed) + } + + /// The current set as an operator listing shows it: persisted + /// records in publish order, listener placements annotated + /// with their live bound address. + #[must_use] + pub fn list(&self) -> Vec { + self.registry + .publications + .iter() + .map(|publication| PublicationStatus { + publication: publication.clone(), + actual_addr: match &publication.placement { + Placement::Listener { addr } => { + self.listeners.get(addr).map(|live| live.actual_addr()) + } + Placement::PathMount { .. } => None, + }, + }) + .collect() + } + + /// Activate one already-persisted publication's live resource + /// (restore's inner step; publish stages its own). + fn activate(&mut self, publication: &Publication) -> Result<()> { + match &publication.placement { + Placement::Listener { addr } => { + let listener = self.factory.bind(&publication.world, *addr)?; + self.listeners.insert(*addr, listener); + Ok(()) + } + Placement::PathMount { path } => { + ensure!( + self.mounts.has_explorer(), + "cannot restore a path mount: no Explorer listener is running" + ); + self.mounts.mount(&publication.world, path)?; + self.mounted.push(path.clone()); + Ok(()) + } + } + } + + fn rollback_all(&mut self) { + for (_, listener) in std::mem::take(&mut self.listeners) { + listener.close(); + } + for path in std::mem::take(&mut self.mounted) { + let _ = self.mounts.unmount(&path); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::publications::KNOWN_EXPLORER_ROUTES; + use std::sync::{Arc, Mutex}; + + #[derive(Default)] + struct FakeState { + next_port: u16, + fail_addrs: Vec, + closed: Vec, + mounted: Vec, + unmounted: Vec, + } + + struct FakeListener { + actual: SocketAddr, + state: Arc>, + } + + impl LiveListener for FakeListener { + fn actual_addr(&self) -> SocketAddr { + self.actual + } + + fn close(self: Box) { + self.state.lock().unwrap().closed.push(self.actual); + } + } + + struct FakeFactory { + state: Arc>, + } + + impl ListenerFactory for FakeFactory { + fn bind(&mut self, _world: &str, addr: SocketAddr) -> Result> { + let mut state = self.state.lock().unwrap(); + ensure!( + !state.fail_addrs.contains(&addr), + "address {addr} is taken (fake)" + ); + let actual = if addr.port() == 0 { + state.next_port += 1; + SocketAddr::new(addr.ip(), 40_000 + state.next_port) + } else { + addr + }; + Ok(Box::new(FakeListener { + actual, + state: self.state.clone(), + })) + } + } + + struct FakeMounts { + state: Arc>, + explorer: bool, + } + + impl MountSink for FakeMounts { + fn has_explorer(&self) -> bool { + self.explorer + } + + fn mount(&mut self, _world: &str, path: &str) -> Result<()> { + self.state.lock().unwrap().mounted.push(path.to_string()); + Ok(()) + } + + fn unmount(&mut self, path: &str) -> Result<()> { + self.state.lock().unwrap().unmounted.push(path.to_string()); + Ok(()) + } + } + + fn listener_pub(addr: &str) -> Publication { + Publication { + world: "arena".to_string(), + placement: Placement::Listener { + addr: addr.parse().unwrap(), + }, + acknowledge_shadow: false, + } + } + + fn mount_pub(path: &str, acknowledge_shadow: bool) -> Publication { + Publication { + world: "arena".to_string(), + placement: Placement::PathMount { + path: path.to_string(), + }, + acknowledge_shadow, + } + } + + struct Fixture { + dir: PathBuf, + file: PathBuf, + state: Arc>, + } + + impl Fixture { + fn new(name: &str) -> Self { + let dir = std::env::temp_dir().join(format!( + "id-publications-runtime-test-{}-{name}", + std::process::id() + )); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("publications.json"); + Self { + dir, + file, + state: Arc::new(Mutex::new(FakeState::default())), + } + } + + fn manager( + &self, + registry: Registry, + explorer: bool, + ) -> PublicationManager { + PublicationManager::new( + registry, + self.file.clone(), + KNOWN_EXPLORER_ROUTES + .iter() + .map(|route| (*route).to_string()) + .collect(), + FakeFactory { + state: self.state.clone(), + }, + FakeMounts { + state: self.state.clone(), + explorer, + }, + ) + } + + fn cleanup(&self) { + let _ = std::fs::remove_dir_all(&self.dir); + } + } + + #[test] + fn restore_applies_the_persisted_set_and_reports_actual_addresses() { + let fx = Fixture::new("restore"); + let mut registry = Registry::default(); + registry + .publish(listener_pub("127.0.0.1:0"), KNOWN_EXPLORER_ROUTES) + .unwrap(); + registry + .publish(mount_pub("/arena", false), KNOWN_EXPLORER_ROUTES) + .unwrap(); + let mut manager = fx.manager(registry, true); + let warnings = manager.restore().unwrap(); + assert!(warnings.is_empty()); + let listed = manager.list(); + assert_eq!(listed.len(), 2); + let actual = listed[0].actual_addr.expect("listener is live"); + assert_ne!(actual.port(), 0, "list reports the OS-assigned port"); + assert_eq!(fx.state.lock().unwrap().mounted, vec!["/arena".to_string()]); + fx.cleanup(); + } + + #[test] + fn restore_rolls_back_when_a_bind_fails() { + let fx = Fixture::new("restore-rollback"); + fx.state + .lock() + .unwrap() + .fail_addrs + .push("127.0.0.1:4002".parse().unwrap()); + let mut registry = Registry::default(); + registry + .publish(listener_pub("127.0.0.1:4001"), KNOWN_EXPLORER_ROUTES) + .unwrap(); + registry + .publish(listener_pub("127.0.0.1:4002"), KNOWN_EXPLORER_ROUTES) + .unwrap(); + let mut manager = fx.manager(registry, true); + assert!(manager.restore().is_err()); + // The first listener was applied, then rolled back. + assert_eq!( + fx.state.lock().unwrap().closed, + vec!["127.0.0.1:4001".parse::().unwrap()] + ); + assert!(manager.list().iter().all(|s| s.actual_addr.is_none())); + fx.cleanup(); + } + + #[test] + fn publish_listener_persists_and_reports_the_actual_address() { + let fx = Fixture::new("publish-listener"); + let mut manager = fx.manager(Registry::default(), true); + let warning = manager.publish(listener_pub("127.0.0.1:0")).unwrap(); + assert!(warning.is_none()); + let listed = manager.list(); + assert_eq!(listed.len(), 1); + assert_ne!(listed[0].actual_addr.unwrap().port(), 0); + // The persisted registry keeps the REQUESTED address. + let on_disk = Registry::load(&fx.file).unwrap(); + assert_eq!(on_disk, *manager.registry()); + assert_eq!( + on_disk.publications[0].placement, + Placement::Listener { + addr: "127.0.0.1:0".parse().unwrap() + } + ); + fx.cleanup(); + } + + #[test] + fn publish_bind_failure_changes_nothing() { + let fx = Fixture::new("publish-bind-fails"); + fx.state + .lock() + .unwrap() + .fail_addrs + .push("127.0.0.1:4002".parse().unwrap()); + let mut manager = fx.manager(Registry::default(), true); + assert!(manager.publish(listener_pub("127.0.0.1:4002")).is_err()); + assert!(manager.registry().publications.is_empty()); + assert!(!fx.file.exists(), "nothing was persisted"); + fx.cleanup(); + } + + #[test] + fn publish_save_failure_closes_the_staged_listener() { + let fx = Fixture::new("publish-save-fails"); + // A registry path whose parent is a FILE: save's temp write + // fails, after the staged bind succeeded. + let blocker = fx.dir.join("blocker"); + std::fs::write(&blocker, "not a directory").unwrap(); + let mut manager = PublicationManager::new( + Registry::default(), + blocker.join("publications.json"), + KNOWN_EXPLORER_ROUTES + .iter() + .map(|route| (*route).to_string()) + .collect(), + FakeFactory { + state: fx.state.clone(), + }, + FakeMounts { + state: fx.state.clone(), + explorer: true, + }, + ); + assert!(manager.publish(listener_pub("127.0.0.1:4001")).is_err()); + assert!(manager.registry().publications.is_empty()); + assert_eq!( + fx.state.lock().unwrap().closed, + vec!["127.0.0.1:4001".parse::().unwrap()], + "the staged listener was closed, not activated" + ); + fx.cleanup(); + } + + #[test] + fn publish_mount_shadow_gate_and_explorer_requirement() { + let fx = Fixture::new("publish-mount"); + let mut manager = fx.manager(Registry::default(), true); + // Unacknowledged shadow: refused by the registry gate, + // nothing mounted, nothing persisted. + assert!(manager.publish(mount_pub("/world", false)).is_err()); + assert!(fx.state.lock().unwrap().mounted.is_empty()); + // Acknowledged: warns loudly and mounts. + let warning = manager.publish(mount_pub("/world", true)).unwrap(); + assert!( + warning + .expect("acknowledged shadow warns") + .contains("/world") + ); + assert_eq!(fx.state.lock().unwrap().mounted, vec!["/world".to_string()]); + + // No Explorer listener: the publish fails before saving. + let fx2 = Fixture::new("publish-mount-no-explorer"); + let mut manager2 = fx2.manager(Registry::default(), false); + assert!(manager2.publish(mount_pub("/arena", false)).is_err()); + assert!(manager2.registry().publications.is_empty()); + assert!(!fx2.file.exists()); + fx.cleanup(); + fx2.cleanup(); + } + + #[test] + fn unpublish_reverts_live_and_persisted_state() { + let fx = Fixture::new("unpublish"); + let mut manager = fx.manager(Registry::default(), true); + manager.publish(listener_pub("127.0.0.1:4001")).unwrap(); + manager.publish(mount_pub("/arena", false)).unwrap(); + + let removed = manager + .unpublish(&Placement::Listener { + addr: "127.0.0.1:4001".parse().unwrap(), + }) + .unwrap(); + assert_eq!(removed.world, "arena"); + assert_eq!( + fx.state.lock().unwrap().closed, + vec!["127.0.0.1:4001".parse::().unwrap()] + ); + manager + .unpublish(&Placement::PathMount { + path: "/arena".to_string(), + }) + .unwrap(); + assert_eq!( + fx.state.lock().unwrap().unmounted, + vec!["/arena".to_string()] + ); + assert!(manager.registry().publications.is_empty()); + assert_eq!(Registry::load(&fx.file).unwrap().publications.len(), 0); + // A second unpublish of the same placement is an error. + assert!( + manager + .unpublish(&Placement::PathMount { + path: "/arena".to_string(), + }) + .is_err() + ); + fx.cleanup(); + } +} From 4eb4e9c9b58c0c60359cd1f49cb85c9c4eed97ff Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 20:10:28 -0600 Subject: [PATCH 3/4] fix(id): keep registry and live mounts in agreement on unpublish failure paths (#2147) Dispositions the three Codex findings on PR #7195 (review of 94a3edb443): P2 (canonicalisation): unpublish resolved the caller's mount spelling with canonicalize_mount_path AFTER the removal was persisted; the registry match accepts spellings (/arena/, //arena) that canonicalize rejects, so the call errored with the record gone and the route still mounted and unrecoverable. Unpublish now acts on the removed record's stored (canonical) placement. P2 (unmount failure): an unmount error after persistence left the route live but unnamed by any registry. MountSink::unmount now contracts that an error means NOT removed, and unpublish restores the pre-op registry (memory and disk) before propagating, so durable and live sets agree and a retry works. P1 (generated coverage): a model-based proptest runs generated publish/unpublish sequences against bind/save/mount/unmount failure injection, checking the manager, the on-disk registry, and the live sets against a reference model after every operation; two example tests pin the P2 scenarios. Mutation-checked: all three new tests fail against the pre-fix unpublish and pass with it. cargo test --lib publications: 26 passed. --- pkgs/id/src/publications_runtime.rs | 397 ++++++++++++++++++++++++++-- 1 file changed, 381 insertions(+), 16 deletions(-) diff --git a/pkgs/id/src/publications_runtime.rs b/pkgs/id/src/publications_runtime.rs index 4faebda595..fdc67584ec 100644 --- a/pkgs/id/src/publications_runtime.rs +++ b/pkgs/id/src/publications_runtime.rs @@ -22,9 +22,12 @@ //! first effective Explorer binding; with no Explorer listener //! the publish fails before anything is saved. //! - **Unpublish:** atomically save the candidate set *before* -//! removing the live route or stopping the listener — those -//! in-process removals are non-failing, and a crash after the -//! save restarts from the new set, so the order is safe. +//! removing the live route or stopping the listener — a crash +//! after the save restarts from the new set, so the order is +//! safe. Listener closes are infallible; a mount removal that +//! fails (its contract: the route was not removed) restores +//! the pre-op registry in memory and on disk before the error +//! propagates, so the durable and live sets still agree. //! - **Restore (boot):** apply the persisted set through the same //! resource path; a failure part-way rolls back what was applied //! and fails loudly, matching slice 1's fail-loud load contract. @@ -79,6 +82,12 @@ pub trait MountSink: Send { fn mount(&mut self, world: &str, path: &str) -> Result<()>; /// Remove the mount at `path`, restoring the Explorer route. + /// + /// Contract: an error means the mount was NOT removed — the + /// route is still live. The manager relies on this to keep the + /// persisted registry and the live set in agreement when a + /// removal fails (it restores the registry entry; see + /// [`PublicationManager::unpublish`]). fn unmount(&mut self, path: &str) -> Result<()>; } @@ -234,6 +243,11 @@ impl PublicationManager { /// candidate set cannot be persisted (the live set is then /// untouched). pub fn unpublish(&mut self, placement: &Placement) -> Result { + // Snapshot for recovery: if removing the live mount fails + // after the candidate set has been persisted, the pre-op + // registry is restored (memory and disk) so the durable + // set and the live set never silently disagree. + let previous = self.registry.clone(); let mut candidate = self.registry.clone(); let removed = candidate.unpublish(placement)?; candidate @@ -241,17 +255,39 @@ impl PublicationManager { .context("persisting the publications registry")?; self.registry = candidate; - match placement { + // Act on the REMOVED record's placement, never the caller's + // spelling: the registry's match normalises spellings + // (`/arena/`, `//arena`) that canonicalize_mount_path + // rejects outright, and the stored placement is the + // canonical path publish saved. Resolving the caller's + // spelling here would fail after the removal was already + // persisted, stranding a live mount no registry names. + match &removed.placement { Placement::Listener { addr } => { if let Some(listener) = self.listeners.remove(addr) { listener.close(); } } Placement::PathMount { path } => { - let canonical = crate::publications::canonicalize_mount_path(path)?; - if self.mounted.iter().any(|mounted| mounted == &canonical) { - self.mounts.unmount(&canonical)?; - self.mounted.retain(|mounted| mounted != &canonical); + if self.mounted.iter().any(|mounted| mounted == path) { + if let Err(error) = self.mounts.unmount(path) { + // The sink's contract is that an error + // means the route was NOT removed, so the + // live set is unchanged: restore the durable + // set to match it before propagating. + let restored = previous.save(&self.registry_path).is_ok(); + self.registry = previous; + return Err(error).context(format!( + "removing the live mount at {path} failed; the publication \ + was restored to the registry{}", + if restored { + "" + } else { + " in memory (restoring it on disk also failed)" + } + )); + } + self.mounted.retain(|mounted| mounted != path); } } } @@ -315,13 +351,38 @@ mod tests { use crate::publications::KNOWN_EXPLORER_ROUTES; use std::sync::{Arc, Mutex}; - #[derive(Default)] struct FakeState { next_port: u16, fail_addrs: Vec, closed: Vec, mounted: Vec, unmounted: Vec, + // Failure injection + live-state tracking for the model + // test: `live_listeners` / `live_mounts` are the CURRENT + // live sets (the `mounted` / `unmounted` logs above are + // append-only history). + explorer: bool, + fail_mounts: Vec, + fail_unmounts: Vec, + live_listeners: Vec, + live_mounts: Vec, + } + + impl Default for FakeState { + fn default() -> Self { + Self { + next_port: 0, + fail_addrs: Vec::new(), + closed: Vec::new(), + mounted: Vec::new(), + unmounted: Vec::new(), + explorer: true, + fail_mounts: Vec::new(), + fail_unmounts: Vec::new(), + live_listeners: Vec::new(), + live_mounts: Vec::new(), + } + } } struct FakeListener { @@ -335,7 +396,9 @@ mod tests { } fn close(self: Box) { - self.state.lock().unwrap().closed.push(self.actual); + let mut state = self.state.lock().unwrap(); + state.closed.push(self.actual); + state.live_listeners.retain(|addr| *addr != self.actual); } } @@ -356,6 +419,7 @@ mod tests { } else { addr }; + state.live_listeners.push(actual); Ok(Box::new(FakeListener { actual, state: self.state.clone(), @@ -365,21 +429,32 @@ mod tests { struct FakeMounts { state: Arc>, - explorer: bool, } impl MountSink for FakeMounts { fn has_explorer(&self) -> bool { - self.explorer + self.state.lock().unwrap().explorer } fn mount(&mut self, _world: &str, path: &str) -> Result<()> { - self.state.lock().unwrap().mounted.push(path.to_string()); + let mut state = self.state.lock().unwrap(); + ensure!( + !state.fail_mounts.iter().any(|failed| failed == path), + "mount at {path} failed (fake)" + ); + state.mounted.push(path.to_string()); + state.live_mounts.push(path.to_string()); Ok(()) } fn unmount(&mut self, path: &str) -> Result<()> { - self.state.lock().unwrap().unmounted.push(path.to_string()); + let mut state = self.state.lock().unwrap(); + ensure!( + !state.fail_unmounts.iter().any(|failed| failed == path), + "unmount at {path} failed (fake): the route was not removed" + ); + state.unmounted.push(path.to_string()); + state.live_mounts.retain(|mounted| mounted != path); Ok(()) } } @@ -431,6 +506,7 @@ mod tests { registry: Registry, explorer: bool, ) -> PublicationManager { + self.state.lock().unwrap().explorer = explorer; PublicationManager::new( registry, self.file.clone(), @@ -443,7 +519,6 @@ mod tests { }, FakeMounts { state: self.state.clone(), - explorer, }, ) } @@ -555,7 +630,6 @@ mod tests { }, FakeMounts { state: fx.state.clone(), - explorer: true, }, ); assert!(manager.publish(listener_pub("127.0.0.1:4001")).is_err()); @@ -633,4 +707,295 @@ mod tests { ); fx.cleanup(); } + + #[test] + fn unpublish_accepts_a_noncanonical_spelling_and_unmounts() { + // Regression pin for the PR #7195 Codex P2 (2026-11-11): the + // registry match normalises `/arena/`, but resolving that + // spelling after the removal was persisted used to fail, + // stranding a live mount no registry named. + let fx = Fixture::new("unpublish-spelling"); + let mut manager = fx.manager(Registry::default(), true); + manager.publish(mount_pub("/arena", false)).unwrap(); + let removed = manager + .unpublish(&Placement::PathMount { + path: "/arena/".to_string(), + }) + .unwrap(); + assert_eq!( + removed.placement, + Placement::PathMount { + path: "/arena".to_string() + } + ); + assert_eq!( + fx.state.lock().unwrap().unmounted, + vec!["/arena".to_string()] + ); + assert!(fx.state.lock().unwrap().live_mounts.is_empty()); + assert!(manager.registry().publications.is_empty()); + fx.cleanup(); + } + + #[test] + fn unpublish_unmount_failure_restores_the_publication() { + // Regression pin for the PR #7195 Codex P2 (2026-11-11): an + // unmount failure after the removal was persisted used to + // leave the route live but unnamed by any registry, and a + // retry then failed with "nothing published". + let fx = Fixture::new("unpublish-unmount-fails"); + let mut manager = fx.manager(Registry::default(), true); + manager.publish(mount_pub("/arena", false)).unwrap(); + fx.state + .lock() + .unwrap() + .fail_unmounts + .push("/arena".to_string()); + assert!( + manager + .unpublish(&Placement::PathMount { + path: "/arena".to_string(), + }) + .is_err() + ); + // Everything is exactly as it was before the failed call. + assert_eq!(manager.registry().publications.len(), 1); + assert_eq!(Registry::load(&fx.file).unwrap().publications.len(), 1); + assert_eq!( + fx.state.lock().unwrap().live_mounts, + vec!["/arena".to_string()] + ); + // And a retry with the failure cleared succeeds. + fx.state.lock().unwrap().fail_unmounts.clear(); + manager + .unpublish(&Placement::PathMount { + path: "/arena".to_string(), + }) + .unwrap(); + assert!(manager.registry().publications.is_empty()); + assert!(fx.state.lock().unwrap().live_mounts.is_empty()); + fx.cleanup(); + } + + // ---- Model-based state-machine coverage (PR #7195 Codex P1) ---- + // + // Oracle: the module doc's transactional ordering, stated as a + // reference model — after EVERY operation the manager's + // registry, the on-disk registry, and the live listener/mount + // sets agree with the model, and a failed operation changes + // none of them. Generated publish/unpublish sequences run + // against bind, save, mount, and unmount failure injection. + + #[derive(Debug, Clone)] + enum Op { + PublishListener(usize), + PublishMount(usize), + UnpublishListener(usize), + UnpublishMount(usize, usize), + ToggleBindFail(usize), + ToggleMountFail(usize), + ToggleUnmountFail(usize), + ToggleSaveFail, + ToggleExplorer, + } + + fn op_strategy() -> impl proptest::strategy::Strategy { + use proptest::prelude::*; + prop_oneof![ + (0..2usize).prop_map(Op::PublishListener), + (0..2usize).prop_map(Op::PublishMount), + (0..2usize).prop_map(Op::UnpublishListener), + (0..2usize, 0..3usize).prop_map(|(path, spelling)| Op::UnpublishMount(path, spelling)), + (0..2usize).prop_map(Op::ToggleBindFail), + (0..2usize).prop_map(Op::ToggleMountFail), + (0..2usize).prop_map(Op::ToggleUnmountFail), + Just(Op::ToggleSaveFail), + Just(Op::ToggleExplorer), + ] + } + + fn toggle(list: &mut Vec, value: &str) { + if let Some(position) = list.iter().position(|item| item == value) { + list.remove(position); + } else { + list.push(value.to_string()); + } + } + + fn run_model_case(ops: &[Op]) { + use std::sync::atomic::{AtomicUsize, Ordering}; + static NEXT: AtomicUsize = AtomicUsize::new(0); + let case = NEXT.fetch_add(1, Ordering::SeqCst); + let fx = Fixture::new(&format!("model-{case}")); + let mut manager = fx.manager(Registry::default(), true); + + const LISTENER_ADDRS: [&str; 2] = ["127.0.0.1:4001", "127.0.0.1:4002"]; + const MOUNT_PATHS: [&str; 2] = ["/arena", "/world"]; + let mut desired: Vec = Vec::new(); + let mut save_broken = false; + + for op in ops { + // Apply toggles to the fakes, and compute the model's + // prediction for manager operations from the documented + // contract — not from the manager's internals. + match op { + Op::ToggleBindFail(index) => { + let addr: SocketAddr = LISTENER_ADDRS[*index].parse().unwrap(); + let mut state = fx.state.lock().unwrap(); + if let Some(position) = state.fail_addrs.iter().position(|item| *item == addr) { + state.fail_addrs.remove(position); + } else { + state.fail_addrs.push(addr); + } + } + Op::ToggleMountFail(index) => { + toggle( + &mut fx.state.lock().unwrap().fail_mounts, + MOUNT_PATHS[*index], + ); + } + Op::ToggleUnmountFail(index) => { + toggle( + &mut fx.state.lock().unwrap().fail_unmounts, + MOUNT_PATHS[*index], + ); + } + Op::ToggleSaveFail => { + save_broken = !save_broken; + // Break saves without touching the registry file + // itself: save writes `.tmp` first, and a + // directory there makes the write fail. + let temp = fx.file.with_extension("json.tmp"); + if save_broken { + std::fs::create_dir_all(&temp).unwrap(); + } else { + let _ = std::fs::remove_dir_all(&temp); + } + } + Op::ToggleExplorer => { + let mut state = fx.state.lock().unwrap(); + state.explorer = !state.explorer; + } + Op::PublishListener(index) => { + let publication = listener_pub(LISTENER_ADDRS[*index]); + let state = fx.state.lock().unwrap(); + let predicted = !desired.contains(&publication) + && !state + .fail_addrs + .contains(&LISTENER_ADDRS[*index].parse::().unwrap()) + && !save_broken; + drop(state); + let result = manager.publish(publication.clone()); + assert_eq!(result.is_ok(), predicted, "op {op:?}"); + if predicted { + desired.push(publication); + } + } + Op::PublishMount(index) => { + let publication = mount_pub(MOUNT_PATHS[*index], true); + let state = fx.state.lock().unwrap(); + let predicted = !desired.contains(&publication) + && state.explorer + && !state + .fail_mounts + .iter() + .any(|path| path == MOUNT_PATHS[*index]) + && !save_broken; + drop(state); + let result = manager.publish(publication.clone()); + assert_eq!(result.is_ok(), predicted, "op {op:?}"); + if predicted { + desired.push(publication); + } + } + Op::UnpublishListener(index) => { + let placement = Placement::Listener { + addr: LISTENER_ADDRS[*index].parse().unwrap(), + }; + let position = desired + .iter() + .position(|existing| existing.placement == placement); + let predicted = position.is_some() && !save_broken; + let result = manager.unpublish(&placement); + assert_eq!(result.is_ok(), predicted, "op {op:?}"); + if predicted { + desired.remove(position.unwrap()); + } + } + Op::UnpublishMount(index, spelling) => { + let canonical = MOUNT_PATHS[*index]; + let spelled = match spelling { + 0 => canonical.to_string(), + 1 => format!("{canonical}/"), + _ => format!("/{canonical}"), + }; + let placement = Placement::PathMount { path: spelled }; + let position = desired.iter().position(|existing| { + existing.placement + == Placement::PathMount { + path: canonical.to_string(), + } + }); + let unmount_fails = fx + .state + .lock() + .unwrap() + .fail_unmounts + .iter() + .any(|path| path == canonical); + let predicted = position.is_some() && !save_broken && !unmount_fails; + let result = manager.unpublish(&placement); + assert_eq!(result.is_ok(), predicted, "op {op:?}"); + if predicted { + desired.remove(position.unwrap()); + } + } + } + + // The invariant, after every operation: the manager's + // registry, the on-disk registry, and the live sets all + // equal the model. + assert_eq!(manager.registry().publications, desired, "after {op:?}"); + assert_eq!( + Registry::load(&fx.file).unwrap().publications, + desired, + "disk after {op:?}" + ); + let state = fx.state.lock().unwrap(); + let mut live_addrs = state.live_listeners.clone(); + live_addrs.sort(); + let mut model_addrs: Vec = desired + .iter() + .filter_map(|publication| match &publication.placement { + Placement::Listener { addr } => Some(*addr), + Placement::PathMount { .. } => None, + }) + .collect(); + model_addrs.sort(); + assert_eq!(live_addrs, model_addrs, "live listeners after {op:?}"); + let model_mounts: Vec = desired + .iter() + .filter_map(|publication| match &publication.placement { + Placement::PathMount { path } => Some(path.clone()), + Placement::Listener { .. } => None, + }) + .collect(); + assert_eq!(state.live_mounts, model_mounts, "live mounts after {op:?}"); + assert_eq!(manager.list().len(), desired.len(), "list after {op:?}"); + } + fx.cleanup(); + } + + use proptest::prelude::*; + + proptest! { + #![proptest_config(ProptestConfig::with_cases(48))] + + #[test] + fn manager_matches_the_reference_model( + ops in proptest::collection::vec(op_strategy(), 1..14) + ) { + run_model_case(&ops); + } + } } From 1345a3e687a650dcfceeacdbae38729986bf9f55 Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 20:38:43 -0600 Subject: [PATCH 4/4] fix(id): make publications_runtime clippy-clean under --all-targets --all-features The slice-2 module was never clippy-verified: one production expect_used (the staged listener in publish) and a test module written without the scoped exceptions the repo pattern uses. The staged expect becomes an anyhow context error (the None is impossible by construction and now surfaces as an error, never a panic); the test module takes the standard mod-level allow (unwrap_used/expect_used/panic) used by src/world/tests.rs and src/world_terminal.rs. Verified: cargo clippy -p id --all-targets --all-features exits 0 and cargo test --lib publications stays 26/26 on the merged tree. --- pkgs/id/src/publications_runtime.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/publications_runtime.rs b/pkgs/id/src/publications_runtime.rs index fdc67584ec..0d41234459 100644 --- a/pkgs/id/src/publications_runtime.rs +++ b/pkgs/id/src/publications_runtime.rs @@ -214,7 +214,11 @@ impl PublicationManager { match &publication.placement { Placement::Listener { addr } => { - let listener = staged.expect("listener placements stage a listener"); + // `staged` holds a listener exactly for listener + // placements: the staging match above binds it + // before the save. Surface the impossible None as + // an error, never a panic. + let listener = staged.context("listener placements stage a listener")?; self.listeners.insert(*addr, listener); } Placement::PathMount { path } => { @@ -346,6 +350,7 @@ impl PublicationManager { } #[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] mod tests { use super::*; use crate::publications::KNOWN_EXPLORER_ROUTES;