-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex-meta.ts
More file actions
261 lines (231 loc) · 8.52 KB
/
index-meta.ts
File metadata and controls
261 lines (231 loc) · 8.52 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
import { promises as fs } from 'fs';
import path from 'path';
import { z } from 'zod';
import {
CODEBASE_CONTEXT_DIRNAME,
INDEX_FORMAT_VERSION,
INDEX_META_FILENAME,
INDEX_META_VERSION,
INTELLIGENCE_FILENAME,
KEYWORD_INDEX_FILENAME,
RELATIONSHIPS_FILENAME,
VECTOR_DB_DIRNAME
} from '../constants/codebase-context.js';
import { IndexCorruptedError } from '../errors/index.js';
const ArtifactHeaderSchema = z.object({
buildId: z.string().min(1),
formatVersion: z.number().int().nonnegative()
});
const KeywordIndexFileSchema = z.object({
header: ArtifactHeaderSchema,
chunks: z.array(z.unknown())
});
const VectorDbBuildSchema = z.object({
buildId: z.string().min(1),
formatVersion: z.number().int().nonnegative()
});
const IntelligenceFileSchema = z
.object({
header: ArtifactHeaderSchema
})
.passthrough();
const RelationshipsFileSchema = z
.object({
header: ArtifactHeaderSchema
})
.passthrough();
export const IndexMetaSchema = z.object({
metaVersion: z.number().int().positive(),
formatVersion: z.number().int().nonnegative(),
buildId: z.string().min(1),
generatedAt: z.string().datetime(),
toolVersion: z.string().min(1),
artifacts: z
.object({
keywordIndex: z.object({
path: z.string().min(1)
}),
vectorDb: z.object({
path: z.string().min(1),
provider: z.string().min(1)
}),
intelligence: z
.object({
path: z.string().min(1)
})
.optional()
})
.passthrough()
});
export type IndexMeta = z.infer<typeof IndexMetaSchema>;
async function pathExists(targetPath: string): Promise<boolean> {
try {
await fs.access(targetPath);
return true;
} catch {
return false;
}
}
async function requireFile(targetPath: string, label: string): Promise<void> {
if (!(await pathExists(targetPath))) {
throw new IndexCorruptedError(`${label} missing: ${targetPath}`);
}
}
async function requireDirectory(targetPath: string, label: string): Promise<void> {
try {
const stat = await fs.stat(targetPath);
if (!stat.isDirectory()) {
throw new IndexCorruptedError(`${label} is not a directory: ${targetPath}`);
}
} catch (error) {
if (error instanceof IndexCorruptedError) throw error;
throw new IndexCorruptedError(`${label} missing: ${targetPath}`);
}
}
function asIndexCorrupted(message: string, error: unknown): IndexCorruptedError {
const suffix = error instanceof Error ? error.message : String(error);
return new IndexCorruptedError(`${message}: ${suffix}`);
}
export async function readIndexMeta(rootDir: string): Promise<IndexMeta> {
const metaPath = path.join(rootDir, CODEBASE_CONTEXT_DIRNAME, INDEX_META_FILENAME);
let parsed: unknown;
try {
const raw = await fs.readFile(metaPath, 'utf-8');
parsed = JSON.parse(raw);
} catch (error) {
throw asIndexCorrupted('Index meta missing or unreadable (rebuild required)', error);
}
const result = IndexMetaSchema.safeParse(parsed);
if (!result.success) {
throw new IndexCorruptedError(
`Index meta schema mismatch (rebuild required): ${result.error.message}`
);
}
const meta = result.data;
if (meta.metaVersion !== INDEX_META_VERSION) {
throw new IndexCorruptedError(
`Index meta version mismatch (rebuild required): expected metaVersion=${INDEX_META_VERSION}, found metaVersion=${meta.metaVersion}`
);
}
if (meta.formatVersion !== INDEX_FORMAT_VERSION) {
throw new IndexCorruptedError(
`Index format version mismatch (rebuild required): expected formatVersion=${INDEX_FORMAT_VERSION}, found formatVersion=${meta.formatVersion}`
);
}
return meta;
}
export async function validateIndexArtifacts(rootDir: string, meta: IndexMeta): Promise<void> {
const contextDir = path.join(rootDir, CODEBASE_CONTEXT_DIRNAME);
const keywordPath = path.join(contextDir, KEYWORD_INDEX_FILENAME);
const vectorDir = path.join(contextDir, VECTOR_DB_DIRNAME);
const vectorBuildPath = path.join(vectorDir, 'index-build.json');
await requireFile(keywordPath, 'Keyword index');
await requireDirectory(vectorDir, 'Vector DB directory');
await requireFile(vectorBuildPath, 'Vector DB build marker');
// Keyword index header (required)
try {
const raw = await fs.readFile(keywordPath, 'utf-8');
const json = JSON.parse(raw);
const parsed = KeywordIndexFileSchema.safeParse(json);
if (!parsed.success) {
throw new IndexCorruptedError(
`Keyword index schema mismatch (rebuild required): ${parsed.error.message}`
);
}
const { buildId, formatVersion } = parsed.data.header;
if (formatVersion !== meta.formatVersion) {
throw new IndexCorruptedError(
`Keyword index formatVersion mismatch (rebuild required): meta=${meta.formatVersion}, index.json=${formatVersion}`
);
}
if (buildId !== meta.buildId) {
throw new IndexCorruptedError(
`Keyword index buildId mismatch (rebuild required): meta=${meta.buildId}, index.json=${buildId}`
);
}
} catch (error) {
if (error instanceof IndexCorruptedError) throw error;
throw asIndexCorrupted('Keyword index corrupted (rebuild required)', error);
}
// Vector DB build marker (required)
try {
const raw = await fs.readFile(vectorBuildPath, 'utf-8');
const json = JSON.parse(raw);
const parsed = VectorDbBuildSchema.safeParse(json);
if (!parsed.success) {
throw new IndexCorruptedError(
`Vector DB build marker schema mismatch (rebuild required): ${parsed.error.message}`
);
}
const { buildId, formatVersion } = parsed.data;
if (formatVersion !== meta.formatVersion) {
throw new IndexCorruptedError(
`Vector DB formatVersion mismatch (rebuild required): meta=${meta.formatVersion}, index-build.json=${formatVersion}`
);
}
if (buildId !== meta.buildId) {
throw new IndexCorruptedError(
`Vector DB buildId mismatch (rebuild required): meta=${meta.buildId}, index-build.json=${buildId}`
);
}
} catch (error) {
if (error instanceof IndexCorruptedError) throw error;
throw asIndexCorrupted('Vector DB build marker corrupted (rebuild required)', error);
}
// Optional intelligence artifact: validate if present, but do not require.
const intelligencePath = path.join(contextDir, INTELLIGENCE_FILENAME);
if (await pathExists(intelligencePath)) {
try {
const raw = await fs.readFile(intelligencePath, 'utf-8');
const json = JSON.parse(raw);
const parsed = IntelligenceFileSchema.safeParse(json);
if (!parsed.success) {
throw new IndexCorruptedError(
`Intelligence schema mismatch (rebuild required): ${parsed.error.message}`
);
}
const { buildId, formatVersion } = parsed.data.header;
if (formatVersion !== meta.formatVersion) {
throw new IndexCorruptedError(
`Intelligence formatVersion mismatch (rebuild required): meta=${meta.formatVersion}, intelligence.json=${formatVersion}`
);
}
if (buildId !== meta.buildId) {
throw new IndexCorruptedError(
`Intelligence buildId mismatch (rebuild required): meta=${meta.buildId}, intelligence.json=${buildId}`
);
}
} catch (error) {
if (error instanceof IndexCorruptedError) throw error;
throw asIndexCorrupted('Intelligence corrupted (rebuild required)', error);
}
}
// Optional relationships sidecar: validate if present, but do not require.
const relationshipsPath = path.join(contextDir, RELATIONSHIPS_FILENAME);
if (await pathExists(relationshipsPath)) {
try {
const raw = await fs.readFile(relationshipsPath, 'utf-8');
const json = JSON.parse(raw);
const parsed = RelationshipsFileSchema.safeParse(json);
if (!parsed.success) {
throw new IndexCorruptedError(
`Relationships schema mismatch (rebuild required): ${parsed.error.message}`
);
}
const { buildId, formatVersion } = parsed.data.header;
if (formatVersion !== meta.formatVersion) {
throw new IndexCorruptedError(
`Relationships formatVersion mismatch (rebuild required): meta=${meta.formatVersion}, relationships.json=${formatVersion}`
);
}
if (buildId !== meta.buildId) {
throw new IndexCorruptedError(
`Relationships buildId mismatch (rebuild required): meta=${meta.buildId}, relationships.json=${buildId}`
);
}
} catch (error) {
if (error instanceof IndexCorruptedError) throw error;
throw asIndexCorrupted('Relationships sidecar corrupted (rebuild required)', error);
}
}
}