Skip to content

Commit 2364da5

Browse files
fix(gitcafe): carry a listing on from its last row, not a count
GitCafe pages only from the top, so a continuation skipped as many rows as it had delivered. A row above that point closing between reads made it skip one row it had never shown. A page can now hand back an opaque resumeAfter position, which the service keeps in its cursor and passes back. GitCafe resumes after the creation time and number of the last row it delivered. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7365e60 commit 2364da5

5 files changed

Lines changed: 107 additions & 28 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,6 +3098,40 @@ it.effect("keeps reading a host's thinned-out slices until a filtered page is fu
30983098
}),
30993099
);
31003100

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+
31013135
it.effect("judges the review filter only on a host that summarises its reviews", () =>
31023136
Effect.gen(function* () {
31033137
const service = yield* makeService({

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ interface ListCursor extends ProviderListCursor {
342342
}
343343

344344
const LIST_CURSOR_PATTERN =
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})*)?$/;
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}))?$/;
346346

347347
function parseListCursor(raw: string): ListCursor | null {
348348
const match = LIST_CURSOR_PATTERN.exec(raw);
@@ -352,6 +352,7 @@ function parseListCursor(raw: string): ListCursor | null {
352352
updatedBefore: match[1]!,
353353
delivered: Number(match[2]),
354354
seenAt: seenAt === undefined ? [] : seenAt.split(",").map(Number),
355+
...(match[4] === undefined ? {} : { resumeAfter: match[4] }),
355356
};
356357
}
357358

@@ -369,27 +370,30 @@ function nextListCursor(
369370
delivered: ReadonlyArray<ProviderChangeRequest>,
370371
/** A provider may consume malformed offset-paged rows that never appear in `delivered`. */
371372
cursorAdvance = delivered.length,
373+
resumeAfter?: string,
372374
): string | null {
373375
if (fetched.length === 0) return null;
374376
// Taken from what the host answered rather than from what survived de-duplication: a slice can
375377
// be entirely rows already sent — a hundred change requests touched in the same second is one
376378
// repository's boring afternoon — and reading "nothing new" as "nothing left" would end the
377379
// walk on the instant it was stuck on, with everything older unreachable for good.
378380
const oldest = fetched.reduce((left, right) => (right.updatedAt < left.updatedAt ? right : left));
379-
return listCursorAt(previous, oldest.updatedAt, fetched, cursorAdvance);
381+
return listCursorAt(previous, oldest.updatedAt, fetched, cursorAdvance, resumeAfter);
380382
}
381383

