fix(external-objects): bound refresh lifecycle and activation scope

Reuse the provider-owned request cancellation contract from merge-confirmation work. Keep host signals out of plugin RPC payloads, preserve refresh debt on shutdown, and target identities or known objects for source writes.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-30 17:18:38 -05:00
1 parent 30730dd619
commit b45b6a225d
7 files changed
+179 -35

No files matched your search

@@ -15,6 +15,7 @@ type FetchLike = (url: string, init?: RequestInit) => Promise<Response>;
export interface GitHubExternalObjectProviderOptions {
fetch?: FetchLike;
requestTimeoutMs?: number;
tokenProvider?: (companyId: string) => Promise<string | null> | string | null;
/**
* Resolve the credential and its source. Prefer this over `tokenProvider` when the
@@ -243,12 +244,12 @@ type RepoProbeOutcome = {
async function probeRepoVisibility(
identity: GitHubObjectIdentity,
opts: { fetch: FetchLike; headers?: Record<string, string> | undefined },
opts: { fetch: FetchLike; headers?: Record<string, string> | undefined; signal?: AbortSignal },
): Promise<RepoProbeOutcome> {
try {
const probe = await opts.fetch(
`${gitHubApiBase(identity.host)}/repos/${encodeURIComponent(identity.owner)}/${encodeURIComponent(identity.repo)}`,
{ headers: opts.headers },
{ headers: opts.headers, signal: opts.signal },
);
if (probe.ok) {
const body = await safeJson(probe);
@@ -427,12 +428,13 @@ export function createGitHubExternalObjectProvider(
companyId: string,
identity: GitHubObjectIdentity,
headers: Record<string, string>,
signal: AbortSignal,
): Promise<RepoProbeOutcome> {
const credentialFingerprint = createHash("sha256").update(headers.authorization ?? "anonymous").digest("hex");
const key = `${companyId}\u0000${credentialFingerprint}\u0000${identity.owner.toLowerCase()}\u0000${identity.repo.toLowerCase()}`;
const cached = probeCache.get(key);
if (cached && cached.expiresAt > Date.now()) return cached.outcome;
const outcome = await probeRepoVisibility(identity, { fetch: fetchImpl, headers });
const outcome = await probeRepoVisibility(identity, { fetch: fetchImpl, headers, signal });
if (outcome.visibility === "visible" || outcome.visibility === "hidden") {
probeCache.set(key, { outcome, expiresAt: Date.now() + GITHUB_OBJECT_TTL_SECONDS * 1000 });
}
@@ -464,7 +466,9 @@ export function createGitHubExternalObjectProvider(
return {
providerKey: "github",
objectType,
async resolve({ companyId, object }) {
async resolve({ companyId, object, signal: ownerSignal }) {
const deadline = AbortSignal.timeout(opts.requestTimeoutMs ?? 10_000);
const signal = ownerSignal ? AbortSignal.any([ownerSignal, deadline]) : deadline;
const identity = parseGitHubObject(object);
if (!identity || identity.objectType !== objectType) {
return {
@@ -509,7 +513,7 @@ export function createGitHubExternalObjectProvider(
// "public" verdict must not authorize a later read of its private contents.
// The cache is still used below to classify a 404, where a stale verdict can
// only make the object look deleted — it can never expose private data.
const probe = await probeRepoVisibility(identity, { fetch: fetchImpl, headers });
const probe = await probeRepoVisibility(identity, { fetch: fetchImpl, headers, signal });
if (probe.visibility === "hidden") {
return {
ok: false,
@@ -554,7 +558,7 @@ export function createGitHubExternalObjectProvider(
let response: Response;
try {
response = await fetchImpl(url, { headers });
response = await fetchImpl(url, { headers, signal });
} catch {
return {
ok: false,
@@ -573,7 +577,7 @@ export function createGitHubExternalObjectProvider(
// Probe the repository metadata to classify: if the repo itself is visible
// the object truly is gone; if it is not, surface credentials-needed
// instead of permanently freezing a reference as "Not found".
const probe = await cachedProbeRepoVisibility(companyId, identity, headers);
const probe = await cachedProbeRepoVisibility(companyId, identity, headers, signal);
if (probe.visibility === "visible") {
return { ok: true, snapshot: notFoundSnapshot(identity, etag) };
}