Skip to content

Commit 3046545

Browse files
fix(pull-requests): a filtered list fills its page instead of waiting for a scroll (pingdotgg#23923)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7beaa01 commit 3046545

6 files changed

Lines changed: 312 additions & 40 deletions

File tree

‎apps/mobile/src/features/pull-requests/PullRequestsScreen.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,15 @@ export function PullRequestsScreen(
348348
onPress={() => void result.loadMore(true)}
349349
/>
350350
</View>
351+
) : result.hasMore ? (
352+
// A filtered page can be too short to scroll, which leaves nothing to reach the end of.
353+
<View className="items-center py-4">
354+
<Button
355+
label="Load more"
356+
accessibilityLabel="Load more pull requests"
357+
onPress={() => void result.loadMore()}
358+
/>
359+
</View>
351360
) : undefined
352361
}
353362
ListEmptyComponent={

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3047,6 +3047,91 @@ it.effect("refuses a way of updating a branch that the host or the viewer does n
30473047
}),
30483048
);
30493049

3050+
it.effect("keeps reading a host's thinned-out slices until a filtered page is full", () =>
3051+
Effect.gen(function* () {
3052+
const reads: Array<number> = [];
3053+
const rows = Array.from({ length: 40 }, (_, index) => ({
3054+
...changeRequest(index + 1, `2026-07-${String(40 - index).padStart(2, "0")}T00:00:00Z`),
3055+
labels: index % 2 === 0 ? [{ name: "bug", color: null }] : [],
3056+
}));
3057+
const service = yield* makeService({
3058+
projects: [
3059+
project({
3060+
id: "p1",
3061+
title: "on gitlab",
3062+
workspaceRoot: "/a",
3063+
repository: "group/project",
3064+
provider: SourceControlProviderKind.make("gitlab"),
3065+
}),
3066+
],
3067+
providers: [
3068+
fakeProvider(SourceControlProviderKind.make("gitlab"), {
3069+
listChangeRequests: (input) => {
3070+
const delivered = input.cursor?.delivered ?? 0;
3071+
reads.push(delivered);
3072+
return Effect.succeed({
3073+
items: rows.slice(delivered, delivered + input.limit),
3074+
truncated: delivered + input.limit < rows.length,
3075+
continues: true,
3076+
});
3077+
},
3078+
}),
3079+
],
3080+
});
3081+
3082+
const bugs = yield* service.list({ state: "open", limit: 4, filters: { labels: [["bug"]] } });
3083+
assert.deepStrictEqual(
3084+
bugs.entries.map((entry) => entry.number),
3085+
[1, 3, 5, 7],
3086+
);
3087+
assert.deepStrictEqual(reads, [0, 4]);
3088+
3089+
reads.length = 0;
3090+
const none = yield* service.list({
3091+
state: "open",
3092+
limit: 4,
3093+
filters: { labels: [["missing"]] },
3094+
});
3095+
assert.deepStrictEqual(none.entries, []);
3096+
assert.strictEqual(reads.length, 5);
3097+
assert.match(Object.values(none.nextCursors)[0] ?? "", /\|20\|/);
3098+
}),
3099+
);
3100+
3101+
it.effect("hands a host's own resume position back on the next read", () =>
3102+
Effect.gen(function* () {
3103+
const resumed: Array<string | undefined> = [];
3104+
const service = yield* makeService({
3105+
projects: [
3106+
project({
3107+
id: "p1",
3108+
title: "on gitlab",
3109+
workspaceRoot: "/a",
3110+
repository: "group/project",
3111+
provider: SourceControlProviderKind.make("gitlab"),
3112+
}),
3113+
],
3114+
providers: [
3115+
fakeProvider(SourceControlProviderKind.make("gitlab"), {
3116+
listChangeRequests: (input) => {
3117+
resumed.push(input.cursor?.resumeAfter);
3118+
return Effect.succeed({
3119+
items: [changeRequest(resumed.length, "2026-07-02T00:00:00Z")],
3120+
truncated: true,
3121+
continues: true,
3122+
resumeAfter: `2026-07-01T00:00:00.000Z~${resumed.length}`,
3123+
});
3124+
},
3125+
}),
3126+
],
3127+
});
3128+
3129+
const first = yield* service.list({ state: "open", limit: 1 });
3130+
yield* service.list({ state: "open", limit: 1, cursors: first.nextCursors });
3131+
assert.deepStrictEqual(resumed, [undefined, "2026-07-01T00:00:00.000Z~1"]);
3132+
}),
3133+
);
3134+
30503135
it.effect("judges the review filter only on a host that summarises its reviews", () =>
30513136
Effect.gen(function* () {
30523137
const service = yield* makeService({

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ function supersedesReading(current: PullRequestReading | undefined, next: PullRe
123123
const DEFAULT_REPOSITORY_LIST_LIMIT = 99;
124124
const REPOSITORY_CONCURRENCY = 12;
125125
const REPOSITORY_SEARCH_CHUNK = 100;
126+
const FILTERED_LIST_READS = 5;
126127

127128
/**
128129
* Every read leaves the process — a CLI per repository, against hosts whose limits are low
@@ -341,7 +342,7 @@ interface ListCursor extends ProviderListCursor {
341342
}
342343

343344
const LIST_CURSOR_PATTERN =
344-
/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:\d{2}))\|(\d{1,9})\|(\d{1,9}(?:,\d{1,9})*)?$/;
345+
/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:\d{2}))\|(\d{1,9})\|(\d{1,9}(?:,\d{1,9})*)?(?:\|([\w.:~+-]{1,128}))?$/;
345346

346347
function parseListCursor(raw: string): ListCursor | null {
347348
const match = LIST_CURSOR_PATTERN.exec(raw);
@@ -351,6 +352,7 @@ function parseListCursor(raw: string): ListCursor | null {
351352
updatedBefore: match[1]!,
352353
delivered: Number(match[2]),
353354
seenAt: seenAt === undefined ? [] : seenAt.split(",").map(Number),
355+
...(match[4] === undefined ? {} : { resumeAfter: match[4] }),
354356
};
355357
}
356358

@@ -368,27 +370,30 @@ function nextListCursor(
368370
delivered: ReadonlyArray<ProviderChangeRequest>,
369371
/** A provider may consume malformed offset-paged rows that never appear in `delivered`. */
370372
cursorAdvance = delivered.length,
373+
resumeAfter?: string,
371374
): string | null {
372375
if (fetched.length === 0) return null;
373376
// Taken from what the host answered rather than from what survived de-duplication: a slice can
374377
// be entirely rows already sent — a hundred change requests touched in the same second is one
375378
// repository's boring afternoon — and reading "nothing new" as "nothing left" would end the
376379
// walk on the instant it was stuck on, with everything older unreachable for good.
377380
const oldest = fetched.reduce((left, right) => (right.updatedAt < left.updatedAt ? right : left));
378-
return listCursorAt(previous, oldest.updatedAt, fetched, cursorAdvance);
381+
return listCursorAt(previous, oldest.updatedAt, fetched, cursorAdvance, resumeAfter);
379382
}
380383

381384
function listCursorAt(
382385
previous: ListCursor | undefined,
383386
boundary: string,
384387
fetched: ReadonlyArray<ProviderChangeRequest>,
385388
deliveredCount: number,
389+
resumeAfter?: string,
386390
): string {
387391
const seenAt = new Set([
388392
...(previous?.updatedBefore === boundary ? previous.seenAt : []),
389393
...fetched.filter((item) => item.updatedAt === boundary).map((item) => item.number),
390394
]);
391-
return `${boundary}|${(previous?.delivered ?? 0) + deliveredCount}|${[...seenAt].join(",")}`;
395+
const cursor = `${boundary}|${(previous?.delivered ?? 0) + deliveredCount}|${[...seenAt].join(",")}`;
396+
return resumeAfter === undefined ? cursor : `${cursor}|${resumeAfter}`;
392397
}
393398

394399
function isProviderUnusable(error: PullRequestProviderError): boolean {
@@ -1165,11 +1170,13 @@ export const make = Effect.gen(function* () {
11651170
const cursorOf = (project: SupportedProject): ListCursor | undefined =>
11661171
continuation?.get(project.cursorKey);
11671172

1168-
const readRepository = (project: SupportedProject): Effect.Effect<RepositoryBatch> => {
1173+
const readSlice = (
1174+
project: SupportedProject,
1175+
cursor: ListCursor | undefined,
1176+
): Effect.Effect<RepositoryBatch> => {
11691177
{
11701178
const viewer = viewers[project.host]!;
11711179
const key = project.cursorKey;
1172-
const cursor = cursorOf(project);
11731180
return project.api
11741181
.listChangeRequests({
11751182
cwd: project.project.workspaceRoot,
@@ -1188,6 +1195,7 @@ export const make = Effect.gen(function* () {
11881195
updatedBefore: cursor.updatedBefore,
11891196
delivered: cursor.delivered,
11901197
seenAt: cursor.seenAt,
1198+
resumeAfter: cursor.resumeAfter,
11911199
},
11921200
}),
11931201
})
@@ -1214,7 +1222,13 @@ export const make = Effect.gen(function* () {
12141222
truncated: page.truncated,
12151223
nextCursor:
12161224
page.continues && page.truncated
1217-
? nextListCursor(cursor, page.items, items, page.cursorAdvance)
1225+
? nextListCursor(
1226+
cursor,
1227+
page.items,
1228+
items,
1229+
page.cursorAdvance,
1230+
page.resumeAfter,
1231+
)
12181232
: null,
12191233
};
12201234
}),
@@ -1235,6 +1249,32 @@ export const make = Effect.gen(function* () {
12351249
}
12361250
};
12371251

1252+
// Filters a host cannot apply thin out the slices it hands over, so a repository keeps
1253+
// reading until it has a page's worth or runs out. The reads are bounded: a filter that
1254+
// matches nothing would otherwise walk the repository's whole history in one request.
1255+
const readRepository = (project: SupportedProject): Effect.Effect<RepositoryBatch> => {
1256+
const fill = (batch: RepositoryBatch, reads: number): Effect.Effect<RepositoryBatch> => {
1257+
const cursor = batch.nextCursor === null ? null : parseListCursor(batch.nextCursor);
1258+
if (
1259+
input.filters === undefined ||
1260+
cursor === null ||
1261+
batch.entries.length >= limit ||
1262+
reads >= FILTERED_LIST_READS
1263+
) {
1264+
return Effect.succeed(batch);
1265+
}
1266+
return readSlice(project, cursor).pipe(
1267+
Effect.flatMap((next) =>
1268+
// A slice that failed keeps the cursor before it, so the reader can try again.
1269+
next.errors.length > 0
1270+
? Effect.succeed(batch)
1271+
: fill({ ...next, entries: [...batch.entries, ...next.entries] }, reads + 1),
1272+
),
1273+
);
1274+
};
1275+
return readSlice(project, cursorOf(project)).pipe(Effect.flatMap((batch) => fill(batch, 1)));
1276+
};
1277+
12381278
const readTogether = (
12391279
chunk: ReadonlyArray<SupportedProject>,
12401280
): Effect.Effect<ReadonlyArray<RepositoryBatch>> => {

‎packages/source-control-core/src/server/PullRequestProvider.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export interface ProviderChangeRequestPage {
144144
* may need to count malformed raw rows it consumed too.
145145
*/
146146
readonly cursorAdvance?: number;
147+
/**
148+
* Where a host that can only page from the top carries on: an opaque position past the last
149+
* row delivered, handed back as the cursor's `resumeAfter`. Unlike a count of rows it survives
150+
* rows above it closing between reads.
151+
*/
152+
readonly resumeAfter?: string;
147153
readonly continues: boolean;
148154
}
149155

@@ -160,6 +166,7 @@ export interface ProviderListCursor {
160166
* applying their limit so a full page of tied updates does not repeat the same prefix.
161167
*/
162168
readonly seenAt?: ReadonlyArray<number> | undefined;
169+
readonly resumeAfter?: string | undefined;
163170
}
164171

165172
export interface ProviderBatchedChangeRequest extends ProviderChangeRequest {

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts‎

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -448,20 +448,87 @@ describe("GitCafePullRequestProvider", () => {
448448
}).pipe(Effect.provide(server.layer));
449449
});
450450

451-
it.effect("leaves an author GitCafe has no profile for to be narrowed by login", () => {
452-
const server = fakeGitCafe({ "GET /repos/owner/repo/pulls": { items: [pull], next: null } });
451+
it.effect("finds a GitHub-synced author's actor on their pull requests to narrow by it", () => {
452+
const synced = {
453+
...pull,
454+
number: 8,
455+
author: { kind: "github", actorId: "act_synced", login: "Synced", avatarUrl: null },
456+
};
457+
const server = fakeGitCafe({
458+
"GET /repos/owner/repo/pulls": (request: SentRequest) =>
459+
request.query.get("authors") === '["act_synced"]'
460+
? { items: [synced], next: null }
461+
: request.query.get("after") === null
462+
? { items: [pull], next: "o1" }
463+
: { items: [synced], next: null },
464+
});
453465
return Effect.gen(function* () {
454466
const provider = yield* GitCafePullRequestProvider.make;
455-
const page = yield* provider.listChangeRequests({
456-
...target,
457-
state: "open",
458-
involvement: "all",
459-
viewer: "alice",
460-
limit: 10,
461-
filters: { author: "synced-only" },
462-
});
463-
assert.strictEqual(page.items.length, 1);
464-
assert.isNull(server.sent.at(-1)!.query.get("authors"));
467+
const list = (author: string) =>
468+
provider.listChangeRequests({
469+
...target,
470+
state: "open",
471+
involvement: "all",
472+
viewer: "alice",
473+
limit: 10,
474+
filters: { author },
475+
});
476+
const page = yield* list("synced");
477+
assert.deepStrictEqual(
478+
page.items.map((item) => item.number),
479+
[8],
480+
);
481+
assert.strictEqual(server.sent.at(-1)!.query.get("authors"), '["act_synced"]');
482+
// Once seen, the actor is narrowed by straight away.
483+
const sentBefore = server.sent.length;
484+
yield* list("synced");
485+
assert.deepStrictEqual(
486+
server.sent.slice(sentBefore).map((request) => request.query.get("authors")),
487+
[null, '["act_synced"]'],
488+
);
489+
// An author on none of the pages it reads gets nothing rather than unnarrowed rows a
490+
// continuation would count against a different listing.
491+
const missing = yield* list("nobody");
492+
assert.deepStrictEqual(missing, { items: [], truncated: false, continues: false });
493+
}).pipe(Effect.provide(server.layer));
494+
});
495+
496+
it.effect("carries a listing on after the last row delivered when rows above it close", () => {
497+
const created = (number: number) => `2026-09-12T12:00:0${9 - number}Z`;
498+
let pulls = [1, 2, 3, 4, 5].map((number) => ({ ...pull, number, createdAt: created(number) }));
499+
const server = fakeGitCafe({
500+
"GET /repos/owner/repo/pulls": (request: SentRequest) => {
501+
const offset = Number(request.query.get("after")?.slice(1) ?? 0);
502+
const end = offset + Number(request.query.get("limit"));
503+
return { items: pulls.slice(offset, end), next: end < pulls.length ? `o${end}` : null };
504+
},
505+
});
506+
return Effect.gen(function* () {
507+
const provider = yield* GitCafePullRequestProvider.make;
508+
const list = (cursor?: { readonly resumeAfter?: string }) =>
509+
provider.listChangeRequests({
510+
...target,
511+
state: "open",
512+
involvement: "all",
513+
viewer: "alice",
514+
limit: 2,
515+
...(cursor === undefined
516+
? {}
517+
: { cursor: { updatedBefore: timestamp, delivered: 2, ...cursor } }),
518+
});
519+
const first = yield* list();
520+
assert.deepStrictEqual(
521+
first.items.map((item) => item.number),
522+
[1, 2],
523+
);
524+
pulls = pulls.filter((candidate) => candidate.number !== 1);
525+
const next = yield* list({ resumeAfter: first.resumeAfter! });
526+
assert.deepStrictEqual(
527+
next.items.map((item) => item.number),
528+
[3, 4],
529+
);
530+
assert.isTrue(next.truncated);
531+
assert.isTrue(next.continues);
465532
}).pipe(Effect.provide(server.layer));
466533
});
467534

0 commit comments

Comments
 (0)