forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfailures.mjs
More file actions
188 lines (168 loc) · 5.34 KB
/
Copy pathfailures.mjs
File metadata and controls
188 lines (168 loc) · 5.34 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
// @ts-check
import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { spawn } from 'node:child_process';
/**
* @typedef {import('../types.mjs').FailurePhase} FailurePhase
* @typedef {import('../types.mjs').FailureMetadata} FailureMetadata
*/
/**
* Extract the first meaningful line from an error string.
* Skips empty lines and lines starting with "node:".
* @param {string} error
* @returns {string}
*/
function firstMeaningfulLine(error) {
const lines = error.split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (trimmed === '') continue;
if (trimmed.startsWith('node:')) continue;
return trimmed;
}
return error.trim().slice(0, 200);
}
/**
* Compute a fingerprint hash for deduplication.
* @param {FailurePhase} phase
* @param {string} error
* @returns {string}
*/
function computeFingerprint(phase, error) {
const key = phase + '\n' + firstMeaningfulLine(error);
return createHash('sha256').update(key).digest('hex').slice(0, 16);
}
/**
* @typedef {{
* onFailureHook?: string
* }} FailureTrackerOptions
*/
/**
* Tracks and deduplicates failures, writing them to disk.
*/
export class FailureTracker {
/** @type {string} */
#failCasesDir;
/** @type {string|undefined} */
#onFailureHook;
/** @type {Map<string, string>} fingerprint -> dirName */
#known;
/** @type {number} */
#nextId;
/**
* @param {string} failCasesDir
* @param {FailureTrackerOptions} [options]
*/
constructor(failCasesDir, options = {}) {
this.#failCasesDir = failCasesDir;
this.#onFailureHook = options.onFailureHook;
this.#known = new Map();
this.#nextId = 1;
// Scan existing failure directories to resume numbering and dedup
if (fs.existsSync(failCasesDir)) {
const entries = fs.readdirSync(failCasesDir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const match = entry.name.match(/^(\d+)-/);
if (!match) continue;
const id = parseInt(match[1], 10);
if (id >= this.#nextId) {
this.#nextId = id + 1;
}
// Load fingerprint from metadata if available
const metaPath = path.join(failCasesDir, entry.name, 'metadata.json');
if (fs.existsSync(metaPath)) {
try {
const meta = JSON.parse(fs.readFileSync(metaPath, 'utf-8'));
if (meta.fingerprint) {
this.#known.set(meta.fingerprint, entry.name);
}
} catch {
// Ignore corrupt metadata
}
}
}
}
}
/**
* Record a failure. Returns the failure directory path if new, null if duplicate.
*
* @param {number} seed
* @param {FailurePhase} phase
* @param {string} error
* @param {{ 'main.swift': string, 'harness.mjs': string }} files
* @param {{ typesInvolved?: string[] }} [extra]
* @returns {Promise<string|null>}
*/
async record(seed, phase, error, files, extra = {}) {
const fingerprint = computeFingerprint(phase, error);
// Check for duplicate
if (this.#known.has(fingerprint)) {
return null;
}
// Create failure directory
const idStr = String(this.#nextId).padStart(3, '0');
const dirName = `${idStr}-${phase}`;
const failDir = path.join(this.#failCasesDir, dirName);
fs.mkdirSync(failDir, { recursive: true });
this.#known.set(fingerprint, dirName);
this.#nextId++;
// Write files
fs.writeFileSync(path.join(failDir, 'seed.json'), JSON.stringify({ seed }, null, 2) + '\n');
fs.writeFileSync(path.join(failDir, 'main.swift'), files['main.swift']);
fs.writeFileSync(path.join(failDir, 'harness.mjs'), files['harness.mjs']);
fs.writeFileSync(path.join(failDir, 'error.txt'), error);
/** @type {FailureMetadata} */
const metadata = {
id: dirName,
phase,
fingerprint,
timestamp: new Date().toISOString(),
seed,
typesInvolved: extra.typesInvolved ?? [],
error: error.slice(0, 2000),
};
const metadataJson = JSON.stringify(metadata, null, 2) + '\n';
fs.writeFileSync(path.join(failDir, 'metadata.json'), metadataJson);
// Invoke hook if configured
if (this.#onFailureHook) {
try {
const hook = spawn(this.#onFailureHook, [failDir, phase, fingerprint], {
stdio: ['pipe', 'inherit', 'inherit'],
});
hook.stdin.write(metadataJson);
hook.stdin.end();
// Don't await — fire and forget
hook.on('error', () => { /* ignore hook errors */ });
} catch {
// Ignore hook spawn errors
}
}
return failDir;
}
/**
* List all recorded failures.
* @returns {FailureMetadata[]}
*/
list() {
/** @type {FailureMetadata[]} */
const results = [];
if (!fs.existsSync(this.#failCasesDir)) {
return results;
}
const entries = fs.readdirSync(this.#failCasesDir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const metaPath = path.join(this.#failCasesDir, entry.name, 'metadata.json');
if (!fs.existsSync(metaPath)) continue;
try {
const meta = JSON.parse(fs.readFileSync(metaPath, 'utf-8'));
results.push(meta);
} catch {
// Skip corrupt entries
}
}
return results.sort((a, b) => a.id.localeCompare(b.id));
}
}