@kontourai/survey 5.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Support verification: a port for an independent check of whether a
3
+ * candidate's value is supported by its evidence, and the immutable record of
4
+ * what such a check said.
5
+ *
6
+ * A {@link CandidateVerification} is a record of a verifier's output, bound to
7
+ * the exact value and evidence it checked. It is not a trust decision: nothing
8
+ * in Survey reads it to route, auto-accept, or calibrate a candidate. Survey
9
+ * ships no verifier implementations; producers or separate adapter packages
10
+ * implement {@link SupportVerifier}.
11
+ *
12
+ * Runtime-neutral (no Node built-ins) so the review workbench bundle can
13
+ * validate records in the browser.
14
+ */
15
+ export declare const candidateVerificationKind: "survey.candidate-verification";
16
+ export type SupportVerificationMethod = "deterministic" | "model" | "human";
17
+ export type SupportVerificationResult = "supported" | "contradicted" | "not-addressed" | "abstain";
18
+ export type SupportAbstainReason = "malformed" | "empty" | "timeout" | "unsupported" | "error";
19
+ export interface SupportEvidence {
20
+ id: string;
21
+ excerpt: string;
22
+ locator: string;
23
+ context?: string;
24
+ }
25
+ export interface SupportVerificationInput {
26
+ candidateId: string;
27
+ value: unknown;
28
+ /** Declared value type when the producer knows it (for example `ReviewValueType`). */
29
+ valueType?: string;
30
+ /** At least one item; ids are unique. */
31
+ evidence: SupportEvidence[];
32
+ }
33
+ /**
34
+ * What a verifier returns. `score` has no meaning unless a calibration record
35
+ * says otherwise: it is optional, never defaulted, and never feeds a
36
+ * confidence. An abstention carries its reason and no score.
37
+ */
38
+ export type SupportVerdict = {
39
+ result: "supported" | "contradicted" | "not-addressed";
40
+ score?: number;
41
+ } | {
42
+ result: "abstain";
43
+ abstainReason: SupportAbstainReason;
44
+ };
45
+ /** The port. Implementations live with producers or in adapter packages. */
46
+ export interface SupportVerifier {
47
+ readonly id: string;
48
+ readonly version: string;
49
+ readonly method: SupportVerificationMethod;
50
+ verify(input: Readonly<SupportVerificationInput>, options?: {
51
+ signal?: AbortSignal;
52
+ }): Promise<SupportVerdict>;
53
+ }
54
+ export interface CandidateVerification {
55
+ /** `sha256:` digest of every other field; recomputed on validation. */
56
+ id: string;
57
+ kind: typeof candidateVerificationKind;
58
+ schemaVersion: 1;
59
+ candidateId: string;
60
+ /** Sorted, unique ids of the evidence the verifier was given. */
61
+ evidenceIds: string[];
62
+ /** {@link valueDigest} of the value checked. */
63
+ valueDigest: string;
64
+ /** {@link supportVerificationInputDigest} of the full input checked. */
65
+ inputDigest: string;
66
+ verifier: {
67
+ id: string;
68
+ version: string;
69
+ };
70
+ method: SupportVerificationMethod;
71
+ result: SupportVerificationResult;
72
+ /** Present exactly when `result` is `abstain`. */
73
+ abstainReason?: SupportAbstainReason;
74
+ /** Uncalibrated unless a calibration record says otherwise. Never defaulted. */
75
+ score?: number;
76
+ createdAt: string;
77
+ }
78
+ export interface BuildCandidateVerificationInput {
79
+ input: SupportVerificationInput;
80
+ verifier: {
81
+ id: string;
82
+ version: string;
83
+ method: SupportVerificationMethod;
84
+ };
85
+ verdict: SupportVerdict;
86
+ createdAt: string;
87
+ }
88
+ /**
89
+ * `"sha256:" + hex SHA-256` of the canonical JSON of a value: the same
90
+ * definition as Surface's `valueDigest`, so a record binds to a value the way a
91
+ * Surface claim does.
92
+ */
93
+ export declare function valueDigest(value: unknown): string;
94
+ /** Digest of everything a verifier was given: candidate id, value, value type and evidence (ordered by id). */
95
+ export declare function supportVerificationInputDigest(input: SupportVerificationInput): string;
96
+ /**
97
+ * Builds a frozen record from a verifier's verdict. Throws on a malformed
98
+ * verdict: use {@link runSupportVerifier} to turn a verifier's failure into an
99
+ * abstention instead.
100
+ */
101
+ export declare function buildCandidateVerification(input: BuildCandidateVerificationInput): CandidateVerification;
102
+ /** How long {@link runSupportVerifier} waits when no `timeoutMs` is given. */
103
+ export declare const DEFAULT_SUPPORT_VERIFIER_TIMEOUT_MS = 30000;
104
+ /**
105
+ * The largest `timeoutMs` {@link runSupportVerifier} accepts: the largest
106
+ * delay a timer honours (2^31 - 1 ms). A larger delay would be clamped to
107
+ * 1 ms, turning a long bound into an immediate timeout, so it is refused.
108
+ */
109
+ export declare const MAX_SUPPORT_VERIFIER_TIMEOUT_MS = 2147483647;
110
+ export interface RunSupportVerifierOptions {
111
+ /**
112
+ * Abstain with `timeout` when the verifier has not answered by then.
113
+ * Defaults to {@link DEFAULT_SUPPORT_VERIFIER_TIMEOUT_MS}; a verifier is
114
+ * never waited on without a bound. At most
115
+ * {@link MAX_SUPPORT_VERIFIER_TIMEOUT_MS}.
116
+ */
117
+ timeoutMs?: number;
118
+ /** Clock for `createdAt`; defaults to the current time. */
119
+ now?: () => string;
120
+ }
121
+ /**
122
+ * Calls a verifier and records what it said. A verifier that throws abstains
123
+ * with `error`, one that returns nothing with `empty`, one that returns a
124
+ * malformed verdict with `malformed`, and one that exceeds `timeoutMs`
125
+ * (default {@link DEFAULT_SUPPORT_VERIFIER_TIMEOUT_MS}) with `timeout`. None of these ever records a pass or a fail.
126
+ *
127
+ * Throws only for caller errors: a malformed input, or a verifier without a
128
+ * usable identity (a record needs one).
129
+ */
130
+ export declare function runSupportVerifier(verifier: SupportVerifier, input: SupportVerificationInput, options?: RunSupportVerifierOptions): Promise<CandidateVerification>;
131
+ /**
132
+ * Validates a record read back from storage and returns a frozen copy. Throws
133
+ * on any unknown field, inconsistent verdict, or an id that is not the digest
134
+ * of the record's own fields (an edited or inconsistent record). The id is a
135
+ * content digest, not a signature: it does not prove who wrote the record, and
136
+ * a record rebuilt with a recomputed id validates.
137
+ */
138
+ export declare function validateCandidateVerification(value: unknown): CandidateVerification;
139
+ /** The candidate a fold reads records for: its id and its value as it is now. */
140
+ export interface CandidateVerificationSubject {
141
+ candidateId: string;
142
+ value: unknown;
143
+ valueType?: string;
144
+ /** When supplied, records must also match the full input digest. */
145
+ evidence?: SupportEvidence[];
146
+ }
147
+ export type CandidateVerificationStatus = "not-evaluated" | "abstained" | "evaluated";
148
+ export type CandidateVerificationInapplicableReason = "other-candidate" | "value-changed" | "input-changed";
149
+ export interface FoldCandidateVerificationsResult {
150
+ /**
151
+ * `not-evaluated` when no record applies to the subject's current value,
152
+ * `abstained` when every applicable record is an abstention, `evaluated`
153
+ * when at least one applicable record carries a verdict (supported,
154
+ * contradicted or not-addressed). Not itself a verdict.
155
+ */
156
+ status: CandidateVerificationStatus;
157
+ /** Valid records bound to this candidate and value, ordered by id. Abstentions included. */
158
+ applicable: readonly CandidateVerification[];
159
+ /** Valid records bound to another candidate, value, or input. */
160
+ inapplicable: readonly {
161
+ record: CandidateVerification;
162
+ reason: CandidateVerificationInapplicableReason;
163
+ }[];
164
+ /** Records that failed validation, by input position. Never read further. */
165
+ rejected: readonly {
166
+ index: number;
167
+ reason: string;
168
+ }[];
169
+ }
170
+ /**
171
+ * Sorts records for one candidate into those that apply to its current value,
172
+ * those bound to something else, and those that failed validation. Never
173
+ * selects a winner among applicable records and never derives a trust state.
174
+ */
175
+ export declare function foldCandidateVerifications(subject: CandidateVerificationSubject, records: readonly unknown[]): FoldCandidateVerificationsResult;
@@ -0,0 +1,334 @@
1
+ import { sha256Hex } from "./sha256.js";
2
+ /**
3
+ * Support verification: a port for an independent check of whether a
4
+ * candidate's value is supported by its evidence, and the immutable record of
5
+ * what such a check said.
6
+ *
7
+ * A {@link CandidateVerification} is a record of a verifier's output, bound to
8
+ * the exact value and evidence it checked. It is not a trust decision: nothing
9
+ * in Survey reads it to route, auto-accept, or calibrate a candidate. Survey
10
+ * ships no verifier implementations; producers or separate adapter packages
11
+ * implement {@link SupportVerifier}.
12
+ *
13
+ * Runtime-neutral (no Node built-ins) so the review workbench bundle can
14
+ * validate records in the browser.
15
+ */
16
+ export const candidateVerificationKind = "survey.candidate-verification";
17
+ const METHODS = ["deterministic", "model", "human"];
18
+ const RESULTS = ["supported", "contradicted", "not-addressed", "abstain"];
19
+ const ABSTAIN_REASONS = ["malformed", "empty", "timeout", "unsupported", "error"];
20
+ /**
21
+ * Canonical JSON with object keys sorted by UTF-16 code unit, matching
22
+ * `canonicalJson` in `@kontourai/surface` (`src/canonical-digest.ts`) byte for
23
+ * byte. Survey's review-workbench `canonicalJson` sorts with `localeCompare`
24
+ * and would disagree on mixed-case keys, so it is not used here.
25
+ */
26
+ function surfaceCanonicalJson(value) {
27
+ if (value === null || typeof value !== "object")
28
+ return JSON.stringify(value);
29
+ if (Array.isArray(value))
30
+ return `[${value.map(surfaceCanonicalJson).join(",")}]`;
31
+ return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${surfaceCanonicalJson(value[key])}`).join(",")}}`;
32
+ }
33
+ /**
34
+ * `"sha256:" + hex SHA-256` of the canonical JSON of a value: the same
35
+ * definition as Surface's `valueDigest`, so a record binds to a value the way a
36
+ * Surface claim does.
37
+ */
38
+ export function valueDigest(value) {
39
+ return `sha256:${sha256Hex(surfaceCanonicalJson(value))}`;
40
+ }
41
+ function digest(domain, payload) {
42
+ return `sha256:${sha256Hex(surfaceCanonicalJson({ domain, payload }))}`;
43
+ }
44
+ /** Digest of everything a verifier was given: candidate id, value, value type and evidence (ordered by id). */
45
+ export function supportVerificationInputDigest(input) {
46
+ const normalized = normalizeInput(input);
47
+ return digest("survey.support-verification-input/v1", {
48
+ candidateId: normalized.candidateId,
49
+ valueDigest: valueDigest(normalized.value),
50
+ ...(normalized.valueType !== undefined ? { valueType: normalized.valueType } : {}),
51
+ evidence: normalized.evidence,
52
+ });
53
+ }
54
+ /**
55
+ * Builds a frozen record from a verifier's verdict. Throws on a malformed
56
+ * verdict: use {@link runSupportVerifier} to turn a verifier's failure into an
57
+ * abstention instead.
58
+ */
59
+ export function buildCandidateVerification(input) {
60
+ const normalized = normalizeInput(input.input);
61
+ const verifier = normalizeVerifier(input.verifier);
62
+ const verdict = normalizeVerdict(input.verdict);
63
+ const payload = {
64
+ kind: candidateVerificationKind,
65
+ schemaVersion: 1,
66
+ candidateId: normalized.candidateId,
67
+ evidenceIds: normalized.evidence.map(({ id }) => id),
68
+ valueDigest: valueDigest(normalized.value),
69
+ inputDigest: supportVerificationInputDigest(normalized),
70
+ verifier: { id: verifier.id, version: verifier.version },
71
+ method: verifier.method,
72
+ result: verdict.result,
73
+ ...(verdict.result === "abstain" ? { abstainReason: verdict.abstainReason } : {}),
74
+ ...(verdict.result !== "abstain" && verdict.score !== undefined ? { score: verdict.score } : {}),
75
+ createdAt: canonicalTimestamp(input.createdAt, "createdAt"),
76
+ };
77
+ return deepFreeze({ id: recordId(payload), ...payload });
78
+ }
79
+ /** How long {@link runSupportVerifier} waits when no `timeoutMs` is given. */
80
+ export const DEFAULT_SUPPORT_VERIFIER_TIMEOUT_MS = 30_000;
81
+ /**
82
+ * The largest `timeoutMs` {@link runSupportVerifier} accepts: the largest
83
+ * delay a timer honours (2^31 - 1 ms). A larger delay would be clamped to
84
+ * 1 ms, turning a long bound into an immediate timeout, so it is refused.
85
+ */
86
+ export const MAX_SUPPORT_VERIFIER_TIMEOUT_MS = 2_147_483_647;
87
+ /**
88
+ * Calls a verifier and records what it said. A verifier that throws abstains
89
+ * with `error`, one that returns nothing with `empty`, one that returns a
90
+ * malformed verdict with `malformed`, and one that exceeds `timeoutMs`
91
+ * (default {@link DEFAULT_SUPPORT_VERIFIER_TIMEOUT_MS}) with `timeout`. None of these ever records a pass or a fail.
92
+ *
93
+ * Throws only for caller errors: a malformed input, or a verifier without a
94
+ * usable identity (a record needs one).
95
+ */
96
+ export async function runSupportVerifier(verifier, input, options = {}) {
97
+ const identity = normalizeVerifier(verifier);
98
+ const normalized = normalizeInput(input);
99
+ const timeoutMs = options.timeoutMs ?? DEFAULT_SUPPORT_VERIFIER_TIMEOUT_MS;
100
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0 || timeoutMs > MAX_SUPPORT_VERIFIER_TIMEOUT_MS) {
101
+ throw new Error(`timeoutMs must be a positive finite number no greater than ${MAX_SUPPORT_VERIFIER_TIMEOUT_MS}`);
102
+ }
103
+ const now = options.now ?? (() => new Date().toISOString());
104
+ const verdict = await callVerifier(verifier, normalized, timeoutMs);
105
+ return buildCandidateVerification({ input: normalized, verifier: identity, verdict, createdAt: now() });
106
+ }
107
+ async function callVerifier(verifier, input, timeoutMs) {
108
+ const controller = new AbortController();
109
+ let timer;
110
+ const timedOut = Symbol("timeout");
111
+ // The verifier gets its own frozen copy: it cannot change what the record binds to.
112
+ const frozenInput = deepFreeze(structuredClone(input));
113
+ try {
114
+ const call = Promise.resolve().then(() => verifier.verify(frozenInput, { signal: controller.signal }));
115
+ const raced = await Promise.race([call, new Promise((resolve) => { timer = setTimeout(() => resolve(timedOut), timeoutMs); })]);
116
+ if (raced === timedOut) {
117
+ controller.abort();
118
+ call.catch(() => undefined);
119
+ return { result: "abstain", abstainReason: "timeout" };
120
+ }
121
+ if (raced === undefined || raced === null)
122
+ return { result: "abstain", abstainReason: "empty" };
123
+ try {
124
+ return normalizeVerdict(raced);
125
+ }
126
+ catch {
127
+ return { result: "abstain", abstainReason: "malformed" };
128
+ }
129
+ }
130
+ catch {
131
+ return { result: "abstain", abstainReason: "error" };
132
+ }
133
+ finally {
134
+ if (timer !== undefined)
135
+ clearTimeout(timer);
136
+ }
137
+ }
138
+ const RECORD_KEYS = new Set(["id", "kind", "schemaVersion", "candidateId", "evidenceIds", "valueDigest", "inputDigest", "verifier", "method", "result", "abstainReason", "score", "createdAt"]);
139
+ /**
140
+ * Validates a record read back from storage and returns a frozen copy. Throws
141
+ * on any unknown field, inconsistent verdict, or an id that is not the digest
142
+ * of the record's own fields (an edited or inconsistent record). The id is a
143
+ * content digest, not a signature: it does not prove who wrote the record, and
144
+ * a record rebuilt with a recomputed id validates.
145
+ */
146
+ export function validateCandidateVerification(value) {
147
+ const record = requiredObject(value, "CandidateVerification");
148
+ for (const key of Object.keys(record)) {
149
+ if (!RECORD_KEYS.has(key))
150
+ throw new Error(`CandidateVerification has unknown field ${key}`);
151
+ }
152
+ if (record.kind !== candidateVerificationKind)
153
+ throw new Error(`CandidateVerification kind must be ${candidateVerificationKind}`);
154
+ if (record.schemaVersion !== 1)
155
+ throw new Error("CandidateVerification schemaVersion must be 1");
156
+ const evidenceIds = record.evidenceIds;
157
+ if (!Array.isArray(evidenceIds) || evidenceIds.length === 0)
158
+ throw new Error("CandidateVerification evidenceIds must be a non-empty array");
159
+ evidenceIds.forEach((id, index) => {
160
+ nonEmpty(id, `evidenceIds[${index}]`);
161
+ if (index > 0 && !(evidenceIds[index - 1] < id))
162
+ throw new Error("CandidateVerification evidenceIds must be sorted and unique");
163
+ });
164
+ const verifier = requiredObject(record.verifier, "verifier");
165
+ if (Object.keys(verifier).some((key) => key !== "id" && key !== "version"))
166
+ throw new Error("CandidateVerification verifier has an unknown field");
167
+ const verdict = normalizeVerdict({
168
+ result: record.result,
169
+ ...("abstainReason" in record ? { abstainReason: record.abstainReason } : {}),
170
+ ...("score" in record ? { score: record.score } : {}),
171
+ });
172
+ const payload = {
173
+ kind: candidateVerificationKind,
174
+ schemaVersion: 1,
175
+ candidateId: nonEmpty(record.candidateId, "candidateId"),
176
+ evidenceIds: evidenceIds,
177
+ valueDigest: sha256Digest(record.valueDigest, "valueDigest"),
178
+ inputDigest: sha256Digest(record.inputDigest, "inputDigest"),
179
+ verifier: { id: nonEmpty(verifier.id, "verifier.id"), version: nonEmpty(verifier.version, "verifier.version") },
180
+ method: oneOf(record.method, METHODS, "method"),
181
+ result: verdict.result,
182
+ ...(verdict.result === "abstain" ? { abstainReason: verdict.abstainReason } : {}),
183
+ ...(verdict.result !== "abstain" && verdict.score !== undefined ? { score: verdict.score } : {}),
184
+ createdAt: canonicalTimestamp(record.createdAt, "createdAt"),
185
+ };
186
+ const id = recordId(payload);
187
+ if (record.id !== id)
188
+ throw new Error("CandidateVerification id is not the digest of its fields");
189
+ return deepFreeze({ id, ...structuredClone(payload) });
190
+ }
191
+ /**
192
+ * Sorts records for one candidate into those that apply to its current value,
193
+ * those bound to something else, and those that failed validation. Never
194
+ * selects a winner among applicable records and never derives a trust state.
195
+ */
196
+ export function foldCandidateVerifications(subject, records) {
197
+ const candidateId = nonEmpty(subject.candidateId, "candidateId");
198
+ const currentValueDigest = valueDigest(subject.value);
199
+ const currentInputDigest = subject.evidence === undefined
200
+ ? undefined
201
+ : supportVerificationInputDigest({ candidateId, value: subject.value, ...(subject.valueType !== undefined ? { valueType: subject.valueType } : {}), evidence: subject.evidence });
202
+ const applicable = new Map();
203
+ const inapplicable = new Map();
204
+ const rejected = [];
205
+ records.forEach((raw, index) => {
206
+ let record;
207
+ try {
208
+ record = validateCandidateVerification(raw);
209
+ }
210
+ catch (error) {
211
+ rejected.push(Object.freeze({ index, reason: error instanceof Error ? error.message : String(error) }));
212
+ return;
213
+ }
214
+ const reason = record.candidateId !== candidateId
215
+ ? "other-candidate"
216
+ : record.valueDigest !== currentValueDigest
217
+ ? "value-changed"
218
+ : currentInputDigest !== undefined && record.inputDigest !== currentInputDigest
219
+ ? "input-changed"
220
+ : undefined;
221
+ if (reason)
222
+ inapplicable.set(record.id, Object.freeze({ record, reason }));
223
+ else
224
+ applicable.set(record.id, record);
225
+ });
226
+ const byId = (entries) => [...entries.keys()].sort().map((id) => entries.get(id));
227
+ const applicableRecords = byId(applicable);
228
+ return Object.freeze({
229
+ status: applicableRecords.length === 0
230
+ ? "not-evaluated"
231
+ : applicableRecords.every((record) => record.result === "abstain") ? "abstained" : "evaluated",
232
+ applicable: Object.freeze(applicableRecords),
233
+ inapplicable: Object.freeze(byId(inapplicable)),
234
+ rejected: Object.freeze(rejected),
235
+ });
236
+ }
237
+ function recordId(payload) {
238
+ return digest("survey.candidate-verification/v1", payload);
239
+ }
240
+ function normalizeInput(input) {
241
+ const value = requiredObject(input, "SupportVerificationInput");
242
+ if (!("value" in value))
243
+ throw new Error("SupportVerificationInput value is required");
244
+ if (!Array.isArray(value.evidence) || value.evidence.length === 0)
245
+ throw new Error("SupportVerificationInput evidence must be a non-empty array");
246
+ const evidence = value.evidence.map((entry, index) => {
247
+ const item = requiredObject(entry, `evidence[${index}]`);
248
+ return {
249
+ id: nonEmpty(item.id, `evidence[${index}].id`),
250
+ excerpt: text(item.excerpt, `evidence[${index}].excerpt`),
251
+ locator: nonEmpty(item.locator, `evidence[${index}].locator`),
252
+ ...(item.context !== undefined ? { context: text(item.context, `evidence[${index}].context`) } : {}),
253
+ };
254
+ }).sort((left, right) => left.id < right.id ? -1 : left.id > right.id ? 1 : 0);
255
+ evidence.forEach((entry, index) => {
256
+ if (index > 0 && evidence[index - 1].id === entry.id)
257
+ throw new Error(`SupportVerificationInput evidence id ${entry.id} is repeated`);
258
+ });
259
+ return {
260
+ candidateId: nonEmpty(value.candidateId, "candidateId"),
261
+ value: value.value,
262
+ ...(value.valueType !== undefined ? { valueType: nonEmpty(value.valueType, "valueType") } : {}),
263
+ evidence,
264
+ };
265
+ }
266
+ function normalizeVerifier(value) {
267
+ const verifier = requiredObject(value, "verifier");
268
+ return {
269
+ id: nonEmpty(verifier.id, "verifier.id"),
270
+ version: nonEmpty(verifier.version, "verifier.version"),
271
+ method: oneOf(verifier.method, METHODS, "verifier.method"),
272
+ };
273
+ }
274
+ function normalizeVerdict(value) {
275
+ const verdict = requiredObject(value, "verdict");
276
+ for (const key of Object.keys(verdict)) {
277
+ if (key !== "result" && key !== "abstainReason" && key !== "score")
278
+ throw new Error(`verdict has unknown field ${key}`);
279
+ }
280
+ const result = oneOf(verdict.result, RESULTS, "verdict.result");
281
+ if (result === "abstain") {
282
+ if ("score" in verdict)
283
+ throw new Error("an abstaining verdict carries no score");
284
+ return { result, abstainReason: oneOf(verdict.abstainReason, ABSTAIN_REASONS, "verdict.abstainReason") };
285
+ }
286
+ if ("abstainReason" in verdict)
287
+ throw new Error("abstainReason is only allowed when result is abstain");
288
+ if (!("score" in verdict))
289
+ return { result };
290
+ const score = verdict.score;
291
+ if (typeof score !== "number" || !Number.isFinite(score) || score < 0 || score > 1)
292
+ throw new Error("verdict.score must be a number from 0 to 1");
293
+ return { result, score };
294
+ }
295
+ function oneOf(value, allowed, label) {
296
+ if (typeof value !== "string" || !allowed.includes(value))
297
+ throw new Error(`${label} must be one of ${allowed.join(", ")}`);
298
+ return value;
299
+ }
300
+ function requiredObject(value, label) {
301
+ if (!value || typeof value !== "object" || Array.isArray(value))
302
+ throw new Error(`${label} must be an object`);
303
+ return value;
304
+ }
305
+ function nonEmpty(value, label) {
306
+ if (typeof value !== "string" || value.trim() === "")
307
+ throw new Error(`${label} must be a non-empty string`);
308
+ return value;
309
+ }
310
+ function text(value, label) {
311
+ if (typeof value !== "string")
312
+ throw new Error(`${label} must be a string`);
313
+ return value;
314
+ }
315
+ function sha256Digest(value, label) {
316
+ if (typeof value !== "string" || !/^sha256:[0-9a-f]{64}$/.test(value))
317
+ throw new Error(`${label} must be a sha256:<64 hex> digest`);
318
+ return value;
319
+ }
320
+ function canonicalTimestamp(value, label) {
321
+ const timestamp = nonEmpty(value, label);
322
+ const parsed = new Date(timestamp);
323
+ if (Number.isNaN(parsed.valueOf()) || parsed.toISOString() !== timestamp)
324
+ throw new Error(`${label} must be a canonical ISO timestamp`);
325
+ return timestamp;
326
+ }
327
+ function deepFreeze(value) {
328
+ if (value && typeof value === "object") {
329
+ for (const nested of Object.values(value))
330
+ deepFreeze(nested);
331
+ Object.freeze(value);
332
+ }
333
+ return value;
334
+ }
@@ -168,7 +168,39 @@ export interface ExtractionEnvelopeImportOptions {
168
168
  sourceKind: RawSource["kind"];
169
169
  /** Survey meaning is supplied at the boundary; it is not added to the upstream wire contract. */
170
170
  claimTarget: (proposal: PortableExtractionProposal, index: number) => ClaimTargetHint;
171
+ /**
172
+ * The prepared artifact as the caller resolved it (the same shape the source
173
+ * inspector takes). When supplied, the import checks the text against the
174
+ * envelope's prepared-artifact digest and length, and every proposal's span
175
+ * against its excerpt: a proposal whose span does not match gets no
176
+ * ReviewItem and an `excerpt-mismatch` diagnostic, and an artifact that does
177
+ * not verify makes the import `unresolved`. The text is not stored in the
178
+ * record. Without it the import records `status.provenance: "unverified"`.
179
+ */
180
+ artifact?: ResolvedExtractionArtifact;
171
181
  }
