Add configurable review modes and conflict recovery

This commit is contained in:
lezi-fun committed 2026-08-13 21:54:50 +08:00
1 parent 8c4e866765
commit f9c632e6ab
21 files changed
+1035 -230

No files matched your search

+88
View File
@@ -0,0 +1,88 @@
import fs from "node:fs/promises";
import path from "node:path";
export const REPOSITORY_KNOWLEDGE_CACHE_PATH = ".ghbot-knowledge/repository.md";
export const REPOSITORY_KNOWLEDGE_SCRATCH_PATH = ".ghbot/repository-knowledge.md";
export const MAX_REPOSITORY_KNOWLEDGE_BYTES = 32 * 1024;
const EMPTY_KNOWLEDGE = `# ghbot repository knowledge
Record only verified, durable facts about this repository, such as architecture,
supported environments, test commands, conventions, and recurring pitfalls.
The repository can evolve. Revise or remove entries when newer code or verified
results show that earlier knowledge is outdated, replaced, or no longer true.
Never record credentials, tokens, private keys, personal data, or temporary PR state.
`;
export async function loadRepositoryKnowledge(
runtimeDirectory: string = process.cwd()
): Promise<string> {
try {
const content = await fs.readFile(
path.join(runtimeDirectory, REPOSITORY_KNOWLEDGE_CACHE_PATH),
"utf8"
);
validateRepositoryKnowledge(content);
return normalizeKnowledge(content);
} catch (error) {
if (!isNotFoundError(error)) {
throw error;
}
return normalizeKnowledge(EMPTY_KNOWLEDGE);
}
}
export async function writeKnowledgeScratch(snapshot: string, content: string): Promise<void> {
validateRepositoryKnowledge(content);
const target = path.join(snapshot, REPOSITORY_KNOWLEDGE_SCRATCH_PATH);
await fs.mkdir(path.dirname(target), { recursive: true });
await fs.writeFile(target, normalizeKnowledge(content), "utf8");
}
export async function readKnowledgeScratch(snapshot: string): Promise<string> {
const content = await fs.readFile(
path.join(snapshot, REPOSITORY_KNOWLEDGE_SCRATCH_PATH),
"utf8"
);
validateRepositoryKnowledge(content);
return normalizeKnowledge(content);
}
export async function saveRepositoryKnowledgeCache(
content: string,
runtimeDirectory: string = process.cwd()
): Promise<void> {
validateRepositoryKnowledge(content);
const target = path.join(runtimeDirectory, REPOSITORY_KNOWLEDGE_CACHE_PATH);
await fs.mkdir(path.dirname(target), { recursive: true });
await fs.writeFile(target, normalizeKnowledge(content), "utf8");
}
export function validateRepositoryKnowledge(content: string): void {
const bytes = Buffer.byteLength(content, "utf8");
if (bytes === 0 || bytes > MAX_REPOSITORY_KNOWLEDGE_BYTES) {
throw new Error(`Repository knowledge must contain 1-${MAX_REPOSITORY_KNOWLEDGE_BYTES} bytes.`);
}
if (content.includes("\0")) {
throw new Error("Repository knowledge must be plain text.");
}
const secretPatterns = [
/-----BEGIN [A-Z ]*PRIVATE KEY-----/i,
/\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{20,}\b/,
/\bgithub_pat_[A-Za-z0-9_]{20,}\b/,
/\bsk-[A-Za-z0-9_-]{20,}\b/,
/\bBearer\s+[A-Za-z0-9._~+/=-]{20,}\b/i
];
if (secretPatterns.some((pattern) => pattern.test(content))) {
throw new Error("Repository knowledge appears to contain a credential or private key.");
}
}
export function normalizeKnowledge(content: string): string {
return `${content.trim()}\n`;
}
function isNotFoundError(error: unknown): boolean {
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
}