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,278 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const assert = require('node:assert/strict');
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const os = require('node:os');
|
|
7
|
+
const path = require('node:path');
|
|
8
|
+
const { execFileSync, spawnSync } = require('node:child_process');
|
|
9
|
+
|
|
10
|
+
const ROOT = path.resolve(__dirname, '..');
|
|
11
|
+
const CLI = path.join(ROOT, 'scripts', 'errors-local.js');
|
|
12
|
+
const localErrors = require(path.join(ROOT, 'hooks', 'scripts', 'lib', 'local-errors.js'));
|
|
13
|
+
const { createProvisionalSource } = require(path.join(ROOT, 'hooks', 'scripts', 'lib', 'local-error-source.js'));
|
|
14
|
+
const { matchLedgerLessons } = require(path.join(ROOT, 'hooks', 'scripts', 'lib', 'ledger-match.js'));
|
|
15
|
+
|
|
16
|
+
let failures = 0;
|
|
17
|
+
const sandboxes = [];
|
|
18
|
+
|
|
19
|
+
function test(name, fn) {
|
|
20
|
+
try {
|
|
21
|
+
fn();
|
|
22
|
+
console.log(` PASS ${name}`);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
failures += 1;
|
|
25
|
+
console.error(` FAIL ${name}\n ${error.stack.split('\n').slice(0, 5).join('\n ')}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function canonicalDir(dir) {
|
|
30
|
+
return fs.realpathSync.native ? fs.realpathSync.native(dir) : fs.realpathSync(dir);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function makeSandbox() {
|
|
34
|
+
const dir = canonicalDir(fs.mkdtempSync(path.join(os.tmpdir(), 'local-err-review-')));
|
|
35
|
+
sandboxes.push(dir);
|
|
36
|
+
const home = path.join(dir, 'home');
|
|
37
|
+
const store = path.join(dir, 'observations');
|
|
38
|
+
const stateHome = path.join(dir, 'state-home');
|
|
39
|
+
const repo = path.join(dir, 'repo');
|
|
40
|
+
const matcherState = path.join(dir, 'matcher-state');
|
|
41
|
+
for (const p of [home, store, stateHome, repo, matcherState]) {
|
|
42
|
+
fs.mkdirSync(p, { recursive: true });
|
|
43
|
+
}
|
|
44
|
+
execFileSync('git', ['init', '-q', repo]);
|
|
45
|
+
const env = {
|
|
46
|
+
...process.env,
|
|
47
|
+
HOME: home,
|
|
48
|
+
USERPROFILE: home,
|
|
49
|
+
HARNESS_OBSERVATIONS_DIR: store,
|
|
50
|
+
HARNESS_STATE_HOME: stateHome,
|
|
51
|
+
};
|
|
52
|
+
delete env.HARNESS_OBSERVATIONS;
|
|
53
|
+
delete env.HARNESS_SYSTEM_ONE_MODE;
|
|
54
|
+
delete env.HARNESS_WORKSPACE_ROOT;
|
|
55
|
+
delete env.CLAUDE_PROJECT_DIR;
|
|
56
|
+
delete env.CODEX_PROJECT_DIR;
|
|
57
|
+
return { dir, store, repo, matcherState, env };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function runCli(args, sandbox) {
|
|
61
|
+
return spawnSync(process.execPath, [CLI, ...args], {
|
|
62
|
+
cwd: sandbox.repo,
|
|
63
|
+
env: sandbox.env,
|
|
64
|
+
encoding: 'utf8',
|
|
65
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function actionMatch() {
|
|
70
|
+
return { actions: [{ tool: 'shell', executable: 'git', subcommand: 'commit' }] };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
console.log('mechanism-496-review-fixes:');
|
|
74
|
+
|
|
75
|
+
test('redacts complete Basic/Bearer authorization values before storage and signature derivation', () => {
|
|
76
|
+
const s = makeSandbox();
|
|
77
|
+
const fixtures = [
|
|
78
|
+
{ header: 'Authorization', scheme: 'Basic', credential: 'BASIC_FIXTURE_ALPHA_91' },
|
|
79
|
+
{ header: 'aUtHoRiZaTiOn', scheme: 'bAsIc', credential: 'BASIC_FIXTURE_BETA_82' },
|
|
80
|
+
{ header: 'Proxy-Authorization', scheme: 'Bearer', credential: 'BEARER_FIXTURE_ALPHA_73' },
|
|
81
|
+
{ header: 'pRoXy-aUtHoRiZaTiOn', scheme: 'bEaReR', credential: 'BEARER_FIXTURE_BETA_64' },
|
|
82
|
+
];
|
|
83
|
+
const credentials = fixtures.map(row => row.credential);
|
|
84
|
+
|
|
85
|
+
for (const [index, row] of fixtures.entries()) {
|
|
86
|
+
const res = localErrors.appendOccurrence({
|
|
87
|
+
workspaceRoot: s.repo,
|
|
88
|
+
host: 'claude',
|
|
89
|
+
sessionKey: `auth-session-${index}`,
|
|
90
|
+
toolUseId: `auth-tool-${index}`,
|
|
91
|
+
command: 'node synthetic-check.js',
|
|
92
|
+
exitCode: 1,
|
|
93
|
+
output: `Fatal synthetic diagnostic AUTH_CONTROL\n${row.header}: ${row.scheme} ${row.credential}`,
|
|
94
|
+
}, { env: s.env });
|
|
95
|
+
assert.equal(res.status, 'appended');
|
|
96
|
+
assert.ok(!res.record.outputExcerpt.includes(row.credential), 'stored occurrence excerpt must omit the whole credential');
|
|
97
|
+
assert.ok(res.record.outputExcerpt.includes('Fatal synthetic diagnostic AUTH_CONTROL'), 'diagnostic remains usable');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const sameFailure = (credential, toolUseId) => localErrors.appendOccurrence({
|
|
101
|
+
workspaceRoot: s.repo,
|
|
102
|
+
host: 'codex',
|
|
103
|
+
sessionKey: 'signature-session',
|
|
104
|
+
toolUseId,
|
|
105
|
+
command: 'node synthetic-check.js',
|
|
106
|
+
exitCode: 1,
|
|
107
|
+
output: `Fatal synthetic diagnostic SIGNATURE_CONTROL\nAuthorization: Basic ${credential}`,
|
|
108
|
+
}, { env: s.env });
|
|
109
|
+
const first = sameFailure('BASIC_SIGNATURE_FIXTURE_11', 'signature-tool-1');
|
|
110
|
+
const second = sameFailure('BASIC_SIGNATURE_FIXTURE_22', 'signature-tool-2');
|
|
111
|
+
assert.equal(first.record.signature, second.record.signature,
|
|
112
|
+
'automatic signatures must ignore only the authorization payload');
|
|
113
|
+
const differentDiagnostic = localErrors.appendOccurrence({
|
|
114
|
+
workspaceRoot: s.repo,
|
|
115
|
+
host: 'codex',
|
|
116
|
+
command: 'node synthetic-check.js',
|
|
117
|
+
exitCode: 1,
|
|
118
|
+
output: 'Fatal synthetic diagnostic DIFFERENT_CONTROL\nAuthorization: Basic BASIC_SIGNATURE_FIXTURE_33',
|
|
119
|
+
}, { env: s.env });
|
|
120
|
+
assert.notEqual(first.record.signature, differentDiagnostic.record.signature,
|
|
121
|
+
'different diagnostics remain distinguishable');
|
|
122
|
+
|
|
123
|
+
const lesson = localErrors.addProvisionalLesson({
|
|
124
|
+
workspaceRoot: s.repo,
|
|
125
|
+
destination: 'project',
|
|
126
|
+
signature: 'v1-git-auth-review-control',
|
|
127
|
+
match: actionMatch(),
|
|
128
|
+
lesson: 'Preserve the safe lesson. Authorization: Basic BASIC_LESSON_FIXTURE_54',
|
|
129
|
+
cue: 'Before committing. Proxy-Authorization: Bearer BEARER_CUE_FIXTURE_45',
|
|
130
|
+
}, { env: s.env });
|
|
131
|
+
assert.equal(lesson.status, 'appended');
|
|
132
|
+
credentials.push('BASIC_SIGNATURE_FIXTURE_11', 'BASIC_SIGNATURE_FIXTURE_22', 'BASIC_SIGNATURE_FIXTURE_33',
|
|
133
|
+
'BASIC_LESSON_FIXTURE_54', 'BEARER_CUE_FIXTURE_45');
|
|
134
|
+
|
|
135
|
+
const rawIndex = fs.readFileSync(path.join(s.store, 'errors-index.json'), 'utf8');
|
|
136
|
+
for (const credential of credentials) {
|
|
137
|
+
assert.ok(!rawIndex.includes(credential), `raw errors-index.json must omit ${credential}`);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const listed = runCli(['list', '--json'], s);
|
|
141
|
+
assert.equal(listed.status, 0, listed.stderr);
|
|
142
|
+
const exported = runCli(['export'], s);
|
|
143
|
+
assert.equal(exported.status, 0, exported.stderr);
|
|
144
|
+
for (const output of [listed.stdout, exported.stdout]) {
|
|
145
|
+
for (const credential of credentials) assert.ok(!output.includes(credential), 'CLI output must omit fixture credentials');
|
|
146
|
+
assert.ok(output.includes('Fatal synthetic diagnostic AUTH_CONTROL'), 'CLI retains a safe diagnostic');
|
|
147
|
+
assert.ok(output.includes('Preserve the safe lesson.'), 'CLI retains the safe lesson');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const source = createProvisionalSource({ env: s.env, workspaceRoot: s.repo });
|
|
151
|
+
const matched = matchLedgerLessons({
|
|
152
|
+
workspaceRoot: s.repo,
|
|
153
|
+
workspaceIdentity: lesson.record.workspaceKey,
|
|
154
|
+
sessionIdentity: 'synthetic-session',
|
|
155
|
+
stateDir: s.matcherState,
|
|
156
|
+
event: { phase: 'action', tool: 'shell', command: 'git commit -m "safe"', outcome: { failed: 'unknown' } },
|
|
157
|
+
provisionalSource: source,
|
|
158
|
+
});
|
|
159
|
+
assert.ok(matched && matched.text.includes('Preserve the safe lesson.'), 'positive control lesson remains deliverable');
|
|
160
|
+
for (const credential of credentials) assert.ok(!matched.text.includes(credential), 'matched context must omit fixture credentials');
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('read-side retention filters before limits, stays read-only, and withholds expired matcher lessons', () => {
|
|
164
|
+
const s = makeSandbox();
|
|
165
|
+
const now = Date.now();
|
|
166
|
+
const retentionMs = localErrors.LIMITS.retentionMs;
|
|
167
|
+
const cutoff = now - retentionMs;
|
|
168
|
+
const workspaceA = localErrors.resolveWorkspaceKey({ workspaceRoot: s.repo }, { env: s.env });
|
|
169
|
+
const otherRepo = path.join(s.dir, 'other-repo');
|
|
170
|
+
fs.mkdirSync(otherRepo);
|
|
171
|
+
execFileSync('git', ['init', '-q', otherRepo]);
|
|
172
|
+
const workspaceB = localErrors.resolveWorkspaceKey({ workspaceRoot: otherRepo }, { env: s.env });
|
|
173
|
+
|
|
174
|
+
const occurrence = (id, workspaceKey, createdAt, outputExcerpt) => ({
|
|
175
|
+
schemaVersion: 1,
|
|
176
|
+
kind: 'occurrence',
|
|
177
|
+
provisionalId: id,
|
|
178
|
+
workspaceKey,
|
|
179
|
+
destination: 'unknown',
|
|
180
|
+
createdAt: new Date(createdAt).toISOString(),
|
|
181
|
+
signature: `v1-git-${id}`,
|
|
182
|
+
source: 'tool-failure',
|
|
183
|
+
host: 'claude',
|
|
184
|
+
sessionKey: null,
|
|
185
|
+
toolUseId: null,
|
|
186
|
+
commandClass: 'git',
|
|
187
|
+
exitCode: 1,
|
|
188
|
+
outputExcerpt,
|
|
189
|
+
matchedLedgerIds: [],
|
|
190
|
+
});
|
|
191
|
+
const lesson = (id, workspaceKey, createdAt, text) => ({
|
|
192
|
+
schemaVersion: 1,
|
|
193
|
+
kind: 'provisional-lesson',
|
|
194
|
+
provisionalId: id,
|
|
195
|
+
workspaceKey,
|
|
196
|
+
destination: 'project',
|
|
197
|
+
createdAt: new Date(createdAt).toISOString(),
|
|
198
|
+
signature: `v1-git-${id}`,
|
|
199
|
+
match: actionMatch(),
|
|
200
|
+
lesson: text,
|
|
201
|
+
issueUrl: null,
|
|
202
|
+
promotionId: null,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const file = path.join(s.store, 'errors-index.json');
|
|
206
|
+
const initialIndex = {
|
|
207
|
+
schemaVersion: 1,
|
|
208
|
+
kind: 'local-error-store',
|
|
209
|
+
records: [
|
|
210
|
+
occurrence('future-occurrence', workspaceA, now + 60_000, 'Fresh future occurrence diagnostic.'),
|
|
211
|
+
occurrence('fresh-occurrence', workspaceA, cutoff + 2, 'Fresh cutoff occurrence diagnostic.'),
|
|
212
|
+
occurrence('expired-occurrence', workspaceA, cutoff - 1, 'Expired occurrence diagnostic.'),
|
|
213
|
+
occurrence('boundary-occurrence', workspaceA, cutoff, 'Boundary occurrence diagnostic.'),
|
|
214
|
+
occurrence('other-workspace-occurrence', workspaceB, now + 60_000, 'Other workspace diagnostic.'),
|
|
215
|
+
lesson('expired-lesson', workspaceA, cutoff - 1, 'Expired matching lesson.'),
|
|
216
|
+
lesson('boundary-lesson', workspaceA, cutoff, 'Boundary matching lesson.'),
|
|
217
|
+
lesson('fresh-cutoff-lesson', workspaceA, cutoff + 2, 'Fresh cutoff matching lesson.'),
|
|
218
|
+
lesson('future-lesson', workspaceA, now + 60_000, 'Fresh matching lesson positive control.'),
|
|
219
|
+
],
|
|
220
|
+
};
|
|
221
|
+
fs.writeFileSync(file, `${JSON.stringify(initialIndex, null, 2)}\n`, 'utf8');
|
|
222
|
+
|
|
223
|
+
const indexBefore = fs.readFileSync(file);
|
|
224
|
+
const storeEntriesBefore = fs.readdirSync(s.store).sort();
|
|
225
|
+
const limited = localErrors.listRecords({
|
|
226
|
+
env: s.env,
|
|
227
|
+
workspaceRoot: s.repo,
|
|
228
|
+
kind: 'occurrence',
|
|
229
|
+
limit: 1,
|
|
230
|
+
now,
|
|
231
|
+
limits: { retentionMs },
|
|
232
|
+
});
|
|
233
|
+
assert.equal(limited.length, 1);
|
|
234
|
+
assert.equal(limited[0].provisionalId, 'fresh-occurrence', 'retention filtering happens before the limit');
|
|
235
|
+
|
|
236
|
+
const records = localErrors.listRecords({ env: s.env, workspaceRoot: s.repo, now, limits: { retentionMs } });
|
|
237
|
+
assert.deepEqual(records.map(record => record.provisionalId), [
|
|
238
|
+
'future-occurrence', 'fresh-occurrence', 'fresh-cutoff-lesson', 'future-lesson',
|
|
239
|
+
]);
|
|
240
|
+
assert.ok(!records.some(record => /expired|boundary|other-workspace/.test(record.provisionalId)));
|
|
241
|
+
assert.deepEqual(fs.readdirSync(s.store).sort(), storeEntriesBefore, 'read must not create a lock or claim in the store');
|
|
242
|
+
assert.equal(fs.existsSync(path.join(s.store, '.index.lock')), false, 'read must not acquire the write lock');
|
|
243
|
+
assert.equal(Buffer.compare(indexBefore, fs.readFileSync(file)), 0, 'read must leave index bytes unchanged');
|
|
244
|
+
|
|
245
|
+
const listed = runCli(['list', '--json'], s);
|
|
246
|
+
assert.equal(listed.status, 0, listed.stderr);
|
|
247
|
+
const exported = runCli(['export'], s);
|
|
248
|
+
assert.equal(exported.status, 0, exported.stderr);
|
|
249
|
+
for (const output of [listed.stdout, exported.stdout]) {
|
|
250
|
+
assert.ok(output.includes('future-occurrence'), 'fresh workspace-matching occurrence is listed/exported');
|
|
251
|
+
assert.ok(output.includes('future-lesson'), 'fresh workspace-matching lesson is listed/exported');
|
|
252
|
+
assert.ok(!output.includes('expired-occurrence'));
|
|
253
|
+
assert.ok(!output.includes('boundary-occurrence'));
|
|
254
|
+
assert.ok(!output.includes('expired-lesson'));
|
|
255
|
+
assert.ok(!output.includes('boundary-lesson'));
|
|
256
|
+
assert.ok(!output.includes('other-workspace-occurrence'));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const source = createProvisionalSource({ env: s.env, workspaceRoot: s.repo });
|
|
260
|
+
const candidates = source(workspaceA);
|
|
261
|
+
assert.deepEqual(candidates.map(candidate => candidate.identity), ['future-lesson'],
|
|
262
|
+
'expired and cutoff-boundary lessons are not offered by the provisional source');
|
|
263
|
+
const matched = matchLedgerLessons({
|
|
264
|
+
workspaceRoot: s.repo,
|
|
265
|
+
workspaceIdentity: workspaceA,
|
|
266
|
+
sessionIdentity: 'retention-session',
|
|
267
|
+
stateDir: s.matcherState,
|
|
268
|
+
event: { phase: 'action', tool: 'shell', command: 'git commit -m "safe"', outcome: { failed: 'unknown' } },
|
|
269
|
+
provisionalSource: source,
|
|
270
|
+
});
|
|
271
|
+
assert.ok(matched && matched.text.includes('Fresh matching lesson positive control.'));
|
|
272
|
+
assert.ok(!matched.text.includes('Expired matching lesson.'));
|
|
273
|
+
assert.ok(!matched.text.includes('Boundary matching lesson.'));
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
for (const dir of sandboxes) fs.rmSync(dir, { recursive: true, force: true });
|
|
277
|
+
console.log(failures ? `\n${failures} failure(s)` : '\nAll review-fix checks passed.');
|
|
278
|
+
process.exit(failures ? 1 : 0);
|
|
@@ -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
|
+
};
|