22 lines · 1.0 KB
import { WorkflowEntrypoint } from "cloudflare:workers";
import type { WorkflowEvent, WorkflowStep } from "cloudflare:workers";
import { applyRun, failRun, planRun } from "./service";

export type MergeFixParams = { runId: string };

/** Plan (model calls) and apply are separate steps without retries: a retry could re-bill the models or push twice. */
export class MergeFixWorkflow extends WorkflowEntrypoint<Env, MergeFixParams> {
  override async run(event: Readonly<WorkflowEvent<MergeFixParams>>, step: WorkflowStep): Promise<void> {
    const { runId } = event.payload;
    try {
      const plan = await step.do("plan", { retries: { limit: 0, delay: "1 second" }, timeout: "10 minutes" }, () =>
        planRun(runId),
      );
      if (!plan.autoApply) return;
      await step.do("apply", { retries: { limit: 0, delay: "1 second" }, timeout: "5 minutes" }, () => applyRun(runId));
    } catch (error) {
      await step.do("fail", () => failRun(runId, error instanceof Error ? error.message : "The fix failed"));
    }
  }
}