harness-everything 0.53.0 → 0.54.1
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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/AGENTS.md +3 -1
- package/CHANGELOG.md +26 -0
- package/bin/cli.js +1 -0
- package/ci/action-gate.test.js +1 -1
- package/ci/invariant-routing.test.js +22 -3
- package/ci/mechanism-2al-codex-hooks-compat.test.js +56 -2
- package/ci/mechanism-2at-fable-execution-phases.test.js +38 -0
- package/ci/mechanism-349-ci-change-scope.test.js +78 -57
- package/ci/mechanism-397-cycle-import.test.js +18 -3
- package/ci/mechanism-check-packet-write-set.test.js +232 -0
- package/ci/mechanism-codex-hook-results.test.js +145 -0
- package/ci/mechanism-codex-prompt-pass-through.test.js +8 -4
- package/ci/mechanism-error-handling-map.test.js +219 -0
- package/ci/mechanism-report-writer.test.js +335 -0
- package/ci/openai-plugin-runtime.test.js +2 -1
- package/ci/prompt-contract-regressions.test.js +6 -2
- package/ci/router-workflow-plan.test.js +50 -7
- package/docs/architecture.md +4 -3
- package/docs/error-handling/README.md +250 -0
- package/docs/error-handling/actions/c1-action-map.md +89 -0
- package/docs/error-handling/actions/c2-action-map.md +22 -0
- package/docs/error-handling/actions/c3-action-map.md +26 -0
- package/docs/error-handling/actions/c5-action-map.md +31 -0
- package/docs/error-handling/actions/c6-action-map.md +37 -0
- package/docs/error-handling/actions/c7-action-map.md +18 -0
- package/docs/error-handling/actions/e0002-action-map.md +101 -0
- package/docs/error-handling/activity.puml +84 -0
- package/docs/error-handling/details/c1-authority-conformance.md +93 -0
- package/docs/error-handling/details/c2-test-root-containment.md +35 -0
- package/docs/error-handling/details/c3-mode-ownership.md +50 -0
- package/docs/error-handling/details/c5-host-replay-contract.md +40 -0
- package/docs/error-handling/details/c6-packet-briefing.md +65 -0
- package/docs/error-handling/details/c7-write-set-check.md +54 -0
- package/docs/error-handling/details/e0002-capture-evidence.md +93 -0
- package/docs/error-handling/details/e0002-report-writer.md +116 -0
- package/docs/error-handling/nodes.yaml +44 -0
- package/docs/error-handling/registry.md +59 -0
- package/docs/error-handling/templates/packet-briefing.md +45 -0
- package/docs/platform-capabilities.md +20 -1
- package/fable-mode/SKILL.md +5 -5
- package/fable-mode/execution-guardrails/SKILL.md +1 -1
- package/fable-mode/fable-haiku/SKILL.md +1 -1
- package/fable-mode/fable-opus/SKILL.md +1 -1
- package/fable-mode/fable-sonnet/SKILL.md +1 -1
- package/fable-mode/references/execution-phases.md +26 -4
- package/hooks/scripts/action-gate.js +5 -0
- package/hooks/scripts/codex-action-gate-post.js +3 -12
- package/hooks/scripts/codex-user-prompt.js +2 -3
- package/hooks/scripts/contract-test.js +17 -7
- package/hooks/scripts/lib/codex-tool-outcome.js +28 -0
- package/hooks/scripts/rule-of-3-tracker.js +11 -4
- package/hooks/scripts/state-persist.js +15 -9
- package/hooks/scripts/subagent-scope-guard.js +32 -25
- package/hooks/scripts/subagent-start.js +1 -1
- package/hooks/scripts/subagent-stop.js +14 -3
- package/opencode-plugin/plugin.json +1 -1
- package/package.json +2 -1
- package/plugins/harness-everything/.codex-plugin/plugin.json +1 -1
- package/plugins/harness-everything/hooks/hooks.json +6 -6
- package/plugins/harness-everything/hooks/scripts/action-gate.js +5 -0
- package/plugins/harness-everything/hooks/scripts/codex-action-gate-post.js +3 -12
- package/plugins/harness-everything/hooks/scripts/codex-user-prompt.js +2 -3
- package/plugins/harness-everything/hooks/scripts/contract-test.js +17 -7
- package/plugins/harness-everything/hooks/scripts/lib/codex-tool-outcome.js +28 -0
- package/plugins/harness-everything/hooks/scripts/rule-of-3-tracker.js +11 -4
- package/plugins/harness-everything/hooks/scripts/state-persist.js +15 -9
- package/plugins/harness-everything/hooks/scripts/subagent-scope-guard.js +32 -25
- package/plugins/harness-everything/hooks/scripts/subagent-start.js +1 -1
- package/plugins/harness-everything/hooks/scripts/subagent-stop.js +14 -3
- package/plugins/harness-everything/plugin.json +1 -1
- package/plugins/harness-everything/skills/fable-mode/SKILL.md +5 -5
- package/plugins/harness-everything/skills/fable-mode/execution-guardrails/SKILL.md +1 -1
- package/plugins/harness-everything/skills/fable-mode/fable-haiku/SKILL.md +1 -1
- package/plugins/harness-everything/skills/fable-mode/fable-opus/SKILL.md +1 -1
- package/plugins/harness-everything/skills/fable-mode/fable-sonnet/SKILL.md +1 -1
- package/plugins/harness-everything/skills/fable-mode/references/execution-phases.md +26 -4
- package/sbom.cdx.json +7 -7
- package/scripts/check-packet-write-set.js +155 -0
- package/scripts/codex-hooks-compat.js +46 -5
- package/scripts/error-handling-map.js +288 -0
- package/scripts/report-writer.js +261 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const crypto = require('node:crypto');
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
const yaml = require('js-yaml');
|
|
8
|
+
|
|
9
|
+
const STATUSES = new Set(['proposed', 'accepted', 'retired']);
|
|
10
|
+
const ALIAS = /^[A-Z]$/;
|
|
11
|
+
const MERMAID_ID = /^[A-Za-z_][A-Za-z0-9_-]*$/;
|
|
12
|
+
const IDENTIFIER = /^ERR-\d{8}-[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
13
|
+
const USAGE = 'Usage: node scripts/error-handling-map.js <new|set-status|check> [--map <nodes.yaml>] [--diagram <README.md>] [--alias <X> --name <text> [--date YYYYMMDD] | --id <identifier> --status <proposed|accepted|retired>]';
|
|
14
|
+
|
|
15
|
+
function parseArgs(argv) {
|
|
16
|
+
const verb = argv[0];
|
|
17
|
+
if (!['new', 'set-status', 'check'].includes(verb)) throw new Error(USAGE);
|
|
18
|
+
const options = {};
|
|
19
|
+
const allowed = new Set(['map', 'diagram', ...(verb === 'new' ? ['alias', 'name', 'date'] : []), ...(verb === 'set-status' ? ['id', 'status'] : [])]);
|
|
20
|
+
for (let i = 1; i < argv.length; i++) {
|
|
21
|
+
const flag = argv[i];
|
|
22
|
+
if (!flag.startsWith('--') || !allowed.has(flag.slice(2))) throw new Error('Unknown option: ' + flag + '\n' + USAGE);
|
|
23
|
+
const key = flag.slice(2);
|
|
24
|
+
if (Object.hasOwn(options, key)) throw new Error('Duplicate option: ' + flag);
|
|
25
|
+
if (argv[i + 1] === undefined || argv[i + 1].startsWith('--')) throw new Error('Missing value for ' + flag);
|
|
26
|
+
options[key] = argv[++i];
|
|
27
|
+
}
|
|
28
|
+
if (verb === 'new') {
|
|
29
|
+
if (!options.alias || !options.name) throw new Error('new requires --alias and --name');
|
|
30
|
+
if (!ALIAS.test(options.alias)) throw new Error('Invalid Action alias: ' + options.alias + ' (use one uppercase letter)');
|
|
31
|
+
if (!options.name.trim()) throw new Error('Action name must not be empty');
|
|
32
|
+
if (options.date && !validDate(options.date)) throw new Error('Invalid date: ' + options.date + ' (expected a real YYYYMMDD date)');
|
|
33
|
+
}
|
|
34
|
+
if (verb === 'set-status') {
|
|
35
|
+
if (!options.id || !options.status) throw new Error('set-status requires --id and --status');
|
|
36
|
+
if (!STATUSES.has(options.status)) throw new Error('Invalid status: ' + options.status + ' (use proposed, accepted, or retired)');
|
|
37
|
+
}
|
|
38
|
+
return { verb, options };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function validDate(value) {
|
|
42
|
+
if (!/^\d{8}$/.test(value)) return false;
|
|
43
|
+
const year = Number(value.slice(0, 4));
|
|
44
|
+
const month = Number(value.slice(4, 6));
|
|
45
|
+
const day = Number(value.slice(6, 8));
|
|
46
|
+
if (year < 1000 || month < 1 || month > 12 || day < 1 || day > 31) return false;
|
|
47
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
48
|
+
return date.toISOString().slice(0, 10).replace(/-/g, '') === value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function readMap(file) {
|
|
52
|
+
const realPath = fs.realpathSync(file);
|
|
53
|
+
const source = fs.readFileSync(realPath, 'utf8');
|
|
54
|
+
let data;
|
|
55
|
+
try {
|
|
56
|
+
data = yaml.load(source);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw new Error('Invalid map YAML: ' + error.message);
|
|
59
|
+
}
|
|
60
|
+
validateMap(data);
|
|
61
|
+
return { file: realPath, source, data };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function validateMap(data) {
|
|
65
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) throw new Error('Map root must be a YAML object');
|
|
66
|
+
if (!data.actions || typeof data.actions !== 'object' || Array.isArray(data.actions)) throw new Error('Map must contain an actions object');
|
|
67
|
+
const identifiers = new Map();
|
|
68
|
+
for (const [alias, action] of Object.entries(data.actions)) {
|
|
69
|
+
if (!ALIAS.test(alias)) throw new Error('Invalid Action alias in map: ' + alias);
|
|
70
|
+
if (!action || typeof action !== 'object' || Array.isArray(action)) throw new Error('Action ' + alias + ' must be an object');
|
|
71
|
+
if (typeof action.identifier !== 'string' || !IDENTIFIER.test(action.identifier)) {
|
|
72
|
+
throw new Error('Action ' + alias + ' has an invalid identifier: ' + String(action.identifier));
|
|
73
|
+
}
|
|
74
|
+
const identity = action.identifier.toLowerCase();
|
|
75
|
+
const previousAlias = identifiers.get(identity);
|
|
76
|
+
if (previousAlias) throw new Error('Duplicate identifier ' + action.identifier + ' on aliases ' + previousAlias + ' and ' + alias);
|
|
77
|
+
identifiers.set(identity, alias);
|
|
78
|
+
if (typeof action.name !== 'string' || !action.name.trim()) throw new Error('Action ' + alias + ' must have a non-empty name');
|
|
79
|
+
if (!STATUSES.has(action.status)) throw new Error('Action ' + alias + ' has invalid status: ' + String(action.status));
|
|
80
|
+
}
|
|
81
|
+
if (!Array.isArray(data.links)) throw new Error('Map must contain a links array');
|
|
82
|
+
const links = new Set();
|
|
83
|
+
for (let i = 0; i < data.links.length; i++) {
|
|
84
|
+
const link = data.links[i];
|
|
85
|
+
if (!link || typeof link !== 'object' || Array.isArray(link)) throw new Error('links[' + i + '] must contain from and to aliases');
|
|
86
|
+
for (const field of ['from', 'to']) {
|
|
87
|
+
if (typeof link[field] !== 'string' || !ALIAS.test(link[field])) {
|
|
88
|
+
throw new Error('links[' + i + '].' + field + ' must be a one-letter Action alias');
|
|
89
|
+
}
|
|
90
|
+
if (!Object.hasOwn(data.actions, link[field])) {
|
|
91
|
+
throw new Error('links[' + i + '].' + field + ' uses unknown Action alias: ' + link[field]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const key = pairKey(link.from, link.to);
|
|
95
|
+
if (links.has(key)) throw new Error('Duplicate link ' + link.from + ' -> ' + link.to);
|
|
96
|
+
links.add(key);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function pairKey(from, to) {
|
|
101
|
+
return from + '\0' + to;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function parseView1(diagramText) {
|
|
105
|
+
const lines = diagramText.split(/\r?\n/);
|
|
106
|
+
const fence = String.fromCharCode(96).repeat(3);
|
|
107
|
+
let view1 = false;
|
|
108
|
+
let inside = false;
|
|
109
|
+
const diagram = [];
|
|
110
|
+
for (const line of lines) {
|
|
111
|
+
if (!view1) {
|
|
112
|
+
if (/^## View 1(?:[ \t]|$)/.test(line)) view1 = true;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (/^## View 2(?:[ \t]|$)/.test(line)) break;
|
|
116
|
+
if (!inside) {
|
|
117
|
+
if (line.trim() === fence + 'mermaid') inside = true;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (line.trim() === fence) return diagram.join('\n');
|
|
121
|
+
diagram.push(line);
|
|
122
|
+
}
|
|
123
|
+
throw new Error('Could not find the View 1 Mermaid block before View 2');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function endpoint(raw, lineNumber, side) {
|
|
127
|
+
let value = raw.trim();
|
|
128
|
+
if (side === 'to' && value.startsWith('|')) {
|
|
129
|
+
const end = value.indexOf('|', 1);
|
|
130
|
+
if (end < 0) throw new Error('Invalid Mermaid edge label at View 1 line ' + lineNumber);
|
|
131
|
+
value = value.slice(end + 1).trim();
|
|
132
|
+
}
|
|
133
|
+
const match = /^([A-Za-z_][A-Za-z0-9_-]*)([\s\S]*)$/.exec(value);
|
|
134
|
+
if (!match) throw new Error('Invalid Mermaid ' + side + ' node at View 1 line ' + lineNumber);
|
|
135
|
+
const suffix = match[2].trim();
|
|
136
|
+
const declaration = /^\[[\s\S]*\]$/.test(suffix) ||
|
|
137
|
+
/^\{[\s\S]*\}$/.test(suffix) ||
|
|
138
|
+
/^\(\([\s\S]*\)\)$/.test(suffix);
|
|
139
|
+
if (suffix && !declaration) throw new Error('Unsupported Mermaid node declaration at View 1 line ' + lineNumber);
|
|
140
|
+
return match[1];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function diagramEdges(diagramText) {
|
|
144
|
+
const adjacency = new Map();
|
|
145
|
+
const lines = parseView1(diagramText).split('\n');
|
|
146
|
+
for (let i = 0; i < lines.length; i++) {
|
|
147
|
+
const line = lines[i].trim();
|
|
148
|
+
if (!line || line.startsWith('%%')) continue;
|
|
149
|
+
const arrows = line.match(/-->/g) || [];
|
|
150
|
+
if (arrows.length > 1) throw new Error('Unsupported chained Mermaid edges at View 1 line ' + (i + 1));
|
|
151
|
+
if (arrows.length === 0) {
|
|
152
|
+
if (/(?:---|==>|-\.\-|--[ox]|<--|<==)/.test(line)) {
|
|
153
|
+
throw new Error('Unsupported Mermaid edge operator at View 1 line ' + (i + 1));
|
|
154
|
+
}
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const arrowAt = line.indexOf('-->');
|
|
158
|
+
const from = endpoint(line.slice(0, arrowAt), i + 1, 'from');
|
|
159
|
+
const to = endpoint(line.slice(arrowAt + 3), i + 1, 'to');
|
|
160
|
+
if (!adjacency.has(from)) adjacency.set(from, new Set());
|
|
161
|
+
adjacency.get(from).add(to);
|
|
162
|
+
}
|
|
163
|
+
return adjacency;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function expectedLinks(actions, adjacency) {
|
|
167
|
+
const expected = new Set();
|
|
168
|
+
const aliases = new Set(actions);
|
|
169
|
+
for (const from of aliases) {
|
|
170
|
+
const pending = [...(adjacency.get(from) || [])];
|
|
171
|
+
const visited = new Set();
|
|
172
|
+
while (pending.length) {
|
|
173
|
+
const node = pending.shift();
|
|
174
|
+
if (aliases.has(node)) {
|
|
175
|
+
if (node !== from) expected.add(pairKey(from, node));
|
|
176
|
+
} else if (!visited.has(node)) {
|
|
177
|
+
visited.add(node);
|
|
178
|
+
pending.push(...(adjacency.get(node) || []));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return expected;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function check(mapPath, diagramPath) {
|
|
186
|
+
const { data } = readMap(mapPath);
|
|
187
|
+
const diagram = fs.readFileSync(fs.realpathSync(diagramPath), 'utf8');
|
|
188
|
+
const expected = expectedLinks(Object.keys(data.actions), diagramEdges(diagram));
|
|
189
|
+
const actual = new Set(data.links.map(link => pairKey(link.from, link.to)));
|
|
190
|
+
const labels = key => key.split('\0').join(' -> ');
|
|
191
|
+
const missing = [...expected].filter(key => !actual.has(key)).sort();
|
|
192
|
+
const extra = [...actual].filter(key => !expected.has(key)).sort();
|
|
193
|
+
if (missing.length || extra.length) {
|
|
194
|
+
const errors = [
|
|
195
|
+
...missing.map(key => 'Missing link ' + labels(key) + ' (present in View 1)'),
|
|
196
|
+
...extra.map(key => 'Extra link ' + labels(key) + ' (not present in View 1)'),
|
|
197
|
+
];
|
|
198
|
+
throw new Error(errors.join('\n'));
|
|
199
|
+
}
|
|
200
|
+
console.log('OK: ' + expected.size + ' Action link(s) match View 1.');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function editStatus(source, alias, status) {
|
|
204
|
+
const actionStart = new RegExp('^ ' + alias + ':[ \\t]*(?:#[^\\r\\n]*)?$', 'm').exec(source);
|
|
205
|
+
if (!actionStart) throw new Error('Could not locate Action ' + alias + ' in map text');
|
|
206
|
+
const bodyStart = actionStart.index + actionStart[0].length;
|
|
207
|
+
const remainder = source.slice(bodyStart);
|
|
208
|
+
const boundary = /^(?: [A-Z]:[ \t]*(?:#[^\r\n]*)?|links:[ \t]*(?:#[^\r\n]*)?)/m.exec(remainder);
|
|
209
|
+
const end = boundary ? bodyStart + boundary.index : source.length;
|
|
210
|
+
const block = source.slice(actionStart.index, end);
|
|
211
|
+
const statusLines = [...block.matchAll(/^[ \t]{4}status:/gm)];
|
|
212
|
+
if (statusLines.length !== 1) throw new Error('Action ' + alias + ' must have exactly one status line');
|
|
213
|
+
const statusLine = /^([ \t]{4}status:[ \t]*)\S+([ \t]*(?:#.*)?)(\r?)$/m.exec(block);
|
|
214
|
+
if (!statusLine) throw new Error('Could not read Action ' + alias + ' status line');
|
|
215
|
+
const replacement = statusLine[1] + status + statusLine[2] + statusLine[3];
|
|
216
|
+
const updatedBlock = block.slice(0, statusLine.index) + replacement + block.slice(statusLine.index + statusLine[0].length);
|
|
217
|
+
return source.slice(0, actionStart.index) + updatedBlock + source.slice(end);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function addAction(source, alias, identifier, name) {
|
|
221
|
+
const linksLine = /^links:[ \t]*(?:#[^\r\n]*)?$/m.exec(source);
|
|
222
|
+
if (!linksLine) throw new Error('Could not locate root links section in map text');
|
|
223
|
+
const newline = source.includes('\r\n') ? '\r\n' : '\n';
|
|
224
|
+
const prefix = source.slice(0, linksLine.index);
|
|
225
|
+
const separator = prefix && !prefix.endsWith('\n') ? newline : '';
|
|
226
|
+
const block = [
|
|
227
|
+
' ' + alias + ':',
|
|
228
|
+
' identifier: ' + identifier,
|
|
229
|
+
' name: ' + JSON.stringify(name),
|
|
230
|
+
' status: proposed',
|
|
231
|
+
'',
|
|
232
|
+
].join(newline);
|
|
233
|
+
return prefix + separator + block + source.slice(linksLine.index);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function writeAtomically(file, contents) {
|
|
237
|
+
const mode = fs.statSync(file).mode & 0o777;
|
|
238
|
+
const temporary = path.join(path.dirname(file), '.' + path.basename(file) + '.' + process.pid + '.' + crypto.randomUUID() + '.tmp');
|
|
239
|
+
let fd;
|
|
240
|
+
try {
|
|
241
|
+
fd = fs.openSync(temporary, 'wx', mode);
|
|
242
|
+
fs.writeFileSync(fd, contents, 'utf8');
|
|
243
|
+
fs.closeSync(fd);
|
|
244
|
+
fd = undefined;
|
|
245
|
+
fs.renameSync(temporary, file);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
if (fd !== undefined) fs.closeSync(fd);
|
|
248
|
+
try { fs.unlinkSync(temporary); } catch (_) {}
|
|
249
|
+
throw new Error('Could not atomically update ' + file + ': ' + error.message);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function create(mapPath, alias, name, requestedDate) {
|
|
254
|
+
const { file, source, data } = readMap(mapPath);
|
|
255
|
+
if (Object.hasOwn(data.actions, alias)) throw new Error('Action alias already exists: ' + alias);
|
|
256
|
+
const date = requestedDate || new Date().toISOString().slice(0, 10).replace(/-/g, '');
|
|
257
|
+
let identifier;
|
|
258
|
+
do {
|
|
259
|
+
identifier = 'ERR-' + date + '-' + crypto.randomUUID();
|
|
260
|
+
} while (Object.values(data.actions).some(action => action.identifier.toLowerCase() === identifier.toLowerCase()));
|
|
261
|
+
writeAtomically(file, addAction(source, alias, identifier, name));
|
|
262
|
+
console.log(identifier);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function setStatus(mapPath, identifier, status) {
|
|
266
|
+
const { file, source, data } = readMap(mapPath);
|
|
267
|
+
const entry = Object.entries(data.actions).find(([, action]) => action.identifier.toLowerCase() === identifier.toLowerCase());
|
|
268
|
+
if (!entry) throw new Error('No Action found with identifier: ' + identifier);
|
|
269
|
+
writeAtomically(file, editStatus(source, entry[0], status));
|
|
270
|
+
console.log('Updated ' + identifier + ' status to ' + status + '.');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function main() {
|
|
274
|
+
const { verb, options } = parseArgs(process.argv.slice(2));
|
|
275
|
+
const mapPath = path.resolve(options.map || 'docs/error-handling/nodes.yaml');
|
|
276
|
+
const diagramPath = path.resolve(options.diagram || 'docs/error-handling/README.md');
|
|
277
|
+
if (verb === 'check') check(mapPath, diagramPath);
|
|
278
|
+
else if (verb === 'new') create(mapPath, options.alias, options.name, options.date);
|
|
279
|
+
else setStatus(mapPath, options.id, options.status);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
try {
|
|
283
|
+
main();
|
|
284
|
+
} catch (error) {
|
|
285
|
+
console.error('[error-handling-map] ' + error.message);
|
|
286
|
+
process.exitCode = 1;
|
|
287
|
+
}
|
|
288
|
+
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const crypto = require('node:crypto');
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
|
|
8
|
+
const USAGE = 'usage: report-writer write --output <path> --status <DONE|BLOCKED|0|nonzero-int> (--body-file <path>|--context <text>|--from-codex-output <path>) [--reason <text>|--error <text>...] | report-writer check --file <path>';
|
|
9
|
+
const WRITE_OPTIONS = new Set(['--output', '--status', '--reason', '--error', '--body-file', '--context', '--from-codex-output']);
|
|
10
|
+
const LABEL_MARKER = '<!-- harness-label';
|
|
11
|
+
|
|
12
|
+
function error(message) {
|
|
13
|
+
throw new Error(message);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function parseOptions(args, allowed, repeatable = new Set()) {
|
|
17
|
+
const options = new Map();
|
|
18
|
+
for (let i = 0; i < args.length; i += 2) {
|
|
19
|
+
const key = args[i];
|
|
20
|
+
if (!allowed.has(key)) error(`unknown option: ${key}`);
|
|
21
|
+
if (i + 1 >= args.length) error(`missing value for ${key}`);
|
|
22
|
+
if (options.has(key) && !repeatable.has(key)) error(`duplicate option: ${key}`);
|
|
23
|
+
if (repeatable.has(key)) options.set(key, [...(options.get(key) || []), args[i + 1]]);
|
|
24
|
+
else options.set(key, args[i + 1]);
|
|
25
|
+
}
|
|
26
|
+
return options;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function required(options, key) {
|
|
30
|
+
const value = options.get(key);
|
|
31
|
+
if (value === undefined || value === '') error(`missing value for ${key}`);
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function singleLine(value, name) {
|
|
36
|
+
if (/\r|\n/.test(value)) error(`${name} must be a single line`);
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function statusFrom(options) {
|
|
41
|
+
const value = singleLine(required(options, '--status'), 'status');
|
|
42
|
+
const reason = options.get('--reason');
|
|
43
|
+
const errors = options.get('--error') || [];
|
|
44
|
+
|
|
45
|
+
let done;
|
|
46
|
+
if (value === 'DONE' || value === '0') done = true;
|
|
47
|
+
else if (value === 'BLOCKED') done = false;
|
|
48
|
+
else if (/^[+-]?\d+$/.test(value) && Number.isSafeInteger(Number(value))) done = Number(value) === 0;
|
|
49
|
+
else error('status must be DONE, BLOCKED, 0, or a nonzero integer');
|
|
50
|
+
|
|
51
|
+
if (done) {
|
|
52
|
+
if (reason !== undefined || errors.length) error('DONE status cannot include a reason or error');
|
|
53
|
+
return 'STATUS: DONE';
|
|
54
|
+
}
|
|
55
|
+
if (reason !== undefined && errors.length) error('provide either --reason or --error, not both');
|
|
56
|
+
const message = reason !== undefined ? reason : errors.join('; ');
|
|
57
|
+
if (message === undefined || message.trim() === '') error('BLOCKED status requires a nonempty --reason or --error');
|
|
58
|
+
singleLine(message, 'reason');
|
|
59
|
+
return `STATUS: BLOCKED: ${message.trim()}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function splitLines(text) {
|
|
63
|
+
if (/\r(?!\n)/.test(text)) error('bare carriage return is not a valid line ending');
|
|
64
|
+
const lines = [];
|
|
65
|
+
const separators = /\r\n|\n/g;
|
|
66
|
+
let start = 0;
|
|
67
|
+
let match;
|
|
68
|
+
while ((match = separators.exec(text))) {
|
|
69
|
+
lines.push({ value: text.slice(start, match.index), start, eol: match[0] });
|
|
70
|
+
start = separators.lastIndex;
|
|
71
|
+
}
|
|
72
|
+
if (start < text.length || text.length === 0 || !text.endsWith('\n')) {
|
|
73
|
+
lines.push({ value: text.slice(start), start, eol: '' });
|
|
74
|
+
}
|
|
75
|
+
return lines;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function statusLineStatus(line) {
|
|
79
|
+
if (line === 'STATUS: DONE') return 'STATUS: DONE';
|
|
80
|
+
const blocked = /^STATUS: BLOCKED: (.+)$/.exec(line);
|
|
81
|
+
if (blocked && blocked[1].trim() !== '') return line;
|
|
82
|
+
error('expected STATUS: DONE or STATUS: BLOCKED: <reason>');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function validateReport(text) {
|
|
86
|
+
if (text.includes(LABEL_MARKER)) error('report contains a host label marker');
|
|
87
|
+
const lines = splitLines(text);
|
|
88
|
+
const controls = lines.filter(line => line.value.startsWith('STATUS:'));
|
|
89
|
+
if (controls.length !== 1) error(`expected exactly one STATUS line; found ${controls.length}`);
|
|
90
|
+
const last = lines[lines.length - 1];
|
|
91
|
+
if (!last || !last.value.startsWith('STATUS:')) error('STATUS line must be terminal with nothing after it');
|
|
92
|
+
return statusLineStatus(last.value);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function validateBody(body) {
|
|
96
|
+
if (body.includes(LABEL_MARKER)) error('body contains a host label marker');
|
|
97
|
+
if (splitLines(body).some(line => line.value.startsWith('STATUS:'))) {
|
|
98
|
+
error('body contains a column-zero STATUS line');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function parseHostLabel(line) {
|
|
103
|
+
const prefix = '<!-- harness-label ';
|
|
104
|
+
const suffix = ' -->';
|
|
105
|
+
if (!line.startsWith(prefix) || !line.endsWith(suffix)) error('malformed terminal host label');
|
|
106
|
+
let value;
|
|
107
|
+
try {
|
|
108
|
+
value = JSON.parse(line.slice(prefix.length, -suffix.length));
|
|
109
|
+
} catch {
|
|
110
|
+
error('terminal host label must contain valid JSON');
|
|
111
|
+
}
|
|
112
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) error('terminal host label JSON must be an object');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function extractCodexBody(raw, expectedStatus) {
|
|
116
|
+
const lines = splitLines(raw);
|
|
117
|
+
let strippedLabel = false;
|
|
118
|
+
let bodyEnd = raw.length;
|
|
119
|
+
const markers = lines.filter(line => line.value.includes(LABEL_MARKER));
|
|
120
|
+
|
|
121
|
+
if (markers.length) {
|
|
122
|
+
if (markers.length !== 1 || markers[0] !== lines[lines.length - 1]) error('host label is allowed only once at EOF');
|
|
123
|
+
parseHostLabel(lines[lines.length - 1].value);
|
|
124
|
+
strippedLabel = true;
|
|
125
|
+
bodyEnd = lines[lines.length - 1].start;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const controls = lines.filter(line => line.value.startsWith('STATUS:'));
|
|
129
|
+
if (controls.length > 1) error(`raw output must contain exactly one STATUS line; found ${controls.length}`);
|
|
130
|
+
if (controls.length === 1) {
|
|
131
|
+
const statusIndex = lines.indexOf(controls[0]);
|
|
132
|
+
const terminalIndex = lines.length - (strippedLabel ? 2 : 1);
|
|
133
|
+
if (statusIndex !== terminalIndex) error('raw STATUS must be terminal or immediately before the host label');
|
|
134
|
+
if (controls[0].value !== expectedStatus) error('raw terminal STATUS does not match the requested status');
|
|
135
|
+
if (strippedLabel && !controls[0].eol) error('host label must be on the line immediately after STATUS');
|
|
136
|
+
bodyEnd = controls[0].start;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const body = raw.slice(0, bodyEnd);
|
|
140
|
+
validateBody(body);
|
|
141
|
+
return { body, strippedLabel };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function assertDistinctFiles(input, output) {
|
|
145
|
+
const inputPath = path.resolve(input);
|
|
146
|
+
const outputPath = path.resolve(output);
|
|
147
|
+
if (inputPath === outputPath) error('output aliases an input file');
|
|
148
|
+
|
|
149
|
+
const inputStat = fs.statSync(inputPath);
|
|
150
|
+
try {
|
|
151
|
+
const outputStat = fs.statSync(outputPath);
|
|
152
|
+
if (inputStat.dev === outputStat.dev && inputStat.ino === outputStat.ino) error('output aliases an input file');
|
|
153
|
+
} catch (cause) {
|
|
154
|
+
if (cause.code !== 'ENOENT') throw cause;
|
|
155
|
+
try {
|
|
156
|
+
const candidate = path.join(fs.realpathSync(path.dirname(outputPath)), path.basename(outputPath));
|
|
157
|
+
if (candidate === fs.realpathSync(inputPath)) error('output aliases an input file');
|
|
158
|
+
} catch (parentError) {
|
|
159
|
+
if (parentError.code !== 'ENOENT') throw parentError;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function readText(file, description) {
|
|
165
|
+
try {
|
|
166
|
+
return fs.readFileSync(file, 'utf8');
|
|
167
|
+
} catch (cause) {
|
|
168
|
+
error(`cannot read ${description} ${file}: ${cause.message}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function reportBytes(body, statusLine) {
|
|
173
|
+
validateBody(body);
|
|
174
|
+
const newline = body.endsWith('\r\n') ? '\r\n' : '\n';
|
|
175
|
+
const separator = body === '' || body.endsWith('\n') ? '' : newline;
|
|
176
|
+
return Buffer.from(`${body}${separator}${statusLine}${newline}`, 'utf8');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function atomicWrite(output, bytes, expectedStatus) {
|
|
180
|
+
const directory = path.dirname(output);
|
|
181
|
+
const temp = path.join(directory, `.${path.basename(output)}.${process.pid}.${crypto.randomBytes(8).toString('hex')}.tmp`);
|
|
182
|
+
let descriptor;
|
|
183
|
+
let published = false;
|
|
184
|
+
try {
|
|
185
|
+
descriptor = fs.openSync(temp, 'wx', 0o666);
|
|
186
|
+
fs.writeFileSync(descriptor, bytes);
|
|
187
|
+
fs.closeSync(descriptor);
|
|
188
|
+
descriptor = undefined;
|
|
189
|
+
|
|
190
|
+
const staged = fs.readFileSync(temp);
|
|
191
|
+
if (!staged.equals(bytes)) error('staged report bytes differ from the intended report');
|
|
192
|
+
if (validateReport(staged.toString('utf8')) !== expectedStatus) error('staged report status differs from the requested status');
|
|
193
|
+
|
|
194
|
+
fs.renameSync(temp, output);
|
|
195
|
+
published = true;
|
|
196
|
+
|
|
197
|
+
const actual = fs.readFileSync(output);
|
|
198
|
+
if (!actual.equals(bytes)) error('published report bytes differ from the staged report');
|
|
199
|
+
if (validateReport(actual.toString('utf8')) !== expectedStatus) error('published report status differs from the requested status');
|
|
200
|
+
} catch (cause) {
|
|
201
|
+
if (descriptor !== undefined) {
|
|
202
|
+
try { fs.closeSync(descriptor); } catch {}
|
|
203
|
+
}
|
|
204
|
+
try { fs.unlinkSync(temp); } catch {}
|
|
205
|
+
if (published) error(`report was published but could not be verified; coordinator disposition required: ${cause.message}`);
|
|
206
|
+
error(`could not publish report before replacement: ${cause.message}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function write(args) {
|
|
211
|
+
const options = parseOptions(args, WRITE_OPTIONS, new Set(['--error']));
|
|
212
|
+
const output = path.resolve(required(options, '--output'));
|
|
213
|
+
const expectedStatus = statusFrom(options);
|
|
214
|
+
const modes = ['--body-file', '--context', '--from-codex-output'].filter(key => options.has(key));
|
|
215
|
+
if (modes.length !== 1) error('provide exactly one of --body-file, --context, or --from-codex-output');
|
|
216
|
+
|
|
217
|
+
let body;
|
|
218
|
+
let strippedLabel = false;
|
|
219
|
+
if (modes[0] === '--context') {
|
|
220
|
+
body = options.get('--context');
|
|
221
|
+
} else {
|
|
222
|
+
const input = required(options, modes[0]);
|
|
223
|
+
assertDistinctFiles(input, output);
|
|
224
|
+
const contents = readText(path.resolve(input), modes[0] === '--body-file' ? 'body file' : 'raw Codex output');
|
|
225
|
+
if (modes[0] === '--from-codex-output') {
|
|
226
|
+
const extracted = extractCodexBody(contents, expectedStatus);
|
|
227
|
+
body = extracted.body;
|
|
228
|
+
strippedLabel = extracted.strippedLabel;
|
|
229
|
+
} else {
|
|
230
|
+
body = contents;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
validateBody(body);
|
|
235
|
+
const bytes = reportBytes(body, expectedStatus);
|
|
236
|
+
atomicWrite(output, bytes, expectedStatus);
|
|
237
|
+
if (strippedLabel) console.error('report-writer: stripped the terminal host label from raw Codex output');
|
|
238
|
+
console.log(`Wrote ${output}`);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function check(args) {
|
|
242
|
+
const options = parseOptions(args, new Set(['--file']));
|
|
243
|
+
if (options.size !== 1) error('check requires only --file <path>');
|
|
244
|
+
const file = path.resolve(required(options, '--file'));
|
|
245
|
+
const status = validateReport(readText(file, 'report'));
|
|
246
|
+
console.log(`${file}: valid (${status})`);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function main() {
|
|
250
|
+
try {
|
|
251
|
+
const [command, ...args] = process.argv.slice(2);
|
|
252
|
+
if (command === 'write') write(args);
|
|
253
|
+
else if (command === 'check') check(args);
|
|
254
|
+
else error(USAGE);
|
|
255
|
+
} catch (cause) {
|
|
256
|
+
console.error(`report-writer: ${cause.message}`);
|
|
257
|
+
process.exitCode = 1;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (require.main === module) main();
|