Repository navigation
Expand file tree
/
Copy pathproofJournal.ts
More file actions
70 lines (68 loc) · 2.53 KB
/
Copy pathproofJournal.ts
File metadata and controls
70 lines (68 loc) · 2.53 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
import path from 'node:path';
import { writeMathTextDurable } from '../../src/math/mathRuntimeIo';
import type { WorkerMessage } from './shared';
import { standaloneObservationFrame, type WorkerObservationFrame } from './observationSources';
export const webProofJournalDirectory = '/var/theoremos-proof';
export const webProofJournalFiles = ['transitions.json', 'observation.json', 'artifact.json', 'terminal.json'] as const;
/** Keep the latest public evidence on disk even when no process is reading stdout. */
export function webProofMessageSink(
output: NodeJS.WritableStream,
apiKey: string,
journal?: { directory: string; webRunId: string },
) {
const secret = JSON.stringify(apiKey).slice(1, -1);
const redact = (value: unknown) => {
const json = JSON.stringify(value);
return secret ? json.replaceAll(secret, '[REDACTED]') : json;
};
const transitions: Extract<WorkerMessage, { type: 'library_restart' }>[] = [];
let disconnected = false;
const disconnect = () => {
disconnected = true;
};
output.on('error', disconnect);
output.on('close', disconnect);
return (message: WorkerMessage, frame?: WorkerObservationFrame) => {
if (journal) {
let file: (typeof webProofJournalFiles)[number] | undefined;
switch (message.type) {
case 'library_restart':
transitions.push(message);
file = 'transitions.json';
break;
case 'observation':
file = 'observation.json';
break;
case 'artifact':
file = 'artifact.json';
break;
case 'result':
case 'error':
file = 'terminal.json';
break;
}
if (file) {
try {
let stored: WorkerMessage | WorkerObservationFrame = message;
if (message.type === 'observation') {
stored = standaloneObservationFrame(message.observation);
if (secret && JSON.stringify(stored).includes(secret)) {
const redacted: typeof message = JSON.parse(redact(message));
stored = standaloneObservationFrame(redacted.observation);
}
}
writeMathTextDurable(
path.join(journal.directory, file),
redact({
webRunId: journal.webRunId,
messages: message.type === 'library_restart' ? transitions : [stored],
}) + '\n',
);
} catch {
/* A failed backup write must not suppress the live result. */
}
}
}
if (!disconnected) output.write(redact(frame ?? message) + '\n');
};
}