Repository navigation
Expand file tree
/
Copy pathevaluation.ts
More file actions
179 lines (175 loc) · 7.02 KB
/
Copy pathevaluation.ts
File metadata and controls
179 lines (175 loc) · 7.02 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
import { MATH_PROOF_ENVIRONMENT_ENV } from '../math/mathProofEnvironment';
import { nativeProcess } from '../math/nativeProcess';
import { runMathematicsSandboxedCommand, type MathematicsSandboxCommandResult } from '../math/verifier';
import { runPersistedLeanEvalOfficialGate } from '../math/leanEvalBootstrap';
import { checkSolverProof, type ProofCheck } from './modules/evidence';
import { git, withSolverWorktree, type SolverProblem } from './problem';
type Execution = Pick<
MathematicsSandboxCommandResult,
'started' | 'exitCode' | 'signal' | 'stderr' | 'timedOut' | 'aborted'
>;
type Method = 'external-command' | 'official-comparator' | 'host-lean-kernel-all-targets';
export interface FinalEvaluationPolicy {
head(head: string): void;
clean(dirty: boolean): void;
integrated(passed: boolean, commit: string, digest: string): Method;
finish(
result: Execution,
comparison: string | undefined,
cleanupFailed: boolean,
): { status: string; reason?: string };
}
/** Execution failures cannot decide whether a mathematical submission is correct. */
export function evaluatorExecutionFailure(result: Execution): string | undefined {
if (result.timedOut) return 'math_evaluation_timeout';
if (
result.aborted ||
!result.started ||
result.signal ||
result.exitCode === null ||
result.exitCode === 126 ||
result.exitCode === 127
)
return 'math_evaluator_environment_unavailable';
if (result.exitCode === 0) return undefined;
// These are diagnostics from the process launcher, runtime or Git, rather
// than Lean's source diagnostics or the comparator's mathematical verdict.
// Keep the signatures anchored and narrow: arbitrary stderr is not evidence
// of an unavailable environment, and a normal rejection remains `failed`.
if (
/^libc\+\+abi: [^\n]*failed to create thread: Resource temporarily unavailable\s*$/mu.test(result.stderr) ||
/^could not execute external process '(?:lean|lake|git|landrun|comparator)'\s*$/mu.test(result.stderr) ||
/^fatal: unable to access '[^'\n]+': (?:Could not resolve host:|Failed to connect to |Network is unreachable)[^\n]*$/mu.test(
result.stderr,
) ||
/^(?:cp|mkdir|chmod|rm): [^\n]*: Read-only file system\s*$/mu.test(result.stderr)
)
return 'math_evaluator_environment_unavailable';
return undefined;
}
function evaluationPolicy(problem: SolverProblem, receipt: ProofCheck, external: boolean): FinalEvaluationPolicy {
const official = problem.kind === 'lean-eval';
if (nativeProcess)
return new nativeProcess.SolverEvaluation(
problem.baseCommit,
receipt.commit,
receipt.sourceDigest,
external,
official,
);
return {
head(head) {
if (head !== problem.baseCommit && head !== receipt.commit) throw new Error('math_main_workspace_changed');
},
clean(dirty) {
if (dirty) throw new Error('math_main_workspace_changed');
},
integrated(passed, commit, digest) {
if (!passed || commit !== receipt.commit || digest !== receipt.sourceDigest)
throw new Error('math_integrated_proof_mismatch');
return external ? 'external-command' : official ? 'official-comparator' : 'host-lean-kernel-all-targets';
},
finish(result, comparison, cleanupFailed) {
const reason = evaluatorExecutionFailure(result);
if (reason) return { status: 'error', reason };
if (cleanupFailed) return { status: 'error' };
if (comparison === undefined) return { status: result.exitCode === 0 ? 'passed' : 'failed' };
return { status: comparison === 'official_verified' ? 'passed' : comparison === 'failed' ? 'failed' : 'error' };
},
};
}
/** S3: final acceptance stays independent of replaceable research modules. */
export async function evaluateSolverProof(
problem: SolverProblem,
receipt: ProofCheck,
env: NodeJS.ProcessEnv,
signal: AbortSignal,
options: { command?: string; timeoutMs: number },
): Promise<Record<string, unknown>> {
const deadline = Date.now() + options.timeoutMs;
const controller = new AbortController();
const expire = () => controller.abort(new Error('math_evaluation_timeout'));
const evaluationSignal = AbortSignal.any([signal, controller.signal]);
let timer: ReturnType<typeof setTimeout> | undefined;
const armTimer = () => {
const remainingMs = deadline - Date.now();
if (remainingMs <= 0) expire();
else if (Number.isFinite(remainingMs)) timer = setTimeout(armTimer, Math.min(remainingMs, 2_147_483_647));
};
armTimer();
const assertActive = () => {
signal.throwIfAborted();
if (Date.now() >= deadline) expire();
evaluationSignal.throwIfAborted();
};
try {
assertActive();
const command = options.command;
const policy = evaluationPolicy(problem, receipt, !!command);
policy.head(git(problem.workspace, ['rev-parse', 'HEAD']));
policy.clean(!!git(problem.workspace, ['status', '--porcelain', '--untracked-files=no']));
git(problem.workspace, ['merge', '--ff-only', receipt.commit]);
assertActive();
const finalCheck = await checkSolverProof(
problem,
problem.workspace,
env,
evaluationSignal,
undefined,
options.timeoutMs,
);
assertActive();
const method = policy.integrated(finalCheck.passed, finalCheck.commit, finalCheck.sourceDigest);
if (method === 'external-command') {
if (!command) throw new Error('math_evaluation_out_of_order');
const result = await withSolverWorktree(problem.workspace, finalCheck.commit, checkoutRoot => {
assertActive();
return runMathematicsSandboxedCommand({
checkoutRoot,
projectRelative: problem.projectRelative,
command: '/bin/sh',
args: ['-c', command],
timeoutMs: options.timeoutMs,
env,
signal: evaluationSignal,
});
});
assertActive();
return {
...policy.finish(result, undefined, false),
method,
official: false,
result,
};
}
if (method === 'official-comparator') {
if (!problem.sourceRepository) throw new Error('math_official_source_missing');
const { comparison } = await runPersistedLeanEvalOfficialGate({
activeProblemPath: problem.activeProblemPath,
repositoryRoot: problem.sourceRepository,
timeoutMs: options.timeoutMs,
env: { ...env, [MATH_PROOF_ENVIRONMENT_ENV]: undefined },
signal: evaluationSignal,
});
assertActive();
return {
...policy.finish(
{ ...comparison, started: comparison.status !== 'unavailable', stderr: comparison.stderrPreview ?? '' },
comparison.status,
!!comparison.cleanupError,
),
method,
official: true,
comparison,
};
}
return { status: 'passed', method, official: false, verification: finalCheck };
} catch (error) {
signal.throwIfAborted();
if (Date.now() >= deadline) expire();
if (controller.signal.aborted) return { status: 'error', reason: 'math_evaluation_timeout', timedOut: true };
throw error;
} finally {
clearTimeout(timer);
}
}