harness-everything 1.0.1 → 1.1.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/CHANGELOG.md +22 -0
- package/benchmarks/results/live-host/agy-2026-11-25/README.md +32 -0
- package/ci/mechanism-394-ledger-match.test.js +219 -0
- 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-windows-verify-and-shell.test.js +328 -0
- package/docs/platform-capabilities.md +2 -2
- package/docs/platform-compatibility.json +2 -2
- package/hooks/scripts/codex-ledger-lesson.js +103 -27
- package/hooks/scripts/ledger-lesson.js +94 -23
- package/hooks/scripts/lib/ledger-match.js +39 -3
- 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/lib/workflow-isolation.js +1 -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/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 +39 -3
- 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/lib/workflow-isolation.js +1 -1
- package/plugins/harness-everything/plugin.json +1 -1
- package/plugins/harness-everything/scripts/lib/verification-contract.js +68 -12
- package/sbom.cdx.json +7 -7
- package/scripts/errors-local.js +357 -0
- package/scripts/lib/verification-contract.js +68 -12
|
@@ -4,58 +4,134 @@
|
|
|
4
4
|
const path = require('node:path');
|
|
5
5
|
const { getWorkspaceRoot, getSessionDir, getSessionId, resolveWorkspaceIdentity } = require('./lib/harness-state');
|
|
6
6
|
const { readCodexToolOutcome } = require('./lib/codex-tool-outcome');
|
|
7
|
-
const { matchLedgerLessons, releaseLedgerMatch } = require('./lib/ledger-match');
|
|
7
|
+
const { matchLedgerLessons, peekRepoMatches, releaseLedgerMatch } = require('./lib/ledger-match');
|
|
8
|
+
|
|
9
|
+
let localErrorSource;
|
|
10
|
+
try {
|
|
11
|
+
localErrorSource = require('./lib/local-error-source');
|
|
12
|
+
} catch (_) {
|
|
13
|
+
// Missing provisional source leaves the host hook fail-open.
|
|
14
|
+
}
|
|
15
|
+
let localErrors;
|
|
16
|
+
try {
|
|
17
|
+
localErrors = require('./lib/local-errors');
|
|
18
|
+
} catch (_) {
|
|
19
|
+
// Missing local error store leaves capture fail-open and silent.
|
|
20
|
+
}
|
|
8
21
|
|
|
9
22
|
const MAX_INPUT_BYTES = 128 * 1024;
|
|
10
23
|
|
|
11
|
-
function
|
|
24
|
+
function buildInvocation(payload) {
|
|
12
25
|
if (payload?.tool_name !== 'Bash' || typeof payload?.tool_input?.command !== 'string') return null;
|
|
13
26
|
const command = payload.tool_input.command;
|
|
14
27
|
if (payload.hook_event_name === 'PreToolUse') {
|
|
15
|
-
return {
|
|
28
|
+
return {
|
|
29
|
+
event: { phase: 'action', tool: 'shell', command, outcome: { failed: 'unknown' } },
|
|
30
|
+
toolOutcome: null,
|
|
31
|
+
};
|
|
16
32
|
}
|
|
17
33
|
if (payload.hook_event_name !== 'PostToolUse') return null;
|
|
18
34
|
|
|
19
|
-
const
|
|
20
|
-
const failed =
|
|
35
|
+
const toolOutcome = readCodexToolOutcome(payload);
|
|
36
|
+
const failed = toolOutcome.outcome === 'failure' ? true : toolOutcome.outcome === 'success' ? false : 'unknown';
|
|
21
37
|
const outcome = { failed };
|
|
22
38
|
// Exact ledger diagnostics can identify a masked failure only when the host
|
|
23
39
|
// provides a structured exit outcome. Opaque text-only responses stay unknown.
|
|
24
|
-
if (
|
|
25
|
-
|
|
40
|
+
if (toolOutcome.outcome !== 'unknown') {
|
|
41
|
+
outcome.output = [toolOutcome.stderr, toolOutcome.stdout].filter(Boolean).join('\n');
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
event: { phase: 'failure', tool: 'shell', command, outcome },
|
|
45
|
+
toolOutcome,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function extractToolUseId(payload) {
|
|
50
|
+
for (const candidate of [payload?.tool_use_id, payload?.toolUseId, payload?.call_id, payload?.callId]) {
|
|
51
|
+
if (typeof candidate === 'string' && candidate.trim()) return candidate.trim();
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function captureFailure(payload, workspaceRoot, sessionIdentity, event, toolOutcome) {
|
|
57
|
+
if (!localErrors || typeof localErrors.appendOccurrence !== 'function') return;
|
|
58
|
+
try {
|
|
59
|
+
const matchedLedgerIds = typeof peekRepoMatches === 'function'
|
|
60
|
+
? peekRepoMatches({ workspaceRoot, event })
|
|
61
|
+
: [];
|
|
62
|
+
const toolUseId = extractToolUseId(payload);
|
|
63
|
+
localErrors.appendOccurrence({
|
|
64
|
+
workspaceRoot,
|
|
65
|
+
host: 'codex',
|
|
66
|
+
sessionKey: sessionIdentity,
|
|
67
|
+
...(toolUseId ? { toolUseId } : {}),
|
|
68
|
+
command: event.command,
|
|
69
|
+
exitCode: Number.isInteger(toolOutcome.exitCode) ? toolOutcome.exitCode : null,
|
|
70
|
+
output: typeof event.outcome?.output === 'string' ? event.outcome.output : '',
|
|
71
|
+
matchedLedgerIds,
|
|
72
|
+
destination: 'unknown',
|
|
73
|
+
}, { env: process.env });
|
|
74
|
+
} catch (_) {
|
|
75
|
+
// Capture is write-only, silent, and fail-open.
|
|
76
|
+
}
|
|
26
77
|
}
|
|
27
78
|
|
|
28
79
|
function deliver(payload) {
|
|
29
80
|
if (typeof payload?.cwd !== 'string' || !payload.cwd.trim()) return;
|
|
30
|
-
const
|
|
81
|
+
const invocation = buildInvocation(payload);
|
|
31
82
|
const sessionIdentity = getSessionId(payload);
|
|
32
|
-
if (!
|
|
83
|
+
if (!invocation || !sessionIdentity) return;
|
|
33
84
|
|
|
85
|
+
const { event, toolOutcome } = invocation;
|
|
34
86
|
const workspaceRoot = getWorkspaceRoot(payload);
|
|
35
87
|
const identity = resolveWorkspaceIdentity(payload);
|
|
36
88
|
if (!workspaceRoot || !identity?.workspaceKey) return;
|
|
37
|
-
const stateDir = path.join(getSessionDir(workspaceRoot, sessionIdentity, payload), 'ledger-lessons');
|
|
38
|
-
const result = matchLedgerLessons({
|
|
39
|
-
workspaceRoot,
|
|
40
|
-
workspaceIdentity: identity.workspaceKey,
|
|
41
|
-
sessionIdentity,
|
|
42
|
-
stateDir,
|
|
43
|
-
event,
|
|
44
|
-
});
|
|
45
|
-
if (!result) return;
|
|
46
89
|
|
|
90
|
+
let provisionalSource;
|
|
91
|
+
if (localErrorSource && typeof localErrorSource.createProvisionalSource === 'function') {
|
|
92
|
+
try {
|
|
93
|
+
provisionalSource = localErrorSource.createProvisionalSource({
|
|
94
|
+
env: process.env,
|
|
95
|
+
workspaceRoot,
|
|
96
|
+
});
|
|
97
|
+
} catch (_) {
|
|
98
|
+
provisionalSource = undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let result = null;
|
|
47
103
|
try {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
104
|
+
const stateDir = path.join(getSessionDir(workspaceRoot, sessionIdentity, payload), 'ledger-lessons');
|
|
105
|
+
result = matchLedgerLessons({
|
|
106
|
+
workspaceRoot,
|
|
107
|
+
workspaceIdentity: identity.workspaceKey,
|
|
108
|
+
sessionIdentity,
|
|
109
|
+
stateDir,
|
|
110
|
+
event,
|
|
111
|
+
...(provisionalSource ? { provisionalSource } : {}),
|
|
56
112
|
});
|
|
57
113
|
} catch (_) {
|
|
58
|
-
|
|
114
|
+
result = null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (result) {
|
|
118
|
+
try {
|
|
119
|
+
const output = JSON.stringify({
|
|
120
|
+
hookSpecificOutput: {
|
|
121
|
+
hookEventName: payload.hook_event_name,
|
|
122
|
+
additionalContext: result.text,
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
process.stdout.write(output, error => {
|
|
126
|
+
if (error) releaseLedgerMatch(result);
|
|
127
|
+
});
|
|
128
|
+
} catch (_) {
|
|
129
|
+
releaseLedgerMatch(result);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (payload.hook_event_name === 'PostToolUse' && toolOutcome?.outcome === 'failure') {
|
|
134
|
+
captureFailure(payload, workspaceRoot, sessionIdentity, event, toolOutcome);
|
|
59
135
|
}
|
|
60
136
|
}
|
|
61
137
|
|
|
@@ -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
|
}
|
|
@@ -211,7 +211,12 @@ function literalRedirectTarget(command, start) {
|
|
|
211
211
|
continue;
|
|
212
212
|
}
|
|
213
213
|
if (char === '"' || char === "'") { quote = char; continue; }
|
|
214
|
-
if (char === '\\'
|
|
214
|
+
if (char === '\\') {
|
|
215
|
+
if (index + 1 >= command.length) return null;
|
|
216
|
+
index += 1;
|
|
217
|
+
length += 1;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
215
220
|
if (/\s/.test(char) || ';|&<>'.includes(char)) break;
|
|
216
221
|
if (char === '#' && index === targetStart) return null;
|
|
217
222
|
if ('()$`'.includes(char) || /[*?\[\]{}]/.test(char) || (char === '~' && length === 0)) return null;
|
|
@@ -311,7 +316,7 @@ function splitSimpleCommandSegments(command) {
|
|
|
311
316
|
if (command[index + 1] !== '&' || command[index + 2] === '&') return null;
|
|
312
317
|
separatorLength = 2;
|
|
313
318
|
} else if (char === '|') {
|
|
314
|
-
if (command[index + 1] === '&' || command[index + 2] === '|') return null;
|
|
319
|
+
if (command[index + 1] === '&' || (command[index + 1] === '|' && command[index + 2] === '|')) return null;
|
|
315
320
|
separatorLength = command[index + 1] === '|' ? 2 : 1;
|
|
316
321
|
}
|
|
317
322
|
if (separatorLength) {
|
|
@@ -597,6 +602,31 @@ function releaseClaim(claimed) {
|
|
|
597
602
|
try { fs.unlinkSync(claimed.file); return true; } catch (_) { return false; }
|
|
598
603
|
}
|
|
599
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
|
+
|
|
600
630
|
// Carriers release a reservation only when the returned context handoff is dropped.
|
|
601
631
|
function releaseLedgerMatch(result) {
|
|
602
632
|
const claims = result && PENDING_HANDOFFS.get(result);
|
|
@@ -610,4 +640,10 @@ function releaseLedgerMatch(result) {
|
|
|
610
640
|
return true;
|
|
611
641
|
}
|
|
612
642
|
|
|
613
|
-
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
|
+
};
|