File size: 11,243 Bytes
55626ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Real processes share synthetic credentials and a loopback-only SSE server."""

import base64
from contextlib import contextmanager
import io
import json
import os
from pathlib import Path
import subprocess
import sys
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

import pytest
from PIL import Image


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


def _wait(path, process=None):
    deadline = time.monotonic() + 20
    while not path.exists():
        if process is not None and process.poll() is not None:
            output, error = process.communicate()
            pytest.fail(f"Synthetic child exited before barrier: {output}\n{error}")
        if time.monotonic() > deadline:
            pytest.fail("Synthetic process barrier timed out")
        time.sleep(0.01)


def _result(process):
    output, error = process.communicate(timeout=30)
    assert process.returncode == 0, (output, error)
    return json.loads(output.splitlines()[-1])


@pytest.fixture
def exchange(tmp_path):
    image = io.BytesIO()
    Image.new("RGB", (2, 2), "red").save(image, format="PNG")
    state = {
        "account": "account-A",
        "posts": [],
        "refreshes": 0,
        "hold_image": False,
        "reject_first": False,
        "received": threading.Event(),
        "release": threading.Event(),
        "png": image.getvalue(),
    }

    class Handler(BaseHTTPRequestHandler):
        def log_message(self, *args):
            pass

        def do_POST(self):
            payload = (
                json.loads(self.rfile.read(int(self.headers["Content-Length"])))
                if self.path == "/responses"
                else None
            )
            if payload is not None:
                state["posts"].append(self.headers.get("chatgpt-account-id"))
                if state["reject_first"]:
                    state["reject_first"] = False
                    self.send_response(401)
                    self.send_header("Content-Length", "2")
                    self.end_headers()
                    self.wfile.write(b"{}")
                    return
                event = {
                    "type": "response.completed",
                    "response": {
                        "status": "completed",
                        "output": [
                            {
                                "id": "synthetic-image",
                                "type": "image_generation_call",
                                "status": "completed",
                                "result": base64.b64encode(state["png"]).decode(),
                            }
                        ],
                    },
                }
                body = b"data: " + json.dumps(event).encode() + b"\n\n"
                self.send_response(200)
                self.send_header("Content-Type", "text/event-stream")
                self.send_header("Content-Length", str(len(body)))
                self.end_headers()
                self.wfile.flush()
                state["received"].set()
                if state["hold_image"]:
                    state["release"].wait(20)
                self.wfile.write(body)
                return
            data = self.rfile.read(int(self.headers["Content-Length"]))
            if self.path == "/token":
                if b"refresh_token" in data:
                    state["refreshes"] += 1
                account = state["account"]
                claims = (
                    base64.urlsafe_b64encode(
                        json.dumps(
                            {
                                "https://api.openai.com/auth": {
                                    "chatgpt_account_id": account
                                }
                            }
                        ).encode()
                    )
                    .decode()
                    .rstrip("=")
                )
                result = {
                    "access_token": "access-" + account,
                    "refresh_token": "refresh-" + account,
                    "id_token": "header." + claims + ".sig",
                    "expires_in": 3600,
                }
            else:
                result = {
                    "authorization_code": "synthetic-code",
                    "code_verifier": "synthetic-verifier",
                }
            body = json.dumps(result).encode()
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

    server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
    thread = threading.Thread(target=server.serve_forever, daemon=True)
    thread.start()
    url = f"http://127.0.0.1:{server.server_port}"
    prelude = f"""
import json, time
from pathlib import Path
from app.services import codex_oauth as oauth, codex_image as image
oauth.ISSUER = {url!r}
oauth.TOKEN_URL = {url + "/token"!r}
oauth.RESPONSES_URL = {url + "/responses"!r}
"""
    children = []

    def child(script):
        environment = dict(
            os.environ, MPT_HOME=str(tmp_path / "runtime"), PYTHONPATH=str(ROOT)
        )
        process = subprocess.Popen(
            [sys.executable, "-X", "utf8", "-c", prelude + script],
            cwd=ROOT,
            env=environment,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            encoding="utf-8",
        )
        children.append(process)
        return process

    def login(account):
        state["account"] = account
        return _result(
            child("""
oauth.disconnect()
flow = dict(device_auth_id='synthetic-device', user_code='synthetic-code',
            generation=oauth.authorization_generation(), session_revision=None, expires_at=time.time()+60)
assert oauth.poll_login(flow)
print(json.dumps({'revision': oauth.capture_session_binding().revision}))
""")
        )

    state.update(child=child, login=login, root=tmp_path)
    try:
        login("account-A")
        yield state
    finally:
        state["release"].set()
        for process in children:
            if process.poll() is None:
                process.kill()
            process.communicate(timeout=5)
        server.shutdown()
        server.server_close()
        thread.join(timeout=5)


@contextmanager
def queued(exchange):
    ready, resume = exchange["root"] / "ready", exchange["root"] / "resume"
    process = exchange["child"](f"""
