harness-everything 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHANGELOG.md +21 -0
  4. package/ci/mechanism-394-ledger-match.test.js +324 -3
  5. package/ci/mechanism-496-claude-capture.test.js +342 -0
  6. package/ci/mechanism-496-codex-capture.test.js +508 -0
  7. package/ci/mechanism-496-local-error-cli-edges.test.js +234 -0
  8. package/ci/mechanism-496-local-error-cli.test.js +517 -0
  9. package/ci/mechanism-496-local-error-store.test.js +731 -0
  10. package/ci/mechanism-496-opencode-capture.test.js +483 -0
  11. package/ci/mechanism-496-review-fixes.test.js +278 -0
  12. package/ci/mechanism-516-agy-post-tool-exit.test.js +472 -0
  13. package/ci/mechanism-516-agy-stop-revision.test.js +215 -0
  14. package/hooks/scripts/agy-post-tool.js +79 -16
  15. package/hooks/scripts/agy-stop.js +63 -10
  16. package/hooks/scripts/codex-ledger-lesson.js +103 -27
  17. package/hooks/scripts/ledger-lesson.js +94 -23
  18. package/hooks/scripts/lib/ledger-match.js +169 -21
  19. package/hooks/scripts/lib/local-error-source.js +127 -0
  20. package/hooks/scripts/lib/local-errors.js +597 -0
  21. package/hooks/scripts/lib/observations.js +1 -1
  22. package/hooks/scripts/state-persist.js +7 -1
  23. package/opencode-plugin/index.mjs +86 -1
  24. package/opencode-plugin/plugin.json +1 -1
  25. package/package.json +2 -1
  26. package/plugins/harness-everything/.antigravity-plugin/plugin.json +1 -1
  27. package/plugins/harness-everything/.codex-plugin/plugin.json +1 -1
  28. package/plugins/harness-everything/hooks/scripts/agy-post-tool.js +79 -16
  29. package/plugins/harness-everything/hooks/scripts/agy-stop.js +63 -10
  30. package/plugins/harness-everything/hooks/scripts/codex-ledger-lesson.js +103 -27
  31. package/plugins/harness-everything/hooks/scripts/ledger-lesson.js +94 -23
  32. package/plugins/harness-everything/hooks/scripts/lib/ledger-match.js +169 -21
  33. package/plugins/harness-everything/hooks/scripts/lib/local-error-source.js +127 -0
  34. package/plugins/harness-everything/hooks/scripts/lib/local-errors.js +597 -0
  35. package/plugins/harness-everything/hooks/scripts/lib/observations.js +1 -1
  36. package/plugins/harness-everything/hooks/scripts/state-persist.js +7 -1
  37. package/plugins/harness-everything/plugin.json +1 -1
  38. package/sbom.cdx.json +7 -7
  39. package/scripts/errors-local.js +357 -0
