Repository navigation
Expand file tree
/
Copy pathcodex-status.py
More file actions
executable file
·402 lines (351 loc) · 11.7 KB
/
Copy pathcodex-status.py
File metadata and controls
executable file
·402 lines (351 loc) · 11.7 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
#!/usr/bin/env python3
"""Show the status of this user's running Codex TUI sessions on Solaris.
The monitor reads Solaris /proc descriptor links and Codex rollout JSONL files.
It does not connect to the app-server daemon or open Codex SQLite databases.
"""
import argparse
from dataclasses import dataclass
from datetime import datetime
import json
import os
from pathlib import Path
import re
import subprocess
import sys
import time
START_EVENTS = {"task_started", "turn_started"}
COMPLETE_EVENTS = {"task_complete", "turn_complete"}
VERSION_RE = re.compile(r"\b(\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?)\b")
THREAD_ID_RE = re.compile(
r"([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-"
r"[0-9a-fA-F]{4}-[0-9a-fA-F]{12})\.jsonl$"
)
_VERSION_CACHE: dict[
tuple[str, int | None, int | None, str | None], tuple[str, str]
] = {}
@dataclass(frozen=True)
class RolloutInfo:
path: Path
thread_id: str | None
cli_version: str | None
cwd: str | None
source: object
thread_source: str | None
originator: str | None
state: str
lifecycle: str | None
last_activity_at: float
error: str | None = None
def _link_target(link: Path) -> Path:
target = Path(os.readlink(link))
if target.is_absolute():
return target
return link.parent.joinpath(target).resolve(strict=False)
def _thread_id_from_path(path: Path) -> str | None:
match = THREAD_ID_RE.search(path.name)
return match.group(1) if match else None
def _read_session_meta(path: Path) -> dict:
with path.open("rb") as stream:
raw = stream.readline()
record = json.loads(raw)
if record.get("type") != "session_meta":
raise ValueError("first rollout record is not session_meta")
payload = record.get("payload")
if not isinstance(payload, dict):
raise ValueError("session_meta payload is not an object")
return payload
def _reverse_lines(path: Path, block_size: int = 64 * 1024):
"""Yield binary lines from a file in reverse order."""
with path.open("rb") as stream:
position = stream.seek(0, os.SEEK_END)
remainder = b""
while position:
size = min(block_size, position)
position -= size
stream.seek(position)
chunk = stream.read(size) + remainder
lines = chunk.split(b"\n")
remainder = lines[0]
for line in reversed(lines[1:]):
if line:
yield line
if remainder:
yield remainder
def _latest_lifecycle(path: Path) -> tuple[str, str | None]:
for raw in _reverse_lines(path):
try:
record = json.loads(raw)
except (UnicodeDecodeError, json.JSONDecodeError):
# A writer may currently have an incomplete final record.
continue
if record.get("type") != "event_msg":
continue
payload = record.get("payload")
if not isinstance(payload, dict):
continue
event_type = payload.get("type")
if event_type in START_EVENTS:
return "WORKING", event_type
if event_type in COMPLETE_EVENTS:
return "IDLE", event_type
if event_type == "turn_aborted":
return "IDLE", event_type
return "UNKNOWN", None
def read_rollout(path: Path) -> RolloutInfo:
metadata = _read_session_meta(path)
state, lifecycle = _latest_lifecycle(path)
modified = path.stat().st_mtime
return RolloutInfo(
path=path,
thread_id=(
metadata.get("id")
or metadata.get("session_id")
or _thread_id_from_path(path)
),
cli_version=metadata.get("cli_version"),
cwd=metadata.get("cwd"),
source=metadata.get("source"),
thread_source=metadata.get("thread_source"),
originator=metadata.get("originator"),
state=state,
lifecycle=lifecycle,
last_activity_at=modified,
)
def unreadable_rollout(path: Path, error: Exception) -> RolloutInfo:
try:
modified = path.stat().st_mtime
except OSError:
modified = time.time()
return RolloutInfo(
path=path,
thread_id=_thread_id_from_path(path),
cli_version=None,
cwd=None,
source=None,
thread_source=None,
originator=None,
state="UNKNOWN",
lifecycle=None,
last_activity_at=modified,
error=str(error),
)
def is_user_tui_rollout(info: RolloutInfo) -> bool:
if info.originator not in (None, "codex-tui"):
return False
return info.thread_source == "user" or info.source == "cli"
def _rollout_links(path_dir: Path) -> set[Path]:
paths = set()
try:
entries = list(path_dir.iterdir())
except OSError:
return paths
for entry in entries:
if not entry.name.isdigit():
continue
try:
target = _link_target(entry)
except OSError:
continue
if (
target.name.startswith("rollout-")
and target.suffix == ".jsonl"
and "sessions" in target.parts
):
paths.add(target)
return paths
def _running_version(
executable: Path, fallback: str | None
) -> tuple[str, str]:
try:
stat = executable.stat()
cache_key = (
str(executable),
stat.st_mtime_ns,
stat.st_size,
fallback,
)
except OSError:
cache_key = (str(executable), None, None, fallback)
cached = _VERSION_CACHE.get(cache_key)
if cached is not None:
return cached
try:
result = subprocess.run(
[str(executable), "--version"],
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
timeout=3,
check=False,
)
match = VERSION_RE.search(result.stdout)
if result.returncode == 0 and match:
version = (match.group(1), "executable")
_VERSION_CACHE[cache_key] = version
return version
except (OSError, subprocess.SubprocessError):
pass
if fallback:
version = (fallback, "rollout")
else:
version = ("unknown", "unknown")
_VERSION_CACHE[cache_key] = version
return version
def _codex_home_from_rollout(path: Path) -> str | None:
indexes = [
index for index, part in enumerate(path.parts) if part == "sessions"
]
if not indexes:
return None
home = Path(*path.parts[: indexes[-1]])
return str(home) or "/"
def collect_sessions(
proc_root: Path = Path("/proc"), uid: int | None = None
) -> list[dict]:
"""Collect one record per current-user Codex TUI process."""
if uid is None:
uid = os.geteuid()
processes = []
try:
pid_dirs = list(proc_root.iterdir())
except OSError:
return processes
for pid_dir in pid_dirs:
if not pid_dir.name.isdigit():
continue
try:
if pid_dir.stat().st_uid != uid:
continue
executable = _link_target(pid_dir / "path" / "a.out")
if executable.name != "codex":
continue
process_cwd = str(_link_target(pid_dir / "path" / "cwd"))
except OSError:
continue
valid = []
unreadable = []
for rollout_path in _rollout_links(pid_dir / "path"):
try:
info = read_rollout(rollout_path)
except (
OSError,
ValueError,
UnicodeDecodeError,
json.JSONDecodeError,
) as error:
unreadable.append(unreadable_rollout(rollout_path, error))
continue
if is_user_tui_rollout(info):
valid.append(info)
if valid:
rollout = max(valid, key=lambda item: item.last_activity_at)
elif unreadable:
rollout = max(unreadable, key=lambda item: item.last_activity_at)
else:
# Exclude daemons, --version invocations and non-TUI processes.
continue
version, version_source = _running_version(
executable, rollout.cli_version
)
processes.append(
{
"pid": int(pid_dir.name),
"version": version,
"version_source": version_source,
"state": rollout.state,
"lifecycle": rollout.lifecycle,
"last_activity_at": rollout.last_activity_at,
"last_activity_seconds": max(
0, int(time.time() - rollout.last_activity_at)
),
"thread_id": rollout.thread_id,
"cwd": rollout.cwd or process_cwd,
"process_cwd": process_cwd,
"executable": str(executable),
"rollout": str(rollout.path),
"codex_home": _codex_home_from_rollout(rollout.path),
"error": rollout.error,
}
)
return sorted(processes, key=lambda item: (item["cwd"], item["pid"]))
def _format_age(seconds: int) -> str:
if seconds < 60:
return f"{seconds}s"
minutes, seconds = divmod(seconds, 60)
if minutes < 60:
return f"{minutes}m{seconds:02d}s"
hours, minutes = divmod(minutes, 60)
if hours < 24:
return f"{hours}h{minutes:02d}m"
days, hours = divmod(hours, 24)
return f"{days}d{hours:02d}h"
def render_table(sessions: list[dict]) -> str:
if not sessions:
return "No running Codex TUI sessions found."
headers = ["PID", "VERSION", "STATE", "LAST", "THREAD", "DIRECTORY"]
rows = [
[
str(item["pid"]),
item["version"],
item["state"],
_format_age(item["last_activity_seconds"]),
(item["thread_id"] or "-")[:12],
item["cwd"],
]
for item in sessions
]
widths = [
max(len(headers[index]), *(len(row[index]) for row in rows))
for index in range(len(headers) - 1)
]
def format_row(row):
columns = [
value.ljust(widths[index]) for index, value in enumerate(row[:-1])
]
return " ".join([*columns, row[-1]])
return "\n".join([format_row(headers), *map(format_row, rows)])
def _positive_interval(value: str) -> float:
interval = float(value)
if interval <= 0:
raise argparse.ArgumentTypeError(
"watch interval must be greater than zero"
)
return interval
def parse_args(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--watch",
nargs="?",
const=5.0,
type=_positive_interval,
metavar="SECONDS",
help="refresh continuously (default interval: 5 seconds)",
)
parser.add_argument(
"--json", action="store_true", help="print a single JSON snapshot"
)
args = parser.parse_args(argv)
if args.json and args.watch is not None:
parser.error("--json and --watch cannot be used together")
return args
def main(argv=None) -> int:
args = parse_args(argv)
if args.json:
print(json.dumps(collect_sessions(), indent=2, sort_keys=True))
return 0
if args.watch is None:
print(render_table(collect_sessions()))
return 0
try:
while True:
if sys.stdout.isatty():
print("\033[H\033[2J", end="")
now = datetime.now().astimezone().strftime("%Y-%m-%d %H:%M:%S %Z")
print(f"Codex sessions at {now}")
print(render_table(collect_sessions()), flush=True)
time.sleep(args.watch)
except KeyboardInterrupt:
return 0
if __name__ == "__main__":
raise SystemExit(main())