🔀 feat: Show the Conversation Pull Request in the Sidebar List (#16815)

* feat: Show the Conversation Pull Request in the Sidebar List

Add a batch lookup route and a sidebar row mark: the state icon with the CI dot, opening the same card beside the row on hover. The row keeps the conversation's own title.

* fix: Address Review Findings on the Sidebar Pull Request Batch and Row Mark

Cap the whole batch with a configurable deadline, make the row mark a keyboard-reachable button that opens the card on focus, refresh rows when the window regains focus through the shared batch, and show a retryable failure state instead of treating a failed first lookup as no pull request.

* fix: Send Sidebar Pull Request Batches One at a Time

Queue every batch request behind the one before it, so an oversized list or an overlapping flush cannot multiply the server's configured lookup concurrency, and bound how long one request may hold the queue so a request that never answers cannot stop the rest.

* fix: Keep the Batch Request Timeout Above the Server Deadline

Default the client's request timeout to just past the longest batchTimeoutSeconds a deployment may configure, so a queued request never starts while the previous request's server-side lookups are still running.

* fix: Keep Batch Lookups Inside the Concurrency Limit and Per-Entry Failures Per Entry

Share one lookup limiter across every batch request so lookups that outlive a request's deadline keep their slot, answer a missing token only for the conversations that needed it, and point the row at its pull request description only while that text is in the page.

* fix: Bound Batch Reads, Scope Concurrency Per Credential, and Gate the Sidebar on the Batch Version

Start the batch deadline when the request arrives and bound the config and lane reads, count lookup slots per credential, advertise a batch route version and ask only a server that has it, take no row space for an absent mark, show a failed refresh over a cached empty answer, keep a malformed response from stalling the request queue, and repair the lane recording tests that still used a mock removed on dev.

* fix: Move Capability Resolution Into the Package and Harden the Batch Route and Queue

Resolve the pull request startup capabilities in packages/api so the route stays wiring only, bound the config and lane reads by the configured deadline, merge identical lanes before taking limiter slots, fall back to the single route when a replica has no batch route, and dispose the batch queue when the signed-in user changes.

* fix: Abort the Batch Fallback With Its Request So the Queue Never Overlaps It

Give the batch fetcher the request's abort signal and pass it to every single-route call, abort a request that times out or is disposed, and stop the fallback workers from starting more calls once it has.

* fix: Forward the Abort Signal to the Batch POST

Pass the request's abort signal through the batch fetcher, the data-provider call and Axios, so a batch request that times out or is disposed is cancelled instead of continuing beside the next one, and settle a request promptly when it is aborted.

* fix: Close the Pull Request Lookup Follow-Ups and Cancel Abandoned Sidebar Rows
This commit is contained in:
Marco Beretta 2026-10-07 12:36:57 +02:00 • committed by GitHub
parent cdf2e94dcf
commit caeb786e97
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
47 changed files with 4260 additions and 128 deletions

View file

@ -20,7 +20,12 @@ export interface ConversationCodeEnvironmentDecision {
export type StoredConversationDecision = Pick<
TConversation,
'conversationId' | 'codeEnvironmentMode' | 'codeWorkspaces'
> & { codeEnvironmentRevision?: number };
> & {
codeEnvironmentRevision?: number;
/** Read in the same snapshot as the workspaces, so a lane report can be fenced by exactly the
* attachment the run was admitted on. Absent when the read did not carry it. */
codeAttachmentEpoch?: number;
};
function canonicalSelections(selections: CodeWorkspaceSelection[]): CodeWorkspaceSelection[] {
return canonicalizeCodeWorkspaceSelections(selections);

View file

@ -466,6 +466,41 @@ describe('createLaneGitRecorder target conversation', () => {
});
});
it('fences an ordinary conversation by the epoch of the snapshot the run was admitted on', async () => {
const getConvoLaneContext = jest.fn().mockResolvedValue({ codeAttachmentEpoch: 2 });
const { record, setConvoLaneGit } = build({ getConvoLaneContext, admittedEpoch: 0 });
await record(laneGit);
expect(written(setConvoLaneGit)[0].workspace.epoch).toBe(0);
});
it('keeps an admitted epoch of zero, rather than reading a newer one in its place', async () => {
const getConvoLaneContext = jest.fn().mockResolvedValue({ codeAttachmentEpoch: 5 });
const { record, setConvoLaneGit } = build({ getConvoLaneContext, admittedEpoch: 0 });
await record(laneGit);
expect(written(setConvoLaneGit)[0].workspace.epoch).toBe(0);
});
it('falls back to the route read when the admitted decision carried no epoch', async () => {
const getConvoLaneContext = jest.fn().mockResolvedValue({ codeAttachmentEpoch: 3 });
const { record, setConvoLaneGit } = build({ getConvoLaneContext });
await record(laneGit);
expect(written(setConvoLaneGit)[0].workspace.epoch).toBe(3);
});
it("fences a subagent thread by its root's epoch, which the admitted decision cannot know", async () => {
const getConvoLaneContext = jest.fn().mockResolvedValue({
subagentThread: { rootConversationId: 'visible-root' },
codeAttachmentEpoch: 6,
});
const { record, setConvoLaneGit } = build({
conversationId: 'child-thread',
getConvoLaneContext,
admittedEpoch: 1,
});
await record(laneGit);
expect(written(setConvoLaneGit)[0].workspace.epoch).toBe(6);
});
it('records an ordinary conversation on itself', async () => {
const getConvoLaneContext = jest.fn().mockResolvedValue({ codeAttachmentEpoch: 0 });
const { record, setConvoLaneGit } = build({ conversationId: 'plain', getConvoLaneContext });

View file

@ -79,6 +79,7 @@ export async function createLaneGitRecorder({
conversationId,
repo,
workspace,
admittedEpoch,
getConvoLaneContext,
reserveConvoLaneGitSeq,
setConvoLaneGit,
@ -89,6 +90,13 @@ export async function createLaneGitRecorder({
conversationId: string | undefined;
repo?: string;
workspace?: { environmentId: string; workspaceId: string };
/**
* The attachment epoch read in the same snapshot as the workspace this run was admitted on (the
* admitted decision carries both). For a conversation that is its own lane it is the epoch the
* fence uses, so an A to B to A move between two separate reads cannot satisfy both. A subagent
* thread writes to its root, whose epoch only the route read can supply, so it still reads it.
*/
admittedEpoch?: number;
getConvoLaneContext?: LaneContextReader;
reserveConvoLaneGitSeq: LaneSeqReserver;
setConvoLaneGit: LaneGitWriter;
@ -103,7 +111,7 @@ export async function createLaneGitRecorder({
placed = {
conversationId: root || conversationId,
required: Boolean(root),
epoch: context?.codeAttachmentEpoch ?? 0,
epoch: (root ? undefined : admittedEpoch) ?? context?.codeAttachmentEpoch ?? 0,
};
} catch (error) {
logger.warn('[LaneGit] Failed to place a lane recorder', getSafeErrorMetadata(error));

View file

@ -0,0 +1,36 @@
import { PULL_REQUEST_BATCH_VERSION } from 'librechat-data-provider';
import type { AppConfig } from '@librechat/data-schemas';
import { resolvePullRequestCapabilities } from './capabilities';
const configWith = (pullRequests?: Record<string, unknown>) =>
({ endpoints: { agents: pullRequests ? { pullRequests } : {} } }) as unknown as AppConfig;
describe('resolvePullRequestCapabilities', () => {
it('advertises the flag, the batch version and the lookup limit together when the feature is on', () => {
expect(resolvePullRequestCapabilities(configWith({ enabled: true }))).toEqual({
pullRequestsEnabled: true,
pullRequestsBatchVersion: PULL_REQUEST_BATCH_VERSION,
pullRequestsMaxConcurrentLookups: 4,
});
});
it('advertises the configured lookup limit', () => {
expect(
resolvePullRequestCapabilities(configWith({ enabled: true, maxConcurrentLookups: 2 })),
).toMatchObject({ pullRequestsMaxConcurrentLookups: 2 });
});
it.each([
['no config', undefined],
['a null config', null],
['a config without endpoints', {} as AppConfig],
['no pull request settings', configWith()],
['the feature switched off', configWith({ enabled: false })],
['a non-true value', configWith({ enabled: 'yes' })],
])('advertises neither the flag nor a version with %s', (_label, appConfig) => {
const capabilities = resolvePullRequestCapabilities(appConfig);
expect(capabilities).toEqual({ pullRequestsEnabled: false });
expect(capabilities).not.toHaveProperty('pullRequestsBatchVersion');
expect(capabilities).not.toHaveProperty('pullRequestsMaxConcurrentLookups');
});
});

View file

@ -0,0 +1,33 @@
import { EModelEndpoint, PULL_REQUEST_BATCH_VERSION } from 'librechat-data-provider';
import type { TAgentsEndpoint } from 'librechat-data-provider';
import type { AppConfig } from '@librechat/data-schemas';
export type PullRequestCapabilities = {
pullRequestsEnabled: boolean;
/** Present only with the feature on, so a client never sees a version without the flag. */
pullRequestsBatchVersion?: typeof PULL_REQUEST_BATCH_VERSION;
/** The lookups one batch runs at once, so a client that falls back to the single route can
* keep to the same limit while a replica without the batch route is still serving it. */
pullRequestsMaxConcurrentLookups?: number;
};
/**
* What the startup config tells a client about pull requests: whether the deployment turned the
* feature on, and, once it has, which version of the batch route this server serves, so the
* sidebar asks only a server that has it.
*/
export function resolvePullRequestCapabilities(
appConfig: Pick<AppConfig, 'endpoints'> | null | undefined,
): PullRequestCapabilities {
const agents = appConfig?.endpoints?.[EModelEndpoint.agents] as TAgentsEndpoint | undefined;
const enabled = agents?.pullRequests?.enabled === true;
return {
pullRequestsEnabled: enabled,
...(enabled
? {
pullRequestsBatchVersion: PULL_REQUEST_BATCH_VERSION,
pullRequestsMaxConcurrentLookups: agents?.pullRequests?.maxConcurrentLookups ?? 4,
}
: {}),
};
}

View file

@ -486,9 +486,9 @@ describe('matching the recorded head', () => {
expect(compares.length).toBeLessThanOrEqual(3);
});
it.each([null, undefined])('does not compare when no head was recorded (%s)', async (head) => {
it('does not compare when the caller never recorded a head', async () => {
const { source, fetchFn } = sourceFor(routes('diverged'));
await expect(findWith(source, head)).resolves.toMatchObject({ number: 7 });
await expect(findWith(source, undefined)).resolves.toMatchObject({ number: 7 });
expect(fetchFn.mock.calls.some(([url]) => String(url).includes('/compare/'))).toBe(false);
});
@ -677,3 +677,260 @@ describe('a secondary rate limit without limiting headers', () => {
expect(String(error.message)).not.toContain('secondary');
});
});
describe('a 429 that carries no wait', () => {
const body = (message: string) => () =>
new Response(JSON.stringify({ message }), { status: 429 });
it('is read for the secondary limit message and waits at least a minute', async () => {
const { source } = sourceFor({
'/pulls?state=open': body('You have exceeded a secondary rate limit.'),
});
const error = await find(source).catch((caught) => caught);
expect(error.code).toBe('RATE_LIMITED');
expect(error.retryAfterMs).toBeGreaterThanOrEqual(60_000);
});
it('keeps the wait GitHub named, whatever the body says', async () => {
const { source } = sourceFor({
'/pulls?state=open': () =>
new Response(JSON.stringify({ message: 'secondary rate limit' }), {
status: 429,
headers: { 'retry-after': '7' },
}),
});
const error = await find(source).catch((caught) => caught);
expect(error.retryAfterMs).toBe(7_000);
});
it('leaves the wait unnamed for an ordinary 429', async () => {
const { source } = sourceFor({ '/pulls?state=open': body('Too many requests') });
const error = await find(source).catch((caught) => caught);
expect(error.code).toBe('RATE_LIMITED');
expect(error.retryAfterMs).toBeUndefined();
});
});
describe('check runs of a head that vanished', () => {
it('is an upstream error, not a pull request with no checks', async () => {
const { source } = sourceFor({
'/pulls?state=open': () => json(listed()),
'/pulls/7': () => json(pull()),
'/check-runs': () => new Response('', { status: 404 }),
});
await expect(find(source)).rejects.toMatchObject({ code: 'UPSTREAM_ERROR' });
});
it('is an upstream error when a later page of check runs disappears', async () => {
let calls = 0;
const { source } = sourceFor({
'/pulls?state=open': () => json(listed()),
'/pulls/7': () => json(pull()),
'/check-runs': () =>
++calls === 1
? json({
total_count: 150,
check_runs: Array.from({ length: 100 }, () => ({
status: 'completed',
conclusion: 'success',
})),
})
: new Response('', { status: 404 }),
});
await expect(find(source)).rejects.toMatchObject({ code: 'UPSTREAM_ERROR' });
});
});
describe('a lane with a branch and no commit yet', () => {
it('matches nothing, so an earlier incarnation of the branch name is never shown', async () => {
const { source, fetchFn } = sourceFor({
'/pulls?state=open': () => json([]),
'/pulls?state=closed': () => json([{ number: 9, state: 'closed', head: { sha } }]),
'/pulls/9': () => json(pull({ number: 9, state: 'closed', merged: true })),
'/check-runs': () => json({ check_runs: [] }),
});
await expect(
source.find({ repo: 'o/r', branch: 'feat/x', head: null, token: 't' }),
).resolves.toBeNull();
expect(fetchFn).not.toHaveBeenCalled();
});
it('still matches by branch when the caller never recorded a head', async () => {
const { source } = sourceFor({
'/pulls?state=open': () => json(listed()),
'/pulls/7': () => json(pull()),
'/check-runs': () => json({ check_runs: [] }),
});
await expect(find(source)).resolves.toMatchObject({ number: 7 });
});
});
describe('open pull requests before closed ones, from any repository', () => {
const recorded = 'c'.repeat(40);
it('prefers an open pull request from a fork over a closed one in the base repository', async () => {
const { source } = sourceFor({
'/pulls?state=open': () => json([]),
'/pulls?state=closed': () => json([{ number: 5, state: 'closed', head: { sha: recorded } }]),
'/pulls?per_page': () =>
json([{ number: 12, state: 'open', head: { sha: recorded, ref: 'feat/x' } }]),
'/pulls/12': () => json(pull({ number: 12, head: { sha: recorded } })),
'/pulls/5': () => json(pull({ number: 5, state: 'closed', head: { sha: recorded } })),
'/check-runs': () => json({ total_count: 0, check_runs: [] }),
});
await expect(
source.find({ repo: 'o/r', branch: 'feat/x', head: recorded, token: 't' }),
).resolves.toMatchObject({ number: 12, state: 'open' });
});
it('still takes a closed pull request when no open one matches anywhere', async () => {
const { source } = sourceFor({
'/pulls?state=open': () => json([]),
'/pulls?state=closed': () => json([{ number: 5, state: 'closed', head: { sha: recorded } }]),
'/pulls?per_page': () => json([]),
'/pulls/5': () =>
json(pull({ number: 5, state: 'closed', merged: true, head: { sha: recorded } })),
'/check-runs': () => json({ total_count: 0, check_runs: [] }),
});
await expect(
source.find({ repo: 'o/r', branch: 'feat/x', head: recorded, token: 't' }),
).resolves.toMatchObject({ number: 5, state: 'merged' });
});
});
describe('the checks of a fork pull request', () => {
const recorded = 'c'.repeat(40);
const routes = () => ({
'/pulls?state=open': () => json([]),
'/pulls?state=closed': () => json([]),
'/pulls?per_page': () =>
json([{ number: 12, state: 'open', head: { sha: recorded, ref: 'feat/x' } }]),
'/pulls/12': () =>
json(
pull({
number: 12,
head: { sha: recorded, repo: { full_name: 'fork-owner/r' } },
}),
),
'/check-runs': () =>
json({ total_count: 1, check_runs: [{ status: 'completed', conclusion: 'failure' }] }),
});
const run = (
source: ReturnType<typeof createGitHubPullRequestSource>,
allowedRepositories?: string[],
) =>
source.find({
repo: 'o/r',
branch: 'feat/x',
head: recorded,
token: 't',
allowedRepositories,
});
const checkUrls = (fetchFn: jest.Mock) =>
fetchFn.mock.calls.map(([url]) => String(url)).filter((url) => url.includes('/check-runs'));
it('reads them from the fork when an administrator allowed that repository', async () => {
const { source, fetchFn } = sourceFor(routes());
await expect(run(source, ['o/r', 'fork-owner/*'])).resolves.toMatchObject({
checks: 'failing',
});
expect(checkUrls(fetchFn)[0]).toContain('/repos/fork-owner/r/commits/');
});
it('never sends the token to a fork nobody allowed, and reads the base repository', async () => {
const { source, fetchFn } = sourceFor(routes());
await run(source, ['o/r']);
expect(checkUrls(fetchFn)[0]).toContain('/repos/o/r/commits/');
expect(fetchFn.mock.calls.some(([url]) => String(url).includes('fork-owner'))).toBe(false);
});
it('reads the base repository when the head lives there', async () => {
const { source, fetchFn } = sourceFor({
...routes(),
'/pulls/12': () =>
json(pull({ number: 12, head: { sha: recorded, repo: { full_name: 'O/R' } } })),
});
await run(source, ['o/r', 'fork-owner/*']);
expect(checkUrls(fetchFn)[0]).toContain('/repos/o/r/commits/');
});
});
describe('reading more than one page of candidates', () => {
const recorded = 'c'.repeat(40);
const item = (number: number, sha_: string) => ({ number, state: 'open', head: { sha: sha_ } });
const pageOf = (url: string) => Number(new URL(url).searchParams.get('page') ?? '1');
function paged(pages: unknown[][]) {
const fetchFn = jest.fn(async (input: string) => {
const url = String(input);
if (url.includes('/pulls?state=open')) return json(pages[pageOf(url) - 1] ?? []);
if (url.includes('/pulls?state=closed')) return json([]);
if (url.includes('/compare/')) {
return json({ status: url.includes(`...${'d'.repeat(40)}`) ? 'ahead' : 'diverged' });
}
if (url.includes('/pulls/')) {
return json(
pull({ number: Number(url.split('/pulls/')[1]), head: { sha: 'd'.repeat(40) } }),
);
}
if (url.includes('/check-runs')) return json({ total_count: 0, check_runs: [] });
if (url.includes('/commits/')) return json([]);
throw new Error(`unexpected ${url}`);
});
return { fetchFn, source: createGitHubPullRequestSource({ fetchFn }) };
}
const first = Array.from({ length: 2 }, (_, i) => item(i + 1, 'e'.repeat(40)));
const second = [item(30, 'd'.repeat(40))];
const limits = { maxCandidatePullRequests: 2, maxHeadComparisons: 5 };
it('reaches an older matching pull request on a later page within the budget', async () => {
const { source } = paged([first, second]);
await expect(
source.find({
repo: 'o/r',
branch: 'feat/x',
head: recorded,
token: 't',
limits: { ...limits, maxCandidatePages: 2 },
}),
).resolves.toMatchObject({ number: 30 });
});
it('stops at one page by default, as before', async () => {
const { source, fetchFn } = paged([first, second]);
await expect(
source.find({ repo: 'o/r', branch: 'feat/x', head: recorded, token: 't', limits }),
).resolves.toBeNull();
expect(
fetchFn.mock.calls.filter(([url]) => String(url).includes('/pulls?state=open')),
).toHaveLength(1);
});
it('does not ask for another page after a short one', async () => {
const { source, fetchFn } = paged([[item(1, 'e'.repeat(40))], second]);
await source.find({
repo: 'o/r',
branch: 'feat/x',
head: recorded,
token: 't',
limits: { ...limits, maxCandidatePages: 5 },
});
expect(
fetchFn.mock.calls.filter(([url]) => String(url).includes('/pulls?state=open')),
).toHaveLength(1);
});
it('never reads past the configured page budget', async () => {
const full = [first, first, first, first, first];
const { source, fetchFn } = paged(full);
await source.find({
repo: 'o/r',
branch: 'feat/x',
head: recorded,
token: 't',
limits: { ...limits, maxCandidatePages: 3 },
});
expect(
fetchFn.mock.calls.filter(([url]) => String(url).includes('/pulls?state=open')),
).toHaveLength(3);
});
});

View file

@ -5,6 +5,7 @@ import type {
TConversationPullRequest,
} from 'librechat-data-provider';
import type { PullRequestSource } from './types';
import { isAllowedRepository } from './repository';
import { PullRequestSourceError } from './types';
const GITHUB_API_BASE = 'https://api.github.com';
@ -15,6 +16,7 @@ const DEFAULT_MAX_CHECK_RUN_PAGES = 10;
const CHECK_RUN_PAGE_SIZE = 100;
const DEFAULT_MAX_CANDIDATE_PULL_REQUESTS = 10;
const DEFAULT_MAX_HEAD_COMPARISONS = 3;
const DEFAULT_MAX_CANDIDATE_PAGES = 1;
/**
* Comparing `recorded...candidate`: `ahead` means the candidate builds on the recorded commit and
* `identical` that it is the recorded commit. `behind` means the candidate does not contain it
@ -48,6 +50,8 @@ type GitHubPull = {
mergeable: boolean | null;
/** The head this detail describes; the check runs are read for this same commit. */
headSha: string;
/** The repository that head lives in; a fork's checks run there, not in the base repository. */
headRepo?: string;
};
type GitHubCheckRun = { status: string; conclusion: string | null };
@ -87,6 +91,10 @@ function parsePull(value: unknown): GitHubPull {
) {
throw new PullRequestSourceError('UPSTREAM_ERROR');
}
const headRepo =
isRecord(value.head.repo) && typeof value.head.repo.full_name === 'string'
? value.head.repo.full_name
: undefined;
return {
number: value.number,
title: value.title,
@ -98,6 +106,7 @@ function parsePull(value: unknown): GitHubPull {
deletions: value.deletions,
mergeable: value.mergeable,
headSha: value.head.sha,
...(headRepo ? { headRepo } : {}),
};
}
@ -198,7 +207,7 @@ function isRateLimited(response: Response): boolean {
* message. The body is read bounded and only matched, never stored or returned.
*/
async function isSecondaryLimit(response: Response): Promise<boolean> {
if (response.status !== 403) return false;
if (response.status !== 403 && response.status !== 429) return false;
try {
const text = (await response.text()).slice(0, MAX_ERROR_BODY_CHARS);
return SECONDARY_LIMIT_MESSAGE.test(text);
@ -245,6 +254,7 @@ export function createGitHubPullRequestSource({
maxCheckRunPages: number;
maxCandidatePullRequests: number;
maxHeadComparisons: number;
maxCandidatePages: number;
};
async function getJson(
@ -252,6 +262,8 @@ export function createGitHubPullRequestSource({
lookup: Lookup,
/** Statuses that mean "nothing there" for this request, besides 404. */
absent: readonly number[] = [],
/** Whether a 404 means "nothing there". Check runs for a commit that was just read are not. */
notFoundIsAbsent = true,
): Promise<unknown | null> {
let response: Response;
try {
@ -275,13 +287,19 @@ export function createGitHubPullRequestSource({
}
}
if (isRateLimited(response)) {
throw new PullRequestSourceError('RATE_LIMITED', retryAfterMs(response));
const hinted = retryAfterMs(response);
/** A 429 that names no wait may still be a secondary limit, told apart by its message. */
const wait =
hinted == null && (await isSecondaryLimit(response)) ? SECONDARY_LIMIT_WAIT_MS : hinted;
throw new PullRequestSourceError('RATE_LIMITED', wait);
}
if (await isSecondaryLimit(response)) {
throw new PullRequestSourceError('RATE_LIMITED', SECONDARY_LIMIT_WAIT_MS);
}
/** A repository the token cannot see is indistinguishable from one without pull requests. */
if (response.status === 404 || absent.includes(response.status)) return null;
if ((notFoundIsAbsent && response.status === 404) || absent.includes(response.status)) {
return null;
}
throw new PullRequestSourceError('UPSTREAM_ERROR');
}
@ -292,11 +310,14 @@ export function createGitHubPullRequestSource({
): Promise<{ runs: GitHubCheckRun[]; incomplete: boolean }> {
const runs: GitHubCheckRun[] = [];
for (let page = 1; page <= lookup.maxCheckRunPages; page++) {
/** The head was read a moment ago, so a 404 here is a head that vanished, not no checks. */
const body = await getJson(
`${pathname}?per_page=${CHECK_RUN_PAGE_SIZE}&page=${page}`,
lookup,
[],
false,
);
if (body == null) return { runs, incomplete: false };
if (body == null) throw new PullRequestSourceError('UPSTREAM_ERROR');
const parsed = parseCheckRuns(body);
runs.push(...parsed.runs);
const done =
@ -309,7 +330,13 @@ export function createGitHubPullRequestSource({
}
return {
async find({ repo, branch, head: recordedHead, token, limits }) {
async find({ repo, branch, head: recordedHead, token, limits, allowedRepositories }) {
/**
* A lane with a branch and no commit yet has nothing to match a pull request against, and
* every candidate would be accepted, including an earlier incarnation of a reused branch
* name. `undefined` is a caller that never recorded a head, which keeps matching by branch.
*/
if (recordedHead === null) return null;
const lookup: Lookup = {
token,
requestTimeoutMs: limits?.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
@ -318,6 +345,7 @@ export function createGitHubPullRequestSource({
maxCandidatePullRequests:
limits?.maxCandidatePullRequests ?? DEFAULT_MAX_CANDIDATE_PULL_REQUESTS,
maxHeadComparisons: limits?.maxHeadComparisons ?? DEFAULT_MAX_HEAD_COMPARISONS,
maxCandidatePages: limits?.maxCandidatePages ?? DEFAULT_MAX_CANDIDATE_PAGES,
};
const match = REPO_PATTERN.exec(repo);
if (match == null || branch.length === 0) return null;
@ -326,9 +354,9 @@ export function createGitHubPullRequestSource({
const base = `/repos/${encodeURIComponent(owner)}/${encodeURIComponent(name)}`;
const headFilter = encodeURIComponent(`${owner}:${branch}`);
/** `undefined`: the repository is not visible. An empty list: it is, and nothing matched. */
const listPulls = async (state: 'open' | 'closed', perPage: number) => {
const listPulls = async (state: 'open' | 'closed', perPage: number, page = 1) => {
const listed = await getJson(
`${base}/pulls?state=${state}&sort=updated&direction=desc&per_page=${perPage}&head=${headFilter}`,
`${base}/pulls?state=${state}&sort=updated&direction=desc&per_page=${perPage}&page=${page}&head=${headFilter}`,
lookup,
);
if (listed == null) return undefined;
@ -363,15 +391,35 @@ export function createGitHubPullRequestSource({
}
return null;
};
/**
* Reads pages of one state until a short page, the configured page budget, or (without a
* recorded commit, where only the newest matters) the first page. `undefined` is a
* repository the token cannot see.
*/
const listAll = async (state: 'open' | 'closed'): Promise<ListItem[] | undefined> => {
const perPage =
state === 'closed' && recorded == null ? 1 : lookup.maxCandidatePullRequests;
const pages = state === 'closed' && recorded == null ? 1 : lookup.maxCandidatePages;
const all: ListItem[] = [];
for (let page = 1; page <= pages; page++) {
const items = await listPulls(state, perPage, page);
if (items === undefined) return page === 1 ? undefined : all;
all.push(...items);
if (items.length < perPage) break;
}
return all;
};
/** Open first on its own, so closed history on a reused branch name cannot hide it. */
const open = await listPulls('open', lookup.maxCandidatePullRequests);
const open = await listAll('open');
if (open === undefined) return null;
/**
* The head filter names the repository's own owner, so a pull request from a fork is not in
* these lists. The recorded commit finds it instead: GitHub lists the pull requests that
* carry a commit whatever fork they come from, and the branch name narrows them.
*/
const forkCandidates = async (): Promise<ListItem[]> => {
let forks: Promise<ListItem[]> | undefined;
const forkCandidates = (): Promise<ListItem[]> => (forks ??= findForkCandidates());
const findForkCandidates = async (): Promise<ListItem[]> => {
if (recorded == null) return [];
const found = await getJson(
`${base}/commits/${recorded}/pulls?per_page=${lookup.maxCandidatePullRequests}`,
@ -382,15 +430,17 @@ export function createGitHubPullRequestSource({
if (!Array.isArray(found)) throw new PullRequestSourceError('UPSTREAM_ERROR');
return found
.filter((item) => isRecord(item) && isRecord(item.head) && item.head.ref === branch)
.map(parseListItem)
.sort((a, b) => Number(a.state !== 'open') - Number(b.state !== 'open'));
.map(parseListItem);
};
/**
* Open pull requests win over closed ones whichever repository they come from, so a closed
* pull request in the base repository cannot hide an open one from a fork.
*/
const chosen =
(await choose(open)) ??
(await choose(
(await listPulls('closed', recorded == null ? 1 : lookup.maxCandidatePullRequests)) ?? [],
)) ??
(await choose(await forkCandidates()));
(await choose((await forkCandidates()).filter((item) => item.state === 'open'))) ??
(await choose((await listAll('closed')) ?? [])) ??
(await choose((await forkCandidates()).filter((item) => item.state !== 'open')));
if (chosen == null) return null;
/**
@ -403,7 +453,24 @@ export function createGitHubPullRequestSource({
const pull = parsePull(detail);
/** A push between the list and the detail can move the head off the recorded commit. */
if (pull.headSha !== chosen.sha && !(await contains(pull.headSha))) return null;
const checks = await readCheckRuns(`${base}/commits/${pull.headSha}/check-runs`, lookup);
/**
* A fork's checks run in the fork. Its repository is read only when an administrator named
* it, so the server's token is never used against a repository nobody authorized.
*/
const checksBase = (() => {
const headRepo = pull.headRepo;
if (headRepo == null || headRepo.toLowerCase() === `${owner}/${name}`.toLowerCase()) {
return base;
}
const parts = REPO_PATTERN.exec(headRepo);
if (parts == null || !isPathSegment(parts[1]) || !isPathSegment(parts[2])) return base;
if (!isAllowedRepository(headRepo, allowedRepositories)) return base;
return `/repos/${encodeURIComponent(parts[1])}/${encodeURIComponent(parts[2])}`;
})();
const checks = await readCheckRuns(
`${checksBase}/commits/${pull.headSha}/check-runs`,
lookup,
);
return toConversationPullRequest(pull, checks.runs, checks.incomplete);
},
};

View file

@ -1,7 +1,11 @@
import type { TConversationPullRequest } from 'librechat-data-provider';
import type { Response } from 'express';
import type { ServerRequest } from '~/types';
import { createConversationPullRequestHandler, resolveTokenReference } from './handler';
import {
createConversationPullRequestHandler,
createConversationPullRequestsHandler,
resolveTokenReference,
} from './handler';
jest.mock('@librechat/data-schemas', () => ({
...jest.requireActual('@librechat/data-schemas'),
@ -84,12 +88,14 @@ describe('createConversationPullRequestHandler', () => {
ttlMs: 30_000,
cacheMaxEntries: 500,
cacheMaxCredentials: 256,
allowedRepositories: ['o/r'],
limits: {
requestTimeoutMs: 10_000,
lookupTimeoutMs: 30_000,
maxCheckRunPages: 10,
maxCandidatePullRequests: 10,
maxHeadComparisons: 3,
maxCandidatePages: 1,
},
});
expect(res.status).toHaveBeenCalledWith(200);
@ -104,6 +110,7 @@ describe('createConversationPullRequestHandler', () => {
lookupTimeoutSeconds: 8,
maxCheckRunPages: 4,
maxCandidatePullRequests: 25,
maxCandidatePages: 4,
maxHeadComparisons: 6,
cacheMaxEntries: 77,
cacheMaxCredentials: 12,
@ -120,6 +127,7 @@ describe('createConversationPullRequestHandler', () => {
maxCheckRunPages: 4,
maxCandidatePullRequests: 25,
maxHeadComparisons: 6,
maxCandidatePages: 4,
},
}),
);
@ -281,3 +289,645 @@ describe('createConversationPullRequestHandler', () => {
expect(JSON.stringify(res.json.mock.calls)).not.toContain('ghp_secret');
});
});
describe('createConversationPullRequestsHandler', () => {
type Row = {
conversationId: string;
laneGit: { branch: string | null; head: string | null; repo?: string };
};
const lane = (conversationId: string, repo = 'o/r', branch = `feat/${conversationId}`): Row => ({
conversationId,
laneGit: { branch, head: null, repo },
});
function batch(
options: {
settings?: Record<string, unknown> | null;
lanes?: Row[];
lookup?: jest.Mock;
env?: Record<string, string | undefined>;
} = {},
) {
const settings = options.settings === undefined ? enabled : (options.settings ?? undefined);
const lanes = options.lanes ?? [lane('a'), lane('b')];
const lookup = options.lookup ?? jest.fn().mockResolvedValue({ ok: true, value: pr });
const getConvosLaneGit = jest.fn().mockResolvedValue(lanes);
const handler = createConversationPullRequestsHandler({
getConvosLaneGit,
getAppConfig: jest.fn().mockResolvedValue(configWith(settings)),
lookup,
env: options.env ?? { GH_TOKEN: 'ghp_secret' },
});
const res = { status: jest.fn().mockReturnThis(), json: jest.fn() };
const run = (...args: [body?: unknown, user?: string | null]) => {
/** Read by count, so an explicit `undefined` is a real "none" and not the default. */
const body = args.length > 0 ? args[0] : { conversationIds: ['a', 'b'] };
const user = args.length > 1 ? args[1] : 'u1';
return handler(
{ user: user ? { id: user } : undefined, body } as unknown as ServerRequest,
res as unknown as Response,
);
};
return { run, res, lookup, getConvosLaneGit };
}
it('answers each conversation in the order asked, from one owner-scoped read', async () => {
const { run, res, getConvosLaneGit, lookup } = batch();
await run({ conversationIds: ['b', 'a'] });
expect(getConvosLaneGit).toHaveBeenCalledTimes(1);
expect(getConvosLaneGit).toHaveBeenCalledWith('u1', ['b', 'a']);
expect(lookup).toHaveBeenCalledTimes(2);
expect(res.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'b', pullRequest: pr },
{ conversationId: 'a', pullRequest: pr },
],
});
});
it('answers no pull request for a conversation without a stored lane, without a lookup', async () => {
const { run, res, lookup } = batch({ lanes: [lane('a')] });
await run({ conversationIds: ['a', 'ghost'] });
expect(lookup).toHaveBeenCalledTimes(1);
expect(res.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'a', pullRequest: pr },
{ conversationId: 'ghost', pullRequest: null },
],
});
});
it('never looks up a repository the administrator did not allow', async () => {
const { run, res, lookup } = batch({ lanes: [lane('a', 'o/r'), lane('b', 'evil/secret')] });
await run();
expect(lookup).toHaveBeenCalledTimes(1);
expect(lookup).toHaveBeenCalledWith(expect.objectContaining({ repo: 'o/r' }));
expect(res.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'a', pullRequest: pr },
{ conversationId: 'b', pullRequest: null },
],
});
});
it('keeps one failure from hiding the others and exposes only its code', async () => {
const lookup = jest
.fn()
.mockResolvedValueOnce({ ok: false, error: { code: 'RATE_LIMITED' } })
.mockResolvedValueOnce({ ok: true, value: pr });
const { run, res } = batch({ lookup });
await run();
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'a', error: { code: 'RATE_LIMITED' } },
{ conversationId: 'b', pullRequest: pr },
],
});
});
it('answers no pull request for everything, and reads no token, when the feature is off', async () => {
const { run, res, lookup } = batch({ settings: { ...enabled, enabled: false } });
await run();
expect(lookup).not.toHaveBeenCalled();
expect(res.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'a', pullRequest: null },
{ conversationId: 'b', pullRequest: null },
],
});
});
it('says not configured only for the conversations that needed the token, and keeps the rest', async () => {
const { run, res, lookup } = batch({ env: {}, lanes: [lane('a')] });
await run({ conversationIds: ['a', 'ghost'] });
expect(res.status).toHaveBeenCalledWith(200);
expect(lookup).not.toHaveBeenCalled();
expect(res.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'a', error: { code: 'NOT_CONFIGURED' } },
{ conversationId: 'ghost', pullRequest: null },
],
});
});
it('answers a plain no pull request when nothing would have needed the token', async () => {
const { run, res } = batch({ env: {}, lanes: [] });
await run();
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'a', pullRequest: null },
{ conversationId: 'b', pullRequest: null },
],
});
});
it('collapses duplicate ids so one conversation is looked up once', async () => {
const { run, lookup, getConvosLaneGit } = batch({ lanes: [lane('a')] });
await run({ conversationIds: ['a', 'a', 'a'] });
expect(getConvosLaneGit).toHaveBeenCalledWith('u1', ['a']);
expect(lookup).toHaveBeenCalledTimes(1);
});
it('runs no more lookups at once than configured', async () => {
let active = 0;
let peak = 0;
const lookup = jest.fn(async () => {
active += 1;
peak = Math.max(peak, active);
await new Promise((resolve) => setTimeout(resolve, 5));
active -= 1;
return { ok: true, value: pr };
});
const ids = ['a', 'b', 'c', 'd', 'e', 'f'];
const { run } = batch({
lookup,
settings: { ...enabled, maxConcurrentLookups: 2 },
lanes: ids.map((id) => lane(id)),
});
await run({ conversationIds: ids });
expect(lookup).toHaveBeenCalledTimes(6);
expect(peak).toBe(2);
});
it.each([
['no body', undefined],
['no list', {}],
['an empty list', { conversationIds: [] }],
['a list that is not an array', { conversationIds: 'a' }],
['a non-string id', { conversationIds: ['a', 7] }],
['a blank id', { conversationIds: ['a', ' '] }],
['an overlong id', { conversationIds: ['x'.repeat(257)] }],
['more ids than the bound', { conversationIds: Array.from({ length: 51 }, (_, i) => `c${i}`) }],
])('rejects %s before reading anything', async (_label, body) => {
const { run, res, getConvosLaneGit, lookup } = batch();
await run(body);
expect(res.status).toHaveBeenCalledWith(400);
expect(getConvosLaneGit).not.toHaveBeenCalled();
expect(lookup).not.toHaveBeenCalled();
});
it('accepts exactly the bound', async () => {
const ids = Array.from({ length: 50 }, (_, i) => `c${i}`);
const { run, res } = batch({ lanes: [] });
await run({ conversationIds: ids });
expect(res.status).toHaveBeenCalledWith(200);
});
it('rejects an unauthenticated request', async () => {
const { run, res, getConvosLaneGit } = batch();
await run({ conversationIds: ['a'] }, null);
expect(res.status).toHaveBeenCalledWith(400);
expect(getConvosLaneGit).not.toHaveBeenCalled();
});
it('answers 500 without the error text when the read fails', async () => {
const { run, res, getConvosLaneGit } = batch();
getConvosLaneGit.mockRejectedValue(new Error('mongodb://u:secret@h'));
await run();
expect(res.status).toHaveBeenCalledWith(500);
expect(JSON.stringify((res.json as jest.Mock).mock.calls)).not.toContain('secret');
});
describe('overall deadline', () => {
const never = () => new Promise(() => undefined);
it('answers within the deadline when a lookup stalls, keeping what finished', async () => {
const lookup = jest
.fn()
.mockResolvedValueOnce({ ok: true, value: pr })
.mockImplementationOnce(never);
const { run, res } = batch({ lookup, settings: { ...enabled, batchTimeoutSeconds: 0.08 } });
const started = Date.now();
await run();
expect(Date.now() - started).toBeLessThan(1000);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'a', pullRequest: pr },
{ conversationId: 'b', error: { code: 'UPSTREAM_ERROR' } },
],
});
});
it('starts nothing new once the deadline has passed', async () => {
const lookup = jest.fn().mockImplementation(never);
const ids = ['a', 'b', 'c'];
const { run, res } = batch({
lookup,
lanes: ids.map((id) => lane(id)),
settings: { ...enabled, maxConcurrentLookups: 1, batchTimeoutSeconds: 0.08 },
});
await run({ conversationIds: ids });
expect(lookup).toHaveBeenCalledTimes(1);
expect(res.json).toHaveBeenCalledWith({
results: ids.map((conversationId) => ({
conversationId,
error: { code: 'UPSTREAM_ERROR' },
})),
});
});
it('counts the time spent reading the config and lanes against the deadline', async () => {
/** The reads take most of the 0.2 s deadline, so the lookups get what is left of it, not a fresh 0.2 s. */
const lookup = jest.fn(() => new Promise(() => undefined));
const slowRead = <T>(value: T, ms: number) =>
new Promise<T>((resolve) => setTimeout(() => resolve(value), ms));
const handler = createConversationPullRequestsHandler({
getConvosLaneGit: jest.fn(() => slowRead([lane('a')], 150)),
getAppConfig: jest.fn(() =>
slowRead(configWith({ ...enabled, batchTimeoutSeconds: 0.2 }), 10),
) as never,
lookup: lookup as never,
env: { GH_TOKEN: 'ghp_secret' },
});
const out = { status: jest.fn().mockReturnThis(), json: jest.fn() };
const started = Date.now();
await handler(
{ user: { id: 'u1' }, body: { conversationIds: ['a'] } } as unknown as ServerRequest,
out as unknown as Response,
);
const took = Date.now() - started;
expect(took).toBeLessThan(330);
expect(out.json).toHaveBeenCalledWith({
results: [{ conversationId: 'a', error: { code: 'UPSTREAM_ERROR' } }],
});
});
const respond503 = {
error: 'Pull request lookup is unavailable',
code: 'UPSTREAM_ERROR',
};
const call = (handler: ReturnType<typeof createConversationPullRequestsHandler>) => {
const out = { status: jest.fn().mockReturnThis(), json: jest.fn() };
const pending = handler(
{ user: { id: 'u1' }, body: { conversationIds: ['a'] } } as unknown as ServerRequest,
out as unknown as Response,
);
return { out, pending };
};
it('ends a request whose lane read never answers at the configured deadline, not the maximum', async () => {
jest.useFakeTimers();
try {
const lookup = jest.fn();
const { out, pending } = call(
createConversationPullRequestsHandler({
getConvosLaneGit: jest.fn(() => new Promise(() => undefined)) as never,
getAppConfig: jest
.fn()
.mockResolvedValue(configWith({ ...enabled, batchTimeoutSeconds: 20 })),
lookup: lookup as never,
env: { GH_TOKEN: 'ghp_secret' },
}),
);
await jest.advanceTimersByTimeAsync(19_000);
expect(out.json).not.toHaveBeenCalled();
await jest.advanceTimersByTimeAsync(2_000);
await pending;
expect(out.status).toHaveBeenCalledWith(503);
expect(out.json).toHaveBeenCalledWith(respond503);
expect(lookup).not.toHaveBeenCalled();
} finally {
jest.useRealTimers();
}
});
it('ends a request whose config read never answers at the default deadline', async () => {
jest.useFakeTimers();
try {
const lookup = jest.fn();
const { out, pending } = call(
createConversationPullRequestsHandler({
getConvosLaneGit: jest.fn().mockResolvedValue([lane('a')]) as never,
getAppConfig: jest.fn(() => new Promise(() => undefined)) as never,
lookup: lookup as never,
env: { GH_TOKEN: 'ghp_secret' },
}),
);
await jest.advanceTimersByTimeAsync(19_000);
expect(out.json).not.toHaveBeenCalled();
await jest.advanceTimersByTimeAsync(2_000);
await pending;
expect(out.status).toHaveBeenCalledWith(503);
expect(lookup).not.toHaveBeenCalled();
} finally {
jest.useRealTimers();
}
});
it('holds the lane read to a configured deadline shorter than the default', async () => {
jest.useFakeTimers();
try {
const { out, pending } = call(
createConversationPullRequestsHandler({
getConvosLaneGit: jest.fn(() => new Promise(() => undefined)) as never,
getAppConfig: jest
.fn()
.mockResolvedValue(configWith({ ...enabled, batchTimeoutSeconds: 3 })),
lookup: jest.fn() as never,
env: { GH_TOKEN: 'ghp_secret' },
}),
);
await jest.advanceTimersByTimeAsync(3_500);
await pending;
expect(out.status).toHaveBeenCalledWith(503);
} finally {
jest.useRealTimers();
}
});
it('starts no lookup when the reads alone used up the deadline', async () => {
const lookup = jest.fn().mockResolvedValue({ ok: true, value: pr });
const { out, pending } = call(
createConversationPullRequestsHandler({
getConvosLaneGit: jest.fn(
() => new Promise((resolve) => setTimeout(() => resolve([lane('a')]), 150)),
) as never,
getAppConfig: jest
.fn()
.mockResolvedValue(configWith({ ...enabled, batchTimeoutSeconds: 0.05 })),
lookup: lookup as never,
env: { GH_TOKEN: 'ghp_secret' },
}),
);
await pending;
expect(lookup).not.toHaveBeenCalled();
expect(out.status).toHaveBeenCalledWith(503);
expect(out.json).toHaveBeenCalledWith(respond503);
});
it('does not fail unhandled when the lane read rejects after the request has ended', async () => {
const unhandled = jest.fn();
process.on('unhandledRejection', unhandled);
try {
const { pending } = call(
createConversationPullRequestsHandler({
getConvosLaneGit: jest.fn(
() => new Promise((_, reject) => setTimeout(() => reject(new Error('late')), 30)),
) as never,
getAppConfig: jest.fn().mockResolvedValue(configWith({ ...enabled, enabled: false })),
lookup: jest.fn() as never,
env: { GH_TOKEN: 'ghp_secret' },
}),
);
await pending;
await new Promise((resolve) => setTimeout(resolve, 80));
expect(unhandled).not.toHaveBeenCalled();
} finally {
process.off('unhandledRejection', unhandled);
}
});
it('does not change the answer of a batch that finishes in time', async () => {
const { run, res } = batch({ settings: { ...enabled, batchTimeoutSeconds: 5 } });
await run();
expect(res.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'a', pullRequest: pr },
{ conversationId: 'b', pullRequest: null },
].map((entry, index) => (index === 1 ? { ...entry, pullRequest: pr } : entry)),
});
});
});
describe('concurrency across requests', () => {
/** Lookups that end only when released, so a request can finish while its lookups still run. */
const heldLookups = () => {
let active = 0;
let peak = 0;
const releases: Array<() => void> = [];
const lookup = jest.fn(
() =>
new Promise((resolve) => {
active += 1;
peak = Math.max(peak, active);
releases.push(() => {
active -= 1;
resolve({ ok: true, value: pr });
});
}),
);
return { lookup, releases, active: () => active, peak: () => peak };
};
const ids = ['a', 'b', 'c', 'd'];
const lanes = ids.map((id) => lane(id));
const settings = { ...enabled, maxConcurrentLookups: 2, batchTimeoutSeconds: 0.08 };
const flush = () => new Promise((resolve) => setTimeout(resolve, 20));
it('keeps the lookups of a timed-out request inside the limit for the next request', async () => {
const held = heldLookups();
const handler = batch({ lookup: held.lookup, lanes, settings });
await handler.run({ conversationIds: ids });
/** The first request has answered, yet its two lookups are still out. */
expect(held.active()).toBe(2);
const second = handler.run({ conversationIds: ids });
await flush();
expect(held.lookup).toHaveBeenCalledTimes(2);
expect(held.peak()).toBe(2);
held.releases.splice(0).forEach((release) => release());
await second;
held.releases.splice(0).forEach((release) => release());
expect(held.peak()).toBe(2);
});
it('starts no lookup for an entry that gave up while it waited behind another request', async () => {
const held = heldLookups();
const handler = batch({ lookup: held.lookup, lanes, settings });
/** The first request ends with its two lookups still holding both slots. */
await handler.run({ conversationIds: ids });
expect(held.lookup).toHaveBeenCalledTimes(2);
/** The second request's entries wait for those slots, and give up at its own deadline. */
await handler.run({ conversationIds: ids });
expect(held.lookup).toHaveBeenCalledTimes(2);
/** Slots free up only now; the entries that already answered must not start work. */
held.releases.splice(0).forEach((release) => release());
await flush();
expect(held.lookup).toHaveBeenCalledTimes(2);
expect(held.active()).toBe(0);
});
it("counts slots per credential, so one tenant's stalled lookups do not block another's", async () => {
const held = heldLookups();
const configFor = (tokenName: string) =>
configWith({
...enabled,
token: `\${${tokenName}}`,
maxConcurrentLookups: 1,
batchTimeoutSeconds: 0.2,
});
/** Each tenant's config names its own token, as principal-scoped configuration can. */
const getAppConfig = jest.fn(async (options: { userId?: string; role?: string }) =>
configFor(options.userId === 'u2' ? 'TOKEN_TWO' : 'TOKEN_ONE'),
);
const handler = createConversationPullRequestsHandler({
getConvosLaneGit: jest.fn().mockResolvedValue([lane('a')]),
getAppConfig: getAppConfig as never,
lookup: held.lookup as never,
env: { TOKEN_ONE: 'token-one', TOKEN_TWO: 'token-two' },
});
const call = async (user: string) => {
const out = { status: jest.fn().mockReturnThis(), json: jest.fn() };
await handler(
{ user: { id: user }, body: { conversationIds: ['a'] } } as unknown as ServerRequest,
out as unknown as Response,
);
return out;
};
/** The first tenant's only slot is now held by a lookup that outlived its request. */
await call('u1');
expect(held.active()).toBe(1);
/** Same credential: the next request waits for that slot, gives up, and starts nothing. */
const same = await call('u1');
expect(held.lookup).toHaveBeenCalledTimes(1);
expect(same.json).toHaveBeenCalledWith({
results: [{ conversationId: 'a', error: { code: 'UPSTREAM_ERROR' } }],
});
/** Another credential has its own slots, so it is not held behind the first tenant. */
await call('u2');
expect(held.lookup).toHaveBeenCalledTimes(2);
});
it('looks identical lanes up once and spends one slot on them, so an unrelated lane still starts', async () => {
const held = heldLookups();
/** Four conversations on one branch and one on another, with room for a single lookup at a time. */
const same = ['a', 'b', 'c', 'd'].map((id) => lane(id, 'o/r', 'feat/shared'));
const other = lane('e', 'o/r', 'feat/other');
const handler = batch({
lookup: held.lookup,
lanes: [...same, other],
settings: { ...enabled, maxConcurrentLookups: 1, batchTimeoutSeconds: 5 },
});
const done = handler.run({ conversationIds: ['a', 'b', 'c', 'd', 'e'] });
await flush();
expect(held.lookup).toHaveBeenCalledTimes(1);
held.releases.splice(0).forEach((release) => release());
await flush();
/** The shared branch finished in one lookup, freeing the only slot for the other branch. */
expect(held.lookup).toHaveBeenCalledTimes(2);
held.releases.splice(0).forEach((release) => release());
await done;
expect(handler.res.json).toHaveBeenCalledWith({
results: ['a', 'b', 'c', 'd', 'e'].map((conversationId) => ({
conversationId,
pullRequest: pr,
})),
});
});
it('joins a lane another request already started instead of taking a slot of its own for it', async () => {
const started: string[] = [];
const releases: Array<() => void> = [];
const lookup = jest.fn(
(input: { branch: string }) =>
new Promise((resolve) => {
started.push(input.branch);
releases.push(() => resolve({ ok: true, value: pr }));
}),
);
const rows = [
lane('a', 'o/r', 'feat/shared'),
lane('b', 'o/r', 'feat/shared'),
lane('e', 'o/r', 'feat/other'),
];
const handler = createConversationPullRequestsHandler({
getConvosLaneGit: jest.fn(async (_user: string, wanted: string[]) =>
rows.filter((row) => wanted.includes(row.conversationId)),
),
getAppConfig: jest
.fn()
.mockResolvedValue(
configWith({ ...enabled, maxConcurrentLookups: 1, batchTimeoutSeconds: 5 }),
),
lookup: lookup as never,
env: { GH_TOKEN: 'ghp_secret' },
});
const call = (conversationIds: string[]) => {
const out = { status: jest.fn().mockReturnThis(), json: jest.fn() };
const done = handler(
{ user: { id: 'u1' }, body: { conversationIds } } as unknown as ServerRequest,
out as unknown as Response,
);
return { out, done };
};
const first = call(['a']);
await flush();
const second = call(['b', 'e']);
await flush();
expect(started).toEqual(['feat/shared']);
releases.shift()?.();
await flush();
/** The shared lane was answered once for both requests, so the only slot went to the other lane. */
expect(started).toEqual(['feat/shared', 'feat/other']);
releases.shift()?.();
await Promise.all([first.done, second.done]);
expect(second.out.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'b', pullRequest: pr },
{ conversationId: 'e', pullRequest: pr },
],
});
});
it('gives every conversation on a shared branch the same failure when that lookup fails', async () => {
const lookup = jest
.fn()
.mockResolvedValueOnce({ ok: false, error: { code: 'RATE_LIMITED' } })
.mockResolvedValue({ ok: true, value: pr });
const { run, res } = batch({
lookup,
lanes: [lane('a', 'o/r', 'feat/s'), lane('b', 'o/r', 'feat/s'), lane('c', 'o/r', 'feat/t')],
});
await run({ conversationIds: ['a', 'b', 'c'] });
expect(lookup).toHaveBeenCalledTimes(2);
expect(res.json).toHaveBeenCalledWith({
results: [
{ conversationId: 'a', error: { code: 'RATE_LIMITED' } },
{ conversationId: 'b', error: { code: 'RATE_LIMITED' } },
{ conversationId: 'c', pullRequest: pr },
],
});
});
it('does not merge lanes that differ in repository, branch or head', async () => {
const head = 'a'.repeat(40);
const rows = [
lane('a', 'o/r', 'feat/x'),
lane('b', 'o/r2', 'feat/x'),
lane('c', 'o/r', 'feat/y'),
{ conversationId: 'd', laneGit: { branch: 'feat/x', head, repo: 'o/r' } },
];
const { run, lookup } = batch({
lanes: rows,
settings: { ...enabled, allowedRepositories: ['o/*'] },
});
await run({ conversationIds: ['a', 'b', 'c', 'd'] });
expect(lookup).toHaveBeenCalledTimes(4);
});
it('frees the slot when a lookup fails, so later requests are not stuck', async () => {
const lookup = jest
.fn()
.mockRejectedValueOnce(new Error('boom'))
.mockResolvedValue({ ok: true, value: pr });
const handler = batch({
lookup,
lanes: [lane('a')],
settings: { ...enabled, maxConcurrentLookups: 1 },
});
await handler.run({ conversationIds: ['a'] });
await handler.run({ conversationIds: ['a'] });
expect(lookup).toHaveBeenCalledTimes(2);
});
});
it('uses the same lookup input as the single route', async () => {
const single = setup({ laneGit: { branch: 'feat/a', head: null, repo: 'o/r' } });
await single.run();
const many = batch({ lanes: [lane('a')] });
await many.run({ conversationIds: ['a'] });
expect(many.lookup.mock.calls[0][0]).toEqual(single.lookup.mock.calls[0][0]);
});
});

