import { advertisement, artifactsSource, relayPack } from "./relay";
import { isHiddenRef, pullBaseRef, pullHeadRef } from "./hidden-refs";
import { canBridge, canBridgeBase, pullHeadAccessible } from "./bridge-policy";
import { branchSha, listRefs } from "./refs";
import { pushToArtifacts } from "./writer";
import { withStore, ZERO_SHA } from "./store";
import { forgetReachability } from "./reachable";
import { enforceRateLimit, CAPS } from "../limits";
import { run } from "../db";
import { repoDb } from "../scoped-db";
import { pullBaseRepoIds } from "../pull-scope";
import { loadRepoById } from "../repos";
import { dropPullAliasesForIssue } from "../sites/pr-aliases";
import {
bridgeQuotaAvailable,
recordBridgedBytes,
releaseBridgedBytes,
releaseBridgedBytesForRepo,
} from "./bridge-quota";
import type { BridgeCharge } from "./bridge-quota";
import type { RepoRow } from "~/lib/types";
const MAX_BRIDGE_BYTES = 64 * 1024 * 1024;
const DROP_ATTEMPTS = 3;
async function hasCommit(repo: RepoRow, sha: string): Promise<boolean> {
return withStore(repo.artifacts_name, async (store) => (await store.commit(sha)) !== null);
}
/**
* Ensures `sha` and its history exist in `to`, pinned by the hidden `ref` so Artifacts keeps them.
* Objects are relayed Artifacts-to-Artifacts, never from user-supplied URLs; the hidden ref is filtered
* out of push processing, so no CI, webhooks or indexing run for it. Returns false when refused.
*/
/**
* Re-reads both repos right before writing, so a bridge that started before a visibility change cannot
* pin objects under the old visibility after the change's cleanup ran.
*/
async function stillAllowed(from: RepoRow, to: RepoRow, extra: (from: RepoRow) => boolean): Promise<boolean> {
const [freshFrom, freshTo] = await Promise.all([loadRepoById(from.id), loadRepoById(to.id)]);