Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkgs/id/src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<crate::world::EmptyWorldProgram>::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?;
Expand Down
57 changes: 53 additions & 4 deletions pkgs/id/src/world/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -44,7 +44,55 @@ impl WorldHandle {
program: Box<dyn WorldProgram>,
journal: Option<Box<dyn WorldJournal>>,
) -> 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<dyn WorldProgram>,
device_manifest: Option<DeviceManifest>,
) -> 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<dyn WorldProgram>,
journal: Option<Box<dyn WorldJournal>>,
device_manifest: Option<DeviceManifest>,
) -> 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.
Expand All @@ -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.
Expand All @@ -78,6 +126,7 @@ impl WorldHandle {
journal: Option<Box<dyn WorldJournal>>,
read_only: bool,
presence: HashMap<u64, usize>,
device_manifest: Option<DeviceManifest>,
) -> Self {
let (commands, receiver) = mpsc::channel(256);
let (events, _) = broadcast::channel(256);
Expand Down Expand Up @@ -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));
Expand Down
75 changes: 75 additions & 0 deletions pkgs/id/src/world/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<Vec<serde_json::Value>>>,
}

impl WorldProgram for DeviceProbe {
fn snapshot(&mut self) -> Result<Option<String>> {
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<Option<String>> {
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,
Expand Down
7 changes: 6 additions & 1 deletion pkgs/id/src/world_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
Loading