@contextrail/assess 0.0.0-stage → 1.0.0-alpha.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/LICENSE +202 -0
- package/README.md +82 -2
- package/assets/brand/icon-dark.png +0 -0
- package/assets/brand/icon-light.png +0 -0
- package/dist/assess.d.ts +109 -0
- package/dist/assess.js +1548 -0
- package/dist/assessment.d.ts +566 -0
- package/dist/assessment.js +404 -0
- package/dist/budget.d.ts +24 -0
- package/dist/budget.js +52 -0
- package/dist/candidate.d.ts +147 -0
- package/dist/candidate.js +321 -0
- package/dist/change-summary.d.ts +33 -0
- package/dist/change-summary.js +26 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +406 -0
- package/dist/contracts.d.ts +99 -0
- package/dist/contracts.js +62 -0
- package/dist/diagnostics.d.ts +44 -0
- package/dist/diagnostics.js +412 -0
- package/dist/evidence.d.ts +73 -0
- package/dist/evidence.js +297 -0
- package/dist/feedback.d.ts +21 -0
- package/dist/feedback.js +140 -0
- package/dist/git.d.ts +40 -0
- package/dist/git.js +207 -0
- package/dist/github.d.ts +90 -0
- package/dist/github.js +1342 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +14 -0
- package/dist/mcp.d.ts +28 -0
- package/dist/mcp.js +173 -0
- package/dist/pack.d.ts +160 -0
- package/dist/pack.js +377 -0
- package/dist/phases.d.ts +65 -0
- package/dist/phases.js +63 -0
- package/dist/planning.d.ts +64 -0
- package/dist/planning.js +135 -0
- package/dist/pr-context.d.ts +316 -0
- package/dist/pr-context.js +198 -0
- package/dist/prepare.d.ts +71 -0
- package/dist/prepare.js +335 -0
- package/dist/presentation.d.ts +309 -0
- package/dist/presentation.js +1026 -0
- package/dist/provider-diagnostics.d.ts +93 -0
- package/dist/provider-diagnostics.js +233 -0
- package/dist/providers.d.ts +45 -0
- package/dist/providers.js +291 -0
- package/dist/report-contract.d.ts +132 -0
- package/dist/report-contract.js +240 -0
- package/dist/results.d.ts +90 -0
- package/dist/results.js +128 -0
- package/dist/resume.d.ts +25 -0
- package/dist/resume.js +116 -0
- package/dist/source.d.ts +167 -0
- package/dist/source.js +675 -0
- package/dist/verification.d.ts +386 -0
- package/dist/verification.js +168 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +9 -0
- package/docs/README.md +24 -0
- package/docs/action-reference.md +85 -0
- package/docs/architecture.md +156 -0
- package/docs/configuration.md +83 -0
- package/docs/contributing.md +46 -0
- package/docs/getting-started.md +92 -0
- package/docs/github-actions.md +141 -0
- package/docs/operations.md +203 -0
- package/docs/review-results.md +173 -0
- package/docs/security.md +64 -0
- package/docs/standards.md +62 -0
- package/examples/assess-pull-request.yml +109 -0
- package/package.json +56 -5
package/dist/github.js
ADDED
|
@@ -0,0 +1,1342 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { appendFileSync, existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { join, resolve } from 'node:path';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { digest, preparationManifest } from './prepare.js';
|
|
7
|
+
import { validatePhaseRecord } from './phases.js';
|
|
8
|
+
import { phaseItems } from './change-summary.js';
|
|
9
|
+
import { revalidateCandidate, validateConcern, projectCandidates } from './candidate.js';
|
|
10
|
+
import { EvidenceLedger } from './evidence.js';
|
|
11
|
+
import { reconcileFindings } from './assessment.js';
|
|
12
|
+
import { object, groupDeveloperFindings, developerFindingLocations, developerFindingGroupLabel, objects, string, markdownText, readHistory, renderAssessmentComment, renderActionSummary, assessmentMarker, runRecord, summarizeUsage, withHistory, } from './presentation.js';
|
|
13
|
+
const sha = z.string().regex(/^[a-f0-9]{40}$/);
|
|
14
|
+
const githubInputSchema = z
|
|
15
|
+
.object({
|
|
16
|
+
version: z.literal(1),
|
|
17
|
+
repository: z.string().regex(/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/),
|
|
18
|
+
pr: z.number().int().positive(),
|
|
19
|
+
reviewer: z.string().startsWith('skill://'),
|
|
20
|
+
base: sha,
|
|
21
|
+
targetBase: sha.optional(),
|
|
22
|
+
head: sha,
|
|
23
|
+
baseBranch: z.string().default(''),
|
|
24
|
+
githubRun: z
|
|
25
|
+
.string()
|
|
26
|
+
.regex(/^[1-9][0-9]{0,19}$/)
|
|
27
|
+
.optional(),
|
|
28
|
+
githubAttempt: z.number().int().positive().optional(),
|
|
29
|
+
})
|
|
30
|
+
.strict();
|
|
31
|
+
class GitHubAdapterError extends Error {
|
|
32
|
+
code;
|
|
33
|
+
constructor(code) {
|
|
34
|
+
super(code);
|
|
35
|
+
this.code = code;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const json = (file) => JSON.parse(readFileSync(file, 'utf8'));
|
|
39
|
+
const write = (file, value) => writeFileSync(file, JSON.stringify(value, null, 2) + '\n', { mode: 0o600 });
|
|
40
|
+
const fail = (code) => {
|
|
41
|
+
throw new GitHubAdapterError(code);
|
|
42
|
+
};
|
|
43
|
+
const validPath = (p) => typeof p === 'string' &&
|
|
44
|
+
p.length > 0 &&
|
|
45
|
+
!p.startsWith('/') &&
|
|
46
|
+
!/\p{Cc}/u.test(p) &&
|
|
47
|
+
!p
|
|
48
|
+
.split('/')
|
|
49
|
+
.some((s) => !s ||
|
|
50
|
+
s === '.' ||
|
|
51
|
+
s === '..' ||
|
|
52
|
+
/^\.env(?:\.|$)|^(?:credentials|secrets)(?:\.|$)|\.(?:pem|key|p12|pfx)$/i.test(s));
|
|
53
|
+
const hash = (value) => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
54
|
+
const bot = (c) => object(c.user).login === 'github-actions[bot]' && object(c.user).type === 'Bot';
|
|
55
|
+
function gitEnvironment() {
|
|
56
|
+
return Object.fromEntries(['PATH', 'HOME', 'TMPDIR', 'SYSTEMROOT'].filter((k) => process.env[k]).map((k) => [k, process.env[k]]));
|
|
57
|
+
}
|
|
58
|
+
function git(repo, args, token) {
|
|
59
|
+
const env = {
|
|
60
|
+
...gitEnvironment(),
|
|
61
|
+
GIT_CONFIG_NOSYSTEM: '1',
|
|
62
|
+
GIT_CONFIG_GLOBAL: '/dev/null',
|
|
63
|
+
GIT_ATTR_NOSYSTEM: '1',
|
|
64
|
+
GIT_NO_REPLACE_OBJECTS: '1',
|
|
65
|
+
GIT_NO_LAZY_FETCH: '1',
|
|
66
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
67
|
+
GIT_OPTIONAL_LOCKS: '0',
|
|
68
|
+
...(token
|
|
69
|
+
? {
|
|
70
|
+
GIT_CONFIG_COUNT: '1',
|
|
71
|
+
GIT_CONFIG_KEY_0: 'http.https://github.com/.extraheader',
|
|
72
|
+
GIT_CONFIG_VALUE_0: `AUTHORIZATION: basic ${Buffer.from(`x-access-token:${token}`).toString('base64')}`,
|
|
73
|
+
}
|
|
74
|
+
: {}),
|
|
75
|
+
};
|
|
76
|
+
const out = spawnSync('git', [
|
|
77
|
+
'--no-pager',
|
|
78
|
+
'-c',
|
|
79
|
+
'core.hooksPath=/dev/null',
|
|
80
|
+
'-c',
|
|
81
|
+
'core.fsmonitor=false',
|
|
82
|
+
'-c',
|
|
83
|
+
'credential.helper=',
|
|
84
|
+
'-c',
|
|
85
|
+
'protocol.file.allow=never',
|
|
86
|
+
...(repo ? ['-C', repo] : []),
|
|
87
|
+
...args,
|
|
88
|
+
], { env, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 120000, maxBuffer: 8 * 1024 * 1024 });
|
|
89
|
+
if (out.status !== 0)
|
|
90
|
+
fail('git_operation_failed');
|
|
91
|
+
return out.stdout;
|
|
92
|
+
}
|
|
93
|
+
async function readGitHubResponse(response, method) {
|
|
94
|
+
const failure = method === 'GET' ? 'github_read_failed' : 'github_write_unknown';
|
|
95
|
+
const reader = response.body?.getReader();
|
|
96
|
+
if (!reader)
|
|
97
|
+
return fail(failure);
|
|
98
|
+
let size = 0;
|
|
99
|
+
const chunks = [];
|
|
100
|
+
try {
|
|
101
|
+
while (true) {
|
|
102
|
+
const { done, value } = await reader.read();
|
|
103
|
+
if (done)
|
|
104
|
+
break;
|
|
105
|
+
size += value.byteLength;
|
|
106
|
+
if (size > 8 * 1024 * 1024) {
|
|
107
|
+
await reader.cancel();
|
|
108
|
+
fail('github_response_too_large');
|
|
109
|
+
}
|
|
110
|
+
chunks.push(value);
|
|
111
|
+
}
|
|
112
|
+
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return fail(failure);
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
reader.releaseLock();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
export function githubRequester(repository, token, fetcher = fetch) {
|
|
122
|
+
if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repository) || !token)
|
|
123
|
+
fail('github_configuration_invalid');
|
|
124
|
+
return async (path, method = 'GET', body) => {
|
|
125
|
+
if (path !== 'graphql' && (!path.startsWith('/') || path.startsWith('//') || path.split(/[/?]/).includes('..')))
|
|
126
|
+
fail('github_path_invalid');
|
|
127
|
+
let response;
|
|
128
|
+
try {
|
|
129
|
+
response = await fetcher(path === 'graphql'
|
|
130
|
+
? 'https://api.github.com/graphql'
|
|
131
|
+
: `https://api.github.com/repos/${repository}${path}`, {
|
|
132
|
+
method,
|
|
133
|
+
headers: {
|
|
134
|
+
Authorization: `Bearer ${token}`,
|
|
135
|
+
Accept: 'application/vnd.github+json',
|
|
136
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
137
|
+
...(body ? { 'Content-Type': 'application/json' } : {}),
|
|
138
|
+
},
|
|
139
|
+
...(body ? { body: JSON.stringify(body) } : {}),
|
|
140
|
+
signal: AbortSignal.timeout(30000),
|
|
141
|
+
redirect: 'error',
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return fail(method === 'GET' ? 'github_read_failed' : 'github_write_unknown');
|
|
146
|
+
}
|
|
147
|
+
if (!response.ok)
|
|
148
|
+
fail(method === 'GET' ? 'github_read_failed' : `github_write_${response.status}`);
|
|
149
|
+
return readGitHubResponse(response, method);
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
async function listAll(request, path, maxPages = 10) {
|
|
153
|
+
const rows = [];
|
|
154
|
+
for (let page = 1; page <= maxPages; page++) {
|
|
155
|
+
const result = await request(`${path}${path.includes('?') ? '&' : '?'}per_page=100&page=${page}`);
|
|
156
|
+
if (!Array.isArray(result))
|
|
157
|
+
throw new GitHubAdapterError('github_lookup_invalid');
|
|
158
|
+
rows.push(...objects(result));
|
|
159
|
+
if (result.length < 100)
|
|
160
|
+
return rows;
|
|
161
|
+
}
|
|
162
|
+
return fail('github_lookup_incomplete');
|
|
163
|
+
}
|
|
164
|
+
export async function findAssessmentComment(c, request) {
|
|
165
|
+
const candidates = (await listAll(request, `/issues/${c.pr}/comments`)).filter((row) => bot(row) &&
|
|
166
|
+
typeof row.id === 'number' &&
|
|
167
|
+
Number.isSafeInteger(row.id) &&
|
|
168
|
+
row.id > 0 &&
|
|
169
|
+
string(row.body).startsWith(assessmentMarker(c) + '\n'));
|
|
170
|
+
return candidates.sort((a, b) => Number(b.id) - Number(a.id))[0] ?? null;
|
|
171
|
+
}
|
|
172
|
+
function sameComparison(pr, c) {
|
|
173
|
+
const p = object(pr), base = object(p.base), head = object(p.head);
|
|
174
|
+
return (p.number === c.pr &&
|
|
175
|
+
object(base.repo).full_name === c.repository &&
|
|
176
|
+
(!c.baseBranch || base.ref === c.baseBranch) &&
|
|
177
|
+
base.sha === (c.targetBase ?? c.base) &&
|
|
178
|
+
head.sha === c.head);
|
|
179
|
+
}
|
|
180
|
+
const discussionQuery = `query($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){pullRequest(number:$number){baseRefOid headRefOid comments(last:20){pageInfo{hasPreviousPage} nodes{id body url createdAt updatedAt author{login} authorAssociation}} reviews(last:10,states:[APPROVED,CHANGES_REQUESTED,COMMENTED,DISMISSED]){pageInfo{hasPreviousPage} nodes{id body url submittedAt state author{login} authorAssociation}} dismissals:timelineItems(last:10,itemTypes:[REVIEW_DISMISSED_EVENT]){pageInfo{hasPreviousPage} nodes{... on ReviewDismissedEvent{id createdAt dismissalMessage actor{login} review{url}}}} reviewThreads(last:10){pageInfo{hasPreviousPage} nodes{id isResolved isOutdated path line resolvedBy{login} comments(last:5){pageInfo{hasPreviousPage} nodes{id body url createdAt updatedAt author{login} authorAssociation}}}}}}}`;
|
|
181
|
+
function discussionEntries(node) {
|
|
182
|
+
const entries = [];
|
|
183
|
+
let partial = false;
|
|
184
|
+
for (const [key, kind] of [
|
|
185
|
+
['comments', 'comment'],
|
|
186
|
+
['reviews', 'review'],
|
|
187
|
+
]) {
|
|
188
|
+
const connection = object(node[key]);
|
|
189
|
+
if (!Array.isArray(connection.nodes)) {
|
|
190
|
+
partial = true;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
partial ||= !!object(connection.pageInfo).hasPreviousPage;
|
|
194
|
+
entries.push(...objects(connection.nodes).map((entry) => ({ kind: kind, node: entry })));
|
|
195
|
+
}
|
|
196
|
+
const threads = object(node.reviewThreads);
|
|
197
|
+
if (!Array.isArray(threads.nodes))
|
|
198
|
+
partial = true;
|
|
199
|
+
partial ||= !!object(threads.pageInfo).hasPreviousPage;
|
|
200
|
+
for (const thread of objects(threads.nodes)) {
|
|
201
|
+
const comments = object(thread.comments);
|
|
202
|
+
partial ||= !!object(comments.pageInfo).hasPreviousPage;
|
|
203
|
+
entries.push(...objects(comments.nodes).map((entry) => ({ kind: 'thread', node: entry, thread })));
|
|
204
|
+
}
|
|
205
|
+
const dismissals = object(node.dismissals);
|
|
206
|
+
if (!Array.isArray(dismissals.nodes))
|
|
207
|
+
partial = true;
|
|
208
|
+
partial ||= !!object(dismissals.pageInfo).hasPreviousPage;
|
|
209
|
+
entries.push(...objects(dismissals.nodes).map((entry) => ({
|
|
210
|
+
kind: 'dismissal',
|
|
211
|
+
node: {
|
|
212
|
+
id: entry.id,
|
|
213
|
+
body: entry.dismissalMessage ?? '',
|
|
214
|
+
url: object(entry.review).url,
|
|
215
|
+
createdAt: entry.createdAt,
|
|
216
|
+
author: entry.actor,
|
|
217
|
+
state: 'DISMISSED',
|
|
218
|
+
},
|
|
219
|
+
})));
|
|
220
|
+
return { entries, partial };
|
|
221
|
+
}
|
|
222
|
+
function discussionThread(thread, clip) {
|
|
223
|
+
return {
|
|
224
|
+
resolved: thread ? !!thread.isResolved : null,
|
|
225
|
+
outdated: thread ? !!thread.isOutdated : null,
|
|
226
|
+
resolvedBy: clip(object(thread?.resolvedBy).login, 100),
|
|
227
|
+
path: clip(thread?.path, 500),
|
|
228
|
+
line: Number.isSafeInteger(thread?.line) && Number(thread?.line) > 0 ? thread?.line : null,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function discussionItem({ kind, node, thread }, c, clip) {
|
|
232
|
+
if (typeof node.body !== 'string')
|
|
233
|
+
return { item: null, partial: false };
|
|
234
|
+
if (object(node.author).login === 'github-actions[bot]' && node.body.startsWith('<!-- contextrail-assess:'))
|
|
235
|
+
return { item: null, partial: false };
|
|
236
|
+
const url = string(node.url);
|
|
237
|
+
if (!url.startsWith(`https://github.com/${c.repository}/`))
|
|
238
|
+
return { item: null, partial: true };
|
|
239
|
+
return {
|
|
240
|
+
partial: node.body.length > 1000,
|
|
241
|
+
item: {
|
|
242
|
+
kind,
|
|
243
|
+
id: clip(node.id, 200),
|
|
244
|
+
author: clip(object(node.author).login, 100),
|
|
245
|
+
association: clip(node.authorAssociation, 100),
|
|
246
|
+
body: clip(node.body),
|
|
247
|
+
url: clip(url),
|
|
248
|
+
at: clip(node.updatedAt ?? node.submittedAt ?? node.createdAt, 50),
|
|
249
|
+
state: clip(node.state, 50),
|
|
250
|
+
...discussionThread(thread, clip),
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
async function captureDiscussion(c, request, clip, limitations) {
|
|
255
|
+
try {
|
|
256
|
+
const [owner, name] = c.repository.split('/');
|
|
257
|
+
const raw = object(await request('graphql', 'POST', { query: discussionQuery, variables: { owner, name, number: c.pr } }));
|
|
258
|
+
const node = object(object(object(raw.data).repository).pullRequest);
|
|
259
|
+
if (node.baseRefOid !== (c.targetBase ?? c.base) || node.headRefOid !== c.head)
|
|
260
|
+
fail('context_comparison_changed');
|
|
261
|
+
const collected = discussionEntries(node), rows = collected.entries.map((entry) => discussionItem(entry, c, clip));
|
|
262
|
+
const items = rows.flatMap(({ item }) => (item ? [item] : []));
|
|
263
|
+
const partial = (Array.isArray(raw.errors) && raw.errors.length > 0) ||
|
|
264
|
+
collected.partial ||
|
|
265
|
+
rows.some((row) => row.partial) ||
|
|
266
|
+
items.length > 80;
|
|
267
|
+
if (partial)
|
|
268
|
+
limitations.push('Discussion is partial: recent bounded comments, reviews, dismissals and threads; at most 80 excerpts capped at 1000 characters.');
|
|
269
|
+
return { status: partial ? 'partial' : 'available', items: items.slice(-80) };
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
limitations.push('PR discussion unavailable or comparison changed; no conversation completeness is claimed.');
|
|
273
|
+
return { status: 'unavailable', items: [] };
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const unavailableChanges = (base, head) => ({
|
|
277
|
+
status: 'unavailable',
|
|
278
|
+
base,
|
|
279
|
+
head,
|
|
280
|
+
relation: 'unknown',
|
|
281
|
+
files: [],
|
|
282
|
+
});
|
|
283
|
+
async function changesSincePrevious(previous, c, request, clip, limitations) {
|
|
284
|
+
if (previous === c.head)
|
|
285
|
+
return { status: 'same_head', base: previous, head: c.head, relation: 'identical', files: [] };
|
|
286
|
+
if (!previous)
|
|
287
|
+
return unavailableChanges(null, c.head);
|
|
288
|
+
try {
|
|
289
|
+
const comparison = object(await request(`/compare/${previous}...${c.head}?per_page=1`));
|
|
290
|
+
if (!Array.isArray(comparison.files) ||
|
|
291
|
+
!['ahead', 'behind', 'diverged', 'identical'].includes(string(comparison.status)))
|
|
292
|
+
return fail('comparison_unavailable');
|
|
293
|
+
const linear = !['behind', 'diverged'].includes(string(comparison.status));
|
|
294
|
+
if (!linear)
|
|
295
|
+
limitations.push('Previous/current heads are not a linear forward increment; path comparison is not the full review.');
|
|
296
|
+
return {
|
|
297
|
+
status: comparison.files.length > 100 || !linear ? 'partial' : 'available',
|
|
298
|
+
base: previous,
|
|
299
|
+
head: c.head,
|
|
300
|
+
relation: comparison.status,
|
|
301
|
+
files: objects(comparison.files)
|
|
302
|
+
.slice(0, 100)
|
|
303
|
+
.map((file) => ({ path: clip(file.filename, 500), status: clip(file.status, 50) })),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
limitations.push('Changes since previous assessment are unavailable; current full comparison still applies.');
|
|
308
|
+
return unavailableChanges(previous, c.head);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
async function retainedHistory(c, request, includeHistory, limitations) {
|
|
312
|
+
if (!includeHistory)
|
|
313
|
+
return null;
|
|
314
|
+
try {
|
|
315
|
+
return readHistory((await findAssessmentComment(c, request))?.body, c);
|
|
316
|
+
}
|
|
317
|
+
catch {
|
|
318
|
+
limitations.push('Bot comment history could not be completely read.');
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
// Metadata receipts only: no check output, annotations, logs, URLs or status descriptions.
|
|
323
|
+
async function captureObservedChecks(c, request, clip) {
|
|
324
|
+
const checkRuns = async () => {
|
|
325
|
+
try {
|
|
326
|
+
const raw = object(await request(`/commits/${c.head}/check-runs?filter=latest&per_page=50`));
|
|
327
|
+
if (!Array.isArray(raw.check_runs) || !Number.isInteger(raw.total_count) || Number(raw.total_count) < 0)
|
|
328
|
+
return { status: 'unavailable', items: [] };
|
|
329
|
+
const rows = objects(raw.check_runs);
|
|
330
|
+
const valid = rows.filter((row) => row.head_sha === c.head &&
|
|
331
|
+
typeof row.name === 'string' &&
|
|
332
|
+
typeof row.status === 'string' &&
|
|
333
|
+
(row.conclusion === null || typeof row.conclusion === 'string'));
|
|
334
|
+
return {
|
|
335
|
+
status: Number(raw.total_count) !== rows.length ||
|
|
336
|
+
rows.length > 50 ||
|
|
337
|
+
valid.length !== raw.check_runs.length
|
|
338
|
+
? 'partial'
|
|
339
|
+
: 'available',
|
|
340
|
+
items: valid.slice(0, 50).map((row) => ({
|
|
341
|
+
name: clip(row.name, 200),
|
|
342
|
+
status: clip(row.status, 50),
|
|
343
|
+
conclusion: row.conclusion === null ? null : clip(row.conclusion, 50),
|
|
344
|
+
})),
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
catch {
|
|
348
|
+
return { status: 'unavailable', items: [] };
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
const commitStatuses = async () => {
|
|
352
|
+
try {
|
|
353
|
+
const raw = object(await request(`/commits/${c.head}/status?per_page=50`));
|
|
354
|
+
if (raw.sha !== c.head ||
|
|
355
|
+
!Array.isArray(raw.statuses) ||
|
|
356
|
+
!Number.isInteger(raw.total_count) ||
|
|
357
|
+
Number(raw.total_count) < 0)
|
|
358
|
+
return { status: 'unavailable', items: [] };
|
|
359
|
+
const rows = objects(raw.statuses);
|
|
360
|
+
const valid = rows.filter((row) => typeof row.context === 'string' && typeof row.state === 'string');
|
|
361
|
+
return {
|
|
362
|
+
status: Number(raw.total_count) !== rows.length || rows.length > 50 || valid.length !== raw.statuses.length
|
|
363
|
+
? 'partial'
|
|
364
|
+
: 'available',
|
|
365
|
+
items: valid
|
|
366
|
+
.slice(0, 50)
|
|
367
|
+
.map((row) => ({ context: clip(row.context, 200), state: clip(row.state, 50) })),
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
catch {
|
|
371
|
+
return { status: 'unavailable', items: [] };
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
const [checks, statuses] = await Promise.all([checkRuns(), commitStatuses()]);
|
|
375
|
+
return {
|
|
376
|
+
source: 'github_api',
|
|
377
|
+
head: c.head,
|
|
378
|
+
capturedAt: new Date().toISOString(),
|
|
379
|
+
checkRuns: checks,
|
|
380
|
+
commitStatuses: statuses,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
// The Action context script loads this public entry dynamically through loadAssessModule.
|
|
384
|
+
export async function collectGitHubContext(c, pr, request, includeHistory, token = '') {
|
|
385
|
+
const p = object(pr), limitations = [
|
|
386
|
+
'PR discussions and hidden bot state are mutable, untrusted data, not permission, approval or proof of a fix.',
|
|
387
|
+
];
|
|
388
|
+
if (string(p.title).length > 1000)
|
|
389
|
+
limitations.push('PR title truncated to 1000 characters.');
|
|
390
|
+
if (string(p.body).length > 20000)
|
|
391
|
+
limitations.push('PR body truncated to 20000 characters; omitted intent was not supplied.');
|
|
392
|
+
const clip = (value, limit = 1000) => {
|
|
393
|
+
let text = string(value).replace(/\p{Cc}/gu, ' ');
|
|
394
|
+
if (token.length >= 8)
|
|
395
|
+
text = text.split(token).join('[REDACTED]');
|
|
396
|
+
return text.slice(0, limit);
|
|
397
|
+
};
|
|
398
|
+
const history = await retainedHistory(c, request, includeHistory, limitations), previous = history?.assessment?.head;
|
|
399
|
+
const context = {
|
|
400
|
+
version: 2,
|
|
401
|
+
repository: c.repository,
|
|
402
|
+
pr: c.pr,
|
|
403
|
+
reviewer: c.reviewer,
|
|
404
|
+
capturedAt: new Date().toISOString(),
|
|
405
|
+
comparison: { base: c.base, head: c.head },
|
|
406
|
+
intent: { title: clip(p.title, 1000), body: clip(p.body, 20000), source: 'github_pull_request' },
|
|
407
|
+
observedChecks: await captureObservedChecks(c, request, clip),
|
|
408
|
+
history: history
|
|
409
|
+
? { status: 'available', runs: history.runs, omitted: history.omitted, assessment: history.assessment }
|
|
410
|
+
: { status: 'unavailable', runs: [], omitted: 0, assessment: null },
|
|
411
|
+
discussion: { status: 'unavailable', items: [] },
|
|
412
|
+
changesSincePrevious: unavailableChanges(previous ?? null, c.head),
|
|
413
|
+
limitations,
|
|
414
|
+
};
|
|
415
|
+
if (!history)
|
|
416
|
+
limitations.push('No valid retained history is available; earlier reviews may exist.');
|
|
417
|
+
if (!includeHistory) {
|
|
418
|
+
limitations.push('Discussion/history collection was not requested.');
|
|
419
|
+
return context;
|
|
420
|
+
}
|
|
421
|
+
context.discussion = await captureDiscussion(c, request, clip, limitations);
|
|
422
|
+
context.changesSincePrevious = await changesSincePrevious(previous, c, request, clip, limitations);
|
|
423
|
+
if (Buffer.byteLength(JSON.stringify(context)) > 200 * 1024) {
|
|
424
|
+
context.discussion = { status: 'unavailable', items: [] };
|
|
425
|
+
limitations.push('Discussion omitted to keep contextual input within 200 KiB.');
|
|
426
|
+
}
|
|
427
|
+
if (Buffer.byteLength(JSON.stringify(context)) > 200 * 1024) {
|
|
428
|
+
context.changesSincePrevious = unavailableChanges(previous ?? null, c.head);
|
|
429
|
+
limitations.push('Changed-path details omitted to keep contextual input within 200 KiB.');
|
|
430
|
+
}
|
|
431
|
+
return context;
|
|
432
|
+
}
|
|
433
|
+
function createComparisonCheckout(outputDir, c, token) {
|
|
434
|
+
const root = resolve(outputDir);
|
|
435
|
+
if (existsSync(root)) {
|
|
436
|
+
const stat = lstatSync(root);
|
|
437
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || (stat.mode & 0o077) !== 0)
|
|
438
|
+
fail('unsafe_output_directory');
|
|
439
|
+
}
|
|
440
|
+
else
|
|
441
|
+
mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
442
|
+
const repository = join(realpathSync(root), 'comparison');
|
|
443
|
+
if (existsSync(repository))
|
|
444
|
+
fail('comparison_directory_exists');
|
|
445
|
+
git(null, ['init', '--template=', repository]);
|
|
446
|
+
git(repository, ['remote', 'add', 'origin', `https://github.com/${c.repository}.git`]);
|
|
447
|
+
git(repository, [
|
|
448
|
+
'fetch',
|
|
449
|
+
'--no-tags',
|
|
450
|
+
'--no-recurse-submodules',
|
|
451
|
+
'origin',
|
|
452
|
+
c.base,
|
|
453
|
+
`refs/pull/${c.pr}/head:refs/contextrail/assess-head`,
|
|
454
|
+
], token);
|
|
455
|
+
if (git(repository, ['rev-parse', '--verify', 'refs/contextrail/assess-head^{commit}']).trim() !== c.head)
|
|
456
|
+
fail('superseded_comparison');
|
|
457
|
+
for (const commit of [c.base, c.head])
|
|
458
|
+
if (git(repository, ['rev-parse', '--verify', `${commit}^{commit}`]).trim() !== commit)
|
|
459
|
+
fail('fixed_commit_invalid');
|
|
460
|
+
return { root, repository };
|
|
461
|
+
}
|
|
462
|
+
export async function prepareGitHubAssessment(options) {
|
|
463
|
+
const request = githubRequester(options.repository, options.token ?? process.env.GITHUB_TOKEN ?? '', options.fetch);
|
|
464
|
+
const pr = object(await request(`/pulls/${options.prNumber}`));
|
|
465
|
+
const c = githubInputSchema.parse({
|
|
466
|
+
version: 1,
|
|
467
|
+
repository: options.repository,
|
|
468
|
+
pr: options.prNumber,
|
|
469
|
+
reviewer: options.reviewer,
|
|
470
|
+
base: options.fixed?.base ?? string(object(pr.base).sha),
|
|
471
|
+
head: options.fixed?.head ?? string(object(pr.head).sha),
|
|
472
|
+
baseBranch: options.baseBranch ?? '',
|
|
473
|
+
...(process.env.GITHUB_RUN_ID
|
|
474
|
+
? { githubRun: process.env.GITHUB_RUN_ID, githubAttempt: Number(process.env.GITHUB_RUN_ATTEMPT ?? 1) }
|
|
475
|
+
: {}),
|
|
476
|
+
});
|
|
477
|
+
if (!sameComparison(pr, c))
|
|
478
|
+
fail('ineligible_pr');
|
|
479
|
+
const { root, repository } = createComparisonCheckout(options.outputDir, c, options.token ?? process.env.GITHUB_TOKEN);
|
|
480
|
+
if (!sameComparison(await request(`/pulls/${c.pr}`), c))
|
|
481
|
+
fail('superseded_comparison');
|
|
482
|
+
// PR evidence uses the common ancestor; freshness still binds the observed target tip.
|
|
483
|
+
const mergeBases = git(repository, ['merge-base', '--all', c.base, c.head]).trim().split(/\s+/u);
|
|
484
|
+
if (mergeBases.length !== 1 || !sha.safeParse(mergeBases[0]).success)
|
|
485
|
+
fail('comparison_merge_base_ambiguous');
|
|
486
|
+
c.targetBase = c.base;
|
|
487
|
+
c.base = mergeBases[0];
|
|
488
|
+
write(join(root, 'github-input.json'), c);
|
|
489
|
+
write(join(root, 'comparison.json'), {
|
|
490
|
+
base: c.base,
|
|
491
|
+
targetBase: c.targetBase,
|
|
492
|
+
head: c.head,
|
|
493
|
+
repository: c.repository,
|
|
494
|
+
pr: c.pr,
|
|
495
|
+
freshness: 'current_before_assessment',
|
|
496
|
+
});
|
|
497
|
+
const context = await collectGitHubContext(c, pr, request, !!options.collectPrContext, options.token ?? process.env.GITHUB_TOKEN ?? '');
|
|
498
|
+
write(join(root, 'pr-context.json'), context);
|
|
499
|
+
return {
|
|
500
|
+
exitCode: 0,
|
|
501
|
+
directory: root,
|
|
502
|
+
repository,
|
|
503
|
+
base: c.base,
|
|
504
|
+
targetBase: c.targetBase,
|
|
505
|
+
head: c.head,
|
|
506
|
+
prContextPath: join(root, 'pr-context.json'),
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
export function parseDiffAnchors(diff) {
|
|
510
|
+
const anchors = new Map();
|
|
511
|
+
let oldPath = '', newPath = '', oldLine = 0, newLine = 0, inHunk = false;
|
|
512
|
+
const add = (path, side, line) => {
|
|
513
|
+
if (!validPath(path) || line < 1)
|
|
514
|
+
return;
|
|
515
|
+
const key = `${path}\0${side}`;
|
|
516
|
+
const set = anchors.get(key) ?? new Set();
|
|
517
|
+
set.add(line);
|
|
518
|
+
anchors.set(key, set);
|
|
519
|
+
};
|
|
520
|
+
for (const line of diff.split('\n')) {
|
|
521
|
+
if (line.startsWith('diff --git ')) {
|
|
522
|
+
oldPath = '';
|
|
523
|
+
newPath = '';
|
|
524
|
+
inHunk = false;
|
|
525
|
+
}
|
|
526
|
+
else if (line.startsWith('--- ')) {
|
|
527
|
+
oldPath = line.slice(4).replace(/^a\//, '');
|
|
528
|
+
inHunk = false;
|
|
529
|
+
}
|
|
530
|
+
else if (line.startsWith('+++ ')) {
|
|
531
|
+
newPath = line.slice(4).replace(/^b\//, '');
|
|
532
|
+
inHunk = false;
|
|
533
|
+
}
|
|
534
|
+
else if (line.startsWith('@@ ')) {
|
|
535
|
+
const h = line.match(/^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
|
|
536
|
+
if (h) {
|
|
537
|
+
oldLine = Number(h[1]);
|
|
538
|
+
newLine = Number(h[2]);
|
|
539
|
+
inHunk = true;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
else if (inHunk && line.startsWith('-')) {
|
|
543
|
+
add(oldPath, 'LEFT', oldLine++);
|
|
544
|
+
}
|
|
545
|
+
else if (inHunk && line.startsWith('+')) {
|
|
546
|
+
add(newPath, 'RIGHT', newLine++);
|
|
547
|
+
}
|
|
548
|
+
else if (inHunk && line.startsWith(' ')) {
|
|
549
|
+
add(oldPath, 'LEFT', oldLine++);
|
|
550
|
+
add(newPath, 'RIGHT', newLine++);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return anchors;
|
|
554
|
+
}
|
|
555
|
+
const diffSide = (side) => side === 'base' ? 'LEFT' : side === 'head' ? 'RIGHT' : null;
|
|
556
|
+
const findingMarker = /<!-- contextrail-finding:([a-f0-9]{64}) -->/;
|
|
557
|
+
const inlineClassification = (finding) => ['binding_violation', 'behavioral_defect'].includes(string(finding.classification));
|
|
558
|
+
const publishableFinding = (finding) => finding.verified === true && inlineClassification(finding);
|
|
559
|
+
const sortedJson = (a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b), 'en');
|
|
560
|
+
const findingIds = (finding, key) => Array.isArray(finding[key]) ? finding[key] : [];
|
|
561
|
+
function coversRange(anchors, path, start, end) {
|
|
562
|
+
return Array.from({ length: end - start + 1 }, (_, i) => start + i).every((line) => anchors.get(`${path}\0RIGHT`)?.has(line));
|
|
563
|
+
}
|
|
564
|
+
function existingSuggestionRange(comment, currentHead) {
|
|
565
|
+
if (!bot(comment) || !findingMarker.test(string(comment.body)) || !string(comment.body).includes('```suggestion\n'))
|
|
566
|
+
return null;
|
|
567
|
+
if (comment.commit_id !== currentHead ||
|
|
568
|
+
comment.side !== 'RIGHT' ||
|
|
569
|
+
(comment.start_side && comment.start_side !== 'RIGHT'))
|
|
570
|
+
return null;
|
|
571
|
+
const path = string(comment.path), start = Number(comment.start_line ?? comment.line), end = Number(comment.line);
|
|
572
|
+
if (!validPath(path) || !Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 1 || end < start)
|
|
573
|
+
return null;
|
|
574
|
+
return { path, start, end };
|
|
575
|
+
}
|
|
576
|
+
function occupyExistingSuggestion(raw, context, currentHead) {
|
|
577
|
+
const range = existingSuggestionRange(object(raw), currentHead);
|
|
578
|
+
if (!range)
|
|
579
|
+
return;
|
|
580
|
+
const { path, start, end } = range;
|
|
581
|
+
try {
|
|
582
|
+
const before = context
|
|
583
|
+
.source('head', path)
|
|
584
|
+
.text.split('\n')
|
|
585
|
+
.slice(start - 1, end);
|
|
586
|
+
if (before.length !== end - start + 1 || !coversRange(context.anchors, path, start, end))
|
|
587
|
+
return;
|
|
588
|
+
const occupied = context.occupied.get(path) ?? [];
|
|
589
|
+
occupied.push([start, end]);
|
|
590
|
+
context.occupied.set(path, occupied);
|
|
591
|
+
}
|
|
592
|
+
catch {
|
|
593
|
+
// Unreadable source cannot establish a current suggestion range.
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
function inlineContext(input, evidenceInput, options) {
|
|
598
|
+
const state = object(input);
|
|
599
|
+
const context = {
|
|
600
|
+
state,
|
|
601
|
+
evidence: object(evidenceInput),
|
|
602
|
+
entries: objects(state.entries),
|
|
603
|
+
reviewer: object(state.reviewer),
|
|
604
|
+
anchors: parseDiffAnchors(options.diff),
|
|
605
|
+
source: options.source,
|
|
606
|
+
seen: new Set(),
|
|
607
|
+
occupied: new Map(),
|
|
608
|
+
};
|
|
609
|
+
const currentHead = string(object(object(state.artifact).head).commit);
|
|
610
|
+
for (const raw of options.existingComments ?? []) {
|
|
611
|
+
const comment = object(raw);
|
|
612
|
+
if (bot(comment)) {
|
|
613
|
+
const marker = string(comment.body).match(findingMarker);
|
|
614
|
+
if (marker)
|
|
615
|
+
context.seen.add(marker[1]);
|
|
616
|
+
}
|
|
617
|
+
occupyExistingSuggestion(raw, context, currentHead);
|
|
618
|
+
}
|
|
619
|
+
return context;
|
|
620
|
+
}
|
|
621
|
+
function quoteMatches(source, line, column, quote) {
|
|
622
|
+
return (Number.isSafeInteger(column) &&
|
|
623
|
+
column >= 0 &&
|
|
624
|
+
!!quote &&
|
|
625
|
+
source.text
|
|
626
|
+
.split('\n')
|
|
627
|
+
.slice(line - 1)
|
|
628
|
+
.join('\n')
|
|
629
|
+
.slice(column)
|
|
630
|
+
.startsWith(quote));
|
|
631
|
+
}
|
|
632
|
+
function accountCitationMatches(citation, context) {
|
|
633
|
+
const provenance = object(citation.provenance), location = object(citation.location);
|
|
634
|
+
const side = provenance.side === 'base' ? 'base' : provenance.side === 'head' ? 'head' : null;
|
|
635
|
+
const path = string(provenance.path), line = Number(location.line), column = Number(location.column);
|
|
636
|
+
if (provenance.kind !== 'source' || !side || !validPath(path) || !Number.isSafeInteger(line) || line < 1)
|
|
637
|
+
return false;
|
|
638
|
+
if (provenance.commit !== object(object(context.state.artifact)[side]).commit)
|
|
639
|
+
return false;
|
|
640
|
+
try {
|
|
641
|
+
const current = context.source(side, path), lines = current.text.split('\n');
|
|
642
|
+
return (provenance.object === current.blob &&
|
|
643
|
+
line <= lines.length &&
|
|
644
|
+
column <= lines[line - 1].length &&
|
|
645
|
+
quoteMatches(current, line, column, string(citation.quote)));
|
|
646
|
+
}
|
|
647
|
+
catch {
|
|
648
|
+
return false;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
function compareCitations(a, b) {
|
|
652
|
+
const ap = object(a.provenance), bp = object(b.provenance), al = object(a.location), bl = object(b.location);
|
|
653
|
+
return (string(ap.path).localeCompare(string(bp.path), 'en') ||
|
|
654
|
+
string(ap.side).localeCompare(string(bp.side), 'en') ||
|
|
655
|
+
Number(al.line) - Number(bl.line) ||
|
|
656
|
+
Number(al.column) - Number(bl.column) ||
|
|
657
|
+
string(a.quote).localeCompare(string(b.quote), 'en'));
|
|
658
|
+
}
|
|
659
|
+
function groupedInlineFinding(accounts) {
|
|
660
|
+
const first = accounts[0];
|
|
661
|
+
if (accounts.length === 1)
|
|
662
|
+
return first;
|
|
663
|
+
return {
|
|
664
|
+
...first,
|
|
665
|
+
locationRefs: accounts.flatMap((account) => objects(account.locationRefs)).sort(compareCitations),
|
|
666
|
+
assessmentIds: [...new Set(accounts.flatMap((account) => findingIds(account, 'assessmentIds')))].sort(),
|
|
667
|
+
checkIds: [...new Set(accounts.flatMap((account) => findingIds(account, 'checkIds')))].sort(),
|
|
668
|
+
findingId: accounts.map((account) => string(account.findingId)).sort(),
|
|
669
|
+
proposedReplacement: accounts.every((account) => object(account.proposedReplacement).verified === true &&
|
|
670
|
+
digest(account.proposedReplacement) === digest(first.proposedReplacement))
|
|
671
|
+
? first.proposedReplacement
|
|
672
|
+
: null,
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
function inlineLocation(citation, context) {
|
|
676
|
+
const record = object(context.evidence[string(citation.ref)]);
|
|
677
|
+
const provenance = object(citation.provenance ?? record.provenance), location = object(citation.location);
|
|
678
|
+
const path = string(provenance.path), side = diffSide(provenance.side);
|
|
679
|
+
const line = Number(location.line ?? object(provenance.start).line);
|
|
680
|
+
if (provenance.kind !== 'source' ||
|
|
681
|
+
!validPath(path) ||
|
|
682
|
+
!side ||
|
|
683
|
+
!Number.isSafeInteger(line) ||
|
|
684
|
+
!context.anchors.get(`${path}\0${side}`)?.has(line))
|
|
685
|
+
return null;
|
|
686
|
+
try {
|
|
687
|
+
const source = context.source(side === 'LEFT' ? 'base' : 'head', path);
|
|
688
|
+
if (string(provenance.object ?? provenance.blob) !== source.blob ||
|
|
689
|
+
!quoteMatches(source, line, Number(location.column ?? 0), string(citation.quote)))
|
|
690
|
+
return null;
|
|
691
|
+
return { path, side, line, source };
|
|
692
|
+
}
|
|
693
|
+
catch {
|
|
694
|
+
return null;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
function criterionReferences(finding, entries) {
|
|
698
|
+
return findingIds(finding, 'assessmentIds')
|
|
699
|
+
.map((id) => {
|
|
700
|
+
const entry = entries.find((entry) => entry.assessmentId === id);
|
|
701
|
+
return entry ? { nodeId: entry.nodeId, version: entry.version, contentHash: entry.contentHash } : id;
|
|
702
|
+
})
|
|
703
|
+
.sort(sortedJson);
|
|
704
|
+
}
|
|
705
|
+
function inlineFingerprint(finding, context, location, classification) {
|
|
706
|
+
return hash({
|
|
707
|
+
reviewer: context.reviewer.nodeId,
|
|
708
|
+
criteria: criterionReferences(finding, context.entries),
|
|
709
|
+
findingId: finding.findingId,
|
|
710
|
+
checkIds: [...findingIds(finding, 'checkIds')].sort(),
|
|
711
|
+
classification,
|
|
712
|
+
path: location.path,
|
|
713
|
+
blob: location.blob,
|
|
714
|
+
side: location.side,
|
|
715
|
+
line: location.line,
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
function accountPreviouslyPosted(account, context) {
|
|
719
|
+
return objects(account.locationRefs).some((citation) => {
|
|
720
|
+
const provenance = object(citation.provenance), location = object(citation.location);
|
|
721
|
+
const path = string(provenance.path), line = Number(location.line);
|
|
722
|
+
const side = diffSide(provenance.side);
|
|
723
|
+
if (!side || !context.anchors.get(`${path}\0${side}`)?.has(line))
|
|
724
|
+
return false;
|
|
725
|
+
return context.seen.has(inlineFingerprint(account, context, { path, line, side, blob: provenance.object }, account.classification));
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
function inlineAccountText(account, index, grouped, entries) {
|
|
729
|
+
const criteria = findingIds(account, 'assessmentIds')
|
|
730
|
+
.map((id) => {
|
|
731
|
+
const entry = entries.find((entry) => entry.assessmentId === id);
|
|
732
|
+
return markdownText(entry ? `${entry.nodeId} @ ${entry.version}` : id, 600);
|
|
733
|
+
})
|
|
734
|
+
.join(', ');
|
|
735
|
+
return `${grouped ? `Verified account ${index + 1}: ` : ''}**${markdownText(account.classification)}**\n\n${markdownText(account.explanation, 1500)}\n\nCriterion: ${criteria}.\n\nConsequence: ${markdownText(account.consequence, 800)}\n\nCorrection: ${markdownText(account.correction, 1200)}${grouped ? `\n\nLocation: ${markdownText(developerFindingLocations(account), 1500)}.\n\nAccount: ${markdownText(account.findingId, 200)}.` : ''}`;
|
|
736
|
+
}
|
|
737
|
+
function inlineBody(accounts, finding, fingerprint, context) {
|
|
738
|
+
const criterionText = criterionReferences(finding, context.entries)
|
|
739
|
+
.map((ref) => {
|
|
740
|
+
const entry = object(ref);
|
|
741
|
+
return markdownText(typeof ref === 'string' ? ref : `${entry.nodeId} @ ${entry.version}`, 600);
|
|
742
|
+
})
|
|
743
|
+
.join(', ');
|
|
744
|
+
const accountText = accounts
|
|
745
|
+
.map((account, index) => inlineAccountText(account, index, accounts.length > 1, context.entries))
|
|
746
|
+
.join('\n\n---\n\n');
|
|
747
|
+
return `<!-- contextrail-finding:${fingerprint} -->\n${accounts.length > 1 ? `**${accounts.length} verified accounts at ${developerFindingGroupLabel(accounts)}.**\n\nCriteria: ${criterionText}.\n\n` : ''}${accountText}`;
|
|
748
|
+
}
|
|
749
|
+
function replacementRange(replacement) {
|
|
750
|
+
const start = Number(replacement.startLine), end = Number(replacement.endLine), text = replacement.replacement;
|
|
751
|
+
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 1 || end < start || end - start + 1 > 20)
|
|
752
|
+
return null;
|
|
753
|
+
if (typeof text !== 'string' || text.split('\n').length > 20 || text.includes('```'))
|
|
754
|
+
return null;
|
|
755
|
+
return { start, end, text };
|
|
756
|
+
}
|
|
757
|
+
function applyInlineSuggestion(comment, finding, source, context) {
|
|
758
|
+
const replacement = object(finding.proposedReplacement), range = replacementRange(replacement);
|
|
759
|
+
if (comment.side !== 'RIGHT' ||
|
|
760
|
+
replacement.verified !== true ||
|
|
761
|
+
replacement.path !== comment.path ||
|
|
762
|
+
replacement.blob !== source.blob ||
|
|
763
|
+
!range)
|
|
764
|
+
return;
|
|
765
|
+
const { start, end, text } = range, occupied = context.occupied.get(comment.path) ?? [];
|
|
766
|
+
if (replacement.before !==
|
|
767
|
+
source.text
|
|
768
|
+
.split('\n')
|
|
769
|
+
.slice(start - 1, end)
|
|
770
|
+
.join('\n') ||
|
|
771
|
+
!coversRange(context.anchors, comment.path, start, end) ||
|
|
772
|
+
occupied.some(([a, b]) => a <= end && b >= start))
|
|
773
|
+
return;
|
|
774
|
+
comment.line = end;
|
|
775
|
+
if (start !== end) {
|
|
776
|
+
comment.start_line = start;
|
|
777
|
+
comment.start_side = 'RIGHT';
|
|
778
|
+
}
|
|
779
|
+
comment.body += `\n\nSource-verified suggestion; tests were not executed.\n\n\`\`\`suggestion\n${text}\n\`\`\``;
|
|
780
|
+
occupied.push([start, end]);
|
|
781
|
+
context.occupied.set(comment.path, occupied);
|
|
782
|
+
}
|
|
783
|
+
function inlineForGroup(accounts, context, remaining) {
|
|
784
|
+
// A shared range never grants another account's quotations source authority.
|
|
785
|
+
if (accounts.length > 1 &&
|
|
786
|
+
!accounts.every((account) => objects(account.locationRefs).every((citation) => accountCitationMatches(citation, context))))
|
|
787
|
+
return { omitted: accounts.length, duplicates: 0 };
|
|
788
|
+
const finding = groupedInlineFinding(accounts);
|
|
789
|
+
if (!publishableFinding(finding))
|
|
790
|
+
return { omitted: 1, duplicates: 0 };
|
|
791
|
+
const classification = accounts.length === 1
|
|
792
|
+
? finding.classification
|
|
793
|
+
: accounts.map((account) => [account.findingId, account.classification]).sort(sortedJson);
|
|
794
|
+
for (const citation of objects(finding.locationRefs ?? finding.citations)) {
|
|
795
|
+
const location = inlineLocation(citation, context);
|
|
796
|
+
if (!location)
|
|
797
|
+
continue;
|
|
798
|
+
const { path, side, line, source } = location;
|
|
799
|
+
const fingerprint = inlineFingerprint(finding, context, { path, blob: source.blob, side, line }, classification);
|
|
800
|
+
if (context.seen.has(fingerprint) ||
|
|
801
|
+
(accounts.length > 1 && accounts.every((account) => accountPreviouslyPosted(account, context))))
|
|
802
|
+
return { omitted: 0, duplicates: 1 };
|
|
803
|
+
context.seen.add(fingerprint);
|
|
804
|
+
if (remaining <= 0)
|
|
805
|
+
return { omitted: 1, duplicates: 0 };
|
|
806
|
+
const body = inlineBody(accounts, finding, fingerprint, context);
|
|
807
|
+
if (Buffer.byteLength(body) > 60000)
|
|
808
|
+
return { omitted: 1, duplicates: 0 };
|
|
809
|
+
const comment = { path, line, side, body };
|
|
810
|
+
applyInlineSuggestion(comment, finding, source, context);
|
|
811
|
+
return { comment, omitted: 0, duplicates: 0 };
|
|
812
|
+
}
|
|
813
|
+
return { omitted: 1, duplicates: 0 };
|
|
814
|
+
}
|
|
815
|
+
export function buildInlineReview(input, evidenceInput, options) {
|
|
816
|
+
const context = inlineContext(input, evidenceInput, options), candidates = objects(context.state.findings);
|
|
817
|
+
const comments = [];
|
|
818
|
+
let omitted = candidates.filter((finding) => !publishableFinding(finding)).length, duplicates = 0;
|
|
819
|
+
for (const accounts of groupDeveloperFindings(candidates.filter(inlineClassification))) {
|
|
820
|
+
const result = inlineForGroup(accounts, context, (options.max ?? 20) - comments.length);
|
|
821
|
+
omitted += result.omitted;
|
|
822
|
+
duplicates += result.duplicates;
|
|
823
|
+
if (result.comment)
|
|
824
|
+
comments.push(result.comment);
|
|
825
|
+
}
|
|
826
|
+
return { comments, omitted, duplicates };
|
|
827
|
+
}
|
|
828
|
+
function publicationManifest(runDir, state, statePath) {
|
|
829
|
+
const manifest = object(json(join(runDir, 'manifest.json'))), files = object(manifest.files);
|
|
830
|
+
if (state.schemaVersion !== 2 ||
|
|
831
|
+
manifest.contractVersion !== 1 ||
|
|
832
|
+
manifest.runId !== state.runId ||
|
|
833
|
+
manifest.scopeIdentity !== state.scopeIdentity ||
|
|
834
|
+
digest(manifest.artifact) !== digest(object(state.artifact)) ||
|
|
835
|
+
!Object.keys(files).length)
|
|
836
|
+
fail('publication_manifest_invalid');
|
|
837
|
+
if (!files[statePath.slice(resolve(runDir).length + 1)])
|
|
838
|
+
fail('publication_report_unbound');
|
|
839
|
+
return { manifest, files };
|
|
840
|
+
}
|
|
841
|
+
function verifyManifestFiles(runDir, files) {
|
|
842
|
+
for (const [name, expected] of Object.entries(files)) {
|
|
843
|
+
if (!/^(?:[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+|phases\/[A-Za-z0-9_-]+\.json)$/.test(name) ||
|
|
844
|
+
!/^sha256:[a-f0-9]{64}$/.test(string(expected)))
|
|
845
|
+
fail('publication_manifest_path_invalid');
|
|
846
|
+
const file = join(runDir, name), stat = lstatSync(file);
|
|
847
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > (name === 'diff.patch' ? 16 : 8) * 1024 * 1024)
|
|
848
|
+
fail('publication_artifact_invalid');
|
|
849
|
+
if (`sha256:${createHash('sha256').update(readFileSync(file)).digest('hex')}` !== expected)
|
|
850
|
+
fail('publication_artifact_changed');
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
function verifyManifestScope(manifest, state) {
|
|
854
|
+
const expected = objects(manifest.entries).map((entry) => entry.assessmentId), actual = objects(state.entries).map((entry) => entry.assessmentId);
|
|
855
|
+
if (new Set(expected).size !== expected.length ||
|
|
856
|
+
new Set(actual).size !== actual.length ||
|
|
857
|
+
expected.length !== actual.length ||
|
|
858
|
+
expected.some((id) => !actual.includes(id)))
|
|
859
|
+
fail('publication_scope_changed');
|
|
860
|
+
return actual;
|
|
861
|
+
}
|
|
862
|
+
function verifyPhaseInputs(record, runDir, files, preparedHash, seen) {
|
|
863
|
+
if (!record.inputRefs.some((input) => input.ref === 'manifest.json' && input.hash === preparedHash) ||
|
|
864
|
+
!record.inputRefs.some((input) => input.ref === 'publications.json' && input.hash === digest(json(join(runDir, 'publications.json')))))
|
|
865
|
+
fail('publication_phase_inputs_missing');
|
|
866
|
+
for (const input of record.inputRefs) {
|
|
867
|
+
if (input.ref === 'manifest.json' && input.hash !== preparedHash)
|
|
868
|
+
fail('publication_phase_manifest_changed');
|
|
869
|
+
if (input.ref === 'publications.json' &&
|
|
870
|
+
(!files['publications.json'] || input.hash !== digest(json(join(runDir, 'publications.json')))))
|
|
871
|
+
fail('publication_phase_publications_changed');
|
|
872
|
+
if (input.ref.startsWith('phase:') && seen.get(input.ref) !== input.hash)
|
|
873
|
+
fail('publication_phase_chain_changed');
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
function retainAssessment(record, handoffs) {
|
|
877
|
+
for (const id of record.assessmentIds) {
|
|
878
|
+
handoffs.proposals.delete(id);
|
|
879
|
+
handoffs.verifications.delete(id);
|
|
880
|
+
handoffs.proposalOrigins.delete(id);
|
|
881
|
+
handoffs.verificationOrigins.delete(id);
|
|
882
|
+
}
|
|
883
|
+
if (record.status !== 'complete')
|
|
884
|
+
return;
|
|
885
|
+
for (const proposal of objects(phaseItems(record.output, 'assessments'))) {
|
|
886
|
+
const id = string(object(proposal.result).assessmentId);
|
|
887
|
+
if (!record.assessmentIds.includes(id))
|
|
888
|
+
fail('publication_foreign_assessment');
|
|
889
|
+
handoffs.proposals.set(id, proposal);
|
|
890
|
+
handoffs.proposalOrigins.set(id, {
|
|
891
|
+
ref: `phase:${record.phase}:${record.workUnitId}:${record.attempt}`,
|
|
892
|
+
hash: digest(record),
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
function retainVerification(record, handoffs) {
|
|
897
|
+
for (const id of record.assessmentIds) {
|
|
898
|
+
handoffs.verifications.delete(id);
|
|
899
|
+
handoffs.verificationOrigins.delete(id);
|
|
900
|
+
}
|
|
901
|
+
if (record.status !== 'complete')
|
|
902
|
+
return;
|
|
903
|
+
for (const entry of objects(phaseItems(record.output, 'entries'))) {
|
|
904
|
+
const id = string(entry.assessmentId);
|
|
905
|
+
if (!record.assessmentIds.includes(id))
|
|
906
|
+
fail('publication_foreign_verification');
|
|
907
|
+
handoffs.verifications.set(id, entry);
|
|
908
|
+
handoffs.verificationOrigins.set(id, record);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
function publicationHandoffs(runDir, state, manifest, files) {
|
|
912
|
+
const preparedHash = digest(preparationManifest(manifest)), seen = new Map();
|
|
913
|
+
const handoffs = {
|
|
914
|
+
proposals: new Map(),
|
|
915
|
+
verifications: new Map(),
|
|
916
|
+
proposalOrigins: new Map(),
|
|
917
|
+
verificationOrigins: new Map(),
|
|
918
|
+
concernRecords: new Map(),
|
|
919
|
+
};
|
|
920
|
+
for (const raw of objects(state.phases)) {
|
|
921
|
+
const record = validatePhaseRecord(raw, {
|
|
922
|
+
runId: string(state.runId),
|
|
923
|
+
artifact: object(state.artifact),
|
|
924
|
+
scopeIdentity: typeof state.scopeIdentity === 'string' ? state.scopeIdentity : null,
|
|
925
|
+
assessmentIds: Array.isArray(raw.assessmentIds) ? raw.assessmentIds : [],
|
|
926
|
+
requireComplete: false,
|
|
927
|
+
});
|
|
928
|
+
const filename = record.phase === 'plan' && record.workUnitId === 'all' && record.attempt === 1
|
|
929
|
+
? 'plan.json'
|
|
930
|
+
: `${record.phase}-${record.workUnitId}-${record.attempt}.json`;
|
|
931
|
+
const name = `phases/${filename}`;
|
|
932
|
+
if (!files[name] || digest(json(join(runDir, name))) !== digest(raw))
|
|
933
|
+
fail('publication_phase_changed');
|
|
934
|
+
verifyPhaseInputs(record, runDir, files, preparedHash, seen);
|
|
935
|
+
seen.set(`phase:${record.phase}:${record.workUnitId}:${record.attempt}`, digest(record));
|
|
936
|
+
if (record.phase === 'assess')
|
|
937
|
+
retainAssessment(record, handoffs);
|
|
938
|
+
if (record.phase === 'verify') {
|
|
939
|
+
if (object(state.concernValidation).scope === 'nominated_concerns')
|
|
940
|
+
handoffs.concernRecords.set(record.workUnitId, record);
|
|
941
|
+
else
|
|
942
|
+
retainVerification(record, handoffs);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
return handoffs;
|
|
946
|
+
}
|
|
947
|
+
function acceptedOriginalFindings(id, handoffs) {
|
|
948
|
+
const proposal = handoffs.proposals.get(id), verification = handoffs.verifications.get(id);
|
|
949
|
+
const origin = handoffs.proposalOrigins.get(id), verifier = handoffs.verificationOrigins.get(id);
|
|
950
|
+
if (!verification)
|
|
951
|
+
return [];
|
|
952
|
+
if (!proposal || !origin || !verifier?.inputRefs.some((ref) => ref.ref === origin.ref && ref.hash === origin.hash))
|
|
953
|
+
return fail('publication_stale_verification');
|
|
954
|
+
if (verification.amendment)
|
|
955
|
+
return objects(object(verification.amendment).findings);
|
|
956
|
+
return objects(proposal.findings).flatMap((original) => {
|
|
957
|
+
const decision = objects(verification.findings).find((finding) => finding.findingId === original.findingId);
|
|
958
|
+
if (decision?.accepted !== true)
|
|
959
|
+
return [];
|
|
960
|
+
// The assessed finding was validated before being committed to the checked phase record.
|
|
961
|
+
return [
|
|
962
|
+
{
|
|
963
|
+
...original,
|
|
964
|
+
verified: true,
|
|
965
|
+
proposedReplacement: original.proposedReplacement
|
|
966
|
+
? { ...object(original.proposedReplacement), verified: decision.suggestionAccepted === true }
|
|
967
|
+
: undefined,
|
|
968
|
+
},
|
|
969
|
+
];
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
function verifyPublishedJudgments(state, handoffs, actualIds) {
|
|
973
|
+
verifyPublishedReaderGuidance(state, handoffs);
|
|
974
|
+
// Report flags and unions are not authority: reconstruct from immutable phase handoffs.
|
|
975
|
+
const originals = objects(state.entries).flatMap((entry) => acceptedOriginalFindings(string(entry.assessmentId), handoffs));
|
|
976
|
+
const canonical = reconcileFindings(originals), reported = objects(state.findings).filter((finding) => finding.verified === true);
|
|
977
|
+
if (canonical.length !== reported.length)
|
|
978
|
+
fail('publication_finding_unverified');
|
|
979
|
+
for (const finding of reported) {
|
|
980
|
+
const expected = canonical.find((original) => original.findingId === finding.findingId);
|
|
981
|
+
if (!expected || digest(expected) !== digest(finding))
|
|
982
|
+
fail('publication_finding_unverified');
|
|
983
|
+
}
|
|
984
|
+
for (const entry of objects(state.entries)) {
|
|
985
|
+
const resolution = handoffs.verifications.get(string(entry.assessmentId))?.resolution;
|
|
986
|
+
if (resolution && digest(resolution) !== digest(entry.result))
|
|
987
|
+
fail('publication_resolution_changed');
|
|
988
|
+
}
|
|
989
|
+
if (object(state.verification).complete === true &&
|
|
990
|
+
actualIds.some((id) => {
|
|
991
|
+
const verdict = handoffs.verifications.get(string(id));
|
|
992
|
+
return verdict?.disposition !== 'accept' && object(verdict?.resolution).assessmentId !== id;
|
|
993
|
+
}))
|
|
994
|
+
fail('publication_coverage_unverified');
|
|
995
|
+
}
|
|
996
|
+
function verifyPublishedReaderGuidance(state, handoffs) {
|
|
997
|
+
const summary = object(state.changeSummary);
|
|
998
|
+
if (summary.status === 'verified') {
|
|
999
|
+
const latest = objects(state.phases)
|
|
1000
|
+
.filter((phase) => phase.phase === 'verify')
|
|
1001
|
+
.at(-1);
|
|
1002
|
+
const accepted = object(latest?.output).changeSummary;
|
|
1003
|
+
if (latest?.status !== 'complete' || !accepted || digest(accepted) !== digest(summary.accepted))
|
|
1004
|
+
fail('publication_summary_unverified');
|
|
1005
|
+
}
|
|
1006
|
+
const expected = objects(state.entries).flatMap((entry) => {
|
|
1007
|
+
const id = string(entry.assessmentId), verdict = handoffs.verifications.get(id);
|
|
1008
|
+
if (verdict?.amendment)
|
|
1009
|
+
return objects(object(verdict.amendment).followUps);
|
|
1010
|
+
if (verdict?.disposition !== 'accept')
|
|
1011
|
+
return [];
|
|
1012
|
+
return objects(handoffs.proposals.get(id)?.followUps).map((item) => ({ ...item, verified: true }));
|
|
1013
|
+
});
|
|
1014
|
+
const reported = objects(state.followUps).filter((item) => item.verified === true);
|
|
1015
|
+
const fingerprints = (items) => items.map((item) => digest(item)).sort();
|
|
1016
|
+
if (digest(fingerprints(expected)) !== digest(fingerprints(reported)))
|
|
1017
|
+
fail('publication_follow_up_unverified');
|
|
1018
|
+
}
|
|
1019
|
+
function verifyCandidateJudgments(runDir, state, handoffs, files) {
|
|
1020
|
+
if (!files['evidence.json'])
|
|
1021
|
+
fail('publication_evidence_unbound');
|
|
1022
|
+
const ledger = new EvidenceLedger();
|
|
1023
|
+
for (const [ref, raw] of Object.entries(object(json(join(runDir, 'evidence.json'))))) {
|
|
1024
|
+
const record = raw;
|
|
1025
|
+
if (record.ref !== ref ||
|
|
1026
|
+
typeof record.text !== 'string' ||
|
|
1027
|
+
ledger.add(record.text, record.provenance, record.fragments).ref !== ref)
|
|
1028
|
+
fail('publication_evidence_changed');
|
|
1029
|
+
if (record.provenance.kind === 'source' &&
|
|
1030
|
+
record.provenance.commit !== object(object(state.artifact)[string(record.provenance.side)]).commit)
|
|
1031
|
+
fail('publication_source_identity_changed');
|
|
1032
|
+
}
|
|
1033
|
+
if (!Array.isArray(state.observations)) {
|
|
1034
|
+
const counts = object(state.concernValidation);
|
|
1035
|
+
if (objects(state.entries).some((entry) => object(entry.result).disposition !== 'not_evaluated') ||
|
|
1036
|
+
objects(state.findings).length ||
|
|
1037
|
+
objects(state.followUps).length ||
|
|
1038
|
+
objects(state.concernDecisions).length ||
|
|
1039
|
+
counts.expected !== 0 ||
|
|
1040
|
+
counts.validated !== 0 ||
|
|
1041
|
+
counts.complete !== false)
|
|
1042
|
+
fail('publication_incomplete_assessment_changed');
|
|
1043
|
+
for (const phase of objects(state.phases).filter((phase) => phase.phase === 'assess')) {
|
|
1044
|
+
for (const proposal of objects(object(phase.output).assessments))
|
|
1045
|
+
revalidateCandidate(proposal, objects(state.entries), ledger);
|
|
1046
|
+
}
|
|
1047
|
+
return;
|
|
1048
|
+
}
|
|
1049
|
+
const entries = structuredClone(objects(state.entries));
|
|
1050
|
+
const proposals = [...handoffs.proposals.values()].map((proposal) => revalidateCandidate(proposal, entries, ledger));
|
|
1051
|
+
const observations = proposals.flatMap((proposal) => proposal.observations ?? []);
|
|
1052
|
+
if (digest(observations) !== digest(state.observations))
|
|
1053
|
+
fail('publication_observations_changed');
|
|
1054
|
+
const clean = ({ ref, quote, start, end }) => ({ ref, quote, start, end });
|
|
1055
|
+
const decisions = [];
|
|
1056
|
+
for (const observation of observations.filter((row) => row.concern)) {
|
|
1057
|
+
const record = handoffs.concernRecords.get(observation.id);
|
|
1058
|
+
if (!record || record.status !== 'complete')
|
|
1059
|
+
continue;
|
|
1060
|
+
const origin = handoffs.proposalOrigins.get(observation.assessmentId);
|
|
1061
|
+
if (!origin ||
|
|
1062
|
+
!record.inputRefs.some((input) => input.ref === origin.ref && input.hash === origin.hash) ||
|
|
1063
|
+
!record.assessmentIds.includes(observation.assessmentId))
|
|
1064
|
+
fail('publication_stale_verification');
|
|
1065
|
+
const output = object(object(record.output).entries ?? record.output);
|
|
1066
|
+
const { observationId, ...raw } = output;
|
|
1067
|
+
if (observationId !== observation.id)
|
|
1068
|
+
fail('publication_concern_identity_changed');
|
|
1069
|
+
const decision = validateConcern({ ...raw, sourceRefs: output.sourceRefs.map(clean) }, observation, ledger);
|
|
1070
|
+
if (digest(decision) !== digest(output))
|
|
1071
|
+
fail('publication_concern_changed');
|
|
1072
|
+
decisions.push(decision);
|
|
1073
|
+
}
|
|
1074
|
+
const orderedDecisions = (values) => {
|
|
1075
|
+
const rows = objects(values);
|
|
1076
|
+
if (!Array.isArray(values) || rows.length !== values.length)
|
|
1077
|
+
fail('publication_concern_decisions_changed');
|
|
1078
|
+
return rows.sort((a, b) => string(a.observationId).localeCompare(string(b.observationId)));
|
|
1079
|
+
};
|
|
1080
|
+
if (digest(orderedDecisions(decisions)) !== digest(orderedDecisions(state.concernDecisions)))
|
|
1081
|
+
fail('publication_concern_decisions_changed');
|
|
1082
|
+
const projected = { entries, checks: [], findings: [], followUps: [] };
|
|
1083
|
+
projectCandidates(projected, proposals, decisions);
|
|
1084
|
+
if (digest(projected.findings) !== digest(state.findings))
|
|
1085
|
+
fail('publication_finding_unverified');
|
|
1086
|
+
if (digest(projected.followUps) !== digest(state.followUps ?? []))
|
|
1087
|
+
fail('publication_follow_up_unverified');
|
|
1088
|
+
for (const entry of objects(state.entries)) {
|
|
1089
|
+
if (object(entry.result).disposition === 'not_evaluated' && object(state.concernValidation).complete !== true)
|
|
1090
|
+
continue;
|
|
1091
|
+
if (digest(projected.entries.find((row) => row.assessmentId === entry.assessmentId)?.result) !==
|
|
1092
|
+
digest(entry.result))
|
|
1093
|
+
fail('publication_resolution_changed');
|
|
1094
|
+
}
|
|
1095
|
+
const expected = observations.filter((row) => row.concern).length;
|
|
1096
|
+
const counters = object(state.concernValidation);
|
|
1097
|
+
if (counters.expected !== expected ||
|
|
1098
|
+
counters.validated !== decisions.length ||
|
|
1099
|
+
counters.complete !== (expected === decisions.length))
|
|
1100
|
+
fail('publication_coverage_unverified');
|
|
1101
|
+
const summary = object(state.changeSummary);
|
|
1102
|
+
if (summary.status === 'verified') {
|
|
1103
|
+
const record = handoffs.concernRecords.get('change-summary');
|
|
1104
|
+
const output = object(object(record?.output).entries ?? record?.output);
|
|
1105
|
+
if (record?.status !== 'complete' || !output.summary || digest(output.summary) !== digest(summary.accepted))
|
|
1106
|
+
fail('publication_summary_unverified');
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
/** Validate caller-controlled retained records before using their judgments for a privileged write.
|
|
1110
|
+
* Hashes detect changed material; they do not authenticate a caller able to replace the whole bundle. */
|
|
1111
|
+
export function validatePublicationArtifacts(runDir, state, statePath) {
|
|
1112
|
+
const { manifest, files } = publicationManifest(runDir, state, statePath);
|
|
1113
|
+
verifyManifestFiles(runDir, files);
|
|
1114
|
+
const actualIds = verifyManifestScope(manifest, state);
|
|
1115
|
+
const handoffs = publicationHandoffs(runDir, state, manifest, files);
|
|
1116
|
+
if (object(state.concernValidation).scope === 'nominated_concerns')
|
|
1117
|
+
verifyCandidateJudgments(runDir, state, handoffs, files);
|
|
1118
|
+
else
|
|
1119
|
+
verifyPublishedJudgments(state, handoffs, actualIds);
|
|
1120
|
+
}
|
|
1121
|
+
function loadPublicationState(options, c) {
|
|
1122
|
+
const reportFile = join(options.runDir, 'report.json'), stateFile = join(options.runDir, 'state.json');
|
|
1123
|
+
const state = object(existsSync(reportFile) ? json(reportFile) : existsSync(stateFile) ? json(stateFile) : null);
|
|
1124
|
+
if (!Object.keys(state).length)
|
|
1125
|
+
return state;
|
|
1126
|
+
const artifact = object(state.artifact);
|
|
1127
|
+
if (object(artifact.base).commit !== c.base ||
|
|
1128
|
+
object(artifact.head).commit !== c.head ||
|
|
1129
|
+
object(state.reviewer).nodeId !== c.reviewer ||
|
|
1130
|
+
typeof state.runId !== 'string')
|
|
1131
|
+
fail('publication_identity_mismatch');
|
|
1132
|
+
validatePublicationArtifacts(resolve(options.runDir), state, existsSync(reportFile) ? resolve(reportFile) : resolve(stateFile));
|
|
1133
|
+
return state;
|
|
1134
|
+
}
|
|
1135
|
+
function publicationPresentation(options, c) {
|
|
1136
|
+
const failedStage = ['setup', 'comparison', 'context', 'assessment', 'evidence'].find((stage) => {
|
|
1137
|
+
const path = join(options.githubDir, `${stage}-status.json`);
|
|
1138
|
+
return existsSync(path) && object(json(path)).status === 'failed';
|
|
1139
|
+
});
|
|
1140
|
+
return {
|
|
1141
|
+
failedStage,
|
|
1142
|
+
artifactUrl: options.artifactUrl,
|
|
1143
|
+
runUrl: c.githubRun ? `https://github.com/${c.repository}/actions/runs/${c.githubRun}` : undefined,
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
function publicationRunRecord(options, c, state) {
|
|
1147
|
+
if (!c.githubRun)
|
|
1148
|
+
return null;
|
|
1149
|
+
const usage = summarizeUsage(state), statusFile = join(options.githubDir, 'assessment-status.json');
|
|
1150
|
+
return runRecord({ ...c, githubRun: c.githubRun, githubAttempt: c.githubAttempt ?? 1 }, c, state, object(state.decision).exitCode ?? (existsSync(statusFile) ? object(json(statusFile)).exit : null), usage.cost.value, usage.cost.complete);
|
|
1151
|
+
}
|
|
1152
|
+
function commentWithRetainedHistory(body, previous, localRecord, c, state) {
|
|
1153
|
+
if (localRecord)
|
|
1154
|
+
return withHistory(body, previous, localRecord, c);
|
|
1155
|
+
const prior = readHistory(previous?.body, c);
|
|
1156
|
+
const history = {
|
|
1157
|
+
version: 2,
|
|
1158
|
+
repository: c.repository,
|
|
1159
|
+
pr: c.pr,
|
|
1160
|
+
reviewer: c.reviewer,
|
|
1161
|
+
runs: prior?.runs ?? [],
|
|
1162
|
+
omitted: prior?.omitted ?? 0,
|
|
1163
|
+
assessment: prior?.assessment ?? null,
|
|
1164
|
+
};
|
|
1165
|
+
return `${assessmentMarker(c)}\n<!-- contextrail-local-run:${hash({ runId: state.runId, base: c.base, head: c.head, scope: state.scopeIdentity })} -->\n<!-- contextrail-state:v2:${Buffer.from(JSON.stringify(history)).toString('base64')} -->\n${body}`;
|
|
1166
|
+
}
|
|
1167
|
+
async function publicationFreshness(previous, c, request) {
|
|
1168
|
+
const old = string(previous?.body).match(/<!-- contextrail-run:([1-9][0-9]*):([1-9][0-9]*) -->/);
|
|
1169
|
+
if (old &&
|
|
1170
|
+
c.githubRun &&
|
|
1171
|
+
(BigInt(old[1]) > BigInt(c.githubRun) || (old[1] === c.githubRun && Number(old[2]) > (c.githubAttempt ?? 1))))
|
|
1172
|
+
return 'skipped_newer';
|
|
1173
|
+
return sameComparison(await request(`/pulls/${c.pr}`), c) ? null : 'skipped_superseded';
|
|
1174
|
+
}
|
|
1175
|
+
function snapshotInlineOptions(state, c, existingComments) {
|
|
1176
|
+
const repository = string(object(state.artifact).repository);
|
|
1177
|
+
return {
|
|
1178
|
+
diff: git(repository, [
|
|
1179
|
+
'diff',
|
|
1180
|
+
'--no-ext-diff',
|
|
1181
|
+
'--no-textconv',
|
|
1182
|
+
'--find-renames',
|
|
1183
|
+
'--unified=3',
|
|
1184
|
+
'--no-color',
|
|
1185
|
+
c.base,
|
|
1186
|
+
c.head,
|
|
1187
|
+
'--',
|
|
1188
|
+
]),
|
|
1189
|
+
source: (side, path) => {
|
|
1190
|
+
if (!validPath(path))
|
|
1191
|
+
return fail('invalid_inline_path');
|
|
1192
|
+
const commit = side === 'base' ? c.base : c.head;
|
|
1193
|
+
const blob = git(repository, ['rev-parse', '--verify', `${commit}:${path}`]).trim();
|
|
1194
|
+
if (!/^[a-f0-9]{40}$/.test(blob))
|
|
1195
|
+
return fail('invalid_inline_blob');
|
|
1196
|
+
return { blob, text: git(repository, ['cat-file', 'blob', blob]) };
|
|
1197
|
+
},
|
|
1198
|
+
existingComments,
|
|
1199
|
+
};
|
|
1200
|
+
}
|
|
1201
|
+
async function inlinePublicationPlan(options, c, state, request) {
|
|
1202
|
+
const empty = { comments: [], omitted: 0, duplicates: 0 };
|
|
1203
|
+
if (!options.inlineComments)
|
|
1204
|
+
return { inline: empty, status: 'disabled', exitCode: 0 };
|
|
1205
|
+
let existing = [];
|
|
1206
|
+
if (request) {
|
|
1207
|
+
try {
|
|
1208
|
+
existing = await listAll(request, `/pulls/${c.pr}/comments`);
|
|
1209
|
+
}
|
|
1210
|
+
catch {
|
|
1211
|
+
return { inline: empty, status: 'unknown', exitCode: 2 };
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
const snapshot = snapshotInlineOptions(state, c, existing);
|
|
1215
|
+
const inline = buildInlineReview(state, json(join(options.runDir, 'evidence.json')), snapshot);
|
|
1216
|
+
return {
|
|
1217
|
+
inline,
|
|
1218
|
+
status: options.dryRun ? 'preview' : inline.comments.length ? 'pending' : 'no_new_findings',
|
|
1219
|
+
exitCode: 0,
|
|
1220
|
+
};
|
|
1221
|
+
}
|
|
1222
|
+
function assessmentRunMarker(c, state) {
|
|
1223
|
+
return `<!-- contextrail-assess-run:${hash({ repository: c.repository, pr: c.pr, base: c.base, head: c.head, scopeIdentity: state.scopeIdentity, runId: state.runId, workflowRun: c.githubRun, attempt: c.githubAttempt })} -->`;
|
|
1224
|
+
}
|
|
1225
|
+
function writePublicationPayloads(options, c, state, body, inline) {
|
|
1226
|
+
write(join(options.runDir, 'publication-payloads.json'), {
|
|
1227
|
+
comment: { body },
|
|
1228
|
+
review: {
|
|
1229
|
+
commit_id: c.head,
|
|
1230
|
+
event: 'COMMENT',
|
|
1231
|
+
body: `${assessmentRunMarker(c, state)}\nContextRail Assess for ${c.head}. ${inline.omitted} findings retained in the main report without a new inline thread.`,
|
|
1232
|
+
comments: inline.comments,
|
|
1233
|
+
},
|
|
1234
|
+
preview: !!options.dryRun,
|
|
1235
|
+
limitations: options.dryRun
|
|
1236
|
+
? ['Current GitHub freshness and prior-thread lookup were not performed in dry-run.']
|
|
1237
|
+
: [],
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
async function deliverMainComment(request, c, previous, body) {
|
|
1241
|
+
try {
|
|
1242
|
+
if (!sameComparison(await request(`/pulls/${c.pr}`), c))
|
|
1243
|
+
return { status: 'skipped_superseded', url: '', exitCode: 0 };
|
|
1244
|
+
const result = object(await request(previous ? `/issues/comments/${previous.id}` : `/issues/${c.pr}/comments`, previous ? 'PATCH' : 'POST', { body }));
|
|
1245
|
+
if (!Number.isSafeInteger(result.id) || Number(result.id) < 1)
|
|
1246
|
+
fail('github_write_unknown');
|
|
1247
|
+
return {
|
|
1248
|
+
status: previous ? 'updated' : 'created',
|
|
1249
|
+
url: `https://github.com/${c.repository}/pull/${c.pr}#issuecomment-${result.id}`,
|
|
1250
|
+
exitCode: 0,
|
|
1251
|
+
};
|
|
1252
|
+
}
|
|
1253
|
+
catch {
|
|
1254
|
+
return { status: 'unknown', url: '', exitCode: 2 };
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
async function deliverInlineReview(request, c, state, inline) {
|
|
1258
|
+
try {
|
|
1259
|
+
if (!sameComparison(await request(`/pulls/${c.pr}`), c))
|
|
1260
|
+
return { status: 'skipped_superseded', exitCode: 0 };
|
|
1261
|
+
const result = object(await request(`/pulls/${c.pr}/reviews`, 'POST', {
|
|
1262
|
+
commit_id: c.head,
|
|
1263
|
+
event: 'COMMENT',
|
|
1264
|
+
body: `${assessmentRunMarker(c, state)}\nContextRail governed findings at ${c.head}.`,
|
|
1265
|
+
comments: inline.comments,
|
|
1266
|
+
}));
|
|
1267
|
+
if (!Number.isSafeInteger(result.id))
|
|
1268
|
+
fail('github_write_unknown');
|
|
1269
|
+
return { status: 'posted', exitCode: 0 };
|
|
1270
|
+
}
|
|
1271
|
+
catch {
|
|
1272
|
+
return { status: 'unknown', exitCode: 2 };
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
function publicationRequest(options, c) {
|
|
1276
|
+
if (options.dryRun || !options.postComment)
|
|
1277
|
+
return null;
|
|
1278
|
+
return githubRequester(c.repository, options.token ?? process.env.GITHUB_TOKEN ?? '', options.fetch);
|
|
1279
|
+
}
|
|
1280
|
+
export async function publishGitHubAssessment(options) {
|
|
1281
|
+
if (options.inlineComments && !options.postComment)
|
|
1282
|
+
fail('inline_requires_comment');
|
|
1283
|
+
const c = githubInputSchema.parse(json(join(options.githubDir, 'github-input.json')));
|
|
1284
|
+
const state = loadPublicationState(options, c), presentation = publicationPresentation(options, c);
|
|
1285
|
+
const summary = renderActionSummary(state, presentation);
|
|
1286
|
+
if (options.summaryFile)
|
|
1287
|
+
appendFileSync(options.summaryFile, summary);
|
|
1288
|
+
const localRecord = publicationRunRecord(options, c, state);
|
|
1289
|
+
const output = {
|
|
1290
|
+
exitCode: 0,
|
|
1291
|
+
status: 'rendered',
|
|
1292
|
+
commentStatus: 'disabled',
|
|
1293
|
+
inlineStatus: 'disabled',
|
|
1294
|
+
omittedInline: 0,
|
|
1295
|
+
duplicateInline: 0,
|
|
1296
|
+
commentUrl: '',
|
|
1297
|
+
};
|
|
1298
|
+
const save = () => {
|
|
1299
|
+
write(join(options.runDir, 'publication.json'), output);
|
|
1300
|
+
return output;
|
|
1301
|
+
};
|
|
1302
|
+
let body = renderAssessmentComment(state, presentation), previous = null;
|
|
1303
|
+
const request = publicationRequest(options, c);
|
|
1304
|
+
if (request) {
|
|
1305
|
+
previous = await findAssessmentComment(c, request);
|
|
1306
|
+
const skipped = await publicationFreshness(previous, c, request);
|
|
1307
|
+
if (skipped) {
|
|
1308
|
+
output.status = skipped;
|
|
1309
|
+
return save();
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
body = commentWithRetainedHistory(body, previous, localRecord, c, state);
|
|
1313
|
+
const plan = await inlinePublicationPlan(options, c, state, request), inline = plan.inline;
|
|
1314
|
+
let inlineStatus = plan.status, commentStatus = 'disabled', exitCode = plan.exitCode;
|
|
1315
|
+
if (options.inlineComments)
|
|
1316
|
+
body = body.replace('<summary>Run details</summary>', `<summary>Run details</summary>\n\nInline feedback plan: ${inline.comments.length} new comments; ${inline.duplicates} existing threads; ${inline.omitted} findings retained without a new inline comment. Delivery outcome is recorded in the Action summary and publication artifact.`);
|
|
1317
|
+
writePublicationPayloads(options, c, state, body, inline);
|
|
1318
|
+
if (request) {
|
|
1319
|
+
const delivery = await deliverMainComment(request, c, previous, body);
|
|
1320
|
+
if (delivery.status === 'skipped_superseded') {
|
|
1321
|
+
output.status = delivery.status;
|
|
1322
|
+
return save();
|
|
1323
|
+
}
|
|
1324
|
+
commentStatus = delivery.status;
|
|
1325
|
+
output.commentUrl = delivery.url;
|
|
1326
|
+
exitCode = Math.max(exitCode, delivery.exitCode);
|
|
1327
|
+
if (options.inlineComments && inlineStatus === 'pending' && commentStatus !== 'unknown') {
|
|
1328
|
+
const delivered = await deliverInlineReview(request, c, state, inline);
|
|
1329
|
+
inlineStatus = delivered.status;
|
|
1330
|
+
exitCode = Math.max(exitCode, delivered.exitCode);
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
Object.assign(output, {
|
|
1334
|
+
exitCode,
|
|
1335
|
+
status: options.dryRun ? 'preview' : exitCode ? 'delivery_incomplete' : request ? 'published' : 'rendered',
|
|
1336
|
+
commentStatus,
|
|
1337
|
+
inlineStatus,
|
|
1338
|
+
omittedInline: inline.omitted,
|
|
1339
|
+
duplicateInline: inline.duplicates,
|
|
1340
|
+
});
|
|
1341
|
+
return save();
|
|
1342
|
+
}
|