feat(resource): add stdin and stderr stdio endpoints
This commit is contained in:
1 parent
7fbe069196
commit
aed6a80ba7
6 files changed
+377
-18
No files matched your search
@@ -203,6 +203,10 @@ pub struct StdHost {
|
||||
broker_audit: Arc<Mutex<Vec<CapabilityAudit>>>,
|
||||
/// Host-issued opaque plugin handles.
|
||||
pub handles: PluginHandleTable,
|
||||
/// Run-scoped stdin bytes supplied by the composition root.
|
||||
stdin: Vec<u8>,
|
||||
/// Program stderr captured separately from stdout and diagnostics.
|
||||
stderr: Vec<u8>,
|
||||
/// Host-owned resource/stream table (ADR-0025).
|
||||
resources: ResourceTable,
|
||||
/// Registered in-process providers keyed by resource kind (ADR-0033).
|
||||
@@ -267,6 +271,8 @@ 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(),
|
||||
semantic_events: Vec::new(),
|
||||
@@ -476,6 +482,25 @@ impl StdHost {
|
||||
&self.output
|
||||
}
|
||||
|
||||
/// Attach run-scoped stdin bytes at assembly time (composition root).
|
||||
///
|
||||
/// 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<Vec<u8>>) -> Self {
|
||||
self.stdin = bytes.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Buffered program stderr without consuming it.
|
||||
pub fn error(&self) -> &[u8] {
|
||||
&self.stderr
|
||||
}
|
||||
|
||||
/// Take buffered program stderr.
|
||||
pub fn take_error(&mut self) -> Vec<u8> {
|
||||
std::mem::take(&mut self.stderr)
|
||||
}
|
||||
|
||||
fn dispatch(
|
||||
&mut self,
|
||||
op: HostOp,
|
||||
@@ -728,7 +753,8 @@ impl StdHost {
|
||||
let requested: BTreeSet<&str> = rights.iter().map(String::as_str).collect();
|
||||
if self.mode == SandboxMode::Strict {
|
||||
// stdio/stdout is the existing console channel already permitted by
|
||||
// strict L1 (CM-A11); file/network/process providers are not.
|
||||
// strict L1 (CM-A11); stdin, stderr, file/network/process providers
|
||||
// are not, because strict isolation has not been proven for them.
|
||||
let stdio_console =
|
||||
kind == "stdio" && descriptor == "stdout" && requested == BTreeSet::from(["write"]);
|
||||
if !stdio_console {
|
||||
@@ -749,27 +775,37 @@ impl StdHost {
|
||||
}
|
||||
match kind {
|
||||
"stdio" => {
|
||||
if descriptor != "stdout" || requested != BTreeSet::from(["write"]) {
|
||||
let allowed = match descriptor {
|
||||
"stdout" | "stderr" => requested == BTreeSet::from(["write"]),
|
||||
"stdin" => requested == BTreeSet::from(["read"]),
|
||||
_ => false,
|
||||
};
|
||||
if !allowed {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
"the first stream slice supports only stdio/stdout with rights [write]",
|
||||
"stdio supports stdout/stderr with rights [write] and stdin with rights [read]",
|
||||
));
|
||||
}
|
||||
if !self.capabilities.console {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"the console capability is required for stdio/stdout",
|
||||
"the console capability is required for stdio resources",
|
||||
));
|
||||
}
|
||||
let handle = self.resources.open_stdout();
|
||||
let handle = match descriptor {
|
||||
"stdin" => self.resources.open_stdin(self.stdin.clone()),
|
||||
"stderr" => self.resources.open_stderr(),
|
||||
"stdout" => self.resources.open_stdout(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some("kind=stdio descriptor=stdout"),
|
||||
Some(&format!("kind=stdio descriptor={descriptor}")),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
Ok(Value::Resource(handle))
|
||||
@@ -999,10 +1035,15 @@ impl Host for StdHost {
|
||||
) -> Result<usize, RuntimeError> {
|
||||
let started = Instant::now();
|
||||
let kind = self.resources.kind(handle, ctx.span)?;
|
||||
let stdio = self.resources.stdio_name(handle, ctx.span)?;
|
||||
let count = self.resources.write(handle, bytes, ctx.span)?;
|
||||
if kind == "stdio" {
|
||||
self.budget.consume_output(count, ctx.span)?;
|
||||
self.output.extend_from_slice(bytes);
|
||||
if stdio == Some("stderr") {
|
||||
self.stderr.extend_from_slice(&bytes[..count]);
|
||||
} else {
|
||||
self.output.extend_from_slice(&bytes[..count]);
|
||||
}
|
||||
}
|
||||
self.record_semantic(
|
||||
"resource.write",
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
//! Host-owned resource/stream table (ADR-0025).
|
||||
//!
|
||||
//! The first slice deliberately supports only stdout writes and bounded
|
||||
//! project-file reads loaded at open. Handles are run-scoped and opaque; the
|
||||
//! table validates run identity, slot identity and generation on every call.
|
||||
//! The stream table owns run-scoped stdio endpoints, bounded project-file
|
||||
//! read/write streams, explicitly registered provider tokens and IP-literal TCP
|
||||
//! streams. Handles are opaque and the table validates run identity, slot
|
||||
//! identity and generation on every call. Bytes move only through `Memory`.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::{Read, Write};
|
||||
@@ -76,9 +77,17 @@ pub struct ResourceRead {
|
||||
}
|
||||
|
||||
enum Stream {
|
||||
Stdin {
|
||||
bytes: Vec<u8>,
|
||||
position: usize,
|
||||
closed: bool,
|
||||
},
|
||||
Stdout {
|
||||
closed: bool,
|
||||
},
|
||||
Stderr {
|
||||
closed: bool,
|
||||
},
|
||||
File {
|
||||
relative: String,
|
||||
bytes: Vec<u8>,
|
||||
@@ -110,7 +119,9 @@ struct Slot {
|
||||
impl Slot {
|
||||
fn closed(&self) -> bool {
|
||||
match &self.stream {
|
||||
Stream::Stdin { closed, .. } => *closed,
|
||||
Stream::Stdout { closed } => *closed,
|
||||
Stream::Stderr { closed } => *closed,
|
||||
Stream::File { closed, .. } => *closed,
|
||||
Stream::FileWriter { closed, .. } => *closed,
|
||||
Stream::Provider { closed, .. } => *closed,
|
||||
@@ -152,6 +163,39 @@ impl ResourceTable {
|
||||
self.insert(Stream::Stdout { closed: false })
|
||||
}
|
||||
|
||||
/// Register the pre-opened stderr stream.
|
||||
pub fn open_stderr(&mut self) -> ResourceHandle {
|
||||
self.insert(Stream::Stderr { closed: false })
|
||||
}
|
||||
|
||||
/// Register a run-scoped stdin stream over already supplied bytes.
|
||||
///
|
||||
/// 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<u8>) -> ResourceHandle {
|
||||
self.insert(Stream::Stdin {
|
||||
bytes,
|
||||
position: 0,
|
||||
closed: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Return the stdio descriptor name for a handle, if it is a stdio stream.
|
||||
pub fn stdio_name(
|
||||
&self,
|
||||
handle: ResourceHandle,
|
||||
span: Option<Span>,
|
||||
) -> Result<Option<&'static str>, RuntimeError> {
|
||||
let slot = self.slot(handle, span)?;
|
||||
Ok(match &slot.stream {
|
||||
Stream::Stdin { .. } => Some("stdin"),
|
||||
Stream::Stdout { .. } => Some("stdout"),
|
||||
Stream::Stderr { .. } => Some("stderr"),
|
||||
Stream::File { .. } | Stream::FileWriter { .. } => None,
|
||||
Stream::Provider { .. } | Stream::Tcp { .. } => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Register a project-file read stream opened from already-bounded bytes.
|
||||
pub fn open_file(&mut self, relative: impl Into<String>, bytes: Vec<u8>) -> ResourceHandle {
|
||||
self.insert(Stream::File {
|
||||
@@ -268,7 +312,10 @@ impl ResourceTable {
|
||||
));
|
||||
}
|
||||
match &mut slot.stream {
|
||||
Stream::File {
|
||||
Stream::Stdin {
|
||||
bytes, position, ..
|
||||
}
|
||||
| Stream::File {
|
||||
bytes, position, ..
|
||||
} => {
|
||||
let start = *position;
|
||||
@@ -285,6 +332,11 @@ impl ResourceTable {
|
||||
span,
|
||||
"stdout does not support resource.read",
|
||||
)),
|
||||
Stream::Stderr { .. } => Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
span,
|
||||
"stderr does not support resource.read",
|
||||
)),
|
||||
Stream::FileWriter { .. } => Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
span,
|
||||
@@ -333,7 +385,12 @@ impl ResourceTable {
|
||||
));
|
||||
}
|
||||
match &mut slot.stream {
|
||||
Stream::Stdout { .. } => Ok(bytes.len()),
|
||||
Stream::Stdout { .. } | Stream::Stderr { .. } => Ok(bytes.len()),
|
||||
Stream::Stdin { .. } => Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
span,
|
||||
"stdin is a read-only resource stream",
|
||||
)),
|
||||
Stream::File { relative, .. } => Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
span,
|
||||
@@ -396,7 +453,9 @@ impl ResourceTable {
|
||||
));
|
||||
}
|
||||
match &mut slot.stream {
|
||||
Stream::Stdin { closed, .. } => *closed = true,
|
||||
Stream::Stdout { closed } => *closed = true,
|
||||
Stream::Stderr { closed } => *closed = true,
|
||||
Stream::File { closed, .. } => *closed = true,
|
||||
Stream::FileWriter { closed, .. } => *closed = true,
|
||||
Stream::Provider {
|
||||
@@ -447,7 +506,9 @@ impl ResourceTable {
|
||||
pub fn kind(&self, handle: ResourceHandle, span: Option<Span>) -> Result<String, RuntimeError> {
|
||||
let slot = self.slot(handle, span)?;
|
||||
Ok(match &slot.stream {
|
||||
Stream::Stdout { .. } => "stdio".to_string(),
|
||||
Stream::Stdin { .. } | Stream::Stdout { .. } | Stream::Stderr { .. } => {
|
||||
"stdio".to_string()
|
||||
}
|
||||
Stream::File { .. } | Stream::FileWriter { .. } => "file".to_string(),
|
||||
Stream::Provider { kind, .. } => kind.clone(),
|
||||
Stream::Tcp { .. } => "tcp".to_string(),
|
||||
@@ -536,4 +597,57 @@ mod tests {
|
||||
"resource.unsupported"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stdin_reads_are_chunked_and_end_at_eof() {
|
||||
let mut table = ResourceTable::new();
|
||||
let stdin = table.open_stdin(b"ab".to_vec());
|
||||
assert_eq!(
|
||||
table.read(stdin, 1, None).unwrap(),
|
||||
ResourceRead {
|
||||
bytes: b"a".to_vec(),
|
||||
eof: false
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
table.read(stdin, 8, None).unwrap(),
|
||||
ResourceRead {
|
||||
bytes: b"b".to_vec(),
|
||||
eof: true
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
table.read(stdin, 1, None).unwrap(),
|
||||
ResourceRead {
|
||||
bytes: Vec::new(),
|
||||
eof: true
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
table.write(stdin, b"x", None).unwrap_err().code(),
|
||||
"resource.unsupported"
|
||||
);
|
||||
table.close(stdin, None).unwrap();
|
||||
assert_eq!(
|
||||
table.read(stdin, 1, None).unwrap_err().code(),
|
||||
"resource.closed"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stderr_is_write_only() {
|
||||
let mut table = ResourceTable::new();
|
||||
let stderr = table.open_stderr();
|
||||
assert_eq!(table.write(stderr, b"diagnostic", None).unwrap(), 10);
|
||||
assert_eq!(
|
||||
table.read(stderr, 1, None).unwrap_err().code(),
|
||||
"resource.unsupported"
|
||||
);
|
||||
assert_eq!(table.stdio_name(stderr, None).unwrap(), Some("stderr"));
|
||||
table.close(stderr, None).unwrap();
|
||||
assert_eq!(
|
||||
table.write(stderr, b"x", None).unwrap_err().code(),
|
||||
"resource.closed"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user