context=image.capture_context({{'codex_image_model_name':'synthetic-model'}})
Path({str(ready)!r}).touch()
deadline=time.monotonic()+20
while not Path({str(resume)!r}).exists():
    if time.monotonic()>deadline: raise RuntimeError('barrier timeout')
    time.sleep(0.01)
try:
    result=image.generate_image('synthetic prompt',context)
    print(json.dumps({{'ok':True,'bytes':len(result),'generation':context.generation}}))
except oauth.CodexError as error:
    print(json.dumps({{'ok':False,'error':error.key,'generation':context.generation}}))
""")
    _wait(ready, process)
    try:
        yield process, resume
    finally:
        resume.touch()


@pytest.mark.parametrize("next_account", ["account-B", "account-A"])
def test_queued_request_rejects_other_process_switch_or_relogin(exchange, next_account):
    with queued(exchange) as (process, resume):
        exchange["login"](next_account)
        resume.touch()
        result = _result(process)
    assert result == {"ok": False, "error": "codex.oauth.changed", "generation": 0}
    assert exchange["posts"] == []


def test_inflight_image_rejected_after_other_process_account_switch(exchange):
    exchange["hold_image"] = True
    with queued(exchange) as (process, resume):
        resume.touch()
        assert exchange["received"].wait(20)
        # This must finish while the image stream is still waiting: the file
        # lock protects dispatch, not the lifetime of the stream.
        exchange["login"]("account-B")
        exchange["release"].set()
        result = _result(process)
    assert result["error"] == "codex.oauth.changed"
    assert exchange["posts"] == ["account-A"]


def test_refresh_in_another_process_preserves_queued_authorization(exchange):
    with queued(exchange) as (process, resume):
        refreshed = _result(
            exchange["child"]("""
before=oauth.capture_session_binding()
oauth._credentials(force=True,session=before)
print(json.dumps({'stable':before==oauth.capture_session_binding()}))
""")
        )
        assert refreshed["stable"]
        resume.touch()
        result = _result(process)
    assert result["ok"]
    assert exchange["posts"] == ["account-A"]
    assert exchange["refreshes"] == 1


def test_two_processes_refresh_rotating_token_once(exchange):
    _result(
        exchange["child"]("""
with oauth._store_guard():
    bundle=oauth._load()
    bundle['expires_at']=0
    oauth._save(bundle)
print('{}')
""")
    )
    script = """
binding=oauth.capture_session_binding()
credentials=oauth._credentials(session=binding)
print(json.dumps({'stable':binding==oauth.capture_session_binding(),'access':credentials['access_token']}))
"""
    first, second = exchange["child"](script), exchange["child"](script)
    results = [_result(first), _result(second)]
    assert all(
        result == {"stable": True, "access": "access-account-A"} for result in results
    )
    assert exchange["refreshes"] == 1


def test_two_processes_migrate_one_legacy_session_revision(exchange):
    _result(
        exchange["child"]("""
with oauth._store_guard():
    bundle=oauth._load()
    bundle.pop('session_revision')
    oauth._credential_path().write_bytes(oauth._protect(json.dumps(bundle).encode()))
print('{}')
""")
    )
    script = "print(json.dumps({'revision':oauth.capture_session_binding().revision}))"
    first, second = exchange["child"](script), exchange["child"](script)
    assert _result(first)["revision"] == _result(second)["revision"]


def test_switch_before_401_refresh_never_sends_as_new_account(exchange):
    exchange["reject_first"] = True
    ready = exchange["root"] / "refresh-ready"
    resume = exchange["root"] / "refresh-resume"
    process = exchange["child"](
        f"""
context=image.capture_context({{'codex_image_model_name':'synthetic-model'}})
original=oauth._credentials
def wait_before_refresh(force=False, **kwargs):
    if force:
        Path({str(ready)!r}).touch()
        deadline=time.monotonic()+20
        while not Path({str(resume)!r}).exists():
            if time.monotonic()>deadline: raise RuntimeError('barrier timeout')
            time.sleep(0.01)
    return original(force=force, **kwargs)
oauth._credentials=wait_before_refresh
try:
    image.generate_image('synthetic prompt',context)
    print(json.dumps({{'ok':True}}))
except oauth.CodexError as error:
    print(json.dumps({{'ok':False,'error':error.key}}))
"""
    )
    _wait(ready, process)
    exchange["login"]("account-B")
    resume.touch()
    result = _result(process)
    assert result == {"ok": False, "error": "codex.oauth.changed"}
    assert exchange["posts"] == ["account-A"]
    assert exchange["refreshes"] == 0