Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/decisions/ci-utility.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Local import-graph feedback uses eight workers on the measured 32-CPU,
47-GiB host. The default is
`max(1, min(floor(cores / 4), floor(RAM * 0.7 / 2 GiB)))`, with
`--import-graph-workers=N` as the override (`localUnitWorkerCount`,
`scripts/unit-workers.ts:15-17`). Changed-mode workers use one fresh prepared
`scripts/unit-workers.ts:16-18`). Changed-mode workers use one fresh prepared
build and private overlay views; `unit-workers --private-tree` retains the
ordinary private-build comparison. Hosted explicit-file partitions retain
`balanceTests`, their private builds and N=2.
Expand Down
2 changes: 2 additions & 0 deletions docs/process/ci-throughput.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ ID/time permit attributed historical observations. IDs cannot be overwritten
with different content. Pairs are `work-started`/`work-finished` and
`wait-started`/`wait-finished`; waits require `reason` set to `reservation`,
`ci`, `review`, `conflict` or `other`. Each pair shares worker/task/reason.
Host admission records each wait for a heavy local slot as a pair with reason
`other` and task `admission-<slot>` ([host admission](testing.md#host-admission-for-heavy-local-commands)).
Unmatched records remain unknown; overlapping tasks on one worker do not create
two workers. Complete task intervals measure task lifetime, not CPU busy time
or token cost. No records means unknown worker concurrency. This endpoint does
Expand Down
47 changes: 47 additions & 0 deletions docs/process/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,53 @@ permits setup failures and does not belong in proof. A PATH change on the host
does not upgrade a fixed `/usr/bin/bwrap` inside a container; qualify that runtime
separately before changing its supplied binary. Hosted CI images stay unchanged.

## Host admission for heavy local commands

Local long suites are for debugging a specific failure only (#2939); they run
through a host-wide admission ledger, `scripts/host-admission.mjs` (#2943).
Separate worktrees on one machine share its slots, so a heavy command that
would exceed the limit waits, printing the holders and its queue position,
instead of starting and timing out. Test deadlines are never raised to absorb
contention.

Heavy commands are the integrated default `bun run test` suite, runtime-host
and native test selections, any selected file whose measured cost in
`scripts/test-shard-costs.json` reaches 30 seconds, `check:pr --full`/`--all`,
the parallel unit-worker queue (one slot for all its workers) and
`bun run test:browser` (held on the host for the whole container run). Focused
tests and default `check:pr` are not admitted. Nested heavy commands inherit
their caller's slot, also when a caller replaced their environment: the
ledger itself shows a live ancestor holding a slot. The admitted command
receives the workspace lease's descriptor, as a nested user namespace needs. A wait is recorded as a `wait-started`/`wait-finished`
pair with reason `other` through `scripts/ci-timings.ts record`
([coordination events](ci-throughput.md#record-coordination-and-incidents)).

| Variable | Meaning |
| --- | --- |
| `BRAIN_HOST_ADMISSION_LIMIT` | Concurrent heavy commands admitted on this host. Defaults to one per four CPUs, at least 1; anything but a positive integer is refused. |
| `BRAIN_HOST_ADMISSION_DIR` | Ledger directory. Defaults to `brain-host-admission-<uid>` under the system temporary directory. The test preload gives each test process a private ledger, which its descendants also find through their ancestry; a test exercising the host ledger names its own. |
| `BRAIN_WORKER_ID`, `BRAIN_ORCHESTRATOR_ID` | Opaque worker and orchestrator IDs recorded with each slot. Without them a worker is `host-<pid>` and has no orchestrator; it still gets the admission limit. |
| `BRAIN_RED_MAIN_FIX` | The PR number of a declared red-main fix. Its commands are admitted ahead of every queued ordinary command; they do not preempt running ones. Equivalent to `run --red-main-fix <pr>`. |
| `BRAIN_HOST_ADMISSION` | Internal: the slot an admitted command's descendants inherit. Never set it by hand. |

```sh
node scripts/host-admission.mjs status
node scripts/host-admission.mjs run [--red-main-fix <pr>] -- <command...>
node scripts/host-admission.mjs preempt <slot> --reason red-main-fix [--grace-ms 10000]
```

The ledger records each slot's holder process identity, worker, orchestrator
and priority class. A holder whose process has died is reclaimed on the next
pass, after its orphaned command group has been killed. `preempt` follows the
[#2938 ruling](https://github.com/schlessera/brain-kit/issues/2938): it refuses
a slot outside the caller's orchestrator subtree, any reason but
`red-main-fix`, and a call while no red-main fix is waiting. It sends SIGTERM to
the holder's process group, lets cleanup run for the grace period, then kills
it; the slot is released only after the group has gone. The preempted run exits
`75` and is recorded as `preempted` in the ledger history, which is neither a
pass nor a failure. Another orchestrator's holder yields only through queue
priority or the maintainer.

## Required proof for a PR

The policy is [the CI decision](../decisions/ci-utility.md); the commands are
Expand Down
7 changes: 7 additions & 0 deletions scripts/check-pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { accessSync, constants, readFileSync, statSync } from "node:fs";
import { changedFiles, planChecks, workspaces, type CheckPlan } from "./ci-plan";
import { runCommands, type Command } from "./ci-runner";
import { checkPackages } from "./check-packages";
import { ensureHostAdmission } from "./host-admission.mjs";

export function localChecks(plan: CheckPlan, full = false, mergeBase?: string, importGraph = false, importGraphWorkers?: number): Command[] {
const bun = process.execPath;
Expand Down Expand Up @@ -152,6 +153,12 @@ if (import.meta.main) {
if (print) process.exit(0);
requireLocalSandbox();
const chrome = full ? requireRuntimeTools(plan) : undefined;
if (full) {
// Full local suites are for debugging only and run through host admission
// (#2939, #2943), after the cheap prerequisite refusals above.
const admitted = await ensureHostAdmission(all ? "check:pr --all" : "check:pr --full");
if (admitted !== undefined) process.exit(admitted);
}
// Setup/metadata are ordered. Full Bun tests and strict types are the only
// independent memory-bounded pair; browser jobs stay sequential to retain
// the measured scheduling/isolation behavior and diagnostic artifacts.
Expand Down
47 changes: 47 additions & 0 deletions scripts/host-admission.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export const MARKER: "BRAIN_HOST_ADMISSION";
export const PREEMPTED_EXIT: 75;
export const RED_MAIN_FIX: "red-main-fix";
export const DEFAULT_GRACE_MS: number;
export function testLedgerFor(pid: number, start: string): string;
export function ledgerDir(env?: Record<string, string | undefined>): string;
export function admissionLimit(env?: Record<string, string | undefined>, cpus?: number): number;
export interface AdmissionEntry {
id: string;
state: "queued" | "holding";
pid: number;
start: string;
worker: string;
orchestrator: string | null;
priority: "red-main-fix" | "ordinary";
fixPR: number | null;
label: string;
seq: number;
queuedAt: string;
admittedAt?: string;
child?: { pid: number; start: string };
preempt?: { reason: string; by: string; at: string; graceMs: number };
}
export interface AdmissionState { version: 1; seq: number; entries: AdmissionEntry[] }
export function admissionOrder(entries: readonly AdmissionEntry[]): AdmissionEntry[];
export function readHistory(dir: string): Record<string, unknown>[];
export function withLedger<T>(dir: string, transaction: (state: AdmissionState) => T): Promise<T>;
export interface AdmissionOptions {
env?: Record<string, string | undefined>;
dir?: string;
limit?: number;
worker?: string;
orchestrator?: string;
redMainFix?: string | number;
label?: string;
pollMs?: number;
root?: string;
timingsOut?: string;
}
export function runAdmitted(command: readonly string[], options?: AdmissionOptions): Promise<number>;
export function preempt(slot: string, options?: { env?: Record<string, string | undefined>; dir?: string; orchestrator?: string; reason?: string; graceMs?: number }):
Promise<{ slot: string; reason: string; preempted: true; forced: boolean }>;
export class RefusedError extends Error {}
export function status(options?: { env?: Record<string, string | undefined>; dir?: string }):
Promise<{ ledger: string; limit: number; holders: AdmissionEntry[]; queue: AdmissionEntry[] }>;
export function inheritedAdmission(env?: Record<string, string | undefined>): boolean;
export function ensureHostAdmission(label: string): Promise<number | undefined>;
Loading
Loading