vibe-coding-qa 0.5.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.
Files changed (58) hide show
  1. dashboard/__init__.py +10 -0
  2. dashboard/backend/__init__.py +11 -0
  3. dashboard/backend/__main__.py +6 -0
  4. dashboard/backend/ai/__init__.py +6 -0
  5. dashboard/backend/ai/provider.py +102 -0
  6. dashboard/backend/ai/service.py +757 -0
  7. dashboard/backend/api/__init__.py +5 -0
  8. dashboard/backend/api/ai.py +142 -0
  9. dashboard/backend/api/dashboard.py +34 -0
  10. dashboard/backend/api/deps.py +51 -0
  11. dashboard/backend/api/findings.py +102 -0
  12. dashboard/backend/api/intel.py +41 -0
  13. dashboard/backend/api/repositories.py +62 -0
  14. dashboard/backend/api/scans.py +168 -0
  15. dashboard/backend/api/system.py +69 -0
  16. dashboard/backend/config.py +180 -0
  17. dashboard/backend/main.py +183 -0
  18. dashboard/backend/persistence/__init__.py +5 -0
  19. dashboard/backend/persistence/database.py +278 -0
  20. dashboard/backend/persistence/store.py +600 -0
  21. dashboard/backend/profiles.py +141 -0
  22. dashboard/backend/schemas.py +452 -0
  23. dashboard/backend/security.py +158 -0
  24. dashboard/backend/services/__init__.py +7 -0
  25. dashboard/backend/services/dashboard.py +172 -0
  26. dashboard/backend/services/intel.py +341 -0
  27. dashboard/backend/services/paths.py +38 -0
  28. dashboard/backend/services/repositories.py +123 -0
  29. dashboard/backend/services/scans.py +328 -0
  30. dashboard/backend/services/settings.py +49 -0
  31. dashboard/frontend/dist/assets/index-COC-CdqD.js +492 -0
  32. dashboard/frontend/dist/assets/index-hWDLRaLr.css +1 -0
  33. dashboard/frontend/dist/index.html +29 -0
  34. dashboard/frontend/dist/phone-mascots.gif +0 -0
  35. dashboard/launcher.py +48 -0
  36. vibe_coding_qa/__init__.py +140 -0
  37. vibe_coding_qa/__main__.py +5 -0
  38. vibe_coding_qa/ai_review.py +165 -0
  39. vibe_coding_qa/cli.py +157 -0
  40. vibe_coding_qa/engine.py +860 -0
  41. vibe_coding_qa/fixes.py +222 -0
  42. vibe_coding_qa/gates.py +300 -0
  43. vibe_coding_qa/profile.py +471 -0
  44. vibe_coding_qa/repo.py +188 -0
  45. vibe_coding_qa/reporters/__init__.py +253 -0
  46. vibe_coding_qa/scanners/__init__.py +195 -0
  47. vibe_coding_qa/scanners/compliance.py +167 -0
  48. vibe_coding_qa/scanners/database.py +316 -0
  49. vibe_coding_qa/scanners/dependencies.py +595 -0
  50. vibe_coding_qa/scanners/infrastructure.py +125 -0
  51. vibe_coding_qa/scanners/performance.py +84 -0
  52. vibe_coding_qa/scanners/quality.py +129 -0
  53. vibe_coding_qa/scanners/security.py +521 -0
  54. vibe_coding_qa-0.5.0.dist-info/METADATA +323 -0
  55. vibe_coding_qa-0.5.0.dist-info/RECORD +58 -0
  56. vibe_coding_qa-0.5.0.dist-info/WHEEL +5 -0
  57. vibe_coding_qa-0.5.0.dist-info/entry_points.txt +3 -0
  58. vibe_coding_qa-0.5.0.dist-info/top_level.txt +2 -0
