Repository navigation
Expand file tree
/
Copy pathworkerBridge.ts
More file actions
652 lines (640 loc) · 26.1 KB
/
Copy pathworkerBridge.ts
File metadata and controls
652 lines (640 loc) · 26.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import fs from 'node:fs';
import path from 'node:path';
import { z } from 'zod';
import { mathGitBytes } from '../../src/math/mathGit';
import {
canonicalMathJson,
mathDigest,
readMathText,
withMathRuntimeLease,
writeMathFrozen,
writeMathJsonDurable,
} from '../../src/math/mathRuntimeIo';
import { assertMathCanonicalPath } from '../../src/math/paths';
import type { SolverRunState } from '../../src/solver/archive';
import {
buildRunInspection,
exportRunPlan,
projectRunInspectionTask,
readRunInspection,
} from '../../src/solver/inspection';
import { queuePlanApproval } from '../../src/solver/humanEdit';
import { planApprovalInputSchema } from '../../src/solver/planApproval';
import { readHumanGuidance } from '../../src/solver/humanGuidance';
import { taskVersion, type TaskRecord } from '../../src/solver/tasks';
import {
allowedKnowledgeFile,
buildTheoremKnowledge,
validateTheoremKnowledge,
type TheoremKnowledgeBase,
} from '../../src/solver/theoremKnowledge';
import { importKnowledgeRuns } from '../../src/solver/theoremKnowledgeRuns';
import { proofReceiptsMatch, type ProofCheck } from '../../src/solver/modules/evidence';
import type {
WebCheckReceipt,
WebCommandResult,
WebLibraryRestart,
WebObservation,
WebTaskSource,
} from './collaboration';
import { retiredResearchCoordinationCode, type WorkerMessage } from './shared';
import { projectWebDeclarationArtifacts } from './declarationArtifacts';
import { projectWebNativeTeam } from './nativeTeamObservation';
import {
maxWorkerObservationBytes,
standaloneObservationFrame,
type WorkerObservationFrame,
} from './observationSources';
export const webActiveRunFile = '/work/active-run.json';
const libraryRestartSchema = z
.object({
previousSolverRunId: z.string().min(1).max(128),
round: z.number().int().positive().max(Number.MAX_SAFE_INTEGER),
})
.strict();
type LibraryRestart = Omit<WebLibraryRestart, 'solverRunId'>;
const activeRunSchema = z
.object({
schema: z.literal('theorem-os.web-active-run.v1'),
webRunId: z.string().min(1).max(128),
directory: z.string().min(1),
output: z.string().min(1),
libraryRestart: libraryRestartSchema.optional(),
})
.strict();
type ActiveRun = { webRunId: string; directory: string; output: string; libraryRestart?: LibraryRestart };
const metaSchema = z
.object({
schema: z.literal('theorem-os.web-bridge.v1'),
webRunId: z.string(),
solverRunId: z.string(),
libraryRound: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).optional(),
sequence: z.number().int().nonnegative(),
contentDigest: z.string(),
// Older worker metadata may include the observation references once used by guidance writes.
observations: z.unknown().optional(),
catalogKey: z.string().optional(),
catalogDigest: z.string().optional(),
})
.strict();
type BridgeMeta = z.infer<typeof metaSchema>;
const maxSourceBytes = 4 * 1024 * 1024,
maxSourcesBytes = 6 * 1024 * 1024,
maxChecksBytes = 2 * 1024 * 1024;
const oid = /^[a-f0-9]{40}(?:[a-f0-9]{24})?$/u;
const childOf = (directory: string, file: string) => path.isAbsolute(file) && file.startsWith(directory + path.sep);
const relativePath = (file: string) =>
file &&
!path.isAbsolute(file) &&
!file.includes('\\') &&
file.split('/').every(part => part && part !== '.' && part !== '..');
export function writeWebActiveRun(input: ActiveRun, file = webActiveRunFile): void {
const directory = fs.realpathSync(input.directory),
output = path.resolve(input.output);
if (!childOf(directory, output)) throw new Error('web_bridge_output_outside_run');
writeMathJsonDurable(
file,
activeRunSchema.parse({
schema: 'theorem-os.web-active-run.v1',
webRunId: input.webRunId,
directory,
output,
libraryRestart: input.libraryRestart,
}),
);
}
function readActiveRun(file: string): ActiveRun {
const value = activeRunSchema.parse(JSON.parse(readMathText(file, 8_192))) as ActiveRun;
if (
path.resolve(value.directory) !== value.directory ||
fs.realpathSync(value.directory) !== value.directory ||
!childOf(value.directory, value.output)
)
throw new Error('web_bridge_active_run_invalid');
return value;
}
export function freezeWebKnowledge(directory: string, catalog?: unknown): string | undefined {
if (!catalog) return;
const base = validateTheoremKnowledge(catalog),
file = path.join(directory, 'input-knowledge.json');
writeMathFrozen(file, canonicalMathJson(base) + '\n');
return file;
}
function bridgeMeta(
file: string,
webRunId: string,
solverRunId: string,
restart?: LibraryRestart,
): BridgeMeta | undefined {
if (!fs.existsSync(file)) return;
const value = metaSchema.parse(JSON.parse(readMathText(file, 32 * 1024)));
if (
value.webRunId !== webRunId ||
(value.solverRunId === solverRunId
? restart && restart.round !== (value.libraryRound ?? 0)
: !restart ||
restart.previousSolverRunId !== value.solverRunId ||
restart.round !== (value.libraryRound ?? 0) + 1)
)
throw new Error('web_bridge_run_identity_changed');
return value;
}
function runRoots(state: SolverRunState, directory: string): string[] {
return [
...new Set([state.worktree, state.problem.workspace, ...(state.workers ?? []).map(worker => worker.worktree)]),
].filter(root => {
try {
if (!childOf(directory, root)) return false;
assertMathCanonicalPath(root);
return fs.realpathSync(root) === root;
} catch {
return false;
}
});
}
/** Read immutable Git blobs from host-selected owned files, never user-supplied paths or mutable worktree bytes. */
function committedOwnedSources(
state: SolverRunState,
roots: readonly string[],
commit: string,
): { files: Record<string, string>; digest: string } {
const problem = state.problem;
if (
!oid.test(commit) ||
!relativePath(problem.projectRelative) ||
!problem.ownedFiles.length ||
problem.ownedFiles.some(file => !allowedKnowledgeFile(file) || !file.endsWith('.lean'))
)
throw new Error('web_bridge_source_selection_invalid');
const paths = problem.ownedFiles.map(file => `${problem.projectRelative}/${file}`);
for (const root of roots) {
try {
mathGitBytes(root, ['merge-base', '--is-ancestor', problem.baseCommit, commit], {
maxBuffer: 8_192,
timeout: 3_000,
errorMessage: 'web_bridge_source_outside_problem',
});
const tree = mathGitBytes(root, ['--literal-pathspecs', 'ls-tree', '--full-tree', '-z', commit, '--', ...paths], {
maxBuffer: 8_192 * paths.length,
timeout: 3_000,
}).toString('utf8');
const regularFiles = new Set(
tree.split('\0').map(entry => /^100(?:644|755) blob [a-f0-9]+\t([\s\S]+)$/u.exec(entry)?.[1]),
);
if (paths.some(file => !regularFiles.has(file))) throw new Error('web_bridge_source_not_regular_file');
const files: Record<string, string> = {};
for (const [index, file] of problem.ownedFiles.entries()) {
const full = paths[index]!;
files[file] = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(
mathGitBytes(root, ['show', `${commit}:${full}`], {
maxBuffer: maxSourceBytes,
timeout: 3_000,
errorMessage: 'web_bridge_source_unavailable',
}),
);
}
return {
files,
digest: mathDigest(canonicalMathJson(problem.ownedFiles.map(file => [file, mathDigest(files[file])]))),
};
} catch {
/* A peer's committed object may only exist in another run-owned worktree. */
}
}
throw new Error('web_bridge_source_unavailable');
}
function sourceProjection(state: SolverRunState, directory: string) {
const sources: WebTaskSource[] = [],
checks: WebCheckReceipt[] = [];
let sourceBytes = 0,
checkBytes = 0,
sourcePartial = false,
checkPartial = false;
const sourceIds = new Set<string>(),
checkIds = new Set<string>();
const sourceTexts = new Set<string>();
const roots = runRoots(state, directory),
snapshots = new Map<string, ReturnType<typeof committedOwnedSources>>();
const snapshot = (commit: string) => {
let value = snapshots.get(commit);
if (!value) {
value = committedOwnedSources(state, roots, commit);
snapshots.set(commit, value);
}
return value;
};
const addSource = (node: Pick<TaskRecord, 'task'>, commit: string, expectedDigest?: string) => {
const version = taskVersion(node.task),
key = canonicalMathJson([node.task.id, version, commit]);
if (sourceIds.has(key)) return;
try {
const selected = snapshot(commit),
file = node.task.proofPath;
if (!file || !Object.hasOwn(selected.files, file) || (expectedDigest && selected.digest !== expectedDigest))
throw new Error('web_bridge_source_identity_mismatch');
const source = selected.files[file],
dependencySources = Object.entries(selected.files)
.filter(([dependency]) => dependency !== file && dependency.endsWith('.lean'))
.map(([file, source]) => ({ file, source }));
const newTexts = [...new Set([source, ...dependencySources.map(dependency => dependency.source)])].filter(
text => !sourceTexts.has(text),
);
const bytes = newTexts.reduce((sum, text) => sum + Buffer.byteLength(text), 0);
if (sourceBytes + bytes > maxSourcesBytes) {
sourcePartial = true;
return;
}
sources.push({
taskId: node.task.id,
taskVersion: version,
commit,
sourceDigest: selected.digest,
environmentDigest: state.environmentDigest,
file,
source,
scope: 'owned-file',
...(dependencySources.length ? { dependencySources } : {}),
});
for (const text of newTexts) sourceTexts.add(text);
sourceBytes += bytes;
sourceIds.add(key);
} catch {
sourcePartial = true;
}
};
const addProof = (
node: Pick<TaskRecord, 'task'>,
proof: ProofCheck,
original?: SolverRunState['problem']['targets'][number],
) => {
const version = taskVersion(node.task);
if ('taskDigest' in proof && proof.taskDigest !== version) {
sourcePartial = true;
checkPartial = true;
return;
}
addSource(node, proof.commit, proof.sourceDigest);
for (const receipt of proof.verification.results) {
if (receipt.proofPath !== node.task.proofPath || receipt.theoremName !== node.task.theoremName) continue;
const id = receipt.receiptId || `observation-${mathDigest(canonicalMathJson(receipt))}`;
const key = canonicalMathJson([
node.task.id,
version,
proof.commit,
id,
proof.verification.checkedAt,
original ? proof.passed : null,
]);
if (checkIds.has(key)) continue;
// The original-problem check can pass one target while failing the batch.
const passed = original
? proofReceiptsMatch({ ...proof.verification, status: receipt.status, results: [receipt] }, proof.commit, [
{
proofPath: original.proofPath,
theoremName: original.theoremName,
expectedSourceDeclarationHash: original.declarationHash,
statementHash: state.problem.contract.statementHash,
leanStatementHash: state.problem.contract.leanStatementHash,
claim: 'statement',
},
])
: proof.passed;
const record = {
...receipt,
passed,
...(original ? { originalProblemPassed: proof.passed } : {}),
...('conditions' in proof ? { conditions: proof.conditions ?? [] } : {}),
...('references' in proof ? { references: proof.references } : {}),
verification: {
schema: proof.verification.schema,
status: proof.verification.status,
targetCommit: proof.verification.targetCommit,
startedAt: proof.verification.startedAt,
checkedAt: proof.verification.checkedAt,
durationMs: proof.verification.durationMs,
},
evidence: 'recorded-not-replayed',
acceptance: 'not-assessed',
};
const bytes = Buffer.byteLength(canonicalMathJson(record));
if (checkBytes + bytes > maxChecksBytes) {
checkPartial = true;
continue;
}
checks.push({
id,
taskId: node.task.id,
taskVersion: version,
commit: proof.commit,
sourceDigest: proof.sourceDigest,
environmentDigest: state.environmentDigest,
record,
});
checkBytes += bytes;
checkIds.add(key);
}
};
for (const node of [...(state.cooperation?.tasks ?? []), ...(state.cooperation?.history ?? [])]) {
if (!['lemma', 'goal'].includes(node.task.kind)) continue;
let hasProof = false;
for (const proof of [node.proof, node.integratedCheck])
if (proof) {
addProof(node, proof);
hasProof = true;
}
if (hasProof || !state.cooperation?.tasks.includes(node as TaskRecord)) continue;
const current = node as TaskRecord;
let commit = state.cooperation?.canonicalCommit;
if (current.assignment && current.assignment.taskDigest === taskVersion(current.task)) {
const worker = state.workers?.find(item => item.id === current.assignment!.workerId);
if (worker && roots.includes(worker.worktree)) {
try {
commit = mathGitBytes(worker.worktree, ['rev-parse', 'HEAD'], { maxBuffer: 128, timeout: 3_000 })
.toString('utf8')
.trim();
} catch {
sourcePartial = true;
}
}
}
if (commit) addSource(node, commit);
else sourcePartial = true;
}
const wholeChecks = [state.check, ...(state.workers ?? []).map(worker => worker.check)]
// Cooperative workers also retain their most recent task-scoped proof in check.
.filter((proof): proof is ProofCheck => proof !== undefined && !('taskDigest' in proof));
for (const [index, target] of state.problem.targets.entries()) {
const node = state.cooperation?.tasks.find(
item =>
item.task.kind === 'goal' &&
item.task.proofPath === target.proofPath &&
item.task.theoremName === target.theoremName,
);
// Direct and independent runs use the same frozen target identity as their knowledge evidence.
const task = node?.task ?? {
id: `original_goal_${index + 1}`,
kind: 'goal' as const,
objective: `Prove ${target.theoremName}`,
revision: 0,
dependsOn: [],
...target,
};
let found = false;
for (const proof of wholeChecks) {
if (
proof.verification.results.some(
receipt => receipt.proofPath === target.proofPath && receipt.theoremName === target.theoremName,
)
) {
addProof({ task }, proof, target);
found = true;
} else checkPartial = true;
}
if (!node && !found) sourcePartial = true;
}
return {
sources,
checks,
coverage: {
sources: sourcePartial ? ('partial' as const) : ('recorded' as const),
checks: checkPartial ? ('partial' as const) : ('recorded' as const),
},
};
}
const catalogKey = (state: SolverRunState, declarations: ReturnType<typeof projectWebDeclarationArtifacts>) =>
mathDigest(
canonicalMathJson({
records: [...(state.cooperation?.tasks ?? []), ...(state.cooperation?.history ?? [])].map(node => ({
task: node.task,
proof: node.proof,
integratedCheck: node.integratedCheck,
finding: node.finding,
})),
deliveries: state.cooperation?.deliveries,
declarations,
}),
);
export function createWebWorkerBridge(options: {
webRunId: string;
output: string;
directory: string;
apiKey?: string;
/** The host authorizes one transition; directory stays fixed while output points at the new round. */
libraryRestart?: LibraryRestart;
onMessage?(message: WorkerMessage, frame?: WorkerObservationFrame): void;
}) {
const directory = fs.realpathSync(options.directory),
metaFile = path.join(directory, 'web-bridge.json');
if (!childOf(directory, path.resolve(options.output))) throw new Error('web_bridge_output_outside_run');
if (
options.libraryRestart &&
(!Number.isSafeInteger(options.libraryRestart.round) ||
options.libraryRestart.round < 1 ||
!options.libraryRestart.previousSolverRunId)
)
throw new Error('web_bridge_run_identity_changed');
// Each new bridge starts with a self-contained frame. Retain only the previous frame's digests.
let emittedSources = new Set<string>();
let captured = false;
async function capture(input: { includeCatalog?: boolean; force?: boolean; waitMs?: number } = {}) {
const messages: WorkerMessage[] = [];
const result = await withMathRuntimeLease(
{ lockPath: path.join(directory, 'web-bridge.lock'), flockExecutable: '/usr/bin/flock', waitMs: input.waitMs },
async lease => {
const stored = readRunInspection(options.output),
{ state, stateFile } = stored;
if (options.libraryRestart?.previousSolverRunId === state.runId)
throw new Error('web_bridge_run_identity_changed');
const { agentTeam, ...report } = buildRunInspection(state, stored.result, {
teamWorkerLimit: 8,
snapshot: stored.snapshot,
});
const teamSources = new Map<string, ReturnType<typeof committedOwnedSources>>();
const nativeTeam =
agentTeam &&
projectWebNativeTeam(state, directory, agentTeam, (archive, commit) => {
const key = archive + '\0' + commit,
cached = teamSources.get(key);
if (cached) return cached;
const snapshot = committedOwnedSources(state, [archive], commit);
const source = options.apiKey
? {
...snapshot,
files: Object.fromEntries(
Object.entries(snapshot.files).filter(([, text]) => !text.includes(options.apiKey!)),
),
}
: snapshot;
teamSources.set(key, source);
return source;
});
const inspection = { ...report, ...(nativeTeam ? { nativeTeam } : agentTeam ? { agentTeam } : {}) };
const previous = bridgeMeta(metaFile, options.webRunId, state.runId, options.libraryRestart);
const restarted = Boolean(previous && previous.solverRunId !== state.runId);
const projection = sourceProjection(state, directory);
const declarations = projectWebDeclarationArtifacts(stateFile, state);
const history = (state.cooperation?.history ?? []).map(node =>
projectRunInspectionTask(state, { status: 'pending', attempts: 0, ...node }),
);
let plan: ReturnType<typeof exportRunPlan> | null = null;
if (state.cooperation?.planVersion && state.cooperation.tasks.length) plan = exportRunPlan(state);
const payload = {
inspection,
snapshot: inspection.snapshot,
plan,
...projection,
declarations,
guidance: readHumanGuidance(stateFile, state.runId),
deliveries: state.cooperation?.deliveries ?? [],
history,
};
let observation: WebObservation = {
schema: 'theorem-os.web-observation.v1',
webRunId: options.webRunId,
solverRunId: state.runId,
sequence: 0,
observedAt: new Date().toISOString(),
evidence: 'recorded-not-replayed',
acceptance: 'not-assessed',
...payload,
};
let frame = standaloneObservationFrame(observation);
const contentDigest = mathDigest(canonicalMathJson({ ...frame.observation, observedAt: '' }));
const changed = restarted || previous?.contentDigest !== contentDigest;
const sequence = (previous?.sequence ?? 0) + Number(changed);
const libraryRestart = previous &&
options.libraryRestart && { ...options.libraryRestart, solverRunId: state.runId };
// Capture through docker exec can commit the new metadata before the streaming worker does.
const announceRestart = libraryRestart && options.onMessage && !captured;
if (announceRestart) messages.push({ type: 'library_restart', ...libraryRestart });
observation.sequence = sequence;
frame.observation.sequence = sequence;
let frameJson = JSON.stringify(frame);
const secret = options.apiKey && JSON.stringify(options.apiKey).slice(1, -1);
if (secret && frameJson.includes(secret)) {
observation = JSON.parse(JSON.stringify(observation).replaceAll(secret, '[REDACTED]'));
observation.coverage.sources = 'partial';
observation.coverage.checks = 'partial';
frame = standaloneObservationFrame(observation);
frameJson = JSON.stringify(frame);
}
if (Buffer.byteLength(frameJson) > maxWorkerObservationBytes) throw new Error('web_observation_too_large');
const meta: BridgeMeta = {
schema: 'theorem-os.web-bridge.v1',
webRunId: options.webRunId,
solverRunId: state.runId,
libraryRound: options.libraryRestart?.round ?? previous?.libraryRound ?? 0,
sequence,
contentDigest,
...(previous?.catalogKey ? { catalogKey: previous.catalogKey } : {}),
...(previous?.catalogDigest ? { catalogDigest: previous.catalogDigest } : {}),
};
let catalog: TheoremKnowledgeBase | undefined;
const key = catalogKey(state, declarations),
catalogFile = path.join(directory, 'web-catalog.json');
if (restarted || key !== previous?.catalogKey) {
try {
const imported = importKnowledgeRuns([stateFile], { includeFindings: true });
if (readRunInspection(options.output).snapshot.stateDigest !== inspection.snapshot.stateDigest)
throw new Error('web_catalog_state_changed');
const base = fs.existsSync(catalogFile)
? validateTheoremKnowledge(JSON.parse(readMathText(catalogFile, maxWorkerObservationBytes)))
: undefined;
const candidate = buildTheoremKnowledge(imported.candidates, base, imported.findings);
const text = canonicalMathJson(candidate);
if (options.apiKey && text.includes(options.apiKey)) throw new Error('web_catalog_secret_redacted');
if (Buffer.byteLength(text) > maxWorkerObservationBytes) throw new Error('web_catalog_too_large');
const digest = mathDigest(text);
if (digest !== previous?.catalogDigest) {
writeMathJsonDurable(catalogFile, candidate);
catalog = candidate;
}
meta.catalogKey = key;
meta.catalogDigest = digest;
if (imported.skipped.length)
messages.push({
type: 'progress',
event: 'knowledge_capture_partial',
message: `知识归档保留已匹配来源的记录;${imported.skipped.length} 条记录暂未导入:${[...new Set(imported.skipped.map(item => item.reason))].slice(0, 4).join('、')}。历史记录均须重新复核。`,
});
} catch (error) {
messages.push({
type: 'progress',
event: 'knowledge_capture_partial',
message: `知识归档尚未完整捕获:${error instanceof Error ? error.message : 'web_catalog_unavailable'}。已有证明状态不变,未用空知识库替代。`,
});
}
}
if (input.includeCatalog && !catalog && fs.existsSync(catalogFile)) {
catalog = validateTheoremKnowledge(JSON.parse(readMathText(catalogFile, maxWorkerObservationBytes)));
}
lease.assertHeld();
if (!previous || canonicalMathJson(previous) !== canonicalMathJson(meta)) writeMathJsonDurable(metaFile, meta);
captured = true;
if (changed || input.force || announceRestart) messages.push({ type: 'observation', observation });
if (catalog) messages.push({ type: 'catalog', catalog });
return {
frame,
result: { observation, ...(catalog ? { catalog } : {}), ...(libraryRestart ? { libraryRestart } : {}) },
};
},
);
for (const message of messages) {
if (!options.onMessage) break;
if (message.type !== 'observation') {
options.onMessage(message);
continue;
}
const blobs = Object.entries(result.frame.sourceBlobs);
const sourceBlobs = Object.fromEntries(blobs.filter(([digest]) => !emittedSources.has(digest)));
options.onMessage(message, { ...result.frame, sourceBlobs });
emittedSources = new Set(blobs.map(([digest]) => digest));
}
return result.result;
}
return { capture };
}
const commandSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('plan-approval'), approval: planApprovalInputSchema }).strict(),
z.object({ type: z.literal('capture') }).strict(),
]);
export async function handleWebWorkerCommand(
value: unknown,
options: { activeRunFile?: string } = {},
): Promise<WebCommandResult> {
if (
value &&
typeof value === 'object' &&
'type' in value &&
['edit-preview', 'edit', 'guidance', 'guidance-withdraw'].includes(String(value.type))
)
throw new Error(retiredResearchCoordinationCode);
const command = commandSchema.parse(value);
const active = readActiveRun(options.activeRunFile ?? webActiveRunFile);
if (command.type === 'capture')
return {
id: 'capture',
status: 'captured',
...(await createWebWorkerBridge(active).capture({ includeCatalog: true, force: true, waitMs: 5_000 })),
};
try {
return { ...(await queuePlanApproval(active.output, command.approval)) };
} catch (error) {
// Each of these is decided from the saved checkpoint before anything is written.
const reason = error instanceof Error ? error.message : '';
if (
[
'math_plan_approval_invalid',
'math_plan_approval_id_conflict',
'math_plan_approval_not_required',
'math_plan_approval_run_inactive',
'math_plan_approval_already_approved',
'math_plan_approval_plan_unavailable',
'math_plan_approval_version_conflict',
'math_plan_approval_limit',
].includes(reason)
)
return { id: command.approval.id, status: 'rejected', reason, planVersion: command.approval.planVersion };
throw error;
}
}