Skip to content

Commit 9f96338

Browse files
authored
Attach context to apply patch operations (#2162)
1 parent df020d1 commit 9f96338

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/agents/_run_impl.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,10 @@ async def execute(
18261826
output_text = ""
18271827

18281828
try:
1829-
operation = _coerce_apply_patch_operation(call.tool_call)
1829+
operation = _coerce_apply_patch_operation(
1830+
call.tool_call,
1831+
context_wrapper=context_wrapper,
1832+
)
18301833
editor = apply_patch_tool.editor
18311834
if operation.type == "create_file":
18321835
result = editor.create_file(operation)
@@ -2093,7 +2096,9 @@ def _extract_apply_patch_call_id(tool_call: Any) -> str:
20932096
return str(value)
20942097

20952098

2096-
def _coerce_apply_patch_operation(tool_call: Any) -> ApplyPatchOperation:
2099+
def _coerce_apply_patch_operation(
2100+
tool_call: Any, *, context_wrapper: RunContextWrapper[Any]
2101+
) -> ApplyPatchOperation:
20972102
raw_operation = _get_mapping_or_attr(tool_call, "operation")
20982103
if raw_operation is None:
20992104
raise ModelBehaviorError("Apply patch call is missing an operation payload.")
@@ -2117,7 +2122,12 @@ def _coerce_apply_patch_operation(tool_call: Any) -> ApplyPatchOperation:
21172122
else:
21182123
diff = None
21192124

2120-
return ApplyPatchOperation(type=op_type_literal, path=str(path), diff=diff)
2125+
return ApplyPatchOperation(
2126+
type=op_type_literal,
2127+
path=str(path),
2128+
diff=diff,
2129+
ctx_wrapper=context_wrapper,
2130+
)
21212131

21222132

21232133
def _normalize_apply_patch_result(

src/agents/editor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from dataclasses import dataclass
55
from typing import Literal, Protocol, runtime_checkable
66

7+
from .run_context import RunContextWrapper
78
from .util._types import MaybeAwaitable
89

910
ApplyPatchOperationType = Literal["create_file", "update_file", "delete_file"]
@@ -18,6 +19,7 @@ class ApplyPatchOperation:
1819
type: ApplyPatchOperationType
1920
path: str
2021
diff: str | None = None
22+
ctx_wrapper: RunContextWrapper | None = None
2123

2224

2325
@dataclass(**_DATACLASS_KWARGS)

tests/test_apply_patch_tool.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ async def test_apply_patch_tool_success() -> None:
6363
assert raw_item["status"] == "completed"
6464
assert raw_item["call_id"] == "call_apply"
6565
assert editor.operations[0].type == "update_file"
66+
assert editor.operations[0].ctx_wrapper is context_wrapper
6667
assert isinstance(raw_item["output"], str)
6768
assert raw_item["output"].startswith("Updated tasks.md")
6869
input_payload = result.to_input_item()
@@ -137,3 +138,4 @@ async def test_apply_patch_tool_accepts_mapping_call() -> None:
137138
raw_item = cast(dict[str, Any], result.raw_item)
138139
assert raw_item["call_id"] == "call_mapping"
139140
assert editor.operations[0].path == "notes.md"
141+
assert editor.operations[0].ctx_wrapper is context_wrapper

0 commit comments

Comments
 (0)