diff --git a/crates/marqdo-host/src/host.rs b/crates/marqdo-host/src/host.rs index 4409d18..012b7fe 100644 --- a/crates/marqdo-host/src/host.rs +++ b/crates/marqdo-host/src/host.rs @@ -203,8 +203,6 @@ pub struct StdHost { broker_audit: Arc>>, /// Host-issued opaque plugin handles. pub handles: PluginHandleTable, - /// Run-scoped stdin bytes supplied by the composition root. - stdin: Vec, /// Program stderr captured separately from stdout and diagnostics. stderr: Vec, /// Host-owned resource/stream table (ADR-0025). @@ -271,7 +269,6 @@ impl StdHost { vendor_capabilities: BTreeSet::new(), broker_audit: Arc::new(Mutex::new(Vec::new())), handles: PluginHandleTable::default(), - stdin: Vec::new(), stderr: Vec::new(), resources: ResourceTable::new(), resource_providers: BTreeMap::new(), @@ -487,7 +484,7 @@ impl StdHost { /// This does not start a thread, spawn a process or read a hidden second /// channel. A program reads these bytes only through `stdio/stdin`. pub fn with_stdin(mut self, bytes: impl Into>) -> Self { - self.stdin = bytes.into(); + self.resources.set_stdin(bytes.into()); self } @@ -795,7 +792,7 @@ impl StdHost { )); } let handle = match descriptor { - "stdin" => self.resources.open_stdin(self.stdin.clone()), + "stdin" => self.resources.open_stdin(), "stderr" => self.resources.open_stderr(), "stdout" => self.resources.open_stdout(), _ => unreachable!(), diff --git a/crates/marqdo-host/src/resource.rs b/crates/marqdo-host/src/resource.rs index 7137e78..a33d4cf 100644 --- a/crates/marqdo-host/src/resource.rs +++ b/crates/marqdo-host/src/resource.rs @@ -76,10 +76,14 @@ pub struct ResourceRead { pub eof: bool, } +struct StdinCursor { + bytes: Vec, + position: usize, +} + enum Stream { Stdin { - bytes: Vec, - position: usize, + cursor: Arc>, closed: bool, }, Stdout { @@ -135,6 +139,7 @@ pub struct ResourceTable { run_id: u64, next_resource: u64, slots: BTreeMap, + stdin: Arc>, } impl Default for ResourceTable { @@ -150,6 +155,10 @@ impl ResourceTable { run_id: NEXT_RUN_ID.fetch_add(1, Ordering::Relaxed), next_resource: 1, slots: BTreeMap::new(), + stdin: Arc::new(Mutex::new(StdinCursor { + bytes: Vec::new(), + position: 0, + })), } } @@ -168,14 +177,18 @@ impl ResourceTable { self.insert(Stream::Stderr { closed: false }) } - /// Register a run-scoped stdin stream over already supplied bytes. + /// Set the run-scoped stdin bytes at assembly time. /// - /// The host supplies bytes at assembly time; `read` chunks them with EOF. - /// There is no blocking OS read and no hidden second input channel here. - pub fn open_stdin(&mut self, bytes: Vec) -> ResourceHandle { + /// All `stdio/stdin` handles share this cursor so repeated console reads + /// continue through the input instead of restarting at byte zero. + pub fn set_stdin(&mut self, bytes: Vec) { + self.stdin = Arc::new(Mutex::new(StdinCursor { bytes, position: 0 })); + } + + /// Register a read-only handle over the run-scoped stdin cursor. + pub fn open_stdin(&mut self) -> ResourceHandle { self.insert(Stream::Stdin { - bytes, - position: 0, + cursor: Arc::clone(&self.stdin), closed: false, }) } @@ -312,10 +325,18 @@ impl ResourceTable { )); } match &mut slot.stream { - Stream::Stdin { - bytes, position, .. + Stream::Stdin { cursor, .. } => { + let mut cursor = cursor.lock().map_err(|_| stdin_lock_error(span))?; + let start = cursor.position; + let end = start.saturating_add(max_bytes).min(cursor.bytes.len()); + let chunk = cursor.bytes[start..end].to_vec(); + cursor.position = end; + Ok(ResourceRead { + bytes: chunk, + eof: end >= cursor.bytes.len(), + }) } - | Stream::File { + Stream::File { bytes, position, .. } => { let start = *position; @@ -524,6 +545,14 @@ fn provider_lock_error(span: Option) -> RuntimeError { ) } +fn stdin_lock_error(span: Option) -> RuntimeError { + RuntimeError::new( + ErrorCategory::HostError, + span, + "stdin cursor lock is poisoned", + ) +} + fn unknown_handle(span: Option) -> RuntimeError { RuntimeError::new( ErrorCategory::ResourceFault, @@ -601,7 +630,8 @@ mod tests { #[test] fn stdin_reads_are_chunked_and_end_at_eof() { let mut table = ResourceTable::new(); - let stdin = table.open_stdin(b"ab".to_vec()); + table.set_stdin(b"ab".to_vec()); + let stdin = table.open_stdin(); assert_eq!( table.read(stdin, 1, None).unwrap(), ResourceRead {