mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 10:07:24 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - `server/src/__tests__/chat-channels.integration.test.ts` had grown into an 86,498-LOC single-file monolith (~1,815s total CPU across 5 serialized line-sharded PR runners) that forced special-case line sharding in `scripts/run-vitest-stable.mjs`, prevented `general-chat` from participating in file-level test selection, and pinned 5 PR CI shards even for single-provider chat edits. > - This pull request splits the monolith into 16 provider- and domain-scoped integration suites plus 4 `<1,000`-LOC shared harness helpers (`harness.ts`, `providers.ts`, `github-harness.ts`, `retry-fixtures.ts`), optimizes redundant setup/polling overhead (`-41.8%` net LOC, `-70.9%` CPU-min: `1,815.3s -> 528.4s` with every original assertion and matrix case preserved), switches `general-chat` to file-level duration-balanced LPT sharding (`265.1s / 263.3s` across 2 shards), wires `general-chat` into `test-selection` and `local-verify`, and shrinks the PR matrix from 5 chat shards to 2 (`general_tests`: `30 -> 27`). > - Every provider (GitHub, Discord, Slack, Teams, Telegram) and shared core behavior retains full assertion coverage while ordinary chat edits now select only affected provider suites instead of all 5 shards. ## Linked Issues or Issue Description Refs #22804 **What existing behavior does this improve?** Test execution time, maintainability, and incremental test selection for `server/src/__tests__/chat-channels/**`, `scripts/run-vitest-stable.mjs`, and `.github/workflows/pr-trusted.yml`. **Subsystem affected** server/ — chat-channels integration tests & CI test runner/matrix **Current behavior** `server/src/__tests__/chat-channels.integration.test.ts` is an 86,498-LOC monolith requiring custom source-line sharding across 5 PR CI jobs (`chat (1/5)`..`chat (5/5)`) and bypassed by file-level test selection. **Proposed behavior** Replace the monolith with 16 focused provider/domain suites + 4 shared harness modules, use standard file-level duration-balanced LPT sharding with `scripts/chat-shard-durations.json`, include `general-chat` in incremental test selection (`select-tests.mjs`, `test-selection-plan.mjs`, `local-verify/plan.mjs`), and run `general-chat` on 2 PR shards (`chat (1/2)`, `chat (2/2)`). ## What Changed - **Split & optimized `chat-channels.integration.test.ts` (`86,498 LOC` → 16 provider/domain suites + 4 `<1,000`-LOC harness modules, `-37,314` net LOC / `-70.9%` CPU-min):** - Shared harnesses: `harness.ts` (957 LOC), `providers.ts` (857 LOC), `github-harness.ts` (817 LOC), `retry-fixtures.ts` (905 LOC). - 16 integration suites covering Board/Core (1), Discord (2), GitHub (4), Slack (4), Teams (2), and Telegram (3), totaling `528.4s` CPU (down from `1,815.3s`), preserving all 20 stopped-workspace-cleanup and 28 response-maintenance GitHub matrix cases. - **File-level LPT sharding & test selection for `general-chat`:** - Updated `scripts/run-vitest-stable.mjs` (947 LOC), `scripts/chat-shard-durations.json`, `scripts/ci/refresh-chat-shard-durations.mjs`, `scripts/ci/select-tests.mjs`, `scripts/ci/test-selection-manifest.mjs`, `scripts/ci/test-selection-plan.mjs`, `scripts/local-verify/plan.mjs`, and `scripts/general-server-shard-durations.json`. - Updated `maps/agent-access-map/chat-channels.md`, `maps/feature-map/{chat-channels,connection-setup,recovery}.md`, `server/scripts/benchmark-response-presentation.mjs` (including `../../services/native-runtime/...` relative baseline import), and `server/scripts/test-slack-sync-protocol.mjs`. - **Shrank PR `general-chat` matrix from 5 shards to 2 (`general_tests`: `30 -> 27`):** - Updated `.github/ci-task-matrix.json`, `.github/workflows/pr-trusted.yml` fallback matrix, and `.github/scripts/tests/ci-plan.test.mjs` (`30 -> 27`). - Kept `release-verify.yml` at 3 chat shards (LPT-balanced). ## Verification - `node --test .github/scripts/tests/ci-plan.test.mjs scripts/__tests__/run-vitest-stable-shard.test.mjs scripts/__tests__/run-vitest-stable-pr-mode.test.mjs scripts/__tests__/local-verify.test.mjs scripts/__tests__/release-verify-workflow.test.mjs scripts/ci/test-selection*.test.mjs` — **197/197 PASS** - `node scripts/check-maps.mjs` — **PASS** (35 areas, 192 capability IDs aligned) - `node scripts/check-test-budget.mjs --base origin/master` — **PASS** (105 nightly suites validated, 17 test files checked) - `node server/scripts/typecheck-layers.mjs` — **0 errors** across all 7 layers - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/chat-channels/github-review-workflow.integration.test.ts` — **89/89 PASS** (`171.27s`) - Live shard execution (`node scripts/run-vitest-stable.mjs --mode general --group general-chat --shard-index 0 --shard-count 2`) — **8/8 files, 455/455 tests PASS in 104.0s** ## Risks - Low risk: all 16 split suites preserve full PostgreSQL-backed integration assertions across every chat provider, `general-chat` file sharding gracefully handles arbitrary shard counts (including `release-verify.yml`'s 3 shards and over-sharded empty shards), and full CI validates all chat suites live before merge. ## Model Used - Google Gemini (Antigravity agent swarm) with extended reasoning, tool use, and local test execution (`ui-prune-2`, `vitest-runner`, `verifier`, `ci-pipeline`). ## 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
80 lines
5.5 KiB
JavaScript
80 lines
5.5 KiB
JavaScript
import { execFileSync } from "node:child_process";
|
|
import { readFileSync } from "node:fs";
|
|
import path from "node:path";
|
|
import { planTasks } from "../../.github/scripts/ci-plan.mjs";
|
|
import { productInputs } from "../../.github/scripts/ci-inputs.mjs";
|
|
import { selectionEligibility } from "../ci/select-tests.mjs";
|
|
|
|
function inventory(root, env, group) {
|
|
const args = group === "serialized" ? ["--mode", "serialized"] : ["--mode", "general", "--group", group];
|
|
const shards = ["general-workspaces-b", "general-workspaces-c", "general-chat"].includes(group) ? []
|
|
: ["--shard-index", "0", "--shard-count", "1"];
|
|
return JSON.parse(execFileSync(process.execPath, [path.join(root, "scripts/run-vitest-stable.mjs"), ...args,
|
|
...shards, "--dry-run"], {
|
|
cwd: root, env: { ...env, PAPERCLIP_CI_FULL: "1" }, encoding: "utf8", maxBuffer: 16 * 1024 * 1024,
|
|
}));
|
|
}
|
|
|
|
export async function planVerification({ root, env, diff, full = false }) {
|
|
const config = JSON.parse(readFileSync(path.join(root, "turbo.json"), "utf8"));
|
|
const lanes = JSON.parse(readFileSync(path.join(root, ".github/ci-task-matrix.json"), "utf8"));
|
|
const coarse = planTasks({ config, lanes, paths: diff.changed });
|
|
const requiredCI = diff.changed.length ? Object.entries(coarse.jobs).filter(([, job]) => job.run).map(([name]) => name) : [];
|
|
if (full) return { kind: "full", jobs: [{ id: "full", args: null, cacheable: false }], requiredCI, selectedFiles: null, reason: "Full Vitest suite explicitly requested; opaque integration coverage runs fresh" };
|
|
if (!productInputs(diff.changed).length) return { kind: "selected", jobs: [], requiredCI, selectedFiles: [], reason: "No changed product inputs" };
|
|
const reason = coarse.forceReason ?? selectionEligibility(coarse, root);
|
|
if (reason) return { kind: "broad", reason, jobs: [], requiredCI };
|
|
try {
|
|
const groups = [...new Set(["general_tests", "verify_serialized_server"].flatMap(job =>
|
|
coarse.jobs[job]?.run ? coarse.jobs[job].matrix.include.map(entry => entry.group ?? "serialized") : []))];
|
|
const inventories = new Map(groups.map(group => [group, inventory(root, env, group)]));
|
|
const projects = [...new Set([...inventories.values()].flatMap(value => value.workspaceProjects ?? ["@paperclipai/server"]))];
|
|
const { selectProjects } = await import("../ci/select-projects.mjs");
|
|
const graphs = await selectProjects({ root, projects, changed: productInputs(diff.changed), concurrency: 1, env });
|
|
if (graphs.some(graph => !graph.isolated)) throw new Error("Non-isolated projects require broad verification");
|
|
const covered = new Set(graphs.flatMap(graph => graph.coveredChanges));
|
|
const uncovered = productInputs(diff.changed).filter(file => !covered.has(file));
|
|
if (uncovered.length) throw new Error(`Changes outside resolved test graphs: ${uncovered.join(", ")}`);
|
|
const byProject = new Map(graphs.map(graph => [graph.project, graph]));
|
|
const manifest = { version: 1, head: diff.head, lanes: {} }, jobs = [], selectedFiles = new Set();
|
|
for (const group of groups) {
|
|
const value = inventories.get(group);
|
|
const owners = value.workspaceProjects
|
|
? Object.fromEntries(value.workspaceProjects.map(project => [project, byProject.get(project).inventory]))
|
|
: { "@paperclipai/server": group === "serialized"
|
|
? value.selectedSerializedSuites
|
|
: group === "general-chat"
|
|
? value.selectedChatSuites
|
|
: value.selectedGeneralServerSuites };
|
|
const reusable = {}, fresh = {};
|
|
for (const [project, files] of Object.entries(owners)) {
|
|
const graph = byProject.get(project), all = new Set(graph.inventory), affected = new Set(graph.selected);
|
|
if (!Array.isArray(files) || files.some(file => !all.has(file))) throw new Error(`Runner/graph inventory mismatch: ${group}`);
|
|
const matches = files.filter(file => affected.has(file));
|
|
const opaque = new Set(graph.alwaysRun);
|
|
const pure = matches.filter(file => !opaque.has(file)), retained = matches.filter(file => opaque.has(file));
|
|
if (pure.length) reusable[project] = pure;
|
|
if (retained.length) fresh[project] = retained;
|
|
matches.forEach(file => selectedFiles.add(file));
|
|
}
|
|
const partitions = [reusable, fresh].filter(files => Object.keys(files).length);
|
|
if (!partitions.length) continue;
|
|
manifest.lanes[group] = { shards: partitions };
|
|
for (const [index, files] of partitions.entries()) jobs.push({ id: `${group}-${files === reusable ? "reusable" : "fresh"}`,
|
|
cacheable: files === reusable, selected: true, args: ["--mode", group === "serialized" ? "serialized" : "general",
|
|
...(group === "serialized" ? [] : ["--group", group]), "--shard-index", String(index), "--shard-count", String(partitions.length)] });
|
|
}
|
|
const expected = new Set(graphs.flatMap(graph => graph.selected));
|
|
if (expected.size !== selectedFiles.size || [...expected].some(file => !selectedFiles.has(file))) {
|
|
throw new Error("Selected tests are not completely covered by local runner ownership");
|
|
}
|
|
const { validateSelection } = await import("../ci/test-selection-manifest.mjs");
|
|
validateSelection(manifest);
|
|
return { kind: "selected", jobs, manifest, requiredCI, selectedFiles: [...selectedFiles].sort(),
|
|
totalFiles: graphs.reduce((count, graph) => count + graph.inventory.length, 0),
|
|
retainedFiles: graphs.reduce((count, graph) => count + graph.alwaysRun.length, 0) };
|
|
} catch (error) {
|
|
return { kind: "broad", reason: `Conservative selection unavailable: ${error.message}`, jobs: [], requiredCI };
|
|
}
|
|
}
|