Repository navigation
Expand file tree
/
Copy paththeoremKnowledgeStore.ts
More file actions
259 lines (246 loc) · 11.3 KB
/
Copy paththeoremKnowledgeStore.ts
File metadata and controls
259 lines (246 loc) · 11.3 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
import fs from 'node:fs';
import path from 'node:path';
import { z } from 'zod';
import { nativeProcess } from '../math/nativeProcess';
import {
canonicalMathJson,
mathDigest,
parseMathJson,
readMathFile,
readMathText,
syncMathDirectory,
writeMathFrozen,
} from '../math/mathRuntimeIo';
import { assertMathCanonicalPath } from '../math/paths';
import {
libraryKnowledgeAuthorizationSchema,
theoremKnowledgeEntrySchema,
theoremKnowledgeStats,
validateKnowledgeMetadata,
validateTheoremKnowledge,
type TheoremKnowledgeBase,
} from './theoremKnowledge';
import { knowledgeRouteFindingSchema } from './theoremKnowledgeFindings';
const hash = z.string().regex(/^[a-f0-9]{64}$/u);
const maxBytes = 256 * 1024 * 1024;
const sourceIdentity = z.object({ bytes: z.number().int().nonnegative().max(maxBytes) }).strict();
const directoryName = z
.string()
.min(1)
.max(255)
.refine(
value =>
!value.startsWith('.') &&
!value.includes('/') &&
!value.includes('\\') &&
!value.includes('\0') &&
Buffer.byteLength(value) <= 255,
);
const packedSchema = z
.object({
schema: z.literal('theorem-os.theorem-knowledge.v2'),
evidence: z.literal('recorded-not-replayed'),
requiresFreshCheck: z.literal(true),
entries: z.array(theoremKnowledgeEntrySchema),
routeFindings: z.array(knowledgeRouteFindingSchema).optional(),
contexts: z.record(hash, z.record(z.string(), hash)),
sources: z.record(hash, sourceIdentity),
objectsDirectory: directoryName,
originSha256: hash.optional(),
libraryAuthorization: libraryKnowledgeAuthorizationSchema.optional(),
})
.strict();
type PackedManifest = Omit<z.infer<typeof packedSchema>, 'sources'> & { sources: Record<string, { bytes: number }> };
function validateManifest(value: unknown): PackedManifest {
const parsed = packedSchema.safeParse(value);
if (!parsed.success) throw new Error('math_knowledge_packed_schema_invalid');
if (Object.values(parsed.data.sources).reduce((total, item) => total + item.bytes, 0) > maxBytes)
throw new Error('math_knowledge_packed_sources_too_large');
return parsed.data as PackedManifest;
}
function parseManifest(text: string): PackedManifest {
return validateManifest(parseMathJson(text, 'math_knowledge_packed_schema_invalid'));
}
function objectsRoot(file: string, manifest: PackedManifest) {
const root = path.join(path.dirname(file), manifest.objectsDirectory);
assertMathCanonicalPath(root);
const stat = fs.lstatSync(root, { throwIfNoEntry: false });
if (!stat?.isDirectory()) throw new Error('math_knowledge_packed_objects_missing');
return root;
}
function inspectSource(root: string, digest: string, bytes: number) {
const file = path.join(root, digest);
assertMathCanonicalPath(file);
const stat = fs.lstatSync(file, { throwIfNoEntry: false });
if (!stat?.isFile() || stat.nlink !== 1) throw new Error('math_knowledge_packed_source_missing_or_invalid');
if (stat.size !== bytes) throw new Error('math_knowledge_source_changed');
if ((stat.mode & 0o222) !== 0) throw new Error('math_knowledge_packed_source_not_frozen');
return file;
}
function readSource(root: string, digest: string, bytes: number) {
const file = inspectSource(root, digest, bytes),
loaded = readMathFile(file, bytes).bytes;
if (loaded.length !== bytes || mathDigest(loaded) !== digest) throw new Error('math_knowledge_source_changed');
try {
return new TextDecoder('utf-8', { fatal: true }).decode(loaded);
} catch {
throw new Error('math_knowledge_packed_source_invalid_utf8');
}
}
function packedView(resolved: string, text: string, digest: string, manifest: PackedManifest) {
const sources: Record<string, string> = {};
const root = path.join(path.dirname(resolved), manifest.objectsDirectory);
for (const [sourceDigest, identity] of Object.entries(manifest.sources))
Object.defineProperty(sources, sourceDigest, {
enumerable: true,
get: () => {
if (nativeProcess)
return nativeProcess.readKnowledgeObject(resolved, manifest.objectsDirectory, sourceDigest, identity.bytes);
objectsRoot(resolved, manifest);
return readSource(root, sourceDigest, identity.bytes);
},
});
const base: TheoremKnowledgeBase = {
schema: 'theorem-os.theorem-knowledge.v1',
evidence: manifest.evidence,
requiresFreshCheck: manifest.requiresFreshCheck,
entries: manifest.entries,
contexts: manifest.contexts,
sources,
...(manifest.libraryAuthorization ? { libraryAuthorization: manifest.libraryAuthorization } : {}),
...(manifest.routeFindings ? { routeFindings: manifest.routeFindings } : {}),
};
validateKnowledgeMetadata(base, manifest.sources);
if (nativeProcess) nativeProcess.inspectKnowledgeObjects(resolved, manifest.objectsDirectory, manifest.sources);
else {
objectsRoot(resolved, manifest);
for (const [sourceDigest, identity] of Object.entries(manifest.sources))
inspectSource(root, sourceDigest, identity.bytes);
}
return { base, digest, text, format: 'v2' as const };
}
function writeFrozen(file: string, contents: string) {
assertMathCanonicalPath(file);
writeMathFrozen(file, contents);
}
/** Publish blobs before their immutable manifest; an interrupted publication never exposes a complete catalog. */
export function writePackedKnowledge(file: string, base: TheoremKnowledgeBase, originSha256?: string) {
const resolved = path.resolve(file);
assertMathCanonicalPath(resolved);
const objectsDirectory = path.basename(resolved) + '.objects';
if (
!directoryName.safeParse(objectsDirectory).success ||
(originSha256 !== undefined && !hash.safeParse(originSha256).success)
)
throw new Error('math_knowledge_packed_schema_invalid');
const root = path.join(path.dirname(resolved), objectsDirectory);
if (fs.lstatSync(resolved, { throwIfNoEntry: false }) || fs.lstatSync(root, { throwIfNoEntry: false }))
throw new Error('math_knowledge_packed_output_exists');
const checked = validateTheoremKnowledge(base);
const manifest: PackedManifest = {
schema: 'theorem-os.theorem-knowledge.v2',
evidence: checked.evidence,
requiresFreshCheck: checked.requiresFreshCheck,
entries: checked.entries,
contexts: checked.contexts,
...(checked.libraryAuthorization ? { libraryAuthorization: checked.libraryAuthorization } : {}),
...(checked.routeFindings ? { routeFindings: checked.routeFindings } : {}),
sources: Object.fromEntries(
Object.entries(checked.sources).map(([digest, text]) => [digest, { bytes: Buffer.byteLength(text) }]),
),
objectsDirectory,
...(originSha256 === undefined ? {} : { originSha256 }),
};
const text = canonicalMathJson(manifest) + '\n';
if (Buffer.byteLength(text) > maxBytes) throw new Error('math_knowledge_packed_too_large');
parseManifest(text);
if (nativeProcess) nativeProcess.writeKnowledgeObjects(resolved, objectsDirectory, text, checked.sources);
else {
fs.mkdirSync(path.dirname(resolved), { recursive: true, mode: 0o700 });
assertMathCanonicalPath(path.dirname(resolved));
fs.mkdirSync(root, { mode: 0o700 });
for (const [sourceDigest, source] of Object.entries(checked.sources))
writeFrozen(path.join(root, sourceDigest), source);
syncMathDirectory(root);
writeFrozen(resolved, text);
syncMathDirectory(path.dirname(resolved));
}
return { file: resolved, sha256: mathDigest(text), format: 'v2' as const, ...theoremKnowledgeStats(checked) };
}
/** Worker copies retain the exact manifest bytes, and verify existing objects before an idempotent retry. */
export function copyPackedKnowledge(manifestText: string, fromCatalogFile: string, toCatalogFile: string) {
const digest = mathDigest(manifestText),
source = readKnowledgeCatalog(fromCatalogFile, digest);
const manifest = parseManifest(manifestText),
resolved = path.resolve(toCatalogFile);
assertMathCanonicalPath(resolved);
const root = path.join(path.dirname(resolved), manifest.objectsDirectory);
assertMathCanonicalPath(root);
const existingManifest = fs.lstatSync(resolved, { throwIfNoEntry: false });
if (
existingManifest &&
(!existingManifest.isFile() ||
(existingManifest.mode & 0o222) !== 0 ||
mathDigest(readMathText(resolved, maxBytes)) !== digest)
)
throw new Error('math_knowledge_identity_changed');
const existingRoot = fs.lstatSync(root, { throwIfNoEntry: false });
if (existingRoot && !existingRoot.isDirectory()) throw new Error('math_knowledge_packed_objects_missing');
const sources = Object.entries(source.base.sources);
for (const [sourceDigest] of sources) {
const target = path.join(root, sourceDigest);
if (fs.lstatSync(target, { throwIfNoEntry: false }))
readSource(root, sourceDigest, manifest.sources[sourceDigest]!.bytes);
}
fs.mkdirSync(path.dirname(resolved), { recursive: true, mode: 0o700 });
assertMathCanonicalPath(path.dirname(resolved));
if (!existingRoot) fs.mkdirSync(root, { mode: 0o700 });
for (const [sourceDigest, text] of sources) {
const target = path.join(root, sourceDigest);
if (!fs.lstatSync(target, { throwIfNoEntry: false })) writeFrozen(target, text);
}
syncMathDirectory(root);
if (!existingManifest) writeFrozen(resolved, manifestText);
syncMathDirectory(path.dirname(resolved));
return { file: resolved, sha256: digest, format: 'v2' as const, ...theoremKnowledgeStats(source.base) };
}
/** Existing monolithic catalogs remain readable while new manifests load only their metadata: the pinned v2 manifest
* authenticates metadata and source hashes, and proof bytes are checked only when requested. */
export function readKnowledgeCatalog(file: string, expectedDigest?: string) {
const resolved = path.resolve(file);
assertMathCanonicalPath(resolved);
const text = readMathText(resolved, maxBytes),
digest = mathDigest(text);
if (expectedDigest && (!hash.safeParse(expectedDigest).success || expectedDigest !== digest))
throw new Error('math_knowledge_identity_changed');
const value = parseMathJson(text, 'math_knowledge_schema_invalid');
const version = (value as { schema?: unknown } | null)?.schema;
if (version === 'theorem-os.theorem-knowledge.v2') return packedView(resolved, text, digest, validateManifest(value));
if (version !== 'theorem-os.theorem-knowledge.v1') throw new Error('math_knowledge_schema_invalid');
return { base: validateTheoremKnowledge(value), digest, text, format: 'v1' as const };
}
export function copyKnowledgeSnapshot(
knowledge: { text: string; digest: string; format: 'v1' | 'v2' },
fromFile: string,
toFile: string,
) {
if (!hash.safeParse(knowledge.digest).success || mathDigest(knowledge.text) !== knowledge.digest)
throw new Error('math_knowledge_identity_changed');
if (knowledge.format === 'v2') return copyPackedKnowledge(knowledge.text, fromFile, toFile);
const resolved = path.resolve(toFile);
assertMathCanonicalPath(resolved);
const existing = fs.lstatSync(resolved, { throwIfNoEntry: false });
if (existing) {
if (
!existing.isFile() ||
(existing.mode & 0o222) !== 0 ||
mathDigest(readMathText(resolved, maxBytes)) !== knowledge.digest
)
throw new Error('math_knowledge_identity_changed');
} else {
fs.mkdirSync(path.dirname(resolved), { recursive: true, mode: 0o700 });
writeFrozen(resolved, knowledge.text);
syncMathDirectory(path.dirname(resolved));
}
return { file: resolved, sha256: knowledge.digest, format: 'v1' as const };
}