291 lines · 12.1 KB
import { env, WorkflowEntrypoint } from "cloudflare:workers";
import { kickRepoInsights } from "./insights/repo-insights-do";
import type { WorkflowEvent, WorkflowStep } from "cloudflare:workers";
import { z } from "zod";
import { run } from "./db";
import { isArtifactsError, openArtifactsRepo, rememberInitialToken } from "./git/artifacts";
import { artifactsCall, ArtifactsUnavailableError } from "./git/artifacts-call";
import { artifactsBinding } from "./git/artifacts-shards";
import { BACKFILL_START, loadRepoByArtifactsName, loadRepoById } from "./repos";
import { refreshRepoStatus } from "./repo-ops";
import { sourceAuthorization, syncRefsFromSource } from "./git/ref-sync";
import { invalidateRefs } from "./git/refs";
import { dropHiddenRefsForRepo, requestHiddenRefCleanup } from "./git/pull-bridge";
import { decryptRepoSecret } from "./secrets";
import { requestCodeIndex } from "./code/search-index";
import { warmLastCommits } from "./code/path-history";
import { notifyProvisioningFailed } from "./notify/import";

const provisionParams = z.discriminatedUnion("kind", [
  z.object({
    repoId: z.string(),
    kind: z.literal("import"),
    sourceUrl: z.string(),
    branch: z.string().nullable(),
    /** Encrypted source credentials for private imports; the Worker relays the pack itself. */
    auth: z.object({ ciphertext: z.string(), iv: z.string() }).nullable().optional(),
  }),
  z.object({ repoId: z.string(), kind: z.literal("fork"), sourceRepoId: z.string() }),
]);

export type ProvisionParams = z.infer<typeof provisionParams>;

/** Small forks finish within seconds; after that the forked event wakes the loop and polling is a fallback. */
const FAST_POLLS = 2;
const MAX_POLLS = 32;
const FAST_INTERVAL = "5 seconds";
const SLOW_INTERVAL = "1 minute";
const BEGIN_TIMEOUT = "15 minutes";
const RELAY_STEP = { retries: { limit: 2, delay: "30 seconds" }, timeout: "15 minutes" } as const;
const RELAY_EVENT_SETTLE = "15 seconds";