feat(FN-4222): complete Step 3 — finalize service
Fusion-Task-Id: FN-4222 Fusion-Task-Lineage: 7de6b18c-36d5-49e8-bbd7-084707e88d6a
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ExperimentSession, ExperimentSessionRecord } from "@fusion/core";
|
||||
import { ExperimentFinalizeService, __activeFinalizeLocksForTesting } from "../experiment/finalize-service.js";
|
||||
import {
|
||||
ExperimentFinalizeBranchExistsError,
|
||||
ExperimentFinalizeCherryPickConflictError,
|
||||
ExperimentFinalizeNoKeptRunsError,
|
||||
ExperimentFinalizeStateError,
|
||||
} from "../experiment/finalize-types.js";
|
||||
|
||||
function createSession(overrides: Partial<ExperimentSession> = {}): ExperimentSession {
|
||||
return {
|
||||
id: "EXP-1",
|
||||
name: "Experiment",
|
||||
status: "active",
|
||||
metric: { name: "score", direction: "maximize" },
|
||||
currentSegment: 1,
|
||||
keptRunIds: ["r1", "r2"],
|
||||
tags: [],
|
||||
createdAt: "2026-01-01T00:00:00.000Z",
|
||||
updatedAt: "2026-01-01T00:00:00.000Z",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function runRecord(id: string, seq: number, status: "keep" | "discard" = "keep", commit?: string): ExperimentSessionRecord {
|
||||
return {
|
||||
id,
|
||||
sessionId: "EXP-1",
|
||||
segment: 1,
|
||||
seq,
|
||||
type: "run",
|
||||
payload: { status, commit, primaryMetric: 1, secondaryMetrics: [] },
|
||||
createdAt: "2026-01-01T00:00:00.000Z",
|
||||
};
|
||||
}
|
||||
|
||||
describe("ExperimentFinalizeService", () => {
|
||||
const records = [runRecord("r1", 1, "keep", "c1"), runRecord("r2", 2, "keep", "c2"), runRecord("r3", 3, "discard")];
|
||||
let session: ExperimentSession;
|
||||
let store: any;
|
||||
let git: any;
|
||||
|
||||
beforeEach(() => {
|
||||
__activeFinalizeLocksForTesting.clear();
|
||||
session = createSession();
|
||||
store = {
|
||||
getSession: vi.fn(() => session),
|
||||
listRecords: vi.fn(() => records),
|
||||
updateSession: vi.fn((_id, patch) => {
|
||||
session = { ...session, ...patch };
|
||||
return session;
|
||||
}),
|
||||
appendRecord: vi.fn(() => ({ id: "fin-1" })),
|
||||
};
|
||||
git = {
|
||||
currentBranch: vi.fn(async () => "main"),
|
||||
head: vi.fn(async () => "head-sha"),
|
||||
mergeBase: vi.fn(async () => "merge-base"),
|
||||
branchExists: vi.fn(async () => false),
|
||||
createBranch: vi.fn(async () => undefined),
|
||||
checkout: vi.fn(async () => undefined),
|
||||
cherryPick: vi.fn(async () => undefined),
|
||||
deleteBranch: vi.fn(async () => undefined),
|
||||
};
|
||||
});
|
||||
|
||||
it("finalizes happy path and appends finalize record", async () => {
|
||||
const service = new ExperimentFinalizeService({ store, git });
|
||||
|
||||
const result = await service.finalize({ sessionId: "EXP-1" });
|
||||
|
||||
expect(result.branches).toHaveLength(1);
|
||||
expect(store.updateSession).toHaveBeenNthCalledWith(1, "EXP-1", { status: "finalizing" });
|
||||
expect(store.updateSession).toHaveBeenLastCalledWith("EXP-1", { status: "finalized" });
|
||||
expect(store.appendRecord).toHaveBeenCalledWith(
|
||||
"EXP-1",
|
||||
expect.objectContaining({ type: "finalize", payload: expect.objectContaining({ keptRunIds: ["r1", "r2"], discardedRunIds: ["r3"] }) }),
|
||||
);
|
||||
});
|
||||
|
||||
it("previewPlan is read-only", async () => {
|
||||
const service = new ExperimentFinalizeService({ store, git });
|
||||
|
||||
const plan = await service.previewPlan({ sessionId: "EXP-1" });
|
||||
|
||||
expect(plan.sessionId).toBe("EXP-1");
|
||||
expect(store.updateSession).not.toHaveBeenCalled();
|
||||
expect(git.createBranch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("throws no-kept-runs and keeps session active", async () => {
|
||||
session = createSession({ keptRunIds: [] });
|
||||
const service = new ExperimentFinalizeService({ store, git });
|
||||
|
||||
await expect(service.finalize({ sessionId: "EXP-1" })).rejects.toBeInstanceOf(ExperimentFinalizeNoKeptRunsError);
|
||||
expect(store.updateSession).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects concurrent finalization with lock", async () => {
|
||||
__activeFinalizeLocksForTesting.add("EXP-1");
|
||||
const service = new ExperimentFinalizeService({ store, git });
|
||||
|
||||
await expect(service.finalize({ sessionId: "EXP-1" })).rejects.toBeInstanceOf(ExperimentFinalizeStateError);
|
||||
});
|
||||
|
||||
it("rolls back when branch already exists", async () => {
|
||||
git.branchExists = vi.fn(async () => true);
|
||||
const service = new ExperimentFinalizeService({ store, git });
|
||||
|
||||
await expect(service.finalize({ sessionId: "EXP-1" })).rejects.toBeInstanceOf(ExperimentFinalizeBranchExistsError);
|
||||
expect(git.deleteBranch).not.toHaveBeenCalled();
|
||||
expect(git.checkout).toHaveBeenCalledWith("main");
|
||||
expect(session.status).toBe("finalizing");
|
||||
});
|
||||
|
||||
it("rolls back branch on cherry-pick conflict", async () => {
|
||||
git.cherryPick = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(undefined)
|
||||
.mockRejectedValueOnce(new ExperimentFinalizeCherryPickConflictError("conflict", { groupId: "segment:1", commit: "c2", stderr: "boom" }));
|
||||
|
||||
const service = new ExperimentFinalizeService({ store, git });
|
||||
|
||||
await expect(service.finalize({ sessionId: "EXP-1" })).rejects.toBeInstanceOf(ExperimentFinalizeCherryPickConflictError);
|
||||
expect(git.deleteBranch).toHaveBeenCalledWith(expect.any(String), { force: true });
|
||||
expect(git.checkout).toHaveBeenCalledWith("main");
|
||||
});
|
||||
|
||||
it("restores detached HEAD by sha", async () => {
|
||||
git.currentBranch = vi.fn(async () => null);
|
||||
git.head = vi.fn(async () => "detached-sha");
|
||||
const service = new ExperimentFinalizeService({ store, git });
|
||||
|
||||
await service.finalize({ sessionId: "EXP-1" });
|
||||
|
||||
expect(git.checkout).toHaveBeenCalledWith("detached-sha");
|
||||
});
|
||||
|
||||
it.each([
|
||||
[new ExperimentFinalizeStateError("x"), "state_error"],
|
||||
[new ExperimentFinalizeNoKeptRunsError("x"), "no_kept_runs"],
|
||||
[new ExperimentFinalizeBranchExistsError("x"), "branch_exists"],
|
||||
[new ExperimentFinalizeCherryPickConflictError("x", { groupId: "g", commit: "c", stderr: "e" }), "cherry_pick_conflict"],
|
||||
])("error code literal %s", (error: any, expectedCode: string) => {
|
||||
expect(error.code).toBe(expectedCode);
|
||||
});
|
||||
});
|
||||
185
packages/engine/src/experiment/finalize-service.ts
Normal file
185
packages/engine/src/experiment/finalize-service.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
import type { ExperimentSessionRecord, ExperimentSessionStore } from "@fusion/core";
|
||||
import { buildDefaultPlan, mergePlanWithUserOverrides } from "./finalize-plan.js";
|
||||
import {
|
||||
ExperimentFinalizeBranchExistsError,
|
||||
ExperimentFinalizeNoKeptRunsError,
|
||||
ExperimentFinalizeStateError,
|
||||
type FinalizePlan,
|
||||
type FinalizePlanOverride,
|
||||
type FinalizeResult,
|
||||
} from "./finalize-types.js";
|
||||
import type { GitOps } from "./git-ops.js";
|
||||
import { createLogger, formatError } from "../logger.js";
|
||||
|
||||
const ACTIVE_FINALIZE_LOCKS = new Set<string>();
|
||||
export const __activeFinalizeLocksForTesting = ACTIVE_FINALIZE_LOCKS;
|
||||
|
||||
interface FinalizeServiceOptions {
|
||||
store: ExperimentSessionStore;
|
||||
git: GitOps;
|
||||
logger?: ReturnType<typeof createLogger>;
|
||||
}
|
||||
|
||||
export class ExperimentFinalizeService {
|
||||
private readonly logger;
|
||||
|
||||
constructor(private readonly options: FinalizeServiceOptions) {
|
||||
this.logger = options.logger ?? createLogger("experiment-finalize");
|
||||
}
|
||||
|
||||
async previewPlan(input: { sessionId: string; integrationBranch?: string }): Promise<FinalizePlan> {
|
||||
const integrationBranch = input.integrationBranch ?? "main";
|
||||
const session = this.options.store.getSession(input.sessionId);
|
||||
if (!session) throw new ExperimentFinalizeStateError(`Experiment session not found: ${input.sessionId}`);
|
||||
if (session.status !== "active") {
|
||||
throw new ExperimentFinalizeStateError(`Session ${input.sessionId} is not active (status: ${session.status})`);
|
||||
}
|
||||
if (!session.keptRunIds.length) throw new ExperimentFinalizeNoKeptRunsError(`Session ${input.sessionId} has no kept runs`);
|
||||
|
||||
const records = this.options.store.listRecords(input.sessionId);
|
||||
const baselineRef = this.resolveBaselineRef(session.baselineRunId, session.metadata?.baselineCommit, records, integrationBranch);
|
||||
const mergeBaseCommit = await this.options.git.mergeBase(baselineRef, integrationBranch);
|
||||
|
||||
return buildDefaultPlan({
|
||||
session,
|
||||
records,
|
||||
integrationBranch,
|
||||
mergeBaseCommit,
|
||||
});
|
||||
}
|
||||
|
||||
async finalize(input: {
|
||||
sessionId: string;
|
||||
integrationBranch?: string;
|
||||
planOverride?: FinalizePlanOverride;
|
||||
summary?: string;
|
||||
allowEmptyDiscarded?: boolean;
|
||||
}): Promise<FinalizeResult> {
|
||||
const integrationBranch = input.integrationBranch ?? "main";
|
||||
if (ACTIVE_FINALIZE_LOCKS.has(input.sessionId)) {
|
||||
throw new ExperimentFinalizeStateError(`Finalize already in progress for session ${input.sessionId}`);
|
||||
}
|
||||
|
||||
ACTIVE_FINALIZE_LOCKS.add(input.sessionId);
|
||||
this.logger.log(`finalize start: ${input.sessionId}`);
|
||||
|
||||
let originalRef = "";
|
||||
let createdBranches: string[] = [];
|
||||
try {
|
||||
const session = this.options.store.getSession(input.sessionId);
|
||||
if (!session) throw new ExperimentFinalizeStateError(`Experiment session not found: ${input.sessionId}`);
|
||||
if (session.status !== "active") {
|
||||
throw new ExperimentFinalizeStateError(`Session ${input.sessionId} is not active (status: ${session.status})`);
|
||||
}
|
||||
if (!session.keptRunIds.length && !input.allowEmptyDiscarded) {
|
||||
throw new ExperimentFinalizeNoKeptRunsError(`Session ${input.sessionId} has no kept runs`);
|
||||
}
|
||||
|
||||
this.options.store.updateSession(input.sessionId, { status: "finalizing" });
|
||||
|
||||
const branch = await this.options.git.currentBranch();
|
||||
originalRef = branch ?? await this.options.git.head();
|
||||
|
||||
const records = this.options.store.listRecords(input.sessionId);
|
||||
const baselineRef = this.resolveBaselineRef(session.baselineRunId, session.metadata?.baselineCommit, records, integrationBranch);
|
||||
const mergeBaseCommit = await this.options.git.mergeBase(baselineRef, integrationBranch);
|
||||
const defaultPlan = buildDefaultPlan({ session, records, integrationBranch, mergeBaseCommit });
|
||||
const plan = mergePlanWithUserOverrides(defaultPlan, input.planOverride);
|
||||
|
||||
const branchResults: FinalizeResult["branches"] = [];
|
||||
createdBranches = [];
|
||||
|
||||
for (const group of plan.groups) {
|
||||
if (await this.options.git.branchExists(group.suggestedBranchName)) {
|
||||
throw new ExperimentFinalizeBranchExistsError(`Branch already exists: ${group.suggestedBranchName}`);
|
||||
}
|
||||
|
||||
await this.options.git.createBranch(group.suggestedBranchName, plan.mergeBaseCommit);
|
||||
createdBranches.push(group.suggestedBranchName);
|
||||
this.logger.log(`created branch ${group.suggestedBranchName}`);
|
||||
|
||||
await this.options.git.checkout(group.suggestedBranchName);
|
||||
for (const commit of group.commits) {
|
||||
await this.options.git.cherryPick(commit);
|
||||
}
|
||||
const tipCommit = await this.options.git.head();
|
||||
branchResults.push({
|
||||
name: group.suggestedBranchName,
|
||||
baseCommit: plan.mergeBaseCommit,
|
||||
tipCommit,
|
||||
runRecordIds: group.runRecordIds,
|
||||
commits: group.commits,
|
||||
});
|
||||
}
|
||||
|
||||
await this.options.git.checkout(originalRef);
|
||||
const discardedRunIds = this.options.store
|
||||
.listRecords(input.sessionId)
|
||||
.filter((record) => record.type === "run" && record.payload.status !== "keep")
|
||||
.map((record) => record.id);
|
||||
|
||||
const finalizeRecord = this.options.store.appendRecord(input.sessionId, {
|
||||
type: "finalize",
|
||||
payload: {
|
||||
keptRunIds: session.keptRunIds,
|
||||
discardedRunIds,
|
||||
branches: branchResults.map((branchResult) => ({
|
||||
name: branchResult.name,
|
||||
baseCommit: branchResult.baseCommit,
|
||||
tipCommit: branchResult.tipCommit,
|
||||
})),
|
||||
summary: input.summary,
|
||||
},
|
||||
});
|
||||
|
||||
this.options.store.updateSession(input.sessionId, { status: "finalized" });
|
||||
this.logger.log(`finalize complete: ${input.sessionId}`);
|
||||
return {
|
||||
sessionId: input.sessionId,
|
||||
mergeBaseCommit: plan.mergeBaseCommit,
|
||||
branches: branchResults,
|
||||
warnings: plan.warnings,
|
||||
finalizeRecordId: finalizeRecord.id,
|
||||
};
|
||||
} catch (error) {
|
||||
if (originalRef) {
|
||||
try {
|
||||
await this.options.git.checkout(originalRef);
|
||||
} catch (checkoutError) {
|
||||
this.logger.error(`rollback checkout failed: ${formatError(checkoutError)}`);
|
||||
}
|
||||
}
|
||||
for (const branchName of createdBranches) {
|
||||
try {
|
||||
await this.options.git.deleteBranch(branchName, { force: true });
|
||||
} catch (deleteError) {
|
||||
this.logger.error(`rollback delete failed for ${branchName}: ${formatError(deleteError)}`);
|
||||
}
|
||||
}
|
||||
this.logger.error(`finalize failed: ${formatError(error)}`);
|
||||
throw error;
|
||||
} finally {
|
||||
ACTIVE_FINALIZE_LOCKS.delete(input.sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private resolveBaselineRef(
|
||||
baselineRunId: string | undefined,
|
||||
metadataBaselineCommit: unknown,
|
||||
records: ExperimentSessionRecord[],
|
||||
integrationBranch: string,
|
||||
): string {
|
||||
if (typeof metadataBaselineCommit === "string" && metadataBaselineCommit.trim()) {
|
||||
return metadataBaselineCommit.trim();
|
||||
}
|
||||
|
||||
if (baselineRunId) {
|
||||
const baselineRun = records.find((record) => record.id === baselineRunId && record.type === "run");
|
||||
if (baselineRun?.type === "run" && baselineRun.payload.commit) {
|
||||
return baselineRun.payload.commit;
|
||||
}
|
||||
}
|
||||
|
||||
return integrationBranch;
|
||||
}
|
||||
}
|
||||
@@ -127,6 +127,23 @@ export {
|
||||
type RunExperimentResult,
|
||||
type LogExperimentInput,
|
||||
} from "./experiment-executor.js";
|
||||
export {
|
||||
ExperimentFinalizeService,
|
||||
__activeFinalizeLocksForTesting,
|
||||
} from "./experiment/finalize-service.js";
|
||||
export {
|
||||
ExperimentFinalizeStateError,
|
||||
ExperimentFinalizeNoKeptRunsError,
|
||||
ExperimentFinalizePlanError,
|
||||
ExperimentFinalizeMergeBaseError,
|
||||
ExperimentFinalizeCherryPickConflictError,
|
||||
ExperimentFinalizeBranchExistsError,
|
||||
type FinalizeGroup,
|
||||
type FinalizePlan,
|
||||
type FinalizeResult,
|
||||
type FinalizePlanOverride,
|
||||
type FinalizePlanOverrideGroup,
|
||||
} from "./experiment/finalize-types.js";
|
||||
export {
|
||||
ResearchStepRunner,
|
||||
ResearchStepTimeoutError,
|
||||
|
||||
Reference in New Issue
Block a user