Skip to content

Commit d303ce8

Browse files
authored
fix(sandbox): check sandbox apply_patch approval scope (#5146)
* fix: scope apply_patch approvals by invocation * fix: simplify apply_patch approval checks * fix: preserve apply_patch approval decisions
1 parent cc14936 commit d303ce8

4 files changed

Lines changed: 235 additions & 21 deletions

File tree

‎src/agents/run_context.py‎

Lines changed: 97 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,39 @@ def _resolve_call_id(approval_item: ToolApprovalItem) -> str | None:
250250
candidate = getattr(raw, "id", None)
251251
return RunContextWrapper._to_str_or_none(candidate)
252252

253+
@staticmethod
254+
def _approval_keys_for_scope(
255+
approval_key: str | HostedMCPApprovalKey, approval_scope: str | None
256+
) -> tuple[str | HostedMCPApprovalKey, ...]:
257+
if approval_key == "apply_patch" and approval_scope is not None:
258+
return (f"apply_patch:{approval_scope}", approval_key)
259+
return (approval_key,)
260+
253261
def _get_or_create_approval_entry(
254262
self,
255263
approval_key: str | HostedMCPApprovalKey,
264+
*,
265+
approval_scope: str | None = None,
256266
) -> _ApprovalRecord:
257267
approval_entry = self._approvals.get(approval_key)
268+
scoped_key = self._approval_keys_for_scope(approval_key, approval_scope)[0]
269+
if scoped_key != approval_key:
270+
other_scope = approval_entry is not None and (
271+
(
272+
approval_entry.sticky_scope is not None
273+
and approval_entry.sticky_scope != approval_scope
274+
)
275+
or any(
276+
invocation.approval_scope != approval_scope
277+
for decisions in (approval_entry.approved, approval_entry.rejected)
278+
if isinstance(decisions, list)
279+
for call_id in decisions
280+
if (invocation := self._tool_invocations.get(call_id)) is not None
281+
)
282+
)
283+
if scoped_key in self._approvals or other_scope:
284+
approval_key = scoped_key
285+
approval_entry = self._approvals.get(approval_key)
258286
if approval_entry is None:
259287
approval_entry = _ApprovalRecord()
260288
self._approvals[approval_key] = approval_entry
@@ -462,13 +490,14 @@ def _matching_sticky_approval_keys(
462490

463491
matching_keys: set[str | HostedMCPApprovalKey] = set()
464492
for approval_key in approval_keys:
465-
record = self._approvals.get(approval_key)
466-
if (
467-
record is not None
468-
and (isinstance(record.approved, bool) or isinstance(record.rejected, bool))
469-
and record.sticky_scope == approval_scope
470-
):
471-
matching_keys.add(approval_key)
493+
for scoped_key in self._approval_keys_for_scope(approval_key, approval_scope):
494+
record = self._approvals.get(scoped_key)
495+
if (
496+
record is not None
497+
and (isinstance(record.approved, bool) or isinstance(record.rejected, bool))
498+
and record.sticky_scope == approval_scope
499+
):
500+
matching_keys.add(scoped_key)
472501
return frozenset(matching_keys)
473502

474503
def _mark_tool_call_completed(
@@ -620,11 +649,38 @@ def is_tool_approved(self, tool_name: str, call_id: str) -> bool | None:
620649
return hosted_query_status
621650
return self._get_approval_status_for_key(tool_name, call_id)
622651

623-
def _get_approval_status_for_key(self, approval_key: str, call_id: str) -> bool | None:
652+
def _get_approval_status_for_key(
653+
self, approval_key: str, call_id: str, *, approval_scope: str | None = None
654+
) -> bool | None:
624655
"""Return True/False/None for a concrete approval key and tool call."""
625-
approval_entry = self._approvals.get(approval_key)
656+
approval_entry = self._get_approval_record_for_key(
657+
approval_key, call_id, approval_scope=approval_scope
658+
)
626659
return self._get_approval_status_for_record(approval_entry, call_id)
627660

661+
def _get_approval_record_for_key(
662+
self, approval_key: str, call_id: str, *, approval_scope: str | None = None
663+
) -> _ApprovalRecord | None:
664+
if approval_scope is None:
665+
invocation = self._tool_invocations.get(call_id)
666+
approval_scope = invocation.approval_scope if invocation is not None else None
667+
for scoped_key in self._approval_keys_for_scope(approval_key, approval_scope):
668+
record = self._approvals.get(scoped_key)
669+
if record is None or self._get_approval_status_for_record(record, call_id) is None:
670+
continue
671+
if self._get_per_call_approval_status_for_record(record, call_id) is not None:
672+
return record
673+
if (
674+
approval_scope is None
675+
or record.sticky_scope == approval_scope
676+
or (
677+
record.sticky_scope is None
678+
and self._allow_legacy_approval_binding_reconstruction
679+
)
680+
):
681+
return record
682+
return None
683+
628684
@staticmethod
629685
def _get_approval_status_for_record(
630686
approval_entry: _ApprovalRecord | None,
@@ -877,7 +933,7 @@ def get_rejection_message(
877933
candidates.append(pending_tool_name)
878934

879935
for candidate in candidates:
880-
approval_entry = self._approvals.get(candidate)
936+
approval_entry = self._get_approval_record_for_key(candidate, call_id)
881937
if not approval_entry:
882938
continue
883939
message = self._get_rejection_message_for_key(approval_entry, call_id)
@@ -991,7 +1047,13 @@ def _apply_approval_decision(
9911047
exact_approval_key = self._resolve_approval_key(approval_item)
9921048
decision_keys = (exact_approval_key,) if always or call_id is None else approval_keys
9931049
approval_entries = tuple(
994-
(self._get_or_create_approval_entry(approval_key), always)
1050+
(
1051+
self._get_or_create_approval_entry(
1052+
approval_key,
1053+
approval_scope=scope_identity[1] if scope_identity is not None else None,
1054+
),
1055+
always,
1056+
)
9951057
for approval_key in decision_keys
9961058
)
9971059

@@ -1162,16 +1224,35 @@ def get_approval_status(
11621224
):
11631225
candidates.append(pending_tool_name)
11641226

1227+
selected_invocation = (
1228+
current_invocation if current_invocation is not None else existing_pending
1229+
)
1230+
if selected_invocation is not None and call_id in self._tool_invocations:
1231+
self._tool_invocation_status(
1232+
selected_invocation.raw_item,
1233+
tool_lookup_key=selected_invocation.tool_lookup_key,
1234+
tool_name=selected_invocation.tool_name,
1235+
)
1236+
scope_identity = (
1237+
tool_invocation_approval_scope(
1238+
selected_invocation.raw_item,
1239+
tool_lookup_key=selected_invocation.tool_lookup_key,
1240+
tool_name=selected_invocation.tool_name,
1241+
)
1242+
if selected_invocation is not None
1243+
else None
1244+
)
11651245
status: bool | None = None
11661246
matched_record: _ApprovalRecord | None = None
11671247
for candidate in candidates:
1168-
status = self._get_approval_status_for_key(candidate, call_id)
1248+
matched_record = self._get_approval_record_for_key(
1249+
candidate,
1250+
call_id,
1251+
approval_scope=scope_identity[1] if scope_identity is not None else None,
1252+
)
1253+
status = self._get_approval_status_for_record(matched_record, call_id)
11691254
if status is not None:
1170-
matched_record = self._approvals.get(candidate)
11711255
break
1172-
selected_invocation = (
1173-
current_invocation if current_invocation is not None else existing_pending
1174-
)
11751256
if status is None or matched_record is None or selected_invocation is None:
11761257
return status
11771258
is_sticky = isinstance(matched_record.approved, bool) or isinstance(

‎src/agents/run_state.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ def _default_run_state_validation_error(
229229
"Persists Docker container labels and current-response generated-item ownership across "
230230
"resume flows, including pending resumed Session writes and terminal-unrecoverable runs."
231231
),
232-
"1.18": "Binds restored local MCP calls to their configured server and original tool name.",
232+
"1.18": (
233+
"Binds restored local MCP calls to their configured server and original tool name, "
234+
"and preserves independent apply_patch approval scopes."
235+
),
233236
}
234237
SUPPORTED_SCHEMA_VERSIONS = frozenset(SCHEMA_VERSION_SUMMARIES)
235238

‎src/agents/sandbox/capabilities/tools/apply_patch_tool.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from collections.abc import Mapping, Sequence
55
from typing import Any
66

7+
from ...._tool_invocation import tool_invocation_approval_scope
78
from ....editor import ApplyPatchEditor, ApplyPatchOperation, ApplyPatchResult
89
from ....run_context import RunContextWrapper
910
from ....tool import (
@@ -239,14 +240,22 @@ async def _needs_custom_approval(
239240
# model gets a recoverable tool error instead of aborting during approval pre-checks.
240241
return False
241242

243+
scope_identity = tool_invocation_approval_scope(
244+
{"type": "custom_tool_call", "name": self.name},
245+
)
246+
assert scope_identity is not None
242247
for operation in operations:
243248
needs_approval = await evaluate_needs_approval_setting(
244249
self.needs_approval,
245250
ctx_wrapper,
246251
operation,
247252
call_id,
248253
)
249-
approval_status = ctx_wrapper.get_approval_status(self.name, call_id)
254+
approval_status = ctx_wrapper._get_approval_status_for_key(
255+
self.name,
256+
call_id,
257+
approval_scope=scope_identity[1],
258+
)
250259
if approval_status is not None or needs_approval:
251260
return needs_approval
252261
return False

‎tests/sandbox/capabilities/test_apply_patch_tool.py‎

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import pytest
1010

1111
import agents.sandbox.apply_patch as sandbox_apply_patch
12-
from agents import Agent, CustomTool, RunHooks
12+
from agents import Agent, ApplyPatchTool, CustomTool, RunHooks
1313
from agents.editor import ApplyPatchOperation, ApplyPatchResult
1414
from agents.items import ToolApprovalItem, ToolCallOutputItem
1515
from agents.models.openai_responses import Converter
1616
from agents.run import RunConfig
1717
from agents.run_context import RunContextWrapper
18-
from agents.run_internal.run_steps import ToolRunCustom
19-
from agents.run_internal.tool_actions import CustomToolAction
18+
from agents.run_internal.run_steps import NextStepInterruption, ToolRunApplyPatchCall, ToolRunCustom
19+
from agents.run_internal.tool_actions import ApplyPatchAction, CustomToolAction
20+
from agents.run_state import RunState
2021
from agents.sandbox import SandboxWorkspaceScope
2122
from agents.sandbox.capabilities.tools import SandboxApplyPatchTool
2223
from agents.sandbox.errors import ApplyPatchDecodeError, ApplyPatchFileNotFoundError
@@ -179,6 +180,126 @@ async def needs_approval(
179180
assert isinstance(result, ToolApprovalItem)
180181
assert checked_paths == [(expected_path, expected_move_to)]
181182

183+
@pytest.mark.parametrize("round_trip", [False, True], ids=["in-memory", "json"])
184+
@pytest.mark.parametrize("native_approved", [True, False])
185+
@pytest.mark.parametrize("sandbox_approved", [True, False])
186+
@pytest.mark.parametrize("sandbox_always", [False, True], ids=["once", "always"])
187+
@pytest.mark.asyncio
188+
async def test_multi_operation_checker_ignores_native_sticky_approval(
189+
self,
190+
native_approved: bool,
191+
sandbox_approved: bool,
192+
sandbox_always: bool,
193+
round_trip: bool,
194+
) -> None:
195+
checked_paths: list[str] = []
196+
197+
async def needs_approval(
198+
_ctx: RunContextWrapper[Any], operation: ApplyPatchOperation, _call_id: str
199+
) -> bool:
200+
checked_paths.append(operation.path)
201+
return operation.type == "delete_file"
202+
203+
protected_path = Path("/workspace/protected.txt")
204+
session = ApplyPatchSession()
205+
session.files[protected_path] = b"protected"
206+
tool = SandboxApplyPatchTool(session=session, needs_approval=needs_approval)
207+
context = make_context_wrapper()
208+
native_approval = ToolApprovalItem(
209+
agent=Agent(name="patcher"),
210+
raw_item={
211+
"type": "apply_patch_call",
212+
"call_id": "native_apply",
213+
"operation": {"type": "create_file", "path": "native.txt", "diff": "+native\n"},
214+
},
215+
tool_name="apply_patch",
216+
)
217+
if native_approved:
218+
context.approve_tool(native_approval, always_approve=True)
219+
else:
220+
context.reject_tool(
221+
native_approval, always_reject=True, rejection_message="Native patch denied"
222+
)
223+
raw_input = (
224+
"*** Begin Patch\n*** Add File: harmless.txt\n+harmless\n"
225+
"*** Delete File: protected.txt\n*** End Patch\n"
226+
)
227+
228+
result = await _execute_custom_tool_call(tool, context_wrapper=context, raw_input=raw_input)
229+
230+
assert isinstance(result, ToolApprovalItem)
231+
assert checked_paths == ["harmless.txt", "protected.txt"]
232+
assert session.files == {protected_path: b"protected"}
233+
state = RunState(context=context, original_input="patch", starting_agent=result.agent)
234+
state._current_step = NextStepInterruption(interruptions=[result])
235+
if round_trip:
236+
state = await RunState.from_json(result.agent, state.to_json())
237+
if sandbox_approved:
238+
state.approve(state.get_interruptions()[0], always_approve=sandbox_always)
239+
else:
240+
state.reject(
241+
state.get_interruptions()[0],
242+
always_reject=sandbox_always,
243+
rejection_message="Sandbox patch denied",
244+
)
245+
if round_trip:
246+
state = await RunState.from_json(result.agent, state.to_json())
247+
assert state._context is not None
248+
249+
resumed = await _execute_custom_tool_call(
250+
tool, context_wrapper=state._context, raw_input=raw_input
251+
)
252+
253+
assert isinstance(resumed, ToolCallOutputItem)
254+
if sandbox_approved:
255+
assert session.files == {Path("/workspace/harmless.txt"): b"harmless"}
256+
else:
257+
assert resumed.output == "Sandbox patch denied"
258+
assert session.files == {protected_path: b"protected"}
259+
260+
native_tool = ApplyPatchTool(editor=tool.editor, needs_approval=native_approved)
261+
native_result = await ApplyPatchAction.execute(
262+
agent=Agent(name="patcher", tools=[native_tool]),
263+
call=ToolRunApplyPatchCall(
264+
tool_call={
265+
"type": "apply_patch_call",
266+
"call_id": "native_followup",
267+
"operation": {
268+
"type": "create_file",
269+
"path": "native.txt",
270+
"diff": "+native\n",
271+
},
272+
},
273+
apply_patch_tool=native_tool,
274+
),
275+
hooks=RunHooks[Any](),
276+
context_wrapper=state._context,
277+
config=RunConfig(),
278+
)
279+
280+
assert isinstance(native_result, ToolCallOutputItem)
281+
if native_approved:
282+
assert session.files[Path("/workspace/native.txt")] == b"native"
283+
else:
284+
assert native_result.output == "Native patch denied"
285+
assert Path("/workspace/native.txt") not in session.files
286+
287+
if sandbox_always:
288+
tool.needs_approval = sandbox_approved
289+
sandbox_result = await _execute_custom_tool_call(
290+
tool,
291+
context_wrapper=state._context,
292+
call_id="sandbox_followup",
293+
raw_input="*** Begin Patch\n*** Add File: sandbox.txt\n+sandbox\n*** End Patch\n",
294+
)
295+
296+
assert isinstance(sandbox_result, ToolCallOutputItem)
297+
if sandbox_approved:
298+
assert session.files[Path("/workspace/sandbox.txt")] == b"sandbox"
299+
else:
300+
assert sandbox_result.output == "Sandbox patch denied"
301+
assert Path("/workspace/sandbox.txt") not in session.files
302+
182303
@pytest.mark.parametrize("approved", [True, False], ids=["approved", "rejected"])
183304
@pytest.mark.asyncio
184305
async def test_multi_operation_checker_stops_when_approval_resolves(

0 commit comments

Comments
 (0)