@ultimat3/cli 31.47.0 → 31.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +29 -29
- package/src/cmd-drill.ts +20 -1
- package/src/cmd-heavy-spec.ts +28 -0
- package/src/cmd-heavy.ts +160 -0
- package/src/cmd-test.ts +13 -3
- package/src/cmd-upgrade.ts +22 -11
- package/src/codemods/docs-many-agents.ts +30 -0
- package/src/codemods/e2e-suite-floor.ts +42 -0
- package/src/codemods/e2e-suite.ts +71 -0
- package/src/codemods/index.ts +4 -0
- package/src/codemods/pending.ts +2 -2
- package/src/codemods/test-ports.ts +40 -33
- package/src/codemods/test-suites.ts +11 -22
- package/src/codemods/verify-floor-skips.ts +18 -6
- package/src/doctor-advisory-floors.ts +30 -8
- package/src/error-codes-tooling.ts +9 -3
- package/src/exec.ts +11 -3
- package/src/heavy-cores.ts +51 -0
- package/src/heavy-env.ts +19 -0
- package/src/heavy-errors.ts +36 -0
- package/src/heavy-flock.ts +73 -0
- package/src/heavy-ledger.ts +358 -0
- package/src/heavy-memory.ts +67 -0
- package/src/heavy-weights.ts +47 -0
- package/src/heavy.ts +290 -0
- package/src/local-cli.ts +6 -5
- package/src/mcp-errors.ts +4 -1
- package/src/messages.ts +11 -0
- package/src/registry.ts +2 -0
- package/src/role-replicator.ts +5 -3
- package/src/runtime-cache.ts +24 -3
- package/src/runtime-overrides.ts +4 -2
- package/src/templates/guard-released-forms.ts +35 -13
- package/src/templates/scaffold-claude-agents.ts +9 -6
- package/src/templates/scaffold-claude-commands.ts +1 -1
- package/src/templates/scaffold-docs.ts +5 -4
- package/src/templates/scaffold-suites.ts +22 -0
- package/src/test-batches.ts +9 -3
- package/src/test-shards.ts +3 -3
- package/src/test-slots.ts +35 -161
- package/src/upgrade-gate-codemods.ts +1 -18
- package/src/verify-checks.ts +4 -3
- package/src/verify-coverage-run.ts +5 -4
- package/src/verify-deadline.ts +18 -2
- package/src/verify-heavy-steps.ts +26 -0
- package/src/verify-test-run.ts +3 -2
- package/src/verify-tests.ts +27 -20
- package/wiki/Building-Your-Own-Base.md +15 -2
- package/wiki/CI-Parallel-Gate.md +2 -3
- package/wiki/CLI-Reference.md +25 -13
- package/wiki/Caching-And-Invalidation.md +1 -1
- package/wiki/Compared.md +13 -11
- package/wiki/Configuration.md +22 -4
- package/wiki/Home.md +1 -0
- package/wiki/Interface-Rules.md +1 -1
- package/wiki/Known-Gaps.md +1 -1
- package/wiki/Mail.md +1 -1
- package/wiki/Many-Agents.md +60 -0
- package/wiki/Stability.md +1 -1
- package/wiki/Testing.md +5 -5
- package/wiki/Tutorial-01-First-App.md +2 -2
- package/wiki/Upgrading.md +2 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// 31.27.0: the test stack's host ports are the app's own (its name picks an offset into each band),
|
|
2
|
-
// so two apps on one machine stop dialling each other's Postgres.
|
|
3
|
-
//
|
|
2
|
+
// so two apps on one machine stop dialling each other's Postgres. The compose file publishes what
|
|
3
|
+
// the env file names, so they move together, only while both are the old scaffold's byte for byte;
|
|
4
|
+
// otherwise neither moves and each is reported with its diff.
|
|
4
5
|
|
|
5
6
|
import type { NameSet } from '../templates/naming';
|
|
6
7
|
import { names } from '../templates/naming';
|
|
@@ -11,9 +12,10 @@ import {
|
|
|
11
12
|
testPorts,
|
|
12
13
|
testServicesFiles,
|
|
13
14
|
} from '../templates/scaffold-test-services';
|
|
14
|
-
import type { Codemod,
|
|
15
|
+
import type { Codemod, CodemodResult } from './codemod';
|
|
15
16
|
import { unifiedDiff } from './engine-diff';
|
|
16
|
-
import {
|
|
17
|
+
import type { FileText } from './engine-edits';
|
|
18
|
+
import { readText } from './engine-edits';
|
|
17
19
|
|
|
18
20
|
const NOTHING: CodemodResult = { changed: [], skipped: [] };
|
|
19
21
|
|
|
@@ -42,30 +44,27 @@ function before(today: string, app: NameSet): string {
|
|
|
42
44
|
return text;
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
},
|
|
67
|
-
],
|
|
68
|
-
};
|
|
47
|
+
interface PortFile {
|
|
48
|
+
readonly path: string;
|
|
49
|
+
readonly today: string;
|
|
50
|
+
readonly old: string;
|
|
51
|
+
readonly file: FileText | undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const isMovable = ({ file, today, old }: PortFile): boolean =>
|
|
55
|
+
file !== undefined && (file.text === today || file.text === old);
|
|
56
|
+
|
|
57
|
+
/** Every file still short of today's, each with why it stays and the change to make by hand. */
|
|
58
|
+
function heldBack(files: readonly PortFile[]): CodemodResult {
|
|
59
|
+
const blockers = files.filter((one) => !isMovable(one)).map((one) => one.path);
|
|
60
|
+
const skipped = files.flatMap(({ path, today, old, file }) => {
|
|
61
|
+
if (file === undefined || file.text === today) return [];
|
|
62
|
+
const reason = blockers.includes(path)
|
|
63
|
+
? "it is no longer the old scaffold's file byte for byte, so the test ports stay in both files: the diff is the scaffold's change, to make by hand in both if two apps share this machine"
|
|
64
|
+
: `${blockers.join(' and ')} cannot move with it, and the two must name the same ports, so this file stays too: the diff is the scaffold's change, to make by hand in both if two apps share this machine`;
|
|
65
|
+
return [{ path, reason, diff: unifiedDiff(path, old, today) }];
|
|
66
|
+
});
|
|
67
|
+
return { changed: [], skipped };
|
|
69
68
|
}
|
|
70
69
|
|
|
71
70
|
export const codemod: Codemod = {
|
|
@@ -76,11 +75,19 @@ export const codemod: Codemod = {
|
|
|
76
75
|
const kebab = appName((await readText(app, TEST_ENV_PATH))?.text ?? '');
|
|
77
76
|
if (kebab === undefined) return NOTHING;
|
|
78
77
|
const name = names(kebab);
|
|
79
|
-
const
|
|
80
|
-
for (const
|
|
81
|
-
if (typeof
|
|
82
|
-
|
|
78
|
+
const files: PortFile[] = [];
|
|
79
|
+
for (const { path, contents } of testServicesFiles(name)) {
|
|
80
|
+
if (typeof contents !== 'string') continue;
|
|
81
|
+
const old = before(contents, name);
|
|
82
|
+
files.push({ path, today: contents, old, file: await readText(app, path) });
|
|
83
|
+
}
|
|
84
|
+
if (!files.every(isMovable)) return heldBack(files);
|
|
85
|
+
const changed: string[] = [];
|
|
86
|
+
for (const { path, today, file } of files) {
|
|
87
|
+
if (file === undefined || file.text === today) continue;
|
|
88
|
+
await app.write(path, file.restore(today));
|
|
89
|
+
changed.push(path);
|
|
83
90
|
}
|
|
84
|
-
return
|
|
91
|
+
return { changed, skipped: [] };
|
|
85
92
|
},
|
|
86
93
|
};
|
|
@@ -1,37 +1,26 @@
|
|
|
1
1
|
// 31.27.0: every scaffold ships a `live` suite (the production driver against a real Postgres) and,
|
|
2
|
-
// where it serves a page, an `e2e` suite (the landing page in a real browser
|
|
3
|
-
//
|
|
2
|
+
// where it serves a page, an `e2e` suite (the landing page in a real browser; `e2e-suite.ts` picks
|
|
3
|
+
// the page for an app with none). An app that predates them gains each; one it holds is its own.
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
E2E_SUITE_PATH,
|
|
7
|
-
e2eSuiteFiles,
|
|
8
|
-
LIVE_SUITE_PATH,
|
|
9
|
-
liveSuiteFiles,
|
|
10
|
-
} from '../templates/scaffold-suites';
|
|
5
|
+
import { LIVE_SUITE_PATH, liveSuiteFiles } from '../templates/scaffold-suites';
|
|
11
6
|
import type { Codemod, CodemodApp, CodemodResult } from './codemod';
|
|
7
|
+
import { e2eSuitePlan, writeE2eSuite } from './e2e-suite';
|
|
12
8
|
import { mergeResults, writeIfAbsent } from './engine-edits';
|
|
13
9
|
|
|
14
10
|
const NOTHING: CodemodResult = { changed: [], skipped: [] };
|
|
15
11
|
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
[
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
async function suite(app: CodemodApp, path: string, host: string): Promise<CodemodResult> {
|
|
23
|
-
if (!(await app.exists(host))) return NOTHING;
|
|
24
|
-
const file = [...liveSuiteFiles(), ...e2eSuiteFiles()].find((one) => one.path === path);
|
|
25
|
-
return typeof file?.contents === 'string' ? writeIfAbsent(app, path, file.contents) : NOTHING;
|
|
12
|
+
/** No db package, no live suite: it lands beside the handle it vouches for. */
|
|
13
|
+
async function liveSuite(app: CodemodApp): Promise<CodemodResult> {
|
|
14
|
+
if (!(await app.exists('packages/db/src/index.ts'))) return NOTHING;
|
|
15
|
+
const contents = liveSuiteFiles()[0]?.contents;
|
|
16
|
+
return typeof contents === 'string' ? writeIfAbsent(app, LIVE_SUITE_PATH, contents) : NOTHING;
|
|
26
17
|
}
|
|
27
18
|
|
|
28
19
|
export const codemod: Codemod = {
|
|
29
20
|
id: 'test-suites',
|
|
30
21
|
since: '31.27.0',
|
|
31
|
-
summary: `the live and e2e suites every scaffold ships: ${LIVE_SUITE_PATH} and
|
|
22
|
+
summary: `the live and e2e suites every scaffold ships: ${LIVE_SUITE_PATH} and a page's *.e2e.test.ts`,
|
|
32
23
|
async run(app) {
|
|
33
|
-
|
|
34
|
-
for (const [path, host] of HOSTS) results.push(await suite(app, path, host));
|
|
35
|
-
return mergeResults(results);
|
|
24
|
+
return mergeResults([await liveSuite(app), await writeE2eSuite(app, await e2eSuitePlan(app))]);
|
|
36
25
|
},
|
|
37
26
|
};
|
|
@@ -1,34 +1,46 @@
|
|
|
1
1
|
// 31.27.0: a scaffold's x.verify.json names every gate step — required in `steps`, or excused in
|
|
2
2
|
// `skip` with its why — and sets `strict`. An older app gains the `skip` rows that hold for it
|
|
3
|
-
// (roadmap; job with no job suite; e2e with no
|
|
3
|
+
// (roadmap; job with no job suite; e2e with no suite to run). `strict` is 31.28.0's
|
|
4
|
+
// (`verify-floor-steps`).
|
|
4
5
|
|
|
5
6
|
import { isJsonObject } from '@ultimat3/core';
|
|
6
7
|
import { floorSteps } from '../templates/scaffold-verify-floor';
|
|
7
8
|
import { VERIFY_FLOOR_FILE } from '../verify-floor';
|
|
8
9
|
import type { Codemod, CodemodApp } from './codemod';
|
|
10
|
+
import type { E2eSuitePlan } from './e2e-suite';
|
|
11
|
+
import { e2eSuitePlan, NO_STATIC_PAGE_WHY } from './e2e-suite';
|
|
9
12
|
|
|
10
13
|
export type Floor = Record<string, unknown>;
|
|
11
14
|
|
|
12
15
|
const any = async (app: CodemodApp, pattern: string): Promise<boolean> =>
|
|
13
16
|
(await app.glob(pattern)).length > 0;
|
|
14
17
|
|
|
15
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* What the app ships suites for, as `floorSteps` asks it: a job suite, and an e2e suite it holds or
|
|
20
|
+
* `test-suites` writes. Never "has a page": that required a suite no codemod had created.
|
|
21
|
+
*/
|
|
16
22
|
export async function floorSuites(
|
|
17
23
|
app: CodemodApp,
|
|
18
24
|
): Promise<{ readonly jobs: boolean; readonly pages: boolean }> {
|
|
19
25
|
return {
|
|
20
26
|
jobs: await any(app, '{apps,packages}/**/*.job.test.ts'),
|
|
21
|
-
pages: await
|
|
27
|
+
pages: (await e2eSuitePlan(app)).kind !== 'none',
|
|
22
28
|
};
|
|
23
29
|
}
|
|
24
30
|
|
|
25
|
-
/**
|
|
31
|
+
/** Why `e2e` cannot run: no page at all is the scaffold's own row, pages with no URL are not. */
|
|
32
|
+
export const e2eExcuse = (plan: E2eSuitePlan): string =>
|
|
33
|
+
plan.kind === 'none' && plan.pages
|
|
34
|
+
? NO_STATIC_PAGE_WHY
|
|
35
|
+
: String(floorSteps({ jobs: false, pages: false }).skip.e2e);
|
|
36
|
+
|
|
37
|
+
/** The steps that cannot run in this app, each with its why. */
|
|
26
38
|
async function excused(app: CodemodApp): Promise<Readonly<Record<string, string>>> {
|
|
27
39
|
const { jobs, pages } = await floorSuites(app);
|
|
28
40
|
const why = floorSteps({ jobs: false, pages: false }).skip;
|
|
29
41
|
const out: Record<string, string> = {};
|
|
30
42
|
if (!jobs && why.job !== undefined) out['job'] = why.job;
|
|
31
|
-
if (!pages
|
|
43
|
+
if (!pages) out['e2e'] = e2eExcuse(await e2eSuitePlan(app));
|
|
32
44
|
if (!(await app.exists('docs/idea/14-roadmap.md')) && why.roadmap !== undefined) {
|
|
33
45
|
out['roadmap'] = why.roadmap;
|
|
34
46
|
}
|
|
@@ -36,7 +48,7 @@ async function excused(app: CodemodApp): Promise<Readonly<Record<string, string>
|
|
|
36
48
|
}
|
|
37
49
|
|
|
38
50
|
/** `floor` with `skip` merged in, placed right after `steps` as the scaffold writes it. */
|
|
39
|
-
function withSkip(floor: Floor, skip: Readonly<Record<string, string>>): Floor {
|
|
51
|
+
export function withSkip(floor: Floor, skip: Readonly<Record<string, string>>): Floor {
|
|
40
52
|
const merged = { ...(isJsonObject(floor['skip']) ? floor['skip'] : {}), ...skip };
|
|
41
53
|
const out: Floor = {};
|
|
42
54
|
for (const [key, value] of Object.entries(floor)) {
|
|
@@ -79,6 +79,35 @@ async function lockPackages(root: string): Promise<Readonly<Record<string, unkno
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/** What `bun.lock` resolves for this row below its floor, lowest first. */
|
|
83
|
+
function versionsBelow(
|
|
84
|
+
packages: Readonly<Record<string, unknown>>,
|
|
85
|
+
row: AdvisoryFloor,
|
|
86
|
+
): readonly string[] {
|
|
87
|
+
// A git, tarball or `file:` head is no release on npm's line, so no advisory range reads it:
|
|
88
|
+
// skipped rather than ordered, because `Bun.semver.order` throws on it and would take every
|
|
89
|
+
// other `x doctor` check down with this warning.
|
|
90
|
+
return resolvedVersions(packages, row.name)
|
|
91
|
+
.filter((version) => RELEASE.test(version))
|
|
92
|
+
.filter((version) => Bun.semver.order(version, row.floor) < 0)
|
|
93
|
+
.sort((a, b) => Bun.semver.order(a, b));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The transitive packages `bun.lock` holds under a floor: `bun install` honours the lockfile, so
|
|
98
|
+
* `x upgrade` names these to `bun update`, and an upgraded app is not left to `x doctor`'s warning.
|
|
99
|
+
*/
|
|
100
|
+
export async function staleTransitiveFloors(
|
|
101
|
+
root: string,
|
|
102
|
+
floors: readonly AdvisoryFloor[] = ADVISORY_FLOORS,
|
|
103
|
+
): Promise<readonly string[]> {
|
|
104
|
+
const packages = await lockPackages(root);
|
|
105
|
+
if (packages === undefined) return [];
|
|
106
|
+
return floors
|
|
107
|
+
.filter((row) => row.via === 'transitive' && versionsBelow(packages, row).length > 0)
|
|
108
|
+
.map((row) => row.name);
|
|
109
|
+
}
|
|
110
|
+
|
|
82
111
|
/** One warning per floor the lockfile resolves under, lowest resolved version named. */
|
|
83
112
|
export async function advisoryFloorFindings(
|
|
84
113
|
root: string,
|
|
@@ -88,14 +117,7 @@ export async function advisoryFloorFindings(
|
|
|
88
117
|
if (packages === undefined) return [];
|
|
89
118
|
const transitive = floors.filter((f) => f.via === 'transitive').map((f) => quoteArg(f.name));
|
|
90
119
|
return floors.flatMap((row) => {
|
|
91
|
-
|
|
92
|
-
// skipped rather than ordered, because `Bun.semver.order` throws on it and would take every
|
|
93
|
-
// other `x doctor` check down with this warning.
|
|
94
|
-
const below = resolvedVersions(packages, row.name)
|
|
95
|
-
.filter((version) => RELEASE.test(version))
|
|
96
|
-
.filter((version) => Bun.semver.order(version, row.floor) < 0)
|
|
97
|
-
.sort((a, b) => Bun.semver.order(a, b));
|
|
98
|
-
const lowest = below[0];
|
|
120
|
+
const lowest = versionsBelow(packages, row)[0];
|
|
99
121
|
if (lowest === undefined) return [];
|
|
100
122
|
return [
|
|
101
123
|
{
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
// The X_* codes of the
|
|
2
|
-
// `x
|
|
1
|
+
// The X_* codes of the tooling commands — `x client`, `x graph` / `x impact`, `x repro`, `x explain`
|
|
2
|
+
// and `x heavy` — split out of `error-codes.ts` at its 500-line ceiling, as `error-codes-shot.ts`
|
|
3
3
|
// was. That file folds these into the CLI's one owned union and registers them in its one call.
|
|
4
4
|
|
|
5
|
-
/** Codes `x client`, `x graph`, `x impact`, `x repro` and `x
|
|
5
|
+
/** Codes `x client`, `x graph`, `x impact`, `x repro`, `x explain` and `x heavy` own. */
|
|
6
6
|
export const CLI_TOOLING_ERROR_CODES = [
|
|
7
7
|
'X_CLIENT_SPEC_UNSUPPORTED',
|
|
8
8
|
'X_CLIENT_STALE',
|
|
@@ -13,6 +13,9 @@ export const CLI_TOOLING_ERROR_CODES = [
|
|
|
13
13
|
'X_REPRO_NOT_REPRODUCED',
|
|
14
14
|
'X_REPRO_CAPTURE_OFF',
|
|
15
15
|
'X_EXPLAIN_PRODUCTION_DATABASE',
|
|
16
|
+
'X_HEAVY_LEDGER_UNAVAILABLE',
|
|
17
|
+
'X_HEAVY_MEMORY_INVALID',
|
|
18
|
+
'X_HEAVY_COMMAND_NOT_FOUND',
|
|
16
19
|
] as const;
|
|
17
20
|
|
|
18
21
|
export type CliToolingErrorCode = (typeof CLI_TOOLING_ERROR_CODES)[number];
|
|
@@ -28,4 +31,7 @@ export const CLI_TOOLING_ERROR_TITLES: Readonly<Record<CliToolingErrorCode, stri
|
|
|
28
31
|
X_REPRO_NOT_REPRODUCED: 'a written repro test did not fail the way the original did',
|
|
29
32
|
X_REPRO_CAPTURE_OFF: 'x repro was given a request id while failure capture is off',
|
|
30
33
|
X_EXPLAIN_PRODUCTION_DATABASE: 'x explain refused to run on a production database',
|
|
34
|
+
X_HEAVY_LEDGER_UNAVAILABLE: 'the machine memory ledger cannot be used here',
|
|
35
|
+
X_HEAVY_MEMORY_INVALID: 'ULTIMATE_HEAVY_MEMORY is not a size',
|
|
36
|
+
X_HEAVY_COMMAND_NOT_FOUND: 'x heavy run was given a command that is not on PATH',
|
|
31
37
|
};
|
package/src/exec.ts
CHANGED
|
@@ -32,6 +32,11 @@ export interface ExecOptions {
|
|
|
32
32
|
*/
|
|
33
33
|
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
34
34
|
readonly stdin?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Descriptors the child inherits, from fd 3 up: the machine ledger's locks (`heavy-ledger.ts`),
|
|
37
|
+
* which must outlive this process when it is killed and the child is not.
|
|
38
|
+
*/
|
|
39
|
+
readonly inheritFds?: readonly number[];
|
|
35
40
|
/**
|
|
36
41
|
* Told the child's pid the moment it exists. A gate step's deadline records it here
|
|
37
42
|
* (`verify-deadline.ts`): on Windows no process's environment is readable, so the tag sweep
|
|
@@ -91,9 +96,12 @@ function spawnOrRefuse(command: readonly string[], options: ExecOptions) {
|
|
|
91
96
|
return Bun.spawn([head, ...rest], {
|
|
92
97
|
cwd: options.cwd,
|
|
93
98
|
env: options.env === undefined ? Bun.env : mergedEnv(options.env),
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
99
|
+
stdio: [
|
|
100
|
+
options.stdin === undefined ? 'ignore' : new TextEncoder().encode(options.stdin),
|
|
101
|
+
'pipe',
|
|
102
|
+
'pipe',
|
|
103
|
+
...(options.inheritFds ?? []),
|
|
104
|
+
],
|
|
97
105
|
});
|
|
98
106
|
} catch (error) {
|
|
99
107
|
throw new UltimateError({
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// How many cores a local run may take: every one in CI, at most `LOCAL_CORE_CAP` on a shared box, and
|
|
2
|
+
// fewer while other sessions' runnable tasks already fill the machine. Oversubscribed cores do not
|
|
3
|
+
// finish work sooner; they turn timing-sensitive tests red.
|
|
4
|
+
|
|
5
|
+
// why: Bun ships no synchronous file read for `/proc/loadavg`.
|
|
6
|
+
import { readFileSync } from 'node:fs';
|
|
7
|
+
// why: Bun exposes no available-parallelism primitive.
|
|
8
|
+
import { availableParallelism } from 'node:os';
|
|
9
|
+
import type { HeavyEnv } from './heavy-env';
|
|
10
|
+
import { HEAVY_CORES_ENV, isCi } from './heavy-env';
|
|
11
|
+
|
|
12
|
+
export const LOCAL_CORE_CAP = 4;
|
|
13
|
+
/** A busy machine still gets this much, or one run behind a burst never makes progress. */
|
|
14
|
+
export const BUSY_CORE_FLOOR = 2;
|
|
15
|
+
|
|
16
|
+
/** The instantaneous runnable count: `/proc/loadavg`'s `running/total`. The load average lags. */
|
|
17
|
+
export function parseRunnable(loadavg: string): number | undefined {
|
|
18
|
+
const match = /^\S+\s+\S+\s+\S+\s+(\d+)\/\d+\s/.exec(loadavg);
|
|
19
|
+
return match === null ? undefined : Number(match[1]);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Runnable tasks now, this reader excluded; 0 where the kernel does not report them. */
|
|
23
|
+
export function runnableTasks(read: () => string = () => readFileSync('/proc/loadavg', 'utf8')) {
|
|
24
|
+
try {
|
|
25
|
+
return Math.max(0, (parseRunnable(read()) ?? 1) - 1);
|
|
26
|
+
} catch {
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** `undefined` means no ceiling: a CI runner, whose every core is this run's. */
|
|
32
|
+
export function coreCap(
|
|
33
|
+
env: HeavyEnv,
|
|
34
|
+
cores: number = availableParallelism(),
|
|
35
|
+
runnable: number = runnableTasks(),
|
|
36
|
+
): number | undefined {
|
|
37
|
+
const raw = Number(env[HEAVY_CORES_ENV]);
|
|
38
|
+
if (Number.isInteger(raw) && raw > 0) return raw;
|
|
39
|
+
if (isCi(env)) return undefined;
|
|
40
|
+
const ceiling = Math.min(LOCAL_CORE_CAP, Math.max(1, cores));
|
|
41
|
+
return Math.min(ceiling, Math.max(BUSY_CORE_FLOOR, cores - runnable));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Thread caps for the native tools: tsc is Go, biome is rayon. An exported value wins. */
|
|
45
|
+
export function nativeToolEnv(env: HeavyEnv, cap: number | undefined): Record<string, string> {
|
|
46
|
+
if (cap === undefined) return {};
|
|
47
|
+
return {
|
|
48
|
+
...(env['GOMAXPROCS'] === undefined ? { GOMAXPROCS: String(cap) } : {}),
|
|
49
|
+
...(env['RAYON_NUM_THREADS'] === undefined ? { RAYON_NUM_THREADS: String(cap) } : {}),
|
|
50
|
+
};
|
|
51
|
+
}
|
package/src/heavy-env.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Every environment variable the machine ledger reads or sets, and the one reading of `CI`. A leaf:
|
|
2
|
+
// the worker planner and the error classes need these names without loading the ledger.
|
|
3
|
+
|
|
4
|
+
export type HeavyEnv = Readonly<Record<string, string | undefined>>;
|
|
5
|
+
|
|
6
|
+
/** The ledger's directory. Set by a test or an unusual machine; also opts a test run back in. */
|
|
7
|
+
export const HEAVY_DIR_ENV = 'ULTIMATE_HEAVY_DIR';
|
|
8
|
+
/** What the ledger admits in total (`8g`), replacing half of RAM. `0` turns it off. */
|
|
9
|
+
export const HEAVY_MEMORY_ENV = 'ULTIMATE_HEAVY_MEMORY';
|
|
10
|
+
/** The local core ceiling, replacing `LOCAL_CORE_CAP`. */
|
|
11
|
+
export const HEAVY_CORES_ENV = 'ULTIMATE_HEAVY_CORES';
|
|
12
|
+
/** Set on a holder's children: the reservation file they run inside. A protocol, not a knob. */
|
|
13
|
+
export const HEAVY_HOLD_ENV = 'ULTIMATE_HEAVY_HOLD';
|
|
14
|
+
|
|
15
|
+
/** A CI runner is single-tenant: nothing to queue behind, and every core is the run's. */
|
|
16
|
+
export const isCi = (env: HeavyEnv): boolean => {
|
|
17
|
+
const value = env['CI'];
|
|
18
|
+
return value !== undefined && value !== '' && value !== '0' && value !== 'false';
|
|
19
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// The failures of the machine memory ledger. Admission is a courtesy between processes, so the two
|
|
2
|
+
// environment failures are reported and the work goes ahead; only `x heavy run` refuses anything.
|
|
3
|
+
|
|
4
|
+
import { UltimateError } from '@ultimat3/core';
|
|
5
|
+
import { HEAVY_MEMORY_ENV } from './heavy-env';
|
|
6
|
+
import { quoteArg } from './shell-quote';
|
|
7
|
+
|
|
8
|
+
export class HeavyLedgerUnavailableError extends UltimateError {
|
|
9
|
+
constructor(reason: string) {
|
|
10
|
+
super({
|
|
11
|
+
code: 'X_HEAVY_LEDGER_UNAVAILABLE',
|
|
12
|
+
cause: `the machine memory ledger cannot be used: ${reason}; this run continues unadmitted`,
|
|
13
|
+
fix: 'x heavy status --json',
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class HeavyMemoryInvalidError extends UltimateError {
|
|
19
|
+
constructor(raw: string) {
|
|
20
|
+
super({
|
|
21
|
+
code: 'X_HEAVY_MEMORY_INVALID',
|
|
22
|
+
cause: `${HEAVY_MEMORY_ENV}=${JSON.stringify(raw)} is not a byte size`,
|
|
23
|
+
fix: 'export ULTIMATE_HEAVY_MEMORY=8g # or 0 to turn the ledger off',
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class HeavyCommandNotFoundError extends UltimateError {
|
|
29
|
+
constructor(command: string) {
|
|
30
|
+
super({
|
|
31
|
+
code: 'X_HEAVY_COMMAND_NOT_FOUND',
|
|
32
|
+
cause: `${JSON.stringify(command)} is not an executable on PATH`,
|
|
33
|
+
fix: `bun --print ${quoteArg(`Bun.which(${JSON.stringify(command)})`)} # null: install it or correct the name, then: x heavy run -- <command>`,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// flock(2) through `bun:ffi`: the one primitive the ledger stands on. The kernel drops the lock when
|
|
2
|
+
// the last descriptor closes, which includes the holder dying by any signal, so "the holder died"
|
|
3
|
+
// and "the lock is free" are one event and nothing has to detect a stale file.
|
|
4
|
+
//
|
|
5
|
+
// WINDOWS has no flock, and a Node descriptor there is not the HANDLE `LockFileEx` wants, so the
|
|
6
|
+
// ledger is OFF on Windows: `x heavy status` says so and the core cap still applies. An honest gap
|
|
7
|
+
// beats a pid file that reintroduces the stale-lock race this module exists to remove.
|
|
8
|
+
|
|
9
|
+
import { HeavyLedgerUnavailableError } from './heavy-errors';
|
|
10
|
+
|
|
11
|
+
const LOCK_EX = 2;
|
|
12
|
+
const LOCK_NB = 4;
|
|
13
|
+
const EINTR = 4;
|
|
14
|
+
|
|
15
|
+
const LIBC: Readonly<Record<string, { libs: readonly string[]; errno: string; busy: number }>> = {
|
|
16
|
+
// glibc first; the two musl names are Alpine's, the base of many app images.
|
|
17
|
+
linux: {
|
|
18
|
+
libs: ['libc.so.6', 'libc.musl-x86_64.so.1', 'libc.musl-aarch64.so.1'],
|
|
19
|
+
errno: '__errno_location',
|
|
20
|
+
busy: 11,
|
|
21
|
+
},
|
|
22
|
+
darwin: { libs: ['libSystem.B.dylib'], errno: '__error', busy: 35 },
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** True when this descriptor now holds the lock, false when another one does. */
|
|
26
|
+
export type TryLock = (fd: number) => boolean;
|
|
27
|
+
|
|
28
|
+
export type FlockBinding =
|
|
29
|
+
| { readonly ok: true; readonly tryLock: TryLock }
|
|
30
|
+
| { readonly ok: false; readonly reason: string };
|
|
31
|
+
|
|
32
|
+
const bound = new Map<string, Promise<FlockBinding>>();
|
|
33
|
+
|
|
34
|
+
async function bind(platform: string): Promise<FlockBinding> {
|
|
35
|
+
const spec = Object.hasOwn(LIBC, platform) ? LIBC[platform] : undefined;
|
|
36
|
+
if (spec === undefined) return { ok: false, reason: `flock(2) does not exist on ${platform}` };
|
|
37
|
+
// Imported here so a command that never reaches the ledger never loads FFI.
|
|
38
|
+
const { dlopen, FFIType, read } = await import('bun:ffi');
|
|
39
|
+
for (const lib of spec.libs) {
|
|
40
|
+
try {
|
|
41
|
+
const { symbols } = dlopen(lib, {
|
|
42
|
+
flock: { args: [FFIType.i32, FFIType.i32], returns: FFIType.i32 },
|
|
43
|
+
[spec.errno]: { args: [], returns: FFIType.ptr },
|
|
44
|
+
});
|
|
45
|
+
const errnoAt = symbols[spec.errno] as unknown as () => Parameters<typeof read.i32>[0];
|
|
46
|
+
const flock = symbols.flock as unknown as (fd: number, operation: number) => number;
|
|
47
|
+
return {
|
|
48
|
+
ok: true,
|
|
49
|
+
tryLock: (fd) => {
|
|
50
|
+
if (flock(fd, LOCK_EX | LOCK_NB) === 0) return true;
|
|
51
|
+
const errno = read.i32(errnoAt(), 0);
|
|
52
|
+
if (errno === spec.busy || errno === EINTR) return false;
|
|
53
|
+
// Reading this as "busy" would wait forever on a filesystem that cannot lock at all.
|
|
54
|
+
throw new HeavyLedgerUnavailableError(
|
|
55
|
+
`flock(2) failed with errno ${String(errno)}, which is not contention`,
|
|
56
|
+
);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
} catch {
|
|
60
|
+
// Not this libc: the next name, and "no libc" below when none loads.
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { ok: false, reason: `no libc with flock(2) loaded (${spec.libs.join(', ')})` };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The process's one binding, loaded on first use. */
|
|
67
|
+
export const flockBinding = (platform: string = process.platform): Promise<FlockBinding> => {
|
|
68
|
+
const known = bound.get(platform);
|
|
69
|
+
if (known !== undefined) return known;
|
|
70
|
+
const binding = bind(platform);
|
|
71
|
+
bound.set(platform, binding);
|
|
72
|
+
return binding;
|
|
73
|
+
};
|