fast ci: task graph runner, sharded tests balanced by recorded durations, ci:quick (typecheck + related tests + scrub), Rust only when src-tauri / sidecar build changed, event waits instead of sleeps
This commit is contained in:
parent
cde9424ce3
commit
c71e690aae
13 changed files with 472 additions and 45 deletions
193
tools/ci-plan.ts
Normal file
193
tools/ci-plan.ts
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
// The planning half of tools/ci.ts: which test files exist, how they are split into shards, which tests a change
|
||||
// touches (ci:quick) and whether the desktop Rust host needs to run. Pure functions plus small file / git reads;
|
||||
// tests/ci-plan.test.ts covers them.
|
||||
import { mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
||||
import { cpus } from 'node:os';
|
||||
import { dirname, join, normalize, relative } from 'node:path';
|
||||
|
||||
export const ROOT = join(import.meta.dir, '..');
|
||||
/** Recorded per-file test durations (seconds), written after every run. Gitignored (node_modules/). */
|
||||
export const TIMINGS = join(ROOT, 'node_modules/.cache/storebound-ci/test-timings.json');
|
||||
|
||||
/** Suites that drive a real Chromium (or a heavy child process): each gets a shard of its own, never another heavy
|
||||
* suite next to it, so they don't fight for CPU and time out. */
|
||||
export const HEAVY = [
|
||||
'tests/console-fill.test.ts', 'tests/site-overflow.test.ts', 'tests/local-fonts.test.ts', 'tests/glyphs.test.ts',
|
||||
'tests/capture-web.test.ts', 'tests/console-session.test.ts',
|
||||
];
|
||||
|
||||
/** Changes that can affect every test: run the whole suite. */
|
||||
export const GLOBAL = /^(package\.json|bun\.lock|bunfig\.toml|tsconfig\.json|tests\/helpers\/preload\.ts)$/;
|
||||
|
||||
const SKIP_DIRS = new Set(['node_modules', '.git', '.scratch', 'dist', 'target', 'fixtures', '.claude']);
|
||||
|
||||
/** Every *.test.ts / *.test.tsx `bun test` would pick up, relative to the root, sorted. */
|
||||
export function listTests(root = ROOT): string[] {
|
||||
const out: string[] = [];
|
||||
const walk = (dir: string) => {
|
||||
for (const e of readdirSync(join(root, dir), { withFileTypes: true })) {
|
||||
const p = dir ? `${dir}/${e.name}` : e.name;
|
||||
if (e.isDirectory()) { if (!SKIP_DIRS.has(e.name) && !e.name.startsWith('.')) walk(p); }
|
||||
else if (/\.test\.tsx?$/.test(e.name)) out.push(p);
|
||||
}
|
||||
};
|
||||
walk('');
|
||||
return out.sort();
|
||||
}
|
||||
|
||||
/** Shard count: CPU cores - 1, at least 1, at most 8 (STOREBOUND_CI_SHARDS overrides). */
|
||||
export function shardCount(env = process.env, cores = cpus().length): number {
|
||||
const n = Number(env.STOREBOUND_CI_SHARDS);
|
||||
if (Number.isInteger(n) && n > 0) return n;
|
||||
return Math.max(1, Math.min(8, cores - 1));
|
||||
}
|
||||
|
||||
export function readTimings(file = TIMINGS): Record<string, number> {
|
||||
try { return JSON.parse(readFileSync(file, 'utf8')) as Record<string, number>; } catch { return {}; }
|
||||
}
|
||||
|
||||
export function writeTimings(t: Record<string, number>, file = TIMINGS): void {
|
||||
try { mkdirSync(dirname(file), { recursive: true }); writeFileSync(file, `${JSON.stringify(t, null, 1)}\n`); } catch { /* cache only */ }
|
||||
}
|
||||
|
||||
/** Per-file seconds from bun's JUnit report (the top-level <testsuite> of each file). */
|
||||
export function junitTimes(xml: string): Record<string, number> {
|
||||
const out: Record<string, number> = {};
|
||||
for (const m of xml.matchAll(/<testsuite name="([^"]+)" file="([^"]+)"[^>]*?time="([\d.]+)"/g)) {
|
||||
if (m[1] === m[2]) out[m[2]!] = Number(m[3]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Estimated cost of a file: its recorded time, else its size (about 1 s per 40 KB, 0.2 s minimum). */
|
||||
export function cost(file: string, timings: Record<string, number>, size: (f: string) => number): number {
|
||||
return timings[file] ?? Math.max(0.2, size(file) / 40_000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Longest-processing-time-first split into at most n shards. Heavy files each open a shard of their own and never
|
||||
* share it with another heavy file; light files fill the least loaded shard. Empty shards are dropped.
|
||||
*/
|
||||
export function balance(files: string[], n: number, costOf: (f: string) => number, heavy: string[] = HEAVY): string[][] {
|
||||
const shards = Array.from({ length: Math.max(1, n) }, () => ({ files: [] as string[], load: 0, heavy: false }));
|
||||
const sorted = [...files].sort((a, b) => costOf(b) - costOf(a) || a.localeCompare(b));
|
||||
const isHeavy = (f: string) => heavy.includes(f);
|
||||
for (const f of sorted.filter(isHeavy)) {
|
||||
const free = shards.filter(s => !s.heavy);
|
||||
const s = (free.length ? free : shards).reduce((a, b) => (b.load < a.load ? b : a));
|
||||
s.files.push(f); s.load += costOf(f); s.heavy = true;
|
||||
}
|
||||
for (const f of sorted.filter(f => !isHeavy(f))) {
|
||||
const s = shards.reduce((a, b) => (b.load < a.load ? b : a));
|
||||
s.files.push(f); s.load += costOf(f);
|
||||
}
|
||||
return shards.filter(s => s.files.length).map(s => s.files);
|
||||
}
|
||||
|
||||
export function planShards(files: string[], n = shardCount(), root = ROOT): string[][] {
|
||||
const t = readTimings();
|
||||
return balance(files, n, f => cost(f, t, p => { try { return statSync(join(root, p)).size; } catch { return 0; } }));
|
||||
}
|
||||
|
||||
// ---- which tests a change touches ----
|
||||
|
||||
const IMPORT_RE = /(?:\bfrom\s*|\bimport\s*\(\s*|\brequire\s*\(\s*|^\s*import\s+)['"](\.{1,2}\/[^'"]+)['"]/gm;
|
||||
const JOIN_RE = /join\(\s*(?:import\.meta\.dir|__dirname|ROOT|root|REPO)\s*,((?:\s*'[^']*'\s*,?)+)\)/g;
|
||||
|
||||
function resolveFile(p: string): string | null {
|
||||
for (const c of [p, `${p}.ts`, `${p}.tsx`, join(p, 'index.ts'), join(p, 'index.tsx')]) {
|
||||
try { if (statSync(c).isFile()) return c; } catch { /* next */ }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Repo files a source file depends on: relative imports, plus `join(import.meta.dir, '..', 'cli', 'index.ts')`
|
||||
* style paths (a test that spawns the CLI depends on it). */
|
||||
export function depsOf(file: string, root = ROOT, src = readFileSync(join(root, file), 'utf8')): string[] {
|
||||
const dir = dirname(join(root, file));
|
||||
const out = new Set<string>();
|
||||
for (const m of src.matchAll(IMPORT_RE)) {
|
||||
const r = resolveFile(normalize(join(dir, m[1]!)));
|
||||
if (r) out.add(relative(root, r));
|
||||
}
|
||||
for (const m of src.matchAll(JOIN_RE)) {
|
||||
const parts = [...m[1]!.matchAll(/'([^']*)'/g)].map(x => x[1]!);
|
||||
for (const base of [dir, root]) {
|
||||
const r = resolveFile(normalize(join(base, ...parts)));
|
||||
if (r && r.startsWith(root)) { out.add(relative(root, r)); break; }
|
||||
}
|
||||
}
|
||||
return [...out];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests related to the changed files: a changed test itself, every test that imports a changed file (transitively),
|
||||
* and every test that names a changed non-code file (fixture, page, skill) by its file name or its fixture folder.
|
||||
* A change to a GLOBAL file returns every test.
|
||||
*/
|
||||
export function relatedTests(changed: string[], tests: string[], root = ROOT): string[] {
|
||||
if (changed.some(f => GLOBAL.test(f))) return tests;
|
||||
const changedSet = new Set(changed);
|
||||
const memo = new Map<string, Set<string>>();
|
||||
const direct = new Map<string, string[]>();
|
||||
const depsCached = (f: string) => {
|
||||
let d = direct.get(f);
|
||||
if (!d) { try { d = depsOf(f, root); } catch { d = []; } direct.set(f, d); }
|
||||
return d;
|
||||
};
|
||||
const closure = (f: string): Set<string> => {
|
||||
const hit = memo.get(f);
|
||||
if (hit) return hit;
|
||||
const seen = new Set<string>([f]);
|
||||
memo.set(f, seen);
|
||||
const stack = [f];
|
||||
while (stack.length) {
|
||||
const cur = stack.pop()!;
|
||||
if (!/\.(ts|tsx)$/.test(cur)) continue;
|
||||
for (const d of depsCached(cur)) if (!seen.has(d)) { seen.add(d); stack.push(d); }
|
||||
}
|
||||
return seen;
|
||||
};
|
||||
const names = changed.filter(f => !/\.(ts|tsx)$/.test(f) || f.includes('/fixtures/')).flatMap(f => {
|
||||
const base = f.split('/').pop()!;
|
||||
const fx = /(?:^|\/)fixtures\/([^/]+)/.exec(f)?.[1];
|
||||
return [base, ...(fx ? [`fixtures/${fx}`, `'${fx}'`] : [])].filter(s => s.length > 3);
|
||||
});
|
||||
return tests.filter(t => {
|
||||
if (changedSet.has(t)) return true;
|
||||
const deps = closure(t);
|
||||
if (changed.some(c => deps.has(c))) return true;
|
||||
if (!names.length) return false;
|
||||
const srcs = [...deps].filter(d => d.startsWith('tests/') || d.startsWith('apps/desktop/tests/')).map(d => {
|
||||
try { return readFileSync(join(root, d), 'utf8'); } catch { return ''; }
|
||||
}).join('\n');
|
||||
return names.some(n => srcs.includes(n));
|
||||
});
|
||||
}
|
||||
|
||||
// ---- git ----
|
||||
|
||||
function git(args: string[], root = ROOT): string | null {
|
||||
const r = Bun.spawnSync(['git', ...args], { cwd: root, stdout: 'pipe', stderr: 'ignore' });
|
||||
return r.exitCode === 0 ? r.stdout.toString() : null;
|
||||
}
|
||||
|
||||
/** Files changed against origin/main (committed since the merge base, staged, unstaged and untracked), or null
|
||||
* when that cannot be told (no git, no origin/main). */
|
||||
export function changedFiles(base = process.env.STOREBOUND_CI_BASE ?? 'origin/main', root = ROOT): string[] | null {
|
||||
const mb = git(['merge-base', base, 'HEAD'], root)?.trim();
|
||||
if (!mb) return null;
|
||||
const lists = [git(['diff', '--name-only', mb], root), git(['ls-files', '--others', '--exclude-standard'], root)];
|
||||
if (lists.some(l => l === null)) return null;
|
||||
return [...new Set(lists.join('\n').split('\n').map(s => s.trim()).filter(Boolean))].sort();
|
||||
}
|
||||
|
||||
/** Desktop Rust host inputs: the crate, and the sidecar build its build script needs (cargo test / clippy never run
|
||||
* the sidecar itself, so a CLI change does not need them; tools/ci.ts rebuilds the sidecar on CLI changes). */
|
||||
export const RUST_INPUTS = /^(apps\/desktop\/src-tauri\/|tools\/build-sidecars\.ts$)/;
|
||||
|
||||
/** Run cargo test / clippy? Always with CI=full or when the change set is unknown; else only when a Rust input changed. */
|
||||
export function rustNeeded(changed: string[] | null, env = process.env): boolean {
|
||||
if (env.CI === 'full' || changed === null) return true;
|
||||
return changed.some(f => RUST_INPUTS.test(f));
|
||||
}
|
||||
203
tools/ci.ts
203
tools/ci.ts
|
|
@ -1,46 +1,173 @@
|
|||
#!/usr/bin/env bun
|
||||
// Everything CI runs, in order; stops at the first failure. bun run ci
|
||||
// `bun test` covers the CLI and the desktop webview (apps/desktop/tests: model, i18n, theme contrast, webview rules),
|
||||
// the desktop boot check (tools/desktop-boot-check.ts: the built window in headless Chromium, needs playwright's
|
||||
// browser or Google Chrome), the site overflow check (tests/site-overflow.test.ts: no sideways scroll at 320-1440 px,
|
||||
// light and dark; skips without Chromium), and the no-hand-written-SVG guard (tests/no-handmade-svg.test.ts: visx charts, Phosphor icons, simple-icons store marks, Mermaid diagrams).
|
||||
// The desktop Rust host (cargo test, clippy
|
||||
// with warnings as errors) runs when cargo is installed; set STOREBOUND_CI_RUST=0 to skip it on a machine without Rust.
|
||||
// Everything CI runs, as a small task graph: independent steps run at the same time, output is streamed with a
|
||||
// [step] prefix, the first failure stops everything, and a summary prints the time per step.
|
||||
// bun run ci full gate before pushing (every check, every test)
|
||||
// bun run ci:quick inner loop: typecheck + the tests related to the change (git diff against origin/main,
|
||||
// mapped through imports) + scrub
|
||||
// The tests run as shards: N = CPU cores - 1 (max 8; STOREBOUND_CI_SHARDS=n overrides) parallel `bun test <files>`
|
||||
// processes, balanced by the durations recorded after the last run (node_modules/.cache/storebound-ci/, falling back
|
||||
// to file size). Browser suites (tools/ci-plan.ts HEAVY) each get a shard of their own.
|
||||
// `bun test` covers the CLI and the desktop webview (apps/desktop/tests), the site overflow check and the
|
||||
// no-hand-written-SVG guard; the desktop boot check (tools/desktop-boot-check.ts: the built window in headless
|
||||
// Chromium, needs playwright's browser or Google Chrome) runs after the desktop build.
|
||||
// The desktop Rust host (cargo test, clippy with warnings as errors) runs when cargo is installed and
|
||||
// apps/desktop/src-tauri or the sidecar build changed against origin/main, or always with CI=full;
|
||||
// STOREBOUND_CI_RUST=0 skips it on a machine without Rust.
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
||||
import { homedir, tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { hostTarget, OUT_DIR, outName } from './build-sidecars';
|
||||
import { changedFiles, HEAVY, junitTimes, listTests, planShards, readTimings, relatedTests, ROOT, rustNeeded, writeTimings } from './ci-plan';
|
||||
|
||||
const root = new URL('..', import.meta.url).pathname;
|
||||
const tauri = join(root, 'apps/desktop/src-tauri');
|
||||
const cargo = process.env.STOREBOUND_CI_RUST === '0' ? null : Bun.which('cargo');
|
||||
const steps: [string, string[], string?][] = [
|
||||
['adapters', ['bun', 'tools/gen-adapters.ts', '--check']],
|
||||
interface Task { name: string; cmd: string[]; cwd?: string; env?: Record<string, string>; after?: string[]; junit?: string }
|
||||
|
||||
const quick = process.argv.includes('--quick');
|
||||
const tauri = join(ROOT, 'apps/desktop/src-tauri');
|
||||
const cache = join(ROOT, 'node_modules/.cache/storebound-ci');
|
||||
const changed = changedFiles();
|
||||
const tests = listTests();
|
||||
const touches = (re: RegExp) => changed === null || changed.some(f => re.test(f));
|
||||
const tasks: Task[] = [];
|
||||
const add = (t: Task) => tasks.push(t);
|
||||
const tsc = (name: string, project: string) => add({ name, cmd: ['bunx', 'tsc', '--noEmit', '--incremental', '-p', project,
|
||||
'--tsBuildInfoFile', join(cache, `${name.replace(/\W+/g, '-')}.tsbuildinfo`)] });
|
||||
const junitDir = mkdtempSync(join(tmpdir(), 'sb-ci-'));
|
||||
|
||||
function addTests(files: string[]) {
|
||||
planShards(files).forEach((shard, i, all) => {
|
||||
const junit = join(junitDir, `shard-${i + 1}.xml`);
|
||||
// the built-bundle check in apps/desktop/tests/webview.test.ts runs as "desktop bundle check" once the build is
|
||||
// done; here it would read dist/ while vite rewrites it
|
||||
// browser suites share the machine with the other steps: a browser start or close can pass bun's 5 s default
|
||||
const timeout = shard.some(f => HEAVY.includes(f)) ? ['--timeout=30000'] : [];
|
||||
add({ name: `tests ${i + 1}/${all.length}`, cmd: ['bun', 'test', ...timeout, '--reporter=junit', `--reporter-outfile=${junit}`, ...shard.map(f => `./${f}`)],
|
||||
env: { STOREBOUND_SKIP_DIST_CHECK: '1' }, junit });
|
||||
});
|
||||
}
|
||||
|
||||
if (quick) {
|
||||
const related = changed === null ? tests : relatedTests(changed, tests);
|
||||
console.log(`ci:quick: ${changed === null ? 'no origin/main to diff against: every test' : `${changed.length} changed file(s), ${related.length} related test file(s)`}`);
|
||||
add({ name: 'adapters', cmd: ['bun', 'tools/gen-adapters.ts', '--check'] });
|
||||
tsc('typecheck', '.');
|
||||
if (touches(/^apps\/desktop\//)) tsc('desktop typecheck', 'apps/desktop');
|
||||
if (related.length) addTests(related);
|
||||
add({ name: 'scrub', cmd: ['bun', 'tools/scrub.ts'] });
|
||||
} else {
|
||||
const cargo = process.env.STOREBOUND_CI_RUST === '0' ? null : Bun.which('cargo');
|
||||
const rust = cargo && rustNeeded(changed);
|
||||
if (!cargo) console.log('ci: cargo not found (or STOREBOUND_CI_RUST=0): desktop Rust tests and clippy skipped');
|
||||
else if (!rust) console.log('ci: apps/desktop/src-tauri and the sidecar build unchanged against origin/main: desktop Rust tests and clippy skipped (CI=full runs them)');
|
||||
add({ name: 'adapters', cmd: ['bun', 'tools/gen-adapters.ts', '--check'] });
|
||||
// the App Store Connect / Google Play operation tables, the wrapped-by scan and docs/api-coverage.md match specs/ and cli/
|
||||
['api tables', ['bun', 'tools/gen-api.ts', '--check']],
|
||||
add({ name: 'api tables', cmd: ['bun', 'tools/gen-api.ts', '--check'] });
|
||||
// one desktop version: package.json desktopVersion = apps/desktop/package.json = tauri.conf.json = Cargo.toml / .lock
|
||||
['desktop version', ['bun', 'tools/desktop-version.ts']],
|
||||
['tests', ['bun', 'test']],
|
||||
['typecheck', ['bunx', 'tsc', '--noEmit']],
|
||||
['desktop i18n', ['bun', 'tools/i18n-check.ts']],
|
||||
['desktop typecheck', ['bunx', 'tsc', '--noEmit', '-p', 'apps/desktop']],
|
||||
['desktop build', ['bun', 'run', '--cwd', 'apps/desktop', 'build']],
|
||||
// the release bundle must not carry the dev fixture mode (apps/desktop/tests/webview.test.ts checks dist/ when present)
|
||||
['desktop bundle check', ['bun', 'test', 'apps/desktop/tests/webview.test.ts']],
|
||||
add({ name: 'desktop version', cmd: ['bun', 'tools/desktop-version.ts'] });
|
||||
add({ name: 'desktop i18n', cmd: ['bun', 'tools/i18n-check.ts'] });
|
||||
tsc('typecheck', '.');
|
||||
tsc('desktop typecheck', 'apps/desktop');
|
||||
add({ name: 'scrub', cmd: ['bun', 'tools/scrub.ts'] });
|
||||
addTests(tests);
|
||||
// `vite build` only: the desktop package's own build script runs tsc first, which "desktop typecheck" already does
|
||||
add({ name: 'desktop build', cmd: ['bunx', 'vite', 'build', '--logLevel', 'warn'], cwd: join(ROOT, 'apps/desktop') });
|
||||
// the release bundle must not carry the dev fixture mode
|
||||
add({ name: 'desktop bundle check', cmd: ['bun', 'test', './apps/desktop/tests/webview.test.ts'], after: ['desktop build'] });
|
||||
// the built window boots (release CSP, Tauri's frozen Object.prototype, mocked host + real sidecar) on an empty, a
|
||||
// D1 (folders gone) and a full D3 saved state, through every screen: no uncaught error, never an empty window
|
||||
['desktop boot check', ['bun', 'tools/desktop-boot-check.ts']],
|
||||
...(cargo ? [
|
||||
// tauri's build script needs the host sidecar binary (gitignored), so a clean checkout builds it first
|
||||
['desktop sidecar (host)', ['bun', 'tools/build-sidecars.ts', '--host']],
|
||||
['desktop rust tests', [cargo, 'test', '--quiet'], tauri],
|
||||
['desktop clippy', [cargo, 'clippy', '--all-targets', '--quiet', '--', '-D', 'warnings'], tauri],
|
||||
] as [string, string[], string][] : []),
|
||||
['scrub', ['bun', 'tools/scrub.ts']],
|
||||
];
|
||||
if (!cargo) console.log('ci: cargo not found (or STOREBOUND_CI_RUST=0): desktop Rust tests and clippy skipped');
|
||||
for (const [name, cmd, cwd] of steps) {
|
||||
const t = Date.now();
|
||||
console.log(`\n== ${name}: ${cmd.join(' ')}`);
|
||||
const r = Bun.spawnSync(cmd, { cwd: cwd ?? root, stdout: 'inherit', stderr: 'inherit' });
|
||||
if (r.exitCode !== 0) { console.error(`\nci: ${name} failed (exit ${r.exitCode})`); process.exit(1); }
|
||||
console.log(`== ${name} ok (${((Date.now() - t) / 1000).toFixed(1)}s)`);
|
||||
add({ name: 'desktop boot check', cmd: ['bun', 'tools/desktop-boot-check.ts'], after: ['desktop build'] });
|
||||
// the CLI compiled into the desktop sidecar (bun build --compile, scanned for this machine's paths): rebuilt when
|
||||
// its inputs changed, with CI=full, or when missing; tauri's build script needs it for cargo test / clippy
|
||||
const hostBin = join(OUT_DIR, outName(hostTarget()));
|
||||
const sidecarBuild = process.env.CI === 'full' || !existsSync(hostBin) || touches(/^(cli\/|tools\/build-sidecars\.ts$|package\.json$|bun\.lock$)/);
|
||||
if (sidecarBuild) add({ name: 'desktop sidecar (host)', cmd: ['bun', 'tools/build-sidecars.ts', '--host'] });
|
||||
if (cargo && rust) {
|
||||
const after = sidecarBuild ? ['desktop sidecar (host)'] : [];
|
||||
// cargo locks the target dir, so test and clippy run one after the other
|
||||
add({ name: 'desktop rust tests', cmd: [cargo, 'test', '--quiet'], cwd: tauri, after });
|
||||
add({ name: 'desktop clippy', cmd: [cargo, 'clippy', '--all-targets', '--quiet', '--', '-D', 'warnings'], cwd: tauri, after: ['desktop rust tests'] });
|
||||
}
|
||||
}
|
||||
console.log('\nci: all green');
|
||||
|
||||
// The browser suites fall back to Google Chrome without playwright's own headless Chromium; Chrome's shutdown takes
|
||||
// seconds to half a minute per browser, which makes those shards several times slower.
|
||||
try {
|
||||
const { registry } = await import('playwright-core/lib/server/registry/index' as string);
|
||||
const exe = registry.findExecutable('chromium-headless-shell')?.executablePath('javascript') as string | undefined;
|
||||
if (exe && !existsSync(exe)) console.log(`ci: playwright's headless Chromium is not installed (${exe.replace(homedir(), '~')}): the browser tests use Google Chrome, which is much slower to close. Install it once: bunx playwright install chromium-headless-shell`);
|
||||
} catch { /* playwright internals moved: no hint */ }
|
||||
|
||||
// ---- runner ----
|
||||
|
||||
interface Run { task: Task; start: number; end?: number; code?: number; proc?: ReturnType<typeof Bun.spawn> }
|
||||
const runs = new Map<string, Run>();
|
||||
const t0 = Date.now();
|
||||
const width = Math.max(...tasks.map(t => t.name.length));
|
||||
const tag = (n: string) => `[${n.padEnd(width)}]`;
|
||||
let failed: string | null = null;
|
||||
|
||||
async function pump(stream: ReadableStream<Uint8Array>, name: string, out: NodeJS.WriteStream) {
|
||||
const dec = new TextDecoder();
|
||||
let buf = '';
|
||||
const reader = stream.getReader();
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
if (failed && failed !== name) continue; // a stopped step's last words are noise
|
||||
buf += dec.decode(value, { stream: true });
|
||||
const lines = buf.split('\n');
|
||||
buf = lines.pop()!;
|
||||
if (lines.length) out.write(lines.map(l => `${tag(name)} ${l}\n`).join(''));
|
||||
}
|
||||
if (buf && !(failed && failed !== name)) out.write(`${tag(name)} ${buf}\n`);
|
||||
}
|
||||
|
||||
async function start(task: Task): Promise<void> {
|
||||
const run: Run = { task, start: Date.now() };
|
||||
runs.set(task.name, run);
|
||||
console.log(`${tag(task.name)} $ ${task.cmd.join(' ').replace(junitDir, '$TMP')}`);
|
||||
const proc = Bun.spawn(task.cmd, { cwd: task.cwd ?? ROOT, stdout: 'pipe', stderr: 'pipe', env: { ...process.env, FORCE_COLOR: '0', ...task.env } });
|
||||
run.proc = proc;
|
||||
await Promise.all([pump(proc.stdout, task.name, process.stdout), pump(proc.stderr, task.name, process.stderr), proc.exited]);
|
||||
run.code = proc.exitCode ?? 1;
|
||||
run.end = Date.now();
|
||||
if (failed) return;
|
||||
console.log(`${tag(task.name)} ${run.code === 0 ? 'ok' : `FAILED (exit ${run.code})`} in ${((run.end - run.start) / 1000).toFixed(1)}s`);
|
||||
if (run.code !== 0) {
|
||||
failed = task.name;
|
||||
for (const r of runs.values()) if (r.end === undefined) r.proc?.kill('SIGKILL'); // bun test ignores SIGTERM until its files finish
|
||||
}
|
||||
}
|
||||
|
||||
const pending = new Set(tasks);
|
||||
const active = new Set<Promise<void>>();
|
||||
while ((pending.size || active.size) && !failed) {
|
||||
for (const t of [...pending]) {
|
||||
if ((t.after ?? []).every(a => runs.get(a)?.code === 0)) {
|
||||
pending.delete(t);
|
||||
const p = start(t).finally(() => active.delete(p));
|
||||
active.add(p);
|
||||
}
|
||||
}
|
||||
if (!active.size) { failed = 'scheduler (unmet dependency)'; break; }
|
||||
await Promise.race(active);
|
||||
}
|
||||
if (failed) await Promise.allSettled([...active]);
|
||||
|
||||
// record per-file durations for the next split (only from shards that finished)
|
||||
const timings = readTimings();
|
||||
for (const r of runs.values()) {
|
||||
if (!r.task.junit || r.end === undefined) continue;
|
||||
try { Object.assign(timings, junitTimes(readFileSync(r.task.junit, 'utf8'))); } catch { /* no report */ }
|
||||
}
|
||||
writeTimings(timings);
|
||||
rmSync(junitDir, { recursive: true, force: true });
|
||||
|
||||
console.log(`\n${quick ? 'ci:quick' : 'ci'} summary (wall ${((Date.now() - t0) / 1000).toFixed(1)}s)`);
|
||||
for (const r of [...runs.values()].sort((a, b) => a.start - b.start)) {
|
||||
const s = r.end === undefined ? 'killed' : r.code === 0 ? 'ok' : r.task.name === failed ? 'FAILED' : 'stopped';
|
||||
const secs = (((r.end ?? Date.now()) - r.start) / 1000).toFixed(1).padStart(6);
|
||||
console.log(` ${r.task.name.padEnd(width)} ${secs}s @${((r.start - t0) / 1000).toFixed(1).padStart(5)}s ${s}`);
|
||||
}
|
||||
for (const t of pending) console.log(` ${t.name.padEnd(width)} - not started`);
|
||||
if (failed) { console.error(`\nci: ${failed} failed`); process.exit(1); }
|
||||
console.log(`\n${quick ? 'ci:quick' : 'ci'}: all green`);
|
||||
|
|
|
|||
|
|
@ -84,6 +84,8 @@ export function generate(root: string): Map<string, string> {
|
|||
'file below and follow it step by step.', '', coreBody.replace(/^/, ''), '', cli, '', '## Skills', '', indexPaths, '',
|
||||
'## Working on storebound itself', '',
|
||||
'- Bun + TypeScript strict, zero runtime dependencies. `bun run ci` must pass (adapters fresh, tests, typecheck, scrub).',
|
||||
'- Inner loop: `bun run ci:quick` (typecheck, the tests related to your change against origin/main, scrub; seconds).',
|
||||
' Before every push: the full `bun run ci` (every check and test, in parallel shards), and check its exit code.',
|
||||
'- Skills are the single source: edit `skills/*/SKILL.md` or `docs/agents-core.md`, then `bun tools/gen-adapters.ts`.',
|
||||
'- Never commit credentials, account / team / issuer / key / app ids or personal data; placeholders use example.com.',
|
||||
''].join('\n'));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue