Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ jobs:
cache-dependency-path: web/package-lock.json
- run: npm ci
- run: npx tsc --noEmit
- run: npm run test:polling
- run: npm run build:static
3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"check": "biome check",
"check:fix": "biome check --write",
"prepare": "husky",
"generate:presets": "ts-node -P tsconfig.scripts.json src/scripts/generate-theme-presets.ts"
"generate:presets": "ts-node -P tsconfig.scripts.json src/scripts/generate-theme-presets.ts",
"test:polling": "node --test scripts/polling.test.mjs"
},
"lint-staged": {
"*.{js,ts,jsx,tsx}": [
Expand Down
120 changes: 120 additions & 0 deletions web/scripts/polling.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import vm from "node:vm";
import path from "node:path";
import { fileURLToPath } from "node:url";
import ts from "typescript";

const web = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
function moduleFor(name, globals = {}) {
const output = ts.transpileModule(fs.readFileSync(path.join(web, "src/lib", name + ".ts"), "utf8"), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
}).outputText;
const exports = {};
vm.runInNewContext(output, { exports, ...globals });
return exports;
}
function setup(hidden = false) {
let sequence = 0;
const timers = new Map();
const listeners = new Set();
const document = {
visibilityState: hidden ? "hidden" : "visible",
addEventListener: (_, callback) => listeners.add(callback),
removeEventListener: (_, callback) => listeners.delete(callback),
};
const { setVisibleInterval } = moduleFor("polling", {
document,
setTimeout: (callback) => {
const id = ++sequence;
timers.set(id, callback);
return id;
},
clearTimeout: (id) => timers.delete(id),
});
return {
setVisibleInterval,
timers,
listeners,
visibility(hidden) {
document.visibilityState = hidden ? "hidden" : "visible";
for (const callback of listeners) callback();
},
tick() {
const entries = [...timers];
timers.clear();
for (const [, callback] of entries) callback();
},
};
}
async function settle() {
for (let i = 0; i < 8; i++) await Promise.resolve();
}
test("hidden tabs make no scheduled requests; returning refreshes immediately and cleanup stops work", async () => {
const s = setup(true);
let calls = 0;
const stop = s.setVisibleInterval(() => {
calls++;
}, 5000);
s.tick();
await settle();
assert.equal(calls, 0);
assert.equal(s.timers.size, 0);
s.visibility(false);
await settle();
assert.equal(calls, 1);
assert.equal(s.timers.size, 1);
s.visibility(true);
assert.equal(s.timers.size, 0);
s.visibility(false);
await settle();
assert.equal(calls, 2);
stop();
s.tick();
s.visibility(false);
await settle();
assert.equal(calls, 2);
assert.equal(s.listeners.size, 0);
assert.equal(s.timers.size, 0);
});
test("a slow poll cannot overlap on timer or visibility changes; dispose while running cannot revive it", async () => {
const s = setup();
let calls = 0;
let release;
const stop = s.setVisibleInterval(() => {
calls++;
return new Promise((resolve) => {
release = resolve;
});
}, 2000);
s.tick();
assert.equal(calls, 1);
s.visibility(true);
s.visibility(false);
s.tick();
assert.equal(calls, 1);
release();
await settle();
assert.equal(s.timers.size, 1);
s.tick();
assert.equal(calls, 2);
stop();
release();
await settle();
assert.equal(s.timers.size, 0);
});
test("a failed background refresh retries on the next interval", async () => {
const s = setup();
let calls = 0;
const stop = s.setVisibleInterval(async () => {
if (++calls === 1) throw Error("temporary");
}, 1000);
s.tick();
await settle();
assert.equal(s.timers.size, 1);
s.tick();
await settle();
assert.equal(calls, 2);
stop();
});
5 changes: 3 additions & 2 deletions web/src/app/(main)/_components/sidebar/nav-main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import type {
NavMainLinkItem,
NavMainParentItem,
} from "@/navigation/sidebar/sidebar-items";
import { setVisibleInterval } from "@/lib/polling";

interface NavMainProps {
readonly items: readonly NavGroup[];
Expand Down Expand Up @@ -174,8 +175,8 @@ function InterceptPendingBadge() {
} catch { /* ignore */ }
}
poll();
const t = setInterval(poll, 5000);
return () => { live = false; clearInterval(t); };
const stopPolling = setVisibleInterval(poll, 5000);
return () => { live = false; stopPolling(); };
}, []);
if (count === 0) return null;
return (
Expand Down
13 changes: 7 additions & 6 deletions web/src/app/(main)/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import type {
UsageStats,
} from "@/lib/types";
import { cn } from "@/lib/utils";
import { setVisibleInterval } from "@/lib/polling";

// ── chart constants ───────────────────────────────────────────────────────────

Expand Down Expand Up @@ -172,10 +173,10 @@ export default function DashboardPage() {
}
};
void load();
const t = setInterval(load, 5000);
const stopPolling = setVisibleInterval(load, 5000);
return () => {
alive = false;
clearInterval(t);
stopPolling();
};
}, []);

