File size: 12,267 Bytes
f15fb1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from __future__ import annotations

import asyncio
from pathlib import Path
from types import SimpleNamespace
from typing import Any
from unittest.mock import AsyncMock, Mock

import pytest

import openai_codex.api as public_api_module
from openai_codex.api import (
    ApprovalMode,
    AsyncCodex,
    Codex,
    ExternalMessage,
    Sandbox,
    TextInput,
)
from openai_codex.client import _params_dict
from openai_codex.generated.v2_all import TurnCompletedNotification, TurnStartParams
from openai_codex.models import InitializeResponse, Notification

ROOT = Path(__file__).resolve().parents[1]


def _approval_settings(params: list[Any]) -> list[dict[str, object]]:
    """Return serialized approval settings from captured Pydantic params."""
    return [
        {
            key: value
            for key, value in param.model_dump(
                by_alias=True,
                exclude_none=True,
                mode="json",
            ).items()
            if key in {"approvalPolicy", "approvalsReviewer"}
        }
        for param in params
    ]


def test_codex_init_failure_closes_client(monkeypatch: pytest.MonkeyPatch) -> None:
    closed: list[bool] = []

    class FakeClient:
        def __init__(self, config=None) -> None:  # noqa: ANN001,ARG002
            self._closed = False

        def start(self) -> None:
            return None

        def initialize(self) -> InitializeResponse:
            return InitializeResponse.model_validate({})

        def close(self) -> None:
            self._closed = True
            closed.append(True)

    monkeypatch.setattr(public_api_module, "CodexClient", FakeClient)

    with pytest.raises(RuntimeError, match="missing required metadata"):
        Codex()

    assert closed == [True]


def test_async_codex_init_failure_closes_client() -> None:
    async def scenario() -> None:
        codex = AsyncCodex()
        close_calls = 0

        async def fake_start() -> None:
            return None

        async def fake_initialize() -> InitializeResponse:
            return InitializeResponse.model_validate({})

        async def fake_close() -> None:
            nonlocal close_calls
            close_calls += 1

        codex._client.start = fake_start  # type: ignore[method-assign]
        codex._client.initialize = fake_initialize  # type: ignore[method-assign]
        codex._client.close = fake_close  # type: ignore[method-assign]

        with pytest.raises(RuntimeError, match="missing required metadata"):
            await codex.models()

        assert close_calls == 1
        assert codex._initialized is False
        assert codex._init is None

    asyncio.run(scenario())


def test_async_codex_initializes_only_once_under_concurrency() -> None:
    async def scenario() -> None:
        codex = AsyncCodex()
        start_calls = 0
        initialize_calls = 0
        ready = asyncio.Event()

        async def fake_start() -> None:
            nonlocal start_calls
            start_calls += 1

        async def fake_initialize() -> InitializeResponse:
            nonlocal initialize_calls
            initialize_calls += 1
            ready.set()
            await asyncio.sleep(0.02)
            return InitializeResponse.model_validate(
                {
                    "userAgent": "codex-cli/1.2.3",
                    "serverInfo": {"name": "codex-cli", "version": "1.2.3"},
                }
            )

        async def fake_model_list(include_hidden: bool = False):  # noqa: ANN202,ARG001
            await ready.wait()
            return object()

        codex._client.start = fake_start  # type: ignore[method-assign]
        codex._client.initialize = fake_initialize  # type: ignore[method-assign]
        codex._client.model_list = fake_model_list  # type: ignore[method-assign]

        await asyncio.gather(codex.models(), codex.models())

        assert start_calls == 1
        assert initialize_calls == 1

    asyncio.run(scenario())


@pytest.mark.parametrize("api_type", [Codex, AsyncCodex])
@pytest.mark.parametrize(
    ("options", "expected"),
    [
        ({}, {}),
        ({"include_turns": None}, {}),
        ({"include_turns": True}, {"excludeTurns": False}),
        ({"include_turns": False}, {"excludeTurns": True}),
    ],
)
def test_include_turns_preserves_omission_and_inverts_explicit_values(
    api_type, options, expected
) -> None:
    async def scenario() -> None:
        async_api = api_type is AsyncCodex
        rpc = AsyncMock if async_api else Mock
        thread_response = SimpleNamespace(thread=SimpleNamespace(id="thread-2"))
        client = SimpleNamespace(
            thread_resume=rpc(return_value=thread_response),
            thread_fork=rpc(return_value=thread_response),
        )
        codex = api_type.__new__(api_type)
        codex._client = client
        codex._initialized = True

        for method in ("thread_resume", "thread_fork"):
            thread = getattr(codex, method)("thread-1", **options)
            if async_api:
                thread = await thread
            assert thread.id == "thread-2"
            assert _params_dict(getattr(client, method).call_args.args[1]) == {
                "threadId": "thread-1",
                **expected,
            }

    asyncio.run(scenario())