@@ -0,0 +1,860 @@
1
+ """Engine orchestrator: run scanners, tests, fixes, gates, and reports."""
2
+
3
+ import json
4
+ import os
5
+ import re
6
+ import shlex
7
+ import shutil
8
+ import subprocess
9
+ import tempfile
10
+ import threading
11
+ import time
12
+ import uuid
13
+ from pathlib import Path
14
+ from typing import Any, Callable
15
+
16
+ from . import ModuleResult, ScanResult, Severity, Status
17
+ from .ai_review import get_ai_provider, review_findings
18
+ from .fixes import FixEngine, suggest_fixes
19
+ from .gates import assess, release_decision
20
+ from .profile import analyze_repo
21
+ from .repo import cleanup_repo, load_repository, repo_metadata
22
+ from .scanners import redact_secrets
23
+ from .scanners.compliance import ComplianceScanner
24
+ from .scanners.database import DatabaseScanner
25
+ from .scanners.dependencies import DependencyScanner
26
+ from .scanners.infrastructure import InfrastructureScanner
27
+ from .scanners.performance import PerformanceScanner
28
+ from .scanners.quality import QualityScanner
29
+ from .scanners.security import SecurityScanner
30
+
31
+
32
+ SCANNERS = [
33
+ SecurityScanner,
34
+ QualityScanner,
35
+ DatabaseScanner,
36
+ InfrastructureScanner,
37
+ PerformanceScanner,
38
+ ComplianceScanner,
39
+ DependencyScanner,
40
+ ]
41
+
42
+
43
+ ProgressCallback = Callable[[str, str, str, dict], None]
44
+
45
+
46
+ def _emit(progress: "ProgressCallback | None", stage: str, status: str, message: str = "", data: dict | None = None) -> None:
47
+ """Report a real scan stage to an optional progress callback.
48
+
49
+ The observer is best-effort: a failing callback must never break a scan.
50
+ """
51
+ if progress is None:
52
+ return
53
+ try:
54
+ progress(stage, status, message, data or {})
55
+ except Exception:
56
+ pass
57
+
58
+
59
+ def run_scan(
60
+ repo: Path,
61
+ metadata: bool = False,
62
+ progress: "ProgressCallback | None" = None,
63
+ modules: list[str] | None = None,
64
+ minimum_score: int | None = None,
65
+ ) -> ScanResult:
66
+ _emit(progress, "preparation", "running", "Preparing repository for analysis")
67
+ selected = set(modules) if modules is not None else None
68
+ result_modules: list[ModuleResult] = []
69
+ for scanner_cls in SCANNERS:
70
+ if selected is not None and scanner_cls.module not in selected:
71
+ _emit(progress, "scanner", "skipped", f"{scanner_cls.module} analysis skipped by scan profile", {"module": scanner_cls.module})
72
+ continue
73
+ scanner = scanner_cls(repo)
74
+ _emit(progress, "scanner", "running", f"Running {scanner.module} analysis", {"module": scanner.module})
75
+ findings = scanner.scan()
76
+ result_modules.append(ModuleResult(
77
+ module=scanner.module,
78
+ status=Status.PASS,
79
+ score=100.0,
80
+ findings=findings,
81
+ notes=getattr(scanner, "notes", []),
82
+ ))
83
+ _emit(progress, "scanner", "completed", f"{scanner.module} analysis complete", {"module": scanner.module, "findings": len(findings)})
84
+ result = ScanResult(repo=repo, modules=result_modules)
85
+ assess(result, minimum_score=minimum_score)
86
+ if metadata:
87
+ _emit(progress, "technology", "running", "Detecting technology stack and architecture")
88
+ result.tests["repo_metadata"] = repo_metadata(repo)
89
+ result.tests["architecture"] = analyze_repo(repo).to_dict()
90
+ _emit(progress, "technology", "completed", "Technology detection complete")
91
+ return result
92
+
93
+
94
+ def _test_environment() -> dict[str, str]:
95
+ allowed = {
96
+ "PATH", "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT", "TEMP", "TMP", "TMPDIR",
97
+ "HOME", "USERPROFILE", "HOMEDRIVE", "HOMEPATH", "APPDATA", "LOCALAPPDATA",
98
+ "LANG", "LC_ALL", "LC_CTYPE", "PYTHONPATH", "PYTHONHOME", "VIRTUAL_ENV",
99
+ "NPM_CONFIG_CACHE", "CI", "NODE_ENV",
100
+ }
101
+ return {key: value for key, value in os.environ.items() if key.upper() in allowed}
102
+
103
+
104
+
105
+ def _run_bounded_process(
106
+ command: list[str],
107
+ *,
108
+ cwd: Path,
109
+ env: dict[str, str],
110
+ timeout: int,
111
+ max_output_bytes: int = 1_000_000,
112
+ ) -> tuple[subprocess.CompletedProcess, bool, bool]:
113
+ process = subprocess.Popen(
114
+ command,
115
+ cwd=str(cwd),
116
+ stdout=subprocess.PIPE,
117
+ stderr=subprocess.PIPE,
118
+ env=env,
119
+ )
120
+ captured = {"stdout": bytearray(), "stderr": bytearray()}
121
+ output_lock = threading.Lock()
122
+ output_exceeded = threading.Event()
123
+
124
+ def drain(name: str, stream) -> None:
125
+ while True:
126
+ chunk = stream.read(8192)
127
+ if not chunk:
128
+ return
129
+ with output_lock:
130
+ remaining = max_output_bytes - len(captured["stdout"]) - len(captured["stderr"])
131
+ if remaining > 0:
132
+ captured[name].extend(chunk[:remaining])
133
+ if len(chunk) > remaining:
134
+ output_exceeded.set()
135
+ if output_exceeded.is_set():
136
+ try:
137
+ process.kill()
138
+ except OSError:
139
+ pass
140
+ return
141
+
142
+ readers = [
143
+ threading.Thread(target=drain, args=("stdout", process.stdout), daemon=True),
144
+ threading.Thread(target=drain, args=("stderr", process.stderr), daemon=True),
145
+ ]
146
+ for reader in readers:
147
+ reader.start()
148
+ timed_out = False
149
+ try:
150
+ process.wait(timeout=timeout)
151
+ except subprocess.TimeoutExpired:
152
+ timed_out = True
153
+ try:
154
+ process.kill()
155
+ except OSError:
156
+ pass
157
+ process.wait()
158
+ for reader in readers:
159
+ reader.join()
160
+ for stream in (process.stdout, process.stderr):
161
+ if stream and not stream.closed:
162
+ stream.close()
163
+ stdout = bytes(captured["stdout"]).decode("utf-8", errors="replace")
164
+ stderr = bytes(captured["stderr"]).decode("utf-8", errors="replace")
165
+ completed = subprocess.CompletedProcess(command, process.returncode, stdout, stderr)
166
+ return completed, timed_out, output_exceeded.is_set()
167
+
168
+
169
+ def _copy_test_snapshot(repo: Path, max_bytes: int = 256 * 1024 * 1024) -> tuple[Path, Path]:
170
+ source = repo.resolve()
171
+ temporary_root = Path(tempfile.mkdtemp(prefix="vcqa-test-snapshot-"))
172
+ snapshot = temporary_root / "repo"
173
+ snapshot.mkdir()
174
+ excluded_dirs = {".git", "node_modules", ".venv", "venv", "__pycache__", ".tox", ".pytest_cache", ".aws", ".ssh", ".gnupg"}
175
+ excluded_files = {".npmrc", ".pypirc", ".netrc", "id_rsa", "id_ed25519", "id_dsa", "id_ecdsa", "credentials.json", "service-account.json"}
176
+ excluded_extensions = {".pem", ".key", ".p12", ".pfx", ".crt", ".cer"}
177
+ total_bytes = 0
178
+ for current, directories, files in os.walk(source, topdown=True, followlinks=False):
179
+ current_path = Path(current)
180
+ directories[:] = [
181
+ name for name in directories
182
+ if name not in excluded_dirs and not (current_path / name).is_symlink()
183
+ ]
184
+ relative_dir = current_path.relative_to(source)
185
+ target_dir = snapshot / relative_dir
186
+ target_dir.mkdir(parents=True, exist_ok=True)
187
+ # The container is unprivileged (--cap-drop ALL) and cannot override file
188
+ # permissions, so ensure the snapshot stays traversable/readable even when
189
+ # the host umask is restrictive.
190
+ try:
191
+ os.chmod(target_dir, 0o755)
192
+ except OSError:
193
+ pass
194
+ for name in files:
195
+ source_file = current_path / name
196
+ lowered = name.lower()
197
+ if source_file.is_symlink() or lowered in excluded_files:
198
+ continue
199
+ if lowered.startswith(".env") and lowered != ".env.example":
200
+ continue
201
+ if source_file.suffix.lower() in excluded_extensions:
202
+ continue
203
+ try:
204
+ source_file.resolve().relative_to(source)
205
+ except ValueError:
206
+ continue
207
+ try:
208
+ remaining = max_bytes - total_bytes
209
+ with source_file.open("rb") as stream:
210
+ content = stream.read(remaining + 1)
211
+ if len(content) > remaining:
212
+ shutil.rmtree(temporary_root, ignore_errors=True)
213
+ raise ValueError(f"Test snapshot exceeds the {max_bytes} byte limit.")
214
+ total_bytes += len(content)
215
+ try:
216
+ text = content.decode("utf-8")
217
+ if "\0" in text:
218
+ continue
219
+ target_file = target_dir / name
220
+ target_file.write_text(redact_secrets(text), encoding="utf-8")
221
+ try:
222
+ os.chmod(target_file, 0o644)
223
+ except OSError:
224
+ pass
225
+ except UnicodeDecodeError:
226
+ continue
227
+ except OSError:
228
+ continue
229
+ return temporary_root, snapshot
230
+
231
+
232
+ def _test_image(command: list[str], configured: str | None) -> str:
233
+ image = configured or os.environ.get("VCQA_TEST_CONTAINER_IMAGE")
234
+ if not image:
235
+ executable = Path(command[0]).name.lower() if command else ""
236
+ image = "node:22-bookworm-slim" if executable in {"npm", "npx", "pnpm", "yarn", "bun"} else "python:3.12-slim"
237
+ if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._/:@+-]*", image):
238
+ raise ValueError("Test container image contains unsupported characters.")
239
+ return image
240
+
241
+
242
+ def _empty_counts() -> dict[str, int | None]:
243
+ return {"discovered": None, "executed": None, "passed": None, "failed": None, "skipped": None, "errors": None}
244
+
245
+
246
+ def _normalized_counts(discovered, passed, failed, skipped, errors) -> dict[str, int]:
247
+ passed = int(passed or 0)
248
+ failed = int(failed or 0)
249
+ skipped = int(skipped or 0)
250
+ errors = int(errors or 0)
251
+ executed = passed + failed + errors
252
+ discovered = int(discovered if discovered is not None else executed + skipped)
253
+ if discovered < executed + skipped:
254
+ discovered = executed + skipped
255
+ return {
256
+ "discovered": discovered,
257
+ "executed": executed,
258
+ "passed": passed,
259
+ "failed": failed,
260
+ "skipped": skipped,
261
+ "errors": errors,
262
+ }
263
+
264
+
265
+ def _parse_unittest_output(output: str) -> dict[str, int] | None:
266
+ match = re.search(r"Ran (\d+) tests? in", output)
267
+ if not match:
268
+ return None
269
+ discovered = int(match.group(1))
270
+ failed = errors = skipped = 0
271
+ failed_header = re.search(r"FAILED \(([^)]*)\)", output)
272
+ if failed_header:
273
+ body = failed_header.group(1)
274
+ failure_match = re.search(r"failures=(\d+)", body)
275
+ error_match = re.search(r"errors=(\d+)", body)
276
+ skip_match = re.search(r"skipped=(\d+)", body)
277
+ failed = int(failure_match.group(1)) if failure_match else 0
278
+ errors = int(error_match.group(1)) if error_match else 0
279
+ skipped = int(skip_match.group(1)) if skip_match else 0
280
+ else:
281
+ ok_match = re.search(r"^OK(?: \(([^)]*)\))?", output, re.MULTILINE)
282
+ if ok_match and ok_match.group(1):
283
+ skip_match = re.search(r"skipped=(\d+)", ok_match.group(1))
284
+ if skip_match:
285
+ skipped = int(skip_match.group(1))
286
+ passed = max(0, discovered - skipped - failed - errors)
287
+ return _normalized_counts(discovered, passed, failed, skipped, errors)
288
+
289
+
290
+ def _parse_pytest_output(output: str) -> dict[str, int] | None:
291
+ # Require a pytest-specific signal so Jest/Vitest summaries do not match.
292
+ no_tests = re.search(r"no tests ran in \d+\.\d+s", output, re.IGNORECASE)
293
+ collected = re.search(r"collected (\d+) item", output)
294
+ summary = re.search(
295
+ r"(?m)^[=\s]*(?:\d+ (?:passed|failed|errors?|skipped|xfailed|xpassed|warnings?|deselected)[,\s]*)+in \d+\.\d+s",
296
+ output,
297
+ )
298
+ if not (no_tests or collected or summary):
299
+ return None
300
+
301
+ def grab(label: str) -> int:
302
+ found = re.search(r"(\d+) " + label + r"\b", output)
303
+ return int(found.group(1)) if found else 0
304
+
305
+ passed = grab("passed")
306
+ failed = grab("failed")
307
+ skipped = grab("skipped")
308
+ errors = grab("errors") or grab("error")
309
+ discovered = passed + failed + errors + skipped
310
+ if collected:
311
+ discovered = max(discovered, int(collected.group(1)))
312
+ return _normalized_counts(discovered, passed, failed, skipped, errors)
313
+
314
+
315
+ def _parse_jest_output(output: str) -> dict[str, int] | None:
316
+ match = re.search(r"^Tests:\s*(.+)$", output, re.MULTILINE)
317
+ if not match:
318
+ return None
319
+ line = match.group(1)
320
+
321
+ def grab(label: str) -> int:
322
+ found = re.search(r"(\d+) " + label + r"\b", line)
323
+ return int(found.group(1)) if found else 0
324
+
325
+ failed = grab("failed")
326
+ passed = grab("passed")
327
+ skipped = grab("skipped")
328
+ todo = grab("todo")
329
+ total_match = re.search(r"(\d+) total", line)
330
+ discovered = int(total_match.group(1)) if total_match else passed + failed + skipped + todo
331
+ return _normalized_counts(discovered, passed, failed, skipped, 0)
332
+
333
+
334
+ def _parse_vitest_output(output: str) -> dict[str, int] | None:
335
+ match = re.search(r"^\s*Tests\s+(.+)$", output, re.MULTILINE)
336
+ if not match:
337
+ return None
338
+ line = match.group(1)
339
+
340
+ def grab(label: str) -> int:
341
+ found = re.search(r"(\d+) " + label + r"\b", line)
342
+ return int(found.group(1)) if found else 0
343
+
344
+ failed = grab("failed")
345
+ passed = grab("passed")
346
+ skipped = grab("skipped")
347
+ if not (failed or passed or skipped):
348
+ return None
349
+ total_match = re.search(r"\((\d+)\)", line)
350
+ discovered = int(total_match.group(1)) if total_match else passed + failed + skipped
351
+ return _normalized_counts(discovered, passed, failed, skipped, 0)
352
+
353
+
354
+ _TEST_OUTPUT_PARSERS = (
355
+ ("unittest", _parse_unittest_output),
356
+ ("pytest", _parse_pytest_output),
357
+ ("jest", _parse_jest_output),
358
+ ("vitest", _parse_vitest_output),
359
+ )
360
+
361
+
362
+ def classify_test_results(output: str, returncode: int) -> dict:
363
+ """Classify a test run from its output and exit code.
364
+
365
+ Returns a normalized structure with ``status``, ``reason``, ``runner`` and
366
+ ``counts``. A zero exit code alone never yields PASS: verified results must
367
+ be recognised from the runner output.
368
+ """
369
+ for runner, parser in _TEST_OUTPUT_PARSERS:
370
+ counts = parser(output)
371
+ if counts is None:
372
+ continue
373
+ failed = counts["failed"] or 0
374
+ errors = counts["errors"] or 0
375
+ discovered = counts["discovered"] or 0
376
+ executed = counts["executed"] or 0
377
+ if failed or errors:
378
+ return {"status": Status.FAIL, "reason": "tests_failed", "runner": runner, "counts": counts}
379
+ if discovered == 0:
380
+ return {"status": Status.SKIP, "reason": "zero_tests_discovered", "runner": runner, "counts": counts}
381
+ if executed == 0:
382
+ return {"status": Status.SKIP, "reason": "all_tests_skipped", "runner": runner, "counts": counts}
383
+ if returncode != 0:
384
+ return {"status": Status.FAIL, "reason": "non_zero_exit", "runner": runner, "counts": counts}
385
+ return {"status": Status.PASS, "reason": "tests_passed", "runner": runner, "counts": counts}
386
+ return {"status": Status.SKIP, "reason": "no_recognizable_results", "runner": None, "counts": _empty_counts()}
387
+
388
+
389
+
390
+ def run_tests(
391
+ repo: Path,
392
+ command: str | None = None,
393
+ timeout: int = 300,
394
+ no_execution: bool = False,
395
+ explicit_permission: bool = False,
396
+ max_memory_mb: int = 512,
397
+ max_cpu_seconds: int = 60,
398
+ test_image: str | None = None,
399
+ max_snapshot_mb: int = 256,
400
+ max_write_mb: int = 64,
401
+ ) -> dict:
402
+ """
403
+ Run tests with security isolation.
404
+
405
+ Args:
406
+ repo: Repository path
407
+ command: Test command to run (uses shlex.split for proper quoting)
408
+ timeout: Wall-clock timeout in seconds
409
+ no_execution: If True, skip execution entirely
410
+ explicit_permission: Must be True to actually run tests (safety gate)
411
+ max_memory_mb: Memory limit in MB (enforced where the platform supports resource limits)
412
+ max_cpu_seconds: CPU time limit in seconds (enforced where the platform supports resource limits)
413
+
414
+ Returns:
415
+ Dict with test results including status, command, summary, etc.
416
+ """
417
+ if no_execution:
418
+ return {
419
+ "status": Status.SKIP,
420
+ "command": None,
421
+ "summary": "Test execution disabled by --no-execution.",
422
+ "duration_ms": 0,
423
+ "execution_mode": "disabled",
424
+ }
425
+
426
+ # Safety gate: require explicit permission to run tests
427
+ if not explicit_permission:
428
+ return {
429
+ "status": Status.SKIP,
430
+ "command": command,
431
+ "summary": "Test execution requires explicit permission (--explicit-permission). Tests were not run.",
432
+ "duration_ms": 0,
433
+ "execution_mode": "blocked_no_permission",
434
+ }
435
+
436
+ if timeout <= 0 or max_memory_mb <= 0 or max_cpu_seconds <= 0 or max_snapshot_mb <= 0 or max_write_mb < 2:
437
+ return {
438
+ "status": Status.SKIP,
439
+ "command": command,
440
+ "summary": "Timeout, memory, CPU and snapshot limits must be positive; writable storage must be at least 2 MB.",
441
+ "duration_ms": 0,
442
+ "execution_mode": "invalid_limits",
443
+ }
444
+
445
+ # Destructive terms that should never be run automatically
446
+ destructive_terms = ("rm -rf", "rm -fr", "rm -r", "kubectl delete", "drop", "deploy", "serverless deploy", "sls deploy")
447
+
448
+ if command:
449
+ # Use shlex.split to properly handle shell-style quoting
450
+ try:
451
+ cmd = shlex.split(command)
452
+ except ValueError as exc:
453
+ return {
454
+ "status": Status.SKIP,
455
+ "command": command,
456
+ "summary": f"Invalid command syntax: {exc}",
457
+ "duration_ms": 0,
458
+ "execution_mode": "error",
459
+ }
460
+ lowered = command.lower()
461
+ if any(term in lowered for term in destructive_terms):
462
+ return {
463
+ "status": Status.SKIP,
464
+ "command": command,
465
+ "summary": f"Refusing to run potentially destructive command: {command}",
466
+ "duration_ms": 0,
467
+ "execution_mode": "blocked_destructive",
468
+ }
469
+ else:
470
+ cmd = _detect_test_command(repo)
471
+ if cmd is None:
472
+ return {
473
+ "status": Status.SKIP,
474
+ "command": None,
475
+ "summary": "No test runner detected.",
476
+ "duration_ms": 0,
477
+ "execution_mode": "no_runner",
478
+ }
479
+
480
+ env = _test_environment()
481
+ env["CI"] = "true"
482
+ env["NODE_ENV"] = "test"
483
+ if timeout <= 0 or max_memory_mb <= 0 or max_cpu_seconds <= 0:
484
+ return {
485
+ "status": Status.SKIP,
486
+ "command": command,
487
+ "summary": "Timeout and resource limits must be positive values.",
488
+ "duration_ms": 0,
489
+ "execution_mode": "error",
490
+ }
491
+
492
+ command_str = redact_secrets(" ".join(shlex.quote(c) for c in cmd))
493
+ if any(term in command_str.lower() for term in destructive_terms):
494
+ return {
495
+ "status": Status.SKIP,
496
+ "command": command_str,
497
+ "summary": f"Refusing to run potentially destructive command: {command_str}",
498
+ "duration_ms": 0,
499
+ "execution_mode": "blocked_destructive",
500
+ }
501
+
502
+ try:
503
+ image = _test_image(cmd, test_image)
504
+ except ValueError as exc:
505
+ return {
506
+ "status": Status.SKIP,
507
+ "command": command_str,
508
+ "summary": str(exc),
509
+ "duration_ms": 0,
510
+ "execution_mode": "error",
511
+ }
512
+ try:
513
+ docker = shutil.which("docker")
514
+ if not docker:
515
+ raise FileNotFoundError("Docker CLI is not installed.")
516
+ availability = subprocess.run(
517
+ [docker, "info", "--format", "{{.ServerVersion}}"],
518
+ capture_output=True,
519
+ text=True,
520
+ timeout=10,
521
+ env=env,
522
+ check=False,
523
+ )
524
+ if availability.returncode != 0:
525
+ return {
526
+ "status": Status.SKIP,
527
+ "command": command_str,
528
+ "summary": "Isolated test execution requires an available Docker daemon; tests were not run.",
529
+ "duration_ms": 0,
530
+ "execution_mode": "sandbox_unavailable",
531
+ }
532
+ image_check = subprocess.run(
533
+ [docker, "image", "inspect", image],
534
+ capture_output=True,
535
+ text=True,
536
+ timeout=10,
537
+ env=env,
538
+ check=False,
539
+ )
540
+ if image_check.returncode != 0:
541
+ return {
542
+ "status": Status.SKIP,
543
+ "command": command_str,
544
+ "summary": f"Configured test image '{image}' is not available locally; no image was pulled and tests were not run.",
545
+ "duration_ms": 0,
546
+ "execution_mode": "sandbox_image_unavailable",
547
+ }
548
+ except (OSError, subprocess.TimeoutExpired):
549
+ return {
550
+ "status": Status.SKIP,
551
+ "command": command_str,
552
+ "summary": "Could not verify an isolated Docker runtime; tests were not run.",
553
+ "duration_ms": 0,
554
+ "execution_mode": "sandbox_unavailable",
555
+ }
556
+
557
+ try:
558
+ temporary_root, snapshot = _copy_test_snapshot(repo, max_bytes=max_snapshot_mb * 1024 * 1024)
559
+ except (OSError, ValueError) as exc:
560
+ return {
561
+ "status": Status.SKIP,
562
+ "command": command_str,
563
+ "summary": f"Could not prepare bounded test snapshot: {type(exc).__name__}.",
564
+ "duration_ms": 0,
565
+ "execution_mode": "snapshot_unavailable",
566
+ }
567
+ container_name = f"vcqa-test-{uuid.uuid4().hex[:12]}"
568
+ resource_info = {
569
+ "memory_mb": max_memory_mb,
570
+ "cpu_seconds": max_cpu_seconds,
571
+ "enforced": True,
572
+ "environment_allowlisted": True,
573
+ "isolation": "docker-network-disabled-read-only-rootfs",
574
+ "snapshot_excludes_secrets": False,
575
+ "snapshot_max_mb": max_snapshot_mb,
576
+ "tmp_and_shm_max_mb": max_write_mb,
577
+ "workspace": "tmpfs-read-write",
578
+ "source_mount": "read-only",
579
+ "workspace_max_mb": max_write_mb,
580
+ }
581
+ # The original repository is never mutated: a secret-filtered snapshot is
582
+ # mounted read-only and copied into a size-bounded writable tmpfs workspace
583
+ # before the test command runs, so the suite may create temp files and build
584
+ # artifacts without touching the source or the read-only root filesystem.
585
+ #
586
+ # The copy must NOT preserve ownership. The container runs with --cap-drop ALL,
587
+ # so it holds no CAP_CHOWN: ``cp -a``/``cp -p`` would try to chown the copies to
588
+ # the snapshot owner and fail with EPERM, aborting under ``set -e``. ``cp -R``
589
+ # copies contents and permissions without preserving ownership.
590
+ inner_command = (
591
+ "set -e; "
592
+ "cp -R /vcqa-src/. /workspace/; "
593
+ "cd /workspace; "
594
+ "exec " + " ".join(shlex.quote(part) for part in cmd)
595
+ )
596
+ docker_command = [
597
+ docker, "run", "--pull=never", "--rm", "--name", container_name,
598
+ "--network", "none",
599
+ "--memory", f"{max_memory_mb}m",
600
+ "--cpus", "1",
601
+ "--pids-limit", "128",
602
+ "--ulimit", f"cpu={max_cpu_seconds}:{max_cpu_seconds + 1}",
603
+ "--security-opt", "no-new-privileges",
604
+ "--cap-drop", "ALL",
605
+ "--read-only",
606
+ "--tmpfs", f"/tmp:rw,nosuid,nodev,size={max_write_mb - 1}m",
607
+ "--tmpfs", f"/workspace:rw,nosuid,nodev,size={max_write_mb}m",
608
+ "--shm-size", "1m",
609
+ "--mount", f"type=bind,source={snapshot},target=/vcqa-src,readonly",
610
+ "--workdir", "/workspace",
611
+ "--env", "CI=true",
612
+ "--env", "NODE_ENV=test",
613
+ "--env", "HOME=/tmp",
614
+ image,
615
+ "sh", "-c", inner_command,
616
+ ]
617
+ start = time.time()
618
+ try:
619
+ result, timed_out, output_exceeded = _run_bounded_process(
620
+ docker_command,
621
+ cwd=snapshot,
622
+ timeout=timeout,
623
+ env=env,
624
+ )
625
+ duration_ms = int((time.time() - start) * 1000)
626
+ if timed_out:
627
+ return {
628
+ "status": Status.FAIL,
629
+ "command": command_str,
630
+ "summary": f"Test command timed out after {timeout}s.",
631
+ "duration_ms": duration_ms,
632
+ "execution_mode": "timeout",
633
+ "resource_limits": resource_info,
634
+ }
635
+ output = result.stdout
636
+ if result.stderr:
637
+ output += "\n" + result.stderr
638
+ output = redact_secrets(output)
639
+ summary = "Test output exceeded 1,000,000 bytes and was terminated." if output_exceeded else (output[-2000:] if len(output) > 2000 else output)
640
+
641
+ missing_infrastructure = bool(re.search(
642
+ r"No module named ['\"]?[\w.]+['\"]?|Cannot find module ['\"][^'\"]+['\"]|(?:pytest|vitest|jest): (?:not found|command not found)|executable file not found in \$PATH",
643
+ output, re.IGNORECASE,
644
+ )) or result.returncode == 127
645
+ real_failures = bool(re.search(r"FAILED \(failures=|\b\d+ failed\b|\b\d+ failing\b", output, re.IGNORECASE))
646
+ space_exceeded = bool(re.search(r"No space left on device|Disk quota exceeded", output, re.IGNORECASE))
647
+
648
+ if output_exceeded:
649
+ classification = {"status": Status.FAIL, "reason": "output_limit_exceeded", "runner": None, "counts": _empty_counts()}
650
+ execution_mode = "docker_isolated"
651
+ elif space_exceeded and not real_failures:
652
+ classification = {"status": Status.FAIL, "reason": "workspace_size_exceeded", "runner": None, "counts": _empty_counts()}
653
+ execution_mode = "workspace_size_exceeded"
654
+ summary = "The bounded writable test workspace ran out of disk space. " + summary
655
+ elif missing_infrastructure and not real_failures:
656
+ classification = {"status": Status.SKIP, "reason": "test_infrastructure_unavailable", "runner": None, "counts": _empty_counts()}
657
+ execution_mode = "test_infrastructure_unavailable"
658
+ summary = "Test infrastructure appears unavailable. Use a locally prepared --test-image with the necessary dependencies. " + summary
659
+ else:
660
+ classification = classify_test_results(output, result.returncode)
661
+ execution_mode = "docker_isolated"
662
+
663
+ counts = classification["counts"]
664
+ return {
665
+ "status": classification["status"],
666
+ "test_reason": classification["reason"],
667
+ "test_runner": classification["runner"],
668
+ "tests_discovered": counts["discovered"],
669
+ "tests_executed": counts["executed"],
670
+ "tests_passed": counts["passed"],
671
+ "tests_failed": counts["failed"],
672
+ "tests_skipped": counts["skipped"],
673
+ "tests_errors": counts["errors"],
674
+ "command": command_str,
675
+ "returncode": result.returncode,
676
+ "summary": summary,
677
+ "stdout": redact_secrets(result.stdout),
678
+ "stderr": redact_secrets(result.stderr),
679
+ "duration_ms": duration_ms,
680
+ "skipped": counts["skipped"],
681
+ "output_truncated": output_exceeded,
682
+ "execution_mode": execution_mode,
683
+ "resource_limits": resource_info,
684
+ "warning": "Tests ran in a restricted Docker container using a filtered snapshot. Redaction is pattern-based and cannot guarantee removal of all secrets; container isolation is not a proof against runtime vulnerabilities.",
685
+ }
686
+ except FileNotFoundError:
687
+ return {
688
+ "status": Status.SKIP,
689
+ "command": command_str,
690
+ "summary": "Docker CLI became unavailable; tests were not run.",
691
+ "duration_ms": int((time.time() - start) * 1000),
692
+ "execution_mode": "sandbox_unavailable",
693
+ }
694
+ except subprocess.TimeoutExpired:
695
+ return {
696
+ "status": Status.FAIL,
697
+ "command": command_str,
698
+ "summary": f"Test command timed out after {timeout}s.",
699
+ "duration_ms": int((time.time() - start) * 1000),
700
+ "execution_mode": "timeout",
701
+ "resource_limits": resource_info,
702
+ }
703
+ finally:
704
+ try:
705
+ subprocess.run(
706
+ [docker, "rm", "--force", container_name],
707
+ capture_output=True,
708
+ text=True,
709
+ timeout=10,
710
+ env=env,
711
+ check=False,
712
+ )
713
+ except (OSError, subprocess.TimeoutExpired):
714
+ pass
715
+ shutil.rmtree(temporary_root, ignore_errors=True)
716
+
717
+
718
+ def _detect_test_command(repo: Path) -> list[str] | None:
719
+ profile = analyze_repo(repo)
720
+ pkg = repo / "package.json"
721
+ if pkg.is_file():
722
+ try:
723
+ data = json.loads(pkg.read_text(encoding="utf-8", errors="ignore"))
724
+ scripts = data.get("scripts", {})
725
+ for key in ("test", "tests", "test:ci", "test:unit", "test:lib"):
726
+ if key in scripts:
727
+ runner = {
728
+ "pnpm": "pnpm",
729
+ "yarn": "yarn",
730
+ "npm": "npm",
731
+ "bun": "bun",
732
+ }.get(profile.package_manager, "npm")
733
+ return [runner, "run", key]
734
+ except Exception:
735
+ pass
736
+ if profile.test_runners:
737
+ if "pytest" in profile.test_runners:
738
+ return ["python", "-m", "pytest"]
739
+ if "unittest" in profile.test_runners:
740
+ test_dir = "tests" if (repo / "tests").is_dir() else "."
741
+ return ["python", "-m", "unittest", "discover", "-s", test_dir]
742
+ return None
743
+
744
+
745
+ def run_pipeline(
746
+ source: str,
747
+ *,
748
+ output_dir: Path | str | None = None,
749
+ run_tests_flag: bool = True,
750
+ no_execution: bool = False,
751
+ explicit_permission: bool = False,
752
+ apply_fixes: bool = False,
753
+ dry_run_fixes: bool = True,
754
+ github_token: str | None = None,
755
+ test_command: str | None = None,
756
+ test_timeout: int = 300,
757
+ max_memory_mb: int = 512,
758
+ max_cpu_seconds: int = 60,
759
+ test_image: str | None = None,
760
+ max_snapshot_mb: int = 256,
761
+ max_write_mb: int = 64,
762
+ keep_clone: bool = False,
763
+ progress: "ProgressCallback | None" = None,
764
+ modules: list[str] | None = None,
765
+ minimum_score: int | None = None,
766
+ ai_review_enabled: bool | None = None,
767
+ ) -> ScanResult:
768
+ workdir: Path | None = None
769
+ try:
770
+ _emit(progress, "preparation", "running", "Loading repository")
771
+ workdir = load_repository(source, token=github_token)
772
+ _emit(progress, "preparation", "completed", "Repository ready")
773
+ result = run_scan(
774
+ workdir, metadata=True, progress=progress, modules=modules,
775
+ minimum_score=minimum_score,
776
+ )
777
+
778
+ fix_engine: FixEngine | None = None
779
+ if dry_run_fixes or apply_fixes:
780
+ fix_engine = FixEngine(workdir, dry_run=not apply_fixes)
781
+ fix_engine.apply(result.findings)
782
+ if apply_fixes and fix_engine.applied:
783
+ # Re-scan the modified repository; verification below compares this
784
+ # fresh result against the pre-fix findings.
785
+ _emit(progress, "fixes", "running", "Re-scanning after applying fixes")
786
+ result = run_scan(
787
+ workdir, metadata=True, progress=progress, modules=modules,
788
+ minimum_score=minimum_score,
789
+ )
790
+
791
+ _emit(progress, "ai", "running", "AI review (optional)")
792
+ ai_reviews = (
793
+ review_findings(workdir, result.findings, provider=get_ai_provider())
794
+ if ai_review_enabled is not False
795
+ else []
796
+ )
797
+ if ai_reviews:
798
+ result.tests["ai_reviews"] = ai_reviews
799
+ _emit(progress, "ai", "completed", f"AI review produced {len(ai_reviews)} review(s)")
800
+ else:
801
+ _emit(progress, "ai", "skipped", "AI review not configured or not applicable")
802
+
803
+ if run_tests_flag:
804
+ _emit(progress, "tests", "running", "Running tests in the isolated sandbox")
805
+ result.tests.update(run_tests(
806
+ workdir,
807
+
808
+ command=test_command,
809
+ timeout=test_timeout,
810
+ max_memory_mb=max_memory_mb,
811
+ max_cpu_seconds=max_cpu_seconds,
812
+ test_image=test_image,
813
+ max_snapshot_mb=max_snapshot_mb,
814
+ max_write_mb=max_write_mb,
815
+ no_execution=no_execution,
816
+ explicit_permission=explicit_permission,
817
+ ))
818
+ _emit(
819
+ progress, "tests", "completed", "Test execution finished",
820
+ {"status": str(result.tests.get("status", ""))},
821
+ )
822
+
823
+ if fix_engine is not None:
824
+ result.tests["fixes"] = {
825
+ "applied": fix_engine.applied,
826
+ "skipped": fix_engine.skipped,
827
+ "dry_run": fix_engine.dry_run,
828
+ # Verification requires both an applied change and a post-fix scan.
829
+ "verification": (
830
+ fix_engine.verify(result.findings, tests=result.tests)
831
+ if (apply_fixes and fix_engine.applied)
832
+ else None
833
+ ),
834
+ }
835
+
836
+ # Re-assess after potential fixes/tests.
837
+ _emit(progress, "gate", "running", "Assessing release gate")
838
+ assessment = assess(result, minimum_score=minimum_score)
839
+ result._assessment = assessment
840
+
841
+ if output_dir:
842
+ _emit(progress, "report", "running", "Writing reports")
843
+ from .reporters import write_reports
844
+ write_reports(result, Path(output_dir))
845
+
846
+ _emit(progress, "gate", "completed", f"Release gate: {assessment.get('gate', 'unknown')}", {"gate": assessment.get("gate")})
847
+ return result
848
+ finally:
849
+ if workdir and not keep_clone and str(workdir) != str(Path(source).resolve()):
850
+ cleanup_repo(workdir)
851
+
852
+
853
+ def critical_summary(result: ScanResult) -> str:
854
+ parts = [
855
+ f"Overall score: {result.overall_score}",
856
+ f"Critical: {result.critical_count}",
857
+ f"High: {result.high_count}",
858
+ release_decision(result),
859
+ ]
860
+ return " | ".join(parts)