feat: capture bounded Bash output over replayable byte transport
This commit is contained in:
@@ -23,7 +23,8 @@ thiserror = { workspace = true }
|
||||
tokio = { workspace = true, features = ["process", "rt", "sync", "time"] }
|
||||
tracing = { workspace = true }
|
||||
workdir = { workspace = true }
|
||||
uuid = { workspace = true, features = ["v7"] }
|
||||
|
||||
[dev-dependencies]
|
||||
filetime = "0.2.27"
|
||||
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
|
||||
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "test-util"] }
|
||||
|
||||
+268
-210
@@ -1,19 +1,19 @@
|
||||
//! Bash Tool using WorkdirSession command capability.
|
||||
//!
|
||||
//! The selected session owns cwd, process launch, bounded output, timeout, and cancellation.
|
||||
//! Workdir owns launch and byte transport; Bash owns bounded preview and retained evidence.
|
||||
//! Bash is not a sandbox; the Runtime process and OS user define the execution boundary.
|
||||
//! Per-execution handles and nonces prevent cancellation of another command. Dropping a future
|
||||
//! does not confirm termination; active registrations remain until cleanup requests cancellation
|
||||
//! and observes terminal output.
|
||||
//! does not confirm termination. Registrations remain during bounded cancellation/drain cleanup;
|
||||
//! an unconfirmed provider outcome leaves partial evidence, never a fabricated terminal fence.
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::bash_output::{BashOutputMetadata, BashOutputView, BashOutputWriter};
|
||||
use agen::tool::{Tool, ToolDefinition, ToolError, ToolMeta, ToolOutput};
|
||||
use async_trait::async_trait;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use workdir::{
|
||||
CommandHandle, CommandOutputRequest, CommandRequest, WorkdirPath, WorkdirSessionHandle,
|
||||
WorkdirSessionRouter,
|
||||
@@ -35,9 +35,9 @@ struct BashParams {
|
||||
timeout: Option<u64>,
|
||||
}
|
||||
|
||||
pub(crate) struct BashTool {
|
||||
pub struct BashTool {
|
||||
router: Arc<WorkdirSessionRouter>,
|
||||
output_dir: PathBuf,
|
||||
output: BashOutputView,
|
||||
state: Arc<Mutex<BashExecutionState>>,
|
||||
}
|
||||
|
||||
@@ -63,6 +63,8 @@ struct CommandGuard {
|
||||
execution_id: String,
|
||||
execution_nonce: u64,
|
||||
handle: Option<CommandHandle>,
|
||||
cursor: u64,
|
||||
retained: Option<BashOutputWriter>,
|
||||
}
|
||||
|
||||
impl Drop for CommandGuard {
|
||||
@@ -74,20 +76,66 @@ impl Drop for CommandGuard {
|
||||
let state = Arc::clone(&self.state);
|
||||
let execution_id = self.execution_id.clone();
|
||||
let execution_nonce = self.execution_nonce;
|
||||
let mut retained = self.retained.take();
|
||||
let cursor = self.cursor;
|
||||
// A dropped provider future is not terminal confirmation. Keep the live
|
||||
// execution registered until cleanup has both requested cancellation and
|
||||
// observed terminal command output, so cancellation/session teardown
|
||||
// cannot race with an apparently empty registry.
|
||||
tokio::spawn(async move {
|
||||
let _ = workdir.cancel_command(handle.clone()).await;
|
||||
let _ = workdir
|
||||
.command_output(CommandOutputRequest {
|
||||
handle,
|
||||
cursor: 0,
|
||||
limit: INLINE_BYTE_BUDGET,
|
||||
wait: true,
|
||||
})
|
||||
.await;
|
||||
let result = tokio::time::timeout(std::time::Duration::from_secs(10), async {
|
||||
let _ = workdir.cancel_command(handle.clone()).await;
|
||||
let mut cursor = cursor;
|
||||
loop {
|
||||
let page = command_page(
|
||||
&workdir,
|
||||
CommandOutputRequest {
|
||||
handle: handle.clone(),
|
||||
cursor,
|
||||
limit: workdir::MAX_COMMAND_READ_BYTES,
|
||||
wait: true,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
if let Some(writer) = &mut retained {
|
||||
let mut next = cursor;
|
||||
for chunk in &page.chunks {
|
||||
if chunk.sequence != next {
|
||||
return Err(workdir::WorkdirError::Unavailable(
|
||||
"command cleanup sequence gap".into(),
|
||||
));
|
||||
}
|
||||
writer
|
||||
.append_chunk(chunk)
|
||||
.map_err(workdir::WorkdirError::Unavailable)?;
|
||||
next = next.checked_add(chunk.bytes.len() as u64).ok_or_else(|| {
|
||||
workdir::WorkdirError::Unavailable(
|
||||
"command cleanup cursor overflow".into(),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
if page.next_cursor != next {
|
||||
return Err(workdir::WorkdirError::Unavailable(
|
||||
"command cleanup cursor gap".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
cursor = page.next_cursor;
|
||||
if let Some(terminal) = page.terminal {
|
||||
if let Some(writer) = &mut retained {
|
||||
writer
|
||||
.finish_transport(&terminal)
|
||||
.map_err(workdir::WorkdirError::Unavailable)?;
|
||||
}
|
||||
return Ok::<(), workdir::WorkdirError>(());
|
||||
}
|
||||
}
|
||||
})
|
||||
.await;
|
||||
// Bounded cleanup can end with an unknown provider outcome. The writer then
|
||||
// terminalizes partial evidence on drop; retaining a dead collector in `active`
|
||||
// would leak registrations and falsely suggest it could still collect output.
|
||||
let _ = result;
|
||||
let mut state = state.lock().unwrap();
|
||||
if state
|
||||
.active
|
||||
@@ -101,15 +149,45 @@ impl Drop for CommandGuard {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Tool for BashTool {
|
||||
async fn execute(
|
||||
// Output pages are replayable only with the exact same command/cursor/limit.
|
||||
// Retry read transport failures, never command start, and cap both attempts and
|
||||
// backoff. The caller's deadline bounds a disconnected or unresponsive provider.
|
||||
async fn command_page(
|
||||
session: &workdir::WorkdirSessionHandle,
|
||||
request: CommandOutputRequest,
|
||||
) -> Result<workdir::CommandOutput, workdir::WorkdirError> {
|
||||
for attempt in 0..3 {
|
||||
match session.command_output(request.clone()).await {
|
||||
Err(
|
||||
workdir::WorkdirError::Transport(_)
|
||||
| workdir::WorkdirError::Unavailable(_)
|
||||
| workdir::WorkdirError::OutcomeUnknown(_),
|
||||
) if attempt < 2 => {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100 << attempt)).await;
|
||||
}
|
||||
result => return result,
|
||||
}
|
||||
}
|
||||
unreachable!("bounded retry loop returns its last result")
|
||||
}
|
||||
|
||||
impl BashTool {
|
||||
pub fn new(router: Arc<WorkdirSessionRouter>, output: BashOutputView) -> Self {
|
||||
Self {
|
||||
router,
|
||||
output,
|
||||
state: Arc::new(Mutex::new(BashExecutionState::default())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_result(
|
||||
&self,
|
||||
input_json: &str,
|
||||
ctx: agen::tool::ToolExecutionContext,
|
||||
) -> Result<ToolOutput, ToolError> {
|
||||
) -> Result<BashResult, ToolError> {
|
||||
let params: BashParams = serde_json::from_str(input_json)
|
||||
.map_err(|error| ToolError::InvalidArgument(format!("invalid Bash input: {error}")))?;
|
||||
let retained = self.output.begin().map_err(ToolError::ExecutionFailed)?;
|
||||
let selected = crate::routing::resolve_session(
|
||||
&self.router,
|
||||
params.target_workdir.as_deref(),
|
||||
@@ -134,14 +212,15 @@ impl Tool for BashTool {
|
||||
execution_id: execution_id.clone(),
|
||||
execution_nonce,
|
||||
handle: None,
|
||||
cursor: 0,
|
||||
retained: Some(retained),
|
||||
};
|
||||
let handle = session
|
||||
.start_command(CommandRequest {
|
||||
command: params.command,
|
||||
timeout_secs,
|
||||
output_limit: INLINE_BYTE_BUDGET,
|
||||
transport_byte_budget: self.output.byte_budget(),
|
||||
cwd: params.cwd,
|
||||
spill_dir: Some(self.output_dir.clone()),
|
||||
tool_call_id: Some(call_id.clone()),
|
||||
})
|
||||
.await
|
||||
@@ -167,15 +246,101 @@ impl Tool for BashTool {
|
||||
.await
|
||||
.map_err(crate::ToolsError::from)?;
|
||||
}
|
||||
let output = session
|
||||
.command_output(CommandOutputRequest {
|
||||
handle,
|
||||
cursor: 0,
|
||||
limit: INLINE_BYTE_BUDGET,
|
||||
wait: true,
|
||||
})
|
||||
let mut cursor = 0;
|
||||
let mut stdout_preview = Vec::new();
|
||||
let mut stderr_preview = Vec::new();
|
||||
let mut seen = [0_u64; 2];
|
||||
let mut empty_polls = 0;
|
||||
let deadline =
|
||||
tokio::time::Instant::now() + std::time::Duration::from_secs(timeout_secs + 30);
|
||||
let terminal = loop {
|
||||
let page = tokio::time::timeout_at(
|
||||
deadline,
|
||||
command_page(
|
||||
&session,
|
||||
CommandOutputRequest {
|
||||
handle: handle.clone(),
|
||||
cursor,
|
||||
limit: workdir::MAX_COMMAND_READ_BYTES,
|
||||
wait: true,
|
||||
},
|
||||
),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
ToolError::ExecutionFailed(
|
||||
"command byte transport deadline exceeded; terminal outcome unknown".into(),
|
||||
)
|
||||
})?
|
||||
.map_err(crate::ToolsError::from)?;
|
||||
let mut next = cursor;
|
||||
for chunk in &page.chunks {
|
||||
if chunk.sequence != next || chunk.bytes.is_empty() {
|
||||
return Err(ToolError::ExecutionFailed(
|
||||
"command byte transport sequence gap".into(),
|
||||
));
|
||||
}
|
||||
next = next
|
||||
.checked_add(chunk.bytes.len() as u64)
|
||||
.filter(|n| *n <= self.output.byte_budget())
|
||||
.ok_or_else(|| {
|
||||
ToolError::ExecutionFailed(
|
||||
"command byte transport exceeds delivery budget".into(),
|
||||
)
|
||||
})?;
|
||||
let index = if chunk.stream == workdir::CommandStream::Stdout {
|
||||
0
|
||||
} else {
|
||||
1
|
||||
};
|
||||
if chunk.offset != seen[index] {
|
||||
return Err(ToolError::ExecutionFailed(
|
||||
"command byte transport offset gap".into(),
|
||||
));
|
||||
}
|
||||
seen[index] += chunk.bytes.len() as u64;
|
||||
guard
|
||||
.retained
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.append_chunk(chunk)
|
||||
.map_err(ToolError::ExecutionFailed)?;
|
||||
let available =
|
||||
INLINE_BYTE_BUDGET.saturating_sub(stdout_preview.len() + stderr_preview.len());
|
||||
let preview = if index == 0 {
|
||||
&mut stdout_preview
|
||||
} else {
|
||||
&mut stderr_preview
|
||||
};
|
||||
preview.extend_from_slice(&chunk.bytes[..available.min(chunk.bytes.len())]);
|
||||
}
|
||||
if page.next_cursor != next {
|
||||
return Err(ToolError::ExecutionFailed(
|
||||
"command byte transport cursor does not fence the page".into(),
|
||||
));
|
||||
}
|
||||
if page.chunks.is_empty() && page.terminal.is_none() {
|
||||
empty_polls += 1;
|
||||
if empty_polls > 32 {
|
||||
return Err(ToolError::ExecutionFailed(
|
||||
"command byte transport made no progress after bounded polls".into(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
empty_polls = 0;
|
||||
}
|
||||
cursor = page.next_cursor;
|
||||
guard.cursor = cursor;
|
||||
if let Some(terminal) = page.terminal {
|
||||
break terminal;
|
||||
}
|
||||
};
|
||||
let metadata = guard
|
||||
.retained
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.finish_transport(&terminal)
|
||||
.map_err(ToolError::ExecutionFailed)?;
|
||||
let cancellation_requested = {
|
||||
let mut state = self.state.lock().unwrap();
|
||||
let owns_registration = state
|
||||
@@ -193,41 +358,78 @@ impl Tool for BashTool {
|
||||
};
|
||||
guard.handle = None;
|
||||
|
||||
let timed_out = output.timed_out;
|
||||
let summary = if cancellation_requested {
|
||||
format!("$ {cmd_summary} (cancelled)")
|
||||
} else if output.timed_out {
|
||||
format!("$ {cmd_summary} (timed out after {timeout_secs}s)")
|
||||
} else {
|
||||
match output.exit_code {
|
||||
Some(0) => format!("$ {cmd_summary}"),
|
||||
Some(code) => format!("$ {cmd_summary} (exit {code})"),
|
||||
None => format!("$ {cmd_summary} (terminated)"),
|
||||
}
|
||||
};
|
||||
let content = if output.content.is_empty() {
|
||||
None
|
||||
} else if output.truncated {
|
||||
let notice = match output.output_path {
|
||||
Some(path) => format!(
|
||||
"[showing bounded WorkdirSession command output; full output saved to {}]",
|
||||
path.display()
|
||||
),
|
||||
None => "[showing bounded WorkdirSession command output; additional output was truncated]"
|
||||
.to_owned(),
|
||||
let timed_out = terminal.status == workdir::CommandStatus::TimedOut;
|
||||
let summary =
|
||||
if cancellation_requested || terminal.status == workdir::CommandStatus::Cancelled {
|
||||
format!("$ {cmd_summary} (cancelled)")
|
||||
} else if timed_out {
|
||||
format!("$ {cmd_summary} (timed out after {timeout_secs}s)")
|
||||
} else {
|
||||
match terminal.exit_code {
|
||||
Some(0) => format!("$ {cmd_summary}"),
|
||||
Some(code) => format!("$ {cmd_summary} (exit {code})"),
|
||||
None => format!("$ {cmd_summary} (terminated)"),
|
||||
}
|
||||
};
|
||||
Some(format!("{notice}\n{}", output.content))
|
||||
} else {
|
||||
Some(output.content)
|
||||
};
|
||||
let output = ToolOutput {
|
||||
Ok(BashResult {
|
||||
summary,
|
||||
content,
|
||||
attachments: Vec::new(),
|
||||
};
|
||||
if cancellation_requested {
|
||||
stdout_preview: String::from_utf8_lossy(&stdout_preview).into_owned(),
|
||||
stderr_preview: String::from_utf8_lossy(&stderr_preview).into_owned(),
|
||||
preview_omitted: terminal.stdout_bytes.saturating_add(terminal.stderr_bytes)
|
||||
> (stdout_preview.len() + stderr_preview.len()) as u64,
|
||||
metadata,
|
||||
cancelled: cancellation_requested
|
||||
|| terminal.status == workdir::CommandStatus::Cancelled,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct BashResult {
|
||||
pub summary: String,
|
||||
pub stdout_preview: String,
|
||||
pub stderr_preview: String,
|
||||
pub preview_omitted: bool,
|
||||
pub metadata: BashOutputMetadata,
|
||||
pub cancelled: bool,
|
||||
}
|
||||
|
||||
impl BashResult {
|
||||
pub fn stdout_entry(&self) -> String {
|
||||
format!("/bash-output/{}/stdout", self.metadata.id)
|
||||
}
|
||||
pub fn stderr_entry(&self) -> String {
|
||||
format!("/bash-output/{}/stderr", self.metadata.id)
|
||||
}
|
||||
pub fn order_entry(&self) -> String {
|
||||
format!("/bash-output/{}/order", self.metadata.id)
|
||||
}
|
||||
pub fn tool_output(&self) -> ToolOutput {
|
||||
let content = serde_json::json!({
|
||||
"stdout_preview": self.stdout_preview, "stderr_preview": self.stderr_preview,
|
||||
"preview_omitted": self.preview_omitted, "output": self.metadata,
|
||||
"stdout_entry": self.stdout_entry(), "stderr_entry": self.stderr_entry(), "order_entry": self.order_entry(),
|
||||
});
|
||||
ToolOutput {
|
||||
summary: self.summary.clone(),
|
||||
content: Some(content.to_string()),
|
||||
attachments: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Tool for BashTool {
|
||||
async fn execute(
|
||||
&self,
|
||||
input: &str,
|
||||
ctx: agen::tool::ToolExecutionContext,
|
||||
) -> Result<ToolOutput, ToolError> {
|
||||
let result = self.execute_result(input, ctx).await?;
|
||||
let output = result.tool_output();
|
||||
if result.cancelled {
|
||||
Err(ToolError::Cancelled(output))
|
||||
} else if timed_out {
|
||||
} else if result.metadata.terminal.status == Some(workdir::CommandStatus::TimedOut) {
|
||||
Err(ToolError::Interrupted(output))
|
||||
} else {
|
||||
Ok(output)
|
||||
@@ -288,164 +490,20 @@ fn truncate_for_summary(command: &str) -> String {
|
||||
summary
|
||||
}
|
||||
|
||||
pub fn bash_tool(session: WorkdirSessionHandle, output_dir: PathBuf) -> ToolDefinition {
|
||||
routed_bash_tool(crate::routing::singleton_router(session), output_dir)
|
||||
pub fn bash_tool(session: WorkdirSessionHandle, output: BashOutputView) -> ToolDefinition {
|
||||
routed_bash_tool(crate::routing::singleton_router(session), output)
|
||||
}
|
||||
|
||||
pub(crate) fn routed_bash_tool(
|
||||
router: Arc<WorkdirSessionRouter>,
|
||||
output_dir: PathBuf,
|
||||
output: BashOutputView,
|
||||
) -> ToolDefinition {
|
||||
Arc::new(move || {
|
||||
let schema = schemars::schema_for!(BashParams);
|
||||
let meta = ToolMeta::new("Bash")
|
||||
.description("Execute a shell command in one selected Workdir. Process start, bounded inline output, full-output spill, timeout and cancellation are owned by that WorkdirSession provider. This is not a sandbox.")
|
||||
.description("Execute a shell command in one selected Workdir. Workdir owns launch, lossless bounded byte transport, timeout and cancel; Bash owns preview, quota-limited ephemeral output and read-only virtual references. This is not a sandbox.")
|
||||
.input_schema(serde_json::to_value(schema).expect("Bash schema serialization"));
|
||||
let tool: Arc<dyn Tool> = Arc::new(BashTool {
|
||||
router: router.clone(),
|
||||
output_dir: output_dir.clone(),
|
||||
state: Arc::new(Mutex::new(BashExecutionState::default())),
|
||||
});
|
||||
let tool: Arc<dyn Tool> = Arc::new(BashTool::new(router.clone(), output.clone()));
|
||||
(meta, tool)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use manifest::{Permission, Scope, ScopeConfig, ScopeRule};
|
||||
use tempfile::TempDir;
|
||||
use workdir::{LocalWorkdirSession, WorkdirSessionHandle};
|
||||
|
||||
use super::bash_tool;
|
||||
use crate::{grep::grep_tool, read::read_tool, tracker::Tracker};
|
||||
|
||||
fn session_with_output_scope(root: &TempDir, output: &TempDir) -> WorkdirSessionHandle {
|
||||
let scope = Scope::from_config(&ScopeConfig {
|
||||
allow: vec![
|
||||
ScopeRule {
|
||||
target: root.path().to_path_buf(),
|
||||
permission: Permission::Write,
|
||||
recursive: true,
|
||||
symlink_policy: Default::default(),
|
||||
},
|
||||
ScopeRule {
|
||||
target: output.path().to_path_buf(),
|
||||
permission: Permission::Read,
|
||||
recursive: true,
|
||||
symlink_policy: Default::default(),
|
||||
},
|
||||
],
|
||||
deny: Vec::new(),
|
||||
})
|
||||
.unwrap();
|
||||
Arc::new(LocalWorkdirSession::new(scope, root.path().to_path_buf()))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn long_output_is_spilled_and_available_to_read_and_grep() {
|
||||
let root = TempDir::new().unwrap();
|
||||
let output = TempDir::new().unwrap();
|
||||
let session = session_with_output_scope(&root, &output);
|
||||
let (_, bash) = bash_tool(session.clone(), output.path().to_path_buf())();
|
||||
let command = "i=0; while [ $i -lt 2000 ]; do printf 'line-%04d\\n' \"$i\"; i=$((i+1)); done; printf 'FINAL-NEEDLE\\n'";
|
||||
let result = bash
|
||||
.execute(
|
||||
&serde_json::json!({ "command": command, "cwd": "." }).to_string(),
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let rendered = result.content.expect("bounded Bash output");
|
||||
let artifact = std::fs::read_dir(output.path())
|
||||
.unwrap()
|
||||
.next()
|
||||
.expect("artifact entry")
|
||||
.unwrap()
|
||||
.path();
|
||||
|
||||
assert!(rendered.contains("full output saved to"));
|
||||
assert!(rendered.contains(&artifact.display().to_string()));
|
||||
let retained = std::fs::read_to_string(&artifact).unwrap();
|
||||
assert!(retained.starts_with("line-0000\n"));
|
||||
assert!(retained.ends_with("FINAL-NEEDLE\n"));
|
||||
assert_eq!(retained.lines().count(), 2001);
|
||||
|
||||
let (_, read) = read_tool(session.clone(), Tracker::new())();
|
||||
let read_result = read
|
||||
.execute(
|
||||
&serde_json::json!({
|
||||
"file_path": artifact,
|
||||
"offset": 2000,
|
||||
"limit": 1,
|
||||
})
|
||||
.to_string(),
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
read_result
|
||||
.content
|
||||
.expect("Read content")
|
||||
.contains("FINAL-NEEDLE")
|
||||
);
|
||||
|
||||
let (_, grep) = grep_tool(session)();
|
||||
let grep_result = grep
|
||||
.execute(
|
||||
&serde_json::json!({
|
||||
"pattern": "FINAL-NEEDLE",
|
||||
"path": artifact,
|
||||
"output_mode": "content",
|
||||
})
|
||||
.to_string(),
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let grep_content = grep_result.content.expect("Grep content");
|
||||
assert!(
|
||||
grep_content.contains("FINAL-NEEDLE"),
|
||||
"unexpected Grep content: {grep_content:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn missing_cwd_is_rejected_before_command_start() {
|
||||
let root = TempDir::new().unwrap();
|
||||
let output = TempDir::new().unwrap();
|
||||
let session = session_with_output_scope(&root, &output);
|
||||
let (_, bash) = bash_tool(session, output.path().to_path_buf())();
|
||||
|
||||
let error = bash
|
||||
.execute(
|
||||
&serde_json::json!({ "command": "printf hidden" }).to_string(),
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(error.to_string().contains("missing field `cwd`"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn short_output_does_not_leave_a_spill_artifact() {
|
||||
let root = TempDir::new().unwrap();
|
||||
let output = TempDir::new().unwrap();
|
||||
let session = session_with_output_scope(&root, &output);
|
||||
let (_, bash) = bash_tool(session, output.path().to_path_buf())();
|
||||
|
||||
let result = bash
|
||||
.execute(
|
||||
&serde_json::json!({ "command": "printf short", "cwd": "." }).to_string(),
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.content.as_deref(), Some("short"));
|
||||
assert_eq!(std::fs::read_dir(output.path()).unwrap().count(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,599 @@
|
||||
//! Bash-owned bounded output, independent of Workdir transport and Console subscribers.
|
||||
//!
|
||||
//! Output is ephemeral evidence, not a filesystem grant. Every lookup checks the live view;
|
||||
//! children can see their own subtree and ancestors can retain evidence after child revocation.
|
||||
//! Root teardown clears all bytes. A new root (including restore) has a new incarnation and
|
||||
//! never reconstructs grants from history. Expiry is measured from admission, enforced on
|
||||
//! lookup and by a weak reaper once commands run in Tokio. Parent and all descendants share
|
||||
//! one pool: admission reserves a whole command budget, evicts the oldest terminal evidence
|
||||
//! first, and refuses rather than evicting a live collector. Revoking a child denies access
|
||||
//! but leaves ancestor evidence until pool eviction/expiry; root teardown clears the pool.
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::{
|
||||
Arc, Mutex, Weak,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use serde::Serialize;
|
||||
use workdir::{CommandStatus, CommandStream};
|
||||
|
||||
const COMMAND_BYTES: usize = 4 * 1024 * 1024;
|
||||
const STORE_BYTES: usize = 32 * 1024 * 1024;
|
||||
const STORE_COUNT: usize = 64;
|
||||
const ORDER_COUNT: usize = 4096;
|
||||
const RETENTION: Duration = Duration::from_secs(3600);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BashOutputView {
|
||||
store: Arc<Mutex<Store>>,
|
||||
lineage: Vec<String>,
|
||||
active: Arc<AtomicBool>,
|
||||
authority: Arc<dyn Fn() -> bool + Send + Sync>,
|
||||
}
|
||||
|
||||
struct Store {
|
||||
records: VecDeque<Arc<Mutex<Record>>>,
|
||||
reserved: usize,
|
||||
retention: Duration,
|
||||
reaper_started: bool,
|
||||
_shutdown: tokio::sync::watch::Sender<()>,
|
||||
}
|
||||
|
||||
struct Record {
|
||||
id: String,
|
||||
owner: Vec<String>,
|
||||
created: Instant,
|
||||
stdout: Vec<u8>,
|
||||
stderr: Vec<u8>,
|
||||
order: Vec<OutputRange>,
|
||||
stdout_seen: u64,
|
||||
stderr_seen: u64,
|
||||
truncated: bool,
|
||||
limit: Option<RetentionLimit>,
|
||||
transport_counts: Option<(u64, u64)>,
|
||||
transport_truncation: Option<workdir::CommandOutputTruncation>,
|
||||
terminal: Option<OutputTerminal>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct OutputRange {
|
||||
pub stream: CommandStream,
|
||||
pub start: u64,
|
||||
pub end: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct OutputTerminal {
|
||||
pub status: Option<CommandStatus>,
|
||||
pub exit_code: Option<i32>,
|
||||
/// False on a forcibly dropped collector or failed transport: received bytes are retained,
|
||||
/// but they are not a claim about the unobserved process tail.
|
||||
pub transport_complete: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum RetentionLimit {
|
||||
ByteQuota,
|
||||
OrderQuota,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct BashOutputMetadata {
|
||||
pub id: String,
|
||||
pub stdout_saved: usize,
|
||||
pub stderr_saved: usize,
|
||||
pub stdout_seen: u64,
|
||||
pub stderr_seen: u64,
|
||||
pub stdout_delivered: u64,
|
||||
pub stderr_delivered: u64,
|
||||
pub retention_limit: Option<RetentionLimit>,
|
||||
pub transport_truncation: Option<workdir::CommandOutputTruncation>,
|
||||
pub storage_truncated: bool,
|
||||
pub terminal: OutputTerminal,
|
||||
pub text_encoding: &'static str,
|
||||
pub ordering: &'static str,
|
||||
}
|
||||
|
||||
/// A bounded reservation acquired before command start. Drop terminalizes partial evidence.
|
||||
pub struct BashOutputWriter {
|
||||
record: Arc<Mutex<Record>>,
|
||||
}
|
||||
|
||||
impl Default for BashOutputView {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl BashOutputView {
|
||||
pub fn new() -> Self {
|
||||
let (shutdown, _) = tokio::sync::watch::channel(());
|
||||
let store = Arc::new(Mutex::new(Store {
|
||||
records: VecDeque::new(),
|
||||
reserved: 0,
|
||||
retention: RETENTION,
|
||||
reaper_started: false,
|
||||
_shutdown: shutdown,
|
||||
}));
|
||||
let view = Self {
|
||||
store,
|
||||
lineage: vec![uuid::Uuid::now_v7().to_string()],
|
||||
active: Arc::new(AtomicBool::new(true)),
|
||||
authority: Arc::new(|| true),
|
||||
};
|
||||
view.start_reaper();
|
||||
view
|
||||
}
|
||||
|
||||
fn start_reaper(&self) {
|
||||
let Ok(runtime) = tokio::runtime::Handle::try_current() else {
|
||||
return;
|
||||
};
|
||||
let mut store = self.store.lock().unwrap();
|
||||
if store.reaper_started {
|
||||
return;
|
||||
}
|
||||
let receiver = store._shutdown.subscribe();
|
||||
store.reaper_started = true;
|
||||
let weak = Arc::downgrade(&self.store);
|
||||
runtime.spawn(reap(weak, receiver));
|
||||
}
|
||||
|
||||
pub fn byte_budget(&self) -> u64 {
|
||||
COMMAND_BYTES as u64
|
||||
}
|
||||
|
||||
pub fn child(&self, authority: Arc<dyn Fn() -> bool + Send + Sync>) -> Self {
|
||||
let parent = self.clone();
|
||||
let mut lineage = self.lineage.clone();
|
||||
lineage.push(uuid::Uuid::now_v7().to_string());
|
||||
Self {
|
||||
store: self.store.clone(),
|
||||
lineage,
|
||||
active: Arc::new(AtomicBool::new(true)),
|
||||
authority: Arc::new(move || parent.is_active() && authority()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_active(&self) -> bool {
|
||||
self.active.load(Ordering::Acquire) && (self.authority)()
|
||||
}
|
||||
|
||||
pub fn is_root(&self) -> bool {
|
||||
self.lineage.len() == 1
|
||||
}
|
||||
|
||||
pub fn revoke(&self) {
|
||||
self.active.store(false, Ordering::Release);
|
||||
if self.lineage.len() == 1 {
|
||||
let mut store = self.store.lock().unwrap();
|
||||
store.records.clear();
|
||||
store.reserved = 0;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn begin(&self) -> Result<BashOutputWriter, String> {
|
||||
if !self.is_active() {
|
||||
return Err("Bash output authority revoked".into());
|
||||
}
|
||||
// A host may construct the Feature before entering Tokio. Command admission
|
||||
// happens inside the runtime, so idle expiry must start here as well.
|
||||
self.start_reaper();
|
||||
let mut store = self.store.lock().unwrap();
|
||||
store.expire(Instant::now());
|
||||
while store.records.len() >= STORE_COUNT || store.reserved + COMMAND_BYTES > STORE_BYTES {
|
||||
let Some(index) = store
|
||||
.records
|
||||
.iter()
|
||||
.position(|r| r.lock().unwrap().terminal.is_some())
|
||||
else {
|
||||
return Err("Bash output capacity is occupied by live commands".into());
|
||||
};
|
||||
store.records.remove(index);
|
||||
store.reserved -= COMMAND_BYTES;
|
||||
}
|
||||
let record = Arc::new(Mutex::new(Record {
|
||||
id: uuid::Uuid::now_v7().to_string(),
|
||||
owner: self.lineage.clone(),
|
||||
created: Instant::now(),
|
||||
stdout: Vec::new(),
|
||||
stderr: Vec::new(),
|
||||
order: Vec::new(),
|
||||
stdout_seen: 0,
|
||||
stderr_seen: 0,
|
||||
truncated: false,
|
||||
limit: None,
|
||||
transport_counts: None,
|
||||
transport_truncation: None,
|
||||
terminal: None,
|
||||
}));
|
||||
store.records.push_back(record.clone());
|
||||
store.reserved += COMMAND_BYTES;
|
||||
Ok(BashOutputWriter { record })
|
||||
}
|
||||
|
||||
fn record(&self, id: &str) -> Option<Arc<Mutex<Record>>> {
|
||||
if !self.is_active() {
|
||||
return None;
|
||||
}
|
||||
let mut store = self.store.lock().unwrap();
|
||||
store.expire(Instant::now());
|
||||
store
|
||||
.records
|
||||
.iter()
|
||||
.find(|r| {
|
||||
let r = r.lock().unwrap();
|
||||
r.id == id && r.owner.starts_with(&self.lineage) && r.terminal.is_some()
|
||||
})
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub fn metadata(&self, id: &str) -> Option<BashOutputMetadata> {
|
||||
let record = self.record(id)?;
|
||||
let r = record.lock().unwrap();
|
||||
Some(r.metadata())
|
||||
}
|
||||
|
||||
/// Copies at most the caller's bounded range; never publishes output bodies in descriptors.
|
||||
pub fn read(
|
||||
&self,
|
||||
id: &str,
|
||||
stream: CommandStream,
|
||||
offset: usize,
|
||||
limit: usize,
|
||||
) -> Option<Vec<u8>> {
|
||||
let record = self.record(id)?;
|
||||
let r = record.lock().unwrap();
|
||||
let bytes = match stream {
|
||||
CommandStream::Stdout => &r.stdout,
|
||||
CommandStream::Stderr => &r.stderr,
|
||||
};
|
||||
let start = offset.min(bytes.len());
|
||||
Some(bytes[start..start.saturating_add(limit.min(64 * 1024)).min(bytes.len())].to_vec())
|
||||
}
|
||||
|
||||
/// Scan the bounded raw source without cloning it, decoding only the requested line window.
|
||||
/// A long single line uses the common byte reader instead of allocating an oversized text page.
|
||||
pub fn line_window(
|
||||
&self,
|
||||
id: &str,
|
||||
stream: CommandStream,
|
||||
offset: usize,
|
||||
limit: usize,
|
||||
) -> Option<Result<(String, bool), String>> {
|
||||
let record = self.record(id)?;
|
||||
let r = record.lock().unwrap();
|
||||
let bytes = match stream {
|
||||
CommandStream::Stdout => &r.stdout,
|
||||
CommandStream::Stderr => &r.stderr,
|
||||
};
|
||||
let mut lines = bytes.split_inclusive(|b| *b == b'\n').skip(offset);
|
||||
let mut window = Vec::new();
|
||||
for line in lines.by_ref().take(limit.min(2000)) {
|
||||
if window.len() + line.len() > 64 * 1024 {
|
||||
return Some(Err(
|
||||
"text source window exceeds 64 KiB; use the common byte reader".into(),
|
||||
));
|
||||
}
|
||||
window.extend_from_slice(line);
|
||||
}
|
||||
let more = lines.next().is_some();
|
||||
Some(Ok((String::from_utf8_lossy(&window).into_owned(), more)))
|
||||
}
|
||||
|
||||
pub fn order(&self, id: &str) -> Option<Vec<OutputRange>> {
|
||||
let record = self.record(id)?;
|
||||
let r = record.lock().unwrap();
|
||||
Some(r.order.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl Record {
|
||||
fn metadata(&self) -> BashOutputMetadata {
|
||||
BashOutputMetadata {
|
||||
id: self.id.clone(),
|
||||
stdout_saved: self.stdout.len(),
|
||||
stderr_saved: self.stderr.len(),
|
||||
stdout_seen: self.transport_counts.map_or(self.stdout_seen, |c| c.0),
|
||||
stderr_seen: self.transport_counts.map_or(self.stderr_seen, |c| c.1),
|
||||
stdout_delivered: self.stdout_seen,
|
||||
stderr_delivered: self.stderr_seen,
|
||||
retention_limit: self.limit,
|
||||
transport_truncation: self.transport_truncation.clone(),
|
||||
storage_truncated: self.truncated || self.transport_truncation.is_some(),
|
||||
terminal: self.terminal.clone().expect("finalized record"),
|
||||
text_encoding: "utf8_lossy",
|
||||
ordering: "provider_read_observation",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Store {
|
||||
fn expire(&mut self, now: Instant) {
|
||||
let before = self.records.len();
|
||||
self.records
|
||||
.retain(|r| now.duration_since(r.lock().unwrap().created) < self.retention);
|
||||
self.reserved -= (before - self.records.len()) * COMMAND_BYTES;
|
||||
}
|
||||
}
|
||||
|
||||
async fn reap(weak: Weak<Mutex<Store>>, mut shutdown: tokio::sync::watch::Receiver<()>) {
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(Duration::from_secs(30)) => {},
|
||||
_ = shutdown.changed() => return,
|
||||
}
|
||||
let Some(store) = weak.upgrade() else {
|
||||
return;
|
||||
};
|
||||
store.lock().unwrap().expire(Instant::now());
|
||||
}
|
||||
}
|
||||
|
||||
impl BashOutputWriter {
|
||||
pub fn append_chunk(&mut self, chunk: &workdir::CommandByteChunk) -> Result<(), String> {
|
||||
let expected = {
|
||||
let r = self.record.lock().unwrap();
|
||||
match chunk.stream {
|
||||
CommandStream::Stdout => r.stdout_seen,
|
||||
CommandStream::Stderr => r.stderr_seen,
|
||||
}
|
||||
};
|
||||
if chunk.offset != expected || chunk.bytes.is_empty() {
|
||||
return Err("command byte transport stream gap or empty chunk".into());
|
||||
}
|
||||
self.append(chunk.stream, &chunk.bytes);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn append(&mut self, stream: CommandStream, bytes: &[u8]) {
|
||||
let mut r = self.record.lock().unwrap();
|
||||
let seen = match stream {
|
||||
CommandStream::Stdout => &mut r.stdout_seen,
|
||||
CommandStream::Stderr => &mut r.stderr_seen,
|
||||
};
|
||||
let start = *seen;
|
||||
*seen = seen.saturating_add(bytes.len() as u64);
|
||||
let available = COMMAND_BYTES.saturating_sub(r.stdout.len() + r.stderr.len());
|
||||
let n = if r.truncated || r.order.len() >= ORDER_COUNT {
|
||||
0
|
||||
} else {
|
||||
available.min(bytes.len())
|
||||
};
|
||||
if n < bytes.len() {
|
||||
r.truncated = true;
|
||||
if r.limit.is_none() {
|
||||
r.limit = Some(if r.order.len() >= ORDER_COUNT {
|
||||
RetentionLimit::OrderQuota
|
||||
} else {
|
||||
RetentionLimit::ByteQuota
|
||||
});
|
||||
}
|
||||
}
|
||||
if n > 0 {
|
||||
match stream {
|
||||
CommandStream::Stdout => r.stdout.extend_from_slice(&bytes[..n]),
|
||||
CommandStream::Stderr => r.stderr.extend_from_slice(&bytes[..n]),
|
||||
}
|
||||
if let Some(last) = r
|
||||
.order
|
||||
.last_mut()
|
||||
.filter(|last| last.stream == stream && last.end == start)
|
||||
{
|
||||
last.end += n as u64;
|
||||
} else {
|
||||
r.order.push(OutputRange {
|
||||
stream,
|
||||
start,
|
||||
end: start + n as u64,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish_transport(
|
||||
&mut self,
|
||||
terminal: &workdir::CommandTerminal,
|
||||
) -> Result<BashOutputMetadata, String> {
|
||||
let mut r = self.record.lock().unwrap();
|
||||
if terminal.stdout_delivered_bytes != r.stdout_seen
|
||||
|| terminal.stderr_delivered_bytes != r.stderr_seen
|
||||
|| terminal.stdout_bytes < r.stdout_seen
|
||||
|| terminal.stderr_bytes < r.stderr_seen
|
||||
|| ((terminal.stdout_bytes != r.stdout_seen || terminal.stderr_bytes != r.stderr_seen)
|
||||
!= terminal.truncation_reason.is_some())
|
||||
{
|
||||
return Err("command terminal counts do not fence delivered bytes".into());
|
||||
}
|
||||
r.transport_counts = Some((terminal.stdout_bytes, terminal.stderr_bytes));
|
||||
r.transport_truncation = terminal.truncation_reason.clone();
|
||||
r.terminal = Some(OutputTerminal {
|
||||
status: Some(terminal.status),
|
||||
exit_code: terminal.exit_code,
|
||||
transport_complete: true,
|
||||
});
|
||||
Ok(r.metadata())
|
||||
}
|
||||
|
||||
pub fn finish(&mut self, terminal: OutputTerminal) -> BashOutputMetadata {
|
||||
let mut r = self.record.lock().unwrap();
|
||||
r.terminal = Some(terminal);
|
||||
r.metadata()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BashOutputWriter {
|
||||
fn drop(&mut self) {
|
||||
let mut r = self.record.lock().unwrap();
|
||||
if r.terminal.is_none() {
|
||||
r.terminal = Some(OutputTerminal {
|
||||
status: None,
|
||||
exit_code: None,
|
||||
transport_complete: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn finish(view: &BashOutputView, text: &[u8]) -> String {
|
||||
let mut writer = view.begin().unwrap();
|
||||
writer.append(CommandStream::Stdout, text);
|
||||
writer
|
||||
.finish(OutputTerminal {
|
||||
status: Some(CommandStatus::Completed),
|
||||
exit_code: Some(0),
|
||||
transport_complete: true,
|
||||
})
|
||||
.id
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn revoked_child_is_denied_while_parent_keeps_evidence_and_sibling_is_denied() {
|
||||
let root = BashOutputView::new();
|
||||
let lease = Arc::new(AtomicBool::new(true));
|
||||
let child = root.child({
|
||||
let lease = lease.clone();
|
||||
Arc::new(move || lease.load(Ordering::Acquire))
|
||||
});
|
||||
let sibling = root.child(Arc::new(|| true));
|
||||
let id = finish(&child, b"child");
|
||||
assert_eq!(
|
||||
child.read(&id, CommandStream::Stdout, 0, 20).unwrap(),
|
||||
b"child"
|
||||
);
|
||||
assert!(sibling.metadata(&id).is_none());
|
||||
assert!(BashOutputView::new().metadata(&id).is_none());
|
||||
lease.store(false, Ordering::Release);
|
||||
assert!(child.metadata(&id).is_none());
|
||||
assert_eq!(
|
||||
root.read(&id, CommandStream::Stdout, 0, 20).unwrap(),
|
||||
b"child"
|
||||
);
|
||||
root.revoke();
|
||||
let restored = BashOutputView::new();
|
||||
assert!(root.metadata(&id).is_none());
|
||||
assert!(restored.metadata(&id).is_none());
|
||||
let new_id = finish(&restored, b"new incarnation");
|
||||
assert_ne!(new_id, id);
|
||||
assert!(child.metadata(&new_id).is_none());
|
||||
assert!(root.begin().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parent_and_descendants_share_admission_without_evicting_live_evidence() {
|
||||
let root = BashOutputView::new();
|
||||
let child = root.child(Arc::new(|| true));
|
||||
let sibling = root.child(Arc::new(|| true));
|
||||
let mut writers = (0..STORE_BYTES / COMMAND_BYTES)
|
||||
.map(|index| {
|
||||
if index % 2 == 0 {
|
||||
root.begin()
|
||||
} else {
|
||||
child.begin()
|
||||
}
|
||||
.unwrap()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert!(sibling.begin().is_err());
|
||||
let mut first = writers.remove(0);
|
||||
first.append(CommandStream::Stdout, b"finished");
|
||||
let metadata = first.finish(OutputTerminal {
|
||||
status: Some(CommandStatus::Completed),
|
||||
exit_code: Some(0),
|
||||
transport_complete: true,
|
||||
});
|
||||
assert!(root.metadata(&metadata.id).is_some());
|
||||
let _replacement = sibling.begin().unwrap();
|
||||
assert!(root.metadata(&metadata.id).is_none());
|
||||
assert!(root.begin().is_err());
|
||||
assert_eq!(writers.len(), STORE_BYTES / COMMAND_BYTES - 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quota_and_forced_drop_are_distinct_from_preview_omission() {
|
||||
let root = BashOutputView::new();
|
||||
let mut writer = root.begin().unwrap();
|
||||
writer.append(CommandStream::Stdout, &vec![b'x'; COMMAND_BYTES + 1]);
|
||||
writer.append(CommandStream::Stderr, b"tail");
|
||||
let id = writer.record.lock().unwrap().id.clone();
|
||||
drop(writer);
|
||||
let m = root.metadata(&id).unwrap();
|
||||
assert_eq!(m.stdout_saved, COMMAND_BYTES);
|
||||
assert_eq!(m.stdout_seen, COMMAND_BYTES as u64 + 1);
|
||||
assert_eq!(m.stderr_seen, 4);
|
||||
assert!(m.storage_truncated);
|
||||
assert!(!m.terminal.transport_complete);
|
||||
assert_eq!(
|
||||
root.read(&id, CommandStream::Stdout, 0, usize::MAX)
|
||||
.unwrap()
|
||||
.len(),
|
||||
64 * 1024
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expiry_and_count_eviction_never_reuse_old_references() {
|
||||
let root = BashOutputView::new();
|
||||
let id = finish(&root, b"old");
|
||||
root.store.lock().unwrap().retention = Duration::ZERO;
|
||||
assert!(root.metadata(&id).is_none());
|
||||
root.store.lock().unwrap().retention = RETENTION;
|
||||
let first = finish(&root, b"first");
|
||||
for _ in 0..STORE_COUNT {
|
||||
finish(&root, b"later");
|
||||
}
|
||||
assert!(root.metadata(&first).is_none());
|
||||
assert!(root.store.lock().unwrap().reserved <= STORE_BYTES);
|
||||
}
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn expiry_reclaims_bytes_without_another_read_and_reaper_ends_with_owner() {
|
||||
// Exercise host construction outside a runtime, followed by real async admission.
|
||||
let root = std::thread::spawn(BashOutputView::new).join().unwrap();
|
||||
finish(&root, b"expires without lookup");
|
||||
let mut closed = root.store.lock().unwrap()._shutdown.subscribe();
|
||||
tokio::task::yield_now().await;
|
||||
// The store uses monotonic wall time for publication and Tokio time only for scheduling.
|
||||
// Age the record deterministically rather than delaying this test in real time.
|
||||
root.store.lock().unwrap().records[0]
|
||||
.lock()
|
||||
.unwrap()
|
||||
.created = Instant::now() - RETENTION;
|
||||
tokio::time::advance(Duration::from_secs(31)).await;
|
||||
tokio::task::yield_now().await;
|
||||
assert_eq!(root.store.lock().unwrap().reserved, 0);
|
||||
drop(root);
|
||||
assert!(closed.changed().await.is_err());
|
||||
}
|
||||
#[test]
|
||||
fn terminal_counts_must_fence_the_received_prefix_before_marking_evidence_complete() {
|
||||
let root = BashOutputView::new();
|
||||
let mut writer = root.begin().unwrap();
|
||||
writer.append(CommandStream::Stdout, b"abc");
|
||||
let mut terminal = serde_json::json!({"status":"completed","exit_code":0,"stdout_bytes":9,"stderr_bytes":0,"stdout_delivered_bytes":4,"stderr_delivered_bytes":0,"truncation_reason":"byte_budget"});
|
||||
assert!(
|
||||
writer
|
||||
.finish_transport(&serde_json::from_value(terminal.clone()).unwrap())
|
||||
.is_err()
|
||||
);
|
||||
terminal["stdout_delivered_bytes"] = serde_json::json!(3);
|
||||
let metadata = writer
|
||||
.finish_transport(&serde_json::from_value(terminal).unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(metadata.stdout_saved, 3);
|
||||
assert_eq!(metadata.stdout_delivered, 3);
|
||||
assert_eq!(metadata.stdout_seen, 9);
|
||||
assert!(metadata.storage_truncated);
|
||||
assert!(metadata.retention_limit.is_none());
|
||||
assert_eq!(
|
||||
metadata.transport_truncation,
|
||||
Some(workdir::CommandOutputTruncation::ByteBudget)
|
||||
);
|
||||
}
|
||||
}
|
||||
+10
-9
@@ -5,6 +5,7 @@ pub mod error;
|
||||
pub mod tracker;
|
||||
|
||||
mod bash;
|
||||
pub mod bash_output;
|
||||
mod edit;
|
||||
mod file_target;
|
||||
mod glob;
|
||||
@@ -16,7 +17,7 @@ pub mod view_image;
|
||||
mod web;
|
||||
mod write;
|
||||
|
||||
pub use bash::bash_tool;
|
||||
pub use bash::{BashResult, BashTool, bash_tool};
|
||||
pub use checkout::{CheckoutToolOutput, execute_checkout_tool};
|
||||
pub use edit::edit_tool;
|
||||
pub use error::ToolsError;
|
||||
@@ -35,7 +36,7 @@ pub use write::write_tool;
|
||||
pub fn core_builtin_tools(
|
||||
session: workdir::WorkdirSessionHandle,
|
||||
tracker: Tracker,
|
||||
bash_output_dir: std::path::PathBuf,
|
||||
bash_output: bash_output::BashOutputView,
|
||||
) -> Vec<agen::tool::ToolDefinition> {
|
||||
use workdir::WorkdirSessionCapability;
|
||||
|
||||
@@ -58,7 +59,7 @@ pub fn core_builtin_tools(
|
||||
tools.push(grep::routed_grep_tool(router.clone()));
|
||||
}
|
||||
if capabilities.supports(WorkdirSessionCapability::Command) {
|
||||
tools.push(bash::routed_bash_tool(router, bash_output_dir));
|
||||
tools.push(bash::routed_bash_tool(router, bash_output));
|
||||
}
|
||||
tools
|
||||
}
|
||||
@@ -71,7 +72,7 @@ pub fn core_builtin_tools(
|
||||
pub fn routed_builtin_tools(
|
||||
router: std::sync::Arc<workdir::WorkdirSessionRouter>,
|
||||
tracker: Tracker,
|
||||
bash_output_dir: std::path::PathBuf,
|
||||
bash_output: bash_output::BashOutputView,
|
||||
) -> Vec<agen::tool::ToolDefinition> {
|
||||
vec![
|
||||
read::routed_read_tool(router.clone(), tracker.clone()),
|
||||
@@ -79,14 +80,14 @@ pub fn routed_builtin_tools(
|
||||
edit::routed_edit_tool(router.clone(), tracker),
|
||||
glob::routed_glob_tool(router.clone()),
|
||||
grep::routed_grep_tool(router.clone()),
|
||||
bash::routed_bash_tool(router, bash_output_dir),
|
||||
bash::routed_bash_tool(router, bash_output),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn routed_builtin_tools_for_current_capabilities(
|
||||
router: std::sync::Arc<workdir::WorkdirSessionRouter>,
|
||||
tracker: Tracker,
|
||||
bash_output_dir: std::path::PathBuf,
|
||||
bash_output: bash_output::BashOutputView,
|
||||
) -> Vec<agen::tool::ToolDefinition> {
|
||||
use workdir::WorkdirSessionCapability;
|
||||
|
||||
@@ -108,7 +109,7 @@ pub fn routed_builtin_tools_for_current_capabilities(
|
||||
tools.push(grep::routed_grep_tool(router.clone()));
|
||||
}
|
||||
if capabilities.supports(WorkdirSessionCapability::Command) {
|
||||
tools.push(bash::routed_bash_tool(router, bash_output_dir));
|
||||
tools.push(bash::routed_bash_tool(router, bash_output));
|
||||
}
|
||||
tools
|
||||
}
|
||||
@@ -121,7 +122,7 @@ pub fn read_only_builtin_tools(
|
||||
workdir::WorkdirSessionCapabilities::READ_ONLY,
|
||||
"read-only tool projection requires a read-only Workdir session"
|
||||
);
|
||||
core_builtin_tools(session, Tracker::new(), std::path::PathBuf::new())
|
||||
core_builtin_tools(session, Tracker::new(), bash_output::BashOutputView::new())
|
||||
}
|
||||
|
||||
pub fn read_only_routed_builtin_tools(
|
||||
@@ -173,7 +174,7 @@ mod workdir_tool_tests {
|
||||
SharedScope::new(Scope::writable(dir.path()).unwrap()),
|
||||
WorkdirSessionCapabilities::READ_ONLY,
|
||||
));
|
||||
let names = core_builtin_tools(session, Tracker::new(), dir.path().join("output"))
|
||||
let names = core_builtin_tools(session, Tracker::new(), bash_output::BashOutputView::new())
|
||||
.into_iter()
|
||||
.map(|definition| definition().0.name)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use agen::tool::{Tool, ToolDefinition};
|
||||
use manifest::{Permission, Scope, ScopeConfig, ScopeRule};
|
||||
use manifest::Scope;
|
||||
use serde_json::json;
|
||||
use tempfile::TempDir;
|
||||
use tools::{Tracker, core_builtin_tools};
|
||||
@@ -28,31 +28,23 @@ impl Registry {
|
||||
}
|
||||
}
|
||||
|
||||
fn setup() -> (TempDir, TempDir, Registry) {
|
||||
fn setup() -> (TempDir, Registry) {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
let base = Scope::writable(dir.path()).unwrap();
|
||||
let mut config = ScopeConfig {
|
||||
allow: base.allow_rules(),
|
||||
deny: base.deny_rules(),
|
||||
};
|
||||
config.allow.push(ScopeRule {
|
||||
target: spill.path().to_path_buf(),
|
||||
permission: Permission::Read,
|
||||
recursive: true,
|
||||
symlink_policy: Default::default(),
|
||||
});
|
||||
let scope = Scope::from_config(&config).unwrap();
|
||||
let scope = Scope::writable(dir.path()).unwrap();
|
||||
let fs: WorkdirSessionHandle =
|
||||
std::sync::Arc::new(LocalWorkdirSession::new(scope, dir.path().to_path_buf()));
|
||||
let tracker = Tracker::new();
|
||||
let reg = Registry::new(core_builtin_tools(fs, tracker, spill.path().to_path_buf()));
|
||||
(dir, spill, reg)
|
||||
let reg = Registry::new(core_builtin_tools(
|
||||
fs,
|
||||
tracker,
|
||||
tools::bash_output::BashOutputView::new(),
|
||||
));
|
||||
(dir, reg)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unicode_path_and_content() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let file = dir.path().join("日本語ファイル.txt");
|
||||
let content = "こんにちは 🦀 世界\nabc\n";
|
||||
|
||||
@@ -87,7 +79,7 @@ async fn unicode_path_and_content() {
|
||||
async fn symlink_to_outside_scope_is_rejected_for_write() {
|
||||
use std::os::unix::fs::symlink;
|
||||
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let outside = TempDir::new().unwrap();
|
||||
let outside_target = outside.path().join("secret.txt");
|
||||
std::fs::write(&outside_target, "secret").unwrap();
|
||||
@@ -151,7 +143,7 @@ async fn symlink_to_outside_scope_is_rejected_for_write() {
|
||||
async fn broken_symlink_reports_target_and_repair_hint() {
|
||||
use std::os::unix::fs::symlink;
|
||||
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let link = dir.path().join("external-project");
|
||||
let target = dir.path().join("missing-target");
|
||||
symlink(&target, &link).unwrap();
|
||||
@@ -173,7 +165,7 @@ async fn broken_symlink_reports_target_and_repair_hint() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn empty_file_read_and_edit() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let file = dir.path().join("empty.txt");
|
||||
std::fs::write(&file, "").unwrap();
|
||||
|
||||
@@ -207,7 +199,7 @@ async fn empty_file_read_and_edit() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn very_long_single_line() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let file = dir.path().join("long.txt");
|
||||
let big: String = "x".repeat(1024 * 1024); // 1 MiB, no newlines
|
||||
std::fs::write(&file, &big).unwrap();
|
||||
@@ -226,7 +218,7 @@ async fn very_long_single_line() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn absolute_path_requires_matching_read_scope() {
|
||||
let (_dir, _spill, reg) = setup();
|
||||
let (_dir, reg) = setup();
|
||||
let outside = tempfile::tempdir().unwrap();
|
||||
let outside_file = outside.path().join("outside.txt");
|
||||
std::fs::write(&outside_file, "secret").unwrap();
|
||||
@@ -247,7 +239,7 @@ async fn absolute_path_requires_matching_read_scope() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn directory_target_is_rejected_for_read() {
|
||||
let (_dir, _spill, reg) = setup();
|
||||
let (_dir, reg) = setup();
|
||||
let read = reg.get("Read");
|
||||
let err = read
|
||||
.execute(&json!({ "file_path": "." }).to_string(), Default::default())
|
||||
@@ -258,7 +250,7 @@ async fn directory_target_is_rejected_for_read() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn deeply_nested_new_file_is_created() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let deep = dir.path().join("a/b/c/d/e/deep.txt");
|
||||
let write = reg.get("Write");
|
||||
write
|
||||
@@ -277,7 +269,7 @@ async fn deeply_nested_new_file_is_created() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn replace_preserves_unicode() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let file = dir.path().join("u.txt");
|
||||
std::fs::write(&file, "🦀 rust 🦀\n").unwrap();
|
||||
|
||||
@@ -306,7 +298,7 @@ async fn replace_preserves_unicode() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn grep_handles_unicode_pattern() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let file = dir.path().join("u.txt");
|
||||
std::fs::write(&file, "English\n日本語\nрусский\n").unwrap();
|
||||
|
||||
|
||||
+349
-153
@@ -11,7 +11,7 @@ use agen::tool::{
|
||||
Tool, ToolDefinition, ToolError, ToolExecutionContext, ToolExecutionHandle,
|
||||
ToolExecutionTerminal, ToolMeta,
|
||||
};
|
||||
use manifest::{Permission, Scope, ScopeConfig, ScopeRule};
|
||||
use manifest::Scope;
|
||||
use serde_json::json;
|
||||
use tempfile::TempDir;
|
||||
use tools::{
|
||||
@@ -23,21 +23,6 @@ use workdir::{
|
||||
WorkdirToolScopeRule,
|
||||
};
|
||||
|
||||
fn scope_with_spill(workspace: &Path, spill: &Path) -> Scope {
|
||||
let base = Scope::writable(workspace).unwrap();
|
||||
let mut config = ScopeConfig {
|
||||
allow: base.allow_rules(),
|
||||
deny: base.deny_rules(),
|
||||
};
|
||||
config.allow.push(ScopeRule {
|
||||
target: spill.to_path_buf(),
|
||||
permission: Permission::Read,
|
||||
recursive: true,
|
||||
symlink_policy: Default::default(),
|
||||
});
|
||||
Scope::from_config(&config).unwrap()
|
||||
}
|
||||
|
||||
struct Registry {
|
||||
entries: Vec<(ToolMeta, Arc<dyn Tool>)>,
|
||||
}
|
||||
@@ -61,39 +46,38 @@ impl Registry {
|
||||
}
|
||||
}
|
||||
|
||||
fn setup() -> (TempDir, TempDir, Registry) {
|
||||
fn setup() -> (TempDir, Registry) {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
let scope = scope_with_spill(dir.path(), spill.path());
|
||||
let scope = Scope::writable(dir.path()).unwrap();
|
||||
let fs: WorkdirSessionHandle =
|
||||
Arc::new(LocalWorkdirSession::new(scope, dir.path().to_path_buf()));
|
||||
let tracker = Tracker::new();
|
||||
let reg = Registry::new(core_builtin_tools(fs, tracker, spill.path().to_path_buf()));
|
||||
(dir, spill, reg)
|
||||
let reg = Registry::new(core_builtin_tools(
|
||||
fs,
|
||||
tracker,
|
||||
tools::bash_output::BashOutputView::new(),
|
||||
));
|
||||
(dir, reg)
|
||||
}
|
||||
|
||||
fn setup_routed() -> (
|
||||
TempDir,
|
||||
TempDir,
|
||||
TempDir,
|
||||
Arc<WorkdirSessionRouter>,
|
||||
Registry,
|
||||
) {
|
||||
fn setup_routed() -> (TempDir, TempDir, Arc<WorkdirSessionRouter>, Registry) {
|
||||
let left = TempDir::new().unwrap();
|
||||
let right = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
let router = Arc::new(WorkdirSessionRouter::new());
|
||||
for (alias, dir) in [("left", &left), ("right", &right)] {
|
||||
let scope = scope_with_spill(dir.path(), spill.path());
|
||||
let scope = Scope::writable(dir.path()).unwrap();
|
||||
let session: WorkdirSessionHandle =
|
||||
Arc::new(LocalWorkdirSession::new(scope, dir.path().to_path_buf()));
|
||||
router
|
||||
.attach(WorkdirAttachmentAlias::new(alias).unwrap(), session)
|
||||
.unwrap();
|
||||
}
|
||||
let definitions =
|
||||
routed_builtin_tools(router.clone(), Tracker::new(), spill.path().to_path_buf());
|
||||
(left, right, spill, router, Registry::new(definitions))
|
||||
let definitions = routed_builtin_tools(
|
||||
router.clone(),
|
||||
Tracker::new(),
|
||||
tools::bash_output::BashOutputView::new(),
|
||||
);
|
||||
(left, right, router, Registry::new(definitions))
|
||||
}
|
||||
|
||||
fn valid_gif_with_len(target_len: usize) -> Vec<u8> {
|
||||
@@ -137,7 +121,7 @@ async fn call_err(tool: &Arc<dyn Tool>, input: serde_json::Value) -> agen::tool:
|
||||
|
||||
#[test]
|
||||
fn core_builtin_tools_registers_full_set() {
|
||||
let (_dir, _spill, reg) = setup();
|
||||
let (_dir, reg) = setup();
|
||||
let mut names = reg.names();
|
||||
names.sort();
|
||||
assert_eq!(names, vec!["Bash", "Edit", "Glob", "Grep", "Read", "Write"]);
|
||||
@@ -145,7 +129,7 @@ fn core_builtin_tools_registers_full_set() {
|
||||
|
||||
#[test]
|
||||
fn meta_has_description_and_schema() {
|
||||
let (_dir, _spill, reg) = setup();
|
||||
let (_dir, reg) = setup();
|
||||
for (meta, _) in ®.entries {
|
||||
assert!(
|
||||
!meta.description.is_empty(),
|
||||
@@ -163,7 +147,7 @@ fn meta_has_description_and_schema() {
|
||||
|
||||
#[test]
|
||||
fn routed_tool_schemas_share_optional_target_workdir() {
|
||||
let (_left, _right, _spill, router, mut reg) = setup_routed();
|
||||
let (_left, _right, router, mut reg) = setup_routed();
|
||||
reg.entries.push(routed_view_image_tool(router)());
|
||||
for (meta, _) in ®.entries {
|
||||
let target = &meta.input_schema["properties"]["target_workdir"];
|
||||
@@ -174,7 +158,7 @@ fn routed_tool_schemas_share_optional_target_workdir() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn routed_tools_require_alias_only_when_attachment_set_is_ambiguous() {
|
||||
let (_left, _right, _spill, router, reg) = setup_routed();
|
||||
let (_left, _right, router, reg) = setup_routed();
|
||||
let error = call_err(®.get("Read"), json!({ "file_path": "same.txt" })).await;
|
||||
let message = error.to_string();
|
||||
assert!(message.contains("target_workdir_required"), "{message}");
|
||||
@@ -199,7 +183,7 @@ async fn empty_routed_surface_returns_no_attachment_error() {
|
||||
let reg = Registry::new(routed_builtin_tools(
|
||||
router,
|
||||
Tracker::new(),
|
||||
Path::new("unused").to_path_buf(),
|
||||
tools::bash_output::BashOutputView::new(),
|
||||
));
|
||||
let message = call_err(®.get("Read"), json!({ "file_path": "file.txt" }))
|
||||
.await
|
||||
@@ -209,7 +193,7 @@ async fn empty_routed_surface_returns_no_attachment_error() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn target_alias_routes_each_tool_and_isolates_read_before_edit() {
|
||||
let (left, right, _spill, router, mut reg) = setup_routed();
|
||||
let (left, right, router, mut reg) = setup_routed();
|
||||
std::fs::write(left.path().join("same.txt"), "left value\n").unwrap();
|
||||
std::fs::write(right.path().join("same.txt"), "right value\n").unwrap();
|
||||
std::fs::write(left.path().join("only-left.log"), "LEFT-NEEDLE\n").unwrap();
|
||||
@@ -282,7 +266,7 @@ async fn target_alias_routes_each_tool_and_isolates_read_before_edit() {
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
std::fs::canonicalize(bash.content.unwrap().trim()).unwrap(),
|
||||
std::fs::canonicalize(serde_json::from_str::<serde_json::Value>(&bash.content.unwrap()).unwrap()["stdout_preview"].as_str().unwrap().trim()).unwrap(),
|
||||
std::fs::canonicalize(right.path()).unwrap()
|
||||
);
|
||||
|
||||
@@ -304,13 +288,12 @@ async fn target_alias_routes_each_tool_and_isolates_read_before_edit() {
|
||||
async fn selected_attachment_capability_is_checked_without_fallback() {
|
||||
let left = TempDir::new().unwrap();
|
||||
let right = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
let router = Arc::new(WorkdirSessionRouter::new());
|
||||
router
|
||||
.attach(
|
||||
WorkdirAttachmentAlias::new("writable").unwrap(),
|
||||
Arc::new(LocalWorkdirSession::new(
|
||||
scope_with_spill(left.path(), spill.path()),
|
||||
Scope::writable(left.path()).unwrap(),
|
||||
left.path().to_path_buf(),
|
||||
)),
|
||||
)
|
||||
@@ -322,7 +305,7 @@ async fn selected_attachment_capability_is_checked_without_fallback() {
|
||||
workdir::Workdir::new("readonly"),
|
||||
right.path().to_path_buf(),
|
||||
right.path().to_path_buf(),
|
||||
manifest::SharedScope::new(scope_with_spill(right.path(), spill.path())),
|
||||
manifest::SharedScope::new(Scope::writable(right.path()).unwrap()),
|
||||
workdir::WorkdirSessionCapabilities::READ_ONLY,
|
||||
)),
|
||||
)
|
||||
@@ -330,7 +313,7 @@ async fn selected_attachment_capability_is_checked_without_fallback() {
|
||||
let reg = Registry::new(routed_builtin_tools(
|
||||
router.clone(),
|
||||
Tracker::new(),
|
||||
spill.path().to_path_buf(),
|
||||
tools::bash_output::BashOutputView::new(),
|
||||
));
|
||||
|
||||
let message = call_err(
|
||||
@@ -352,7 +335,7 @@ async fn selected_attachment_capability_is_checked_without_fallback() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn routed_tools_observe_detach_after_registration() {
|
||||
let (_left, _right, _spill, router, reg) = setup_routed();
|
||||
let (_left, _right, router, reg) = setup_routed();
|
||||
let alias = WorkdirAttachmentAlias::new("left").unwrap();
|
||||
router.detach(&alias).await.unwrap();
|
||||
|
||||
@@ -372,7 +355,6 @@ async fn routed_tools_observe_detach_after_registration() {
|
||||
async fn detach_and_reattach_same_alias_requires_a_fresh_read() {
|
||||
let first = TempDir::new().unwrap();
|
||||
let second = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
std::fs::write(first.path().join("same.txt"), "same bytes\n").unwrap();
|
||||
std::fs::write(second.path().join("same.txt"), "same bytes\n").unwrap();
|
||||
|
||||
@@ -382,7 +364,7 @@ async fn detach_and_reattach_same_alias_requires_a_fresh_read() {
|
||||
.attach(
|
||||
alias.clone(),
|
||||
Arc::new(LocalWorkdirSession::new(
|
||||
scope_with_spill(first.path(), spill.path()),
|
||||
Scope::writable(first.path()).unwrap(),
|
||||
first.path().to_path_buf(),
|
||||
)),
|
||||
)
|
||||
@@ -390,7 +372,7 @@ async fn detach_and_reattach_same_alias_requires_a_fresh_read() {
|
||||
let reg = Registry::new(routed_builtin_tools(
|
||||
router.clone(),
|
||||
Tracker::new(),
|
||||
spill.path().to_path_buf(),
|
||||
tools::bash_output::BashOutputView::new(),
|
||||
));
|
||||
call(
|
||||
®.get("Read"),
|
||||
@@ -403,7 +385,7 @@ async fn detach_and_reattach_same_alias_requires_a_fresh_read() {
|
||||
.attach(
|
||||
alias.clone(),
|
||||
Arc::new(LocalWorkdirSession::new(
|
||||
scope_with_spill(second.path(), spill.path()),
|
||||
Scope::writable(second.path()).unwrap(),
|
||||
second.path().to_path_buf(),
|
||||
)),
|
||||
)
|
||||
@@ -432,6 +414,8 @@ async fn detach_and_reattach_same_alias_requires_a_fresh_read() {
|
||||
struct ExternalValidatingSession {
|
||||
inner: LocalWorkdirSession,
|
||||
truncate_first_binary_response: bool,
|
||||
command_response_losses: std::sync::atomic::AtomicUsize,
|
||||
lose_terminal_only: bool,
|
||||
mutate_after_first_binary_response: Option<std::path::PathBuf>,
|
||||
}
|
||||
|
||||
@@ -550,7 +534,23 @@ impl workdir::WorkdirSession for ExternalValidatingSession {
|
||||
&self,
|
||||
request: workdir::CommandOutputRequest,
|
||||
) -> Result<workdir::CommandOutput, workdir::WorkdirError> {
|
||||
self.inner.command_output(request).await
|
||||
let output = self.inner.command_output(request).await?;
|
||||
if (!self.lose_terminal_only || output.terminal.is_some())
|
||||
&& self
|
||||
.command_response_losses
|
||||
.fetch_update(
|
||||
std::sync::atomic::Ordering::AcqRel,
|
||||
std::sync::atomic::Ordering::Acquire,
|
||||
|remaining| remaining.checked_sub(1),
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
// The production provider has committed a page, but its response is lost.
|
||||
return Err(workdir::WorkdirError::Transport(
|
||||
"injected lost response".into(),
|
||||
));
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
async fn cancel_command(
|
||||
@@ -568,11 +568,12 @@ impl workdir::WorkdirSession for ExternalValidatingSession {
|
||||
#[tokio::test]
|
||||
async fn external_view_image_uses_a_protocol_bounded_request() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
let scope = scope_with_spill(dir.path(), spill.path());
|
||||
let scope = Scope::writable(dir.path()).unwrap();
|
||||
let session: WorkdirSessionHandle = Arc::new(ExternalValidatingSession {
|
||||
inner: LocalWorkdirSession::new(scope, dir.path().to_path_buf()),
|
||||
truncate_first_binary_response: false,
|
||||
command_response_losses: std::sync::atomic::AtomicUsize::new(0),
|
||||
lose_terminal_only: false,
|
||||
mutate_after_first_binary_response: None,
|
||||
});
|
||||
let png = include_bytes!("fixtures/view-image/valid.png");
|
||||
@@ -590,13 +591,14 @@ async fn external_view_image_uses_a_protocol_bounded_request() {
|
||||
#[tokio::test]
|
||||
async fn external_view_image_rejects_truncated_and_cross_version_chunks() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
let image_path = dir.path().join("image.png");
|
||||
let scope = scope_with_spill(dir.path(), spill.path());
|
||||
let scope = Scope::writable(dir.path()).unwrap();
|
||||
std::fs::write(&image_path, include_bytes!("fixtures/view-image/valid.png")).unwrap();
|
||||
let truncated: WorkdirSessionHandle = Arc::new(ExternalValidatingSession {
|
||||
inner: LocalWorkdirSession::new(scope.clone(), dir.path().to_path_buf()),
|
||||
truncate_first_binary_response: true,
|
||||
command_response_losses: std::sync::atomic::AtomicUsize::new(0),
|
||||
lose_terminal_only: false,
|
||||
mutate_after_first_binary_response: None,
|
||||
});
|
||||
let (_meta, tool) = view_image_tool(truncated)();
|
||||
@@ -613,6 +615,8 @@ async fn external_view_image_rejects_truncated_and_cross_version_chunks() {
|
||||
let changing: WorkdirSessionHandle = Arc::new(ExternalValidatingSession {
|
||||
inner: LocalWorkdirSession::new(scope, dir.path().to_path_buf()),
|
||||
truncate_first_binary_response: false,
|
||||
command_response_losses: std::sync::atomic::AtomicUsize::new(0),
|
||||
lose_terminal_only: false,
|
||||
mutate_after_first_binary_response: Some(image_path),
|
||||
});
|
||||
let (_meta, tool) = view_image_tool(changing)();
|
||||
@@ -626,8 +630,7 @@ async fn external_view_image_rejects_truncated_and_cross_version_chunks() {
|
||||
async fn view_image_enforces_the_ten_mib_boundary_without_partial_attachments() {
|
||||
const LIMIT: usize = 10 * 1024 * 1024;
|
||||
let dir = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
let scope = scope_with_spill(dir.path(), spill.path());
|
||||
let scope = Scope::writable(dir.path()).unwrap();
|
||||
let session: WorkdirSessionHandle =
|
||||
Arc::new(LocalWorkdirSession::new(scope, dir.path().to_path_buf()));
|
||||
let (_meta, tool) = view_image_tool(session)();
|
||||
@@ -650,8 +653,7 @@ async fn view_image_enforces_the_ten_mib_boundary_without_partial_attachments()
|
||||
#[tokio::test]
|
||||
async fn view_image_attaches_valid_supported_image_fixtures() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
let scope = scope_with_spill(dir.path(), spill.path());
|
||||
let scope = Scope::writable(dir.path()).unwrap();
|
||||
let session: WorkdirSessionHandle =
|
||||
Arc::new(LocalWorkdirSession::new(scope, dir.path().to_path_buf()));
|
||||
let (_meta, tool) = view_image_tool(session)();
|
||||
@@ -726,8 +728,7 @@ async fn delegated_view_image_preserves_caller_relative_path_identity() {
|
||||
#[tokio::test]
|
||||
async fn view_image_reads_scoped_bytes_into_durable_tool_detail() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
let scope = scope_with_spill(dir.path(), spill.path());
|
||||
let scope = Scope::writable(dir.path()).unwrap();
|
||||
let session: WorkdirSessionHandle =
|
||||
Arc::new(LocalWorkdirSession::new(scope, dir.path().to_path_buf()));
|
||||
let png = include_bytes!("fixtures/view-image/valid.png");
|
||||
@@ -752,7 +753,7 @@ async fn view_image_reads_scoped_bytes_into_durable_tool_detail() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_then_edit_then_read_roundtrip() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let file = dir.path().join("a.txt");
|
||||
std::fs::write(&file, "hello world\n").unwrap();
|
||||
let p = "a.txt";
|
||||
@@ -784,7 +785,7 @@ async fn read_then_edit_then_read_roundtrip() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn write_then_grep_finds_content() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let write = reg.get("Write");
|
||||
let grep = reg.get("Grep");
|
||||
|
||||
@@ -813,7 +814,7 @@ async fn write_then_grep_finds_content() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn glob_finds_written_files() {
|
||||
let (_dir, _spill, reg) = setup();
|
||||
let (_dir, reg) = setup();
|
||||
let write = reg.get("Write");
|
||||
let glob = reg.get("Glob");
|
||||
|
||||
@@ -837,7 +838,7 @@ async fn glob_finds_written_files() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn absolute_path_is_rejected() {
|
||||
let (_dir, _spill, reg) = setup();
|
||||
let (_dir, reg) = setup();
|
||||
let outside = TempDir::new().unwrap();
|
||||
let write = reg.get("Write");
|
||||
|
||||
@@ -859,7 +860,7 @@ async fn absolute_path_is_rejected() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn write_to_existing_without_read_fails() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let file = dir.path().join("exists.txt");
|
||||
std::fs::write(&file, "preexisting").unwrap();
|
||||
|
||||
@@ -880,7 +881,7 @@ async fn write_to_existing_without_read_fails() {
|
||||
async fn shared_workdir_across_tools() {
|
||||
// The key invariant: all builtin tools share the same WorkdirSession instance,
|
||||
// so read-history set by Read is visible to Edit and Write.
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let file = dir.path().join("shared.txt");
|
||||
std::fs::write(&file, "one\n").unwrap();
|
||||
|
||||
@@ -907,7 +908,7 @@ async fn shared_workdir_across_tools() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn edit_requires_read_across_tools() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let file = dir.path().join("a.txt");
|
||||
std::fs::write(&file, "foo\n").unwrap();
|
||||
|
||||
@@ -928,7 +929,7 @@ async fn edit_requires_read_across_tools() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn deterministic_tool_order_is_registration_order() {
|
||||
let (_dir, _spill, reg) = setup();
|
||||
let (_dir, reg) = setup();
|
||||
// Registration order from core_builtin_tools(): Read, Write, Edit, Glob, Grep, Bash
|
||||
let names: Vec<&str> = reg.entries.iter().map(|(m, _)| m.name.as_str()).collect();
|
||||
assert_eq!(
|
||||
@@ -940,7 +941,7 @@ async fn deterministic_tool_order_is_registration_order() {
|
||||
// Regression: tool name capitalization matches Claude Code reference
|
||||
#[test]
|
||||
fn tool_names_match_reference_spec() {
|
||||
let (_dir, _spill, reg) = setup();
|
||||
let (_dir, reg) = setup();
|
||||
for expected in ["Read", "Write", "Edit", "Glob", "Grep", "Bash"] {
|
||||
assert!(
|
||||
reg.entries.iter().any(|(m, _)| m.name == expected),
|
||||
@@ -953,15 +954,14 @@ fn tool_names_match_reference_spec() {
|
||||
async fn tracker_recent_files_tracks_read_write_edit() {
|
||||
// Build a fresh registry that shares a tracker we can query afterwards.
|
||||
let dir = TempDir::new().unwrap();
|
||||
let spill = TempDir::new().unwrap();
|
||||
let scope = scope_with_spill(dir.path(), spill.path());
|
||||
let scope = Scope::writable(dir.path()).unwrap();
|
||||
let fs: WorkdirSessionHandle =
|
||||
Arc::new(LocalWorkdirSession::new(scope, dir.path().to_path_buf()));
|
||||
let tracker = Tracker::new();
|
||||
let reg = Registry::new(core_builtin_tools(
|
||||
fs,
|
||||
tracker.clone(),
|
||||
spill.path().to_path_buf(),
|
||||
tools::bash_output::BashOutputView::new(),
|
||||
));
|
||||
|
||||
let a = dir.path().join("a.txt");
|
||||
@@ -1003,118 +1003,314 @@ async fn tracker_recent_files_tracks_read_write_edit() {
|
||||
async fn bash_inherits_workdir_cwd() {
|
||||
// The Bash tool starts at the WorkdirSession's pwd. Without any `cd`, its
|
||||
// `pwd` should canonicalize to the workspace root we set up.
|
||||
let (dir, _spill, reg) = setup();
|
||||
let (dir, reg) = setup();
|
||||
let bash = reg.get("Bash");
|
||||
let out = call(&bash, json!({ "command": "pwd", "cwd": "." })).await;
|
||||
let body = out.content.unwrap();
|
||||
let actual = std::fs::canonicalize(body.trim()).unwrap();
|
||||
let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
|
||||
let actual = std::fs::canonicalize(parsed["stdout_preview"].as_str().unwrap().trim()).unwrap();
|
||||
let expected = std::fs::canonicalize(dir.path()).unwrap();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
fn output_fixture() -> (
|
||||
TempDir,
|
||||
WorkdirSessionHandle,
|
||||
tools::bash_output::BashOutputView,
|
||||
Arc<dyn Tool>,
|
||||
) {
|
||||
let root = TempDir::new().unwrap();
|
||||
let session: WorkdirSessionHandle = Arc::new(LocalWorkdirSession::new(
|
||||
Scope::writable(root.path()).unwrap(),
|
||||
root.path().into(),
|
||||
));
|
||||
let view = tools::bash_output::BashOutputView::new();
|
||||
let (_, bash) = tools::bash_tool(session.clone(), view.clone())();
|
||||
(root, session, view, bash)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bash_provider_output_exposes_readable_retained_path() {
|
||||
let (_dir, spill, reg) = setup();
|
||||
let bash = reg.get("Bash");
|
||||
async fn bash_output_is_retained_without_a_filesystem_grant_or_telemetry_subscriber() {
|
||||
let (root, _session, view, bash) = output_fixture();
|
||||
let out = call(
|
||||
&bash,
|
||||
json!({ "command": "printf 'x%.0s' {1..20480}", "cwd": "." }),
|
||||
json!({ "command": "printf 'x%.0s' {1..20480}; printf '\\377€' >&2; exit 7", "cwd": "." }),
|
||||
)
|
||||
.await;
|
||||
let body = out.content.unwrap();
|
||||
assert!(body.contains("bounded WorkdirSession command output"));
|
||||
assert!(body.contains("full output saved to"));
|
||||
assert!(body.contains(spill.path().to_str().unwrap()));
|
||||
let artifact = std::fs::read_dir(spill.path())
|
||||
.unwrap()
|
||||
.next()
|
||||
.expect("retained output")
|
||||
.unwrap()
|
||||
.path();
|
||||
assert_eq!(std::fs::metadata(artifact).unwrap().len(), 20_480);
|
||||
let body: serde_json::Value = serde_json::from_str(&out.content.unwrap()).unwrap();
|
||||
assert_eq!(body["output"]["terminal"]["exit_code"], 7);
|
||||
assert_eq!(body["preview_omitted"], true);
|
||||
assert_eq!(body["output"]["storage_truncated"], false);
|
||||
let id = body["output"]["id"].as_str().unwrap();
|
||||
assert_eq!(
|
||||
view.read(id, workdir::CommandStream::Stdout, 0, 65536)
|
||||
.unwrap(),
|
||||
vec![b'x'; 20480]
|
||||
);
|
||||
assert_eq!(
|
||||
view.read(id, workdir::CommandStream::Stderr, 0, 65536)
|
||||
.unwrap(),
|
||||
[vec![255], "€".as_bytes().to_vec()].concat()
|
||||
);
|
||||
assert!(
|
||||
body["stdout_entry"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.starts_with("/bash-output/")
|
||||
);
|
||||
assert!(!body.to_string().contains(root.path().to_str().unwrap()));
|
||||
assert_eq!(std::fs::read_dir(root.path()).unwrap().count(), 0);
|
||||
}
|
||||
|
||||
async fn await_command_output(
|
||||
events: &mut tokio::sync::broadcast::Receiver<workdir::CommandEvent>,
|
||||
) {
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), async {
|
||||
loop {
|
||||
if matches!(
|
||||
events.recv().await.unwrap(),
|
||||
workdir::CommandEvent::Output { .. }
|
||||
) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("command output readiness");
|
||||
}
|
||||
async fn await_command_terminal(
|
||||
events: &mut tokio::sync::broadcast::Receiver<workdir::CommandEvent>,
|
||||
) {
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), async {
|
||||
loop {
|
||||
if let workdir::CommandEvent::Terminal { status, .. } = events.recv().await.unwrap() {
|
||||
assert_eq!(status, workdir::CommandStatus::Cancelled);
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("command terminal fence");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bash_cancellation_returns_bounded_progress_as_terminal_output() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let marker = dir.path().join("must-not-run-after-cancel");
|
||||
let command = format!(
|
||||
"printf 'before\\n'; printf 'err-before\\n' >&2; sleep 1; touch {}; printf 'after\\n'",
|
||||
marker.display()
|
||||
);
|
||||
let input = serde_json::to_string(&json!({ "command": command, "cwd": "." })).unwrap();
|
||||
let (_root, session, _view, bash) = output_fixture();
|
||||
let mut events = session.subscribe_command_events().unwrap();
|
||||
let context = ToolExecutionContext::new("call-heavy", "attempt-heavy", 0);
|
||||
let bash = reg.get("Bash");
|
||||
let executing = bash.clone();
|
||||
let execution_context = context.clone();
|
||||
let execution = tokio::spawn(async move { executing.execute(&input, execution_context).await });
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
bash.cancel_execution(&context)
|
||||
.await
|
||||
.expect("signal exact execution cancellation");
|
||||
let error = tokio::time::timeout(std::time::Duration::from_secs(2), execution)
|
||||
.await
|
||||
.expect("cancelled Bash should terminate inside the Engine grace budget")
|
||||
.expect("Bash task join");
|
||||
|
||||
let ToolError::Cancelled(output) = error.expect_err("cancelled command is non-success") else {
|
||||
panic!("expected typed cancellation result");
|
||||
let execution = tokio::spawn(async move {
|
||||
executing.execute(r#"{"command":"printf 'before'; printf 'err-before' >&2; while :; do :; done","cwd":"."}"#, execution_context).await
|
||||
});
|
||||
await_command_output(&mut events).await;
|
||||
bash.cancel_execution(&context).await.unwrap();
|
||||
let ToolError::Cancelled(output) =
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), execution)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.unwrap_err()
|
||||
else {
|
||||
panic!("expected cancellation");
|
||||
};
|
||||
let content = output.content.expect("bounded progress output");
|
||||
assert!(
|
||||
content.contains("before"),
|
||||
"missing pre-cancel stdout: {content}"
|
||||
);
|
||||
assert!(
|
||||
content.contains("err-before"),
|
||||
"missing pre-cancel stderr: {content}"
|
||||
);
|
||||
assert!(
|
||||
!content.contains("after"),
|
||||
"post-cancel output leaked: {content}"
|
||||
);
|
||||
assert!(content.len() <= 16 * 1024, "output must remain bounded");
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_millis(1_100)).await;
|
||||
assert!(
|
||||
!marker.exists(),
|
||||
"the cancelled command continued executing after terminal confirmation"
|
||||
);
|
||||
let body: serde_json::Value = serde_json::from_str(&output.content.unwrap()).unwrap();
|
||||
assert_eq!(body["stdout_preview"], "before");
|
||||
assert_eq!(body["stderr_preview"], "err-before");
|
||||
assert_eq!(body["output"]["terminal"]["status"], "cancelled");
|
||||
assert_eq!(body["output"]["terminal"]["transport_complete"], true);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bash_force_close_cleanup_stops_command_and_keeps_session_reusable() {
|
||||
let (dir, _spill, reg) = setup();
|
||||
let marker = dir.path().join("must-not-survive-force-close");
|
||||
let command = format!("sleep 1; touch {}", marker.display());
|
||||
let input = serde_json::to_string(&json!({ "command": command, "cwd": "." })).unwrap();
|
||||
let bash = reg.get("Bash");
|
||||
let (_root, session, _view, bash) = output_fixture();
|
||||
let mut events = session.subscribe_command_events().unwrap();
|
||||
let context = ToolExecutionContext::new("call-force", "attempt-force", 0);
|
||||
let (handle, terminal) = ToolExecutionHandle::start(bash.clone(), input, context);
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
let (handle, terminal) = ToolExecutionHandle::start(
|
||||
bash.clone(),
|
||||
r#"{"command":"printf ready; while :; do :; done","cwd":"."}"#.into(),
|
||||
context,
|
||||
);
|
||||
await_command_output(&mut events).await;
|
||||
handle.force_close();
|
||||
assert!(matches!(
|
||||
terminal.await,
|
||||
ToolExecutionTerminal::OutcomeUnknown
|
||||
));
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_millis(1_100)).await;
|
||||
assert!(
|
||||
!marker.exists(),
|
||||
"CommandGuard cleanup allowed a force-closed command to continue"
|
||||
);
|
||||
|
||||
let output = bash
|
||||
.execute(
|
||||
r#"{"command":"printf 'reused'","cwd":"."}"#,
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.expect("workdir session remains reusable after cleanup");
|
||||
assert_eq!(output.content.as_deref(), Some("reused"));
|
||||
await_command_terminal(&mut events).await;
|
||||
let output = call(&bash, json!({"command":"printf reused", "cwd":"."})).await;
|
||||
let body: serde_json::Value = serde_json::from_str(&output.content.unwrap()).unwrap();
|
||||
assert_eq!(body["stdout_preview"], "reused");
|
||||
}
|
||||
|
||||
// Sanity: unused Path import guard
|
||||
const _: fn() -> &'static Path = || Path::new("/");
|
||||
|
||||
#[tokio::test]
|
||||
async fn parent_and_sibling_broker_commands_keep_output_ownership_after_revoke() {
|
||||
let (root, source, parent_view, parent_bash) = output_fixture();
|
||||
let broker = WorkdirToolBroker::new(source);
|
||||
let mut leases = Vec::new();
|
||||
let mut views = Vec::new();
|
||||
let mut commands = Vec::new();
|
||||
for name in ["left", "right"] {
|
||||
std::fs::create_dir(root.path().join(name)).unwrap();
|
||||
let lease = Arc::new(
|
||||
broker
|
||||
.scope(WorkdirToolScope {
|
||||
rules: vec![WorkdirToolScopeRule {
|
||||
target: WorkdirPath::new(name).unwrap(),
|
||||
permission: WorkdirToolScopePermission::Write,
|
||||
recursive: true,
|
||||
symlink_policy: Default::default(),
|
||||
}],
|
||||
cwd: WorkdirPath::new(name).unwrap(),
|
||||
command: true,
|
||||
})
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
let authority = lease.clone();
|
||||
let view = parent_view.child(Arc::new(move || authority.is_active()));
|
||||
let (_, bash) = tools::bash_tool(lease.tool_session(), view.clone())();
|
||||
leases.push(lease);
|
||||
views.push(view);
|
||||
commands.push(bash);
|
||||
}
|
||||
let left_input =
|
||||
json!({"command":"printf 'left-%.0s' {1..10000}; printf left-err >&2", "cwd":"."});
|
||||
let right_input =
|
||||
json!({"command":"printf 'right-%.0s' {1..10000}; printf right-err >&2", "cwd":"."});
|
||||
let parent_input = json!({"command":"printf parent; printf parent-err >&2", "cwd":"."});
|
||||
let (left, right, parent) = tokio::join!(
|
||||
call(&commands[0], left_input),
|
||||
call(&commands[1], right_input),
|
||||
call(&parent_bash, parent_input)
|
||||
);
|
||||
let outputs = [left, right, parent]
|
||||
.map(|out| serde_json::from_str::<serde_json::Value>(&out.content.unwrap()).unwrap());
|
||||
for (index, label) in ["left", "right", "parent"].iter().enumerate() {
|
||||
let id = outputs[index]["output"]["id"].as_str().unwrap();
|
||||
let bytes = parent_view
|
||||
.read(id, workdir::CommandStream::Stderr, 0, 65536)
|
||||
.unwrap();
|
||||
assert_eq!(bytes, format!("{label}-err").as_bytes());
|
||||
for (child_index, view) in views.iter().enumerate() {
|
||||
assert_eq!(view.metadata(id).is_some(), child_index == index);
|
||||
}
|
||||
}
|
||||
let left_id = outputs[0]["output"]["id"].as_str().unwrap();
|
||||
leases[0].close().await.unwrap();
|
||||
assert!(views[0].metadata(left_id).is_none());
|
||||
assert!(parent_view.metadata(left_id).is_some());
|
||||
assert!(
|
||||
views[1]
|
||||
.metadata(outputs[1]["output"]["id"].as_str().unwrap())
|
||||
.is_some()
|
||||
);
|
||||
leases[1].close().await.unwrap();
|
||||
broker.close().await.unwrap();
|
||||
parent_view.revoke();
|
||||
assert!(parent_view.metadata(left_id).is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn utf8_quota_boundary_preserves_raw_prefix_and_reports_saved_vs_seen() {
|
||||
let (_root, _session, view, bash) = output_fixture();
|
||||
let out = call(
|
||||
&bash,
|
||||
json!({"command":"printf '%4194303s' ''; printf '€'", "cwd":"."}),
|
||||
)
|
||||
.await;
|
||||
let body: serde_json::Value = serde_json::from_str(&out.content.unwrap()).unwrap();
|
||||
let id = body["output"]["id"].as_str().unwrap();
|
||||
let saved = body["output"]["stdout_saved"].as_u64().unwrap() as usize;
|
||||
assert_eq!(saved, 4 * 1024 * 1024);
|
||||
assert_eq!(body["output"]["stdout_seen"], 4 * 1024 * 1024 + 2);
|
||||
assert_eq!(body["output"]["stderr_seen"], 0);
|
||||
assert_eq!(body["output"]["storage_truncated"], true);
|
||||
assert_eq!(
|
||||
view.read(id, workdir::CommandStream::Stdout, saved - 1, 10)
|
||||
.unwrap(),
|
||||
vec![0xe2]
|
||||
);
|
||||
assert!(
|
||||
view.line_window(id, workdir::CommandStream::Stdout, 0, 1)
|
||||
.unwrap()
|
||||
.is_err()
|
||||
);
|
||||
assert!(out.summary.starts_with("$ "));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn timeout_terminal_fences_retained_stdout_and_stderr() {
|
||||
let (_root, _session, view, bash) = output_fixture();
|
||||
let error = tokio::time::timeout(std::time::Duration::from_secs(5), bash.execute(r#"{"command":"printf before; printf err-before >&2; while :; do :; done","cwd":".","timeout":1}"#, Default::default())).await.unwrap().unwrap_err();
|
||||
let ToolError::Interrupted(out) = error else {
|
||||
panic!("expected timeout output");
|
||||
};
|
||||
let body: serde_json::Value = serde_json::from_str(&out.content.unwrap()).unwrap();
|
||||
let id = body["output"]["id"].as_str().unwrap();
|
||||
assert_eq!(body["output"]["terminal"]["status"], "timed_out");
|
||||
assert_eq!(body["output"]["terminal"]["transport_complete"], true);
|
||||
assert_eq!(
|
||||
view.read(id, workdir::CommandStream::Stdout, 0, 65536)
|
||||
.unwrap(),
|
||||
b"before"
|
||||
);
|
||||
assert_eq!(
|
||||
view.read(id, workdir::CommandStream::Stderr, 0, 65536)
|
||||
.unwrap(),
|
||||
b"err-before"
|
||||
);
|
||||
}
|
||||
|
||||
// Integration: real process pipes and provider replay into the production Bash collector.
|
||||
// Only response delivery is fault-injected; Runtime's HTTP suite covers actual wire routing.
|
||||
#[tokio::test]
|
||||
async fn lost_page_and_terminal_responses_replay_without_restarting_or_double_appending() {
|
||||
for lose_terminal_only in [false, true] {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let session = Arc::new(ExternalValidatingSession {
|
||||
inner: LocalWorkdirSession::new(
|
||||
Scope::writable(dir.path()).unwrap(),
|
||||
dir.path().into(),
|
||||
),
|
||||
truncate_first_binary_response: false,
|
||||
mutate_after_first_binary_response: None,
|
||||
command_response_losses: std::sync::atomic::AtomicUsize::new(1),
|
||||
lose_terminal_only,
|
||||
});
|
||||
let view = tools::bash_output::BashOutputView::new();
|
||||
let definitions = core_builtin_tools(session.clone(), Tracker::new(), view.clone());
|
||||
let bash = Registry::new(definitions).get("Bash");
|
||||
let out = call(&bash, json!({"cwd":".","command":"printf once >> effect.txt; printf 'x%.0s' {1..200000}; printf err >&2; exit 3"})).await;
|
||||
let body: serde_json::Value = serde_json::from_str(&out.content.unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
std::fs::read(dir.path().join("effect.txt")).unwrap(),
|
||||
b"once"
|
||||
);
|
||||
assert_eq!(
|
||||
session
|
||||
.command_response_losses
|
||||
.load(std::sync::atomic::Ordering::Acquire),
|
||||
0
|
||||
);
|
||||
assert_eq!(body["output"]["stdout_saved"], 200000);
|
||||
assert_eq!(body["output"]["stdout_delivered"], 200000);
|
||||
assert_eq!(body["output"]["stderr_saved"], 3);
|
||||
assert_eq!(body["output"]["terminal"]["exit_code"], 3);
|
||||
assert_eq!(body["output"]["terminal"]["transport_complete"], true);
|
||||
assert_eq!(body["output"]["storage_truncated"], false);
|
||||
let id = body["output"]["id"].as_str().unwrap();
|
||||
for offset in (0..200000).step_by(65536) {
|
||||
assert_eq!(
|
||||
view.read(id, workdir::CommandStream::Stdout, offset, 65536)
|
||||
.unwrap(),
|
||||
vec![b'x'; (200000 - offset).min(65536)]
|
||||
);
|
||||
}
|
||||
use workdir::WorkdirSession as _;
|
||||
session.close().await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user