test(FN-4432): complete Step 3 — cover post-merge audit failure emission

Fusion-Task-Id: FN-4432
Fusion-Task-Lineage: ac5d5aa4-1c6e-4cd1-a779-c58f62ee0357
This commit is contained in:
Fusion
2026-05-13 23:41:41 -07:00
committed by gsxdsm
parent 7bb1497dc8
commit f3bc82dbe0
2 changed files with 308 additions and 47 deletions

View File

@@ -0,0 +1,236 @@
import { describe, expect, it, vi } from "vitest";
import type { TaskStore, RunAuditEventInput } from "@fusion/core";
import {
SquashAuditError,
handleDirtyPostMergeAuditOutcome,
} from "../merger.js";
import { createRunAuditor, type RunAuditor } from "../run-audit.js";
import type {
SquashAuditDuplicateSubjectFinding,
SquashAuditFindings,
SquashAuditTouchedFileOverlapFinding,
} from "../merger-squash-audit.js";
function overlap(file: string): SquashAuditTouchedFileOverlapFinding {
return {
type: "touched-file-overlap",
file,
recentMainCommits: [{ sha: "abcdef12", subject: "chore: recent main edit" }],
};
}
function duplicateSubject(subject: string): SquashAuditDuplicateSubjectFinding {
return { type: "duplicate-subject", subject };
}
function findings(opts: {
strategy: "squash" | "rebase";
duplicates?: SquashAuditDuplicateSubjectFinding[];
overlaps?: SquashAuditTouchedFileOverlapFinding[];
}): SquashAuditFindings {
const duplicates = opts.duplicates ?? [];
const overlaps = opts.overlaps ?? [];
const all = [...duplicates, ...overlaps];
const base = {
parentSha: "0".repeat(40),
lookback: 30,
branchSubjects: [],
recentMainSubjects: [],
duplicateSubjects: duplicates,
touchedFiles: overlaps.map((o) => o.file),
touchedFileOverlaps: overlaps,
findings: all,
issueCount: all.length,
clean: all.length === 0,
};
if (opts.strategy === "rebase") {
return {
...base,
strategy: "rebase",
rangeBaseSha: "1".repeat(40),
rangeHeadSha: "2".repeat(40),
auditTargetLabel: "11111111..22222222",
};
}
return {
...base,
strategy: "squash",
squashSha: "3".repeat(40),
squashSubject: "feat: squash",
auditTargetLabel: "33333333",
};
}
function createStore(recordImpl?: (input: RunAuditEventInput) => Promise<void>) {
const recordRunAuditEvent = vi.fn(recordImpl ?? (async () => {}));
const appendAgentLog = vi.fn(async () => {});
const updateTask = vi.fn(async () => {});
const store = {
recordRunAuditEvent,
appendAgentLog,
updateTask,
} as unknown as TaskStore;
return { store, recordRunAuditEvent, appendAgentLog, updateTask };
}
function createAudit(store: TaskStore): RunAuditor {
return createRunAuditor(store, {
runId: "run-1",
agentId: "agent-1",
taskId: "FN-4432",
phase: "merge-attempt-1",
});
}
async function dispatchLikeMerger(opts: {
mode: "block" | "warn" | "off";
dirtyFindings: SquashAuditFindings;
audit: RunAuditor;
store: TaskStore;
verificationPassed: boolean;
}) {
if (opts.mode === "off") return;
if (opts.dirtyFindings.clean) return;
await handleDirtyPostMergeAuditOutcome({
taskId: "FN-4432",
auditSha: "a".repeat(40),
mode: opts.mode,
strategy: opts.dirtyFindings.strategy,
findings: opts.dirtyFindings,
verificationPassed: opts.verificationPassed,
audit: opts.audit,
store: opts.store,
mergerLog: { warn: vi.fn(), log: vi.fn() },
});
}
describe("post-merge audit failure run_audit emission (FN-4432)", () => {
it("emits merge:audit-failure in block mode and still throws SquashAuditError", async () => {
const { store, recordRunAuditEvent } = createStore();
const audit = createAudit(store);
await expect(
handleDirtyPostMergeAuditOutcome({
taskId: "FN-4432",
auditSha: "a".repeat(40),
mode: "block",
strategy: "squash",
findings: findings({ strategy: "squash", duplicates: [duplicateSubject("feat: collide")] }),
verificationPassed: false,
audit,
store,
mergerLog: { warn: vi.fn(), log: vi.fn() },
}),
).rejects.toBeInstanceOf(SquashAuditError);
expect(recordRunAuditEvent).toHaveBeenCalledTimes(1);
expect(recordRunAuditEvent.mock.calls[0][0].mutationType).toBe("merge:audit-failure");
expect(recordRunAuditEvent.mock.calls[0][0].metadata).toMatchObject({
mode: "block",
action: "block",
reason: "mode-block",
strategy: "squash",
});
});
it("emits once in warn mode with pass/mode-warn and does not throw", async () => {
const { store, recordRunAuditEvent } = createStore();
const audit = createAudit(store);
await expect(
handleDirtyPostMergeAuditOutcome({
taskId: "FN-4432",
auditSha: "a".repeat(40),
mode: "warn",
strategy: "squash",
findings: findings({ strategy: "squash", duplicates: [duplicateSubject("feat: collide")] }),
verificationPassed: false,
audit,
store,
mergerLog: { warn: vi.fn(), log: vi.fn() },
}),
).resolves.toMatchObject({ action: "pass", reason: "mode-warn" });
expect(recordRunAuditEvent).toHaveBeenCalledTimes(1);
expect(recordRunAuditEvent.mock.calls[0][0].metadata).toMatchObject({ mode: "warn", action: "pass", reason: "mode-warn" });
});
it("emits verified-short-circuit for rebase overlap-only findings", async () => {
const { store, recordRunAuditEvent } = createStore();
const audit = createAudit(store);
await expect(
handleDirtyPostMergeAuditOutcome({
taskId: "FN-4432",
auditSha: "a".repeat(40),
mode: "block",
strategy: "rebase",
findings: findings({ strategy: "rebase", overlaps: [overlap("docs/README.md")] }),
verificationPassed: true,
audit,
store,
mergerLog: { warn: vi.fn(), log: vi.fn() },
}),
).resolves.toMatchObject({ action: "pass", reason: "verified-short-circuit" });
expect(recordRunAuditEvent).toHaveBeenCalledTimes(1);
expect(recordRunAuditEvent.mock.calls[0][0].metadata).toMatchObject({
strategy: "rebase",
action: "pass",
reason: "verified-short-circuit",
});
});
it("skips emission for clean audits", async () => {
const { store, recordRunAuditEvent } = createStore();
const audit = createAudit(store);
await dispatchLikeMerger({
mode: "block",
dirtyFindings: findings({ strategy: "squash" }),
audit,
store,
verificationPassed: false,
});
expect(recordRunAuditEvent).not.toHaveBeenCalled();
});
it("skips emission when mode=off", async () => {
const { store, recordRunAuditEvent } = createStore();
const audit = createAudit(store);
await dispatchLikeMerger({
mode: "off",
dirtyFindings: findings({ strategy: "squash", duplicates: [duplicateSubject("feat: collide")] }),
audit,
store,
verificationPassed: false,
});
expect(recordRunAuditEvent).not.toHaveBeenCalled();
});
it("swallows recording failures and still throws block-mode SquashAuditError", async () => {
const { store, recordRunAuditEvent } = createStore(async () => {
throw new Error("db unavailable");
});
const audit = createAudit(store);
await expect(
handleDirtyPostMergeAuditOutcome({
taskId: "FN-4432",
auditSha: "a".repeat(40),
mode: "block",
strategy: "squash",
findings: findings({ strategy: "squash", duplicates: [duplicateSubject("feat: collide")] }),
verificationPassed: false,
audit,
store,
mergerLog: { warn: vi.fn(), log: vi.fn() },
}),
).rejects.toBeInstanceOf(SquashAuditError);
expect(recordRunAuditEvent).toHaveBeenCalledTimes(1);
});
});

