diff --git a/src/main/github/accounts/github-project-account.ts b/src/main/github/accounts/github-project-account.ts index c091d564bac..aa96fd8636c 100644 --- a/src/main/github/accounts/github-project-account.ts +++ b/src/main/github/accounts/github-project-account.ts @@ -87,3 +87,16 @@ export function resolveGitHubProjectAccount(context: GitHubRepoContext) { ? { ghAccount: normalizeGhAccountBinding(matches[0].ghAccount) ?? undefined } : null } + +/** Policy observes project ownership changes without resolving every workspace's account. */ +export function githubPRPolicyProjects(): { source: object | null; selection: string } { + return { + source, + selection: JSON.stringify( + source?.store + .getRepos() + .map((repo) => [repo.id, repo.path, getStoredRepoExecutionHostId(repo), repo.ghAccount]) ?? + [] + ) + } +} diff --git a/src/main/github/client/lookup/pr-branch-read-resources.ts b/src/main/github/client/lookup/pr-branch-read-resources.ts new file mode 100644 index 00000000000..46d94d85763 --- /dev/null +++ b/src/main/github/client/lookup/pr-branch-read-resources.ts @@ -0,0 +1,94 @@ +import type { CoalescedProbes } from '../../../git/remotes/coalesced-probe' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import type { readTimedPROutcome } from './pr-last-outcome' + +export type PRBranchReadResult = { + outcome: PRRefreshOutcome + sequence: number + current: () => boolean +} + +type PRBranchReadResource = { + probes: CoalescedProbes + running: number + latestStart: number +} + +const resources = new Map() + +export function prBranchReadResource(key: string): PRBranchReadResource { + let resource = resources.get(key) + if (!resource) { + resource = { probes: new Map(), running: 0, latestStart: 0 } + resources.set(key, resource) + } + return resource +} + +export function pendingPRBranchRead( + key: string, + intent: 'fresh' | 'timed' +): Promise | null { + const pending = resources.get(key)?.probes.get(intent) + return pending && Date.now() - pending.startedAt < 2 * 60_000 ? pending.promise : null +} + +export function retirePRBranchReadResource(key: string, resource: PRBranchReadResource): void { + if (resource.probes.size === 0 && resource.running === 0 && resources.get(key) === resource) { + resources.delete(key) + } +} + +export function startPRBranchRead( + key: string, + resource: PRBranchReadResource, + sequence: number +): { current: () => boolean; finish: () => void } { + resource.running += 1 + resource.latestStart = sequence + return { + current: () => + Math.max(resource.latestStart, resources.get(key)?.latestStart ?? 0) === sequence, + finish: () => { + resource.running -= 1 + retirePRBranchReadResource(key, resource) + } + } +} + +export function deliverablePRBranchOutcome( + prepared: Pick, + result: PRBranchReadResult +): PRRefreshOutcome | null { + if (result.current()) { + return result.outcome + } + const cached = prepared.isCurrent() ? prepared.cached() : null + return cached && cached.sequence > result.sequence ? cached.outcome : null +} + +export type PRReadSettlement = ( + outcome: PRRefreshOutcome, + producer: { retained: boolean } +) => PRRefreshOutcome + +export type PreparedPRBranchRead = { + key: string + reprepare: () => Promise + cached: () => ReturnType + isCurrent: () => boolean + accepts: (outcome: PRRefreshOutcome) => boolean + didRead: () => boolean + release: () => void + hold: () => () => void + readRetained: (settle?: PRReadSettlement) => Promise + canRetain: () => boolean + scheduleError: (expected: PRRefreshOutcome, outcome: PRRefreshOutcome) => void + readResult: ( + settle?: PRReadSettlement, + requireRetention?: boolean + ) => Promise + pending: () => Promise | null + withExecution: (operation: () => Promise) => Promise + read: (settle?: PRReadSettlement) => Promise +} diff --git a/src/main/github/client/lookup/pr-for-branch-outcome.ts b/src/main/github/client/lookup/pr-for-branch-outcome.ts index a1d4bc779a5..e4952ecfe99 100644 --- a/src/main/github/client/lookup/pr-for-branch-outcome.ts +++ b/src/main/github/client/lookup/pr-for-branch-outcome.ts @@ -1,56 +1,44 @@ import { withGitHubRepoExecution, - ghRepoExecOptions, - githubRepoContext + captureGitHubRepoExecution, + outsideGitHubRepoExecution } from '../../repositories/github-repository-identity' -import { getRepoExecutionHostId } from '../../../../shared/execution-host' +import { runCoalescedProbe } from '../../../git/remotes/coalesced-probe' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { acquire, release } from '../../execution/github-operation-slots' + +import { hostedReviewLocalGitOptionArgs } from './../github-exec-scope' +import type { GitHubPRBranchLookupOptions } from './pull-request-lookup-data' +import { prRefreshUpstreamError } from './../gh-error-predicates' +import { resolvePRForBranchOutcome } from './branch-lookup-resolution' +import { + readRecentOpenPROutcome, + readTimedPROutcome, + canRetainPROutcome, + beginExactPRLookup, + releaseExactPRLookup, + holdExactPROutcome, + scheduleExactPRError, + recordPRLastOutcome, + startPRLookup +} from './pr-last-outcome' +import { prLookupScope, lastOutcomeKey, completeReadKey } from './pr-read-identity' +import { refreshPRPolicyContext, recordPRPolicyOutcome } from './pr-quiet-policy-evidence' import { hostedReviewRepoScope, scopeGeneration } from '../../../source-control/hosted-review-scope-generations' -import { runCoalescedProbe, type CoalescedProbes } from '../../../git/remotes/coalesced-probe' -import { codeHostReadExecutionScope as githubReadExecutionScope } from '../../../source-control/code-host-read-execution-scope' -import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' -import { acquire, release } from '../../execution/github-operation-slots' - -import { hostedReviewLocalGitOptionArgs, githubPRStackExecutionScope } from './../github-exec-scope' -import type { GitHubPRBranchLookupOptions } from './pull-request-lookup-data' -import { prRefreshUpstreamError } from './../gh-error-predicates' -import { resolvePRForBranchOutcome } from './branch-lookup-resolution' -import { readRecentOpenPROutcome, recordPRLastOutcome, startPRLookup } from './pr-last-outcome' -const reads: CoalescedProbes = new Map() - -function prLookupScope( - repoPath: string, - connectionId: string | null | undefined, - options: GitHubPRBranchLookupOptions -) { - const localGitOptions = hostedReviewLocalGitOptionArgs(options)[0] ?? {} - const ghOptions = ghRepoExecOptions(githubRepoContext(repoPath, connectionId, localGitOptions)) - return { - localGitOptions, - ghOptions, - executionScope: githubPRStackExecutionScope(connectionId, localGitOptions), - generation: scopeGeneration( - hostedReviewRepoScope(repoPath, getRepoExecutionHostId({ connectionId })) - ), - readScope: githubReadExecutionScope(ghOptions) - } -} - -function lastOutcomeKey( - repoPath: string, - branchName: string, - scope: ReturnType -): string { - return JSON.stringify([ - scope.executionScope, - repoPath, - scope.generation, - branchName, - scope.readScope - ]) -} +import { getRepoExecutionHostId } from '../../../../shared/execution-host' +import { + prBranchReadResource, + pendingPRBranchRead, + retirePRBranchReadResource, + startPRBranchRead, + type PRBranchReadResult, + type PRReadSettlement, + type PreparedPRBranchRead +} from './pr-branch-read-resources' +class PRLookupRetentionRefused extends Error {} /** A phone read may take the last lookup's open PR; it was already observed when it was looked up. */ export async function readRecentPhonePROutcome( @@ -84,19 +72,39 @@ export async function readRecentPhonePROutcome( ) } -export async function getPRForBranchOutcome( +export async function preparePRBranchRead( repoPath: string, branch: string, linkedPRNumber?: number | null, connectionId?: string | null, fallbackPRNumber?: number | null, options: GitHubPRBranchLookupOptions = {} -): Promise { +): Promise { + options = { + ...options, + ...(options.localGitExecOptions + ? { + localGitExecOptions: { + ...options.localGitExecOptions, + ...(options.localGitExecOptions.ghAccount + ? { ghAccount: { ...options.localGitExecOptions.ghAccount } } + : {}) + } + } + : {}), + ...(options.sharedLinkPaths ? { sharedLinkPaths: [...options.sharedLinkPaths] } : {}), + ...(options.exactRepository ? { exactRepository: { ...options.exactRepository } } : {}) + } + const reprepare = () => + outsideGitHubRepoExecution(() => + preparePRBranchRead(repoPath, branch, linkedPRNumber, connectionId, fallbackPRNumber, options) + ) return withGitHubRepoExecution( repoPath, connectionId, hostedReviewLocalGitOptionArgs(options)[0] ?? {}, - async (): Promise => { + async (): Promise => { + const withExecution = captureGitHubRepoExecution() const branchName = branch.replace(/^refs\/heads\//, '') // Detached HEAD cannot discover by branch, but explicit PR numbers remain safe. if ( @@ -104,63 +112,193 @@ export async function getPRForBranchOutcome( typeof linkedPRNumber !== 'number' && typeof fallbackPRNumber !== 'number' ) { - return { kind: 'no-pr', fetchedAt: Date.now() } + return { + key: '', + reprepare, + cached: () => null, + isCurrent: () => true, + accepts: () => true, + didRead: () => false, + release: () => {}, + hold: () => () => {}, + readRetained: async () => ({ kind: 'no-pr', fetchedAt: Date.now() }), + readResult: async () => ({ + outcome: { kind: 'no-pr', fetchedAt: Date.now() }, + sequence: 0, + current: () => true + }), + canRetain: () => true, + scheduleError: () => {}, + pending: () => null, + withExecution, + read: async () => ({ kind: 'no-pr', fetchedAt: Date.now() }) + } } + const policyRevision = refreshPRPolicyContext() const scope = prLookupScope(repoPath, connectionId, options) const { localGitOptions, ghOptions, executionScope } = scope - const key = JSON.stringify([ - executionScope, + const key = completeReadKey( repoPath, - scope.generation, branchName, - linkedPRNumber ?? null, - fallbackPRNumber ?? null, - options.acceptMergedFallbackPR ?? false, - options.currentHeadOid ?? null, - options.checkPageLimit ?? null, - options.exactRepository ?? null, - options.includeDerivedData !== false, - scope.readScope - ]) - return runCoalescedProbe( - reads, - key, - async () => { - await acquire() - const start = startPRLookup() - let outcome: PRRefreshOutcome - try { - outcome = await resolvePRForBranchOutcome({ - repoPath, - branchName, - linkedPRNumber, - connectionId, - fallbackPRNumber, - options, - localGitOptions, - ghOptions, - executionScope - }) - } catch (err) { - outcome = prRefreshUpstreamError(err) - } finally { - release() - } - const narrowed = - options.checkPageLimit !== undefined || - options.exactRepository || - options.includeDerivedData === false - recordPRLastOutcome(lastOutcomeKey(repoPath, branchName, scope), { - start, - linkedPRNumber: linkedPRNumber ?? null, - fallbackPRNumber: fallbackPRNumber ?? null, - outcome: narrowed ? null : outcome - }) - return outcome - }, - 2 * 60_000, - options + linkedPRNumber, + fallbackPRNumber, + options, + scope ) + const ownerKey = lastOutcomeKey(repoPath, branchName, scope) + const coalescingKey = options.resetQuietBackoff || options.userRefresh ? 'fresh' : 'timed' + const pendingAt = (intent: 'fresh' | 'timed') => pendingPRBranchRead(key, intent) + const narrowed = + options.checkPageLimit !== undefined || + options.exactRepository || + options.includeDerivedData === false + let didRead = false + const readCore = (requireRetention = false, settle?: PRReadSettlement) => + withExecution(() => { + const resource = prBranchReadResource(key) + return ( + (!options.userRefresh && coalescingKey === 'timed' ? pendingAt('fresh') : null) ?? + runCoalescedProbe( + resource.probes, + coalescingKey, + async (ownsKey) => { + const start = startPRLookup() + const retained = + !narrowed && + beginExactPRLookup( + ownerKey, + key, + start, + options.resetQuietBackoff || options.userRefresh + ) + if (requireRetention && !retained) { + throw new PRLookupRetentionRefused() + } + const producer = startPRBranchRead(key, resource, start.sequence) + didRead = true + try { + await acquire() + let outcome: PRRefreshOutcome + try { + outcome = await resolvePRForBranchOutcome({ + repoPath, + branchName, + linkedPRNumber, + connectionId, + fallbackPRNumber, + options, + localGitOptions, + ghOptions, + executionScope + }) + } catch (err) { + outcome = prRefreshUpstreamError(err) + } finally { + release() + } + recordPRLastOutcome(ownerKey, { + start, + requestKey: key, + currentHeadOid: options.currentHeadOid ?? null, + resetQuiet: options.userRefresh === true || options.resetQuietBackoff === true, + linkedPRNumber: linkedPRNumber ?? null, + fallbackPRNumber: fallbackPRNumber ?? null, + outcome: narrowed ? null : outcome + }) + if (!narrowed) { + recordPRPolicyOutcome(outcome, { + revision: policyRevision, + current: () => + scopeGeneration( + hostedReviewRepoScope(repoPath, getRepoExecutionHostId({ connectionId })) + ) === scope.generation, + cached: () => readTimedPROutcome(ownerKey, key) + }) + } + if ( + settle && + prepared.isCurrent() && + producer.current() && + (retained ? readTimedPROutcome(ownerKey, key)?.outcome === outcome : ownsKey()) + ) { + outcome = settle(outcome, { retained }) + } + return { + outcome, + sequence: start.sequence, + current: () => + producer.current() && + prepared.isCurrent() && + (!retained || prepared.accepts(outcome)) + } + } finally { + if (retained) { + releaseExactPRLookup(key, start.sequence) + } + producer.finish() + } + }, + 2 * 60_000, + { userRefresh: options.userRefresh === true } + ).finally(() => retirePRBranchReadResource(key, resource)) + ) + }) + const readResult = async ( + settle?: PRReadSettlement, + requireRetention = false + ): Promise => { + try { + return await readCore(requireRetention, settle) + } catch (error) { + if (!(error instanceof PRLookupRetentionRefused)) { + throw error + } + return requireRetention ? null : readCore(false, settle) + } + } + const read = async (settle?: PRReadSettlement): Promise => + (await readResult(settle))!.outcome + const readRetained = async (settle?: PRReadSettlement): Promise => + (await readResult(settle, true))?.outcome ?? null + const prepared: PreparedPRBranchRead = { + key, + reprepare, + cached: () => readTimedPROutcome(ownerKey, key), + isCurrent: () => + outsideGitHubRepoExecution( + () => + lastOutcomeKey( + repoPath, + branchName, + prLookupScope(repoPath, connectionId, options) + ) === ownerKey + ), + accepts: (outcome) => readTimedPROutcome(ownerKey, key)?.outcome === outcome, + didRead: () => didRead, + release: () => {}, + hold: () => holdExactPROutcome(key), + readRetained, + readResult, + canRetain: () => canRetainPROutcome(key), + scheduleError: (expected, outcome) => scheduleExactPRError(key, expected, outcome), + pending: () => pendingAt('fresh') ?? pendingAt('timed'), + withExecution, + read + } + return prepared } ) } + +export type { PRReadSettlement, PreparedPRBranchRead } from './pr-branch-read-resources' + +export async function getPRForBranchOutcome( + ...args: Parameters +): Promise { + const prepared = await preparePRBranchRead(...args) + try { + return await prepared.read() + } finally { + prepared.release() + } +} diff --git a/src/main/github/client/lookup/pr-last-outcome.ts b/src/main/github/client/lookup/pr-last-outcome.ts index 7d6dec32aac..cc2a6032eb4 100644 --- a/src/main/github/client/lookup/pr-last-outcome.ts +++ b/src/main/github/client/lookup/pr-last-outcome.ts @@ -1,4 +1,8 @@ import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { + isQuietPR, + QUIET_PR_REFRESH_INTERVAL_MS +} from '../../../../shared/github/pull-requests/refresh/pr-quiet-refresh' // A phone's re-read right after the desktop's own lookup reuses it instead of asking GitHub again. const PHONE_REUSE_MAX_AGE_MS = 30_000 @@ -10,33 +14,228 @@ type PRLastOutcome = { start: PRLookupStart linkedPRNumber: number | null fallbackPRNumber: number | null + requestKey?: string + currentHeadOid?: string | null + resetQuiet?: boolean + quiet?: boolean // null: the last lookup was narrowed (CI-line watch), so nothing is reusable until a full one. outcome: PRRefreshOutcome | null } +type ExactOutcome = PRLastOutcome & { + latestSequence: number + activeSequence: number | null + holds: number + invalidatedBeforeSequence: number + ownerKey: string +} const lastOutcomes = new Map() +const exactOutcomes = new Map() let lookupSequence = 0 -// Two lookups can start in the same millisecond; the sequence still orders them. export function startPRLookup(): PRLookupStart { lookupSequence += 1 return { startedAt: Date.now(), sequence: lookupSequence } } -export function recordPRLastOutcome(key: string, entry: PRLastOutcome): void { - const existing = lastOutcomes.get(key) - // A lookup that started earlier but settled later must not replace a newer answer. - if (existing && existing.start.sequence > entry.start.sequence) { +function trim(entries: Map): void { + while (entries.size > MAX_ENTRIES) { + entries.delete(entries.keys().next().value!) + } +} + +function pinned(entry: ExactOutcome): boolean { + return ( + entry.activeSequence !== null || + entry.holds > 0 || + (entry.outcome?.kind === 'upstream-error' && + Math.max(entry.outcome.nextAutoRetryAt ?? 0, entry.outcome.retryDisabledUntil ?? 0) > + Date.now()) + ) +} + +export function canRetainPROutcome(requestKey: string): boolean { + return ( + exactOutcomes.has(requestKey) || + exactOutcomes.size < MAX_ENTRIES || + [...exactOutcomes.values()].some((entry) => !pinned(entry)) + ) +} + +function evictExactOutcome(requestKey: string): void { + const entry = exactOutcomes.get(requestKey) + if (!entry) { return } - lastOutcomes.delete(key) - lastOutcomes.set(key, entry) - const now = Date.now() - for (const [oldestKey, oldest] of lastOutcomes) { - if (lastOutcomes.size <= MAX_ENTRIES && now - oldest.start.startedAt < PHONE_REUSE_MAX_AGE_MS) { - break + const projection = lastOutcomes.get(entry.ownerKey) + if (projection?.requestKey === requestKey) { + projection.outcome = null + } + exactOutcomes.delete(requestKey) +} + +function makeRoom(requestKey: string): boolean { + if (exactOutcomes.has(requestKey) || exactOutcomes.size < MAX_ENTRIES) { + return true + } + const victim = [...exactOutcomes].find(([, value]) => !pinned(value)) + if (!victim) { + return false + } + evictExactOutcome(victim[0]) + return true +} + +export function beginExactPRLookup( + ownerKey: string, + requestKey: string, + start: PRLookupStart, + resetQuiet = false +): boolean { + const previous = exactOutcomes.get(requestKey) + if (previous) { + previous.latestSequence = start.sequence + previous.activeSequence = start.sequence + if (resetQuiet) { + previous.quiet = false } - lastOutcomes.delete(oldestKey) + } else if (makeRoom(requestKey)) { + exactOutcomes.set(requestKey, { + ownerKey, + start, + latestSequence: start.sequence, + activeSequence: start.sequence, + holds: 0, + invalidatedBeforeSequence: 0, + linkedPRNumber: null, + fallbackPRNumber: null, + outcome: null + }) + } else { + return false + } + return true +} + +export function holdExactPROutcome(requestKey: string): () => void { + const entry = exactOutcomes.get(requestKey) + if (!entry) { + return () => {} + } + entry.holds += 1 + let held = true + return () => { + if (held) { + held = false + const current = exactOutcomes.get(requestKey) + if (current) { + current.holds -= 1 + } + } + } +} + +export function releaseExactPRLookup(requestKey: string, sequence: number): void { + const entry = exactOutcomes.get(requestKey) + if (entry?.activeSequence === sequence) { + entry.activeSequence = null + } +} + +function invalidateOwnerSuccesses(ownerKey: string, sequence: number): void { + for (const entry of exactOutcomes.values()) { + if (entry.ownerKey !== ownerKey) { + continue + } + entry.invalidatedBeforeSequence = Math.max(entry.invalidatedBeforeSequence, sequence) + if (entry.outcome?.kind !== 'upstream-error' && entry.start.sequence < sequence) { + entry.outcome = null + entry.quiet = false + } + } +} + +export function recordPRLastOutcome(key: string, entry: PRLastOutcome): void { + const previous = entry.requestKey ? exactOutcomes.get(entry.requestKey) : undefined + if (entry.outcome) { + if ( + !previous || + previous.ownerKey !== key || + previous.latestSequence !== entry.start.sequence || + (entry.outcome.kind !== 'upstream-error' && + entry.start.sequence < previous.invalidatedBeforeSequence) + ) { + return + } + } + if (!entry.outcome || entry.outcome.kind === 'upstream-error') { + invalidateOwnerSuccesses(key, entry.start.sequence) + } + let retained = false + if (entry.requestKey && entry.outcome && previous) { + const quiet = + !entry.resetQuiet && + previous.outcome?.kind === 'found' && + entry.outcome.kind === 'found' && + isQuietPR(entry.outcome.pr, entry.currentHeadOid ?? null) && + JSON.stringify(previous.outcome.pr) === JSON.stringify(entry.outcome.pr) + exactOutcomes.delete(entry.requestKey) + exactOutcomes.set(entry.requestKey, { + ...entry, + ownerKey: key, + quiet, + activeSequence: previous.activeSequence, + holds: previous.holds, + invalidatedBeforeSequence: previous.invalidatedBeforeSequence, + latestSequence: entry.start.sequence + }) + retained = true + } + const existing = lastOutcomes.get(key) + if (!existing || existing.start.sequence <= entry.start.sequence) { + lastOutcomes.delete(key) + lastOutcomes.set(key, retained ? entry : { ...entry, outcome: null }) + trim(lastOutcomes) + } +} + +export function readTimedPROutcome( + key: string, + requestKey: string +): { + outcome: PRRefreshOutcome + quietIntervalMs: number | null + sequence: number +} | null { + const entry = exactOutcomes.get(requestKey) + if (!entry?.outcome || entry.ownerKey !== key) { + return null + } + if ( + entry.outcome.kind !== 'upstream-error' && + entry.start.sequence < entry.invalidatedBeforeSequence + ) { + return null + } + return { + outcome: entry.outcome, + quietIntervalMs: entry.quiet ? QUIET_PR_REFRESH_INTERVAL_MS : null, + sequence: entry.start.sequence + } +} + +export function scheduleExactPRError( + requestKey: string, + expected: PRRefreshOutcome, + scheduled: PRRefreshOutcome +): void { + const entry = exactOutcomes.get(requestKey) + if ( + entry?.outcome === expected && + entry.start.sequence === entry.latestSequence && + scheduled.kind === 'upstream-error' + ) { + entry.outcome = scheduled } } diff --git a/src/main/github/client/lookup/pr-quiet-policy-evidence.ts b/src/main/github/client/lookup/pr-quiet-policy-evidence.ts new file mode 100644 index 00000000000..c4a7d9546ac --- /dev/null +++ b/src/main/github/client/lookup/pr-quiet-policy-evidence.ts @@ -0,0 +1,89 @@ +import type { + GitHubPRRefreshCandidate, + PRRefreshOutcome +} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { githubPRPolicyProjects } from '../../accounts/github-project-account' +import type { readTimedPROutcome } from './pr-last-outcome' + +let revision = 0 +let projects: ReturnType | undefined +let environment: Record = {} +let cwd = '' + +// Called once at a report boundary, never from a per-row policy decision. +export function refreshPRPolicyContext(): number { + const next = githubPRPolicyProjects() + const keys = Object.keys(process.env) + if ( + next.source !== projects?.source || + next.selection !== projects?.selection || + cwd !== process.cwd() || + keys.length !== Object.keys(environment).length || + keys.some((key) => process.env[key] !== environment[key]) + ) { + revision += 1 + projects = next + environment = { ...process.env } + cwd = process.cwd() + } + return revision +} + +type Evidence = { + revision: number + current: () => boolean + cached: () => ReturnType +} +const completed = new WeakMap() +const projections = new Map() + +function key(candidate: GitHubPRRefreshCandidate): string { + return JSON.stringify([ + candidate.repoId, + candidate.repoPath, + candidate.connectionId ?? null, + candidate.localGitOptions?.wslDistro ?? null, + candidate.branch.replace(/^refs\/heads\//, ''), + candidate.currentHeadOid ?? null, + candidate.linkedPRNumber ?? null, + candidate.linkedPRNumber == null ? (candidate.fallbackPRNumber ?? null) : null, + candidate.linkedPRNumber == null ? (candidate.fallbackPRSource ?? null) : null + ]) +} + +export function recordPRPolicyOutcome(outcome: PRRefreshOutcome, evidence: Evidence): void { + completed.set(outcome, evidence) +} + +export function projectPRPolicyOutcome( + candidate: GitHubPRRefreshCandidate, + outcome: PRRefreshOutcome +): void { + const evidence = completed.get(outcome) + const identity = key(candidate) + projections.delete(identity) + if (evidence) { + projections.set(identity, evidence) + } + while (projections.size > 256) { + projections.delete(projections.keys().next().value!) + } +} + +export function quietPRIntervalForCandidate(candidate: GitHubPRRefreshCandidate): number | null { + const evidence = projections.get(key(candidate)) + if (!evidence || evidence.revision !== revision || !evidence.current()) { + return null + } + const cached = evidence.cached() + if ( + cached?.outcome.kind !== 'found' || + cached.outcome.fetchedAt !== candidate.cachedFetchedAt || + candidate.cachedHasPR !== true || + candidate.cachedPRState !== cached.outcome.pr.state || + candidate.cachedChecksStatus !== cached.outcome.pr.checksStatus + ) { + return null + } + return cached.quietIntervalMs +} diff --git a/src/main/github/client/lookup/pr-read-identity.ts b/src/main/github/client/lookup/pr-read-identity.ts new file mode 100644 index 00000000000..ba55c8871b9 --- /dev/null +++ b/src/main/github/client/lookup/pr-read-identity.ts @@ -0,0 +1,83 @@ +import { ghRepoExecOptions, githubRepoContext } from '../../repositories/github-repository-identity' +import { getRepoExecutionHostId } from '../../../../shared/execution-host' +import { + hostedReviewRepoScope, + scopeGeneration +} from '../../../source-control/hosted-review-scope-generations' +import { codeHostReadExecutionScope as githubReadExecutionScope } from '../../../source-control/code-host-read-execution-scope' +import { hostedReviewLocalGitOptionArgs, githubPRStackExecutionScope } from '../github-exec-scope' +import type { GitHubPRBranchLookupOptions } from './pull-request-lookup-data' +import { readTimedPROutcome } from './pr-last-outcome' + +export function prLookupScope( + repoPath: string, + connectionId: string | null | undefined, + options: GitHubPRBranchLookupOptions +) { + const localGitOptions = hostedReviewLocalGitOptionArgs(options)[0] ?? {} + const ghOptions = ghRepoExecOptions(githubRepoContext(repoPath, connectionId, localGitOptions)) + return { + localGitOptions, + ghOptions, + executionScope: githubPRStackExecutionScope(connectionId, localGitOptions), + generation: scopeGeneration( + hostedReviewRepoScope(repoPath, getRepoExecutionHostId({ connectionId })) + ), + readScope: githubReadExecutionScope(ghOptions) + } +} + +export function lastOutcomeKey( + repoPath: string, + branchName: string, + scope: ReturnType +): string { + return JSON.stringify([ + scope.executionScope, + repoPath, + scope.generation, + branchName, + scope.readScope + ]) +} + +export function completeReadKey( + repoPath: string, + branchName: string, + linkedPRNumber: number | null | undefined, + fallbackPRNumber: number | null | undefined, + options: GitHubPRBranchLookupOptions, + scope: ReturnType +): string { + return JSON.stringify([ + scope.executionScope, + repoPath, + scope.generation, + branchName, + linkedPRNumber ?? null, + fallbackPRNumber ?? null, + options.acceptMergedFallbackPR ?? false, + options.currentHeadOid ?? null, + options.checkPageLimit ?? null, + options.exactRepository ?? null, + options.includeDerivedData !== false, + options.sharedLinkPaths ?? null, + scope.readScope + ]) +} + +export function cachedPRBranchOutcome( + repoPath: string, + branch: string, + linkedPRNumber: number | null, + connectionId: string | null, + fallbackPRNumber: number | null, + options: GitHubPRBranchLookupOptions +): ReturnType { + const branchName = branch.replace(/^refs\/heads\//, '') + const scope = prLookupScope(repoPath, connectionId, options) + return readTimedPROutcome( + lastOutcomeKey(repoPath, branchName, scope), + completeReadKey(repoPath, branchName, linkedPRNumber, fallbackPRNumber, options, scope) + ) +} diff --git a/src/main/github/client/lookup/pull-request-lookup-data.ts b/src/main/github/client/lookup/pull-request-lookup-data.ts index 62f23ec8f95..b6812559e5b 100644 --- a/src/main/github/client/lookup/pull-request-lookup-data.ts +++ b/src/main/github/client/lookup/pull-request-lookup-data.ts @@ -92,6 +92,7 @@ export const PR_BRANCH_LIST_JSON_FIELDS = 'number,title,state,url,updatedAt,isDraft,mergeable,baseRefName,headRefName,baseRefOid,headRefOid,headRepositoryOwner,headRepository,isCrossRepository' export type GitHubPRBranchLookupOptions = HostedReviewExecutionOptions & { + resetQuietBackoff?: boolean userRefresh?: boolean /** CI watches need provider status, without local conflict or stack enrichment. */ includeDerivedData?: boolean diff --git a/src/main/github/pull-requests/refresh/hosted-pr-read-jobs.ts b/src/main/github/pull-requests/refresh/hosted-pr-read-jobs.ts new file mode 100644 index 00000000000..ecd7a1d962f --- /dev/null +++ b/src/main/github/pull-requests/refresh/hosted-pr-read-jobs.ts @@ -0,0 +1,308 @@ +import type { + GitHubPRRefreshCandidate, + PRRefreshOutcome, + PRSnapshotReply +} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { reviewRefreshIntervalMs } from '../../../../shared/review-refresh-policy' +import { deliverablePRBranchOutcome } from '../../client/lookup/pr-branch-read-resources' +import type { PreparedPRBranchRead } from '../../client/lookup/pr-for-branch-outcome' +import type { PRRefreshQueue, PRRefreshQueueEntry } from './pr-refresh-queue' +import type { PRRefreshPacing } from './pr-refresh-pacing' +import type { PRRefreshRetryState } from './pr-refresh-retry-state' + +export const HOSTED_PR_COMPLETION_WAIT_MS = 25_000 +const RETRY_MS = 10_000 +const MAX_JOBS = 256 +const MAX_WAITERS = 256 +const JOB_LEASE_MS = 5 * 60_000 + +export type HostedPRReadJob = { + prepared: PreparedPRBranchRead + expiresAt: number + running?: boolean + promoteSelected: () => void + listeners: Set<(outcome: PRRefreshOutcome | null) => void> + finish: (outcome: PRRefreshOutcome | null) => void +} + +export class HostedPRReadJobs { + private readonly jobs = new Map() + private waiters = 0 + + constructor( + private readonly queue: PRRefreshQueue, + private readonly pacing: PRRefreshPacing, + private readonly retry: PRRefreshRetryState, + private readonly wake: () => void, + private readonly observe: ( + candidate: GitHubPRRefreshCandidate, + outcome: PRRefreshOutcome + ) => void + ) {} + + has(prepared: PreparedPRBranchRead): boolean { + return this.jobs.has(`hosted::${prepared.key}`) + } + + fresh(prepared: PreparedPRBranchRead): PRRefreshOutcome | null { + return this.freshOutcome(prepared.cached()) + } + + freshOutcome( + cached: ReturnType, + selected = true + ): PRRefreshOutcome | null { + if (!cached || cached.outcome.kind === 'upstream-error') { + return null + } + const outcome = cached.outcome + const interval = + cached.quietIntervalMs ?? + reviewRefreshIntervalMs({ + state: outcome.kind === 'found' ? outcome.pr.state : null, + checksStatus: outcome.kind === 'found' ? outcome.pr.checksStatus : null, + hasReview: outcome.kind === 'found', + selected + }) + return interval === null || Date.now() - outcome.fetchedAt < interval ? outcome : null + } + + private async legacyRead( + prepared: PreparedPRBranchRead, + candidate: GitHubPRRefreshCandidate + ): Promise { + const current = await prepared.reprepare() + try { + const result = await current.readResult((outcome) => { + this.observe(candidate, outcome) + return outcome + }) + return result!.outcome + } finally { + current.release() + } + } + + private gateUntil(prepared: PreparedPRBranchRead): number { + const outcome = prepared.cached()?.outcome + return Math.max( + this.queue.backgroundGateUntil(`hosted::${prepared.key}`), + outcome?.kind === 'upstream-error' + ? Math.max(outcome.nextAutoRetryAt ?? 0, outcome.retryDisabledUntil ?? 0) + : 0 + ) + } + + retryAfterMs(prepared: PreparedPRBranchRead): number { + return Math.max( + RETRY_MS, + this.pacing.nextBudgetDelay(), + prepared.isCurrent() ? this.gateUntil(prepared) - Date.now() : 0 + ) + } + + async complete( + prepared: PreparedPRBranchRead, + candidate: GitHubPRRefreshCandidate, + outcome: PRRefreshOutcome | null, + acceptRevalidationOnly: boolean + ): Promise { + if (outcome && !acceptRevalidationOnly) { + return outcome + } + const current = prepared.isCurrent() + if (current) { + if (outcome && prepared.accepts(outcome)) { + return outcome + } + const fresh = this.fresh(prepared) + if (fresh) { + return fresh + } + } + const delay = this.retryAfterMs(prepared) + const cached = current ? prepared.cached()?.outcome : null + if (cached) { + return cached.kind === 'upstream-error' + ? cached + : { ...cached, revalidation: { retryAfterMs: delay } } + } + return acceptRevalidationOnly + ? { revalidation: { invalidated: true, retryAfterMs: delay } } + : this.legacyRead(prepared, candidate) + } + + async readCold( + candidate: GitHubPRRefreshCandidate, + prepared: PreparedPRBranchRead, + acceptRevalidationOnly: boolean + ): Promise { + try { + const result = await prepared.readResult((outcome) => { + const settled = + acceptRevalidationOnly && outcome.kind === 'upstream-error' + ? this.retry.settleProducerError( + outcome, + `hosted::${prepared.key}`, + { ...candidate, isSelected: false }, + prepared, + this.queue, + true + ) + : outcome + this.observe(candidate, settled) + return settled + }, acceptRevalidationOnly) + const outcome = result + ? acceptRevalidationOnly + ? deliverablePRBranchOutcome(prepared, result) + : result.outcome + : null + return await this.complete(prepared, candidate, outcome, acceptRevalidationOnly) + } finally { + prepared.release() + } + } + + async read( + prepared: PreparedPRBranchRead, + candidate: GitHubPRRefreshCandidate, + deadline: number, + signal?: AbortSignal, + acceptRevalidationOnly = false + ): Promise { + if (!prepared.isCurrent()) { + return this.complete(prepared, candidate, null, acceptRevalidationOnly) + } + const fresh = this.fresh(prepared) + if (fresh) { + return this.complete(prepared, candidate, fresh, acceptRevalidationOnly) + } + const key = `hosted::${prepared.key}` + for (const [oldKey, old] of this.jobs) { + if (old.expiresAt > Date.now()) { + break + } + if (old.running) { + continue + } + this.queue.delete(oldKey) + old.finish(null) + } + const fallback = () => this.complete(prepared, candidate, null, acceptRevalidationOnly) + const gate = this.gateUntil(prepared) + if (prepared.cached()?.outcome.kind === 'upstream-error' && gate > Date.now()) { + return fallback() + } + let job = this.jobs.get(key) + if (!job) { + if (this.jobs.size >= MAX_JOBS) { + return fallback() + } + const listeners: HostedPRReadJob['listeners'] = new Set() + const releaseHold = prepared.hold() + const expiresAt = Date.now() + JOB_LEASE_MS + const entry: PRRefreshQueueEntry = { + key, + candidate, + aliases: new Map(), + reason: 'swr', + priority: candidate.isSelected ? 80 : prepared.cached()?.quietIntervalMs ? 20 : 40, + dueAt: Math.min(expiresAt, Math.max(Date.now(), gate)), + queuedAt: this.queue.nextOrder() + } + job = { + prepared, + promoteSelected: () => { + if (!entry.candidate.isSelected) { + entry.candidate = { ...entry.candidate, isSelected: true } + entry.priority = 80 + this.wake() + } + }, + expiresAt, + listeners, + finish: (outcome) => { + releaseHold() + if (this.jobs.get(key) === job) { + this.jobs.delete(key) + } + const listenersAtCompletion = Array.from(listeners) + for (const listener of listenersAtCompletion) { + listener(outcome) + } + listeners.clear() + } + } + this.jobs.set(key, job) + entry.hosted = job + this.queue.set(key, entry) + this.wake() + } + if (candidate.isSelected && !job.running) { + job.promoteSelected() + } + if ( + !acceptRevalidationOnly || + this.waiters >= MAX_WAITERS || + Date.now() >= deadline || + Math.max(gate - Date.now(), this.pacing.nextBudgetDelay()) > deadline - Date.now() + ) { + return fallback() + } + const waitingJob = job + return new Promise((resolve, reject) => { + let timer: ReturnType + const settle = (outcome: PRRefreshOutcome | null): void => { + cleanup() + void this.complete(prepared, candidate, outcome, acceptRevalidationOnly).then( + resolve, + reject + ) + } + const abort = (): void => { + cleanup() + reject(new Error('Pull request wait was cancelled')) + } + const cleanup = (): void => { + if (!waitingJob.listeners.delete(settle)) { + return + } + this.waiters -= 1 + clearTimeout(timer) + signal?.removeEventListener('abort', abort) + } + waitingJob.listeners.add(settle) + this.waiters += 1 + signal?.addEventListener('abort', abort, { once: true }) + timer = setTimeout( + () => { + cleanup() + try { + resolve(fallback()) + } catch (error) { + reject(error) + } + }, + Math.max(0, deadline - Date.now()) + ) + if (signal?.aborted) { + abort() + } + }) + } + completeFromCache(entry: PRRefreshQueueEntry): boolean { + const job = entry.hosted + if (!job) { + return false + } + const current = job.prepared.isCurrent() + const fresh = current ? this.fresh(job.prepared) : null + if (job.expiresAt > Date.now() && current && !fresh && job.prepared.canRetain()) { + return false + } + this.queue.delete(entry.key) + job.finish(current && job.expiresAt > Date.now() ? fresh : null) + return true + } +} diff --git a/src/main/github/pull-requests/refresh/pr-refresh-candidate-policy.ts b/src/main/github/pull-requests/refresh/pr-refresh-candidate-policy.ts index de532e3d8ad..e3a3e12355f 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-candidate-policy.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-candidate-policy.ts @@ -1,5 +1,8 @@ import { shouldAcceptMergedFallbackPR } from '../../../../shared/github/pull-requests/refresh/pr-branch-fallback-rule' -import { reviewRefreshIntervalMs } from '../../../../shared/review-refresh-policy' +import { + REVIEW_REFRESH_COOLDOWN_MS, + reviewRefreshIntervalMs +} from '../../../../shared/review-refresh-policy' import type { GitHubPRRefreshAlias, GitHubPRRefreshCandidate, @@ -10,6 +13,8 @@ import type { import type { GitHubPRBranchLookupOptions } from '../../client/lookup/pull-request-lookup-data' import { PR_CHECK_PAGE_LIMIT } from '../../client/check/pr-check-rollup' import { parsePRKey } from '../../pr-watch/pr-watch-types' +import { quietPRIntervalForCandidate } from '../../client/lookup/pr-quiet-policy-evidence' +import { cachedPRBranchOutcome } from '../../client/lookup/pr-read-identity' export const MANUAL_MERGEABILITY_PENDING_REFRESH_MS = 2_500 export const POST_PUSH_DELAY_MS = 2_500 @@ -26,6 +31,9 @@ export function hostedReviewOptionArgs( ): [] | [GitHubPRBranchLookupOptions] { const options: GitHubPRBranchLookupOptions = requestOptions.userRefresh === true ? { userRefresh: true } : {} + if (bypassesFreshnessDelay(reason)) { + options.resetQuietBackoff = true + } options.localGitExecOptions = { ...(candidate.localGitOptions?.wslDistro ? { wslDistro: candidate.localGitOptions.wslDistro } @@ -126,10 +134,52 @@ export function shouldSkipFresh( return Date.now() - candidate.cachedFetchedAt < refreshIntervalForCandidate(candidate) } -export function freshRetryAt(candidate: GitHubPRRefreshCandidate): number | null { +export function foregroundRefreshAgeMs(candidate: GitHubPRRefreshCandidate): number { + const interval = refreshIntervalForCandidate(candidate) + return Number.isFinite(interval) ? interval : REVIEW_REFRESH_COOLDOWN_MS +} + +export function foregroundExposureDueAt( + candidate: GitHubPRRefreshCandidate, + reexposed: boolean +): number | null { + return reexposed && + !Number.isFinite(refreshIntervalForCandidate(candidate)) && + candidate.cachedFetchedAt != null && + Date.now() - candidate.cachedFetchedAt >= foregroundRefreshAgeMs(candidate) + ? Date.now() + : null +} + +export function freshRetryAt( + candidate: GitHubPRRefreshCandidate, + foreground = false +): number | null { return candidate.cachedFetchedAt == null || hasStaleHead(candidate) ? null - : candidate.cachedFetchedAt + refreshIntervalForCandidate(candidate) + : candidate.cachedFetchedAt + + (foreground ? foregroundRefreshAgeMs(candidate) : refreshIntervalForCandidate(candidate)) +} + +export function freshQueuedPROutcome( + candidate: GitHubPRRefreshCandidate, + reason: GitHubPRRefreshReason, + foreground = false +): PRRefreshOutcome | null { + const cached = cachedPRBranchOutcome( + candidate.repoPath, + candidate.branch, + candidate.linkedPRNumber ?? null, + candidate.connectionId ?? null, + candidate.linkedPRNumber == null ? (candidate.fallbackPRNumber ?? null) : null, + hostedReviewOptionArgs(candidate, reason)[0] ?? {} + ) + const outcome = cached?.outcome + if (!outcome || outcome.kind === 'upstream-error') { + return null + } + const dueAt = freshRetryAt(visibleCandidateAfterOutcome(candidate, outcome), foreground) + return dueAt !== null && dueAt > Date.now() ? outcome : null } export function aliasFromCandidate(candidate: GitHubPRRefreshCandidate): GitHubPRRefreshAlias { @@ -189,6 +239,12 @@ export function hasStaleHead(candidate: GitHubPRRefreshCandidate): boolean { } export function refreshIntervalForCandidate(candidate: GitHubPRRefreshCandidate): number { + if (candidate.cachedFetchedAt != null && !hasStaleHead(candidate)) { + const quiet = quietPRIntervalForCandidate(candidate) + if (quiet !== null) { + return quiet + } + } return ( reviewRefreshIntervalMs({ state: candidate.cachedPRState, diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-active-visible-priority.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-active-visible-priority.test.ts index 14c258f2d5c..61692bcb6c6 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-active-visible-priority.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-active-visible-priority.test.ts @@ -8,9 +8,7 @@ const { coordinatorMocks, moduleMocks } = await vi.hoisted(async () => { vi.mock('./pr-refresh-renderer-port', () => moduleMocks.rendererPortModuleMock(coordinatorMocks)) vi.mock('../../client/lookup/pr-for-branch-outcome', () => { - return { - getPRForBranchOutcome: coordinatorMocks.getPRForBranchOutcomeMock - } + return moduleMocks.clientModuleMock(coordinatorMocks) }) vi.mock('../../repositories/github-api-repository-remote-probe', () => { return { diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-alias-coalescing.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-alias-coalescing.test.ts index db6f0138965..721960799a4 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-alias-coalescing.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-alias-coalescing.test.ts @@ -7,9 +7,7 @@ const { coordinatorMocks, moduleMocks } = await vi.hoisted(async () => { vi.mock('./pr-refresh-renderer-port', () => moduleMocks.rendererPortModuleMock(coordinatorMocks)) vi.mock('../../client/lookup/pr-for-branch-outcome', () => { - return { - getPRForBranchOutcome: coordinatorMocks.getPRForBranchOutcomeMock - } + return moduleMocks.clientModuleMock(coordinatorMocks) }) vi.mock('../../repositories/github-api-repository-remote-probe', () => { return { diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-cache-follow-up.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-cache-follow-up.test.ts new file mode 100644 index 00000000000..830cb2b2295 --- /dev/null +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-cache-follow-up.test.ts @@ -0,0 +1,162 @@ +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { makeCandidate, makePR } from './pr-refresh-coordinator-test-harness' + +const mocks = vi.hoisted(() => ({ resolve: vi.fn(), send: vi.fn(), selection: 0 })) +vi.mock('../../client/lookup/branch-lookup-resolution', () => ({ + resolvePRForBranchOutcome: mocks.resolve +})) +vi.mock('../../accounts/github-project-account', () => ({ + githubPRPolicyProjects: () => ({ source: null, selection: mocks.selection }), + resolveGitHubProjectAccount: () => null +})) +vi.mock('../../execution/github-operation-slots', () => ({ + acquire: async () => {}, + release: () => {} +})) +vi.mock('./pr-refresh-rate-limit-gate', () => ({ prRefreshRateLimitPausedUntil: async () => null })) +vi.mock('./pr-refresh-renderer-port', () => ({ + prRefreshRenderers: () => ({ isRendererOpen: () => true, sendToTrustedUI: mocks.send }) +})) + +beforeEach(() => { + vi.resetModules() + vi.useFakeTimers() + vi.setSystemTime(1_000) + mocks.selection = 0 + mocks.resolve.mockReset() + mocks.send.mockReset() +}) +afterEach(() => { + vi.clearAllTimers() + vi.useRealTimers() +}) + +function found(overrides: Parameters[0] = {}): PRRefreshOutcome { + return { + kind: 'found', + fetchedAt: Date.now(), + pr: makePR({ + checksStatus: 'success', + checksReadComplete: true, + checksFinal: true, + autoMergeEnabled: false, + mergeQueueRequired: false, + ...overrides + }) + } +} + +it('delivers a real refresh after quiet evidence retires without repeating the cached follow-up', async () => { + mocks.resolve.mockImplementation(async () => found()) + const coordinator = await import('./pr-refresh-coordinator') + const { getPRForBranchOutcome } = await import('../../client/lookup/pr-for-branch-outcome') + const candidate = makeCandidate({ currentHeadOid: 'head-sha' }) + const seed = () => + coordinator.runHostedPRRead(candidate, () => + getPRForBranchOutcome('/repo', candidate.branch, null, null, null, { + currentHeadOid: 'head-sha' + }) + ) + await seed() + vi.setSystemTime(2_000) + await seed() + const visible = { + ...candidate, + cachedFetchedAt: 2_000, + cachedHeadOid: 'head-sha', + cachedHasPR: true, + cachedPRState: 'open' as const, + cachedChecksStatus: 'success' as const + } + coordinator.reportVisiblePRRefreshCandidates([visible], 1, 1) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + vi.setSystemTime(132_001) + mocks.selection++ + mocks.resolve.mockImplementation(async () => found({ title: 'updated' })) + mocks.send.mockClear() + mocks.send.mockImplementation(() => { + // Bound the unfixed synchronous loop even though its fake clock would otherwise never advance. + if (mocks.send.mock.calls.length === 8) { + vi.setSystemTime(1_000_000) + } + }) + coordinator.reportVisiblePRRefreshCandidates([visible], 2, 1) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(3) + expect(mocks.send.mock.calls.length).toBeLessThanOrEqual(2) + expect(mocks.send.mock.calls.at(-1)?.[1]).toMatchObject({ + outcome: { fetchedAt: 132_001, pr: { title: 'updated' } } + }) + await vi.advanceTimersByTimeAsync(120_001) + expect(mocks.send.mock.calls.at(-1)?.[1]).toMatchObject({ + outcome: { fetchedAt: 252_001, pr: { title: 'updated' } } + }) +}) + +it('rereads unresolved manual mergeability at its forced follow-up despite the timed cache', async () => { + mocks.resolve + .mockImplementationOnce(async () => + found({ mergeable: 'UNKNOWN', mergeStateStatus: 'UNKNOWN' }) + ) + .mockImplementation(async () => found({ mergeable: 'MERGEABLE', mergeStateStatus: 'CLEAN' })) + const coordinator = await import('./pr-refresh-coordinator') + const candidate = makeCandidate({ currentHeadOid: 'head-sha' }) + coordinator.reportVisiblePRRefreshCandidates([candidate], 1, 1) + expect(await coordinator.refreshPRNow(candidate)).toMatchObject({ + pr: { mergeable: 'UNKNOWN' } + }) + await vi.advanceTimersByTimeAsync(2_501) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + expect(mocks.send.mock.calls.at(-1)?.[1]).toMatchObject({ + outcome: { pr: { mergeable: 'MERGEABLE', mergeStateStatus: 'CLEAN' } } + }) +}) + +it.each([false, true])( + 'refreshes a settled row on re-exposure and shares a newer exact answer (refreshed while queued: %s)', + async (refreshedWhileQueued) => { + mocks.resolve.mockImplementation(async () => found({ state: 'merged' })) + const coordinator = await import('./pr-refresh-coordinator') + const { getPRForBranchOutcome } = await import('../../client/lookup/pr-for-branch-outcome') + const candidate = makeCandidate({ currentHeadOid: 'head-sha', linkedPRNumber: 12 }) + const seed = () => + coordinator.runHostedPRRead(candidate, () => + getPRForBranchOutcome('/repo', candidate.branch, 12, null, null, { + currentHeadOid: 'head-sha', + userRefresh: true + }) + ) + await seed() + const visible = { + ...candidate, + cachedFetchedAt: 1_000, + cachedHeadOid: 'head-sha', + cachedHasPR: true, + cachedPRState: 'merged' as const, + cachedChecksStatus: 'success' as const + } + coordinator.reportVisiblePRRefreshCandidates([visible], 1, 1) + coordinator.reportVisiblePRRefreshCandidates([], 2, 1) + vi.setSystemTime(2_000) + coordinator.reportVisiblePRRefreshCandidates([visible], 3, 1) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + coordinator.reportVisiblePRRefreshCandidates([], 4, 1) + vi.setSystemTime(21_000) + mocks.resolve.mockImplementation(async () => found({ state: 'merged', title: 'updated' })) + mocks.send.mockClear() + coordinator.reportVisiblePRRefreshCandidates([visible], 5, 1) + if (refreshedWhileQueued) { + await seed() + } + await vi.advanceTimersByTimeAsync(0) + expect(mocks.send.mock.calls.at(-1)?.[1]).toMatchObject({ + outcome: { fetchedAt: 21_000, pr: { state: 'merged', title: 'updated' } } + }) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + await vi.advanceTimersByTimeAsync(600_000) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + } +) diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-completion-order.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-completion-order.test.ts index f5470dd7ad2..fd787cc66fe 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-completion-order.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-completion-order.test.ts @@ -9,9 +9,7 @@ const { coordinatorMocks, moduleMocks } = await vi.hoisted(async () => { }) vi.mock('./pr-refresh-renderer-port', () => moduleMocks.rendererPortModuleMock(coordinatorMocks)) vi.mock('../../client/lookup/pr-for-branch-outcome', () => { - return { - getPRForBranchOutcome: coordinatorMocks.getPRForBranchOutcomeMock - } + return moduleMocks.clientModuleMock(coordinatorMocks) }) vi.mock('../../repositories/github-api-repository-remote-probe', () => { return { diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-test-mocks.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-test-mocks.ts index cc79d26e1ea..063957a1c12 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-test-mocks.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-test-mocks.ts @@ -1,4 +1,8 @@ import type { PRRefreshRendererPort } from './pr-refresh-renderer-port' +import type { + PreparedPRBranchRead, + preparePRBranchRead +} from '../../client/lookup/pr-for-branch-outcome' import { vi, type Mock } from 'vitest' import { isWslUncPath } from '../../../../shared/wsl-paths' @@ -51,11 +55,44 @@ export function rendererPortModuleMock(mocks: PRRefreshCoordinatorMocks): Render return { prRefreshRenderers: () => port } } -export type ClientModuleMock = { getPRForBranchOutcome: Mock } +export type ClientModuleMock = { + getPRForBranchOutcome: Mock + preparePRBranchRead: typeof preparePRBranchRead +} export function clientModuleMock(mocks: PRRefreshCoordinatorMocks): ClientModuleMock { return { - getPRForBranchOutcome: mocks.getPRForBranchOutcomeMock + getPRForBranchOutcome: mocks.getPRForBranchOutcomeMock, + preparePRBranchRead: async (...args) => { + const prepared: PreparedPRBranchRead = { + key: '', + reprepare: async () => prepared, + cached: () => null, + isCurrent: () => true, + accepts: () => true, + didRead: () => true, + release: () => {}, + hold: () => () => {}, + canRetain: () => true, + scheduleError: () => {}, + pending: () => null, + withExecution: (operation) => operation(), + read: async (settle) => { + const outcome = await mocks.getPRForBranchOutcomeMock(...args) + return settle ? settle(outcome, { retained: true }) : outcome + }, + readResult: async (settle) => ({ + outcome: await prepared.read(settle), + sequence: 0, + current: () => true + }), + readRetained: async (settle) => { + const outcome = await mocks.getPRForBranchOutcomeMock(...args) + return settle ? settle(outcome, { retained: true }) : outcome + } + } + return prepared + } } } diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-wsl-stopped-distro.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-wsl-stopped-distro.test.ts index ae9ffdc3d13..f14db379010 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-wsl-stopped-distro.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-wsl-stopped-distro.test.ts @@ -13,9 +13,7 @@ const gate = vi.hoisted(() => ({ vi.mock('./pr-refresh-renderer-port', () => moduleMocks.rendererPortModuleMock(coordinatorMocks)) vi.mock('../../client/lookup/pr-for-branch-outcome', () => { - return { - getPRForBranchOutcome: coordinatorMocks.getPRForBranchOutcomeMock - } + return moduleMocks.clientModuleMock(coordinatorMocks) }) vi.mock('../../repositories/github-api-repository-remote-probe', () => { return { diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator.ts index 231f2911d74..522c1252ff1 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator.ts @@ -1,14 +1,18 @@ +import { sharedPromiseSettlementWaiters } from '../../../../shared/promise-settlement-waiters' +import { runImmediatePRRefresh } from './pr-refresh-immediate-read' +import { + deliverablePRBranchOutcome, + type PRBranchReadResult +} from '../../client/lookup/pr-branch-read-resources' import type { GitHubPRRefreshCandidate, GitHubPRRefreshReason, - PRRefreshOutcome + PRRefreshOutcome, + PRSnapshotReply } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' -import { getPRForBranchOutcome } from '../../client/lookup/pr-for-branch-outcome' +import type { PreparedPRBranchRead } from '../../client/lookup/pr-for-branch-outcome' import { aliasFromCandidate, - hostedReviewOptionArgs, - isBackground, - MANUAL_MERGEABILITY_PENDING_REFRESH_MS, refreshKey, shouldBroadcastQueued, validateCandidate @@ -26,13 +30,9 @@ import { type PRWatchJob, type PRWatchJobHooks } from './pr-refresh-watch-jobs' -import { prRefreshRateLimitPausedUntil } from './pr-refresh-rate-limit-gate' import { PRRefreshRetryState } from './pr-refresh-retry-state' import { PRRefreshVisibility } from './pr-refresh-visibility' -import { - blockedWslDistroForPath, - wslDistroForPath -} from '../../../wsl/filesystem/wsl-unc-access-gate' +import { refreshPRPolicyContext } from '../../client/lookup/pr-quiet-policy-evidence' const retry = new PRRefreshRetryState() const queue = new PRRefreshQueue((key) => retry.reset(key)) @@ -46,7 +46,7 @@ export function addPRRefreshOutcomeObserver(observer: PRRefreshOutcomeObserver): } export function setPRRefreshRegistrationCheck(check: PRRefreshRegistrationCheck | null): void { - drainer.setRegistrationCheck(check) + drainer.registrationCheck = check } export function setPRWatchJobHooks(hooks: PRWatchJobHooks | null): void { @@ -72,11 +72,71 @@ export function cancelPRWatchRead(job: PRWatchJob): void { * so a desktop read in flight for the same checkout keeps its follow-up. */ export async function runHostedPRRead( candidate: GitHubPRRefreshCandidate, - read: () => Promise + read: (() => Promise) | PreparedPRBranchRead, + didRead: () => boolean = () => true, + release: () => void = () => {} ): Promise { - const outcome = await read() - events.observe(candidate, outcome) - return outcome + try { + if (typeof read !== 'function') { + const result = await read.readResult((outcome) => { + events.observe(candidate, outcome) + return outcome + }) + return result!.outcome + } + const outcome = await read() + if (didRead()) { + events.observe(candidate, outcome) + } + return outcome + } finally { + if (typeof read !== 'function') { + read.release() + } else { + release() + } + } +} + +export async function runHostedPRSnapshot( + candidate: GitHubPRRefreshCandidate, + prepared: PreparedPRBranchRead, + deadline: number, + signal?: AbortSignal, + acceptRevalidationOnly = false +): Promise { + if (prepared.cached() || drainer.hostedJobs.has(prepared)) { + return drainer.hostedJobs.read(prepared, candidate, deadline, signal, acceptRevalidationOnly) + } + if (acceptRevalidationOnly && !prepared.canRetain()) { + return drainer.hostedJobs.complete(prepared, candidate, null, acceptRevalidationOnly) + } + const pending = prepared.pending() + if (pending) { + const timeout = new Error('Pull request snapshot wait timed out') + let result: PRBranchReadResult + try { + result = await sharedPromiseSettlementWaiters(pending).wait({ + signal, + ...(acceptRevalidationOnly + ? { timeoutMs: Math.max(0, deadline - Date.now()), createTimeoutError: () => timeout } + : {}), + createAbortError: () => new Error('Pull request wait was cancelled') + }) + } catch (error) { + if (error === timeout) { + return drainer.hostedJobs.complete(prepared, candidate, null, acceptRevalidationOnly) + } + throw error + } + return drainer.hostedJobs.complete( + prepared, + candidate, + acceptRevalidationOnly ? deliverablePRBranchOutcome(prepared, result) : result.outcome, + acceptRevalidationOnly + ) + } + return drainer.hostedJobs.readCold(candidate, prepared, acceptRevalidationOnly) } function removeInvisibleVisibleRefreshes(): void { @@ -139,6 +199,7 @@ export function reportVisiblePRRefreshCandidates( generation: number, windowId: number ): void { + refreshPRPolicyContext() const newlyExposed = new Set(candidates.map(refreshKey).filter((key) => !visibility.has(key))) if (!visibility.report(candidates, generation, windowId)) { return @@ -178,110 +239,15 @@ export function _getPRRefreshAliasCountForTests(key: string): number { return queue.aliasCount(key) } -export async function refreshPRNow( +export function refreshPRNow( candidate: GitHubPRRefreshCandidate, reason: GitHubPRRefreshReason = 'manual', options: { userRefresh?: boolean } = {} -): Promise { - const alias = aliasFromCandidate(candidate) - const key = refreshKey(candidate) - const existing = queue.get(key) - const aliasMap = new Map(existing ? existing.aliases : []) - aliasMap.set(alias.cacheKey, alias) - const aliases = Array.from(aliasMap.values()) - const skippedReason = validateCandidate(candidate) - if (skippedReason) { - queue.removeInvalidAlias(key, alias) - const outcome: PRRefreshOutcome = { - kind: 'upstream-error', - errorType: 'unknown', - message: `Cannot refresh PR for this worktree: ${skippedReason}`, - fetchedAt: Date.now() - } - events.broadcast({ aliases: [alias], reason, status: 'skipped', skippedReason }) - return outcome - } - // E9: renderer fallbacks reach here with automatic reasons; only a manual refresh may start a stopped distro. - if ( - isBackground(reason) && - wslDistroForPath(candidate.repoPath, candidate.localGitOptions?.wslDistro) && - (await blockedWslDistroForPath(candidate.repoPath, candidate.localGitOptions?.wslDistro)) - ) { - events.broadcast({ aliases: [alias], reason, status: 'skipped', skippedReason: 'fresh' }) - // Local callers keep their cached PR on an upstream error; nothing is recorded or retried here. - return { - kind: 'upstream-error', - errorType: 'unknown', - message: 'WSL distro is not running', - fetchedAt: Date.now() - } - } - - const primaryGateUntil = await prRefreshRateLimitPausedUntil(candidate, false) - const gateUntil = Math.max(primaryGateUntil ?? 0, retry.manualGateUntil(key)) - if (gateUntil > Date.now()) { - queue.protectBackgroundUntil(key, gateUntil) - queue.set(key, { - key, - candidate, - aliases: aliasMap, - reason, - priority: 40, - dueAt: gateUntil, - queuedAt: queue.nextOrder() - }) - events.broadcast({ - aliases, - reason, - status: 'paused', - pausedUntil: gateUntil, - skippedReason: 'rate-limit' - }) - drainer.schedule(Math.max(1_000, gateUntil - Date.now())) - return { - kind: 'upstream-error', - errorType: 'rate_limited', - message: 'GitHub is temporarily limiting requests. Try again after the limit resets.', - fetchedAt: Date.now(), - nextAutoRetryAt: gateUntil, - retryDisabledUntil: gateUntil - } - } - - queue.delete(key) - const requestSequence = events.nextSequence() - const requestStartedAt = Date.now() - queue.noteRequestStarted(key, requestSequence) - events.broadcast({ aliases, reason, status: 'in-flight', requestStartedAt }, requestSequence) - const outcome = await getPRForBranchOutcome( - candidate.repoPath, - candidate.branch, - candidate.linkedPRNumber ?? null, - candidate.connectionId ?? null, - candidate.linkedPRNumber == null ? (candidate.fallbackPRNumber ?? null) : null, - ...hostedReviewOptionArgs(candidate, reason, options) +): Promise { + return runImmediatePRRefresh( + { queue, retry, visibility, events, drainer }, + candidate, + reason, + options ) - if (!queue.ownsRequest(key, requestSequence)) { - events.broadcast({ aliases, reason, outcome, requestStartedAt }, requestSequence) - return outcome - } - let plannedRetryAt: number | undefined - let broadcastOutcome = outcome - if (outcome.kind === 'upstream-error' && visibility.has(key)) { - plannedRetryAt = retry.nextVisibleErrorRetryAt(key, visibility.candidate(key, candidate)) - broadcastOutcome = retry.withErrorSchedule(outcome, plannedRetryAt) - } - events.observe(candidate, outcome, aliases) - retry.noteManualGate(key, broadcastOutcome) - events.broadcast( - { aliases, reason, outcome: broadcastOutcome, requestStartedAt }, - requestSequence - ) - drainer.scheduleVisibleFollowUp(key, candidate, outcome, 40, aliases, undefined, { - plannedRetryAt, - ...(reason === 'manual' - ? { pendingMergeabilityDelayMs: MANUAL_MERGEABILITY_PENDING_REFRESH_MS } - : {}) - }) - return broadcastOutcome } diff --git a/src/main/github/pull-requests/refresh/pr-refresh-event-publisher.ts b/src/main/github/pull-requests/refresh/pr-refresh-event-publisher.ts index 28af9df1b82..1f16af98ef5 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-event-publisher.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-event-publisher.ts @@ -16,6 +16,8 @@ export type PRRefreshOutcomeObserver = ( aliases: readonly GitHubPRRefreshAlias[] ) => void +import { projectPRPolicyOutcome } from '../../client/lookup/pr-quiet-policy-evidence' + const DIAGNOSTIC_BREADCRUMB_MIN_INTERVAL_MS = 30_000 export class PRRefreshEventPublisher { @@ -35,6 +37,7 @@ export class PRRefreshEventPublisher { outcome: PRRefreshOutcome, aliases: readonly GitHubPRRefreshAlias[] = [] ): void { + projectPRPolicyOutcome(candidate, outcome) for (const observer of this.observers) { try { observer(candidate, outcome, aliases) diff --git a/src/main/github/pull-requests/refresh/pr-refresh-immediate-read.ts b/src/main/github/pull-requests/refresh/pr-refresh-immediate-read.ts new file mode 100644 index 00000000000..8438d1a3604 --- /dev/null +++ b/src/main/github/pull-requests/refresh/pr-refresh-immediate-read.ts @@ -0,0 +1,173 @@ +import type { + GitHubPRRefreshCandidate, + GitHubPRRefreshReason, + PRRefreshOutcome, + PRSnapshotReply +} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { preparePRBranchRead } from '../../client/lookup/pr-for-branch-outcome' +import { deliverablePRBranchOutcome } from '../../client/lookup/pr-branch-read-resources' +import { + aliasFromCandidate, + refreshKey, + validateCandidate, + isBackground, + hostedReviewOptionArgs, + MANUAL_MERGEABILITY_PENDING_REFRESH_MS +} from './pr-refresh-candidate-policy' +import { + blockedWslDistroForPath, + wslDistroForPath +} from '../../../wsl/filesystem/wsl-unc-access-gate' +import { prRefreshRateLimitPausedUntil } from './pr-refresh-rate-limit-gate' +import type { PRRefreshQueue } from './pr-refresh-queue' +import type { PRRefreshRetryState } from './pr-refresh-retry-state' +import type { PRRefreshVisibility } from './pr-refresh-visibility' +import type { PRRefreshEventPublisher } from './pr-refresh-event-publisher' +import type { PRRefreshQueueDrainer } from './pr-refresh-queue-drainer' + +type ImmediateRefreshOwners = { + queue: PRRefreshQueue + retry: PRRefreshRetryState + visibility: PRRefreshVisibility + events: PRRefreshEventPublisher + drainer: PRRefreshQueueDrainer +} +export async function runImmediatePRRefresh( + { queue, retry, visibility, events, drainer }: ImmediateRefreshOwners, + candidate: GitHubPRRefreshCandidate, + reason: GitHubPRRefreshReason = 'manual', + options: { userRefresh?: boolean } = {} +): Promise { + const alias = aliasFromCandidate(candidate) + const key = refreshKey(candidate) + const existing = queue.get(key) + const aliasMap = new Map(existing ? existing.aliases : []) + aliasMap.set(alias.cacheKey, alias) + const aliases = Array.from(aliasMap.values()) + const skippedReason = validateCandidate(candidate) + if (skippedReason) { + queue.removeInvalidAlias(key, alias) + const outcome: PRRefreshOutcome = { + kind: 'upstream-error', + errorType: 'unknown', + message: `Cannot refresh PR for this worktree: ${skippedReason}`, + fetchedAt: Date.now() + } + events.broadcast({ aliases: [alias], reason, status: 'skipped', skippedReason }) + return outcome + } + // E9: renderer fallbacks reach here with automatic reasons; only a manual refresh may start a stopped distro. + if ( + isBackground(reason) && + wslDistroForPath(candidate.repoPath, candidate.localGitOptions?.wslDistro) && + (await blockedWslDistroForPath(candidate.repoPath, candidate.localGitOptions?.wslDistro)) + ) { + events.broadcast({ aliases: [alias], reason, status: 'skipped', skippedReason: 'fresh' }) + // Local callers keep their cached PR on an upstream error; nothing is recorded or retried here. + return { + kind: 'upstream-error', + errorType: 'unknown', + message: 'WSL distro is not running', + fetchedAt: Date.now() + } + } + + const primaryGateUntil = await prRefreshRateLimitPausedUntil(candidate, false) + const gateUntil = Math.max(primaryGateUntil ?? 0, retry.manualGateUntil(key)) + if (gateUntil > Date.now()) { + queue.protectBackgroundUntil(key, gateUntil) + queue.set(key, { + key, + candidate, + aliases: aliasMap, + reason, + priority: 40, + dueAt: gateUntil, + queuedAt: queue.nextOrder() + }) + events.broadcast({ + aliases, + reason, + status: 'paused', + pausedUntil: gateUntil, + skippedReason: 'rate-limit' + }) + drainer.schedule(Math.max(1_000, gateUntil - Date.now())) + return { + kind: 'upstream-error', + errorType: 'rate_limited', + message: 'GitHub is temporarily limiting requests. Try again after the limit resets.', + fetchedAt: Date.now(), + nextAutoRetryAt: gateUntil, + retryDisabledUntil: gateUntil + } + } + + queue.delete(key) + const requestSequence = events.nextSequence() + const requestStartedAt = Date.now() + queue.noteRequestStarted(key, requestSequence) + events.broadcast({ aliases, reason, status: 'in-flight', requestStartedAt }, requestSequence) + const prepared = await preparePRBranchRead( + candidate.repoPath, + candidate.branch, + candidate.linkedPRNumber ?? null, + candidate.connectionId ?? null, + candidate.linkedPRNumber == null ? (candidate.fallbackPRNumber ?? null) : null, + ...hostedReviewOptionArgs(candidate, reason, options) + ) + try { + const result = await prepared.readResult( + drainer.scheduler.settleRead(key, candidate, aliases, prepared, requestSequence) + ) + if (!result) { + throw new Error('An unrestricted PR read must return its own result') + } + const outcome = deliverablePRBranchOutcome(prepared, result) + if (!outcome) { + if (queue.ownsRequest(key, requestSequence)) { + events.broadcast( + { aliases, reason, status: 'skipped', skippedReason: 'fresh' }, + requestSequence + ) + drainer.scheduler.completeWithoutOutcome( + key, + candidate, + aliases, + 40, + undefined, + !isBackground(reason) + ) + } + return { + revalidation: { invalidated: true, retryAfterMs: drainer.hostedJobs.retryAfterMs(prepared) } + } + } + if (!queue.ownsRequest(key, requestSequence)) { + events.broadcast({ aliases, reason, outcome, requestStartedAt }, requestSequence) + return outcome + } + let plannedRetryAt: number | undefined + let broadcastOutcome = outcome + if (outcome.kind === 'upstream-error' && visibility.has(key)) { + plannedRetryAt = + outcome.nextAutoRetryAt ?? + retry.nextVisibleErrorRetryAt(key, visibility.candidate(key, candidate)) + broadcastOutcome = retry.withErrorSchedule(outcome, plannedRetryAt) + } + retry.noteManualGate(key, broadcastOutcome) + events.broadcast( + { aliases, reason, outcome: broadcastOutcome, requestStartedAt }, + requestSequence + ) + drainer.scheduleVisibleFollowUp(key, candidate, outcome, 40, aliases, undefined, { + plannedRetryAt, + ...(reason === 'manual' + ? { pendingMergeabilityDelayMs: MANUAL_MERGEABILITY_PENDING_REFRESH_MS } + : {}) + }) + return broadcastOutcome + } finally { + prepared.release() + } +} diff --git a/src/main/github/pull-requests/refresh/pr-refresh-pacing.ts b/src/main/github/pull-requests/refresh/pr-refresh-pacing.ts index 2013a5e05ae..0353b30c4b4 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-pacing.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-pacing.ts @@ -7,6 +7,13 @@ const BACKGROUND_BUDGET_MAX = 20 const ACTIVE_BURST_WINDOW_MS = 30_000 const ACTIVE_BURST_MAX = 3 +export function isSelectedBudgetedPRRefresh(entry: PRRefreshQueueEntry): boolean { + return ( + entry.candidate.isSelected === true && + (entry.reason === 'visible' || (entry.reason === 'swr' && entry.hosted !== undefined)) + ) +} + export function usesActiveRefreshPacing(entry: PRRefreshQueueEntry): boolean { // Selected admission survives deselection while queued; periodic follow-ups clear the flag. return ( @@ -31,8 +38,7 @@ export class PRRefreshPacing { } noteBackgroundStart(entry: PRRefreshQueueEntry): void { - this.lastBudgetedStartWasSelected = - entry.reason === 'visible' && entry.candidate.isSelected === true + this.lastBudgetedStartWasSelected = isSelectedBudgetedPRRefresh(entry) const now = Date.now() this.lastBackgroundStartAt = now this.backgroundStarts.push(now) @@ -70,8 +76,7 @@ export class PRRefreshPacing { private priority(entry: PRRefreshQueueEntry): number { // Reserve the next budgeted slot for other ready reviews after a selected list read. return this.lastBudgetedStartWasSelected && - entry.reason === 'visible' && - entry.candidate.isSelected === true && + isSelectedBudgetedPRRefresh(entry) && entry.bypassBackgroundBudget !== true ? 0 : entry.priority diff --git a/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts b/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts index 17d3712655f..54c39d72422 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts @@ -1,41 +1,38 @@ -import type { - GitHubPRRefreshAlias, - GitHubPRRefreshCandidate, - PRRefreshOutcome -} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' -import { getPRForBranchOutcome } from '../../client/lookup/pr-for-branch-outcome' +import { admitPreparedPRRead } from './pr-refresh-read-admission' +import { deliverablePRBranchOutcome } from '../../client/lookup/pr-branch-read-resources' +import type { GitHubPRRefreshCandidate } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { preparePRBranchRead } from '../../client/lookup/pr-for-branch-outcome' import { - aliasFromCandidate, - freshRetryAt, hostedReviewOptionArgs, + freshQueuedPROutcome, + bypassesFreshnessDelay, isBackground, isBudgetedBackground, - isMergeabilityPendingOutcome, - sameAliasRequestIdentity, - validateCandidate, - visibleCandidateAfterOutcome, - visibleFollowUpPriority + validateCandidate } from './pr-refresh-candidate-policy' import type { PRRefreshEventPublisher } from './pr-refresh-event-publisher' import { type PRRefreshPacing, usesActiveRefreshPacing } from './pr-refresh-pacing' -import type { PRRefreshQueue, PRRefreshQueueEntry } from './pr-refresh-queue' +import type { PRRefreshQueue } from './pr-refresh-queue' import { PRWatchJobRunner } from './pr-refresh-watch-jobs' import { prRefreshRateLimitPausedUntil } from './pr-refresh-rate-limit-gate' import { blockedWslDistroForPath, wslDistroForPath } from '../../../wsl/filesystem/wsl-unc-access-gate' -import { outsideWslUserAct } from '../../../wsl/discovery/wsl-user-act-scope' import type { PRRefreshRetryState } from './pr-refresh-retry-state' import type { PRRefreshVisibility } from './pr-refresh-visibility' +import { HostedPRReadJobs } from './hosted-pr-read-jobs' +import { PRRefreshQueueScheduler } from './pr-refresh-queue-scheduler' export type PRRefreshRegistrationCheck = (candidate: GitHubPRRefreshCandidate) => boolean export class PRRefreshQueueDrainer { private draining = false - private timer: ReturnType | null = null - private registrationCheck: PRRefreshRegistrationCheck | null = null + registrationCheck: PRRefreshRegistrationCheck | null = null readonly watchJobs: PRWatchJobRunner + readonly hostedJobs: HostedPRReadJobs + readonly scheduler: PRRefreshQueueScheduler + readonly scheduleVisibleFollowUp: PRRefreshQueueScheduler['scheduleVisibleFollowUp'] constructor( private readonly queue: PRRefreshQueue, @@ -45,110 +42,27 @@ export class PRRefreshQueueDrainer { private readonly events: PRRefreshEventPublisher ) { this.watchJobs = new PRWatchJobRunner(queue, retry, this) - } - - setRegistrationCheck(check: PRRefreshRegistrationCheck | null): void { - this.registrationCheck = check + this.scheduler = new PRRefreshQueueScheduler( + queue, + pacing, + visibility, + retry, + events, + this.watchJobs, + () => this.drain() + ) + this.scheduleVisibleFollowUp = this.scheduler.scheduleVisibleFollowUp.bind(this.scheduler) + this.hostedJobs = new HostedPRReadJobs( + queue, + pacing, + retry, + () => this.schedule(), + (candidate, outcome) => events.observe(candidate, outcome) + ) } schedule(delay = 0): void { - clearTimeout(this.timer ?? undefined) - this.timer = setTimeout(() => { - this.timer = null - // The drain serves every queued repo; a scheduling call inside a WSL user act must not lend it that act. - void outsideWslUserAct(() => this.drain()) - }, delay) - } - - scheduleVisibleFollowUp( - key: string, - candidate: GitHubPRRefreshCandidate, - outcome: PRRefreshOutcome, - priority: number, - aliases: GitHubPRRefreshAlias[], - windowId?: number, - options?: { pendingMergeabilityDelayMs?: number; plannedRetryAt?: number } - ): void { - if (outcome.kind === 'upstream-error') { - const retryAt = Math.max( - options?.plannedRetryAt ?? - this.retry.nextVisibleErrorRetryAt(key, this.visibility.candidate(key, candidate)), - this.retry.manualGateUntil(key) - ) - this.queue.protectBackgroundUntil(key, retryAt) - if (!this.visibility.has(key)) { - return - } - this.queue.setVisibleFollowUp({ - key, - candidate, - aliases: new Map(aliases.map((alias) => [alias.cacheKey, alias])), - reason: 'visible', - priority, - dueAt: retryAt, - queuedAt: this.queue.nextOrder(), - windowId - }) - this.schedule(retryAt - Date.now()) - return - } - this.retry.reset(key) - const refreshed = visibleCandidateAfterOutcome(candidate, outcome) - this.visibility.update(refreshed) - if (!this.visibility.has(key)) { - return - } - const followUpCandidate = this.visibility.candidate(key, refreshed) - const regularDueAt = freshRetryAt(followUpCandidate) ?? Date.now() - const pendingDueAt = - options?.pendingMergeabilityDelayMs !== undefined && isMergeabilityPendingOutcome(outcome) - ? outcome.fetchedAt + options.pendingMergeabilityDelayMs - : null - const dueAt = pendingDueAt === null ? regularDueAt : Math.min(regularDueAt, pendingDueAt) - if (!Number.isFinite(dueAt)) { - const pending = this.queue.get(key) - if ( - pending?.reason === 'visible' && - sameAliasRequestIdentity( - aliasFromCandidate(pending.candidate), - aliasFromCandidate(followUpCandidate) - ) - ) { - this.queue.delete(key) - } - return - } - this.queue.setVisibleFollowUp({ - key, - candidate: followUpCandidate, - aliases: new Map(aliases.map((alias) => [alias.cacheKey, alias])), - reason: 'visible', - priority: visibleFollowUpPriority(followUpCandidate, priority), - dueAt, - queuedAt: this.queue.nextOrder(), - bypassBackgroundBudget: pendingDueAt !== null, - followUp: true, - windowId - }) - this.schedule(Math.max(0, dueAt - Date.now())) - } - - private ordered(): PRRefreshQueueEntry[] { - this.watchJobs.age(this.queue.values(), Date.now()) - return this.queue.ordered((a, b) => this.pacing.order(a, b)) - } - - private nextQueuedWakeDelay(excludedKey: string): number | null { - const now = Date.now() - let nextDelay = Number.POSITIVE_INFINITY - for (const entry of this.queue.values()) { - if (entry.key === excludedKey) { - continue - } - const delay = entry.dueAt > now ? entry.dueAt - now : this.pacing.entryDelay(entry) - nextDelay = Math.min(nextDelay, delay) - } - return Number.isFinite(nextDelay) ? Math.max(0, nextDelay) : null + this.scheduler.schedule(delay) } private async drain(): Promise { @@ -157,8 +71,12 @@ export class PRRefreshQueueDrainer { } this.draining = true try { + const { queue, pacing, events, scheduler, watchJobs } = this while (this.queue.size > 0) { - let next = this.ordered()[0] + let next = this.scheduler.ordered()[0] + if (this.hostedJobs.completeFromCache(next)) { + continue + } const waitMs = next.dueAt - Date.now() if (waitMs > 0) { this.schedule(waitMs) @@ -167,23 +85,27 @@ export class PRRefreshQueueDrainer { let delay = this.pacing.entryDelay(next) if (delay > 0) { - const runnable = this.ordered().find( - (entry) => entry.dueAt <= Date.now() && this.pacing.entryDelay(entry) === 0 - ) + const runnable = this.scheduler + .ordered() + .find((entry) => entry.dueAt <= Date.now() && this.pacing.entryDelay(entry) === 0) if (runnable && runnable.key !== next.key) { next = runnable delay = 0 } else { - this.notePacingDelay(next) - this.schedule(Math.min(delay, this.nextQueuedWakeDelay(next.key) ?? delay)) + this.scheduler.notePacingDelay(next) + this.schedule(Math.min(delay, this.scheduler.nextQueuedWakeDelay(next.key) ?? delay)) return } } this.queue.delete(next.key) + if (this.hostedJobs.completeFromCache(next)) { + continue + } const aliases = Array.from(next.aliases.values()) const skippedReason = validateCandidate(next.candidate) if (skippedReason) { + next.hosted?.finish(null) this.events.record('skipped', next.reason, skippedReason) this.events.broadcast({ aliases, reason: next.reason, status: 'skipped', skippedReason }) continue @@ -199,6 +121,7 @@ export class PRRefreshQueueDrainer { next.candidate.localGitOptions?.wslDistro ))) ) { + next.hosted?.finish(null) this.events.broadcast({ aliases, reason: next.reason, @@ -208,70 +131,158 @@ export class PRRefreshQueueDrainer { this.watchJobs.skipped(next) continue } + if ( + !next.hosted && + !next.watch && + !next.bypassBackgroundBudget && + (next.reason === 'visible' || next.reason === 'swr') + ) { + const candidate = next.candidate + const fresh = freshQueuedPROutcome(candidate, next.reason, next.foreground) + if (fresh) { + this.events.broadcast({ aliases, reason: next.reason, outcome: fresh }) + this.scheduleVisibleFollowUp( + next.key, + candidate, + fresh, + next.priority, + aliases, + next.windowId + ) + continue + } + } const requestSequence = this.events.nextSequence() const requestStartedAt = Date.now() this.queue.noteRequestStarted(next.key, requestSequence) - this.events.broadcast( - { aliases, reason: next.reason, status: 'in-flight', requestStartedAt }, - requestSequence - ) - - if (isBackground(next.reason)) { - const pausedUntil = await prRefreshRateLimitPausedUntil(next.candidate, true) + if (!next.hosted) { + this.events.broadcast( + { aliases, reason: next.reason, status: 'in-flight', requestStartedAt }, + requestSequence + ) + } + let joinedRead = next.hosted?.prepared.pending() + if (isBackground(next.reason) && !joinedRead) { + const checkRateLimit = () => prRefreshRateLimitPausedUntil(next.candidate, true) + const pausedUntil = await (next.hosted + ? next.hosted.prepared.withExecution(checkRateLimit) + : checkRateLimit()) if (!this.queue.ownsRequest(next.key, requestSequence)) { + next.hosted?.finish(null) continue } if (pausedUntil !== null) { this.queue.protectBackgroundUntil(next.key, pausedUntil) - this.queue.set(next.key, { ...next, dueAt: pausedUntil }) - this.events.broadcast({ - aliases, - reason: next.reason, - status: 'paused', - pausedUntil, - skippedReason: 'rate-limit' - }) - this.schedule(Math.max(1_000, pausedUntil - Date.now())) + const dueAt = Math.min(pausedUntil, next.hosted?.expiresAt ?? Infinity) + next.dueAt = dueAt + this.queue.set(next.key, next) + if (!next.hosted) { + this.events.broadcast({ + aliases, + reason: next.reason, + status: 'paused', + pausedUntil, + skippedReason: 'rate-limit' + }) + } + this.schedule(Math.max(1_000, dueAt - Date.now())) continue } - if (next.bypassBackgroundBudget !== true && isBudgetedBackground(next.reason)) { + if (this.hostedJobs.completeFromCache(next)) { + continue + } + if ( + next.watch && + next.bypassBackgroundBudget !== true && + isBudgetedBackground(next.reason) + ) { this.pacing.noteBackgroundStart(next) this.watchJobs.noteBudgetedStart(next) } - if (usesActiveRefreshPacing(next)) { + if (next.watch && usesActiveRefreshPacing(next)) { this.pacing.noteActiveStart(next) } } - if (await this.watchJobs.readHead(next)) { continue } - const outcome = await getPRForBranchOutcome( - next.candidate.repoPath, - next.candidate.branch, - next.candidate.linkedPRNumber ?? null, - next.candidate.connectionId ?? null, - next.candidate.linkedPRNumber == null ? (next.candidate.fallbackPRNumber ?? null) : null, - ...hostedReviewOptionArgs(next.candidate, next.reason, { watchPRKey: next.watch?.prKey }) + if (next.hosted) { + next.hosted.running = true + } + const prepared = + next.hosted?.prepared ?? + (await preparePRBranchRead( + next.candidate.repoPath, + next.candidate.branch, + next.candidate.linkedPRNumber ?? null, + next.candidate.connectionId ?? null, + next.candidate.linkedPRNumber == null + ? (next.candidate.fallbackPRNumber ?? null) + : null, + ...hostedReviewOptionArgs(next.candidate, next.reason, { + watchPRKey: next.watch?.prKey + }) + )) + joinedRead ??= bypassesFreshnessDelay(next.reason) ? undefined : prepared.pending() + const admitted = admitPreparedPRRead( + { queue, pacing, events, scheduler, watchJobs }, + next, + prepared, + aliases, + requestSequence, + requestStartedAt, + Boolean(joinedRead) ) + if (!admitted) { + continue + } + const settle = this.scheduler.settleRead( + next.key, + next.candidate, + aliases, + prepared, + requestSequence, + Boolean(next.hosted) + ) + const result = await (joinedRead ?? prepared.readResult(settle, Boolean(next.hosted))) + const outcome = result ? deliverablePRBranchOutcome(prepared, result) : null + if (!outcome) { + this.scheduler.finishWithoutOutcome(next, aliases, requestSequence) + continue + } + if ( + next.hosted && + (!next.hosted.prepared.isCurrent() || !next.hosted.prepared.accepts(outcome)) + ) { + next.hosted.finish(null) + continue + } if (!this.queue.ownsRequest(next.key, requestSequence)) { + next.hosted?.finish(null) this.events.broadcast( { aliases, reason: next.reason, outcome, requestStartedAt }, requestSequence ) continue } - let plannedRetryAt: number | undefined - let broadcastOutcome = outcome - if (outcome.kind === 'upstream-error' && this.visibility.has(next.key)) { - plannedRetryAt = this.retry.nextVisibleErrorRetryAt( - next.key, - this.visibility.candidate(next.key, next.candidate) - ) - broadcastOutcome = this.retry.withErrorSchedule(outcome, plannedRetryAt) - } - this.events.observe(next.candidate, outcome, aliases) + const plannedRetryAt = this.retry.errorFollowUpAt( + next.key, + this.visibility.candidate(next.key, next.candidate), + outcome, + !next.hosted && this.visibility.has(next.key) + ) + const broadcastOutcome = + plannedRetryAt === undefined + ? outcome + : this.retry.withErrorSchedule(outcome, plannedRetryAt) this.retry.noteManualGate(next.key, broadcastOutcome) + if (next.hosted) { + if (outcome.kind !== 'upstream-error') { + this.retry.reset(next.key) + } + next.hosted.finish(broadcastOutcome) + continue + } this.events.broadcast( { aliases, reason: next.reason, outcome: broadcastOutcome, requestStartedAt }, requestSequence @@ -294,22 +305,4 @@ export class PRRefreshQueueDrainer { this.draining = false } } - - private notePacingDelay(entry: PRRefreshQueueEntry): void { - if (this.pacing.isActiveBurstDelayed(entry) && !entry.activeDelayNotified) { - entry.activeDelayNotified = true - this.events.broadcast({ - aliases: Array.from(entry.aliases.values()), - reason: entry.reason, - status: 'queued' - }) - } - if ( - entry.bypassBackgroundBudget !== true && - isBudgetedBackground(entry.reason) && - this.pacing.nextBudgetDelay() > 0 - ) { - this.events.record('background-pause', entry.reason) - } - } } diff --git a/src/main/github/pull-requests/refresh/pr-refresh-queue-growth-bound.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-queue-growth-bound.test.ts index 980557b97bc..2a2323a7209 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-queue-growth-bound.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-queue-growth-bound.test.ts @@ -8,9 +8,7 @@ const { coordinatorMocks, moduleMocks } = await vi.hoisted(async () => { vi.mock('./pr-refresh-renderer-port', () => moduleMocks.rendererPortModuleMock(coordinatorMocks)) vi.mock('../../client/lookup/pr-for-branch-outcome', () => { - return { - getPRForBranchOutcome: coordinatorMocks.getPRForBranchOutcomeMock - } + return moduleMocks.clientModuleMock(coordinatorMocks) }) vi.mock('../../repositories/github-api-repository-remote-probe', () => { return { diff --git a/src/main/github/pull-requests/refresh/pr-refresh-queue-scheduler.ts b/src/main/github/pull-requests/refresh/pr-refresh-queue-scheduler.ts new file mode 100644 index 00000000000..2a2459cd804 --- /dev/null +++ b/src/main/github/pull-requests/refresh/pr-refresh-queue-scheduler.ts @@ -0,0 +1,298 @@ +import type { + GitHubPRRefreshAlias, + GitHubPRRefreshCandidate, + PRRefreshOutcome +} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { + aliasFromCandidate, + freshRetryAt, + isBudgetedBackground, + isMergeabilityPendingOutcome, + sameAliasRequestIdentity, + visibleCandidateAfterOutcome, + visibleFollowUpPriority +} from './pr-refresh-candidate-policy' +import type { + PreparedPRBranchRead, + PRReadSettlement +} from '../../client/lookup/pr-for-branch-outcome' +import type { PRRefreshEventPublisher } from './pr-refresh-event-publisher' +import type { PRRefreshPacing } from './pr-refresh-pacing' +import type { PRRefreshQueue, PRRefreshQueueEntry } from './pr-refresh-queue' +import type { PRWatchJobRunner } from './pr-refresh-watch-jobs' +import type { PRRefreshRetryState } from './pr-refresh-retry-state' +import type { PRRefreshVisibility } from './pr-refresh-visibility' +import { outsideWslUserAct } from '../../../wsl/discovery/wsl-user-act-scope' + +export class PRRefreshQueueScheduler { + private timer: ReturnType | null = null + private timerDueAt = Infinity + + constructor( + private readonly queue: PRRefreshQueue, + private readonly pacing: PRRefreshPacing, + private readonly visibility: PRRefreshVisibility, + private readonly retry: PRRefreshRetryState, + private readonly events: PRRefreshEventPublisher, + private readonly watchJobs: PRWatchJobRunner, + private readonly runDrain: () => Promise + ) {} + + schedule(delay = 0): void { + const dueAt = performance.now() + Math.max(0, delay) + if (this.timer !== null && this.timerDueAt <= dueAt) { + return + } + clearTimeout(this.timer ?? undefined) + this.timerDueAt = dueAt + this.timer = setTimeout(() => { + this.timer = null + this.timerDueAt = Infinity + // The drain serves every queued repo; a scheduling call inside a WSL user act must not lend it that act. + void outsideWslUserAct(() => this.runDrain()) + }, delay) + } + + settleRead( + key: string, + candidate: GitHubPRRefreshCandidate, + aliases: GitHubPRRefreshAlias[], + prepared: PreparedPRBranchRead, + requestSequence: number, + hosted = false + ): PRReadSettlement { + return (outcome, producer) => { + if (!prepared.isCurrent()) { + return outcome + } + const settled = + producer.retained && + outcome.kind === 'upstream-error' && + (hosted || this.visibility.has(key)) + ? this.retry.settleProducerError( + outcome, + key, + hosted + ? { ...candidate, isSelected: false } + : this.visibility.candidate(key, candidate), + prepared, + this.queue, + this.queue.ownsRequest(key, requestSequence) + ) + : outcome + this.events.observe(candidate, settled, aliases) + return settled + } + } + + completeWithoutOutcome( + key: string, + candidate: GitHubPRRefreshCandidate, + aliases: GitHubPRRefreshAlias[], + priority: number, + windowId?: number, + foreground = false + ): void { + if (!this.visibility.has(key) || this.queue.get(key)) { + return + } + const current = this.visibility.candidate(key, candidate) + const dueAt = Math.max( + freshRetryAt(current, foreground) ?? Date.now(), + this.queue.backgroundGateUntil(key), + this.retry.manualGateUntil(key) + ) + if (!Number.isFinite(dueAt)) { + return + } + this.queue.setVisibleFollowUp({ + key, + candidate: current, + aliases: new Map(aliases.map((alias) => [alias.cacheKey, alias])), + reason: 'visible', + priority: visibleFollowUpPriority(current, priority), + dueAt, + queuedAt: this.queue.nextOrder(), + followUp: !foreground, + foreground, + windowId + }) + this.schedule(Math.max(0, dueAt - Date.now())) + } + + finishWithoutOutcome( + next: PRRefreshQueueEntry, + aliases: GitHubPRRefreshAlias[], + requestSequence: number + ): void { + next.hosted?.finish(null) + if (!next.hosted && this.queue.ownsRequest(next.key, requestSequence)) { + this.events.broadcast( + { aliases, reason: next.reason, status: 'skipped', skippedReason: 'fresh' }, + requestSequence + ) + if (next.watch) { + this.watchJobs.skipped(next) + } else { + this.completeWithoutOutcome( + next.key, + next.candidate, + aliases, + next.priority, + next.windowId, + next.foreground + ) + } + } + } + + consumeGatedError( + entry: PRRefreshQueueEntry, + prepared: PreparedPRBranchRead, + requestSequence: number, + requestStartedAt: number + ): boolean { + if ( + entry.hosted || + entry.watch || + entry.bypassBackgroundBudget || + (entry.reason !== 'visible' && entry.reason !== 'swr') || + !prepared.isCurrent() + ) { + return false + } + const outcome = prepared.cached()?.outcome + if (outcome?.kind !== 'upstream-error') { + return false + } + const retryAt = Math.max(outcome.nextAutoRetryAt ?? 0, outcome.retryDisabledUntil ?? 0) + if (retryAt <= Date.now()) { + return false + } + const aliases = Array.from(entry.aliases.values()) + this.retry.noteManualGate(entry.key, outcome) + this.events.broadcast( + { aliases, reason: entry.reason, outcome, requestStartedAt }, + requestSequence + ) + this.scheduleVisibleFollowUp( + entry.key, + entry.candidate, + outcome, + entry.priority, + aliases, + entry.windowId, + { plannedRetryAt: retryAt } + ) + return true + } + + scheduleVisibleFollowUp( + key: string, + candidate: GitHubPRRefreshCandidate, + outcome: PRRefreshOutcome, + priority: number, + aliases: GitHubPRRefreshAlias[], + windowId?: number, + options?: { pendingMergeabilityDelayMs?: number; plannedRetryAt?: number } + ): void { + if (outcome.kind === 'upstream-error') { + const retryAt = Math.max( + options?.plannedRetryAt ?? + this.retry.nextVisibleErrorRetryAt(key, this.visibility.candidate(key, candidate)), + this.retry.manualGateUntil(key) + ) + this.queue.protectBackgroundUntil(key, retryAt) + if (!this.visibility.has(key)) { + return + } + this.queue.setVisibleFollowUp({ + key, + candidate, + aliases: new Map(aliases.map((alias) => [alias.cacheKey, alias])), + reason: 'visible', + priority, + dueAt: retryAt, + queuedAt: this.queue.nextOrder(), + windowId + }) + this.schedule(retryAt - Date.now()) + return + } + this.retry.reset(key) + const refreshed = visibleCandidateAfterOutcome(candidate, outcome) + this.visibility.update(refreshed) + if (!this.visibility.has(key)) { + return + } + const followUpCandidate = this.visibility.candidate(key, refreshed) + const regularDueAt = freshRetryAt(followUpCandidate) ?? Date.now() + const pendingDueAt = + options?.pendingMergeabilityDelayMs !== undefined && isMergeabilityPendingOutcome(outcome) + ? outcome.fetchedAt + options.pendingMergeabilityDelayMs + : null + const dueAt = pendingDueAt === null ? regularDueAt : Math.min(regularDueAt, pendingDueAt) + if (!Number.isFinite(dueAt)) { + const pending = this.queue.get(key) + if ( + pending?.reason === 'visible' && + sameAliasRequestIdentity( + aliasFromCandidate(pending.candidate), + aliasFromCandidate(followUpCandidate) + ) + ) { + this.queue.delete(key) + } + return + } + this.queue.setVisibleFollowUp({ + key, + candidate: followUpCandidate, + aliases: new Map(aliases.map((alias) => [alias.cacheKey, alias])), + reason: 'visible', + priority: visibleFollowUpPriority(followUpCandidate, priority), + dueAt, + queuedAt: this.queue.nextOrder(), + bypassBackgroundBudget: pendingDueAt !== null, + followUp: true, + windowId + }) + this.schedule(Math.max(0, dueAt - Date.now())) + } + + ordered(): PRRefreshQueueEntry[] { + this.watchJobs.age(this.queue.values(), Date.now()) + return this.queue.ordered((a, b) => this.pacing.order(a, b)) + } + + nextQueuedWakeDelay(excludedKey: string): number | null { + const now = Date.now() + let nextDelay = Number.POSITIVE_INFINITY + for (const entry of this.queue.values()) { + if (entry.key === excludedKey) { + continue + } + const delay = entry.dueAt > now ? entry.dueAt - now : this.pacing.entryDelay(entry) + nextDelay = Math.min(nextDelay, delay) + } + return Number.isFinite(nextDelay) ? Math.max(0, nextDelay) : null + } + + notePacingDelay(entry: PRRefreshQueueEntry): void { + if (this.pacing.isActiveBurstDelayed(entry) && !entry.activeDelayNotified) { + entry.activeDelayNotified = true + this.events.broadcast({ + aliases: Array.from(entry.aliases.values()), + reason: entry.reason, + status: 'queued' + }) + } + if ( + entry.bypassBackgroundBudget !== true && + isBudgetedBackground(entry.reason) && + this.pacing.nextBudgetDelay() > 0 + ) { + this.events.record('background-pause', entry.reason) + } + } +} diff --git a/src/main/github/pull-requests/refresh/pr-refresh-queue.ts b/src/main/github/pull-requests/refresh/pr-refresh-queue.ts index 00caf6e68ae..77db82b084b 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-queue.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-queue.ts @@ -1,4 +1,5 @@ import { REVIEW_REFRESH_COOLDOWN_MS } from '../../../../shared/review-refresh-policy' +import type { HostedPRReadJob } from './hosted-pr-read-jobs' import type { GitHubPRRefreshAlias, GitHubPRRefreshCandidate, @@ -7,11 +8,11 @@ import type { import { aliasFromCandidate, bypassesFreshnessDelay, + foregroundExposureDueAt, freshRetryAt, POST_PUSH_DELAY_MS, refreshKey, sameAliasRequestIdentity, - refreshIntervalForCandidate, shouldSkipFresh, visibleFollowUpPriority } from './pr-refresh-candidate-policy' @@ -27,9 +28,11 @@ export type PRRefreshQueueEntry = { bypassBackgroundBudget?: boolean activeDelayNotified?: boolean followUp?: boolean + foreground?: boolean windowId?: number /** A watch job: the PR's current head, or with `headSha` one earlier head's checks. */ watch?: { prKey: string; headSha?: string } + hosted?: HostedPRReadJob } export type PRRefreshEnqueue = { @@ -108,6 +111,10 @@ export class PRRefreshQueue { return this.backgroundNotBefore.get(key)?.requestSequence === requestSequence } + backgroundGateUntil(key: string): number { + return this.backgroundNotBefore.get(key)?.until ?? 0 + } + retimeVisible( candidateFor: (key: string, candidate: GitHubPRRefreshCandidate) => GitHubPRRefreshCandidate ): void { @@ -138,14 +145,7 @@ export class PRRefreshQueue { const key = refreshKey(candidate) const existing = this.entries.get(key) const freshDueAt = shouldSkipFresh(candidate, reason) ? freshRetryAt(candidate) : null - const stopped = !Number.isFinite(refreshIntervalForCandidate(candidate)) - const exposureDueAt = - reexposed && - stopped && - candidate.cachedFetchedAt != null && - Date.now() - candidate.cachedFetchedAt >= REVIEW_REFRESH_COOLDOWN_MS - ? Date.now() - : null + const exposureDueAt = foregroundExposureDueAt(candidate, reexposed) const dueAt = Math.max( exposureDueAt ?? freshDueAt ?? Date.now() + (reason === 'post-push' ? POST_PUSH_DELAY_MS : 0), bypassesFreshnessDelay(reason) ? 0 : (this.backgroundNotBefore.get(key)?.until ?? 0) @@ -163,6 +163,7 @@ export class PRRefreshQueue { dueAt, queuedAt: this.nextOrder(), followUp: reason === 'visible' && freshDueAt !== null && exposureDueAt === null, + foreground: exposureDueAt !== null, windowId }) return { alias, key, dueAt, coalesced: false } @@ -171,11 +172,12 @@ export class PRRefreshQueue { setLiveAlias(existing.aliases, alias) if ( reason === 'visible' && - !shouldSkipFresh(candidate, reason) && + (exposureDueAt !== null || !shouldSkipFresh(candidate, reason)) && existing.reason === 'visible' ) { existing.dueAt = Math.min(existing.dueAt, dueAt) existing.followUp = false + existing.foreground ||= exposureDueAt !== null } const shouldPromote = priority > existing.priority || diff --git a/src/main/github/pull-requests/refresh/pr-refresh-quiet-policy.perf.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-quiet-policy.perf.test.ts new file mode 100644 index 00000000000..4aa8e2d8c4e --- /dev/null +++ b/src/main/github/pull-requests/refresh/pr-refresh-quiet-policy.perf.test.ts @@ -0,0 +1,111 @@ +import { afterEach, expect, it, vi } from 'vitest' +import { makeCandidate, makePR } from './pr-refresh-coordinator-test-harness' + +vi.mock('../../client/lookup/branch-lookup-resolution', () => ({ + resolvePRForBranchOutcome: async () => ({ + kind: 'found', + fetchedAt: Date.now(), + pr: makePR({ + headSha: 'head', + checksStatus: 'success', + checksReadComplete: true, + checksFinal: true, + autoMergeEnabled: false, + mergeQueueRequired: false + }) + }) +})) +vi.mock('../../execution/github-operation-slots', () => ({ + acquire: async () => {}, + release: () => {} +})) + +let release: (() => void) | undefined +afterEach(() => { + release?.() + vi.useRealTimers() + vi.restoreAllMocks() +}) + +it.each([750, 7500])( + 'selects owned quiet policy without inventory discovery at %i workspaces', + async (population) => { + vi.resetModules() + vi.useFakeTimers() + vi.setSystemTime(1_000) + const accounts = await import('../../accounts/github-project-account') + const execution = await import('../../../source-control/code-host-read-execution-scope') + const repository = { + id: 'repo', + path: '/repo', + displayName: 'Repo', + badgeColor: 'blue', + addedAt: 0, + ghAccount: { host: 'github.com', user: 'first' } + } + const inventory = vi.fn(() => + Array.from({ length: population }, (_, row) => ({ repoId: 'repo', path: `/repo/wt-${row}` })) + ) + const metadata = vi.fn(() => ({})) + release = accounts.installGitHubProjectAccounts( + { getRepos: () => [repository], getAllWorktreeMetaForHost: metadata }, + inventory + ) + const discovery = vi.spyOn(accounts, 'resolveGitHubProjectAccount') + const hashing = vi.spyOn(execution, 'codeHostReadExecutionScope') + const { getPRForBranchOutcome } = await import('../../client/lookup/pr-for-branch-outcome') + const { runHostedPRRead, reportVisiblePRRefreshCandidates } = + await import('./pr-refresh-coordinator') + const { refreshIntervalForCandidate } = await import('./pr-refresh-candidate-policy') + const { refreshPRPolicyContext } = await import('../../client/lookup/pr-quiet-policy-evidence') + const candidate = makeCandidate({ + repoId: 'repo', + repoPath: '/repo/wt-0', + branch: 'branch', + currentHeadOid: 'head', + cachedHeadOid: 'head', + cachedFetchedAt: 2_000, + cachedPRState: 'open', + cachedChecksStatus: 'success', + cachedHasPR: true + }) + const read = () => + runHostedPRRead(candidate, () => + getPRForBranchOutcome('/repo/wt-0', 'branch', null, null, null, { currentHeadOid: 'head' }) + ) + await read() + vi.setSystemTime(2_000) + await read() + expect(refreshIntervalForCandidate(candidate)).toBe(300_000) + discovery.mockClear() + hashing.mockClear() + inventory.mockClear() + metadata.mockClear() + reportVisiblePRRefreshCandidates([candidate], 1, 1) + for (let row = 0; row < population; row++) { + refreshIntervalForCandidate(candidate) + } + expect(discovery).not.toHaveBeenCalled() + expect(hashing).not.toHaveBeenCalled() + expect(inventory).not.toHaveBeenCalled() + expect(metadata).not.toHaveBeenCalled() + expect(refreshIntervalForCandidate({ ...candidate, fallbackPRNumber: 9 })).toBe(120_000) + const { bumpScopeGeneration, hostedReviewRepoScope } = + await import('../../../source-control/hosted-review-scope-generations') + bumpScopeGeneration(hostedReviewRepoScope(candidate.repoPath, 'local')) + expect(refreshIntervalForCandidate(candidate)).toBe(120_000) + vi.setSystemTime(3_000) + await read() + vi.setSystemTime(4_000) + await read() + expect(refreshIntervalForCandidate({ ...candidate, cachedFetchedAt: 4_000 })).toBe(300_000) + repository.ghAccount = { host: 'github.com', user: 'second' } + refreshPRPolicyContext() + expect(refreshIntervalForCandidate({ ...candidate, cachedFetchedAt: 4_000 })).toBe(120_000) + vi.setSystemTime(5_000) + await read() + vi.setSystemTime(6_000) + await read() + expect(refreshIntervalForCandidate({ ...candidate, cachedFetchedAt: 6_000 })).toBe(300_000) + } +) diff --git a/src/main/github/pull-requests/refresh/pr-refresh-read-admission.ts b/src/main/github/pull-requests/refresh/pr-refresh-read-admission.ts new file mode 100644 index 00000000000..d6a0f23af2a --- /dev/null +++ b/src/main/github/pull-requests/refresh/pr-refresh-read-admission.ts @@ -0,0 +1,64 @@ +import type { GitHubPRRefreshAlias } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import type { PreparedPRBranchRead } from '../../client/lookup/pr-for-branch-outcome' +import { isBackground, isBudgetedBackground } from './pr-refresh-candidate-policy' +import type { PRRefreshEventPublisher } from './pr-refresh-event-publisher' +import { type PRRefreshPacing, usesActiveRefreshPacing } from './pr-refresh-pacing' +import type { PRRefreshQueue, PRRefreshQueueEntry } from './pr-refresh-queue' +import type { PRRefreshQueueScheduler } from './pr-refresh-queue-scheduler' +import type { PRWatchJobRunner } from './pr-refresh-watch-jobs' + +type PRReadAdmissionOwners = { + queue: PRRefreshQueue + pacing: PRRefreshPacing + events: PRRefreshEventPublisher + scheduler: PRRefreshQueueScheduler + watchJobs: PRWatchJobRunner +} + +export function admitPreparedPRRead( + { queue, pacing, events, scheduler, watchJobs }: PRReadAdmissionOwners, + next: PRRefreshQueueEntry, + prepared: PreparedPRBranchRead, + aliases: GitHubPRRefreshAlias[], + requestSequence: number, + requestStartedAt: number, + joinedRead: boolean +): boolean { + if (!queue.ownsRequest(next.key, requestSequence) || !prepared.isCurrent()) { + next.hosted?.finish(null) + prepared.release() + if (!next.hosted && queue.ownsRequest(next.key, requestSequence)) { + events.broadcast( + { aliases, reason: next.reason, status: 'skipped', skippedReason: 'fresh' }, + requestSequence + ) + if (next.watch) { + watchJobs.skipped(next) + } else { + scheduler.completeWithoutOutcome( + next.key, + next.candidate, + aliases, + next.priority, + next.windowId, + next.foreground + ) + } + } + return false + } + if (scheduler.consumeGatedError(next, prepared, requestSequence, requestStartedAt)) { + prepared.release() + return false + } + if (!next.watch && isBackground(next.reason) && !joinedRead) { + if (next.bypassBackgroundBudget !== true && isBudgetedBackground(next.reason)) { + pacing.noteBackgroundStart(next) + watchJobs.noteBudgetedStart(next) + } + if (usesActiveRefreshPacing(next)) { + pacing.noteActiveStart(next) + } + } + return true +} diff --git a/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts b/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts index 13a29365156..3423968a04c 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts @@ -4,6 +4,8 @@ import type { } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import { lookupBackoffDelayMs } from '../../../source-control/hosted-review-refresh-pacing' import { refreshIntervalForCandidate } from './pr-refresh-candidate-policy' +import type { PreparedPRBranchRead } from '../../client/lookup/pr-for-branch-outcome' +import type { PRRefreshQueue } from './pr-refresh-queue' export class PRRefreshRetryState { private readonly errorBackoff = new Map() @@ -50,4 +52,36 @@ export class PRRefreshRetryState { nextAutoRetryAt: cooldownUntil !== undefined ? Math.max(retryAt, cooldownUntil) : retryAt } } + + errorFollowUpAt( + key: string, + candidate: GitHubPRRefreshCandidate, + outcome: PRRefreshOutcome, + visible: boolean + ): number | undefined { + return outcome.kind === 'upstream-error' + ? (outcome.nextAutoRetryAt ?? + (visible ? this.nextVisibleErrorRetryAt(key, candidate) : undefined)) + : undefined + } + + settleProducerError( + outcome: PRRefreshOutcome, + key: string, + candidate: GitHubPRRefreshCandidate, + prepared: PreparedPRBranchRead, + queue: PRRefreshQueue, + ownsRequest: boolean + ): PRRefreshOutcome { + const retryAt = this.nextVisibleErrorRetryAt(key, candidate) + const result = this.withErrorSchedule(outcome, retryAt) + prepared.scheduleError(outcome, result) + if (ownsRequest) { + queue.protectBackgroundUntil( + key, + Math.max(retryAt, outcome.kind === 'upstream-error' ? (outcome.retryDisabledUntil ?? 0) : 0) + ) + } + return result + } } diff --git a/src/main/github/pull-requests/refresh/pr-refresh-watch-jobs.ts b/src/main/github/pull-requests/refresh/pr-refresh-watch-jobs.ts index 9551d0e28a0..66d6737f246 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-watch-jobs.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-watch-jobs.ts @@ -2,9 +2,11 @@ import type { GitHubPRRefreshCandidate, PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' -import type { PRInfo } from '../../../../shared/github/pull-requests/pull-request-types' +import { prChecksDone } from '../../../../shared/github/pull-requests/refresh/pr-quiet-refresh' +export { prChecksDone } from '../../../../shared/github/pull-requests/refresh/pr-quiet-refresh' import { reviewRefreshIntervalMs } from '../../../../shared/review-refresh-policy' import type { PRRefreshQueue, PRRefreshQueueEntry } from './pr-refresh-queue' +import { isSelectedBudgetedPRRefresh } from './pr-refresh-pacing' import type { PRRefreshRetryState } from './pr-refresh-retry-state' export type PRWatchJob = NonNullable @@ -33,14 +35,6 @@ export function prWatchQueueKey(job: PRWatchJob): string { return job.headSha ? `sha-checks::${job.prKey}::${job.headSha}` : `watch::${job.prKey}` } -/** A complete read with nothing left running, or a complete read with no checks at all. A failed - * check beside a running one is not done, whatever the sidebar's failure-first summary says. */ -export function prChecksDone(pr: PRInfo): boolean { - return ( - pr.checksFinal === true || (pr.checksReadComplete === true && pr.checksSummary?.total === 0) - ) -} - /** A watch's next read: the background cadence, or none once the PR has settled. */ export function prWatchNextDueAt(outcome: PRRefreshOutcome): number | null { if (outcome.kind === 'upstream-error') { @@ -104,7 +98,7 @@ export class PRWatchJobRunner { noteBudgetedStart(entry: PRRefreshQueueEntry): void { // Selected list reads have their own reserved turn; preserve the watch/list alternation. - if (entry.reason !== 'visible' || entry.candidate.isSelected !== true) { + if (!isSelectedBudgetedPRRefresh(entry)) { this.lastBudgetedStartWasWatch = entry.watch !== undefined } } diff --git a/src/main/github/repositories/github-repository-identity.ts b/src/main/github/repositories/github-repository-identity.ts index 360bdd7d285..0e2f1c7640d 100644 --- a/src/main/github/repositories/github-repository-identity.ts +++ b/src/main/github/repositories/github-repository-identity.ts @@ -61,6 +61,15 @@ export async function withGitHubRepoExecution( return operations.run(context, operation) } +export function captureGitHubRepoExecution(): (operation: () => Promise) => Promise { + const context = operations.getStore() + return (operation) => (context ? operations.run(context, operation) : operation()) +} + +export function outsideGitHubRepoExecution(operation: () => T): T { + return operations.exit(operation) +} + export function githubRepoContext( repoPath: string, connectionId?: string | null, diff --git a/src/main/ipc/runtime-environments/requests/runtime-environment-call-queue.ts b/src/main/ipc/runtime-environments/requests/runtime-environment-call-queue.ts index e8cb219318e..c75ac4540f9 100644 --- a/src/main/ipc/runtime-environments/requests/runtime-environment-call-queue.ts +++ b/src/main/ipc/runtime-environments/requests/runtime-environment-call-queue.ts @@ -1,12 +1,13 @@ import { RuntimeRpcCallQueuePool } from '../../../../shared/runtime-rpc-call-queue' -const runtimeCallQueuePool = new RuntimeRpcCallQueuePool() +export const runtimeCallQueuePool = new RuntimeRpcCallQueuePool() export function enqueueRuntimeCall( selector: string, method: string, run: () => Promise, - signal?: AbortSignal + signal?: AbortSignal, + params?: unknown ): Promise { - return runtimeCallQueuePool.enqueue(selector, method, run, 0, signal) + return runtimeCallQueuePool.enqueue(selector, method, run, 0, signal, params) } diff --git a/src/main/ipc/runtime-environments/requests/runtime-environment-subscription-routing.ts b/src/main/ipc/runtime-environments/requests/runtime-environment-subscription-routing.ts new file mode 100644 index 00000000000..8096694a40c --- /dev/null +++ b/src/main/ipc/runtime-environments/requests/runtime-environment-subscription-routing.ts @@ -0,0 +1,182 @@ +import { RemoteRuntimeClientError } from '../../../../shared/remote-runtime/remote-runtime-client-error' +import { throwIfSignalAborted } from '../../../../shared/abort-signal-reason' +import { + type RuntimeEnvironmentSetupFailure, + runtimeEnvironmentSetupFailure +} from '../../../../shared/remote-runtime/host-catalog/runtime-environment-subscription-setup' +import { assertCrewSourceCurrent } from '../subscriptions/runtime-environment-subscription-setup' +import { getPreferredPairedRuntimeConnection } from '../../../../shared/remote-runtime/host-catalog/runtime-environments' +import { ELECTRON_REMOTE_RUNTIME_CLIENT_CAPABILITIES } from '../../../../shared/electron-remote-runtime-client-capabilities' +import { resolveEnvironment } from '../../../../shared/remote-runtime/host-store/runtime-environment-store' +import { + resolveManagedRuntimeEnvironment, + type ManagedRuntimeCallPreparation +} from '../managed-orcad/runtime-environment-managed-tunnel' +import { recordRuntimeEnvironmentUsage } from '../../../../shared/remote-runtime/host-store/runtime-environment-usage-record' +import type * as Rpc from '../../../../shared/runtime-rpc-envelope' +import { subscribeRemoteRuntimeTransport } from '../../../../shared/remote-runtime/requests/remote-runtime-subscription-transport' +import type { RemoteRuntimeTransportSubscription } from '../../../../shared/remote-runtime/requests/remote-runtime-subscription-contract' +import { withRuntimeUnreachableHint } from './runtime-environment-unreachable-hint' +import { decodeRuntimeEnvironmentSubscriptionEvents } from '../subscriptions/runtime-environment-subscription-events' +import { runtimeCallQueuePool } from './runtime-environment-call-queue' +import { isTimedPRRead } from '../../../../shared/runtime-rpc-call-queue' +import { enqueueRuntimeSubscription } from '../../../../shared/runtime-rpc-subscription-queue' +import { + shouldRouteSubscriptionBySupport, + subscribeSupportRoutedRuntimeEnvironment +} from './runtime-environment-support-routing' + +const DEFAULT_REMOTE_RUNTIME_TIMEOUT_MS = 15_000 + +export async function subscribeRuntimeEnvironment( + userDataPath: string, + selector: string, + method: string, + params: unknown, + timeoutMs: number | undefined, + rawCallbacks: { + onEvent: ( + payload: + | { type: 'response'; response: Rpc.RuntimeRpcResponse } + | { type: 'binary'; bytes: Uint8Array } + | { type: 'error'; code: string; message: string } + | { type: 'close' } + ) => void + onClose: () => void + }, + isCurrent: () => boolean = () => true, + signal?: AbortSignal, + preparation?: ManagedRuntimeCallPreparation, + crewAttempt?: { + assertCurrent: () => void + observeFailure: (failure: RuntimeEnvironmentSetupFailure) => void + } +): Promise { + let completeTimed = (): void => {} + const callbacks = { + onEvent: decodeRuntimeEnvironmentSubscriptionEvents( + method, + (event) => { + if (event.type !== 'binary') { + completeTimed() + } + rawCallbacks.onEvent(event) + }, + isCurrent + ), + onClose: () => { + completeTimed() + rawCallbacks.onClose() + } + } + crewAttempt?.assertCurrent() + const environment = crewAttempt + ? resolveEnvironment(userDataPath, selector) + : await resolveManagedRuntimeEnvironment(userDataPath, selector, preparation, method, signal) + crewAttempt?.assertCurrent() + if (crewAttempt) { + assertCrewSourceCurrent(environment) + } + const pairedConnection = getPreferredPairedRuntimeConnection(environment) + const effectiveTimeoutMs = timeoutMs ?? DEFAULT_REMOTE_RUNTIME_TIMEOUT_MS + let markedUsed = false + const markUsedOnce = (runtimeId: string): void => { + if (markedUsed || !isCurrent()) { + return + } + markedUsed = true + recordRuntimeEnvironmentUsage(userDataPath, environment.id, { runtimeId }) + } + const callbacksWithMarkUsed = { + onResponse: (response: Rpc.RuntimeRpcResponse) => { + if ( + crewAttempt && + response._meta?.runtimeId != null && + response._meta.runtimeId !== environment.runtimeId + ) { + throw new RemoteRuntimeClientError( + 'runtime_environment_changed', + 'Runtime environment identity changed; refresh and try again' + ) + } + if (response.ok === true) { + markUsedOnce(response._meta.runtimeId) + } + callbacks.onEvent({ type: 'response' as const, response }) + }, + onBinary: (bytes: Uint8Array) => + callbacks.onEvent({ type: 'binary' as const, bytes }), + onError: (error: { code: string; message: string }) => + callbacks.onEvent({ + type: 'error' as const, + code: error.code, + message: withRuntimeUnreachableHint(environment, error.message, pairedConnection.endpoint) + }), + onClose: () => { + callbacks.onEvent({ type: 'close' as const }) + callbacks.onClose() + } + } + // Initial connection failures reject; midstream errors use onError above. + const connect = async (): Promise => { + try { + if (shouldRouteSubscriptionBySupport(method)) { + return await subscribeSupportRoutedRuntimeEnvironment({ + userDataPath, + environment, + method, + params, + timeoutMs: effectiveTimeoutMs, + callbacks, + isCurrent, + signal + }) + } + const subscription = await subscribeRemoteRuntimeTransport( + pairedConnection, + method, + params, + effectiveTimeoutMs, + callbacksWithMarkUsed, + { clientCapabilities: ELECTRON_REMOTE_RUNTIME_CLIENT_CAPABILITIES, signal } + ) + if (crewAttempt) { + try { + throwIfSignalAborted(signal) + crewAttempt.assertCurrent() + assertCrewSourceCurrent(environment) + } catch (error) { + subscription.close() + throw error + } + } + return subscription + } catch (error) { + if (error instanceof Error && error.name !== 'AbortError') { + error.message = withRuntimeUnreachableHint( + environment, + error.message, + pairedConnection.endpoint + ) + } + crewAttempt?.observeFailure(runtimeEnvironmentSetupFailure(error, 'subscription')) + throw error + } + } + if (!isTimedPRRead(method, params)) { + return connect() + } + const subscription = await enqueueRuntimeSubscription( + runtimeCallQueuePool, + environment.id, + method, + params, + (complete) => { + completeTimed = complete + return connect() + }, + (handle) => handle.close(), + signal + ) + return { ...subscription, close: () => completeTimed() } +} diff --git a/src/main/ipc/runtime-environments/requests/runtime-environment-transport-routing.ts b/src/main/ipc/runtime-environments/requests/runtime-environment-transport-routing.ts index 8190d290334..bfea8ea7b1f 100644 --- a/src/main/ipc/runtime-environments/requests/runtime-environment-transport-routing.ts +++ b/src/main/ipc/runtime-environments/requests/runtime-environment-transport-routing.ts @@ -1,10 +1,3 @@ -import { RemoteRuntimeClientError } from '../../../../shared/remote-runtime/remote-runtime-client-error' -import { throwIfSignalAborted } from '../../../../shared/abort-signal-reason' -import { - type RuntimeEnvironmentSetupFailure, - runtimeEnvironmentSetupFailure -} from '../../../../shared/remote-runtime/host-catalog/runtime-environment-subscription-setup' -import { assertCrewSourceCurrent } from '../subscriptions/runtime-environment-subscription-setup' import { getRuntimeEnvironmentStatus } from '../status/runtime-environment-status-probe' import { getPreferredPairedRuntimeConnection } from '../../../../shared/remote-runtime/host-catalog/runtime-environments' import { ELECTRON_REMOTE_RUNTIME_CLIENT_CAPABILITIES } from '../../../../shared/electron-remote-runtime-client-capabilities' @@ -12,16 +5,12 @@ import { resolveEnvironment } from '../../../../shared/remote-runtime/host-store import { prepareFencedRuntimeRequest } from './runtime-environment-request-preparation' import { requireWslHostWithoutStart, - resolveManagedRuntimeEnvironment, type ManagedRuntimeCallPreparation } from '../managed-orcad/runtime-environment-managed-tunnel' import { recordRuntimeEnvironmentUsage } from '../../../../shared/remote-runtime/host-store/runtime-environment-usage-record' import type * as Rpc from '../../../../shared/runtime-rpc-envelope' import '../../../../shared/remote-runtime/requests/remote-runtime-request-socket' -import { subscribeRemoteRuntimeTransport } from '../../../../shared/remote-runtime/requests/remote-runtime-subscription-transport' -import type { RemoteRuntimeTransportSubscription } from '../../../../shared/remote-runtime/requests/remote-runtime-subscription-contract' import { withRuntimeUnreachableHint } from './runtime-environment-unreachable-hint' -import { decodeRuntimeEnvironmentSubscriptionEvents } from '../subscriptions/runtime-environment-subscription-events' import { enqueueRuntimeCall } from './runtime-environment-call-queue' import { sendRemoteRuntimeConnectionRequestAbortable, @@ -30,9 +19,7 @@ import { import { runtimeEnvironmentRevisionFailure } from './runtime-environment-revision-guard' import { executeSupportRoutedCall, - shouldRouteCallBySupport, - shouldRouteSubscriptionBySupport, - subscribeSupportRoutedRuntimeEnvironment + shouldRouteCallBySupport } from './runtime-environment-support-routing' import { shouldUseSharedControlEnvelope } from '../connections/runtime-environment-shared-control-envelope' import { sendEnvelopedOneShotCall } from './runtime-environment-runtime-pinned-send' @@ -158,7 +145,8 @@ export async function callRuntimeEnvironment( markEnvironmentUsedFromResponse(userDataPath, currentEnvironment.id, response) return response }, - options?.signal + options?.signal, + params ) } catch (error) { if (error instanceof Error && error.name !== 'AbortError') { @@ -168,127 +156,7 @@ export async function callRuntimeEnvironment( } } -export async function subscribeRuntimeEnvironment( - userDataPath: string, - selector: string, - method: string, - params: unknown, - timeoutMs: number | undefined, - rawCallbacks: { - onEvent: ( - payload: - | { type: 'response'; response: Rpc.RuntimeRpcResponse } - | { type: 'binary'; bytes: Uint8Array } - | { type: 'error'; code: string; message: string } - | { type: 'close' } - ) => void - onClose: () => void - }, - isCurrent: () => boolean = () => true, - signal?: AbortSignal, - preparation?: ManagedRuntimeCallPreparation, - crewAttempt?: { - assertCurrent: () => void - observeFailure: (failure: RuntimeEnvironmentSetupFailure) => void - } -): Promise { - const callbacks = { - ...rawCallbacks, - onEvent: decodeRuntimeEnvironmentSubscriptionEvents(method, rawCallbacks.onEvent, isCurrent) - } - crewAttempt?.assertCurrent() - const environment = crewAttempt - ? resolveEnvironment(userDataPath, selector) - : await resolveManagedRuntimeEnvironment(userDataPath, selector, preparation, method, signal) - crewAttempt?.assertCurrent() - if (crewAttempt) { - assertCrewSourceCurrent(environment) - } - const pairedConnection = getPreferredPairedRuntimeConnection(environment) - const effectiveTimeoutMs = timeoutMs ?? DEFAULT_REMOTE_RUNTIME_TIMEOUT_MS - let markedUsed = false - const markUsedOnce = (runtimeId: string): void => { - if (markedUsed || !isCurrent()) { - return - } - markedUsed = true - recordRuntimeEnvironmentUsage(userDataPath, environment.id, { runtimeId }) - } - const callbacksWithMarkUsed = { - onResponse: (response: Rpc.RuntimeRpcResponse) => { - if ( - crewAttempt && - response._meta?.runtimeId != null && - response._meta.runtimeId !== environment.runtimeId - ) { - throw new RemoteRuntimeClientError( - 'runtime_environment_changed', - 'Runtime environment identity changed; refresh and try again' - ) - } - if (response.ok === true) { - markUsedOnce(response._meta.runtimeId) - } - callbacks.onEvent({ type: 'response' as const, response }) - }, - onBinary: (bytes: Uint8Array) => - callbacks.onEvent({ type: 'binary' as const, bytes }), - onError: (error: { code: string; message: string }) => - callbacks.onEvent({ - type: 'error' as const, - code: error.code, - message: withRuntimeUnreachableHint(environment, error.message, pairedConnection.endpoint) - }), - onClose: () => { - callbacks.onEvent({ type: 'close' as const }) - callbacks.onClose() - } - } - // Initial connection failures reject; midstream errors use onError above. - try { - if (shouldRouteSubscriptionBySupport(method)) { - return await subscribeSupportRoutedRuntimeEnvironment({ - userDataPath, - environment, - method, - params, - timeoutMs: effectiveTimeoutMs, - callbacks, - isCurrent, - signal - }) - } - const subscription = await subscribeRemoteRuntimeTransport( - pairedConnection, - method, - params, - effectiveTimeoutMs, - callbacksWithMarkUsed, - { clientCapabilities: ELECTRON_REMOTE_RUNTIME_CLIENT_CAPABILITIES, signal } - ) - if (crewAttempt) { - try { - throwIfSignalAborted(signal) - crewAttempt.assertCurrent() - assertCrewSourceCurrent(environment) - } catch (error) { - subscription.close() - throw error - } - } - return subscription - } catch (error) { - if (error instanceof Error && error.name !== 'AbortError') { - error.message = withRuntimeUnreachableHint( - environment, - error.message, - pairedConnection.endpoint - ) - } - crewAttempt?.observeFailure(runtimeEnvironmentSetupFailure(error, 'subscription')) - throw error - } -} +export { subscribeRuntimeEnvironment } from './runtime-environment-subscription-routing' function markEnvironmentUsedFromResponse( userDataPath: string, diff --git a/src/main/ipc/runtime-environments/runtime-environment-call-queue.test.ts b/src/main/ipc/runtime-environments/runtime-environment-call-queue.test.ts new file mode 100644 index 00000000000..8e1edc94148 --- /dev/null +++ b/src/main/ipc/runtime-environments/runtime-environment-call-queue.test.ts @@ -0,0 +1,19 @@ +import { expect, it } from 'vitest' +import { enqueueRuntimeCall } from './requests/runtime-environment-call-queue' + +it('passes timed intent to the desktop queue without holding status or manual PR reads', async () => { + const release = Promise.withResolvers() + const waiting = [0, 1].map(() => + enqueueRuntimeCall('host', 'github.prForBranch', () => release.promise, undefined, { + reason: 'swr' + }) + ) + await expect( + enqueueRuntimeCall('host', 'github.prForBranch', async () => 'current', undefined, { + reason: 'manual' + }) + ).resolves.toBe('current') + await expect(enqueueRuntimeCall('host', 'git.status', async () => 'clean')).resolves.toBe('clean') + release.resolve() + await Promise.all(waiting) +}) diff --git a/src/main/ipc/runtime-environments/subscriptions/runtime-environments-subscription-lifecycle.test.ts b/src/main/ipc/runtime-environments/subscriptions/runtime-environments-subscription-lifecycle.test.ts index 9f6a0fcbc9c..b56261ab229 100644 --- a/src/main/ipc/runtime-environments/subscriptions/runtime-environments-subscription-lifecycle.test.ts +++ b/src/main/ipc/runtime-environments/subscriptions/runtime-environments-subscription-lifecycle.test.ts @@ -118,6 +118,70 @@ describe('registerRuntimeEnvironmentHandlers', () => { rmSync(userDataPath, { recursive: true, force: true }) }) + it('holds timed signal-route admission after setup while foreground and status subscriptions remain runnable', async () => { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: these handlers use only the fixture settings methods. + registerRuntimeEnvironmentHandlers(store as never) + const finish: (() => void)[] = [] + subscribeRemoteRuntimeRequestMock.mockImplementation( + async (_connection, _method, _params, _timeout, callbacks) => { + finish.push(() => + callbacks.onResponse({ id: 'rpc', ok: true, result: {}, _meta: { runtimeId: 'runtime' } }) + ) + return { requestId: `request-${finish.length}`, close: vi.fn(), sendBinary: vi.fn() } + } + ) + const add = handler<{ name: string; pairingCode: string }, unknown>( + 'runtimeEnvironments:addFromPairingCode' + ) + await add(null, { name: 'desk', pairingCode: pairingCode() }) + const sender = { + id: 1, + isDestroyed: () => false, + send: vi.fn(), + on: vi.fn(), + removeListener: vi.fn() + } + const subscribe = handler< + { + selector: string + method: string + params: { reason?: string; selected?: boolean } + subscriptionId: string + }, + { subscriptionId: string; requestId: string } + >('runtimeEnvironments:subscribe') + const start = (subscriptionId: string, reason: string, method = 'github.prForBranch') => + subscribe( + { sender }, + { selector: 'desk', subscriptionId, method, params: { reason, selected: true } } + ) + await start('timer-1', 'swr') + expect(subscribeRemoteRuntimeRequestMock.mock.calls[0][2]).toMatchObject({ + reason: 'swr', + selected: true + }) + await start('timer-2', 'swr') + const third = start('timer-3', 'swr') + const cancelled = expect(third).rejects.toThrow() + await start('manual', 'manual') + await start('status', '', 'git.status') + await start('review', '', 'hostedReview.forBranch') + expect(finish).toHaveLength(5) + const cancel = handler<{ subscriptionId: string }, unknown>('runtimeEnvironments:unsubscribe') + await cancel({ sender }, { subscriptionId: 'timer-3' }) + await cancelled + const fourth = start('timer-4', 'swr') + finish[0]() + await fourth + expect(finish).toHaveLength(6) + const unsubscribe = handler<{ subscriptionId: string }, unknown>( + 'runtimeEnvironments:unsubscribe' + ) + for (const subscriptionId of ['timer-1', 'timer-2', 'timer-4', 'manual', 'status', 'review']) { + await unsubscribe({ sender }, { subscriptionId }) + } + }) + it('rejects cross-window streaming subscription control', async () => { registerRuntimeEnvironmentHandlers(store as never) const close = vi.fn() diff --git a/src/main/ipc/source-control/github-pr-refresh-handlers.ts b/src/main/ipc/source-control/github-pr-refresh-handlers.ts index 6fd54453d72..b174d20d914 100644 --- a/src/main/ipc/source-control/github-pr-refresh-handlers.ts +++ b/src/main/ipc/source-control/github-pr-refresh-handlers.ts @@ -4,7 +4,7 @@ import type { GitHubPRRefreshCandidate, GitHubPRRefreshEnqueueResult, GitHubPRRefreshReason, - PRRefreshOutcome + PRSnapshotReply } from '../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import type { Repo } from '../../../shared/repo-types' import type { OrcaRuntimeService } from '../../runtime/orca-runtime' @@ -44,8 +44,8 @@ export function registerGitHubPRRefreshHandlers( ): void { // The windows these handlers serve are the renderers the coordinator reports to. setPRRefreshRendererPort(electronPRRefreshRenderer) - function recordPRIfNeeded(repo: Repo, outcome: PRRefreshOutcome): void { - if (outcome.kind === 'found' && !stats.hasCountedPR(outcome.pr.url)) { + function recordPRIfNeeded(repo: Repo, outcome: PRSnapshotReply): void { + if ('kind' in outcome && outcome.kind === 'found' && !stats.hasCountedPR(outcome.pr.url)) { stats.record({ type: 'pr_created', at: Date.now(), diff --git a/src/main/runtime/integrations/reviews/hosted-pr-producer-settlement-test-cases.ts b/src/main/runtime/integrations/reviews/hosted-pr-producer-settlement-test-cases.ts new file mode 100644 index 00000000000..420d5caeb56 --- /dev/null +++ b/src/main/runtime/integrations/reviews/hosted-pr-producer-settlement-test-cases.ts @@ -0,0 +1,235 @@ +import { it, expect, vi, type Mock } from 'vitest' +import { makeCandidate } from '../../../github/pull-requests/refresh/pr-refresh-coordinator-test-harness' +import type { + PRRefreshOutcome, + PRSnapshotReply +} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' + +export function registerPRProducerSettlementTests({ + read, + mocks, + found +}: { + read: ( + branch: string, + reason?: 'active' | 'visible' | 'swr' | 'manual', + head?: string + ) => Promise + mocks: { resolve: Mock; gate: Mock } + found: () => PRRefreshOutcome +}): void { + it('retires a superseded producer error without waiting for or scheduling over fresh work', async () => { + const timedProvider = Promise.withResolvers() + const freshProvider = Promise.withResolvers() + mocks.resolve + .mockReturnValueOnce(timedProvider.promise) + .mockReturnValueOnce(freshProvider.promise) + const timed = read('superseded-error') + await vi.advanceTimersByTimeAsync(0) + const joined = read('superseded-error') + await vi.advanceTimersByTimeAsync(0) + const fresh = read('superseded-error', 'active') + await vi.advanceTimersByTimeAsync(0) + timedProvider.reject(new Error('obsolete network error')) + expect(await Promise.all([timed, joined])).toEqual([ + { revalidation: { invalidated: true, retryAfterMs: 10_000 } }, + { revalidation: { invalidated: true, retryAfterMs: 10_000 } } + ]) + freshProvider.resolve(found()) + const current = await fresh + expect(await read('superseded-error')).toEqual(current) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + }) + + it('delivers the producer error schedule to cold pending consumers before ordinary retry', async () => { + const { addPRRefreshOutcomeObserver } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const cold = read('cold-error') + await vi.advanceTimersByTimeAsync(0) + const joined = read('cold-error') + await vi.advanceTimersByTimeAsync(0) + provider.reject(new Error('network offline')) + const results = await Promise.all([cold, joined]) + expect(results[0]).toMatchObject({ + kind: 'upstream-error', + nextAutoRetryAt: expect.any(Number) + }) + expect(results[1]).toEqual(results[0]) + expect(await read('cold-error')).toEqual(results[0]) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + expect(observe).toHaveBeenCalledTimes(1) + remove() + }) + + it.each(['queued', 'immediate'] as const)( + 'keeps the %s producer policy when another active consumer joins the same task', + async (origin) => { + const { + addPRRefreshOutcomeObserver, + reportVisiblePRRefreshCandidates, + enqueuePRRefresh, + refreshPRNow + } = await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const { setPRRefreshRendererPort } = + await import('../../../github/pull-requests/refresh/pr-refresh-renderer-port') + setPRRefreshRendererPort({ isRendererOpen: () => true, sendToTrustedUI: () => {} }) + const candidate = makeCandidate({ + cacheKey: '/repo::joined-active', + repoId: 'repo', + branch: 'joined-active', + currentHeadOid: 'head-a', + isSelected: true + }) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + try { + mocks.gate.mockResolvedValue(Date.now() + 60_000) + reportVisiblePRRefreshCandidates([candidate], 1, 1) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).not.toHaveBeenCalled() + mocks.gate.mockResolvedValue(null) + let producer: Promise | undefined + if (origin === 'queued') { + enqueuePRRefresh(candidate, 'active', 80) + } else { + producer = refreshPRNow(candidate, 'active') + } + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + const sibling = refreshPRNow(candidate, 'active') + await vi.advanceTimersByTimeAsync(0) + const hosted = read('joined-active') + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + provider.reject(new Error('network offline')) + const result = await hosted + expect(result).toMatchObject({ + kind: 'upstream-error', + nextAutoRetryAt: expect.any(Number) + }) + expect(await sibling).toEqual(result) + if (producer) { + expect(await producer).toEqual(result) + } + expect(await read('joined-active')).toEqual(result) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + expect(observe).toHaveBeenCalledTimes(1) + } finally { + remove() + setPRRefreshRendererPort(null) + } + } + ) + + it.each([false, true])( + 'observes one joined success while fencing a genuine successor (userRefresh=%s)', + async (userRefresh) => { + const { addPRRefreshOutcomeObserver, refreshPRNow } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const candidate = makeCandidate({ + cacheKey: '/repo::joined-success', + repoId: 'repo', + branch: 'joined-success', + currentHeadOid: 'head-a' + }) + const first = Promise.withResolvers() + const successor = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(first.promise).mockReturnValueOnce(successor.promise) + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + try { + const producer = refreshPRNow(candidate, 'active') + await vi.advanceTimersByTimeAsync(0) + const sibling = refreshPRNow(candidate, 'active', { userRefresh }) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(userRefresh ? 2 : 1) + const joined = read('joined-success') + await vi.advanceTimersByTimeAsync(0) + first.resolve(found()) + await producer + if (userRefresh) { + expect(observe).not.toHaveBeenCalled() + successor.resolve(found()) + } + const result = await sibling + expect(await joined).toEqual(result) + expect(result).toMatchObject({ kind: 'found' }) + expect(observe).toHaveBeenCalledTimes(1) + } finally { + remove() + } + } + ) + + it.each(['queued', 'immediate', 'headless-immediate', 'foreground'] as const)( + 'settles the %s producer before its hosted pending error consumer', + async (origin) => { + const { addPRRefreshOutcomeObserver, reportVisiblePRRefreshCandidates, refreshPRNow } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const { setPRRefreshRendererPort } = + await import('../../../github/pull-requests/refresh/pr-refresh-renderer-port') + if (origin === 'queued' || origin === 'immediate') { + setPRRefreshRendererPort({ isRendererOpen: () => true, sendToTrustedUI: () => {} }) + } + const immediate = origin === 'immediate' || origin === 'headless-immediate' + const candidate = makeCandidate({ + cacheKey: '/repo::origin-error', + repoId: 'repo', + branch: 'origin-error', + currentHeadOid: 'head-a', + isSelected: true + }) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + try { + let producer: Promise | undefined + if (origin === 'foreground') { + producer = read('origin-error', 'active') + } else { + if (immediate) { + mocks.gate.mockResolvedValue(Date.now() + 60_000) + } + reportVisiblePRRefreshCandidates([candidate], 1, 1) + await vi.advanceTimersByTimeAsync(0) + if (immediate) { + expect(mocks.resolve).not.toHaveBeenCalled() + mocks.gate.mockResolvedValue(null) + producer = refreshPRNow(candidate, 'manual') + } + } + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + const joined = read('origin-error') + await vi.advanceTimersByTimeAsync(0) + provider.reject(new Error('network offline')) + const result = await joined + expect(result).toMatchObject({ kind: 'upstream-error' }) + if (!('kind' in result) || result.kind !== 'upstream-error') { + throw new Error('expected the actual provider failure') + } + if (origin === 'foreground' || origin === 'headless-immediate') { + expect(result.nextAutoRetryAt).toBeUndefined() + } else { + expect(result.nextAutoRetryAt).toEqual(expect.any(Number)) + } + if (producer) { + expect(await producer).toEqual(result) + } + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + expect(observe).toHaveBeenCalledTimes(1) + } finally { + remove() + setPRRefreshRendererPort(null) + } + } + ) +} diff --git a/src/main/runtime/integrations/reviews/hosted-pr-selected-demand.test.ts b/src/main/runtime/integrations/reviews/hosted-pr-selected-demand.test.ts new file mode 100644 index 00000000000..d241badea7a --- /dev/null +++ b/src/main/runtime/integrations/reviews/hosted-pr-selected-demand.test.ts @@ -0,0 +1,157 @@ +import { expect, it, vi } from 'vitest' +import { commands, found, mocks, read, rpcRead } from './runtime-hosted-pr-snapshot-test-harness' +import { PrForBranch } from '../../../../shared/rpc-contract/github/github-pull-request-params' + +it('promotes a shared selected demand without losing other rows or renewing its attempt', async () => { + const branches = ['ordinary-a', 'selected-a', 'selected-b'] + await Promise.all(branches.map((branch) => read(branch, 'active'))) + await vi.advanceTimersByTimeAsync(60_000) + // ast-grep-ignore: test-no-wall-clock -- The imported runtime fixture installs the fake clock. + const gate = Date.now() + 60_000 + mocks.gate.mockResolvedValue(gate) + const starts: string[] = [] + mocks.resolve.mockImplementation(async ({ branchName }) => { + starts.push(branchName) + return found() + }) + const demand = (branch: string, selected = false) => + rpcRead({ + repo: 'repo', + branch, + currentHeadOid: 'head-a', + reason: 'swr', + acceptRevalidationOnly: true, + selected + }) + const waiting = branches.map((branch) => demand(branch)) + await vi.advanceTimersByTimeAsync(25_000) + await Promise.all(waiting) + expect(starts).toEqual([]) + await demand('selected-a', true) + await demand('selected-b', true) + await demand('selected-a', false) + const { schedulePRWatchRead } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const { makeCandidate } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator-test-harness') + schedulePRWatchRead( + { prKey: 'github.com/acme/repo#12' }, + makeCandidate({ + repoId: 'repo', + branch: 'watched', + cacheKey: '/repo::watched', + currentHeadOid: 'head-a' + }), + gate + ) + mocks.gate.mockResolvedValue(null) + await vi.advanceTimersByTimeAsync(65_000) + expect(starts).toEqual(['selected-a', 'ordinary-a', 'selected-b', 'watched']) + + // A selected request stays finite even when its only waiter detaches. + await read('expires', 'active') + await vi.advanceTimersByTimeAsync(60_000) + // ast-grep-ignore: test-no-wall-clock -- The imported runtime fixture installs the fake clock. + mocks.gate.mockResolvedValue(Date.now() + 7 * 60_000) + const abort = new AbortController() + const expires = commands.getRepoPRForBranch( + 'repo', + 'expires', + null, + null, + false, + 'head-a', + 'swr', + false, + undefined, + abort.signal, + true, + true + ) + const cancelled = expect(expires).rejects.toThrow('cancelled') + await vi.advanceTimersByTimeAsync(0) + abort.abort() + await cancelled + await vi.advanceTimersByTimeAsync(4 * 60_000) + await demand('expires', true) + const beforeExpiry = starts.length + await vi.advanceTimersByTimeAsync(4 * 60_000) + expect(starts.length).toBe(beforeExpiry) +}) + +it('keeps selected preference optional for old snapshot request parsers', () => { + const params = { repo: 'repo', branch: 'main', reason: 'swr', selected: true } + expect(PrForBranch.parse(params)).toMatchObject({ selected: true }) + expect(PrForBranch.omit({ selected: true }).parse(params)).toEqual({ + repo: 'repo', + branch: 'main', + reason: 'swr' + }) + expect(PrForBranch.parse({ repo: 'repo', branch: 'main' })).toEqual({ + repo: 'repo', + branch: 'main' + }) +}) + +it('keeps a selected join during an awaited quota probe and its later paused admission', async () => { + await read('quiet-selected', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const confirmation = read('quiet-selected') + await vi.advanceTimersByTimeAsync(0) + await confirmation + const branches = ['ordinary-a', 'ordinary-b', 'ordinary-c'] + await Promise.all(branches.map((branch) => read(branch, 'active'))) + await vi.advanceTimersByTimeAsync(300_000) + const quotaProbe = Promise.withResolvers() + // ast-grep-ignore: test-no-wall-clock -- The imported runtime fixture installs the fake clock. + const resetAt = Date.now() + 60_000 + mocks.gate.mockResolvedValue(resetAt).mockReturnValueOnce(quotaProbe.promise) + const starts: string[] = [] + mocks.resolve.mockImplementation(async ({ branchName }) => { + starts.push(branchName) + return found() + }) + const demand = (branch: string, selected = false) => + rpcRead({ + repo: 'repo', + branch, + currentHeadOid: 'head-a', + reason: 'swr', + acceptRevalidationOnly: true, + selected + }) + const first = demand('quiet-selected') + await vi.advanceTimersByTimeAsync(0) + expect(mocks.gate).toHaveBeenCalled() + expect(starts).toEqual([]) + const joined = demand('quiet-selected', true) + const ordinary = branches.map((branch) => demand(branch)) + await vi.advanceTimersByTimeAsync(0) + quotaProbe.resolve(resetAt) + await vi.advanceTimersByTimeAsync(25_000) + await Promise.all([first, joined, ...ordinary]) + mocks.gate.mockResolvedValue(null) + await vi.advanceTimersByTimeAsync(65_000) + expect(starts).toEqual(['quiet-selected', 'ordinary-a', 'ordinary-b', 'ordinary-c']) +}) + +it('uses selected demand only for admission priority, preserving the shared retry interval', async () => { + await read('retry-policy', 'active') + await vi.advanceTimersByTimeAsync(60_000) + mocks.resolve.mockRejectedValueOnce(new Error('offline')) + // ast-grep-ignore: test-no-wall-clock -- The imported runtime fixture installs the fake clock. + const requestedAt = Date.now() + const response = rpcRead({ + repo: 'repo', + branch: 'retry-policy', + currentHeadOid: 'head-a', + reason: 'swr', + acceptRevalidationOnly: true, + selected: true + }) + await vi.advanceTimersByTimeAsync(0) + expect(await response).toMatchObject({ + kind: 'upstream-error', + nextAutoRetryAt: requestedAt + 120_000 + }) +}) diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-pr-deadlines.test.ts b/src/main/runtime/integrations/reviews/runtime-hosted-pr-deadlines.test.ts new file mode 100644 index 00000000000..a12fd68ed62 --- /dev/null +++ b/src/main/runtime/integrations/reviews/runtime-hosted-pr-deadlines.test.ts @@ -0,0 +1,142 @@ +import { expect, it, vi } from 'vitest' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { makeCandidate } from '../../../github/pull-requests/refresh/pr-refresh-coordinator-test-harness' +import { fixture, found, mocks, read, rpcRead } from './runtime-hosted-pr-snapshot-test-harness' + +it('delivers post-push data from its fresh producer while an older timed producer is pending', async () => { + const coordinator = await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const old = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(old.promise) + const producer = read('pushed', 'swr') + await vi.advanceTimersByTimeAsync(0) + const delivered: PRRefreshOutcome[] = [] + const remove = coordinator.addPRRefreshOutcomeObserver((_candidate, outcome) => + delivered.push(outcome) + ) + try { + fixture.title = 'after push' + coordinator.enqueuePRRefresh( + makeCandidate({ branch: 'pushed', cacheKey: '/repo::pushed', currentHeadOid: 'head-a' }), + 'post-push' + ) + await vi.advanceTimersByTimeAsync(2_500) + expect(delivered).toContainEqual({ + kind: 'found', + fetchedAt: 3_500, + pr: expect.objectContaining({ title: 'after push' }) + }) + old.resolve(found()) + await producer + } finally { + remove() + } +}) + +it('delivers an unrelated row without a spacing delay after joining an existing producer', async () => { + const coordinator = await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const result = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(result.promise) + const producer = read('joining', 'active') + await vi.advanceTimersByTimeAsync(0) + const delivered: { branch: string; outcome: PRRefreshOutcome }[] = [] + const remove = coordinator.addPRRefreshOutcomeObserver((candidate, outcome) => { + delivered.push({ branch: candidate.branch, outcome }) + }) + try { + for (const branch of ['joining', 'unrelated']) { + coordinator.enqueuePRRefresh( + makeCandidate({ branch, cacheKey: `/repo::${branch}`, currentHeadOid: 'head-a' }), + 'swr' + ) + } + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'shared answer' + result.resolve(found()) + await producer + await vi.advanceTimersByTimeAsync(0) + expect(delivered).toContainEqual({ + branch: 'unrelated', + outcome: { + kind: 'found', + fetchedAt: 1_000, + pr: expect.objectContaining({ title: 'shared answer' }) + } + }) + } finally { + remove() + } +}) + +it.each([false, true])( + 'delivers post-push data while another row waits for a later rate-limit reset (clock jump: %s)', + async (clockJump) => { + const coordinator = await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const delivered: PRRefreshOutcome[] = [] + const remove = coordinator.addPRRefreshOutcomeObserver((_candidate, outcome) => + delivered.push(outcome) + ) + const candidate = (branch: string) => + makeCandidate({ + branch, + cacheKey: `/repo::${branch}`, + currentHeadOid: 'head-a' + }) + try { + fixture.title = 'after push' + mocks.gate.mockImplementation(async (row) => (row.branch === 'later' ? 61_000 : null)) + if (clockJump) { + await coordinator.refreshPRNow(candidate('later')) + vi.setSystemTime(301_000) + } + coordinator.enqueuePRRefresh(candidate('pushed'), 'post-push') + await vi.advanceTimersByTimeAsync(0) + if (!clockJump) { + await coordinator.refreshPRNow(candidate('later')) + } + await vi.advanceTimersByTimeAsync(2_500) + expect(delivered).toContainEqual({ + kind: 'found', + fetchedAt: clockJump ? 303_500 : 3_500, + pr: expect.objectContaining({ title: 'after push' }) + }) + } finally { + remove() + } + } +) + +it.each(['deadline', 'abort'] as const)( + 'detaches an automatic cold-read waiter on %s while its producer completes for another client', + async (end) => { + const result = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(result.promise) + const producer = read('pending', 'active') + await vi.advanceTimersByTimeAsync(0) + const controller = new AbortController() + const joined = rpcRead( + { + repo: 'repo', + branch: 'pending', + currentHeadOid: 'head-a', + reason: 'swr', + acceptRevalidationOnly: true + }, + controller.signal + ) + const abandoned = + end === 'abort' + ? expect(joined).rejects.toThrow('cancelled') + : expect(joined).resolves.toMatchObject({ revalidation: { retryAfterMs: 10_000 } }) + await vi.advanceTimersByTimeAsync(0) + if (end === 'abort') { + controller.abort() + } else { + await vi.advanceTimersByTimeAsync(25_000) + } + await abandoned + fixture.title = 'producer completed' + result.resolve(found()) + await expect(producer).resolves.toMatchObject({ pr: { title: 'producer completed' } }) + await expect(read('pending')).resolves.toMatchObject({ pr: { title: 'producer completed' } }) + } +) diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-pr-owner-delivery.test.ts b/src/main/runtime/integrations/reviews/runtime-hosted-pr-owner-delivery.test.ts new file mode 100644 index 00000000000..244cfff8ffd --- /dev/null +++ b/src/main/runtime/integrations/reviews/runtime-hosted-pr-owner-delivery.test.ts @@ -0,0 +1,105 @@ +import { expect, it, vi } from 'vitest' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { commands, fixture, found, mocks, read } from './runtime-hosted-pr-snapshot-test-harness' + +it('returns completed legacy cold and pending replies without fallback IO or a successor wait', async () => { + const { preparePRBranchRead } = + await import('../../../github/client/lookup/pr-for-branch-outcome') + const { addPRRefreshOutcomeObserver } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const releases: (() => void)[] = [] + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + try { + for (let index = 0; index < 256; index++) { + const branch = `protected-${index}` + await read(branch, 'active') + const prepared = await preparePRBranchRead('/repo', branch, null, null, null, { + currentHeadOid: 'head-a' + }) + releases.push(prepared.hold()) + } + mocks.resolve.mockClear() + observe.mockClear() + for (const freshFirst of [true, false]) { + const branch = freshFirst ? 'legacy-finished-successor' : 'legacy-running-successor' + const old = Promise.withResolvers() + const fresh = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(old.promise).mockReturnValueOnce(fresh.promise) + const legacy = () => + commands.getRepoPRForBranch('repo', branch, null, null, false, 'head-a', 'swr') + const first = legacy() + await vi.advanceTimersByTimeAsync(0) + const sibling = legacy() + await vi.advanceTimersByTimeAsync(0) + const foreground = read(branch, 'active') + await vi.advanceTimersByTimeAsync(0) + const delivered = vi.fn() + const completed = Promise.all([first, sibling]).then(delivered) + try { + expect(await read(branch)).toEqual({ + revalidation: { invalidated: true, retryAfterMs: 10_000 } + }) + if (freshFirst) { + fresh.resolve(found()) + await foreground + } + const own: PRRefreshOutcome = { kind: 'no-pr', fetchedAt: 1_000 } + old.resolve(own) + await vi.advanceTimersByTimeAsync(0) + expect(delivered).toHaveBeenCalledWith([own, own]) + } finally { + old.resolve({ kind: 'no-pr', fetchedAt: 1_000 }) + fresh.resolve(found()) + await Promise.all([completed, foreground]) + } + } + expect(mocks.resolve).toHaveBeenCalledTimes(4) + expect(observe).toHaveBeenCalledTimes(2) + } finally { + for (const release of releases) { + release() + } + remove() + } + fixture.title = 'legitimate automatic read' + expect(await read('after-capacity')).toMatchObject({ pr: { title: 'legitimate automatic read' } }) +}) + +it.each(['generation-cold', 'account-cold', 'account-direct'] as const)( + 'fences %s settlement before obsolete error policy and observation', + async (change) => { + const { addPRRefreshOutcomeObserver, _getPRRefreshErrorBackoffCountForTests } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const { invalidateHostedReviewBranchCache } = + await import('../../../source-control/hosted-review-branch-cache') + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + const provider = Promise.withResolvers() + mocks.account.mockReturnValue({ ghAccount: { host: 'github.com', user: 'before' } }) + mocks.resolve.mockReturnValueOnce(provider.promise) + const pending = read('scope', change === 'account-direct' ? 'active' : 'swr') + await vi.advanceTimersByTimeAsync(0) + if (change === 'generation-cold') { + invalidateHostedReviewBranchCache('/repo', 'local') + } else { + mocks.account.mockReturnValue({ ghAccount: { host: 'github.com', user: 'after' } }) + } + try { + provider.reject(new Error('network offline')) + const result = await pending + if (change === 'account-direct') { + expect(result).toMatchObject({ kind: 'upstream-error' }) + } else { + expect(result).toEqual({ revalidation: { invalidated: true, retryAfterMs: 10_000 } }) + } + expect(observe).not.toHaveBeenCalled() + expect(_getPRRefreshErrorBackoffCountForTests()).toBe(0) + fixture.title = 'current scope' + expect(await read('scope', 'active')).toMatchObject({ pr: { title: 'current scope' } }) + expect(observe).toHaveBeenCalledTimes(1) + } finally { + remove() + } + } +) diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshot-test-harness.ts b/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshot-test-harness.ts new file mode 100644 index 00000000000..0f617d38b8a --- /dev/null +++ b/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshot-test-harness.ts @@ -0,0 +1,110 @@ +// oxlint-disable-next-line typescript/triple-slash-reference -- Renderer integration tests also compile this main fixture with its ambient build constants. +/// +import { afterEach, beforeEach, vi, type Mock } from 'vitest' +import { makePR } from '../../../github/pull-requests/refresh/pr-refresh-coordinator-test-harness' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import type { RuntimeHostedReviewCommands } from './runtime-hosted-review-commands' +import type { RpcContext } from '../../rpc/core' + +const mocks: { resolve: Mock; account: Mock; gate: Mock } = vi.hoisted(() => ({ + resolve: vi.fn(), + account: vi.fn(), + gate: vi.fn() +})) +export { mocks } +vi.mock('../../../github/accounts/github-project-account', () => ({ + githubPRPolicyProjects: () => ({ source: null, selection: String(mocks.account()) }), + resolveGitHubProjectAccount: mocks.account +})) +vi.mock('../../../github/execution/github-operation-slots', () => ({ + acquire: async () => {}, + release: () => {} +})) +vi.mock('../../../github/client/lookup/branch-lookup-resolution', () => ({ + resolvePRForBranchOutcome: mocks.resolve +})) +vi.mock('../../../github/pull-requests/refresh/pr-refresh-rate-limit-gate', () => ({ + prRefreshRateLimitPausedUntil: mocks.gate +})) + +export let commands: RuntimeHostedReviewCommands +export const fixture = { title: 'before' } +export const found = (): PRRefreshOutcome => ({ + kind: 'found', + fetchedAt: Date.now(), + pr: makePR({ + title: fixture.title, + headSha: 'head-a', + checksStatus: 'success', + checksReadComplete: true, + checksFinal: true, + autoMergeEnabled: false, + mergeQueueRequired: false + }) +}) +export const read = ( + branch: string, + reason: 'active' | 'visible' | 'swr' | 'manual' = 'swr', + head = 'head-a' +) => + commands.getRepoPRForBranch( + 'repo', + branch, + null, + null, + false, + head, + reason, + undefined, + undefined, + undefined, + true + ) + +beforeEach(async () => { + vi.resetModules() + vi.useFakeTimers() + vi.setSystemTime(1_000) + fixture.title = 'before' + mocks.resolve.mockReset().mockImplementation(async () => found()) + mocks.account.mockReset().mockReturnValue(null) + mocks.gate.mockReset().mockResolvedValue(null) + // Resolve the real handler before fake-clock assertions begin. + await import('../../rpc/methods/source-control/github/github-pull-request-methods') + const { RuntimeHostedReviewCommands: Commands } = await import('./runtime-hosted-review-commands') + const repo = { + id: 'repo', + path: '/repo', + kind: 'git' as const, + displayName: 'repo', + badgeColor: 'blue', + addedAt: 0 + } + commands = new Commands({ + resolveRepo: async () => repo, + resolveTarget: async () => ({ repo, repoPath: repo.path }), + getExecutionOptions: () => undefined, + recordCreated: () => {} + }) +}) +afterEach(() => { + vi.clearAllTimers() + vi.useRealTimers() +}) + +export async function rpcRead(params: unknown, signal?: AbortSignal) { + const { GITHUB_PULL_REQUEST_METHODS } = + await import('../../rpc/methods/source-control/github/github-pull-request-methods') + const { PrForBranch } = + await import('../../../../shared/rpc-contract/github/github-pull-request-params') + const { repo, branch, ...options } = PrForBranch.parse(params) + const parsed = { repo, branch, ...options } + const runtime = { getRepoPRForBranch: commands.getRepoPRForBranch.bind(commands) } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: this handler only needs the bound real PR command and signal. + const context = { runtime, signal } as unknown as RpcContext + const method = GITHUB_PULL_REQUEST_METHODS.find((entry) => entry.name === 'github.prForBranch') + if (!method) { + throw new Error('PR RPC method is missing') + } + return method.handler(parsed, context) +} diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshots.test.ts b/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshots.test.ts new file mode 100644 index 00000000000..5b078a94251 --- /dev/null +++ b/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshots.test.ts @@ -0,0 +1,576 @@ +import { describe, expect, it, vi } from 'vitest' +import { makeCandidate } from '../../../github/pull-requests/refresh/pr-refresh-coordinator-test-harness' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { registerPRProducerSettlementTests } from './hosted-pr-producer-settlement-test-cases' +import { commands, fixture, found, mocks, read } from './runtime-hosted-pr-snapshot-test-harness' + +describe('hosted PR snapshots', () => { + it('shares true-aged timer data while legacy discovery and actions read current data', async () => { + await read('shared', 'active') + fixture.title = 'after' + expect(await read('shared')).toMatchObject({ + kind: 'found', + pr: { title: 'before' }, + fetchedAt: 1_000 + }) + vi.setSystemTime(2_000) + expect(await read('shared', 'visible')).toMatchObject({ + kind: 'found', + pr: { title: 'after' }, + fetchedAt: 2_000 + }) + fixture.title = 'manual' + expect(await read('shared', 'manual')).toMatchObject({ kind: 'found', pr: { title: 'manual' } }) + }) + + it('keeps a confirmed unchanged quiet result, but a different request head reads again', async () => { + await read('quiet', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const confirmation = read('quiet') + await vi.advanceTimersByTimeAsync(0) + expect(await confirmation).toMatchObject({ fetchedAt: 61_000 }) + await vi.advanceTimersByTimeAsync(120_000) + fixture.title = 'new provider state' + expect(await read('quiet')).toMatchObject({ pr: { title: 'before' }, fetchedAt: 61_000 }) + expect(await read('quiet', 'swr', 'head-b')).toMatchObject({ + pr: { title: 'new provider state' }, + fetchedAt: 181_000 + }) + }) + + it('returns completed checks in each due response for three queued rows', async () => { + await Promise.all(['one', 'two', 'three'].map((branch) => read(branch, 'active'))) + fixture.title = 'finished checks' + mocks.resolve.mockImplementation( + () => + new Promise((resolve) => { + // ast-grep-ignore: test-no-real-time-wait -- Fake-clock provider delay exercises host admission and completion. + setTimeout(() => resolve(found()), 1_000) + }) + ) + await vi.advanceTimersByTimeAsync(60_000) + const responses = Promise.all(['one', 'two', 'three'].map((branch) => read(branch))) + await vi.advanceTimersByTimeAsync(21_000) + expect(await responses).toEqual([ + expect.objectContaining({ pr: expect.objectContaining({ title: 'finished checks' }) }), + expect.objectContaining({ pr: expect.objectContaining({ title: 'finished checks' }) }), + expect.objectContaining({ pr: expect.objectContaining({ title: 'finished checks' }) }) + ]) + }) + + it('delivers a deferred provider failure to the next timer instead of reviving old success', async () => { + await read('slow', 'active') + const provider = Promise.withResolvers() + mocks.resolve.mockImplementation(() => provider.promise) + await vi.advanceTimersByTimeAsync(60_000) + const waiting = read('slow') + await vi.advanceTimersByTimeAsync(25_000) + expect(await waiting).toMatchObject({ + kind: 'found', + fetchedAt: 1_000, + revalidation: { retryAfterMs: 10_000 } + }) + provider.reject(new Error('network down')) + await vi.advanceTimersByTimeAsync(0) + expect(await read('slow')).toMatchObject({ kind: 'upstream-error' }) + mocks.resolve.mockImplementation(async () => found()) + fixture.title = 'manual recovery' + expect(await read('slow', 'manual')).toMatchObject({ pr: { title: 'manual recovery' } }) + }) + + it('retrieves a queued completion on its short retry after local work delays admission', async () => { + await Promise.all(['one', 'two', 'three'].map((branch) => read(branch, 'active'))) + mocks.resolve.mockImplementation( + ({ branchName }) => + new Promise((resolve) => { + // ast-grep-ignore: test-no-real-time-wait -- Fake-clock provider delay exercises host admission and completion. + setTimeout(() => resolve(found()), branchName === 'blocking' ? 6_000 : 1_000) + }) + ) + await vi.advanceTimersByTimeAsync(60_000) + const { enqueuePRRefresh } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + enqueuePRRefresh(makeCandidate({ branch: 'blocking' }), 'active') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'delivered' + const responses = Promise.all(['one', 'two', 'three'].map((branch) => read(branch))) + await vi.advanceTimersByTimeAsync(25_000) + expect(await responses).toMatchObject([ + { pr: { title: 'delivered' } }, + { pr: { title: 'delivered' } }, + { pr: { title: 'before' }, revalidation: { retryAfterMs: 10_000 } } + ]) + await vi.advanceTimersByTimeAsync(10_000) + expect(await read('three')).toMatchObject({ pr: { title: 'delivered' }, fetchedAt: 88_000 }) + }) + + it('returns a no-data retry after the project account changes', async () => { + mocks.account.mockReturnValue({ ghAccount: { host: 'github.com', user: 'before' } }) + await read('account', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const gate = Promise.withResolvers() + mocks.gate.mockReturnValue(gate.promise) + const waiting = read('account') + await vi.advanceTimersByTimeAsync(0) + mocks.account.mockReturnValue({ ghAccount: { host: 'github.com', user: 'after' } }) + gate.resolve(null) + await vi.advanceTimersByTimeAsync(0) + expect(await waiting).toEqual({ revalidation: { invalidated: true, retryAfterMs: 10_000 } }) + fixture.title = 'new account' + expect(await read('account')).toMatchObject({ pr: { title: 'new account' } }) + }) + + it('cancels one client wait while another receives the shared provider result', async () => { + await read('cancel', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValue(provider.promise) + const controller = new AbortController() + const cancelled = commands.getRepoPRForBranch( + 'repo', + 'cancel', + null, + null, + false, + 'head-a', + 'swr', + false, + undefined, + controller.signal, + true + ) + const rejected = expect(cancelled).rejects.toThrow('cancelled') + const remaining = read('cancel') + await vi.advanceTimersByTimeAsync(0) + controller.abort() + await rejected + fixture.title = 'shared completion' + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await remaining).toMatchObject({ pr: { title: 'shared completion' } }) + }) + + it('returns a receipt to excess waiters and keeps the shared result after cancellation', async () => { + await read('many', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValue(provider.promise) + const controllers = Array.from({ length: 256 }, () => new AbortController()) + const waiting = controllers.map((controller) => + commands.getRepoPRForBranch( + 'repo', + 'many', + null, + null, + false, + 'head-a', + 'swr', + false, + undefined, + controller.signal, + true + ) + ) + const cancellations = Promise.allSettled(waiting) + await vi.advanceTimersByTimeAsync(0) + expect(await read('many')).toMatchObject({ + fetchedAt: 1_000, + revalidation: { retryAfterMs: 10_000 } + }) + for (const controller of controllers) { + controller.abort() + } + await cancellations + fixture.title = 'completed without waiters' + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await read('many')).toMatchObject({ pr: { title: 'completed without waiters' } }) + }) + + it('lets a rate-limited job expire without starting it after demand stops', async () => { + await read('lease', 'active') + await vi.advanceTimersByTimeAsync(60_000) + mocks.gate.mockResolvedValue(Date.now() + 900_000) + const waiting = read('lease') + await vi.advanceTimersByTimeAsync(25_000) + expect(await waiting).toMatchObject({ + fetchedAt: 1_000, + revalidation: { retryAfterMs: 875_000 } + }) + fixture.title = 'not requested' + await vi.advanceTimersByTimeAsync(900_000) + const { readRecentPhonePROutcome } = + await import('../../../github/client/lookup/pr-for-branch-outcome') + expect( + await readRecentPhonePROutcome('/repo', 'lease', null, null, null, { + currentHeadOid: 'head-a' + }) + ).toBeNull() + mocks.gate.mockResolvedValue(null) + expect(await read('lease', 'manual')).toMatchObject({ pr: { title: 'not requested' } }) + }) + it('retains the exact late error and budget gate after narrowed and other-head writes, then recovers', async () => { + await read('retained', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const waiting = read('retained') + await vi.advanceTimersByTimeAsync(25_000) + expect(await waiting).toMatchObject({ revalidation: { retryAfterMs: 10_000 } }) + provider.resolve({ + kind: 'upstream-error', + errorType: 'network', + message: 'offline', + fetchedAt: Date.now() + }) + await vi.advanceTimersByTimeAsync(0) + const { getPRForBranchOutcome } = + await import('../../../github/client/lookup/pr-for-branch-outcome') + await getPRForBranchOutcome('/repo', 'retained', null, null, null, { + currentHeadOid: 'head-a', + includeDerivedData: false + }) + await read('retained', 'active', 'head-b') + const before = mocks.resolve.mock.calls.length + const error = await read('retained') + expect(error).toMatchObject({ + kind: 'upstream-error', + message: 'offline', + nextAutoRetryAt: expect.any(Number) + }) + expect(mocks.resolve).toHaveBeenCalledTimes(before) + if (!('kind' in error) || error.kind !== 'upstream-error') { + throw new Error('expected the actual provider failure') + } + await vi.advanceTimersByTimeAsync(error.nextAutoRetryAt! - Date.now()) + fixture.title = 'recovered' + const recovered = read('retained') + await vi.advanceTimersByTimeAsync(0) + expect(await recovered).toMatchObject({ kind: 'found', pr: { title: 'recovered' } }) + }) + + it('delivers its exact completion when another full identity advances the phone projection', async () => { + await read('identities', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const waiting = read('identities') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'other head' + await read('identities', 'active', 'head-b') + fixture.title = 'original exact completion' + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await waiting).toMatchObject({ + kind: 'found', + pr: { title: 'original exact completion' } + }) + }) + + it('shares overlapping fresh discovery across clients without joining older timed work', async () => { + mocks.resolve.mockImplementation(async () => { + // ast-grep-ignore: test-no-real-time-wait -- Fake-clock provider delay exercises host admission and completion. + await new Promise((resolve) => setTimeout(resolve, 1_000)) + return found() + }) + const discoveries = Promise.all( + Array.from({ length: 2 }, () => + Promise.all(Array.from({ length: 14 }, (_, row) => read(`discovery-${row}`, 'active'))) + ) + ) + await vi.advanceTimersByTimeAsync(1_000) + await discoveries + expect(mocks.resolve).toHaveBeenCalledTimes(14) + await vi.advanceTimersByTimeAsync(60_000) + const slow = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(slow.promise) + const timed = read('discovery-0') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'fresh action' + const actions = Promise.all([read('discovery-0', 'active'), read('discovery-0', 'active')]) + await vi.advanceTimersByTimeAsync(1_000) + expect(await actions).toMatchObject([ + { pr: { title: 'fresh action' } }, + { pr: { title: 'fresh action' } } + ]) + expect(mocks.resolve).toHaveBeenCalledTimes(16) + fixture.title = 'obsolete timer' + slow.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await timed).toMatchObject({ pr: { title: 'fresh action' } }) + expect(await read('discovery-0')).toMatchObject({ pr: { title: 'fresh action' } }) + }) + + it.each([true, false])( + 'does not restore full content after a narrowed observation (warm=%s)', + async (warm) => { + if (warm) { + await read('fenced', 'active') + await vi.advanceTimersByTimeAsync(60_000) + } + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const timed = read('fenced') + await vi.advanceTimersByTimeAsync(0) + const joined = read('fenced') + await vi.advanceTimersByTimeAsync(0) + const { getPRForBranchOutcome, readRecentPhonePROutcome } = + await import('../../../github/client/lookup/pr-for-branch-outcome') + await getPRForBranchOutcome('/repo', 'fenced', null, null, null, { + currentHeadOid: 'head-a', + includeDerivedData: false + }) + fixture.title = 'obsolete full content' + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await Promise.all([timed, joined])).toEqual([ + { revalidation: { invalidated: true, retryAfterMs: 10_000 } }, + { revalidation: { invalidated: true, retryAfterMs: 10_000 } } + ]) + expect(mocks.resolve).toHaveBeenCalledTimes(warm ? 3 : 2) + expect( + await readRecentPhonePROutcome('/repo', 'fenced', null, null, null, { + currentHeadOid: 'head-a' + }) + ).toBeNull() + fixture.title = 'new foreground observation' + expect(await read('fenced', 'active')).toMatchObject({ + pr: { title: 'new foreground observation' } + }) + } + ) + + it('delivers a retained cold no-PR completion to both timed consumers', async () => { + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const cold = read('cold-absence') + await vi.advanceTimersByTimeAsync(0) + const joined = read('cold-absence') + await vi.advanceTimersByTimeAsync(0) + provider.resolve({ kind: 'no-pr', fetchedAt: 1_000 }) + expect(await Promise.all([cold, joined])).toEqual([ + { kind: 'no-pr', fetchedAt: 1_000 }, + { kind: 'no-pr', fetchedAt: 1_000 } + ]) + }) + + registerPRProducerSettlementTests({ read, mocks, found }) + + it('delivers the current result to cold timers and their pending joiners after a fresh successor', async () => { + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const cold = read('cold-successor') + await vi.advanceTimersByTimeAsync(0) + const joined = read('cold-successor') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'authoritative foreground' + await read('cold-successor', 'active') + fixture.title = 'superseded cold content' + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await Promise.all([cold, joined])).toMatchObject([ + { kind: 'found', pr: { title: 'authoritative foreground' } }, + { kind: 'found', pr: { title: 'authoritative foreground' } } + ]) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + }) + + it('gives a superseded timer a short retry until another client fresh read completes', async () => { + await read('successor', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const timedProvider = Promise.withResolvers() + const freshProvider = Promise.withResolvers() + mocks.resolve + .mockReturnValueOnce(timedProvider.promise) + .mockReturnValueOnce(freshProvider.promise) + const timed = read('successor') + await vi.advanceTimersByTimeAsync(0) + const fresh = read('successor', 'active') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'superseded timer' + timedProvider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await timed).toMatchObject({ + kind: 'found', + pr: { title: 'before' }, + fetchedAt: 1_000, + revalidation: { retryAfterMs: 10_000 } + }) + fixture.title = 'another client fresh result' + freshProvider.resolve(found()) + await fresh + const starts = mocks.resolve.mock.calls.length + expect(await read('successor')).toMatchObject({ pr: { title: 'another client fresh result' } }) + expect(mocks.resolve).toHaveBeenCalledTimes(starts) + }) + + it('retains an in-flight timer failure when a new branch fills the exact-result cache', async () => { + for (let branch = 0; branch < 256; branch += 1) { + await read(`capacity-${branch}`, 'active') + } + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const waiting = read('capacity-0') + await vi.advanceTimersByTimeAsync(25_000) + expect(await waiting).toMatchObject({ revalidation: { retryAfterMs: 10_000 } }) + await read('capacity-new', 'active') + provider.resolve({ + kind: 'upstream-error', + errorType: 'network', + message: 'retained late failure', + fetchedAt: Date.now() + }) + await vi.advanceTimersByTimeAsync(0) + const starts = mocks.resolve.mock.calls.length + expect(await read('capacity-0')).toMatchObject({ + kind: 'upstream-error', + message: 'retained late failure', + nextAutoRetryAt: expect.any(Number) + }) + expect(mocks.resolve).toHaveBeenCalledTimes(starts) + const error = await read('capacity-0') + if (!('kind' in error) || error.kind !== 'upstream-error') { + throw new Error('expected the retained capacity failure') + } + await vi.advanceTimersByTimeAsync(error.nextAutoRetryAt! - Date.now()) + const pausedUntil = Date.now() + 60_000 + mocks.gate.mockResolvedValue(pausedUntil) + const gated = read('capacity-0') + await vi.advanceTimersByTimeAsync(25_000) + expect(await gated).toMatchObject({ kind: 'upstream-error', message: 'retained late failure' }) + expect(mocks.resolve).toHaveBeenCalledTimes(starts) + fixture.title = 'capacity recovery' + mocks.gate.mockResolvedValue(null) + await vi.advanceTimersByTimeAsync(35_000) + expect(await read('capacity-0')).toMatchObject({ + kind: 'found', + pr: { title: 'capacity recovery' } + }) + }) + + it('keeps unrelated timer data after many branch watches invalidate their own content', async () => { + const { getPRForBranchOutcome } = + await import('../../../github/client/lookup/pr-for-branch-outcome') + const watch = (branch: string) => + getPRForBranchOutcome('/repo', branch, null, null, null, { + currentHeadOid: 'head-a', + includeDerivedData: false + }) + for (let branch = 0; branch < 256; branch += 1) { + await watch(`watch-${branch}`) + } + await read('unrelated', 'active') + await watch('watch-0') + await watch('watch-256') + fixture.title = 'should wait for its own refresh' + const starts = mocks.resolve.mock.calls.length + expect(await read('unrelated')).toMatchObject({ + kind: 'found', + pr: { title: 'before' }, + fetchedAt: 1_000 + }) + expect(mocks.resolve).toHaveBeenCalledTimes(starts) + }) + + it('refuses extra automatic retention while protected reads fill capacity but leaves foreground usable', async () => { + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValue(provider.promise) + const foreground = Array.from({ length: 256 }, (_, branch) => + read(`protected-${branch}`, 'active') + ) + await vi.advanceTimersByTimeAsync(0) + expect(await read('excess')).toEqual({ + revalidation: { invalidated: true, retryAfterMs: 10_000 } + }) + expect(mocks.resolve).toHaveBeenCalledTimes(256) + mocks.resolve.mockImplementation(async () => found()) + fixture.title = 'foreground still available' + expect(await read('excess', 'active')).toMatchObject({ + pr: { title: 'foreground still available' } + }) + provider.resolve(found()) + await Promise.all(foreground) + expect(await read('excess')).toMatchObject({ kind: 'found' }) + }) + + it.each(['runtime', 'local'] as const)( + 'observes one provider read when %s starts the local/runtime overlap', + async (first) => { + const { addPRRefreshOutcomeObserver, enqueuePRRefresh } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const { setPRRefreshRendererPort } = + await import('../../../github/pull-requests/refresh/pr-refresh-renderer-port') + const observe = vi.fn() + const broadcast = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + setPRRefreshRendererPort({ isRendererOpen: () => true, sendToTrustedUI: broadcast }) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValue(provider.promise) + const local = () => + enqueuePRRefresh( + makeCandidate({ + cacheKey: '/repo::observed', + repoId: 'repo', + branch: 'observed', + currentHeadOid: 'head-a' + }), + 'active' + ) + try { + let runtime: ReturnType + if (first === 'runtime') { + runtime = read('observed', 'active') + await vi.advanceTimersByTimeAsync(0) + local() + await vi.advanceTimersByTimeAsync(0) + } else { + local() + await vi.advanceTimersByTimeAsync(0) + runtime = read('observed', 'active') + await vi.advanceTimersByTimeAsync(0) + } + fixture.title = 'shared observable result' + provider.resolve(found()) + expect(await runtime).toMatchObject({ pr: { title: 'shared observable result' } }) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + expect(observe).toHaveBeenCalledTimes(1) + expect(broadcast).toHaveBeenCalledWith( + 'gh:prRefreshEvent', + expect.objectContaining({ + aliases: expect.arrayContaining([ + expect.objectContaining({ cacheKey: '/repo::observed' }) + ]), + outcome: expect.objectContaining({ + kind: 'found', + pr: expect.objectContaining({ title: 'shared observable result' }) + }) + }) + ) + } finally { + remove() + setPRRefreshRendererPort(null) + } + } + ) + + it('returns legacy timed cache immediately and never sends a receipt-only reply', async () => { + await read('legacy', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const legacy = await commands.getRepoPRForBranch( + 'repo', + 'legacy', + null, + null, + false, + 'head-a', + 'swr' + ) + expect(legacy).toMatchObject({ kind: 'found', fetchedAt: 1_000 }) + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + }) +}) diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-review-commands.ts b/src/main/runtime/integrations/reviews/runtime-hosted-review-commands.ts index 6a14b007a13..48c026c69f0 100644 --- a/src/main/runtime/integrations/reviews/runtime-hosted-review-commands.ts +++ b/src/main/runtime/integrations/reviews/runtime-hosted-review-commands.ts @@ -1,18 +1,25 @@ import { - getPRForBranchOutcome, + preparePRBranchRead, readRecentPhonePROutcome } from '../../../github/client/lookup/pr-for-branch-outcome' import type { GitHubRepositoryIdentity as GitHubOwnerRepo } from '../../../../shared/github/repositories/repository-identity' import type { GitHubPRRefreshReason, - PRRefreshOutcome + PRSnapshotReply } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import type { Repo } from '../../../../shared/repo-types' import type { DeviceScope } from '../../../../shared/runtime-session-contracts' import { getRepoSlug, getRepoUpstream } from '../../../github/client/fetch/repo-slug-upstream' -import { admissionTierForRefreshReason } from '../../../github/pull-requests/refresh/pr-refresh-candidate-policy' +import { + admissionTierForRefreshReason, + bypassesFreshnessDelay +} from '../../../github/pull-requests/refresh/pr-refresh-candidate-policy' import type { GitHubPRBranchLookupOptions } from '../../../github/client/lookup/pull-request-lookup-data' -import { runHostedPRRead } from '../../../github/pull-requests/refresh/pr-refresh-coordinator' +import { + runHostedPRRead, + runHostedPRSnapshot +} from '../../../github/pull-requests/refresh/pr-refresh-coordinator' +import { HOSTED_PR_COMPLETION_WAIT_MS } from '../../../github/pull-requests/refresh/hosted-pr-read-jobs' import { type CreateHostedReviewInput, type CreateHostedReviewResult, @@ -78,8 +85,12 @@ export class RuntimeHostedReviewCommands { currentHeadOid?: string | null, reason?: GitHubPRRefreshReason, userRefresh?: boolean, - clientKind?: DeviceScope - ): Promise { + clientKind?: DeviceScope, + signal?: AbortSignal, + acceptRevalidationOnly = false, + selected = false + ): Promise { + const deadline = Date.now() + HOSTED_PR_COMPLETION_WAIT_MS const repo = await this.deps.resolveRepo(repoSelector) const lookupOptions: GitHubPRBranchLookupOptions = { ...this.deps.getExecutionOptions( @@ -90,16 +101,17 @@ export class RuntimeHostedReviewCommands { if (userRefresh === true) { lookupOptions.userRefresh = true } + if (reason && bypassesFreshnessDelay(reason)) { + lookupOptions.resetQuietBackoff = true + } if (acceptMergedFallbackPR === true) { lookupOptions.acceptMergedFallbackPR = true } if (typeof currentHeadOid === 'string' && currentHeadOid.trim().length > 0) { lookupOptions.currentHeadOid = currentHeadOid.trim() } - const lookupOptionArgs: [] | [GitHubPRBranchLookupOptions] = - Object.keys(lookupOptions).length > 0 ? [lookupOptions] : [] const connectionId = hostedReviewSshConnectionId(getStoredRepoExecutionHostId(repo)) - // Desktop callers and reason-carrying refreshes keep their own GitHub read. + // Legacy phone reads retain their narrower thirty-second reuse contract. if (clientKind === 'mobile' && reason === undefined) { const recent = await readRecentPhonePROutcome( repo.path, @@ -113,27 +125,31 @@ export class RuntimeHostedReviewCommands { return recent } } - // Through the coordinator, so a client's read also serves this server's CI-line watches. - return runHostedPRRead( - { - cacheKey: `${repo.path}::${branch}`, - repoKind: repo.kind ?? 'git', - repoId: repo.id, - repoPath: repo.path, - branch, - connectionId, - linkedPRNumber: linkedPRNumber ?? null - }, - () => - getPRForBranchOutcome( - repo.path, - branch, - linkedPRNumber ?? null, - connectionId, - linkedPRNumber == null ? (fallbackPRNumber ?? null) : null, - ...lookupOptionArgs - ) + const candidate = { + cacheKey: `${repo.path}::${branch}`, + repoKind: repo.kind ?? ('git' as const), + repoId: repo.id, + repoPath: repo.path, + branch, + connectionId, + currentHeadOid: lookupOptions.currentHeadOid ?? null, + linkedPRNumber: linkedPRNumber ?? null, + fallbackPRNumber: linkedPRNumber == null ? (fallbackPRNumber ?? null) : null, + localGitOptions: lookupOptions.localGitExecOptions, + isSelected: reason === 'swr' && userRefresh !== true && selected + } + const prepared = await preparePRBranchRead( + repo.path, + branch, + linkedPRNumber ?? null, + connectionId, + linkedPRNumber == null ? (fallbackPRNumber ?? null) : null, + lookupOptions ) + if (reason === 'swr' && userRefresh !== true) { + return runHostedPRSnapshot(candidate, prepared, deadline, signal, acceptRevalidationOnly) + } + return runHostedPRRead(candidate, prepared) } async getHostedReviewForBranch(args: { diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-review-pr-reuse.test.ts b/src/main/runtime/integrations/reviews/runtime-hosted-review-pr-reuse.test.ts index caa4161c0a1..5da571c10ae 100644 --- a/src/main/runtime/integrations/reviews/runtime-hosted-review-pr-reuse.test.ts +++ b/src/main/runtime/integrations/reviews/runtime-hosted-review-pr-reuse.test.ts @@ -1,3 +1,4 @@ +import type { PreparedPRBranchRead } from '../../../github/client/lookup/pr-for-branch-outcome' import type * as GithubRepositoryIdentityModule from '../../../github/repositories/github-repository-identity' import { beforeEach, describe, expect, it, vi } from 'vitest' import type { @@ -31,7 +32,10 @@ vi.mock('../../../github/client/lookup/branch-lookup-resolution', () => ({ resolvePRForBranchOutcome: mocks.resolve })) vi.mock('../../../github/pull-requests/refresh/pr-refresh-coordinator', () => ({ - runHostedPRRead: (_candidate: unknown, read: () => Promise) => read() + runHostedPRRead: ( + _candidate: unknown, + read: (() => Promise) | PreparedPRBranchRead + ) => (typeof read === 'function' ? read() : read.read()) })) import { RuntimeHostedReviewCommands } from './runtime-hosted-review-commands' @@ -153,6 +157,6 @@ describe('phone PR lookups reuse the desktop’s last result', () => { userRefresh, clientKind ) - expect(outcome.kind === 'found' ? outcome.pr.title : null).toBe(expected) + expect('kind' in outcome && outcome.kind === 'found' ? outcome.pr.title : null).toBe(expected) }) }) diff --git a/src/main/runtime/rpc/methods/source-control/github/github-pull-request-methods.ts b/src/main/runtime/rpc/methods/source-control/github/github-pull-request-methods.ts index 0c2c03dbfa0..924b2cf2bbe 100644 --- a/src/main/runtime/rpc/methods/source-control/github/github-pull-request-methods.ts +++ b/src/main/runtime/rpc/methods/source-control/github/github-pull-request-methods.ts @@ -16,7 +16,7 @@ export const GITHUB_PULL_REQUEST_METHODS = [ name: 'github.prForBranch', permission: 'workspace', params: PrForBranch, - handler: async (params, { runtime, clientKind }) => + handler: async (params, { runtime, clientKind, signal }) => runtime.getRepoPRForBranch( params.repo, params.branch, @@ -26,7 +26,10 @@ export const GITHUB_PULL_REQUEST_METHODS = [ params.currentHeadOid, params.reason, params.userRefresh, - clientKind + clientKind, + signal, + params.acceptRevalidationOnly, + params.selected ) }), defineMethod({ diff --git a/src/preload/api/github/github-pull-request-api.ts b/src/preload/api/github/github-pull-request-api.ts index c763ebb7798..0a56df1cf50 100644 --- a/src/preload/api/github/github-pull-request-api.ts +++ b/src/preload/api/github/github-pull-request-api.ts @@ -14,7 +14,7 @@ import type { GitHubPRRefreshEnqueueResult, GitHubPRRefreshEvent, GitHubPRRefreshReason, - PRRefreshOutcome + PRSnapshotReply } from '../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import type { GitHubRepositoryIdentity as GitHubOwnerRepo } from '../../../shared/github/repositories/repository-identity' import type { @@ -52,7 +52,7 @@ export type GithubPullRequestApi = { candidate: GitHubPRRefreshCandidate reason?: GitHubPRRefreshReason userRefresh?: boolean - }) => Promise + }) => Promise enqueuePRRefresh: (args: { candidate: GitHubPRRefreshCandidate reason: GitHubPRRefreshReason diff --git a/src/renderer/src/store/github/github-slice-contract.ts b/src/renderer/src/store/github/github-slice-contract.ts index 182a10164f0..450eaa75f38 100644 --- a/src/renderer/src/store/github/github-slice-contract.ts +++ b/src/renderer/src/store/github/github-slice-contract.ts @@ -9,6 +9,7 @@ import type { } from '../../../../shared/github/comments/comment-types' import type { GitHubPRRefreshEvent, + PRSnapshotReply, GitHubPRRefreshReason } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import type { GitHubRepositoryIdentity as GitHubOwnerRepo } from '../../../../shared/github/repositories/repository-identity' @@ -70,6 +71,9 @@ export type GitHubSlice = { fallbackPRSource?: GitHubPRFallbackSource | null reason?: GitHubPRRefreshReason userRefresh?: boolean + selected?: boolean + onReadOutcome?: (outcome: PRSnapshotReply) => void + signal?: AbortSignal } ) => Promise fetchIssue: ( diff --git a/src/renderer/src/store/github/pull-requests/github-pr-request-lifetime.test.ts b/src/renderer/src/store/github/pull-requests/github-pr-request-lifetime.test.ts index e97a508a13f..1663d6d6673 100644 --- a/src/renderer/src/store/github/pull-requests/github-pr-request-lifetime.test.ts +++ b/src/renderer/src/store/github/pull-requests/github-pr-request-lifetime.test.ts @@ -5,7 +5,8 @@ import { makePR, mockApi, resetRemoteRuntimeMocks, - runtimeEnvironmentCall + runtimeEnvironmentCall, + runtimeEnvironmentSubscribe } from '../github-slice-test-harness' beforeEach(() => { @@ -23,7 +24,7 @@ afterEach(() => { vi.restoreAllMocks() }) -function found(number: number): PRRefreshOutcome { +function found(number: number): Extract { return { kind: 'found', pr: makePR({ number }), fetchedAt: Date.now() } } @@ -116,3 +117,163 @@ describe('pull-request lookup ownership after newer settlement', () => { } ) }) + +it.each(['post-push', 'active'] as const)( + 'keeps %s fresh while a timed snapshot is waiting', + async (reason) => { + const store = createTestStore() + store.setState({ + repos: [ + { + id: 'runtime-repo', + path: '/runtime/repo', + displayName: 'Runtime repo', + badgeColor: 'blue', + addedAt: 1, + executionHostId: 'runtime:env-1' + } + ] + }) + const timed = Promise.withResolvers() + const action = Promise.withResolvers() + const actionStarted = Promise.withResolvers() + runtimeEnvironmentCall.mockImplementation(async (args) => { + const isAction = + typeof args.params === 'object' && + args.params !== null && + 'reason' in args.params && + args.params.reason === reason + if (isAction) { + actionStarted.resolve() + } + return { + id: 'rpc', + ok: true, + result: await (isAction ? action.promise : timed.promise) + } + }) + let timerReceipt: unknown + const timer = store.getState().fetchPRForBranch('/runtime/repo', 'intent', { + force: true, + reason: 'swr', + onReadOutcome: (outcome) => { + timerReceipt = outcome + } + }) + await vi.advanceTimersByTimeAsync(0) + const refresh = store + .getState() + .fetchPRForBranch('/runtime/repo', 'intent', { force: true, reason }) + await actionStarted.promise + action.resolve(found(20)) + await refresh + timed.resolve({ ...found(10), revalidation: { retryAfterMs: 10_000 } }) + await timer + expect(store.getState().prCache['runtime:env-1::runtime-repo::intent']?.data?.number).toBe(20) + expect(timerReceipt).toBeUndefined() + } +) + +it('handles negotiated invalidation before data or linked-PR settlement', async () => { + const store = createTestStore() + store.setState({ + repos: [ + { + id: 'runtime-repo', + path: '/runtime/repo', + displayName: 'Runtime repo', + badgeColor: 'blue', + addedAt: 1, + executionHostId: 'runtime:env-1' + } + ] + }) + runtimeEnvironmentCall.mockResolvedValue({ id: 'rpc', ok: true, result: found(20) }) + await store + .getState() + .fetchPRForBranch('/runtime/repo', 'receipt', { force: true, reason: 'active' }) + const before = store.getState().prCache['runtime:env-1::runtime-repo::receipt'] + const onReadOutcome = vi.fn() + runtimeEnvironmentCall.mockResolvedValue({ + id: 'rpc', + ok: true, + result: { revalidation: { invalidated: true, retryAfterMs: 10_000 } } + }) + await store + .getState() + .fetchPRForBranch('/runtime/repo', 'receipt', { force: true, reason: 'swr', onReadOutcome }) + expect(onReadOutcome).toHaveBeenCalledWith({ + revalidation: { invalidated: true, retryAfterMs: 10_000 } + }) + expect(store.getState().prCache['runtime:env-1::runtime-repo::receipt']).toBe(before) + expect(store.getState().prRefreshStates['runtime:env-1::runtime-repo::receipt']).toBeUndefined() + expect(runtimeEnvironmentCall.mock.calls.at(-1)?.[0].params).toMatchObject({ + reason: 'swr', + acceptRevalidationOnly: true + }) + runtimeEnvironmentCall.mockResolvedValue({ id: 'rpc', ok: true, result: found(30).pr }) + await store + .getState() + .fetchPRForBranch('/runtime/repo', 'receipt', { force: true, reason: 'swr' }) + expect(store.getState().prCache['runtime:env-1::runtime-repo::receipt']?.data?.number).toBe(30) + runtimeEnvironmentCall.mockResolvedValue({ id: 'rpc', ok: true, result: null }) + await store + .getState() + .fetchPRForBranch('/runtime/repo', 'receipt', { force: true, reason: 'active' }) + expect(store.getState().prCache['runtime:env-1::runtime-repo::receipt']?.data).toBeNull() +}) + +it.each(['origin', 'joiner'] as const)( + 'detaches an aborted %s while the live consumer receives the shared result', + async (cancelled) => { + const store = createTestStore() + store.setState({ + repos: [ + { + id: 'runtime-repo', + path: '/runtime/repo', + displayName: 'Runtime repo', + badgeColor: 'blue', + addedAt: 1, + executionHostId: 'runtime:env-1' + } + ] + }) + const reply = Promise.withResolvers() + runtimeEnvironmentCall.mockImplementation(async () => ({ + id: 'rpc', + ok: true, + result: await reply.promise + })) + const origin = new AbortController() + const joiner = new AbortController() + const originOutcome = vi.fn() + const joinerOutcome = vi.fn() + const first = store.getState().fetchPRForBranch('/runtime/repo', 'shared-cancel', { + force: true, + reason: 'active', + signal: origin.signal, + onReadOutcome: originOutcome + }) + await vi.advanceTimersByTimeAsync(0) + const second = store.getState().fetchPRForBranch('/runtime/repo', 'shared-cancel', { + force: true, + reason: 'active', + signal: joiner.signal, + onReadOutcome: joinerOutcome + }) + ;(cancelled === 'origin' ? origin : joiner).abort() + expect(await (cancelled === 'origin' ? first : second)).toBeNull() + reply.resolve(found(30)) + expect((await (cancelled === 'origin' ? second : first))?.number).toBe(30) + expect( + store.getState().prCache['runtime:env-1::runtime-repo::shared-cancel']?.data?.number + ).toBe(30) + expect(cancelled === 'origin' ? originOutcome : joinerOutcome).not.toHaveBeenCalled() + expect(cancelled === 'origin' ? joinerOutcome : originOutcome).toHaveBeenCalledWith( + expect.objectContaining({ kind: 'found', pr: expect.objectContaining({ number: 30 }) }) + ) + expect(runtimeEnvironmentCall).toHaveBeenCalledTimes(1) + expect(runtimeEnvironmentSubscribe).toHaveBeenCalledTimes(1) + } +) diff --git a/src/renderer/src/store/github/pull-requests/pull-request-actions.ts b/src/renderer/src/store/github/pull-requests/pull-request-actions.ts index 8280dca0ec6..4d7e04d136f 100644 --- a/src/renderer/src/store/github/pull-requests/pull-request-actions.ts +++ b/src/renderer/src/store/github/pull-requests/pull-request-actions.ts @@ -8,14 +8,24 @@ import { findRepoForGitHubOwner } from '../repositories/repository-routing' import '../cache/cache-identity' import { getGitHubRepoCacheKey } from '../../../../../shared/github/cache/github-cache-key' import { isFresh } from '../cache/cache-policy' -import { buildGitHubPRRefreshStateClearToken } from '../refresh/pr-refresh-state' +import { + buildGitHubPRRefreshStateClearToken, + bypassesGitHubPRRefreshFreshness +} from '../refresh/pr-refresh-state' import { githubHostedReviewFallbackPRNumber, prLookupHintKey } from './pr-result-routing' import { nextLookupGeneration } from '../../slices/hosted-review/refresh/lookup-generation-sequence' -import { inflightPRRequests, prRequestGenerations } from '../requests/request-coordination' +import { + inflightPRRequests, + prRequestGenerations, + type InflightPR +} from '../requests/request-coordination' +import { sharedPromiseSettlementWaiters } from '../../../../../shared/promise-settlement-waiters' import { getGitHubSourceTarget, settingsForGitHubRepoOwner } from '../work-items/work-item-routing' import { captureLinkedReviewClearScope, clearLinkedReview } from './linked-review-clear' import { shouldClearDivergedLinkedMergedPR } from '../../../../../shared/github/pull-requests/refresh/linked-pr-staleness' import { startPullRequestLookup } from './pull-request-execution' +import { findWorktreeById } from '../refresh/worktree-refresh' +import { getRepoExecutionHostId } from '../../../../../shared/execution-host' export const createPullRequestActions = ( set: Parameters>[0], @@ -40,6 +50,9 @@ export const createPullRequestActions = ( repo !== undefined ) const cached = get().prCache[cacheKey] + if (options?.signal?.aborted) { + return cached?.data ?? null + } const hostedReviewCacheKey = getHostedReviewCacheKey( repoPath, branch, @@ -68,7 +81,16 @@ export const createPullRequestActions = ( ? null : (options?.fallbackPRSource ?? (explicitFallbackPRNumber != null ? 'explicit' : 'hosted-review')) - const lookupHintKey = prLookupHintKey(linkedPRNumber, fallbackPRNumber) + const lookupHintKey = JSON.stringify([ + prLookupHintKey(linkedPRNumber, fallbackPRNumber), + options?.worktreeId + ? (findWorktreeById( + get(), + options.worktreeId, + repo ? getRepoExecutionHostId(repo) : undefined + )?.head ?? null) + : null + ]) const linkedRefetch = cached?.data === null && (linkedPRNumber !== null || fallbackPRNumber !== null) if (!options?.force && !options?.userRefresh && !linkedRefetch && isFresh(cached)) { @@ -102,13 +124,25 @@ export const createPullRequestActions = ( if ( !options?.userRefresh && inflightRequest && + !inflightRequest.controller.signal.aborted && (!options?.force || inflightRequest.force) && inflightRequest.lookupHintKey === lookupHintKey && + !( + inflightRequest.reason === 'swr' && + bypassesGitHubPRRefreshFreshness(options?.reason ?? 'manual') + ) && !linkedRefetch ) { - return inflightRequest.promise + return waitForPullRequest( + inflightRequest, + options, + () => get().prCache[cacheKey]?.data ?? null + ) } + if (inflightRequest) { + inflightRequest.superseded = true + } const generation = nextLookupGeneration() const requestStartedAt = Date.now() const requestStartedHostedReviewEntry = get().hostedReviewCache[hostedReviewCacheKey] @@ -120,12 +154,13 @@ export const createPullRequestActions = ( ) prRequestGenerations.set(cacheKey, generation) + const controller = new AbortController() const request = startPullRequestLookup({ set, get, repoPath, branch, - options, + options: { ...options, signal: controller.signal, onReadOutcome: undefined }, repo, repoId, requestSettings, @@ -141,12 +176,44 @@ export const createPullRequestActions = ( requestStartedPRRefreshToken }) - inflightPRRequests.set(cacheKey, { + const inflight: InflightPR = { promise: request, + controller, + waiters: sharedPromiseSettlementWaiters(request), force: Boolean(options?.force), generation, + reason: options?.reason, lookupHintKey - }) - return request + } + inflightPRRequests.set(cacheKey, inflight) + return waitForPullRequest(inflight, options, () => get().prCache[cacheKey]?.data ?? null) } }) + +function waitForPullRequest( + inflight: InflightPR, + options: Parameters[2], + fallback: () => PRInfo | null +): Promise { + return inflight.waiters + .wait({ + signal: options?.signal, + deferSettled: true, + onFulfilled: () => { + if (inflight.outcome && !inflight.superseded) { + options?.onReadOutcome?.(inflight.outcome) + } + }, + onAbandon: () => { + if (inflight.waiters.waiterCount === 0) { + inflight.controller.abort() + } + } + }) + .catch((error: unknown) => { + if (options?.signal?.aborted) { + return fallback() + } + throw error + }) +} diff --git a/src/renderer/src/store/github/pull-requests/pull-request-execution.ts b/src/renderer/src/store/github/pull-requests/pull-request-execution.ts index 317b4d28b4c..3315464036a 100644 --- a/src/renderer/src/store/github/pull-requests/pull-request-execution.ts +++ b/src/renderer/src/store/github/pull-requests/pull-request-execution.ts @@ -14,12 +14,13 @@ import { import type { GitHubPRRefreshCandidate, - PRRefreshOutcome + PRSnapshotReply } from '../../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import { normalizeGitHubPRForBranchOutcome, githubPRTransportFailureOutcome } from '../../../../../shared/github/pull-requests/refresh/pull-request-for-branch-outcome' +import { isPRRevalidationOnly } from '../../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import { getRepoExecutionHostId } from '../../../../../shared/execution-host' import { callRuntimeRpc } from '../../../runtime/rpc/calls/runtime-rpc-call' import { inflightPRRequests, prRequestGenerations } from '../requests/request-coordination' @@ -95,7 +96,7 @@ export function startPullRequestLookup(args: { const requestHeadOid = candidateWorktree?.head ?? null let transportFailed = false const response = runtimeRepo - ? callRuntimeRpc( + ? callRuntimeRpc( runtimeRepo.target, 'github.prForBranch', { @@ -104,13 +105,23 @@ export function startPullRequestLookup(args: { linkedPRNumber, currentHeadOid: requestHeadOid, ...(options?.reason ? { reason: options.reason } : {}), + ...(options?.reason === 'swr' + ? { + acceptRevalidationOnly: true, + ...(options.userRefresh !== true && options.selected === true + ? { selected: true } + : {}) + } + : {}), ...(options?.userRefresh === true ? { userRefresh: true } : {}), ...(fallbackPRNumber !== null ? { fallbackPRNumber, acceptMergedFallbackPR: fallbackPRSource !== null } : {}) }, - { timeoutMs: 30_000, ...checkContract.sourceRpcOptions } - ).then((result) => normalizeGitHubPRForBranchOutcome(result)) + { timeoutMs: 30_000, signal: options?.signal, ...checkContract.sourceRpcOptions } + ).then((result) => + isPRRevalidationOnly(result) ? result : normalizeGitHubPRForBranchOutcome(result) + ) : (async () => { const candidate: GitHubPRRefreshCandidate = { repoId: repoId ?? '', @@ -136,15 +147,26 @@ export function startPullRequestLookup(args: { ...requestOptions, repoOwnerExecutionHostId: options?.repoOwnerExecutionHostId }) - return normalizeGitHubPRForBranchOutcome(response) + return isPRRevalidationOnly(response) + ? response + : normalizeGitHubPRForBranchOutcome(response) })() const outcome = await response.catch(() => { transportFailed = true return githubPRTransportFailureOutcome() }) - if (!checkContract.sourceCurrent()) { + if (!checkContract.sourceCurrent() || options?.signal?.aborted) { return cached?.data ?? null } + if (prRequestGenerations.get(cacheKey) === generation) { + const inflight = inflightPRRequests.get(cacheKey) + if (inflight?.generation === generation) { + inflight.outcome = outcome + } + } + if (isPRRevalidationOnly(outcome)) { + return get().prCache[cacheKey]?.data ?? null + } const pr = outcome.kind === 'found' ? outcome.pr : null let acceptedPR: PRInfo | null = null // RPC and lost IPC replies get no coordinator settlement; a lost reply is reported only to the click that asked, so polls stay silent. diff --git a/src/renderer/src/store/github/refresh/refresh-event-actions.ts b/src/renderer/src/store/github/refresh/refresh-event-actions.ts index 0feb59c1623..7a301f72c31 100644 --- a/src/renderer/src/store/github/refresh/refresh-event-actions.ts +++ b/src/renderer/src/store/github/refresh/refresh-event-actions.ts @@ -69,8 +69,14 @@ export const createRefreshEventActions = ( nextSequences[alias.cacheKey] ?? 0, prRefreshStartedLinkedReviewScopes.get(alias.cacheKey)?.sequence ?? 0 ) + const completesOwnedRequest = + event.status === 'skipped' && + event.sequence === previousSequence && + nextStates[alias.cacheKey]?.status === 'in-flight' if ( - event.outcome ? event.sequence < previousSequence : event.sequence <= previousSequence + event.outcome + ? event.sequence < previousSequence + : event.sequence <= previousSequence && !completesOwnedRequest ) { if (event.outcome || event.status !== 'in-flight') { deletePRRefreshStartedEntry(event.sequence, alias.cacheKey) diff --git a/src/renderer/src/store/github/requests/request-coordination.ts b/src/renderer/src/store/github/requests/request-coordination.ts index 311fc38181b..18d5d558c1f 100644 --- a/src/renderer/src/store/github/requests/request-coordination.ts +++ b/src/renderer/src/store/github/requests/request-coordination.ts @@ -1,6 +1,10 @@ import type { PRCheckContract } from '@/lib/source-control/checks/pr-check-observation-contract' import type { PRCheckDetail } from '../../../../../shared/github/checks/check-types' import type { PRComment } from '../../../../../shared/github/comments/comment-types' +import type { + GitHubPRRefreshReason, + PRSnapshotReply +} from '../../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import type { IssueInfo, PRInfo @@ -10,12 +14,18 @@ import type { GetProjectViewTableResult } from '../../../../../shared/github/pro import type { LinkedReviewClearScope } from '../pull-requests/linked-review-clear' import type { AppState } from '../../types' import { matchesRepoCacheEviction, type RepoCacheEviction } from '../cache/cache-identity' +import type { PromiseSettlementWaiters } from '../../../../../shared/promise-settlement-waiters' export type InflightPR = { promise: Promise + controller: AbortController + waiters: PromiseSettlementWaiters force: boolean generation: number lookupHintKey: string + reason?: GitHubPRRefreshReason + superseded?: boolean + outcome?: PRSnapshotReply } export type InflightChecks = { promise: Promise diff --git a/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler.test.ts b/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler.test.ts index 4b47d853ca1..1c362ed2503 100644 --- a/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler.test.ts +++ b/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler.test.ts @@ -40,6 +40,26 @@ describe('visible hosted review scheduler', () => { vi.useRealTimers() }) + it('retrieves a deferred completion with an ordinary timer read and resets on a head change', async () => { + const refresh = vi.fn(async () => ({ succeeded: true, retryAfterMs: 10_000 })) + const row = target({ fetchedAt: Date.now() - 120_000, refresh }) + const scheduler = setup([row]) + await vi.advanceTimersByTimeAsync(0) + expect(refresh.mock.calls[0]).toEqual([true, expect.any(AbortSignal), 'discovery']) + await vi.advanceTimersByTimeAsync(10_000) + expect(refresh.mock.calls[1]).toEqual([false, expect.any(AbortSignal), 'timer']) + scheduler.update([{ ...row, revision: 'new-head|' }]) + await vi.advanceTimersByTimeAsync(10_000) + expect(refresh.mock.calls[2]).toEqual([true, expect.any(AbortSignal), 'discovery']) + }) + + it('keeps successful same-age cache replies on the normal interval without a receipt', async () => { + const refresh = vi.fn(async () => ({ succeeded: true })) + setup([target({ fetchedAt: Date.now() - 120_000, refresh })]) + await vi.advanceTimersByTimeAsync(120_000) + expect(refresh.mock.calls[1]).toEqual([false, expect.any(AbortSignal), 'timer']) + }) + it.each(['remove', 'hide', 'dispose'])( 'aborts abandoned demand on %s without releasing native work early', async (action) => { @@ -77,3 +97,28 @@ describe('visible hosted review scheduler', () => { } ) }) + +it('limits refocus discovery to opted-in open GitHub rows and the existing stopped interval', async () => { + vi.useFakeTimers() + vi.setSystemTime(1_000_000) + const github = target({ + key: 'github', + fetchedAt: Date.now(), + intervalMs: 120_000, + refreshOnForeground: true + }) + const closed = target({ key: 'closed', fetchedAt: Date.now(), intervalMs: 900_000 }) + const other = target({ key: 'other', fetchedAt: Date.now(), intervalMs: 120_000 }) + const stopped = target({ key: 'stopped', fetchedAt: Date.now(), intervalMs: null }) + const scheduler = setup([github, closed, other, stopped]) + scheduler.setVisible(false) + await vi.advanceTimersByTimeAsync(130_000) + scheduler.setVisible(true) + await vi.advanceTimersByTimeAsync(0) + expect(github.refresh).toHaveBeenCalledWith(true, expect.any(AbortSignal), 'discovery') + expect(stopped.refresh).toHaveBeenCalledWith(true, expect.any(AbortSignal), 'discovery') + expect(closed.refresh).not.toHaveBeenCalled() + expect(other.refresh).toHaveBeenCalledWith(false, expect.any(AbortSignal), 'timer') + scheduler.dispose() + vi.useRealTimers() +}) diff --git a/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler.ts b/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler.ts index 4f0b05d4adc..56a05906ad1 100644 --- a/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler.ts +++ b/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler.ts @@ -7,15 +7,25 @@ export type VisibleHostedReviewRefreshTarget = { fetchedAt: number | null intervalMs: number | null selected: boolean - refresh: (force?: boolean, signal?: AbortSignal) => Promise + refreshOnForeground?: boolean + refresh: ( + force?: boolean, + signal?: AbortSignal, + intent?: 'discovery' | 'timer' + ) => Promise } +export type VisibleHostedReviewRefreshResult = + | boolean + | { succeeded: boolean; retryAfterMs?: number } + type Entry = { target: VisibleHostedReviewRefreshTarget lastAttemptAt: number failures: number retryAt: number | null discovery: boolean + deferredUntil: number | null } function discoveryIdentityChanged( @@ -56,6 +66,9 @@ export function createVisibleHostedReviewRefreshScheduler() { const dueAt = (entry: Entry): number => { const cooldown = entry.lastAttemptAt + REVIEW_REFRESH_COOLDOWN_MS + if (entry.deferredUntil !== null && !entry.discovery) { + return Math.max(cooldown, entry.deferredUntil) + } if (entry.retryAt !== null) { return Math.max(cooldown, entry.retryAt) } @@ -84,8 +97,9 @@ export function createVisibleHostedReviewRefreshScheduler() { key: string, startedAt: number, target: VisibleHostedReviewRefreshTarget, - succeeded: boolean + result: VisibleHostedReviewRefreshResult ): void => { + const succeeded = typeof result === 'boolean' ? result : result.succeeded const cancelled = inFlight.get(key)?.controller.signal.aborted inFlight.get(key)?.controller.abort() inFlight.delete(key) @@ -94,11 +108,17 @@ export function createVisibleHostedReviewRefreshScheduler() { } const entry = entries.get(key) ?? recent.get(key) if (entry && entry.lastAttemptAt === startedAt) { - if (cancelled) { + if (cancelled || discoveryIdentityChanged(target, entry.target)) { entry.discovery = true - } else if (!succeeded || !discoveryIdentityChanged(target, entry.target)) { + entry.deferredUntil = null + } else { entry.discovery = false entry.failures = succeeded ? 0 : entry.failures + 1 + const retry = typeof result === 'boolean' ? undefined : result.retryAfterMs + entry.deferredUntil = + succeeded && retry !== undefined && Number.isFinite(retry) && retry >= 0 + ? Date.now() + Math.max(REVIEW_REFRESH_COOLDOWN_MS, retry) + : null entry.retryAt = succeeded ? null : Date.now() + @@ -134,13 +154,14 @@ export function createVisibleHostedReviewRefreshScheduler() { continue } const force = entry.discovery || entry.failures > 0 || entry.target.fetchedAt === null + const intent = entry.discovery || entry.target.fetchedAt === null ? 'discovery' : 'timer' entry.lastAttemptAt = now entry.discovery = false const startedTarget = entry.target const controller = new AbortController() inFlight.set(key, { target: startedTarget, controller }) try { - void entry.target.refresh(force, controller.signal).then( + void entry.target.refresh(force, controller.signal, intent).then( (succeeded) => finish(key, now, startedTarget, succeeded), () => finish(key, now, startedTarget, false) ) @@ -192,11 +213,13 @@ export function createVisibleHostedReviewRefreshScheduler() { discovery: (previous !== undefined && discoveryIdentityChanged(previous.target, target)) || target.fetchedAt === null || - Date.now() - target.fetchedAt >= (target.intervalMs ?? 60_000) + Date.now() - target.fetchedAt >= (target.intervalMs ?? 60_000), + deferredUntil: null } entries.set(target.key, entry) } else if (discoveryIdentityChanged(entry.target, target)) { entry.discovery = true + entry.deferredUntil = null } else if ( (target.fetchedAt ?? -Infinity) > (entry.target.fetchedAt ?? -Infinity) && (!inFlight.has(target.key) || @@ -205,6 +228,7 @@ export function createVisibleHostedReviewRefreshScheduler() { entry.failures = 0 entry.retryAt = null entry.discovery = false + entry.deferredUntil = null } entry.target = target } @@ -216,6 +240,9 @@ export function createVisibleHostedReviewRefreshScheduler() { } visible = next if (!visible) { + for (const entry of entries.values()) { + entry.deferredUntil = null + } for (const request of inFlight.values()) { request.controller.abort() } @@ -223,8 +250,9 @@ export function createVisibleHostedReviewRefreshScheduler() { if (visible) { for (const entry of entries.values()) { if ( - entry.target.intervalMs === null && - Date.now() - (entry.target.fetchedAt ?? -Infinity) >= 60_000 + (entry.target.intervalMs === null || entry.target.refreshOnForeground === true) && + Date.now() - (entry.target.fetchedAt ?? -Infinity) >= + Math.max(60_000, entry.target.intervalMs ?? 60_000) ) { entry.discovery = true } diff --git a/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-targets.ts b/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-targets.ts index 8c4853d057b..bba964017ea 100644 --- a/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-targets.ts +++ b/src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-targets.ts @@ -25,6 +25,7 @@ import { selectWorktreesByRepo } from '../../../workspaces/catalog/workspace-record-fields' import { isWslHostOffForForge } from '../../../github/repositories/wsl-host-off-forge' +import type { PRSnapshotReply } from '../../../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' export function* getVisibleHostedReviewWorkspaces( state: AppState, @@ -180,13 +181,16 @@ export function getVisibleHostedReviewRefreshTargets( ? null : fetchedAt, selected, + refreshOnForeground: + !!runtime && knownGitHub && (review?.state === 'open' || review?.state === 'draft'), intervalMs: reviewRefreshIntervalMs({ state: review?.state, checksStatus: usePR ? prEntry.data?.checksStatus : hostedEntry?.data?.status, hasReview: review ? true : fetchedAt !== null ? false : null, selected }), - refresh: async (force = true) => { + refresh: async (force = true, signal, intent) => { + let outcome: PRSnapshotReply | undefined const before = getState() const beforeFetchedAt = knownGitHub ? before.prCache[candidate.cacheKey]?.fetchedAt @@ -194,7 +198,12 @@ export function getVisibleHostedReviewRefreshTargets( await (runtime ? before.fetchPRForBranch(candidate.repoPath, candidate.branch, { force: true, - reason: 'visible', + signal, + reason: (intent ? intent === 'discovery' : force) ? 'active' : 'swr', + selected, + onReadOutcome: (next) => { + outcome = next + }, repoId: candidate.repoId, repoOwnerExecutionHostId: getRepoExecutionHostId(candidate), worktreeId: id, @@ -214,6 +223,12 @@ export function getVisibleHostedReviewRefreshTargets( admissionTier: selected ? 'interactive' : 'background' })) const after = getState() + if (runtime && outcome) { + return { + succeeded: !('kind' in outcome) || outcome.kind !== 'upstream-error', + retryAfterMs: outcome.revalidation?.retryAfterMs + } + } const afterFetchedAt = knownGitHub ? after.prCache[candidate.cacheKey]?.fetchedAt : after.hostedReviewCache[key]?.fetchedAt diff --git a/src/renderer/src/web/preload-api/web-runtime-calls-connection-pin.test.ts b/src/renderer/src/web/preload-api/web-runtime-calls-connection-pin.test.ts index 51ef1ad4cca..d4c6d56f431 100644 --- a/src/renderer/src/web/preload-api/web-runtime-calls-connection-pin.test.ts +++ b/src/renderer/src/web/preload-api/web-runtime-calls-connection-pin.test.ts @@ -9,17 +9,24 @@ const session = vi.hoisted(() => { return { client, environment: { id: 'env-1', createdAt: 1 } } }) -vi.mock('./web-runtime-session', () => ({ - getClientForEnvironment: () => session.client, - manuallyDisconnectedEnvironmentIds: new Set(), - manuallyDisconnectedResponse: () => ({ ok: false }), - requireActiveEnvironment: () => session.environment, - resolveEnvironment: () => session.environment, - runtimeCallQueuePool: { enqueue: (_id: string, _method: string, run: () => unknown) => run() }, - updateEnvironmentFromResponse: () => undefined -})) +vi.mock('./web-runtime-session', async () => { + const { RuntimeRpcCallQueuePool } = await import('../../../../shared/runtime-rpc-call-queue') + return { + getClientForEnvironment: () => session.client, + manuallyDisconnectedEnvironmentIds: new Set(), + manuallyDisconnectedResponse: () => ({ ok: false }), + requireActiveEnvironment: () => session.environment, + resolveEnvironment: () => session.environment, + runtimeCallQueuePool: new RuntimeRpcCallQueuePool(), + updateEnvironmentFromResponse: () => undefined + } +}) -import { callEnvironmentEnvelope, callRuntimeResult } from './web-runtime-calls' +import { + callRuntimeEnvelope, + callEnvironmentEnvelope, + callRuntimeResult +} from './web-runtime-calls' import { RuntimeConnectionChangedBeforeSendError, RuntimeRpcCallError @@ -88,3 +95,21 @@ it('preserves runtime failure details for web callers', async () => { response: { error: { data: { reason: 'windows_drive' } } } }) }) + +it('routes both web callers timed PR waits away from status and action calls', async () => { + const release = Promise.withResolvers() + session.client.call.mockImplementation(async (_method, params) => { + if (params?.reason === 'swr') { + await release.promise + } + return { id: 'rpc', ok: true, result: 'current' } + }) + const first = callRuntimeEnvelope('github.prForBranch', { reason: 'swr' }) + const second = callEnvironmentEnvelope('env-1', 'github.prForBranch', { reason: 'swr' }) + await expect( + callEnvironmentEnvelope('env-1', 'github.prForBranch', { reason: 'manual' }) + ).resolves.toMatchObject({ result: 'current' }) + await expect(callRuntimeEnvelope('git.status')).resolves.toMatchObject({ result: 'current' }) + release.resolve() + await Promise.all([first, second]) +}) diff --git a/src/renderer/src/web/preload-api/web-runtime-calls.ts b/src/renderer/src/web/preload-api/web-runtime-calls.ts index e476a1d42af..8722c09b6f8 100644 --- a/src/renderer/src/web/preload-api/web-runtime-calls.ts +++ b/src/renderer/src/web/preload-api/web-runtime-calls.ts @@ -43,12 +43,19 @@ export async function callRuntimeEnvelope( if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { return manuallyDisconnectedResponse(environment) } - const response = await runtimeCallQueuePool.enqueue(environment.id, method, () => { - if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { - return Promise.resolve(manuallyDisconnectedResponse(environment)) - } - return getClientForEnvironment(environment).call(method, params, { timeoutMs }) - }) + const response = await runtimeCallQueuePool.enqueue( + environment.id, + method, + () => { + if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { + return Promise.resolve(manuallyDisconnectedResponse(environment)) + } + return getClientForEnvironment(environment).call(method, params, { timeoutMs }) + }, + 0, + undefined, + params + ) if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { return manuallyDisconnectedResponse(environment) } @@ -137,19 +144,26 @@ export async function callEnvironmentEnvelope( if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { return manuallyDisconnectedResponse(environment) } - const response = await runtimeCallQueuePool.enqueue(environment.id, method, () => { - assertPairing() - if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { - return Promise.resolve(manuallyDisconnectedResponse(environment)) - } - return callPinnedToRuntime( - getClientForEnvironment(environment), - method, - params, - timeoutMs, - expectedEnvironmentRuntimeId - ) - }) + const response = await runtimeCallQueuePool.enqueue( + environment.id, + method, + () => { + assertPairing() + if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { + return Promise.resolve(manuallyDisconnectedResponse(environment)) + } + return callPinnedToRuntime( + getClientForEnvironment(environment), + method, + params, + timeoutMs, + expectedEnvironmentRuntimeId + ) + }, + 0, + undefined, + params + ) if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { return manuallyDisconnectedResponse(environment) } diff --git a/src/renderer/src/web/preload-api/web-runtime-environments-api.ts b/src/renderer/src/web/preload-api/web-runtime-environments-api.ts index f1228dd392f..6be6c3b6916 100644 --- a/src/renderer/src/web/preload-api/web-runtime-environments-api.ts +++ b/src/renderer/src/web/preload-api/web-runtime-environments-api.ts @@ -1,3 +1,4 @@ +import { createWebRuntimeEnvironmentSubscriber } from './web-runtime-subscription' import { pairedRuntimeConnectionFromOffer } from '../../../../shared/remote-runtime/pairing/paired-runtime-connection' import { createWebRuntimeSubscriptionApiControl, @@ -20,11 +21,9 @@ import { import { translate } from '@/i18n/i18n' import { translateHostAccessLinkError } from '@/lib/remote-pairing/remote-pairing-copy' import { - assertEnvironmentPairing, callEnvironmentEnvelope, callWebRuntimeEnvironment, - getWebRuntimeStatus, - pinnedWebSubscriptionOptions + getWebRuntimeStatus } from './web-runtime-calls' import { closeActiveRuntimeClients, @@ -36,10 +35,7 @@ import { removeActiveRuntimeEnvironment, requireActiveEnvironmentOrNull, resolveEnvironment, - webRuntimeState, - runtimeCallQueuePool, - updateEnvironmentFromResponse, - manuallyDisconnectedResponse + webRuntimeState } from './web-runtime-session' export function createRuntimeEnvironmentsApi(): PreloadApi['runtimeEnvironments'] { return { @@ -216,86 +212,6 @@ export function createRuntimeEnvironmentsApi(): PreloadApi['runtimeEnvironments' prepareBrowserClientHostPlacement: async () => ({ kind: 'server' }), call: callWebRuntimeEnvironment, ...createWebRuntimeSubscriptionApiControl(pendingSubscriptions), - subscribe: async ( - { - selector, - method, - params, - timeoutMs, - subscriptionId, - expectedEnvironmentPairingRevision, - expectedEnvironmentRuntimeId - }, - callbacks - ) => { - const environment = resolveEnvironment(selector) - assertEnvironmentPairing(selector, expectedEnvironmentPairingRevision) - const client = getClientForEnvironment(environment) - const controller = new AbortController() - if (subscriptionId) { - if (pendingSubscriptions.has(subscriptionId)) { - throw new Error('Subscription id already exists') - } - pendingSubscriptions.set(subscriptionId, { controller }) - } - let subscription - try { - subscription = await runtimeCallQueuePool.enqueue( - environment.id, - method, - () => { - assertEnvironmentPairing(selector, expectedEnvironmentPairingRevision) - if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { - throw new Error('runtime_manually_disconnected') - } - return client.subscribe( - method, - params, - { - ...callbacks, - onResponse: (response) => { - try { - assertEnvironmentPairing(selector, expectedEnvironmentPairingRevision) - } catch (error) { - callbacks.onError?.({ - code: 'runtime_environment_changed', - message: String(error) - }) - callbacks.onClose?.() - controller.abort() - return - } - const result = manuallyDisconnectedEnvironmentIds.has(environment.id) - ? manuallyDisconnectedResponse(environment) - : response - updateEnvironmentFromResponse(environment, result) - callbacks.onResponse(result) - } - }, - { - timeoutMs, - signal: controller.signal, - ...pinnedWebSubscriptionOptions( - selector, - expectedEnvironmentPairingRevision, - expectedEnvironmentRuntimeId - ) - } - ) - }, - 0, - controller.signal - ) - } finally { - if (subscriptionId && pendingSubscriptions.get(subscriptionId)?.controller === controller) { - pendingSubscriptions.delete(subscriptionId) - } - } - if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { - subscription.unsubscribe() - throw new Error('runtime_manually_disconnected') - } - return subscription - } + subscribe: createWebRuntimeEnvironmentSubscriber(pendingSubscriptions) } } diff --git a/src/renderer/src/web/preload-api/web-runtime-pr-subscription-queue.test.ts b/src/renderer/src/web/preload-api/web-runtime-pr-subscription-queue.test.ts new file mode 100644 index 00000000000..4291b757a40 --- /dev/null +++ b/src/renderer/src/web/preload-api/web-runtime-pr-subscription-queue.test.ts @@ -0,0 +1,85 @@ +import { expect, it, vi } from 'vitest' + +const session = vi.hoisted(() => ({ + environment: { id: 'env', createdAt: 1, runtimeId: 'runtime' }, + client: { connectionEpoch: 1, runtimeId: 'runtime', call: vi.fn(), subscribe: vi.fn() } +})) +vi.mock('./web-runtime-session', async () => { + const { RuntimeRpcCallQueuePool } = await import('../../../../shared/runtime-rpc-call-queue') + return { + closeActiveRuntimeClients: vi.fn(), + subscribeWebRuntimeStatus: vi.fn(), + readWebRuntimeStatusSnapshots: vi.fn(), + disconnectActiveRuntimeEnvironment: vi.fn(), + getClientForEnvironment: () => session.client, + manuallyDisconnectedEnvironmentIds: new Set(), + removeActiveRuntimeEnvironment: vi.fn(), + requireActiveEnvironment: () => session.environment, + requireActiveEnvironmentOrNull: () => session.environment, + resolveEnvironment: () => session.environment, + webRuntimeState: { activeEnvironment: session.environment }, + runtimeCallQueuePool: new RuntimeRpcCallQueuePool(), + updateEnvironmentFromResponse: vi.fn(), + manuallyDisconnectedResponse: vi.fn() + } +}) +import { createRuntimeEnvironmentsApi } from './web-runtime-environments-api' + +it('holds web signal-route slots through responses and releases queued and active cancellations', async () => { + const responseOrder: string[] = [] + const finish: (() => void)[] = [] + const closed = vi.fn() + session.client.subscribe.mockImplementation(async (_method, _params, callbacks) => { + finish.push(() => + callbacks.onResponse({ id: 'rpc', ok: true, result: {}, _meta: { runtimeId: 'runtime' } }) + ) + return { + unsubscribe: () => { + closed() + responseOrder.push('close') + callbacks.onClose?.() + }, + sendBinary: vi.fn() + } + }) + const api = createRuntimeEnvironmentsApi() + const start = (subscriptionId: string, reason: string, method = 'github.prForBranch') => + api.subscribe( + { + selector: 'env', + subscriptionId, + method, + params: { reason, selected: true }, + timeoutMs: 30_000 + }, + { onResponse: () => responseOrder.push('response') } + ) + const first = await start('timer-1', 'swr') + expect(session.client.subscribe.mock.calls[0][1]).toMatchObject({ reason: 'swr', selected: true }) + const second = await start('timer-2', 'swr') + const third = start('timer-3', 'swr') + const cancelled = expect(third).rejects.toThrow() + const manual = await start('manual', 'manual') + const status = await start('status', '', 'git.status') + const review = await start('review', '', 'hostedReview.forBranch') + expect(finish).toHaveLength(5) + await api.cancelSubscription({ subscriptionId: 'timer-3' }) + await cancelled + const fourth = start('timer-4', 'swr') + finish[0]() + const fourthHandle = await fourth + expect(finish).toHaveLength(6) + expect(responseOrder.slice(0, 2)).toEqual(['response', 'close']) + const fifth = start('timer-5', 'swr') + second.unsubscribe() + const fifthHandle = await fifth + expect(finish).toHaveLength(7) + first.unsubscribe() + fourthHandle.unsubscribe() + fifthHandle.unsubscribe() + manual.unsubscribe() + status.unsubscribe() + review.unsubscribe() + await Promise.resolve() + expect(closed).toHaveBeenCalledTimes(7) +}) diff --git a/src/renderer/src/web/preload-api/web-runtime-subscription.ts b/src/renderer/src/web/preload-api/web-runtime-subscription.ts new file mode 100644 index 00000000000..ea0c85dddde --- /dev/null +++ b/src/renderer/src/web/preload-api/web-runtime-subscription.ts @@ -0,0 +1,112 @@ +import type { PreloadApi } from '../../../../preload/api-types' +import { enqueueRuntimeSubscription } from '../../../../shared/runtime-rpc-subscription-queue' +import { isTimedPRRead } from '../../../../shared/runtime-rpc-call-queue' +import type { WebRuntimeSubscriptionRegistration } from './web-runtime-crew-subscription' +import { assertEnvironmentPairing, pinnedWebSubscriptionOptions } from './web-runtime-calls' +import { + resolveEnvironment, + getClientForEnvironment, + manuallyDisconnectedEnvironmentIds, + manuallyDisconnectedResponse, + runtimeCallQueuePool, + updateEnvironmentFromResponse +} from './web-runtime-session' + +export function createWebRuntimeEnvironmentSubscriber( + pendingSubscriptions: Map +): PreloadApi['runtimeEnvironments']['subscribe'] { + return async ( + { + selector, + method, + params, + timeoutMs, + subscriptionId, + expectedEnvironmentPairingRevision, + expectedEnvironmentRuntimeId + }, + callbacks + ) => { + const environment = resolveEnvironment(selector) + assertEnvironmentPairing(selector, expectedEnvironmentPairingRevision) + const client = getClientForEnvironment(environment) + const controller = new AbortController() + if (subscriptionId) { + if (pendingSubscriptions.has(subscriptionId)) { + throw new Error('Subscription id already exists') + } + pendingSubscriptions.set(subscriptionId, { controller }) + } + let subscription + try { + subscription = await enqueueRuntimeSubscription( + runtimeCallQueuePool, + environment.id, + method, + params, + (complete) => { + assertEnvironmentPairing(selector, expectedEnvironmentPairingRevision) + if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { + throw new Error('runtime_manually_disconnected') + } + return client.subscribe( + method, + params, + { + ...callbacks, + onError: (error) => { + complete() + callbacks.onError?.(error) + }, + onClose: () => { + complete() + callbacks.onClose?.() + }, + onResponse: (response) => { + complete() + try { + assertEnvironmentPairing(selector, expectedEnvironmentPairingRevision) + } catch (error) { + callbacks.onError?.({ + code: 'runtime_environment_changed', + message: String(error) + }) + callbacks.onClose?.() + controller.abort() + return + } + const result = manuallyDisconnectedEnvironmentIds.has(environment.id) + ? manuallyDisconnectedResponse(environment) + : response + updateEnvironmentFromResponse(environment, result) + callbacks.onResponse(result) + } + }, + { + timeoutMs, + signal: controller.signal, + ...pinnedWebSubscriptionOptions( + selector, + expectedEnvironmentPairingRevision, + expectedEnvironmentRuntimeId + ) + } + ) + }, + (handle) => handle.unsubscribe(), + controller.signal + ) + } finally { + if (subscriptionId && pendingSubscriptions.get(subscriptionId)?.controller === controller) { + pendingSubscriptions.delete(subscriptionId) + } + } + if (manuallyDisconnectedEnvironmentIds.has(environment.id)) { + subscription.unsubscribe() + throw new Error('runtime_manually_disconnected') + } + return isTimedPRRead(method, params) + ? { ...subscription, unsubscribe: () => controller.abort() } + : subscription + } +} diff --git a/src/shared/github/pull-requests/refresh/pr-quiet-refresh.ts b/src/shared/github/pull-requests/refresh/pr-quiet-refresh.ts new file mode 100644 index 00000000000..3bb9101d790 --- /dev/null +++ b/src/shared/github/pull-requests/refresh/pr-quiet-refresh.ts @@ -0,0 +1,22 @@ +import type { PRInfo } from '../pull-request-types' + +export const QUIET_PR_REFRESH_INTERVAL_MS = 5 * 60_000 + +export function prChecksDone(pr: PRInfo): boolean { + return ( + pr.checksFinal === true || (pr.checksReadComplete === true && pr.checksSummary?.total === 0) + ) +} + +export function isQuietPR(pr: PRInfo, currentHeadOid: string | null): boolean { + return ( + (pr.state === 'open' || pr.state === 'draft') && + !!currentHeadOid && + pr.headSha === currentHeadOid && + pr.checksReadComplete === true && + prChecksDone(pr) && + (pr.mergeable === 'MERGEABLE' || pr.mergeable === 'CONFLICTING') && + pr.autoMergeEnabled === false && + pr.mergeQueueRequired === false + ) +} diff --git a/src/shared/github/pull-requests/refresh/pull-request-refresh-types.ts b/src/shared/github/pull-requests/refresh/pull-request-refresh-types.ts index bce9d7bac2d..734830dca3e 100644 --- a/src/shared/github/pull-requests/refresh/pull-request-refresh-types.ts +++ b/src/shared/github/pull-requests/refresh/pull-request-refresh-types.ts @@ -12,7 +12,7 @@ export type PRRefreshErrorType = | 'server_error' | 'unknown' -export type PRRefreshOutcome = +export type PRRefreshOutcome = ( | { kind: 'found'; pr: PRInfo; fetchedAt: number } | { kind: 'no-pr'; fetchedAt: number; rejectedPRUrls?: string[] } | { @@ -24,6 +24,27 @@ export type PRRefreshOutcome = nextAutoRetryAt?: number retryDisabledUntil?: number } +) & { revalidation?: { retryAfterMs: number } } + +export type PRRevalidationOnly = { revalidation: { invalidated: true; retryAfterMs: number } } +export type PRSnapshotReply = PRRefreshOutcome | PRRevalidationOnly + +export function isPRRevalidationOnly(value: unknown): value is PRRevalidationOnly { + if (!value || typeof value !== 'object' || 'kind' in value || !('revalidation' in value)) { + return false + } + const receipt = value.revalidation + return ( + !!receipt && + typeof receipt === 'object' && + 'invalidated' in receipt && + receipt.invalidated === true && + 'retryAfterMs' in receipt && + typeof receipt.retryAfterMs === 'number' && + Number.isFinite(receipt.retryAfterMs) && + receipt.retryAfterMs >= 0 + ) +} /** `watch` is host-only: a background read for a chat's CI lines, never sent by a client. */ export type GitHubPRRefreshReason = 'visible' | 'active' | 'post-push' | 'manual' | 'swr' | 'watch' diff --git a/src/shared/rpc-contract/github/github-pull-request-params.ts b/src/shared/rpc-contract/github/github-pull-request-params.ts index 925315ce91a..2573a5e20af 100644 --- a/src/shared/rpc-contract/github/github-pull-request-params.ts +++ b/src/shared/rpc-contract/github/github-pull-request-params.ts @@ -20,6 +20,8 @@ export const PrForBranch = RepoSelector.extend({ branch: requiredString('Missing branch'), reason: OptionalPRRefreshReason, userRefresh: z.boolean().optional(), + acceptRevalidationOnly: z.boolean().optional(), + selected: z.boolean().optional(), linkedPRNumber: z.number().int().positive().nullable().optional(), fallbackPRNumber: z.number().int().positive().nullable().optional(), acceptMergedFallbackPR: z.boolean().optional(), diff --git a/src/shared/runtime-rpc-call-queue.test.ts b/src/shared/runtime-rpc-call-queue.test.ts index 6c870dbdb3e..1d91d969cc6 100644 --- a/src/shared/runtime-rpc-call-queue.test.ts +++ b/src/shared/runtime-rpc-call-queue.test.ts @@ -245,3 +245,68 @@ describe('runtime RPC call queue', () => { ).resolves.toBe('recovered') }) }) + +it('keeps actions and status runnable while timed PR snapshots wait', async () => { + const pool = new RuntimeRpcCallQueuePool() + const release = Promise.withResolvers() + const started: string[] = [] + const timers = [0, 1, 2].map((row) => + pool.enqueue( + 'host', + 'github.prForBranch', + async () => { + started.push(`timer-${row}`) + await release.promise + }, + 0, + undefined, + { reason: 'swr' } + ) + ) + expect(started).toEqual(['timer-0', 'timer-1']) + for (const reason of ['manual', 'active', 'post-push', 'visible', undefined, 'swr']) { + await expect( + pool.enqueue('host', 'github.prForBranch', async () => 'current', 0, undefined, { + reason, + userRefresh: reason === 'swr' + }) + ).resolves.toBe('current') + } + await expect(pool.enqueue('host', 'git.status', async () => 'clean')).resolves.toBe('clean') + await expect(pool.enqueue('host', 'hostedReview.forBranch', async () => 'review')).resolves.toBe( + 'review' + ) + expect(started).toEqual(['timer-0', 'timer-1']) + release.resolve() + await Promise.all(timers) + expect(started).toEqual(['timer-0', 'timer-1', 'timer-2']) +}) + +it('shares pending and byte limits across ordinary and timed PR lanes and removes cancelled calls', async () => { + const pool = new RuntimeRpcCallQueuePool(1, 1, 1, 5, 4) + const release = Promise.withResolvers() + const active = pool.enqueue('host', 'github.prForBranch', () => release.promise, 1, undefined, { + reason: 'swr' + }) + const controller = new AbortController() + const queued = pool.enqueue( + 'host', + 'github.prForBranch', + async () => 'unused', + 1, + controller.signal, + { reason: 'swr' } + ) + await expect(pool.enqueue('host', 'git.status', async () => 'overflow')).rejects.toMatchObject({ + scope: 'selector' + }) + const cancelled = expect(queued).rejects.toMatchObject({ name: 'AbortError' }) + controller.abort() + await cancelled + await expect(pool.enqueue('host', 'git.status', async () => 'clean', 3)).resolves.toBe('clean') + await expect(pool.enqueue('host', 'git.status', async () => 'oversize', 4)).rejects.toMatchObject( + { scope: 'memory' } + ) + release.resolve() + await active +}) diff --git a/src/shared/runtime-rpc-call-queue.ts b/src/shared/runtime-rpc-call-queue.ts index 532d6241358..8a88ef672ec 100644 --- a/src/shared/runtime-rpc-call-queue.ts +++ b/src/shared/runtime-rpc-call-queue.ts @@ -53,6 +53,17 @@ export function isBackgroundRuntimeMethod(method: string): boolean { ) } +export function isTimedPRRead(method: string, params: unknown): boolean { + return ( + method === 'github.prForBranch' && + typeof params === 'object' && + params !== null && + 'reason' in params && + params.reason === 'swr' && + !('userRefresh' in params && params.userRefresh === true) + ) +} + // Slow worktree removal/model listing waits get a separate lane so foreground refresh/send slots remain free; // background slots belong to status. function isLongWaitRuntimeMethod(method: string): boolean { @@ -86,23 +97,38 @@ export class RuntimeRpcCallQueuePool { method: string, run: () => Promise, retainedBytes = 0, - signal?: AbortSignal + signal?: AbortSignal, + params?: unknown ): Promise { if (signal?.aborted) { return Promise.reject(abortSignalReason(signal)) } // Same concurrency bound, counted apart from the selector's other calls; global caps still apply. const bulkRead = isBulkReadRuntimeMethod(method) + const timedPR = isTimedPRRead(method, params) + const timedKey = `${selector}\u0000timed-pr` const queueKey = isLongWaitRuntimeMethod(method) ? `${selector}\u0000long-wait` : bulkRead ? `${selector}\u0000bulk-read` - : selector + : timedPR + ? timedKey + : selector if (this.queuedCallCount >= this.maxQueuedTotal) { return Promise.reject(new RuntimeRpcCallQueueOverloadError('global')) } const existingQueue = this.queues.get(queueKey) - if (existingQueue && this.queuedCount(existingQueue) >= this.maxQueuedPerSelector) { + const pending = (key: string): number => { + const queue = this.queues.get(key) + return queue ? this.queuedCount(queue) : 0 + } + const queued = + queueKey === selector || timedPR + ? pending(selector) + pending(timedKey) + : existingQueue + ? this.queuedCount(existingQueue) + : 0 + if (queued >= this.maxQueuedPerSelector) { return Promise.reject(new RuntimeRpcCallQueueOverloadError('selector')) } if ( @@ -115,11 +141,15 @@ export class RuntimeRpcCallQueuePool { const queue = this.getQueue( queueKey, - bulkRead ? Math.min(this.concurrency, REMOTE_RUNTIME_BULK_READ_CONCURRENCY) : this.concurrency + timedPR + ? Math.min(this.concurrency, 2) + : bulkRead + ? Math.min(this.concurrency, REMOTE_RUNTIME_BULK_READ_CONCURRENCY) + : this.concurrency ) return new Promise((resolve, reject) => { const call: QueuedRuntimeCall = { - background: isBackgroundRuntimeMethod(method), + background: !timedPR && isBackgroundRuntimeMethod(method), retainedBytes, run, resolve, diff --git a/src/shared/runtime-rpc-subscription-queue.test.ts b/src/shared/runtime-rpc-subscription-queue.test.ts new file mode 100644 index 00000000000..b47ed6eb628 --- /dev/null +++ b/src/shared/runtime-rpc-subscription-queue.test.ts @@ -0,0 +1,114 @@ +import { expect, it, vi } from 'vitest' +import { RuntimeRpcCallQueuePool } from './runtime-rpc-call-queue' +import { enqueueRuntimeSubscription } from './runtime-rpc-subscription-queue' + +it('shares timed occupancy with one-shot calls through first response and removes a queued abort', async () => { + const pool = new RuntimeRpcCallQueuePool() + const oneShot = Promise.withResolvers() + const running = pool.enqueue('host', 'github.prForBranch', () => oneShot.promise, 0, undefined, { + reason: 'swr' + }) + let respond = (): void => {} + const close = vi.fn() + const handle = await enqueueRuntimeSubscription( + pool, + 'host', + 'github.prForBranch', + { reason: 'swr' }, + async (complete) => { + respond = complete + return { close } + }, + (value) => value.close() + ) + const controller = new AbortController() + const sendThird = vi.fn(async () => ({ close })) + const third = enqueueRuntimeSubscription( + pool, + 'host', + 'github.prForBranch', + { reason: 'swr' }, + sendThird, + (value) => value.close(), + controller.signal + ) + const rejected = expect(third).rejects.toThrow() + await expect(pool.enqueue('host', 'git.status', async () => 'clean')).resolves.toBe('clean') + expect(sendThird).not.toHaveBeenCalled() + controller.abort() + await rejected + respond() + await Promise.resolve() + expect(close).toHaveBeenCalledOnce() + expect(handle).toEqual({ close }) + oneShot.resolve() + await running +}) + +it('releases a response before setup settles and closes its late handle', async () => { + const pool = new RuntimeRpcCallQueuePool() + const late = Promise.withResolvers<{ close: () => void }>() + const close = vi.fn() + const setup = enqueueRuntimeSubscription( + pool, + 'host', + 'github.prForBranch', + { reason: 'swr' }, + (complete) => { + complete() + return late.promise + }, + (value) => value.close() + ) + await expect( + pool.enqueue('host', 'github.prForBranch', async () => 'next', 0, undefined, { reason: 'swr' }) + ).resolves.toBe('next') + late.resolve({ close }) + await setup + expect(close).toHaveBeenCalledOnce() +}) + +it('cancels an active setup, closes its late handle, and recovers after setup rejection', async () => { + const pool = new RuntimeRpcCallQueuePool() + const late = Promise.withResolvers<{ close: () => void }>() + const close = vi.fn() + const controller = new AbortController() + const started = Promise.withResolvers() + const setup = enqueueRuntimeSubscription( + pool, + 'host', + 'github.prForBranch', + { reason: 'swr' }, + () => { + started.resolve() + return late.promise + }, + (value) => value.close(), + controller.signal + ) + const rejected = expect(setup).rejects.toThrow() + await started.promise + controller.abort() + await rejected + late.resolve({ close }) + await Promise.resolve() + await Promise.resolve() + expect(close).toHaveBeenCalledOnce() + await expect( + enqueueRuntimeSubscription( + pool, + 'host', + 'github.prForBranch', + { reason: 'swr' }, + async () => { + throw new Error('setup failed') + }, + (value: { close: () => void }) => value.close() + ) + ).rejects.toThrow('setup failed') + await expect( + pool.enqueue('host', 'github.prForBranch', async () => 'recovered', 0, undefined, { + reason: 'swr' + }) + ).resolves.toBe('recovered') +}) diff --git a/src/shared/runtime-rpc-subscription-queue.ts b/src/shared/runtime-rpc-subscription-queue.ts new file mode 100644 index 00000000000..a4978f042ed --- /dev/null +++ b/src/shared/runtime-rpc-subscription-queue.ts @@ -0,0 +1,77 @@ +import { abortSignalReason } from './abort-signal-reason' +import { isTimedPRRead, type RuntimeRpcCallQueuePool } from './runtime-rpc-call-queue' + +export function enqueueRuntimeSubscription( + pool: RuntimeRpcCallQueuePool, + selector: string, + method: string, + params: unknown, + open: (complete: () => void) => Promise, + close: (handle: T) => void, + signal?: AbortSignal, + retainedBytes = 0 +): Promise { + if (!isTimedPRRead(method, params)) { + return pool.enqueue(selector, method, () => open(() => {}), retainedBytes, signal, params) + } + const setup = Promise.withResolvers() + const completion = Promise.withResolvers() + let handle: T | undefined + let completed = false + const closeHandle = (value: T): void => { + try { + close(value) + } catch { + // Terminal cleanup must still release admission if transport close throws. + } + } + const complete = (): void => { + if (completed) { + return + } + completed = true + completion.resolve() + if (handle !== undefined) { + const terminalHandle = handle + queueMicrotask(() => closeHandle(terminalHandle)) + } + } + const onAbort = (): void => { + complete() + if (signal) { + setup.reject(abortSignalReason(signal)) + } + } + signal?.addEventListener('abort', onAbort, { once: true }) + void pool + .enqueue( + selector, + method, + async () => { + void Promise.resolve() + .then(() => open(complete)) + .then( + (opened) => { + handle = opened + if (completed) { + closeHandle(opened) + } + setup.resolve(opened) + }, + (error) => { + setup.reject(error) + complete() + } + ) + await completion.promise + }, + retainedBytes, + signal, + params + ) + .catch(setup.reject) + .finally(() => { + signal?.removeEventListener('abort', onAbort) + }) + return setup.promise +} diff --git a/tests/e2e/github-pr-hosted-receipt-loop.unit.test.ts b/tests/e2e/github-pr-hosted-receipt-loop.unit.test.ts new file mode 100644 index 00000000000..24610ba73a5 --- /dev/null +++ b/tests/e2e/github-pr-hosted-receipt-loop.unit.test.ts @@ -0,0 +1,284 @@ +import { describe, expect, it, vi } from 'vitest' +import { + commands, + fixture, + found, + mocks, + rpcRead +} from '../../src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshot-test-harness' +import type { PRRefreshOutcome } from '../../src/shared/github/pull-requests/refresh/pull-request-refresh-types' + +describe('hosted PR receipt delivery', () => { + it('carries a real deferred reply through the visible target, store and scheduler to its completion', async () => { + const { + createTestStore, + makePRRefreshWorktree, + resetRemoteRuntimeMocks, + runtimeEnvironmentCall + } = await import('../../src/renderer/src/store/github/github-slice-test-harness') + const { getVisibleHostedReviewRefreshTargets } = + await import('../../src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-targets') + const { createVisibleHostedReviewRefreshScheduler } = + await import('../../src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler') + const { PrForBranch } = + await import('../../src/shared/rpc-contract/github/github-pull-request-params') + const readReceipt = (reason: 'active' | 'swr') => + commands.getRepoPRForBranch( + 'repo', + 'receipt', + 12, + null, + false, + 'head-a', + reason, + undefined, + undefined, + undefined, + true + ) + resetRemoteRuntimeMocks() + const store = createTestStore() + const seed = await readReceipt('active') + if (!('kind' in seed) || seed.kind !== 'found') { + throw new Error('expected the initial PR') + } + store.setState({ + repos: [ + { + id: 'repo', + path: '/repo', + kind: 'git', + executionHostId: 'runtime:env-1', + displayName: 'repo', + badgeColor: 'blue', + addedAt: 0 + } + ], + worktreesByRepo: { + repo: [ + makePRRefreshWorktree({ + id: 'receipt-worktree', + repoId: 'repo', + branch: 'receipt', + head: 'head-a', + linkedPR: 12, + hostId: 'runtime:env-1', + runtimeOwnerEnvironmentId: 'env-1' + }) + ] + }, + detectedWorktreesByRepo: {}, + activeWorktreeId: 'receipt-worktree', + activeWorkspaceExecutionHostId: 'runtime:env-1', + visibleReviewWorktreeIds: ['receipt-worktree'], + prCache: { 'runtime:env-1::repo::receipt': { data: seed.pr, fetchedAt: 1_000 } } + }) + const reasons: string[] = [] + const replies: unknown[] = [] + runtimeEnvironmentCall.mockImplementation(async (args) => { + if (args.method !== 'github.prForBranch') { + throw new Error('unexpected fixture RPC') + } + const params = PrForBranch.parse(args.params) + expect(params).toMatchObject({ + repo: 'repo', + branch: 'receipt', + linkedPRNumber: 12, + currentHeadOid: 'head-a', + reason: 'swr', + acceptRevalidationOnly: true, + selected: true + }) + reasons.push(params.reason!) + const result = await rpcRead(args.params) + replies.push(result) + return { id: 'rpc', ok: true, result } + }) + const timerProvider = Promise.withResolvers() + const freshProvider = Promise.withResolvers() + mocks.resolve + .mockReturnValueOnce(timerProvider.promise) + .mockReturnValueOnce(freshProvider.promise) + const scheduler = createVisibleHostedReviewRefreshScheduler() + const update = () => + scheduler.update(getVisibleHostedReviewRefreshTargets(store.getState(), store.getState)) + const unsubscribe = store.subscribe(update) + try { + expect(getVisibleHostedReviewRefreshTargets(store.getState(), store.getState)).toMatchObject([ + { selected: true, intervalMs: 60_000, fetchedAt: 1_000 } + ]) + update() + scheduler.setVisible(true) + await vi.advanceTimersByTimeAsync(60_000) + // Fake timers defer the host's nested zero-delay drain to the next clock tick. + await vi.advanceTimersByTimeAsync(1) + expect(reasons).toEqual(['swr']) + expect(replies).toEqual([]) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + const otherClient = readReceipt('active') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'superseded provider result' + timerProvider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(replies).toMatchObject([{ fetchedAt: 1_000, revalidation: { retryAfterMs: 10_000 } }]) + expect(store.getState().prCache['runtime:env-1::repo::receipt']?.data?.title).toBe('before') + expect(store.getState().prCache['runtime:env-1::repo::receipt']?.fetchedAt).toBe(1_000) + fixture.title = 'completed by another client' + freshProvider.resolve(found()) + await otherClient + await vi.advanceTimersByTimeAsync(9_999) + expect(reasons).toEqual(['swr']) + await vi.advanceTimersByTimeAsync(1) + expect(reasons).toEqual(['swr', 'swr']) + expect(store.getState().prCache['runtime:env-1::repo::receipt']?.data?.title).toBe( + 'completed by another client' + ) + expect(mocks.resolve).toHaveBeenCalledTimes(3) + } finally { + unsubscribe() + scheduler.dispose() + } + }) +}) + +it('applies data-bearing retry replies to both clients and sends selection after an unselected wait', async () => { + const seed = await commands.getRepoPRForBranch( + 'repo', + 'shared', + 12, + null, + false, + 'head-a', + 'active' + ) + if (!('kind' in seed) || seed.kind !== 'found') { + throw new Error('expected initial PR') + } + const clients: { + select: () => void + title: () => string | undefined + fetchedAt: () => number | undefined + dispose: () => void + }[] = [] + const selection: boolean[][] = [[], []] + const receipts: unknown[][] = [[], []] + const reasons: string[] = [] + for (let client = 0; client < 2; client += 1) { + // Separate module graphs retain each renderer's own request coalescer. + vi.resetModules() + const { + createTestStore, + makePRRefreshWorktree, + resetRemoteRuntimeMocks, + runtimeEnvironmentCall + } = await import('../../src/renderer/src/store/github/github-slice-test-harness') + const { getVisibleHostedReviewRefreshTargets } = + await import('../../src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-targets') + const { createVisibleHostedReviewRefreshScheduler } = + await import('../../src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler') + resetRemoteRuntimeMocks() + runtimeEnvironmentCall.mockImplementation(async (args) => { + if (args.method !== 'github.prForBranch') { + throw new Error('unexpected fixture RPC') + } + const { PrForBranch } = + await import('../../src/shared/rpc-contract/github/github-pull-request-params') + const params = PrForBranch.parse(args.params) + const requestClient = 'selector' in args && args.selector === 'env-client-0' ? 0 : 1 + reasons.push(params.reason ?? 'missing') + selection[requestClient].push(params.selected === true) + const result = await rpcRead(args.params) + receipts[requestClient].push(result) + return { id: 'rpc', ok: true, result } + }) + const store = createTestStore() + store.setState({ + repos: [ + { + id: 'repo', + path: '/repo', + kind: 'git', + executionHostId: `runtime:env-client-${client}`, + displayName: 'repo', + badgeColor: 'blue', + addedAt: 0 + } + ], + worktreesByRepo: { + repo: [ + makePRRefreshWorktree({ + id: 'shared-worktree', + repoId: 'repo', + branch: 'shared', + head: 'head-a', + linkedPR: 12, + hostId: `runtime:env-client-${client}`, + runtimeOwnerEnvironmentId: `env-client-${client}` + }) + ] + }, + detectedWorktreesByRepo: {}, + activeWorktreeId: null, + activeWorkspaceExecutionHostId: `runtime:env-client-${client}`, + visibleReviewWorktreeIds: ['shared-worktree'], + prCache: { + [`runtime:env-client-${client}::repo::shared`]: { data: seed.pr, fetchedAt: seed.fetchedAt } + } + }) + const scheduler = createVisibleHostedReviewRefreshScheduler() + const update = () => + scheduler.update(getVisibleHostedReviewRefreshTargets(store.getState(), store.getState)) + const unsubscribe = store.subscribe(update) + update() + scheduler.setVisible(true) + clients.push({ + select: () => store.setState({ activeWorktreeId: 'shared-worktree' }), + title: () => + store.getState().prCache[`runtime:env-client-${client}::repo::shared`]?.data?.title, + fetchedAt: () => + store.getState().prCache[`runtime:env-client-${client}::repo::shared`]?.fetchedAt, + dispose: () => { + unsubscribe() + scheduler.dispose() + } + }) + } + try { + // A second client's fresh result can settle in the same millisecond as the first. + fixture.title = 'changed on another client' + await commands.getRepoPRForBranch('repo', 'shared', 12, null, false, 'head-a', 'active') + mocks.gate.mockResolvedValue(181_000) + await vi.advanceTimersByTimeAsync(120_001) + // The first job learns the quota pause after admission, so its wait ends at the response deadline. + await vi.advanceTimersByTimeAsync(25_000) + expect(selection).toEqual([[false], [false]]) + for (let client = 0; client < 2; client += 1) { + expect(receipts[client]).toMatchObject([ + { + kind: 'found', + pr: { title: 'changed on another client' }, + revalidation: { retryAfterMs: expect.any(Number) } + } + ]) + expect(clients[client].title()).toBe('changed on another client') + expect(clients[client].fetchedAt()).toBe(1_000) + clients[client].select() + } + mocks.gate.mockResolvedValue(null) + await vi.advanceTimersByTimeAsync(70_000) + expect(selection).toEqual([ + [false, true], + [false, true] + ]) + expect(reasons).toEqual(['swr', 'swr', 'swr', 'swr']) + for (const client of clients) { + expect(client.title()).toBe('changed on another client') + expect(client.fetchedAt()).toBe(181_000) + } + expect(mocks.resolve).toHaveBeenCalledTimes(3) + } finally { + for (const client of clients) { + client.dispose() + } + } +}) diff --git a/tests/e2e/pr-refresh-actual-completion.unit.test.ts b/tests/e2e/pr-refresh-actual-completion.unit.test.ts new file mode 100644 index 00000000000..eb2031837bc --- /dev/null +++ b/tests/e2e/pr-refresh-actual-completion.unit.test.ts @@ -0,0 +1,389 @@ +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import { createStore } from 'zustand/vanilla' +import type { AppState } from '../../src/renderer/src/store/types' +import type { + GitHubPRRefreshEvent, + PRRefreshOutcome, + PRSnapshotReply +} from '../../src/shared/github/pull-requests/refresh/pull-request-refresh-types' +import { + makeCandidate, + makePR +} from '../../src/main/github/pull-requests/refresh/pr-refresh-coordinator-test-harness' + +const mocks = vi.hoisted(() => ({ resolve: vi.fn(), send: vi.fn(), gate: vi.fn() })) +vi.mock('../../src/main/github/client/lookup/branch-lookup-resolution', () => ({ + resolvePRForBranchOutcome: mocks.resolve +})) +vi.mock('../../src/main/github/accounts/github-project-account', () => ({ + githubPRPolicyProjects: () => ({ source: null, selection: 0 }), + resolveGitHubProjectAccount: () => null +})) +vi.mock('../../src/main/github/execution/github-operation-slots', () => ({ + acquire: async () => {}, + release: () => {} +})) +vi.mock('../../src/main/github/pull-requests/refresh/pr-refresh-rate-limit-gate', () => ({ + prRefreshRateLimitPausedUntil: mocks.gate +})) +vi.mock('../../src/main/github/pull-requests/refresh/pr-refresh-renderer-port', () => ({ + prRefreshRenderers: () => ({ isRendererOpen: () => true, sendToTrustedUI: mocks.send }) +})) + +let context: Awaited> +beforeEach(async () => { + vi.resetModules() + vi.useFakeTimers() + vi.setSystemTime(1_000) + mocks.resolve.mockReset() + mocks.send.mockReset() + mocks.gate.mockReset().mockResolvedValue(null) + context = await fixture() +}) +afterEach(() => { + vi.clearAllTimers() + vi.useRealTimers() + vi.unstubAllGlobals() +}) + +async function fixture() { + const { createGitHubSlice } = await import('../../src/renderer/src/store/github/github') + const coordinator = + await import('../../src/main/github/pull-requests/refresh/pr-refresh-coordinator') + const { RuntimeHostedReviewCommands } = + await import('../../src/main/runtime/integrations/reviews/runtime-hosted-review-commands') + const candidate = makeCandidate({ + cacheKey: 'repo::feature/test', + repoId: 'repo', + currentHeadOid: 'head-sha' + }) + const repo = { + id: 'repo', + path: '/repo', + kind: 'git' as const, + displayName: 'repo', + badgeColor: '', + addedAt: 0 + } + const commands = new RuntimeHostedReviewCommands({ + resolveRepo: async () => repo, + resolveTarget: async () => ({ repo, repoPath: repo.path }), + getExecutionOptions: () => undefined, + recordCreated: () => {} + }) + const store = createStore()((set, get, api) => ({ + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: This fixture supplies only the state read by real PR event application. + ...({ + settings: {}, + repos: [repo], + worktreesByRepo: { + repo: [{ id: 'wt-1', repoId: 'repo', branch: candidate.branch, head: 'head-sha' }] + }, + prCache: {}, + hostedReviewCache: {}, + prRefreshStates: {}, + prRefreshSequences: {} + } as unknown as AppState), + ...createGitHubSlice(set, get, api) + })) + mocks.send.mockImplementation((_channel, event: GitHubPRRefreshEvent) => { + store.getState().applyGitHubPRRefreshEvent(event) + }) + vi.stubGlobal('window', { + api: { + gh: { + refreshPRNow: (args: Parameters[0]) => + coordinator.refreshPRNow(args.candidate, args.reason, { userRefresh: args.userRefresh }) + } + } + }) + const foreground = (forced = false) => + commands.getRepoPRForBranch( + 'repo', + candidate.branch, + null, + null, + false, + 'head-sha', + 'active', + forced + ) + const found = (title: string): PRRefreshOutcome => ({ + kind: 'found', + fetchedAt: Date.now(), + pr: makePR({ title }) + }) + return { coordinator, candidate, store, foreground, found } +} + +it.each(['queued', 'immediate'] as const)( + 'delivers the accepted external result after a rejected late %s completion', + async (origin) => { + const { coordinator, candidate, store, foreground, found } = context + const old = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(old.promise).mockImplementation(async () => found('current')) + coordinator.reportVisiblePRRefreshCandidates([candidate], 1, 1) + let immediate: Promise | undefined + if (origin === 'immediate') { + immediate = coordinator.refreshPRNow(candidate, 'active') + } + await vi.advanceTimersByTimeAsync(0) + vi.setSystemTime(5_000) + await expect(foreground(origin === 'immediate')).resolves.toMatchObject({ + fetchedAt: 5_000, + pr: { title: 'current' } + }) + vi.setSystemTime(6_000) + old.resolve({ kind: 'no-pr', fetchedAt: 6_000 }) + await immediate + await vi.advanceTimersByTimeAsync(0) + expect(store.getState().prCache[candidate.cacheKey]).toMatchObject({ + fetchedAt: 5_000, + data: { title: 'current' } + }) + expect(store.getState().prRefreshStates[candidate.cacheKey]?.status).not.toBe('in-flight') + const { preparePRBranchRead } = + await import('../../src/main/github/client/lookup/pr-for-branch-outcome') + const prepared = await preparePRBranchRead('/repo', candidate.branch, null, null, null, { + currentHeadOid: 'head-sha' + }) + expect(prepared.cached()?.outcome).toMatchObject({ fetchedAt: 5_000, pr: { title: 'current' } }) + mocks.resolve.mockImplementation(async () => found('next ordinary read')) + await vi.advanceTimersByTimeAsync(118_999) + expect(store.getState().prCache[candidate.cacheKey]?.data?.title).toBe('current') + await vi.advanceTimersByTimeAsync(1) + expect(store.getState().prCache[candidate.cacheKey]).toMatchObject({ + fetchedAt: 125_000, + data: { title: 'next ordinary read' } + }) + } +) + +it('keeps the accepted external failure and gate when the old local success completes', async () => { + const { coordinator, candidate, store, foreground, found } = context + const old = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(old.promise).mockImplementation(async () => ({ + kind: 'upstream-error', + fetchedAt: Date.now(), + errorType: 'rate_limited', + message: 'wait', + retryDisabledUntil: 305_000 + })) + coordinator.reportVisiblePRRefreshCandidates([candidate], 1, 1) + await vi.advanceTimersByTimeAsync(0) + vi.setSystemTime(5_000) + await expect(foreground()).resolves.toMatchObject({ errorType: 'rate_limited' }) + vi.setSystemTime(6_000) + old.resolve(found('obsolete')) + await vi.advanceTimersByTimeAsync(0) + expect(store.getState().prCache[candidate.cacheKey]?.data).toBeUndefined() + expect(store.getState().prRefreshStates[candidate.cacheKey]).toMatchObject({ + status: 'error', + retryDisabledUntil: 305_000, + nextAutoRetryAt: 305_000 + }) + await expect(coordinator.refreshPRNow(candidate)).resolves.toMatchObject({ + errorType: 'rate_limited' + }) + mocks.resolve.mockImplementation(async () => found('after gate')) + await vi.advanceTimersByTimeAsync(298_999) + expect(store.getState().prCache[candidate.cacheKey]?.data).toBeUndefined() + await vi.advanceTimersByTimeAsync(1) + expect(store.getState().prCache[candidate.cacheKey]).toMatchObject({ + fetchedAt: 305_000, + data: { title: 'after gate' } + }) +}) + +it.each(['queued', 'immediate'] as const)( + 'finishes rejected %s loading and later delivers the pending actual result', + async (origin) => { + const { coordinator, candidate, store, foreground, found } = context + const old = Promise.withResolvers() + const fresh = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(old.promise).mockReturnValueOnce(fresh.promise) + coordinator.reportVisiblePRRefreshCandidates([candidate], 1, 1) + const immediate = origin === 'immediate' ? coordinator.refreshPRNow(candidate, 'active') : null + await vi.advanceTimersByTimeAsync(0) + vi.setSystemTime(5_000) + const external = foreground(origin === 'immediate') + await vi.advanceTimersByTimeAsync(0) + vi.setSystemTime(6_000) + old.resolve({ kind: 'no-pr', fetchedAt: 6_000 }) + if (immediate) { + await expect(immediate).resolves.toEqual({ + revalidation: { invalidated: true, retryAfterMs: 10_000 } + }) + } + await vi.advanceTimersByTimeAsync(0) + expect(store.getState().prCache[candidate.cacheKey]).toBeUndefined() + expect(store.getState().prRefreshStates[candidate.cacheKey]?.status).toBe('skipped') + await vi.advanceTimersByTimeAsync(1_000) + fresh.resolve(found('current')) + await external + await vi.advanceTimersByTimeAsync(4_000) + expect(store.getState().prCache[candidate.cacheKey]).toMatchObject({ + fetchedAt: 7_000, + data: { title: 'current' } + }) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + } +) + +it('keeps rejected raw data out of the actual local action reply writer', async () => { + const { coordinator, candidate, store, foreground, found } = context + const old = Promise.withResolvers() + const fresh = Promise.withResolvers() + store.setState({ + prCache: { [candidate.cacheKey]: { fetchedAt: 500, data: makePR({ title: 'displayed' }) } } + }) + mocks.resolve.mockReturnValueOnce(old.promise).mockReturnValueOnce(fresh.promise) + coordinator.reportVisiblePRRefreshCandidates([candidate], 1, 1) + const action = store.getState().fetchPRForBranch('/repo', candidate.branch, { + repoId: 'repo', + worktreeId: 'wt-1', + reason: 'active', + force: true + }) + await vi.advanceTimersByTimeAsync(0) + vi.setSystemTime(5_000) + const external = foreground(true) + await vi.advanceTimersByTimeAsync(0) + vi.setSystemTime(6_000) + old.resolve({ kind: 'no-pr', fetchedAt: 6_000 }) + await expect(action).resolves.toMatchObject({ title: 'displayed' }) + expect(store.getState().prCache[candidate.cacheKey]).toMatchObject({ + fetchedAt: 500, + data: { title: 'displayed' } + }) + expect(store.getState().prRefreshStates[candidate.cacheKey]?.status).not.toBe('in-flight') + await vi.advanceTimersByTimeAsync(1_000) + fresh.resolve(found('current')) + await external + await vi.advanceTimersByTimeAsync(4_000) + expect(store.getState().prCache[candidate.cacheKey]).toMatchObject({ + fetchedAt: 500, + data: { title: 'displayed' } + }) + await vi.advanceTimersByTimeAsync(109_499) + expect(store.getState().prCache[candidate.cacheKey]).toMatchObject({ + fetchedAt: 500, + data: { title: 'displayed' } + }) + await vi.advanceTimersByTimeAsync(1) + expect(store.getState().prCache[candidate.cacheKey]).toMatchObject({ + fetchedAt: 7_000, + data: { title: 'current' } + }) +}) + +it.each([ + { duringQuota: false, retryAt: 307_000, failureAt: 7_000, remaining: 295_999 }, + { duringQuota: true, retryAt: 312_000, failureAt: 12_000, remaining: 299_999 } +])( + 'honors a later actual Retry-After error (during quota: $duringQuota)', + async ({ duringQuota, retryAt, failureAt, remaining }) => { + const { coordinator, candidate, store, foreground, found } = context + const old = Promise.withResolvers() + const fresh = Promise.withResolvers() + const observe = vi.fn() + const remove = coordinator.addPRRefreshOutcomeObserver(observe) + mocks.resolve.mockReturnValueOnce(old.promise).mockReturnValueOnce(fresh.promise) + try { + coordinator.reportVisiblePRRefreshCandidates([candidate], 1, 1) + await vi.advanceTimersByTimeAsync(0) + vi.setSystemTime(5_000) + const external = foreground() + await vi.advanceTimersByTimeAsync(0) + vi.setSystemTime(6_000) + old.resolve({ kind: 'no-pr', fetchedAt: 6_000 }) + await vi.advanceTimersByTimeAsync(0) + expect(store.getState().prRefreshStates[candidate.cacheKey]?.status).toBe('skipped') + const quota = Promise.withResolvers() + if (duringQuota) { + mocks.gate.mockReturnValueOnce(quota.promise) + await vi.advanceTimersByTimeAsync(6_000) + expect(mocks.gate).toHaveBeenCalledTimes(2) + } else { + await vi.advanceTimersByTimeAsync(1_000) + } + fresh.reject(new Error('HTTP 429: secondary rate limit\nRetry-After: 300')) + await expect(external).resolves.toMatchObject({ + errorType: 'rate_limited', + fetchedAt: failureAt, + retryDisabledUntil: retryAt + }) + mocks.resolve.mockImplementation(async () => found('after actual gate')) + if (duringQuota) { + quota.resolve(null) + await vi.advanceTimersByTimeAsync(0) + } else { + await vi.advanceTimersByTimeAsync(4_000) + } + expect(mocks.resolve).toHaveBeenCalledTimes(2) + expect(observe).toHaveBeenCalledTimes(1) + expect(store.getState().prRefreshStates[candidate.cacheKey]).toMatchObject({ + status: 'error', + nextAutoRetryAt: retryAt, + retryDisabledUntil: retryAt + }) + await vi.advanceTimersByTimeAsync(remaining) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + await vi.advanceTimersByTimeAsync(1) + expect(store.getState().prCache[candidate.cacheKey]).toMatchObject({ + fetchedAt: retryAt, + data: { title: 'after actual gate' } + }) + expect(mocks.resolve).toHaveBeenCalledTimes(3) + expect(observe).toHaveBeenCalledTimes(2) + } finally { + remove() + } + } +) + +it('restores an overdue watch turn after one ordinary actual read', async () => { + const { coordinator, found } = context + vi.setSystemTime(241_000) + const starts: string[] = [] + mocks.resolve.mockImplementation(async ({ branchName }) => { + starts.push(branchName) + return found('current') + }) + const candidate = (branch: string) => + makeCandidate({ + branch, + cacheKey: `/repo::${branch}`, + currentHeadOid: 'head-sha' + }) + coordinator.schedulePRWatchRead({ prKey: 'github.com/acme/repo#1' }, candidate('watch-a'), 1_000) + coordinator.schedulePRWatchRead({ prKey: 'github.com/acme/repo#2' }, candidate('watch-b'), 2_000) + coordinator.reportVisiblePRRefreshCandidates( + [candidate('ordinary-a'), candidate('ordinary-b')], + 1, + 1 + ) + await vi.advanceTimersByTimeAsync(20_000) + expect(starts).toEqual(['watch-a', 'ordinary-a', 'watch-b']) +}) + +it('admits only the still-owned equal-sequence terminal event', () => { + const { store, candidate } = context + const aliases = [candidate] + const apply = store.getState().applyGitHubPRRefreshEvent + apply({ aliases, reason: 'active', sequence: 1, status: 'in-flight', requestStartedAt: 1_000 }) + apply({ aliases, reason: 'active', sequence: 2, status: 'in-flight', requestStartedAt: 2_000 }) + apply({ aliases, reason: 'active', sequence: 1, status: 'skipped', skippedReason: 'fresh' }) + expect(store.getState().prRefreshStates[candidate.cacheKey]?.status).toBe('in-flight') + apply({ + aliases, + reason: 'active', + sequence: 2, + outcome: { kind: 'upstream-error', errorType: 'network', fetchedAt: 3_000, message: 'offline' } + }) + apply({ aliases, reason: 'active', sequence: 2, status: 'skipped', skippedReason: 'fresh' }) + expect(store.getState().prRefreshStates[candidate.cacheKey]).toMatchObject({ + status: 'error', + message: 'offline' + }) +}) diff --git a/tests/e2e/runtime-hosted-pr-snapshots-perf.unit.test.ts b/tests/e2e/runtime-hosted-pr-snapshots-perf.unit.test.ts new file mode 100644 index 00000000000..db4bd7080c0 --- /dev/null +++ b/tests/e2e/runtime-hosted-pr-snapshots-perf.unit.test.ts @@ -0,0 +1,247 @@ +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import { makePR } from '../../src/main/github/pull-requests/refresh/pr-refresh-coordinator-test-harness' +import type { PRRefreshOutcome } from '../../src/shared/github/pull-requests/refresh/pull-request-refresh-types' +import type { VisibleHostedReviewRefreshTarget as Target } from '../../src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler' +import type { RuntimeHostedReviewCommands } from '../../src/main/runtime/integrations/reviews/runtime-hosted-review-commands' + +const mocks = vi.hoisted(() => ({ resolve: vi.fn(), account: vi.fn(), gate: vi.fn() })) +vi.mock('../../src/main/github/accounts/github-project-account', () => ({ + githubPRPolicyProjects: () => ({ source: null, selection: String(mocks.account()) }), + resolveGitHubProjectAccount: mocks.account +})) +vi.mock('../../src/main/github/execution/github-operation-slots', () => ({ + acquire: async () => {}, + release: () => {} +})) +vi.mock('../../src/main/github/client/lookup/branch-lookup-resolution', () => ({ + resolvePRForBranchOutcome: mocks.resolve +})) +vi.mock('../../src/main/github/pull-requests/refresh/pr-refresh-rate-limit-gate', () => ({ + prRefreshRateLimitPausedUntil: mocks.gate +})) + +let commands: RuntimeHostedReviewCommands +let title = 'before' +const found = (): PRRefreshOutcome => ({ + kind: 'found', + fetchedAt: Date.now(), + pr: makePR({ + title, + headSha: 'head-a', + checksStatus: 'success', + checksReadComplete: true, + checksFinal: true, + autoMergeEnabled: false, + mergeQueueRequired: false + }) +}) +const read = async ( + branch: string, + reason: 'active' | 'visible' | 'swr' | 'manual' = 'swr', + head = 'head-a' +): Promise => { + const reply = await commands.getRepoPRForBranch( + 'repo', + branch, + null, + null, + false, + head, + reason, + undefined, + undefined, + undefined, + true + ) + if (!('kind' in reply)) { + throw new Error('Stable benchmark ownership was invalidated') + } + return reply +} + +beforeEach(async () => { + vi.resetModules() + vi.useFakeTimers() + vi.setSystemTime(1_000) + title = 'before' + mocks.resolve.mockReset().mockImplementation(async () => found()) + mocks.account.mockReset().mockReturnValue(null) + mocks.gate.mockReset().mockResolvedValue(null) + const { RuntimeHostedReviewCommands: Commands } = + await import('../../src/main/runtime/integrations/reviews/runtime-hosted-review-commands') + const repo = { + id: 'repo', + path: '/repo', + kind: 'git' as const, + displayName: 'repo', + badgeColor: 'blue', + addedAt: 0 + } + commands = new Commands({ + resolveRepo: async () => repo, + resolveTarget: async () => ({ repo, repoPath: repo.path }), + getExecutionOptions: () => undefined, + recordCreated: () => {} + }) +}) +afterEach(() => { + vi.clearAllTimers() + vi.useRealTimers() +}) + +it.each([ + { rows: 14, clients: 2 }, + { rows: 14, clients: 20 }, + { rows: 140, clients: 2 } +])( + 'bounds shared polling for $rows rows and $clients clients over thirty minutes', + async ({ rows, clients }) => { + const { createVisibleHostedReviewRefreshScheduler } = + await import('../../src/renderer/src/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler') + const start = Date.now() + const spans: { branch: string; startedAt: number; completedAt: number }[] = [] + const deliveries: number[] = [] + const admissionDelays: number[] = [] + const pendingDemand = new Map() + const settledAt: number[] = [] + let requests = 0 + let receipts = 0 + mocks.resolve.mockImplementation(async ({ branchName }) => { + const startedAt = Date.now() + const demand = pendingDemand.get(branchName) + if (demand !== undefined) { + admissionDelays.push(startedAt - demand) + } + pendingDemand.delete(branchName) + // ast-grep-ignore: test-no-real-time-wait -- Fake-clock provider delay exercises host admission and completion. + await new Promise((resolve) => setTimeout(resolve, 1_000)) + const pending = Number(branchName.slice(4)) < 2 && Date.now() - start < 600_000 + const outcome: PRRefreshOutcome = { + kind: 'found', + fetchedAt: Date.now(), + pr: makePR({ + title: pending ? 'running' : 'settled', + headSha: 'head-a', + checksStatus: pending ? 'pending' : 'success', + checksReadComplete: true, + checksFinal: !pending, + autoMergeEnabled: false, + mergeQueueRequired: false + }) + } + spans.push({ branch: branchName, startedAt, completedAt: Date.now() }) + return outcome + }) + const warm = Promise.all(Array.from({ length: rows }, (_, i) => read(`row-${i}`, 'active'))) + await vi.advanceTimersByTimeAsync(1_000) + const initial = await warm + const schedulers = Array.from({ length: clients }, (_, client) => { + const scheduler = createVisibleHostedReviewRefreshScheduler() + const cache = initial.map((outcome) => outcome.fetchedAt) + const delivered = new Set(initial.map((outcome, row) => `${row}:${outcome.fetchedAt}`)) + const sawSettled = new Set() + const targets = (): Target[] => + cache.map((fetchedAt, row) => ({ + key: `row-${row}`, + revision: 'head-a', + fetchedAt, + intervalMs: row === 0 ? 60_000 : 120_000, + selected: row === 0, + refresh: async (_force, signal, intent) => { + requests += 1 + const branch = `row-${row}` + if (!pendingDemand.has(branch)) { + pendingDemand.set(branch, Date.now()) + } + const outcome = await commands.getRepoPRForBranch( + 'repo', + `row-${row}`, + null, + null, + false, + 'head-a', + intent === 'discovery' ? 'active' : 'swr', + false, + undefined, + signal, + true + ) + if (!('kind' in outcome)) { + throw new Error('Stable benchmark ownership was invalidated') + } + if (outcome.kind !== 'upstream-error') { + cache[row] = outcome.fetchedAt + if (outcome.revalidation) { + receipts += 1 + } else { + pendingDemand.delete(branch) + const completion = `${row}:${outcome.fetchedAt}` + if (!delivered.has(completion)) { + delivered.add(completion) + deliveries.push(Date.now() - outcome.fetchedAt) + } + if ( + outcome.kind === 'found' && + row < 2 && + outcome.pr.title === 'settled' && + !sawSettled.has(row) + ) { + sawSettled.add(row) + settledAt.push(Date.now() - start - 600_000) + } + } + scheduler.update(targets()) + } + return { + succeeded: outcome.kind !== 'upstream-error', + retryAfterMs: outcome.revalidation?.retryAfterMs + } + } + })) + scheduler.update(targets()) + setTimeout(() => scheduler.setVisible(true), (client % 2) * 5_000) + return scheduler + }) + await vi.advanceTimersByTimeAsync(30 * 60_000) + for (const scheduler of schedulers) { + scheduler.dispose() + } + const atHide = spans.length + await vi.advanceTimersByTimeAsync(6 * 60_000) + const drained = spans.length + await vi.advanceTimersByTimeAsync(5 * 60_000) + expect(spans.length).toBe(drained) + // Timers share one twenty-start/five-minute budget, independent of client multiplicity. + const timed = spans.slice(rows) + for (const span of timed) { + expect( + timed.filter( + (other) => other.startedAt > span.startedAt - 300_000 && other.startedAt <= span.startedAt + ).length + ).toBeLessThanOrEqual(20) + } + expect(timed.length).toBeLessThanOrEqual(140) + expect( + spans.some((span) => span.branch === 'row-0' && span.completedAt - start >= 600_000) + ).toBe(true) + console.info( + 'SERVE_PR_BENCH', + JSON.stringify({ + clients, + rows, + simulatedMinutes: 30, + providerReads: spans.length, + atHide, + drainedAfterHide: drained - atHide, + snapshotRequests: requests, + deferredReceipts: receipts, + maxProviderMs: Math.max(...spans.map((s) => s.completedAt - s.startedAt)), + maxCompletionToFirstClientApplicationMs: Math.max(0, ...deliveries), + maxAdmissionDelayMs: Math.max(0, ...admissionDelays), + settledRunningRowsApplied: settledAt.length, + maxCiFinishToApplicationMs: Math.max(0, ...settledAt) + }) + ) + }, + 60_000 +)