@nodii/dev 0.6.130 → 0.6.132
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/README.md +34 -2
- package/dist/cli.js +22 -5
- package/dist/core.js +1 -1
- package/dist/flags.js +1 -0
- package/dist/heavy-slot.sh +206 -32
- package/dist/heavy.js +36 -8
- package/dist/impact.js +470 -0
- package/dist/lanes.js +3 -0
- package/dist/repo-policy.js +5 -2
- package/dist/review.js +54 -4
- package/dist/surface.js +207 -32
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -89,20 +89,46 @@ checks the local stack.
|
|
|
89
89
|
Every command accepts `--json`; without it, output is formatted for humans.
|
|
90
90
|
Dev failures print Dev's error code to stderr and exit non-zero.
|
|
91
91
|
|
|
92
|
+
## Tests: only what changed (hub-401)
|
|
93
|
+
|
|
94
|
+
A lane, a pre-push hook and per-push CI run only the tests the change reaches. The full suite runs in the twice-daily scheduled CI and the release gate.
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
nodii-dev test affected # what changed against origin/main, the affected tests, and the command per package
|
|
98
|
+
nodii-dev test affected --run # run them, in a light slot when at most 20 test files are affected
|
|
99
|
+
nodii-dev lane check KEY --gate 'pnpm typecheck' --gate 'pnpm lint' --gate 'nodii-dev test affected --run'
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- The change is the worktree against its merge base with `--base` (default `origin/main`), with uncommitted, untracked and deleted files.
|
|
103
|
+
- The graph is read from source: static and dynamic imports, re-exports, `require`, `vi.mock`, `jest.mock` and `mock.module`, `new URL("./x", import.meta.url)`, the nearest tsconfig's `paths`, and workspace packages by name. A data file (SQL, JSON, a template) reaches the code that names it.
|
|
104
|
+
- It fails closed. A manifest, lockfile, tsconfig, test or build config, a data file no source names, or a changed source file no test reaches selects the full suite (`full` lists why). Docs, `.github/` and the policy file select nothing.
|
|
105
|
+
- Each package runs its tests with its own `test` script's runner and flags, and the selected files replace the script's paths. A script that is not one runner call (`bun run gate && bun test`) runs whole.
|
|
106
|
+
- `lane check` refuses a full-suite gate (`GATE_FULL_SUITE`) unless `--full-suite` is passed.
|
|
107
|
+
|
|
92
108
|
## Heavy work: one machine-wide slot queue
|
|
93
109
|
|
|
94
110
|
Every heavy command a lane runs waits for one machine-wide heavy slot (MW2-51, K02-4; hub-377): typechecks, full and affected test suites, builds, gates, `git push` (its pre-push hooks run the gates), image builds and a detached `docker compose up`.
|
|
95
111
|
|
|
96
112
|
```sh
|
|
97
113
|
nodii-dev heavy run -- pnpm -r typecheck
|
|
114
|
+
nodii-dev heavy run --size light -- bun test tests/unit/money.test.ts
|
|
98
115
|
nodii-dev heavy run -- git push -u origin HEAD
|
|
99
116
|
nodii-dev heavy status
|
|
100
117
|
```
|
|
101
118
|
|
|
102
|
-
-
|
|
103
|
-
-
|
|
119
|
+
- Slots are sized (v27, hub-401). A light job takes one of `LIGHT_SLOTS` light slots beside the heavy ones, under `LIGHT_MIN_AVAIL_GB` (default: the heavy floor) and with no stagger. The machine's measured limits file sizes the pool (K02-4): without `LIGHT_SLOTS` there is no light pool and every job takes a heavy slot. A light job is a test run naming at most `LIGHT_MAX_TEST_FILES` (20) test files (`bun test a.test.ts`, `vitest run x.spec.ts`, optionally piped to tail, head, grep or tee), as `test affected --run` sends for a small change. `--size light` or `HEAVY_SIZE=light` on anything else is refused and takes a heavy slot. Everything else, and every landing, takes a heavy slot. The floor's one-job-alone exception counts both pools, so a light job beside running heavy jobs waits for its floor. Light locks live in `/tmp/nodii-heavy-slots/light`, which older copies never touch.
|
|
120
|
+
- The queue is `heavy-slot` v27, shipped in this package (`src/heavy-slot.sh`, run by bash 3.2 or later). It carries v23's rules: a slot cap, a memory floor, a stagger between admissions, a landing reserve that only a push may use, FIFO order, reclaiming dead holders, and pass-through for teardowns (`compose down`, `stack:down`), a single `pnpm validate:<name>`, and a command that already holds a slot (K02-6). It refuses dev servers (K02-7). A foreground `docker compose up` never exits, so it never takes a slot either.
|
|
121
|
+
- Limits come from `~/.config/nodii-resources/limits.env`, re-read on every admission: `HEAVY_SLOTS`, `HEAVY_MIN_AVAIL_GB`, `HEAVY_STAGGER_SEC`, `HEAVY_LANDING_RESERVE`, `HEAVY_RECLAIM_MIN`, `LIGHT_SLOTS`, `LIGHT_MIN_AVAIL_GB` and `LIGHT_MAX_TEST_FILES`. The script header (`src/heavy-slot.sh`) gives the schema and the defaults. Locks live in `/tmp/nodii-heavy-slots`, the same directory v23 copies use, so every copy counts against the same slots.
|
|
104
122
|
- When no heavy job holds a slot, waiting can free no memory, so one job is admitted even under the floor, with a warning. The floor still holds every job beside it.
|
|
105
123
|
- A waiting job prints its queue place and its wait when it starts waiting and once a minute after. It never gives up, so the queue never times out a gate.
|
|
124
|
+
- **No nested queuing (NODII-DEV-CLI-86).** A job that holds a slot sees `NODII_HEAVY_SLOT=<n>` (`light/<n>` for a light slot). Heavy work inside it (another `heavy run`, `lane check`'s gates, a reviewer) runs in that same slot and never queues for a second one. A marker naming a slot that is no longer held queues for a slot of its own.
|
|
125
|
+
- **Orphaned slots are reclaimed (NODII-DEV-CLI-86).** A slot records its lane, the agent session that took it, the job's worktree and its process group. A slot's holder (the `heavy run` that started the job) checks its own slot once a minute, and reclaims it only when all of these hold:
|
|
126
|
+
- it has been held at least `HEAVY_RECLAIM_MIN` minutes (default 10; 0 turns the reclaim off);
|
|
127
|
+
- Dev reports its lane down (`nodii-dev heavy lane-state <key>`: liveness `orphan` or `ended`, or status `stopped` or `non_issue`);
|
|
128
|
+
- the agent session that took it is gone;
|
|
129
|
+
- no `claude` or `codex` process works in the job's worktree.
|
|
130
|
+
|
|
131
|
+
It then stops its own job's process tree and logs the reclaim (slot, lane, time held and why) to `/tmp/nodii-heavy-slots/reclaim.log`. `heavy status` shows each slot's lane, marks a reclaim candidate and lists the last reclaims. No session ever signals another session's processes (MW2-49): a waiter only waits. A slot taken outside a lane, or held by a pre-v26 copy, is never reclaimed this way; a check that can't tell (Dev unreachable, no `lsof`) keeps the slot.
|
|
106
132
|
- It can't be skipped. In a lane, the safety hook (`hook safety-pre`) refuses an unqueued heavy command and prints the same command with `nodii-dev heavy run --` in front of each heavy part. `lane check --gate` and `receipt write --run` queue every gate themselves, `lane check` runs each reviewer under a slot (its timeout starts once the slot is granted), and `lane closeout` pushes through the landing reserve.
|
|
107
133
|
- `[TOOL GAP hub-86]` MW5-19 places this helper as the `heavy-slot` util. It ships here until the util pipeline exists.
|
|
108
134
|
|
|
@@ -940,6 +966,12 @@ product-page catalog names the Surface by its published component-set key
|
|
|
940
966
|
`surface generate --all --check` with a pinned `@nodii/dev`; `--all`
|
|
941
967
|
without `--fetch` covers the Surfaces that have a committed tree or a
|
|
942
968
|
generated file and lists the rest as not generated yet.
|
|
969
|
+
- **Generated pages with no catalog row.** The catalog is the one lookup
|
|
970
|
+
(MW8-17.1), so a committed `*.surface.generated.tsx` that no catalog row
|
|
971
|
+
names can't be regenerated or compared. `--all` names each one (`--json`:
|
|
972
|
+
`data.generatedWithoutCatalogRow`, narrowed by `--module`) and leaves the
|
|
973
|
+
exit code alone. Its component-set key answers `SURFACE_NOT_IN_CATALOG` with
|
|
974
|
+
the file's path.
|
|
943
975
|
- **Unpublished Surfaces.** `--all --fetch` skips a Surface whose component set
|
|
944
976
|
is not among its file's published sets, names it as not published (`--json`:
|
|
945
977
|
`data.unpublished`), and still generates and scores the rest. A Surface named
|
package/dist/cli.js
CHANGED
|
@@ -24,7 +24,8 @@ import {
|
|
|
24
24
|
import { FIGMA_HOOK_DENIALS, heartbeatFromHook, postHook, postHookFailure, preHook, settingsSnippet, workflowSuggestion } from "./hooks.js";
|
|
25
25
|
import { setupCommand } from "./setup.js";
|
|
26
26
|
import { harnessCommand } from "./harness.js";
|
|
27
|
-
import { repoCommand } from "./repo-policy.js";
|
|
27
|
+
import { readPolicy, repoCommand } from "./repo-policy.js";
|
|
28
|
+
import { testCommand } from "./impact.js";
|
|
28
29
|
import { hookRuntime, recordHookFire, safetyContext, safetyPostHook, safetyPreHook } from "./safety.js";
|
|
29
30
|
import { isLaneRunSession, isReviewSession } from "./review-session.js";
|
|
30
31
|
import { SUBMIT_CHECK_FLAGS, changedPaths, nudgeCandidate, nudgePre, nudgeStop, recordGathered, recordGuidelineRead, recordSubmitChecks, steerAct, stewardSteerNudges, surfaceGenPre } from "./nudges.js";
|
|
@@ -368,13 +369,28 @@ Usage: nodii-dev <command> [options] [--json]
|
|
|
368
369
|
[--run CMD ...] [--from-file DETAILS.json] [--out FILE]
|
|
369
370
|
# one signed receipt (STD-22); --run gates stop at the first failure and exit 1
|
|
370
371
|
receipt verify <file> [--commit SHA] # the signature, and that it names this commit (S06-55)
|
|
371
|
-
|
|
372
|
+
test affected [--base REF] [--max-light N] [--run]
|
|
373
|
+
# hub-401: only the tests the change reaches (MW2-20): the import
|
|
374
|
+
# graph from the files changed against origin/main (uncommitted and
|
|
375
|
+
# untracked included) back to the test files that import them, run with
|
|
376
|
+
# each package's own runner. A manifest, lockfile, test or build config, or
|
|
377
|
+
# a data file nothing names selects the full suite. --run runs them in a
|
|
378
|
+
# light slot (where the machine has a light pool) when at most --max-light (20) files are affected, else heavy
|
|
379
|
+
heavy run [--size light|heavy] -- <command...>
|
|
380
|
+
# run heavy work (tests, typecheck, build, gate, git push, image build,
|
|
372
381
|
# detached stack start) under one machine-wide slot (MW2-51): waits in FIFO
|
|
373
382
|
# order, printing its place and wait, while slots are full, memory is under
|
|
374
383
|
# the floor or the stagger runs; never times out. Limits: ~/.config/
|
|
375
|
-
# nodii-resources/limits.env. A push uses the landing reserve
|
|
376
|
-
#
|
|
377
|
-
|
|
384
|
+
# nodii-resources/limits.env. A push uses the landing reserve. The job sees
|
|
385
|
+
# NODII_HEAVY_SLOT=<n>; heavy work inside it (a heavy run, lane check's gates)
|
|
386
|
+
# runs in that slot and never queues for a second (K02-6). A slot held
|
|
387
|
+
# HEAVY_RECLAIM_MIN (10) whose lane is down in Dev and whose agent is gone is
|
|
388
|
+
# reclaimed and logged. A lane's safety hook refuses unqueued heavy work.
|
|
389
|
+
# A test run naming at most LIGHT_MAX_TEST_FILES test files takes a light
|
|
390
|
+
# slot (many in parallel, low memory floor, no stagger); --size light asks for one
|
|
391
|
+
heavy status # limits, available memory, the slots held (with lane and reclaim candidates)
|
|
392
|
+
# and the queues
|
|
393
|
+
heavy lane-state <lane-key> # up|down|unknown: Dev's view of a slot's lane, as the reclaim reads it
|
|
378
394
|
sync pending [--discard FILE --reason TEXT]
|
|
379
395
|
# send the lane records queued while Dev was down (MW1-64); --discard drops
|
|
380
396
|
# one Dev refuses for good, with a note on its lane
|
|
@@ -947,6 +963,7 @@ export async function main(argv = process.argv.slice(2)) {
|
|
|
947
963
|
return emitCommand(await harnessCommand(action, f, { installManaged: installManagedLatest }));
|
|
948
964
|
}
|
|
949
965
|
if (command === "repo" && ["policy", "lint"].includes(action)) return emitCommand(await repoCommand(action, rest, f));
|
|
966
|
+
if (command === "test") return emitCommand(await testCommand(action, rest, f, { readPolicy }));
|
|
950
967
|
if (command === "watchdog") {
|
|
951
968
|
if (rest.length) throw new CliError("USAGE", `watchdog ${action ?? ""} takes no arguments, got: ${rest.join(" ")}.`, 2);
|
|
952
969
|
return emitCommand(await watchdogCommand(action, f));
|
package/dist/core.js
CHANGED
|
@@ -4,7 +4,7 @@ import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import { homedir, platform } from "node:os";
|
|
5
5
|
import { dirname, join } from "node:path";
|
|
6
6
|
|
|
7
|
-
export const VERSION = "0.6.
|
|
7
|
+
export const VERSION = "0.6.132";
|
|
8
8
|
export const configPath = () =>
|
|
9
9
|
process.env.NODII_DEV_CONFIG_PATH ??
|
|
10
10
|
join(homedir(), ".config/nodii-dev/config.json");
|
package/dist/flags.js
CHANGED
|
@@ -55,6 +55,7 @@ export const COMMAND_FLAGS = {
|
|
|
55
55
|
"lane land": [],
|
|
56
56
|
"lane verify": ["dev-check", "checks"],
|
|
57
57
|
"repo policy": ["root"],
|
|
58
|
+
"test affected": ["base", "max-light", "run"],
|
|
58
59
|
"repo lint": ["root"],
|
|
59
60
|
"lane review": ["file", "not-required", "decision", "question", "quote", "at", "treat", "ruling"],
|
|
60
61
|
"lane non-issue": ["reason", "reroute", "evidence", "commits", "branch"],
|
package/dist/heavy-slot.sh
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
|
-
# heavy-slot (
|
|
2
|
+
# heavy-slot (v27, shipped with @nodii/dev): run a memory-heavy job (typecheck, build, full or affected test suite,
|
|
3
3
|
# a gate, `git push` and its pre-push hooks, an image build, a detached stack start) only when a machine-wide slot
|
|
4
4
|
# is free AND enough REAL memory is available. Every session and lane on this machine shares one queue (MW2-51, K02-4).
|
|
5
5
|
#
|
|
6
|
-
# nodii-dev heavy run -- <command...>
|
|
6
|
+
# nodii-dev heavy run [--size light|heavy] -- <command...>
|
|
7
|
+
# wait for a slot of that size, run the command, release the slot when it exits
|
|
7
8
|
# nodii-dev heavy status the limits, available memory, the slots held and the queue
|
|
8
9
|
#
|
|
10
|
+
# v27 (13 Nov, hub-401): slots are sized to the job. A LIGHT job (a test run that names at most
|
|
11
|
+
# LIGHT_MAX_TEST_FILES test files, or anything run with --size light / HEAVY_SIZE=light, as
|
|
12
|
+
# `nodii-dev test affected --run` does for a small change) takes a light slot: many run in parallel, under a low
|
|
13
|
+
# memory floor and with no stagger. Everything else takes a heavy slot, as before. Light slots are lock dirs under
|
|
14
|
+
# $D/light/<n> with their own queue $D/lq, so a v24/v25 copy still running sees only the heavy slots it knows.
|
|
15
|
+
#
|
|
9
16
|
# [TOOL GAP hub-86] MW5-19 places this helper as the `heavy-slot` util (MW5-26: the first util to publish).
|
|
10
17
|
# Until the util pipeline (`nodii-dev util run`, ~/.nodii/utils) exists, @nodii/dev ships it and its hooks enforce it
|
|
11
18
|
# (hub-377). It carries heavy-slot.sh v23 (nodii-hr-service docs/figma-ux-drive/dev/heavy-slot.sh) and uses
|
|
@@ -18,9 +25,19 @@
|
|
|
18
25
|
# `memory_pressure`'s "free percentage" is NOT used: it read 84% while far less was available.
|
|
19
26
|
# HEAVY_STAGGER_SEC=60 minimum gap between two admissions, so a job's memory growth shows before the next one starts
|
|
20
27
|
# HEAVY_LANDING_RESERVE=1 slots only a landing (`git push`, or anything under HEAVY_LANDING=1) may use
|
|
21
|
-
#
|
|
28
|
+
# HEAVY_RECLAIM_MIN=10 reclaim a slot held at least this long whose lane is down and whose agent is gone (0: never)
|
|
29
|
+
# LIGHT_SLOTS= concurrent light jobs machine-wide (v27); they do not count against HEAVY_SLOTS. Unset or
|
|
30
|
+
# 0: no light pool, every job takes a heavy slot. Only the machine's measured limits set it (K02-4).
|
|
31
|
+
# LIGHT_MIN_AVAIL_GB= admit a light job only when available memory >= this (v27); unset: HEAVY_MIN_AVAIL_GB
|
|
32
|
+
# LIGHT_MAX_TEST_FILES=20 a test run naming at most this many test files is light without --size (v27)
|
|
33
|
+
# Environment: NODII_RESOURCE_LIMITS (the limits file), HEAVY_SLOT_DIR (the lock directory), HEAVY_SLOT_POLL_SEC (10),
|
|
34
|
+
# HEAVY_LANE_PROBE (a command printing `up|down|unknown <why>` for a lane key; default `nodii-dev heavy lane-state`),
|
|
35
|
+
# HEAVY_RECLAIM_POLL_SEC (60: how often a holder checks whether its own slot is an orphan).
|
|
22
36
|
#
|
|
23
37
|
# Locks are mkdir dirs under /tmp/nodii-heavy-slots/<n> holding the owner pid; stale locks (dead pid) are reclaimed.
|
|
38
|
+
# v26 (NODII-DEV-CLI-86): a slot also records when it was granted (since), the lane (lane), the agent session that
|
|
39
|
+
# asked (session, session-start), the job's worktree (worktree) and the job's process group (job). The job sees
|
|
40
|
+
# NODII_HEAVY_SLOT=<n>; a heavy-slot inside it runs in that slot. Reclaims are logged to <lock dir>/reclaim.log.
|
|
24
41
|
# The whole body is one function, called on the last line: bash then parses the entire file before running any of it,
|
|
25
42
|
# so overwriting this file while copies are running cannot splice old and new code (15 Oct: an in-place cp of v6
|
|
26
43
|
# over a running v5 produced 'others: unbound variable'). Runs on the bash macOS ships (3.2) (MW5-22).
|
|
@@ -28,20 +45,31 @@ main() {
|
|
|
28
45
|
set -u
|
|
29
46
|
LIMITS="${NODII_RESOURCE_LIMITS:-$HOME/.config/nodii-resources/limits.env}"
|
|
30
47
|
D="${HEAVY_SLOT_DIR:-/tmp/nodii-heavy-slots}"
|
|
48
|
+
# The pool this job takes a slot from: $D for heavy (where every older copy looks), $D/light for light (v27).
|
|
49
|
+
SD="$D"
|
|
31
50
|
POLL="${HEAVY_SLOT_POLL_SEC:-10}"; case "$POLL" in ''|*[!0-9]*|0) POLL=10;; esac
|
|
32
51
|
IS_LANDING=0
|
|
52
|
+
SIZE=heavy
|
|
33
53
|
load_limits() {
|
|
34
|
-
SLOTS=2; MIN_GB=16; STAGGER=60; RESERVE=1
|
|
35
|
-
HEAVY_SLOTS=""; HEAVY_MIN_AVAIL_GB=""; HEAVY_STAGGER_SEC=""; HEAVY_LANDING_RESERVE=""
|
|
54
|
+
SLOTS=2; MIN_GB=16; STAGGER=60; RESERVE=1; RECLAIM_MIN=10; LSLOTS=0; LMIN_GB=""; LMAX=20
|
|
55
|
+
HEAVY_SLOTS=""; HEAVY_MIN_AVAIL_GB=""; HEAVY_STAGGER_SEC=""; HEAVY_LANDING_RESERVE=""; HEAVY_RECLAIM_MIN=""
|
|
56
|
+
LIGHT_SLOTS=""; LIGHT_MIN_AVAIL_GB=""; LIGHT_MAX_TEST_FILES=""
|
|
36
57
|
if [ -r "$LIMITS" ]; then
|
|
37
58
|
# shellcheck disable=SC1090
|
|
38
59
|
. "$LIMITS"
|
|
39
60
|
fi
|
|
40
61
|
SLOTS="${HEAVY_SLOTS:-$SLOTS}"; MIN_GB="${HEAVY_MIN_AVAIL_GB:-$MIN_GB}"; STAGGER="${HEAVY_STAGGER_SEC:-$STAGGER}"
|
|
41
|
-
RESERVE="${HEAVY_LANDING_RESERVE:-$RESERVE}"
|
|
62
|
+
RESERVE="${HEAVY_LANDING_RESERVE:-$RESERVE}"; RECLAIM_MIN="${HEAVY_RECLAIM_MIN:-$RECLAIM_MIN}"
|
|
63
|
+
LSLOTS="${LIGHT_SLOTS:-$LSLOTS}"; LMIN_GB="${LIGHT_MIN_AVAIL_GB:-$LMIN_GB}"; LMAX="${LIGHT_MAX_TEST_FILES:-$LMAX}"
|
|
42
64
|
# A garbled limit must never break the arithmetic below (an arithmetic error aborts the wait loop).
|
|
43
65
|
case "$SLOTS" in ''|*[!0-9]*|0) SLOTS=2;; esac; case "$MIN_GB" in ''|*[!0-9]*) MIN_GB=16;; esac
|
|
44
66
|
case "$STAGGER" in ''|*[!0-9]*) STAGGER=60;; esac; case "$RESERVE" in ''|*[!0-9]*) RESERVE=1;; esac
|
|
67
|
+
case "$RECLAIM_MIN" in ''|*[!0-9]*) RECLAIM_MIN=10;; esac
|
|
68
|
+
# v27: no light pool unless the limits file sizes one, and its floor is the heavy floor unless the file sets its own.
|
|
69
|
+
case "$LSLOTS" in ''|*[!0-9]*) LSLOTS=0;; esac; case "$LMIN_GB" in ''|*[!0-9]*) LMIN_GB=$MIN_GB;; esac
|
|
70
|
+
case "$LMAX" in ''|*[!0-9]*) LMAX=20;; esac
|
|
71
|
+
# v27: a light job uses the light pool's own cap and floor, with no stagger and no landing reserve.
|
|
72
|
+
if [ "$SIZE" = light ]; then SLOTS=$LSLOTS; MIN_GB=$LMIN_GB; STAGGER=0; RESERVE=0; fi
|
|
45
73
|
# v15 (24 Oct): landings get a reserved slot. A `git push` (whose pre-push runs the gates) stuck behind
|
|
46
74
|
# typechecks/tests blocks the whole fleet (HR's FE landings queued ~70 min, 10:30Z). Non-landing jobs may use
|
|
47
75
|
# only slots 1..SLOTS-RESERVE.
|
|
@@ -67,7 +95,7 @@ mtime() { local m; m=$(stat -f %m "$1" 2>/dev/null) || m=$(stat -c %Y "$1" 2>/de
|
|
|
67
95
|
since_last_admit() {
|
|
68
96
|
# Any missing/empty/garbled stamp means "long ago". Never feed an empty string to $(( )): in bash an arithmetic
|
|
69
97
|
# error aborts the enclosing loop, which once let a job run with NO slot (15 Oct, found by task-tracking).
|
|
70
|
-
local t; t=$(cat "$
|
|
98
|
+
local t; t=$(cat "$SD/last-admit" 2>/dev/null); case "$t" in ''|*[!0-9]*) echo 999999; return;; esac
|
|
71
99
|
echo $(( $(date +%s) - t ))
|
|
72
100
|
}
|
|
73
101
|
admit_lock_age() {
|
|
@@ -81,36 +109,112 @@ dead() { local e; [ -n "$1" ] || return 1; e=$(kill -0 "$1" 2>&1) && return 1; c
|
|
|
81
109
|
# v17 (24 Oct): reclaim dead holders in EVERY slot dir (a slot numbered above a lowered cap was never scanned, so a
|
|
82
110
|
# dead holder kept it "held" forever), and count only LIVE holders.
|
|
83
111
|
held_count() { local o op n=0
|
|
84
|
-
for o in "$
|
|
112
|
+
for o in "${1:-$SD}"/[0-9]*; do
|
|
85
113
|
[ -d "$o" ] || continue
|
|
86
114
|
op=$(cat "$o/pid" 2>/dev/null || echo)
|
|
87
115
|
if dead "$op"; then rm -rf "$o"; continue; fi
|
|
88
116
|
n=$((n + 1))
|
|
89
117
|
done; echo "$n"; }
|
|
90
118
|
|
|
119
|
+
# --- v26 (NODII-DEV-CLI-86): orphaned slots -------------------------------------------------------------------------
|
|
120
|
+
# 13 Nov: lane infra32-services's session died while its `bun test` gate hung on a down Redis. The holder (this script)
|
|
121
|
+
# stayed alive, so `dead` never fired, and slot 6 was held 2h20m. A slot is reclaimed only when ALL of these hold:
|
|
122
|
+
# - it has been held at least HEAVY_RECLAIM_MIN minutes;
|
|
123
|
+
# - it was taken by a lane (a slot with no lane is never reclaimed this way);
|
|
124
|
+
# - the agent session that took it is gone: its pid is dead, or now belongs to a process started at another time;
|
|
125
|
+
# - no `claude` or `codex` process has its working directory in the job's worktree (a relaunched session);
|
|
126
|
+
# - Dev reports the lane down: liveness orphan or ended, or status stopped or non_issue (`heavy lane-state`).
|
|
127
|
+
# Any check that cannot tell (no lsof, Dev unreachable) keeps the slot. The HOLDER watches its own slot and, once it
|
|
128
|
+
# is an orphan, logs the reclaim and stops its own job's tree (MW2-49: a session tears down only what it started);
|
|
129
|
+
# a waiter never signals another session's processes. `heavy status` shows a slot that is a candidate.
|
|
130
|
+
lane_key() { local b
|
|
131
|
+
if [ -n "${NODII_LANE_KEY:-}" ]; then echo "$NODII_LANE_KEY"; return; fi
|
|
132
|
+
b=$(basename -- "${NODII_DEV_CONFIG_PATH:-}" .json); case "$b" in lane-?*) echo "${b#lane-}";; esac; }
|
|
133
|
+
started() { ps -o lstart= -p "$1" 2>/dev/null | tr -s ' '; }
|
|
134
|
+
# Seconds since path $1 changed; 0 when it is gone (another waiter may remove it between a test and the stat, and an
|
|
135
|
+
# empty operand in $(( )) is an arithmetic error that aborts the wait loop).
|
|
136
|
+
age_of() { local m; m=$(mtime "$1"); echo $(( $(date +%s) - ${m:-$(date +%s)} )); }
|
|
137
|
+
slot_since() { local s; s=$(cat "$1/since" 2>/dev/null); case "$s" in ''|*[!0-9]*) s=$(mtime "$1");; esac; echo "${s:-$(date +%s)}"; }
|
|
138
|
+
# The session that took slot dir $1 is still running (same pid, same start time).
|
|
139
|
+
session_alive() { local sp st; sp=$(cat "$1/session" 2>/dev/null); st=$(cat "$1/session-start" 2>/dev/null)
|
|
140
|
+
case "$sp" in ''|*[!0-9]*) return 1;; esac
|
|
141
|
+
dead "$sp" && return 1
|
|
142
|
+
[ -z "$st" ] || [ "$(started "$sp")" = "$st" ]; }
|
|
143
|
+
# A claude or codex process works in worktree $1. Without lsof we can't tell, so we say yes.
|
|
144
|
+
agent_in() { local wt="$1" c
|
|
145
|
+
[ -n "$wt" ] || return 1
|
|
146
|
+
command -v lsof >/dev/null 2>&1 || return 0
|
|
147
|
+
while IFS= read -r c; do case "$c/" in "$wt"/*) return 0;; esac
|
|
148
|
+
done < <(lsof -a -d cwd -c claude -c codex -Fn 2>/dev/null | sed -n 's/^n//p')
|
|
149
|
+
return 1; }
|
|
150
|
+
lane_state() {
|
|
151
|
+
if [ -n "${HEAVY_LANE_PROBE:-}" ]; then "$HEAVY_LANE_PROBE" "$1" 2>/dev/null; return 0; fi
|
|
152
|
+
# K04-1, K04-3: the nodii-dev on PATH. One without `heavy lane-state` fails, which reads as unknown and keeps the slot.
|
|
153
|
+
nodii-dev heavy lane-state "$1" 2>/dev/null || echo "unknown probe failed"; }
|
|
154
|
+
# Why slot dir $1 is an orphan, or fail. Skips the age check when $2 is "any-age" (heavy status).
|
|
155
|
+
orphan_why() { local o="$1" now lane age st wt
|
|
156
|
+
[ "$RECLAIM_MIN" -gt 0 ] || return 1
|
|
157
|
+
lane=$(cat "$o/lane" 2>/dev/null); [ -n "$lane" ] || return 1
|
|
158
|
+
now=$(date +%s); age=$(( now - $(slot_since "$o") ))
|
|
159
|
+
[ "${2:-}" = any-age ] || [ "$age" -ge $(( RECLAIM_MIN * 60 )) ] || return 1
|
|
160
|
+
session_alive "$o" && return 1
|
|
161
|
+
wt=$(cat "$o/worktree" 2>/dev/null)
|
|
162
|
+
agent_in "$wt" && return 1
|
|
163
|
+
st=$(lane_state "$lane"); case "$st" in down*) ;; *) return 1;; esac
|
|
164
|
+
echo "lane $lane is down in Dev (${st#down }), its agent session $(cat "$o/session" 2>/dev/null || echo '?') is gone and no agent works in ${wt:-its worktree}"; }
|
|
165
|
+
# One line per reclaim, in <lock dir>/reclaim.log and in the slot (the holder's stop trap prints it on its stderr).
|
|
166
|
+
log_reclaim() { local o="$1" why="$2" line
|
|
167
|
+
line="$(date -u +%Y-%m-%dT%H:%M:%SZ) reclaimed slot $(basename "$o") held $(( $(date +%s) - $(slot_since "$o") ))s by lane $(cat "$o/lane" 2>/dev/null || echo '?') (holder $$, job group $(cat "$o/job" 2>/dev/null || echo '?')): $why"
|
|
168
|
+
echo "$line" >> "$D/reclaim.log"; echo "$line" > "$o/reclaimed"; }
|
|
169
|
+
|
|
91
170
|
if [ "${1:-}" = "--status" ]; then
|
|
92
171
|
[ -d "$D" ] || mkdir -p "$D" 2>/dev/null
|
|
93
172
|
load_limits
|
|
94
|
-
local now held q t tp o; now=$(date +%s); held=$(held_count)
|
|
95
|
-
echo "heavy-slot
|
|
173
|
+
local now held lheld q t tp o qd; now=$(date +%s); held=$(held_count "$D"); lheld=$(held_count "$D/light")
|
|
174
|
+
echo "heavy-slot v27 · limits $LIMITS"
|
|
96
175
|
echo "slots ${held}/${SLOTS} held (landing reserve ${RESERVE}: other jobs use at most $(( SLOTS - RESERVE < 1 ? 1 : SLOTS - RESERVE )))"
|
|
97
|
-
echo "
|
|
98
|
-
|
|
176
|
+
if [ "$LSLOTS" -eq 0 ]; then echo "light slots off (the limits file sets no LIGHT_SLOTS): every job takes a heavy slot"
|
|
177
|
+
else echo "light slots ${lheld}/${LSLOTS} held (floor ${LMIN_GB} GB, no stagger; a test run naming at most ${LMAX} test files is light)"; fi
|
|
178
|
+
echo "available memory $(avail_gb) GB (floor ${MIN_GB} GB) · stagger ${STAGGER}s · reclaim after ${RECLAIM_MIN} min"
|
|
179
|
+
local lane why age
|
|
180
|
+
for o in "$D"/[0-9]* "$D"/light/[0-9]*; do
|
|
99
181
|
[ -d "$o" ] || continue
|
|
100
|
-
|
|
182
|
+
case "$o" in */light/*) qd="light slot";; *) qd="slot";; esac
|
|
183
|
+
t=$(slot_since "$o"); lane=$(cat "$o/lane" 2>/dev/null)
|
|
184
|
+
echo " ${qd} $(basename "$o") pid $(cat "$o/pid" 2>/dev/null) for $(( now - ${t:-$now} ))s${lane:+ lane $lane}: $(cat "$o/what" 2>/dev/null)"
|
|
185
|
+
if [ -n "$lane" ] && ! session_alive "$o" && why=$(orphan_why "$o" any-age); then
|
|
186
|
+
age=$(( RECLAIM_MIN * 60 - (now - ${t:-$now}) ))
|
|
187
|
+
if [ "$age" -gt 0 ]; then echo " reclaim candidate: $why; reclaimed in ${age}s"
|
|
188
|
+
else echo " reclaim candidate: $why; its holder reclaims it at its next check"; fi
|
|
189
|
+
fi
|
|
101
190
|
done
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
191
|
+
[ -s "$D/reclaim.log" ] && { echo "last reclaims ($D/reclaim.log):"; tail -3 "$D/reclaim.log" | sed 's/^/ /'; }
|
|
192
|
+
local sp=0 st hq=0; st=" $(ps -axo pid=,stat= 2>/dev/null | awk '$2 ~ /^T/ {print $1}' | tr '\n' ' ') "
|
|
193
|
+
for qd in q lq; do
|
|
194
|
+
q=0
|
|
195
|
+
for t in $(ls "$D/$qd" 2>/dev/null | sort); do
|
|
196
|
+
tp=$(cat "$D/$qd/$t" 2>/dev/null)
|
|
197
|
+
if dead "$tp"; then rm -f "$D/$qd/$t"; continue; fi
|
|
198
|
+
case "$st" in *" $tp "*) sp=$((sp + 1)); echo " stopped: pid ${tp} waiting $(( now - ${t%%.*} ))s (holds no place until it resumes)"; continue;; esac
|
|
199
|
+
q=$((q + 1)); if [ "$qd" = lq ]; then echo " light queued $q: pid ${tp} waiting $(( now - ${t%%.*} ))s"; else echo " queued $q: pid ${tp} waiting $(( now - ${t%%.*} ))s"; fi
|
|
200
|
+
done
|
|
201
|
+
if [ "$qd" = q ]; then hq=$q; else echo "queue ${hq} waiting, light queue ${q} waiting, ${sp} stopped"; fi
|
|
108
202
|
done
|
|
109
|
-
echo "queue ${q} waiting, ${sp} stopped"
|
|
110
203
|
return 0
|
|
111
204
|
fi
|
|
112
205
|
|
|
113
|
-
|
|
206
|
+
# v27: --size light|heavy before the command (`nodii-dev heavy run --size light -- CMD`), or HEAVY_SIZE.
|
|
207
|
+
SIZE="${HEAVY_SIZE:-}"
|
|
208
|
+
while [ $# -gt 0 ]; do
|
|
209
|
+
case "$1" in
|
|
210
|
+
--size) [ $# -ge 2 ] || { echo "usage: nodii-dev heavy run [--size light|heavy] -- <command...>" >&2; return 2; }; SIZE="$2"; shift 2;;
|
|
211
|
+
--size=*) SIZE="${1#--size=}"; shift;;
|
|
212
|
+
--) shift; break;;
|
|
213
|
+
*) break;;
|
|
214
|
+
esac
|
|
215
|
+
done
|
|
216
|
+
case "$SIZE" in ''|auto|light|heavy) ;; *) echo "[heavy-slot] --size must be light or heavy, got: $SIZE" >&2; return 2;; esac
|
|
217
|
+
[ $# -gt 0 ] || { echo "usage: nodii-dev heavy run [--size light|heavy] -- <command...> | nodii-dev heavy status" >&2; return 2; }
|
|
114
218
|
# Long-running dev/preview servers are not jobs: they never exit, so they would hold a slot forever
|
|
115
219
|
# (15 Oct: a `vite --port 5260` sat in a slot while 7 real jobs queued). Refuse them; run them outside the gate,
|
|
116
220
|
# at most DEV_SERVER_CAP host-wide, and kill them by PID when done (K02-7). `vite build`, vitest and typechecks are jobs.
|
|
@@ -123,7 +227,17 @@ if { printf '%s' "$cmd" | grep -Eq '[ /]vite( |$)' && ! printf '%s' "$cmd" | gre
|
|
|
123
227
|
fi
|
|
124
228
|
# v20 (24 Oct): already inside a heavy slot (an outer heavy-slot or a gated-bin wrapper set this) -> run directly.
|
|
125
229
|
# A nested heavy-slot used to wait for a SECOND slot while its parent held one: tenant's db:setup sat 74 min that way (K02-6).
|
|
126
|
-
|
|
230
|
+
# v26 (NODII-DEV-CLI-86): the job of a held slot sees NODII_HEAVY_SLOT=<n>. Inside it, heavy work (a `heavy run`, a
|
|
231
|
+
# `lane check` gate, a reviewer) runs in that same slot and never queues for a second one. A marker whose slot is no
|
|
232
|
+
# longer held (a process that outlived its job) queues like any other job. A marker-less NODII_HEAVY_SLOT_HELD=1 comes
|
|
233
|
+
# from an older copy (v20-v25) or a gated-bin wrapper and still runs at once.
|
|
234
|
+
if [ -n "${NODII_HEAVY_SLOT:-}" ]; then
|
|
235
|
+
# v27: light/<n> names a light slot.
|
|
236
|
+
case "${NODII_HEAVY_SLOT#light/}" in ''|*[!0-9]*) ;; *)
|
|
237
|
+
if [ -d "$D/$NODII_HEAVY_SLOT" ] && ! dead "$(cat "$D/$NODII_HEAVY_SLOT/pid" 2>/dev/null)"; then exec "$@"; fi;; esac
|
|
238
|
+
echo "[heavy-slot] NODII_HEAVY_SLOT=${NODII_HEAVY_SLOT} names a slot that is no longer held; queueing for a slot of its own." >&2
|
|
239
|
+
unset NODII_HEAVY_SLOT NODII_HEAVY_SLOT_HELD
|
|
240
|
+
elif [ "${NODII_HEAVY_SLOT_HELD:-}" = 1 ]; then exec "$@"; fi
|
|
127
241
|
# v18 (24 Oct): a teardown FREES memory, so it never waits for a slot. `docker compose … down -v` sat 3 min behind
|
|
128
242
|
# 14 queued jobs (12:27Z) while its stack kept its containers' memory.
|
|
129
243
|
case " $* " in *" compose "*" down"*|*" down "*|*stack:down*|*":down "*) exec "$@";; esac
|
|
@@ -159,10 +273,44 @@ is_push() {
|
|
|
159
273
|
esac
|
|
160
274
|
done; return 1; }
|
|
161
275
|
is_push "$@" && IS_LANDING=1; [ "${HEAVY_LANDING:-}" = 1 ] && IS_LANDING=1
|
|
276
|
+
# v27 (hub-401): a job's size. Unsized, a test run that names its test files, at most LIGHT_MAX_TEST_FILES
|
|
277
|
+
# of them (`bun test a.test.ts`, `vitest run x.spec.ts`, `node --test t/a.test.js`, optionally piped to tail, head,
|
|
278
|
+
# grep or tee), is light; everything else is heavy. A landing is always heavy: it keeps its reserve.
|
|
279
|
+
light_test_run() {
|
|
280
|
+
local s="$*" w n=0 runner=0 prev=""
|
|
281
|
+
case "$s" in "/bin/sh -c "*|"sh -c "*|"bash -c "*|"/bin/bash -c "*) s="${s#* -c }";; esac
|
|
282
|
+
s=$(printf '%s' "$s" | sed -E 's/[0-9]?>&[0-9]//g; s/[|][[:space:]]*(tail|head|grep|tee)([[:space:]][^|;&]*)?$//')
|
|
283
|
+
case "$s" in *"&&"*|*"||"*|*";"*|*"|"*|*'$('*|*'`'*|*"<"*|*">"*) return 1;; esac
|
|
284
|
+
load_limits
|
|
285
|
+
set -f
|
|
286
|
+
for w in $s; do
|
|
287
|
+
case "$w" in
|
|
288
|
+
vitest|*/vitest|jest|*/jest|mocha|*/mocha) runner=1;;
|
|
289
|
+
test) case "$prev" in bun|*/bun) runner=1;; esac;;
|
|
290
|
+
--test) case "$prev" in node|*/node) runner=1;; esac;;
|
|
291
|
+
*'*'*|*'?'*) set +f; return 1;;
|
|
292
|
+
*.test.[cm][jt]s|*.test.[jt]s|*.test.[jt]sx|*.spec.[cm][jt]s|*.spec.[jt]s|*.spec.[jt]sx) n=$((n + 1));;
|
|
293
|
+
esac
|
|
294
|
+
prev="$w"
|
|
295
|
+
done
|
|
296
|
+
set +f
|
|
297
|
+
[ "$runner" = 1 ] && [ "$n" -ge 1 ] && [ "$n" -le "$LMAX" ]
|
|
298
|
+
}
|
|
299
|
+
# A light slot skips the heavy cap, floor and stagger, so only a test run naming its test files may take one: an
|
|
300
|
+
# explicit --size light (or HEAVY_SIZE=light) on anything else is refused and takes a heavy slot (K02-4).
|
|
301
|
+
load_limits
|
|
302
|
+
if [ "$IS_LANDING" = 1 ]; then SIZE=heavy
|
|
303
|
+
elif [ "$LSLOTS" -eq 0 ]; then SIZE=heavy
|
|
304
|
+
elif [ -z "$SIZE" ] || [ "$SIZE" = auto ]; then if light_test_run "$@"; then SIZE=light; else SIZE=heavy; fi
|
|
305
|
+
elif [ "$SIZE" = light ] && ! light_test_run "$@"; then
|
|
306
|
+
echo "[heavy-slot] --size light is only for a test run naming at most ${LMAX} test files; this job takes a heavy slot: $*" >&2
|
|
307
|
+
SIZE=heavy
|
|
308
|
+
fi
|
|
309
|
+
if [ "$SIZE" = light ]; then SD="$D/light"; mkdir -p "$SD" 2>/dev/null; fi
|
|
162
310
|
# v19 (24 Oct): FIFO. Waiters raced for freed slots, so an unlucky job starved: tenant's `test:stack:up` waited 71 min
|
|
163
311
|
# behind 14 newer jobs (12:27Z). Each non-landing waiter takes a ticket (time-ordered name); only the oldest
|
|
164
312
|
# (limit - held) live tickets may try for a slot. Landings keep their reserve and skip the line.
|
|
165
|
-
local Q="$D/q" TICKET=""; mkdir -p "$Q" 2>/dev/null
|
|
313
|
+
local Q="$D/q" TICKET=""; [ "$SIZE" = light ] && Q="$D/lq"; mkdir -p "$Q" 2>/dev/null
|
|
166
314
|
if [ "$IS_LANDING" != 1 ]; then TICKET="$Q/$(date +%s).$(printf '%06d' $$)"; echo $$ > "$TICKET"; fi
|
|
167
315
|
trap '[ -n "$TICKET" ] && rm -f "$TICKET"' EXIT
|
|
168
316
|
# v25 (13 Nov, hub-377): a STOPPED waiter (ps state T: ^Z, SIGSTOP, a suspended session) never polls, so it can never
|
|
@@ -176,7 +324,7 @@ fifo_rank() { local r=0 t tp st; st=" $(stopped_pids | tr '\n' ' ') "
|
|
|
176
324
|
[ "$Q/$t" = "$TICKET" ] && { echo "$r"; return; }
|
|
177
325
|
case "$st" in *" $tp "*) continue;; esac
|
|
178
326
|
r=$((r + 1)); done; echo 0; }
|
|
179
|
-
local slot="" waited=0 said=-1 held rank LIMIT i l o a g others av why
|
|
327
|
+
local slot="" waited=0 said=-1 held rank LIMIT i l o a g others alone av why
|
|
180
328
|
# A queued job says its place and its wait when it starts waiting and then once a minute; it never gives up, so a
|
|
181
329
|
# gate is never timed out by the queue (hub-377).
|
|
182
330
|
say() { if [ "$said" -lt 0 ] || [ $((waited - said)) -ge 60 ]; then said=$waited; echo "[heavy-slot] waiting ${waited}s: $1. Job: $JOB" >&2; fi; }
|
|
@@ -199,7 +347,7 @@ while :; do
|
|
|
199
347
|
pause; continue
|
|
200
348
|
fi
|
|
201
349
|
for i in $(seq 1 "$SLOTS"); do
|
|
202
|
-
l="$
|
|
350
|
+
l="$SD/$i"
|
|
203
351
|
if [ -d "$l" ]; then o=$(cat "$l/pid" 2>/dev/null || echo); dead "$o" && rm -rf "$l"; fi
|
|
204
352
|
if mkdir "$l" 2>/dev/null; then
|
|
205
353
|
echo $$ > "$l/pid"; echo "$(pwd) :: $*" > "$l/what"
|
|
@@ -212,7 +360,10 @@ while :; do
|
|
|
212
360
|
# The stagger only matters while another admitted job is still running (its memory may still be growing).
|
|
213
361
|
# With every other slot idle, admit at once: a fixed 60 s gap serialised quick jobs (biome, lint) to one a
|
|
214
362
|
# minute and queued 29 of them with zero heavy jobs running (15 Oct 15:38Z).
|
|
215
|
-
others=0; for o in "$
|
|
363
|
+
others=0; for o in "$SD"/[0-9]*; do [ -d "$o" ] && [ "$o" != "$l" ] && others=$((others + 1)); done
|
|
364
|
+
# v27: "no other job is running" counts both pools. A light job alone in its pool beside running heavy jobs
|
|
365
|
+
# (or a heavy job beside light ones) is held by the floor like any other job: waiting can free memory then.
|
|
366
|
+
if [ "$SD" = "$D" ]; then alone=$(( others + $(held_count "$D/light") )); else alone=$(( others + $(held_count "$D") )); fi
|
|
216
367
|
# v24 (hub-377): with NO heavy job running, waiting cannot free memory (heavy jobs hold none of it), so the
|
|
217
368
|
# floor would stop every job on the machine for good, landings too: once every lane went through the queue,
|
|
218
369
|
# 6 GB available under a 14 GB floor with 0 slots held queued all of them. One job is then admitted, with a
|
|
@@ -223,10 +374,10 @@ while :; do
|
|
|
223
374
|
# v24 (hub-377): the cap is checked again HERE, under the admit lock. The held count at the top of the loop
|
|
224
375
|
# is read before the lock, so two waiters could both see one free slot, take two slot numbers and run one
|
|
225
376
|
# job over the cap (seen in the suite under load: 3 at once with a limit of 2; v23 had the same race).
|
|
226
|
-
if [ "$others" -lt "$LIMIT" ] && { [ "$a" -ge "$MIN_GB" ] || [ "$
|
|
227
|
-
if [ "$a" -lt "$MIN_GB" ] && [ "$
|
|
377
|
+
if [ "$others" -lt "$LIMIT" ] && { [ "$a" -ge "$MIN_GB" ] || [ "$alone" -eq 0 ] || [ "$IS_LANDING" = 1 ]; } && { [ "$others" -eq 0 ] || [ "$g" -ge "$STAGGER" ]; }; then
|
|
378
|
+
if [ "$a" -lt "$MIN_GB" ] && [ "$alone" -eq 0 ]; then echo "[heavy-slot] available ${a} GB is under the floor ${MIN_GB} GB, but no other heavy job is running, so waiting cannot free memory: admitting this one job alone." >&2
|
|
228
379
|
elif [ "$a" -lt "$MIN_GB" ]; then echo "[heavy-slot] available ${a} GB is under the floor ${MIN_GB} GB; a landing is held only by the cap and the stagger: admitting it." >&2; fi
|
|
229
|
-
date +%s > "$
|
|
380
|
+
date +%s > "$SD/last-admit"; rmdir "$D/.admit"; slot="$l"; [ -n "$TICKET" ] && rm -f "$TICKET"; TICKET=""; break 2
|
|
230
381
|
fi
|
|
231
382
|
rmdir "$D/.admit"
|
|
232
383
|
fi
|
|
@@ -243,13 +394,22 @@ while :; do
|
|
|
243
394
|
done
|
|
244
395
|
# belt and braces: never run without a slot
|
|
245
396
|
[ -n "$slot" ] && [ -d "$slot" ] || { echo "[heavy-slot] internal error: no slot acquired; refusing to run: $*" >&2; exit 70; }
|
|
246
|
-
|
|
397
|
+
LBL=""; [ "$SIZE" = light ] && LBL=" light"
|
|
398
|
+
echo "[heavy-slot] got $(basename "$slot")/${SLOTS}${LBL} after ${waited}s (available $(avail_gb) GB): $*" >&2
|
|
399
|
+
# v26: who holds the slot, for the orphan reclaim, and the marker the job's own heavy work runs under.
|
|
400
|
+
date +%s > "$slot/since"
|
|
401
|
+
lane_key > "$slot/lane"
|
|
402
|
+
echo "${CLAUDE_PID:-}" > "$slot/session"; [ -n "${CLAUDE_PID:-}" ] && started "$CLAUDE_PID" > "$slot/session-start"
|
|
403
|
+
{ git rev-parse --show-toplevel 2>/dev/null || pwd; } > "$slot/worktree"
|
|
404
|
+
# v27: a light slot's marker is light/<n>, so the nested check finds it under $D.
|
|
405
|
+
if [ "$SIZE" = light ]; then export NODII_HEAVY_SLOT="light/$(basename "$slot")"; else export NODII_HEAVY_SLOT="$(basename "$slot")"; fi
|
|
247
406
|
# Run the job as its OWN process group and forward INT/TERM to the whole group. Without this, stopping the wrapper
|
|
248
407
|
# left the job running (15 Oct: a sharded tsc kept spawning 4 GB shards after its wrapper was killed) AND released
|
|
249
408
|
# the slot, so the gate under-counted live memory.
|
|
250
409
|
set -m
|
|
251
410
|
"$@" &
|
|
252
411
|
child=$!
|
|
412
|
+
echo "$child" > "$slot/job"
|
|
253
413
|
# v13 (24 Oct): a wrapper killed with SIGKILL (Codex's per-command timeout) can't run its traps, and the job
|
|
254
414
|
# lives in its own process group, so it was orphaned and kept running with NO slot. A watchdog in ITS OWN group
|
|
255
415
|
# outlives the wrapper and takes the job down with it.
|
|
@@ -272,11 +432,25 @@ kill_tree() { local g gs; gs=$(tree_groups "$child"); gs=$(printf '%s\n%s\n' "$c
|
|
|
272
432
|
kill_tree ) </dev/null >/dev/null 2>&1 &
|
|
273
433
|
watchdog=$!
|
|
274
434
|
set +m
|
|
275
|
-
|
|
435
|
+
# v26 (NODII-DEV-CLI-86): the orphan watch. Once a minute (HEAVY_RECLAIM_POLL_SEC) the holder asks whether its own slot
|
|
436
|
+
# is an orphan (orphan_why); if so it logs the reclaim and stops itself, and its stop trap takes down the job's tree.
|
|
437
|
+
POLL_RECLAIM="${HEAVY_RECLAIM_POLL_SEC:-60}"; case "$POLL_RECLAIM" in ''|*[!0-9]*|0) POLL_RECLAIM=60;; esac
|
|
438
|
+
( trap '' HUP PIPE
|
|
439
|
+
while sleep "$POLL_RECLAIM"; do
|
|
440
|
+
dead "$wrapper" && exit 0; [ -d "$slot" ] || exit 0
|
|
441
|
+
load_limits
|
|
442
|
+
if why=$(orphan_why "$slot"); then log_reclaim "$slot" "$why"; kill -TERM "$wrapper" 2>/dev/null; exit 0; fi
|
|
443
|
+
done ) </dev/null >/dev/null 2>&1 &
|
|
444
|
+
# Its output goes nowhere: a watch (or its sleep) holding the holder's stdout or stderr kept a caller's pipe open up to
|
|
445
|
+
# a minute after the job ended.
|
|
446
|
+
orphanwatch=$!
|
|
447
|
+
stop() { kill "$orphanwatch" 2>/dev/null
|
|
448
|
+
[ -s "$slot/reclaimed" ] && echo "[heavy-slot] $(cat "$slot/reclaimed" 2>/dev/null)" >&2
|
|
449
|
+
kill_tree; rm -rf "$slot"; exit 143; }
|
|
276
450
|
trap stop INT TERM
|
|
277
451
|
trap 'rm -rf "$slot"' EXIT
|
|
278
452
|
wait "$child"; rc=$?
|
|
279
|
-
kill "$watchdog" 2>/dev/null
|
|
453
|
+
kill "$watchdog" "$orphanwatch" 2>/dev/null
|
|
280
454
|
exit "$rc"
|
|
281
455
|
}
|
|
282
456
|
main "$@"; exit $?
|
package/dist/heavy.js
CHANGED
|
@@ -9,23 +9,50 @@
|
|
|
9
9
|
import { spawn } from "node:child_process";
|
|
10
10
|
import { basename } from "node:path";
|
|
11
11
|
import { fileURLToPath } from "node:url";
|
|
12
|
-
import { CliError } from "./core.js";
|
|
12
|
+
import { CliError, devRequest, encode } from "./core.js";
|
|
13
13
|
|
|
14
14
|
export const HEAVY_SLOT_SCRIPT = fileURLToPath(new URL("./heavy-slot.sh", import.meta.url));
|
|
15
15
|
export const HEAVY_PREFIX = "nodii-dev heavy run -- ";
|
|
16
|
-
export const HEAVY_USAGE = "nodii-dev heavy run -- <command...> | nodii-dev heavy status";
|
|
16
|
+
export const HEAVY_USAGE = "nodii-dev heavy run [--size light|heavy] -- <command...> | nodii-dev heavy status";
|
|
17
17
|
|
|
18
18
|
/** The process that runs ARGV under a heavy slot: `[file, args]` for spawn. A nested call runs at once (K02-6). */
|
|
19
19
|
export const heavySlotArgv = (argv) => ["/bin/bash", [HEAVY_SLOT_SCRIPT, ...argv]];
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
// NODII-DEV-CLI-86: Dev's view of a slot's lane for the orphan reclaim. Down means no live run (liveness orphan or
|
|
22
|
+
// ended) or a lane that was stopped or closed. Quiet and blocked lanes are up: a lane waiting on another still owns its job.
|
|
23
|
+
const DOWN_LIVENESS = new Set(["orphan", "ended"]);
|
|
24
|
+
const DOWN_STATUS = new Set(["stopped", "non_issue"]);
|
|
25
|
+
|
|
26
|
+
/** `up|down <why>` for a lane record from Dev. */
|
|
27
|
+
export function laneUpOrDown(lane) {
|
|
28
|
+
const why = `liveness=${lane?.liveness ?? "none"} status=${lane?.status ?? "none"}`;
|
|
29
|
+
return DOWN_LIVENESS.has(lane?.liveness) || DOWN_STATUS.has(lane?.status) ? `down ${why}` : `up ${why}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** `nodii-dev heavy lane-state KEY`: one line, `up|down|unknown <why>`. Unknown keeps the slot; it never throws. */
|
|
33
|
+
export async function laneState(key, { request = devRequest, timeoutMs = 15_000 } = {}) {
|
|
34
|
+
try {
|
|
35
|
+
const payload = await request("GET", `/api/v1/lanes/${encode(key)}`, undefined, { signal: AbortSignal.timeout(timeoutMs) });
|
|
36
|
+
return laneUpOrDown(payload?.data);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
return `unknown ${String(error?.code ?? error?.name ?? "error").replace(/\s+/g, "_")}`;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** `nodii-dev heavy run|status|lane-state` when the CLI was started on Node or Bun rather than through bin/nodii-dev. */
|
|
43
|
+
export async function heavyCommand(args, { spawnImpl = spawn, env = process.env, signals = process, request = devRequest, write = (text) => process.stdout.write(text) } = {}) {
|
|
23
44
|
const [action, ...rest] = args;
|
|
24
45
|
let argv;
|
|
46
|
+
if (action === "lane-state") {
|
|
47
|
+
if (!rest[0]) throw new CliError("USAGE", "nodii-dev heavy lane-state <lane-key>", 2);
|
|
48
|
+
write(`${await laneState(rest[0], { request })}\n`);
|
|
49
|
+
return 0;
|
|
50
|
+
}
|
|
25
51
|
if (action === "status") argv = ["--status"];
|
|
26
52
|
else if (action === "run") {
|
|
53
|
+
// heavy-slot.sh reads --size and the `--` itself (v27); only a bare `--` in front is dropped here.
|
|
27
54
|
argv = rest[0] === "--" ? rest.slice(1) : rest;
|
|
28
|
-
if (
|
|
55
|
+
if (argv.every((word) => word === "--")) throw new CliError("USAGE", HEAVY_USAGE, 2);
|
|
29
56
|
} else throw new CliError("USAGE", HEAVY_USAGE, 2);
|
|
30
57
|
const [file, fileArgs] = heavySlotArgv(argv);
|
|
31
58
|
return new Promise((resolve) => {
|
|
@@ -249,13 +276,14 @@ function queued(argv) {
|
|
|
249
276
|
return ["bash", "sh", "zsh"].includes(name) && /(?:^|\/)heavy-slot\.sh$/.test(argv.find((word, i) => i > 0 && !word.startsWith("-")) ?? "");
|
|
250
277
|
}
|
|
251
278
|
|
|
252
|
-
// `nodii-dev lane check --gate …` and `nodii-dev receipt write --run …` queue each gate themselves,
|
|
253
|
-
// `nodii-dev run … -- CMD` queues CMD when it is heavy (needsSlot)
|
|
279
|
+
// `nodii-dev lane check --gate …` and `nodii-dev receipt write --run …` queue each gate themselves,
|
|
280
|
+
// `nodii-dev run … -- CMD` queues CMD when it is heavy (needsSlot), and `nodii-dev test affected --run`
|
|
281
|
+
// queues each job at the change's size (hub-401).
|
|
254
282
|
function selfQueued(argv) {
|
|
255
283
|
const name = plainName(argv[0]);
|
|
256
284
|
if (!/^nodii-dev(?:\.js)?$/.test(name)) return false;
|
|
257
285
|
const [command, action] = argv.slice(1).filter((word) => !word.startsWith("-"));
|
|
258
|
-
return (command === "lane" && action === "check") || (command === "receipt" && action === "write") || command === "run";
|
|
286
|
+
return (command === "lane" && action === "check") || (command === "receipt" && action === "write") || command === "run" || (command === "test" && action === "affected");
|
|
259
287
|
}
|
|
260
288
|
|
|
261
289
|
/** Whether ARGV, run directly (no shell), is heavy work not yet in the queue; a shell running a string counts its commands. */
|