harness-everything 1.0.1 → 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 +14 -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/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/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/plugin.json +1 -1
- package/sbom.cdx.json +7 -7
- package/scripts/errors-local.js +357 -0
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const assert = require('node:assert/strict');
|
|
5
|
+
const { execFileSync, spawnSync } = require('node:child_process');
|
|
6
|
+
const fs = require('node:fs');
|
|
7
|
+
const os = require('node:os');
|
|
8
|
+
const path = require('node:path');
|
|
9
|
+
|
|
10
|
+
const ROOT = path.resolve(__dirname, '..');
|
|
11
|
+
const CLI = path.join(ROOT, 'scripts', 'errors-local.js');
|
|
12
|
+
const ERRORS_SYNC = path.join(ROOT, 'scripts', 'errors-sync.js');
|
|
13
|
+
const SOURCE_PATH = path.join(ROOT, 'hooks', 'scripts', 'lib', 'local-error-source.js');
|
|
14
|
+
const PACKAGED_SOURCE_PATH = path.join(ROOT, 'plugins', 'harness-everything', 'hooks', 'scripts', 'lib', 'local-error-source.js');
|
|
15
|
+
const LEDGER_DIR = path.join(ROOT, '.agents', 'harness-everything', 'errors');
|
|
16
|
+
const E3_FILE = fs.readdirSync(LEDGER_DIR).find(file => /^E-0003-[a-z0-9-]+\.md$/.test(file));
|
|
17
|
+
const E3_TEXT = fs.readFileSync(path.join(LEDGER_DIR, E3_FILE), 'utf8');
|
|
18
|
+
|
|
19
|
+
const localErrors = require(path.join(ROOT, 'hooks', 'scripts', 'lib', 'local-errors.js'));
|
|
20
|
+
const { matchLedgerLessons } = require(path.join(ROOT, 'hooks', 'scripts', 'lib', 'ledger-match.js'));
|
|
21
|
+
|
|
22
|
+
let failures = 0;
|
|
23
|
+
const sandboxes = [];
|
|
24
|
+
|
|
25
|
+
function test(name, fn) {
|
|
26
|
+
try {
|
|
27
|
+
fn();
|
|
28
|
+
console.log(` PASS ${name}`);
|
|
29
|
+
} catch (error) {
|
|
30
|
+
failures += 1;
|
|
31
|
+
console.error(` FAIL ${name}\n ${error.stack.split('\n').slice(0, 5).join('\n ')}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function canonicalDir(dir) {
|
|
36
|
+
return fs.realpathSync.native ? fs.realpathSync.native(dir) : fs.realpathSync(dir);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function makeSandbox() {
|
|
40
|
+
const dir = canonicalDir(fs.mkdtempSync(path.join(os.tmpdir(), 'local-err-cli-')));
|
|
41
|
+
sandboxes.push(dir);
|
|
42
|
+
const home = path.join(dir, 'home');
|
|
43
|
+
const store = path.join(dir, 'observations');
|
|
44
|
+
const stateHome = path.join(dir, 'state-home');
|
|
45
|
+
const repoA = path.join(dir, 'repo-a');
|
|
46
|
+
const repoB = path.join(dir, 'repo-b');
|
|
47
|
+
const matcherState = path.join(dir, 'matcher-state');
|
|
48
|
+
for (const p of [home, store, stateHome, repoA, repoB, matcherState]) {
|
|
49
|
+
fs.mkdirSync(p, { recursive: true });
|
|
50
|
+
}
|
|
51
|
+
execFileSync('git', ['init', '-q', repoA]);
|
|
52
|
+
execFileSync('git', ['init', '-q', repoB]);
|
|
53
|
+
const env = {
|
|
54
|
+
...process.env,
|
|
55
|
+
HOME: home,
|
|
56
|
+
USERPROFILE: home,
|
|
57
|
+
HARNESS_OBSERVATIONS_DIR: store,
|
|
58
|
+
HARNESS_STATE_HOME: stateHome,
|
|
59
|
+
};
|
|
60
|
+
delete env.HARNESS_OBSERVATIONS;
|
|
61
|
+
delete env.HARNESS_SYSTEM_ONE_MODE;
|
|
62
|
+
delete env.HARNESS_WORKSPACE_ROOT;
|
|
63
|
+
delete env.CLAUDE_PROJECT_DIR;
|
|
64
|
+
delete env.CODEX_PROJECT_DIR;
|
|
65
|
+
return { dir, home, store, stateHome, repoA, repoB, matcherState, env };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function runCli(args, { cwd, env }) {
|
|
69
|
+
return spawnSync(process.execPath, [CLI, ...args], {
|
|
70
|
+
cwd,
|
|
71
|
+
env,
|
|
72
|
+
encoding: 'utf8',
|
|
73
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function installRealE3Ledger(repoRoot, env) {
|
|
78
|
+
const targetDir = path.join(repoRoot, '.agents', 'harness-everything', 'errors');
|
|
79
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
80
|
+
fs.writeFileSync(path.join(targetDir, E3_FILE), E3_TEXT, 'utf8');
|
|
81
|
+
const res = spawnSync(process.execPath, [ERRORS_SYNC, '--root', repoRoot], {
|
|
82
|
+
cwd: ROOT,
|
|
83
|
+
env,
|
|
84
|
+
encoding: 'utf8',
|
|
85
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
86
|
+
});
|
|
87
|
+
assert.equal(res.status, 0, `errors-sync failed: ${res.stderr}`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function loadProvisionalSource() {
|
|
91
|
+
return require(SOURCE_PATH);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
console.log('mechanism-496-local-error-cli:');
|
|
95
|
+
|
|
96
|
+
test('package.json registers errors:local script pointing to scripts/errors-local.js', () => {
|
|
97
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8'));
|
|
98
|
+
assert.equal(pkg.scripts['errors:local'], 'node scripts/errors-local.js');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('CLI add valid provisional lesson via flags and list shows it', () => {
|
|
102
|
+
const s = makeSandbox();
|
|
103
|
+
const addRes = runCli([
|
|
104
|
+
'add',
|
|
105
|
+
'--executable', 'git',
|
|
106
|
+
'--subcommand', 'commit',
|
|
107
|
+
'--lesson', 'Keep commit subjects lowercase and under 100 chars.',
|
|
108
|
+
'--cue', 'Commit rejected locally by commitlint.',
|
|
109
|
+
'--issue', 'https://github.com/dyphn1/Harness-everything/issues/496',
|
|
110
|
+
'--destination', 'harness',
|
|
111
|
+
'--json',
|
|
112
|
+
], { cwd: s.repoA, env: s.env });
|
|
113
|
+
|
|
114
|
+
assert.equal(addRes.status, 0, `expected exit 0, got ${addRes.status}: ${addRes.stderr}`);
|
|
115
|
+
const added = JSON.parse(addRes.stdout);
|
|
116
|
+
assert.equal(added.status, 'appended');
|
|
117
|
+
assert.equal(added.record.kind, 'provisional-lesson');
|
|
118
|
+
assert.equal(added.record.destination, 'harness');
|
|
119
|
+
assert.equal(added.record.issueUrl, 'https://github.com/dyphn1/Harness-everything/issues/496');
|
|
120
|
+
assert.deepEqual(added.record.match, {
|
|
121
|
+
actions: [{ tool: 'shell', executable: 'git', subcommand: 'commit' }],
|
|
122
|
+
});
|
|
123
|
+
assert.match(added.record.provisionalId, /^[a-z0-9][a-z0-9_-]{0,63}$/);
|
|
124
|
+
assert.doesNotMatch(added.record.provisionalId, /^E-/i);
|
|
125
|
+
|
|
126
|
+
const listJson = runCli(['list', '--kind', 'provisional-lesson', '--json'], { cwd: s.repoA, env: s.env });
|
|
127
|
+
assert.equal(listJson.status, 0, listJson.stderr);
|
|
128
|
+
const items = JSON.parse(listJson.stdout);
|
|
129
|
+
assert.equal(items.length, 1);
|
|
130
|
+
assert.equal(items[0].provisionalId, added.record.provisionalId);
|
|
131
|
+
assert.equal(items[0].lesson, 'Keep commit subjects lowercase and under 100 chars.');
|
|
132
|
+
|
|
133
|
+
const listText = runCli(['list'], { cwd: s.repoA, env: s.env });
|
|
134
|
+
assert.equal(listText.status, 0, listText.stderr);
|
|
135
|
+
assert.ok(listText.stdout.includes(added.record.provisionalId));
|
|
136
|
+
assert.ok(listText.stdout.includes('Keep commit subjects lowercase'));
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test('CLI add supports --diagnostic for failure rules and --match-json for full rules', () => {
|
|
140
|
+
const s = makeSandbox();
|
|
141
|
+
const diagRes = runCli([
|
|
142
|
+
'add',
|
|
143
|
+
'--executable', 'git',
|
|
144
|
+
'--subcommand', 'commit',
|
|
145
|
+
'--diagnostic', 'subject-case',
|
|
146
|
+
'--diagnostic', 'header-max-length',
|
|
147
|
+
'--lesson', 'Lowercase subject and keep header <= 100 chars.',
|
|
148
|
+
'--json',
|
|
149
|
+
], { cwd: s.repoA, env: s.env });
|
|
150
|
+
assert.equal(diagRes.status, 0, diagRes.stderr);
|
|
151
|
+
const diagRec = JSON.parse(diagRes.stdout).record;
|
|
152
|
+
assert.deepEqual(diagRec.match, {
|
|
153
|
+
failures: [{
|
|
154
|
+
tool: 'shell',
|
|
155
|
+
executable: 'git',
|
|
156
|
+
subcommand: 'commit',
|
|
157
|
+
diagnostics: ['subject-case', 'header-max-length'],
|
|
158
|
+
}],
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const matchJson = JSON.stringify({
|
|
162
|
+
actions: [{ tool: 'shell', executable: 'npm', subcommand: 'test' }],
|
|
163
|
+
});
|
|
164
|
+
const jsonRes = runCli([
|
|
165
|
+
'add',
|
|
166
|
+
'--match-json', matchJson,
|
|
167
|
+
'--lesson', 'Isolate HOME and HARNESS_OBSERVATIONS_DIR in tests.',
|
|
168
|
+
'--destination', 'project',
|
|
169
|
+
'--json',
|
|
170
|
+
], { cwd: s.repoA, env: s.env });
|
|
171
|
+
assert.equal(jsonRes.status, 0, jsonRes.stderr);
|
|
172
|
+
const jsonRec = JSON.parse(jsonRes.stdout).record;
|
|
173
|
+
assert.equal(jsonRec.destination, 'project');
|
|
174
|
+
assert.deepEqual(jsonRec.match, {
|
|
175
|
+
actions: [{ tool: 'shell', executable: 'npm', subcommand: 'test' }],
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('CLI add rejects invalid match, invalid destination, and credential URLs with exit 2 and leaves store unchanged', () => {
|
|
180
|
+
const s = makeSandbox();
|
|
181
|
+
const storeFile = localErrors.getStorePath({ env: s.env }).file;
|
|
182
|
+
|
|
183
|
+
const badCases = [
|
|
184
|
+
['add', '--match-json', '{bad json', '--lesson', 'x'],
|
|
185
|
+
['add', '--match-json', JSON.stringify({ actions: [{ tool: 'shell', executable: 'GIT', subcommand: 'commit' }] }), '--lesson', 'x'],
|
|
186
|
+
['add', '--executable', 'git', '--subcommand', 'commit', '--lesson', 'x', '--destination', 'invalid-dest'],
|
|
187
|
+
['add', '--executable', 'git', '--subcommand', 'commit', '--lesson', 'x', '--issue', 'http://github.com/org/repo/issues/1'],
|
|
188
|
+
['add', '--executable', 'git', '--subcommand', 'commit', '--lesson', 'x', '--issue', 'https://user:pass@github.com/org/repo/issues/1'],
|
|
189
|
+
['add', '--executable', 'git', '--subcommand', 'commit', '--lesson', 'x', '--issue', 'https://github.com/org/repo/issues/1?token=ghp_1234567890abcdefghijklmnopqrstuv'],
|
|
190
|
+
['add', '--executable', 'git', '--subcommand', 'commit'],
|
|
191
|
+
['add', '--lesson', 'missing match rule'],
|
|
192
|
+
['add', '--match-json', JSON.stringify({ actions: [{ tool: 'shell', executable: 'git', subcommand: 'commit' }] }), '--executable', 'git', '--subcommand', 'commit', '--lesson', 'ambiguous'],
|
|
193
|
+
];
|
|
194
|
+
|
|
195
|
+
for (const args of badCases) {
|
|
196
|
+
const res = runCli(args, { cwd: s.repoA, env: s.env });
|
|
197
|
+
assert.equal(res.status, 2, `expected exit 2 for ${JSON.stringify(args)}, got ${res.status} (${res.stdout} / ${res.stderr})`);
|
|
198
|
+
assert.equal(fs.existsSync(storeFile), false, `store must remain untouched after invalid args: ${JSON.stringify(args)}`);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test('CLI export emits redacted bounded JSONL lines with no raw output or counts', () => {
|
|
203
|
+
const s = makeSandbox();
|
|
204
|
+
const secretToken = 'ghp_1234567890abcdefghijklmnopqrstuv';
|
|
205
|
+
const rawOutput = `Error for alice@example.com in ${s.home}/secret/file.js with ${secretToken}\n${'x'.repeat(5000)}`;
|
|
206
|
+
const occ = localErrors.appendOccurrence({
|
|
207
|
+
workspaceRoot: s.repoA,
|
|
208
|
+
host: 'claude',
|
|
209
|
+
sessionKey: 'raw-session-1',
|
|
210
|
+
toolUseId: 'raw-tool-1',
|
|
211
|
+
command: `node deploy.js --token ${secretToken}`,
|
|
212
|
+
exitCode: 1,
|
|
213
|
+
output: rawOutput,
|
|
214
|
+
}, { env: s.env });
|
|
215
|
+
assert.equal(occ.status, 'appended');
|
|
216
|
+
|
|
217
|
+
const addRes = runCli([
|
|
218
|
+
'add',
|
|
219
|
+
'--executable', 'git',
|
|
220
|
+
'--subcommand', 'commit',
|
|
221
|
+
'--lesson', `Avoid leaking alice@example.com or ${s.home}/keys or ${secretToken}`,
|
|
222
|
+
'--destination', 'project',
|
|
223
|
+
], { cwd: s.repoA, env: s.env });
|
|
224
|
+
assert.equal(addRes.status, 0, addRes.stderr);
|
|
225
|
+
|
|
226
|
+
const expRes = runCli(['export'], { cwd: s.repoA, env: s.env });
|
|
227
|
+
assert.equal(expRes.status, 0, expRes.stderr);
|
|
228
|
+
const lines = expRes.stdout.trim().split('\n');
|
|
229
|
+
assert.equal(lines.length, 2);
|
|
230
|
+
|
|
231
|
+
for (const line of lines) {
|
|
232
|
+
assert.ok(!line.includes('alice@example.com'), 'email must be redacted in export');
|
|
233
|
+
assert.ok(!line.includes(s.home), 'HOME path must be redacted in export');
|
|
234
|
+
assert.ok(!line.includes(secretToken), 'token must be redacted in export');
|
|
235
|
+
const obj = JSON.parse(line);
|
|
236
|
+
assert.equal(Object.hasOwn(obj, 'output'), false, 'raw output field must not appear');
|
|
237
|
+
assert.equal(Object.hasOwn(obj, 'rawOutput'), false, 'rawOutput field must not appear');
|
|
238
|
+
assert.equal(Object.hasOwn(obj, 'command'), false, 'raw command field must not appear');
|
|
239
|
+
assert.equal(Object.hasOwn(obj, 'count'), false, 'count must not appear');
|
|
240
|
+
assert.equal(Object.hasOwn(obj, 'counts'), false, 'counts must not appear');
|
|
241
|
+
if (obj.kind === 'occurrence') {
|
|
242
|
+
assert.ok(Buffer.byteLength(obj.outputExcerpt, 'utf8') <= localErrors.LIMITS.maxExcerptBytes);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test('provisional source projects only matcher fields and delivers provisional lesson via matchLedgerLessons', () => {
|
|
248
|
+
const s = makeSandbox();
|
|
249
|
+
const { createProvisionalSource } = loadProvisionalSource();
|
|
250
|
+
|
|
251
|
+
// Add an occurrence too, to confirm occurrences are never projected as lessons.
|
|
252
|
+
localErrors.appendOccurrence({
|
|
253
|
+
workspaceRoot: s.repoA,
|
|
254
|
+
host: 'claude',
|
|
255
|
+
command: 'git commit -m "Bad"',
|
|
256
|
+
exitCode: 1,
|
|
257
|
+
output: '✖ subject must be lower-case [subject-case]',
|
|
258
|
+
}, { env: s.env });
|
|
259
|
+
|
|
260
|
+
const addRes = runCli([
|
|
261
|
+
'add',
|
|
262
|
+
'--executable', 'git',
|
|
263
|
+
'--subcommand', 'commit',
|
|
264
|
+
'--signature', 'local-commit-rule',
|
|
265
|
+
'--lesson', 'Use lowercase commit subjects in this repo.',
|
|
266
|
+
'--cue', 'Before running git commit.',
|
|
267
|
+
'--issue', 'https://github.com/dyphn1/Harness-everything/issues/496',
|
|
268
|
+
'--destination', 'project',
|
|
269
|
+
], { cwd: s.repoA, env: s.env });
|
|
270
|
+
assert.equal(addRes.status, 0, addRes.stderr);
|
|
271
|
+
|
|
272
|
+
const source = createProvisionalSource({ env: s.env, workspaceRoot: s.repoA });
|
|
273
|
+
const projected = source('workspace:a', { maxCandidates: 32, maxBytes: 65536, maxTextChars: 4096 });
|
|
274
|
+
assert.equal(projected.length, 1);
|
|
275
|
+
|
|
276
|
+
const allowedKeys = new Set(['identity', 'signature', 'promotionId', 'match', 'cue', 'lesson']);
|
|
277
|
+
for (const key of Object.keys(projected[0])) {
|
|
278
|
+
assert.ok(allowedKeys.has(key), `unexpected projected key: ${key}`);
|
|
279
|
+
}
|
|
280
|
+
assert.equal(Object.hasOwn(projected[0], 'issueUrl'), false);
|
|
281
|
+
assert.equal(Object.hasOwn(projected[0], 'outputExcerpt'), false);
|
|
282
|
+
assert.equal(Object.hasOwn(projected[0], 'count'), false);
|
|
283
|
+
assert.equal(Object.hasOwn(projected[0], 'counts'), false);
|
|
284
|
+
|
|
285
|
+
const matched = matchLedgerLessons({
|
|
286
|
+
workspaceRoot: s.repoA,
|
|
287
|
+
workspaceIdentity: 'workspace:a',
|
|
288
|
+
sessionIdentity: 'session:1',
|
|
289
|
+
stateDir: path.join(s.matcherState, 's1'),
|
|
290
|
+
event: { phase: 'action', tool: 'shell', command: 'git commit -m "test"', outcome: { failed: 'unknown' } },
|
|
291
|
+
provisionalSource: source,
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
assert.ok(matched, 'expected matchLedgerLessons to return the provisional lesson');
|
|
295
|
+
assert.ok(matched.text.includes('Provisional local lesson (unreviewed)'), 'must be labelled provisional');
|
|
296
|
+
assert.ok(matched.text.includes('Use lowercase commit subjects in this repo.'));
|
|
297
|
+
assert.ok(!matched.text.includes('https://github.com/dyphn1/Harness-everything/issues/496'), 'issueUrl must stay out of model context');
|
|
298
|
+
assert.equal(matched.ids.length, 1);
|
|
299
|
+
assert.equal(matched.ids[0], projected[0].identity);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test('repo lesson retains precedence over provisional lesson for the same rule', () => {
|
|
303
|
+
const s = makeSandbox();
|
|
304
|
+
installRealE3Ledger(s.repoA, s.env);
|
|
305
|
+
const { createProvisionalSource } = loadProvisionalSource();
|
|
306
|
+
|
|
307
|
+
// Same signature as E-0003 (commitlint-subject-case-length).
|
|
308
|
+
const addRes = runCli([
|
|
309
|
+
'add',
|
|
310
|
+
'--executable', 'git',
|
|
311
|
+
'--subcommand', 'commit',
|
|
312
|
+
'--signature', 'commitlint-subject-case-length',
|
|
313
|
+
'--lesson', 'Provisional duplicate that repo E-0003 must supersede.',
|
|
314
|
+
], { cwd: s.repoA, env: s.env });
|
|
315
|
+
assert.equal(addRes.status, 0, addRes.stderr);
|
|
316
|
+
|
|
317
|
+
const source = createProvisionalSource({ env: s.env, workspaceRoot: s.repoA });
|
|
318
|
+
const matched = matchLedgerLessons({
|
|
319
|
+
workspaceRoot: s.repoA,
|
|
320
|
+
workspaceIdentity: 'workspace:a',
|
|
321
|
+
sessionIdentity: 'session:precedence',
|
|
322
|
+
stateDir: path.join(s.matcherState, 'precedence'),
|
|
323
|
+
event: { phase: 'action', tool: 'shell', command: 'git commit -m "feat: ok"', outcome: { failed: 'unknown' } },
|
|
324
|
+
provisionalSource: source,
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
assert.ok(matched);
|
|
328
|
+
assert.deepEqual(matched.ids, ['E-0003']);
|
|
329
|
+
assert.ok(!matched.text.includes('Provisional duplicate that repo E-0003 must supersede.'));
|
|
330
|
+
assert.ok(!matched.text.includes('Provisional local lesson (unreviewed)'));
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test('workspace B cannot see workspace A lessons in CLI or provisional source', () => {
|
|
334
|
+
const s = makeSandbox();
|
|
335
|
+
const { createProvisionalSource } = loadProvisionalSource();
|
|
336
|
+
|
|
337
|
+
const addRes = runCli([
|
|
338
|
+
'add',
|
|
339
|
+
'--executable', 'git',
|
|
340
|
+
'--subcommand', 'commit',
|
|
341
|
+
'--lesson', 'Secret workspace A workaround.',
|
|
342
|
+
], { cwd: s.repoA, env: s.env });
|
|
343
|
+
assert.equal(addRes.status, 0, addRes.stderr);
|
|
344
|
+
|
|
345
|
+
const listB = runCli(['list', '--json'], { cwd: s.repoB, env: s.env });
|
|
346
|
+
assert.equal(listB.status, 0, listB.stderr);
|
|
347
|
+
assert.deepEqual(JSON.parse(listB.stdout), []);
|
|
348
|
+
|
|
349
|
+
const exportB = runCli(['export'], { cwd: s.repoB, env: s.env });
|
|
350
|
+
assert.equal(exportB.status, 0, exportB.stderr);
|
|
351
|
+
assert.equal(exportB.stdout.trim(), '');
|
|
352
|
+
|
|
353
|
+
const sourceB = createProvisionalSource({ env: s.env, workspaceRoot: s.repoB });
|
|
354
|
+
assert.deepEqual(sourceB('workspace:b', { maxCandidates: 32, maxBytes: 65536, maxTextChars: 4096 }), []);
|
|
355
|
+
|
|
356
|
+
const matchedB = matchLedgerLessons({
|
|
357
|
+
workspaceRoot: s.repoB,
|
|
358
|
+
workspaceIdentity: 'workspace:b',
|
|
359
|
+
sessionIdentity: 'session:b',
|
|
360
|
+
stateDir: path.join(s.matcherState, 'ws-b'),
|
|
361
|
+
event: { phase: 'action', tool: 'shell', command: 'git commit', outcome: { failed: 'unknown' } },
|
|
362
|
+
provisionalSource: sourceB,
|
|
363
|
+
});
|
|
364
|
+
assert.equal(matchedB, null);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
test('provisional source reflects newly added lessons on the next hook event', () => {
|
|
368
|
+
const s = makeSandbox();
|
|
369
|
+
const { createProvisionalSource } = loadProvisionalSource();
|
|
370
|
+
const source = createProvisionalSource({ env: s.env, workspaceRoot: s.repoA });
|
|
371
|
+
|
|
372
|
+
// Before CLI add: nothing matches.
|
|
373
|
+
const before = matchLedgerLessons({
|
|
374
|
+
workspaceRoot: s.repoA,
|
|
375
|
+
workspaceIdentity: 'workspace:a',
|
|
376
|
+
sessionIdentity: 'session:live',
|
|
377
|
+
stateDir: path.join(s.matcherState, 'live'),
|
|
378
|
+
event: { phase: 'action', tool: 'shell', command: 'git commit', outcome: { failed: 'unknown' } },
|
|
379
|
+
provisionalSource: source,
|
|
380
|
+
});
|
|
381
|
+
assert.equal(before, null);
|
|
382
|
+
|
|
383
|
+
// Add via CLI without recreating source.
|
|
384
|
+
const addRes = runCli([
|
|
385
|
+
'add',
|
|
386
|
+
'--executable', 'git',
|
|
387
|
+
'--subcommand', 'commit',
|
|
388
|
+
'--lesson', 'Added live between hook events.',
|
|
389
|
+
], { cwd: s.repoA, env: s.env });
|
|
390
|
+
assert.equal(addRes.status, 0, addRes.stderr);
|
|
391
|
+
|
|
392
|
+
const after = matchLedgerLessons({
|
|
393
|
+
workspaceRoot: s.repoA,
|
|
394
|
+
workspaceIdentity: 'workspace:a',
|
|
395
|
+
sessionIdentity: 'session:live',
|
|
396
|
+
stateDir: path.join(s.matcherState, 'live'),
|
|
397
|
+
event: { phase: 'action', tool: 'shell', command: 'git commit', outcome: { failed: 'unknown' } },
|
|
398
|
+
provisionalSource: source,
|
|
399
|
+
});
|
|
400
|
+
assert.ok(after);
|
|
401
|
+
assert.ok(after.text.includes('Added live between hook events.'));
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
test('unreadable or corrupt store fails open and matcher still serves repo lessons', () => {
|
|
405
|
+
const s = makeSandbox();
|
|
406
|
+
installRealE3Ledger(s.repoA, s.env);
|
|
407
|
+
const { createProvisionalSource } = loadProvisionalSource();
|
|
408
|
+
|
|
409
|
+
// Corrupt store: create a directory at the index file path so readFileSync fails cross-platform (EISDIR).
|
|
410
|
+
const storeFile = localErrors.getStorePath({ env: s.env }).file;
|
|
411
|
+
fs.mkdirSync(storeFile, { recursive: true });
|
|
412
|
+
|
|
413
|
+
const source = createProvisionalSource({ env: s.env, workspaceRoot: s.repoA });
|
|
414
|
+
assert.deepEqual(source('workspace:a', { maxCandidates: 32, maxBytes: 65536, maxTextChars: 4096 }), []);
|
|
415
|
+
|
|
416
|
+
const matched = matchLedgerLessons({
|
|
417
|
+
workspaceRoot: s.repoA,
|
|
418
|
+
workspaceIdentity: 'workspace:a',
|
|
419
|
+
sessionIdentity: 'session:failopen',
|
|
420
|
+
stateDir: path.join(s.matcherState, 'failopen'),
|
|
421
|
+
event: { phase: 'action', tool: 'shell', command: 'git commit', outcome: { failed: 'unknown' } },
|
|
422
|
+
provisionalSource: source,
|
|
423
|
+
});
|
|
424
|
+
assert.ok(matched, 'matcher must still serve repo lessons when local store is unreadable');
|
|
425
|
+
assert.deepEqual(matched.ids, ['E-0003']);
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
test('packaged local-error-source copy exists and matches canonical behavior', () => {
|
|
429
|
+
const s = makeSandbox();
|
|
430
|
+
const { createProvisionalSource } = require(PACKAGED_SOURCE_PATH);
|
|
431
|
+
|
|
432
|
+
const addRes = runCli([
|
|
433
|
+
'add',
|
|
434
|
+
'--executable', 'git',
|
|
435
|
+
'--subcommand', 'commit',
|
|
436
|
+
'--lesson', 'Packaged source check.',
|
|
437
|
+
], { cwd: s.repoA, env: s.env });
|
|
438
|
+
assert.equal(addRes.status, 0, addRes.stderr);
|
|
439
|
+
|
|
440
|
+
const source = createProvisionalSource({ env: s.env, workspaceRoot: s.repoA });
|
|
441
|
+
const projected = source('workspace:a', { maxCandidates: 32, maxBytes: 65536, maxTextChars: 4096 });
|
|
442
|
+
assert.equal(projected.length, 1);
|
|
443
|
+
assert.equal(projected[0].lesson, 'Packaged source check.');
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
test('provisional source prefers newest lessons within the 32 bound and leaves <=32 unchanged', () => {
|
|
447
|
+
const s = makeSandbox();
|
|
448
|
+
const { createProvisionalSource } = loadProvisionalSource();
|
|
449
|
+
const source = createProvisionalSource({ env: s.env, workspaceRoot: s.repoA });
|
|
450
|
+
const budget = { maxCandidates: 32, maxBytes: 65536, maxTextChars: 4096 };
|
|
451
|
+
const baseMs = Date.parse('2026-11-24T00:00:00.000Z');
|
|
452
|
+
|
|
453
|
+
function addLesson(n) {
|
|
454
|
+
const pad = String(n).padStart(2, '0');
|
|
455
|
+
const res = localErrors.addProvisionalLesson({
|
|
456
|
+
workspaceRoot: s.repoA,
|
|
457
|
+
provisionalId: `lesson-${pad}`,
|
|
458
|
+
signature: `sig-${pad}`,
|
|
459
|
+
destination: 'project',
|
|
460
|
+
match: { actions: [{ tool: 'shell', executable: 'git', subcommand: `cmd-${pad}` }] },
|
|
461
|
+
lesson: `Provisional lesson number ${pad}.`,
|
|
462
|
+
}, { env: s.env, now: baseMs + n * 1000 });
|
|
463
|
+
assert.equal(res.status, 'appended');
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
for (let i = 1; i <= 32; i += 1) {
|
|
467
|
+
addLesson(i);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// <=32 unchanged: all 32 lessons remain projected in order, oldest and 32nd both match.
|
|
471
|
+
const at32 = source('workspace:a', budget);
|
|
472
|
+
assert.equal(at32.length, 32);
|
|
473
|
+
assert.deepEqual(
|
|
474
|
+
at32.map(c => c.identity),
|
|
475
|
+
Array.from({ length: 32 }, (_, idx) => `lesson-${String(idx + 1).padStart(2, '0')}`)
|
|
476
|
+
);
|
|
477
|
+
|
|
478
|
+
// Add 33rd lesson: bound stays 32, oldest (lesson-01) is dropped, newest (lesson-33) wins.
|
|
479
|
+
addLesson(33);
|
|
480
|
+
const at33 = source('workspace:a', budget);
|
|
481
|
+
assert.equal(at33.length, 32, 'candidate bound must remain 32');
|
|
482
|
+
assert.equal(at33.some(c => c.identity === 'lesson-01'), false, 'oldest lesson must be dropped');
|
|
483
|
+
assert.equal(at33.some(c => c.identity === 'lesson-02'), true, 'second oldest lesson must remain');
|
|
484
|
+
assert.equal(at33.some(c => c.identity === 'lesson-33'), true, 'newest 33rd lesson must be projected');
|
|
485
|
+
|
|
486
|
+
const matched33 = matchLedgerLessons({
|
|
487
|
+
workspaceRoot: s.repoA,
|
|
488
|
+
workspaceIdentity: 'workspace:a',
|
|
489
|
+
sessionIdentity: 'session:newest-33',
|
|
490
|
+
stateDir: path.join(s.matcherState, 'newest-33'),
|
|
491
|
+
event: { phase: 'action', tool: 'shell', command: 'git cmd-33', outcome: { failed: 'unknown' } },
|
|
492
|
+
provisionalSource: source,
|
|
493
|
+
});
|
|
494
|
+
assert.ok(matched33, 'expected matchLedgerLessons to deliver the 33rd (newest) lesson');
|
|
495
|
+
assert.deepEqual(matched33.ids, ['lesson-33']);
|
|
496
|
+
assert.ok(matched33.text.includes('Provisional lesson number 33.'));
|
|
497
|
+
|
|
498
|
+
const matched01 = matchLedgerLessons({
|
|
499
|
+
workspaceRoot: s.repoA,
|
|
500
|
+
workspaceIdentity: 'workspace:a',
|
|
501
|
+
sessionIdentity: 'session:oldest-01',
|
|
502
|
+
stateDir: path.join(s.matcherState, 'oldest-01'),
|
|
503
|
+
event: { phase: 'action', tool: 'shell', command: 'git cmd-01', outcome: { failed: 'unknown' } },
|
|
504
|
+
provisionalSource: source,
|
|
505
|
+
});
|
|
506
|
+
assert.equal(matched01, null, 'dropped oldest lesson must not be delivered');
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
for (const dir of sandboxes) {
|
|
510
|
+
try { fs.rmSync(dir, { recursive: true, force: true }); } catch (_) {}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (failures > 0) {
|
|
514
|
+
console.error(`\n${failures} test(s) failed`);
|
|
515
|
+
process.exit(1);
|
|
516
|
+
}
|
|
517
|
+
console.log('\nall tests passed');
|