snow-agent 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- snow_agent/__init__.py +3 -0
- snow_agent/__main__.py +8 -0
- snow_agent/adapters/__init__.py +1 -0
- snow_agent/adapters/_process.py +185 -0
- snow_agent/adapters/antigravity.py +162 -0
- snow_agent/adapters/claude_code.py +171 -0
- snow_agent/adapters/codex.py +284 -0
- snow_agent/adapters/opencode.py +209 -0
- snow_agent/adapters/zcode.py +374 -0
- snow_agent/cli/__init__.py +5 -0
- snow_agent/cli/app.py +578 -0
- snow_agent/cli/parser.py +202 -0
- snow_agent/cli/render.py +70 -0
- snow_agent/core/__init__.py +6 -0
- snow_agent/core/agent_adapters.py +239 -0
- snow_agent/core/capabilities.py +995 -0
- snow_agent/core/config.py +432 -0
- snow_agent/core/controlproto.py +120 -0
- snow_agent/core/delegation.py +650 -0
- snow_agent/core/errors.py +29 -0
- snow_agent/core/execution.py +45 -0
- snow_agent/core/help.py +182 -0
- snow_agent/core/local_exec.py +184 -0
- snow_agent/core/logutil.py +48 -0
- snow_agent/core/models.py +231 -0
- snow_agent/core/probes.py +239 -0
- snow_agent/core/services.py +219 -0
- snow_agent/core/shellencode.py +35 -0
- snow_agent/core/ssh_exec.py +369 -0
- snow_agent/core/task_contract.py +503 -0
- snow_agent/core/task_git.py +526 -0
- snow_agent/core/task_ipc.py +435 -0
- snow_agent/core/task_store.py +775 -0
- snow_agent/core/task_transport.py +202 -0
- snow_agent/core/util.py +98 -0
- snow_agent/core/wrapper.py +75 -0
- snow_agent/entrypoints/__init__.py +1 -0
- snow_agent/entrypoints/cli.py +20 -0
- snow_agent/entrypoints/tui.py +19 -0
- snow_agent/runtime/__init__.py +1 -0
- snow_agent/runtime/daemon.py +292 -0
- snow_agent/runtime/worker.py +806 -0
- snow_agent-0.1.0.dist-info/METADATA +114 -0
- snow_agent-0.1.0.dist-info/RECORD +48 -0
- snow_agent-0.1.0.dist-info/WHEEL +5 -0
- snow_agent-0.1.0.dist-info/entry_points.txt +2 -0
- snow_agent-0.1.0.dist-info/licenses/LICENSE +21 -0
- snow_agent-0.1.0.dist-info/top_level.txt +1 -0
snow_agent/__init__.py
ADDED
snow_agent/__main__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Agent adapters: one module per delivered delegation target."""
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"""Shared process plumbing for JSONL-emitting agent CLIs.
|
|
2
|
+
|
|
3
|
+
The adapters for OpenCode, Claude Code and Antigravity drive plain CLI
|
|
4
|
+
processes whose stdout is one JSON object per line; the turn ends when the
|
|
5
|
+
process exits (their exit codes and envelopes never prove business success —
|
|
6
|
+
that decision stays in the workflow core). ``JsonlProcess`` owns the process
|
|
7
|
+
group started with ``start_new_session``, feeds the prompt (stdin or argv per
|
|
8
|
+
product contract) and leaves per-product event handling to subclasses.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import json
|
|
14
|
+
import os
|
|
15
|
+
import subprocess
|
|
16
|
+
import time
|
|
17
|
+
from threading import Thread
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
from ..core.agent_adapters import AgentProcess, AgentStartSpec
|
|
21
|
+
from ..core.errors import SnowError
|
|
22
|
+
|
|
23
|
+
_STDERR_TAIL_BYTES = 8192
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class JsonlProcess(AgentProcess):
|
|
27
|
+
"""One CLI agent round: process group + stdout/stderr reader threads.
|
|
28
|
+
|
|
29
|
+
``stop_sequence`` is the escalation ladder for :meth:`stop` (Antigravity
|
|
30
|
+
needs SIGINT first; the others stop on SIGTERM like Codex).
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
proc: subprocess.Popen[bytes],
|
|
36
|
+
argv_label: str,
|
|
37
|
+
stop_sequence: tuple[tuple[int, float], ...],
|
|
38
|
+
) -> None:
|
|
39
|
+
super().__init__()
|
|
40
|
+
self._proc = proc
|
|
41
|
+
self._argv_label = argv_label
|
|
42
|
+
self._stop_sequence = stop_sequence
|
|
43
|
+
self.stderr_tail = ""
|
|
44
|
+
self.exit_code: int | None = None
|
|
45
|
+
self._threads = [
|
|
46
|
+
Thread(target=self._read_stdout, daemon=True),
|
|
47
|
+
Thread(target=self._read_stderr, daemon=True),
|
|
48
|
+
]
|
|
49
|
+
for thread in self._threads:
|
|
50
|
+
thread.start()
|
|
51
|
+
|
|
52
|
+
# ---- per-product event handling ------------------------------------------
|
|
53
|
+
|
|
54
|
+
def _handle_event(self, event: dict[str, Any]) -> None:
|
|
55
|
+
"""Inspect one parsed stdout line; set ``session_id`` / queue events."""
|
|
56
|
+
|
|
57
|
+
# ---- process lifecycle -----------------------------------------------------
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def pid(self) -> int | None:
|
|
61
|
+
return self._proc.pid
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def pgid(self) -> int | None:
|
|
65
|
+
return self._proc.pid if self._proc.pid else None
|
|
66
|
+
|
|
67
|
+
def _read_stdout(self) -> None:
|
|
68
|
+
assert self._proc.stdout is not None
|
|
69
|
+
for line in self._proc.stdout:
|
|
70
|
+
text = line.decode("utf-8", errors="replace").strip()
|
|
71
|
+
if not text:
|
|
72
|
+
continue
|
|
73
|
+
try:
|
|
74
|
+
event = json.loads(text)
|
|
75
|
+
except json.JSONDecodeError:
|
|
76
|
+
continue
|
|
77
|
+
if isinstance(event, dict):
|
|
78
|
+
self._handle_event(event)
|
|
79
|
+
|
|
80
|
+
def _read_stderr(self) -> None:
|
|
81
|
+
assert self._proc.stderr is not None
|
|
82
|
+
data = self._proc.stderr.read() or b""
|
|
83
|
+
try:
|
|
84
|
+
self.stderr_tail = data[-_STDERR_TAIL_BYTES:].decode("utf-8", errors="replace")
|
|
85
|
+
except Exception:
|
|
86
|
+
self.stderr_tail = ""
|
|
87
|
+
|
|
88
|
+
def poll(self) -> int | None:
|
|
89
|
+
return self._proc.poll()
|
|
90
|
+
|
|
91
|
+
def wait(self, timeout: float | None = None) -> int | None:
|
|
92
|
+
try:
|
|
93
|
+
self.exit_code = self._proc.wait(timeout=timeout)
|
|
94
|
+
except subprocess.TimeoutExpired:
|
|
95
|
+
return None
|
|
96
|
+
for thread in self._threads:
|
|
97
|
+
thread.join(timeout=5)
|
|
98
|
+
return self.exit_code
|
|
99
|
+
|
|
100
|
+
def group_alive(self) -> bool:
|
|
101
|
+
"""True while any member of the agent process group exists."""
|
|
102
|
+
pgid = self.pgid
|
|
103
|
+
if pgid is None:
|
|
104
|
+
return False
|
|
105
|
+
try:
|
|
106
|
+
os.killpg(pgid, 0)
|
|
107
|
+
except ProcessLookupError:
|
|
108
|
+
return False
|
|
109
|
+
except PermissionError:
|
|
110
|
+
return True
|
|
111
|
+
return True
|
|
112
|
+
|
|
113
|
+
def stop(self, *, grace_s: float = 10.0) -> None:
|
|
114
|
+
"""Escalate through ``stop_sequence`` to the group; verify it is gone."""
|
|
115
|
+
pgid = self.pgid
|
|
116
|
+
if pgid is None:
|
|
117
|
+
return
|
|
118
|
+
for sig, wait_s in self._stop_sequence:
|
|
119
|
+
try:
|
|
120
|
+
os.killpg(pgid, sig)
|
|
121
|
+
except (ProcessLookupError, PermissionError):
|
|
122
|
+
break
|
|
123
|
+
deadline = time.monotonic() + wait_s
|
|
124
|
+
while time.monotonic() < deadline:
|
|
125
|
+
if self._proc.poll() is not None and not self.group_alive():
|
|
126
|
+
self.exit_code = self._proc.returncode
|
|
127
|
+
return
|
|
128
|
+
time.sleep(0.2)
|
|
129
|
+
try:
|
|
130
|
+
self.exit_code = self._proc.wait(timeout=5)
|
|
131
|
+
except subprocess.TimeoutExpired:
|
|
132
|
+
pass
|
|
133
|
+
|
|
134
|
+
def join(self, timeout: float = 5.0) -> None:
|
|
135
|
+
for thread in self._threads:
|
|
136
|
+
thread.join(timeout=timeout)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def spawn_jsonl_agent(
|
|
140
|
+
argv: list[str],
|
|
141
|
+
spec: AgentStartSpec,
|
|
142
|
+
*,
|
|
143
|
+
product: str,
|
|
144
|
+
stop_sequence: tuple[tuple[int, float], ...],
|
|
145
|
+
prompt_stdin: bool = True,
|
|
146
|
+
process_cls: type[JsonlProcess] | None = None,
|
|
147
|
+
) -> JsonlProcess:
|
|
148
|
+
"""Spawn one agent round and return its :class:`JsonlProcess`.
|
|
149
|
+
|
|
150
|
+
``prompt_stdin`` selects the product's prompt channel: OpenCode and
|
|
151
|
+
Claude Code read the prompt from stdin (EOF right after); Antigravity's
|
|
152
|
+
``--print`` takes the prompt as an argv value and does not read stdin.
|
|
153
|
+
"""
|
|
154
|
+
env = {**os.environ, **spec.env}
|
|
155
|
+
stdin_target = subprocess.PIPE if prompt_stdin else subprocess.DEVNULL
|
|
156
|
+
try:
|
|
157
|
+
proc = subprocess.Popen(
|
|
158
|
+
argv,
|
|
159
|
+
cwd=str(spec.cwd),
|
|
160
|
+
stdin=stdin_target,
|
|
161
|
+
stdout=subprocess.PIPE,
|
|
162
|
+
stderr=subprocess.PIPE,
|
|
163
|
+
start_new_session=True,
|
|
164
|
+
env=env,
|
|
165
|
+
)
|
|
166
|
+
except OSError as err:
|
|
167
|
+
raise SnowError(
|
|
168
|
+
"agent_start_failed",
|
|
169
|
+
f"Starting {product} failed: {err}",
|
|
170
|
+
next_steps=(f"Check the {product} installation on this device.",),
|
|
171
|
+
) from err
|
|
172
|
+
if prompt_stdin:
|
|
173
|
+
assert proc.stdin is not None
|
|
174
|
+
try:
|
|
175
|
+
proc.stdin.write(spec.prompt.encode("utf-8"))
|
|
176
|
+
proc.stdin.flush()
|
|
177
|
+
except OSError:
|
|
178
|
+
pass # agent died immediately; the observer path reports it
|
|
179
|
+
finally:
|
|
180
|
+
try:
|
|
181
|
+
proc.stdin.close()
|
|
182
|
+
except OSError:
|
|
183
|
+
pass
|
|
184
|
+
cls = process_cls or JsonlProcess
|
|
185
|
+
return cls(proc, argv_label=argv[0], stop_sequence=stop_sequence)
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""Google Antigravity CLI (``agy``) adapter for delegated execution.
|
|
2
|
+
|
|
3
|
+
Launch contract (research-verified on 1.5.3, re-smoked on 1.5.9):
|
|
4
|
+
``agy --dangerously-skip-permissions --output-format stream-json -p <prompt>``
|
|
5
|
+
with the prompt as an argv value — ``--print`` never reads plain stdin. The
|
|
6
|
+
first ``init`` event carries ``conversation_id`` at the top level (1.5.3 also
|
|
7
|
+
accepted ``init.conversation_id``; 1.5.9 only emits the top-level key), and
|
|
8
|
+
resume appends ``--conversation <id>``. Unattended authorization is only
|
|
9
|
+
verified to work with ``--dangerously-skip-permissions`` (fine-grained allow
|
|
10
|
+
rules were measured broken in headless 1.5.3), so tasks must target a
|
|
11
|
+
dedicated sandbox repository — that boundary is operational, documented in
|
|
12
|
+
the CLI usage guide, and enforced nowhere in code. Cancellation sends SIGINT
|
|
13
|
+
first (agy reports ``finished_reason: interrupted``), then escalates; the
|
|
14
|
+
signal must reach the real agy PID/group, never a ``timeout`` wrapper.
|
|
15
|
+
``status=SUCCESS`` only means the turn ended; business success is decided by
|
|
16
|
+
the workflow core.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import re
|
|
22
|
+
import signal
|
|
23
|
+
|
|
24
|
+
from ..core.agent_adapters import (
|
|
25
|
+
AgentStartSpec,
|
|
26
|
+
ProbeResult,
|
|
27
|
+
probe_binary,
|
|
28
|
+
run_probe,
|
|
29
|
+
)
|
|
30
|
+
from ..core.errors import SnowError
|
|
31
|
+
from ._process import JsonlProcess, spawn_jsonl_agent
|
|
32
|
+
|
|
33
|
+
AGENT_NAME = "antigravity"
|
|
34
|
+
MINIMUM_VERSION = (1, 5, 3)
|
|
35
|
+
STOP_SEQUENCE = ((signal.SIGINT, 10.0), (signal.SIGTERM, 5.0), (signal.SIGKILL, 5.0))
|
|
36
|
+
_BASE_ARGV = ("--dangerously-skip-permissions", "--output-format", "stream-json")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _version_supported(version: str) -> tuple[bool, str]:
|
|
40
|
+
match = re.search(r"(\d+)\.(\d+)\.(\d+)", version)
|
|
41
|
+
if not match:
|
|
42
|
+
return False, f"unrecognized version output: {version.strip()!r}"
|
|
43
|
+
triple = tuple(int(part) for part in match.groups())
|
|
44
|
+
if triple >= MINIMUM_VERSION:
|
|
45
|
+
return True, ""
|
|
46
|
+
dots = ".".join(str(part) for part in triple)
|
|
47
|
+
return (
|
|
48
|
+
False,
|
|
49
|
+
f"version {dots} is below the required minimum 1.5.3 (1.1.x writes files"
|
|
50
|
+
" outside the working directory)",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def probe() -> ProbeResult:
|
|
55
|
+
"""Check binary, minimum version and cached credentials (``agy models``)."""
|
|
56
|
+
result = ProbeResult(agent=AGENT_NAME, available=True)
|
|
57
|
+
binary, reason = probe_binary("agy")
|
|
58
|
+
if binary is None:
|
|
59
|
+
result.available = False
|
|
60
|
+
result.unavailable_reason = reason
|
|
61
|
+
result.checks.append({"name": "binary", "result": "fail", "detail": reason})
|
|
62
|
+
return result
|
|
63
|
+
result.checks.append({"name": "binary", "result": "pass", "detail": binary})
|
|
64
|
+
try:
|
|
65
|
+
version = run_probe([binary, "--version"]).stdout
|
|
66
|
+
except Exception as err: # noqa: BLE001 - probe reports, never raises
|
|
67
|
+
result.available = False
|
|
68
|
+
reason = f"agy --version failed: {err}"
|
|
69
|
+
result.unavailable_reason = reason
|
|
70
|
+
result.checks.append({"name": "version", "result": "fail", "detail": reason})
|
|
71
|
+
return result
|
|
72
|
+
ok, detail = _version_supported(version)
|
|
73
|
+
result.checks.append(
|
|
74
|
+
{"name": "version", "result": "pass" if ok else "fail", "detail": detail or version.strip()}
|
|
75
|
+
)
|
|
76
|
+
if not ok:
|
|
77
|
+
result.available = False
|
|
78
|
+
result.unavailable_reason = detail
|
|
79
|
+
return result
|
|
80
|
+
try:
|
|
81
|
+
models = run_probe([binary, "models"], timeout_s=60.0)
|
|
82
|
+
authenticated = models.returncode == 0 and bool(models.stdout.strip())
|
|
83
|
+
detail = "cached credentials available" if authenticated else (
|
|
84
|
+
(models.stderr or models.stdout).strip()[-400:] or "agy models returned no models"
|
|
85
|
+
)
|
|
86
|
+
except Exception as err: # noqa: BLE001 - probe reports, never raises
|
|
87
|
+
authenticated = False
|
|
88
|
+
detail = f"agy models failed: {err}"
|
|
89
|
+
result.checks.append(
|
|
90
|
+
{"name": "login", "result": "pass" if authenticated else "fail", "detail": detail}
|
|
91
|
+
)
|
|
92
|
+
if not authenticated:
|
|
93
|
+
result.available = False
|
|
94
|
+
result.unavailable_reason = (
|
|
95
|
+
"agy has no usable cached credentials; run 'agy' interactively once to"
|
|
96
|
+
" log in on this device (headless mode cannot log in)"
|
|
97
|
+
)
|
|
98
|
+
return result
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class AntigravityProcess(JsonlProcess):
|
|
102
|
+
"""One ``agy -p`` round: conversation_id from init/step/result events."""
|
|
103
|
+
|
|
104
|
+
def _handle_event(self, event: dict) -> None:
|
|
105
|
+
kind = event.get("event")
|
|
106
|
+
session_id = event.get("conversation_id")
|
|
107
|
+
if not isinstance(session_id, str) or not session_id:
|
|
108
|
+
payload = event.get(kind) if isinstance(event.get(kind), dict) else {}
|
|
109
|
+
session_id = payload.get("conversation_id") if isinstance(payload, dict) else None
|
|
110
|
+
if isinstance(session_id, str) and session_id:
|
|
111
|
+
self.session_id = session_id
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class AntigravityAdapter:
|
|
115
|
+
"""Launch and resume Antigravity CLI rounds with the verified argument set."""
|
|
116
|
+
|
|
117
|
+
def __init__(self, binary: str | None = None) -> None:
|
|
118
|
+
self._binary = binary
|
|
119
|
+
|
|
120
|
+
def _resolve_binary(self) -> str:
|
|
121
|
+
if self._binary:
|
|
122
|
+
return self._binary
|
|
123
|
+
binary, reason = probe_binary("agy")
|
|
124
|
+
if binary is None:
|
|
125
|
+
raise SnowError(
|
|
126
|
+
"agent_binary_missing",
|
|
127
|
+
f"Cannot start agy: {reason}.",
|
|
128
|
+
next_steps=("Install the Antigravity CLI or add it to PATH on this device.",),
|
|
129
|
+
)
|
|
130
|
+
return binary
|
|
131
|
+
|
|
132
|
+
def _spawn(self, argv: list[str], spec: AgentStartSpec) -> AntigravityProcess:
|
|
133
|
+
return spawn_jsonl_agent(
|
|
134
|
+
argv,
|
|
135
|
+
spec,
|
|
136
|
+
product="agy",
|
|
137
|
+
stop_sequence=STOP_SEQUENCE,
|
|
138
|
+
prompt_stdin=False,
|
|
139
|
+
process_cls=AntigravityProcess,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
def start(self, spec: AgentStartSpec) -> AntigravityProcess:
|
|
143
|
+
"""Fresh conversation: prompt as the ``-p`` argv value."""
|
|
144
|
+
binary = self._resolve_binary()
|
|
145
|
+
return self._spawn([binary, *_BASE_ARGV, "-p", spec.prompt], spec)
|
|
146
|
+
|
|
147
|
+
def resume(self, session_id: str, spec: AgentStartSpec) -> AntigravityProcess:
|
|
148
|
+
"""Continue the same conversation with ``--conversation <id>``."""
|
|
149
|
+
if not session_id:
|
|
150
|
+
raise SnowError(
|
|
151
|
+
"agent_session_missing",
|
|
152
|
+
"Cannot resume without a native conversation id.",
|
|
153
|
+
next_steps=("Let Snow start a fresh implement round instead.",),
|
|
154
|
+
)
|
|
155
|
+
binary = self._resolve_binary()
|
|
156
|
+
return self._spawn(
|
|
157
|
+
[binary, *_BASE_ARGV, "--conversation", session_id, "-p", spec.prompt],
|
|
158
|
+
spec,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
Adapter = AntigravityAdapter
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"""Claude Code CLI adapter for delegated execution.
|
|
2
|
+
|
|
3
|
+
Launch contract (research-verified on 2.1.204/2.1.269, re-smoked on 2.1.204):
|
|
4
|
+
``claude -p --output-format stream-json --verbose --permission-mode
|
|
5
|
+
bypassPermissions`` with the prompt on stdin; resume appends ``--resume
|
|
6
|
+
<session-id>`` and reuses the same session id. The first ``system/init``
|
|
7
|
+
event carries ``session_id``; the final ``result`` event ends the turn. The
|
|
8
|
+
envelope's ``subtype=success``/exit 0 never proves business success, and the
|
|
9
|
+
explicit permission mode is required because newer versions default ``-p`` to
|
|
10
|
+
manual approval (verified to deny writes deterministically). Authentication
|
|
11
|
+
on this device comes from the user's configured ``claude`` login state; the
|
|
12
|
+
probe reports whatever ``claude auth status`` says without touching tokens.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import json
|
|
18
|
+
import re
|
|
19
|
+
import signal
|
|
20
|
+
|
|
21
|
+
from ..core.agent_adapters import (
|
|
22
|
+
AgentStartSpec,
|
|
23
|
+
ProbeResult,
|
|
24
|
+
probe_binary,
|
|
25
|
+
run_probe,
|
|
26
|
+
)
|
|
27
|
+
from ..core.errors import SnowError
|
|
28
|
+
from ._process import JsonlProcess, spawn_jsonl_agent
|
|
29
|
+
|
|
30
|
+
AGENT_NAME = "claude-code"
|
|
31
|
+
ACCEPTED_VERSIONS = ((2, 1, 204), (2, 1, 269))
|
|
32
|
+
STOP_SEQUENCE = ((signal.SIGTERM, 10.0), (signal.SIGKILL, 5.0))
|
|
33
|
+
_BASE_ARGV = (
|
|
34
|
+
"-p",
|
|
35
|
+
"--output-format",
|
|
36
|
+
"stream-json",
|
|
37
|
+
"--verbose",
|
|
38
|
+
"--permission-mode",
|
|
39
|
+
"bypassPermissions",
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _version_supported(version: str) -> tuple[bool, str]:
|
|
44
|
+
match = re.search(r"(\d+)\.(\d+)\.(\d+)", version)
|
|
45
|
+
if not match:
|
|
46
|
+
return False, f"unrecognized version output: {version.strip()!r}"
|
|
47
|
+
triple = tuple(int(part) for part in match.groups())
|
|
48
|
+
if triple in ACCEPTED_VERSIONS:
|
|
49
|
+
return True, ""
|
|
50
|
+
dots = ".".join(str(part) for part in triple)
|
|
51
|
+
return (
|
|
52
|
+
False,
|
|
53
|
+
f"version {dots} is outside the verified baseline (2.1.204/2.1.269);"
|
|
54
|
+
" upgrade acceptance has not been run for it",
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def probe() -> ProbeResult:
|
|
59
|
+
"""Check binary, verified version baseline and the CLI login state."""
|
|
60
|
+
result = ProbeResult(agent=AGENT_NAME, available=True)
|
|
61
|
+
binary, reason = probe_binary("claude")
|
|
62
|
+
if binary is None:
|
|
63
|
+
result.available = False
|
|
64
|
+
result.unavailable_reason = reason
|
|
65
|
+
result.checks.append({"name": "binary", "result": "fail", "detail": reason})
|
|
66
|
+
return result
|
|
67
|
+
result.checks.append({"name": "binary", "result": "pass", "detail": binary})
|
|
68
|
+
try:
|
|
69
|
+
version = run_probe([binary, "--version"]).stdout
|
|
70
|
+
except Exception as err: # noqa: BLE001 - probe reports, never raises
|
|
71
|
+
result.available = False
|
|
72
|
+
reason = f"claude --version failed: {err}"
|
|
73
|
+
result.unavailable_reason = reason
|
|
74
|
+
result.checks.append({"name": "version", "result": "fail", "detail": reason})
|
|
75
|
+
return result
|
|
76
|
+
ok, detail = _version_supported(version)
|
|
77
|
+
result.checks.append(
|
|
78
|
+
{"name": "version", "result": "pass" if ok else "fail", "detail": detail or version.strip()}
|
|
79
|
+
)
|
|
80
|
+
if not ok:
|
|
81
|
+
result.available = False
|
|
82
|
+
result.unavailable_reason = detail
|
|
83
|
+
return result
|
|
84
|
+
try:
|
|
85
|
+
auth = run_probe([binary, "auth", "status"])
|
|
86
|
+
logged_in = False
|
|
87
|
+
detail = (auth.stdout or auth.stderr).strip()[-400:]
|
|
88
|
+
if auth.returncode == 0:
|
|
89
|
+
try:
|
|
90
|
+
status = json.loads(auth.stdout)
|
|
91
|
+
logged_in = bool(status.get("loggedIn"))
|
|
92
|
+
method = status.get("authMethod")
|
|
93
|
+
detail = f"loggedIn via {method}" if logged_in else "loggedIn is false"
|
|
94
|
+
except json.JSONDecodeError:
|
|
95
|
+
detail = "auth status did not return JSON"
|
|
96
|
+
except Exception as err: # noqa: BLE001 - probe reports, never raises
|
|
97
|
+
logged_in = False
|
|
98
|
+
detail = f"claude auth status failed: {err}"
|
|
99
|
+
result.checks.append(
|
|
100
|
+
{"name": "login", "result": "pass" if logged_in else "fail", "detail": detail}
|
|
101
|
+
)
|
|
102
|
+
if not logged_in:
|
|
103
|
+
result.available = False
|
|
104
|
+
result.unavailable_reason = (
|
|
105
|
+
"claude is not logged in on this device; run 'claude login' or configure"
|
|
106
|
+
" the account's authentication in ~/.claude/settings.json"
|
|
107
|
+
)
|
|
108
|
+
return result
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class ClaudeCodeProcess(JsonlProcess):
|
|
112
|
+
"""One ``claude -p`` round: session_id from the init/result events."""
|
|
113
|
+
|
|
114
|
+
def _handle_event(self, event: dict) -> None:
|
|
115
|
+
kind = event.get("type")
|
|
116
|
+
if kind == "system" and event.get("subtype") == "init":
|
|
117
|
+
session_id = event.get("session_id")
|
|
118
|
+
if self.session_id is None and isinstance(session_id, str) and session_id:
|
|
119
|
+
self.session_id = session_id
|
|
120
|
+
elif kind == "result":
|
|
121
|
+
session_id = event.get("session_id")
|
|
122
|
+
if isinstance(session_id, str) and session_id:
|
|
123
|
+
self.session_id = session_id
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class ClaudeCodeAdapter:
|
|
127
|
+
"""Launch and resume Claude Code CLI rounds with the verified argument set."""
|
|
128
|
+
|
|
129
|
+
def __init__(self, binary: str | None = None) -> None:
|
|
130
|
+
self._binary = binary
|
|
131
|
+
|
|
132
|
+
def _resolve_binary(self) -> str:
|
|
133
|
+
if self._binary:
|
|
134
|
+
return self._binary
|
|
135
|
+
binary, reason = probe_binary("claude")
|
|
136
|
+
if binary is None:
|
|
137
|
+
raise SnowError(
|
|
138
|
+
"agent_binary_missing",
|
|
139
|
+
f"Cannot start claude: {reason}.",
|
|
140
|
+
next_steps=("Install the Claude Code CLI or add it to PATH on this device.",),
|
|
141
|
+
)
|
|
142
|
+
return binary
|
|
143
|
+
|
|
144
|
+
def _spawn(self, argv: list[str], spec: AgentStartSpec) -> ClaudeCodeProcess:
|
|
145
|
+
return spawn_jsonl_agent(
|
|
146
|
+
argv,
|
|
147
|
+
spec,
|
|
148
|
+
product="claude",
|
|
149
|
+
stop_sequence=STOP_SEQUENCE,
|
|
150
|
+
prompt_stdin=True,
|
|
151
|
+
process_cls=ClaudeCodeProcess,
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
def start(self, spec: AgentStartSpec) -> ClaudeCodeProcess:
|
|
155
|
+
"""Fresh session: ``claude -p --output-format stream-json ...``."""
|
|
156
|
+
binary = self._resolve_binary()
|
|
157
|
+
return self._spawn([binary, *_BASE_ARGV], spec)
|
|
158
|
+
|
|
159
|
+
def resume(self, session_id: str, spec: AgentStartSpec) -> ClaudeCodeProcess:
|
|
160
|
+
"""Continue the same session; ``--resume`` keeps the session id."""
|
|
161
|
+
if not session_id:
|
|
162
|
+
raise SnowError(
|
|
163
|
+
"agent_session_missing",
|
|
164
|
+
"Cannot resume without a native session id.",
|
|
165
|
+
next_steps=("Let Snow start a fresh implement round instead.",),
|
|
166
|
+
)
|
|
167
|
+
binary = self._resolve_binary()
|
|
168
|
+
return self._spawn([binary, "-p", "--resume", session_id, *_BASE_ARGV], spec)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
Adapter = ClaudeCodeAdapter
|