Files
PaperClipAI/scripts/__tests__/e2e-shard.test.mjs
T
Devin FoleyandPaperclip 90bf446ad5 Guard release onboarding with scoped source E2E (#23432)
## Thinking Path

> - Paperclip helps people manage work with AI agents.
> - A release must pass onboarding before it reaches nightly.
> - Onboarding changes already run source browser tests on PRs.
> - The published release smoke used a separate scenario, which could
drift from those tests.
> - That drift blocked the December 1 nightly after onboarding
succeeded.
> - This PR fixes the stale welcome assertion and shares the scenario
between release smoke and relevant PR E2E runs.

## Linked Issues or Issue Description

Refs #23334 and #22259.

**What happened?**

[The December 1 nightly
release](https://github.com/paperclipai/paperclip/actions/runs/46614290688)
failed twice at `The onboarding chat must queue the lead's live
welcome`. PR #23334 changed the welcome to a saved message and a
question card. The greeting endpoint succeeded with `started: true` and
`runId: null`, but the release test still required a model run. PR
browser tests did not run those release expectations against the changed
source.

**Expected behavior**

Onboarding PRs and changes to their shared dependencies must run the
release scenario before merge. Known unrelated leaf-page changes and
source-unit-test-only changes can omit the extra case. Shared and
unfamiliar source paths must keep coverage. The scenario must check the
lead's welcome and all three first-chat choices, with no model run
before the user sends a choice or message. A mismatch must fail the
required `ci / e2e` check.

**Steps to reproduce**

1. Start the Docker onboarding harness with
`PAPERCLIPAI_VERSION=2026.1130.0-canary.873300` and its provider
verification mock.
2. Run the original release smoke test.
3. Observe successful login, setup, hire, and chat entry, followed by a
timeout because the run list stays empty.

**Paperclip version or commit**

Failed release source: `7ce7d99f8af81ae90f97650ef7a8692a86c63a35`.
Published package: `2026.1130.0-canary.873300`.

**Deployment mode**

The release test uses authenticated private Docker mode. The new PR test
uses the existing disposable source E2E instance and provider
verification fixture.

## What Changed

- Check the saved welcome and the three visible choices, then require an
empty agent run list.
- Move the complete wizard, hire, and chat assertions into one shared
onboarding scenario. Keep sign-in in the authenticated release wrapper.
- Add the shared scenario to the normal PR E2E suite. Omit only this
extra case when eligible source changes are confined to the Costs or
Approvals UI page, source unit tests, and audited prose. Shared
components, hooks, libraries, server code, mixed changes, and unfamiliar
source paths keep it. The repository rules require `ci / e2e` before
merge.
- Pass the trusted CI change plan to shard selection and bind it to the
checked-out commit. Reuse the existing lightweight source-diff
eligibility gate for both Vitest and E2E, with the old Vitest export
preserved. Full and shadow CI, broad or deleted inputs, invalid plans,
and stale plans keep the case. No extra browser job or matrix is added.
- Add release smoke files to the E2E and typecheck task inputs. Test
that onboarding changes select E2E and that smoke-only changes select
their two owners without forcing unrelated lanes.
- Document the shared test command and the source and installation
coverage.

## Verification

The shared browser scenario is unchanged from
`2044ada98e953504af68febe4c196f414d0bb1ae`. The selector review fix is
based on master’s hosted-shard speed improvement. Selector and
eligibility verification covers
`236640e419d1177aafb64f3f7dd1ee49848ee707`.

- Reproduced the original nightly timeout locally against the exact
published canary.
- The extracted release scenario passed against that canary in 22.1
seconds.
- The new source E2E passed against PR source and the built UI in 32.3
seconds.
- Restored the obsolete model-run assertion temporarily. The new source
E2E failed with the same `Expected: > 0 / Received: 0` timeout. Restored
the correct assertion. The final source confirmation passed in 31.7
seconds.
- `node --test scripts/__tests__/e2e-shard.test.mjs
.github/scripts/tests/ci-plan.test.mjs
scripts/ci/test-selection-workflow.test.mjs`: all 52 tests passed after
rebase. Coverage includes unrelated changes, relevant dependencies,
rename paths, full/invalid/stale plans, exact CLI shard partitions, and
both hosted shard policies. The unrelated-change and wiring tests failed
before the filter was implemented.
- Regression selection tests failed on `TaskChatComposer.tsx` and shadow
mode before this review fix. They now keep the composer, new-agent
runtime configuration, AI connection components, hooks, libraries, and
mixed source changes.
- The 3 existing source-diff eligibility contract tests passed after
extraction into the shared module.
- `actionlint .github/workflows/pr-trusted.yml` passed.
- `node --test scripts/ci/test-selection-inputs.test.mjs`: 4 tests
passed.
- The Playwright program and focused source scenario typechecks passed.
The test-budget check and `git diff --check` passed.
- The default local source bootstrap could not load the macOS embedded
PostgreSQL binary because its distribution lacked `libzstd.1.dylib`.
Source browser verification used a separate disposable Docker PostgreSQL
16 instance. Linux CI will cover the normal embedded database path.
- Earlier [hosted release
smoke](https://github.com/paperclipai/paperclip/actions/runs/46776940022)
passed both Docker onboarding and Linux background-service onboarding at
the first fix commit. The service job first hit npm package propagation
and passed on retry after publication completed.
- `pnpm verify --explain` requested broader CI coverage because the CI
task inputs changed. The focused checks above passed; the required CI
jobs will provide broad coverage.
- [Final-revision
CI](https://github.com/paperclipai/paperclip/actions/runs/46782167000)
passed on `236640e419d1177aafb64f3f7dd1ee49848ee707`. Both required
aggregates, all 16 browser shards, and all selected typecheck, build,
server, Runner, and canary dry-run jobs passed. [Incremental CI
qualification](https://github.com/paperclipai/paperclip/actions/runs/46782166749)
also passed.
- [The new source onboarding
case](https://github.com/paperclipai/paperclip/actions/runs/46782167000/job/143123174120)
ran on the hosted Linux runner and passed in 20.8 seconds.
- [Greptile
review](https://github.com/paperclipai/paperclip/pull/23432#issuecomment-7330325410)
scored `236640e419d1177aafb64f3f7dd1ee49848ee707` 5/5 and confirmed the
shared-dependency finding is fixed. There are no unresolved review
threads.

## Risks

- This changes test coverage and CI selection. It does not change
application behavior, release permissions, or publication rules.
- The source case stays on for shared and unfamiliar source changes. It
skips only the documented leaf pages and source-unit-test-only edits in
eligible plans. Local full E2E still includes it. Extend exclusions only
after checking that a path cannot affect onboarding.
- The trusted PR workflow is loaded from master. Before merge, the
current workflow keeps the full fallback because it does not yet pass
the change plan to the shard command. The unit and CLI tests qualify the
filtered path; after merge, the new workflow environment activates it.
- The source test uses local trusted authentication. The authenticated
published-package test remains responsible for login and installation
checks. Neither run needs paid model completions.
- GitHub master rules require code-owner approval for the protected CI
changes. Those review requests are already open. There are no conflicts
or unresolved review threads.
- A replacement nightly still needs a successful release run after this
reaches master. This PR does not bypass qualification or promote beta or
stable.

## Model Used

OpenAI Codex, `gpt-6.1-sol`, with xhigh reasoning effort, terminal
tools, GitHub CLI, and Playwright. The harness does not report the
context window size.

## 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-12-01 17:23:13 -08:00

607 lines
27 KiB
JavaScript

import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import test from "node:test";
import { defaultSuiteWeight, loadShardDurations } from "../general-server-shard.mjs";
import {
DEFAULT_PARALLEL_WORKERS,
EFFECTIVE_PARALLEL_SPEEDUP,
EFFECTIVE_PARALLEL_SPEEDUP_4W,
IGNORED_SPECS,
SERIAL_SPECS,
isSerialE2eSpec,
listE2eSpecs,
partitionE2eShards,
SERIAL_SLOWDOWN_2W,
resolveEffectiveParallelSpeedup,
resolveSerialSlowdown,
selectE2eShard,
selectE2eSpecsForPlan,
} from "../e2e-shard.mjs";
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..");
const script = path.join(repoRoot, "scripts", "e2e-shard.mjs");
const durationsManifest = path.join(repoRoot, "scripts", "e2e-shard-durations.json");
const playwrightConfig = path.join(repoRoot, "tests", "e2e", "playwright.config.ts");
const prCallerWorkflow = path.join(repoRoot, ".github", "workflows", "pr.yml");
const trustedPrWorkflowPath = ".github/workflows/pr-trusted.yml";
const trustedPrWorkflow = path.join(repoRoot, trustedPrWorkflowPath);
const routingWorkflow = path.join(repoRoot, ".github", "workflows", "ci-routing.yml");
const ciTaskMatrixPath = path.join(repoRoot, ".github", "ci-task-matrix.json");
const SHARD_COUNT = JSON.parse(readFileSync(ciTaskMatrixPath, "utf8")).e2e_shards.length;
const TARGET_AWS_SHARD_COUNT = 16;
const TARGET_HOSTED_SHARD_COUNT = 4;
function runShard(args, ciPlan = "") {
const result = spawnSync(process.execPath, [script, ...args], {
cwd: repoRoot, encoding: "utf8", env: { ...process.env, CI_PLAN: ciPlan },
});
assert.equal(result.status, 0, `expected success for ${args.join(" ")}: ${result.stderr}`);
return result.stdout.trim().split(/\s+/).filter(Boolean);
}
function readTrustedPrWorkflow() {
const caller = readFileSync(prCallerWorkflow, "utf8");
assert.match(
caller,
/^\s+uses: paperclipai\/paperclip\/\.github\/workflows\/pr-trusted\.yml@master\s*$/m,
"pr.yml must call the trusted workflow from CODEOWNERS-protected master",
);
// Validate proposed workflow changes locally; CI executes the merged master version.
return readFileSync(trustedPrWorkflow, "utf8");
}
function readWorkflowJobs(workflow) {
const jobs = new Map();
let current = null;
for (const line of workflow.split("\n")) {
const header = /^ {2}([A-Za-z0-9_-]+):\s*$/.exec(line);
if (header) {
current = header[1];
jobs.set(current, []);
continue;
}
if (current && /^\S/.test(line)) current = null;
if (current) jobs.get(current).push(line);
}
for (const [id, lines] of jobs) jobs.set(id, lines.join("\n"));
return jobs;
}
function runStackScope(stack, prBaseRef) {
const workflow = readFileSync(routingWorkflow, "utf8");
const match = workflow.match(
/ - name: Select PR CI scope[\s\S]*? run: \|\n([\s\S]*?)(?=\n - name:|$)/,
);
assert.ok(match, "routing workflow must define the PR CI scope script");
const script = match[1]
.split("\n")
.map((line) => line.replace(/^ {10}/, ""))
.join("\n");
const scratch = mkdtempSync(path.join(tmpdir(), "paperclip-stack-scope-"));
const output = path.join(scratch, "github-output");
try {
const result = spawnSync("bash", ["-c", script], {
encoding: "utf8",
env: {
...process.env,
GITHUB_OUTPUT: output,
PR_BASE_REF: prBaseRef,
STACK_JSON: JSON.stringify(stack),
},
});
assert.equal(result.status, 0, result.stderr);
return Object.fromEntries(
readFileSync(output, "utf8")
.trim()
.split("\n")
.map((line) => line.split("=")),
);
} finally {
rmSync(scratch, { recursive: true, force: true });
}
}
function runAggregate(block, fullCi, overrides = {}) {
const code = block.split(" node --input-type=module <<'NODE'\n")[1].split("\n NODE")[0]
.split("\n").map((line) => line.slice(10)).join("\n");
const names = /needs: \[([^\]]+)\]/.exec(block)[1].split(", ");
const results = Object.fromEntries(names.map((name) => [name, { result: overrides[name]
?? (name === "gate" || name === "policy" || fullCi ? "success" : "skipped") }]));
return spawnSync(process.execPath, ["--input-type=module", "-e", code], { encoding: "utf8", env: {
...process.env, FULL_CI: String(fullCi), CI_PLAN: "", CI_RESULTS: JSON.stringify(results),
} });
}
test("the e2e shards form a complete, non-overlapping partition", () => {
const specs = listE2eSpecs();
assert.ok(specs.length > 0, "expected a non-empty e2e spec set");
const shards = Array.from({ length: SHARD_COUNT }, (_, index) =>
runShard(["--shard-index", String(index), "--shard-count", String(SHARD_COUNT)]),
);
const combined = shards.flat();
assert.equal(combined.length, specs.length, "every spec must land on exactly one shard");
assert.deepEqual([...combined].sort(), [...specs].sort());
for (const shard of shards) {
assert.ok(shard.length > 0, "no shard may be empty — Playwright fails a run with no matching specs");
}
});
const planHead = "a".repeat(40);
const scopedPlan = (changed, overrides = {}) => JSON.stringify({
version: 1, mode: "enabled", forceReason: null, changed,
diff: { head: planHead }, ...overrides,
});
test("release onboarding is omitted for unrelated changes without removing other browser specs", () => {
const files = listE2eSpecs();
const ordinary = files.filter(file => file !== "tests/e2e/release-onboarding.spec.ts");
for (const changed of [
["ui/src/pages/Costs.tsx"],
["ui/src/lib/onboarding-route.test.ts", "server/src/__tests__/onboarding-chat-route.test.ts"],
["ui/src/pages/Approvals.tsx", "doc/testing/onboarding-first-chat-test-drive.md"],
]) {
assert.deepEqual(selectE2eSpecsForPlan(files, scopedPlan(changed), planHead), ordinary);
}
});
test("release onboarding is retained for its flow, shared dependencies, and smoke fixtures", () => {
const files = listE2eSpecs();
for (const file of [
"ui/src/components/OnboardingWizard.tsx", "ui/src/components/onboarding/FooterNav.tsx",
"ui/src/lib/onboarding-route.ts", "ui/src/pages/AgentChat.tsx", "ui/src/pages/IssueDetail.tsx",
"ui/src/components/ui/button.tsx", "ui/src/api/agents.ts", "ui/src/context/CompanyContext.tsx",
"ui/src/index.css", "ui/src/pages/IssueDetail/history.ts",
"ui/src/components/task-chat/TaskChatComposer.tsx",
"ui/src/lib/new-agent-runtime-config.ts", "ui/src/components/ai-connections/AiConnectionField.tsx",
"ui/src/hooks/useAgentChatEnabled.ts", "ui/src/lib/recent-agent-chats.ts",
"server/src/routes/agents.ts", "server/src/services/onboarding-chat.ts",
"server/src/services/costs.ts",
"server/src/services/ai-api-key-test.ts", "server/src/services/agent-conversations.ts",
"packages/shared/src/question-set.ts", "packages/db/src/schema/agents.ts",
"tests/release-smoke/onboarding-scenario.ts", "tests/e2e/release-onboarding.spec.ts",
"tests/e2e/start-e2e-server.ts", "tests/e2e/fixtures/ai-connection-provider.mjs",
]) {
assert.deepEqual(selectE2eSpecsForPlan(files, scopedPlan([file]), planHead), files, file);
}
for (const changed of [
["ui/src/components/OnboardingWizard.tsx", "ui/src/components/SetupWizard.tsx"],
["ui/src/pages/Costs.tsx", "ui/src/lib/new-agent-runtime-config.ts"],
["ui/src/pages/Approvals.tsx", "ui/src/components/task-chat/TaskChatComposer.tsx"],
]) assert.deepEqual(selectE2eSpecsForPlan(files, scopedPlan(changed), planHead), files);
});
test("release onboarding remains in full, unavailable, malformed, or stale plans", () => {
const files = listE2eSpecs();
for (const serialized of [
undefined, "", "{", "null", "[]", "{}",
scopedPlan([]), scopedPlan([null]), scopedPlan(["../outside.ts"]),
scopedPlan(["ui/src/pages/Costs.tsx"], { mode: "invalid" }),
scopedPlan(["ui/src/pages/Costs.tsx"], { mode: "shadow" }),
scopedPlan(["ui/src/pages/Costs.tsx"], { diff: null }),
scopedPlan(["ui/src/pages/Costs.tsx"], { version: 2 }),
scopedPlan(["ui/src/pages/Costs.tsx"], { mode: "full" }),
scopedPlan(["ui/src/pages/Costs.tsx"], { forceReason: "Diff unavailable" }),
scopedPlan(["ui/src/pages/Costs.tsx"], { diff: { head: "b".repeat(40) } }),
]) {
assert.deepEqual(selectE2eSpecsForPlan(files, serialized, planHead), files);
}
});
test("conditional onboarding selection keeps an exact CLI shard partition", () => {
const result = spawnSync("git", ["rev-parse", "HEAD"], { cwd: repoRoot, encoding: "utf8" });
assert.equal(result.status, 0);
const serialized = scopedPlan(["ui/src/pages/Costs.tsx"], { diff: { head: result.stdout.trim() } });
const expected = listE2eSpecs().filter(file => file !== "tests/e2e/release-onboarding.spec.ts");
const shards = Array.from({ length: TARGET_HOSTED_SHARD_COUNT }, (_, index) =>
runShard(["--shard-index", String(index), "--shard-count", String(TARGET_HOSTED_SHARD_COUNT)], serialized));
assert.deepEqual(shards.flat().sort(), expected);
assert.ok(shards.every(shard => shard.length > 0));
});
test("the PR browser step passes the trusted change plan to shard selection", () => {
const block = readWorkflowJobs(readTrustedPrWorkflow()).get("e2e_shards");
const step = block.split(" - name: Run e2e tests\n")[1].split(" - name:")[0];
assert.match(step, /CI_PLAN: \$\{\{ needs\.gate\.outputs\.plan \}\}/);
});
test("the ignored spec list matches playwright.config.ts testIgnore", () => {
const config = readFileSync(playwrightConfig, "utf8");
const match = config.match(/testIgnore:\s*\[([^\]]*)\]/);
assert.ok(match, "expected a testIgnore array in playwright.config.ts");
const configured = [...match[1].matchAll(/"([^"]+)"/g)].map((entry) => entry[1]);
assert.deepEqual([...configured].sort(), [...IGNORED_SPECS].sort());
});
test("the serial spec list matches playwright.config.ts SERIAL_SPECS and never mixes with parallel specs", () => {
const config = readFileSync(playwrightConfig, "utf8");
const match = config.match(/export const SERIAL_SPECS = \[([\s\S]*?)\];/);
assert.ok(match, "expected a SERIAL_SPECS array in playwright.config.ts");
const configured = [...match[1].matchAll(/"([^"]+)"/g)].map((entry) => entry[1]);
assert.deepEqual([...configured].sort(), [...SERIAL_SPECS].sort());
const specs = listE2eSpecs();
const durations = loadShardDurations(durationsManifest);
const shards = partitionE2eShards(specs, SHARD_COUNT, durations);
assert.equal(shards.length, SHARD_COUNT);
for (const shard of shards) {
const serialCount = shard.files.filter(isSerialE2eSpec).length;
assert.ok(
serialCount === 0 || serialCount === shard.files.length,
`shard must not mix serial and parallel specs: ${shard.files.join(", ")}`,
);
}
});
test("playwright.config.ts supports skipping the UI build when PAPERCLIP_E2E_SKIP_UI_BUILD=1 and uses unminified inline builds", () => {
const config = readFileSync(playwrightConfig, "utf8");
assert.match(config, /PAPERCLIP_E2E_SKIP_UI_BUILD/);
assert.match(config, /ui-dist.*index\.html/);
assert.match(config, /PAPERCLIP_E2E_BUILD_UI/);
assert.match(config, /availableParallelism/);
});
test("start-e2e-server.ts boots disposable embedded Postgres without durable fsync and overlaps UI build", () => {
const bootstrap = readFileSync(path.join(repoRoot, "tests", "e2e", "start-e2e-server.ts"), "utf8");
assert.match(bootstrap, /--no-sync/);
assert.match(bootstrap, /fsync = off/);
assert.match(bootstrap, /synchronous_commit = off/);
assert.match(bootstrap, /full_page_writes = off/);
assert.match(bootstrap, /PAPERCLIP_E2E_BUILD_UI/);
assert.match(bootstrap, /startParallelUiBuild/);
assert.match(bootstrap, /"--minify",\s*"false"/);
assert.match(bootstrap, /Promise\.all\(/);
});
test("resolveEffectiveParallelSpeedup matches Playwright worker scaling for 4-vCPU and 8-vCPU runners", () => {
assert.equal(resolveEffectiveParallelSpeedup(2), 1);
assert.equal(resolveEffectiveParallelSpeedup(4), EFFECTIVE_PARALLEL_SPEEDUP);
assert.equal(resolveEffectiveParallelSpeedup(6), 2.55);
assert.equal(resolveEffectiveParallelSpeedup(8), EFFECTIVE_PARALLEL_SPEEDUP_4W);
assert.equal(resolveEffectiveParallelSpeedup(16), EFFECTIVE_PARALLEL_SPEEDUP_4W);
});
test("resolveSerialSlowdown only calibrates 2-worker (4-vCPU hosted) runners", () => {
assert.equal(resolveSerialSlowdown(2), 1);
assert.equal(resolveSerialSlowdown(4), SERIAL_SLOWDOWN_2W);
assert.equal(resolveSerialSlowdown(5), SERIAL_SLOWDOWN_2W);
assert.equal(resolveSerialSlowdown(8), 1);
});
test("4-shard hosted partitioning splits serial specs across two shards with the hosted calibration", () => {
const durations = loadShardDurations(durationsManifest);
const specs = listE2eSpecs();
const options = { parallelWorkers: EFFECTIVE_PARALLEL_SPEEDUP, serialSlowdown: SERIAL_SLOWDOWN_2W };
const shards = partitionE2eShards(specs, TARGET_HOSTED_SHARD_COUNT, durations, options);
assert.equal(shards.length, TARGET_HOSTED_SHARD_COUNT);
assert.deepEqual([...shards.flatMap((shard) => shard.files)].sort(), [...specs].sort());
const serialShards = shards.filter((shard) => shard.mode === "serial");
assert.ok(
serialShards.length >= 2,
`hosted runs must not put every serial spec on one shard: ${serialShards.map((s) => s.files.length).join(",")}`,
);
});
test("the CLI applies the hosted serial calibration by default on a 4-vCPU runner", () => {
const env = { ...process.env };
delete env.PAPERCLIP_E2E_PARALLEL_SPEEDUP;
delete env.PAPERCLIP_E2E_SERIAL_SLOWDOWN;
const fourCpus = "data:text/javascript,import os from 'node:os'; os.availableParallelism = () => 4;";
const shards = Array.from({ length: TARGET_HOSTED_SHARD_COUNT }, (_, index) => {
const result = spawnSync(
process.execPath,
["--import", fourCpus, script, "--shard-index", String(index), "--shard-count", String(TARGET_HOSTED_SHARD_COUNT)],
{ cwd: repoRoot, encoding: "utf8", env },
);
assert.equal(result.status, 0, result.stderr);
return result.stdout.trim().split(/\s+/).filter(Boolean);
});
const serialShards = shards.filter((files) => files.some(isSerialE2eSpec));
assert.equal(serialShards.length, 2);
for (const files of serialShards) assert.ok(files.every(isSerialE2eSpec));
});
test("16-shard AWS partitioning stays complete, segregated, and balanced under 85s estimated wall-clock", () => {
const durations = loadShardDurations(durationsManifest);
const specs = listE2eSpecs();
const fallbackWeight = defaultSuiteWeight(durations);
const shards = partitionE2eShards(specs, TARGET_AWS_SHARD_COUNT, durations, {
parallelWorkers: EFFECTIVE_PARALLEL_SPEEDUP_4W,
});
assert.equal(shards.length, TARGET_AWS_SHARD_COUNT);
const combined = shards.flatMap((shard) => shard.files);
assert.deepEqual([...combined].sort(), [...specs].sort());
for (const shard of shards) {
assert.ok(shard.files.length > 0, "no 16-shard bucket may be empty");
const serialCount = shard.files.filter(isSerialE2eSpec).length;
assert.ok(
serialCount === 0 || serialCount === shard.files.length,
`16-shard bucket must not mix serial and parallel specs: ${shard.files.join(", ")}`,
);
}
const wallWeights = shards.map((shard) => {
const serialSum = shard.files.reduce((sum, file) => sum + (durations[file] ?? fallbackWeight), 0);
return shard.mode === "parallel" ? serialSum / EFFECTIVE_PARALLEL_SPEEDUP_4W : serialSum;
});
const heaviest = Math.max(...wallWeights);
const totalWall = wallWeights.reduce((sum, weight) => sum + weight, 0);
const largestSerialSpec = Math.max(
0,
...specs.filter(isSerialE2eSpec).map((file) => durations[file] ?? fallbackWeight),
);
const bound = Math.max((totalWall / TARGET_AWS_SHARD_COUNT) * 1.15, largestSerialSpec);
assert.ok(
heaviest <= bound,
`heaviest 16-shard bucket ${Math.round(heaviest)}ms exceeds the balance bound (${Math.round(bound)}ms)`,
);
assert.ok(
heaviest <= 85_000,
`heaviest 16-shard bucket ${Math.round(heaviest)}ms exceeds 85s Phase 5 ceiling`,
);
});
test("4-shard hosted partitioning stays complete and segregated on 4-vCPU runners", () => {
const durations = loadShardDurations(durationsManifest);
const specs = listE2eSpecs();
const shards = partitionE2eShards(specs, TARGET_HOSTED_SHARD_COUNT, durations, {
parallelWorkers: EFFECTIVE_PARALLEL_SPEEDUP,
});
assert.equal(shards.length, TARGET_HOSTED_SHARD_COUNT);
const combined = shards.flatMap((shard) => shard.files);
assert.deepEqual([...combined].sort(), [...specs].sort());
for (const shard of shards) {
assert.ok(shard.files.length > 0, "no 4-shard hosted bucket may be empty");
const serialCount = shard.files.filter(isSerialE2eSpec).length;
assert.ok(
serialCount === 0 || serialCount === shard.files.length,
`4-shard hosted bucket must not mix serial and parallel specs: ${shard.files.join(", ")}`,
);
}
});
test("the duration manifest only names specs that still exist", () => {
const durations = loadShardDurations(durationsManifest);
assert.ok(Object.keys(durations).length > 0, "expected a populated duration manifest");
const specs = new Set(listE2eSpecs());
for (const file of Object.keys(durations)) {
assert.ok(specs.has(file), `duration manifest names a spec that no longer runs: ${file}`);
}
});
test("the weighted partition keeps the shards close to balanced", () => {
const durations = loadShardDurations(durationsManifest);
const specs = listE2eSpecs();
const fallbackWeight = defaultSuiteWeight(durations);
const shards = partitionE2eShards(specs, SHARD_COUNT, durations, {
parallelWorkers: EFFECTIVE_PARALLEL_SPEEDUP_4W,
});
const wallWeights = shards.map((shard) => {
const serialSum = shard.files.reduce((sum, file) => sum + (durations[file] ?? fallbackWeight), 0);
return shard.mode === "parallel" ? serialSum / EFFECTIVE_PARALLEL_SPEEDUP_4W : serialSum;
});
const heaviest = Math.max(...wallWeights);
const totalWall = wallWeights.reduce((sum, weight) => sum + weight, 0);
const largestSerialSpec = Math.max(
0,
...specs.filter(isSerialE2eSpec).map((file) => durations[file] ?? fallbackWeight),
);
const bound = Math.max((totalWall / SHARD_COUNT) * 1.15, largestSerialSpec);
assert.ok(
heaviest <= bound,
`heaviest shard ${Math.round(heaviest)}ms exceeds the balance bound (${Math.round(bound)}ms)`,
);
});
test("shard arguments are validated", () => {
for (const args of [
["--shard-index", "2", "--shard-count", "2"],
["--shard-index", "-1", "--shard-count", "2"],
["--shard-index", "0", "--shard-count", "0"],
]) {
const result = spawnSync(process.execPath, [script, ...args], { cwd: repoRoot, encoding: "utf8" });
assert.notEqual(result.status, 0, `expected failure for ${args.join(" ")}`);
}
});
test("pr.yml calls the trusted PR workflow from master", () => {
assert.ok(readTrustedPrWorkflow().length > 0);
});
test("the trusted PR workflow keeps a stable aggregate check named e2e over the shard matrix", () => {
// Branch protection requires a check literally named `e2e`. The shards run
// as `e2e shard (n/3)`, so the aggregate job below is what keeps the
// required-check contract intact — same pattern as the `verify` aggregate.
const workflow = readTrustedPrWorkflow();
const jobs = readWorkflowJobs(workflow);
const aggregate = jobs.get("e2e");
assert.ok(aggregate, "pr-trusted.yml must define an `e2e` job to satisfy branch protection");
assert.match(aggregate, /^ {4}name: e2e$/m, "the aggregate job must be named exactly `e2e`");
assert.match(aggregate, /^ {4}if: \$\{\{ always\(\) \}\}$/m, "the aggregate must run even when a shard fails");
assert.match(
aggregate,
/^ {4}needs: \[gate, (?:policy, )?e2e_shards\]$/m,
"the aggregate must depend on the runner gate, optional policy gate, and shard matrix",
);
assert.equal(runAggregate(aggregate, true).status, 0);
assert.equal(runAggregate(aggregate, false).status, 0);
for (const result of ["failure", "cancelled", "skipped"]) {
assert.notEqual(runAggregate(aggregate, true, { e2e_shards: result }).status, 0);
}
const shards = jobs.get("e2e_shards");
assert.ok(shards, "pr-trusted.yml must define the `e2e_shards` matrix job");
const matrixEntries = JSON.parse(readFileSync(path.join(repoRoot, ".github/ci-task-matrix.json"), "utf8"))
.e2e_shards.map((entry) => ({
shardIndex: entry.shard_index,
shardCount: entry.shard_count,
shardLabel: entry.shard_label,
}));
assert.equal(matrixEntries.length, SHARD_COUNT, "the shard matrix must define exactly SHARD_COUNT entries");
assert.deepEqual(
matrixEntries.map((entry) => entry.shardIndex).sort((a, b) => a - b),
Array.from({ length: SHARD_COUNT }, (_, index) => index),
"the shard matrix must define each shard index exactly once",
);
for (const entry of matrixEntries) {
assert.equal(entry.shardCount, SHARD_COUNT, "each shard matrix entry must use the same SHARD_COUNT");
assert.equal(entry.shardLabel, `${entry.shardIndex + 1}/${SHARD_COUNT}`, "each shard label must match its index");
}
});
test("runner authorization uses reserved capacity and executes metadata checks only", () => {
const workflow = readFileSync(routingWorkflow, "utf8");
const jobs = readWorkflowJobs(workflow.split(/^jobs:\n/m)[1]);
assert.deepEqual([...jobs.keys()], ["gate"]);
assert.match(jobs.get("gate"), /runs-on\/fleet=paperclip-ci-routing-x64\/env=public-ci/);
assert.doesNotMatch(jobs.get("gate"), /uses:|actions\/checkout|secrets\./);
assert.match(workflow, /^ {6}runner:\n {8}value: \$\{\{ jobs\.gate\.outputs\.runner \}\}$/m);
assert.match(workflow, /^ {6}full_ci:\n {8}value: \$\{\{ jobs\.gate\.outputs\.full_ci \}\}$/m);
assert.doesNotMatch(workflow, /^ {2}(?:actions|contents|pull-requests): write$/m);
});
test("the trusted PR workflow limits full CI to merge-relevant stack layers", () => {
const workflow = readFileSync(trustedPrWorkflow, "utf8");
const jobs = readWorkflowJobs(workflow);
assert.match(jobs.get("gate"), /uses: paperclipai\/paperclip\/\.github\/workflows\/ci-routing\.yml@master/);
const gate = readWorkflowJobs(readFileSync(routingWorkflow, "utf8")).get("gate");
assert.match(gate, /^ {6}full_ci: \$\{\{ steps\.scope\.outputs\.full_ci \}\}$/m);
assert.match(gate, /STACK_JSON: \$\{\{ toJSON\(github\.event\.pull_request\.stack\) \}\}/);
assert.match(gate, /stack_position == stack_size/);
assert.match(gate, /"\$stack_base_ref" == "\$PR_BASE_REF"/);
for (const jobId of [
"typecheck_release_registry",
"general_tests",
"verify_paperclip_runner",
"build",
"verify_serialized_server",
"canary_dry_run",
"e2e_shards",
"verify_runner_e2e_support",
]) {
assert.match(
jobs.get(jobId),
/^ {4}if: \$\{\{ (?:always\(\) && !cancelled\(\) && needs\.gate\.result == 'success' && )?needs\.gate\.outputs\.full_ci == 'true' && /m,
`${jobId} must run only when the gate selects full CI`,
);
}
assert.doesNotMatch(
jobs.get("policy"),
/needs\.gate\.outputs\.full_ci/,
"the policy job must run on every PR layer",
);
const verify = jobs.get("verify");
assert.match(
verify,
/^ {4}needs: \[gate, test_selection, policy, typecheck_release_registry, general_tests, verify_paperclip_runner, build, docker_context_integrity, sandbox_provider_tests, verify_serialized_server, canary_dry_run, verify_runner_e2e_support\]$/m,
);
assert.equal(runAggregate(verify, true).status, 0);
assert.equal(runAggregate(verify, false).status, 0);
for (const result of ["failure", "cancelled", "skipped"]) {
assert.equal(runAggregate(verify, true, { test_selection: result }).status, 0,
"an optional selector cannot block the successful original matrix");
}
for (const job of ["gate", "policy", "typecheck_release_registry", "general_tests", "verify_paperclip_runner",
"build", "docker_context_integrity", "sandbox_provider_tests", "verify_serialized_server", "canary_dry_run", "verify_runner_e2e_support"]) {
for (const result of ["failure", "cancelled", "skipped"]) {
assert.notEqual(runAggregate(verify, true, { [job]: result }).status, 0, `${job}: ${result}`);
}
if (job !== "gate" && job !== "policy") {
assert.notEqual(runAggregate(verify, false, { [job]: "success" }).status, 0, job);
}
}
const e2e = jobs.get("e2e");
assert.match(e2e, /^ {4}needs: \[gate, policy, e2e_shards\]$/m);
assert.notEqual(runAggregate(e2e, true, { policy: "failure" }).status, 0);
assert.notEqual(runAggregate(e2e, false, { e2e_shards: "success" }).status, 0);
});
test("the stacked PR scope selector runs full CI only where intended", () => {
assert.equal(runStackScope(null, "master").full_ci, "true");
assert.equal(
runStackScope({ position: 11, size: 11, base: { ref: "master" } }, "stack-10").full_ci,
"true",
);
assert.equal(
runStackScope({ position: 1, size: 11, base: { ref: "master" } }, "master").full_ci,
"true",
);
assert.equal(
runStackScope({ position: 6, size: 11, base: { ref: "master" } }, "stack-5").full_ci,
"false",
);
assert.equal(
runStackScope({ position: "invalid", size: 11, base: { ref: "master" } }, "stack-5").full_ci,
"true",
);
});
test("the trusted PR workflow passes the shard's spec filter to Playwright without a literal --", () => {
// `pnpm run test:e2e -- $specs` forwards the literal separator to Playwright,
// so the specs after it are not applied as file filters.
const workflow = readTrustedPrWorkflow();
assert.ok(
!/pnpm run test:e2e --\s/.test(workflow),
"pr-trusted.yml must not insert a literal `--` between `pnpm run test:e2e` and the spec filter",
);
assert.match(
workflow,
/pnpm run test:e2e \$specs/,
"pr-trusted.yml e2e_shards must invoke `pnpm run test:e2e $specs`",
);
});
test("the trusted PR workflow regenerates stale stacked lockfiles", () => {
// Validate the proposed workflow here. The caller executes the merged master
// workflow; edits to this workflow take effect after code-owner review and merge.
const workflow = readFileSync(trustedPrWorkflow, "utf8");
assert.match(
workflow,
/policy:\n needs: \[gate\][\s\S]{0,160}timeout-minutes: 10/,
"the unconditional resolution step needs the same timeout headroom as the lockfile refresh workflow",
);
const policy = workflow.split(" policy:\n")[1].split(" typecheck_release_registry:\n")[0];
assert.doesNotMatch(
policy,
/cache: pnpm|uses: actions\/cache/,
"resolution-only policy must not restore or save a dependency store",
);
assert.match(
policy,
/pnpm install --resolution-only --ignore-scripts --no-frozen-lockfile/,
"the policy job must resolve the complete merge tree without rewriting platform metadata",
);
// Test lanes no longer wait on a policy-job artifact: each install step
// resolves a stale lockfile inline, so a manifest-changing or stacked PR
// still installs while the policy job validates resolution in parallel.
const fallbackInstalls = workflow.match(
/if ! pnpm install --frozen-lockfile; then\n[\s\S]{0,240}?pnpm install --resolution-only --ignore-scripts --no-frozen-lockfile\n\s+pnpm install --frozen-lockfile\n\s+fi/g,
) ?? [];
assert.equal(
fallbackInstalls.length,
7,
"every downstream install job must resolve a stale lockfile inline",
);
assert.doesNotMatch(
workflow,
/Restore regenerated PR lockfile|lockfile_regenerated|name: pr-lockfile/,
"the policy lockfile artifact chain must stay removed; it put the policy job on every lane's critical path",
);
});