Expand Down Expand Up @@ -211,10 +212,10 @@ export default function DashboardPage() {
}
};
void load();
const timer = setInterval(load, 5000);
const stopPolling = setVisibleInterval(load, 5000);
return () => {
alive = false;
clearInterval(timer);
stopPolling();
};
}, []);

Expand Down Expand Up @@ -247,10 +248,10 @@ export default function DashboardPage() {
}
};
void load();
const t = setInterval(load, 15000);
const stopPolling = setVisibleInterval(load, 15000);
return () => {
alive = false;
clearInterval(t);
stopPolling();
};
}, []);

Expand Down
9 changes: 5 additions & 4 deletions web/src/app/(main)/function/findings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
VERIFICATION_META,
VERIFICATION_STATUSES,
} from "./_components/findings-table";
import { setVisibleInterval } from "@/lib/polling";

const FINDING_LIST_PREFERENCE_KEY = "artex_finding_list_preferences";

Expand Down Expand Up @@ -573,8 +574,8 @@ export default function FindingsPage() {
};
refresh();
if (view === "asset") return;
const timer = setInterval(refresh, 5000);
return () => clearInterval(timer);
const stopPolling = setVisibleInterval(refresh, 5000);
return stopPolling;
}, [activeRetestFingerprint, loadFlat, loadGroup, preferencesHydrated, refreshGroups, view]);

React.useEffect(() => {
Expand Down Expand Up @@ -606,10 +607,10 @@ export default function FindingsPage() {
});
};
load();
const t = setInterval(load, 5000);
const stopPolling = setVisibleInterval(load, 5000);
return () => {
alive = false;
clearInterval(t);
stopPolling();
};
}, []);

Expand Down
5 changes: 3 additions & 2 deletions web/src/app/(main)/function/tasks/detail/_tabs/assets-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { parseCompanyScopeText } from "@/lib/company-scope";
import { taskAssetSourceLabel } from "@/lib/task-assets";
import type { Asset, NewAssetType } from "@/lib/types";
import { cn } from "@/lib/utils";
import { setVisibleInterval } from "@/lib/polling";

const PAGE_SIZES = [25, 50, 100, 200];

Expand Down Expand Up @@ -415,11 +416,11 @@ export function AssetsTab({ taskId }: { taskId: string }) {
};
// Debounce DSL keystrokes; plain (re)loads and polling run immediately.
const debounce = setTimeout(() => void load(), dsl ? 400 : 0);
const timer = setInterval(() => void load(), 10_000);
const stopPolling = setVisibleInterval(() => void load(), 10_000);
return () => {
active = false;
clearTimeout(debounce);
clearInterval(timer);
stopPolling();
};
}, [page, query, refreshKey, size, tab, taskId]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { type Tone, toneClasses, toneDot } from "@/lib/status";
import { taskAssetTypeLabel } from "@/lib/task-assets";
import type { Edge, ExploreKind, FindingAsset, NewAssetType, TaskNode } from "@/lib/types";
import { cn } from "@/lib/utils";
import { setVisibleInterval } from "@/lib/polling";