182
+ export type ArtifactUnavailableCode = "not-found" | "storage-error" | "access-denied" | "invalid-artifact" | "unknown";
183
+ /** A prepared artifact as a caller resolved it: its text and digest, or why it could not be read. */
184
+ export type ResolvedExtractionArtifact = {
185
+ status: "available";
186
+ text: string;
187
+ actualDigest: string;
188
+ } | {
189
+ status: "unavailable";
190
+ code: ArtifactUnavailableCode;
191
+ } | {
192
+ status: "digest-mismatch";
193
+ actualDigest: string;
194
+ };
195
+ /**
196
+ * Whether the import checked its proposals' excerpts against the prepared
197
+ * artifact text. `verified` means the supplied text matched the envelope's
198
+ * digest and length and every proposal that produced a ReviewItem matched its
199
+ * span; `unverified` means no text was supplied, or the text did not verify
200
+ * (a diagnostic then says why). Records written before this field existed have
201
+ * no `provenance` and are unverified.
202
+ */
203
+ export type ExtractionEnvelopeImportProvenance = "verified" | "unverified";
172
204
  /**
173
205
  * Why an import is `unresolved`: its prepared artifact did not resolve, or the
174
206
  * extraction itself produced nothing reviewable. `extraction-failed` is a
@@ -180,6 +212,8 @@ export type ExtractionEnvelopeImportDiagnostic = {
180
212
  kind: "artifact-unavailable";
181
213
  status: "unavailable" | "storage-error" | "identity-mismatch" | "invalid-artifact";
182
214
  artifactRef?: string;
215
+ /** The caller's resolver code, present when the artifact supplied at import could not be read. */
216
+ code?: ArtifactUnavailableCode;
183
217
  message: string;