382384
function listCursorAt(
383385
previous: ListCursor | undefined,
384386
boundary: string,
385387
fetched: ReadonlyArray<ProviderChangeRequest>,
386388
deliveredCount: number,
389+
resumeAfter?: string,
387390
): string {
388391
const seenAt = new Set([
389392
...(previous?.updatedBefore === boundary ? previous.seenAt : []),
390393
...fetched.filter((item) => item.updatedAt === boundary).map((item) => item.number),
391394
]);
392-
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}`;
393397
}
394398

395399
function isProviderUnusable(error: PullRequestProviderError): boolean {
@@ -1191,6 +1195,7 @@ export const make = Effect.gen(function* () {
11911195
updatedBefore: cursor.updatedBefore,
11921196
delivered: cursor.delivered,
11931197
seenAt: cursor.seenAt,
1198+
resumeAfter: cursor.resumeAfter,
11941199
},
11951200
}),
11961201
})
@@ -1217,7 +1222,13 @@ export const make = Effect.gen(function* () {
12171222
truncated: page.truncated,
12181223
nextCursor:
12191224
page.continues && page.truncated
1220-
? nextListCursor(cursor, page.items, items, page.cursorAdvance)
1225+
? nextListCursor(
1226+
cursor,
1227+
page.items,
1228+
items,
1229+
page.cursorAdvance,
1230+
page.resumeAfter,
1231+
)
12211232
: null,
12221233
};
12231234
}),

‎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: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,9 @@ describe("GitCafePullRequestProvider", () => {
493493
}).pipe(Effect.provide(server.layer));
494494
});
495495

496-
it.effect("carries a listing on past the rows it already delivered", () => {
497-
const pulls = [1, 2, 3, 4, 5].map((number) => ({ ...pull, number }));
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) }));
498499
const server = fakeGitCafe({
499500
"GET /repos/owner/repo/pulls": (request: SentRequest) => {
500501
const offset = Number(request.query.get("after")?.slice(1) ?? 0);
@@ -504,20 +505,30 @@ describe("GitCafePullRequestProvider", () => {
504505
});
505506
return Effect.gen(function* () {
506507
const provider = yield* GitCafePullRequestProvider.make;
507-
const page = yield* provider.listChangeRequests({
508-
...target,
509-
state: "open",
510-
involvement: "all",
511-
viewer: "alice",
512-
limit: 2,
513-
cursor: { updatedBefore: timestamp, delivered: 2 },
514-
});
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();
515520
assert.deepStrictEqual(
516-
page.items.map((item) => item.number),
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),
517528
[3, 4],
518529
);
519-
assert.isTrue(page.truncated);
520-
assert.isTrue(page.continues);
530+
assert.isTrue(next.truncated);
531+
assert.isTrue(next.continues);
521532
}).pipe(Effect.provide(server.layer));
522533
});
523534

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ function stackHeadsCurrent(
175175
);
176176
}
177177

178+
const POSITION = /^(.+)~(\d+)$/u;
179+
const parsePosition = (raw: string | undefined) => {
180+
const match = raw === undefined ? null : POSITION.exec(raw);
181+
const created = match === null ? Number.NaN : Date.parse(match[1]!);
182+
return match === null || Number.isNaN(created) ? null : { created, number: Number(match[2]) };
183+
};
184+
178185
const withDetails = (
179186
files: ReadonlyArray<Json.GitCafeDiffFile>,
180187
details: ReadonlyArray<Json.GitCafeDiffFile>,
@@ -682,8 +689,8 @@ export const make = Effect.gen(function* () {
682689
).pipe(Effect.tap((page) => Effect.sync(() => learnAuthors(input.host, page.items))));
683690
};
684691
if (unresolvedAuthor !== null) {
685-
// Rows are only ever handed out narrowed by actor, so a continuation's count of rows
686-
// already delivered always refers to the same narrowed listing.
692+
// Rows are only ever handed out narrowed by actor, so a continuation always carries on
693+
// the same narrowed listing.
687694
const key = authorKey(input.host, unresolvedAuthor);
688695
let after: string | null = null;
689696
let pages = 0;
@@ -699,22 +706,31 @@ export const make = Effect.gen(function* () {
699706
}
700707
const limit = Math.max(1, input.limit);
701708
const items: Array<Json.GitCafePull> = [];
702-
// GitCafe pages by an opaque token and orders by creation, so a continuation counts past
703-
// the rows already delivered rather than asking for older ones.
704-
let skip = input.cursor?.delivered ?? 0;
709+
// GitCafe pages by an opaque token and orders by creation, so a continuation reads from the
710+
// top and keeps only the rows created after the last one delivered. A count of rows would
711+
// skip one for every row above it that closed in between.
712+
const resume = parsePosition(input.cursor?.resumeAfter);
713+
const isNew = (pull: Json.GitCafePull) => {
714+
if (resume === null) return true;
715+
const created = Date.parse(pull.createdAt);
716+
return (
717+
created < resume.created || (created === resume.created && pull.number < resume.number)
718+
);
719+
};
705720
let next: string | null = null;
706721
do {
707722
const page: typeof Json.GitCafePulls.Type = yield* readPage(
708-
Math.min(PAGE_SIZE, skip + limit - items.length),
723+
resume === null ? Math.min(PAGE_SIZE, limit - items.length) : PAGE_SIZE,
709724
next,
710725
);
711-
const skipped = Math.min(skip, page.items.length);
712-
skip -= skipped;
713-
items.push(...page.items.slice(skipped));
726+
items.push(...page.items.filter(isNew));
714727
next = page.items.length === 0 ? null : page.next;
715728
} while (next !== null && items.length < limit);
729+
const delivered = items.slice(0, limit);
730+
const last = delivered.at(-1);
716731
return {
717-
items: items.slice(0, limit).map((pull) => {
732+
...(last === undefined ? {} : { resumeAfter: `${last.createdAt}~${last.number}` }),
733+
items: delivered.map((pull) => {
718734
const item = Json.toChangeRequest(pull, input);
719735
return input.involvement === "reviewing" && actorId !== undefined
720736
? {
@@ -723,7 +739,7 @@ export const make = Effect.gen(function* () {
723739
}
724740
: item;
725741
}),
726-
truncated: next !== null,
742+
truncated: next !== null || items.length > limit,
727743
continues: true,
728744
};
729745
},

0 commit comments

Comments
 (0)