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.
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +21 -0
- package/ci/mechanism-394-ledger-match.test.js +324 -3
- package/ci/mechanism-496-claude-capture.test.js +342 -0
- package/ci/mechanism-496-codex-capture.test.js +508 -0
- package/ci/mechanism-496-local-error-cli-edges.test.js +234 -0
- package/ci/mechanism-496-local-error-cli.test.js +517 -0
- package/ci/mechanism-496-local-error-store.test.js +731 -0
- package/ci/mechanism-496-opencode-capture.test.js +483 -0
- package/ci/mechanism-496-review-fixes.test.js +278 -0
- package/ci/mechanism-516-agy-post-tool-exit.test.js +472 -0
- package/ci/mechanism-516-agy-stop-revision.test.js +215 -0
- package/hooks/scripts/agy-post-tool.js +79 -16
- package/hooks/scripts/agy-stop.js +63 -10
- package/hooks/scripts/codex-ledger-lesson.js +103 -27
- package/hooks/scripts/ledger-lesson.js +94 -23
- package/hooks/scripts/lib/ledger-match.js +169 -21
- package/hooks/scripts/lib/local-error-source.js +127 -0
- package/hooks/scripts/lib/local-errors.js +597 -0
- package/hooks/scripts/lib/observations.js +1 -1
- package/hooks/scripts/state-persist.js +7 -1
- package/opencode-plugin/index.mjs +86 -1
- package/opencode-plugin/plugin.json +1 -1
- package/package.json +2 -1
- package/plugins/harness-everything/.antigravity-plugin/plugin.json +1 -1
- package/plugins/harness-everything/.codex-plugin/plugin.json +1 -1
- package/plugins/harness-everything/hooks/scripts/agy-post-tool.js +79 -16
- package/plugins/harness-everything/hooks/scripts/agy-stop.js +63 -10
- package/plugins/harness-everything/hooks/scripts/codex-ledger-lesson.js +103 -27
- package/plugins/harness-everything/hooks/scripts/ledger-lesson.js +94 -23
- package/plugins/harness-everything/hooks/scripts/lib/ledger-match.js +169 -21
- package/plugins/harness-everything/hooks/scripts/lib/local-error-source.js +127 -0
- package/plugins/harness-everything/hooks/scripts/lib/local-errors.js +597 -0
- package/plugins/harness-everything/hooks/scripts/lib/observations.js +1 -1
- package/plugins/harness-everything/hooks/scripts/state-persist.js +7 -1
- package/plugins/harness-everything/plugin.json +1 -1
- package/sbom.cdx.json +7 -7
- package/scripts/errors-local.js +357 -0
|
@@ -12,6 +12,18 @@ try {
|
|
|
12
12
|
} catch (_) {
|
|
13
13
|
// Missing runtime pieces leave the host hook fail-open and silent.
|
|
14
14
|
}
|
|
15
|
+
let localErrorSource;
|
|
16
|
+
try {
|
|
17
|
+
localErrorSource = require('./lib/local-error-source');
|
|
18
|
+
} catch (_) {
|
|
19
|
+
// Missing provisional source leaves the host hook fail-open.
|
|
20
|
+
}
|
|
21
|
+
let localErrors;
|
|
22
|
+
try {
|
|
23
|
+
localErrors = require('./lib/local-errors');
|
|
24
|
+
} catch (_) {
|
|
25
|
+
// Missing local error store leaves capture fail-open and silent.
|
|
26
|
+
}
|
|
15
27
|
|
|
16
28
|
let pending = null;
|
|
17
29
|
function releasePending() {
|
|
@@ -22,6 +34,39 @@ function releasePending() {
|
|
|
22
34
|
|
|
23
35
|
process.stdout.on('error', releasePending);
|
|
24
36
|
|
|
37
|
+
function structuredExitCode(payload) {
|
|
38
|
+
const response = payload.tool_response && typeof payload.tool_response === 'object' && !Array.isArray(payload.tool_response)
|
|
39
|
+
? payload.tool_response : null;
|
|
40
|
+
for (const candidate of [payload.exit_code, payload.exitCode, response?.exit_code, response?.exitCode]) {
|
|
41
|
+
if (Number.isInteger(candidate)) return candidate;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function captureFailure(payload, workspace, sessionIdentity, command, event) {
|
|
47
|
+
if (!localErrors || typeof localErrors.appendOccurrence !== 'function') return;
|
|
48
|
+
try {
|
|
49
|
+
const matchedLedgerIds = typeof matcher?.peekRepoMatches === 'function'
|
|
50
|
+
? matcher.peekRepoMatches({ workspaceRoot: workspace.workspaceRoot, event })
|
|
51
|
+
: [];
|
|
52
|
+
localErrors.appendOccurrence({
|
|
53
|
+
workspaceRoot: workspace.workspaceRoot,
|
|
54
|
+
host: 'claude',
|
|
55
|
+
sessionKey: sessionIdentity,
|
|
56
|
+
...(typeof payload.tool_use_id === 'string' && payload.tool_use_id.trim()
|
|
57
|
+
? { toolUseId: payload.tool_use_id }
|
|
58
|
+
: {}),
|
|
59
|
+
command,
|
|
60
|
+
exitCode: structuredExitCode(payload),
|
|
61
|
+
output: typeof payload.error === 'string' ? payload.error : '',
|
|
62
|
+
matchedLedgerIds,
|
|
63
|
+
destination: 'unknown'
|
|
64
|
+
}, { env: process.env });
|
|
65
|
+
} catch (_) {
|
|
66
|
+
// Capture is write-only, silent, and fail-open.
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
25
70
|
function handle(payload) {
|
|
26
71
|
try {
|
|
27
72
|
if (!payload || typeof payload !== 'object' || Array.isArray(payload) || !state || !matcher) return;
|
|
@@ -38,30 +83,56 @@ function handle(payload) {
|
|
|
38
83
|
|
|
39
84
|
const workspace = state.resolveWorkspaceIdentity(payload);
|
|
40
85
|
if (!workspace.workspaceRoot || !workspace.workspaceKey) return;
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
86
|
+
|
|
87
|
+
const event = {
|
|
88
|
+
phase,
|
|
89
|
+
tool: 'shell',
|
|
90
|
+
command,
|
|
91
|
+
outcome: phase === 'failure'
|
|
92
|
+
? { failed: true, ...(typeof payload.error === 'string' ? { output: payload.error } : {}) }
|
|
93
|
+
: { failed: 'unknown' }
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
let provisionalSource;
|
|
97
|
+
if (localErrorSource && typeof localErrorSource.createProvisionalSource === 'function') {
|
|
98
|
+
try {
|
|
99
|
+
provisionalSource = localErrorSource.createProvisionalSource({
|
|
100
|
+
env: process.env,
|
|
101
|
+
workspaceRoot: workspace.workspaceRoot
|
|
102
|
+
});
|
|
103
|
+
} catch (_) {
|
|
104
|
+
provisionalSource = undefined;
|
|
54
105
|
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let result = null;
|
|
109
|
+
try {
|
|
110
|
+
const sessionDir = state.getSessionDir(workspace.workspaceRoot, sessionIdentity, payload);
|
|
111
|
+
result = matcher.matchLedgerLessons({
|
|
112
|
+
workspaceRoot: workspace.workspaceRoot,
|
|
113
|
+
workspaceIdentity: workspace.workspaceKey,
|
|
114
|
+
sessionIdentity,
|
|
115
|
+
stateDir: path.join(sessionDir, 'ledger-lessons'),
|
|
116
|
+
event,
|
|
117
|
+
...(provisionalSource ? { provisionalSource } : {})
|
|
118
|
+
});
|
|
119
|
+
} catch (_) {
|
|
120
|
+
result = null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (result) {
|
|
124
|
+
pending = result;
|
|
125
|
+
process.stdout.write(`${JSON.stringify({
|
|
126
|
+
hookSpecificOutput: { hookEventName, additionalContext: result.text }
|
|
127
|
+
})}\n`, error => {
|
|
128
|
+
if (error) releasePending();
|
|
129
|
+
else pending = null;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (phase === 'failure') {
|
|
134
|
+
captureFailure(payload, workspace, sessionIdentity, command, event);
|
|
135
|
+
}
|
|
65
136
|
} catch (_) {
|
|
66
137
|
releasePending();
|
|
67
138
|
}
|
|
@@ -190,40 +190,157 @@ function words(command) {
|
|
|
190
190
|
return tokens;
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
-
|
|
194
|
-
|
|
193
|
+
function literalRedirectTarget(command, start) {
|
|
194
|
+
let index = start;
|
|
195
|
+
while (index < command.length && /\s/.test(command[index])) index += 1;
|
|
196
|
+
const targetStart = index;
|
|
197
|
+
let quote = null;
|
|
198
|
+
let length = 0;
|
|
199
|
+
for (; index < command.length; index += 1) {
|
|
200
|
+
const char = command[index];
|
|
201
|
+
if (quote === "'") {
|
|
202
|
+
if (char === "'") quote = null;
|
|
203
|
+
else length += 1;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (quote === '"') {
|
|
207
|
+
if (char === '"') quote = null;
|
|
208
|
+
else if (char === '\\' && index + 1 < command.length) { index += 1; length += 1; }
|
|
209
|
+
else if (char === '$' || char === '`') return null;
|
|
210
|
+
else length += 1;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (char === '"' || char === "'") { quote = char; continue; }
|
|
214
|
+
if (char === '\\') {
|
|
215
|
+
if (index + 1 >= command.length) return null;
|
|
216
|
+
index += 1;
|
|
217
|
+
length += 1;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (/\s/.test(char) || ';|&<>'.includes(char)) break;
|
|
221
|
+
if (char === '#' && index === targetStart) return null;
|
|
222
|
+
if ('()$`'.includes(char) || /[*?\[\]{}]/.test(char) || (char === '~' && length === 0)) return null;
|
|
223
|
+
length += 1;
|
|
224
|
+
}
|
|
225
|
+
return quote || length === 0 ? null : index;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function readRedirect(command, index, fileDescriptor) {
|
|
229
|
+
const char = command[index];
|
|
230
|
+
let operatorEnd = index + 1;
|
|
231
|
+
if (char === '<') {
|
|
232
|
+
if (command[index + 1] === '<' || command[index + 1] === '>') return null;
|
|
233
|
+
} else if (char === '>') {
|
|
234
|
+
if (command[index + 1] === '|') return null;
|
|
235
|
+
if (command[index + 1] === '>') operatorEnd += 1;
|
|
236
|
+
else if (command[index + 1] === '&') {
|
|
237
|
+
if (!fileDescriptor) return null;
|
|
238
|
+
let targetEnd = index + 2;
|
|
239
|
+
const digitsStart = targetEnd;
|
|
240
|
+
while (/[0-9]/.test(command[targetEnd] || '')) targetEnd += 1;
|
|
241
|
+
if (targetEnd === digitsStart ||
|
|
242
|
+
(targetEnd < command.length && !/\s/.test(command[targetEnd]) && !';|&<>'.includes(command[targetEnd]))) return null;
|
|
243
|
+
return targetEnd;
|
|
244
|
+
}
|
|
245
|
+
} else if (char === '&') {
|
|
246
|
+
if (command[index + 1] !== '>' || command[index + 2] === '>') return null;
|
|
247
|
+
operatorEnd += 1;
|
|
248
|
+
} else return null;
|
|
249
|
+
return literalRedirectTarget(command, operatorEnd);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Recognize a bounded top-level command list; redirects are removed before
|
|
253
|
+
// candidate matching so their filenames never become command words.
|
|
195
254
|
function splitSimpleCommandSegments(command) {
|
|
196
255
|
if (typeof command !== 'string' || command.length > LIMITS.command || /[\r\n\0]/.test(command)) return null;
|
|
197
256
|
const segments = [];
|
|
257
|
+
let segment = '';
|
|
198
258
|
let quote = null;
|
|
199
|
-
let
|
|
259
|
+
let tokenStart = true;
|
|
260
|
+
let afterSeparator = false;
|
|
200
261
|
for (let index = 0; index < command.length; index += 1) {
|
|
201
262
|
const char = command[index];
|
|
202
|
-
if (quote) {
|
|
203
|
-
|
|
204
|
-
|
|
263
|
+
if (quote === "'") {
|
|
264
|
+
segment += char;
|
|
265
|
+
if (char === "'") quote = null;
|
|
266
|
+
tokenStart = false;
|
|
267
|
+
afterSeparator = false;
|
|
205
268
|
continue;
|
|
206
269
|
}
|
|
207
|
-
if (
|
|
208
|
-
|
|
209
|
-
|
|
270
|
+
if (quote === '"') {
|
|
271
|
+
if (char === '"') { segment += char; quote = null; tokenStart = false; afterSeparator = false; continue; }
|
|
272
|
+
if (char === '\\' && index + 1 < command.length) { segment += char + command[++index]; afterSeparator = false; continue; }
|
|
273
|
+
if (char === '`' || (char === '$' && command[index + 1] === '(')) return null;
|
|
274
|
+
segment += char;
|
|
275
|
+
afterSeparator = false;
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (char === '"' || char === "'") { segment += char; quote = char; tokenStart = false; afterSeparator = false; continue; }
|
|
279
|
+
if (char === '\\' && index + 1 < command.length) { segment += char + command[++index]; tokenStart = false; afterSeparator = false; continue; }
|
|
280
|
+
if (char === '`' || (char === '$' && command[index + 1] === '(') || '()'.includes(char)) return null;
|
|
281
|
+
if (char === '#' && tokenStart) {
|
|
282
|
+
if (afterSeparator) return null;
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
if (/\s/.test(char)) { segment += char; tokenStart = true; continue; }
|
|
286
|
+
|
|
287
|
+
let descriptorEnd = index;
|
|
288
|
+
if (tokenStart && /[0-9]/.test(char)) {
|
|
289
|
+
while (/[0-9]/.test(command[descriptorEnd] || '')) descriptorEnd += 1;
|
|
290
|
+
if (command[descriptorEnd] === '>' || command[descriptorEnd] === '<') {
|
|
291
|
+
const end = readRedirect(command, descriptorEnd, true);
|
|
292
|
+
if (end === null) return null;
|
|
293
|
+
index = end - 1;
|
|
294
|
+
tokenStart = true;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (char === '<' || char === '>') {
|
|
299
|
+
const end = readRedirect(command, index, false);
|
|
300
|
+
if (end === null) return null;
|
|
301
|
+
index = end - 1;
|
|
302
|
+
tokenStart = true;
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
if (char === '&' && command[index + 1] === '>') {
|
|
306
|
+
const end = readRedirect(command, index, false);
|
|
307
|
+
if (end === null) return null;
|
|
308
|
+
index = end - 1;
|
|
309
|
+
tokenStart = true;
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
|
|
210
313
|
let separatorLength = 0;
|
|
211
314
|
if (char === ';') separatorLength = 1;
|
|
212
|
-
else if (char === '&'
|
|
213
|
-
if (command[index + 1] !==
|
|
315
|
+
else if (char === '&') {
|
|
316
|
+
if (command[index + 1] !== '&' || command[index + 2] === '&') return null;
|
|
214
317
|
separatorLength = 2;
|
|
318
|
+
} else if (char === '|') {
|
|
319
|
+
if (command[index + 1] === '&' || (command[index + 1] === '|' && command[index + 2] === '|')) return null;
|
|
320
|
+
separatorLength = command[index + 1] === '|' ? 2 : 1;
|
|
215
321
|
}
|
|
216
|
-
if (
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
322
|
+
if (separatorLength) {
|
|
323
|
+
const current = segment.trim();
|
|
324
|
+
if (!current || segments.length >= LIMITS.commandSegments - 1) return null;
|
|
325
|
+
segments.push(current);
|
|
326
|
+
segment = '';
|
|
327
|
+
index += separatorLength - 1;
|
|
328
|
+
tokenStart = true;
|
|
329
|
+
afterSeparator = true;
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (char === '&' || char === '|') return null;
|
|
333
|
+
segment += char;
|
|
334
|
+
tokenStart = false;
|
|
335
|
+
afterSeparator = false;
|
|
222
336
|
}
|
|
223
337
|
if (quote) return null;
|
|
224
|
-
const finalSegment =
|
|
225
|
-
if (
|
|
226
|
-
|
|
338
|
+
const finalSegment = segment.trim();
|
|
339
|
+
if (afterSeparator) return null;
|
|
340
|
+
if (finalSegment) {
|
|
341
|
+
if (segments.length >= LIMITS.commandSegments) return null;
|
|
342
|
+
segments.push(finalSegment);
|
|
343
|
+
}
|
|
227
344
|
return segments;
|
|
228
345
|
}
|
|
229
346
|
|
|
@@ -485,6 +602,31 @@ function releaseClaim(claimed) {
|
|
|
485
602
|
try { fs.unlinkSync(claimed.file); return true; } catch (_) { return false; }
|
|
486
603
|
}
|
|
487
604
|
|
|
605
|
+
// Read-only repo match lookup before delivery claims or reminder caps (#496).
|
|
606
|
+
function peekRepoMatches(input) {
|
|
607
|
+
try {
|
|
608
|
+
if (!isObject(input)) return [];
|
|
609
|
+
const root = canonicalWorkspace(input.workspaceRoot);
|
|
610
|
+
const event = normalizeEvent(input.event);
|
|
611
|
+
if (!event) return [];
|
|
612
|
+
const repo = matchedRepoCandidates(root, event);
|
|
613
|
+
if (!repo.length) return [];
|
|
614
|
+
const compare = (left, right) => left < right ? -1 : left > right ? 1 : 0;
|
|
615
|
+
const ordered = [...repo].sort((a, b) => compare(a.signature, b.signature) || compare(a.id, b.id));
|
|
616
|
+
const ids = [];
|
|
617
|
+
const seen = new Set();
|
|
618
|
+
for (const candidate of ordered) {
|
|
619
|
+
if (seen.has(candidate.id)) continue;
|
|
620
|
+
seen.add(candidate.id);
|
|
621
|
+
ids.push(candidate.id);
|
|
622
|
+
if (ids.length >= LIMITS.candidates) break;
|
|
623
|
+
}
|
|
624
|
+
return ids;
|
|
625
|
+
} catch (_) {
|
|
626
|
+
return [];
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
488
630
|
// Carriers release a reservation only when the returned context handoff is dropped.
|
|
489
631
|
function releaseLedgerMatch(result) {
|
|
490
632
|
const claims = result && PENDING_HANDOFFS.get(result);
|
|
@@ -498,4 +640,10 @@ function releaseLedgerMatch(result) {
|
|
|
498
640
|
return true;
|
|
499
641
|
}
|
|
500
642
|
|
|
501
|
-
module.exports = {
|
|
643
|
+
module.exports = {
|
|
644
|
+
matchLedgerLessons,
|
|
645
|
+
normalizeGitCommitCommand,
|
|
646
|
+
peekRepoMatches,
|
|
647
|
+
releaseLedgerMatch,
|
|
648
|
+
validateMatch
|
|
649
|
+
};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Provisional local lesson source reader (#496 C2) for hooks/scripts/lib/ledger-match.js.
|
|
3
|
+
// Reads workspace-scoped provisional lessons from local-errors.js on each lookup,
|
|
4
|
+
// projecting only the bounded matcher fields and failing open on any store error.
|
|
5
|
+
|
|
6
|
+
const localErrors = require('./local-errors');
|
|
7
|
+
|
|
8
|
+
const IDENTIFIER_PATTERN = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
9
|
+
const RECORD_ID_PATTERN = /^E-\d{4,}$/;
|
|
10
|
+
const RESERVED_E_PREFIX = /^E-/i;
|
|
11
|
+
const DEFAULT_MAX_CANDIDATES = 32;
|
|
12
|
+
const DEFAULT_MAX_BYTES = 64 * 1024;
|
|
13
|
+
const DEFAULT_MAX_TEXT_CHARS = 4096;
|
|
14
|
+
|
|
15
|
+
function validIdentifier(value) {
|
|
16
|
+
return typeof value === 'string' && IDENTIFIER_PATTERN.test(value);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function projectProvisionalRecord(record, maxTextChars) {
|
|
20
|
+
if (!record || typeof record !== 'object' || record.kind !== 'provisional-lesson') return null;
|
|
21
|
+
|
|
22
|
+
const identity = record.provisionalId;
|
|
23
|
+
if (!validIdentifier(identity) || RESERVED_E_PREFIX.test(identity)) return null;
|
|
24
|
+
|
|
25
|
+
if (typeof record.lesson !== 'string') return null;
|
|
26
|
+
const lesson = record.lesson.trim().slice(0, maxTextChars);
|
|
27
|
+
if (!lesson) return null;
|
|
28
|
+
|
|
29
|
+
let match;
|
|
30
|
+
try {
|
|
31
|
+
match = localErrors.validateMatch(record.match);
|
|
32
|
+
} catch (_) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const candidate = { identity, match, lesson };
|
|
37
|
+
|
|
38
|
+
if (validIdentifier(record.signature)) {
|
|
39
|
+
candidate.signature = record.signature;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (typeof record.promotionId === 'string' && RECORD_ID_PATTERN.test(record.promotionId)) {
|
|
43
|
+
candidate.promotionId = record.promotionId;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (typeof record.cue === 'string') {
|
|
47
|
+
candidate.cue = record.cue.slice(0, maxTextChars);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return candidate;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Mirror ledger-match.js validProvisionalCandidate shape when checking byte budget
|
|
54
|
+
// so readProvisional's serialized byte check cannot fail on accepted candidates.
|
|
55
|
+
function matcherSerializedBytes(candidates) {
|
|
56
|
+
const transformed = candidates.map(candidate => ({
|
|
57
|
+
id: candidate.identity,
|
|
58
|
+
identity: candidate.signature ? `signature:${candidate.signature}` : `identity:${candidate.identity}`,
|
|
59
|
+
signature: candidate.signature || null,
|
|
60
|
+
promotionId: candidate.promotionId || null,
|
|
61
|
+
match: candidate.match,
|
|
62
|
+
cue: candidate.cue || '',
|
|
63
|
+
lesson: candidate.lesson,
|
|
64
|
+
source: 'provisional',
|
|
65
|
+
}));
|
|
66
|
+
return Buffer.byteLength(JSON.stringify(transformed), 'utf8');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function createProvisionalSource(options = {}) {
|
|
70
|
+
const env = options && options.env ? options.env : process.env;
|
|
71
|
+
const root = options && typeof options.root === 'string' && options.root ? options.root : undefined;
|
|
72
|
+
const boundWorkspaceRoot = options && typeof options.workspaceRoot === 'string' && options.workspaceRoot.trim()
|
|
73
|
+
? options.workspaceRoot.trim()
|
|
74
|
+
: null;
|
|
75
|
+
|
|
76
|
+
return function provisionalSource(workspaceIdentity, budget = {}) {
|
|
77
|
+
try {
|
|
78
|
+
const maxCandidates = Number.isInteger(budget && budget.maxCandidates) && budget.maxCandidates > 0
|
|
79
|
+
? budget.maxCandidates
|
|
80
|
+
: DEFAULT_MAX_CANDIDATES;
|
|
81
|
+
const maxBytes = Number.isInteger(budget && budget.maxBytes) && budget.maxBytes > 0
|
|
82
|
+
? budget.maxBytes
|
|
83
|
+
: DEFAULT_MAX_BYTES;
|
|
84
|
+
const maxTextChars = Number.isInteger(budget && budget.maxTextChars) && budget.maxTextChars > 0
|
|
85
|
+
? budget.maxTextChars
|
|
86
|
+
: DEFAULT_MAX_TEXT_CHARS;
|
|
87
|
+
|
|
88
|
+
const query = {
|
|
89
|
+
env,
|
|
90
|
+
kind: 'provisional-lesson',
|
|
91
|
+
...(root ? { root } : {}),
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
if (boundWorkspaceRoot) {
|
|
95
|
+
query.workspaceRoot = boundWorkspaceRoot;
|
|
96
|
+
} else if (typeof workspaceIdentity === 'string' && workspaceIdentity.trim()) {
|
|
97
|
+
query.workspaceKey = workspaceIdentity.trim();
|
|
98
|
+
} else {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const records = localErrors.listRecords(query);
|
|
103
|
+
if (!Array.isArray(records) || !records.length) return [];
|
|
104
|
+
|
|
105
|
+
// localErrors.listRecords returns oldest-first; walk newest-first so recent lessons win within budget.
|
|
106
|
+
const projected = [];
|
|
107
|
+
for (let i = records.length - 1; i >= 0; i -= 1) {
|
|
108
|
+
if (projected.length >= maxCandidates) break;
|
|
109
|
+
const candidate = projectProvisionalRecord(records[i], maxTextChars);
|
|
110
|
+
if (!candidate) continue;
|
|
111
|
+
const next = [...projected, candidate];
|
|
112
|
+
if (Buffer.byteLength(JSON.stringify(next), 'utf8') > maxBytes) continue;
|
|
113
|
+
if (matcherSerializedBytes(next) > maxBytes) continue;
|
|
114
|
+
projected.push(candidate);
|
|
115
|
+
}
|
|
116
|
+
projected.reverse();
|
|
117
|
+
return projected;
|
|
118
|
+
} catch (_) {
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = {
|
|
125
|
+
createProvisionalSource,
|
|
126
|
+
projectProvisionalRecord,
|
|
127
|
+
};
|