diff --git a/pkgs/id/src/commands/serve.rs b/pkgs/id/src/commands/serve.rs index 40b41a4699..d45ee9ade9 100644 --- a/pkgs/id/src/commands/serve.rs +++ b/pkgs/id/src/commands/serve.rs @@ -897,7 +897,12 @@ impl ServeWorlds { "serve startup: records ready, building world service (#190 breadcrumb)" ); let service = if self.ephemeral { - make(WorldHandle::spawn(WorldCore::new(name, limits)?)).with_records_store(records) + make(WorldHandle::spawn_with_device_manifest( + WorldCore::new(name, limits)?, + Box::::default(), + Some(WorldHandle::host_device_manifest()), + )) + .with_records_store(records) } else { let (service, report) = crate::world_store::open_world(&dir, name, limits, runtime, make).await?; diff --git a/pkgs/id/src/world/handle.rs b/pkgs/id/src/world/handle.rs index d4c180f27b..b580b650f8 100644 --- a/pkgs/id/src/world/handle.rs +++ b/pkgs/id/src/world/handle.rs @@ -22,7 +22,7 @@ use std::sync::Arc; use anyhow::Result; use tokio::sync::{broadcast, mpsc, oneshot, watch}; -use crate::world_caps::CapsReport; +use crate::world_caps::{CapsReport, DeviceManifest}; impl WorldHandle { /// Spawn a world actor on the current Tokio runtime. @@ -44,7 +44,55 @@ impl WorldHandle { program: Box, journal: Option>, ) -> Self { - Self::spawn_inner(core, program, journal, false, HashMap::new()) + Self::spawn_inner(core, program, journal, false, HashMap::new(), None) + } + + /// Spawn a world actor whose host supplies a device manifest + /// (#7942): the manifest is enumerated once, here at the adapter + /// boundary, cached by the actor, and served by `device.info` + /// for the life of the world — never re-enumerated per request. + /// `None` keeps the historical behaviour: the host cannot + /// describe its device and `device.info` refuses `unknown_cap`. + pub fn spawn_with_device_manifest( + core: WorldCore, + program: Box, + device_manifest: Option, + ) -> Self { + Self::spawn_durable_with_device_manifest(core, program, None, device_manifest) + } + + /// [`Self::spawn_durable`] with a host-supplied device manifest + /// (#7942); see [`Self::spawn_with_device_manifest`]. + pub fn spawn_durable_with_device_manifest( + core: WorldCore, + program: Box, + journal: Option>, + device_manifest: Option, + ) -> Self { + Self::spawn_inner( + core, + program, + journal, + false, + HashMap::new(), + device_manifest, + ) + } + + /// The device manifest of a served host (#7942): a headless + /// server process. It has no display, keys, sensors, or audio + /// device of its own; programs reach it over the network. + /// Hosts with real hardware describe themselves instead of + /// using this. + #[must_use] + pub fn host_device_manifest() -> DeviceManifest { + DeviceManifest { + display: serde_json::json!({"kind": "headless"}), + input: serde_json::json!({"keys": 0, "encoder": false, "pointer": false, "text_entry": "none"}), + sensors: serde_json::json!({"gnss": null, "microphone": false, "camera": false}), + audio: serde_json::json!({"out": null, "in": null}), + network: serde_json::json!({"transport": "https"}), + } } /// Spawn a replica actor over a verified, already-restored world. @@ -63,7 +111,7 @@ impl WorldHandle { .into_iter() .map(|(id, count)| (id, usize::try_from(count).map_or(usize::MAX, |count| count))) .collect(); - Ok(Self::spawn_inner(core, program, None, true, presence)) + Ok(Self::spawn_inner(core, program, None, true, presence, None)) } /// Whether this handle fronts a read-only replica actor. @@ -78,6 +126,7 @@ impl WorldHandle { journal: Option>, read_only: bool, presence: HashMap, + device_manifest: Option, ) -> Self { let (commands, receiver) = mpsc::channel(256); let (events, _) = broadcast::channel(256); @@ -128,7 +177,7 @@ impl WorldHandle { overdue_at_open, acting: None, acting_session: None, - device_manifest: None, + device_manifest, auth: crate::directory_auth::DirectoryAuth::default(), }; tokio::spawn(actor.run(receiver)); diff --git a/pkgs/id/src/world/tests.rs b/pkgs/id/src/world/tests.rs index 50693cb17e..ea6364656c 100644 --- a/pkgs/id/src/world/tests.rs +++ b/pkgs/id/src/world/tests.rs @@ -1831,6 +1831,81 @@ mod tests { assert_eq!(oversized_result["error"], "bad_args"); } + /// #7942: `device.info` driven through the public `WorldHandle` + /// spawn path — the manifest supplied at spawn comes back in the + /// result event, twice (the cached copy serves repeated requests; + /// nothing re-enumerates), with no direct field mutation. + #[tokio::test] + async fn device_info_through_the_public_spawn_path_returns_the_supplied_manifest() { + use std::sync::Mutex; + + #[derive(Debug, Default)] + struct DeviceProbe { + results: Arc>>, + } + + impl WorldProgram for DeviceProbe { + fn snapshot(&mut self) -> Result> { + Ok(Some("{}".to_owned())) + } + + fn update(&mut self, event: &WorldEvent) -> Result<()> { + if let WorldEventKind::System { event } = &event.kind { + let value: serde_json::Value = + serde_json::from_str(event).unwrap_or(serde_json::Value::Null); + if value["cap"] == "result" { + self.results.lock().unwrap().push(value); + } + } + Ok(()) + } + + fn wants(&mut self) -> Result> { + Ok(Some( + serde_json::json!({ + "v": 1, + "requests": [ + {"id": "device-1", "cap": "device.info", "args": {}}, + {"id": "device-2", "cap": "device.info", "args": {}}, + ], + }) + .to_string(), + )) + } + } + + let manifest = crate::world_caps::DeviceManifest { + display: serde_json::json!({"kind": "pixel", "width": 128, "height": 64, "colors": 1}), + input: serde_json::json!({"keys": 8, "encoder": true, "pointer": false, "text_entry": "none"}), + sensors: serde_json::json!({"gnss": {"accuracy_m": 5}, "microphone": false, "camera": false}), + audio: serde_json::json!({"out": "pcm", "in": null}), + network: serde_json::json!({"transport": "mqtt", "rtt_class": "slow"}), + }; + let mut core = WorldCore::new("lobby", WorldLimits::default()).unwrap(); + core.ledger + .set_granted(BTreeSet::from(["device.info".to_owned()])); + let results = Arc::new(Mutex::new(Vec::new())); + let probe = DeviceProbe { + results: Arc::clone(&results), + }; + let _world = + WorldHandle::spawn_with_device_manifest(core, Box::new(probe), Some(manifest.clone())); + let deadline = Instant::now() + Duration::from_secs(5); + loop { + if results.lock().unwrap().len() >= 2 { + break; + } + assert!(Instant::now() < deadline, "device.info was not answered"); + tokio::task::yield_now().await; + } + let results = results.lock().unwrap(); + assert_eq!(results.len(), 2); + for result in results.iter() { + assert_eq!(result["ok"], true, "{result}"); + assert_eq!(result["value"], manifest.to_json(), "{result}"); + } + } + #[tokio::test] async fn records_read_pages_fit_the_result_event_that_delivers_them() { // Regression for #4888: sixteen ~8 KiB records total ~128 KiB, diff --git a/pkgs/id/src/world_store.rs b/pkgs/id/src/world_store.rs index 3d33764621..aeecf46058 100644 --- a/pkgs/id/src/world_store.rs +++ b/pkgs/id/src/world_store.rs @@ -498,7 +498,12 @@ pub async fn open_world( }, }; report.sequence = restored.core.current_sequence(); - let handle = WorldHandle::spawn_durable(restored.core, program, Some(Box::new(journal))); + let handle = WorldHandle::spawn_durable_with_device_manifest( + restored.core, + program, + Some(Box::new(journal)), + Some(WorldHandle::host_device_manifest()), + ); let service = service(handle).with_module_dir(modules).with_outbox(outbox); if let Some(wasm) = restored_module { service.adopt_module(wasm).await?;