Add R2 persistence and manual conflict repair

This commit is contained in:
lezi-fun committed 2026-08-13 22:42:43 +08:00
1 parent 2d50da41cd
commit e8ee698fa4
19 files changed
+1258 -62

No files matched your search

+137
View File
@@ -0,0 +1,137 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import {
normalizeObjectPrefix,
pullRequestReviewHistoryObjectKey,
pullRequestReviewObjectKey,
repositoryKnowledgeObjectKey,
restorePersistentCache,
savePersistentCache,
type PersistentObjectStore
} from "../src/storage/cacheStore.js";
import { saveRepositoryKnowledgeCache } from "../src/repository/knowledge.js";
import { saveReviewCache } from "../src/review/cache.js";
const decision = {
review: [],
change: [],
comment: "Looks good.",
result: {
canMerge: true,
summary: "Safe to merge.",
shouldClosePullRequest: false,
closeReason: ""
}
};
test("R2 cache keys are repository scoped and support a safe object prefix", () => {
assert.equal(
repositoryKnowledgeObjectKey("12345", "forum-114614"),
"forum-114614/repositories/12345/knowledge/repository.md"
);
assert.equal(
pullRequestReviewObjectKey("12345", 17, "/forum-114614/"),
"forum-114614/repositories/12345/pulls/17/latest.json"
);
assert.equal(
pullRequestReviewHistoryObjectKey("12345", 17, "a".repeat(40), "forum-114614"),
`forum-114614/repositories/12345/pulls/17/reviews/${"a".repeat(40)}.json`
);
assert.equal(normalizeObjectPrefix(" nested/cache "), "nested/cache");
assert.throws(() => normalizeObjectPrefix("../escape"), /safe slash-separated/);
assert.throws(() => repositoryKnowledgeObjectKey("owner-name"), /only digits/);
});
test("persistent cache restores and saves validated repository and PR state", async () => {
const source = await fs.mkdtemp(path.join(os.tmpdir(), "ghbot-r2-source-"));
const restored = await fs.mkdtemp(path.join(os.tmpdir(), "ghbot-r2-restored-"));
const objects = new Map<string, Buffer>();
const storage: PersistentObjectStore = {
download: async (key) => objects.get(key),
upload: async ({ key, body }) => { objects.set(key, Buffer.from(body)); }
};
try {
const originalCwd = process.cwd();
process.chdir(source);
try {
await saveRepositoryKnowledgeCache("# Tests\n\nRun npm test.\n", source);
await saveReviewCache({
owner: "forumlify",
repo: "public",
pullNumber: 17,
headSha: "a".repeat(40),
decision
});
await savePersistentCache({
repositoryId: "12345",
owner: "forumlify",
repo: "public",
pullNumber: 17,
prefix: "forum-114614",
runtimeDirectory: source,
storage
});
} finally {
process.chdir(originalCwd);
}
assert.equal(objects.size, 3);
await restorePersistentCache({
repositoryId: "12345",
owner: "forumlify",
repo: "public",
pullNumber: 17,
prefix: "forum-114614",
runtimeDirectory: restored,
storage
});
assert.equal(
await fs.readFile(path.join(restored, ".ghbot-knowledge/repository.md"), "utf8"),
"# Tests\n\nRun npm test.\n"
);
const restoredReview = JSON.parse(
await fs.readFile(path.join(restored, ".ghbot-cache/pr-17.json"), "utf8")
);
assert.equal(restoredReview.headSha, "a".repeat(40));
} finally {
await fs.rm(source, { recursive: true, force: true });
await fs.rm(restored, { recursive: true, force: true });
}
});
test("persistent cache rejects a review object belonging to another repository", async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "ghbot-r2-invalid-"));
const key = pullRequestReviewObjectKey("12345", 17);
const storage: PersistentObjectStore = {
download: async (requestedKey) => requestedKey === key
? Buffer.from(JSON.stringify({
version: 1,
repository: "attacker/fork",
pullNumber: 17,
headSha: "b".repeat(40),
reviewedAt: new Date().toISOString(),
decision
}))
: undefined,
upload: async () => undefined
};
try {
await assert.rejects(
restorePersistentCache({
repositoryId: "12345",
owner: "forumlify",
repo: "public",
pullNumber: 17,
runtimeDirectory: root,
storage
}),
/identity does not match/
);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
});
+24 -5
View File
@@ -1,9 +1,28 @@
import assert from "node:assert/strict";
import test from "node:test";
import { reviewCacheKeyPrefix } from "../src/review/cache.js";
import { parseReviewCacheContent } from "../src/review/cache.js";
test("review cache keys are isolated by repository and pull request", () => {
assert.equal(reviewCacheKeyPrefix("12345", 17), "ghbot-review-12345-pr-17-");
assert.notEqual(reviewCacheKeyPrefix("12345", 17), reviewCacheKeyPrefix("12345", 18));
assert.notEqual(reviewCacheKeyPrefix("12345", 17), reviewCacheKeyPrefix("67890", 17));
test("review cache parser accepts the structured review result", () => {
const parsed = parseReviewCacheContent(JSON.stringify({
version: 1,
repository: "forumlify/public",
pullNumber: 17,
headSha: "a".repeat(40),
reviewedAt: "2026-08-13T00:00:00.000Z",
decision: {
review: [],
change: [],
comment: "Looks good.",
result: {
canMerge: true,
summary: "Safe to merge.",
shouldClosePullRequest: false,
closeReason: ""
}
}
}));
assert.equal(parsed.repository, "forumlify/public");
assert.equal(parsed.pullNumber, 17);
assert.equal(parsed.decision.result.canMerge, true);
assert.throws(() => parseReviewCacheContent("{}"));
});
+8 -1
View File
@@ -11,7 +11,7 @@ import {
parseReviewStateMarker
} from "../src/review/policy.js";
import { config } from "../src/config.js";
import { isRecheckComment } from "../src/review/processor.js";
import { isConflictComment, isRecheckComment } from "../src/review/processor.js";
const cleanDecision: ReviewDecision = {
review: [],
@@ -123,6 +123,13 @@ test("only the exact recheck command triggers a manual review", () => {
assert.equal(isRecheckComment("/recheck something"), false);
});
test("only the exact conflict command triggers manual conflict resolution", () => {
assert.equal(isConflictComment("/conflict"), true);
assert.equal(isConflictComment(" /conflict\n"), true);
assert.equal(isConflictComment("/conflicts"), false);
assert.equal(isConflictComment("/conflict now"), false);
});
test("only the latest decisive review per user can approve the current head", () => {
assert.deepEqual(
approvedLoginsForHead(