Skip to content

Commit be0951f

Browse files
Record commands denied during executor startup
1 parent d7a076e commit be0951f

4 files changed

Lines changed: 91 additions & 18 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
diff --git a/codex-rs/core/src/unified_exec/process_manager.rs b/codex-rs/core/src/unified_exec/process_manager.rs
2+
index 211d99de7cf4e92c21779712cb31dc1b0f1c4426..55d39c46d1eaff030fb9f303077d807f12c7fe85 100644
3+
--- a/codex-rs/core/src/unified_exec/process_manager.rs
4+
+++ b/codex-rs/core/src/unified_exec/process_manager.rs
5+
@@ -32,6 +32,7 @@ use crate::tools::ApprovalContext;
6+
use crate::tools::context::ExecCommandToolOutput;
7+
use crate::tools::events::ToolEmitter;
8+
use crate::tools::events::ToolEventCtx;
9+
+use crate::tools::events::ToolEventFailure;
10+
use crate::tools::events::ToolEventStage;
11+
use crate::tools::network_approval::DeferredNetworkApproval;
12+
use crate::tools::network_approval::finish_deferred_network_approval;
13+
@@ -533,6 +534,32 @@ impl UnifiedExecProcessManager {
14+
Ok((attempt, deferred_network_approval)) => (attempt, deferred_network_approval),
15+
Err(err) => {
16+
self.release_process_id(request.process_id).await;
17+
+ // A process denied during the early-exit check returns before Begin.
18+
+ // Record the final denial only after the sandbox orchestrator has
19+
+ // decided whether to retry, using the original process output.
20+
+ if let UnifiedExecError::SandboxDenied { output, .. } = &err {
21+
+ let model_context = context.step_context.model_context();
22+
+ let mut event_ctx = ToolEventCtx::new(
23+
+ context.session.as_ref(),
24+
+ context.step_context.turn.as_ref(),
25+
+ &context.step_context.settings.model_info,
26+
+ &context.call_id,
27+
+ /*turn_diff_tracker*/ None,
28+
+ );
29+
+ event_ctx.model_context = Some(&model_context);
30+
+ ToolEmitter::unified_exec(
31+
+ &request.command,
32+
+ cwd,
33+
+ ExecCommandSource::UnifiedExecStartup,
34+
+ /*process_id*/ None,
35+
+ /*plugin_attribution*/ None,
36+
+ )
37+
+ .emit(
38+
+ event_ctx,
39+
+ ToolEventStage::Failure(ToolEventFailure::Output(output.clone())),
40+
+ )
41+
+ .await;
42+
+ }
43+
return Err(err);
44+
}
45+
};

‎packages/codex/patches/series.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@
8787
{
8888
"path": "0022-sdk-local-network-receipts.patch",
8989
"sha256": "44d43b139d8d1f8a010dbab32a3bb7ae8e54f10a695e97026e63c93d73bb0fcf"
90+
},
91+
{
92+
"path": "0023-retain-early-sandbox-denials.patch",
93+
"sha256": "31eee8fd638f35abd56e216a18bbc70dad6b8b93f90fe2db16b2deaae1bd004f"
9094
}
9195
],
92-
"result_tree": "940cdeb7147a2f2bbdd9b6065c5cbdd73754167c"
96+
"result_tree": "bfb3f0280e467a641999c4be006f4188bfc85734"
9397
}

‎tests/native_executor_fixtures.py‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class ExecutorPeer:
112112
drop_start_reply_after: Path | None = None
113113
start_reply_dropped: asyncio.Event = field(default_factory=asyncio.Event)
114114
synthetic_process_output: str | None = field(default=None, repr=False)
115+
synthetic_sandbox_denied: bool = False
115116
synthetic_process_starts: list[str] = field(default_factory=list)
116117

