-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex_exec.py
More file actions
419 lines (351 loc) · 13.4 KB
/
codex_exec.py
File metadata and controls
419 lines (351 loc) · 13.4 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
"""Codex backend using `codex exec` subprocesses."""
from __future__ import annotations
import asyncio
import json
import logging
import os
import signal
import tempfile
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable
from codex_common import CodexResult, StatusCallback
from settings import Settings
from text_utils import format_codex_status, format_command_output, truncate_text
STOP_GRACE_SECONDS = 5
@dataclass
class ExecActiveJob:
process: asyncio.subprocess.Process
stopped: bool = False
class ExecCodexBackend:
def __init__(self) -> None:
self._active_jobs: dict[int, ExecActiveJob] = {}
self._lock = asyncio.Lock()
async def run(
self,
settings: Settings,
prompt: str,
*,
chat_id: int | None,
thread_id: str | None = None,
image_paths: Iterable[Path] = (),
status_callback: StatusCallback | None = None,
) -> CodexResult:
return await run_codex(
settings,
prompt,
thread_id=thread_id,
image_paths=image_paths,
status_callback=status_callback,
chat_id=chat_id,
backend=self,
)
async def steer(self, chat_id: int, text: str) -> str:
raise RuntimeError(
"The exec backend cannot steer a running task. Set CODEX_BACKEND=app-server."
)
async def stop(self, chat_id: int) -> str:
async with self._lock:
job = self._active_jobs.get(chat_id)
if job is None or job.process.returncode is not None:
return "No running Codex task is active for this chat."
job.stopped = True
signal_process_group(job.process, signal.SIGINT)
try:
await asyncio.wait_for(job.process.wait(), timeout=STOP_GRACE_SECONDS)
except asyncio.TimeoutError:
logging.warning(
"Codex exec process did not stop within %s seconds; killing it",
STOP_GRACE_SECONDS,
)
signal_process_group(job.process, signal.SIGKILL)
await job.process.wait()
return "Stop requested for the running Codex task."
async def shutdown(self) -> None:
async with self._lock:
jobs = list(self._active_jobs.values())
for job in jobs:
if job.process.returncode is None:
signal_process_group(job.process, signal.SIGTERM)
async def register(self, chat_id: int | None, process: asyncio.subprocess.Process) -> None:
if chat_id is None:
return
async with self._lock:
self._active_jobs[chat_id] = ExecActiveJob(process)
async def unregister(self, chat_id: int | None, process: asyncio.subprocess.Process) -> bool:
if chat_id is None:
return False
async with self._lock:
job = self._active_jobs.get(chat_id)
if job is None or job.process is not process:
return False
stopped = job.stopped
del self._active_jobs[chat_id]
return stopped
def signal_process_group(
process: asyncio.subprocess.Process, sig: signal.Signals
) -> None:
try:
os.killpg(os.getpgid(process.pid), sig)
except ProcessLookupError:
return
except OSError:
if process.returncode is None:
process.send_signal(sig)
def append_image_options(command: list[str], image_paths: Iterable[Path]) -> None:
for image_path in image_paths:
command.extend(["--image", str(image_path)])
def build_codex_command(
settings: Settings, output_file: Path, image_paths: Iterable[Path] = ()
) -> list[str]:
command = [
settings.codex_binary,
"exec",
]
if "--color" in settings.codex_exec_options:
command.extend(["--color", "never"])
if "--output-last-message" in settings.codex_exec_options:
command.extend(["--output-last-message", str(output_file)])
if "--json" in settings.codex_exec_options:
command.append("--json")
if settings.codex_model:
command.extend(["--model", settings.codex_model])
if settings.codex_profile:
command.extend(["--profile", settings.codex_profile])
if settings.codex_dangerously_bypass:
command.append("--dangerously-bypass-approvals-and-sandbox")
else:
if "--sandbox" in settings.codex_exec_options:
command.extend(["--sandbox", settings.codex_sandbox])
if "--ask-for-approval" in settings.codex_exec_options:
command.extend(["--ask-for-approval", settings.codex_approval])
if (
settings.codex_skip_git_repo_check
and "--skip-git-repo-check" in settings.codex_exec_options
):
command.append("--skip-git-repo-check")
if settings.codex_extra_args:
command.extend(settings.codex_extra_args)
append_image_options(command, image_paths)
command.extend(["--cd", str(settings.codex_workdir), "-"])
return command
def build_codex_resume_command(
settings: Settings,
output_file: Path,
thread_id: str,
image_paths: Iterable[Path] = (),
) -> list[str]:
command = [
settings.codex_binary,
"exec",
"resume",
thread_id,
]
if "--output-last-message" in settings.codex_resume_options:
command.extend(["--output-last-message", str(output_file)])
if "--json" in settings.codex_resume_options:
command.append("--json")
if settings.codex_model:
command.extend(["--model", settings.codex_model])
if settings.codex_dangerously_bypass:
command.append("--dangerously-bypass-approvals-and-sandbox")
if (
settings.codex_skip_git_repo_check
and "--skip-git-repo-check" in settings.codex_resume_options
):
command.append("--skip-git-repo-check")
append_image_options(command, image_paths)
command.append("-")
return command
def parse_codex_json_stdout(stdout: str) -> tuple[str | None, str]:
thread_id: str | None = None
final_text = ""
for line in stdout.splitlines():
line = line.strip()
if not line:
continue
try:
event = json.loads(line)
except json.JSONDecodeError:
continue
if event.get("type") == "thread.started" and event.get("thread_id"):
thread_id = str(event["thread_id"])
elif event.get("type") == "item.completed":
item = event.get("item")
if isinstance(item, dict) and item.get("type") == "agent_message":
text = item.get("text")
if isinstance(text, str):
final_text = text.strip()
return thread_id, final_text
def format_codex_progress_event(event: dict) -> str | None:
event_type = event.get("type")
if event_type == "thread.started":
thread_id = event.get("thread_id")
if thread_id:
return f"Started session\n{thread_id}"
return "Started a new session"
if event_type == "turn.started":
return "Working on the request..."
if event_type == "turn.completed":
return "Completed. Sending the final answer..."
if event_type not in {"item.started", "item.updated", "item.completed"}:
return None
item = event.get("item")
if not isinstance(item, dict):
return None
item_type = item.get("type")
if item_type == "command_execution":
command = str(item.get("command") or "").strip()
command = truncate_text(command, 350)
exit_code = item.get("exit_code")
if event_type == "item.completed" or item.get("status") == "completed":
status = "Finished command"
if exit_code is not None:
status += f" (exit {exit_code})"
else:
status = "Running command"
parts = [status]
if command:
parts.append(command)
output = format_command_output(str(item.get("aggregated_output") or ""))
if output:
parts.append(f"Latest output:\n{output}")
return "\n\n".join(parts)
if item_type == "agent_message":
return "Writing the final answer..."
if item_type in {"reasoning", "agent_reasoning"}:
return "Reasoning..."
if isinstance(item_type, str) and item_type:
label = item_type.replace("_", " ")
if event_type == "item.completed":
return f"Completed {label}."
return f"Working on {label}..."
return None
async def write_process_stdin(
process: asyncio.subprocess.Process, prompt: str
) -> None:
if process.stdin is None:
return
try:
process.stdin.write(prompt.encode("utf-8"))
await process.stdin.drain()
except (BrokenPipeError, ConnectionResetError):
pass
finally:
process.stdin.close()
try:
await process.stdin.wait_closed()
except (BrokenPipeError, ConnectionResetError):
pass
async def read_codex_stdout(
stream: asyncio.StreamReader,
lines: list[str],
status_callback: StatusCallback | None,
) -> None:
while True:
line_bytes = await stream.readline()
if not line_bytes:
break
line = line_bytes.decode("utf-8", errors="replace").rstrip("\n")
lines.append(line)
if status_callback is None:
continue
try:
event = json.loads(line.strip())
except json.JSONDecodeError:
continue
status = format_codex_progress_event(event)
if status:
await status_callback(format_codex_status(status), False)
async def run_codex(
settings: Settings,
prompt: str,
*,
thread_id: str | None = None,
image_paths: Iterable[Path] = (),
status_callback: StatusCallback | None = None,
chat_id: int | None = None,
backend: ExecCodexBackend | None = None,
) -> CodexResult:
image_paths = tuple(image_paths)
codex_options = settings.codex_resume_options if thread_id else settings.codex_exec_options
if image_paths and "--image" not in codex_options:
return CodexResult(
2,
"",
"This Codex CLI does not support image attachments. Upgrade Codex or remove the image.",
thread_id,
)
with tempfile.TemporaryDirectory(prefix="codex-telegram-") as tmpdir:
output_file = Path(tmpdir) / "last-message.txt"
command = (
build_codex_resume_command(settings, output_file, thread_id, image_paths)
if thread_id
else build_codex_command(settings, output_file, image_paths)
)
process = await asyncio.create_subprocess_exec(
*command,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=settings.codex_workdir,
start_new_session=True,
)
if backend is not None:
await backend.register(chat_id, process)
stdout_lines: list[str] = []
stdout_task: asyncio.Task[None] | None = None
stderr_task: asyncio.Task[bytes] | None = None
try:
if process.stdout is None or process.stderr is None:
raise RuntimeError("Codex process streams were not created")
stdout_task = asyncio.create_task(
read_codex_stdout(process.stdout, stdout_lines, status_callback)
)
stderr_task = asyncio.create_task(process.stderr.read())
await write_process_stdin(process, prompt)
await asyncio.wait_for(
process.wait(),
timeout=settings.codex_timeout_seconds,
)
await stdout_task
stderr_bytes = await stderr_task
except asyncio.TimeoutError:
signal_process_group(process, signal.SIGKILL)
await process.wait()
tasks = [task for task in (stdout_task, stderr_task) if task is not None]
if tasks:
await asyncio.gather(*tasks, return_exceptions=True)
return CodexResult(
124,
"",
f"Codex timed out after {settings.codex_timeout_seconds} seconds.",
thread_id,
)
finally:
stopped = False
if backend is not None:
stopped = await backend.unregister(chat_id, process)
if stopped:
logging.info("Codex exec process was stopped for chat_id=%s", chat_id)
stdout = "\n".join(stdout_lines).strip()
stderr = stderr_bytes.decode("utf-8", errors="replace").strip()
parsed_thread_id, json_answer = parse_codex_json_stdout(stdout)
final_answer = ""
if output_file.exists():
final_answer = output_file.read_text(encoding="utf-8", errors="replace").strip()
if status_callback is not None:
await status_callback(
format_codex_status("Completed. Sending the final answer..."), True
)
returncode = process.returncode or 0
if returncode == -int(signal.SIGINT):
return CodexResult(130, "Codex task was stopped.", "", parsed_thread_id or thread_id)
if stopped and returncode != 0:
return CodexResult(130, "Codex task was stopped.", "", parsed_thread_id or thread_id)
return CodexResult(
returncode,
final_answer or json_answer or stdout,
stderr,
parsed_thread_id or thread_id,
)