-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cli): dispatch the three engines concurrently and merge their findings #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thecodedrift
wants to merge
6
commits into
openspec/add-vale-rule-engine-2-verify
from
openspec/add-vale-rule-engine-3-orchestration
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0ad1c1e
feat(cli): dispatch the three engines concurrently and merge their fi…
thecodedrift 378a1d7
fix(cli): run Vale for a project whose only rules are Vale's
thecodedrift 65b53be
fix(cli): stop hasValeRules from reading an IO error as "no rules"
thecodedrift 5e74df9
ref(cli): read Vale's severity off the outcome instead of asking a he…
thecodedrift 1774645
ref(cli): pass dispatch the ast-grep configs it actually runs
thecodedrift 5d8d080
ref(cli): carry the exit code on DispatchResult instead of deriving it
thecodedrift File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| import { readdir } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
|
|
||
| import type { CheckResult } from "../types/check"; | ||
| import { dedupeFindings, ENGINE_LAYOUTS, type EngineName } from "./engines"; | ||
| import { executeRuntimeRules } from "./runtime/harness"; | ||
| import type { RuntimeRule } from "./runtime/discover"; | ||
| import { runAstGrepScan } from "./scan"; | ||
| import { runVale } from "./vale/run"; | ||
|
|
||
| /** Errno values that mean "the directory is not there", and nothing worse. */ | ||
| const ABSENT_DIRECTORY_CODES = new Set(["ENOENT", "ENOTDIR"]); | ||
|
|
||
| /** | ||
| * Whether `.taskless/vale/rules/` holds anything to run. | ||
| * | ||
| * The spec is explicit that an empty rules directory means Vale is not invoked | ||
| * at all. Worth an explicit check rather than letting Vale run and report | ||
| * nothing: a scaffolded-but-empty engine directory is the common state after | ||
| * `taskless init`, and spawning a subprocess per check to confirm it found | ||
| * nothing is pure cost. | ||
| * | ||
| * Only absence is swallowed. A blanket `catch` here would read an unreadable | ||
| * rules directory (`EACCES`, a bad mount) as "no rules" and skip Vale with no | ||
| * notice and no failure — the same silent-disable that `ValeRunOutcome`'s | ||
| * `blocking` field exists to prevent one file over. Anything that is not | ||
| * absence propagates, so `runEngines` reports it as an engine failure rather | ||
| * than a clean run. | ||
| */ | ||
| export async function hasValeRules(cwd: string): Promise<boolean> { | ||
| try { | ||
| const entries = await readdir( | ||
| join(cwd, ".taskless", ENGINE_LAYOUTS.vale.rulesDirectory) | ||
| ); | ||
| return entries.some((entry) => entry.endsWith(".yml")); | ||
| } catch (error) { | ||
| const code = (error as NodeJS.ErrnoException).code; | ||
| if (code !== undefined && ABSENT_DIRECTORY_CODES.has(code)) return false; | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** One engine's contribution to a check. */ | ||
| export interface EngineOutcome { | ||
| engine: EngineName; | ||
| results: CheckResult[]; | ||
| /** | ||
| * Something the user should see that is not a finding — an engine that could | ||
| * not run. Advisory: it does not affect the exit code. | ||
| */ | ||
| notice?: string; | ||
| /** | ||
| * The engine was present and failed. Unlike a notice this must reach the exit | ||
| * code, or a broken engine reads as a clean run. | ||
| */ | ||
| failure?: string; | ||
| } | ||
|
|
||
| export interface DispatchOptions { | ||
| cwd: string; | ||
| /** Target paths, already filtered to those that exist. */ | ||
| paths: string[]; | ||
| /** | ||
| * One `--config` path per ast-grep rule source, already resolved. The source | ||
| * each was derived from is the caller's concern; dispatch only runs configs. | ||
| */ | ||
| astGrepConfigPaths: string[]; | ||
| /** Runtime rules that survived planning. Empty means the harness is skipped. */ | ||
| runtimeRules: RuntimeRule[]; | ||
| runtimeTimeoutMs?: number; | ||
| valeTimeoutMs?: number; | ||
| } | ||
|
|
||
| export interface DispatchResult { | ||
| /** Every engine's findings, merged. */ | ||
| results: CheckResult[]; | ||
| /** Advisory messages: engines that could not run. */ | ||
| notices: string[]; | ||
| /** Failures that must fail the check even with no findings. */ | ||
| failures: string[]; | ||
| /** Per-engine detail, for callers that report engine by engine. */ | ||
| outcomes: EngineOutcome[]; | ||
| /** | ||
| * The process exit code this run implies. | ||
| * | ||
| * Two independent reasons to fail, and both are needed. An error-severity | ||
| * finding is the ordinary one. An engine failure is the one that is easy to | ||
| * miss: a Vale that timed out or rejected its config produces no findings, so | ||
| * without it a broken engine exits 0 and reads exactly like a clean run. | ||
| * | ||
| * Carried on the result rather than derived by each caller. It is a fact | ||
| * about a completed dispatch, fixed the moment the engines settle, so | ||
| * computing it once here removes the chance of two callers disagreeing about | ||
| * what counts as failure. | ||
| */ | ||
| exitCode: number; | ||
| } | ||
|
|
||
| /** | ||
| * ast-grep over every source, deduped. | ||
| * | ||
| * `sg/rules/` and the legacy `.taskless/rules/` are scanned separately, so a | ||
| * rule present in both reports twice; the finding is its own identity, so | ||
| * identical matches collapse. | ||
| */ | ||
| async function runAstGrepEngine( | ||
| options: DispatchOptions | ||
| ): Promise<EngineOutcome> { | ||
| const results: CheckResult[] = []; | ||
| for (const configPath of options.astGrepConfigPaths) { | ||
| const scan = await runAstGrepScan(options.cwd, options.paths, { | ||
| configPath, | ||
| }); | ||
| results.push(...scan.results); | ||
| } | ||
| return { engine: "sg", results: dedupeFindings(results) }; | ||
| } | ||
|
|
||
| /** | ||
| * Vale, when it has rules to run. | ||
| * | ||
| * The three non-ok outcomes divide along the line `outcome.blocking` draws: an | ||
| * absent binary is a notice, because an unsupported arch is an ordinary state | ||
| * and failing there would make `check` unrunnable on a machine where the other | ||
| * engines work; a timeout or a crash is a failure, because Vale was present and | ||
| * asked to work, and reporting that as a skip lets a broken rule file read as | ||
| * "no Vale findings". | ||
| * | ||
| * Reading the severity off the outcome rather than asking a helper is the point | ||
| * of that field: an engine reports how bad its own trouble is, and a caller | ||
| * cannot forget to ask. Every engine we add answers the same question the same | ||
| * way. | ||
| */ | ||
| async function runValeEngine(options: DispatchOptions): Promise<EngineOutcome> { | ||
| if (!(await hasValeRules(options.cwd))) { | ||
| return { engine: "vale", results: [] }; | ||
| } | ||
|
|
||
| const outcome = await runVale({ | ||
| cwd: options.cwd, | ||
| paths: options.paths, | ||
| timeoutMs: options.valeTimeoutMs, | ||
| }); | ||
|
|
||
| if (outcome.status === "ok") { | ||
| return { engine: "vale", results: outcome.results }; | ||
| } | ||
| return outcome.blocking | ||
| ? { engine: "vale", results: [], failure: outcome.message } | ||
| : { engine: "vale", results: [], notice: outcome.message }; | ||
| } | ||
|
|
||
| /** The runtime harness, over rules that planning already cleared to run. */ | ||
| async function runRuntimeEngine( | ||
| options: DispatchOptions | ||
| ): Promise<EngineOutcome> { | ||
| if (options.runtimeRules.length === 0) { | ||
| return { engine: "runtime", results: [] }; | ||
| } | ||
| const results = await executeRuntimeRules(options.cwd, options.runtimeRules, { | ||
| paths: options.paths, | ||
| timeoutMs: options.runtimeTimeoutMs, | ||
| }); | ||
| return { engine: "runtime", results }; | ||
| } | ||
|
|
||
| /** | ||
| * Run every engine that has work, concurrently, and merge what they report. | ||
| * | ||
| * Concurrency is the point: the engines are independent subprocesses over the | ||
| * same paths, and running them in sequence makes a check as slow as the sum of | ||
| * its engines for no benefit. | ||
| * | ||
| * It also forces the isolation question. `allSettled`, not `all`: `all` rejects | ||
| * on the first rejection and abandons the others, so one engine throwing would | ||
| * discard results the rest had already produced — exactly the "an unavailable | ||
| * engine must not abort the others" requirement, and the shape that makes it | ||
| * true by construction rather than by everyone remembering to catch. | ||
| * | ||
| * A rejected engine becomes a failure rather than being swallowed. The engines | ||
| * themselves report expected trouble as an outcome; a thrown error is something | ||
| * unforeseen, and treating it as "no findings" would be the silent-disable | ||
| * failure again. | ||
| */ | ||
| export async function runEngines( | ||
| options: DispatchOptions | ||
| ): Promise<DispatchResult> { | ||
| const engines: Array<[EngineName, Promise<EngineOutcome>]> = [ | ||
| ["sg", runAstGrepEngine(options)], | ||
| ["vale", runValeEngine(options)], | ||
| ["runtime", runRuntimeEngine(options)], | ||
| ]; | ||
|
|
||
| const settled = await Promise.allSettled(engines.map(([, task]) => task)); | ||
|
|
||
| const outcomes: EngineOutcome[] = settled.map((entry, index) => { | ||
| const engine = engines[index]?.[0] ?? "sg"; | ||
| if (entry.status === "fulfilled") return entry.value; | ||
| const reason: unknown = entry.reason; | ||
| return { | ||
| engine, | ||
| results: [], | ||
| failure: `${engine} engine failed: ${ | ||
| reason instanceof Error ? reason.message : String(reason) | ||
| }`, | ||
| }; | ||
| }); | ||
|
|
||
| const results = outcomes.flatMap((outcome) => outcome.results); | ||
| const failures = outcomes.flatMap((outcome) => outcome.failure ?? []); | ||
|
|
||
| return { | ||
| results, | ||
| notices: outcomes.flatMap((outcome) => outcome.notice ?? []), | ||
| failures, | ||
| outcomes, | ||
| exitCode: | ||
| results.some((finding) => finding.severity === "error") || | ||
| failures.length > 0 | ||
| ? 1 | ||
| : 0, | ||
| }; | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.