117118
async def wait_disconnected(self) -> None:
@@ -195,22 +196,24 @@ async def forward(source: Any, destination: Any, record: bool) -> None:
195196
method = value.get("method")
196197
params = value.get("params", {})
197198
process_id = params.get("processId")
199+
sandbox_denied = self.synthetic_sandbox_denied
200+
exit_code = int(sandbox_denied)
198201
if method == "process/start":
199202
assert (
200203
isinstance(process_id, str) and process_id
201204
)
202205
assert process_id not in synthetic_processes
203206
synthetic_processes.add(process_id)
204207
self.synthetic_process_starts.append(process_id)
205-
await downstream.send_json(
206-
{
207-
"id": value["id"],
208-
"result": {
209-
"processId": process_id,
210-
"sandboxType": "none",
211-
},
212-
}
213-
)
208+
start_reply = {
209+
"id": value["id"],
210+
"result": {
211+
"processId": process_id,
212+
"sandboxType": "none",
213+
},
214+
}
215+
if not sandbox_denied:
216+
await downstream.send_json(start_reply)
214217
chunk = base64.b64encode(
215218
self.synthetic_process_output.encode()
216219
).decode()
@@ -229,7 +232,12 @@ async def forward(source: Any, destination: Any, record: bool) -> None:
229232
{
230233
"processId": process_id,
231234
"seq": 2,
232-
"exitCode": 0,
235+
"exitCode": exit_code,
236+
**(
237+
{"sandboxDenied": True}
238+
if sandbox_denied
239+
else {}
240+
),
233241
},
234242
),
235243
(
@@ -243,6 +251,9 @@ async def forward(source: Any, destination: Any, record: bool) -> None:
243251
"params": payload,
244252
}
245253
)
254+
if sandbox_denied:
255+
# Queue the exit before acknowledging start.
256+
await downstream.send_json(start_reply)
246257
# Never forward a synthetic process/start.
247258
continue
248259
if process_id in synthetic_processes:
@@ -265,10 +276,10 @@ async def forward(source: Any, destination: Any, record: bool) -> None:
265276
"chunks": chunks,
266277
"nextSeq": 3,
267278
"exited": True,
268-
"exitCode": 0,
279+
"exitCode": exit_code,
269280
"closed": True,
270281
"failure": None,
271-
"sandboxDenied": False,
282+
"sandboxDenied": sandbox_denied,
272283
}
273284
elif method == "process/terminate":
274285
reply = {"running": False}

‎tests/test_native_child_tool_integration.py‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,27 +235,37 @@ async def redact(ctx: MCPResultContext[Memory], value: dict[str, Any]) -> dict[s
235235

236236

237237
@pytest.mark.parametrize(
238-
("programmatic_tool_calling", "execute"),
238+
("programmatic_tool_calling", "execute", "synthetic_denial"),
239239
[
240-
pytest.param(False, False, id="direct-function-mcp"),
241-
pytest.param(True, False, id="code-function-mcp"),
240+
pytest.param(False, False, False, id="direct-function-mcp"),
241+
pytest.param(True, False, False, id="code-function-mcp"),
242+
pytest.param(True, True, True, id="code-function-mcp-fast-executor-denial"),
242243
pytest.param(
243244
True,
244245
True,
246+
False,
245247
id="code-function-mcp-native-executor",
246248
marks=pytest.mark.requires_native_linux_sandbox,
247249
),
248250
],
249251
)
250252
async def test_two_children_can_use_context_and_mcp_after_root_finishes(
251-
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, programmatic_tool_calling: bool, execute: bool
253+
tmp_path: Path,
254+
monkeypatch: pytest.MonkeyPatch,
255+
programmatic_tool_calling: bool,
256+
execute: bool,
257+
synthetic_denial: bool,
252258
) -> None:
253259
memory = Memory()
254260
vault = Peer(names=("find-record", "excluded"), label="mcp-private-f126")
255261
async with (
256262
timeout(60),
257263
family_peer(programmatic_tool_calling=programmatic_tool_calling, execute=execute) as peer,
258-
ExecutorPeer(tmp_path / "remote").serve() as executor,
264+
ExecutorPeer(
265+
tmp_path / "remote",
266+
synthetic_process_output="Permission denied" if synthetic_denial else None,
267+
synthetic_sandbox_denied=synthetic_denial,
268+
).serve() as executor,
259269
vault.serve() as url,
260270
):
261271
trust(executor, tmp_path, monkeypatch)
@@ -358,6 +368,7 @@ async def test_two_children_can_use_context_and_mcp_after_root_finishes(
358368
handle.turn_id,
359369
)
360370
assert command["argv"][-1:] == [f"printf {name} > ../{name}.txt"]
371+
assert command["call_id"] and command["call_id"] == command["id"]
361372
assert command["exit_code"] == denied["exit_code"]
362373
assert command["native_status"] == "failed"
363374
output = denied["output"].lower()
@@ -367,6 +378,8 @@ async def test_two_children_can_use_context_and_mcp_after_root_finishes(
367378
assert any(
368379
request.get("method") == "process/start" for request in executor.requests
369380
)
381+
if synthetic_denial:
382+
assert len(executor.synthetic_process_starts) == 2
370383
if not programmatic_tool_calling:
371384
assert {ctx.call_id for ctx in memory.entries.values()} == {"shared-call-id"}
372385
wire = json.dumps(peer.requests, ensure_ascii=False)

0 commit comments

Comments
 (0)