mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-25 16:14:26 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Pull-request CI checks run on every commit before maintainers can merge code > - PR #22679 cut PR CI from 15–30+ minutes down toward 4.5–5.5 minutes on 4-core runners > - Remaining wall-clock bottlenecks sit in unsharded `workspaces-b`, serial typecheck preparation, E2E server/UI startup, fixed Vitest worker caps, and remaining soak and duplicated UI tests > - This pull request adds Phase 2 optimizations across server soak triage, typecheck DAG overlap, E2E fast boot and shard balance, dynamic CPU-aware workers, pure-Node workspace link preflight, UI pruning, and permanent test-budget enforcement > - The benefit is ordinary feature PRs finish in ~3.1–3.8 minutes on 4-core runners and scale automatically to ~2.0–3.0 minutes on 8-core runners while keeping core invariants and nightly coverage intact ## Linked Issues or Issue Description Refs #22679 **Problem Statement** After #22679 landed, several PR lanes still took 4.5–6 minutes on 4-core runners (`general-workspaces-b` ran unsharded, `Typecheck + Release Registry` waited on a serial server build before starting UI and test-program checks, E2E shards paid full `cli onboard` + minified UI build overhead, and Vitest worker counts did not scale with runner CPU count). **Proposed Solution** Split `general-workspaces-b` across two duration-balanced shards, overlap typecheck preparation tasks behind a shared runner-vendor node, replace E2E `onboard` with a direct template-PG boot + unminified inline UI build, scale Vitest/Playwright/Typecheck workers dynamically by CPU and RAM, replace `tsx` in workspace-link preflight with pure Node, prune redundant mocked UI tests and additional real-clock server soak suites (still run on `master`, `release-verify`, `cloud-readiness`, and `PAPERCLIP_CI_FULL=1`), and add a permanent test-budget policy check. **Expected Benefit** Brings ordinary PR CI wall-clock down to ~3.1–3.8 minutes on 4-core runners (and ~2.0–3.0 minutes on 8-core runners) with automated guardrails against future test-time regressions. ## What Changed - **Server soak triage & permanent test-budget guardrail (`scripts/ci/nightly-only-suites.json`, `rules/test-budget.mdc`, `.greptile/files.json`, `scripts/check-test-budget.mjs`, `scripts/check-test-budget.test.mjs`, `.github/workflows/pr-trusted.yml`):** Expanded the PR-excluded soak/heavy-integration manifest to 568 genuine heavy suites (preserving 100% of company isolation, authz, checkout, approval, budget, activity-log, security, telemetry, and serialized suites on every PR) and wired `node scripts/check-test-budget.mjs` into the `policy` job. - **Typecheck DAG overlap & CPU/RAM scaling (`scripts/ci-typecheck-lane.mjs`, `scripts/typecheck-test-programs.mjs`, `scripts/__tests__/ci-typecheck-lane.test.mjs`, `scripts/typecheck-test-programs.test.mjs`):** Moved `build server without type checks` and `check server runtime assets` out of serial `prepare` into parallel DAG nodes behind `prepare runner vendor`, added `computeResourceProfile` scaling across 2/4/8-core runners, and added missing UI/Runner consumer routing inputs (`packages/paperclip-runner/` for `@paperclipai/ui`, `packages/adapter-utils/` for Runner test programs). - **E2E fast boot, dynamic workers & 8-shard rebalance (`tests/e2e/start-e2e-server.ts`, `tests/e2e/playwright.config.ts`, `scripts/e2e-shard.mjs`, `scripts/e2e-shard-durations.json`, `.github/workflows/pr-trusted.yml`, `.github/workflows/release-verify.yml`):** Added direct throwaway template-PG boot (`fsync=off`), unminified inline Vite fallback build, dynamic parallel workers (`min(4, floor(cpus/2))`), empirical `1.5x` parallel speedup weighting across 8 shards, targeted trims to slowest E2E specs, and hashed workspace `package.json` manifests into the `e2e-ui-dist-v2` cache key. - **Dynamic Vitest workers, 2-shard `workspaces-b`, and pure-Node preflight (`scripts/ci/vitest-workers.mjs`, `scripts/ci/workspaces-b-shard.mjs`, `scripts/ensure-workspace-package-links.mjs`, `scripts/ci-dependency-cache.mjs`, `packages/db/src/test-embedded-postgres.ts`, `.github/ci-task-matrix.json`, `.github/actions/setup-ci-dependencies/action.yml`, `package.json`):** Scaled Vitest workers dynamically (`4→7` general, `3→5` serialized) with `max_connections=300` before PostgreSQL start, split `general-workspaces-b` into 2 duration-weighted shards (backward-compatible unsharded for release workflows), replaced `tsx` workspace-link preflight with pure Node (~29× faster), and parallelized `ci-dependency-cache.mjs` child queries. - **UI Vitest worker scaling, fake timers & E2E-duplicated suite pruning (`ui/vitest.config.ts`, 4 `maps/` files, and UI test suites):** Scaled UI `maxWorkers` dynamically, converted real-clock sleeps in kept suites to fake timers, removed 34 E2E-duplicated mocked jsdom suites, and updated corresponding `maps/` prose references. ## Verification - `node --test .github/scripts/tests/ci-plan.test.mjs .github/scripts/tests/ci-dependency-cache.test.mjs .github/scripts/tests/pr-dependency-cache.test.mjs scripts/__tests__/run-vitest-stable-shard.test.mjs scripts/__tests__/run-vitest-stable-pr-mode.test.mjs scripts/__tests__/ci-typecheck-lane.test.mjs scripts/typecheck-test-programs.test.mjs scripts/__tests__/e2e-shard.test.mjs scripts/check-test-budget.test.mjs scripts/ci/test-selection-pr-timings.test.mjs`: **160/160 PASS** - `node scripts/check-test-budget.mjs`: **PASS** (`568` nightly suites validated) - `node scripts/check-maps.mjs`: **PASS** (`35 areas, 192 capability IDs`) - `pnpm --filter @paperclipai/ui typecheck` & `pnpm --filter @paperclipai/db typecheck`: **PASS** - Live CI verification runs on the new `pr-trusted.yml@master` (from #22679) with `profile-ci` enabled to export the full job/step/file waterfall to Honeycomb `paperclip-ci`. ## Risks - Medium CI scope: touches workflow matrix partitioning, E2E startup, and test runners; does not change production app runtime behavior. - All PR-demoted soak suites continue to execute on `master`, `release-verify`, `cloud-readiness`, local `pnpm test:run`, and `PAPERCLIP_CI_FULL=1`. - All new `policy` and `setup-ci-dependencies` commands keep file-existence guards so reruns of older PR branches continue to work. ## Model Used - Google Gemini (Antigravity swarm orchestrating specialized worker agents with shell, file editing, and test execution tools) and Anthropic Claude Opus (verification and optimization sub-agents). ## 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 - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge
213 lines
8.5 KiB
JavaScript
213 lines
8.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import test from "node:test";
|
|
import { fileURLToPath } from "node:url";
|
|
import {
|
|
checkTestFileBudget,
|
|
findUnbudgetedSleeps,
|
|
runTestBudgetCheck,
|
|
validateNightlyManifest,
|
|
} from "./check-test-budget.mjs";
|
|
|
|
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
|
|
test("checked-in scripts/ci/nightly-only-suites.json passes full manifest & invariant validation", () => {
|
|
const result = runTestBudgetCheck({ manifestOnly: true });
|
|
assert.deepEqual(result.violations, []);
|
|
assert.ok(result.nightlyCount > 0, "expected non-empty nightly-only-suites.json");
|
|
});
|
|
|
|
test("findUnbudgetedSleeps flags real-clock sleeps >1000ms (including inline callbacks and products) and respects allow-sleep annotations", () => {
|
|
const source = [
|
|
"await delay(500);",
|
|
"await delay(1000);",
|
|
"await delay(1_500);",
|
|
"await setTimeout(resolve, 2500);",
|
|
"await new Promise((resolve) => setTimeout(() => resolve(), 2500));",
|
|
"await new Promise((resolve) => setTimeout(() => { resolve(); }, 60 * 1000));",
|
|
"await delay(2 * 1000);",
|
|
"await page.waitForTimeout(3000);",
|
|
"await delay(5000); // test-budget:allow-sleep: socket idle check",
|
|
"// await delay(9000);",
|
|
].join("\n");
|
|
|
|
const violations = findUnbudgetedSleeps(source);
|
|
assert.deepEqual(
|
|
violations.map((v) => ({ rule: v.rule, line: v.line })),
|
|
[
|
|
{ rule: "sleep-over-1s", line: 3 },
|
|
{ rule: "sleep-over-1s", line: 4 },
|
|
{ rule: "sleep-over-1s", line: 5 },
|
|
{ rule: "sleep-over-1s", line: 6 },
|
|
{ rule: "sleep-over-1s", line: 7 },
|
|
{ rule: "sleep-over-1s", line: 8 },
|
|
],
|
|
);
|
|
|
|
// Cancellable deadline timers and full fake-timer scopes must not produce false positives.
|
|
assert.deepEqual(
|
|
findUnbudgetedSleeps(
|
|
"const timer = setTimeout(() => reject(new Error('timeout')), 60_000);\nclearTimeout(timer);",
|
|
),
|
|
[],
|
|
);
|
|
assert.deepEqual(
|
|
findUnbudgetedSleeps("vi.useFakeTimers();\nsetTimeout(() => done(), 10_000);"),
|
|
[],
|
|
);
|
|
assert.deepEqual(
|
|
findUnbudgetedSleeps(
|
|
'beforeEach(() => { vi.useFakeTimers(); });\nafterEach(() => { vi.useRealTimers(); });\nit("faked", () => {\n setTimeout(() => done(), 5000);\n});',
|
|
),
|
|
[],
|
|
);
|
|
assert.deepEqual(
|
|
findUnbudgetedSleeps(
|
|
'vi.useFakeTimers({ toFake: ["Date", "setTimeout"] });\nsetTimeout(() => done(), 2500);',
|
|
),
|
|
[],
|
|
);
|
|
|
|
// Partial fake timers (e.g. Date-only) and real-clock tests in mixed-clock files MUST still be flagged.
|
|
assert.deepEqual(
|
|
findUnbudgetedSleeps(
|
|
'vi.useFakeTimers({ toFake: ["Date"] });\nawait new Promise((resolve) => setTimeout(resolve, 2500));',
|
|
).map((v) => ({ rule: v.rule, line: v.line })),
|
|
[{ rule: "sleep-over-1s", line: 2 }],
|
|
);
|
|
assert.deepEqual(
|
|
findUnbudgetedSleeps(
|
|
'vi.useFakeTimers({ doNotFake: ["setTimeout"] });\nawait new Promise((resolve) => setTimeout(resolve, 2500));',
|
|
).map((v) => ({ rule: v.rule, line: v.line })),
|
|
[{ rule: "sleep-over-1s", line: 2 }],
|
|
);
|
|
assert.deepEqual(
|
|
findUnbudgetedSleeps(
|
|
[
|
|
'it("faked", () => {',
|
|
" vi.useFakeTimers();",
|
|
" setTimeout(() => done(), 5000);",
|
|
"});",
|
|
'it("real", async () => {',
|
|
" await new Promise((resolve) => setTimeout(resolve, 2500));",
|
|
"});",
|
|
].join("\n"),
|
|
).map((v) => ({ rule: v.rule, line: v.line })),
|
|
[{ rule: "sleep-over-1s", line: 6 }],
|
|
);
|
|
assert.deepEqual(
|
|
findUnbudgetedSleeps(
|
|
[
|
|
'it("restores", async () => {',
|
|
" vi.useFakeTimers();",
|
|
" setTimeout(() => done(), 5000);",
|
|
" vi.useRealTimers();",
|
|
" await new Promise((resolve) => setTimeout(resolve, 2500));",
|
|
"});",
|
|
].join("\n"),
|
|
).map((v) => ({ rule: v.rule, line: v.line })),
|
|
[{ rule: "sleep-over-1s", line: 5 }],
|
|
);
|
|
});
|
|
|
|
test("checkTestFileBudget enforces migration, prose-guidance, and tiny embedded-PG rules on new files", () => {
|
|
const mig = checkTestFileBudget(
|
|
"packages/db/src/old-backfill-migration.test.ts",
|
|
'const sql = readFileSync("./migrations/0420_backfill.sql", "utf8");',
|
|
{ isNew: true },
|
|
);
|
|
assert.deepEqual(mig.map((v) => v.rule), ["historical-migration-test"]);
|
|
|
|
const guidance = checkTestFileBudget(
|
|
"server/src/__tests__/foo-guidance.test.ts",
|
|
'const text = readFileSync("ui/src/App.tsx", "utf8"); expect(text).toContain("Settings");',
|
|
{ isNew: true },
|
|
);
|
|
assert.deepEqual(guidance.map((v) => v.rule), ["prose-guidance-grep"]);
|
|
|
|
const nestedGuidance = checkTestFileBudget(
|
|
"server/src/__tests__/bar-guidance.test.ts",
|
|
'const text = readFileSync(path.join(dir, "README.md"), "utf8"); expect(text).toContain("Usage");',
|
|
{ isNew: true },
|
|
);
|
|
assert.deepEqual(nestedGuidance.map((v) => v.rule), ["prose-guidance-grep"]);
|
|
|
|
const tinyPg = checkTestFileBudget(
|
|
"server/src/__tests__/tiny-one-off.test.ts",
|
|
'import { startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";\nit("works", async () => {});',
|
|
{ isNew: true },
|
|
);
|
|
assert.deepEqual(tinyPg.map((v) => v.rule), ["tiny-standalone-db-test"]);
|
|
|
|
const tinyPgAllowed = checkTestFileBudget(
|
|
"server/src/__tests__/tiny-one-off.test.ts",
|
|
'// test-budget:allow-standalone-db: needs isolated process env\nimport { startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";',
|
|
{ isNew: true },
|
|
);
|
|
assert.deepEqual(tinyPgAllowed, []);
|
|
|
|
// Nightly-registered suites are exempt from PR sleep & tiny-DB limits.
|
|
const nightly = checkTestFileBudget(
|
|
"server/src/__tests__/soak-example.test.ts",
|
|
'import { startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";\nawait delay(5000);',
|
|
{ isNew: true, nightlyPaths: new Set(["server/src/__tests__/soak-example.test.ts"]) },
|
|
);
|
|
assert.deepEqual(nightly, []);
|
|
});
|
|
|
|
test("validateNightlyManifest rejects missing files, duplicates, and protected core invariant suites", () => {
|
|
const tmp = mkdtempSync(path.join(os.tmpdir(), "paperclip-test-budget-"));
|
|
try {
|
|
mkdirSync(path.join(tmp, "scripts/ci"), { recursive: true });
|
|
mkdirSync(path.join(tmp, "server/src/__tests__"), { recursive: true });
|
|
mkdirSync(path.join(tmp, "server/src/services/connectors"), { recursive: true });
|
|
writeFileSync(path.join(tmp, "server/src/__tests__/authz-company-access.test.ts"), "// authz\n");
|
|
writeFileSync(path.join(tmp, "server/src/services/connectors/slack-access.test.ts"), "// access\n");
|
|
writeFileSync(path.join(tmp, "server/src/services/budget-resume-continuation.test.ts"), "// budget\n");
|
|
writeFileSync(path.join(tmp, "server/src/__tests__/webhook-ingress.test.ts"), "// webhook\n");
|
|
writeFileSync(path.join(tmp, "server/src/__tests__/storage-s3-credential-rotation.test.ts"), "// cred\n");
|
|
writeFileSync(path.join(tmp, "server/src/__tests__/connection-management.integration.test.ts"), "// isolation\n");
|
|
writeFileSync(path.join(tmp, "server/src/__tests__/real-soak.test.ts"), "// soak\n");
|
|
writeFileSync(
|
|
path.join(tmp, "scripts/ci/nightly-only-suites.json"),
|
|
JSON.stringify({
|
|
suites: [
|
|
{ path: "server/src/__tests__/real-soak.test.ts" },
|
|
{ path: "server/src/__tests__/real-soak.test.ts" },
|
|
{ path: "server/src/__tests__/missing-suite.test.ts" },
|
|
{ path: "server/src/__tests__/authz-company-access.test.ts" },
|
|
{ path: "server/src/services/connectors/slack-access.test.ts" },
|
|
{ path: "server/src/services/budget-resume-continuation.test.ts" },
|
|
{ path: "server/src/__tests__/webhook-ingress.test.ts" },
|
|
{ path: "server/src/__tests__/storage-s3-credential-rotation.test.ts" },
|
|
{ path: "server/src/__tests__/connection-management.integration.test.ts" },
|
|
],
|
|
}),
|
|
);
|
|
|
|
const { violations } = validateNightlyManifest(tmp);
|
|
assert.deepEqual(
|
|
violations.map((v) => v.rule),
|
|
[
|
|
"nightly-manifest",
|
|
"nightly-manifest",
|
|
"protected-invariant",
|
|
"protected-invariant",
|
|
"protected-invariant",
|
|
"protected-invariant",
|
|
"protected-invariant",
|
|
"protected-invariant",
|
|
],
|
|
);
|
|
} finally {
|
|
rmSync(tmp, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("pr-trusted.yml invokes check-test-budget.mjs with --base on pull requests", () => {
|
|
const workflow = readFileSync(path.join(repoRoot, ".github/workflows/pr-trusted.yml"), "utf8");
|
|
assert.match(workflow, /node \.\/scripts\/check-test-budget\.mjs --base "\$base_sha"/);
|
|
});
|