@farmslot/agent-runtime 0.45.13 → 0.47.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/CHANGELOG.md +8 -0
- package/dist/execution-template/bundled-catalog.d.ts +35 -0
- package/dist/execution-template/bundled-catalog.d.ts.map +1 -0
- package/dist/execution-template/bundled-catalog.js +110 -0
- package/dist/execution-template/bundled-catalog.js.map +1 -0
- package/dist/execution-template/index.d.ts +1 -0
- package/dist/execution-template/index.d.ts.map +1 -1
- package/dist/execution-template/index.js +1 -0
- package/dist/execution-template/index.js.map +1 -1
- package/dist/execution-template/source-config.d.ts +2 -0
- package/dist/execution-template/source-config.d.ts.map +1 -1
- package/dist/execution-template/source-config.js +12 -5
- package/dist/execution-template/source-config.js.map +1 -1
- package/dist/native/acp.d.ts.map +1 -1
- package/dist/native/acp.js +1 -0
- package/dist/native/acp.js.map +1 -1
- package/dist/native/claude.d.ts.map +1 -1
- package/dist/native/claude.js +56 -2
- package/dist/native/claude.js.map +1 -1
- package/dist/native/client.d.ts +3 -1
- package/dist/native/client.d.ts.map +1 -1
- package/dist/native/client.js +26 -3
- package/dist/native/client.js.map +1 -1
- package/dist/native/codex.d.ts.map +1 -1
- package/dist/native/codex.js +1 -0
- package/dist/native/codex.js.map +1 -1
- package/dist/native/host.js +8 -1
- package/dist/native/host.js.map +1 -1
- package/dist/native/index.d.ts +1 -1
- package/dist/native/index.d.ts.map +1 -1
- package/dist/native/index.js +1 -1
- package/dist/native/index.js.map +1 -1
- package/dist/native/ipc.d.ts +10 -2
- package/dist/native/ipc.d.ts.map +1 -1
- package/dist/native/ipc.js +22 -2
- package/dist/native/ipc.js.map +1 -1
- package/dist/native/manager.d.ts +2 -1
- package/dist/native/manager.d.ts.map +1 -1
- package/dist/native/manager.js +73 -0
- package/dist/native/manager.js.map +1 -1
- package/dist/native/pi.d.ts.map +1 -1
- package/dist/native/pi.js +1 -0
- package/dist/native/pi.js.map +1 -1
- package/dist/native/supervisor.js +40 -4
- package/dist/native/supervisor.js.map +1 -1
- package/dist/native/types.d.ts +3 -1
- package/dist/native/types.d.ts.map +1 -1
- package/dist/native/worker-launch.d.ts +5 -0
- package/dist/native/worker-launch.d.ts.map +1 -1
- package/dist/native/worker-launch.js +2 -0
- package/dist/native/worker-launch.js.map +1 -1
- package/dist/native/worker-service.d.ts.map +1 -1
- package/dist/native/worker-service.js +8 -2
- package/dist/native/worker-service.js.map +1 -1
- package/package.json +14 -4
- package/scripts/context-compaction.cjs +52 -0
- package/scripts/context-compaction.d.cts +7 -0
- package/scripts/mark-checklist-step.cjs +2 -0
- package/scripts/review-compaction.cjs +138 -0
- package/scripts/review-coverage.cjs +598 -0
- package/scripts/review-coverage.d.cts +126 -0
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { createHash } = require('node:crypto');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const { execFileSync } = require('node:child_process');
|
|
7
|
+
const { enumerateChecklistCheckboxes } = require('./checklist-target.cjs');
|
|
8
|
+
|
|
9
|
+
const OUTCOMES = new Set(['PASS', 'FINDING', 'NOT_APPLICABLE', 'NOT_CHECKED']);
|
|
10
|
+
const sha256 = (text) => createHash('sha256').update(text).digest('hex');
|
|
11
|
+
const object = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
12
|
+
const hashMap = (value) =>
|
|
13
|
+
object(value) && Object.values(value).every((entry) => typeof entry === 'string' && entry);
|
|
14
|
+
|
|
15
|
+
function coverageRowKey(row) {
|
|
16
|
+
if (row.kind === 'scope' && typeof row.path === 'string' && row.path) return `scope:${row.path}`;
|
|
17
|
+
if (
|
|
18
|
+
row.kind === 'criterion' &&
|
|
19
|
+
typeof row.unitId === 'string' &&
|
|
20
|
+
row.unitId &&
|
|
21
|
+
Number.isInteger(row.stepNumber) &&
|
|
22
|
+
row.stepNumber > 0 &&
|
|
23
|
+
typeof row.ruleSourceSha256 === 'string' &&
|
|
24
|
+
row.ruleSourceSha256
|
|
25
|
+
)
|
|
26
|
+
return `criterion:${row.unitId}:${row.stepNumber}:${row.ruleSourceSha256}`;
|
|
27
|
+
throw new Error('Invalid review coverage row identity');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The caller supplies hashes from a validated earlier review, never from model claims. */
|
|
31
|
+
function readCoverageIndex(bytes, expected) {
|
|
32
|
+
if (
|
|
33
|
+
typeof bytes !== 'string' ||
|
|
34
|
+
!expected ||
|
|
35
|
+
!expected.indexSha256 ||
|
|
36
|
+
sha256(bytes) !== expected.indexSha256
|
|
37
|
+
)
|
|
38
|
+
return null;
|
|
39
|
+
let value;
|
|
40
|
+
try {
|
|
41
|
+
value = JSON.parse(bytes);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
// A legacy/malformed index disables reuse; it cannot invalidate the delta range.
|
|
44
|
+
if (error instanceof SyntaxError) return null;
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
if (
|
|
48
|
+
!object(value) ||
|
|
49
|
+
value.schemaVersion !== 1 ||
|
|
50
|
+
!Array.isArray(value.rows) ||
|
|
51
|
+
!['runId', 'headSha', 'reportSha256', 'authorizationHash', 'ruleHash'].every(
|
|
52
|
+
(key) => typeof value[key] === 'string' && value[key],
|
|
53
|
+
) ||
|
|
54
|
+
['runId', 'headSha', 'reportSha256'].some((key) => value[key] !== expected[key])
|
|
55
|
+
)
|
|
56
|
+
return null;
|
|
57
|
+
const keys = new Set();
|
|
58
|
+
for (const row of value.rows) {
|
|
59
|
+
if (
|
|
60
|
+
!object(row) ||
|
|
61
|
+
!object(row.definition) ||
|
|
62
|
+
typeof row.key !== 'string' ||
|
|
63
|
+
!object(row.value) ||
|
|
64
|
+
!hashMap(row.sourceHashes) ||
|
|
65
|
+
!hashMap(row.evidenceHashes) ||
|
|
66
|
+
keys.has(row.key)
|
|
67
|
+
)
|
|
68
|
+
return null;
|
|
69
|
+
try {
|
|
70
|
+
if (coverageRowKey(row.definition) !== row.key) return null;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if (error.message === 'Invalid review coverage row identity') return null;
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
if (row.definition.kind === 'criterion' && !OUTCOMES.has(row.value.outcome)) return null;
|
|
76
|
+
if (
|
|
77
|
+
row.definition.kind === 'scope' &&
|
|
78
|
+
![row.value.scope, row.value.justification].every(
|
|
79
|
+
(text) => typeof text === 'string' && text.trim(),
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
return null;
|
|
83
|
+
keys.add(row.key);
|
|
84
|
+
}
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function reusableCoverageRows(index, inputs) {
|
|
89
|
+
const reusable = [],
|
|
90
|
+
invalidated = [];
|
|
91
|
+
if (!index) return { reusable, invalidated, unavailable: 'missing-or-unverified-index' };
|
|
92
|
+
const definitions = new Map(inputs.definitions.map((row) => [coverageRowKey(row), row]));
|
|
93
|
+
for (const row of index.rows) {
|
|
94
|
+
let reason;
|
|
95
|
+
if (index.authorizationHash !== inputs.authorizationHash) reason = 'authorization-changed';
|
|
96
|
+
else if (index.ruleHash !== inputs.ruleHash) reason = 'rules-changed';
|
|
97
|
+
else if (!definitions.has(row.key)) reason = 'row-no-longer-current';
|
|
98
|
+
else if (
|
|
99
|
+
row.definition.kind === 'criterion' &&
|
|
100
|
+
(row.definition.label !== definitions.get(row.key).label ||
|
|
101
|
+
row.definition.family !== definitions.get(row.key).family)
|
|
102
|
+
)
|
|
103
|
+
reason = 'rule-binding-changed';
|
|
104
|
+
else if (
|
|
105
|
+
row.definition.kind === 'criterion' &&
|
|
106
|
+
['PASS', 'FINDING'].includes(row.value.outcome) &&
|
|
107
|
+
!Object.keys(row.sourceHashes).length &&
|
|
108
|
+
!Object.keys(row.evidenceHashes).length
|
|
109
|
+
)
|
|
110
|
+
reason = 'no-verifiable-row-input';
|
|
111
|
+
else if (
|
|
112
|
+
Object.entries(row.sourceHashes).some(
|
|
113
|
+
([key, value]) => value === 'missing' || inputs.sourceHashes[key] !== value,
|
|
114
|
+
)
|
|
115
|
+
)
|
|
116
|
+
reason = 'source-changed-or-missing';
|
|
117
|
+
else if (
|
|
118
|
+
Object.entries(row.evidenceHashes).some(
|
|
119
|
+
([key, value]) => value === 'missing' || inputs.evidenceHashes[key] !== value,
|
|
120
|
+
)
|
|
121
|
+
)
|
|
122
|
+
reason = 'evidence-changed-or-missing';
|
|
123
|
+
if (reason) invalidated.push({ key: row.key, reason });
|
|
124
|
+
else reusable.push(row);
|
|
125
|
+
}
|
|
126
|
+
return { reusable, invalidated };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function strings(value, name) {
|
|
130
|
+
if (!Array.isArray(value) || !value.every((entry) => typeof entry === 'string' && entry))
|
|
131
|
+
throw new Error(`Review coverage ${name} must be an explicit array of strings`);
|
|
132
|
+
return [...new Set(value)];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function fingerprints(paths, hashes, allowMissing) {
|
|
136
|
+
return Object.fromEntries(
|
|
137
|
+
paths.map((file) => {
|
|
138
|
+
const value = Object.hasOwn(hashes, file) ? hashes[file] : undefined;
|
|
139
|
+
if (typeof value !== 'string' || (!allowMissing && value === 'missing'))
|
|
140
|
+
throw new Error(`Review coverage input is missing: ${file}`);
|
|
141
|
+
return [file, value];
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Reuse is an explicit reviewer applicability claim, not a current-head verdict. */
|
|
147
|
+
function composeCoverageRows(inputs, update, baseline) {
|
|
148
|
+
if (!object(update) || update.schemaVersion !== 1 || !Array.isArray(update.rows))
|
|
149
|
+
throw new Error('Expected review-result.json coverage schemaVersion 1 and rows');
|
|
150
|
+
const reuse = strings(update.reuse, 'reuse');
|
|
151
|
+
if (reuse.length && update.applicabilityChecked !== true)
|
|
152
|
+
throw new Error('Reused review rows require current-delta applicability confirmation');
|
|
153
|
+
const available = new Map(
|
|
154
|
+
reusableCoverageRows(baseline, inputs).reusable.map((row) => [row.key, row]),
|
|
155
|
+
);
|
|
156
|
+
const definitions = new Map(inputs.definitions.map((row) => [coverageRowKey(row), row]));
|
|
157
|
+
if (definitions.size !== inputs.definitions.length)
|
|
158
|
+
throw new Error('Duplicate coverage definition');
|
|
159
|
+
const result = new Map();
|
|
160
|
+
for (const key of reuse) {
|
|
161
|
+
const row = available.get(key);
|
|
162
|
+
if (!row) throw new Error(`Review coverage row requires current evaluation: ${key}`);
|
|
163
|
+
result.set(key, {
|
|
164
|
+
...row,
|
|
165
|
+
definition: definitions.get(key),
|
|
166
|
+
provenance: {
|
|
167
|
+
runId: baseline.runId,
|
|
168
|
+
headSha: baseline.headSha,
|
|
169
|
+
reportSha256: baseline.reportSha256,
|
|
170
|
+
applicabilityChecked: true,
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
for (const row of update.rows) {
|
|
175
|
+
if (!object(row) || typeof row.key !== 'string' || !definitions.has(row.key))
|
|
176
|
+
throw new Error('Review coverage update has an unknown row');
|
|
177
|
+
if (result.has(row.key)) throw new Error(`Duplicate review coverage update: ${row.key}`);
|
|
178
|
+
const definition = definitions.get(row.key);
|
|
179
|
+
const sources = strings(row.sources, 'sources');
|
|
180
|
+
const artifacts = strings(row.artifacts, 'artifacts');
|
|
181
|
+
const value =
|
|
182
|
+
definition.kind === 'scope'
|
|
183
|
+
? { scope: row.scope, justification: row.justification }
|
|
184
|
+
: { outcome: row.outcome, evidence: row.evidence };
|
|
185
|
+
if (definition.kind === 'scope') {
|
|
186
|
+
if (!sources.includes(definition.path)) sources.push(definition.path);
|
|
187
|
+
if (
|
|
188
|
+
![value.scope, value.justification].every((text) => typeof text === 'string' && text.trim())
|
|
189
|
+
)
|
|
190
|
+
throw new Error('Scope coverage requires authorization and justification');
|
|
191
|
+
} else if (
|
|
192
|
+
!OUTCOMES.has(value.outcome) ||
|
|
193
|
+
typeof value.evidence !== 'string' ||
|
|
194
|
+
!value.evidence.trim()
|
|
195
|
+
)
|
|
196
|
+
throw new Error('Criterion coverage requires an outcome and evidence or gap');
|
|
197
|
+
const allowMissing = value.outcome === 'NOT_CHECKED';
|
|
198
|
+
result.set(row.key, {
|
|
199
|
+
key: row.key,
|
|
200
|
+
definition,
|
|
201
|
+
value,
|
|
202
|
+
sourceHashes: fingerprints(sources, inputs.sourceHashes, allowMissing),
|
|
203
|
+
evidenceHashes: fingerprints(artifacts, inputs.evidenceHashes, allowMissing),
|
|
204
|
+
provenance: { runId: inputs.runId, headSha: inputs.headSha, applicabilityChecked: true },
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
for (const key of definitions.keys())
|
|
208
|
+
if (!result.has(key)) throw new Error(`Missing current review coverage row: ${key}`);
|
|
209
|
+
return {
|
|
210
|
+
schemaVersion: 1,
|
|
211
|
+
runId: inputs.runId,
|
|
212
|
+
headSha: inputs.headSha,
|
|
213
|
+
authorizationHash: inputs.authorizationHash,
|
|
214
|
+
ruleHash: inputs.ruleHash,
|
|
215
|
+
rows: [...definitions.keys()].map((key) => result.get(key)),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function cell(value) {
|
|
220
|
+
return String(value).replaceAll('|', '\\|').replaceAll('\n', ' ');
|
|
221
|
+
}
|
|
222
|
+
function renderCoverage(index) {
|
|
223
|
+
const scope = [
|
|
224
|
+
'| File | Ticket AC or required test | Scope justification or finding |',
|
|
225
|
+
'| --- | --- | --- |',
|
|
226
|
+
];
|
|
227
|
+
const ledger = [
|
|
228
|
+
'# Review ledger',
|
|
229
|
+
'',
|
|
230
|
+
`Current head: ${index.headSha}`,
|
|
231
|
+
'',
|
|
232
|
+
'## Criteria ledger',
|
|
233
|
+
'',
|
|
234
|
+
];
|
|
235
|
+
const units = new Map();
|
|
236
|
+
for (const row of index.rows) {
|
|
237
|
+
const d = row.definition,
|
|
238
|
+
v = row.value;
|
|
239
|
+
const reused =
|
|
240
|
+
row.provenance.headSha !== index.headSha
|
|
241
|
+
? ` Reused from ${row.provenance.headSha}; current-delta applicability checked.`
|
|
242
|
+
: '';
|
|
243
|
+
if (d.kind === 'scope')
|
|
244
|
+
scope.push(`| \`${cell(d.path)}\` | ${cell(v.scope)} | ${cell(v.justification + reused)} |`);
|
|
245
|
+
else {
|
|
246
|
+
if (!units.has(d.unitId)) units.set(d.unitId, []);
|
|
247
|
+
units
|
|
248
|
+
.get(d.unitId)
|
|
249
|
+
.push(
|
|
250
|
+
`| ${d.stepNumber} | ${cell(d.family ?? d.unitId)} | ${cell(d.label)} | ${v.outcome} | ${cell(v.evidence + reused)} |`,
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
for (const [unit, rows] of units)
|
|
255
|
+
ledger.push(
|
|
256
|
+
`### ${unit}`,
|
|
257
|
+
'',
|
|
258
|
+
'| # | Family | Rule | Outcome | Evidence or gap |',
|
|
259
|
+
'| --- | --- | --- | --- | --- |',
|
|
260
|
+
...rows,
|
|
261
|
+
'',
|
|
262
|
+
);
|
|
263
|
+
ledger.push('## Diff scope', '', ...scope, '');
|
|
264
|
+
return { scope: scope.join('\n'), ledger: ledger.join('\n') };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function relativeFile(value) {
|
|
268
|
+
if (
|
|
269
|
+
typeof value !== 'string' ||
|
|
270
|
+
!value ||
|
|
271
|
+
value.includes('\\') ||
|
|
272
|
+
value.includes('\0') ||
|
|
273
|
+
path.posix.isAbsolute(value) ||
|
|
274
|
+
path.posix.normalize(value) !== value ||
|
|
275
|
+
value.startsWith('../') ||
|
|
276
|
+
value === '..'
|
|
277
|
+
)
|
|
278
|
+
throw new Error('Review coverage requires a canonical relative path');
|
|
279
|
+
return value;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function optionalFile(file, encoding = 'utf8') {
|
|
283
|
+
try {
|
|
284
|
+
return fs.readFileSync(file, encoding);
|
|
285
|
+
} catch (error) {
|
|
286
|
+
if (error.code === 'ENOENT') return undefined;
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function readReviewTaskFile(taskDir, file, encoding = 'utf8') {
|
|
292
|
+
const root = fs.realpathSync(taskDir);
|
|
293
|
+
const target = path.join(root, relativeFile(file));
|
|
294
|
+
let resolved;
|
|
295
|
+
try {
|
|
296
|
+
resolved = fs.realpathSync(target);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
if (error.code === 'ENOENT') return undefined;
|
|
299
|
+
throw error;
|
|
300
|
+
}
|
|
301
|
+
const relative = path.relative(root, resolved);
|
|
302
|
+
if (relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
|
|
303
|
+
throw new Error('Review coverage file escapes its task directory');
|
|
304
|
+
return fs.readFileSync(resolved, encoding);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function readCoveragePolicy(manifest) {
|
|
308
|
+
const data = typeof manifest === 'string' ? JSON.parse(manifest) : manifest;
|
|
309
|
+
const policy = data?.reviewCoverage;
|
|
310
|
+
if (policy === undefined) return undefined;
|
|
311
|
+
if (
|
|
312
|
+
!object(policy) ||
|
|
313
|
+
policy.schemaVersion !== 1 ||
|
|
314
|
+
!['runId', 'repo', 'headSha', 'authorizationHash', 'ruleHash'].every(
|
|
315
|
+
(key) => typeof policy[key] === 'string' && policy[key],
|
|
316
|
+
) ||
|
|
317
|
+
!path.isAbsolute(policy.repo) ||
|
|
318
|
+
!Array.isArray(policy.scopePaths)
|
|
319
|
+
)
|
|
320
|
+
throw new Error('Invalid frozen review coverage policy');
|
|
321
|
+
policy.scopePaths.forEach(relativeFile);
|
|
322
|
+
if (
|
|
323
|
+
policy.baseline &&
|
|
324
|
+
(!object(policy.baseline) ||
|
|
325
|
+
!['runId', 'headSha', 'reportSha256', 'indexSha256'].every(
|
|
326
|
+
(key) => typeof policy.baseline[key] === 'string' && policy.baseline[key],
|
|
327
|
+
) ||
|
|
328
|
+
!hashMap(policy.baseline.evidenceHashes ?? {}))
|
|
329
|
+
)
|
|
330
|
+
throw new Error('Invalid review coverage baseline identity');
|
|
331
|
+
return policy;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function coverageDefinitions(taskDir, policy) {
|
|
335
|
+
const registry = JSON.parse(readReviewTaskFile(taskDir, 'subtasks/index.json') ?? '{"units":[]}');
|
|
336
|
+
const units = registry.units?.length
|
|
337
|
+
? registry.units
|
|
338
|
+
: [{ id: 'root', checklist: 'CHECKLIST.md', source: { sha256: policy.ruleHash } }];
|
|
339
|
+
return [
|
|
340
|
+
...policy.scopePaths.map((file) => ({ kind: 'scope', path: relativeFile(file) })),
|
|
341
|
+
...units.flatMap((unit) => {
|
|
342
|
+
const text = readReviewTaskFile(taskDir, relativeFile(unit.checklist));
|
|
343
|
+
if (text === undefined) throw new Error('Registered review checklist is missing');
|
|
344
|
+
const lines = text.split('\n');
|
|
345
|
+
return enumerateChecklistCheckboxes(text).map((item, index) => ({
|
|
346
|
+
kind: 'criterion',
|
|
347
|
+
unitId: unit.id,
|
|
348
|
+
stepNumber: index + 1,
|
|
349
|
+
ruleSourceSha256: unit.source.sha256,
|
|
350
|
+
label: lines[item.lineIndex].replace(/^\s*- \[[ xX]\]\s*/, '').trim(),
|
|
351
|
+
}));
|
|
352
|
+
}),
|
|
353
|
+
];
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** One Git batch for the selected paths, including explicit absence for deleted contribution files. */
|
|
357
|
+
function coverageSourceHashes(repo, headSha, files, scopePaths) {
|
|
358
|
+
const unique = [...new Set(files.map(relativeFile))];
|
|
359
|
+
if (!unique.length) return {};
|
|
360
|
+
const output = execFileSync(
|
|
361
|
+
'git',
|
|
362
|
+
['ls-tree', '-r', '-z', '--full-tree', headSha, '--', ...unique],
|
|
363
|
+
{
|
|
364
|
+
cwd: repo,
|
|
365
|
+
encoding: 'utf8',
|
|
366
|
+
env: { ...process.env, GIT_LITERAL_PATHSPECS: '1' },
|
|
367
|
+
},
|
|
368
|
+
);
|
|
369
|
+
return parseCoverageSourceHashes(output, unique, scopePaths);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function parseCoverageSourceHashes(output, files, scopePaths) {
|
|
373
|
+
const result = Object.fromEntries(
|
|
374
|
+
files.map((file) => [relativeFile(file), scopePaths.includes(file) ? 'absent' : 'missing']),
|
|
375
|
+
);
|
|
376
|
+
for (const entry of output.split('\0').filter(Boolean)) {
|
|
377
|
+
const separator = entry.indexOf('\t');
|
|
378
|
+
const [mode, , objectId] = entry.slice(0, separator).split(' ');
|
|
379
|
+
const file = entry.slice(separator + 1);
|
|
380
|
+
if (Object.hasOwn(result, file)) result[file] = `${mode}:${objectId}`;
|
|
381
|
+
}
|
|
382
|
+
return result;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function stripScopeSection(markdown) {
|
|
386
|
+
const lines = markdown.split('\n');
|
|
387
|
+
const start = lines.findIndex((line) => line.trim() === '## Diff scope');
|
|
388
|
+
if (start < 0) return markdown.trimEnd();
|
|
389
|
+
const end = lines.findIndex((line, index) => index > start && /^## /.test(line));
|
|
390
|
+
return [...lines.slice(0, start), ...(end < 0 ? [] : lines.slice(end))].join('\n').trimEnd();
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function writeCoverageFile(file, text) {
|
|
394
|
+
if (optionalFile(file) === text) return;
|
|
395
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
396
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
397
|
+
fs.writeFileSync(temporary, text, { flag: 'wx' });
|
|
398
|
+
fs.renameSync(temporary, file);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function reviewCoverageEvidencePaths(update) {
|
|
402
|
+
if (!object(update) || !Array.isArray(update.rows))
|
|
403
|
+
throw new Error('Review result requires coverage rows');
|
|
404
|
+
return [...new Set(update.rows.flatMap((row) => strings(row.artifacts, 'artifacts')))].map(
|
|
405
|
+
(file) => {
|
|
406
|
+
relativeFile(file);
|
|
407
|
+
if (
|
|
408
|
+
!file.startsWith('artifacts/') ||
|
|
409
|
+
[
|
|
410
|
+
'artifacts/acceptance-status.json',
|
|
411
|
+
'artifacts/review.md',
|
|
412
|
+
'artifacts/review-ledger.md',
|
|
413
|
+
'artifacts/review-coverage.json',
|
|
414
|
+
'artifacts/review-result.json',
|
|
415
|
+
].includes(file)
|
|
416
|
+
)
|
|
417
|
+
throw new Error(
|
|
418
|
+
'Coverage evidence must be separate reviewer proof, never a managed or engine ledger',
|
|
419
|
+
);
|
|
420
|
+
return file;
|
|
421
|
+
},
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function normalizeCoverageUpdate(update, definitions, runId) {
|
|
426
|
+
reviewCoverageEvidencePaths(update);
|
|
427
|
+
const byPosition = new Map(
|
|
428
|
+
definitions
|
|
429
|
+
.filter((d) => d.kind === 'criterion')
|
|
430
|
+
.map((d) => [`${d.unitId}:${d.stepNumber}`, coverageRowKey(d)]),
|
|
431
|
+
);
|
|
432
|
+
return {
|
|
433
|
+
...update,
|
|
434
|
+
rows: update.rows.map((row) => ({
|
|
435
|
+
...row,
|
|
436
|
+
key:
|
|
437
|
+
row.key ??
|
|
438
|
+
(row.kind === 'scope'
|
|
439
|
+
? `scope:${relativeFile(row.path)}`
|
|
440
|
+
: byPosition.get(`${row.unitId}:${row.stepNumber}`)),
|
|
441
|
+
artifacts: strings(row.artifacts, 'artifacts').map((file) => `${runId}:${file}`),
|
|
442
|
+
})),
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function reviewCoverageSourcePaths(scopePaths, baseline, update) {
|
|
447
|
+
return [
|
|
448
|
+
...new Set([
|
|
449
|
+
...scopePaths,
|
|
450
|
+
...(baseline?.rows.flatMap((row) => Object.keys(row.sourceHashes)) ?? []),
|
|
451
|
+
...update.rows.flatMap((row) => strings(row.sources, 'sources')),
|
|
452
|
+
]),
|
|
453
|
+
].map(relativeFile);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/** Used by the marker and by the accepted-result verifier, with independently read hashes. */
|
|
457
|
+
function composeReviewArtifacts(
|
|
458
|
+
policy,
|
|
459
|
+
definitions,
|
|
460
|
+
result,
|
|
461
|
+
rawReport,
|
|
462
|
+
baseline,
|
|
463
|
+
sourceHashes,
|
|
464
|
+
artifactHashes,
|
|
465
|
+
baselineEvidenceHashes = policy.baseline?.evidenceHashes ?? {},
|
|
466
|
+
) {
|
|
467
|
+
const normalized = normalizeCoverageUpdate(result.coverage, definitions, policy.runId);
|
|
468
|
+
const evidenceHashes = { ...baselineEvidenceHashes };
|
|
469
|
+
for (const file of reviewCoverageEvidencePaths(result.coverage))
|
|
470
|
+
evidenceHashes[`${policy.runId}:${file}`] = artifactHashes[file] ?? 'missing';
|
|
471
|
+
const index = composeCoverageRows(
|
|
472
|
+
{ ...policy, definitions, sourceHashes, evidenceHashes },
|
|
473
|
+
normalized,
|
|
474
|
+
baseline,
|
|
475
|
+
);
|
|
476
|
+
if (
|
|
477
|
+
index.rows.some((row) => row.value.outcome === 'FINDING') &&
|
|
478
|
+
(!Array.isArray(result.issues) || !result.issues.length)
|
|
479
|
+
)
|
|
480
|
+
throw new Error('Coverage findings require current typed issues; reuse cannot clear them');
|
|
481
|
+
const rendered = renderCoverage(index);
|
|
482
|
+
const prose = stripScopeSection(rawReport);
|
|
483
|
+
const report = `${prose}\n\n## Diff scope\n\n${rendered.scope}\n`;
|
|
484
|
+
index.reportSha256 = sha256(report);
|
|
485
|
+
index.ledgerSha256 = sha256(rendered.ledger);
|
|
486
|
+
index.compositionFingerprint = sha256(
|
|
487
|
+
JSON.stringify({
|
|
488
|
+
policy,
|
|
489
|
+
normalized,
|
|
490
|
+
definitions,
|
|
491
|
+
prose,
|
|
492
|
+
issues: result.issues,
|
|
493
|
+
artifactHashes,
|
|
494
|
+
}),
|
|
495
|
+
);
|
|
496
|
+
return { index, report, ledger: rendered.ledger };
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** Existing mark terminal paths call this before their artifact checks and hashes. */
|
|
500
|
+
function materializeReviewCoverage(taskDir) {
|
|
501
|
+
const policy = readCoveragePolicy(
|
|
502
|
+
readReviewTaskFile(taskDir, 'inputs/instruction-manifest.json') ?? '{}',
|
|
503
|
+
);
|
|
504
|
+
if (!policy) return null; // Older tasks keep their frozen report contract.
|
|
505
|
+
if (policy.schemaVersion !== 1 || !Array.isArray(policy.scopePaths))
|
|
506
|
+
throw new Error('Invalid frozen review coverage policy');
|
|
507
|
+
const result = JSON.parse(readReviewTaskFile(taskDir, 'artifacts/review-result.json') ?? 'null');
|
|
508
|
+
if (!object(result)) throw new Error('Current structured review result is missing');
|
|
509
|
+
const update = result.coverage;
|
|
510
|
+
if (!object(update) || !Array.isArray(update.rows))
|
|
511
|
+
throw new Error('Review result requires coverage rows');
|
|
512
|
+
const definitions = coverageDefinitions(taskDir, policy);
|
|
513
|
+
const normalized = normalizeCoverageUpdate(update, definitions, policy.runId);
|
|
514
|
+
const artifactHashes = Object.fromEntries(
|
|
515
|
+
reviewCoverageEvidencePaths(update).map((file) => {
|
|
516
|
+
const bytes = readReviewTaskFile(taskDir, file, null);
|
|
517
|
+
return [file, bytes === undefined ? 'missing' : sha256(bytes)];
|
|
518
|
+
}),
|
|
519
|
+
);
|
|
520
|
+
const priorBytes = policy.baseline
|
|
521
|
+
? readReviewTaskFile(taskDir, 'inputs/prior-review-coverage.json')
|
|
522
|
+
: undefined;
|
|
523
|
+
const baseline = readCoverageIndex(priorBytes, policy.baseline);
|
|
524
|
+
const rawReport = readReviewTaskFile(taskDir, 'artifacts/review.md') ?? '';
|
|
525
|
+
if (!rawReport.trim())
|
|
526
|
+
throw new Error('Current review report must be authored before coverage composition');
|
|
527
|
+
const prose = stripScopeSection(rawReport);
|
|
528
|
+
const fingerprint = sha256(
|
|
529
|
+
JSON.stringify({
|
|
530
|
+
policy,
|
|
531
|
+
normalized,
|
|
532
|
+
definitions,
|
|
533
|
+
prose,
|
|
534
|
+
issues: result.issues,
|
|
535
|
+
artifactHashes,
|
|
536
|
+
}),
|
|
537
|
+
);
|
|
538
|
+
const outputFile = path.join(taskDir, 'artifacts/review-coverage.json');
|
|
539
|
+
const cachedBytes = readReviewTaskFile(taskDir, 'artifacts/review-coverage.json');
|
|
540
|
+
const cached = cachedBytes ? JSON.parse(cachedBytes) : undefined;
|
|
541
|
+
const oldLedger = readReviewTaskFile(taskDir, 'artifacts/review-ledger.md');
|
|
542
|
+
const actualHead = execFileSync('git', ['rev-parse', 'HEAD'], {
|
|
543
|
+
cwd: policy.repo,
|
|
544
|
+
encoding: 'utf8',
|
|
545
|
+
}).trim();
|
|
546
|
+
if (actualHead !== policy.headSha)
|
|
547
|
+
throw new Error('Review source head changed before coverage composition');
|
|
548
|
+
if (
|
|
549
|
+
cached?.compositionFingerprint === fingerprint &&
|
|
550
|
+
cached.reportSha256 === sha256(rawReport) &&
|
|
551
|
+
oldLedger !== undefined &&
|
|
552
|
+
cached.ledgerSha256 === sha256(oldLedger)
|
|
553
|
+
)
|
|
554
|
+
return cached;
|
|
555
|
+
const sourcePaths = reviewCoverageSourcePaths(policy.scopePaths, baseline, update);
|
|
556
|
+
const sourceHashes = coverageSourceHashes(
|
|
557
|
+
policy.repo,
|
|
558
|
+
policy.headSha,
|
|
559
|
+
sourcePaths,
|
|
560
|
+
policy.scopePaths,
|
|
561
|
+
);
|
|
562
|
+
const { index, report, ledger } = composeReviewArtifacts(
|
|
563
|
+
policy,
|
|
564
|
+
definitions,
|
|
565
|
+
result,
|
|
566
|
+
rawReport,
|
|
567
|
+
baseline,
|
|
568
|
+
sourceHashes,
|
|
569
|
+
artifactHashes,
|
|
570
|
+
);
|
|
571
|
+
// The task's output parent is fixed; never follow a reviewer-created directory link.
|
|
572
|
+
const parent = fs.realpathSync(path.join(taskDir, 'artifacts'));
|
|
573
|
+
if (path.relative(fs.realpathSync(taskDir), parent) !== 'artifacts')
|
|
574
|
+
throw new Error('Review coverage output directory changed');
|
|
575
|
+
writeCoverageFile(path.join(taskDir, 'artifacts/review.md'), report);
|
|
576
|
+
writeCoverageFile(path.join(taskDir, 'artifacts/review-ledger.md'), ledger);
|
|
577
|
+
writeCoverageFile(outputFile, JSON.stringify(index, null, 2) + '\n');
|
|
578
|
+
return index;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
module.exports = {
|
|
582
|
+
sha256,
|
|
583
|
+
coverageRowKey,
|
|
584
|
+
readCoverageIndex,
|
|
585
|
+
reusableCoverageRows,
|
|
586
|
+
composeCoverageRows,
|
|
587
|
+
renderCoverage,
|
|
588
|
+
relativeFile,
|
|
589
|
+
coverageDefinitions,
|
|
590
|
+
coverageSourceHashes,
|
|
591
|
+
parseCoverageSourceHashes,
|
|
592
|
+
reviewCoverageEvidencePaths,
|
|
593
|
+
reviewCoverageSourcePaths,
|
|
594
|
+
readReviewTaskFile,
|
|
595
|
+
readCoveragePolicy,
|
|
596
|
+
composeReviewArtifacts,
|
|
597
|
+
materializeReviewCoverage,
|
|
598
|
+
};
|