Repository navigation
Expand file tree
/
Copy pathagents.py
More file actions
551 lines (468 loc) · 23.9 KB
/
Copy pathagents.py
File metadata and controls
551 lines (468 loc) · 23.9 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
"""AI coding agents: which CLIs exist, which one runs in a terminal, and with what model."""
import json
import os
import re
import shutil
import sqlite3
import time
import tomllib
from contextlib import closing
from datetime import datetime
from pathlib import Path
import psutil
# key: (label, executable names, cmdline markers for node/python/bun installs, fallback models)
# Codex and opencode models come from their own caches (models()); the rest are static fallbacks.
# ponytail: static lists go stale; the --model flag or the agent's config still shows the real one
AGENTS: dict[str, tuple[str, tuple[str, ...], tuple[str, ...], tuple[str, ...]]] = {
"claude": ("Claude Code", ("claude",), ("@anthropic-ai/claude-code",), ("fable", "opus", "sonnet", "haiku", "claude-fable-5-1", "claude-opus-5-5", "claude-sonnet-5-5", "claude-haiku-4-5")),
"codex": ("Codex", ("codex",), ("@openai/codex",), ()),
"gemini": ("Gemini CLI", ("gemini",), ("@google/gemini-cli",), ("gemini-2.5-pro", "gemini-2.5-flash")),
"devin": ("Devin CLI", ("devin",), (), ()),
"copilot": ("Copilot CLI", ("copilot",), ("@github/copilot",), ()),
"cursor": ("Cursor Agent", ("cursor-agent",), (), ()),
"opencode": ("opencode", ("opencode",), ("opencode-ai",), ()),
"aider": ("Aider", ("aider",), ("aider",), ()),
"amp": ("Amp", ("amp",), ("@sourcegraph/amp",), ()),
"qwen": ("Qwen Code", ("qwen",), ("@qwen-code/qwen-code",), ()),
"goose": ("Goose", ("goose",), (), ()),
"droid": ("Factory Droid", ("droid",), (), ()),
"crush": ("Crush", ("crush",), (), ()),
"kiro": ("Kiro CLI", ("kiro-cli",), (), ()),
"pi": ("Pi", ("pi",), (), ()),
"omp": ("OMP", ("omp",), (), ()),
"kimi": ("Kimi Code CLI", ("kimi",), (), ()),
"kilo": ("Kilo Code CLI", ("kilo",), (), ()),
"hermes": ("Hermes Agent", ("hermes",), (), ()),
"qodercli": ("Qoder CLI", ("qodercli",), (), ()),
"letta": ("Letta Code", ("letta",), (), ()),
"mastracode": ("MastraCode", ("mastracode",), (), ()),
"grok": ("Grok CLI", ("grok",), (), ()),
"antigravity": ("Antigravity CLI", ("agy",), (), ()),
"maki": ("Maki", ("maki",), (), ()),
"cline": ("Cline", ("cline",), (), ()),
"command": ("Command Code", ("command",), (), ()),
"muse": ("Muse Code", ("muse",), (), ()),
"prime": ("Prime Agent", ("prime",), (), ()),
# found, never launched: a devin(.exe) outside Devin's cli/ install folder (see identify)
"devin_desktop": ("Devin desktop", (), (), ()),
}
# Approval menus and questions of Claude Code, Codex and Devin (strings from their binaries), plus [y/n].
# Keep in sync with QUESTION_RE in static/app.js.
def question_rule(text: str) -> str | None:
"""Which built-in prompt phrase QUESTION matched (a fixed phrase from the agents' binaries, not screen content)."""
found = list(QUESTION.finditer(text))
return found[-1].group(0).lower()[:40] if found else None
QUESTION = re.compile(
r"do you want to (?:proceed|make this edit|create|run|allow)|would you like to (?:proceed|run|make|grant|continue)"
r"|yes, allow once|yes, and don't ask|allow (?:once|for this session)|do you trust the files|yes, i trust"
r"|enter to (?:select|confirm|approve)|plan needs changes|\[y/n\]|\(y/n\)",
re.I,
)
# helper processes of an agent, not an agent session: Claude's browser bridge, Electron children
HELPER_ARGS = ("--chrome-native-host", "--type=")
INTERPRETERS = {"node", "bun", "deno", "python", "python3", "pythonw", "py"}
_EXE = {name: key for key, a in AGENTS.items() for name in a[1]}
_seen: dict[int, tuple[str, str | None] | None] = {} # pid -> (agent, model); a cmdline never changes
MODEL_FLAG = re.compile(r"^--?(?:m|model)(?:=(.+))?$")
def _flag_model(cmd: list[str]) -> str | None:
for i, arg in enumerate(cmd):
if m := MODEL_FLAG.match(arg):
return m.group(1) or (cmd[i + 1] if i + 1 < len(cmd) else None)
return None
def _exe_parts(p: psutil.Process) -> tuple[str, ...]:
try:
return tuple(x.lower() for x in Path(p.exe()).parts)
except (psutil.Error, OSError):
return ("cli",) # unknown: assume the CLI
def identify(p: psutil.Process) -> tuple[str, str | None] | None:
"""(agent key, model from --model or its config) when `p` is an agent CLI, else None."""
if p.pid in _seen:
return _seen[p.pid]
name = p.name().lower().removesuffix(".exe")
key, cmd = _EXE.get(name), None
if key or name in INTERPRETERS or name.startswith("python"): # python3.12 on Linux
try:
cmd = p.cmdline()
except psutil.Error:
cmd = []
if any(a.startswith(HELPER_ARGS) for a in cmd):
key, cmd = None, []
elif key == "devin" and "cli" not in (_exe_parts(p)):
key = "devin_desktop" # ponytail: path heuristic, unverified (no desktop install to test on)
elif not key:
joined = " ".join(cmd).replace("\\", "/")
key = next((k for k, a in AGENTS.items() if any(m in joined for m in a[2])), None)
# `python -m aider` / `node .../bin/codex`: last path part of the script
script = next((i for i, a in enumerate(cmd) if i and not a.startswith("-")), None)
if not key and script:
key = _EXE.get(Path(cmd[script]).stem.lower())
cmd = cmd[script + 1:] if script else [] # `python -m aider`: -m is not a model flag
hit = (key, _flag_model(cmd or []) or _env_model(p, key) or _resumed_model(key, cmd or []) or default_model(key)) if key else None
_seen[p.pid] = hit
return hit
MODEL_ENV = {"claude": "ANTHROPIC_MODEL", "devin": "DEVIN_MODEL", "gemini": "GEMINI_MODEL"}
def _env_model(p: psutil.Process, key: str) -> str | None:
"""The model an env var picks for this process (e.g. `$env:DEVIN_MODEL = 'swe-1.6'; devin`)."""
if var := MODEL_ENV.get(key):
try:
return p.environ().get(var) or None
except psutil.Error:
pass
return None
def _varint(b: bytes, i: int) -> tuple[int, int]:
n = shift = 0
while True:
c = b[i]
i += 1
n |= (c & 0x7F) << shift
shift += 7
if c < 0x80:
return n, i
def _resumed_model(key: str, cmd: list[str]) -> str | None:
"""`devin -r <id>` keeps the model of that session, stored in Devin's session store."""
if key != "devin":
return None
sid = next((cmd[i + 1] for i, a in enumerate(cmd[:-1]) if a in ("-r", "--resume")), None)
return next((d["model"] for d in devin_sessions(500) if d["id"] == sid), None) if sid else None
def _proto(b: bytes):
"""(field, value) pairs of a protobuf message; length-delimited values stay bytes."""
i = 0
while i < len(b):
key, i = _varint(b, i)
wire = key & 7
if wire == 0:
v, i = _varint(b, i)
elif wire == 2:
n, i = _varint(b, i)
v, i = b[i:i + n], i + n
elif wire in (1, 5):
n = 8 if wire == 1 else 4
v, i = b[i:i + n], i + n
else:
raise ValueError(f"wire type {wire}")
yield key >> 3, v
def devin_models(cache: Path) -> list[str]:
"""Names `devin --model` accepts, from the model_configs.bin Devin keeps next to its bin/ folder.
Each entry (field 1) has a display name (1, "Claude Opus 4.7 Medium") and a base model (30.1,
"Claude Opus 4.7"); the CLI takes the base name lowercased and dashed: claude-opus-4.7.
ponytail: reverse-engineered format; on a parse error the page just shows no models."""
names = []
for field, entry in _proto(cache.read_bytes()):
if field != 1:
continue
parts = dict(_proto(entry))
base = dict(_proto(parts[30])).get(1) if 30 in parts else None
name = (base or parts.get(1) or b"").decode("utf-8").strip().lower().replace(" ", "-")
if name and name not in names:
names.append(name)
return sorted(names)
def _devin_windows() -> dict[str, int]:
"""Devin model id (sessions.model, e.g. swe-1-6) -> context window, from field 23.4 of each entry."""
exe = shutil.which("devin")
cache = Path(exe).resolve().parent.parent / "model_configs.bin" if exe else None
if not cache or not cache.exists():
return {}
key = cache.stat().st_mtime
if _cache.get("devin_windows", (None,))[0] != key:
out = {}
for field, entry in _proto(cache.read_bytes()):
parts = dict(_proto(entry)) if field == 1 else {}
if 22 in parts and 23 in parts and isinstance(size := dict(_proto(parts[23])).get(4), int):
out[parts[22].decode("utf-8", "replace")] = size
_cache["devin_windows"] = (key, out)
return _cache["devin_windows"][1]
_cache: dict[str, tuple] = {}
_files: dict[tuple[int, str], Path] = {} # (agent pid, agent) -> the session log it writes
def _tail_lines(path: Path, size: int = 512 * 1024) -> list[str]:
"""Last lines of a JSONL log, newest first (the first may be cut, so JSON errors are skipped)."""
with path.open("rb") as f:
f.seek(max(0, path.stat().st_size - size))
return f.read().decode("utf-8", "replace").splitlines()[::-1]
# Claude logs no window size. 1M models per Devin's model catalog (Opus 4.7/4.8, every 5.x); the rest 200k.
# ponytail: static rule, marked estimated in the UI; update when new models ship
CLAUDE_1M = re.compile(r"claude-(?:opus-4-[78]|(?:opus|sonnet|fable)-5)|claude-5")
def _claude_context(p: psutil.Process) -> dict | None:
home = Path.home() / ".claude"
info = _json(home / "sessions" / f"{p.pid}.json")
log = _files.get((p.pid, "claude"))
if not log or log.stem != info.get("sessionId"):
log = next((home / "projects").glob(f"*/{info['sessionId']}.jsonl"), None)
if not log:
return {"state": info.get("status")}
_files[(p.pid, "claude")] = log
for line in _tail_lines(log):
try:
d = json.loads(line)
except ValueError:
continue
u = (d.get("message") or {}).get("usage") if d.get("type") == "assistant" and not d.get("isSidechain") else None
if u:
used = sum(u.get(k) or 0 for k in ("input_tokens", "cache_creation_input_tokens", "cache_read_input_tokens", "output_tokens"))
model = d["message"].get("model") or ""
window = 1_000_000 if "[1m]" in model or CLAUDE_1M.search(model) or used > 200_000 else 200_000
return {"used": used, "window": window, "estimated": True, "state": info.get("status"), "model": model}
return {"state": info.get("status")}
def _codex_log(p: psutil.Process) -> Path | None:
log = _files.get((p.pid, "codex"))
if not log:
# the newest rollout started in this folder after the process did
start, cwd = p.create_time() - 5, os.path.normcase(p.cwd())
logs = sorted((Path.home() / ".codex" / "sessions").glob("*/*/*/rollout-*.jsonl"), key=lambda f: f.stat().st_mtime, reverse=True)
for f in logs[:30]:
if f.stat().st_mtime < start:
break
with f.open(encoding="utf-8", errors="replace") as fh:
meta = json.loads(fh.readline() or "{}").get("payload") or {}
if os.path.normcase(meta.get("cwd") or "") == cwd:
log = _files[(p.pid, "codex")] = f
break
return log
def _codex_context(p: psutil.Process) -> dict | None:
log = _codex_log(p)
if not log:
return None
window = used = None
for line in _tail_lines(log): # newest first: the latest usage and the latest window, whichever comes first
try:
payload = json.loads(line).get("payload") or {}
except ValueError:
continue
info = payload.get("info") or {}
window = window or payload.get("model_context_window") or info.get("model_context_window")
if used is None and payload.get("type") == "token_count" and info.get("last_token_usage"):
used = info["last_token_usage"].get("total_tokens") or 0
if window and used is not None:
break
return {"used": used or 0, "window": window} if window else None
def _devin_context(p: psutil.Process) -> dict | None:
db = _devin_db()
if not db:
return None
cmd = p.cmdline()
sid = next((cmd[i + 1] for i, a in enumerate(cmd[:-1]) if a in ("-r", "--resume")), None)
with closing(sqlite3.connect(f"file:{db.as_posix()}?mode=ro", uri=True, timeout=1)) as conn:
if sid:
row = conn.execute("SELECT id, model FROM sessions WHERE id = ?", (sid,)).fetchone()
else: # the newest session in this folder started after the process did
row = conn.execute(
"SELECT id, model FROM sessions WHERE working_directory = ? AND created_at >= ? ORDER BY created_at DESC LIMIT 1",
(p.cwd(), int(p.create_time()) - 5),
).fetchone()
if not row:
return None
msgs = conn.execute(
"SELECT chat_message FROM message_nodes WHERE session_id = ? ORDER BY row_id DESC LIMIT 50", (row[0],)
).fetchall()
window = _devin_windows().get(row[1])
for (msg,) in msgs:
m = (json.loads(msg).get("metadata") or {}).get("metrics") or {}
if m.get("input_tokens") is not None:
used = sum(m.get(k) or 0 for k in ("input_tokens", "cache_read_tokens", "cache_creation_tokens", "output_tokens"))
return {"used": used, "window": window, "model": row[1]} if window else {"used": used, "model": row[1]}
return {"used": 0, "window": window} if window else None
CONTEXT = {"claude": _claude_context, "codex": _codex_context, "devin": _devin_context}
def context(pid: int, key: str) -> dict | None:
"""How full the agent's context window is: {used, window, estimated?, state?} from its own logs."""
if key not in CONTEXT:
return None
try:
return CONTEXT[key](psutil.Process(pid))
except (psutil.Error, OSError, ValueError, KeyError, TypeError, sqlite3.Error):
return None
# Codex rollout events (as dotpals reads them) -> lifecycle state; the newest one decides
CODEX_EVENTS = {"task_started": "working", "user_message": "working", "function_call": "working", "custom_tool_call": "working",
"function_call_output": "working", "custom_tool_call_output": "working", "task_complete": "done"}
def _codex_native(p: psutil.Process) -> dict | None:
log = _codex_log(p)
if not log:
return None
with log.open(encoding="utf-8", errors="replace") as fh:
meta = json.loads(fh.readline() or "{}").get("payload") or {}
native = meta.get("id") or meta.get("session_id")
for line in _tail_lines(log, 128 * 1024):
try:
d = json.loads(line)
except ValueError:
continue
state = CODEX_EVENTS.get((d.get("payload") or {}).get("type"))
if state:
try:
at = datetime.fromisoformat(str(d.get("timestamp")).replace("Z", "+00:00")).timestamp()
except ValueError:
at = log.stat().st_mtime
out = {"state": state, "age": max(0.0, time.time() - at)}
if isinstance(native, str) and native:
out |= {"session_id": native, "resume_argv": ["codex", "resume", native]}
return out
return None
def _claude_native(p: psutil.Process) -> dict | None:
try:
info = _json(Path.home() / ".claude" / "sessions" / f"{p.pid}.json")
except (OSError, ValueError):
return None
out = {"state": "working" if info.get("status") == "busy" else None, "age": 0.0} # "idle" may be a prompt: screen decides
if isinstance(info.get("sessionId"), str) and info["sessionId"]:
out |= {"session_id": info["sessionId"], "resume_argv": ["claude", "--resume", info["sessionId"]]}
return out
def _devin_native(p: psutil.Process) -> dict | None:
"""Devin's session id from its sessions.db (the `-r` id, else the newest session in this folder since the process
started), so it can be resumed even when a hook payload carries no id. State comes from hooks or the screen."""
db = _devin_db()
if not db:
return None
cmd = p.cmdline()
sid = next((cmd[i + 1] for i, a in enumerate(cmd[:-1]) if a in ("-r", "--resume")), None)
if not sid:
with closing(sqlite3.connect(f"file:{db.as_posix()}?mode=ro", uri=True, timeout=1)) as conn:
row = conn.execute("SELECT id FROM sessions WHERE working_directory = ? AND created_at >= ? ORDER BY created_at DESC LIMIT 1",
(p.cwd(), int(p.create_time()) - 5)).fetchone()
sid = row[0] if row else None
if not isinstance(sid, str) or not sid or sid.startswith("-"):
return None
return {"state": None, "age": 0.0, "session_id": sid, "resume_argv": ["devin", "--resume", sid]}
NATIVE = {"codex": _codex_native, "claude": _claude_native, "devin": _devin_native}
_ppids: dict = {"at": 0.0, "kids": {}}
def descendants(pid: int) -> list[int]:
"""Every pid under `pid`, outermost first. psutil's children() takes a full process snapshot per call
(on Windows: per terminal, several times per poll); this shares one snapshot for a second."""
now = time.monotonic()
if now - _ppids["at"] > 1:
kids: dict[int, list[int]] = {}
for child, parent in psutil._ppid_map().items(): # ponytail: private psutil API; children() if it ever goes
if child != parent:
kids.setdefault(parent, []).append(child)
_ppids.update(at=now, kids=kids)
out = [pid]
for p in out: # breadth first: callers pick the outermost agent
out += [c for c in _ppids["kids"].get(p, ()) if c not in out]
return out[1:]
def native(shells: dict[str, int]) -> dict[str, dict]:
"""Lifecycle the agents write in their own logs (no hook needed): session id -> {state, age, session_id?, resume_argv?}."""
out = {}
for sid, pid in shells.items():
for k in descendants(pid):
try:
k = psutil.Process(k)
hit = identify(k)
if hit and hit[0] in NATIVE:
if found := NATIVE[hit[0]](k):
out[sid] = found
break
except (psutil.Error, OSError, ValueError, sqlite3.Error):
continue
return out
def running(shells: dict[str, int]) -> dict[str, tuple[str, str | None]]:
"""session id -> (agent, model) for terminals (shell pid) with an agent running under them."""
out = {}
for sid, pid in shells.items():
for k in descendants(pid):
try:
k = psutil.Process(k)
if hit := identify(k):
out[sid] = hit
break
except psutil.Error:
continue
return out
def outside(inside: set[int]) -> list[dict]:
"""Agents running on this machine but not in a shelldeck terminal (desktop apps, other terminals).
inside: pids already under a shelldeck shell. Only the outermost agent process counts."""
found = {}
for p in psutil.process_iter():
if p.pid in inside:
continue
try:
if hit := identify(p):
found[p.pid] = (p, hit)
except psutil.Error:
continue
out = []
for pid, (p, (key, model)) in found.items():
try:
parent = p.parent()
if parent and parent.pid in found:
continue
cwd = p.cwd()
host = parent.name().removesuffix(".exe") if parent else ""
except psutil.Error:
cwd, host = "", ""
out.append({"pid": pid, "agent": key, "label": AGENTS[key][0], "model": model, "cwd": cwd, "host": host})
return sorted(out, key=lambda a: (a["label"], a["pid"]))
def _devin_db() -> Path | None:
"""Devin keeps every session (CLI and hosted by apps over ACP) in one sessions.db.
ponytail: Windows path verified; the Linux/macOS locations are guesses."""
roots = [Path(os.environ.get("APPDATA", "")), Path.home() / ".local" / "share", Path.home() / ".config"]
return next((f for r in roots if (f := r / "devin" / "cli" / "sessions.db").exists()), None)
def devin_sessions(limit: int = 50) -> list[dict]:
"""Recent Devin sessions, newest first (read-only; Devin may be writing)."""
db = _devin_db()
if not db:
return []
try:
with closing(sqlite3.connect(f"file:{db.as_posix()}?mode=ro", uri=True, timeout=1)) as conn:
rows = conn.execute(
"SELECT id, title, working_directory, model, backend_type, created_at, last_activity_at FROM sessions "
"WHERE hidden = 0 ORDER BY last_activity_at DESC LIMIT ?", (limit,)
).fetchall()
except sqlite3.Error:
return []
keys = ("id", "title", "cwd", "model", "backend", "created_at", "last_activity_at")
return [dict(zip(keys, r, strict=True)) for r in rows]
def forget(alive: set[int]) -> None:
for pid in [p for p in _seen if p not in alive]:
del _seen[pid]
for k in [k for k in _files if k[0] not in alive]:
del _files[k]
def default_model(key: str) -> str | None:
"""The model an agent's own config picks when no --model flag is given."""
home = Path.home()
try:
if key == "claude":
return _json(home / ".claude" / "settings.json").get("model")
if key == "codex":
return tomllib.loads((home / ".codex" / "config.toml").read_text(encoding="utf-8")).get("model")
if key == "gemini":
return (_json(home / ".gemini" / "settings.json").get("model") or {}).get("name")
except (OSError, ValueError, AttributeError):
pass
return None
def _json(path: Path):
return json.loads(path.read_text(encoding="utf-8"))
def models(key: str) -> list[str]:
"""Models an agent offers: its own cache when it keeps one, else the static fallback."""
home = Path.home()
try:
if key == "codex": # refreshed by codex itself from OpenAI
return [m["slug"] for m in _json(home / ".codex" / "models_cache.json")["models"] if m.get("visibility") == "list"]
if key == "opencode": # the models.dev catalog; only opencode's own provider and signed-in ones
catalog, auth = _json(home / ".cache" / "opencode" / "models.json"), home / ".local" / "share" / "opencode" / "auth.json"
providers = ["opencode", *(_json(auth) if auth.exists() else {})]
return [f"{p}/{m}" for p in providers for m in catalog.get(p, {}).get("models", {})]
if key == "devin" and (exe := shutil.which("devin")): # <install>/bin/devin(.exe) -> <install>/model_configs.bin
return devin_models(Path(exe).resolve().parent.parent / "model_configs.bin")
except (OSError, ValueError, KeyError, TypeError, IndexError, UnicodeDecodeError):
pass
return list(AGENTS[key][3])
def catalog() -> list[dict]:
"""Every known agent, whether its CLI is on PATH, and the models it offers."""
out = []
for key, (label, exes, _, _) in AGENTS.items():
if not exes:
continue
path = next((p for e in exes if (p := shutil.which(e))), None)
out.append({"key": key, "label": label, "command": exes[0], "installed": bool(path), "path": path,
"models": models(key) if path else list(AGENTS[key][3]), "default_model": default_model(key) if path else None})
return out
if __name__ == "__main__":
assert _flag_model(["claude", "--model", "opus"]) == "opus"
assert _flag_model(["codex", "-m", "gpt-5-codex"]) == "gpt-5-codex"
assert _flag_model(["x", "--model=sonnet"]) == "sonnet"
assert _flag_model(["x", "--mode", "y"]) is None
# one Devin entry: display name (1) + base model (30.1) -> the name `devin --model` takes
base = b"\x0a\x0fClaude Opus 4.7"
entry = b"\x0a\x16Claude Opus 4.7 Medium" + b"\x18\x01" + b"\xf2\x01" + bytes([len(base)]) + base
import tempfile
with tempfile.TemporaryDirectory() as d:
(Path(d) / "m.bin").write_bytes(b"\x0a" + bytes([len(entry)]) + entry + b"\x0a\x0b\x0a\x09Kimi K2.6")
assert devin_models(Path(d) / "m.bin") == ["claude-opus-4.7", "kimi-k2.6"]
print("ok")