@elvis1513/auto-coding-skill 4.3.7 → 5.0.0
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.
- package/README.md +26 -761
- package/cli/assets/managed-install.json +38 -1
- package/cli/assets/skill/SKILL.md +15 -100
- package/cli/assets/skill/data/templates/ENVIRONMENT.md +30 -0
- package/cli/assets/skill/data/templates/architecture/.gitkeep +1 -0
- package/cli/assets/skill/data/templates/bridges/AGENTS.md +39 -145
- package/cli/assets/skill/data/templates/deployment/.gitkeep +1 -0
- package/cli/assets/skill/data/templates/design/.gitkeep +1 -0
- package/cli/assets/skill/data/templates/interfaces/.gitkeep +1 -0
- package/cli/assets/skill/data/templates/product/.gitkeep +1 -0
- package/cli/src/index.js +127 -2589
- package/cli/src/sync-assets.js +38 -95
- package/package.json +9 -17
- package/cli/assets/agents/browser-debugger.toml +0 -27
- package/cli/assets/agents/docs-researcher.toml +0 -18
- package/cli/assets/agents/explorer.toml +0 -26
- package/cli/assets/agents/fixer.toml +0 -29
- package/cli/assets/agents/reviewer.toml +0 -50
- package/cli/assets/skill/data/contracts/orchestration-v1.schema.json +0 -567
- package/cli/assets/skill/data/policies/feedback-resolutions-v1.json +0 -68
- package/cli/assets/skill/data/policies/workflow-migrations-v1.json +0 -657
- package/cli/assets/skill/data/templates/ENGINEERING.md +0 -115
- package/cli/assets/skill/data/templates/tools/ap.py +0 -16
- package/cli/assets/skill/requirements.txt +0 -1
- package/cli/assets/skill/scripts/ap.py +0 -17791
- package/cli/assets/skill/scripts/core.py +0 -510
- package/cli/assets/skill/scripts/http_checks.py +0 -92
- package/cli/assets/skill/scripts/install_integrity.py +0 -427
- package/cli/assets/skill/scripts/scaffold_templates.py +0 -325
- package/cli/tests/ap_concurrency_test.py +0 -3602
- package/cli/tests/ap_profiles_test.py +0 -3272
- package/cli/tests/core_shell_test.py +0 -345
- package/cli/tests/index.test.js +0 -2967
|
@@ -1,510 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
"""AutoPipeline Pro Max - Core helpers"""
|
|
4
|
-
|
|
5
|
-
from __future__ import annotations
|
|
6
|
-
|
|
7
|
-
import copy
|
|
8
|
-
import math
|
|
9
|
-
import os
|
|
10
|
-
import re
|
|
11
|
-
import signal
|
|
12
|
-
import shlex
|
|
13
|
-
import shutil
|
|
14
|
-
import stat
|
|
15
|
-
import subprocess
|
|
16
|
-
import sys
|
|
17
|
-
import urllib.error
|
|
18
|
-
import urllib.request
|
|
19
|
-
from pathlib import Path
|
|
20
|
-
from typing import Iterable, Optional, Dict, Any
|
|
21
|
-
|
|
22
|
-
try:
|
|
23
|
-
import yaml # type: ignore
|
|
24
|
-
except Exception:
|
|
25
|
-
yaml = None
|
|
26
|
-
|
|
27
|
-
class APError(RuntimeError):
|
|
28
|
-
pass
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
MANAGED_CONFIG_RELATIVE_PATH = Path("docs/ENGINEERING.md")
|
|
32
|
-
PROJECT_CONFIG_PATH = Path("docs/project/auto-coding-skill.yaml")
|
|
33
|
-
PROJECT_CONFIG_OVERLAY_RELATIVE_PATH = PROJECT_CONFIG_PATH
|
|
34
|
-
PROJECT_CONFIG_SCHEMA = "auto-coding-skill/project-config/v1"
|
|
35
|
-
MANAGED_CONFIG_FRONTMATTER_MAX_BYTES = 256 * 1024
|
|
36
|
-
PROJECT_CONFIG_OVERLAY_MAX_BYTES = 128 * 1024
|
|
37
|
-
PROJECT_CONFIG_MAX_YAML_DEPTH = 64
|
|
38
|
-
PROJECT_CONFIG_MAX_YAML_NODES = 10_000
|
|
39
|
-
PROJECT_CONFIG_PROTECTED_WORKFLOW_FIELDS = frozenset(
|
|
40
|
-
{"skill_version", "mode", "completion"}
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
class _ConfigYamlViolation(RuntimeError):
|
|
45
|
-
"""Internal marker whose message never includes user-controlled YAML."""
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def _is_windows_reparse_point(metadata: os.stat_result) -> bool:
|
|
49
|
-
attributes = int(getattr(metadata, "st_file_attributes", 0) or 0)
|
|
50
|
-
reparse_flag = int(getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0x400))
|
|
51
|
-
return stat.S_ISLNK(metadata.st_mode) or bool(attributes & reparse_flag)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
def _file_identity(metadata: os.stat_result) -> tuple[int, int]:
|
|
55
|
-
return int(metadata.st_dev), int(metadata.st_ino)
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
def runtime_requirements_path() -> Path:
|
|
59
|
-
return Path(__file__).resolve().parents[1] / "requirements.txt"
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
def require_yaml() -> Any:
|
|
63
|
-
if yaml is None:
|
|
64
|
-
install = shlex.join(
|
|
65
|
-
[
|
|
66
|
-
sys.executable,
|
|
67
|
-
"-m",
|
|
68
|
-
"pip",
|
|
69
|
-
"install",
|
|
70
|
-
"--requirement",
|
|
71
|
-
str(runtime_requirements_path()),
|
|
72
|
-
]
|
|
73
|
-
)
|
|
74
|
-
raise APError(
|
|
75
|
-
f"PyYAML is missing from {sys.executable}. Run: {install}"
|
|
76
|
-
)
|
|
77
|
-
return yaml
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
def run(cmd: Iterable[str], cwd: Optional[Path] = None, check: bool = True) -> subprocess.CompletedProcess:
|
|
81
|
-
p = subprocess.run(list(cmd), cwd=str(cwd) if cwd else None, text=True, capture_output=True)
|
|
82
|
-
if check and p.returncode != 0:
|
|
83
|
-
raise APError(
|
|
84
|
-
f"Command failed ({p.returncode}): {' '.join(cmd)}\nSTDOUT:\n{p.stdout}\nSTDERR:\n{p.stderr}"
|
|
85
|
-
)
|
|
86
|
-
return p
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
def ensure_git_repo(repo: Path) -> None:
|
|
90
|
-
try:
|
|
91
|
-
run(["git", "rev-parse", "--is-inside-work-tree"], cwd=repo)
|
|
92
|
-
except APError as e:
|
|
93
|
-
raise APError(f"Not a git repository: {repo}\n{e}") from e
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
def load_yaml(path: Path) -> Dict[str, Any]:
|
|
97
|
-
if not path.exists():
|
|
98
|
-
raise APError(f"YAML not found: {path}")
|
|
99
|
-
yaml_module = require_yaml()
|
|
100
|
-
if path.suffix.lower() in {".md", ".markdown"}:
|
|
101
|
-
text = path.read_text(encoding="utf-8")
|
|
102
|
-
m = re.match(r"^---\s*\n(.*?)\n---\s*(\n|$)", text, flags=re.DOTALL)
|
|
103
|
-
if not m:
|
|
104
|
-
raise APError(
|
|
105
|
-
f"Config markdown frontmatter not found: {path}\n"
|
|
106
|
-
"Expected YAML frontmatter wrapped by '---' at top of file."
|
|
107
|
-
)
|
|
108
|
-
data = yaml_module.safe_load(m.group(1))
|
|
109
|
-
else:
|
|
110
|
-
with path.open("r", encoding="utf-8") as f:
|
|
111
|
-
data = yaml_module.safe_load(f)
|
|
112
|
-
return data or {}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
def copy_tree(src: Path, dst: Path) -> None:
|
|
116
|
-
if src.is_file():
|
|
117
|
-
if src.name == ".DS_Store" or src.name.endswith(".pyc"):
|
|
118
|
-
return
|
|
119
|
-
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
120
|
-
shutil.copy2(src, dst)
|
|
121
|
-
return
|
|
122
|
-
for root, _, files in os.walk(src):
|
|
123
|
-
root_p = Path(root)
|
|
124
|
-
if "__pycache__" in root_p.parts:
|
|
125
|
-
continue
|
|
126
|
-
rel = root_p.relative_to(src)
|
|
127
|
-
for fn in files:
|
|
128
|
-
if fn == ".DS_Store" or fn.endswith(".pyc"):
|
|
129
|
-
continue
|
|
130
|
-
s = root_p / fn
|
|
131
|
-
d = dst / rel / fn
|
|
132
|
-
d.parent.mkdir(parents=True, exist_ok=True)
|
|
133
|
-
shutil.copy2(s, d)
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
def http_get_status(url: str, timeout_s: int = 5) -> int:
|
|
137
|
-
request = urllib.request.Request(url, method="GET")
|
|
138
|
-
try:
|
|
139
|
-
with urllib.request.urlopen(request, timeout=timeout_s) as response:
|
|
140
|
-
return int(response.status)
|
|
141
|
-
except urllib.error.HTTPError as exc:
|
|
142
|
-
return int(exc.code)
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
def find_config(repo: Path) -> Path:
|
|
146
|
-
"""Find single source project config file."""
|
|
147
|
-
candidates = [
|
|
148
|
-
repo / MANAGED_CONFIG_RELATIVE_PATH,
|
|
149
|
-
]
|
|
150
|
-
for c in candidates:
|
|
151
|
-
if c.exists():
|
|
152
|
-
return c
|
|
153
|
-
raise APError(
|
|
154
|
-
"Project config not found. Create docs/ENGINEERING.md "
|
|
155
|
-
"and put commands + runtime + jenkins fields in YAML frontmatter."
|
|
156
|
-
)
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
def _config_source_name(relative: Path) -> str:
|
|
160
|
-
if relative == MANAGED_CONFIG_RELATIVE_PATH:
|
|
161
|
-
return "Managed project configuration"
|
|
162
|
-
return "Project configuration overlay"
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
def _read_project_config_file(
|
|
166
|
-
repo: Path,
|
|
167
|
-
relative: Path,
|
|
168
|
-
*,
|
|
169
|
-
max_bytes: int,
|
|
170
|
-
require_eof: bool,
|
|
171
|
-
) -> bytes | None:
|
|
172
|
-
"""Read a bounded project-relative config without following in-repo links."""
|
|
173
|
-
source = _config_source_name(relative)
|
|
174
|
-
if relative.is_absolute() or not relative.parts or any(
|
|
175
|
-
part in {"", ".", ".."} for part in relative.parts
|
|
176
|
-
):
|
|
177
|
-
raise APError(f"{source} path is invalid.")
|
|
178
|
-
if max_bytes <= 0:
|
|
179
|
-
raise APError(f"{source} read bound is invalid.")
|
|
180
|
-
|
|
181
|
-
repo = Path(repo)
|
|
182
|
-
if os.name == "nt":
|
|
183
|
-
try:
|
|
184
|
-
repo_metadata = repo.lstat()
|
|
185
|
-
if _is_windows_reparse_point(repo_metadata) or not stat.S_ISDIR(repo_metadata.st_mode):
|
|
186
|
-
raise APError(f"{source} repository root must be a real directory.")
|
|
187
|
-
checked_directories = [(repo, _file_identity(repo_metadata))]
|
|
188
|
-
current = repo
|
|
189
|
-
for part in relative.parts[:-1]:
|
|
190
|
-
current = current / part
|
|
191
|
-
if not os.path.lexists(current):
|
|
192
|
-
return None
|
|
193
|
-
metadata = current.lstat()
|
|
194
|
-
if _is_windows_reparse_point(metadata) or not stat.S_ISDIR(metadata.st_mode):
|
|
195
|
-
raise APError(f"{source} parent must be a real directory.")
|
|
196
|
-
checked_directories.append((current, _file_identity(metadata)))
|
|
197
|
-
target = current / relative.parts[-1]
|
|
198
|
-
if not os.path.lexists(target):
|
|
199
|
-
return None
|
|
200
|
-
metadata = target.lstat()
|
|
201
|
-
if _is_windows_reparse_point(metadata) or not stat.S_ISREG(metadata.st_mode):
|
|
202
|
-
raise APError(f"{source} must be a regular non-symlink file.")
|
|
203
|
-
if require_eof and metadata.st_size > max_bytes:
|
|
204
|
-
raise APError(f"{source} exceeds its size limit.")
|
|
205
|
-
with target.open("rb") as handle:
|
|
206
|
-
opened = os.fstat(handle.fileno())
|
|
207
|
-
if not stat.S_ISREG(opened.st_mode) or _file_identity(opened) != _file_identity(metadata):
|
|
208
|
-
raise APError(f"{source} changed during its safety check.")
|
|
209
|
-
for directory, identity in checked_directories:
|
|
210
|
-
checked = directory.lstat()
|
|
211
|
-
if (
|
|
212
|
-
_is_windows_reparse_point(checked)
|
|
213
|
-
or not stat.S_ISDIR(checked.st_mode)
|
|
214
|
-
or _file_identity(checked) != identity
|
|
215
|
-
):
|
|
216
|
-
raise APError(f"{source} parent changed during its safety check.")
|
|
217
|
-
payload = handle.read(max_bytes + 1)
|
|
218
|
-
except APError:
|
|
219
|
-
raise
|
|
220
|
-
except OSError as exc:
|
|
221
|
-
raise APError(f"{source} could not be read safely.") from exc
|
|
222
|
-
else:
|
|
223
|
-
directory_flags = (
|
|
224
|
-
os.O_RDONLY
|
|
225
|
-
| getattr(os, "O_DIRECTORY", 0)
|
|
226
|
-
| getattr(os, "O_NOFOLLOW", 0)
|
|
227
|
-
)
|
|
228
|
-
descriptors: list[int] = []
|
|
229
|
-
file_descriptor = -1
|
|
230
|
-
try:
|
|
231
|
-
current = os.open(repo, directory_flags)
|
|
232
|
-
descriptors.append(current)
|
|
233
|
-
for part in relative.parts[:-1]:
|
|
234
|
-
try:
|
|
235
|
-
metadata = os.stat(part, dir_fd=current, follow_symlinks=False)
|
|
236
|
-
except FileNotFoundError:
|
|
237
|
-
return None
|
|
238
|
-
if stat.S_ISLNK(metadata.st_mode) or not stat.S_ISDIR(metadata.st_mode):
|
|
239
|
-
raise APError(f"{source} parent must be a real directory.")
|
|
240
|
-
next_descriptor = os.open(part, directory_flags, dir_fd=current)
|
|
241
|
-
descriptors.append(next_descriptor)
|
|
242
|
-
current = next_descriptor
|
|
243
|
-
try:
|
|
244
|
-
metadata = os.stat(
|
|
245
|
-
relative.parts[-1],
|
|
246
|
-
dir_fd=current,
|
|
247
|
-
follow_symlinks=False,
|
|
248
|
-
)
|
|
249
|
-
if stat.S_ISLNK(metadata.st_mode) or not stat.S_ISREG(metadata.st_mode):
|
|
250
|
-
raise APError(f"{source} must be a regular non-symlink file.")
|
|
251
|
-
file_descriptor = os.open(
|
|
252
|
-
relative.parts[-1],
|
|
253
|
-
os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0),
|
|
254
|
-
dir_fd=current,
|
|
255
|
-
)
|
|
256
|
-
except FileNotFoundError:
|
|
257
|
-
return None
|
|
258
|
-
metadata = os.fstat(file_descriptor)
|
|
259
|
-
if not stat.S_ISREG(metadata.st_mode):
|
|
260
|
-
raise APError(f"{source} must be a regular non-symlink file.")
|
|
261
|
-
if require_eof and metadata.st_size > max_bytes:
|
|
262
|
-
raise APError(f"{source} exceeds its size limit.")
|
|
263
|
-
with os.fdopen(file_descriptor, "rb") as handle:
|
|
264
|
-
file_descriptor = -1
|
|
265
|
-
payload = handle.read(max_bytes + 1)
|
|
266
|
-
except APError:
|
|
267
|
-
raise
|
|
268
|
-
except OSError as exc:
|
|
269
|
-
raise APError(f"{source} could not be read safely.") from exc
|
|
270
|
-
finally:
|
|
271
|
-
if file_descriptor >= 0:
|
|
272
|
-
os.close(file_descriptor)
|
|
273
|
-
for descriptor in reversed(descriptors):
|
|
274
|
-
try:
|
|
275
|
-
os.close(descriptor)
|
|
276
|
-
except OSError:
|
|
277
|
-
pass
|
|
278
|
-
|
|
279
|
-
if require_eof and len(payload) > max_bytes:
|
|
280
|
-
raise APError(f"{source} exceeds its size limit.")
|
|
281
|
-
return payload
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
_FRONTMATTER_BYTES_RE = re.compile(
|
|
285
|
-
rb"\A---[ \t]*\r?\n(.*?)\r?\n---[ \t]*(?:\r?\n|\Z)",
|
|
286
|
-
flags=re.DOTALL,
|
|
287
|
-
)
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
def _decode_managed_frontmatter(payload: bytes) -> str:
|
|
291
|
-
match = _FRONTMATTER_BYTES_RE.match(payload)
|
|
292
|
-
if not match or match.end() > MANAGED_CONFIG_FRONTMATTER_MAX_BYTES:
|
|
293
|
-
raise APError(
|
|
294
|
-
"Managed project configuration must contain bounded YAML frontmatter at the file start."
|
|
295
|
-
)
|
|
296
|
-
try:
|
|
297
|
-
return match.group(1).decode("utf-8")
|
|
298
|
-
except UnicodeDecodeError as exc:
|
|
299
|
-
raise APError("Managed project configuration frontmatter must be UTF-8.") from exc
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
def _strict_yaml_mapping(text: str, source: str) -> Dict[str, Any]:
|
|
303
|
-
yaml_module = require_yaml()
|
|
304
|
-
merge_tag = "tag:yaml.org,2002:merge"
|
|
305
|
-
|
|
306
|
-
class StrictConfigLoader(yaml_module.SafeLoader):
|
|
307
|
-
def compose_node(self, parent: Any, index: Any) -> Any:
|
|
308
|
-
event = self.peek_event()
|
|
309
|
-
if isinstance(event, yaml_module.events.AliasEvent) or getattr(event, "anchor", None):
|
|
310
|
-
raise _ConfigYamlViolation("alias")
|
|
311
|
-
depth = getattr(self, "_project_config_depth", 0)
|
|
312
|
-
nodes = getattr(self, "_project_config_nodes", 0) + 1
|
|
313
|
-
if depth >= PROJECT_CONFIG_MAX_YAML_DEPTH or nodes > PROJECT_CONFIG_MAX_YAML_NODES:
|
|
314
|
-
raise _ConfigYamlViolation("complexity")
|
|
315
|
-
self._project_config_depth = depth + 1
|
|
316
|
-
self._project_config_nodes = nodes
|
|
317
|
-
try:
|
|
318
|
-
return super().compose_node(parent, index)
|
|
319
|
-
finally:
|
|
320
|
-
self._project_config_depth = depth
|
|
321
|
-
|
|
322
|
-
def construct_mapping(self, node: Any, deep: bool = False) -> Dict[str, Any]:
|
|
323
|
-
if not isinstance(node, yaml_module.nodes.MappingNode):
|
|
324
|
-
raise _ConfigYamlViolation("mapping")
|
|
325
|
-
result: Dict[str, Any] = {}
|
|
326
|
-
for key_node, value_node in node.value:
|
|
327
|
-
if getattr(key_node, "tag", "") == merge_tag:
|
|
328
|
-
raise _ConfigYamlViolation("merge")
|
|
329
|
-
key = self.construct_object(key_node, deep=deep)
|
|
330
|
-
if not isinstance(key, str):
|
|
331
|
-
raise _ConfigYamlViolation("key")
|
|
332
|
-
if key in result:
|
|
333
|
-
raise _ConfigYamlViolation("duplicate")
|
|
334
|
-
result[key] = self.construct_object(value_node, deep=deep)
|
|
335
|
-
return result
|
|
336
|
-
|
|
337
|
-
try:
|
|
338
|
-
value = yaml_module.load(text, Loader=StrictConfigLoader)
|
|
339
|
-
except _ConfigYamlViolation as exc:
|
|
340
|
-
messages = {
|
|
341
|
-
"alias": "must not use YAML anchors or aliases.",
|
|
342
|
-
"merge": "must not use YAML merge keys.",
|
|
343
|
-
"mapping": "contains an invalid YAML mapping.",
|
|
344
|
-
"key": "must use string YAML mapping keys.",
|
|
345
|
-
"duplicate": "contains duplicate YAML keys.",
|
|
346
|
-
"complexity": "exceeds the YAML complexity limit.",
|
|
347
|
-
}
|
|
348
|
-
raise APError(f"{source} {messages.get(str(exc), 'contains invalid YAML.')}") from None
|
|
349
|
-
except Exception:
|
|
350
|
-
raise APError(f"{source} contains invalid YAML.") from None
|
|
351
|
-
|
|
352
|
-
if value is None:
|
|
353
|
-
raise APError(f"{source} must not contain null values.")
|
|
354
|
-
if not isinstance(value, dict):
|
|
355
|
-
raise APError(f"{source} root must be a mapping.")
|
|
356
|
-
_validate_config_value(value, source)
|
|
357
|
-
return value
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
def _validate_config_value(value: Any, source: str) -> None:
|
|
361
|
-
if value is None:
|
|
362
|
-
raise APError(f"{source} must not contain null values.")
|
|
363
|
-
if isinstance(value, dict):
|
|
364
|
-
for key, nested in value.items():
|
|
365
|
-
if not isinstance(key, str):
|
|
366
|
-
raise APError(f"{source} must use string YAML mapping keys.")
|
|
367
|
-
_validate_config_string(key, source)
|
|
368
|
-
_validate_config_value(nested, source)
|
|
369
|
-
return
|
|
370
|
-
if isinstance(value, list):
|
|
371
|
-
for nested in value:
|
|
372
|
-
_validate_config_value(nested, source)
|
|
373
|
-
return
|
|
374
|
-
if isinstance(value, float) and not math.isfinite(value):
|
|
375
|
-
raise APError(f"{source} contains an unsupported scalar value.")
|
|
376
|
-
if isinstance(value, str):
|
|
377
|
-
_validate_config_string(value, source)
|
|
378
|
-
return
|
|
379
|
-
if not isinstance(value, (str, bool, int, float)):
|
|
380
|
-
raise APError(f"{source} contains an unsupported scalar value.")
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
def _validate_config_string(value: str, source: str) -> None:
|
|
384
|
-
try:
|
|
385
|
-
value.encode("utf-8")
|
|
386
|
-
except UnicodeEncodeError:
|
|
387
|
-
raise APError(f"{source} contains an invalid Unicode string.") from None
|
|
388
|
-
if any(
|
|
389
|
-
(ord(char) < 0x20 and char not in {"\t", "\n", "\r"})
|
|
390
|
-
or 0x7F <= ord(char) <= 0x9F
|
|
391
|
-
for char in value
|
|
392
|
-
):
|
|
393
|
-
raise APError(f"{source} contains an unsupported control character.")
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
def parse_managed_config_payload(payload: bytes) -> Dict[str, Any]:
|
|
397
|
-
"""Strictly parse a bounded managed ENGINEERING document already in memory."""
|
|
398
|
-
frontmatter = _decode_managed_frontmatter(payload)
|
|
399
|
-
return _strict_yaml_mapping(frontmatter, "Managed project configuration")
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
def load_managed_config(repo: Path) -> Dict[str, Any]:
|
|
403
|
-
"""Load the Skill-managed defaults from docs/ENGINEERING.md frontmatter."""
|
|
404
|
-
payload = _read_project_config_file(
|
|
405
|
-
repo,
|
|
406
|
-
MANAGED_CONFIG_RELATIVE_PATH,
|
|
407
|
-
max_bytes=MANAGED_CONFIG_FRONTMATTER_MAX_BYTES,
|
|
408
|
-
require_eof=False,
|
|
409
|
-
)
|
|
410
|
-
if payload is None:
|
|
411
|
-
raise APError("Managed project configuration was not found.")
|
|
412
|
-
return parse_managed_config_payload(payload)
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
def parse_project_overrides_payload(payload: bytes) -> Dict[str, Any]:
|
|
416
|
-
"""Strictly parse a bounded project overlay already in memory."""
|
|
417
|
-
if len(payload) > PROJECT_CONFIG_OVERLAY_MAX_BYTES:
|
|
418
|
-
raise APError("Project configuration overlay exceeds its size limit.")
|
|
419
|
-
try:
|
|
420
|
-
text = payload.decode("utf-8")
|
|
421
|
-
except UnicodeDecodeError as exc:
|
|
422
|
-
raise APError("Project configuration overlay must be UTF-8.") from exc
|
|
423
|
-
document = _strict_yaml_mapping(text, "Project configuration overlay")
|
|
424
|
-
if set(document) != {"schema", "overrides"}:
|
|
425
|
-
raise APError("Project configuration overlay has unsupported top-level wrapper fields.")
|
|
426
|
-
if document.get("schema") != PROJECT_CONFIG_SCHEMA:
|
|
427
|
-
raise APError("Project configuration overlay schema is unsupported.")
|
|
428
|
-
overrides = document.get("overrides")
|
|
429
|
-
if not isinstance(overrides, dict):
|
|
430
|
-
raise APError("Project configuration overlay overrides must be a mapping.")
|
|
431
|
-
workflow = overrides.get("workflow")
|
|
432
|
-
if workflow is not None:
|
|
433
|
-
if not isinstance(workflow, dict) or PROJECT_CONFIG_PROTECTED_WORKFLOW_FIELDS.intersection(
|
|
434
|
-
workflow
|
|
435
|
-
):
|
|
436
|
-
raise APError(
|
|
437
|
-
"Project configuration overlay cannot override managed workflow fields."
|
|
438
|
-
)
|
|
439
|
-
return overrides
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
def load_project_overrides(repo: Path) -> Dict[str, Any]:
|
|
443
|
-
"""Load the optional project-owned overlay after validating its wrapper."""
|
|
444
|
-
payload = _read_project_config_file(
|
|
445
|
-
repo,
|
|
446
|
-
PROJECT_CONFIG_OVERLAY_RELATIVE_PATH,
|
|
447
|
-
max_bytes=PROJECT_CONFIG_OVERLAY_MAX_BYTES,
|
|
448
|
-
require_eof=True,
|
|
449
|
-
)
|
|
450
|
-
if payload is None:
|
|
451
|
-
return {}
|
|
452
|
-
return parse_project_overrides_payload(payload)
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
def merge_project_config(
|
|
456
|
-
defaults: Dict[str, Any],
|
|
457
|
-
overrides: Dict[str, Any],
|
|
458
|
-
) -> Dict[str, Any]:
|
|
459
|
-
"""Recursively merge mappings; project scalars and lists replace defaults."""
|
|
460
|
-
if not isinstance(defaults, dict) or not isinstance(overrides, dict):
|
|
461
|
-
raise APError("Effective project configuration layers must be mappings.")
|
|
462
|
-
_validate_config_value(defaults, "Managed project configuration")
|
|
463
|
-
_validate_config_value(overrides, "Project configuration overlay")
|
|
464
|
-
workflow = overrides.get("workflow")
|
|
465
|
-
if workflow is not None:
|
|
466
|
-
if not isinstance(workflow, dict) or PROJECT_CONFIG_PROTECTED_WORKFLOW_FIELDS.intersection(
|
|
467
|
-
workflow
|
|
468
|
-
):
|
|
469
|
-
raise APError(
|
|
470
|
-
"Project configuration overlay cannot override managed workflow fields."
|
|
471
|
-
)
|
|
472
|
-
|
|
473
|
-
def merge(base: Dict[str, Any], project: Dict[str, Any]) -> Dict[str, Any]:
|
|
474
|
-
result = copy.deepcopy(base)
|
|
475
|
-
for key, value in project.items():
|
|
476
|
-
current = result.get(key)
|
|
477
|
-
if isinstance(current, dict) and isinstance(value, dict):
|
|
478
|
-
result[key] = merge(current, value)
|
|
479
|
-
else:
|
|
480
|
-
result[key] = copy.deepcopy(value)
|
|
481
|
-
return result
|
|
482
|
-
|
|
483
|
-
return merge(defaults, overrides)
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
def load_effective_config(repo: Path) -> Dict[str, Any]:
|
|
487
|
-
"""Return managed defaults merged with the project-owned overlay."""
|
|
488
|
-
return merge_project_config(load_managed_config(repo), load_project_overrides(repo))
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
def run_shell(command: str, cwd: Optional[Path] = None, timeout_s: Optional[float] = None) -> None:
|
|
492
|
-
"""Run in the caller's environment and terminate the process group on timeout."""
|
|
493
|
-
p = subprocess.Popen(
|
|
494
|
-
["bash", "-c", command],
|
|
495
|
-
cwd=str(cwd) if cwd else None,
|
|
496
|
-
text=True,
|
|
497
|
-
start_new_session=True,
|
|
498
|
-
)
|
|
499
|
-
try:
|
|
500
|
-
returncode = p.wait(timeout=timeout_s)
|
|
501
|
-
except subprocess.TimeoutExpired as exc:
|
|
502
|
-
os.killpg(p.pid, signal.SIGTERM)
|
|
503
|
-
try:
|
|
504
|
-
p.wait(timeout=2)
|
|
505
|
-
except subprocess.TimeoutExpired:
|
|
506
|
-
os.killpg(p.pid, signal.SIGKILL)
|
|
507
|
-
p.wait()
|
|
508
|
-
raise APError(f"Command timed out after {timeout_s:.1f}s: {command}") from exc
|
|
509
|
-
if returncode != 0:
|
|
510
|
-
raise APError(f"Command failed ({returncode}): {command}")
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
import argparse
|
|
5
|
-
import json
|
|
6
|
-
import urllib.error
|
|
7
|
-
import urllib.request
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class CheckError(RuntimeError):
|
|
11
|
-
pass
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
def fetch(method: str, url: str, headers: dict[str, str] | None = None) -> tuple[int, str]:
|
|
15
|
-
req = urllib.request.Request(url, headers=headers or {}, method=method)
|
|
16
|
-
try:
|
|
17
|
-
with urllib.request.urlopen(req, timeout=10) as response:
|
|
18
|
-
return response.status, response.read().decode("utf-8", errors="replace")
|
|
19
|
-
except urllib.error.HTTPError as exc:
|
|
20
|
-
return exc.code, exc.read().decode("utf-8", errors="replace")
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def cmd_status(args: argparse.Namespace) -> None:
|
|
24
|
-
status, body = fetch("GET", args.url)
|
|
25
|
-
if status != args.expect:
|
|
26
|
-
raise CheckError(f"GET {args.url} expected {args.expect}, got {status}: {body[:400]}")
|
|
27
|
-
print(f"[status] OK: {args.url} -> {status}")
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def cmd_text_contains(args: argparse.Namespace) -> None:
|
|
31
|
-
status, body = fetch("GET", args.url)
|
|
32
|
-
if status != args.expect:
|
|
33
|
-
raise CheckError(f"GET {args.url} expected {args.expect}, got {status}: {body[:400]}")
|
|
34
|
-
if args.contains not in body:
|
|
35
|
-
raise CheckError(f"Response from {args.url} does not contain expected text: {args.contains}")
|
|
36
|
-
print(f"[text-contains] OK: {args.url}")
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def cmd_json_field(args: argparse.Namespace) -> None:
|
|
40
|
-
status, body = fetch("GET", args.url, headers={"Accept": "application/json"})
|
|
41
|
-
if status != args.expect:
|
|
42
|
-
raise CheckError(f"GET {args.url} expected {args.expect}, got {status}: {body[:400]}")
|
|
43
|
-
try:
|
|
44
|
-
payload = json.loads(body)
|
|
45
|
-
except json.JSONDecodeError as exc:
|
|
46
|
-
raise CheckError(f"Response from {args.url} is not valid JSON: {exc}") from exc
|
|
47
|
-
|
|
48
|
-
current = payload
|
|
49
|
-
for part in args.path.split("."):
|
|
50
|
-
if isinstance(current, dict) and part in current:
|
|
51
|
-
current = current[part]
|
|
52
|
-
continue
|
|
53
|
-
raise CheckError(f"JSON path not found: {args.path}")
|
|
54
|
-
|
|
55
|
-
if str(current) != args.equals:
|
|
56
|
-
raise CheckError(f"JSON field {args.path} expected {args.equals}, got {current}")
|
|
57
|
-
print(f"[json-field] OK: {args.url} {args.path}={current}")
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
def main() -> int:
|
|
61
|
-
parser = argparse.ArgumentParser(prog="http_checks")
|
|
62
|
-
sub = parser.add_subparsers(dest="cmd", required=True)
|
|
63
|
-
|
|
64
|
-
s = sub.add_parser("status")
|
|
65
|
-
s.add_argument("url")
|
|
66
|
-
s.add_argument("--expect", type=int, default=200)
|
|
67
|
-
s.set_defaults(func=cmd_status)
|
|
68
|
-
|
|
69
|
-
s = sub.add_parser("text-contains")
|
|
70
|
-
s.add_argument("url")
|
|
71
|
-
s.add_argument("--contains", required=True)
|
|
72
|
-
s.add_argument("--expect", type=int, default=200)
|
|
73
|
-
s.set_defaults(func=cmd_text_contains)
|
|
74
|
-
|
|
75
|
-
s = sub.add_parser("json-field")
|
|
76
|
-
s.add_argument("url")
|
|
77
|
-
s.add_argument("--path", required=True)
|
|
78
|
-
s.add_argument("--equals", required=True)
|
|
79
|
-
s.add_argument("--expect", type=int, default=200)
|
|
80
|
-
s.set_defaults(func=cmd_json_field)
|
|
81
|
-
|
|
82
|
-
try:
|
|
83
|
-
args = parser.parse_args()
|
|
84
|
-
args.func(args)
|
|
85
|
-
return 0
|
|
86
|
-
except CheckError as exc:
|
|
87
|
-
print(f"[ERROR] {exc}")
|
|
88
|
-
return 2
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if __name__ == "__main__":
|
|
92
|
-
raise SystemExit(main())
|