35 lines · 1.6 KB
import { WorkflowEntrypoint } from "cloudflare:workers";
import type { WorkflowEvent, WorkflowStep } from "cloudflare:workers";
import { applyRun, failRun, planRun } from "./service";
import { checkRequeue } from "./requeue";
import { MAX_CI_POLLS, MAX_ENQUEUE_ATTEMPTS } from "./requeue-policy";

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;
      const applied = await step.do("apply", { retries: { limit: 0, delay: "1 second" }, timeout: "5 minutes" }, () =>
        applyRun(runId),
      );
      if (!applied.autoApply) return;
      for (let poll = 1; poll <= MAX_CI_POLLS + MAX_ENQUEUE_ATTEMPTS; poll++) {
        await step.sleep(`requeue-wait-${poll}`, "2 minutes");
        const next = await step.do(`requeue-${poll}`, { retries: { limit: 2, delay: "30 seconds" } }, () =>
          checkRequeue(runId, poll),
        );
        if (next === "done") return;
      }
      await step.do("requeue-timeout", () => checkRequeue(runId, Number.MAX_SAFE_INTEGER));
    } catch (error) {
      await step.do("fail", () => failRun(runId, error instanceof Error ? error.message : "The fix failed"));
    }
  }
}