forked from nexu-io/open-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-test.ts
More file actions
278 lines (257 loc) · 9.87 KB
/
Copy pathbuild-test.ts
File metadata and controls
278 lines (257 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Phase 7 entry slice / spec §10 / §22.4 — build-test atom runner.
//
// Spec-side atom contract lives in `plugins/_official/atoms/build-test/SKILL.md`.
// This module is the daemon-side shell-out implementation: given a
// project cwd + an optional command override, run typecheck + test
// commands, write `critique/build-test.json`, and return the
// signals the devloop's `until` evaluator reads
// (`build.passing`, `tests.passing`, plus the legacy `critique.score`).
//
// The runner is intentionally framework-agnostic — it prefers the
// declared override, falls back to scripts inside `package.json`,
// and otherwise emits a `tests: 'skipped'` outcome with an explicit
// reason so the agent doesn't claim success on a project it never
// actually tested.
import path from 'node:path';
import { promises as fsp } from 'node:fs';
import { spawn, type SpawnOptions } from 'node:child_process';
import type { UntilSignals } from '../until.js';
export type BuildTestStatus = 'passing' | 'failing' | 'skipped';
export interface BuildTestCommandResult {
command: string;
exitCode: number;
durationMs: number;
status: BuildTestStatus;
// Truncated combined stdout + stderr. Bounded by `logBudgetBytes`.
log: string;
// When status='skipped'; explains why.
reason?: string;
}
export interface BuildTestReport {
build: BuildTestCommandResult;
tests: BuildTestCommandResult;
// Signals the pipeline runner forwards into the devloop `until` eval.
signals: UntilSignals;
// ISO timestamp of when the run completed.
endedAt: string;
}
export interface BuildTestRunOptions {
// Project cwd. The atom shells out inside this directory; the
// caller is responsible for staging the imported repo here.
cwd: string;
// Explicit overrides (highest priority). Either OR both can be
// null — the atom skips the matching half cleanly.
buildCommand?: string | null;
testCommand?: string | null;
// Per-command timeout (ms). Default 5 min.
timeoutMs?: number;
// Cap on how many bytes of combined stdout/stderr we keep per
// command. Default 1 MiB.
logBudgetBytes?: number;
// Pluggable child-process spawner so unit tests don't actually
// shell out. Production passes node:child_process.spawn.
spawnFn?: (cmd: string, args: string[], opts: SpawnOptions) => ReturnType<typeof spawn>;
}
const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000;
const DEFAULT_LOG_BUDGET = 1 * 1024 * 1024;
export async function runBuildTest(opts: BuildTestRunOptions): Promise<BuildTestReport> {
const cwd = path.resolve(opts.cwd);
const inferred = await inferCommands(cwd);
const buildCmd = opts.buildCommand !== undefined ? opts.buildCommand : inferred.build;
const testCmd = opts.testCommand !== undefined ? opts.testCommand : inferred.test;
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
const logBudget = opts.logBudgetBytes ?? DEFAULT_LOG_BUDGET;
const build = await runCommandOrSkip({
label: 'build',
command: buildCmd,
cwd,
timeoutMs,
logBudget,
spawnFn: opts.spawnFn,
skipReason: 'no build / typecheck command resolved (set --build-command or add `typecheck` to package.json scripts)',
});
const tests = await runCommandOrSkip({
label: 'tests',
command: testCmd,
cwd,
timeoutMs,
logBudget,
spawnFn: opts.spawnFn,
skipReason: 'no test command resolved (set --test-command or add `test` to package.json scripts)',
});
const signals = computeSignals({ build, tests });
return { build, tests, signals, endedAt: new Date().toISOString() };
}
// Persist the report under the project cwd in the canonical layout the
// SKILL.md fragment locks. Returns the file paths so the caller can
// echo them on the run's SSE stream.
export async function writeBuildTestReport(args: {
cwd: string;
report: BuildTestReport;
}): Promise<{ jsonPath: string; logPath: string }> {
const dir = path.join(args.cwd, 'critique');
await fsp.mkdir(dir, { recursive: true });
const jsonPath = path.join(dir, 'build-test.json');
const logPath = path.join(dir, 'build-test.log');
const json = {
build: statusOnly(args.report.build),
tests: statusOnly(args.report.tests),
durationMs: args.report.build.durationMs + args.report.tests.durationMs,
commandsRun: [args.report.build.command, args.report.tests.command].filter(Boolean),
failures: [args.report.build, args.report.tests]
.filter((c) => c.status === 'failing')
.map((c) => ({ command: c.command, exitCode: c.exitCode })),
endedAt: args.report.endedAt,
signals: args.report.signals,
};
await fsp.writeFile(jsonPath, JSON.stringify(json, null, 2) + '\n', 'utf8');
const logBody = [
`# build (${args.report.build.status}) — ${args.report.build.command || '<skipped>'}`,
args.report.build.log,
'',
`# tests (${args.report.tests.status}) — ${args.report.tests.command || '<skipped>'}`,
args.report.tests.log,
].join('\n');
await fsp.writeFile(logPath, logBody, 'utf8');
return { jsonPath, logPath };
}
interface InferredCommands {
build: string | null;
test: string | null;
}
async function inferCommands(cwd: string): Promise<InferredCommands> {
try {
const raw = await fsp.readFile(path.join(cwd, 'package.json'), 'utf8');
const pkg = JSON.parse(raw) as { scripts?: Record<string, string> };
const scripts = pkg.scripts ?? {};
const pmRunner = await detectPackageManager(cwd);
const buildScript = scripts.typecheck ?? scripts.build ?? null;
const testScript = scripts.test ?? null;
return {
build: buildScript ? `${pmRunner} run ${pickScriptName(scripts, ['typecheck', 'build'])}` : null,
test: testScript ? `${pmRunner} run test` : null,
};
} catch {
return { build: null, test: null };
}
}
function pickScriptName(scripts: Record<string, string>, preferred: string[]): string {
for (const name of preferred) if (scripts[name]) return name;
return preferred[0]!;
}
async function detectPackageManager(cwd: string): Promise<'pnpm' | 'npm' | 'yarn'> {
if (await pathExists(path.join(cwd, 'pnpm-lock.yaml'))) return 'pnpm';
if (await pathExists(path.join(cwd, 'yarn.lock'))) return 'yarn';
return 'npm';
}
async function pathExists(p: string): Promise<boolean> {
try { await fsp.access(p); return true; } catch { return false; }
}
interface RunCommandArgs {
label: 'build' | 'tests';
command: string | null | undefined;
cwd: string;
timeoutMs: number;
logBudget: number;
spawnFn: BuildTestRunOptions['spawnFn'];
skipReason: string;
}
async function runCommandOrSkip(args: RunCommandArgs): Promise<BuildTestCommandResult> {
if (!args.command) {
return {
command: '',
exitCode: 0,
durationMs: 0,
status: 'skipped',
log: '',
reason: args.skipReason,
};
}
return runShell({
command: args.command,
cwd: args.cwd,
timeoutMs: args.timeoutMs,
logBudget: args.logBudget,
spawnFn: args.spawnFn,
});
}
interface RunShellArgs {
command: string;
cwd: string;
timeoutMs: number;
logBudget: number;
spawnFn?: BuildTestRunOptions['spawnFn'];
}
function runShell(args: RunShellArgs): Promise<BuildTestCommandResult> {
return new Promise((resolve) => {
const startedAt = Date.now();
const launcher = args.spawnFn ?? spawn;
const child = launcher('sh', ['-c', args.command], {
cwd: args.cwd,
stdio: ['ignore', 'pipe', 'pipe'],
});
let buffer = '';
let truncated = false;
const onChunk = (chunk: Buffer | string) => {
const text = typeof chunk === 'string' ? chunk : chunk.toString('utf8');
const remaining = args.logBudget - buffer.length;
if (remaining <= 0) { truncated = true; return; }
buffer += text.length > remaining ? text.slice(0, remaining) : text;
if (text.length > remaining) truncated = true;
};
child.stdout?.on('data', onChunk);
child.stderr?.on('data', onChunk);
const timer = setTimeout(() => {
try { child.kill('SIGTERM'); } catch { /* already dead */ }
}, args.timeoutMs);
timer.unref?.();
child.on('close', (code, signal) => {
clearTimeout(timer);
const exitCode = typeof code === 'number' ? code : (signal ? 124 : 1);
const status: BuildTestStatus = exitCode === 0 ? 'passing' : 'failing';
resolve({
command: args.command,
exitCode,
durationMs: Date.now() - startedAt,
status,
log: truncated ? buffer + '\n…[truncated]' : buffer,
});
});
child.on('error', (err) => {
clearTimeout(timer);
resolve({
command: args.command,
exitCode: 1,
durationMs: Date.now() - startedAt,
status: 'failing',
log: `[spawn-error] ${err.message ?? String(err)}`,
});
});
});
}
function statusOnly(c: BuildTestCommandResult) {
const out: Record<string, unknown> = {
command: c.command,
status: c.status,
exitCode: c.exitCode,
durationMs: c.durationMs,
};
if (c.reason) out.reason = c.reason;
return out;
}
function computeSignals({ build, tests }: { build: BuildTestCommandResult; tests: BuildTestCommandResult }): UntilSignals {
const buildPassing = build.status === 'passing' || build.status === 'skipped';
const testsPassing = tests.status === 'passing' || tests.status === 'skipped';
// Legacy critique.score axis: 5 when both pass, 3 when only build
// passes, 1 otherwise. spec §22.4 says the scoring is meant for
// pipelines that already read critique.score; new pipelines should
// read build.passing / tests.passing directly.
const score = buildPassing && testsPassing ? 5
: buildPassing ? 3
: 1;
return {
'build.passing': buildPassing,
'tests.passing': testsPassing,
'critique.score': score,
};
}