harness-everything 0.56.1 → 0.57.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/claude-2026-11-21-ledger-lesson/README.md +33 -0
- package/benchmarks/results/live-host/claude-2026-11-21-ledger-lesson/trace.jsonl +13 -0
- package/ci/fixtures/394-claude-ledger-lesson/post-tool-use-failure.json +13 -0
- package/ci/fixtures/394-claude-ledger-lesson/pre-tool-use.json +10 -0
- package/ci/fixtures/394-codex-ledger-lesson/post-tool-use-failure.json +17 -0
- package/ci/fixtures/394-codex-ledger-lesson/post-tool-use-success.json +17 -0
- package/ci/fixtures/394-codex-ledger-lesson/post-tool-use-unknown.json +15 -0
- package/ci/fixtures/394-codex-ledger-lesson/pre-tool-use.json +12 -0
- package/ci/fixtures/394-ledger-match/commitlint-empty-subject.txt +5 -0
- package/ci/fixtures/394-ledger-match/commitlint-subject-case.txt +5 -0
- package/ci/fixtures/394-ledger-match/provisional-candidates.json +20 -0
- package/ci/fixtures/394-ledger-match/quoted-match-record.md +25 -0
- package/ci/mechanism-394-claude-ledger-lesson.test.js +176 -0
- package/ci/mechanism-394-codex-ledger-lesson.test.js +203 -0
- package/ci/mechanism-394-ledger-match.test.js +521 -0
- package/ci/mechanism-394-opencode-ledger-lesson.test.js +232 -0
- package/ci/mechanism-errors-sync-root.test.js +86 -4
- package/docs/adr/ROUTE-001-error-lesson-delivery.md +52 -49
- package/docs/governed-index.md +1 -1
- package/docs/platform-capabilities.md +4 -0
- package/hooks/hooks.json +22 -0
- package/hooks/scripts/codex-ledger-lesson.js +82 -0
- package/hooks/scripts/ledger-lesson.js +94 -0
- package/hooks/scripts/lib/ledger-match.js +452 -0
- package/opencode-plugin/index.mjs +113 -4
- package/opencode-plugin/plugin.json +1 -1
- package/package.json +1 -1
- package/plugins/harness-everything/.codex-plugin/plugin.json +1 -1
- package/plugins/harness-everything/hooks/hooks.json +24 -0
- package/plugins/harness-everything/hooks/scripts/codex-ledger-lesson.js +82 -0
- package/plugins/harness-everything/hooks/scripts/ledger-lesson.js +94 -0
- package/plugins/harness-everything/hooks/scripts/lib/ledger-match.js +452 -0
- package/plugins/harness-everything/plugin.json +1 -1
- package/sbom.cdx.json +7 -7
- package/scripts/errors-sync.js +63 -2
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const assert = require('node:assert/strict');
|
|
5
|
+
const { spawn, spawnSync } = require('node:child_process');
|
|
6
|
+
const fs = require('node:fs');
|
|
7
|
+
const os = require('node:os');
|
|
8
|
+
const path = require('node:path');
|
|
9
|
+
const test = require('node:test');
|
|
10
|
+
|
|
11
|
+
let matcher;
|
|
12
|
+
try {
|
|
13
|
+
matcher = require('../hooks/scripts/lib/ledger-match.js');
|
|
14
|
+
} catch (error) {
|
|
15
|
+
if (error.code !== 'MODULE_NOT_FOUND') throw error;
|
|
16
|
+
}
|
|
17
|
+
const packagedMatcher = require('../plugins/harness-everything/hooks/scripts/lib/ledger-match.js');
|
|
18
|
+
|
|
19
|
+
const ROOT = path.resolve(__dirname, '..');
|
|
20
|
+
const ERRORS_SYNC = path.join(ROOT, 'scripts', 'errors-sync.js');
|
|
21
|
+
const LEDGER_DIR = path.join(ROOT, '.agents', 'harness-everything', 'errors');
|
|
22
|
+
const E3_FILE = fs.readdirSync(LEDGER_DIR).find(file => /^E-0003-[a-z0-9-]+\.md$/.test(file));
|
|
23
|
+
const E3_TEXT = fs.readFileSync(path.join(LEDGER_DIR, E3_FILE), 'utf8');
|
|
24
|
+
const COMMITLINT_OUTPUT = fs.readFileSync(path.join(__dirname, 'fixtures/394-ledger-match/commitlint-subject-case.txt'), 'utf8');
|
|
25
|
+
const COMMITLINT_EMPTY_SUBJECT_OUTPUT = fs.readFileSync(
|
|
26
|
+
path.join(__dirname, 'fixtures/394-ledger-match/commitlint-empty-subject.txt'), 'utf8');
|
|
27
|
+
const QUOTED_RECORD = fs.readFileSync(path.join(__dirname, 'fixtures/394-ledger-match/quoted-match-record.md'), 'utf8');
|
|
28
|
+
const PROVISIONAL_FIXTURE = JSON.parse(fs.readFileSync(
|
|
29
|
+
path.join(__dirname, 'fixtures/394-ledger-match/provisional-candidates.json'), 'utf8'));
|
|
30
|
+
const TMP = fs.realpathSync.native(fs.mkdtempSync(path.join(os.tmpdir(), '394 ledger match ')));
|
|
31
|
+
let serial = 0;
|
|
32
|
+
const GENERATED_E3_ROOT = path.join(TMP, 'generated-e3');
|
|
33
|
+
write(path.join(GENERATED_E3_ROOT, '.agents', 'harness-everything', 'errors', E3_FILE), E3_TEXT);
|
|
34
|
+
const E3_GENERATION = spawnSync(process.execPath, [ERRORS_SYNC, '--root', GENERATED_E3_ROOT], {
|
|
35
|
+
cwd: ROOT,
|
|
36
|
+
env: Object.fromEntries(Object.entries(process.env).filter(([key]) => key !== 'HARNESS_SYSTEM_ONE_MODE')),
|
|
37
|
+
encoding: 'utf8'
|
|
38
|
+
});
|
|
39
|
+
if (E3_GENERATION.status !== 0) throw new Error(E3_GENERATION.stderr);
|
|
40
|
+
const REAL_E3 = JSON.parse(fs.readFileSync(
|
|
41
|
+
path.join(GENERATED_E3_ROOT, '.agents', 'harness-everything', 'errors', 'index.jsonl'), 'utf8').trim());
|
|
42
|
+
|
|
43
|
+
test.after(() => fs.rmSync(TMP, { recursive: true, force: true }));
|
|
44
|
+
|
|
45
|
+
function select(input) {
|
|
46
|
+
assert.equal(typeof matcher?.matchLedgerLessons, 'function',
|
|
47
|
+
'the shared matchLedgerLessons(input) acceptance API exists');
|
|
48
|
+
return matcher.matchLedgerLessons(input);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function workspace(name) {
|
|
52
|
+
const root = fs.realpathSync.native(fs.mkdtempSync(path.join(TMP, `${name} `)));
|
|
53
|
+
return root;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function stateDir(name = 'state') {
|
|
57
|
+
return path.join(TMP, `${name}-${++serial}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function write(file, value) {
|
|
61
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
62
|
+
fs.writeFileSync(file, value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function repoWorkspace(name, { indexText, recordText = E3_TEXT, recordFile = REAL_E3.file, missingRecord = false } = {}) {
|
|
66
|
+
const root = workspace(name);
|
|
67
|
+
const ledger = path.join(root, '.agents', 'harness-everything', 'errors');
|
|
68
|
+
const row = { ...REAL_E3, file: recordFile };
|
|
69
|
+
if (!missingRecord) write(path.join(ledger, recordFile.split('/').at(-1)), recordText);
|
|
70
|
+
write(path.join(ledger, 'index.jsonl'), indexText ?? `${JSON.stringify(row)}\n`);
|
|
71
|
+
return root;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function localSource(candidates) {
|
|
75
|
+
return (identity, budget) => {
|
|
76
|
+
assert.equal(identity, 'workspace:fixture');
|
|
77
|
+
assert.equal(typeof budget.maxCandidates, 'number');
|
|
78
|
+
assert.equal(typeof budget.maxBytes, 'number');
|
|
79
|
+
return candidates;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function input(overrides = {}) {
|
|
84
|
+
return {
|
|
85
|
+
workspaceRoot: GENERATED_E3_ROOT,
|
|
86
|
+
workspaceIdentity: 'workspace:fixture',
|
|
87
|
+
sessionIdentity: 'session:one',
|
|
88
|
+
stateDir: stateDir('session-state'),
|
|
89
|
+
event: {
|
|
90
|
+
phase: 'action',
|
|
91
|
+
tool: 'shell',
|
|
92
|
+
command: 'git commit',
|
|
93
|
+
outcome: { failed: 'unknown' }
|
|
94
|
+
},
|
|
95
|
+
...overrides
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function fixtureCandidate(overrides = {}) {
|
|
100
|
+
return {
|
|
101
|
+
...PROVISIONAL_FIXTURE[0],
|
|
102
|
+
...overrides
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
test('exports the conservative git commit executable/subcommand normalizer', () => {
|
|
107
|
+
assert.equal(typeof matcher?.normalizeGitCommitCommand, 'function',
|
|
108
|
+
'the shared normalizeGitCommitCommand(command) export exists');
|
|
109
|
+
assert.deepEqual(matcher.normalizeGitCommitCommand('git commit'), {
|
|
110
|
+
tool: 'shell', executable: 'git', subcommand: 'commit'
|
|
111
|
+
});
|
|
112
|
+
assert.deepEqual(matcher.normalizeGitCommitCommand('git commit -m "message"'), {
|
|
113
|
+
tool: 'shell', executable: 'git', subcommand: 'commit'
|
|
114
|
+
});
|
|
115
|
+
assert.deepEqual(matcher.normalizeGitCommitCommand('/usr/bin/git commit --amend'), {
|
|
116
|
+
tool: 'shell', executable: 'git', subcommand: 'commit'
|
|
117
|
+
});
|
|
118
|
+
for (const command of [
|
|
119
|
+
'echo git commit',
|
|
120
|
+
'echo "git commit"',
|
|
121
|
+
"printf '%s' 'git commit'",
|
|
122
|
+
'git status',
|
|
123
|
+
'git merge origin/main',
|
|
124
|
+
'sudo git commit',
|
|
125
|
+
'git commit && echo done',
|
|
126
|
+
'"git commit"'
|
|
127
|
+
]) assert.equal(matcher.normalizeGitCommitCommand(command), null, command);
|
|
128
|
+
assert.deepEqual(matcher.normalizeGitCommitCommand("git commit -m 'message; with separator'"), {
|
|
129
|
+
tool: 'shell', executable: 'git', subcommand: 'commit'
|
|
130
|
+
});
|
|
131
|
+
for (const command of [
|
|
132
|
+
'git -C x commit',
|
|
133
|
+
'git -c user.name=Harness commit',
|
|
134
|
+
'git -c core.pager= commit',
|
|
135
|
+
'git --git-dir=/repo/.git commit',
|
|
136
|
+
'git --work-tree=/repo commit',
|
|
137
|
+
'git --no-pager commit',
|
|
138
|
+
'git -P commit',
|
|
139
|
+
'git -C x -c user.name=Harness --git-dir=/repo/.git --work-tree=/repo --no-pager -P commit'
|
|
140
|
+
]) assert.deepEqual(matcher.normalizeGitCommitCommand(command), {
|
|
141
|
+
tool: 'shell', executable: 'git', subcommand: 'commit'
|
|
142
|
+
}, command);
|
|
143
|
+
for (const command of [
|
|
144
|
+
'git -C',
|
|
145
|
+
'git -C x',
|
|
146
|
+
'git -c',
|
|
147
|
+
'git -c user.name commit',
|
|
148
|
+
'git -c =x commit',
|
|
149
|
+
'git -c = commit',
|
|
150
|
+
'git -c =Harness commit',
|
|
151
|
+
'git --git-dir= commit',
|
|
152
|
+
'git --work-tree= commit',
|
|
153
|
+
'git --git-dir /repo/.git commit',
|
|
154
|
+
'git --other=option commit',
|
|
155
|
+
'git -C x status'
|
|
156
|
+
]) assert.equal(matcher.normalizeGitCommitCommand(command), null, command);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('source and packaged matchers accept an empty Git config value', () => {
|
|
160
|
+
const expected = { tool: 'shell', executable: 'git', subcommand: 'commit' };
|
|
161
|
+
for (const implementation of [matcher, packagedMatcher]) {
|
|
162
|
+
assert.deepEqual(implementation.normalizeGitCommitCommand('git -c core.pager= commit'), expected);
|
|
163
|
+
assert.equal(implementation.normalizeGitCommitCommand('git -c =x commit'), null);
|
|
164
|
+
assert.equal(implementation.normalizeGitCommitCommand('git -c = commit'), null);
|
|
165
|
+
assert.equal(implementation.normalizeGitCommitCommand('git -c user.name commit'), null);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('a direct git commit selects the real E-0003 record at action', () => {
|
|
170
|
+
const result = select(input({ event: { phase: 'action', tool: 'shell', command: 'git commit', outcome: { failed: 'unknown' } } }));
|
|
171
|
+
assert.deepEqual(result.ids, ['E-0003']);
|
|
172
|
+
assert.ok(result.text.includes('A commit is rejected locally by commitlint'));
|
|
173
|
+
assert.ok(result.text.includes('Check the subject line against both rules'));
|
|
174
|
+
assert.ok(result.text.endsWith('Consider a fresh look, and zoom out if warranted.'));
|
|
175
|
+
assert.ok(!result.text.includes('commitlint.config.js'));
|
|
176
|
+
assert.ok(!result.text.includes('docs(errors): Record PR stuff'));
|
|
177
|
+
|
|
178
|
+
const withArgs = select(input({
|
|
179
|
+
stateDir: stateDir('action-args'),
|
|
180
|
+
event: { phase: 'action', tool: 'shell', command: 'git commit --amend', outcome: { failed: 'unknown' } }
|
|
181
|
+
}));
|
|
182
|
+
assert.deepEqual(withArgs.ids, ['E-0003']);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('a failed commit with captured commitlint output selects again at failure', () => {
|
|
186
|
+
const base = input();
|
|
187
|
+
const action = select(base);
|
|
188
|
+
const failure = select({
|
|
189
|
+
...base,
|
|
190
|
+
event: { phase: 'failure', tool: 'shell', command: 'git commit -m "Bad Subject"', outcome: { failed: true, output: COMMITLINT_OUTPUT } }
|
|
191
|
+
});
|
|
192
|
+
assert.deepEqual(action.ids, ['E-0003']);
|
|
193
|
+
assert.deepEqual(failure.ids, ['E-0003']);
|
|
194
|
+
const emptySubject = select(input({
|
|
195
|
+
event: { phase: 'failure', tool: 'shell', command: 'git commit', outcome: { failed: true, output: COMMITLINT_EMPTY_SUBJECT_OUTPUT } }
|
|
196
|
+
}));
|
|
197
|
+
assert.deepEqual(emptySubject.ids, ['E-0003']);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('the real generator and runtime accept quoted identity and match scalars', () => {
|
|
201
|
+
const root = workspace('generated quoted record');
|
|
202
|
+
const ledger = path.join(root, '.agents', 'harness-everything', 'errors');
|
|
203
|
+
const recordFile = 'E-0999-quoted-match-record.md';
|
|
204
|
+
write(path.join(ledger, recordFile), QUOTED_RECORD);
|
|
205
|
+
const env = { ...process.env };
|
|
206
|
+
delete env.HARNESS_SYSTEM_ONE_MODE;
|
|
207
|
+
const generated = spawnSync(process.execPath, [ERRORS_SYNC, '--root', root], {
|
|
208
|
+
cwd: ROOT,
|
|
209
|
+
env,
|
|
210
|
+
encoding: 'utf8'
|
|
211
|
+
});
|
|
212
|
+
assert.equal(generated.status, 0, generated.stderr);
|
|
213
|
+
const row = JSON.parse(fs.readFileSync(path.join(ledger, 'index.jsonl'), 'utf8').trim());
|
|
214
|
+
const state = stateDir('generated-quoted');
|
|
215
|
+
const action = select(input({
|
|
216
|
+
workspaceRoot: root,
|
|
217
|
+
stateDir: state,
|
|
218
|
+
event: { phase: 'action', tool: 'shell', command: 'git commit', outcome: { failed: 'unknown' } }
|
|
219
|
+
}));
|
|
220
|
+
assert.deepEqual(action?.ids, ['E-0999'], 'the generated quoted rule selects its real Cue/Lesson at action');
|
|
221
|
+
assert.ok(action.text.includes('A generated quoted rule is still an ordinary Git commit.'));
|
|
222
|
+
assert.ok(action.text.includes('Review the commit message before retrying.'));
|
|
223
|
+
assert.match(row.matchBinding, /^[0-9a-f]{64}$/, 'the generator binds every match-bearing row');
|
|
224
|
+
|
|
225
|
+
const failure = select(input({
|
|
226
|
+
workspaceRoot: root,
|
|
227
|
+
stateDir: state,
|
|
228
|
+
event: {
|
|
229
|
+
phase: 'failure',
|
|
230
|
+
tool: 'shell',
|
|
231
|
+
command: 'git commit',
|
|
232
|
+
outcome: { failed: true, output: COMMITLINT_OUTPUT }
|
|
233
|
+
}
|
|
234
|
+
}));
|
|
235
|
+
assert.deepEqual(failure?.ids, ['E-0999'], 'the same generated rule selects at failure');
|
|
236
|
+
|
|
237
|
+
const recordPath = path.join(ledger, recordFile);
|
|
238
|
+
fs.writeFileSync(recordPath, QUOTED_RECORD.replace('status: "lesson"', 'status: "open"'));
|
|
239
|
+
assert.equal(select(input({ workspaceRoot: root, stateDir: stateDir('stale-frontmatter') })), null,
|
|
240
|
+
'a frontmatter change makes its generated binding stale');
|
|
241
|
+
fs.writeFileSync(recordPath, QUOTED_RECORD.replace('id: "E-0999"', 'id: "E-1000"'));
|
|
242
|
+
assert.equal(select(input({ workspaceRoot: root, stateDir: stateDir('wrong-record-id') })), null,
|
|
243
|
+
'a record identity change makes its generated binding stale');
|
|
244
|
+
fs.writeFileSync(recordPath, QUOTED_RECORD);
|
|
245
|
+
const indexPath = path.join(ledger, 'index.jsonl');
|
|
246
|
+
const tamperedRow = { ...row, signature: 'tampered-signature' };
|
|
247
|
+
fs.writeFileSync(indexPath, JSON.stringify(tamperedRow) + '\n');
|
|
248
|
+
assert.equal(select(input({ workspaceRoot: root, stateDir: stateDir('tampered-row') })), null,
|
|
249
|
+
'an independently edited row fails open');
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test('git -C commit reaches E-0003 at action and captured failure', () => {
|
|
253
|
+
const command = 'git -C x commit';
|
|
254
|
+
const state = stateDir('git-C-action-failure');
|
|
255
|
+
const action = select(input({
|
|
256
|
+
stateDir: state,
|
|
257
|
+
event: { phase: 'action', tool: 'shell', command, outcome: { failed: 'unknown' } }
|
|
258
|
+
}));
|
|
259
|
+
assert.deepEqual(action?.ids, ['E-0003'], 'git -C x commit selects at action');
|
|
260
|
+
const failure = select(input({
|
|
261
|
+
stateDir: state,
|
|
262
|
+
event: { phase: 'failure', tool: 'shell', command, outcome: { failed: true, output: COMMITLINT_OUTPUT } }
|
|
263
|
+
}));
|
|
264
|
+
assert.deepEqual(failure?.ids, ['E-0003'], 'git -C x commit selects at failure with captured commitlint output');
|
|
265
|
+
assert.equal(action.text, failure.text, 'action and failure carry the same reminder under separate phase keys');
|
|
266
|
+
|
|
267
|
+
for (const prefix of [
|
|
268
|
+
'-c user.name=Harness',
|
|
269
|
+
'--no-pager',
|
|
270
|
+
'-P',
|
|
271
|
+
'-C x -c user.name=Harness --git-dir=/repo/.git --work-tree=/repo --no-pager -P'
|
|
272
|
+
]) {
|
|
273
|
+
assert.deepEqual(matcher.normalizeGitCommitCommand('git ' + prefix + ' commit'), {
|
|
274
|
+
tool: 'shell', executable: 'git', subcommand: 'commit'
|
|
275
|
+
}, prefix);
|
|
276
|
+
}
|
|
277
|
+
const combined = select(input({
|
|
278
|
+
stateDir: stateDir('combined-global-options'),
|
|
279
|
+
event: {
|
|
280
|
+
phase: 'action',
|
|
281
|
+
tool: 'shell',
|
|
282
|
+
command: 'git -C x -c user.name=Harness --git-dir=/repo/.git --work-tree=/repo --no-pager -P commit',
|
|
283
|
+
outcome: { failed: 'unknown' }
|
|
284
|
+
}
|
|
285
|
+
}));
|
|
286
|
+
assert.deepEqual(combined?.ids, ['E-0003'], 'combined Git globals do not change the bound workspace ledger');
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test('quoted mentions, unrelated commands and unqualified failures stay silent', () => {
|
|
290
|
+
const controls = [
|
|
291
|
+
{ phase: 'action', command: 'echo "git commit"', failed: true, output: COMMITLINT_OUTPUT },
|
|
292
|
+
{ phase: 'action', command: "printf '%s' 'git commit'", failed: true, output: COMMITLINT_OUTPUT },
|
|
293
|
+
{ phase: 'action', command: 'git status', failed: true, output: COMMITLINT_OUTPUT },
|
|
294
|
+
{ phase: 'action', command: 'git merge origin/main', failed: true, output: COMMITLINT_OUTPUT },
|
|
295
|
+
{ phase: 'failure', command: 'git commit', failed: false, output: COMMITLINT_OUTPUT },
|
|
296
|
+
{ phase: 'failure', command: 'git commit', failed: 'unknown', output: COMMITLINT_OUTPUT },
|
|
297
|
+
{ phase: 'failure', command: 'git commit', failed: true, output: 'subject-case appears in an ordinary warning' },
|
|
298
|
+
{ phase: 'failure', command: 'git commit', failed: true, output: '⚠ subject must be lower-case [subject-case]' },
|
|
299
|
+
{ phase: 'failure', command: 'echo "git commit"', failed: true, output: COMMITLINT_OUTPUT },
|
|
300
|
+
{ phase: 'failure', command: 'git status', failed: true, output: COMMITLINT_OUTPUT }
|
|
301
|
+
];
|
|
302
|
+
for (const control of controls) {
|
|
303
|
+
const { phase, command, failed, output } = control;
|
|
304
|
+
assert.equal(select(input({ event: { phase, tool: 'shell', command, outcome: { failed, output } } })), null, command);
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test('dedupe is keyed by workspace, session, lesson and phase', () => {
|
|
309
|
+
const base = input();
|
|
310
|
+
assert.deepEqual(select(base).ids, ['E-0003']);
|
|
311
|
+
assert.equal(select(base), null, 'same phase repeats silently');
|
|
312
|
+
assert.deepEqual(select({ ...base, sessionIdentity: 'session:two' }).ids, ['E-0003']);
|
|
313
|
+
assert.deepEqual(select({
|
|
314
|
+
...base,
|
|
315
|
+
workspaceRoot: repoWorkspace('second workspace'),
|
|
316
|
+
workspaceIdentity: 'workspace:other'
|
|
317
|
+
}).ids, ['E-0003']);
|
|
318
|
+
assert.deepEqual(select({
|
|
319
|
+
...base,
|
|
320
|
+
event: { phase: 'failure', tool: 'shell', command: 'git commit', outcome: { failed: true, output: COMMITLINT_OUTPUT } }
|
|
321
|
+
}).ids, ['E-0003']);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
test('concurrent processes atomically emit one copy of a phase key', async () => {
|
|
325
|
+
assert.equal(typeof matcher?.matchLedgerLessons, 'function', 'concurrent acceptance requires the shared matcher');
|
|
326
|
+
const shared = input({ stateDir: stateDir('concurrent-state') });
|
|
327
|
+
const script = [
|
|
328
|
+
"const { matchLedgerLessons } = require('./hooks/scripts/lib/ledger-match.js');",
|
|
329
|
+
'process.stdout.write(JSON.stringify(matchLedgerLessons(JSON.parse(process.env.INPUT))));'
|
|
330
|
+
].join('\n');
|
|
331
|
+
const run = () => new Promise((resolve, reject) => {
|
|
332
|
+
const env = { ...process.env, INPUT: JSON.stringify(shared) };
|
|
333
|
+
delete env.HARNESS_SYSTEM_ONE_MODE;
|
|
334
|
+
const child = spawn(process.execPath, ['-e', script], {
|
|
335
|
+
cwd: ROOT,
|
|
336
|
+
env,
|
|
337
|
+
stdio: ['ignore', 'pipe', 'pipe']
|
|
338
|
+
});
|
|
339
|
+
let stdout = '';
|
|
340
|
+
let stderr = '';
|
|
341
|
+
child.stdout.setEncoding('utf8').on('data', chunk => { stdout += chunk; });
|
|
342
|
+
child.stderr.setEncoding('utf8').on('data', chunk => { stderr += chunk; });
|
|
343
|
+
child.once('error', reject);
|
|
344
|
+
child.once('close', code => code === 0 ? resolve(JSON.parse(stdout)) : reject(new Error(stderr)));
|
|
345
|
+
});
|
|
346
|
+
const results = await Promise.all([run(), run()]);
|
|
347
|
+
assert.equal(results.filter(Boolean).length, 1);
|
|
348
|
+
assert.deepEqual(results.filter(Boolean)[0].ids, ['E-0003']);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test('a dropped pending handoff can release its atomic reservation', () => {
|
|
352
|
+
assert.equal(typeof matcher?.releaseLedgerMatch, 'function', 'carriers can release a dropped handoff');
|
|
353
|
+
const base = input();
|
|
354
|
+
const pending = select(base);
|
|
355
|
+
assert.equal(matcher.releaseLedgerMatch(pending), true);
|
|
356
|
+
assert.deepEqual(select(base).ids, ['E-0003']);
|
|
357
|
+
assert.equal(matcher.releaseLedgerMatch(pending), false, 'a reservation can be released only once');
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
test('failed reservation cleanup remains visible and can be retried', () => {
|
|
361
|
+
const base = input();
|
|
362
|
+
const pending = select(base);
|
|
363
|
+
const unlink = fs.unlinkSync;
|
|
364
|
+
fs.unlinkSync = () => { throw Object.assign(new Error('state directory is temporarily unavailable'), { code: 'EACCES' }); };
|
|
365
|
+
try {
|
|
366
|
+
assert.equal(matcher.releaseLedgerMatch(pending), false);
|
|
367
|
+
} finally {
|
|
368
|
+
fs.unlinkSync = unlink;
|
|
369
|
+
}
|
|
370
|
+
assert.equal(select(base), null, 'the unremoved reservation still dedupes');
|
|
371
|
+
assert.equal(matcher.releaseLedgerMatch(pending), true, 'the carrier can retry cleanup');
|
|
372
|
+
assert.deepEqual(select(base).ids, ['E-0003']);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test('provisional source is bounded, workspace-scoped and works without a repo index', () => {
|
|
376
|
+
const root = workspace('local only');
|
|
377
|
+
let seenBudget;
|
|
378
|
+
const result = select(input({
|
|
379
|
+
workspaceRoot: root,
|
|
380
|
+
provisionalSource: (identity, budget) => {
|
|
381
|
+
assert.equal(identity, 'workspace:fixture');
|
|
382
|
+
seenBudget = budget;
|
|
383
|
+
return [PROVISIONAL_FIXTURE[0]];
|
|
384
|
+
}
|
|
385
|
+
}));
|
|
386
|
+
assert.deepEqual(result.ids, ['local-commit-a']);
|
|
387
|
+
assert.ok(result.text.includes('Provisional local lesson (unreviewed)'));
|
|
388
|
+
assert.ok(result.text.startsWith('Provisional local lesson (unreviewed)\n'));
|
|
389
|
+
assert.ok(result.text.includes('Check the proposed message before retrying.'));
|
|
390
|
+
assert.ok(!result.text.includes('A local note about a commit.'), 'cue is matching metadata, not reminder text');
|
|
391
|
+
assert.deepEqual(seenBudget, { maxCandidates: 32, maxBytes: 65536, maxTextChars: 4096 });
|
|
392
|
+
assert.equal(result.text.split('Consider a fresh look, and zoom out if warranted.').length - 1, 1);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
test('repo candidates precede provisional candidates and canonical duplicates collapse', () => {
|
|
396
|
+
const duplicate = fixtureCandidate({
|
|
397
|
+
identity: 'local-copy-of-e3',
|
|
398
|
+
signature: REAL_E3.signature,
|
|
399
|
+
promotionId: REAL_E3.id,
|
|
400
|
+
lesson: 'LOCAL_DUPLICATE_SENTINEL'
|
|
401
|
+
});
|
|
402
|
+
const distinct = fixtureCandidate({ identity: 'local-distinct', signature: 'local-distinct', lesson: 'Distinct local lesson.' });
|
|
403
|
+
const base = input({ provisionalSource: localSource([distinct, duplicate]) });
|
|
404
|
+
const result = select(base);
|
|
405
|
+
assert.deepEqual(result.ids, ['E-0003', 'local-distinct']);
|
|
406
|
+
assert.ok(Array.from(result.text).length <= 1500);
|
|
407
|
+
assert.ok(result.text.includes('Check the subject line against both rules'));
|
|
408
|
+
assert.ok(result.text.includes('Distinct local lesson.'));
|
|
409
|
+
assert.ok(!result.text.includes('LOCAL_DUPLICATE_SENTINEL'));
|
|
410
|
+
assert.equal(select({ ...base, provisionalSource: localSource([duplicate]) }), null,
|
|
411
|
+
'an exhausted repo identity does not make its local duplicate eligible');
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
test('stable ordering, two-lesson cap and whole-record character cap', () => {
|
|
415
|
+
const candidates = [
|
|
416
|
+
fixtureCandidate({ identity: 'local-z', signature: 'z-local', lesson: 'z lesson' }),
|
|
417
|
+
fixtureCandidate({ identity: 'local-a', signature: 'a-local', lesson: 'a lesson' }),
|
|
418
|
+
fixtureCandidate({ identity: 'local-b', signature: 'b-local', lesson: 'b lesson' })
|
|
419
|
+
];
|
|
420
|
+
const first = select(input({ workspaceRoot: workspace('stable a'), provisionalSource: localSource(candidates) }));
|
|
421
|
+
const second = select(input({ workspaceRoot: workspace('stable b'), provisionalSource: localSource([...candidates].reverse()) }));
|
|
422
|
+
assert.deepEqual(first.ids, ['local-a', 'local-b']);
|
|
423
|
+
assert.deepEqual(second.ids, first.ids, 'source ordering does not change selection');
|
|
424
|
+
assert.ok(Array.from(first.text).length <= 1500);
|
|
425
|
+
|
|
426
|
+
const capped = select(input({
|
|
427
|
+
workspaceRoot: workspace('cap'),
|
|
428
|
+
provisionalSource: localSource([
|
|
429
|
+
fixtureCandidate({ identity: 'local-too-large', signature: 'a-large', lesson: 'X'.repeat(1500) }),
|
|
430
|
+
fixtureCandidate({ identity: 'local-fits', signature: 'b-small', lesson: 'Small complete lesson.' })
|
|
431
|
+
])
|
|
432
|
+
}));
|
|
433
|
+
assert.deepEqual(capped.ids, ['local-fits']);
|
|
434
|
+
assert.ok(!capped.text.includes('X'.repeat(100)), 'an oversized excerpt is skipped whole');
|
|
435
|
+
assert.ok(Array.from(capped.text).length <= 1500);
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
test('a repo Cue+Lesson excerpt that cannot fit is skipped whole', () => {
|
|
439
|
+
const oversizedCue = E3_TEXT.replace(/(^## Cue\r?\n\r?\n)[\s\S]*?(\r?\n\r?\n## What happened)/m,
|
|
440
|
+
(_, start, end) => `${start}${'C'.repeat(1500)}${end}`);
|
|
441
|
+
const root = repoWorkspace('whole repo excerpt', { recordText: oversizedCue });
|
|
442
|
+
const result = select(input({ workspaceRoot: root, provisionalSource: localSource([PROVISIONAL_FIXTURE[0]]) }));
|
|
443
|
+
assert.deepEqual(result.ids, ['local-commit-a']);
|
|
444
|
+
assert.ok(!result.text.includes('C'.repeat(100)), 'repo cue is not truncated or partially delivered');
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
test('a throwing or oversized provisional source does not hide a valid repo match', () => {
|
|
448
|
+
const throwing = select(input({ provisionalSource: () => { throw new Error('private source failure'); } }));
|
|
449
|
+
assert.deepEqual(throwing.ids, ['E-0003']);
|
|
450
|
+
const oversized = select(input({
|
|
451
|
+
provisionalSource: localSource([fixtureCandidate({ lesson: 'L'.repeat(5000) })])
|
|
452
|
+
}));
|
|
453
|
+
assert.deepEqual(oversized.ids, ['E-0003']);
|
|
454
|
+
assert.ok(!oversized.text.includes('L'.repeat(100)));
|
|
455
|
+
const malformed = select(input({ provisionalSource: () => [{}] }));
|
|
456
|
+
assert.deepEqual(malformed.ids, ['E-0003']);
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
test('a newly written provisional candidate is read on the next event without a module restart', () => {
|
|
460
|
+
let candidates = [];
|
|
461
|
+
const base = input({ workspaceRoot: workspace('new local lesson'), provisionalSource: () => candidates });
|
|
462
|
+
assert.equal(select(base), null);
|
|
463
|
+
candidates = [PROVISIONAL_FIXTURE[0]];
|
|
464
|
+
assert.deepEqual(select(base).ids, ['local-commit-a']);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
test('repo corruption fails open while an independent valid provisional source still works', () => {
|
|
468
|
+
const root = repoWorkspace('broken index', { indexText: '{bad json}\n' });
|
|
469
|
+
const result = select(input({ workspaceRoot: root, provisionalSource: localSource([PROVISIONAL_FIXTURE[0]]) }));
|
|
470
|
+
assert.deepEqual(result.ids, ['local-commit-a']);
|
|
471
|
+
assert.ok(!result.text.includes('commitlint'));
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
test('malformed or oversized repo inputs produce no repo text', () => {
|
|
475
|
+
const unboundRow = { ...REAL_E3 };
|
|
476
|
+
delete unboundRow.matchBinding;
|
|
477
|
+
const cases = [
|
|
478
|
+
['unbound matched row', repoWorkspace('unbound row', { indexText: JSON.stringify(unboundRow) + '\n' })],
|
|
479
|
+
['oversized index', repoWorkspace('large index', { indexText: ' '.repeat(600 * 1024) })],
|
|
480
|
+
['missing record', repoWorkspace('missing record', { missingRecord: true })],
|
|
481
|
+
['oversized record', repoWorkspace('large record', { recordText: E3_TEXT + 'x'.repeat(40 * 1024) })],
|
|
482
|
+
['malformed record', repoWorkspace('malformed record', { recordText: 'not a Markdown ledger record\n' })],
|
|
483
|
+
['missing cue', repoWorkspace('no cue', { recordText: E3_TEXT.replace(/^## Cue$/m, '## Situation') })],
|
|
484
|
+
['missing lesson', repoWorkspace('no lesson', { recordText: E3_TEXT.replace(/^## Lesson$/m, '## Advice') })]
|
|
485
|
+
];
|
|
486
|
+
for (const [label, root] of cases) {
|
|
487
|
+
assert.equal(select(input({ workspaceRoot: root })), null, label);
|
|
488
|
+
}
|
|
489
|
+
const staleRow = { ...REAL_E3, match: { ...REAL_E3.match, actions: [{ tool: 'shell', executable: 'git', subcommand: 'status' }] } };
|
|
490
|
+
const stale = repoWorkspace('stale index', { indexText: `${JSON.stringify(staleRow)}\n` });
|
|
491
|
+
assert.equal(select(input({ workspaceRoot: stale, event: { phase: 'action', tool: 'shell', command: 'git status', outcome: { failed: 'unknown' } } })), null,
|
|
492
|
+
'a well-formed edited index does not override its canonical record');
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
test('traversal and escaping symlinks are rejected', t => {
|
|
496
|
+
const traversal = repoWorkspace('traversal', {
|
|
497
|
+
indexText: `${JSON.stringify({ ...REAL_E3, file: '../../outside.md' })}\n`
|
|
498
|
+
});
|
|
499
|
+
assert.equal(select(input({ workspaceRoot: traversal })), null, 'traversal is silent');
|
|
500
|
+
|
|
501
|
+
const root = workspace('symlink');
|
|
502
|
+
const ledger = path.join(root, '.agents', 'harness-everything', 'errors');
|
|
503
|
+
const record = path.join(ledger, REAL_E3.file.split('/').at(-1));
|
|
504
|
+
const outside = path.join(TMP, `outside-${++serial}.md`);
|
|
505
|
+
fs.mkdirSync(ledger, { recursive: true });
|
|
506
|
+
fs.writeFileSync(path.join(ledger, 'index.jsonl'), `${JSON.stringify(REAL_E3)}\n`);
|
|
507
|
+
fs.writeFileSync(outside, E3_TEXT);
|
|
508
|
+
try {
|
|
509
|
+
fs.symlinkSync(outside, record, 'file');
|
|
510
|
+
} catch (error) {
|
|
511
|
+
if (['EPERM', 'EACCES', 'ENOSYS'].includes(error.code)) return t.skip(`file symlink unavailable: ${error.code}`);
|
|
512
|
+
throw error;
|
|
513
|
+
}
|
|
514
|
+
assert.equal(select(input({ workspaceRoot: root })), null, 'a record symlink outside the ledger is silent');
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
test('an unavailable dedupe directory is silent and never falls back', () => {
|
|
518
|
+
const blocker = path.join(TMP, `state-file-${++serial}`);
|
|
519
|
+
fs.writeFileSync(blocker, 'not a directory');
|
|
520
|
+
assert.equal(select(input({ stateDir: path.join(blocker, 'session') })), null);
|
|
521
|
+
});
|