184
218
  } | {
185
219
  kind: "digest-mismatch";
@@ -196,6 +230,13 @@ export type ExtractionEnvelopeImportDiagnostic = {
196
230
  kind: "extraction-incomplete";
197
231
  reason: PortableExtractionPartialReason;
198
232
  message: string;
233
+ }
234
+ /** The prepared text at this proposal's span is not its excerpt; the proposal produced no ReviewItem. */
235
+ | {
236
+ kind: "excerpt-mismatch";
237
+ proposalIndex: number;
238
+ locator: string;
239
+ message: string;
199
240
  };
200
241
  export interface ExtractionEnvelopeImport {
201
242
  apiVersion: typeof extractionEnvelopeImportApiVersion;
@@ -212,6 +253,7 @@ export interface ExtractionEnvelopeImport {
212
253
  status: {
213
254
  state: "grounded" | "unresolved";
214
255
  diagnostics: ExtractionEnvelopeImportDiagnostic[];
256
+ provenance?: ExtractionEnvelopeImportProvenance;
215
257
  };
216
258
  }
217
259
  export interface ExtractionEnvelopeImportResult {
@@ -242,5 +284,29 @@ export declare function buildReviewItemsFromExtractionEnvelopeImport(record: Ext
242
284
  export declare function exportExtractionEnvelopeImport(record: ExtractionEnvelopeImport): string;
243
285
  export declare function reimportExtractionEnvelope(serialized: string): ExtractionEnvelopeImport;
244
286
  export declare function createExtractionEnvelopeResolutionIdentity(record: ExtractionEnvelopeImport, proposalIndex: number): ExtractionEnvelopeResolutionIdentity;
287
+ /** Proposals the import excluded because their span did not match their excerpt. */
288
+ export declare function excerptMismatchProposalIndices(record: ExtractionEnvelopeImport): Set<number>;
289
+ /** Validate the caller-resolved artifact shape; resolver detail beyond the code is refused. */
290
+ export declare function assertResolvedExtractionArtifact(artifact: unknown): asserts artifact is ResolvedExtractionArtifact;
291
+ export type PreparedArtifactCheck = {
292
+ status: "aligned";
293
+ text: string;
294
+ } | {
295
+ status: "unavailable";
296
+ code: ArtifactUnavailableCode;
297
+ } | {
298
+ status: "digest-mismatch";
299
+ actualDigest: string;
300
+ } | {
301
+ status: "wrong-length";
302
+ };
303
+ /**
304
+ * Checks resolved text against the envelope's prepared-artifact identity: the
305
+ * caller's reported digest, the digest of the text itself, and the declared
306
+ * length. Shared by import-time verification and the source inspector.
307
+ */
308
+ export declare function checkPreparedArtifact(prepared: PortableExtractionResultEnvelope["result"]["preparedArtifact"], artifact: ResolvedExtractionArtifact): PreparedArtifactCheck;
309
+ /** Whether the verified prepared text at a proposal's `chars:` span is exactly its excerpt. */
310
+ export declare function proposalSpanMatches(text: string, proposal: PortableExtractionProposal): boolean;
245
311
  /** Validate and defensively clone a persisted or browser-delivered import. */
246
312
  export declare function validateExtractionEnvelopeImport(value: unknown): ExtractionEnvelopeImport;