Repository navigation
Expand file tree
/
Copy pathtest_runtime.py
More file actions
475 lines (408 loc) · 19.7 KB
/
Copy pathtest_runtime.py
File metadata and controls
475 lines (408 loc) · 19.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
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
"""Managed runtime and safe-dispatch regressions."""
from __future__ import annotations
import importlib.util
import json
import os
import re
import signal
import subprocess
import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "scripts"))
SPEC = importlib.util.spec_from_file_location("claude_seo_runtime", ROOT / "scripts/runtime.py")
assert SPEC and SPEC.loader
runtime = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(runtime)
def _fixture_root(tmp_path: Path) -> Path:
root = tmp_path / "plugin root"
(root / "scripts").mkdir(parents=True)
(root / "extensions" / "banana" / "scripts").mkdir(parents=True)
(root / ".claude-plugin").mkdir()
(root / "requirements.txt").write_text("requests>=2\n", encoding="utf-8")
(root / ".claude-plugin" / "plugin.json").write_text('{"version":"2.2.4"}\n', encoding="utf-8")
(root / "scripts" / "render_page.py").write_text("print('ok')\n", encoding="utf-8")
(root / "extensions" / "banana" / "scripts" / "generate.py").write_text("print('ok')\n", encoding="utf-8")
return root
def test_dispatch_is_allowlisted_and_rejects_traversal(tmp_path: Path) -> None:
root = _fixture_root(tmp_path)
assert runtime._resolve_script(root, "render_page.py", None).name == "render_page.py"
assert runtime._resolve_script(root, "generate.py", "banana").name == "generate.py"
with pytest.raises(ValueError):
runtime._resolve_script(root, "../render_page.py", None)
with pytest.raises(ValueError):
runtime._resolve_script(root, "runtime.py", None)
with pytest.raises(ValueError):
runtime._resolve_script(root, "generate.py", "../banana")
def test_manual_extension_fallback_stays_inside_installed_skill(tmp_path: Path) -> None:
root = tmp_path / "skills" / "seo"
scripts = tmp_path / "skills" / "seo-image-gen" / "scripts"
scripts.mkdir(parents=True)
(scripts / "generate.py").write_text("print('ok')\n", encoding="utf-8")
assert runtime._resolve_script(root, "generate.py", "banana") == (
scripts / "generate.py"
).resolve()
def test_status_uses_plugin_data_and_hash_marker(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
root = _fixture_root(tmp_path)
data = tmp_path / "persistent data"
python = data / ".venv" / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
python.parent.mkdir(parents=True)
python.write_text("", encoding="utf-8")
monkeypatch.setenv("CLAUDE_PLUGIN_DATA", str(data))
expected = runtime._expected(root)
(data / "runtime-state.json").write_text(json.dumps({**expected, "browser_ready": True}), encoding="utf-8")
status = runtime._status(root)
assert status["ready"] is True
assert status["mode"] == "plugin"
(root / "requirements.txt").write_text("requests>=3\n", encoding="utf-8")
assert runtime._status(root)["ready"] is False
def test_source_checkout_uses_repository_venv_without_plugin_environment(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
root = _fixture_root(tmp_path)
(root / ".git").mkdir()
monkeypatch.delenv("CLAUDE_PLUGIN_DATA", raising=False)
monkeypatch.delenv("CLAUDE_PLUGIN_ROOT", raising=False)
monkeypatch.delenv("CLAUDE_SEO_DATA_DIR", raising=False)
data_dir, mode = runtime._data_dir(root)
assert data_dir == root
assert mode == "manual"
def test_doctor_json_omits_paths_and_environment_values(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
root = _fixture_root(tmp_path)
sensitive_path = tmp_path / "person-name" / "private"
monkeypatch.setattr(runtime, "_root", lambda: root)
monkeypatch.setattr(
runtime,
"_probe_render",
lambda _status: {"backend": "static", "degraded": True, "available": {}},
)
monkeypatch.setenv("CLAUDE_PLUGIN_DATA", str(sensitive_path))
rc = runtime.command_doctor(SimpleNamespace(json=True))
output = capsys.readouterr().out
payload = json.loads(output)
assert rc == 3
assert str(sensitive_path) not in output
assert set(payload) == {
"browser_ready", "degraded", "mode", "plugin_version", "python_version",
"ready", "reasons", "render_backend",
}
def test_child_environment_forces_utf8_and_persistent_browser_path(tmp_path: Path) -> None:
env = runtime._safe_env({"data_dir": tmp_path})
assert env["PYTHONUTF8"] == "1"
assert env["PYTHONIOENCODING"] == "utf-8"
assert env["PYTHONDONTWRITEBYTECODE"] == "1"
assert env["OPENCODE_SEO_BROWSERS_PATH"] == str(tmp_path / "ms-playwright")
# An empty managed directory must not shadow the system browser cache.
assert "PLAYWRIGHT_BROWSERS_PATH" not in env
def test_child_environment_uses_managed_browser_path_when_it_has_chromium(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
import render_backend
managed = tmp_path / "ms-playwright"
managed.mkdir()
monkeypatch.setattr(
render_backend,
"has_chromium",
lambda path: path is not None and Path(path) == managed,
)
env = runtime._safe_env({"data_dir": tmp_path})
assert env["PLAYWRIGHT_BROWSERS_PATH"] == str(managed)
def test_configured_data_dir_rejects_filesystem_root_and_user_home(
monkeypatch: pytest.MonkeyPatch,
) -> None:
with pytest.raises(ValueError):
runtime._configured_data_dir(str(Path(Path.cwd().anchor)))
monkeypatch.setattr(runtime.Path, "home", classmethod(lambda cls: Path.cwd()))
with pytest.raises(ValueError):
runtime._configured_data_dir(str(Path.cwd()))
def test_version_only_change_does_not_stale_environment(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
root = _fixture_root(tmp_path)
data = tmp_path / "data"
python = data / ".venv" / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
python.parent.mkdir(parents=True)
python.write_text("", encoding="utf-8")
monkeypatch.setenv("CLAUDE_PLUGIN_DATA", str(data))
expected = runtime._expected(root)
expected["plugin_version"] = "2.2.2"
(data / "runtime-state.json").write_text(json.dumps(expected), encoding="utf-8")
assert runtime._status(root)["ready"] is True
def test_error_redaction_hides_credentials_and_tokens() -> None:
text = (
f"{Path.home()}/private user@example.test "
"https://user:password@example.test/path?token=visible token=abc123 api_key: xyz"
)
redacted = runtime._redact(text)
assert str(Path.home()) not in redacted
assert "user@example.test" not in redacted
assert "password@example" not in redacted
assert "visible" not in redacted
assert "abc123" not in redacted
assert " xyz" not in redacted
def test_browser_marker_requires_browser_files(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
import render_backend
root = _fixture_root(tmp_path)
data = tmp_path / "data"
python = data / ".venv" / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
python.parent.mkdir(parents=True)
python.write_text("", encoding="utf-8")
monkeypatch.setenv("CLAUDE_PLUGIN_DATA", str(data))
monkeypatch.setattr(render_backend, "system_browsers_dir", lambda: tmp_path / "system-empty")
monkeypatch.setattr(render_backend, "channel_candidates", lambda: ())
monkeypatch.setattr(render_backend, "cli_executable", lambda: None)
monkeypatch.setattr(runtime, "_venv_has_package", lambda _venv, name: name == "playwright")
monkeypatch.setattr(
render_backend,
"has_chromium",
lambda path: path is not None and (Path(path) / "chromium-123").is_dir(),
)
expected = runtime._expected(root)
(data / "runtime-state.json").write_text(
json.dumps({**expected, "browser_ready": True}), encoding="utf-8"
)
# The state flag is not evidence: the directory has to hold a browser.
assert runtime._status(root)["browser_ready"] is False
(data / "ms-playwright" / "chromium-123").mkdir(parents=True)
assert runtime._status(root)["browser_ready"] is True
def test_setup_restores_previous_environment_when_staged_swap_fails(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
root = _fixture_root(tmp_path)
data = tmp_path / "data"
final = data / ".venv"
old_python = final / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
old_python.parent.mkdir(parents=True)
old_python.write_text("old", encoding="utf-8")
(final / "old-sentinel").write_text("keep", encoding="utf-8")
data.mkdir(parents=True, exist_ok=True)
(data / "runtime-state.json").write_text(
json.dumps(runtime._expected(root)), encoding="utf-8"
)
monkeypatch.setenv("CLAUDE_PLUGIN_DATA", str(data))
monkeypatch.setattr(runtime, "_root", lambda: root)
def fake_checked(argv: list[str], *, env: dict[str, str], stage: str) -> subprocess.CompletedProcess[str]:
if stage == "virtual environment creation":
staged = Path(argv[-1])
staged_python = runtime._venv_python(staged)
staged_python.parent.mkdir(parents=True)
staged_python.write_text("new", encoding="utf-8")
return subprocess.CompletedProcess(argv, 0, "", "")
original_replace = Path.replace
def fail_staged_replace(self: Path, target: Path) -> Path:
if self.name.startswith(".venv.next-"):
raise OSError("injected staged swap failure")
return original_replace(self, target)
monkeypatch.setattr(runtime, "_run_checked", fake_checked)
monkeypatch.setattr(Path, "replace", fail_staged_replace)
rc = runtime.command_setup(SimpleNamespace(skip_browser=True))
assert rc == 1
assert (final / "old-sentinel").read_text(encoding="utf-8") == "keep"
assert not (data / ".venv.previous").exists()
assert not (data / ".runtime-state.next").exists()
def _setup_with_previous_environment(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> tuple[Path, Path]:
root = _fixture_root(tmp_path)
data = tmp_path / "data"
final = data / ".venv"
old_python = final / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
old_python.parent.mkdir(parents=True)
old_python.write_text("old", encoding="utf-8")
(final / "old-sentinel").write_text("keep", encoding="utf-8")
monkeypatch.setenv("CLAUDE_PLUGIN_DATA", str(data))
monkeypatch.setattr(runtime, "_root", lambda: root)
def fake_checked(argv: list[str], *, env: dict[str, str], stage: str) -> subprocess.CompletedProcess[str]:
if stage == "virtual environment creation":
staged_python = runtime._venv_python(Path(argv[-1]))
staged_python.parent.mkdir(parents=True)
staged_python.write_text("new", encoding="utf-8")
return subprocess.CompletedProcess(argv, 0, "", "")
monkeypatch.setattr(runtime, "_run_checked", fake_checked)
monkeypatch.setattr(runtime.time, "sleep", lambda _seconds: None)
return data, final
def test_setup_retries_venv_swaps_denied_while_windows_releases_handles(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
data, final = _setup_with_previous_environment(tmp_path, monkeypatch)
original_replace = Path.replace
denied: set[str] = set()
def deny_first_venv_swap(self: Path, target: Path) -> Path:
if self.name.startswith(".venv") and self.name not in denied:
denied.add(self.name)
raise PermissionError(5, "Access is denied")
return original_replace(self, target)
monkeypatch.setattr(Path, "replace", deny_first_venv_swap)
rc = runtime.command_setup(SimpleNamespace(skip_browser=True))
assert rc == 0
assert len(denied) == 2
assert runtime._venv_python(final).read_text(encoding="utf-8") == "new"
assert not (data / ".venv.previous").exists()
def test_setup_rollback_retries_a_denied_restore_of_the_previous_venv(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
data, final = _setup_with_previous_environment(tmp_path, monkeypatch)
original_replace = Path.replace
restore_denied = False
def fail_staged_then_deny_restore(self: Path, target: Path) -> Path:
nonlocal restore_denied
if self.name.startswith(".venv.next-"):
raise OSError("injected staged swap failure")
if self.name == ".venv.previous" and not restore_denied:
restore_denied = True
raise PermissionError(5, "Access is denied")
return original_replace(self, target)
monkeypatch.setattr(Path, "replace", fail_staged_then_deny_restore)
rc = runtime.command_setup(SimpleNamespace(skip_browser=True))
assert rc == 1
assert restore_denied
assert (final / "old-sentinel").read_text(encoding="utf-8") == "keep"
assert not (data / ".venv.previous").exists()
def test_run_propagates_child_signal(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
root = _fixture_root(tmp_path)
managed_python = tmp_path / "python"
managed_python.write_text("", encoding="utf-8")
status = {
"ready": True,
"python_path": managed_python,
"data_dir": tmp_path,
}
delivered: list[tuple[int, int]] = []
monkeypatch.setattr(runtime, "_root", lambda: root)
monkeypatch.setattr(runtime, "_status", lambda _: status)
monkeypatch.setattr(
runtime.subprocess,
"run",
lambda *args, **kwargs: subprocess.CompletedProcess(args[0], -signal.SIGTERM),
)
monkeypatch.setattr(runtime.os, "kill", lambda pid, sig: delivered.append((pid, sig)))
rc = runtime.command_run(
SimpleNamespace(script="render_page.py", extension=None, script_args=[])
)
assert rc == 128 + signal.SIGTERM
assert delivered == [(os.getpid(), signal.SIGTERM)]
# Scripts run without an --extension flag are dispatched straight from the
# core scripts/ directory, so they must be listed in ALLOWED_CORE_SCRIPTS or
# `claude-seo run` refuses them at runtime, even though the SKILL.md/agent
# instructions look correct. This regression was caught by hand for
# keywordseverywhere_api.py (SKILL.md wired it up, ALLOWED_CORE_SCRIPTS did
# not list it) -- this test makes sure the next one doesn't ship silently.
_RUN_INVOCATION = re.compile(r'run\s+([A-Za-z0-9_]+\.py)(?!["\']?\s*--extension)')
_EXTENSION_FLAG = re.compile(r'run\s+[A-Za-z0-9_]+\.py[^\n]*--extension\b')
def _instruction_files() -> list[Path]:
files: list[Path] = []
for pattern in ("skills/**/SKILL.md", "agents/*.md"):
files.extend(sorted(ROOT.glob(pattern)))
return files
def test_every_skill_invoked_script_is_runtime_allowlisted() -> None:
missing: list[str] = []
for path in _instruction_files():
text = path.read_text(encoding="utf-8")
rel = path.relative_to(ROOT).as_posix()
for line in text.splitlines():
if _EXTENSION_FLAG.search(line):
# Extension scripts are dispatched via extensions/<name>/scripts/
# and are not, and should not be, in ALLOWED_CORE_SCRIPTS.
continue
for match in _RUN_INVOCATION.finditer(line):
script = match.group(1)
if script not in runtime.ALLOWED_CORE_SCRIPTS:
missing.append(f"{rel}: {script}")
assert not missing, (
"scripts invoked from SKILL.md/agent instructions but missing from "
"ALLOWED_CORE_SCRIPTS in scripts/runtime.py (claude-seo run would "
"refuse them): " + "; ".join(missing)
)
def test_failed_stage_reports_child_diagnostics(monkeypatch: pytest.MonkeyPatch) -> None:
stderr = "\n".join(
[
"Looking in links: /tmp/tmp8b58vt29",
"ERROR: Could not install packages due to an OSError: [WinError 206] "
"Der Dateiname oder die Erweiterung ist zu lang",
"",
]
)
monkeypatch.setattr(
runtime.subprocess,
"run",
lambda argv, **kwargs: subprocess.CompletedProcess(argv, 1, "", stderr),
)
with pytest.raises(RuntimeError) as excinfo:
runtime._run_checked(["python", "-m", "venv", "x"], env={}, stage="virtual environment creation")
message = str(excinfo.value)
assert message.startswith("virtual environment creation failed with exit code 1")
assert "[WinError 206]" in message
def test_failed_stage_falls_back_to_stdout_and_bounds_the_tail(monkeypatch: pytest.MonkeyPatch) -> None:
stdout = "\n".join(f"line {index}" for index in range(40))
monkeypatch.setattr(
runtime.subprocess,
"run",
lambda argv, **kwargs: subprocess.CompletedProcess(argv, 2, stdout, ""),
)
with pytest.raises(RuntimeError) as excinfo:
runtime._run_checked(["pip"], env={}, stage="dependency installation")
message = str(excinfo.value)
assert "line 39" in message
assert "line 10" in message
assert "line 9" not in message
def test_successful_stage_returns_the_completed_process(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
runtime.subprocess,
"run",
lambda argv, **kwargs: subprocess.CompletedProcess(argv, 0, "ok", "warning noise"),
)
assert runtime._run_checked(["pip"], env={}, stage="dependency installation").stdout == "ok"
def test_redaction_covers_repr_quoted_home(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(runtime.Path, "home", classmethod(lambda cls: Path(r"C:\Users\someone")))
quoted = repr(r"C:\Users\someone\.claude\skills\seo\.venv.next-1\Scripts\python.exe")
nested = repr(f"sys.path = [{quoted}]")
redacted = runtime._redact(
f"Command '[{quoted}, '-c', {nested}]' returned non-zero exit status 1.\n"
"Looking in links: c:\\users\\someone\\AppData\\Local\\Temp\\tmp1"
)
assert "someone" not in redacted
assert redacted.count("<home>") == 3
def test_browser_setup_warning_is_redacted(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
# _run_checked now embeds the child's own stderr tail (#300), so a failed
# Chromium install can carry the home directory (playwright's cache path,
# a download URL, ...). That message is printed as a non-fatal warning
# rather than routed through the fatal-error handler, so it must be
# redacted at its own print site instead of relying on the one at the
# bottom of command_setup.
root = _fixture_root(tmp_path)
data = tmp_path / "data"
data.mkdir(parents=True, exist_ok=True)
(data / "runtime-state.json").write_text(
json.dumps(runtime._expected(root)), encoding="utf-8"
)
monkeypatch.setenv("CLAUDE_PLUGIN_DATA", str(data))
monkeypatch.setattr(runtime, "_root", lambda: root)
fake_home = tmp_path / "home" / "someone"
monkeypatch.setattr(runtime.Path, "home", classmethod(lambda cls: fake_home))
def fake_checked(argv: list[str], *, env: dict[str, str], stage: str) -> subprocess.CompletedProcess[str]:
if stage == "virtual environment creation":
staged = Path(argv[-1])
staged_python = runtime._venv_python(staged)
staged_python.parent.mkdir(parents=True)
staged_python.write_text("new", encoding="utf-8")
if stage == "Chromium installation":
raise RuntimeError(
"Chromium installation failed with exit code 1\n"
f" Failed to download to {fake_home}/.cache/ms-playwright/chromium"
)
return subprocess.CompletedProcess(argv, 0, "", "")
monkeypatch.setattr(runtime, "_run_checked", fake_checked)
monkeypatch.setattr(
runtime,
"_render_status",
lambda _status: {"backend": "static", "degraded": True, "available": {}},
)
rc = runtime.command_setup(SimpleNamespace(skip_browser=False))
assert rc == 10
captured = capsys.readouterr()
assert str(fake_home) not in captured.err
assert "<home>" in captured.err