@pytest.mark.parametrize("api_type", [Codex, AsyncCodex])
@pytest.mark.parametrize("method", ["run", "turn"])
@pytest.mark.parametrize(
    "content", [None, "External update", [{"type": "input_text", "text": "External update"}]]
)
def test_turn_inputs_and_options_reach_the_client(api_type, method, content) -> None:
    """User and external inputs preserve distinct wire representations for every entry point."""

    async def scenario() -> None:
        async_api = api_type is AsyncCodex
        rpc = AsyncMock if async_api else Mock
        completed = Notification(
            method="turn/completed",
            payload=TurnCompletedNotification.model_validate(
                {
                    "threadId": "thread-1",
                    "turn": {"id": "turn-1", "items": [], "status": "completed"},
                }
            ),
        )
        subscription = SimpleNamespace(next=Mock(return_value=completed), close=Mock())
        client = SimpleNamespace(
            _start_turn=rpc(
                return_value=(SimpleNamespace(turn=SimpleNamespace(id="turn-1")), subscription)
            ),
        )
        codex = api_type.__new__(api_type)
        codex._client = client
        codex._initialized = True
        thread = (
            public_api_module.AsyncThread(codex, "thread-1")
            if async_api
            else public_api_module.Thread(client, "thread-1")
        )
        input = (
            "Continue."
            if content is None
            else ExternalMessage(tool_name="notifications", namespace="slack", content=content)
        )
        turn = getattr(thread, method)(
            input,
            service_tier="priority",
            turn_service_tier="default",
            source="automation",
        )
        if async_api:
            turn = await turn
        assert turn.id == "turn-1"
        expected_input = (
            [{"type": "text", "text": "Continue.", "text_elements": []}] if content is None else []
        )
        expected_tool_output = (
            {}
            if content is None
            else {
                "toolOutput": {"name": "notifications", "namespace": "slack", "output": content},
            }
        )
        assert _params_dict(client._start_turn.call_args.kwargs["params"]) == {
            "threadId": "thread-1",
            "input": expected_input,
            "serviceTier": "priority",
            "serviceTierForTurn": "default",
            "turnTrigger": "automation",
            **expected_tool_output,
        }

    asyncio.run(scenario())


@pytest.mark.parametrize("api_type", [Codex, AsyncCodex])
def test_external_messages_cannot_be_mixed_with_user_input_or_sent_as_user_steering(
    api_type,
) -> None:
    async def scenario() -> None:
        async_api = api_type is AsyncCodex
        rpc = AsyncMock if async_api else Mock
        client = SimpleNamespace(
            _start_turn=rpc(), turn_steer=rpc(), _subscribe_turn_notifications=Mock()
        )
        codex = api_type.__new__(api_type)
        codex._client = client
        codex._initialized = True
        thread = (
            public_api_module.AsyncThread(codex, "thread-1")
            if async_api
            else public_api_module.Thread(client, "thread-1")
        )
        handle = (
            public_api_module.AsyncTurnHandle(codex, "thread-1", "turn-1")
            if async_api
            else public_api_module.TurnHandle(client, "thread-1", "turn-1")
        )
        external = ExternalMessage(tool_name="notifications", content="Untrusted update")
        for operation, input in (
            (thread.turn, [TextInput("User request"), external]),
            (handle.steer, external),
        ):
            with pytest.raises(TypeError):
                result = operation(input)
                if async_api:
                    await result
        with pytest.raises(ValueError, match="tool_name"):
            result = thread.turn(ExternalMessage(tool_name="  ", content="Untrusted update"))
            if async_api:
                await result
        client._start_turn.assert_not_called()
        client.turn_steer.assert_not_called()

    asyncio.run(scenario())


def _approval_mode_turn_params(approval_mode: ApprovalMode) -> TurnStartParams:
    """Build real generated turn params from one public approval mode."""
    approval_policy, approvals_reviewer = public_api_module._approval_mode_settings(approval_mode)
    return TurnStartParams(
        thread_id="thread-1",
        input=[],
        approval_policy=approval_policy,
        approvals_reviewer=approvals_reviewer,
    )


def test_approval_modes_serialize_to_expected_start_params() -> None:
    """ApprovalMode should map to the app-server params sent for new work."""
    assert {
        mode.value: _approval_settings([_approval_mode_turn_params(mode)])[0]
        for mode in ApprovalMode
    } == {
        "deny_all": {"approvalPolicy": "never"},
        "auto_review": {
            "approvalPolicy": "on-request",
            "approvalsReviewer": "auto_review",
        },
    }


def test_unknown_approval_mode_is_rejected() -> None:
    """Invalid approval modes should fail before params are constructed."""
    with pytest.raises(ValueError, match="deny_all, auto_review"):
        public_api_module._approval_mode_settings("allow_all")  # type: ignore[arg-type]


def test_sandbox_presets_serialize_for_threads_and_turns() -> None:
    """One public sandbox enum should map to both stable wire representations."""
    assert {
        sandbox.name: public_api_module._sandbox_mode(sandbox).value for sandbox in Sandbox
    } == {
        "read_only": "read-only",
        "workspace_write": "workspace-write",
        "full_access": "danger-full-access",
    }
    assert {
        sandbox.name: public_api_module._sandbox_policy(sandbox).model_dump(
            by_alias=True,
            mode="json",
        )
        for sandbox in Sandbox
    } == {
        "read_only": {"networkAccess": False, "type": "readOnly"},
        "workspace_write": {
            "excludeSlashTmp": False,
            "excludeTmpdirEnvVar": False,
            "networkAccess": False,
            "type": "workspaceWrite",
            "writableRoots": [],
        },
        "full_access": {"type": "dangerFullAccess"},
    }


def test_raw_sandbox_strings_are_rejected() -> None:
    """Callers should use the discoverable enum rather than memorizing values."""
    with pytest.raises(ValueError, match="Sandbox\\.workspace_write"):
        public_api_module._sandbox_mode("workspace")  # type: ignore[arg-type]


def test_retry_examples_compare_status_with_enum() -> None:
    for path in (
        ROOT / "examples" / "10_error_handling_and_retry" / "sync.py",
        ROOT / "examples" / "10_error_handling_and_retry" / "async.py",
    ):
        source = path.read_text()
        assert '== "failed"' not in source
        assert "TurnStatus.failed" in source