GeezerVison is showing github.com — Merge pull request #3916 from developing-today/queue/4860-840-world-i… · developing-today/code@b963332
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pkgs/id/src/world/actor.rs +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -104,6 +104,7 @@ pub(crate) enum WorldCommand { | 104 | 104 | Input { | 105 | 105 | token: JoinCapability, | 106 | 106 | input: Vec<u8>, | | 107 | + session_id: Option<SessionId>, | 107 | 108 | /// The player-signed move event bytes for this input (#2814), when | 108 | 109 | /// the participant plays signed. `None` is the unsigned path, | 109 | 110 | /// which refuses participants with a bound author key. | @@ -325,6 +326,9 @@ pub(crate) struct WorldActor { | 325 | 326 | /// The participant whose input is being processed. Requests the program | 326 | 327 | /// makes in response are checked against that participant's scopes. | 327 | 328 | pub(crate) acting: Option<u64>, | | 329 | + /// The connection whose input is being processed. Ephemeral actor context; | | 330 | + /// it is deliberately absent from `WorldEvent` and the journal. | | 331 | + pub(crate) acting_session: Option<SessionId>, | 328 | 332 | } | 329 | 333 |
| 330 | 334 | impl WorldActor { | @@ -363,6 +367,7 @@ impl WorldActor { | 363 | 367 | self.metrics.record_command(started.elapsed()); | 364 | 368 | self.after_change().await; | 365 | 369 | self.acting = None; | | 370 | + self.acting_session = None; | 366 | 371 | } | 367 | 372 | } | 368 | 373 |
| @@ -1295,6 +1300,7 @@ impl WorldActor { | 1295 | 1300 | WorldCommand::Input { | 1296 | 1301 | token, | 1297 | 1302 | input, | | 1303 | + session_id, | 1298 | 1304 | signed_move, | 1299 | 1305 | reply, | 1300 | 1306 | } => { | @@ -1320,6 +1326,7 @@ impl WorldActor { | 1320 | 1326 | )), | 1321 | 1327 | Ok((candidate, signed)) if self.program_healthy => { | 1322 | 1328 | self.acting = Some(candidate.participant_id); | | 1329 | + self.acting_session = session_id; | 1323 | 1330 | match self | 1324 | 1331 | .program | 1325 | 1332 | .update(&candidate) | @@ -2070,6 +2077,7 @@ impl WorldActor { | 2070 | 2077 | ticks: BTreeMap::new(), | 2071 | 2078 | overdue_at_open, | 2072 | 2079 | acting: None, | | 2080 | + acting_session: None, | 2073 | 2081 | auth: crate::directory_auth::DirectoryAuth::default(), | 2074 | 2082 | metrics: WorldMetrics::default(), | 2075 | 2083 | } | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/id/src/world/handle.rs +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -127,6 +127,7 @@ impl WorldHandle { | 127 | 127 | ticks: BTreeMap::new(), | 128 | 128 | overdue_at_open, | 129 | 129 | acting: None, | | 130 | + acting_session: None, | 130 | 131 | auth: crate::directory_auth::DirectoryAuth::default(), | 131 | 132 | }; | 132 | 133 | tokio::spawn(actor.run(receiver)); | @@ -682,11 +683,32 @@ impl WorldHandle { | 682 | 683 |
| 683 | 684 | /// Submit bounded opaque game input. | 684 | 685 | pub async fn input(&self, token: JoinCapability, input: Vec<u8>) -> Result<WorldEvent> { | | 686 | + self.input_with_session(token, input, None).await | | 687 | + } | | 688 | + | | 689 | + /// Submit bounded opaque game input from a live session connection. | | 690 | + pub(crate) async fn input_from_session( | | 691 | + &self, | | 692 | + token: JoinCapability, | | 693 | + input: Vec<u8>, | | 694 | + session_id: SessionId, | | 695 | + ) -> Result<WorldEvent> { | | 696 | + self.input_with_session(token, input, Some(session_id)) | | 697 | + .await | | 698 | + } | | 699 | + | | 700 | + async fn input_with_session( | | 701 | + &self, | | 702 | + token: JoinCapability, | | 703 | + input: Vec<u8>, | | 704 | + session_id: Option<SessionId>, | | 705 | + ) -> Result<WorldEvent> { | 685 | 706 | let (reply, response) = oneshot::channel(); | 686 | 707 | self.commands | 687 | 708 | .send(WorldCommand::Input { | 688 | 709 | token, | 689 | 710 | input, | | 711 | + session_id, | 690 | 712 | signed_move: None, | 691 | 713 | reply, | 692 | 714 | }) | @@ -732,6 +754,7 @@ impl WorldHandle { | 732 | 754 | .send(WorldCommand::Input { | 733 | 755 | token, | 734 | 756 | input, | | 757 | + session_id: None, | 735 | 758 | signed_move: Some(signed_move), | 736 | 759 | reply, | 737 | 760 | }) | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/id/src/world/mod.rs +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,6 +74,17 @@ pub(crate) use util::{ | 74 | 74 | sanitize_display_name, token_digest, | 75 | 75 | }; | 76 | 76 |
| | 77 | +/// Identity of one live connection. It stays in memory only and is never part | | 78 | +/// of a world event or journal entry. | | 79 | +#[derive(Clone, Copy, Eq, Hash, PartialEq)] | | 80 | +pub(crate) struct SessionId(u64); | | 81 | + | | 82 | +impl SessionId { | | 83 | + pub(crate) const fn new(value: u64) -> Self { | | 84 | + Self(value) | | 85 | + } | | 86 | +} | | 87 | + | 77 | 88 | /// A chat participant may send at most this many lines during the rolling window. | 78 | 89 | pub(crate) const CHAT_RATE_BURST: u8 = 50; | 79 | 90 | pub(crate) const CHAT_RATE_WINDOW: std::time::Duration = std::time::Duration::from_secs(10); | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/id/src/world/tests.rs +52Lines changed: 52 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5481,6 +5481,7 @@ mod schedule_tests { | 5481 | 5481 | .handle(WorldCommand::Input { | 5482 | 5482 | token: token.clone(), | 5483 | 5483 | input: document.to_string().into_bytes(), | | 5484 | + session_id: None, | 5484 | 5485 | signed_move: None, | 5485 | 5486 | reply, | 5486 | 5487 | }) | @@ -5490,6 +5491,57 @@ mod schedule_tests { | 5490 | 5491 | let _ = response.await.unwrap().unwrap(); | 5491 | 5492 | } | 5492 | 5493 |
| | 5494 | + #[tokio::test] | | 5495 | + async fn input_session_attribution_is_ephemeral_and_distinguishes_connections() { | | 5496 | + let (mut actor, _, token, journal) = scheduling_actor("sessions", &[], false); | | 5497 | + const SESSION_A: u64 = 0x5eed_cafe_0000_0001; | | 5498 | + const SESSION_B: u64 = 0x5eed_cafe_0000_0002; | | 5499 | + let session_ids = [SessionId::new(SESSION_A), SessionId::new(SESSION_B)]; | | 5500 | + let mut participant = None; | | 5501 | + | | 5502 | + for session_id in session_ids { | | 5503 | + let (reply, response) = tokio::sync::oneshot::channel(); | | 5504 | + actor | | 5505 | + .handle(WorldCommand::Input { | | 5506 | + token: token.clone(), | | 5507 | + input: b"{}".to_vec(), | | 5508 | + session_id: Some(session_id), | | 5509 | + signed_move: None, | | 5510 | + reply, | | 5511 | + }) | | 5512 | + .await; | | 5513 | + | | 5514 | + let acting = actor | | 5515 | + .acting | | 5516 | + .expect("input participant is set while processing"); | | 5517 | + if let Some(first) = participant { | | 5518 | + assert_eq!(acting, first); | | 5519 | + } else { | | 5520 | + participant = Some(acting); | | 5521 | + } | | 5522 | + assert!(actor.acting_session == Some(session_id)); | | 5523 | + response.await.unwrap().unwrap(); | | 5524 | + actor.after_change().await; | | 5525 | + actor.acting = None; | | 5526 | + actor.acting_session = None; | | 5527 | + } | | 5528 | + | | 5529 | + let committed: Vec<_> = journal | | 5530 | + .entries() | | 5531 | + .into_iter() | | 5532 | + .filter(|entry| matches!(entry, JournalEntry::Committed { .. })) | | 5533 | + .collect(); | | 5534 | + assert_eq!(committed.len(), 2); | | 5535 | + for entry in committed { | | 5536 | + let value = serde_json::to_value(entry).unwrap(); | | 5537 | + assert_eq!(value.as_object().unwrap().len(), 2); | | 5538 | + assert!(value.get("session_id").is_none()); | | 5539 | + let serialized = value.to_string(); | | 5540 | + assert!(!serialized.contains(&SESSION_A.to_string())); | | 5541 | + assert!(!serialized.contains(&SESSION_B.to_string())); | | 5542 | + } | | 5543 | + } | | 5544 | + | 5493 | 5545 | /// The records the program last published. | 5494 | 5546 | fn records(actor: &WorldActor) -> Arc<Records> { | 5495 | 5547 | actor.records_sender.borrow().1.clone() | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/id/src/world_session/mod.rs +20-4Lines changed: 20 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | //! is (an Iroh node ID); that never grants world authority. | 16 | 16 |
| 17 | 17 | use std::sync::Arc; | | 18 | +use std::sync::atomic::Ordering; | 18 | 19 | use std::time::Duration; | 19 | 20 |
| 20 | 21 | use anyhow::Context as _; | @@ -30,8 +31,8 @@ use tokio::sync::{Mutex, OwnedSemaphorePermit, RwLock, Semaphore, broadcast}; | 30 | 31 | use crate::directory_auth::Signed; | 31 | 32 | use crate::time::unix_ms; | 32 | 33 | use crate::world::{ | 33 | | - Authority, CapabilityBounds, FileScope, Isolation, JoinCapability, Participant, WorldHandle, | 34 | | - WorldMetrics, WorldPolicy, WorldScopes, | | 34 | + Authority, CapabilityBounds, FileScope, Isolation, JoinCapability, Participant, SessionId, | | 35 | + WorldHandle, WorldMetrics, WorldPolicy, WorldScopes, | 35 | 36 | }; | 36 | 37 | use crate::world_compile::Compiler; | 37 | 38 | #[cfg(feature = "sandbox")] | @@ -64,6 +65,11 @@ pub use types::{ | 64 | 65 | }; | 65 | 66 |
| 66 | 67 | impl WorldService { | | 68 | + /// Allocate an ephemeral identity for one accepted connection. | | 69 | + pub(crate) fn next_session_id(&self) -> SessionId { | | 70 | + SessionId::new(self.next_session_id.fetch_add(1, Ordering::Relaxed)) | | 71 | + } | | 72 | + | 67 | 73 | /// Require module artifacts to carry a valid signature from one of these | 68 | 74 | /// Ed25519 public keys. An empty list preserves the legacy unsigned path. | 69 | 75 | #[must_use] | @@ -193,6 +199,7 @@ impl WorldService { | 193 | 199 | mail: None, | 194 | 200 | outbox: None, | 195 | 201 | profile_operation: Arc::new(Mutex::new(())), | | 202 | + next_session_id: Arc::new(std::sync::atomic::AtomicU64::new(1)), | 196 | 203 | } | 197 | 204 | } | 198 | 205 |
| @@ -2109,6 +2116,7 @@ async fn joined<I: SessionIo>( | 2109 | 2116 | screen_patches: bool, | 2110 | 2117 | ) { | 2111 | 2118 | let world = service.world(); | | 2119 | + let session_id = service.next_session_id(); | 2112 | 2120 | // Subscribe before taking the snapshot so no event can fall in the gap; | 2113 | 2121 | // events at or below the cursor are skipped when delivered. | 2114 | 2122 | let mut events = world.subscribe(); | @@ -2230,6 +2238,7 @@ async fn joined<I: SessionIo>( | 2230 | 2238 | &text, | 2231 | 2239 | &mut viewport, | 2232 | 2240 | cursor, | | 2241 | + session_id, | 2233 | 2242 | &mut view_state, | 2234 | 2243 | screen_patches, | 2235 | 2244 | &mut pending_view_deadline, | @@ -2501,6 +2510,7 @@ async fn handle_client_frame<I: SessionIo>( | 2501 | 2510 | text: &str, | 2502 | 2511 | viewport: &mut Option<Viewport>, | 2503 | 2512 | sequence: u64, | | 2513 | + session_id: SessionId, | 2504 | 2514 | view_state: &mut ViewRevisionState, | 2505 | 2515 | screen_patches: bool, | 2506 | 2516 | pending_view_deadline: &mut Option<tokio::time::Instant>, | @@ -2591,10 +2601,16 @@ async fn handle_client_frame<I: SessionIo>( | 2591 | 2601 | ) | 2592 | 2602 | .await; | 2593 | 2603 | }; | 2594 | | - world.input(token.clone(), bytes).await | | 2604 | + world | | 2605 | + .input_from_session(token.clone(), bytes, session_id) | | 2606 | + .await | 2595 | 2607 | } | 2596 | 2608 | ClientFrame::Input { data_hex } => match decode_hex(&data_hex) { | 2597 | | - Some(data) => world.input(token.clone(), data).await, | | 2609 | + Some(data) => { | | 2610 | + world | | 2611 | + .input_from_session(token.clone(), data, session_id) | | 2612 | + .await | | 2613 | + } | 2598 | 2614 | None => { | 2599 | 2615 | return send_error(service.frame_meter(), io, "input data must be hexadecimal") | 2600 | 2616 | .await; | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/id/src/world_session/tests.rs +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -276,6 +276,14 @@ mod tests { | 276 | 276 | assert_eq!(records["sequence"], 1); | 277 | 277 | } | 278 | 278 |
| | 279 | + #[tokio::test] | | 280 | + async fn cloned_service_allocates_distinct_ids_for_connections() { | | 281 | + let service = service(None); | | 282 | + let another_connection = service.clone(); | | 283 | + | | 284 | + assert!(service.next_session_id() != another_connection.next_session_id()); | | 285 | + } | | 286 | + | 279 | 287 | #[tokio::test] | 280 | 288 | async fn execution_profile_change_is_admin_only_and_does_not_replace_live_program() { | 281 | 289 | struct MarkerProgram; | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/id/src/world_session/types.rs +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | use std::collections::BTreeMap; | 2 | 2 | use std::sync::Arc; | | 3 | +use std::sync::atomic::AtomicU64; | 3 | 4 | use std::time::Duration; | 4 | 5 |
| 5 | 6 | use iroh_blobs::api::{Store, TempTag}; | @@ -8,8 +9,8 @@ use tokio::sync::{Mutex, RwLock, Semaphore}; | 8 | 9 |
| 9 | 10 | use crate::directory_auth::Signed; | 10 | 11 | use crate::world::{ | 11 | | - CapabilityBounds, FileScope, Isolation, MetricsSnapshot, WorldEvent, WorldHandle, WorldMetrics, | 12 | | - WorldPolicy, WorldSnapshot, | | 12 | + CapabilityBounds, FileScope, Isolation, MetricsSnapshot, SessionId, WorldEvent, WorldHandle, | | 13 | + WorldMetrics, WorldPolicy, WorldSnapshot, | 13 | 14 | }; | 14 | 15 | use crate::world_compile::Compiler; | 15 | 16 | use crate::world_hub::WorldInfo; | @@ -546,6 +547,8 @@ pub struct WorldService { | 546 | 547 | /// The world's host metrics registry (F11.7), shared with the actor: | 547 | 548 | /// the session layer records the frame bytes it sends into it. | 548 | 549 | pub(crate) metrics: WorldMetrics, | | 550 | + /// Monotonic, process-local IDs for live connections to this service. | | 551 | + pub(crate) next_session_id: Arc<AtomicU64>, | 549 | 552 | } | 550 | 553 |
| 551 | 554 | impl std::fmt::Debug for WorldService { | |
|