code-intelligence 0.0.0-stage → 0.1.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.
@@ -0,0 +1,135 @@
1
+ import { copyFileSync, existsSync, linkSync, mkdirSync, mkdtempSync, rmSync, statSync, } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { dirname, join } from "node:path";
4
+ import { exec } from "../core/exec.js";
5
+ import { hashPaths } from "../core/hashing.js";
6
+ import { isPlainFileInside } from "../core/safefs.js";
7
+ import { isLockfile } from "./engine.js";
8
+ import { detectBinary, parseJson, processFailure, toRel } from "./util.js";
9
+ const MAX_FILE_BYTES = 5 * 1024 * 1024;
10
+ const REMEDIATION = "Revoke and rotate this credential now, remove it from the code and git history, and load it from a secret manager or environment variable.";
11
+ /** Stage only the files in scope into a temp dir (hardlink, copy as fallback) so gitleaks scans exactly them. */
12
+ function stage(root, files) {
13
+ const dir = mkdtempSync(join(tmpdir(), "code-intel-gl-"));
14
+ let skippedLarge = 0;
15
+ for (const f of files) {
16
+ const src = join(root, f);
17
+ try {
18
+ if (statSync(src).size > MAX_FILE_BYTES) {
19
+ skippedLarge += 1;
20
+ continue;
21
+ }
22
+ const dst = join(dir, f);
23
+ mkdirSync(dirname(dst), { recursive: true });
24
+ try {
25
+ linkSync(src, dst);
26
+ }
27
+ catch {
28
+ copyFileSync(src, dst);
29
+ }
30
+ }
31
+ catch {
32
+ /* file vanished between listing and scan */
33
+ }
34
+ }
35
+ return { dir, skippedLarge };
36
+ }
37
+ export const gitleaks = {
38
+ name: "gitleaks",
39
+ category: "secrets",
40
+ priority: 0,
41
+ binaryName: "gitleaks",
42
+ installHint: "Install: brew install gitleaks (or see https://github.com/gitleaks/gitleaks#installing)",
43
+ versionArgs: ["version"],
44
+ detect: (opts) => detectBinary("gitleaks", gitleaks.versionArgs, gitleaks.installHint, opts?.withVersion ?? true),
45
+ select(target) {
46
+ const files = target.files.filter((f) => !isLockfile(f));
47
+ return files.length > 0
48
+ ? { files, skipReason: null }
49
+ : { files: null, skipReason: "no scannable files in scope" };
50
+ },
51
+ cacheable: () => true,
52
+ cacheInputs: (c, root) => [
53
+ c.engines.gitleaks.configPath ?? "",
54
+ String(c.repoConfigIgnored),
55
+ // Results depend on the contents of the repository's gitleaks config and ignore file, not just their names.
56
+ hashPaths(root, [c.engines.gitleaks.configPath ?? ".gitleaks.toml", ".gitleaksignore"]),
57
+ ],
58
+ async run(ctx) {
59
+ // gitleaks auto-discovers <scanned dir>/.gitleaks.toml, so with --ignore-repo-config the repository's own
60
+ // gitleaks files must not be staged into the directory it scans.
61
+ const files = ctx.config.repoConfigIgnored
62
+ ? ctx.files.filter((f) => f !== ".gitleaks.toml" && f !== ".gitleaksignore")
63
+ : ctx.files;
64
+ if (files.length === 0)
65
+ return { findings: [], warnings: [] };
66
+ const { dir, skippedLarge } = stage(ctx.root, files);
67
+ try {
68
+ // Scan "." with the staging directory as the working directory: gitleaks then reports repo-relative
69
+ // paths and fingerprints (creds.txt:rule:line), exactly like a manual run, so a user's own
70
+ // .gitleaksignore entries match.
71
+ const args = [
72
+ "dir",
73
+ ".",
74
+ "--no-banner",
75
+ "--no-color",
76
+ "--redact",
77
+ "--exit-code",
78
+ "0",
79
+ "--report-format",
80
+ "json",
81
+ "--report-path",
82
+ "-",
83
+ "--max-target-megabytes",
84
+ "5",
85
+ ];
86
+ const repoWarnings = [];
87
+ if (ctx.config.repoConfigIgnored) {
88
+ // By default gitleaks reads .gitleaksignore from its working directory (the repository root).
89
+ // Point it at the staging directory, which cannot contain one.
90
+ args.push("--gitleaks-ignore-path", dir);
91
+ }
92
+ else {
93
+ const cfgPath = ctx.config.engines.gitleaks.configPath;
94
+ const cfgRel = cfgPath ?? ".gitleaks.toml";
95
+ const cfg = join(ctx.root, cfgRel);
96
+ if (isPlainFileInside(ctx.root, cfgRel)) {
97
+ args.push("--config", cfg);
98
+ repoWarnings.push("using the repository's gitleaks configuration, which can allowlist paths or rules (--ignore-repo-config disables it)");
99
+ }
100
+ else if (cfgPath)
101
+ throw new Error(`gitleaks config not found: ${cfgPath}`);
102
+ const ignore = join(ctx.root, ".gitleaksignore");
103
+ if (isPlainFileInside(ctx.root, ".gitleaksignore")) {
104
+ args.push("--gitleaks-ignore-path", ignore);
105
+ repoWarnings.push("honoring the repository's .gitleaksignore (--ignore-repo-config disables it)");
106
+ }
107
+ }
108
+ const r = await exec(ctx.binary, args, { cwd: dir, timeoutMs: ctx.timeoutMs });
109
+ const fail = processFailure("gitleaks", r, [0]);
110
+ if (fail)
111
+ throw fail;
112
+ const leaks = r.stdout.trim() ? parseJson("gitleaks", r.stdout) : [];
113
+ const findings = (leaks ?? []).map((l) => ({
114
+ engine: "gitleaks",
115
+ ruleId: l.RuleID,
116
+ category: "secrets",
117
+ severity: "high",
118
+ file: toRel(dir, l.File),
119
+ line: l.StartLine || null,
120
+ endLine: l.EndLine || null,
121
+ message: l.Description,
122
+ remediation: REMEDIATION,
123
+ references: [],
124
+ }));
125
+ const warnings = [
126
+ ...repoWarnings,
127
+ ...(skippedLarge > 0 ? [`${skippedLarge} file(s) larger than 5 MB were not scanned`] : []),
128
+ ];
129
+ return { findings, warnings };
130
+ }
131
+ finally {
132
+ rmSync(dir, { recursive: true, force: true });
133
+ }
134
+ },
135
+ };
@@ -0,0 +1,100 @@
1
+ import { exec } from "../core/exec.js";
2
+ import { basename, isLockfile } from "./engine.js";
3
+ import { detectBinary, parseJson, processFailure, toRel } from "./util.js";
4
+ export function cvssToSeverity(score) {
5
+ if (score >= 9)
6
+ return "critical";
7
+ if (score >= 7)
8
+ return "high";
9
+ if (score >= 4)
10
+ return "medium";
11
+ return "low";
12
+ }
13
+ const LABEL = {
14
+ LOW: "low",
15
+ MODERATE: "medium",
16
+ MEDIUM: "medium",
17
+ HIGH: "high",
18
+ CRITICAL: "critical",
19
+ };
20
+ function fixedVersions(v, pkg, eco) {
21
+ const out = new Set();
22
+ for (const a of v.affected ?? []) {
23
+ if (a.package?.name !== pkg || a.package?.ecosystem !== eco)
24
+ continue;
25
+ for (const r of a.ranges ?? [])
26
+ for (const e of r.events ?? [])
27
+ if (e.fixed)
28
+ out.add(e.fixed);
29
+ }
30
+ return [...out];
31
+ }
32
+ export const osvScanner = {
33
+ name: "osv-scanner",
34
+ category: "deps",
35
+ priority: 0,
36
+ binaryName: "osv-scanner",
37
+ installHint: "Install: brew install osv-scanner (or go install github.com/google/osv-scanner/v2/cmd/osv-scanner@latest)",
38
+ versionArgs: ["--version"],
39
+ detect: (opts) => detectBinary("osv-scanner", osvScanner.versionArgs, osvScanner.installHint, opts?.withVersion ?? true),
40
+ select(target) {
41
+ const files = target.files.filter(isLockfile);
42
+ return files.length > 0
43
+ ? { files, skipReason: null }
44
+ : { files: null, skipReason: "no lockfile or manifest in scope" };
45
+ },
46
+ // Vulnerability databases change daily: identical inputs can have different results.
47
+ cacheable: () => false,
48
+ cacheInputs: () => [],
49
+ async run(ctx) {
50
+ const args = ["scan", "source", "--format", "json"];
51
+ for (const f of ctx.files)
52
+ args.push("--lockfile", f.startsWith("-") ? `./${f}` : f);
53
+ const r = await exec(ctx.binary, args, { cwd: ctx.root, timeoutMs: ctx.timeoutMs });
54
+ // 0 = clean, 1 = vulnerabilities found, 128 = nothing parseable in the given files
55
+ const fail = processFailure("osv-scanner", r, [0, 1, 128]);
56
+ if (fail)
57
+ throw fail;
58
+ const warnings = [];
59
+ if (r.code === 128 || !r.stdout.trim()) {
60
+ warnings.push(`osv-scanner found no resolvable packages in: ${ctx.files.map(basename).join(", ")}`);
61
+ return { findings: [], warnings };
62
+ }
63
+ const out = parseJson("osv-scanner", r.stdout);
64
+ const findings = [];
65
+ for (const res of out.results ?? []) {
66
+ const file = toRel(ctx.root, res.source.path);
67
+ for (const pk of res.packages ?? []) {
68
+ const { name, version, ecosystem } = pk.package;
69
+ const byId = new Map((pk.vulnerabilities ?? []).map((v) => [v.id, v]));
70
+ for (const g of pk.groups ?? []) {
71
+ const v = g.ids.map((id) => byId.get(id)).find(Boolean);
72
+ if (!v)
73
+ continue;
74
+ const score = Number(g.max_severity);
75
+ const severity = Number.isFinite(score) && g.max_severity
76
+ ? cvssToSeverity(score)
77
+ : (LABEL[(v.database_specific?.severity ?? "").toUpperCase()] ?? "medium");
78
+ const fixed = fixedVersions(v, name, ecosystem);
79
+ const aliases = [...new Set([...g.ids, ...(v.aliases ?? [])])].filter((a) => a !== v.id);
80
+ findings.push({
81
+ engine: "osv-scanner",
82
+ ruleId: v.id,
83
+ category: "deps",
84
+ severity,
85
+ file,
86
+ line: null,
87
+ endLine: null,
88
+ message: `${name}@${version} (${ecosystem}) is affected by ${v.id}${aliases.length ? ` (${aliases.slice(0, 3).join(", ")})` : ""}: ${v.summary ?? "see advisory"}`,
89
+ remediation: fixed.length
90
+ ? `Upgrade ${name} to ${fixed.slice(0, 3).join(" or ")} or later.`
91
+ : `No fixed version is published for ${name}; replace it or mitigate the affected code path.`,
92
+ references: [`https://osv.dev/${v.id}`],
93
+ key: `${ecosystem}:${name}@${version}#${v.id}`,
94
+ });
95
+ }
96
+ }
97
+ }
98
+ return { findings, warnings };
99
+ },
100
+ };
@@ -0,0 +1,7 @@
1
+ import { astGrep } from "./ast-grep.js";
2
+ import { gitleaks } from "./gitleaks.js";
3
+ import { osvScanner } from "./osv-scanner.js";
4
+ import { semgrep } from "./semgrep.js";
5
+ import { trivy } from "./trivy.js";
6
+ /** Every engine the CLI knows. Order is irrelevant; category + priority decide selection. */
7
+ export const ENGINES = [gitleaks, semgrep, osvScanner, trivy, astGrep];
@@ -0,0 +1,139 @@
1
+ import { EngineFailure } from "../core/errors.js";
2
+ import { chunkByLength, exec } from "../core/exec.js";
3
+ import { hashPaths } from "../core/hashing.js";
4
+ import { assertPlainPath } from "../core/safefs.js";
5
+ import { isLockfile } from "./engine.js";
6
+ import { detectBinary, parseJson, processFailure, toRel } from "./util.js";
7
+ const SEVERITY = { ERROR: "high", WARNING: "medium", INFO: "low" };
8
+ const ARG_BUDGET = process.platform === "win32" ? 6000 : 60_000;
9
+ /** Source files semgrep can analyze; used only to decide whether a "not scanned" warning is meaningful. */
10
+ const CODE_RE = /\.(?:[cm]?[jt]sx?|py|go|rs|java|kt|rb|php|cs|swift|scala|c|cc|cpp|h|hpp|sh)$/;
11
+ /** Characters that would turn a file name into a broader glob when passed to --include. */
12
+ const GLOB_CHARS = /[*?[\]{}\\!]/;
13
+ /** Semgrep's hardcoded-credential rules live under `*.secrets.*`; they are secrets findings, not generic SAST. */
14
+ const isSecretsRule = (id) => id.split(".").includes("secrets");
15
+ const isRegistry = (c) => /^[pr]\//.test(c);
16
+ export const semgrep = {
17
+ name: "semgrep",
18
+ category: "sast",
19
+ priority: 0,
20
+ binaryName: "semgrep",
21
+ installHint: "Install: brew install semgrep (or pipx install semgrep)",
22
+ versionArgs: ["--version"],
23
+ detect: (opts) => detectBinary("semgrep", semgrep.versionArgs, semgrep.installHint, opts?.withVersion ?? true),
24
+ select(target) {
25
+ const files = target.files.filter((f) => !isLockfile(f));
26
+ return files.length > 0
27
+ ? { files, skipReason: null }
28
+ : { files: null, skipReason: "no scannable files in scope" };
29
+ },
30
+ // Registry rulesets change remotely, so only fully local rule sets are safe to cache.
31
+ cacheable: (c) => c.engines.semgrep.configs.every((x) => !isRegistry(x)),
32
+ cacheInputs: (c, root) => [
33
+ ...c.engines.semgrep.configs,
34
+ // Cacheable runs only use local rule files; their contents are part of the result's identity.
35
+ hashPaths(root, c.engines.semgrep.configs.filter((x) => !isRegistry(x))),
36
+ ],
37
+ async run(ctx) {
38
+ const base = ["scan", "--metrics=off", "--json", "--quiet", "--disable-version-check"];
39
+ for (const c of ctx.config.engines.semgrep.configs) {
40
+ if (!isRegistry(c)) {
41
+ try {
42
+ assertPlainPath(ctx.root, c); // a symlinked rule path could point at files outside the repository
43
+ }
44
+ catch (e) {
45
+ throw new EngineFailure("error", `semgrep: ${e.message}`);
46
+ }
47
+ }
48
+ base.push("--config", c);
49
+ }
50
+ // Semgrep's own file discovery applies .gitignore, .semgrepignore and its default ignores
51
+ // (tests, vendored and minified code). Passing explicit file paths would bypass all of that and
52
+ // make us noisier than the tool we wrap, so we always hand it the directory and narrow it with
53
+ // root-anchored --include patterns (a bare `index.js` would also match every nested index.js).
54
+ const runs = [];
55
+ if (ctx.mode === "all") {
56
+ runs.push([...base, ...ctx.exclude.flatMap((e) => ["--exclude", e]), "."]);
57
+ }
58
+ else {
59
+ const plain = ctx.files.filter((f) => !GLOB_CHARS.test(f) && !f.includes("\n"));
60
+ const special = ctx.files.filter((f) => !plain.includes(f));
61
+ for (const chunk of chunkByLength(plain.map((f) => `/${f}`), ARG_BUDGET)) {
62
+ runs.push([...base, ...chunk.flatMap((p) => ["--include", p]), "."]);
63
+ }
64
+ // Rare: names with glob characters cannot be anchored safely, so pass them explicitly.
65
+ for (const chunk of chunkByLength(special.map((f) => (f.startsWith("-") ? `./${f}` : f)), ARG_BUDGET)) {
66
+ runs.push([...base, ...chunk]);
67
+ }
68
+ }
69
+ const findings = [];
70
+ const nonFatal = [];
71
+ const scanned = new Set();
72
+ let sawScannedList = false;
73
+ let version;
74
+ for (const args of runs) {
75
+ const r = await exec(ctx.binary, args, {
76
+ cwd: ctx.root,
77
+ timeoutMs: ctx.timeoutMs,
78
+ env: { SEMGREP_SEND_METRICS: "off" },
79
+ });
80
+ const fail = processFailure("semgrep", r, [0]);
81
+ if (fail)
82
+ throw fail;
83
+ const out = parseJson("semgrep", r.stdout);
84
+ version ??= out.version;
85
+ if (out.paths?.scanned)
86
+ sawScannedList = true;
87
+ for (const p of out.paths?.scanned ?? [])
88
+ scanned.add(toRel(ctx.root, p));
89
+ for (const er of out.errors ?? []) {
90
+ // Type and path only: semgrep error messages can quote source code.
91
+ const type = Array.isArray(er.type)
92
+ ? String(er.type[0])
93
+ : typeof er.type === "string"
94
+ ? er.type
95
+ : "Error";
96
+ nonFatal.push(`${type}${er.path ? ` in ${toRel(ctx.root, er.path)}` : ""}`);
97
+ }
98
+ for (const m of out.results) {
99
+ const secret = isSecretsRule(m.check_id);
100
+ const refs = Array.isArray(m.extra.metadata?.references)
101
+ ? m.extra.metadata.references.filter((x) => typeof x === "string")
102
+ : [];
103
+ if (m.extra.metadata?.shortlink)
104
+ refs.unshift(m.extra.metadata.shortlink);
105
+ findings.push({
106
+ engine: "semgrep",
107
+ ruleId: m.check_id,
108
+ category: secret ? "secrets" : "sast",
109
+ severity: SEVERITY[m.extra.severity] ?? "medium",
110
+ file: toRel(ctx.root, m.path),
111
+ line: m.start.line,
112
+ endLine: m.end.line,
113
+ // Rule messages and fixes can interpolate the matched text (`$VALUE`). For credentials that would
114
+ // copy the secret into reports and the cache, so secrets findings get a fixed message.
115
+ message: secret
116
+ ? `Possible hardcoded credential detected by rule ${m.check_id.split(".").slice(-1)[0]}`
117
+ : m.extra.message.replace(/\s+/g, " ").trim(),
118
+ remediation: secret
119
+ ? "Treat it as exposed: revoke and rotate it, remove it from the code and history, and load it from a secret manager."
120
+ : m.extra.fix
121
+ ? `Suggested fix: ${m.extra.fix}`
122
+ : null,
123
+ references: [...new Set(refs)].slice(0, 3),
124
+ });
125
+ }
126
+ }
127
+ const warnings = [];
128
+ if (nonFatal.length > 0) {
129
+ warnings.push(`semgrep reported ${nonFatal.length} non-fatal error(s): ${nonFatal.slice(0, 3).join("; ")}${nonFatal.length > 3 ? "; ..." : ""}`);
130
+ }
131
+ if (ctx.mode !== "all" && sawScannedList) {
132
+ const missed = ctx.files.filter((f) => CODE_RE.test(f) && !scanned.has(f));
133
+ if (missed.length > 0) {
134
+ warnings.push(`${missed.length} changed source file(s) were not scanned by semgrep because of its default ignores (for example tests/ or vendored code) or a .semgrepignore: ${missed.slice(0, 3).join(", ")}${missed.length > 3 ? ", ..." : ""}`);
135
+ }
136
+ }
137
+ return version ? { findings, warnings, version } : { findings, warnings };
138
+ },
139
+ };
@@ -0,0 +1,71 @@
1
+ import { exec } from "../core/exec.js";
2
+ import { isLockfile } from "./engine.js";
3
+ import { detectBinary, parseJson, processFailure } from "./util.js";
4
+ const SEVERITY = {
5
+ CRITICAL: "critical",
6
+ HIGH: "high",
7
+ MEDIUM: "medium",
8
+ LOW: "low",
9
+ UNKNOWN: "low",
10
+ };
11
+ /** Fallback for the `deps` category when osv-scanner is not installed. Scans lockfiles one by one. */
12
+ export const trivy = {
13
+ name: "trivy",
14
+ category: "deps",
15
+ priority: 1,
16
+ binaryName: "trivy",
17
+ installHint: "Install: brew install trivy (or see https://trivy.dev)",
18
+ versionArgs: ["--version"],
19
+ detect: (opts) => detectBinary("trivy", trivy.versionArgs, trivy.installHint, opts?.withVersion ?? true),
20
+ select(target) {
21
+ const files = target.files.filter(isLockfile);
22
+ return files.length > 0
23
+ ? { files, skipReason: null }
24
+ : { files: null, skipReason: "no lockfile or manifest in scope" };
25
+ },
26
+ cacheable: () => false, // vulnerability DB changes over time
27
+ cacheInputs: () => [],
28
+ async run(ctx) {
29
+ const findings = [];
30
+ // One trivy process at a time: concurrent runs contend on its vulnerability DB lock.
31
+ for (const file of ctx.files) {
32
+ const r = await exec(ctx.binary, [
33
+ "fs",
34
+ "--scanners",
35
+ "vuln",
36
+ "--format",
37
+ "json",
38
+ "--quiet",
39
+ "--skip-version-check",
40
+ file.startsWith("-") ? `./${file}` : file,
41
+ ], { cwd: ctx.root, timeoutMs: ctx.timeoutMs });
42
+ const fail = processFailure("trivy", r, [0]);
43
+ if (fail)
44
+ throw fail;
45
+ const out = parseJson("trivy", r.stdout);
46
+ for (const res of out.Results ?? []) {
47
+ for (const v of res.Vulnerabilities ?? []) {
48
+ const fixed = v.FixedVersion?.split(",")
49
+ .map((x) => x.trim())
50
+ .filter(Boolean) ?? [];
51
+ findings.push({
52
+ engine: "trivy",
53
+ ruleId: v.VulnerabilityID,
54
+ category: "deps",
55
+ severity: SEVERITY[(v.Severity ?? "").toUpperCase()] ?? "medium",
56
+ file,
57
+ line: null,
58
+ endLine: null,
59
+ message: `${v.PkgName}@${v.InstalledVersion}${res.Type ? ` (${res.Type})` : ""} is affected by ${v.VulnerabilityID}: ${v.Title ?? "see advisory"}`,
60
+ remediation: fixed.length
61
+ ? `Upgrade ${v.PkgName} to ${fixed.slice(0, 3).join(" or ")} or later.`
62
+ : `No fixed version is published for ${v.PkgName}; replace it or mitigate the affected code path.`,
63
+ references: v.PrimaryURL ? [v.PrimaryURL] : [],
64
+ key: `${res.Type ?? "pkg"}:${v.PkgName}@${v.InstalledVersion}#${v.VulnerabilityID}`,
65
+ });
66
+ }
67
+ }
68
+ }
69
+ return { findings, warnings: [] };
70
+ },
71
+ };
@@ -0,0 +1,129 @@
1
+ import { realpathSync } from "node:fs";
2
+ import { isAbsolute, relative, sep } from "node:path";
3
+ import { EngineFailure } from "../core/errors.js";
4
+ import { exec, findBinary } from "../core/exec.js";
5
+ /** Run `<binary> <versionArgs>` and parse a semantic version out of the output (null if none). */
6
+ export async function readVersion(binary, versionArgs) {
7
+ const r = await exec(binary, versionArgs, { cwd: process.cwd(), timeoutMs: 15_000 });
8
+ const m = /(\d+\.\d+\.\d+(?:[-+][\w.]+)?)/.exec(`${r.stdout}\n${r.stderr}`);
9
+ return m?.[1] ?? null;
10
+ }
11
+ /**
12
+ * Locate an engine. `withVersion: false` only looks the binary up on PATH (microseconds); asking for the
13
+ * version spawns the tool, and for semgrep (Python) that alone takes about half a second. Scans therefore
14
+ * locate first and read versions concurrently with the engines' own runs; `doctor` asks for versions.
15
+ */
16
+ export async function detectBinary(binaryName, versionArgs, hint, withVersion = true) {
17
+ const binary = findBinary(binaryName);
18
+ if (!binary) {
19
+ return {
20
+ available: false,
21
+ version: null,
22
+ binary: null,
23
+ reason: `'${binaryName}' not found on PATH. ${hint}`,
24
+ };
25
+ }
26
+ const version = withVersion ? await readVersion(binary, versionArgs) : null;
27
+ return { available: true, version, binary, reason: null };
28
+ }
29
+ export const firstLine = (s, max = 300) => (s
30
+ .trim()
31
+ .split("\n")
32
+ .find((l) => l.trim().length > 0) ?? "")
33
+ .trim()
34
+ .slice(0, max);
35
+ /** Absolute paths in an engine's error text would leak the machine's layout into reports; replace them. */
36
+ export function scrubPaths(s) {
37
+ return s
38
+ .replace(/[A-Za-z]:\\(?:[^\s\\'"]+\\)+[^\s\\'"]*/g, "<path>")
39
+ .replace(/\/(?:[\w.@+~-]+\/)+[\w.@+~-]*/g, "<path>");
40
+ }
41
+ /**
42
+ * The most useful single line of an engine's stderr, without paths. A Python traceback starts with the
43
+ * boilerplate line "Traceback (most recent call last):" and ends with the actual exception, so for those
44
+ * the last line is reported instead of the first.
45
+ */
46
+ export function errorDetail(stderr) {
47
+ const lines = stderr
48
+ .trim()
49
+ .split("\n")
50
+ .map((l) => l.trim())
51
+ .filter(Boolean);
52
+ if (lines.length === 0)
53
+ return "no error output";
54
+ const first = lines[0] ?? "";
55
+ let pick = first;
56
+ // Runtime boilerplate headers say nothing: a Python "Traceback (most recent call last):" or Node's
57
+ // "node:internal/process/promises:394". Prefer the actual exception line, else the last line.
58
+ if (/^(Traceback\b|node:internal|file:\/\/|at\s)/.test(first)) {
59
+ pick =
60
+ lines.find((l) => /^[\w.$]*(Error|Exception)\b.*:/.test(l)) ??
61
+ lines[lines.length - 1] ??
62
+ first;
63
+ }
64
+ return scrubPaths(pick).slice(0, 300);
65
+ }
66
+ /** Turn an abnormal process result into a typed failure (never includes stdout, which may hold code). */
67
+ export function processFailure(name, r, okCodes) {
68
+ if (r.spawnError)
69
+ return new EngineFailure("error", `${name}: cannot start (${r.spawnError.code ?? r.spawnError.message})`);
70
+ if (r.timedOut)
71
+ return new EngineFailure("timeout", `${name}: exceeded the time limit and was killed`);
72
+ if (r.truncated)
73
+ return new EngineFailure("error", `${name}: output exceeded the size limit`);
74
+ if (r.code === null || !okCodes.includes(r.code)) {
75
+ const detail = errorDetail(r.stderr);
76
+ return new EngineFailure("error", `${name}: exited with code ${r.code ?? r.signal} (${detail})`);
77
+ }
78
+ return null;
79
+ }
80
+ const realRoots = new Map();
81
+ function realRoot(root) {
82
+ let r = realRoots.get(root);
83
+ if (r === undefined) {
84
+ try {
85
+ r = realpathSync(root);
86
+ }
87
+ catch {
88
+ r = root;
89
+ }
90
+ realRoots.set(root, r);
91
+ }
92
+ return r;
93
+ }
94
+ const outside = (rel) => rel === ".." || rel.startsWith(`..${sep}`) || rel.startsWith("../");
95
+ function realPath(p) {
96
+ try {
97
+ return realpathSync(p);
98
+ }
99
+ catch {
100
+ return null; // file vanished or never existed: no real path to offer
101
+ }
102
+ }
103
+ /**
104
+ * Repo-relative POSIX path from an absolute or already-relative engine path. Tools disagree on whether
105
+ * they report symlink-resolved paths (macOS: /var/... vs /private/var/...): git may give one form and the
106
+ * engine the other, in either direction. So an absolute path is tried as logical and as real, against
107
+ * the root as logical and as real, and the first candidate that stays inside the repository wins.
108
+ */
109
+ export function toRel(root, p) {
110
+ let rel = p;
111
+ if (isAbsolute(p)) {
112
+ const realP = realPath(p);
113
+ const candidates = [
114
+ relative(root, p),
115
+ relative(realRoot(root), p),
116
+ ...(realP ? [relative(root, realP), relative(realRoot(root), realP)] : []),
117
+ ];
118
+ rel = candidates.find((c) => !outside(c)) ?? candidates[0] ?? p;
119
+ }
120
+ return rel.split(sep).join("/").replace(/^\.\//, "");
121
+ }
122
+ export function parseJson(name, text) {
123
+ try {
124
+ return JSON.parse(text);
125
+ }
126
+ catch {
127
+ throw new EngineFailure("error", `${name}: produced output that is not valid JSON`);
128
+ }
129
+ }