tripwire-e2e 0.1.0 → 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.
@@ -1,80 +1,102 @@
1
- // Injected by the Tripwire proxy during the baseline run only.
2
- // Reports which controls and texts each page showed and which ones the test clicked,
3
- // so mutants can target things a test saw (visible) but never interacted with.
4
- (function () {
5
- if (window.__twRecorder) return;
6
- window.__twRecorder = true;
7
- var ENDPOINT = '/_tw/dom';
8
- var clean = function (s) { return (s || '').replace(/\s+/g, ' ').trim().slice(0, 120); };
9
- var roleOf = function (el) {
10
- var t = el.tagName;
11
- if (t === 'BUTTON' || el.getAttribute('role') === 'button' || (t === 'INPUT' && /submit|button/.test(el.type))) return 'button';
12
- if (t === 'A' && el.hasAttribute('href')) return 'link';
13
- if (/^H[1-3]$/.test(t)) return 'heading';
14
- return 'text';
15
- };
16
- var visible = function (el) {
17
- var r = el.getBoundingClientRect();
18
- var cs = getComputedStyle(el);
19
- return r.width > 0 && r.height > 0 && cs.visibility !== 'hidden' && cs.display !== 'none';
20
- };
21
- var describe = function (el) {
22
- return {
23
- role: roleOf(el),
24
- text: clean(el.innerText || el.value || el.getAttribute('aria-label')),
25
- label: clean(el.getAttribute('aria-label')),
26
- href: el.getAttribute('href') || undefined,
27
- inRow: !!el.closest('tr, li'),
28
- tag: el.tagName.toLowerCase(),
29
- cls: (el.className && typeof el.className === 'string') ? el.className.slice(0, 60) : undefined
30
- };
31
- };
32
- var SELECTOR = 'button, [role=button], a[href], input[type=submit], h1, h2, h3, [role=status], .status, .badge, .tag, strong';
33
- var queue = [];
34
- var timer = null;
35
- function snapshot() {
36
- var controls = [];
37
- var seen = {};
38
- document.querySelectorAll(SELECTOR).forEach(function (el) {
39
- if (el.closest('#__tw')) return;
40
- if (!visible(el)) return;
41
- var d = describe(el);
42
- if (!d.text) return;
43
- var k = d.role + '|' + d.text;
44
- seen[k] = (seen[k] || 0) + 1;
45
- d.count = seen[k];
46
- controls.push(d);
47
- });
48
- queue.push({ kind: 'page', url: location.pathname + location.search + location.hash, controls: controls, text: clean(document.body ? document.body.innerText : '').length ? (document.body.innerText || '').slice(0, 20000) : '' });
49
- flushSoon();
50
- }
51
- function flushSoon() {
52
- clearTimeout(timer);
53
- timer = setTimeout(flush, 300);
54
- }
55
- function flush() {
56
- if (!queue.length) return;
57
- var batch = queue.splice(0, queue.length);
58
- var body = JSON.stringify({ events: batch });
59
- try {
60
- if (navigator.sendBeacon) navigator.sendBeacon(ENDPOINT, new Blob([body], { type: 'application/json' }));
61
- else fetch(ENDPOINT, { method: 'POST', body: body, keepalive: true, headers: { 'content-type': 'application/json' } });
62
- } catch (e) { /* recording must never break the page */ }
63
- }
64
- var snapTimer = null;
65
- function scheduleSnapshot() { clearTimeout(snapTimer); snapTimer = setTimeout(snapshot, 250); }
66
- document.addEventListener('click', function (ev) {
67
- var el = ev.target && ev.target.closest && ev.target.closest(SELECTOR);
68
- if (!el) return;
69
- var d = describe(el);
70
- queue.push({ kind: 'click', url: location.pathname + location.search + location.hash, control: d });
71
- flushSoon();
72
- }, true);
73
- window.addEventListener('hashchange', scheduleSnapshot);
74
- window.addEventListener('pagehide', function () { snapshot(); flush(); });
75
- var start = function () {
76
- scheduleSnapshot();
77
- new MutationObserver(scheduleSnapshot).observe(document.documentElement, { childList: true, subtree: true, characterData: true });
78
- };
79
- if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start); else start();
80
- })();
1
+ // Injected by the Tripwire proxy during the baseline run only.
2
+ // Reports which controls and texts each page showed and which ones the test clicked,
3
+ // so mutants can target things a test saw (visible) but never interacted with.
4
+ (function () {
5
+ if (window.__twRecorder) return;
6
+ window.__twRecorder = true;
7
+ var ENDPOINT = '/_tw/dom';
8
+ var clean = function (s) { return (s || '').replace(/\s+/g, ' ').trim().slice(0, 120); };
9
+ var roleOf = function (el) {
10
+ var t = el.tagName;
11
+ if (t === 'BUTTON' || el.getAttribute('role') === 'button' || (t === 'INPUT' && /submit|button/.test(el.type))) return 'button';
12
+ if (t === 'A' && el.hasAttribute('href')) return 'link';
13
+ if (/^H[1-3]$/.test(t)) return 'heading';
14
+ if (t === 'INPUT' && el.type === 'checkbox') return 'checkbox';
15
+ return 'text';
16
+ };
17
+ var visible = function (el) {
18
+ var r = el.getBoundingClientRect();
19
+ var cs = getComputedStyle(el);
20
+ return r.width > 0 && r.height > 0 && cs.visibility !== 'hidden' && cs.display !== 'none';
21
+ };
22
+ // A control without visible text (icon button, checkbox) is identified by tag and first class:
23
+ // "[input.toggle]". The mutator finds it again the same way.
24
+ var anonKey = function (el) {
25
+ var c = (typeof el.className === 'string' && el.className.trim().split(/\s+/)[0]) || '';
26
+ return '[' + el.tagName.toLowerCase() + (c ? '.' + c : '') + ']';
27
+ };
28
+ var textOf = function (el) {
29
+ if (el.tagName === 'INPUT' && /checkbox|radio/.test(el.type)) return clean(el.getAttribute('aria-label') || el.title) || anonKey(el);
30
+ return clean(el.innerText || (el.tagName === 'INPUT' ? el.value : '') || el.getAttribute('aria-label') || el.title) || (el.tagName === 'BUTTON' ? anonKey(el) : '');
31
+ };
32
+ // A row is named by its title (heading, label or link), else its first cell, else its first line.
33
+ var rowName = function (row) {
34
+ var t = row.querySelector('h1, h2, h3, h4, label, a[href]');
35
+ var cell = row.tagName === 'TR' ? row.querySelector('td, th') : null;
36
+ var s = (t && clean(t.innerText)) || (cell && clean(cell.innerText)) || clean((row.innerText || '').split(/\n|\t/)[0]);
37
+ return s.slice(0, 60);
38
+ };
39
+ var describe = function (el) {
40
+ var row = el.closest('tr, li');
41
+ return {
42
+ role: roleOf(el),
43
+ text: textOf(el),
44
+ row: row ? rowName(row) : undefined,
45
+ // the words around a short text ("3" inside "3 items left"), for readable descriptions
46
+ ctx: (el.parentElement && clean(el.innerText).length < 12) ? clean(el.parentElement.innerText).slice(0, 60) : undefined,
47
+ label: clean(el.getAttribute('aria-label')),
48
+ href: el.getAttribute('href') || undefined,
49
+ inRow: !!el.closest('tr, li'),
50
+ tag: el.tagName.toLowerCase(),
51
+ cls: (el.className && typeof el.className === 'string') ? el.className.slice(0, 60) : undefined
52
+ };
53
+ };
54
+ var SELECTOR = 'button, [role=button], a[href], input[type=submit], input[type=checkbox], label, h1, h2, h3, [role=status], .status, .badge, .tag, strong';
55
+ var queue = [];
56
+ var timer = null;
57
+ function snapshot() {
58
+ var controls = [];
59
+ var seen = {};
60
+ document.querySelectorAll(SELECTOR).forEach(function (el) {
61
+ if (el.closest('#__tw')) return;
62
+ if (!visible(el)) return;
63
+ var d = describe(el);
64
+ if (!d.text) return;
65
+ var k = d.role + '|' + d.text;
66
+ seen[k] = (seen[k] || 0) + 1;
67
+ d.count = seen[k];
68
+ controls.push(d);
69
+ });
70
+ queue.push({ kind: 'page', url: location.pathname + location.search + location.hash, controls: controls, text: clean(document.body ? document.body.innerText : '').length ? (document.body.innerText || '').slice(0, 20000) : '' });
71
+ flushSoon();
72
+ }
73
+ function flushSoon() {
74
+ clearTimeout(timer);
75
+ timer = setTimeout(flush, 300);
76
+ }
77
+ function flush() {
78
+ if (!queue.length) return;
79
+ var batch = queue.splice(0, queue.length);
80
+ var body = JSON.stringify({ events: batch });
81
+ try {
82
+ if (navigator.sendBeacon) navigator.sendBeacon(ENDPOINT, new Blob([body], { type: 'application/json' }));
83
+ else fetch(ENDPOINT, { method: 'POST', body: body, keepalive: true, headers: { 'content-type': 'application/json' } });
84
+ } catch (e) { /* recording must never break the page */ }
85
+ }
86
+ var snapTimer = null;
87
+ function scheduleSnapshot() { clearTimeout(snapTimer); snapTimer = setTimeout(snapshot, 250); }
88
+ document.addEventListener('click', function (ev) {
89
+ var el = ev.target && ev.target.closest && ev.target.closest(SELECTOR);
90
+ if (!el) return;
91
+ var d = describe(el);
92
+ queue.push({ kind: 'click', url: location.pathname + location.search + location.hash, control: d });
93
+ flushSoon();
94
+ }, true);
95
+ window.addEventListener('hashchange', scheduleSnapshot);
96
+ window.addEventListener('pagehide', function () { snapshot(); flush(); });
97
+ var start = function () {
98
+ scheduleSnapshot();
99
+ new MutationObserver(scheduleSnapshot).observe(document.documentElement, { childList: true, subtree: true, characterData: true });
100
+ };
101
+ if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start); else start();
102
+ })();
package/src/cli.js CHANGED
@@ -1,37 +1,41 @@
1
- // Command-line entry. Subcommands are loaded lazily so `tripwire proxy` starts instantly.
2
- import { parseArgs } from 'node:util';
3
-
4
- const HELP = `tripwire: break the app on purpose to find out whether a green Kane suite means anything
5
-
6
- Usage:
7
- tripwire proxy --target <url> [--port 8080] run the fault-injecting proxy in front of an app
8
- tripwire run [--tests <glob>] [--max-mutants 40] baseline, generate mutants, replay, score
9
- tripwire fix [--agent claude|codex] [--max-attempts 3] strengthen tests for every survivor
10
- tripwire sabotage [--agent claude|codex] ask the agent for domain-specific mutants
11
- tripwire report rebuild the HTML report and badge
12
- tripwire list show the mutant catalogue for the last baseline
13
- tripwire check [--threshold 80] exit 1 if the score is below the threshold (for hooks)
14
-
15
- Run "tripwire <command> --help" for the options of one command.`;
16
-
17
- export async function main(argv) {
18
- const [cmd, ...rest] = argv;
19
- if (!cmd || cmd === '--help' || cmd === '-h' || cmd === 'help') { console.log(HELP); return 0; }
20
- if (cmd === '--version' || cmd === '-v') {
21
- const { readFileSync } = await import('node:fs');
22
- console.log(JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version);
23
- return 0;
24
- }
25
- switch (cmd) {
26
- case 'proxy': return (await import('./commands/proxy.js')).default(rest, parseArgs);
27
- case 'run': return (await import('./commands/run.js')).default(rest, parseArgs);
28
- case 'fix': return (await import('./commands/fix.js')).default(rest, parseArgs);
29
- case 'sabotage': return (await import('./commands/sabotage.js')).default(rest, parseArgs);
30
- case 'report': return (await import('./commands/report.js')).default(rest, parseArgs);
31
- case 'list': return (await import('./commands/list.js')).default(rest, parseArgs);
32
- case 'check': return (await import('./commands/check.js')).default(rest, parseArgs);
33
- default:
34
- console.error(`Unknown command "${cmd}".\n\n${HELP}`);
35
- return 2;
36
- }
37
- }
1
+ // Command-line entry. Subcommands are loaded lazily so `tripwire proxy` starts instantly.
2
+ import { parseArgs } from 'node:util';
3
+
4
+ const HELP = `tripwire: break the app on purpose to find out whether a green Kane suite means anything
5
+
6
+ Usage:
7
+ tripwire proxy --target <url> [--port 8080] run the fault-injecting proxy in front of an app
8
+ tripwire run [--tests <glob>] [--max-mutants 40] baseline, generate mutants, replay, score
9
+ tripwire fix [--agent claude|codex] [--max-attempts 3] strengthen tests for every survivor
10
+ tripwire sabotage [--agent claude|codex] ask the agent for domain-specific mutants
11
+ tripwire report rebuild the HTML report and badge
12
+ tripwire list show the mutant catalogue for the last baseline
13
+ tripwire check [--threshold 80] exit 1 if the score is below the threshold (for hooks)
14
+ tripwire coverage [--criteria ACCEPTANCE.md] acceptance criteria: owed, proven, or proven but unguarded
15
+ tripwire doctor [--kill] check kane-cli, login, and leftover Kane browsers
16
+
17
+ Run "tripwire <command> --help" for the options of one command.`;
18
+
19
+ export async function main(argv) {
20
+ const [cmd, ...rest] = argv;
21
+ if (!cmd || cmd === '--help' || cmd === '-h' || cmd === 'help') { console.log(HELP); return 0; }
22
+ if (cmd === '--version' || cmd === '-v') {
23
+ const { readFileSync } = await import('node:fs');
24
+ console.log(JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version);
25
+ return 0;
26
+ }
27
+ switch (cmd) {
28
+ case 'proxy': return (await import('./commands/proxy.js')).default(rest, parseArgs);
29
+ case 'run': return (await import('./commands/run.js')).default(rest, parseArgs);
30
+ case 'fix': return (await import('./commands/fix.js')).default(rest, parseArgs);
31
+ case 'sabotage': return (await import('./commands/sabotage.js')).default(rest, parseArgs);
32
+ case 'report': return (await import('./commands/report.js')).default(rest, parseArgs);
33
+ case 'list': return (await import('./commands/list.js')).default(rest, parseArgs);
34
+ case 'check': return (await import('./commands/check.js')).default(rest, parseArgs);
35
+ case 'coverage': return (await import('./commands/coverage.js')).default(rest, parseArgs);
36
+ case 'doctor': return (await import('./commands/doctor.js')).default(rest, parseArgs);
37
+ default:
38
+ console.error(`Unknown command "${cmd}".\n\n${HELP}`);
39
+ return 2;
40
+ }
41
+ }
@@ -4,9 +4,23 @@ import path from 'node:path';
4
4
  import { loadConfig } from '../config.js';
