traceyard 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +217 -2
- package/lib/cli/bin/traceyard.mjs +4 -0
- package/lib/cli/package.json +5 -0
- package/lib/cli/src/compact.mjs +73 -0
- package/lib/cli/src/errors.mjs +9 -0
- package/lib/cli/src/git.mjs +81 -0
- package/lib/cli/src/github.mjs +137 -0
- package/lib/cli/src/main.mjs +780 -0
- package/lib/cli/src/mcp.mjs +304 -0
- package/lib/cli/src/receipt.mjs +143 -0
- package/lib/cli/src/run.mjs +457 -0
- package/lib/cli/src/verdicts.mjs +189 -0
- package/lib/cli/src/version.mjs +5 -0
- package/lib/core/src/agent-result.mjs +128 -0
- package/lib/core/src/ci-link.mjs +87 -0
- package/lib/core/src/discovery.mjs +105 -0
- package/lib/core/src/failure-summary.mjs +39 -0
- package/lib/core/src/index.mjs +70 -0
- package/lib/core/src/package-manager.mjs +97 -0
- package/lib/core/src/pytest-runner.py +68 -0
- package/lib/core/src/pytest.mjs +118 -0
- package/lib/core/src/repository-index.mjs +310 -0
- package/lib/core/src/runners.mjs +103 -0
- package/lib/core/src/source-flow.mjs +135 -0
- package/lib/core/src/test-evidence.mjs +311 -0
- package/lib/core/src/test-report.mjs +64 -0
- package/lib/core/src/worktree.mjs +95 -0
- package/package.json +45 -4
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { readFile, realpath, mkdtemp, rm } from "node:fs/promises";
|
|
2
|
+
import { join, resolve, relative, isAbsolute } from "node:path";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { summarizeCommandFailure } from "./failure-summary.mjs";
|
|
6
|
+
import { relatedTests } from "./repository-index.mjs";
|
|
7
|
+
import { isPytestFile } from "./pytest.mjs";
|
|
8
|
+
import { adapterForFile, adapterForScript, runnerAdapters } from "./runners.mjs";
|
|
9
|
+
import { detectPackageManager, scriptCommand } from "./package-manager.mjs";
|
|
10
|
+
|
|
11
|
+
import { sanitizeDiagnostic, normalizeVitestReport } from "./test-report.mjs";
|
|
12
|
+
export { sanitizeDiagnostic, normalizeVitestReport };
|
|
13
|
+
export function selectTest(index, question, testPath) {
|
|
14
|
+
const tests = index.files.filter((f) => f.isTest);
|
|
15
|
+
if (testPath) {
|
|
16
|
+
const test = tests.find((f) => f.path === testPath);
|
|
17
|
+
return test
|
|
18
|
+
? { test }
|
|
19
|
+
: {
|
|
20
|
+
reason: "The selected test file is not present in this snapshot's index.",
|
|
21
|
+
candidates: [],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const words =
|
|
25
|
+
question
|
|
26
|
+
.toLowerCase()
|
|
27
|
+
.match(/[a-z0-9_-]{3,}/g)
|
|
28
|
+
?.filter(
|
|
29
|
+
(w) =>
|
|
30
|
+
![
|
|
31
|
+
"investigate",
|
|
32
|
+
"failing",
|
|
33
|
+
"failed",
|
|
34
|
+
"failure",
|
|
35
|
+
"tests",
|
|
36
|
+
"test",
|
|
37
|
+
"debug",
|
|
38
|
+
"please",
|
|
39
|
+
"why",
|
|
40
|
+
"the",
|
|
41
|
+
"with",
|
|
42
|
+
"and",
|
|
43
|
+
"this",
|
|
44
|
+
].includes(w),
|
|
45
|
+
) ?? [];
|
|
46
|
+
const ranked = tests
|
|
47
|
+
.map((test) => ({
|
|
48
|
+
test,
|
|
49
|
+
score: words.reduce(
|
|
50
|
+
(n, w) =>
|
|
51
|
+
n +
|
|
52
|
+
(test.path.toLowerCase().includes(w) ? 3 : 0) +
|
|
53
|
+
(test.tests.some((t) => t.name.toLowerCase().includes(w)) ? 1 : 0),
|
|
54
|
+
0,
|
|
55
|
+
),
|
|
56
|
+
}))
|
|
57
|
+
.sort((a, b) => b.score - a.score || a.test.path.localeCompare(b.test.path));
|
|
58
|
+
if (ranked.length === 1 || (ranked[0]?.score > 0 && ranked[0].score > (ranked[1]?.score ?? -1)))
|
|
59
|
+
return { test: ranked[0].test };
|
|
60
|
+
return {
|
|
61
|
+
reason: tests.length
|
|
62
|
+
? "More than one test could match. Select one of the indexed test paths."
|
|
63
|
+
: "No supported existing test was discovered.",
|
|
64
|
+
candidates: ranked.slice(0, 8).map((x) => x.test.path),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export async function prepareTargetedTest(workspace, index, plan) {
|
|
68
|
+
const selection = selectTest(index, plan.question, plan.testPath);
|
|
69
|
+
if (!selection.test)
|
|
70
|
+
return {
|
|
71
|
+
reason:
|
|
72
|
+
selection.reason +
|
|
73
|
+
(selection.candidates.length ? " Candidates: " + selection.candidates.join(", ") : ""),
|
|
74
|
+
};
|
|
75
|
+
const test = selection.test;
|
|
76
|
+
if (!/^[A-Za-z0-9_./@-]+$/.test(test.path))
|
|
77
|
+
return { reason: "This test path is outside the supported command argument format." };
|
|
78
|
+
const root = await realpath(workspace);
|
|
79
|
+
const directory = await realpath(resolve(root, test.packageDirectory));
|
|
80
|
+
const file = await realpath(resolve(root, test.path));
|
|
81
|
+
const packagePath = relative(root, directory);
|
|
82
|
+
const filePath = relative(directory, file);
|
|
83
|
+
if ([packagePath, filePath].some((path) => path.startsWith("..") || isAbsolute(path)))
|
|
84
|
+
return { reason: "The selected test escapes its package workspace." };
|
|
85
|
+
const fileAdapter = adapterForFile(test.path);
|
|
86
|
+
if (fileAdapter?.prepare) return fileAdapter.prepare(directory, file, test, plan);
|
|
87
|
+
if (plan.testCase !== undefined) return { reason: "A case selector requires a Pytest file." };
|
|
88
|
+
let pkg;
|
|
89
|
+
try {
|
|
90
|
+
pkg = JSON.parse(await readFile(join(directory, "package.json"), "utf8"));
|
|
91
|
+
} catch {
|
|
92
|
+
return { reason: "The selected test needs a readable package.json." };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const candidates = Object.entries(pkg.scripts ?? {}).flatMap(([name, command]) => {
|
|
96
|
+
if (!/^[A-Za-z0-9:_-]+$/.test(name) || typeof command !== "string" || /[;&|\r\n]/.test(command))
|
|
97
|
+
return [];
|
|
98
|
+
const adapter = adapterForScript(command);
|
|
99
|
+
return adapter ? [{ name, runner: adapter.name }] : [];
|
|
100
|
+
});
|
|
101
|
+
const matching = candidates.filter(({ name }) => {
|
|
102
|
+
const kind = name.replace(/^test:/, "").toLowerCase();
|
|
103
|
+
return name.startsWith("test:") && test.path.toLowerCase().split("/").includes(kind);
|
|
104
|
+
});
|
|
105
|
+
const selected =
|
|
106
|
+
candidates.find(({ name }) => name === "test") ??
|
|
107
|
+
(matching.length === 1 ? matching[0] : candidates.length === 1 ? candidates[0] : null);
|
|
108
|
+
if (!selected)
|
|
109
|
+
return {
|
|
110
|
+
reason:
|
|
111
|
+
"Select a test in an npm package with a clear Vitest or Jest script. Use a direct test command rather than a script that chains several checks.",
|
|
112
|
+
};
|
|
113
|
+
if (plan.replayRecording && !runnerAdapters[selected.runner].supports.replay)
|
|
114
|
+
return { reason: "Isolated interaction replay requires Vitest." };
|
|
115
|
+
return {
|
|
116
|
+
root,
|
|
117
|
+
directory,
|
|
118
|
+
file,
|
|
119
|
+
runner: selected.runner,
|
|
120
|
+
command: { directory: test.packageDirectory, script: selected.name, testPath: test.path },
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Options:
|
|
126
|
+
* - `replay(root, command, plan)`: runs a recorded replay. Only the hosted runner
|
|
127
|
+
* provides it, because it needs Docker; without it replay plans are blocked.
|
|
128
|
+
* - `packageManager`: force "npm" | "pnpm" | "yarn" | "bun" instead of detecting it.
|
|
129
|
+
*/
|
|
130
|
+
export async function runTargetedTest(workspace, index, plan, env, options = {}) {
|
|
131
|
+
const started = Date.now();
|
|
132
|
+
const blocked = (message, command = { directory: ".", script: "", testPath: null }) => ({
|
|
133
|
+
status: "BLOCKED",
|
|
134
|
+
counts: { passed: 0, failed: 0, skipped: 0 },
|
|
135
|
+
failures: [],
|
|
136
|
+
truncated: false,
|
|
137
|
+
message: sanitizeDiagnostic(message, 2000),
|
|
138
|
+
durationMs: Date.now() - started,
|
|
139
|
+
exitCode: null,
|
|
140
|
+
nodeVersion: process.version,
|
|
141
|
+
command,
|
|
142
|
+
});
|
|
143
|
+
const prepared = await prepareTargetedTest(workspace, index, plan);
|
|
144
|
+
if (prepared.reason) return blocked(prepared.reason);
|
|
145
|
+
const { root, directory, file, runner, command } = prepared;
|
|
146
|
+
if (plan.replayRecording)
|
|
147
|
+
return options.replay
|
|
148
|
+
? options.replay(root, command, plan)
|
|
149
|
+
: blocked("Recorded replay needs the isolated Traceyard runner.", command);
|
|
150
|
+
const adapter = runnerAdapters[runner];
|
|
151
|
+
const path = relative(directory, file).split("\\").join("/");
|
|
152
|
+
const testArgument = path.startsWith("-") ? "./" + path : path;
|
|
153
|
+
const temporary = await mkdtemp(join(tmpdir(), "traceyard-evidence-")),
|
|
154
|
+
output = join(temporary, "report.json");
|
|
155
|
+
let tail = "",
|
|
156
|
+
timedOut = false;
|
|
157
|
+
try {
|
|
158
|
+
const isPytest = adapter.language === "python";
|
|
159
|
+
if (isPytest && !env.TRACEYARD_PYTEST_PYTHON)
|
|
160
|
+
return blocked("Install this package's Pytest dependencies before rerunning.", command);
|
|
161
|
+
const runnerArgs = adapter.args({ testArgument, output, plan });
|
|
162
|
+
const invocation = isPytest
|
|
163
|
+
? { program: env.TRACEYARD_PYTEST_PYTHON, args: runnerArgs }
|
|
164
|
+
: scriptCommand(
|
|
165
|
+
options.packageManager ?? (await detectPackageManager(root, directory)),
|
|
166
|
+
command.script,
|
|
167
|
+
runnerArgs,
|
|
168
|
+
);
|
|
169
|
+
let evidenceError;
|
|
170
|
+
const exitCode = await new Promise((done, reject) => {
|
|
171
|
+
const child = spawn(invocation.program, invocation.args, {
|
|
172
|
+
cwd: directory,
|
|
173
|
+
env: adapter.environment ? adapter.environment(env) : env,
|
|
174
|
+
shell: !isPytest && process.platform === "win32",
|
|
175
|
+
detached: process.platform !== "win32",
|
|
176
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
177
|
+
});
|
|
178
|
+
const capture = (chunk) => {
|
|
179
|
+
tail = (tail + chunk.toString()).slice(-16000);
|
|
180
|
+
};
|
|
181
|
+
child.stdout.on("data", capture);
|
|
182
|
+
child.stderr.on("data", capture);
|
|
183
|
+
const timer = setTimeout(() => {
|
|
184
|
+
timedOut = true;
|
|
185
|
+
try {
|
|
186
|
+
if (process.platform === "win32") child.kill("SIGKILL");
|
|
187
|
+
else process.kill(-child.pid, "SIGKILL");
|
|
188
|
+
} catch {
|
|
189
|
+
/* Already stopped. */
|
|
190
|
+
}
|
|
191
|
+
}, plan.timeoutMs);
|
|
192
|
+
child.on("error", (error) => {
|
|
193
|
+
clearTimeout(timer);
|
|
194
|
+
reject(error);
|
|
195
|
+
});
|
|
196
|
+
child.on("close", (code) => {
|
|
197
|
+
clearTimeout(timer);
|
|
198
|
+
if (process.platform !== "win32")
|
|
199
|
+
try {
|
|
200
|
+
process.kill(-child.pid, "SIGTERM");
|
|
201
|
+
} catch {
|
|
202
|
+
/* No children remain. */
|
|
203
|
+
}
|
|
204
|
+
done(code);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
let result;
|
|
208
|
+
const retainedOutput = sanitizeDiagnostic(tail.slice(-4000), 4000);
|
|
209
|
+
try {
|
|
210
|
+
const text = await readFile(output, "utf8");
|
|
211
|
+
if (Buffer.byteLength(text) > 8_000_000)
|
|
212
|
+
return blocked("The structured reporter exceeded the supported artifact size.");
|
|
213
|
+
if (timedOut)
|
|
214
|
+
return {
|
|
215
|
+
...blocked("The focused test exceeded its time budget.", command),
|
|
216
|
+
output: retainedOutput,
|
|
217
|
+
};
|
|
218
|
+
const data = JSON.parse(text);
|
|
219
|
+
try {
|
|
220
|
+
result = adapter.normalize(data, exitCode, { root, directory, file, command });
|
|
221
|
+
} catch (error) {
|
|
222
|
+
if (isPytest) evidenceError = error.message;
|
|
223
|
+
throw error;
|
|
224
|
+
}
|
|
225
|
+
if (result.blocked) return { ...blocked(result.blocked, command), output: retainedOutput };
|
|
226
|
+
} catch {
|
|
227
|
+
return {
|
|
228
|
+
...blocked(
|
|
229
|
+
timedOut
|
|
230
|
+
? "The focused test exceeded its time budget."
|
|
231
|
+
: (evidenceError ?? summarizeCommandFailure(tail, "test", exitCode ?? "interrupted")),
|
|
232
|
+
command,
|
|
233
|
+
),
|
|
234
|
+
output: retainedOutput,
|
|
235
|
+
exitCode,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
return {
|
|
239
|
+
...result,
|
|
240
|
+
output: retainedOutput,
|
|
241
|
+
durationMs: Date.now() - started,
|
|
242
|
+
exitCode,
|
|
243
|
+
nodeVersion: process.version,
|
|
244
|
+
command,
|
|
245
|
+
};
|
|
246
|
+
} finally {
|
|
247
|
+
await rm(temporary, { recursive: true, force: true });
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
export async function runInvestigationPlan(workspace, index, plan, env, options = {}) {
|
|
251
|
+
const started = Date.now(),
|
|
252
|
+
selection = selectTest(index, plan.question, plan.testPath);
|
|
253
|
+
if (!selection.test || isPytestFile(selection.test.path))
|
|
254
|
+
return runTargetedTest(workspace, index, plan, env, options);
|
|
255
|
+
const related = [
|
|
256
|
+
...new Set(
|
|
257
|
+
(plan.changedFiles ?? []).flatMap((file) => relatedTests(index, file).map((t) => t.path)),
|
|
258
|
+
),
|
|
259
|
+
].filter((path) => path !== selection.test.path && !isPytestFile(path));
|
|
260
|
+
const selected = [
|
|
261
|
+
selection.test.path,
|
|
262
|
+
...related.slice(0, (plan.replayRecording ? 1 : (plan.maxTestFiles ?? 1)) - 1),
|
|
263
|
+
];
|
|
264
|
+
const results = [];
|
|
265
|
+
for (const path of selected) {
|
|
266
|
+
const remaining = plan.timeoutMs - (Date.now() - started);
|
|
267
|
+
if (remaining < 1000) break;
|
|
268
|
+
const result = await runTargetedTest(
|
|
269
|
+
workspace,
|
|
270
|
+
index,
|
|
271
|
+
{ ...plan, testPath: path, timeoutMs: remaining },
|
|
272
|
+
env,
|
|
273
|
+
options,
|
|
274
|
+
);
|
|
275
|
+
results.push(result);
|
|
276
|
+
if (result.status !== "SUCCEEDED") break;
|
|
277
|
+
}
|
|
278
|
+
const primary = results[0];
|
|
279
|
+
if (!primary) return runTargetedTest(workspace, index, plan, env, options);
|
|
280
|
+
const failed = results.some((r) => r.status === "FAILED"),
|
|
281
|
+
blocked = results.some((r) => r.status === "BLOCKED");
|
|
282
|
+
const counts = results.reduce(
|
|
283
|
+
(sum, r) => ({
|
|
284
|
+
passed: sum.passed + r.counts.passed,
|
|
285
|
+
failed: sum.failed + r.counts.failed,
|
|
286
|
+
skipped: sum.skipped + r.counts.skipped,
|
|
287
|
+
}),
|
|
288
|
+
{ passed: 0, failed: 0, skipped: 0 },
|
|
289
|
+
);
|
|
290
|
+
const failures = results.flatMap((r) => r.failures),
|
|
291
|
+
omittedTests = 1 + related.length - results.length;
|
|
292
|
+
return {
|
|
293
|
+
...primary,
|
|
294
|
+
status: failed ? "FAILED" : blocked ? "BLOCKED" : "SUCCEEDED",
|
|
295
|
+
counts,
|
|
296
|
+
failures: failures.slice(0, 8),
|
|
297
|
+
exitCode: failed ? 1 : blocked ? null : 0,
|
|
298
|
+
truncated: results.some((r) => r.truncated) || failures.length > 8,
|
|
299
|
+
durationMs: Date.now() - started,
|
|
300
|
+
message:
|
|
301
|
+
`${counts.passed} passed, ${counts.failed} failed across ${results.length} test file(s). ${omittedTests ? `${omittedTests} related test file(s) remain outside this attempt.` : "Selected coverage completed."} ${results.find((r) => r.status !== "SUCCEEDED")?.message ?? ""}`.slice(
|
|
302
|
+
0,
|
|
303
|
+
2000,
|
|
304
|
+
),
|
|
305
|
+
checks: results.map((r) => ({
|
|
306
|
+
testPath: r.command.testPath ?? selection.test.path,
|
|
307
|
+
status: r.status,
|
|
308
|
+
})),
|
|
309
|
+
coverage: { omittedTests },
|
|
310
|
+
};
|
|
311
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { isAbsolute, relative } from "node:path";
|
|
2
|
+
export function sanitizeDiagnostic(value, max = 1500) {
|
|
3
|
+
return String(value)
|
|
4
|
+
.replace(/Bearer\s+[A-Za-z0-9._-]+/gi, "Bearer [redacted]")
|
|
5
|
+
.replace(/(?:sk-|tyk_|tyr_)[A-Za-z0-9_-]{16,}/g, "[redacted]")
|
|
6
|
+
.replace(/((?:api[_-]?key|password|secret)\s*[=:]\s*)[^\s,;]+/gi, "$1[redacted]")
|
|
7
|
+
.slice(0, max);
|
|
8
|
+
}
|
|
9
|
+
export function normalizeVitestReport(data, exitCode, root = "") {
|
|
10
|
+
const failures = [];
|
|
11
|
+
let omitted = false;
|
|
12
|
+
for (const file of data.testResults ?? []) {
|
|
13
|
+
const path = String(file.name ?? "");
|
|
14
|
+
const fileName = (root && isAbsolute(path) ? relative(root, path) : path).split("\\").join("/");
|
|
15
|
+
for (const test of file.assertionResults ?? [])
|
|
16
|
+
if (test.status === "failed") {
|
|
17
|
+
if (failures.length === 8) {
|
|
18
|
+
omitted = true;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const raw = (test.failureMessages ?? []).join("\n");
|
|
22
|
+
if (raw.length > 1500) omitted = true;
|
|
23
|
+
failures.push({
|
|
24
|
+
file: fileName.slice(0, 240),
|
|
25
|
+
name: sanitizeDiagnostic(test.fullName ?? test.title ?? "Unnamed assertion", 250),
|
|
26
|
+
message: sanitizeDiagnostic(raw || "Assertion failed without a retained message."),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
if (
|
|
30
|
+
file.status === "failed" &&
|
|
31
|
+
!file.assertionResults?.some((t) => t.status === "failed") &&
|
|
32
|
+
failures.length < 8
|
|
33
|
+
)
|
|
34
|
+
failures.push({
|
|
35
|
+
file: fileName.slice(0, 240),
|
|
36
|
+
name: "Suite initialization",
|
|
37
|
+
message: sanitizeDiagnostic(file.message ?? "The suite could not initialize."),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const counts = {
|
|
41
|
+
passed: Math.max(0, Number(data.numPassedTests) || 0),
|
|
42
|
+
failed: Math.max(0, Number(data.numFailedTests) || 0),
|
|
43
|
+
skipped: Math.max(0, Number(data.numPendingTests) || 0),
|
|
44
|
+
};
|
|
45
|
+
const status =
|
|
46
|
+
exitCode === 0 && counts.passed > 0 && !counts.failed && !failures.length
|
|
47
|
+
? "SUCCEEDED"
|
|
48
|
+
: exitCode === 0
|
|
49
|
+
? "BLOCKED"
|
|
50
|
+
: "FAILED";
|
|
51
|
+
const text = failures.map((f) => f.message).join("\n");
|
|
52
|
+
const configuration = /No Groq API keys configured|GROQ_API_KEY_PRIMARY is missing/.test(text)
|
|
53
|
+
? " A Groq configuration guard stopped execution; check whether a mocked suite reaches its provider mock before requesting real credentials."
|
|
54
|
+
: /supabaseUrl is required|supabaseKey is required/.test(text)
|
|
55
|
+
? " Supabase initialized before the test mock; inspect fixture initialization."
|
|
56
|
+
: "";
|
|
57
|
+
return {
|
|
58
|
+
status,
|
|
59
|
+
counts,
|
|
60
|
+
failures,
|
|
61
|
+
truncated: omitted,
|
|
62
|
+
message: `${counts.passed} passed, ${counts.failed} failed, ${counts.skipped} skipped.${configuration}${status === "BLOCKED" ? " No passing executed test establishes success." : ""}`,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
|
|
7
|
+
const run = promisify(execFile);
|
|
8
|
+
const shaPattern = /^[0-9a-f]{7,64}$/i;
|
|
9
|
+
|
|
10
|
+
async function git(cwd, args, options = {}) {
|
|
11
|
+
const { stdout } = await run("git", args, {
|
|
12
|
+
cwd,
|
|
13
|
+
timeout: options.timeoutMs ?? 15000,
|
|
14
|
+
env: options.env ?? process.env,
|
|
15
|
+
maxBuffer: 1024 * 1024,
|
|
16
|
+
});
|
|
17
|
+
return stdout.trim();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Build an environment that authenticates GitHub fetches without writing the token to disk. */
|
|
21
|
+
export function githubFetchEnv(token, env = process.env) {
|
|
22
|
+
if (!token) return env;
|
|
23
|
+
return {
|
|
24
|
+
...env,
|
|
25
|
+
GIT_CONFIG_COUNT: "1",
|
|
26
|
+
GIT_CONFIG_KEY_0: "http.https://github.com/.extraheader",
|
|
27
|
+
GIT_CONFIG_VALUE_0:
|
|
28
|
+
"AUTHORIZATION: basic " + Buffer.from("x-access-token:" + token).toString("base64"),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Make sure `commit` exists locally. Fetching is opt-in so local runs never
|
|
34
|
+
* touch the network unless asked to.
|
|
35
|
+
*/
|
|
36
|
+
export async function ensureCommit(repo, commit, { fetch = false, token } = {}) {
|
|
37
|
+
if (!shaPattern.test(commit)) throw Error("Use a full or abbreviated commit SHA.");
|
|
38
|
+
try {
|
|
39
|
+
return await git(repo, ["rev-parse", "--verify", "--quiet", commit + "^{commit}"]);
|
|
40
|
+
} catch {
|
|
41
|
+
if (!fetch)
|
|
42
|
+
throw Error(
|
|
43
|
+
`Commit ${commit} is not in this clone. Run \`git fetch origin ${commit}\` or allow fetching.`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
await git(repo, ["fetch", "--no-tags", "origin", commit], { env: githubFetchEnv(token) });
|
|
48
|
+
return await git(repo, ["rev-parse", "--verify", "--quiet", commit + "^{commit}"]);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
throw Error(
|
|
51
|
+
"Git could not fetch the selected commit. Confirm it is accessible from the origin remote.",
|
|
52
|
+
{ cause: error },
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Check out `commit` into a detached, throwaway worktree. The developer's own
|
|
59
|
+
* working tree and index are never touched. Always call `remove()`.
|
|
60
|
+
*/
|
|
61
|
+
export async function checkoutCommit(repo, commit, options = {}) {
|
|
62
|
+
const sha = await ensureCommit(repo, commit, options);
|
|
63
|
+
const parent = await mkdtemp(join(options.tmpdir ?? tmpdir(), "traceyard-worktree-"));
|
|
64
|
+
const path = join(parent, "checkout");
|
|
65
|
+
try {
|
|
66
|
+
await git(repo, ["worktree", "add", "--detach", "--force", path, sha], { timeoutMs: 120000 });
|
|
67
|
+
} catch (error) {
|
|
68
|
+
await rm(parent, { recursive: true, force: true });
|
|
69
|
+
throw Error("Git could not create a checkout for " + sha.slice(0, 12) + ": " + error.message, {
|
|
70
|
+
cause: error,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
let removed = false;
|
|
74
|
+
return {
|
|
75
|
+
path,
|
|
76
|
+
sha,
|
|
77
|
+
async remove() {
|
|
78
|
+
if (removed) return;
|
|
79
|
+
removed = true;
|
|
80
|
+
await git(repo, ["worktree", "remove", "--force", path]).catch(() => {});
|
|
81
|
+
await rm(parent, { recursive: true, force: true });
|
|
82
|
+
await git(repo, ["worktree", "prune"]).catch(() => {});
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Run `fn(checkout)` against an exact commit and always clean up afterwards. */
|
|
88
|
+
export async function withCommit(repo, commit, fn, options) {
|
|
89
|
+
const checkout = await checkoutCommit(repo, commit, options);
|
|
90
|
+
try {
|
|
91
|
+
return await fn(checkout);
|
|
92
|
+
} finally {
|
|
93
|
+
await checkout.remove();
|
|
94
|
+
}
|
|
95
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "traceyard",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Reproduce a failing test at the exact commit, check a fix, and record a receipt that it passed. No account, no server.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"traceyard": "lib/cli/bin/traceyard.mjs"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"lib",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=22.12.0"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@babel/parser": "^7.29.14",
|
|
20
|
+
"@modelcontextprotocol/sdk": "1.32.2",
|
|
21
|
+
"zod": "^4.6.7"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"testing",
|
|
25
|
+
"ci",
|
|
26
|
+
"reproduce",
|
|
27
|
+
"flaky-tests",
|
|
28
|
+
"mcp",
|
|
29
|
+
"coding-agents",
|
|
30
|
+
"vitest",
|
|
31
|
+
"jest",
|
|
32
|
+
"pytest"
|
|
33
|
+
],
|
|
34
|
+
"homepage": "https://github.com/fysaio/traceyard/blob/main/docs/CLI.md",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/fysaio/traceyard.git",
|
|
38
|
+
"directory": "packages/cli"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/fysaio/traceyard/issues"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public",
|
|
45
|
+
"provenance": true
|
|
46
|
+
}
|
|
47
|
+
}
|