# Optimize CI Speed Without Weakening Checks (/prompts/existing/quality/optimize-ci-speed)

```markdown title="optimize-ci-speed.md" lineNumbers
## Context

You are making this repository's CI as fast as possible without letting it lie. This is
a ONE-TIME restructure task. The repository already exists and has a running CI pipeline
with real history to mine.

**The rules in the Guardrails section outrank speed. Read them before starting.**

## Goal

A PR gets its CI result in under 1 minute at the median and under 2 minutes at p95.

## Inputs

This prompt is self-contained: it discovers everything it needs from the repository and
its CI run history. The named tools and services (bun, oxlint, TypeScript 7, Blacksmith,
hk, GitHub-hosted runners) are defaults — replace them with what the repo actually uses
per the tool-switching rule in **Instructions**.

## Instructions

### Audit first

Pull the last 150 CI runs and split every job into waiting, setup and running, at p50
and p95. Fix the biggest bucket first.

### Hit these budgets

- **Waiting, under 5 seconds**: move every job off GitHub's hosted runners to Blacksmith
  and keep machines warm. Cancel a branch's in-progress run when a new commit lands.
- **Setup, under 15 seconds**: bake the toolchain into the runner image. Get PR cache
  hits above 95% by seeding caches from main, and cache `node_modules` itself. Use a
  blobless clone (`filter: blob:none`).
- **Slowest job, under 35 seconds**. No required job waits on another.
- **Lint, under 1 second**: `bun lint`. It reads the existing `.oxlintrc.json`. Turn on
  every rule you can.
- **Format, under 1 second**: `bun fmt`.
- **Typecheck, under 5 seconds**: `bun check` on TypeScript 7.
- **Tests, under 30 seconds per shard**: `bun test`. Test files keep their `vitest`
  imports. Isolate modules, not processes, reset state after every file, and run in
  random order to flush out leaks.
- **Install and scripts**: bun. Delete npm, yarn, pnpm, Prettier, ESLint, oxlint, Vitest
  and Jest. If a tool only exists in a canary, pin that exact canary.
- **Shards**: buy cores before shards, match workers to cores, and give each shard its
  own throwaway database. Split any test file over 20 seconds. Balance shards from file
  times that main records on every full run, and prove the shards together ran every
  file.
- **Fewer tests, on evidence**: for two weeks, log every test failure you fix as a
  caught regression or a change detector. Delete the files that only detect change and
  ban their patterns in `AGENTS.md`.
- **Shape**: run lint, typecheck, build and tests in parallel. Fold the small checks
  into one job that lists every failure.
- **Selection**: on PRs, run only the tests the diff can reach, and list the inputs that
  rerun everything. Then hide a real change from it on purpose and confirm CI goes red.
- **Local, under 9 seconds**: hk hooks. Pre-commit formats and lints staged files;
  pre-push runs the same check as CI. A hook, not a line in `AGENTS.md`, because agents
  skip steps.
- **Keep it fast**: fail any job that exceeds its budget.

### Tool switching

The tools above are defaults; you need a measured reason to keep an old one. Before each
switch, run old and new side by side and list anything the new one misses.

### Working agreement

One small PR per change, with before and after numbers from real runs. Do everything you
can without me first.

## Output format

- One small PR per change, each including before and after numbers from real runs.
- One consolidated list of anything that needs an account, a secret or a bigger bill,
  with expected monthly cost for each item.
- A final report ending with total CI time and cost, before and after.

## Guardrails

These rules outrank speed — binding throughout the whole task:

- The full suite runs before anything reaches main, in a merge queue. Selection only
  narrows PR feedback.
- Never hide failures. No retries, wider timeouts or skipped tests. A check that needs a
  retry has a bug; find it.
- Never check less. Lint rules and required checks only stay or grow.
- Never delete tests for authorization, privacy, migrations or concurrency.
- You may modify repository files (CI workflows, configs, hooks, `AGENTS.md`) as part of
  the one-small-PR-per-change flow. Anything that requires an account, a secret or
  spend goes on the escalation list instead of being done unilaterally.
```