5
5
  import { readLedger, latest, lastRunId, score } from '../ledger.js';
6
6
 
7
+ const HELP = `tripwire check [--threshold 80] [--json] [--allow-suspected-equivalents]
8
+
9
+ exit 1 if the latest mutation score is below the threshold
10
+
11
+ --allow-suspected-equivalents for the pass/fail decision only, leave out survivors that Tripwire
12
+ flagged as suspected equivalents (they fired but no page ever
13
+ changed). The reported score still counts them.`;
14
+
15
+ // The score the gate decides on: optionally without flagged suspected equivalents.
16
+ export function gateScore(rows, allowSuspected) {
17
+ const kept = allowSuspected ? rows.filter((r) => !(r.status === 'survived' && r.suspectedEquivalent)) : rows;
18
+ return { ...score(kept), setAside: rows.length - kept.length };
19
+ }
20
+
7
21
  export default async function check(argv, parseArgs) {
8
- const { values } = parseArgs({ args: argv, options: { threshold: { type: 'string' }, dir: { type: 'string' }, json: { type: 'boolean' }, help: { type: 'boolean', short: 'h' } } });
9
- if (values.help) { console.log('tripwire check [--threshold 80] [--json] exit 1 if the latest mutation score is below the threshold'); return 0; }
22
+ const { values } = parseArgs({ args: argv, options: { threshold: { type: 'string' }, dir: { type: 'string' }, json: { type: 'boolean' }, 'allow-suspected-equivalents': { type: 'boolean' }, help: { type: 'boolean', short: 'h' } } });
23
+ if (values.help) { console.log(HELP); return 0; }
10
24
  const cfg = loadConfig();
11
25
  const dir = path.resolve(values.dir || cfg.dir);
12
26
  const threshold = Number(values.threshold ?? cfg.threshold);
@@ -15,13 +29,16 @@ export default async function check(argv, parseArgs) {
15
29
  console.log('tripwire: no runs yet. Run `tripwire run` first.');
16
30
  return 1;
17
31
  }
18
- const s = score(latest(rows, lastRunId(rows)));
19
- const survivors = latest(rows, lastRunId(rows)).filter((r) => r.status === 'survived' || r.status === 'masked');
20
- const ok = s.score !== null && s.score >= threshold;
21
- if (values.json) console.log(JSON.stringify({ ok, threshold, ...s, survivors: survivors.map((r) => r.describe) }));
32
+ const current = latest(rows, lastRunId(rows));
33
+ const s = score(current);
34
+ const g = gateScore(current, values['allow-suspected-equivalents']);
35
+ const survivors = current.filter((r) => r.status === 'survived' || r.status === 'masked');
36
+ const ok = g.score !== null && g.score >= threshold;
37
+ if (values.json) console.log(JSON.stringify({ ok, threshold, ...s, gateScore: g.score, setAside: g.setAside, survivors: survivors.map((r) => r.describe) }));
22
38
  else {
23
39
  console.log(`tripwire: score ${s.score ?? '-'}% (threshold ${threshold}%) · killed ${s.killed} · survived ${s.survived} · masked ${s.masked}`);
24
- if (!ok) for (const r of survivors.slice(0, 8)) console.log(` - not caught: ${r.describe}`);
40
+ if (g.setAside) console.log(` gate: ${g.score ?? '-'}% with ${g.setAside} suspected equivalent(s) set aside (--allow-suspected-equivalents)`);
41
+ if (!ok) for (const r of survivors.filter((r) => !(values['allow-suspected-equivalents'] && r.suspectedEquivalent)).slice(0, 8)) console.log(` - not caught: ${r.describe}`);
25
42
  }
26
43
  if (fs.existsSync(path.join(dir, 'report', 'index.html')) && !values.json) console.log(` report: ${path.join(dir, 'report', 'index.html')}`);
27
44
  return ok ? 0 : 1;
@@ -0,0 +1,110 @@
1
+ // tripwire coverage: a third state on top of Kane's "proven vs owed".
2
+ // owed no test checks the criterion
3
+ // proven its tests pass on the real app and caught every mutant that broke it
4
+ // proven but unguarded its tests pass, but a mutant that broke the criterion got past them
5
+ // The agent maps criteria to tests and mutants to criteria (written to .tripwire/criteria.json,
6
+ // reviewable and editable); the states are computed from the ledger. When the project has a Kane
7
+ // assurance store (.context/), Kane's own per-criterion status is shown alongside.
8
+ import fs from 'node:fs';
9
+ import path from 'node:path';
10
+ import { loadConfig, findTests } from '../config.js';
11
+ import { readLedger, latest, lastRunId } from '../ledger.js';
12
+ import { runAgent } from '../agent.js';
13
+ import { runKane } from '../kane.js';
14
+ import { writeReport } from '../report.js';
15
+
16
+ const HELP = `tripwire coverage [options]
17
+
18
+ --criteria <file> acceptance criteria with ids like AC1, AC2 (default ACCEPTANCE.md)
19
+ --remap ask the agent to rebuild .tripwire/criteria.json
20
+ --agent claude|codex which agent maps criteria (default from config)
21
+ --dir <path> state directory (default .tripwire)`;
22
+
23
+ export function parseCriteria(md) {
24
+ const out = [];
25
+ for (const m of md.matchAll(/^\s*[-*]?\s*(AC\d+)[.:)]\s*(.+)$/gm)) out.push({ id: m[1], text: m[2].trim() });
26
+ return out;
27
+ }
28
+
29
+ // Pure: compute each criterion's state from the mapping and the ledger rows.
30
+ export function computeCoverage(criteria, mapping, rows, kaneStatus = {}) {
31
+ const testsFor = mapping.criteria || {};
32
+ const mutantsFor = {};
33
+ for (const [mid, acs] of Object.entries(mapping.mutants || {})) for (const ac of [].concat(acs)) (mutantsFor[ac] ||= []).push(mid);
34
+ const byId = new Map(rows.map((r) => [r.id, r]));
35
+ return criteria.map((c) => {
36
+ const tests = testsFor[c.id] || [];
37
+ const related = (mutantsFor[c.id] || []).map((id) => byId.get(id)).filter(Boolean).filter((r) => r.status !== 'not-reached' && r.status !== 'error');
38
+ const escaped = related.filter((r) => r.status === 'survived' || r.status === 'masked');
39
+ const caught = related.filter((r) => r.status === 'killed');
40
+ let state;
41
+ if (!tests.length) state = 'owed';
42
+ else if (escaped.length) state = 'proven but unguarded';
43
+ else if (caught.length) state = 'proven and guarded';
44
+ else state = 'proven, untested by mutants';
45
+ return {
46
+ id: c.id, criterion: `${c.id}. ${c.text}`, tests, state,
47
+ kane: kaneStatus[c.id] || (tests.length ? 'green (tests pass)' : 'owed'),
48
+ caught: caught.map((r) => r.describe), escaped: escaped.map((r) => r.describe)
49
+ };
50
+ });
51
+ }
52
+
53
+ async function kaneCover(cwd) {
54
+ if (!fs.existsSync(path.join(cwd, '.context'))) return {};
55
+ const r = await runKane(['cover', '--json'], { cwd, killAfterMs: 120000 });
56
+ const status = {};
57
+ try {
58
+ const doc = JSON.parse(r.stdout.slice(r.stdout.indexOf('{')));
59
+ JSON.stringify(doc, (k, v) => { if (v && typeof v === 'object' && /^AC\d+$/i.test(v.ac_ref || v.ref || '') && v.execution) status[(v.ac_ref || v.ref).toUpperCase()] = v.execution; return v; });
60
+ } catch { /* no usable Kane coverage */ }
61
+ return status;
62
+ }
63
+
64
+ export default async function coverage(argv, parseArgs) {
65
+ const { values } = parseArgs({ args: argv, options: { criteria: { type: 'string' }, remap: { type: 'boolean' }, agent: { type: 'string' }, dir: { type: 'string' }, help: { type: 'boolean', short: 'h' } } });
66
+ if (values.help) { console.log(HELP); return 0; }
67
+ const cfg = loadConfig();
68
+ const dir = path.resolve(values.dir || cfg.dir);
69
+ const critFile = values.criteria || 'ACCEPTANCE.md';
70
+ if (!fs.existsSync(critFile)) throw new Error(`no acceptance criteria at ${critFile}`);
71
+ const criteria = parseCriteria(fs.readFileSync(critFile, 'utf8'));
72
+ if (!criteria.length) throw new Error(`${critFile} has no criteria in the form "AC1. ..."`);
73
+ const rowsAll = readLedger(dir);
74
+ if (!rowsAll.length) throw new Error('no ledger yet: run `tripwire run` first');
75
+ const rows = latest(rowsAll, lastRunId(rowsAll));
76
+ const mapFile = path.join(dir, 'criteria.json');
77
+
78
+ if (values.remap || !fs.existsSync(mapFile)) {
79
+ const tests = findTests(cfg.tests);
80
+ const prompt = `Map acceptance criteria to end-to-end tests and to planted bugs. Write JSON to ${mapFile}:
81
+ { "criteria": { "AC1": ["tests/x_test.md", ...], ... }, "mutants": { "<mutant id>": ["AC3", ...], ... } }
82
+ - criteria: for each AC, the test files that contain a step which actually CHECKS that criterion (a "Verify ..." of its outcome). A test that only passes through a page does not count. Use [] when no test checks it.
83
+ - mutants: for each mutant id below, the ACs that the bug breaks. Use [] when it breaks none.
84
+ Be strict and literal. Do not edit any other file.
85
+
86
+ ----- Acceptance criteria -----
87
+ ${criteria.map((c) => `${c.id}. ${c.text}`).join('\n')}
88
+
89
+ ----- Tests -----
90
+ ${tests.map((t) => `# ${t}\n${fs.readFileSync(t, 'utf8')}`).join('\n\n')}
91
+
92
+ ----- Mutants (id: what breaks) -----
93
+ ${rows.map((r) => `${r.id}: ${r.describe}`).join('\n')}
94
+
95
+ Reply with "done" when the file is written.`;
96
+ fs.writeFileSync(path.join(dir, 'coverage_prompt.txt'), prompt);
97
+ console.log(`Asking ${values.agent || cfg.agent} to map ${criteria.length} criteria to tests and mutants...`);
98
+ const r = await runAgent(values.agent || cfg.agent, prompt);
99
+ if (r.code === 127) throw new Error(`could not start the agent CLI: ${r.stderr.trim()}`);
100
+ if (!fs.existsSync(mapFile)) throw new Error(`the agent did not write ${mapFile}; its reply: ${r.stdout.slice(-500)}`);
101
+ }
102
+ const mapping = JSON.parse(fs.readFileSync(mapFile, 'utf8'));
103
+ const result = computeCoverage(criteria, mapping, rows, await kaneCover(process.cwd()));
104
+ fs.writeFileSync(path.join(dir, 'coverage.json'), JSON.stringify(result, null, 2));
105
+ const count = (s) => result.filter((x) => x.state === s).length;
106
+ for (const x of result) console.log(` ${x.state.padEnd(28)} ${x.criterion.slice(0, 90)}${x.escaped.length ? `\n${' '.repeat(32)}got past: ${x.escaped.join('; ')}` : ''}`);
107
+ console.log(`\n${criteria.length} criteria: ${count('proven and guarded')} proven and guarded · ${count('proven but unguarded')} proven but unguarded · ${count('proven, untested by mutants')} proven, no mutant touched them · ${count('owed')} owed`);
108
+ writeReport(dir);
109
+ return 0;
110
+ }
@@ -0,0 +1,28 @@
1
+ // tripwire doctor: environment checks that have bitten real runs.
2
+ import { findOrphanKaneBrowsers, killKaneBrowser } from '../orphans.js';
3
+ import { runKane } from '../kane.js';
4
+
5
+ export default async function doctor(argv, parseArgs) {
6
+ const { values } = parseArgs({ args: argv, options: { kill: { type: 'boolean' }, 'min-age': { type: 'string' }, help: { type: 'boolean', short: 'h' } } });
7
+ if (values.help) { console.log('tripwire doctor [--kill] [--min-age 120]\n\nChecks kane-cli and lists Kane Chrome browsers left over from earlier runs (older than --min-age minutes). --kill stops them.'); return 0; }
8
+ let problems = 0;
9
+ const v = await runKane(['--version'], { killAfterMs: 30000 });
10
+ if (v.code !== 0) { console.log('✗ kane-cli not found: npm install -g @testmuai/kane-cli'); problems++; }
11
+ else console.log(`✓ kane-cli ${v.stdout.trim().split('\n').pop()}`);
12
+ if (/runtime not found|runtime package missing/i.test(v.stderr + v.stdout)) {
13
+ console.log('✗ kane-cli is missing its platform packages (a download probably failed). Reinstall: npm install -g @testmuai/kane-cli');
14
+ problems++;
15
+ }
16
+ const w = await runKane(['whoami'], { killAfterMs: 30000 });
17
+ if (!/Authenticated|valid/i.test(w.stdout)) { console.log('✗ not logged in: kane-cli login'); problems++; } else console.log('✓ logged in to Kane');
18
+ const orphans = findOrphanKaneBrowsers(Number(values['min-age'] || 120));
19
+ if (!orphans.length) console.log('✓ no leftover Kane browsers');
20
+ else {
21
+ problems++;
22
+ console.log(`✗ ${orphans.length} Kane browser(s) left over from earlier runs (they can change a shared app's state):`);
23
+ for (const o of orphans) console.log(` pid ${o.pid}, ${Math.round(o.ageMinutes / 60)} h old, profile ${o.profile}`);
24
+ if (values.kill) { for (const o of orphans) killKaneBrowser(o.profile); console.log(' stopped.'); problems--; }
25
+ else console.log(' stop them with: tripwire doctor --kill');
26
+ }
27
+ return problems ? 1 : 0;
28
+ }
@@ -85,10 +85,17 @@ export function appendOnly(before, after) {
85
85
  return null;
86
86
  }
87
87
 
88
- function appendLesson(dir, m, line) {
88
+ // The step body (without its heading) that an agent appended.
89
+ export function addedStep(original, edited) {
90
+ const added = edited.replace(/\r\n/g, '\n').slice(original.replace(/\r\n/g, '\n').trimEnd().length).trim();
91
+ return added.replace(/^##[^\n]*\n?/, '').replace(/\s+/g, ' ').trim();
92
+ }
93
+
94
+ // A lesson is what a good test checks plus the assertion that proved it, never agent chatter.
95
+ export function appendLesson(dir, m, step) {
89
96
  const f = path.join(dir, 'lessons.md');
90
97
  const head = fs.existsSync(f) ? fs.readFileSync(f, 'utf8') : '# Assertion patterns that caught real bugs in this repo\n\n';
91
- const lesson = `- ${m.expect || 'Check the visible outcome'} (caught: ${m.operator}; ${line.trim().slice(0, 160)})\n`;
98
+ const lesson = `- ${m.expect || 'Check the visible outcome'} (${m.operator}). Proven step: "${step.slice(0, 220)}"\n`;
92
99
  if (!head.includes(lesson)) fs.writeFileSync(f, head + lesson);
93
100
  }
94
101
 
@@ -137,6 +144,7 @@ export default async function fix(argv, parseArgs) {
137
144
  let proof = null;
138
145
  let note = '';
139
146
  let attempt = 0;
147
+ let added = '';
140
148
  for (attempt = 1; attempt <= maxAttempts; attempt++) {
141
149
  const original = fs.readFileSync(test, 'utf8');
142
150
  const restore = snapshotRecording(test);
@@ -172,7 +180,8 @@ export default async function fix(argv, parseArgs) {
172
180
  if (broken.code === 1 && hits > 0) {
173
181
  result = 'killed';
174
182
  proof = { clean: { code: clean.code, session: clean.sessionId, pack: clean.pack }, mutant: { code: broken.code, session: broken.sessionId, pack: broken.pack, failure: broken.failureReason } };
175
- appendLesson(dir, m, note);
183
+ added = addedStep(original, edited);
184
+ appendLesson(dir, m, added);
176
185
  console.log(` attempt ${attempt}: proven. Real app: pass. Broken app: fail. ${note}`);
177
186
  break;
178
187
  }
@@ -182,7 +191,7 @@ export default async function fix(argv, parseArgs) {
182
191
  : `the run on the broken app ended with exit ${broken.code} (${hits ? 'mutant fired' : 'mutant never fired'}), not a clean assertion failure.`;
183
192
  console.log(` attempt ${attempt}: not proven (${previousFailure.split('.')[0]}), reverted`);
184
193
  }
185
- const rec = { at: new Date().toISOString(), id: m.id, describe: m.describe, operator: m.operator, status_before: row.status, test, agent, attempts: Math.min(attempt, maxAttempts), result, proof, note };
194
+ const rec = { at: new Date().toISOString(), id: m.id, describe: m.describe, operator: m.operator, status_before: row.status, test, agent, attempts: Math.min(attempt, maxAttempts), result, proof, note, added: added || undefined };
186
195
  fs.appendFileSync(path.join(dir, 'fixes.jsonl'), JSON.stringify(rec) + '\n');
187
196
  // The proof's mutant run is a Kane run in which the strengthened suite caught this bug.
188
197
  // Appending a step cannot un-catch any other mutant (earlier failures still fail first),