Repository navigation
Expand file tree
/
Copy pathfailure.ts
More file actions
61 lines (57 loc) · 2.95 KB
/
Copy pathfailure.ts
File metadata and controls
61 lines (57 loc) · 2.95 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
import { researchFailureCategory, researchFailureReason } from '../../src/solver/modules/search';
import { webFailureKinds, webFailureReason, type WebFailure, type WebFailureKind } from './shared';
// Earlier entries explain later ones: a rejected key also ends every Agent session.
const priority: WebFailureKind[] = [
'authentication',
'rate_limit',
'budget',
'environment',
'source',
'network',
'protocol',
];
// Only runtime reason codes leave the worker; provider text may quote the request.
const reasonCode = (value: string) => (webFailureReason.test(value) ? value : undefined);
function categoryOf(error: string): WebFailureKind {
const category = researchFailureCategory(error);
return category && category !== 'proof' && (webFailureKinds as readonly string[]).includes(category)
? (category as WebFailureKind)
: 'unknown';
}
function failure(kind: WebFailureKind, error?: string): WebFailure {
// The fallback 'math_model_turn_failed' would only restate that the text was unrecognised.
const derived = error === undefined ? undefined : researchFailureReason(error);
const code =
error === undefined
? undefined
: (reasonCode(error) ?? (derived === 'math_model_turn_failed' ? undefined : derived));
return { kind, ...(code ? { reason: code } : {}) };
}
/** Classify a finished solver result. Accepted and rejected proofs are not failures. */
export function classifyWebResult(raw: Record<string, unknown>): WebFailure | undefined {
const status = String(raw.status ?? 'runtime_error'),
reason = typeof raw.reason === 'string' ? raw.reason : undefined;
if (!['limit_reached', 'runtime_error', 'incomplete'].includes(status)) return;
if (reason === 'math_run_stopped' || reason === 'math_preparation_stopped') return failure('time_limit', reason);
if (reason === 'math_total_token_limit' || reason === 'math_model_usage_unavailable')
return failure('budget', reason);
const workers = Array.isArray(raw.workers) ? raw.workers : [];
const errors = [
reason,
...workers.map(worker =>
worker && typeof worker === 'object' ? (worker as { error?: unknown }).error : undefined,
),
].filter((value): value is string => typeof value === 'string' && value.length > 0);
const classified = errors.map(error => ({ error, kind: categoryOf(error) }));
const found = priority.map(kind => classified.find(item => item.kind === kind)).find(Boolean);
if (found) return failure(found.kind, found.error);
if (status === 'limit_reached') return failure('time_limit');
return status === 'runtime_error' ? failure('unknown', errors[0]) : undefined;
}
/** Classify an exception thrown before the solver wrote a result. */
export function classifyWebError(error: unknown): WebFailure {
const message = error instanceof Error ? error.message : String(error);
if (/^lean_task_/u.test(message)) return failure('source', message);
const kind = categoryOf(message);
return failure(kind === 'unknown' ? 'environment' : kind, message);
}