@@ -0,0 +1,357 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+ // Local error CLI (#496 C2): add provisional local lessons, list workspace-scoped
4
+ // records, and export bounded redacted JSONL for triage / #498 bootstrap input.
5
+ // Exit codes: 0 ok, 2 usage/validation error, 1 runtime error.
6
+
7
+ const crypto = require('node:crypto');
8
+ const path = require('node:path');
9
+ const localErrors = require(path.join(__dirname, '..', 'hooks', 'scripts', 'lib', 'local-errors.js'));
10
+ const observations = require(path.join(__dirname, '..', 'hooks', 'scripts', 'lib', 'observations.js'));
11
+
12
+ const IDENTIFIER_PATTERN = /^[a-z0-9][a-z0-9_-]{0,63}$/;
13
+ const KINDS = new Set(['occurrence', 'provisional-lesson']);
14
+ const DESTINATIONS = new Set(localErrors.DESTINATIONS);
15
+
16
+ class UsageError extends Error {
17
+ constructor(message) {
18
+ super(message);
19
+ this.name = 'UsageError';
20
+ }
21
+ }
22
+
23
+ function usageText() {
24
+ return [
25
+ 'Usage:',
26
+ ' node scripts/errors-local.js add (--match-json <json> | --executable <id> --subcommand <id> [--diagnostic <id>...]) --lesson <text> [--cue <text>] [--issue <https-url>] [--destination harness|project|unknown] [--signature <id>] [--promotion-id <E-xxxx>] [--json]',
27
+ ' node scripts/errors-local.js list [--kind occurrence|provisional-lesson] [--destination harness|project|unknown] [--signature <id>] [--limit <n>] [--json]',
28
+ ' node scripts/errors-local.js export [--kind occurrence|provisional-lesson] [--destination harness|project|unknown] [--signature <id>] [--limit <n>]',
29
+ ].join('\n');
30
+ }
31
+
32
+ function requireValue(argv, index, flag) {
33
+ if (index + 1 >= argv.length) throw new UsageError(`missing value for ${flag}`);
34
+ return argv[index + 1];
35
+ }
36
+
37
+ function parseLimit(raw) {
38
+ if (!/^\d+$/.test(String(raw))) throw new UsageError(`invalid --limit: ${raw}`);
39
+ const value = Number.parseInt(raw, 10);
40
+ if (!Number.isSafeInteger(value) || value < 1) throw new UsageError(`invalid --limit: ${raw}`);
41
+ return Math.min(value, localErrors.LIMITS.maxRecordsPerWorkspace);
42
+ }
43
+
44
+ function deriveSignature(match, lesson, env = process.env) {
45
+ const redactedLesson = localErrors.redactErrorText(String(lesson || '').trim(), env)
46
+ .slice(0, localErrors.LIMITS.maxLessonChars);
47
+ const firstRule = (match.failures && match.failures[0]) || (match.actions && match.actions[0]);
48
+ const hash = crypto.createHash('sha256').update(JSON.stringify([match, redactedLesson])).digest('hex').slice(0, 8);
49
+ if (firstRule && firstRule.executable && firstRule.subcommand) {
50
+ const base = `local-${firstRule.executable}-${firstRule.subcommand}-${hash}`;
51
+ if (base.length <= 64 && IDENTIFIER_PATTERN.test(base)) return base;
52
+ }
53
+ return `local-${hash}`;
54
+ }
55
+
56
+ function isValidationError(error) {
57
+ return error instanceof Error && !error.code && typeof error.message === 'string' && /^invalid\b/i.test(error.message);
58
+ }
59
+
60
+ function sanitizeExportRecord(record, env) {
61
+ const limits = localErrors.LIMITS;
62
+ if (!record || typeof record !== 'object') return null;
63
+
64
+ if (record.kind === 'provisional-lesson') {
65
+ const out = {
66
+ schemaVersion: record.schemaVersion,
67
+ kind: 'provisional-lesson',
68
+ provisionalId: record.provisionalId,
69
+ workspaceKey: record.workspaceKey,
70
+ destination: record.destination,
71
+ createdAt: record.createdAt,
72
+ signature: record.signature,
73
+ match: record.match,
74
+ lesson: localErrors.redactErrorText(String(record.lesson || ''), env).slice(0, limits.maxLessonChars),
75
+ issueUrl: record.issueUrl ?? null,
76
+ promotionId: record.promotionId ?? null,
77
+ };
78
+ if (typeof record.cue === 'string') {
79
+ out.cue = localErrors.redactErrorText(record.cue, env).slice(0, limits.maxCueChars);
80
+ }
81
+ return out;
82
+ }
83
+
84
+ if (record.kind === 'occurrence') {
85
+ const redactedExcerpt = localErrors.redactErrorText(String(record.outputExcerpt || ''), env);
86
+ return {
87
+ schemaVersion: record.schemaVersion,
88
+ kind: 'occurrence',
89
+ provisionalId: record.provisionalId,
90
+ workspaceKey: record.workspaceKey,
91
+ destination: record.destination,
92
+ createdAt: record.createdAt,
93
+ signature: record.signature,
94
+ source: record.source,
95
+ host: record.host,
96
+ sessionKey: record.sessionKey ?? null,
97
+ toolUseId: record.toolUseId ?? null,
98
+ commandClass: record.commandClass,
99
+ exitCode: Number.isInteger(record.exitCode) ? record.exitCode : null,
100
+ outputExcerpt: observations.tailBytes(redactedExcerpt, limits.maxExcerptBytes),
101
+ matchedLedgerIds: Array.isArray(record.matchedLedgerIds) ? record.matchedLedgerIds.slice(0, limits.maxMatchedLedgerIds) : [],
102
+ };
103
+ }
104
+
105
+ return null;
106
+ }
107
+
108
+ function parseAddArgs(argv, env = process.env) {
109
+ const opts = {
110
+ matchJson: null,
111
+ executable: null,
112
+ subcommand: null,
113
+ diagnostics: [],
114
+ lesson: null,
115
+ cue: undefined,
116
+ issueUrl: undefined,
117
+ destination: 'unknown',
118
+ signature: null,
119
+ promotionId: undefined,
120
+ json: false,
121
+ };
122
+
123
+ for (let i = 0; i < argv.length; i += 1) {
124
+ const arg = argv[i];
125
+ if (arg === '--match-json') { opts.matchJson = requireValue(argv, i, arg); i += 1; }
126
+ else if (arg === '--executable') { opts.executable = requireValue(argv, i, arg); i += 1; }
127
+ else if (arg === '--subcommand') { opts.subcommand = requireValue(argv, i, arg); i += 1; }
128
+ else if (arg === '--diagnostic') { opts.diagnostics.push(requireValue(argv, i, arg)); i += 1; }
129
+ else if (arg === '--lesson') { opts.lesson = requireValue(argv, i, arg); i += 1; }
130
+ else if (arg === '--cue') { opts.cue = requireValue(argv, i, arg); i += 1; }
131
+ else if (arg === '--issue') { opts.issueUrl = requireValue(argv, i, arg); i += 1; }
132
+ else if (arg === '--destination') { opts.destination = requireValue(argv, i, arg); i += 1; }
133
+ else if (arg === '--signature') { opts.signature = requireValue(argv, i, arg); i += 1; }
134
+ else if (arg === '--promotion-id') { opts.promotionId = requireValue(argv, i, arg); i += 1; }
135
+ else if (arg === '--json') { opts.json = true; }
136
+ else if (arg === '--help' || arg === '-h') { return { help: true }; }
137
+ else { throw new UsageError(`unknown option for add: ${arg}`); }
138
+ }
139
+
140
+ const hasMatchJson = opts.matchJson !== null;
141
+ const hasFlagRule = opts.executable !== null || opts.subcommand !== null || opts.diagnostics.length > 0;
142
+ if (hasMatchJson && hasFlagRule) {
143
+ throw new UsageError('use either --match-json or --executable/--subcommand[/--diagnostic], not both');
144
+ }
145
+ if (!hasMatchJson && !hasFlagRule) {
146
+ throw new UsageError('match rule required: provide --match-json or --executable and --subcommand');
147
+ }
148
+
149
+ let match;
150
+ if (hasMatchJson) {
151
+ try {
152
+ match = JSON.parse(opts.matchJson);
153
+ } catch (error) {
154
+ throw new UsageError(`invalid --match-json: ${error.message}`);
155
+ }
156
+ } else {
157
+ if (!opts.executable || !opts.subcommand) {
158
+ throw new UsageError('both --executable and --subcommand are required');
159
+ }
160
+ if (opts.diagnostics.length > 0) {
161
+ match = {
162
+ failures: [{
163
+ tool: 'shell',
164
+ executable: opts.executable,
165
+ subcommand: opts.subcommand,
166
+ diagnostics: opts.diagnostics,
167
+ }],
168
+ };
169
+ } else {
170
+ match = {
171
+ actions: [{
172
+ tool: 'shell',
173
+ executable: opts.executable,
174
+ subcommand: opts.subcommand,
175
+ }],
176
+ };
177
+ }
178
+ }
179
+
180
+ try {
181
+ match = localErrors.validateMatch(match);
182
+ } catch (error) {
183
+ throw new UsageError(error.message);
184
+ }
185
+
186
+ if (typeof opts.lesson !== 'string' || !opts.lesson.trim()) {
187
+ throw new UsageError('--lesson is required and must be non-empty');
188
+ }
189
+ if (!DESTINATIONS.has(opts.destination)) {
190
+ throw new UsageError(`invalid --destination: ${opts.destination} (expected harness|project|unknown)`);
191
+ }
192
+
193
+ const signature = opts.signature || deriveSignature(match, opts.lesson.trim(), env);
194
+ if (!IDENTIFIER_PATTERN.test(signature)) {
195
+ throw new UsageError(`invalid --signature: ${signature}`);
196
+ }
197
+
198
+ return { ...opts, match, signature };
199
+ }
200
+
201
+ function parseQueryArgs(subcommand, argv, allowJson) {
202
+ const opts = {
203
+ kind: null,
204
+ destination: null,
205
+ signature: null,
206
+ limit: localErrors.LIMITS.maxRecordsPerWorkspace,
207
+ json: false,
208
+ };
209
+
210
+ for (let i = 0; i < argv.length; i += 1) {
211
+ const arg = argv[i];
212
+ if (arg === '--kind') {
213
+ const value = requireValue(argv, i, arg);
214
+ if (!KINDS.has(value)) throw new UsageError(`invalid --kind: ${value} (expected occurrence|provisional-lesson)`);
215
+ opts.kind = value;
216
+ i += 1;
217
+ } else if (arg === '--destination') {
218
+ const value = requireValue(argv, i, arg);
219
+ if (!DESTINATIONS.has(value)) throw new UsageError(`invalid --destination: ${value} (expected harness|project|unknown)`);
220
+ opts.destination = value;
221
+ i += 1;
222
+ } else if (arg === '--signature') {
223
+ opts.signature = requireValue(argv, i, arg);
224
+ i += 1;
225
+ } else if (arg === '--limit') {
226
+ opts.limit = parseLimit(requireValue(argv, i, arg));
227
+ i += 1;
228
+ } else if (allowJson && arg === '--json') {
229
+ opts.json = true;
230
+ } else if (arg === '--help' || arg === '-h') {
231
+ return { help: true };
232
+ } else {
233
+ throw new UsageError(`unknown option for ${subcommand}: ${arg}`);
234
+ }
235
+ }
236
+
237
+ return opts;
238
+ }
239
+
240
+ function handleAdd(argv, env, cwd) {
241
+ const opts = parseAddArgs(argv, env);
242
+ if (opts.help) {
243
+ process.stdout.write(`${usageText()}\n`);
244
+ return 0;
245
+ }
246
+
247
+ let res;
248
+ try {
249
+ res = localErrors.addProvisionalLesson({
250
+ workspaceRoot: cwd,
251
+ destination: opts.destination,
252
+ signature: opts.signature,
253
+ match: opts.match,
254
+ lesson: opts.lesson,
255
+ ...(opts.cue !== undefined ? { cue: opts.cue } : {}),
256
+ ...(opts.issueUrl !== undefined ? { issueUrl: opts.issueUrl } : {}),
257
+ ...(opts.promotionId !== undefined ? { promotionId: opts.promotionId } : {}),
258
+ }, { env });
259
+ } catch (error) {
260
+ if (isValidationError(error)) {
261
+ throw new UsageError(error.message);
262
+ }
263
+ throw error;
264
+ }
265
+
266
+ if (res.status === 'lock-timeout' || !res.record) {
267
+ process.stderr.write(`error: could not acquire local error store lock\n`);
268
+ return 1;
269
+ }
270
+
271
+ const sanitized = sanitizeExportRecord(res.record, env);
272
+ if (opts.json) {
273
+ process.stdout.write(`${JSON.stringify({ status: res.status, record: sanitized })}\n`);
274
+ } else {
275
+ process.stdout.write(`${res.status} ${sanitized.provisionalId} ${sanitized.signature} (${sanitized.destination})\n`);
276
+ }
277
+ return 0;
278
+ }
279
+
280
+ function handleList(argv, env, cwd) {
281
+ const opts = parseQueryArgs('list', argv, true);
282
+ if (opts.help) {
283
+ process.stdout.write(`${usageText()}\n`);
284
+ return 0;
285
+ }
286
+
287
+ const records = localErrors.listRecords({
288
+ env,
289
+ workspaceRoot: cwd,
290
+ ...(opts.kind ? { kind: opts.kind } : {}),
291
+ ...(opts.destination ? { destination: opts.destination } : {}),
292
+ ...(opts.signature ? { signature: opts.signature } : {}),
293
+ limit: opts.limit,
294
+ }).map(r => sanitizeExportRecord(r, env)).filter(Boolean);
295
+
296
+ if (opts.json) {
297
+ process.stdout.write(`${JSON.stringify(records)}\n`);
298
+ return 0;
299
+ }
300
+
301
+ for (const record of records) {
302
+ const summary = record.kind === 'provisional-lesson'
303
+ ? record.lesson.replace(/\s+/g, ' ').slice(0, 120)
304
+ : `${record.commandClass} exit=${record.exitCode === null ? 'unknown' : record.exitCode}`;
305
+ process.stdout.write(`${record.provisionalId}\t${record.kind}\t${record.destination}\t${record.signature}\t${summary}\n`);
306
+ }
307
+ return 0;
308
+ }
309
+
310
+ function handleExport(argv, env, cwd) {
311
+ const opts = parseQueryArgs('export', argv, false);
312
+ if (opts.help) {
313
+ process.stdout.write(`${usageText()}\n`);
314
+ return 0;
315
+ }
316
+
317
+ const records = localErrors.listRecords({
318
+ env,
319
+ workspaceRoot: cwd,
320
+ ...(opts.kind ? { kind: opts.kind } : {}),
321
+ ...(opts.destination ? { destination: opts.destination } : {}),
322
+ ...(opts.signature ? { signature: opts.signature } : {}),
323
+ limit: opts.limit,
324
+ }).map(r => sanitizeExportRecord(r, env)).filter(Boolean);
325
+
326
+ for (const record of records) {
327
+ process.stdout.write(`${JSON.stringify(record)}\n`);
328
+ }
329
+ return 0;
330
+ }
331
+
332
+ function main(argv = process.argv.slice(2), env = process.env, cwd = process.cwd()) {
333
+ try {
334
+ const [subcommand, ...rest] = argv;
335
+ if (!subcommand || subcommand === '--help' || subcommand === '-h') {
336
+ process.stdout.write(`${usageText()}\n`);
337
+ return subcommand ? 0 : 2;
338
+ }
339
+ if (subcommand === 'add') return handleAdd(rest, env, cwd);
340
+ if (subcommand === 'list') return handleList(rest, env, cwd);
341
+ if (subcommand === 'export') return handleExport(rest, env, cwd);
342
+ throw new UsageError(`unknown subcommand: ${subcommand}`);
343
+ } catch (error) {
344
+ if (error instanceof UsageError) {
345
+ process.stderr.write(`usage error: ${error.message}\n`);
346
+ return 2;
347
+ }
348
+ process.stderr.write(`error: ${error && error.message ? error.message : String(error)}\n`);
349
+ return 1;
350
+ }
351
+ }
352
+
353
+ if (require.main === module) {
354
+ process.exit(main());
355
+ }
356
+
357
+ module.exports = { main, sanitizeExportRecord };