forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_shell_call_serialization.py
More file actions
125 lines (107 loc) · 3.96 KB
/
test_shell_call_serialization.py
File metadata and controls
125 lines (107 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from __future__ import annotations
import pytest
from agents.agent import Agent
from agents.exceptions import ModelBehaviorError
from agents.items import ToolCallOutputItem
from agents.run_internal import run_loop
from agents.tool import ShellCallOutcome, ShellCommandOutput
from tests.fake_model import FakeModel
def test_coerce_shell_call_reads_max_output_length() -> None:
tool_call = {
"call_id": "shell-1",
"action": {
"commands": ["ls"],
"maxOutputLength": 512,
},
"status": "in_progress",
}
result = run_loop.coerce_shell_call(tool_call)
assert result.action.max_output_length == 512
def test_coerce_shell_call_requires_commands() -> None:
tool_call = {"call_id": "shell-2", "action": {"commands": []}}
with pytest.raises(ModelBehaviorError):
run_loop.coerce_shell_call(tool_call)
def test_coerce_shell_call_rejects_string_like_commands() -> None:
for commands in ("echo hi", b"echo hi", bytearray(b"echo hi")):
tool_call = {"call_id": "shell-3", "action": {"commands": commands}}
with pytest.raises(ModelBehaviorError, match="list of strings"):
run_loop.coerce_shell_call(tool_call)
def test_normalize_shell_output_handles_timeout() -> None:
entry = {
"stdout": "",
"stderr": "",
"outcome": {"type": "timeout"},
"provider_data": {"truncated": True},
}
normalized = run_loop.normalize_shell_output(entry)
assert normalized.status == "timeout"
assert normalized.provider_data == {"truncated": True}
def test_normalize_shell_output_converts_string_outcome() -> None:
entry = {
"stdout": "hi",
"stderr": "",
"status": "completed",
"outcome": "success",
"exit_code": 0,
}
normalized = run_loop.normalize_shell_output(entry)
assert normalized.status == "completed"
assert normalized.exit_code in (None, 0)
def test_serialize_shell_output_emits_canonical_outcome() -> None:
output = ShellCommandOutput(
stdout="hello",
stderr="",
outcome=ShellCallOutcome(type="exit", exit_code=0),
)
payload = run_loop.serialize_shell_output(output)
assert payload["outcome"]["type"] == "exit"
assert payload["outcome"]["exit_code"] == 0
assert "exitCode" not in payload["outcome"]
def test_shell_rejection_payload_preserves_missing_exit_code() -> None:
agent = Agent(name="tester", model=FakeModel())
raw_item = {
"type": "shell_call_output",
"call_id": "call-1",
"output": [
{
"stdout": "",
"stderr": "rejected",
"outcome": {"type": "exit", "exit_code": None},
}
],
}
item = ToolCallOutputItem(agent=agent, raw_item=raw_item, output="rejected")
payload = item.to_input_item()
assert isinstance(payload, dict)
outputs = payload.get("output")
assert isinstance(outputs, list)
first_output = outputs[0]
assert isinstance(first_output, dict)
outcome = first_output.get("outcome")
assert isinstance(outcome, dict)
assert outcome.get("exit_code") is None
assert "exitCode" not in outcome
def test_shell_output_preserves_zero_exit_code() -> None:
agent = Agent(name="tester", model=FakeModel())
raw_item = {
"type": "shell_call_output",
"call_id": "call-2",
"output": [
{
"stdout": "ok",
"stderr": "",
"outcome": {"type": "exit", "exit_code": 0},
}
],
}
item = ToolCallOutputItem(agent=agent, raw_item=raw_item, output="ok")
payload = item.to_input_item()
assert isinstance(payload, dict)
outputs = payload.get("output")
assert isinstance(outputs, list)
first_output = outputs[0]
assert isinstance(first_output, dict)
outcome = first_output.get("outcome")
assert isinstance(outcome, dict)
assert outcome["exit_code"] == 0
assert "exitCode" not in outcome