tripwire-e2e 0.0.0-stage → 0.2.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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -3
  3. package/bin/tripwire.js +8 -0
  4. package/operators/_util.js +127 -0
  5. package/operators/agent-proposed.js +41 -0
  6. package/operators/auth-drop.js +30 -0
  7. package/operators/dead-control.js +23 -0
  8. package/operators/double-apply.js +20 -0
  9. package/operators/fake-success.js +21 -0
  10. package/operators/field-swap.js +42 -0
  11. package/operators/hide-control.js +30 -0
  12. package/operators/index.js +81 -0
  13. package/operators/item-drop.js +36 -0
  14. package/operators/item-dup.js +20 -0
  15. package/operators/label-swap.js +30 -0
  16. package/operators/missing-row-action.js +24 -0
  17. package/operators/number-drift.js +40 -0
  18. package/operators/rendered-item-drop.js +31 -0
  19. package/operators/rendered-item-dup.js +20 -0
  20. package/operators/rendered-number-drift.js +24 -0
  21. package/operators/silent-fail.js +19 -0
  22. package/operators/stale-read.js +34 -0
  23. package/operators/text-change.js +43 -0
  24. package/operators/validation-off.js +21 -0
  25. package/operators/wrong-link.js +28 -0
  26. package/operators/wrong-owner.js +41 -0
  27. package/package.json +41 -4
  28. package/policies/tripwire.clean.policy.yaml +23 -0
  29. package/policies/tripwire.mutant.policy.yaml +13 -0
  30. package/src/actions.js +59 -0
  31. package/src/agent.js +30 -0
  32. package/src/browser/mutate.js +156 -0
  33. package/src/browser/recorder.js +102 -0
  34. package/src/cli.js +41 -0
  35. package/src/commands/check.js +45 -0
  36. package/src/commands/coverage.js +110 -0
  37. package/src/commands/doctor.js +28 -0
  38. package/src/commands/fix.js +208 -0
  39. package/src/commands/list.js +24 -0
  40. package/src/commands/proxy.js +28 -0
  41. package/src/commands/report.js +11 -0
  42. package/src/commands/run.js +370 -0
  43. package/src/commands/sabotage.js +85 -0
  44. package/src/config.js +52 -0
  45. package/src/jsonpath.js +58 -0
  46. package/src/kane.js +158 -0
  47. package/src/ledger.js +53 -0
  48. package/src/mutant.js +26 -0
  49. package/src/orphans.js +40 -0
  50. package/src/profile.js +46 -0
  51. package/src/proxy.js +292 -0
  52. package/src/report.js +183 -0
