Skip to content

Commit 00b414f

Browse files
committed
test: cover review context window hydration
1 parent e74aa62 commit 00b414f

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

‎src/clawsweeper.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,7 +2092,7 @@ function ghPaged<T>(path: string): T[] {
20922092
return pages.flatMap((page) => (Array.isArray(page) ? (page as T[]) : []));
20932093
}
20942094

2095-
interface ContextHydration<T> {
2095+
export interface ContextHydration<T> {
20962096
items: T[];
20972097
total: number;
20982098
hydrated: number;
@@ -2140,22 +2140,28 @@ export function githubContextWindowPlan(
21402140
};
21412141
}
21422142

2143-
function ghPagedContextWindow<T>(
2143+
export function ghPagedContextWindow<T>(
21442144
path: string,
21452145
totalCount: unknown,
21462146
promptLimit: number,
2147+
fetchers: {
2148+
page?: (path: string, page: number) => T[];
2149+
paged?: (path: string) => T[];
2150+
} = {},
21472151
): ContextHydration<T> {
2152+
const fetchPage = fetchers.page ?? ghPage<T>;
2153+
const fetchPaged = fetchers.paged ?? ghPaged<T>;
21482154
const total = githubCount(totalCount);
21492155
const boundedLimit = Math.max(0, Math.floor(promptLimit));
21502156
if (total === null) {
2151-
const items = ghPaged<T>(path);
2157+
const items = fetchPaged(path);
21522158
return { items, total: items.length, hydrated: items.length, truncated: false };
21532159
}
21542160
if (total === 0 || boundedLimit === 0) {
21552161
return { items: [], total, hydrated: 0, truncated: total > 0 };
21562162
}
21572163
if (total <= boundedLimit) {
2158-
const items = total <= 100 ? ghPage<T>(path, 1) : ghPaged<T>(path);
2164+
const items = total <= 100 ? fetchPage(path, 1) : fetchPaged(path);
21592165
return {
21602166
items,
21612167
total: Math.max(total, items.length),
@@ -2165,12 +2171,12 @@ function ghPagedContextWindow<T>(
21652171
}
21662172

21672173
const plan = githubContextWindowPlan(total, boundedLimit);
2168-
const firstPage = plan.keepStart > 0 ? ghPage<T>(path, 1) : [];
2174+
const firstPage = plan.keepStart > 0 ? fetchPage(path, 1) : [];
21692175
const headItems = firstPage.slice(0, plan.keepStart);
21702176
const tailPages: T[] = [];
21712177
if (plan.keepEnd > 0) {
21722178
for (let page = plan.tailFirstPageNumber; page <= plan.lastPageNumber; page += 1) {
2173-
tailPages.push(...(page === 1 && plan.keepStart > 0 ? firstPage : ghPage<T>(path, page)));
2179+
tailPages.push(...(page === 1 && plan.keepStart > 0 ? firstPage : fetchPage(path, page)));
21742180
}
21752181
}
21762182
const tailItems = tailPages.slice(plan.tailOffset, plan.tailOffset + plan.keepEnd);

‎test/clawsweeper.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
fixedPullRequestFromCommitPullsForTest,
2424
formatRecentClosedRows,
2525
githubContextWindowPlan,
26+
ghPagedContextWindow,
2627
githubPaginatedPath,
2728
ghRetryKind,
2829
hotIntakeRecencyMs,
@@ -276,6 +277,53 @@ test("githubContextWindowPlan keeps large tails to the final page when possible"
276277
});
277278
});
278279

280+
test("ghPagedContextWindow reuses first page when tail overlaps the head page", () => {
281+
const fetchedPages: number[] = [];
282+
const window = ghPagedContextWindow<number>(
283+
"repos/openclaw/openclaw/issues/123/comments",
284+
101,
285+
80,
286+
{
287+
page: (_path, page) => {
288+
fetchedPages.push(page);
289+
const start = (page - 1) * 100 + 1;
290+
const end = Math.min(page * 100, 101);
291+
return Array.from({ length: end - start + 1 }, (_, index) => start + index);
292+
},
293+
},
294+
);
295+
296+
assert.deepEqual(fetchedPages, [1, 2]);
297+
assert.deepEqual(window.items, [
298+
...Array.from({ length: 40 }, (_, index) => index + 1),
299+
...Array.from({ length: 40 }, (_, index) => index + 62),
300+
]);
301+
assert.equal(window.total, 101);
302+
assert.equal(window.hydrated, 80);
303+
assert.equal(window.truncated, true);
304+
});
305+
306+
test("ghPagedContextWindow falls back to full pagination when total is missing", () => {
307+
const window = ghPagedContextWindow<number>(
308+
"repos/openclaw/openclaw/pulls/123/files",
309+
undefined,
310+
80,
311+
{
312+
paged: () => [1, 2, 3],
313+
page: () => {
314+
throw new Error("page fetch should not be used without a total count");
315+
},
316+
},
317+
);
318+
319+
assert.deepEqual(window, {
320+
items: [1, 2, 3],
321+
total: 3,
322+
hydrated: 3,
323+
truncated: false,
324+
});
325+
});
326+
279327
test("review prompt assets match tracked files", () => {
280328
assert.equal(reviewPromptTemplate(), readFileSync("prompts/review-item.md", "utf8"));
281329
assert.deepEqual(

0 commit comments

Comments
 (0)