Files
gstack/lib/pregate/lane-selection.ts
5ebda5999f v1.91.161.0 fix(pregate): Django, Dart, asset and tool-config lanes; init drafts from the repo (#3266-#3269) (#3306)
* fix(pregate): Dart import graph and test lanes (#3285)

Port of arlophoenix's #3285. The import graph reads Dart import/export/part
directives and resolves package: URIs through every tracked pubspec.yaml;
_test.dart files under test/ are tests. lanes runs each Dart package's
selected tests from its own root with flutter test or dart test (through fvm
when the package pins it). flutter/dart/melos/very_good CI jobs are lanes,
and run-step test paths resolve against working-directory and literal cd.
A missing Dart SDK or a .dart file outside any package leaves the lane
incomplete with a fix line; device tests (integration_test/, test_driver/)
are not unit lanes.

Co-authored-by: arlophoenix <arlo.phoenix@gmail.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): a --base that names a commit gets a branch fix, not an impossible fetch (#3269)

gstack-pregate --base <sha> refused with "fix: git fetch origin <sha>
--depth=1". That fetch can never create origin/<sha>: a short SHA fails
with "couldn't find remote ref", and a full SHA only reaches FETCH_HEAD.
A fork clone hits the same wall with --base upstream/main.

When origin/<base> does not resolve but <base> names a commit here and
no local branch (a SHA, a tag, another remote's ref), the refusal now
says --base takes a branch name on origin and suggests origin/HEAD's
branch. A local branch, or a name that resolves nowhere, keeps the
fetch fix: that is the unfetched or shallow checkout it repairs. Same
code, same exit 3, and pin files are still read only from the reviewed
origin/<base>.

Fixes #3269

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(pregate): Django, asset and tool-config lanes; init drafts from the repo (#3266, #3267, #3268)

- Django: settings modules, settings strings, include(), app migrations,
  admin, templates, static files and project templates get framework edges
  (lib/pregate/django.ts).
- Assets, stylesheets and dotfile tool config take a no-tests empty lane;
  licence text under any name is documentation.
- gstack-pregate init drafts languages, runner notes and nearest-test
  dependencies; never writes [].
- Review fixes on the #3285 port: untracked shell scopes leave a lane
  unknown; Dart directives read to their semicolon; nested comments.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(pregate): init draft API is exercised directly, so init-draft.ts has a lane

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(changelog): restore the history a truncated rewrite dropped

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): outside-voice review fixes for the non-JS lanes

- Tool config is an allowlist: unknown dotfiles (.rspec, .husky hooks) and
  stylesheets still need a lane; snapshot, golden and fixture trees and
  .gstack/ are never inert.
- Django edges need Django evidence (manage.py or code importing django)
  and settings assignments in code, not strings or comments.
- A cd after ||, &, control flow or braces leaves a lane unknown.
- Dart directives allow metadata and quoted semicolons.
- init drafts at most 256 dependencies (the schema limit); the rest are TODO.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(windows): pregate-dart and pregate-non-js join the POSIX-run pregate fixtures

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* chore: release v1.91.161.0

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: arlophoenix <arlo.phoenix@gmail.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Co-authored-by: y$un_ <forrest.sun527@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 01:38:26 +00:00

147 lines
8.4 KiB
TypeScript

/**
* pregate/lane-selection — from touched files to the tests that must run and
* the platform lanes that must run them (plan C8 `lanes`). Mapping goes
* through lib/pregate/import-graph.ts plus the generated-file registry (a
* touched template selects the tests of its rendered outputs). Lane file
* sets come from the workflow files: literal test paths in run steps, or the
* runner-owned selection manifest `.gstack/pregate.json` declares for a
* planner-computed matrix. Every uncertainty widens the selection.
*/
import { GENERATED_PATH } from '../regen';
import { runBashSync } from '../script-invocation';
import { matchesAny } from '../ship-policy';
import { PREGATE_CONFIG_PATH } from './config';
import { isDocsEmptyLane, isInertNoTests } from './docs-lane';
import { buildGraph, isTestFile, listRepoFiles, testsFor, type Graph } from './import-graph';
import type { PregateContext, RemoteObligation } from './types';
import type { Lane } from './workflows';
export interface Selection {
/** Touched files with no lane and no `[]` declaration. */
noLane: string[];
/** Markdown (and `empty_lanes` paths) with no import-graph test: the docs/no-tests empty lane. */
emptyLane: string[];
/** Assets and dotfile tool config with no import-graph test: files no runner loads, also an empty lane. */
inert: string[];
/** Every test the touched files select, sorted. */
tests: string[];
/** Tests that run on this machine. */
localTests: string[];
/** Platform lanes this machine cannot run that select part of the set. */
remote: RemoteObligation[];
/** Tiers of the local lanes the selection touches. */
tiersRan: string[];
/** `lane id → files` for every lane whose set is known, plus `?` for planner-computed lanes without a manifest. */
laneSets: Record<string, string[] | '?'>;
provenance: string[];
}
/** One path-shaped token: framework route segments (`(group)`, `[id]`, `[...slug]`, `@slot`, `$param`, `+page`) stay whole. */
const PATH_TOKEN = /[\w.@()[\]$+~-]+(?:\/[\w.@()[\]$+~-]+)*/g;
/** Test files a selection manifest printed, in order: every path-shaped token the import graph calls a test file. */
export function manifestTestPaths(output: string): string[] {
const found = new Set<string>();
for (const line of output.split('\n')) {
for (const [token] of line.matchAll(PATH_TOKEN)) {
const bare = token.replace(/^\.\//, '');
const trimmed = bare.replace(/^[([]+/, '').replace(/[)\].,]+$/, '');
const hit = [bare, trimmed].find(isTestFile);
if (hit) found.add(hit);
}
}
return [...found];
}
/** Resolve one lane's test-file set: literal paths, then the declared selection manifest, else `?` when its matrix is an expression. */
export function laneFileSet(ctx: PregateContext, lane: Lane): string[] | '?' | 'all' {
const files = new Set(lane.testFiles);
const manifest = ctx.config.selection[lane.id];
if (manifest) {
if (!ctx.allowRepoCommands) { ctx.explain.push(`lane ${lane.id}: selection manifest \`${manifest}\` not run (pass --allow-repo-commands); treated as unknown`); return files.size ? [...files] : '?'; }
const r = runBashSync(`set -o pipefail; ${manifest}`, { cwd: ctx.repoRoot, encoding: 'utf8', timeout: Math.min(ctx.timeoutMs, 300_000), maxBuffer: 64 * 1024 * 1024, env: ctx.env });
if (r.error && r.status === null && r.signal === null) { ctx.explain.push(`lane ${lane.id}: selection manifest could not start (${r.error.message}); treated as unknown`); return '?'; }
if (r.status !== 0) { ctx.explain.push(`lane ${lane.id}: selection manifest exited ${r.status}; treated as unknown`); return '?'; }
for (const f of manifestTestPaths(r.stdout ?? '')) files.add(f);
ctx.explain.push(`lane ${lane.id}: ${files.size} files from manifest \`${manifest}\``);
return [...files];
}
if (lane.matrixExpression && files.size === 0) { ctx.explain.push(`lane ${lane.id}: unsupported workflow expression ${JSON.stringify(lane.matrixExpression)} and no selection manifest; treated as unknown`); return '?'; }
return files.size ? [...files] : 'all';
}
/** Tracked files matching a registry entry's output globs. */
function outputsOf(all: readonly string[], globs: readonly string[]): string[] { return all.filter(f => matchesAny(globs, f)); }
export function selectLanes(ctx: PregateContext): Selection {
const touched = ctx.touched;
const gone = touched.flatMap(t => [t.status === 'D' ? t.path : null, t.renamedFrom ?? null]).filter((p): p is string => !!p);
const graph: Graph = buildGraph(ctx.repoRoot, { declarations: ctx.config.dependencies, extraFiles: gone });
const all = listRepoFiles(ctx.repoRoot);
const provenance: string[] = [];
const tests = new Set<string>();
const noLane: string[] = [];
const emptyLane: string[] = [];
const inert: string[] = [];
const seeds = new Map<string, string>();
for (const t of touched) { seeds.set(t.path, t.status === 'D' ? 'deleted' : 'touched'); if (t.renamedFrom) seeds.set(t.renamedFrom, `renamed to ${t.path}`); }
for (const e of ctx.registry.entries) {
if (!touched.some(t => matchesAny(e.inputs, t.path) || matchesAny(e.outputs, t.path))) continue;
for (const out of outputsOf(all, e.outputs)) if (!seeds.has(out)) seeds.set(out, `generated by ${e.id}`);
}
for (const [file, why] of seeds) {
const m = testsFor(graph, file, ctx.config.dependencies);
for (const t of m.tests) tests.add(t);
provenance.push(...m.provenance.map(p => `${p} [${why}]`));
if (m.tests.length || why.startsWith('generated')) continue;
// An explicit `[]` already cleared the file. Label it docs/no-tests only when it is that class.
if (m.declaredNone) {
if (isDocsEmptyLane(file, ctx.config.empty_lanes ?? [])) emptyLane.push(file);
continue;
}
// A renamed file keeps the lane its old name had: those tests run (and fail loudly if they still import the old path).
const renamedFrom = touched.find(t => t.path === file && t.renamedFrom)?.renamedFrom;
if (renamedFrom && testsFor(graph, renamedFrom, ctx.config.dependencies).tests.length) { provenance.push(`${file}: lane via its old name ${renamedFrom}`); continue; }
// A registry input with a regen command is verified by the `regen` check (its outputs are compared fresh).
const generator = ctx.registry.entries.find(e => e.command && matchesAny(e.inputs, file));
if (generator) { provenance.push(`${file}: covered by regen ${generator.id} (registry input)`); continue; }
// The pin files are validated by pregate itself (validateConfig / validateRegistry) on every run.
if (file === PREGATE_CONFIG_PATH || file === GENERATED_PATH) { provenance.push(`${file}: pin file validated by pregate`); continue; }
// Reached only when testsFor mapped nothing, so a Markdown file a test reads never lands here.
if (isDocsEmptyLane(file, ctx.config.empty_lanes ?? [])) {
emptyLane.push(file);
provenance.push(`${file}: docs/no-tests (empty lane)`);
continue;
}
if (isInertNoTests(file)) {
inert.push(file);
provenance.push(`${file}: no-tests (asset or tool config no runner loads)`);
continue;
}
noLane.push(file);
}
const laneSets: Record<string, string[] | '?'> = {};
const local = new Set<string>();
const remote: RemoteObligation[] = [];
const tiers = new Set<string>();
const selected = [...tests].sort();
let anyLocalAll = false;
for (const lane of ctx.lanes.filter(l => l.runsTests && !l.manual)) {
const set = laneFileSet(ctx, lane);
if (set !== 'all') laneSets[lane.id] = set;
const inLane = set === 'all' ? selected : set === '?' ? selected : selected.filter(t => set.includes(t));
if (lane.tier === 'platform') {
if (inLane.length) remote.push({ lane: lane.id, platform: lane.platform, workflow: lane.workflow, job: lane.job, tests: inLane, receipt: null });
continue;
}
if (set === 'all') anyLocalAll = true;
if (inLane.length) { tiers.add(lane.tier); for (const t of inLane) local.add(t); }
}
if (anyLocalAll || ctx.lanes.filter(l => l.runsTests && !l.manual && l.tier !== 'platform').length === 0) for (const t of selected) local.add(t);
for (const t of selected) if (!local.has(t) && !remote.some(r => r.tests.includes(t))) local.add(t);
if (local.size && tiers.size === 0) tiers.add('unit');
return { noLane: noLane.sort(), emptyLane: emptyLane.sort(), inert: inert.sort(), tests: selected, localTests: [...local].sort(), remote, tiersRan: [...tiers].sort(), laneSets, provenance };
}
export function isTest(file: string): boolean { return isTestFile(file); }