|
| 1 | +import { readdir } from "node:fs/promises"; |
| 2 | +import { join } from "node:path"; |
| 3 | + |
| 4 | +import type { CheckResult } from "../types/check"; |
| 5 | +import { |
| 6 | + dedupeFindings, |
| 7 | + ENGINE_LAYOUTS, |
| 8 | + type AstGrepRuleSource, |
| 9 | + type EngineName, |
| 10 | +} from "./engines"; |
| 11 | +import { executeRuntimeRules } from "./runtime/harness"; |
| 12 | +import type { RuntimeRule } from "./runtime/discover"; |
| 13 | +import { runAstGrepScan } from "./scan"; |
| 14 | +import { isValeFailure, runVale } from "./vale/run"; |
| 15 | + |
| 16 | +/** |
| 17 | + * Whether `.taskless/vale/rules/` holds anything to run. |
| 18 | + * |
| 19 | + * The spec is explicit that an empty rules directory means Vale is not invoked |
| 20 | + * at all. Worth an explicit check rather than letting Vale run and report |
| 21 | + * nothing: a scaffolded-but-empty engine directory is the common state after |
| 22 | + * `taskless init`, and spawning a subprocess per check to confirm it found |
| 23 | + * nothing is pure cost. |
| 24 | + */ |
| 25 | +export async function hasValeRules(cwd: string): Promise<boolean> { |
| 26 | + try { |
| 27 | + const entries = await readdir( |
| 28 | + join(cwd, ".taskless", ENGINE_LAYOUTS.vale.rulesDirectory) |
| 29 | + ); |
| 30 | + return entries.some((entry) => entry.endsWith(".yml")); |
| 31 | + } catch { |
| 32 | + return false; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** One engine's contribution to a check. */ |
| 37 | +export interface EngineOutcome { |
| 38 | + engine: EngineName; |
| 39 | + results: CheckResult[]; |
| 40 | + /** |
| 41 | + * Something the user should see that is not a finding — an engine that could |
| 42 | + * not run. Advisory: it does not affect the exit code. |
| 43 | + */ |
| 44 | + notice?: string; |
| 45 | + /** |
| 46 | + * The engine was present and failed. Unlike a notice this must reach the exit |
| 47 | + * code, or a broken engine reads as a clean run. |
| 48 | + */ |
| 49 | + failure?: string; |
| 50 | +} |
| 51 | + |
| 52 | +export interface DispatchOptions { |
| 53 | + cwd: string; |
| 54 | + /** Target paths, already filtered to those that exist. */ |
| 55 | + paths: string[]; |
| 56 | + /** ast-grep sources, each with the config that scans it. */ |
| 57 | + astGrepSources: Array<{ source: AstGrepRuleSource; configPath: string }>; |
| 58 | + /** Runtime rules that survived planning. Empty means the harness is skipped. */ |
| 59 | + runtimeRules: RuntimeRule[]; |
| 60 | + runtimeTimeoutMs?: number; |
| 61 | + valeTimeoutMs?: number; |
| 62 | +} |
| 63 | + |
| 64 | +export interface DispatchResult { |
| 65 | + /** Every engine's findings, merged. */ |
| 66 | + results: CheckResult[]; |
| 67 | + /** Advisory messages: engines that could not run. */ |
| 68 | + notices: string[]; |
| 69 | + /** Failures that must fail the check even with no findings. */ |
| 70 | + failures: string[]; |
| 71 | + /** Per-engine detail, for callers that report engine by engine. */ |
| 72 | + outcomes: EngineOutcome[]; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * ast-grep over every source, deduped. |
| 77 | + * |
| 78 | + * `sg/rules/` and the legacy `.taskless/rules/` are scanned separately, so a |
| 79 | + * rule present in both reports twice; the finding is its own identity, so |
| 80 | + * identical matches collapse. |
| 81 | + */ |
| 82 | +async function runAstGrepEngine( |
| 83 | + options: DispatchOptions |
| 84 | +): Promise<EngineOutcome> { |
| 85 | + const results: CheckResult[] = []; |
| 86 | + for (const { configPath } of options.astGrepSources) { |
| 87 | + const scan = await runAstGrepScan(options.cwd, options.paths, { |
| 88 | + configPath, |
| 89 | + }); |
| 90 | + results.push(...scan.results); |
| 91 | + } |
| 92 | + return { engine: "sg", results: dedupeFindings(results) }; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Vale, when it has rules to run. |
| 97 | + * |
| 98 | + * The three non-ok outcomes divide along the line `isValeFailure` draws: an |
| 99 | + * absent binary is a notice, because an unsupported arch is an ordinary state |
| 100 | + * and failing there would make `check` unrunnable on a machine where the other |
| 101 | + * engines work; a timeout or a crash is a failure, because Vale was present and |
| 102 | + * asked to work, and reporting that as a skip lets a broken rule file read as |
| 103 | + * "no Vale findings". |
| 104 | + */ |
| 105 | +async function runValeEngine(options: DispatchOptions): Promise<EngineOutcome> { |
| 106 | + if (!(await hasValeRules(options.cwd))) { |
| 107 | + return { engine: "vale", results: [] }; |
| 108 | + } |
| 109 | + |
| 110 | + const outcome = await runVale({ |
| 111 | + cwd: options.cwd, |
| 112 | + paths: options.paths, |
| 113 | + timeoutMs: options.valeTimeoutMs, |
| 114 | + }); |
| 115 | + |
| 116 | + if (outcome.status === "ok") { |
| 117 | + return { engine: "vale", results: outcome.results }; |
| 118 | + } |
| 119 | + return isValeFailure(outcome) |
| 120 | + ? { engine: "vale", results: [], failure: outcome.message } |
| 121 | + : { engine: "vale", results: [], notice: outcome.message }; |
| 122 | +} |
| 123 | + |
| 124 | +/** The runtime harness, over rules that planning already cleared to run. */ |
| 125 | +async function runRuntimeEngine( |
| 126 | + options: DispatchOptions |
| 127 | +): Promise<EngineOutcome> { |
| 128 | + if (options.runtimeRules.length === 0) { |
| 129 | + return { engine: "runtime", results: [] }; |
| 130 | + } |
| 131 | + const results = await executeRuntimeRules(options.cwd, options.runtimeRules, { |
| 132 | + paths: options.paths, |
| 133 | + timeoutMs: options.runtimeTimeoutMs, |
| 134 | + }); |
| 135 | + return { engine: "runtime", results }; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Run every engine that has work, concurrently, and merge what they report. |
| 140 | + * |
| 141 | + * Concurrency is the point: the engines are independent subprocesses over the |
| 142 | + * same paths, and running them in sequence makes a check as slow as the sum of |
| 143 | + * its engines for no benefit. |
| 144 | + * |
| 145 | + * It also forces the isolation question. `allSettled`, not `all`: `all` rejects |
| 146 | + * on the first rejection and abandons the others, so one engine throwing would |
| 147 | + * discard results the rest had already produced — exactly the "an unavailable |
| 148 | + * engine must not abort the others" requirement, and the shape that makes it |
| 149 | + * true by construction rather than by everyone remembering to catch. |
| 150 | + * |
| 151 | + * A rejected engine becomes a failure rather than being swallowed. The engines |
| 152 | + * themselves report expected trouble as an outcome; a thrown error is something |
| 153 | + * unforeseen, and treating it as "no findings" would be the silent-disable |
| 154 | + * failure again. |
| 155 | + */ |
| 156 | +export async function runEngines( |
| 157 | + options: DispatchOptions |
| 158 | +): Promise<DispatchResult> { |
| 159 | + const engines: Array<[EngineName, Promise<EngineOutcome>]> = [ |
| 160 | + ["sg", runAstGrepEngine(options)], |
| 161 | + ["vale", runValeEngine(options)], |
| 162 | + ["runtime", runRuntimeEngine(options)], |
| 163 | + ]; |
| 164 | + |
| 165 | + const settled = await Promise.allSettled(engines.map(([, task]) => task)); |
| 166 | + |
| 167 | + const outcomes: EngineOutcome[] = settled.map((entry, index) => { |
| 168 | + const engine = engines[index]?.[0] ?? "sg"; |
| 169 | + if (entry.status === "fulfilled") return entry.value; |
| 170 | + const reason: unknown = entry.reason; |
| 171 | + return { |
| 172 | + engine, |
| 173 | + results: [], |
| 174 | + failure: `${engine} engine failed: ${ |
| 175 | + reason instanceof Error ? reason.message : String(reason) |
| 176 | + }`, |
| 177 | + }; |
| 178 | + }); |
| 179 | + |
| 180 | + return { |
| 181 | + results: outcomes.flatMap((outcome) => outcome.results), |
| 182 | + notices: outcomes |
| 183 | + .map((outcome) => outcome.notice) |
| 184 | + .filter((notice): notice is string => notice !== undefined), |
| 185 | + failures: outcomes |
| 186 | + .map((outcome) => outcome.failure) |
| 187 | + .filter((failure): failure is string => failure !== undefined), |
| 188 | + outcomes, |
| 189 | + }; |
| 190 | +} |
| 191 | + |
| 192 | +/** |
| 193 | + * The exit code for a completed check. |
| 194 | + * |
| 195 | + * Two independent reasons to fail, and both are needed. An error-severity |
| 196 | + * finding is the ordinary one. An engine failure is the one that is easy to |
| 197 | + * miss: a Vale that timed out or rejected its config produces no findings, so |
| 198 | + * without this a broken engine exits 0 and reads exactly like a clean run. |
| 199 | + */ |
| 200 | +export function deriveExitCode(result: DispatchResult): number { |
| 201 | + const hasErrorFinding = result.results.some( |
| 202 | + (finding) => finding.severity === "error" |
| 203 | + ); |
| 204 | + return hasErrorFinding || result.failures.length > 0 ? 1 : 0; |
| 205 | +} |
0 commit comments