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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/cortex-engine/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
mod environment;
mod output;
mod policy;
#[cfg(any(target_os = "linux", target_os = "macos"))]
// Process-group teardown is Unix-only; the runner itself is required on Windows
// so local_shell / plugin exec still compile (job-object isolation is separate).
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Restricted Commands Cannot Run

This change enables the command runner on Windows, but ReadOnly and WorkspaceWrite commands still enter the legacy sandbox preparation path. That path rejects non-full-access policies on Windows before spawning the command, so local-shell and plugin commands using the normal restricted policies fail instead of running.

Artifacts

Evidence from the check

Command output from the check

  • Executed parent-source comparison showing the exec runner cfg excluded Windows before the change, ending with the prior runner-unavailable condition.

Command output from the check

  • Executed validation and focused Cargo tests showing Windows runner enablement while non-full-access policies reach the rejecting legacy Windows backend, ending with confirmed behavior.

Evidence from the check

  • Python harness authored and executed against the parent and PR sources; it verifies the Windows full-access timeout path lacks tree cleanup.

Command output from the check

  • Executed environment check showing only the Linux Rust target is installed and PowerShell, pwsh, and Wine are unavailable; native Windows reproduction cannot run on this host.

Command output from the check

  • Captured execution of the authored harness showing all path assertions passed and confirming the direct-child-only Windows timeout cleanup.

View artifacts

T-Rex Ran code and verified through T-Rex

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Timeouts Leave Child Processes

This change enables the runner on Windows, but full-access commands receive neither process-group cleanup nor Job Object ownership. When a timeout occurs, the runner kills only the immediate child; descendants it started can remain running after the command is reported as timed out.

Artifacts

Evidence from the check

Command output from the check

  • Executed parent-source comparison showing the exec runner cfg excluded Windows before the change, ending with the prior runner-unavailable condition.

Command output from the check

  • Executed validation and focused Cargo tests showing Windows runner enablement while non-full-access policies reach the rejecting legacy Windows backend, ending with confirmed behavior.

Evidence from the check

  • Python harness authored and executed against the parent and PR sources; it verifies the Windows full-access timeout path lacks tree cleanup.

Command output from the check

  • Executed environment check showing only the Linux Rust target is installed and PowerShell, pwsh, and Wine are unavailable; native Windows reproduction cannot run on this host.

Command output from the check

  • Captured execution of the authored harness showing all path assertions passed and confirming the direct-child-only Windows timeout cleanup.

View artifacts

T-Rex Ran code and verified through T-Rex

mod runner;
pub use environment::{build_safe_environment, is_sensitive_env_name};

pub use output::OutputCapture;
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
pub use runner::{
ExecOptions, ExecOutput, OutputChunk, execute_command, execute_command_streaming,
};
Expand Down
76 changes: 64 additions & 12 deletions src/cortex-engine/src/exec/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,39 @@ use super::ExecOptions;
use crate::error::{CortexError, Result};
use crate::sandbox::{SandboxPolicyType, SandboxRunner, SandboxedCommand, WritableRoot};
use cortex_protocol::SandboxPolicy;
use std::path::Path;

pub(super) async fn prepare(command: &[String], options: &ExecOptions) -> Result<SandboxedCommand> {
evaluate(command, options.approval_granted).await?;
let policy = match &options.sandbox_policy {
let policy = mapped_sandbox_policy(&options.sandbox_policy, &options.cwd)?;
SandboxRunner::new().prepare(command, &policy, &options.cwd)
}

fn mapped_sandbox_policy(policy: &SandboxPolicy, cwd: &Path) -> Result<SandboxPolicyType> {
Ok(match policy {
SandboxPolicy::DangerFullAccess => SandboxPolicyType::DangerFullAccess,
SandboxPolicy::ReadOnly => SandboxPolicyType::Custom {
writable_roots: Vec::new(),
network_access: false,
allow_read_outside_workspace: true,
},
SandboxPolicy::WorkspaceWrite {
writable_roots,
network_access,
..
} => {
SandboxPolicy::WorkspaceWrite { writable_roots, .. } => {
// Never infer permission from HOME caches, TMPDIR, or /tmp.
let mut roots = vec![WritableRoot::with_standard_protections(
options.cwd.canonicalize()?,
)];
let mut roots = vec![WritableRoot::with_standard_protections(cwd.canonicalize()?)];
for path in writable_roots {
roots.push(WritableRoot::with_standard_protections(
path.canonicalize()?,
));
}
SandboxPolicyType::Custom {
writable_roots: roots,
network_access: *network_access,
// `network_access` is a `bool`. Do not dereference it: rustc match
// ergonomics bind the Copy field as `bool` on Windows nightly (E0614).
network_access: policy.has_full_network_access(),
allow_read_outside_workspace: true,
}
}
};
SandboxRunner::new().prepare(command, &policy, &options.cwd)
})
}

async fn evaluate(command: &[String], approved: bool) -> Result<()> {
Expand All @@ -52,3 +53,54 @@ async fn evaluate(command: &[String], approved: bool) -> Result<()> {
)),
}
}

#[cfg(test)]
mod tests {
use super::*;

fn workspace_write(network_access: bool) -> SandboxPolicy {
SandboxPolicy::WorkspaceWrite {
writable_roots: vec![],
network_access,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
}
}

#[test]
fn workspace_write_network_access_is_copied_as_bool() {
let dir = tempfile::tempdir().unwrap();
let cwd = dir.path();

let enabled = mapped_sandbox_policy(&workspace_write(true), cwd).unwrap();
assert!(matches!(
enabled,
SandboxPolicyType::Custom {
network_access: true,
..
}
));

let disabled = mapped_sandbox_policy(&workspace_write(false), cwd).unwrap();
assert!(matches!(
disabled,
SandboxPolicyType::Custom {
network_access: false,
..
}
));
}

#[test]
fn read_only_disables_network_access() {
let dir = tempfile::tempdir().unwrap();
let policy = mapped_sandbox_policy(&SandboxPolicy::ReadOnly, dir.path()).unwrap();
assert!(matches!(
policy,
SandboxPolicyType::Custom {
network_access: false,
..
}
));
}
}
2 changes: 1 addition & 1 deletion src/cortex-engine/src/exec/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub async fn execute_command_streaming(
Ok(output)
}

#[cfg(test)]
#[cfg(all(test, unix))]
mod tests {
use super::*;
#[tokio::test]
Expand Down
Loading