Initialize repository knowledge cache

This commit is contained in:
lezi-fun committed 2026-08-13 22:04:41 +08:00
1 parent 14a30a3695
commit 2d50da41cd
3 files changed
+31 -2

No files matched your search

+3 -1
View File
@@ -28,7 +28,9 @@ export async function loadRepositoryKnowledge(
if (!isNotFoundError(error)) { if (!isNotFoundError(error)) {
throw error; throw error;
} }
return normalizeKnowledge(EMPTY_KNOWLEDGE); const initial = normalizeKnowledge(EMPTY_KNOWLEDGE);
await saveRepositoryKnowledgeCache(initial, runtimeDirectory);
return initial;
} }
} }
+13 -1
View File
@@ -430,13 +430,25 @@ function validateAgentChangePath(relativePath: string): void {
async function assertConflictMarkersRemoved(root: string, conflictFiles: string[]): Promise<void> { async function assertConflictMarkersRemoved(root: string, conflictFiles: string[]): Promise<void> {
for (const relativePath of conflictFiles) { for (const relativePath of conflictFiles) {
const content = await fs.readFile(safeFilePath(root, relativePath), "utf8"); const content = await fs.readFile(safeFilePath(root, relativePath), "utf8").catch((error: unknown) => {
if (isFileNotFoundError(error)) {
return undefined;
}
throw error;
});
if (content === undefined) {
continue;
}
if (/^(?:<<<<<<<|=======|>>>>>>>)(?: |$)/m.test(content)) { if (/^(?:<<<<<<<|=======|>>>>>>>)(?: |$)/m.test(content)) {
throw new Error(`Conflict markers remain in ${relativePath}.`); throw new Error(`Conflict markers remain in ${relativePath}.`);
} }
} }
} }
function isFileNotFoundError(error: unknown): boolean {
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
}
function safeFilePath(root: string, relativePath: string): string { function safeFilePath(root: string, relativePath: string): string {
if (path.isAbsolute(relativePath) || relativePath.split(/[\\/]/).includes("..")) { if (path.isAbsolute(relativePath) || relativePath.split(/[\\/]/).includes("..")) {
throw new Error(`Unsafe conflict path: ${relativePath}`); throw new Error(`Unsafe conflict path: ${relativePath}`);
+15
View File
@@ -31,6 +31,21 @@ test("repository knowledge persists in a repository-level cache file", async ()
} }
}); });
test("loading an empty repository knowledge cache initializes the persisted file", async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "ghbot-knowledge-initial-"));
try {
const knowledge = await loadRepositoryKnowledge(root);
assert.match(knowledge, /repository can evolve/i);
assert.equal(await loadRepositoryKnowledge(root), knowledge);
assert.equal(
await fs.readFile(path.join(root, ".ghbot-knowledge", "repository.md"), "utf8"),
knowledge
);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
});
test("repository knowledge rejects credentials and oversized content", () => { test("repository knowledge rejects credentials and oversized content", () => {
assert.throws( assert.throws(
() => validateRepositoryKnowledge("-----BEGIN PRIVATE KEY-----\nsecret"), () => validateRepositoryKnowledge("-----BEGIN PRIVATE KEY-----\nsecret"),