View File

@@ -73,7 +73,7 @@ import { withRateLimitRetry } from "./rate-limit-retry.js";
import { resolveAgentInstructions, buildSystemPromptWithInstructions } from "./agent-instructions.js";
import type { ToolDefinition } from "@mariozechner/pi-coding-agent";
import { Type } from "typebox";
import { createRunAuditor, generateSyntheticRunId, type EngineRunContext } from "./run-audit.js";
import { createRunAuditor, generateSyntheticRunId, type EngineRunContext, type RunAuditor } from "./run-audit.js";
import { createWebFetchTool } from "./agent-tools.js";
import { auditSquashMerge, MERGER_MAIN_OVERLAP_LOOKBACK_COMMITS, type PostMergeAuditStrategy, type SquashAuditFindings } from "./merger-squash-audit.js";
import { detectMergeOverlap, restoreBranchWinsFiles } from "./merger-overlap-guard.js";
@@ -4838,6 +4838,71 @@ export function resolvePostMergeAuditAction(opts: {
return { action: "block", reason: "mode-block" };
}
export async function handleDirtyPostMergeAuditOutcome(opts: {
taskId: string;
auditSha: string;
mode: PostMergeAuditMode;
strategy: PostMergeAuditStrategy;
findings: SquashAuditFindings;
verificationPassed: boolean;
audit: RunAuditor;
store: TaskStore;
mergerLog: Pick<ReturnType<typeof createLogger>, "warn" | "log">;
}): Promise<PostMergeAuditAction> {
const decision = resolvePostMergeAuditAction({
mode: opts.mode,
strategy: opts.strategy,
findings: opts.findings,
verificationPassed: opts.verificationPassed,
});
try {
await opts.audit.git({
type: "merge:audit-failure",
target: opts.auditSha,
metadata: {
mode: opts.mode,
strategy: opts.strategy,
action: decision.action,
reason: decision.reason,
issueCount: opts.findings.issueCount,
duplicateSubjectCount: opts.findings.duplicateSubjects.length,
touchedFileOverlapCount: opts.findings.touchedFileOverlaps.length,
verificationPassed: opts.verificationPassed,
auditTargetLabel: opts.findings.auditTargetLabel,
},
});
} catch (err) {
opts.mergerLog.warn(`${opts.taskId}: failed to record merge:audit-failure run_audit event: ${String(err)}`);
}
if (decision.action === "block") {
const auditError = new SquashAuditError(opts.taskId, opts.auditSha, opts.findings);
await opts.store.appendAgentLog(
opts.taskId,
auditError.message,
"tool_error",
formatSquashAuditAgentLog(opts.findings),
"merger",
);
await opts.store.updateTask(opts.taskId, { status: null });
throw auditError;
}
const passLabel = decision.reason === "verified-short-circuit"
? `${opts.strategy === "rebase" ? "post-rebase" : "post-squash"} audit overlap cleared by deterministic verification`
: `${opts.strategy === "rebase" ? "post-rebase" : "post-squash"} audit found ${opts.findings.issueCount} risk(s) — continuing (mode=warn)`;
await opts.store.appendAgentLog(
opts.taskId,
passLabel,
"text",
formatSquashAuditAgentLog(opts.findings),
"merger",
);
opts.mergerLog.log(`${opts.taskId}: ${passLabel}`);
return decision;
}
function buildPostMergeAuditBlockingMessage(taskId: string, findings: SquashAuditFindings): string {
const riskParts: string[] = [];
if (findings.duplicateSubjects.length > 0) {
@@ -6950,57 +7015,17 @@ export async function aiMergeTask(
}
}
const decision = resolvePostMergeAuditAction({
await handleDirtyPostMergeAuditOutcome({
taskId,
auditSha,
mode: postMergeAuditMode,
strategy: selectedPostMergeAuditStrategy,
findings: auditFindings,
verificationPassed,
audit,
store,
mergerLog,
});
try {
await audit.git({
type: "merge:audit-failure",
target: auditSha,
metadata: {
mode: postMergeAuditMode,
strategy: selectedPostMergeAuditStrategy,
action: decision.action,
reason: decision.reason,
issueCount: auditFindings.issueCount,
duplicateSubjectCount: auditFindings.duplicateSubjects.length,
touchedFileOverlapCount: auditFindings.touchedFileOverlaps.length,
verificationPassed,
auditTargetLabel: auditFindings.auditTargetLabel,
},
});
} catch (err) {
mergerLog.warn(`${taskId}: failed to record merge:audit-failure run_audit event: ${String(err)}`);
}
if (decision.action === "block") {
const auditError = new SquashAuditError(taskId, auditSha, auditFindings);
await store.appendAgentLog(
taskId,
auditError.message,
"tool_error",
formatSquashAuditAgentLog(auditFindings),
"merger",
);
await store.updateTask(taskId, { status: null });
throw auditError;
}
const passLabel = decision.reason === "verified-short-circuit"
? `${selectedPostMergeAuditStrategy === "rebase" ? "post-rebase" : "post-squash"} audit overlap cleared by deterministic verification`
: `${selectedPostMergeAuditStrategy === "rebase" ? "post-rebase" : "post-squash"} audit found ${auditFindings.issueCount} risk(s) — continuing (mode=warn)`;
await store.appendAgentLog(
taskId,
passLabel,
"text",
formatSquashAuditAgentLog(auditFindings),
"merger",
);
mergerLog.log(`${taskId}: ${passLabel}`);
} else {
await store.appendAgentLog(
taskId,