const PAGE_SIZES = [20, 50, 100];
const POLL_MS = 8000;
Expand Down Expand Up @@ -519,10 +520,10 @@ export function BroadcastTab({ taskId }: { taskId: string }) {
alive = false;
};
}
const timer = setInterval(load, POLL_MS);
const stopPolling = setVisibleInterval(load, POLL_MS);
return () => {
alive = false;
clearInterval(timer);
stopPolling();
};
}, [taskId, page, size, kinds, query, order, live, atLive]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Input } from "@/components/ui/input";
import { Skeleton } from "@/components/ui/skeleton";
import { api } from "@/lib/api";
import type { TaskCredential } from "@/lib/types";
import { setVisibleInterval } from "@/lib/polling";

// Credentials tab (#3): the task's test accounts — user-declared (program alias
// accounts) and agent-recorded (accounts the workers registered themselves).
Expand All @@ -38,8 +39,8 @@ export function CredentialsTab({ taskId }: { taskId: string }) {
React.useEffect(() => {
load();
// Agent runs can record credentials mid-task; a light poll keeps the tab current.
const timer = setInterval(load, 15000);
return () => clearInterval(timer);
const stopPolling = setVisibleInterval(load, 15000);
return stopPolling;
}, [load]);

async function add() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useStoredSortPreference } from "@/lib/sort-preference";
import { statusMeta } from "@/lib/status";
import type { Finding, FindingStatus } from "@/lib/types";
import { cn } from "@/lib/utils";
import { setVisibleInterval } from "@/lib/polling";

type FindingSortField = "time";

Expand Down Expand Up @@ -159,10 +160,10 @@ export function FindingsTab({ taskId }: { taskId: string }) {
});
};
load();
const t = setInterval(load, 3000);
const stopPolling = setVisibleInterval(load, 3000);
return () => {
active = false;
clearInterval(t);
stopPolling();
};
}, [taskId]);

Expand Down
5 changes: 3 additions & 2 deletions web/src/app/(main)/function/tasks/detail/_tabs/graph-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Card, CardContent } from "@/components/ui/card";
import { ExplorationGraph } from "@/components/exploration-graph";
import { api } from "@/lib/api";
import type { Edge, TaskNode } from "@/lib/types";
import { setVisibleInterval } from "@/lib/polling";

export function GraphTab({ taskId }: { taskId: string }) {
const [nodes, setNodes] = React.useState<TaskNode[]>([]);
Expand Down Expand Up @@ -38,10 +39,10 @@ export function GraphTab({ taskId }: { taskId: string }) {
});
};
load();
const timer = setInterval(load, 20000);
const stopPolling = setVisibleInterval(load, 20000);
return () => {
cancelled = true;
clearInterval(timer);
stopPolling();
};
}, [taskId]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import type {
TaskNode,
TaskScopeRow,
} from "@/lib/types";
import { setVisibleInterval } from "@/lib/polling";

// Compact token count formatting (12345 → 12.3k, 2000000 → 2M).
function fmtTokens(n: number): string {
Expand Down Expand Up @@ -402,15 +403,15 @@ export function OverviewTab({ taskId }: { taskId: string }) {
void loadTokens();
void loadGoals();
void loadConstraints();
const timer = setInterval(() => {
const stopPolling = setVisibleInterval(() => {
void load();
void loadTokens();
void loadGoals();
void loadConstraints();
}, 3000);
return () => {
cancelled = true;
clearInterval(timer);
stopPolling();
};
}, [taskId, loadScope, loadTokens, loadGoals, loadConstraints]);

Expand Down
Loading
Loading