feat(FN-4500): complete Step 2 — harden zero-unique classifier
Fusion-Task-Id: FN-4500 Fusion-Task-Lineage: 81d33759-70f6-4958-abf8-3db20f918c01
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { mkdtemp, rm, writeFile, appendFile, mkdir } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { exec } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import { inspectBranchConflict } from "../branch-conflicts.js";
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
async function run(command: string, cwd: string): Promise<string> {
|
||||
const { stdout } = await execAsync(command, { cwd, encoding: "utf-8" });
|
||||
return stdout.trim();
|
||||
}
|
||||
|
||||
describe("inspectBranchConflict zero-unique behavior", () => {
|
||||
const dirs: string[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
|
||||
});
|
||||
|
||||
async function setupRepo() {
|
||||
const repoDir = await mkdtemp(path.join(tmpdir(), "fn-4500-branch-conflict-"));
|
||||
dirs.push(repoDir);
|
||||
await run("git init -b main", repoDir);
|
||||
await run("git config user.email test@example.com", repoDir);
|
||||
await run("git config user.name 'Test User'", repoDir);
|
||||
await writeFile(path.join(repoDir, "note.txt"), "base\n", "utf-8");
|
||||
await run("git add note.txt && git commit -m 'chore: base'", repoDir);
|
||||
return repoDir;
|
||||
}
|
||||
|
||||
it("returns fully-subsumed when branch tip is ancestor of main", async () => {
|
||||
const repoDir = await setupRepo();
|
||||
await run("git checkout -b fusion/fn-9001", repoDir);
|
||||
await run("git checkout main", repoDir);
|
||||
const livePath = path.join(repoDir, "..", "live-9001");
|
||||
await run(`git worktree add ${JSON.stringify(livePath)} fusion/fn-9001`, repoDir);
|
||||
const stalePath = path.join(repoDir, "..", "stale-9001");
|
||||
await mkdir(stalePath, { recursive: true });
|
||||
|
||||
const result = await inspectBranchConflict({
|
||||
repoDir,
|
||||
branchName: "fusion/fn-9001",
|
||||
conflictingWorktreePath: stalePath,
|
||||
requestingTaskId: "FN-9001",
|
||||
ownerTaskId: "FN-9001",
|
||||
startPoint: "main",
|
||||
});
|
||||
|
||||
expect(result.kind).toBe("fully-subsumed");
|
||||
});
|
||||
|
||||
it("returns fully-subsumed when branch patch already exists upstream", async () => {
|
||||
const repoDir = await setupRepo();
|
||||
await run("git checkout -b fusion/fn-9001", repoDir);
|
||||
await appendFile(path.join(repoDir, "note.txt"), "change\n", "utf-8");
|
||||
await run("git add note.txt", repoDir);
|
||||
await run("git commit -m 'feat(FN-9001): change' -m 'Fusion-Task-Id: FN-9001'", repoDir);
|
||||
const branchCommit = await run("git rev-parse HEAD", repoDir);
|
||||
|
||||
await run("git checkout main", repoDir);
|
||||
await run(`git cherry-pick ${branchCommit}`, repoDir);
|
||||
|
||||
const livePath = path.join(repoDir, "..", "live-9001-upstream");
|
||||
await run(`git worktree add ${JSON.stringify(livePath)} fusion/fn-9001`, repoDir);
|
||||
const stalePath = path.join(repoDir, "..", "stale-9001-upstream");
|
||||
await mkdir(stalePath, { recursive: true });
|
||||
|
||||
const result = await inspectBranchConflict({
|
||||
repoDir,
|
||||
branchName: "fusion/fn-9001",
|
||||
conflictingWorktreePath: stalePath,
|
||||
requestingTaskId: "FN-9001",
|
||||
ownerTaskId: "FN-9001",
|
||||
startPoint: "main",
|
||||
});
|
||||
|
||||
expect(result.kind).toBe("fully-subsumed");
|
||||
});
|
||||
|
||||
it("returns reclaimable when branch still has unique commit", async () => {
|
||||
const repoDir = await setupRepo();
|
||||
await run("git checkout -b fusion/fn-9001", repoDir);
|
||||
await appendFile(path.join(repoDir, "note.txt"), "unique\n", "utf-8");
|
||||
await run("git add note.txt", repoDir);
|
||||
await run("git commit -m 'feat(FN-9001): unique' -m 'Fusion-Task-Id: FN-9001'", repoDir);
|
||||
await run("git checkout main", repoDir);
|
||||
|
||||
const livePath = path.join(repoDir, "..", "live-9001-unique");
|
||||
await run(`git worktree add ${JSON.stringify(livePath)} fusion/fn-9001`, repoDir);
|
||||
const stalePath = path.join(repoDir, "..", "stale-9001-unique");
|
||||
await mkdir(stalePath, { recursive: true });
|
||||
|
||||
const result = await inspectBranchConflict({
|
||||
repoDir,
|
||||
branchName: "fusion/fn-9001",
|
||||
conflictingWorktreePath: stalePath,
|
||||
requestingTaskId: "FN-9001",
|
||||
ownerTaskId: "FN-9001",
|
||||
startPoint: "main",
|
||||
});
|
||||
|
||||
expect(result.kind).toBe("reclaimable");
|
||||
});
|
||||
|
||||
it("keeps zero-attributed foreign branch as live-foreign", async () => {
|
||||
const repoDir = await setupRepo();
|
||||
await run("git checkout -b topic/other", repoDir);
|
||||
await appendFile(path.join(repoDir, "note.txt"), "other\n", "utf-8");
|
||||
await run("git add note.txt", repoDir);
|
||||
await run("git commit -m 'chore: other work'", repoDir);
|
||||
await run("git checkout main", repoDir);
|
||||
|
||||
const livePath = path.join(repoDir, "..", "live-other");
|
||||
await run(`git worktree add ${JSON.stringify(livePath)} topic/other`, repoDir);
|
||||
const stalePath = path.join(repoDir, "..", "stale-other");
|
||||
await mkdir(stalePath, { recursive: true });
|
||||
|
||||
const result = await inspectBranchConflict({
|
||||
repoDir,
|
||||
branchName: "topic/other",
|
||||
conflictingWorktreePath: stalePath,
|
||||
requestingTaskId: "FN-9001",
|
||||
ownerTaskId: "FN-9001",
|
||||
startPoint: "main",
|
||||
});
|
||||
|
||||
expect(result.kind).toBe("live-foreign");
|
||||
});
|
||||
});
|
||||
@@ -507,12 +507,56 @@ export async function autoRecoverCrossContamination(
|
||||
};
|
||||
}
|
||||
|
||||
function deriveTaskIdFromFusionBranch(branchName: string): string | null {
|
||||
export function deriveTaskIdFromFusionBranch(branchName: string): string | null {
|
||||
const match = /^fusion\/(fn-\d+)$/i.exec(branchName.trim());
|
||||
if (!match) return null;
|
||||
return match[1].toUpperCase();
|
||||
}
|
||||
|
||||
async function isZeroUniqueCommitBranchViaPatchIdFallback(
|
||||
repoDir: string,
|
||||
startPoint: string,
|
||||
branchName: string,
|
||||
mainRef: string,
|
||||
): Promise<boolean> {
|
||||
const range = `${startPoint}..${branchName}`;
|
||||
const branchCommitsOutput = await runGit(repoDir, `git rev-list ${quoteShellArg(range)}`).catch(() => "");
|
||||
const branchCommitShas = branchCommitsOutput
|
||||
.split("\n")
|
||||
.map((line) => line.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
if (branchCommitShas.length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const upstreamPatchIdsOutput = await runGit(
|
||||
repoDir,
|
||||
`git rev-list ${quoteShellArg(mainRef)} | while read c; do git show "$c" | git patch-id --stable; done`,
|
||||
).catch(() => "");
|
||||
|
||||
const upstreamPatchIds = new Set(
|
||||
upstreamPatchIdsOutput
|
||||
.split("\n")
|
||||
.map((line) => line.trim().split(" ")[0])
|
||||
.filter(Boolean),
|
||||
);
|
||||
|
||||
if (upstreamPatchIds.size === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const sha of branchCommitShas) {
|
||||
const patchIdLine = await runGit(repoDir, `git show ${quoteShellArg(sha)} | git patch-id --stable`).catch(() => "");
|
||||
const patchId = patchIdLine.trim().split(" ")[0];
|
||||
if (!patchId || !upstreamPatchIds.has(patchId)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function inspectBranchConflict(
|
||||
input: InspectBranchConflictInput,
|
||||
): Promise<BranchConflictInspectionResult> {
|
||||
@@ -557,6 +601,22 @@ export async function inspectBranchConflict(
|
||||
};
|
||||
}
|
||||
|
||||
if (uniqueCommitResult.degraded && uniqueCommitResult.commits.length === 0) {
|
||||
const isZeroUnique = await isZeroUniqueCommitBranchViaPatchIdFallback(
|
||||
input.repoDir,
|
||||
startPoint,
|
||||
input.branchName,
|
||||
uniqueCommitResult.mainRef,
|
||||
);
|
||||
if (isZeroUnique) {
|
||||
return {
|
||||
kind: "fully-subsumed",
|
||||
livePath,
|
||||
tipSha: existingTipSha,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const normalizedOwnerTaskId = (input.ownerTaskId ?? input.requestingTaskId).trim().toUpperCase();
|
||||
const branchOwnerTaskId = deriveTaskIdFromFusionBranch(input.branchName);
|
||||
const isSelfOwnedWorktree =
|
||||
|
||||
Reference in New Issue
Block a user