Files
PaperClipAI/ui/src/lib/polling.test.ts
T
DottaandPaperclip 176645187c Fix request storm polling and issue-list coalescing (#9190)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The board UI keeps issue, agent, activity, and run state fresh
through polling across several pages and sidebar surfaces
> - When multiple components or browser tabs poll the same company data
at the same time, the API can receive bursts of duplicate issue-list
requests
> - Those duplicate requests increase database and server load without
returning meaningfully different data
> - This pull request adds server-side compression/coalescing plus
client-side visibility-aware and cross-tab shared polling
> - The benefit is lower request volume during normal board usage while
preserving fresh UI data for active users

## Linked Issues or Issue Description

No exact public GitHub issue was found for this request.

Problem:
- The board can issue redundant polling requests for the same issue-list
data from multiple UI surfaces and tabs.
- In busy operator sessions, those bursts can trigger request-storm
behavior and unnecessary issue-list load.
- Expected behavior is to reuse identical in-flight work server-side and
reduce hidden-tab or duplicate-tab polling client-side while preserving
normal refresh behavior.

Related public context found during duplicate search:
- #8206 covers a different board UI 404-storm scope.
- #5165 covers separate issue-list behavior around page-size truncation.

## What Changed

- Added API compression middleware foundation and a company/created-at
index for heartbeat run access.
- Added server-side issue-list request storm detection and identical
in-flight request coalescing.
- Added UI fetch metadata, visibility-aware polling, and request
deduplication for issue/activity/client calls.
- Added cross-tab shared polling primitives and wired them into the
sidebar, inbox, dashboard, issue, project, routine, and agent surfaces.
- Resolved the latest `master` migration collision by keeping upstream
`0140_built_in_managed_resources.sql` and renumbering this branch's
heartbeat-run index migration to
`0141_heartbeat_runs_company_created_at_index.sql`; the SQL uses `CREATE
INDEX IF NOT EXISTS` for idempotency.
- Stabilized server heartbeat cleanup tests exposed by the PR check
matrix.
- Fixed the Greptile compression follow-up by weakening strong ETags on
encoded JSON responses and bypassing compression for streamed/download
responses.

## Verification

- `pnpm exec vitest run ui/src/components/IssuesList.test.tsx
ui/src/pages/Inbox.test.tsx` — passed after resolving the latest
`master` conflict in `IssuesList.tsx` and updating the 200-result cap
expectations.
- `pnpm check:token-gates` — passed after the UI conflict resolution.
- `jq -e '.entries | length as $n | (map(.idx) | unique | length == $n)
and (map(.tag) | unique | length == $n)'
packages/db/src/migrations/meta/_journal.json` — passed after
renumbering the migration to `0141`.
- `pnpm exec vitest run server/src/__tests__/api-compression.test.ts` -
passed after the compression follow-up.
- `pnpm exec vitest run
server/src/__tests__/issue-list-assignee-filter-routes.test.ts` - passed
after the compression follow-up.
- `pnpm --filter @paperclipai/server typecheck` - passed after the
compression follow-up.
- Greptile Review for head `8dbddac41ec273fda404100b4981ddb912fad57b` -
passed after the latest conflict/migration fix; all Greptile review
threads are resolved.
- `pnpm exec vitest run server/src/__tests__/api-compression.test.ts
server/src/__tests__/issue-list-assignee-filter-routes.test.ts
ui/src/api/client.test.ts ui/src/api/issues.test.ts
ui/src/lib/polling.test.ts ui/src/lib/cross-tab-poll.test.ts
ui/src/pages/Inbox.test.tsx ui/src/components/SidebarProjects.test.tsx
ui/src/components/SidebarAccountMenu.test.tsx
ui/src/lib/issueDetailCache.test.ts` — passed.
- `pnpm exec vitest run ui/src/api/client.test.ts` — passed.
- `pnpm exec vitest run ui/src/pages/Inbox.test.tsx
ui/src/components/SidebarProjects.test.tsx
ui/src/components/SidebarAccountMenu.test.tsx
ui/src/lib/issueDetailCache.test.ts` — passed.
- `pnpm exec vitest run
server/src/__tests__/heartbeat-worktree-suppression.test.ts` — passed.
- `pnpm exec vitest run
server/src/__tests__/low-trust-red-team-routes.test.ts` — passed.
- `pnpm exec vitest run
server/src/__tests__/heartbeat-workspace-finalize-branch.test.ts` —
passed.
- `pnpm --filter @paperclipai/ui typecheck` — passed.
- `pnpm --filter @paperclipai/server typecheck` — passed.
- GitHub PR checks for head `8dbddac41ec273fda404100b4981ddb912fad57b`:
all GitHub Actions/status checks passed; Greptile, Superagent, Socket,
Snyk, build, typecheck/release registry, general tests, serialized
server suites, e2e, canary dry run, policy, and commitperclip review are
green; Storybook visual regression and security-review were
skipped/neutral by policy.
- Confirmed this branch does not include `pnpm-lock.yaml` or
`.github/workflows` changes.

## Risks

- Medium risk: issue-list coalescing changes request timing and cache
semantics for a hot API path.
- Medium risk: cross-tab polling uses browser coordination primitives,
so older or unusual browser environments need fallback behavior to stay
correct.
- Low migration risk: the new index migration is ordered after current
`master` and uses `CREATE INDEX IF NOT EXISTS`.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

OpenAI Codex coding agent, GPT-5-based model, tool-enabled with
shell/git execution. Exact hosted deployment identifier and
context-window size were not surfaced in the agent runtime.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-07-09 17:38:34 -05:00

105 lines
3.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
BACKGROUND_JITTER_MAX_MS,
FOCUSED_JITTER_MAX_MS,
computeStartupJitterMs,
jitterBoundForVisibility,
resolvePollingInterval,
} from "./polling";
describe("resolvePollingInterval", () => {
it("polls at the visible cadence when focused", () => {
expect(
resolvePollingInterval({ visible: true, focused: true }, { visibleMs: 5000 }),
).toBe(5000);
});
it("falls back to visibleMs when visible but unfocused and no unfocusedMs given", () => {
expect(
resolvePollingInterval({ visible: true, focused: false }, { visibleMs: 5000 }),
).toBe(5000);
});
it("uses unfocusedMs for a visible-but-unfocused tab when provided", () => {
expect(
resolvePollingInterval(
{ visible: true, focused: false },
{ visibleMs: 5000, unfocusedMs: 15000 },
),
).toBe(15000);
});
it("stops polling when hidden by default", () => {
expect(
resolvePollingInterval({ visible: false, focused: false }, { visibleMs: 5000 }),
).toBe(false);
});
it("slows polling when hidden if hiddenMs is provided", () => {
expect(
resolvePollingInterval(
{ visible: false, focused: false },
{ visibleMs: 5000, hiddenMs: 60000 },
),
).toBe(60000);
});
});
describe("computeStartupJitterMs", () => {
it("returns 0 when maxMs <= 0", () => {
expect(computeStartupJitterMs(0, () => 0.5)).toBe(0);
expect(computeStartupJitterMs(-100, () => 0.5)).toBe(0);
});
it("scales the rng value into [0, maxMs]", () => {
expect(computeStartupJitterMs(1000, () => 0)).toBe(0);
expect(computeStartupJitterMs(1000, () => 0.25)).toBe(250);
expect(computeStartupJitterMs(1000, () => 0.999999)).toBe(1000);
});
it("clamps rng values outside [0, 1)", () => {
expect(computeStartupJitterMs(1000, () => -1)).toBe(0);
expect(computeStartupJitterMs(1000, () => 5)).toBe(1000);
});
it("never exceeds the bound for a focused tab", () => {
for (const r of [0, 0.3, 0.7, 0.9999]) {
const jitter = computeStartupJitterMs(FOCUSED_JITTER_MAX_MS, () => r);
expect(jitter).toBeGreaterThanOrEqual(0);
expect(jitter).toBeLessThanOrEqual(FOCUSED_JITTER_MAX_MS);
}
});
});
describe("jitterBoundForVisibility", () => {
it("uses the small focused bound for a focused tab", () => {
expect(jitterBoundForVisibility({ visible: true, focused: true })).toBe(
FOCUSED_JITTER_MAX_MS,
);
});
it("uses the wider background bound for a hidden or unfocused tab", () => {
expect(jitterBoundForVisibility({ visible: false, focused: false })).toBe(
BACKGROUND_JITTER_MAX_MS,
);
expect(jitterBoundForVisibility({ visible: true, focused: false })).toBe(
BACKGROUND_JITTER_MAX_MS,
);
});
it("respects custom bounds", () => {
expect(
jitterBoundForVisibility(
{ visible: true, focused: true },
{ focusedMaxMs: 100, backgroundMaxMs: 9000 },
),
).toBe(100);
expect(
jitterBoundForVisibility(
{ visible: false, focused: false },
{ focusedMaxMs: 100, backgroundMaxMs: 9000 },
),
).toBe(9000);
});
});