Skip to content

Commit 3e90168

Browse files
Merge pull request #338 from GenerationSoftware/GEN-2816-idle-run-list-polling
GEN-2816 back off idle run-list polling
2 parents e2bf11b + be0abd7 commit 3e90168

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎packages/app/src/features/runs/queries.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export const diagnosticsQuery = (runId: string) => queryOptions({ queryKey: ["ru
2222
export const runPageQuery = ({ jobId, projectIds, jobIds, cursor, limit, sort, direction, states }: { jobId?: string; projectIds?: string[]; jobIds?: string[]; cursor?: string; limit: number; sort?: "job" | "run" | "status" | "created" | "agent"; direction?: "asc" | "desc"; states?: RunStatus[] }) => queryOptions({
2323
queryKey: ["runs", "page", jobId, projectIds, jobIds, cursor, limit, sort, direction, states],
2424
queryFn: async ({ signal }) => { const { data, error } = await api.GET("/api/v1/runs", { signal, params: { query: { jobId: jobIds ?? jobId, projectId: projectIds, cursor, limit, sort, direction, state: states } } }); if (!data || error) throw new Error(messageOf(error)); return data; },
25-
staleTime: 0, refetchInterval: 2_000, refetchIntervalInBackground: false,
25+
staleTime: 1_000,
26+
refetchInterval: query => query.state.error ? 10_000 : query.state.data?.items.some(run => ["queued", "reserved", "starting", "running", "blocked", "stopping"].includes(run.state)) ? 2_000 : 15_000,
27+
refetchIntervalInBackground: false,
2628
});
2729
export const historyQuery = (jobId: string, cursor?: string, sort?: "run" | "status" | "created" | "agent", direction?: "asc" | "desc", states?: RunStatus[]) => queryOptions({ queryKey: ["runs", "history", jobId, cursor, sort, direction, states], queryFn: async ({ signal }) => { const { data, error } = await api.GET("/api/v1/runs", { signal, params: { query: { jobId, cursor, sort, direction: direction ?? (sort ? "asc" : undefined), state: states, limit: 30 } } }); if (!data || error) throw new Error(messageOf(error)); return data; }, placeholderData: keepPreviousData, staleTime: 0, refetchOnMount: false });

‎packages/app/test/runs.browser.test.mjs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,40 @@ const jobId = "00000000-0000-4000-8000-000000000001", projectId = "00000000-0000
88
before(async () => { server = createServer(async (req, res) => { const path = new URL(req.url, "http://local").pathname, file = (/^(\/assets\/|\/theme-init.js$|\/bee-mark-monochrome.png$|\/favicon.ico$)/.test(path)) ? path : "/index.html"; try { const body = await readFile(new URL("../dist" + file, import.meta.url)); res.setHeader("Content-Type", file.endsWith(".js") ? "application/javascript" : file.endsWith(".css") ? "text/css" : "text/html"); res.end(body); } catch { res.statusCode = 404; res.end(); } }); await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); origin = "http://127.0.0.1:" + server.address().port; browser = await chromium.launch({ headless: true }); });
99
after(async () => { await browser?.close(); if (server) await new Promise(resolve => server.close(resolve)); });
1010
async function setup() { const context = await browser.newContext(), page = await context.newPage(); page.setDefaultTimeout(10000); await page.route("**/api/v1/session", route => route.fulfill({ json: { authenticated: true, user: { id: "owner", email: "owner@example.test" }, workspace: { id: "tenant", name: "Workspace" }, capabilities: [], expiresAt: "2026-10-10T00:00:00Z" } })); return { context, page }; }
11+
test("run page polling backs off when idle, discovers runs, and stays fast while active", async () => {
12+
const idle = await setup(); let idleCount = 0;
13+
await idle.page.clock.install({ time: new Date(time) });
14+
await idle.page.route("**/api/v1/runs?**", route => { idleCount++; const state = idleCount > 1 ? "running" : "succeeded"; return route.fulfill({ json: { items: [{ id: runId, run_name: "Polling run", state, created_at: time, agent_kind: "codex" }], nextCursor: null } }); });
15+
await idle.page.goto(origin + "/job-runs"); await idle.page.getByRole("link", { name: "Polling run", exact: true }).waitFor();
16+
await new Promise(resolve => setTimeout(resolve, 200)); assert.equal(idleCount, 1, "loader data should be reused when the list component mounts");
17+
await idle.page.clock.runFor(4100); assert.equal(idleCount, 1, "an idle page should make one request in the fixed comparison window");
18+
await idle.page.clock.runFor(10_900); await idle.page.getByRole("img", { name: "Status: Running" }).waitFor(); assert.equal(idleCount, 2, "idle polling should discover a newly created run within 15 seconds");
19+
await idle.context.close();
20+
21+
const active = await setup(); let activeCount = 0;
22+
await active.page.route("**/api/v1/runs?**", route => { activeCount++; return route.fulfill({ json: { items: [{ id: runId, run_name: "Polling run", state: "running", created_at: time, agent_kind: "codex" }], nextCursor: null } }); });
23+
await active.page.goto(origin + "/job-runs"); await active.page.getByRole("link", { name: "Polling run", exact: true }).waitFor();
24+
await new Promise(resolve => setTimeout(resolve, 200)); assert.equal(activeCount, 1, "active loader data should also be reused on mount");
25+
await new Promise(resolve => setTimeout(resolve, 4100)); assert.equal(activeCount, 3, "active rows should make three requests in the same 4.1-second window");
26+
await active.context.close();
27+
});
28+
test("run page errors back off for ten seconds and recover to the idle interval", async () => {
29+
const { context, page } = await setup(); let count = 0;
30+
await page.clock.install({ time: new Date(time) });
31+
await page.route("**/api/v1/runs?**", route => {
32+
count++;
33+
if (count === 1 || count >= 4) return route.fulfill({ json: { items: [{ id: runId, run_name: "Polling run", state: "succeeded", created_at: time, agent_kind: "codex" }], nextCursor: null } });
34+
return route.fulfill({ status: 503, json: { error: { code: "unavailable", message: "Try again" } } });
35+
});
36+
await page.goto(origin + "/job-runs"); await page.getByRole("link", { name: "Polling run", exact: true }).waitFor();
37+
await page.clock.runFor(16_100); await page.getByRole("alert").filter({ hasText: "Try again" }).waitFor();
38+
assert.equal(count, 3, "the idle refetch and its single retry should expose an error");
39+
await page.clock.runFor(9000); assert.equal(count, 3, "errors should wait about ten seconds before another query");
40+
await page.clock.runFor(1100); await page.getByRole("link", { name: "Polling run", exact: true }).waitFor(); assert.equal(count, 4, "the ten-second error interval should retry and recover");
41+
await page.clock.runFor(14999); assert.equal(count, 4, "a successful response should return to the 15-second idle interval");
42+
await page.clock.runFor(1); assert.equal(count, 5);
43+
await context.close();
44+
});
1145
test("all-jobs runs index uses a tenant-scoped page, navigation, refresh, and created time", async () => {
1246
const { context, page } = await setup(); const requests = []; let refreshes = 0;
1347
const runs = Array.from({ length: 21 }, (_, index) => ({ id: `00000000-0000-4000-8000-${String(index + 1).padStart(12, "0")}`, project_id: projectId, project_name: "Project Alpha", job_id: jobId, job_name: "Review builds", run_name: `Run ${index}`, state: index === 0 ? "running" : "succeeded", created_at: time, updated_at: time, started_at: index === 0 ? time : null, agent_kind: "codex", elapsed_time: index === 0 ? { low: 60, high: 180 } : { duration: 120 } }));

0 commit comments

Comments
 (0)