Repository navigation
Expand file tree
/
Copy pathevidence.ts
More file actions
331 lines (320 loc) · 13.1 KB
/
Copy pathevidence.ts
File metadata and controls
331 lines (320 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import fs from 'node:fs';
import path from 'node:path';
import {
checkManyFormalVerifications,
type MathematicsVerifierBatchItem,
type MathematicsVerifierBatchRequest,
type MathematicsVerifierBatchResult,
type MathematicsVerifierExecutionOptions,
} from '../../math/verifier';
import { canonicalMathJson, mathDigest } from '../../math/mathRuntimeIo';
import { commitSolverSources, git, withSolverWorktree, type SolverProblem } from '../problem';
import { bindLeanPackages } from '../../math/leanDiagnostics';
import { findLeanTypeQueryDeclaration, hashMathFormalSourceDeclaration } from '../../math/leanSource';
import {
hasDeterministicLeanFailure,
hasLeanResourceLimitFailure,
proofReuseEnvironment,
proofVerificationDigest,
} from '../proofReuse';
import { checkpointVerification } from '../verificationCheckpoint';
import { VerifierFailureError } from '../verifierFailure';
export type ProofCheck = Readonly<{
commit: string;
sourceDigest: string;
passed: boolean;
verification: MathematicsVerifierBatchResult;
}>;
const checkedRejections = new WeakMap<ProofCheck, { inputDigest: string; verificationDigest: string }>();
const checkedProofs = new WeakMap<ProofCheck, string>();
const proofIdentity = (check: ProofCheck) =>
mathDigest(
canonicalMathJson({
commit: check.commit,
sourceDigest: check.sourceDigest,
passed: check.passed,
verification: check.verification,
}),
);
/** Serialized or modified receipts cannot narrow a fixed candidate's required reviews. */
export const hasFreshSolverCheck = (check: ProofCheck): boolean =>
check.passed && checkedProofs.get(check) === proofIdentity(check);
export const solverSourceDigest = (problem: SolverProblem, worktree: string) =>
mathDigest(
canonicalMathJson(
problem.ownedFiles.map(file => [
file,
mathDigest(fs.readFileSync(path.join(worktree, problem.projectRelative, file))),
]),
),
);
/** Reuse binds a recorded check to the current files as well as its committed tree. */
export function solverProofIsCurrent(problem: SolverProblem, worktree: string, check: ProofCheck): boolean {
return (
git(worktree, ['rev-parse', 'HEAD']) === check.commit &&
!git(worktree, [
'status',
'--porcelain',
'--',
...problem.ownedFiles.map(file => path.join(problem.projectRelative, file)),
]) &&
solverSourceDigest(problem, worktree) === check.sourceDigest
);
}
/** Match every receipt, in target order, to the exact committed proof request. */
export function proofReceiptsMatch(
verification: MathematicsVerifierBatchResult,
commit: string,
items: readonly MathematicsVerifierBatchItem[],
): boolean {
return (
verification.status === 'kernel_verified' &&
verification.targetCommit === commit &&
verification.results.length === items.length &&
verification.results.every(
(result, index) =>
result.status === 'kernel_verified' &&
result.targetCommit === commit &&
result.proofPath === items[index]!.proofPath &&
result.theoremName === items[index]!.theoremName &&
result.statementHash === items[index]!.statementHash &&
result.leanStatementHash === items[index]!.leanStatementHash &&
result.claim === 'statement' &&
result.sourceDeclarationHash === items[index]!.expectedSourceDeclarationHash,
)
);
}
type AttributedProofCheck = Pick<ProofCheck, 'passed' | 'verification'> & { attributable: boolean };
/** Slice only a fully bound batch; receipt identities and their original metadata stay intact. */
export function attributeProofEvidence(
verification: MathematicsVerifierBatchResult,
commit: string,
items: readonly MathematicsVerifierBatchItem[],
): AttributedProofCheck[] {
const matches = (index: number) => {
const result = verification.results[index],
item = items[index];
return Boolean(
result &&
item &&
result.targetCommit === commit &&
result.proofPath === item.proofPath &&
result.theoremName === item.theoremName &&
result.statementHash === item.statementHash &&
result.leanStatementHash === item.leanStatementHash &&
result.claim === 'statement' &&
result.sourceDeclarationHash === item.expectedSourceDeclarationHash,
);
};
if (items.length > 1) {
const status = verification.results.every(result => result.status === 'kernel_verified')
? 'kernel_verified'
: verification.results.some(result => result.status === 'failed')
? 'failed'
: verification.results.some(result => result.status === 'blocked')
? 'blocked'
: 'unavailable';
const distinct = (values: readonly string[]) =>
values.every(value => typeof value === 'string' && Boolean(value.trim())) &&
new Set(values).size === values.length;
if (
verification.schema !== 'deepscientist.mathematics.verification-batch/v1' ||
verification.targetCommit !== commit ||
typeof verification.sessionId !== 'string' ||
!verification.sessionId.trim() ||
verification.status !== status ||
verification.results.length !== items.length ||
!distinct(items.map(item => `${item.proofPath}\0${item.theoremName}`)) ||
!distinct(verification.results.map(result => result.receiptId)) ||
!distinct(verification.results.map(result => result.checkId)) ||
verification.results.some(
(result, index) =>
!matches(index) ||
result.sessionId !== verification.sessionId ||
result.checkId !== (items[index]!.id?.trim() || `check-${index + 1}`),
)
)
throw new VerifierFailureError(
{ stage: 'batch-receipt-attribution', sourceCommit: commit, verification },
undefined,
'math_task_check_identity_changed',
);
}
// Infrastructure failures belong to the shared invocation, even when aggregateStatus reports failed.
const interrupted = verification.results.some(
result =>
result.cleanupError ||
/\b(?:timed?\s*out|timeout|deadline|abort(?:ed)?|interrupt(?:ed)?)\b/iu.test(result.error ?? ''),
);
const unavailable =
verification.status === 'unavailable' || verification.results.some(result => result.status === 'unavailable');
const blocked =
interrupted ||
verification.status === 'blocked' ||
verification.results.some(result => result.status === 'blocked');
return items.map((item, index) => {
const result = verification.results[index];
const view: MathematicsVerifierBatchResult = {
...verification,
status: unavailable
? 'unavailable'
: blocked
? 'blocked'
: items.length > 1
? result!.status
: verification.status,
results: result ? [result] : [],
};
const passed = proofReceiptsMatch(view, commit, [item]);
// Only a single-target run binds a resource-limit failure to that target.
return {
verification: view,
passed,
attributable:
verification.targetCommit === commit &&
matches(index) &&
(passed ||
unavailable ||
blocked ||
hasDeterministicLeanFailure(view) ||
(items.length === 1 && hasLeanResourceLimitFailure(view))),
};
});
}
export async function checkProofEvidence(
request: MathematicsVerifierBatchRequest & { targetCommit: string },
options: MathematicsVerifierExecutionOptions,
): Promise<Pick<ProofCheck, 'passed' | 'verification'>> {
const verification = await checkpointVerification(
{ ...request, batchTimeoutMs: request.timeoutMs === Infinity ? Infinity : request.batchTimeoutMs },
bound =>
checkManyFormalVerifications(bound, {
...options,
trustedLakePackagesRoots: (options.env?.DEEPSCIENTIST_MATH_TRUSTED_CACHE_ROOTS || '')
.split(path.delimiter)
.filter(Boolean),
}),
);
return { verification, passed: proofReceiptsMatch(verification, request.targetCommit, request.items) };
}
export async function checkProjectedDeclarations(
problem: SolverProblem,
checkout: string,
sourceWorktree: string,
commit: string,
targets: readonly { proofPath: string; theoremName: string; digest: string; missingError: string }[],
options: MathematicsVerifierExecutionOptions & { timeoutMs: number },
) {
const projectRoot = path.join(checkout, problem.projectRelative);
const items = targets.map(target => {
const source = fs.readFileSync(path.join(projectRoot, target.proofPath), 'utf8');
const declarationHash = hashMathFormalSourceDeclaration(source, target.theoremName);
// The hash matches a local name; a typed check queries the exact name and would otherwise throw outside any receipt.
const typed =
(options.expectedDeclarationTypes?.[target.theoremName] ?? options.expectedDeclarationType) !== undefined;
if (!declarationHash || (typed && !findLeanTypeQueryDeclaration(source, target.theoremName)))
throw new Error(target.missingError);
return {
proofPath: target.proofPath,
theoremName: target.theoremName,
expectedSourceDeclarationHash: declarationHash,
statementHash: target.digest,
leanStatementHash: target.digest,
claim: 'statement' as const,
};
});
const packages = path.join(sourceWorktree, problem.projectRelative, '.lake', 'packages');
if (fs.existsSync(packages)) bindLeanPackages(projectRoot, packages);
const { timeoutMs, ...execution } = options;
const evidence = await checkProofEvidence({ projectRoot, targetCommit: commit, timeoutMs, items }, execution);
options.signal?.throwIfAborted();
return { ...evidence, attributions: attributeProofEvidence(evidence.verification, commit, items) };
}
/** M2: exact-source Lean evidence. Reused rejections must come from this process and pinned environment. */
export async function checkSolverProof(
problem: SolverProblem,
worktree: string,
env: NodeJS.ProcessEnv,
signal?: AbortSignal,
previous?: ProofCheck,
timeoutMs = 180_000,
collectDirectProofReferences = false,
): Promise<ProofCheck> {
signal?.throwIfAborted();
const commit = commitSolverSources(problem, worktree);
const projectRoot = path.join(worktree, problem.projectRelative);
const sourceDigest = solverSourceDigest(problem, worktree);
const environment = proofReuseEnvironment(projectRoot, env);
const inputDigest = environment.reusable
? mathDigest(
canonicalMathJson({
tree: git(worktree, ['ls-tree', '-r', '-z', '--full-tree', 'HEAD'])
.split('\0')
.filter(entry => entry && entry.slice(entry.indexOf('\t') + 1) !== 'RESEARCH.md'),
problem: problem.receiptDigest,
kind: problem.kind,
project: problem.projectRelative,
targets: problem.targets,
statement: problem.contract.statementHash,
leanStatement: problem.contract.leanStatementHash,
sourceDigest,
environment: environment.digest,
}),
)
: undefined;
const provenance = previous && checkedRejections.get(previous);
if (
inputDigest &&
provenance?.inputDigest === inputDigest &&
previous &&
!previous.passed &&
previous.sourceDigest === sourceDigest &&
provenance.verificationDigest === proofVerificationDigest(previous.verification)
) {
const reused = previous.commit === commit ? previous : { ...previous, commit };
checkedRejections.set(reused, provenance);
return reused;
}
const verify = (checkout: string) =>
checkProofEvidence(
{
projectRoot: path.join(checkout, problem.projectRelative),
targetCommit: commit,
timeoutMs,
items: problem.targets.map(target => ({
proofPath: target.proofPath,
theoremName: target.theoremName,
expectedSourceDeclarationHash: target.declarationHash,
statementHash: problem.contract.statementHash,
leanStatementHash: problem.contract.leanStatementHash,
claim: 'statement' as const,
})),
},
{
env: environment.env,
signal,
prepareLocalDependencies: problem.kind === 'lean-eval',
collectDeclarationReferences: true,
collectDirectProofReferences,
},
);
// Advisory scratch and diagnostic logs stay in the worker. The strict verifier receives
// only the fixed commit and the same pinned packages, without weakening its clean-tree gate.
const evidence = git(worktree, ['status', '--porcelain=v1', '--untracked-files=all'])
? await withSolverWorktree(worktree, commit, async checkout => {
const packages = path.join(projectRoot, '.lake', 'packages');
if (fs.existsSync(packages)) bindLeanPackages(path.join(checkout, problem.projectRelative), packages);
return verify(checkout);
})
: await verify(worktree);
const check = { commit, sourceDigest, ...evidence };
if (check.passed) checkedProofs.set(check, proofIdentity(check));
if (
!check.passed &&
inputDigest &&
hasDeterministicLeanFailure(check.verification) &&
proofReuseEnvironment(projectRoot, environment.env).digest === environment.digest
)
checkedRejections.set(check, { inputDigest, verificationDigest: proofVerificationDigest(check.verification) });
return check;
}