View file

@ -1,12 +1,20 @@
import { createHash } from 'node:crypto';
import { logger } from '@librechat/data-schemas';
import { EModelEndpoint } from 'librechat-data-provider';
import type { TAgentsEndpoint, TConversationPullRequestResponse } from 'librechat-data-provider';
import { EModelEndpoint, PULL_REQUEST_BATCH_MAX } from 'librechat-data-provider';
import type {
TAgentsEndpoint,
TConversationPullRequest,
TConversationPullRequestsEntry,
TConversationPullRequestResponse,
TConversationPullRequestsResponse,
} from 'librechat-data-provider';
import type { AppConfig } from '@librechat/data-schemas';
import type { Response } from 'express';
import type { GetAppConfigOptions } from '~/app/service';
import type { PullRequestLookup } from './types';
import type { ServerRequest } from '~/types';
import { getAppConfigOptionsFromUser } from '~/app/service';
import { isAllowedRepository } from './repository';
import { getSafeErrorMetadata } from '~/utils';
const MAX_CONVERSATION_ID_LENGTH = 256;
@ -24,20 +32,198 @@ export function resolveTokenReference(
return env[name]?.trim() || null;
}
/** `owner/name` or `owner/*`, compared without case as GitHub does. Nothing else matches. */
export function isAllowedRepository(repo: string, allowed: readonly string[] | undefined): boolean {
const [owner, name] = repo.toLowerCase().split('/');
return (allowed ?? []).some((entry) => {
const [allowedOwner, allowedName] = entry.toLowerCase().split('/');
return allowedOwner === owner && (allowedName === '*' || allowedName === name);
});
}
const validConversationId = (value: string | undefined): value is string =>
value != null && value.trim() !== '' && value.length <= MAX_CONVERSATION_ID_LENGTH;
const NONE: TConversationPullRequestResponse = { pullRequest: null };
type PullRequestSettings = NonNullable<TAgentsEndpoint['pullRequests']>;
type EligibleLane = { branch: string; head: string | null; repo: string };
/** Lookups in flight at once for one batch, unless configured. */
const DEFAULT_BATCH_CONCURRENCY = 4;
/** Longest one batch request may stay open, unless configured. */
const DEFAULT_BATCH_TIMEOUT_SECONDS = 20;
const TIMED_OUT = Symbol('timed-out');
/**
* Lookups in flight across every batch request of one handler. A lookup that outlives its
* request's deadline keeps running (and keeps its slot) until it really ends, so the next request
* cannot start another on top of it: the configured concurrency holds across requests, not only
* within one. Waiters are served in order.
*/
function createLookupLimiter() {
type Scope = { active: number; waiting: Array<() => void> };
const scopes = new Map<string, Scope>();
return {
/** Resolves once a slot is free under `limit`; the caller must `release` exactly once. */
acquire(scopeKey: string, limit: number): Promise<void> {
const scope = scopes.get(scopeKey) ?? { active: 0, waiting: [] };
scopes.set(scopeKey, scope);
if (scope.active < limit) {
scope.active += 1;
return Promise.resolve();
}
return new Promise<void>((resolve) => {
scope.waiting.push(() => {
scope.active += 1;
resolve();
});
});
},
release(scopeKey: string, limit: number): void {
const scope = scopes.get(scopeKey);
if (scope == null) return;
scope.active -= 1;
while (scope.waiting.length > 0 && scope.active < limit) scope.waiting.shift()?.();
/** An idle scope holds nothing, so credentials that come and go leave no entries behind. */
if (scope.active === 0 && scope.waiting.length === 0) scopes.delete(scopeKey);
},
};
}
type LookupResultOf = Awaited<ReturnType<PullRequestLookup>>;
type SharedLookup = { promise: Promise<LookupResultOf | typeof TIMED_OUT>; interested: number };
/**
* One upstream lookup per distinct input across every batch request of a handler. The first
* request that needs a lane starts it and takes its slot; any other request for the same lane,
* however concurrent, joins that promise and takes none. Interest is counted, so a lookup still
* waiting for a slot starts only while some request still wants its answer, and one request
* giving up never cancels what another is waiting for.
*/
function createSharedLookups(
limiter: ReturnType<typeof createLookupLimiter>,
lookup: PullRequestLookup,
) {
const shared = new Map<string, SharedLookup>();
return {
/** `leave` must be called by a caller that stops waiting before the answer arrives. */
join(
key: string,
scope: string,
limit: number,
input: Parameters<PullRequestLookup>[0],
): { promise: SharedLookup['promise']; leave: () => void } {
let entry = shared.get(key);
if (entry == null) {
const created: SharedLookup = {
interested: 0,
promise: Promise.resolve(TIMED_OUT),
};
created.promise = (async () => {
await limiter.acquire(scope, limit);
try {
if (created.interested === 0) {
/** Nobody is waiting any more; a later request must start its own. */
if (shared.get(key) === created) shared.delete(key);
return TIMED_OUT;
}
return await lookup(input);
} finally {
limiter.release(scope, limit);
}
})().finally(() => {
if (shared.get(key) === created) shared.delete(key);
});
shared.set(key, created);
entry = created;
}
const joined = entry;
joined.interested += 1;
let left = false;
return {
promise: joined.promise,
leave: () => {
if (left) return;
left = true;
joined.interested -= 1;
},
};
},
};
}
/** A credential's digest, never the token, names the scope a lookup's slot is counted in. */
const limiterScope = (token: string): string =>
createHash('sha256').update(token).digest('hex').slice(0, 32);
/** Settles with `TIMED_OUT` once `ms` has passed, and never leaves its timer running. */
async function withDeadline<T>(work: Promise<T>, ms: number): Promise<T | typeof TIMED_OUT> {
if (ms <= 0) return TIMED_OUT;
let timer: ReturnType<typeof setTimeout> | undefined;
const expired = new Promise<typeof TIMED_OUT>((resolve) => {
timer = setTimeout(() => resolve(TIMED_OUT), ms);
});
try {
return await Promise.race([work, expired]);
} finally {
clearTimeout(timer);
}
}
/** A lane the server's token may be used for: it names a branch and an allowed repository. */
function eligibleLane(
laneGit: ConversationLaneGit | null | undefined,
settings: PullRequestSettings,
): EligibleLane | null {
if (laneGit?.branch == null || laneGit.repo == null) return null;
if (!isAllowedRepository(laneGit.repo, settings.allowedRepositories)) return null;
return { branch: laneGit.branch, head: laneGit.head, repo: laneGit.repo };
}
/** Everything one lookup needs from the settings, so the single and batch routes cannot differ. */
function lookupInput(settings: PullRequestSettings, token: string, lane: EligibleLane) {
return {
repo: lane.repo,
branch: lane.branch,
head: lane.head,
token,
ttlMs: (settings.cacheTtlSeconds ?? 30) * 1000,
cacheMaxEntries: settings.cacheMaxEntries ?? 500,
cacheMaxCredentials: settings.cacheMaxCredentials ?? 256,
allowedRepositories: settings.allowedRepositories ?? [],
limits: {
requestTimeoutMs: (settings.requestTimeoutSeconds ?? 10) * 1000,
lookupTimeoutMs: (settings.lookupTimeoutSeconds ?? 30) * 1000,
maxCheckRunPages: settings.maxCheckRunPages ?? 10,
maxCandidatePullRequests: settings.maxCandidatePullRequests ?? 10,
maxHeadComparisons: settings.maxHeadComparisons ?? 3,
maxCandidatePages: settings.maxCandidatePages ?? 1,
},
};
}
/** Runs `task` over `items` with at most `limit` in flight, keeping the results in order. */
async function mapWithLimit<T, R>(
items: readonly T[],
limit: number,
task: (item: T) => Promise<R>,
): Promise<R[]> {
const results = new Array<R>(items.length);
let next = 0;
const worker = async (): Promise<void> => {
for (let index = next++; index < items.length; index = next++) {
results[index] = await task(items[index]);
}
};
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
return results;
}
/** Accepts only a non-empty list of valid ids within the batch bound; duplicates collapse. */
function parseConversationIds(body: unknown): string[] | null {
const ids = (body as { conversationIds?: unknown } | null | undefined)?.conversationIds;
if (!Array.isArray(ids) || ids.length === 0 || ids.length > PULL_REQUEST_BATCH_MAX) return null;
const seen = new Set<string>();
for (const id of ids) {
if (typeof id !== 'string' || !validConversationId(id)) return null;
seen.add(id);
}
return [...seen];
}
/**
* Serves the pull request of the branch a conversation's code workspace last reported. The
* stored branch is read owner-scoped, so another user's conversation is indistinguishable from
@ -77,15 +263,11 @@ export function createConversationPullRequestHandler(deps: {
return;
}
if (laneGit?.branch == null || laneGit.repo == null) {
res.status(200).json(NONE);
return;
}
/** The repository comes from the worker, so it is never used with the token unless the
* administrator named it. A repository that is not allowed looks like one without a pull
* request. */
if (!isAllowedRepository(laneGit.repo, settings.allowedRepositories)) {
const lane = eligibleLane(laneGit, settings);
if (lane == null) {
res.status(200).json(NONE);
return;
}
@ -97,22 +279,7 @@ export function createConversationPullRequestHandler(deps: {
return;
}
const result = await deps.lookup({
repo: laneGit.repo,
branch: laneGit.branch,
head: laneGit.head,
token,
ttlMs: (settings.cacheTtlSeconds ?? 30) * 1000,
cacheMaxEntries: settings.cacheMaxEntries ?? 500,
cacheMaxCredentials: settings.cacheMaxCredentials ?? 256,
limits: {
requestTimeoutMs: (settings.requestTimeoutSeconds ?? 10) * 1000,
lookupTimeoutMs: (settings.lookupTimeoutSeconds ?? 30) * 1000,
maxCheckRunPages: settings.maxCheckRunPages ?? 10,
maxCandidatePullRequests: settings.maxCandidatePullRequests ?? 10,
maxHeadComparisons: settings.maxHeadComparisons ?? 3,
},
});
const result = await deps.lookup(lookupInput(settings, token, lane));
if (!result.ok) {
res.status(503).json({
error: 'Pull request lookup is unavailable',
@ -127,3 +294,167 @@ export function createConversationPullRequestHandler(deps: {
}
};
}
/**
* Serves the pull requests of many conversations at once, for the sidebar list. It answers what
* the single route answers for each id, from one owner-scoped read of the stored lanes, so a
* conversation that is missing, expired, another user's or without a lane is simply "no pull
* request". Each entry carries its own result or a stable failure code, so one repository's
* rate limit does not hide the others. Lookups share the single route's cache, in-flight
* sharing and rate-limit cooldown, and run a few at a time.
*/
export function createConversationPullRequestsHandler(deps: {
getConvosLaneGit: (
user: string,
conversationIds: string[],
) => Promise<Array<{ conversationId: string; laneGit: ConversationLaneGit }>>;
getAppConfig: (options: GetAppConfigOptions) => Promise<AppConfig>;
lookup: PullRequestLookup;
env: Readonly<Record<string, string | undefined>>;
}) {
const limiter = createLookupLimiter();
const sharedLookups = createSharedLookups(limiter, deps.lookup);
return async (req: ServerRequest, res: Response): Promise<void> => {
const userId = req.user?.id;
const ids = parseConversationIds(req.body);
if (!userId || ids == null) {
res.status(400).json({ error: 'Invalid conversation list' });
return;
}
try {
/**
* The clock starts when the request arrives. The configured deadline lives in the config,
* so until the config has loaded the default applies to it; once it has, the lane read and
* everything after it share what is left of the configured deadline, however the two reads
* overlapped.
*/
const startedAt = Date.now();
const configRead = deps.getAppConfig({
...getAppConfigOptionsFromUser(req.user),
skipRuntimeAugmentation: true,
failClosed: true,
});
const laneRead = deps.getConvosLaneGit(userId, ids);
/** A lane read nobody waits for (feature off, config timed out) must not fail unhandled. */
laneRead.catch(() => undefined);
const unavailable = () =>
res
.status(503)
.json({ error: 'Pull request lookup is unavailable', code: 'UPSTREAM_ERROR' });
const loaded = await withDeadline(configRead, DEFAULT_BATCH_TIMEOUT_SECONDS * 1000);
if (loaded === TIMED_OUT) {
unavailable();
return;
}
const appConfig = loaded;
const settings = (appConfig.endpoints?.[EModelEndpoint.agents] as TAgentsEndpoint | undefined)
?.pullRequests;
const deadline =
startedAt + (settings?.batchTimeoutSeconds ?? DEFAULT_BATCH_TIMEOUT_SECONDS) * 1000;
const none = (): TConversationPullRequestsResponse => ({
results: ids.map((conversationId) => ({ conversationId, pullRequest: null })),
});
if (settings?.enabled !== true) {
res.status(200).json(none());
return;
}
const lanes = await withDeadline(laneRead, deadline - Date.now());
if (lanes === TIMED_OUT) {
unavailable();
return;
}
const eligible = new Map<string, EligibleLane>();
for (const { conversationId, laneGit } of lanes) {
const lane = eligibleLane(laneGit, settings);
if (lane != null) eligible.set(conversationId, lane);
}
if (eligible.size === 0) {
res.status(200).json(none());
return;
}
/**
* A missing token is a fact about the conversations that needed one, not about the request:
* those entries say so, and every other conversation keeps its correct "no pull request".
*/
const token = resolveTokenReference(settings.token, deps.env);
if (token == null) {
logger.warn('[PullRequests] Enabled without a usable token reference');
res.status(200).json({
results: ids.map(
(conversationId): TConversationPullRequestsEntry =>
eligible.has(conversationId)
? { conversationId, error: { code: 'NOT_CONFIGURED' } }
: { conversationId, pullRequest: null },
),
});
return;
}
/**
* One deadline for the whole request, not per lookup: each lookup may run for its own
* configured limit, and a stalled upstream would otherwise hold a full batch open for many
* times that. Past the deadline nothing new starts and the entries still waiting answer
* with an upstream error; a lookup already running finishes in the background under its own
* limit and fills the cache for the next request.
*/
const limit = settings.maxConcurrentLookups ?? DEFAULT_BATCH_CONCURRENCY;
const scope = limiterScope(token);
/**
* Conversations that share a repository, branch and head share one answer, and the lookup
* already coalesces them, so each distinct lookup takes one slot and one deadline, not one
* per conversation. Otherwise duplicates would hold every slot while a single request ran.
*/
const keyOf = (lane: EligibleLane) => `${lane.repo}\0${lane.branch}\0${lane.head ?? ''}`;
const distinct = new Map<string, EligibleLane>();
for (const lane of eligible.values()) distinct.set(keyOf(lane), lane);
type Outcome =
| { ok: true; value: TConversationPullRequest | null }
| { ok: false; code: string };
const outcomes = new Map<string, Outcome>();
await mapWithLimit([...distinct.entries()], limit, async ([key, lane]): Promise<void> => {
/** Checked before anything is joined, so nothing starts once time is up. */
const remaining = deadline - Date.now();
if (remaining <= 0) {
outcomes.set(key, { ok: false, code: 'UPSTREAM_ERROR' });
return;
}
const input = lookupInput(settings, token, lane);
/** The same lane under the same policy is one lookup for every request that wants it. */
const shareKey = `${scope}\0${JSON.stringify([
input.repo,
input.branch,
input.head,
input.ttlMs,
input.limits,
input.allowedRepositories,
])}`;
const joined = sharedLookups.join(shareKey, scope, limit, input);
const result = await withDeadline(joined.promise, remaining);
if (result === TIMED_OUT) {
joined.leave();
outcomes.set(key, { ok: false, code: 'UPSTREAM_ERROR' });
return;
}
outcomes.set(
key,
result.ok ? { ok: true, value: result.value } : { ok: false, code: result.error.code },
);
});
const looked = ids.map((conversationId): TConversationPullRequestsEntry => {
const lane = eligible.get(conversationId);
const outcome = lane == null ? undefined : outcomes.get(keyOf(lane));
if (outcome == null) return { conversationId, pullRequest: null };
return outcome.ok
? { conversationId, pullRequest: outcome.value }
: { conversationId, error: { code: outcome.code } };
});
res.status(200).json({ results: looked });
} catch (error) {
logger.error('[PullRequests] Batch handler failed', getSafeErrorMetadata(error));
res.status(500).json({ error: 'Failed to load the pull requests' });
}
};
}

View file

@ -2,3 +2,6 @@ export * from './types';
export * from './github';
export * from './lookup';
export * from './handler';
export * from './transport';
export * from './repository';
export * from './capabilities';

View file

@ -402,3 +402,44 @@ describe('cache capacity per credential', () => {
expect(find).toHaveBeenCalledTimes(before);
});
});
describe('cache capacity shared by principals with one token', () => {
it('keeps the largest capacity seen, so a smaller one does not trim what a larger keeps', async () => {
const find = jest.fn().mockResolvedValue(value);
const lookup = createPullRequestLookup({ source: { find } });
for (const branch of ['a', 'b', 'c', 'd']) {
await lookup({ ...input, branch, cacheMaxEntries: 100 });
}
await lookup({ ...input, branch: 'e', cacheMaxEntries: 2 });
const before = find.mock.calls.length;
for (const branch of ['a', 'b', 'c', 'd']) {
await lookup({ ...input, branch, cacheMaxEntries: 2 });
}
expect(find).toHaveBeenCalledTimes(before);
});
it('does not let a larger capacity of one token raise the bound of another', async () => {
const find = jest.fn().mockResolvedValue(value);
const lookup = createPullRequestLookup({ source: { find } });
await lookup({ ...input, token: 'big', branch: 'x', cacheMaxEntries: 100 });
for (const branch of ['a', 'b', 'c']) {
await lookup({ ...input, token: 'small', branch, cacheMaxEntries: 2 });
}
const before = find.mock.calls.length;
await lookup({ ...input, token: 'small', branch: 'a', cacheMaxEntries: 2 });
expect(find).toHaveBeenCalledTimes(before + 1);
});
});
describe('the repositories the credential may be used for', () => {
it('passes them to the source, and keeps answers for different lists apart', async () => {
const find = jest.fn().mockResolvedValue(value);
const lookup = createPullRequestLookup({ source: { find } });
await lookup({ ...input, allowedRepositories: ['o/r'] });
await lookup({ ...input, allowedRepositories: ['o/r', 'fork/*'] });
expect(find).toHaveBeenCalledTimes(2);
expect(find.mock.calls[1][0].allowedRepositories).toEqual(['o/r', 'fork/*']);
await lookup({ ...input, allowedRepositories: ['FORK/*', 'o/r'] });
expect(find).toHaveBeenCalledTimes(2);
});
});

View file

@ -41,6 +41,9 @@ export function createPullRequestLookup({
/** One bounded partition per credential, so a capacity setting only ever evicts entries
* fetched with its own credential. */
const partitions = new Map<string, Map<string, Entry>>();
/** The largest capacity any caller asked for per credential. Principals can share a token yet
* carry different configured sizes, and the smaller must not trim what the larger keeps. */
const capacities = new Map<string, number>();
const inflight = new Map<string, Promise<PullRequestLookupResult>>();
const cooldowns = new Map<string, number>();
const rateLimited: PullRequestLookupResult = { ok: false, error: { code: 'RATE_LIMITED' } };
@ -69,6 +72,7 @@ export function createPullRequestLookup({
const idle = partitions.keys().next();
if (idle.done) break;
partitions.delete(idle.value);
capacities.delete(idle.value);
}
return partition;
}
@ -83,9 +87,11 @@ export function createPullRequestLookup({
): void {
const lifetime = result.ok ? ttlMs : Math.min(ttlMs, FAILURE_TTL_MS);
const entries = partitionOf(scope, credentials);
const bound = Math.max(capacities.get(scope) ?? 0, capacity);
capacities.set(scope, bound);
entries.delete(key);
entries.set(key, { result, expiresAt: now() + lifetime });
while (entries.size > capacity) {
while (entries.size > bound) {
const oldest = entries.keys().next();
if (oldest.done) break;
entries.delete(oldest.value);
@ -107,6 +113,7 @@ export function createPullRequestLookup({
token,
ttlMs,
limits,
allowedRepositories,
cacheMaxEntries,
cacheMaxCredentials,
}) => {
@ -119,6 +126,12 @@ export function createPullRequestLookup({
limits?.maxCheckRunPages,
limits?.maxCandidatePullRequests,
limits?.maxHeadComparisons,
limits?.maxCandidatePages,
/** Which fork a pull request's checks may be read from depends on this list. */
[...(allowedRepositories ?? [])]
.map((entry) => entry.toLowerCase())
.sort()
.join('|'),
].join(',');
const key = `${scope}\0${repo}#${branch}\0${head ?? ''}\0${policy}`;
const cached = touch(scope)?.get(key);
@ -134,7 +147,10 @@ export function createPullRequestLookup({
const run = (async (): Promise<PullRequestLookupResult> => {
let result: PullRequestLookupResult;
try {
result = { ok: true, value: await source.find({ repo, branch, head, token, limits }) };
result = {
ok: true,
value: await source.find({ repo, branch, head, token, limits, allowedRepositories }),
};
} catch (error) {
if (!(error instanceof PullRequestSourceError)) {
logger.warn('[PullRequests] Lookup failed', getSafeErrorMetadata(error));

View file

@ -0,0 +1,8 @@
/** `owner/name` or `owner/*`, compared without case as GitHub does. Nothing else matches. */
export function isAllowedRepository(repo: string, allowed: readonly string[] | undefined): boolean {
const [owner, name] = repo.toLowerCase().split('/');
return (allowed ?? []).some((entry) => {
const [allowedOwner, allowedName] = entry.toLowerCase().split('/');
return allowedOwner === owner && (allowedName === '*' || allowedName === name);
});
}

View file

@ -0,0 +1,49 @@
import type { Dispatcher } from 'undici';
import { createProxyAwareFetch } from './transport';
const dispatcher = { name: 'proxy' } as unknown as Dispatcher;
type Fetch = typeof globalThis.fetch;
const recordingFetch = () => {
const calls: Array<[unknown, RequestInit | undefined]> = [];
const fetchFn = Object.assign(
async (input: unknown, init?: RequestInit) => {
calls.push([input, init]);
return new Response('{}');
},
{ preconnect: () => undefined },
) as unknown as Fetch;
return { fetchFn, calls };
};
describe('createProxyAwareFetch', () => {
it('sends the request through the proxy dispatcher when one is configured', async () => {
const { fetchFn, calls } = recordingFetch();
const signal = new AbortController().signal;
await createProxyAwareFetch(fetchFn, () => dispatcher)('https://api.github.com/x', {
headers: { a: 'b' },
signal,
});
expect(calls).toEqual([
['https://api.github.com/x', { headers: { a: 'b' }, signal, dispatcher }],
]);
});
it('goes direct, with the request untouched, when no proxy is configured', async () => {
const { fetchFn, calls } = recordingFetch();
const init = { headers: { a: 'b' } };
await createProxyAwareFetch(fetchFn, () => undefined)('https://api.github.com/x', init);
expect(calls).toEqual([['https://api.github.com/x', init]]);
expect((calls[0][1] as { dispatcher?: unknown }).dispatcher).toBeUndefined();
});
it('resolves the dispatcher for each request', async () => {
const { fetchFn, calls } = recordingFetch();
const getDispatcher = jest.fn<Dispatcher | undefined, []>().mockReturnValue(undefined);
const wrapped = createProxyAwareFetch(fetchFn, getDispatcher);
await wrapped('https://api.github.com/a');
getDispatcher.mockReturnValue(dispatcher);
await wrapped('https://api.github.com/b');
expect(calls[1][1]).toMatchObject({ dispatcher });
expect((calls[0][1] as { dispatcher?: unknown } | undefined)?.dispatcher).toBeUndefined();
});
});

View file

@ -0,0 +1,21 @@
import type { Dispatcher } from 'undici';
import type { PullRequestFetch } from './github';
import { getEnvProxyDispatcher } from '~/utils/proxy';
/**
* Wraps a fetch so every request goes through the deployment's proxy when one is configured
* (`HTTPS_PROXY` and friends, honoring `NO_PROXY`), and goes direct when none is. The dispatcher is
* resolved per request, so an environment that changes at runtime is respected, and the client and
* the dispatcher are both arguments, so a caller or a test supplies its own.
*/
export function createProxyAwareFetch(
fetchFn: typeof globalThis.fetch = globalThis.fetch,
getDispatcher: () => Dispatcher | undefined = getEnvProxyDispatcher,
): PullRequestFetch {
return (input, init) => {
const dispatcher = getDispatcher();
if (dispatcher == null) return fetchFn(input, init);
const request: RequestInit & { dispatcher?: Dispatcher } = { ...init, dispatcher };
return fetchFn(input, request);
};
}

View file

@ -32,6 +32,9 @@ export type PullRequestLookupLimits = {
maxCandidatePullRequests?: number;
/** Most candidates compared with the recorded commit before the search gives up. */
maxHeadComparisons?: number;
/** Pages of candidates read per state, so a branch name reused more than one page of times
* can still reach an older pull request. One page is what the feature shipped with. */
maxCandidatePages?: number;
};
export type PullRequestFindInput = {
@ -42,6 +45,9 @@ export type PullRequestFindInput = {
head?: string | null;
token: string;
limits?: PullRequestLookupLimits;
/** The repositories the token may be used for. A pull request from a fork has its checks read
* from the fork only when the fork is named here, so the token never reaches an unlisted one. */
allowedRepositories?: readonly string[];
};
/** Finds the pull request for a branch. Null is a documented absence, not a failure. */