@contextrail/assess 0.0.0-stage → 1.0.0-alpha.2
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 +1563 -0
- package/dist/assessment.d.ts +571 -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 +156 -0
- package/dist/candidate.js +361 -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 +154 -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 +310 -0
- package/dist/presentation.js +1076 -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 +137 -0
- package/dist/report-contract.js +248 -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 +158 -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 +211 -0
- package/docs/review-results.md +189 -0
- package/docs/security.md +68 -0
- package/docs/standards.md +62 -0
- package/examples/assess-pull-request.yml +109 -0
- package/package.json +56 -5
package/dist/evidence.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { digest } from './prepare.js';
|
|
3
|
+
export const selectionSchema = z.strictObject({
|
|
4
|
+
ref: z.string().min(1).describe('Copy the returned evidence identifier verbatim.'),
|
|
5
|
+
startSegment: z.number().int().positive().describe('First evidence-relative segment index, inclusive.'),
|
|
6
|
+
endSegment: z.number().int().positive().describe('Last evidence-relative segment index, inclusive.'),
|
|
7
|
+
});
|
|
8
|
+
export const citationSchema = z
|
|
9
|
+
.strictObject({
|
|
10
|
+
ref: z.string().min(1),
|
|
11
|
+
quote: z.string().min(1),
|
|
12
|
+
start: z.number().int().nonnegative().optional(),
|
|
13
|
+
end: z.number().int().nonnegative().optional(),
|
|
14
|
+
})
|
|
15
|
+
.refine((value) => (value.start === undefined) === (value.end === undefined), {
|
|
16
|
+
message: 'Citation offsets must be supplied together.',
|
|
17
|
+
path: ['end'],
|
|
18
|
+
});
|
|
19
|
+
/** Content-free failure identity; rejected quotations never enter exception messages or diagnostics. */
|
|
20
|
+
export class CitationError extends Error {
|
|
21
|
+
code;
|
|
22
|
+
path;
|
|
23
|
+
ref;
|
|
24
|
+
refHash;
|
|
25
|
+
constructor(code, ref, path = []) {
|
|
26
|
+
super({
|
|
27
|
+
citation_unknown: 'Unknown evidence ref. Copy a ref returned with pinned guidance or a source read.',
|
|
28
|
+
citation_absent: 'Quote absent from that evidence. Copy exact text, preserving whitespace, or read the relevant source range.',
|
|
29
|
+
citation_ambiguous: 'Quote ambiguous in that evidence. Include more surrounding text or read a narrower range.',
|
|
30
|
+
citation_range_invalid: 'Citation range invalid. Select a narrower returned evidence-relative segment range.',
|
|
31
|
+
}[code]);
|
|
32
|
+
this.code = code;
|
|
33
|
+
this.path = path;
|
|
34
|
+
this.name = 'CitationError';
|
|
35
|
+
this.ref = /^evidence:[a-f0-9]{64}$/u.test(ref) ? ref : null;
|
|
36
|
+
this.refHash = digest(ref);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Segment indices belong to this evidence record, never to a file or publication's line numbering. */
|
|
40
|
+
function evidenceSegments(text) {
|
|
41
|
+
return Array.from(text.matchAll(/[^\r\n]*(?:\r\n|\r|\n)|[^\r\n]+$/gu), (match, index) => ({
|
|
42
|
+
index: index + 1,
|
|
43
|
+
text: match[0],
|
|
44
|
+
start: match.index,
|
|
45
|
+
end: match.index + match[0].length,
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
/** A single source of text for model inputs: no duplicate full-text representation. */
|
|
49
|
+
export function modelEvidence(record) {
|
|
50
|
+
return {
|
|
51
|
+
ref: record.ref,
|
|
52
|
+
provenance: record.provenance,
|
|
53
|
+
segments: evidenceSegments(record.text).map(({ index, text }) => ({ index, text })),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** Model-only handles and compact metadata; the ledger and audit artifacts stay canonical. */
|
|
57
|
+
export class ModelIdentifiers {
|
|
58
|
+
evidence;
|
|
59
|
+
handles = new Map();
|
|
60
|
+
canonical = new Map();
|
|
61
|
+
evidenceCount = 0;
|
|
62
|
+
constructor(assessmentIds, evidence) {
|
|
63
|
+
this.evidence = evidence;
|
|
64
|
+
assessmentIds.forEach((id, index) => this.register(id, `context_${index + 1}`));
|
|
65
|
+
Object.keys(evidence)
|
|
66
|
+
.sort()
|
|
67
|
+
.forEach((ref) => this.evidenceHandle(ref));
|
|
68
|
+
}
|
|
69
|
+
register(id, handle) {
|
|
70
|
+
this.handles.set(id, handle);
|
|
71
|
+
this.canonical.set(handle, id);
|
|
72
|
+
return handle;
|
|
73
|
+
}
|
|
74
|
+
evidenceHandle(ref) {
|
|
75
|
+
return this.handles.get(ref) ?? this.register(ref, `evidence_${++this.evidenceCount}`);
|
|
76
|
+
}
|
|
77
|
+
translate(value, outward, field = '') {
|
|
78
|
+
if (Array.isArray(value))
|
|
79
|
+
return value.map((item) => this.translate(item, outward, field));
|
|
80
|
+
if (value && typeof value === 'object') {
|
|
81
|
+
const citation = outward &&
|
|
82
|
+
'ref' in value &&
|
|
83
|
+
typeof value.ref === 'string' &&
|
|
84
|
+
Object.hasOwn(this.evidence, value.ref) &&
|
|
85
|
+
'quote' in value &&
|
|
86
|
+
typeof value.quote === 'string';
|
|
87
|
+
const result = outward &&
|
|
88
|
+
'assessmentId' in value &&
|
|
89
|
+
typeof value.assessmentId === 'string' &&
|
|
90
|
+
this.handles.has(value.assessmentId) &&
|
|
91
|
+
'disposition' in value &&
|
|
92
|
+
'adherence' in value;
|
|
93
|
+
return Object.fromEntries(Object.entries(value)
|
|
94
|
+
.filter(([key]) => (!citation || !['quote', 'start', 'end'].includes(key)) &&
|
|
95
|
+
(!result || !['revision', 'revisionOf'].includes(key)))
|
|
96
|
+
.map(([key, item]) => [key, this.translate(item, outward, key)]));
|
|
97
|
+
}
|
|
98
|
+
if (typeof value !== 'string' || !MODEL_IDENTIFIER_FIELDS.has(field))
|
|
99
|
+
return value;
|
|
100
|
+
if (!outward)
|
|
101
|
+
return this.canonical.get(value) ?? value;
|
|
102
|
+
if (Object.hasOwn(this.evidence, value))
|
|
103
|
+
return this.evidenceHandle(value);
|
|
104
|
+
return this.handles.get(value) ?? value;
|
|
105
|
+
}
|
|
106
|
+
toModel(value) {
|
|
107
|
+
return this.translate(value, true);
|
|
108
|
+
}
|
|
109
|
+
/** Audit-only identity map; never includes the source text behind an evidence reference. */
|
|
110
|
+
snapshot() {
|
|
111
|
+
return Object.fromEntries(this.canonical);
|
|
112
|
+
}
|
|
113
|
+
fromModel(value) {
|
|
114
|
+
return this.translate(value, false);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const MODEL_IDENTIFIER_FIELDS = new Set([
|
|
118
|
+
'assessmentId',
|
|
119
|
+
'assessmentIds',
|
|
120
|
+
'remaining',
|
|
121
|
+
'newlyAccepted',
|
|
122
|
+
'alreadyAccepted',
|
|
123
|
+
'ref',
|
|
124
|
+
'allowedRefs',
|
|
125
|
+
'evidenceRefs',
|
|
126
|
+
]);
|
|
127
|
+
const MAX_SELECTION_BYTES = 64 * 1024;
|
|
128
|
+
export const MAX_SUBMISSION_QUOTE_BYTES = 1024 * 1024;
|
|
129
|
+
function parseSelection(input, path) {
|
|
130
|
+
const parsed = selectionSchema.safeParse(input);
|
|
131
|
+
if (!parsed.success) {
|
|
132
|
+
const field = parsed.error.issues[0]?.path[0], ref = input && typeof input === 'object' && 'ref' in input && typeof input.ref === 'string' ? input.ref : '';
|
|
133
|
+
throw new CitationError('citation_range_invalid', ref, [
|
|
134
|
+
...path,
|
|
135
|
+
...(typeof field === 'string' && ['ref', 'startSegment', 'endSegment'].includes(field) ? [field] : []),
|
|
136
|
+
]);
|
|
137
|
+
}
|
|
138
|
+
return parsed.data;
|
|
139
|
+
}
|
|
140
|
+
function selectedByteCount(text, start, end, maxBytes, ref, path) {
|
|
141
|
+
let bytes = 0;
|
|
142
|
+
// Count UTF-8 size without first allocating the selected quotation. Lone surrogates match
|
|
143
|
+
// Buffer's replacement encoding; segment boundaries never split an intact surrogate pair.
|
|
144
|
+
for (let offset = start; offset < end; offset++) {
|
|
145
|
+
const point = text.codePointAt(offset);
|
|
146
|
+
bytes += point <= 0x7f ? 1 : point <= 0x7ff ? 2 : point <= 0xffff ? 3 : 4;
|
|
147
|
+
if (point > 0xffff)
|
|
148
|
+
offset++;
|
|
149
|
+
if (bytes > maxBytes)
|
|
150
|
+
throw new CitationError('citation_range_invalid', ref, [...path, 'endSegment']);
|
|
151
|
+
}
|
|
152
|
+
return bytes;
|
|
153
|
+
}
|
|
154
|
+
function selectionRange(records, input, path, maxBytes) {
|
|
155
|
+
const selection = parseSelection(input, path), record = Object.hasOwn(records, selection.ref) ? records[selection.ref] : undefined;
|
|
156
|
+
if (!record)
|
|
157
|
+
throw new CitationError('citation_unknown', selection.ref, [...path, 'ref']);
|
|
158
|
+
const segments = evidenceSegments(record.text);
|
|
159
|
+
if (selection.startSegment > segments.length)
|
|
160
|
+
throw new CitationError('citation_range_invalid', selection.ref, [...path, 'startSegment']);
|
|
161
|
+
if (selection.endSegment < selection.startSegment || selection.endSegment > segments.length)
|
|
162
|
+
throw new CitationError('citation_range_invalid', selection.ref, [...path, 'endSegment']);
|
|
163
|
+
const start = segments[selection.startSegment - 1].start, end = segments[selection.endSegment - 1].end;
|
|
164
|
+
const bytes = selectedByteCount(record.text, start, end, maxBytes, selection.ref, path);
|
|
165
|
+
return { record, start, end, bytes };
|
|
166
|
+
}
|
|
167
|
+
/** The caller validates a selection-only model schema before this deterministic transformation. */
|
|
168
|
+
export function resolveCitationSelections(input, ledger, path = []) {
|
|
169
|
+
let remaining = MAX_SUBMISSION_QUOTE_BYTES;
|
|
170
|
+
const resolve = (value, currentPath) => {
|
|
171
|
+
if (Array.isArray(value))
|
|
172
|
+
return value.map((item, index) => resolve(item, [...currentPath, index]));
|
|
173
|
+
if (value && typeof value === 'object') {
|
|
174
|
+
if (Object.hasOwn(value, 'ref') ||
|
|
175
|
+
Object.hasOwn(value, 'startSegment') ||
|
|
176
|
+
Object.hasOwn(value, 'endSegment')) {
|
|
177
|
+
const range = selectionRange(ledger.records, value, currentPath, Math.min(MAX_SELECTION_BYTES, remaining));
|
|
178
|
+
remaining -= range.bytes;
|
|
179
|
+
return {
|
|
180
|
+
ref: range.record.ref,
|
|
181
|
+
quote: range.record.text.slice(range.start, range.end),
|
|
182
|
+
start: range.start,
|
|
183
|
+
end: range.end,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, resolve(item, [...currentPath, key])]));
|
|
187
|
+
}
|
|
188
|
+
return value;
|
|
189
|
+
};
|
|
190
|
+
return resolve(input, path);
|
|
191
|
+
}
|
|
192
|
+
function explicitCitationRange(record, input, path) {
|
|
193
|
+
if (!Number.isSafeInteger(input.start) ||
|
|
194
|
+
input.start === undefined ||
|
|
195
|
+
input.start < 0 ||
|
|
196
|
+
input.start >= record.text.length)
|
|
197
|
+
throw new CitationError('citation_range_invalid', input.ref, [...path, 'start']);
|
|
198
|
+
if (!Number.isSafeInteger(input.end) ||
|
|
199
|
+
input.end === undefined ||
|
|
200
|
+
input.end <= input.start ||
|
|
201
|
+
input.end > record.text.length)
|
|
202
|
+
throw new CitationError('citation_range_invalid', input.ref, [...path, 'end']);
|
|
203
|
+
const start = input.start;
|
|
204
|
+
const end = input.end;
|
|
205
|
+
if (record.text.slice(start, end) !== input.quote)
|
|
206
|
+
throw new CitationError('citation_range_invalid', input.ref, [...path, 'quote']);
|
|
207
|
+
return { start, end };
|
|
208
|
+
}
|
|
209
|
+
function citationRange(record, input, path) {
|
|
210
|
+
if (input.start !== undefined || input.end !== undefined)
|
|
211
|
+
return explicitCitationRange(record, input, path);
|
|
212
|
+
const start = record.text.indexOf(input.quote);
|
|
213
|
+
if (start < 0)
|
|
214
|
+
throw new CitationError('citation_absent', input.ref, path);
|
|
215
|
+
if (record.text.indexOf(input.quote, start + 1) >= 0)
|
|
216
|
+
throw new CitationError('citation_ambiguous', input.ref, path);
|
|
217
|
+
const end = start + input.quote.length;
|
|
218
|
+
return { start, end };
|
|
219
|
+
}
|
|
220
|
+
function citationLocation(record, start) {
|
|
221
|
+
let location;
|
|
222
|
+
if (record.fragments) {
|
|
223
|
+
let offset = 0;
|
|
224
|
+
for (const fragment of record.fragments) {
|
|
225
|
+
if (start < offset + fragment.text.length) {
|
|
226
|
+
location = { line: fragment.line, column: fragment.column + start - offset };
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
offset += fragment.text.length;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return location;
|
|
233
|
+
}
|
|
234
|
+
export class EvidenceLedger {
|
|
235
|
+
records = {};
|
|
236
|
+
add(text, provenance, fragments) {
|
|
237
|
+
const ref = `evidence:${digest({ text, provenance }).slice(7)}`;
|
|
238
|
+
this.records[ref] = { ref, text, provenance, ...(fragments ? { fragments } : {}) };
|
|
239
|
+
return this.records[ref];
|
|
240
|
+
}
|
|
241
|
+
publication(publication) {
|
|
242
|
+
const records = [];
|
|
243
|
+
const visit = (value, path) => {
|
|
244
|
+
if (typeof value === 'string' && value.trim())
|
|
245
|
+
records.push(this.add(value, {
|
|
246
|
+
kind: 'publication',
|
|
247
|
+
nodeId: publication.nodeId,
|
|
248
|
+
version: publication.version,
|
|
249
|
+
path,
|
|
250
|
+
contentHash: digest(publication.content),
|
|
251
|
+
}));
|
|
252
|
+
else if (value && typeof value === 'object')
|
|
253
|
+
for (const [key, child] of Object.entries(value))
|
|
254
|
+
visit(child, `${path}/${key.replaceAll('~', '~0').replaceAll('/', '~1')}`);
|
|
255
|
+
};
|
|
256
|
+
visit(publication.content, '/content');
|
|
257
|
+
return records;
|
|
258
|
+
}
|
|
259
|
+
source(result) {
|
|
260
|
+
if (!result.entry || !result.fragments.length)
|
|
261
|
+
return null;
|
|
262
|
+
return this.add(result.fragments.map((f) => f.text).join(''), {
|
|
263
|
+
...result.entry,
|
|
264
|
+
sourceKind: result.entry.kind,
|
|
265
|
+
kind: 'source',
|
|
266
|
+
start: { line: result.fragments[0].line, column: result.fragments[0].column },
|
|
267
|
+
readStatus: result.status,
|
|
268
|
+
next: structuredClone(result.next),
|
|
269
|
+
}, result.fragments);
|
|
270
|
+
}
|
|
271
|
+
select(input, path = [], remainingBytes = MAX_SELECTION_BYTES) {
|
|
272
|
+
const cap = Number.isSafeInteger(remainingBytes) && remainingBytes >= 0
|
|
273
|
+
? Math.min(MAX_SELECTION_BYTES, remainingBytes)
|
|
274
|
+
: 0;
|
|
275
|
+
const range = selectionRange(this.records, input, path, cap);
|
|
276
|
+
return this.cite({
|
|
277
|
+
ref: range.record.ref,
|
|
278
|
+
quote: range.record.text.slice(range.start, range.end),
|
|
279
|
+
start: range.start,
|
|
280
|
+
end: range.end,
|
|
281
|
+
}, path);
|
|
282
|
+
}
|
|
283
|
+
cite(input, path = []) {
|
|
284
|
+
const record = Object.hasOwn(this.records, input.ref) ? this.records[input.ref] : undefined;
|
|
285
|
+
if (!record)
|
|
286
|
+
throw new CitationError('citation_unknown', input.ref, path);
|
|
287
|
+
const { start, end } = citationRange(record, input, path);
|
|
288
|
+
const location = citationLocation(record, start);
|
|
289
|
+
return {
|
|
290
|
+
...input,
|
|
291
|
+
start,
|
|
292
|
+
end,
|
|
293
|
+
provenance: record.provenance,
|
|
294
|
+
...(location ? { location } : {}),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type ReviewReportInput, type ReviewReportReceipt } from './report-contract.js';
|
|
2
|
+
import type { AssessmentState } from './assess.js';
|
|
3
|
+
export type ReportingStatus = {
|
|
4
|
+
status: 'pending' | 'off';
|
|
5
|
+
} | {
|
|
6
|
+
status: 'received';
|
|
7
|
+
receipt: ReviewReportReceipt;
|
|
8
|
+
} | {
|
|
9
|
+
status: 'failed';
|
|
10
|
+
code: 'projection_invalid' | 'sender_unavailable' | 'submission_failed' | 'receipt_invalid' | 'local_status_write_failed' | 'finalization_unavailable';
|
|
11
|
+
};
|
|
12
|
+
export interface AssessmentFeedbackSender {
|
|
13
|
+
submitReport(report: ReviewReportInput): Promise<unknown>;
|
|
14
|
+
}
|
|
15
|
+
/** Construct from the allowlist. Never serialize local state then remove fields. */
|
|
16
|
+
export declare function projectAssessmentReport(state: AssessmentState, repositoryAttribution?: boolean): ReviewReportInput;
|
|
17
|
+
export declare function sendAssessmentReport(state: AssessmentState, options: {
|
|
18
|
+
mode?: 'send' | 'off';
|
|
19
|
+
repositoryAttribution?: boolean;
|
|
20
|
+
sender?: AssessmentFeedbackSender;
|
|
21
|
+
}): Promise<ReportingStatus>;
|
package/dist/feedback.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { packageVersion } from './version.js';
|
|
2
|
+
import { reviewReportInputSchema } from './report-contract.js';
|
|
3
|
+
import { reportedUsage } from './providers.js';
|
|
4
|
+
/** An unresolved criterion can still have independently verified, actionable finding accounts. */
|
|
5
|
+
function reportedFinding(state, entry) {
|
|
6
|
+
if (entry.result.disposition !== 'unresolved')
|
|
7
|
+
return 'finding' in entry.result ? entry.result.finding : 'none';
|
|
8
|
+
const classes = state.findings
|
|
9
|
+
.filter((finding) => finding.verified && finding.assessmentIds.includes(entry.assessmentId))
|
|
10
|
+
.map((finding) => finding.classification);
|
|
11
|
+
return (['binding_violation', 'behavioral_defect', 'guidance_departure'].find((classification) => classes.includes(classification)) ?? 'none');
|
|
12
|
+
}
|
|
13
|
+
function externalEvidenceCounts(state, assessmentId) {
|
|
14
|
+
const counts = { external_dependency: 0, operational_state: 0, business_record: 0 };
|
|
15
|
+
for (const item of state.followUps ?? []) {
|
|
16
|
+
if (item.assessmentId !== assessmentId ||
|
|
17
|
+
item.kind !== 'external_evidence' ||
|
|
18
|
+
!item.verified ||
|
|
19
|
+
item.required ||
|
|
20
|
+
!item.externalEvidence)
|
|
21
|
+
continue;
|
|
22
|
+
counts[item.externalEvidence.kind]++;
|
|
23
|
+
}
|
|
24
|
+
return Object.values(counts).some((count) => count > 0) ? { externalEvidence: counts } : {};
|
|
25
|
+
}
|
|
26
|
+
/** Construct from the allowlist. Never serialize local state then remove fields. */
|
|
27
|
+
export function projectAssessmentReport(state, repositoryAttribution = false) {
|
|
28
|
+
if (!state.reviewer || !state.decision || state.assessmentStatus === 'running')
|
|
29
|
+
throw new Error('assessment_not_finalized');
|
|
30
|
+
const e = state.execution;
|
|
31
|
+
const phaseProviders = Object.values(e.phaseModels ?? { shared: e.requested });
|
|
32
|
+
const providers = new Set(phaseProviders.map((profile) => profile.provider));
|
|
33
|
+
const models = new Set(phaseProviders.map((profile) => profile.model));
|
|
34
|
+
const stop = {
|
|
35
|
+
finished: 'finished',
|
|
36
|
+
preparation_incomplete: 'preparation_incomplete',
|
|
37
|
+
cancelled: 'cancelled',
|
|
38
|
+
max_time_ms: 'timeout',
|
|
39
|
+
max_turns: 'max_turns',
|
|
40
|
+
model_call_limit: 'max_turns',
|
|
41
|
+
max_total_tokens: 'max_total_tokens',
|
|
42
|
+
usage_unavailable: 'usage_unavailable',
|
|
43
|
+
provider_error: 'provider_error',
|
|
44
|
+
execution_error: 'execution_error',
|
|
45
|
+
report_delivery_failed: 'report_delivery_failed',
|
|
46
|
+
per_call_output_limit: 'output_limit',
|
|
47
|
+
model_stopped: 'model_stopped',
|
|
48
|
+
total_token_limit_unmeasurable: 'usage_unavailable',
|
|
49
|
+
};
|
|
50
|
+
const repository = state.artifact.repositoryEnvironment;
|
|
51
|
+
return reviewReportInputSchema.parse({
|
|
52
|
+
kind: 'review_report',
|
|
53
|
+
contractVersion: 1,
|
|
54
|
+
runId: state.runId,
|
|
55
|
+
origin: 'npm',
|
|
56
|
+
packageVersion,
|
|
57
|
+
methodVersion: state.concernValidation ? 'candidate-adherence-v3' : 'artifact-review-v1',
|
|
58
|
+
reviewer: { nodeId: state.reviewer.nodeId, version: state.reviewer.version },
|
|
59
|
+
repository: repositoryAttribution
|
|
60
|
+
? repository.status === 'reported'
|
|
61
|
+
? { status: 'reported', provider: repository.provider, owner: repository.owner, name: repository.name }
|
|
62
|
+
: { status: 'unavailable', reason: repository.reason }
|
|
63
|
+
: { status: 'unavailable', reason: 'withheld' },
|
|
64
|
+
scope: {
|
|
65
|
+
enumeration: state.preparationStatus === 'prepared' ? 'complete' : 'incomplete',
|
|
66
|
+
expectedCount: state.expectedContextCount,
|
|
67
|
+
expected: state.entries.map((entry) => ({
|
|
68
|
+
entryId: entry.assessmentId,
|
|
69
|
+
context: { nodeId: entry.nodeId, version: entry.version },
|
|
70
|
+
})),
|
|
71
|
+
},
|
|
72
|
+
outcomes: state.entries.map((entry) => ({
|
|
73
|
+
entryId: entry.assessmentId,
|
|
74
|
+
disposition: entry.result.disposition,
|
|
75
|
+
adherence: entry.result.adherence,
|
|
76
|
+
confidence: entry.result.confidence,
|
|
77
|
+
finding: reportedFinding(state, entry),
|
|
78
|
+
blocking: state.decision.reasons.some((reason) => reason.assessmentId === entry.assessmentId && reason.blocking),
|
|
79
|
+
...externalEvidenceCounts(state, entry.assessmentId),
|
|
80
|
+
})),
|
|
81
|
+
runner: {
|
|
82
|
+
policy: {
|
|
83
|
+
enforcement: e.policy.enforcement,
|
|
84
|
+
},
|
|
85
|
+
reviewExit: state.decision.reviewExit,
|
|
86
|
+
exitCode: state.decision.exitCode,
|
|
87
|
+
},
|
|
88
|
+
completion: state.assessmentStatus,
|
|
89
|
+
...(state.concernValidation ? { concernValidation: state.concernValidation } : {}),
|
|
90
|
+
stopReason: stop[e.stopReason ?? ''] ?? 'execution_error',
|
|
91
|
+
telemetry: {
|
|
92
|
+
durationMs: e.elapsedMs,
|
|
93
|
+
provider: providers.size === 1 ? phaseProviders[0].provider : null,
|
|
94
|
+
model: providers.size === 1 && models.size === 1 ? phaseProviders[0].model : null,
|
|
95
|
+
harness: 'contextrail-assess',
|
|
96
|
+
returned: e.returnedIdentities.map((id) => ({ provider: id.provider, model: id.model })),
|
|
97
|
+
calls: e.calls,
|
|
98
|
+
usage: Array.from({ length: e.calls }, (_, i) => {
|
|
99
|
+
const call = e.providerUsage.find((u) => u.call === i + 1);
|
|
100
|
+
const u = reportedUsage(call?.provider ?? e.requested.provider, call?.usage);
|
|
101
|
+
return {
|
|
102
|
+
call: i + 1,
|
|
103
|
+
inputTokens: u.inputTokens,
|
|
104
|
+
outputTokens: u.outputTokens,
|
|
105
|
+
totalTokens: u.totalTokens,
|
|
106
|
+
cachedInputTokens: u.cachedInputTokens,
|
|
107
|
+
reasoningTokens: u.reasoningTokens,
|
|
108
|
+
};
|
|
109
|
+
}),
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
export async function sendAssessmentReport(state, options) {
|
|
114
|
+
if (options.mode === 'off')
|
|
115
|
+
return { status: 'off' };
|
|
116
|
+
let report;
|
|
117
|
+
try {
|
|
118
|
+
report = projectAssessmentReport(state, options.repositoryAttribution);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return { status: 'failed', code: 'projection_invalid' };
|
|
122
|
+
}
|
|
123
|
+
if (!options.sender)
|
|
124
|
+
return { status: 'failed', code: 'sender_unavailable' };
|
|
125
|
+
try {
|
|
126
|
+
const raw = await options.sender.submitReport(report);
|
|
127
|
+
if (!raw || typeof raw !== 'object')
|
|
128
|
+
return { status: 'failed', code: 'receipt_invalid' };
|
|
129
|
+
const receipt = raw;
|
|
130
|
+
// A hostile response must never become an arbitrary local reporting metadata bag.
|
|
131
|
+
if (receipt.runId !== state.runId ||
|
|
132
|
+
typeof receipt.duplicate !== 'boolean' ||
|
|
133
|
+
typeof receipt.eventId !== 'string' ||
|
|
134
|
+
!/^effectiveness:\/\/[A-Za-z0-9_/-]+$/u.test(receipt.eventId) ||
|
|
135
|
+
receipt.eventId.length > 500 ||
|
|
136
|
+
typeof receipt.receivedAt !== 'string' ||
|
|
137
|
+
!/^\d{4}-\d{2}-\d{2}T[\d:.]+Z$/u.test(receipt.receivedAt) ||
|
|
138
|
+
!Number.isFinite(Date.parse(receipt.receivedAt)))
|
|
139
|
+
return { status: 'failed', code: 'receipt_invalid' };
|
|
140
|
+
return {
|
|
141
|
+
status: 'received',
|
|
142
|
+
receipt: {
|
|
143
|
+
runId: receipt.runId,
|
|
144
|
+
eventId: receipt.eventId,
|
|
145
|
+
receivedAt: receipt.receivedAt,
|
|
146
|
+
duplicate: receipt.duplicate,
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
// No provider bodies, upstream error strings or unknown keys reach diagnostics.
|
|
152
|
+
return { status: 'failed', code: 'submission_failed' };
|
|
153
|
+
}
|
|
154
|
+
}
|
package/dist/git.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export declare const MAX_PREPARATION_BYTES: number;
|
|
2
|
+
export declare function sensitiveGitPath(path: string): boolean;
|
|
3
|
+
/** Ordinary unified Git output, bounded as one complete artifact; never a model transformation. */
|
|
4
|
+
export declare function readableDiff(artifact: ReturnType<typeof fixGitIdentity>, options?: {
|
|
5
|
+
timeoutMs?: number;
|
|
6
|
+
maxBytes?: number;
|
|
7
|
+
}): {
|
|
8
|
+
patch: string;
|
|
9
|
+
stat: string;
|
|
10
|
+
files: string[];
|
|
11
|
+
excludedPaths: string[];
|
|
12
|
+
};
|
|
13
|
+
export declare function passiveGitEnvironment(): NodeJS.ProcessEnv;
|
|
14
|
+
export type RepositoryEnvironment = {
|
|
15
|
+
status: 'reported';
|
|
16
|
+
provider: string;
|
|
17
|
+
owner: string;
|
|
18
|
+
name: string;
|
|
19
|
+
} | {
|
|
20
|
+
status: 'unavailable';
|
|
21
|
+
reason: 'no_remote' | 'ambiguous_remote' | 'not_determined';
|
|
22
|
+
};
|
|
23
|
+
export declare function fixGitIdentity(repository: string, base: string, head: string): {
|
|
24
|
+
kind: "git_comparison";
|
|
25
|
+
repository: string;
|
|
26
|
+
repositoryEnvironment: RepositoryEnvironment;
|
|
27
|
+
base: {
|
|
28
|
+
suppliedRef: string;
|
|
29
|
+
commit: string;
|
|
30
|
+
tree: string;
|
|
31
|
+
};
|
|
32
|
+
head: {
|
|
33
|
+
suppliedRef: string;
|
|
34
|
+
commit: string;
|
|
35
|
+
tree: string;
|
|
36
|
+
};
|
|
37
|
+
limitations: string[];
|
|
38
|
+
};
|
|
39
|
+
export declare function repositoryEnvironment(root: string): RepositoryEnvironment;
|
|
40
|
+
export declare function parseRepositoryRemote(remote: string | undefined): RepositoryEnvironment;
|