package/src/proxy.js ADDED
@@ -0,0 +1,292 @@
1
+ // The Tripwire proxy: a reverse proxy in front of the app under test.
2
+ // Disarmed it passes everything through (and, in record mode, notes traffic and what the page showed).
3
+ // Armed with one mutant it changes exactly one realistic thing about the app's behaviour.
4
+ import http from 'node:http';
5
+ import net from 'node:net';
6
+ import fs from 'node:fs';
7
+ import path from 'node:path';
8
+ import { fileURLToPath } from 'node:url';
9
+ import crypto from 'node:crypto';
10
+ import { applyJsonOps, injectHtml, replaceHtml } from './actions.js';
11
+
12
+ const here = path.dirname(fileURLToPath(import.meta.url));
13
+ const RECORDER_JS = fs.readFileSync(path.join(here, 'browser/recorder.js'), 'utf8');
14
+ const MUTATE_JS = fs.readFileSync(path.join(here, 'browser/mutate.js'), 'utf8');
15
+ // Fingerprint of the code a proxy serves; a runner refuses to reuse a proxy built from older code.
16
+ export const PROXY_CODE = crypto.createHash('sha1').update(RECORDER_JS + MUTATE_JS + fs.readFileSync(fileURLToPath(import.meta.url), 'utf8')).digest('hex').slice(0, 12);
17
+
18
+ const HOP = ['connection', 'keep-alive', 'proxy-connection', 'transfer-encoding', 'upgrade', 'te', 'trailer'];
19
+ const MAX_BODY_SAMPLE = 256 * 1024;
20
+ const STATIC_EXT = /\.(js|mjs|css|png|jpe?g|gif|svg|ico|webp|woff2?|ttf|map)$/i;
21
+
22
+ export function matches(match, req) {
23
+ if (!match) return false;
24
+ if (match.document) return req.isDocument;
25
+ if (match.method && match.method !== '*' && match.method.toUpperCase() !== req.method) return false;
26
+ if (match.path) {
27
+ if (match.path.startsWith('re:')) { if (!new RegExp(match.path.slice(3)).test(req.path)) return false; }
28
+ else if (match.path !== req.path) return false;
29
+ }
30
+ return true;
31
+ }
32
+
33
+ export function createProxy({ target, port = 8080, log = () => {} }) {
34
+ const upstream = new URL(target);
35
+ const state = {
36
+ armed: null, // current mutant definition
37
+ hits: 0, // times the armed mutant actually changed something
38
+ uses: 0, // times it matched (for `times` limits)
39
+ record: false, // inject the DOM recorder + keep full bodies
40
+ test: null, // label of the test currently running (set by the runner)
41
+ seq: 0,
42
+ traffic: [],
43
+ dom: [],
44
+ lastGet: new Map(), // path -> last GET response (for stale-read)
45
+ staleFor: new Map() // path -> response as it was before the most recent write
46
+ };
47
+
48
+ const resetRun = (test) => {
49
+ state.test = test ?? null;
50
+ state.traffic = [];
51
+ state.dom = [];
52
+ state.hits = 0;
53
+ state.uses = 0;
54
+ state.lastGet.clear();
55
+ state.staleFor.clear();
56
+ };
57
+
58
+ function send(res, status, body, headers = {}) {
59
+ const data = typeof body === 'string' ? body : JSON.stringify(body);
60
+ res.writeHead(status, { 'content-type': typeof body === 'string' ? 'text/plain' : 'application/json', 'cache-control': 'no-store', ...headers });
61
+ res.end(data);
62
+ }
63
+
64
+ function readBody(req) {
65
+ return new Promise((resolve, reject) => {
66
+ const chunks = [];
67
+ req.on('data', (c) => chunks.push(c));
68
+ req.on('end', () => resolve(Buffer.concat(chunks)));
69
+ req.on('error', reject);
70
+ });
71
+ }
72
+
73
+ function forward(req, body) {
74
+ return new Promise((resolve, reject) => {
75
+ const headers = { ...req.headers, host: upstream.host };
76
+ for (const h of HOP) delete headers[h];
77
+ // Ask for plain bodies and no conditional requests: a 304 would skip the mutation.
78
+ delete headers['accept-encoding'];
79
+ delete headers['if-none-match'];
80
+ delete headers['if-modified-since'];
81
+ if (body.length) headers['content-length'] = String(body.length);
82
+ const out = http.request({ protocol: upstream.protocol, hostname: upstream.hostname, port: upstream.port || 80, method: req.method, path: req.url, headers }, (up) => {
83
+ const chunks = [];
84
+ up.on('data', (c) => chunks.push(c));
85
+ up.on('end', () => resolve({ status: up.statusCode, headers: up.headers, body: Buffer.concat(chunks) }));
86
+ up.on('error', reject);
87
+ });
88
+ out.on('error', reject);
89
+ out.end(body);
90
+ });
91
+ }
92
+
93
+ function cleanHeaders(h) {
94
+ const out = { ...h };
95
+ for (const k of [...HOP, 'content-length', 'content-encoding', 'etag', 'last-modified', 'content-security-policy', 'content-security-policy-report-only']) delete out[k];
96
+ out['cache-control'] = 'no-store';
97
+ return out;
98
+ }
99
+
100
+ const isJson = (h) => /json/i.test(h['content-type'] || '');
101
+ const isHtml = (h) => /text\/html/i.test(h['content-type'] || '');
102
+ const tryJson = (buf) => { try { return JSON.parse(buf.toString('utf8')); } catch { return undefined; } };
103
+
104
+ function record(entry) {
105
+ state.traffic.push({ seq: ++state.seq, test: state.test, ...entry });
106
+ }
107
+
108
+ function fire(note) {
109
+ state.hits++;
110
+ log(`fired ${state.armed?.id}${note ? ' (' + note + ')' : ''}`);
111
+ }
112
+
113
+ async function handleControl(req, res, url) {
114
+ const body = req.method === 'POST' ? tryJson(await readBody(req)) ?? {} : {};
115
+ switch (`${req.method} ${url.pathname}`) {
116
+ case 'GET /_tw/health': return send(res, 200, { ok: true, target, code: PROXY_CODE, armed: state.armed?.id ?? null, record: state.record });
117
+ case 'POST /_tw/arm':
118
+ if (!body || !body.id || !body.action) return send(res, 400, { error: 'mutant needs id and action' });
119
+ state.armed = body; state.hits = 0; state.uses = 0; state.staleFor.clear(); state.lastGet.clear();
120
+ log(`armed ${body.id}`);
121
+ return send(res, 200, { armed: body.id });
122
+ case 'POST /_tw/disarm':
123
+ log(`disarmed ${state.armed?.id ?? '(none)'}`);
124
+ state.armed = null; state.hits = 0; state.uses = 0;
125
+ return send(res, 200, { armed: null });
126
+ case 'POST /_tw/reset':
127
+ resetRun(body.test);
128
+ if (typeof body.record === 'boolean') state.record = body.record;
129
+ return send(res, 200, { test: state.test, record: state.record });
130
+ case 'GET /_tw/traffic': return send(res, 200, { traffic: state.traffic });
131
+ case 'GET /_tw/dom': return send(res, 200, { dom: state.dom });
132
+ case 'GET /_tw/hit': return send(res, 200, { armed: state.armed?.id ?? null, hits: state.hits, fired: state.hits > 0 });
133
+ case 'POST /_tw/dom':
134
+ for (const ev of body.events || []) state.dom.push({ test: state.test, ...ev });
135
+ return send(res, 204, '');
136
+ case 'POST /_tw/fired':
137
+ if (state.armed && body.id === state.armed.id) fire('in page');
138
+ return send(res, 204, '');
139
+ default: return send(res, 404, { error: 'unknown control endpoint' });
140
+ }
141
+ }
142
+
143
+ async function handle(req, res) {
144
+ const url = new URL(req.url, 'http://proxy');
145
+ if (url.pathname.startsWith('/_tw/')) return handleControl(req, res, url);
146
+
147
+ const reqBody = await readBody(req);
148
+ const accept = req.headers.accept || '';
149
+ const info = { method: req.method, path: url.pathname, isDocument: req.method === 'GET' && /text\/html/.test(accept) };
150
+ const m = state.armed;
151
+ const action = m?.action;
152
+ const limited = m && m.times && state.uses >= m.times;
153
+ const armedHere = m && !limited && matches(m.match, info);
154
+ const isWrite = !['GET', 'HEAD', 'OPTIONS'].includes(req.method);
155
+ const reqJson = isJson(req.headers) ? tryJson(reqBody) : undefined;
156
+
157
+ // ---- request-phase mutants: the app never sees (or sees twice) the request ----
158
+ if (armedHere && action.type === 'respond') {
159
+ state.uses++; fire(`${action.status} instead of forwarding`);
160
+ if (isWrite) snapshotStale(url.pathname);
161
+ const body = action.body === undefined ? { error: action.status >= 500 ? 'Internal Server Error' : 'Unauthorized' } : action.body;
162
+ record({ method: req.method, path: url.pathname, status: action.status, type: 'json', req: reqJson, json: body, mutated: true });
163
+ return send(res, action.status, body);
164
+ }
165
+ if (armedHere && action.type === 'stale-read' && req.method === 'GET' && state.staleFor.has(url.pathname)) {
166
+ state.uses++; fire('served the previous response');
167
+ const prev = state.staleFor.get(url.pathname);
168
+ record({ method: req.method, path: url.pathname, status: prev.status, type: 'json', json: tryJson(prev.body), mutated: true });
169
+ res.writeHead(prev.status, cleanHeaders(prev.headers));
170
+ return res.end(prev.body);
171
+ }
172
+
173
+ let up;
174
+ try {
175
+ if (isWrite) snapshotStale(url.pathname);
176
+ up = await forward(req, reqBody);
177
+ if (armedHere && action.type === 'forward-twice' && isWrite) {
178
+ state.uses++; fire('request applied twice');
179
+ up = await forward(req, reqBody);
180
+ }
181
+ } catch (err) {
182
+ return send(res, 502, { error: `tripwire proxy could not reach ${target}: ${err.message}` });
183
+ }
184
+
185
+ let status = up.status;
186
+ let body = up.body;
187
+ const headers = cleanHeaders(up.headers);
188
+ let mutated = false;
189
+
190
+ // ---- response-phase mutants ----
191
+ if (armedHere && action.type === 'status-map' && action.from.includes(status)) {
192
+ state.uses++; fire(`${status} -> ${action.to}`);
193
+ status = action.to; mutated = true;
194
+ }
195
+ if (armedHere && action.type === 'json' && isJson(up.headers)) {
196
+ const parsed = tryJson(body);
197
+ if (parsed !== undefined) {
198
+ const r = applyJsonOps(parsed, action.ops);
199
+ if (r.changed) { state.uses++; fire(); body = Buffer.from(JSON.stringify(r.body)); mutated = true; }
200
+ }
201
+ }
202
+ if (isHtml(up.headers)) {
203
+ let html = body.toString('utf8');
204
+ if (state.record) html = injectHtml(html, `<script id="__tw-recorder">${RECORDER_JS}</script>`);
205
+ if (armedHere && action.type === 'dom') {
206
+ state.uses++;
207
+ // fired is reported by the page once the element was really changed
208
+ html = injectHtml(html, `<script id="__tw-mutant">window.__TW_MUTANT=${JSON.stringify({ id: m.id, ...action.dom }).replace(/</g, '\\u003c')};${MUTATE_JS}</script>`);
209
+ mutated = true;
210
+ }
211
+ if (armedHere && action.type === 'inject-css') {
212
+ state.uses++; fire('css injected');
213
+ html = injectHtml(html, `<style id="__tw-mutant">${action.css}</style>`);
214
+ mutated = true;
215
+ }
216
+ if (armedHere && action.type === 'html-replace') {
217
+ const r = replaceHtml(html, action.find, action.replace);
218
+ if (r.changed) { state.uses++; fire(); html = r.html; mutated = true; }
219
+ }
220
+ body = Buffer.from(html);
221
+ }
222
+
223
+ if (req.method === 'GET' && !mutated && status < 400) state.lastGet.set(url.pathname, { status, headers: up.headers, body: up.body });
224
+
225
+ if (!STATIC_EXT.test(url.pathname)) {
226
+ const entry = { method: req.method, path: url.pathname, query: url.search || undefined, status, type: isJson(up.headers) ? 'json' : isHtml(up.headers) ? 'html' : 'other', mutated };
227
+ if (reqJson !== undefined) entry.req = reqJson;
228
+ if (entry.type === 'json' && body.length <= MAX_BODY_SAMPLE) entry.json = tryJson(body);
229
+ if (entry.type === 'html') entry.document = info.isDocument;
230
+ record(entry);
231
+ }
232
+
233
+ res.writeHead(status, headers);
234
+ res.end(body);
235
+ }
236
+
237
+ function snapshotStale(writePath) {
238
+ // Everything read so far is now potentially out of date; remember it as the "stale" version.
239
+ for (const [p, v] of state.lastGet) if (p !== writePath) state.staleFor.set(p, v);
240
+ }
241
+
242
+ const server = http.createServer((req, res) => {
243
+ handle(req, res).catch((err) => {
244
+ log(`error: ${err.stack || err}`);
245
+ if (!res.headersSent) send(res, 500, { error: 'tripwire proxy error', detail: String(err.message || err) });
246
+ else res.end();
247
+ });
248
+ });
249
+
250
+ // Websockets and other upgrades are passed through untouched.
251
+ server.on('upgrade', (req, socket, head) => {
252
+ const conn = net.connect(upstream.port || 80, upstream.hostname, () => {
253
+ const lines = [`${req.method} ${req.url} HTTP/${req.httpVersion}`];
254
+ for (let i = 0; i < req.rawHeaders.length; i += 2) {
255
+ const k = req.rawHeaders[i];
256
+ lines.push(`${k}: ${k.toLowerCase() === 'host' ? upstream.host : req.rawHeaders[i + 1]}`);
257
+ }
258
+ conn.write(lines.join('\r\n') + '\r\n\r\n');
259
+ if (head && head.length) conn.write(head);
260
+ socket.pipe(conn).pipe(socket);
261
+ });
262
+ conn.on('error', () => socket.destroy());
263
+ socket.on('error', () => conn.destroy());
264
+ });
265
+
266
+ return {
267
+ state,
268
+ server,
269
+ listen: () => new Promise((resolve) => server.listen(port, () => resolve(server.address().port))),
270
+ close: () => new Promise((resolve) => { server.closeAllConnections?.(); server.close(() => resolve()); })
271
+ };
272
+ }
273
+
274
+ // Small client used by the runner and tests.
275
+ export function proxyClient(base) {
276
+ const call = async (method, p, body) => {
277
+ const r = await fetch(base + p, { method, headers: { 'content-type': 'application/json' }, body: body ? JSON.stringify(body) : undefined });
278
+ if (r.status === 204) return null;
279
+ const data = await r.json();
280
+ if (!r.ok) throw new Error(`${method} ${p}: ${r.status} ${data.error || ''}`);
281
+ return data;
282
+ };
283
+ return {
284
+ health: () => call('GET', '/_tw/health'),
285
+ arm: (mutant) => call('POST', '/_tw/arm', mutant),
286
+ disarm: () => call('POST', '/_tw/disarm'),
287
+ reset: (test, record) => call('POST', '/_tw/reset', { test, record }),
288
+ traffic: async () => (await call('GET', '/_tw/traffic')).traffic,
289
+ dom: async () => (await call('GET', '/_tw/dom')).dom,
290
+ hit: () => call('GET', '/_tw/hit')
291
+ };
292
+ }
package/src/report.js ADDED
@@ -0,0 +1,183 @@
1
+ // Static HTML report and README badge, built from .tripwire/ state. No server, no external assets.
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import AdmZip from 'adm-zip';
5
+ import { readLedger, latest, lastRunId, score, perTest } from './ledger.js';
6
+
7
+ const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
8
+ const slug = (s) => s.replace(/[^\w.-]+/g, '_').slice(0, 80);
9
+ const readJson = (f, d) => { try { return JSON.parse(fs.readFileSync(f, 'utf8')); } catch { return d; } };
10
+ const readLines = (f) => (fs.existsSync(f) ? fs.readFileSync(f, 'utf8').split('\n').filter(Boolean).map((l) => JSON.parse(l)) : []);
11
+
12
+ // The last screenshot of a test inside an evidence pack (the page as it was when the run ended).
13
+ export function lastScreenshot(pack) {
14
+ if (!pack || !fs.existsSync(pack)) return null;
15
+ try {
16
+ const zip = new AdmZip(pack);
17
+ // Kane writes steps/<ordinal>-<id>/screenshot.jpg (older packs: .png)
18
+ const shots = zip.getEntries().map((e) => e.entryName).filter((n) => /\/steps\/[^/]+\/screenshot\.(png|jpe?g)$/.test(n));
19
+ if (!shots.length) return null;
20
+ const ord = (n) => Number((n.match(/\/steps\/(\d+)/) || [])[1] || 0);
21
+ shots.sort((a, b) => ord(a) - ord(b));
22
+ return zip.getEntry(shots[shots.length - 1]).getData();
23
+ } catch { return null; }
24
+ }
25
+
26
+ // File extension from the image's magic bytes.
27
+ export const imageExt = (buf) => (buf[0] === 0x89 && buf[1] === 0x50 ? 'png' : 'jpg');
28
+
29
+ export function badge(scoreValue) {
30
+ const label = 'tripwire';
31
+ const value = scoreValue === null || scoreValue === undefined ? 'n/a' : `${Math.round(scoreValue)}%`;
32
+ const color = scoreValue === null || scoreValue === undefined ? '#9f9f9f' : scoreValue >= 80 ? '#2e7d32' : scoreValue >= 60 ? '#b26a00' : '#c62828';
33
+ const lw = 58, vw = 8 + value.length * 7.5;
34
+ return `<svg xmlns="http://www.w3.org/2000/svg" width="${lw + vw}" height="20" role="img" aria-label="${label}: ${value}"><title>${label}: ${value}</title><rect width="${lw}" height="20" fill="#444"/><rect x="${lw}" width="${vw}" height="20" fill="${color}"/><g fill="#fff" font-family="Verdana,DejaVu Sans,sans-serif" font-size="11" text-anchor="middle"><text x="${lw / 2}" y="14">${label}</text><text x="${lw + vw / 2}" y="14">${value}</text></g></svg>`;
35
+ }
36
+
37
+ export function writeReport(dir) {
38
+ const out = path.join(dir, 'report');
39
+ const shotsDir = path.join(out, 'shots');
40
+ fs.mkdirSync(shotsDir, { recursive: true });
41
+ const all = readLedger(dir);
42
+ const runId = lastRunId(all);
43
+ const rows = latest(all, runId);
44
+ const s = score(rows);
45
+ // the same run before any proven fix: what the agent's first-draft suite caught on its own
46
+ const firstDraft = score(latest(all.filter((r) => !r.fixed), runId));
47
+ const fixedCount = rows.filter((r) => r.fixed).length;
48
+ const tests = perTest(rows);
49
+ const baseline = readJson(path.join(dir, 'baseline.json'), { tests: [] });
50
+ const summary = readJson(path.join(dir, 'runs', runId || '-', 'summary.json'), {});
51
+ const fixes = readLines(path.join(dir, 'fixes.jsonl'));
52
+ const equivalents = readJson(path.join(dir, 'equivalents.json'), []);
53
+ const lessons = fs.existsSync(path.join(dir, 'lessons.md')) ? fs.readFileSync(path.join(dir, 'lessons.md'), 'utf8') : '';
54
+ const coverage = readJson(path.join(dir, 'coverage.json'), null);
55
+
56
+ // score history: one point per run (first-draft vs now)
57
+ const runs = [...new Set(all.map((r) => r.run))];
58
+ const history = runs.map((r) => ({ run: r, ...score(latest(all, r)) })).filter((h) => h.score !== null);
59
+
60
+ const shot = (row) => {
61
+ const run = row.status === 'killed' ? row.runs?.find((x) => x.code === 1) : row.status === 'masked' ? null : row.runs?.find((x) => x.code === 0 && x.fired);
62
+ const buf = lastScreenshot(run?.pack);
63
+ if (!buf) return '';
64
+ const name = `${slug(row.id)}.${imageExt(buf)}`;
65
+ fs.writeFileSync(path.join(shotsDir, name), buf);
66
+ return `<figure><img src="shots/${esc(name)}" alt="The broken page as the test left it" loading="lazy"><figcaption>The broken app at the end of ${esc(run.test)}. The test still passed.</figcaption></figure>`;
67
+ };
68
+
69
+ // Kane triages each failed replay on its own. Tripwire knows the app was broken on purpose,
70
+ // so it can show how that triage read a real product bug.
71
+ const triage = (r) => {
72
+ const t = r.runs?.find((x) => x.kaneTriage)?.kaneTriage;
73
+ if (!t) return '';
74
+ const product = t.family === 'product_bug' || t.confirmed;
75
+ return `<p class="triage">Kane's triage of the failure: <strong>${esc((t.family || '').replace(/_/g, ' '))}</strong>${t.category ? ` / ${esc(t.category.replace(/_/g, ' '))}` : ''}${t.confidence ? ` (${Math.round(t.confidence * 100)}%)` : ''}. ${product ? 'It identified a product bug.' : 'The app really was broken: this was a product bug.'}</p>`;
76
+ };
77
+ const triaged = rows.filter((r) => r.status === 'killed' && r.runs?.some((x) => x.kaneTriage));
78
+ const triagedAsTest = triaged.filter((r) => { const t = r.runs.find((x) => x.kaneTriage).kaneTriage; return !(t.family === 'product_bug' || t.confirmed); });
79
+
80
+ const byStatus = (st) => rows.filter((r) => r.status === st);
81
+ const fixFor = new Map(fixes.filter((f) => f.result === 'killed').map((f) => [f.id, f]));
82
+ const card = (r, withShot) => `
83
+ <article class="mutant ${r.status}">
84
+ <header><span class="pill ${r.status}">${esc(r.status.replace('-', ' '))}</span><span class="op">${esc(r.family)} · ${esc(r.operator)}</span></header>
85
+ <h3>${esc(r.describe)}</h3>
86
+ ${r.expect ? `<p class="expect">A good test would: ${esc(r.expect.charAt(0).toLowerCase() + r.expect.slice(1))}.</p>` : ''}
87
+ <p class="meta">Tests that walked past it: ${(r.touches_tests || []).map((t) => `<code>${esc(t)}</code>`).join(' ')}</p>
88
+ ${r.suspectedEquivalent ? `<p class="warn">Suspected equivalent: ${esc(r.suspectedEquivalent)} If a person agrees, add it to <code>.tripwire/equivalents.json</code> with the reason; it then leaves the score and is listed below.</p>` : ''}
89
+ ${r.status === 'masked' ? `<p class="warn">With healing off, <code>${esc(r.killed_by)}</code> failed. With Kane's default healing it re-authored its steps and <strong>passed on the broken app</strong>.</p>` : ''}
90
+ ${triage(r)}
91
+ ${fixFor.has(r.id) ? `<p class="fixed">Fixed by <code>tripwire fix</code>: clean run passed, mutant run failed (${esc(fixFor.get(r.id).proof?.clean?.session || '')} / ${esc(fixFor.get(r.id).proof?.mutant?.session || '')}).</p>` : ''}
92
+ ${withShot ? shot(r) : ''}
93
+ <details><summary>Mutant id</summary><code>${esc(r.id)}</code></details>
94
+ </article>`;
95
+
96
+ const pct = (v) => (v === null || v === undefined ? '–' : `${v}%`);
97
+ const minutes = Math.round(rows.reduce((a, r) => a + (r.runs || []).reduce((b, x) => b + (x.durationMs || 0), 0), 0) / 600) / 100;
98
+ const perMutant = rows.length ? Math.round((minutes * 60) / rows.length) : 0;
99
+
100
+ const html = `<!doctype html>
101
+ <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
102
+ <title>Tripwire report</title>
103
+ <style>
104
+ :root{--bg:#f6f5f1;--surface:#fff;--ink:#1c1b19;--muted:#66625b;--line:#e2ded5;--killed:#2e7d32;--survived:#c62828;--masked:#8e24aa;--nr:#6d6d6d;--accent:#2f5d50}
105
+ @media (prefers-color-scheme:dark){:root{--bg:#141412;--surface:#1d1c1a;--ink:#ecebe7;--muted:#a19d95;--line:#33312d;--killed:#66bb6a;--survived:#ef5350;--masked:#ce93d8;--nr:#9e9e9e;--accent:#7fb7a5}}
106
+ *{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--ink);font:16px/1.55 system-ui,-apple-system,"Segoe UI",sans-serif}
107
+ main{max-width:1040px;margin:0 auto;padding:32px 16px 80px}h1{font-size:1.5rem;margin:0}h2{font-size:1.15rem;margin:40px 0 12px}h3{font-size:1rem;margin:8px 0}
108
+ .top{display:flex;flex-wrap:wrap;gap:24px;align-items:flex-end;justify-content:space-between;border-bottom:1px solid var(--line);padding-bottom:24px}
109
+ .score{font-size:3.5rem;font-weight:700;line-height:1}.sub{color:var(--muted)}
110
+ .counts{display:flex;flex-wrap:wrap;gap:8px}.count{background:var(--surface);border:1px solid var(--line);border-radius:8px;padding:8px 14px;min-width:110px}.count b{display:block;font-size:1.4rem}
111
+ .count.killed b{color:var(--killed)}.count.survived b{color:var(--survived)}.count.masked b{color:var(--masked)}.count.nr b{color:var(--nr)}
112
+ table{width:100%;border-collapse:collapse;background:var(--surface);border:1px solid var(--line);border-radius:8px;overflow:hidden}th,td{text-align:left;padding:10px 12px;border-bottom:1px solid var(--line);vertical-align:top}th{font-size:.85rem;color:var(--muted)}
113
+ .grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:12px}
114
+ .mutant{background:var(--surface);border:1px solid var(--line);border-left:4px solid var(--nr);border-radius:8px;padding:14px}
115
+ .mutant.survived{border-left-color:var(--survived)}.mutant.masked{border-left-color:var(--masked)}.mutant.killed{border-left-color:var(--killed)}
116
+ .pill{font-size:.75rem;font-weight:600;text-transform:uppercase;letter-spacing:.04em;margin-right:8px}.pill.survived{color:var(--survived)}.pill.masked{color:var(--masked)}.pill.killed{color:var(--killed)}.pill.not-reached,.pill.error{color:var(--nr)}
117
+ .op{font-size:.8rem;color:var(--muted)}.expect,.meta{color:var(--muted);font-size:.9rem;margin:4px 0}.warn{color:var(--masked);font-size:.9rem}.triage{font-size:.85rem;color:var(--muted);border-left:2px solid var(--line);padding-left:8px}.fixed{color:var(--killed);font-size:.9rem}
118
+ figure{margin:10px 0 0}img{max-width:100%;border:1px solid var(--line);border-radius:6px}figcaption{font-size:.8rem;color:var(--muted)}
119
+ details{margin-top:8px;font-size:.8rem;color:var(--muted)}code{font-family:ui-monospace,Consolas,monospace;font-size:.85em;word-break:break-all}
120
+ .bar{height:8px;background:var(--line);border-radius:4px;overflow:hidden;min-width:80px}.bar i{display:block;height:100%;background:var(--killed)}
121
+ .history{display:flex;gap:16px;flex-wrap:wrap}.history div{background:var(--surface);border:1px solid var(--line);border-radius:8px;padding:8px 14px}
122
+ .lessons{white-space:pre-wrap;background:var(--surface);border:1px solid var(--line);border-radius:8px;padding:14px;font-size:.9rem}
123
+ .note{color:var(--muted);font-size:.9rem;max-width:70ch}
124
+ </style></head><body><main>
125
+ <section class="top">
126
+ <div><h1>Tripwire</h1><p class="sub">Would the tests fail if the app were broken? Run ${esc(runId || '–')}${summary.commit ? ` · commit ${esc(summary.commit)}` : ''}</p>
127
+ <div class="score" aria-label="mutation score">${pct(s.score)}</div><p class="sub">of reachable bugs caught (killed ÷ killed + survived + masked)</p>
128
+ ${fixedCount ? `<p class="sub"><strong>${pct(firstDraft.score)}</strong> for the first-draft suite → <strong>${pct(s.score)}</strong> after <code>tripwire fix</code> strengthened ${fixedCount} test${fixedCount > 1 ? 's' : ''}, each proven by Kane: pass on the real app, fail on the broken one.</p>` : ''}</div>
129
+ <div class="counts">
130
+ <div class="count killed"><b>${s.killed}</b>killed</div>
131
+ <div class="count survived"><b>${s.survived}</b>survived</div>
132
+ <div class="count masked"><b>${s.masked}</b>masked by heal</div>
133
+ <div class="count nr"><b>${s['not-reached']}</b>not reached</div>
134
+ </div>
135
+ </section>
136
+
137
+ ${history.length > 1 ? `<h2>Score over time</h2><div class="history">${history.map((h, i) => `<div><b>${pct(h.score)}</b><br><span class="sub">${i === 0 ? 'first run' : esc(h.run.slice(5, 16).replace('T', ' '))}</span></div>`).join('')}</div>` : ''}
138
+
139
+ <h2>Bugs the suite walked past (${s.survived})</h2>
140
+ ${s.survived ? `<div class="grid">${byStatus('survived').map((r) => card(r, true)).join('')}</div>` : '<p class="note">None. Every mutant that fired was caught.</p>'}
141
+
142
+ <h2>Masked by healing (${s.masked})</h2>
143
+ <p class="note">Strict replay caught these, but Kane's default adaptive healing re-authored the failing steps and the run passed on the broken app. Run suites that guard releases with <code>--no-adaptive-heal</code>, or gate on <code>metrics.heal.healed_tests</code>.</p>
144
+ ${s.masked ? `<div class="grid">${byStatus('masked').map((r) => card(r, false)).join('')}</div>` : ''}
145
+
146
+ <h2>Per test</h2>
147
+ <table><thead><tr><th>Test</th><th>Mutants touched</th><th>Killed</th><th>Walked past</th><th>Score</th><th>Assertions</th></tr></thead><tbody>
148
+ ${tests.map((t) => { const b = baseline.tests?.find((x) => x.test === t.test); return `<tr><td><code>${esc(t.test)}</code></td><td>${t.touched}</td><td>${t.killed}</td><td>${t.survivedPast + t.masked}</td><td><div class="bar" title="${t.score ?? '–'}%"><i style="width:${t.score ?? 0}%"></i></div>${t.score ?? '–'}%</td><td>${b?.testsWithoutChecks ? '<strong style="color:var(--survived)">none</strong>' : 'yes'}</td></tr>`; }).join('')}
149
+ </tbody></table>
150
+
151
+ ${coverage ? `<h2>Acceptance criteria</h2><p class="note">Kane reports a criterion as proven or owed. Tripwire adds a third state: <b>proven but unguarded</b>, where the tests that check it pass on the real app but a bug that broke it got past them.</p><table><thead><tr><th>Criterion</th><th>Kane</th><th>Tripwire</th><th>Bugs that got past</th></tr></thead><tbody>${coverage.map((c) => `<tr><td>${esc(c.criterion)}</td><td>${esc(c.kane)}</td><td><strong class="${c.state === 'proven but unguarded' ? 'warn' : ''}">${esc(c.state)}</strong></td><td>${(c.escaped || []).map(esc).join('<br>')}</td></tr>`).join('')}</tbody></table>` : ''}
152
+
153
+ ${triaged.length ? `<h2>How Kane's triage read the planted bugs</h2>
154
+ <p class="note">Every mutant below was a real break in the app. Kane triaged ${triaged.length} of the failing runs; it attributed <strong>${triagedAsTest.length}</strong> of them to the test rather than the product (typically "locator rot": the page changed, so the recording looks stale). From inside a single run a broken app and an outdated test look alike; Tripwire is the outside reference that tells them apart. Treat a triage of "automation bug" on a release branch as a prompt to look, not as a reason to re-record.</p>` : ''}
155
+
156
+ <h2>Caught (${s.killed})</h2>
157
+ <details><summary>Show all ${s.killed} killed mutants</summary><div class="grid">${byStatus('killed').map((r) => card(r, false)).join('')}</div></details>
158
+
159
+ <h2>Not reached (${s['not-reached']})</h2>
160
+ <p class="note">The mutant never fired during any test, so it says nothing about the assertions. It usually means no test exercises that path (for example, no test ever submits invalid input). Not counted in the score.</p>
161
+ <details><summary>Show</summary><ul>${byStatus('not-reached').map((r) => `<li>${esc(r.describe)}</li>`).join('')}</ul></details>
162
+
163
+ ${byStatus('error').length ? `<h2>Errors (${byStatus('error').length})</h2><p class="note">Kane exited 2 (error) or 3 (timeout) twice. Never counted as kills.</p><ul>${byStatus('error').map((r) => `<li>${esc(r.describe)}</li>`).join('')}</ul>` : ''}
164
+
165
+ <h2>Excluded as equivalent (${equivalents.length})</h2>
166
+ ${equivalents.length ? `<ul>${equivalents.map((e) => `<li><code>${esc(e.id)}</code> – ${esc(e.reason)}${e.approvedBy ? ` (approved by ${esc(e.approvedBy)})` : ''}</li>`).join('')}</ul>` : '<p class="note">None. Mutants are excluded only with a human-approved reason in <code>.tripwire/equivalents.json</code>.</p>'}
167
+
168
+ ${fixes.length ? `<h2>Strengthened tests (${fixes.filter((f) => f.result === 'killed').length} of ${fixes.length} attempts)</h2><table><thead><tr><th>Mutant</th><th>Test</th><th>Result</th><th>Attempts</th></tr></thead><tbody>${fixes.map((f) => `<tr><td>${esc(f.describe)}</td><td><code>${esc(f.test || '')}</code></td><td>${esc(f.result)}</td><td>${f.attempts}</td></tr>`).join('')}</tbody></table>` : ''}
169
+
170
+ ${lessons ? `<h2>Lessons for the test-writing agent</h2><div class="lessons">${esc(lessons)}</div>` : ''}
171
+
172
+ <h2>Cost</h2>
173
+ <p>${rows.length} mutants · ${minutes} minutes of Kane runs (${perMutant}s per mutant)${summary.creditsUsed !== null && summary.creditsUsed !== undefined ? ` · ${summary.creditsUsed} credits` : ''}. Replays run from Kane's cache with healing off.</p>
174
+
175
+ <h2>What this does not measure</h2>
176
+ <p class="note">Tripwire mutates traffic and the rendered page, not source code. It tells you whether the suite notices breakage a user would see. It does not replace unit-level mutation testing, and it cannot see server-side effects that never reach the browser.</p>
177
+ </main></body></html>`;
178
+
179
+ fs.writeFileSync(path.join(out, 'index.html'), html);
180
+ fs.writeFileSync(path.join(out, 'badge.svg'), badge(s.score));
181
+ fs.writeFileSync(path.join(out, 'score.json'), JSON.stringify({ run: runId, ...s }, null, 2));
182
+ return path.join(out, 'index.html');
183
+ }