@nodii/dev 0.6.175 → 0.6.177

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 CHANGED
@@ -571,7 +571,7 @@ map. Gather pins the always-on clauses for a lane's repository and side
571
571
  (`GET /rules/gather-pins`), marked `always-on` in the Clause map.
572
572
 
573
573
  The hooks that run on every tool call or stop (`safety-pre`, `nudge-pre`,
574
- `nudge-stop`, `lane-heartbeat`, and `claude-figma-pre` on a Figma write) read the lane from Dev on each call, never from
574
+ `nudge-stop`, `lane-heartbeat` on stop, and `claude-figma-pre` on a Figma write) read the lane from Dev on each call, never from
575
575
  a kept copy, and ask for as little as decides the same thing
576
576
  (hub-528). A lane is read as `GET /lanes/KEY?view=hook`: only the
577
577
  fields the hooks use, without the lane's timeline. A Dev that does not know
@@ -596,7 +596,14 @@ blocks, silently when satisfied (AGENT-HARNESS-15):
596
596
  deploy/apply without a `GO deploy <sha>` grant; shell writes outside the
597
597
  worktree (redirects, `tee`, `cp`, `mv`, `rm`, `mkdir`, `touch` …; temp space
598
598
  is fine) and mutating `git -C` into another checkout; Edit/Write outside the
599
- worktree, outside owned paths or on generated files.
599
+ worktree, outside owned paths or on generated files. One exception
600
+ (hub-548): an edit to a generated Surface (`*.surface.generated.tsx`)
601
+ is allowed when it only adds or changes keyed `@handrolled <KEY>: ...` /
602
+ `@end-handrolled` regions (DEC-0488, DEC-0494, MW8-17.11). The file after the
603
+ edit must parse and pass the surface-gen check's own test against the file
604
+ before it: every byte outside the regions unchanged and in order. Edit,
605
+ MultiEdit, Write and Codex `apply_patch` Update hunks are judged this way; a
606
+ delete, a move or any other generated file stays refused.
600
607
  - **A coordinator** may use `--no-verify` only with `NODII_DEV_NO_VERIFY_REASON`
601
608
  set; the reason is recorded in private Git metadata.
602
609
 
@@ -652,6 +659,9 @@ nodii-dev lane ask withdraw ID
652
659
  nodii-dev lane answer ID --text TEXT
653
660
  nodii-dev lane defer ID --reason TEXT [--minutes N] [--leeway]
654
661
  nodii-dev lane asks [--machine M] [--state S] [--lane KEY] [--cursor C] [--limit N]
662
+ nodii-dev lane message TO --body TEXT|--body-file FILE [--issue ISSUE]
663
+ nodii-dev lane inbox [KEY] [--all]
664
+ # lane-to-lane messaging (NODII-DEV-CLI-191); see "Lane messages"
655
665
  nodii-dev coordinator items [--machine NAME|ID]
656
666
  nodii-dev lane resume --host-lost [--min-free-gb N] [--stagger-seconds N]
657
667
  # after a reboot (coordinator): resume every host_lost lane
@@ -673,6 +683,12 @@ nodii-dev lane resume KEY --profile PATH --class long|short|no-build
673
683
  # offline or unread account, and LANE_ALREADY_ON_ACCOUNT. A
674
684
  # lane stopped by two deaths moves off an exhausted account
675
685
  # with no amendment or override, and its death count restarts
686
+ # NODII-DEV-CLI-191: any lane resume first reads the
687
+ # profiles recorded exhausted (see "Account failures" below).
688
+ # A lane on one is moved to another account of its runtime
689
+ # whose fresh band admits its class, and the output says so;
690
+ # with none it is refused ACCOUNT_EXHAUSTED naming the reset.
691
+ # --profile naming another account goes as before
676
692
  nodii-dev lane resume KEY [--override --reason TEXT]
677
693
  # --override (coordinator): resume a lane the two-identical-
678
694
  # deaths rule stopped; Dev refuses it with
@@ -683,6 +699,30 @@ nodii-dev lane resume KEY [--override --reason TEXT]
683
699
  # running run or a lane-agent heartbeat since the death
684
700
  # that is not yet old enough to be dead: one process per
685
701
  # lane (the coordinator's own heartbeats do not count)
702
+ nodii-dev lane resume KEY --if-changed
703
+ # OFF unless NODII_DEV_RESUME_IF_CHANGED=1 (see "Switched-off
704
+ # lane features"); off, it refuses RESUME_IF_CHANGED_OFF.
705
+ # NODII-DEV-CLI-191: resume only when what blocks the lane
706
+ # changed since its last resume: each PR wait's state, head
707
+ # and merge commit (gh), each issue wait's status and each
708
+ # blocking lane's status (Dev), its leeways and its latest
709
+ # amendment. Heartbeat notes never count. Unchanged prints
710
+ # BLOCKERS_UNCHANGED and exits 0; the fingerprint is kept in
711
+ # runs/resume-fingerprints.json. A superseded lane (every
712
+ # issue it holds closed) is refused as LANE_DEAD with the
713
+ # evidence; nothing stops it here (the coordinator ends it by
714
+ # the MW1-28 exit that fits). A
715
+ # lane whose work is landed (MW1-30.1: its changed paths
716
+ # match origin/main) before deploy is refused as
717
+ # LANDED_NOT_DONE and never stopped: landed is not done
718
+ # (MW1-28), so it goes on to deploy and the dev check. With
719
+ # NODII_DEV_SAME_SESSION_RESUME=1, every resume
720
+ # continues the dead run's own session when this machine has
721
+ # its log and the lane stayed on its account (`claude
722
+ # --resume`, `codex exec resume`), with a prompt of its last
723
+ # conclusion, what changed and the claim-and-fix directive,
724
+ # never the full Freeze; otherwise it starts fresh with the
725
+ # Freeze as before
686
726
  nodii-dev lane check KEY [--gate CMD ...] [--critical] [--max-iterations N] [--picker agent]
687
727
  # from the lane's worktree: the rules, Figma and code checks (DEV-REV) over
688
728
  # the diff against origin/main, tracked and untracked; --gate runs the gates
@@ -937,18 +977,40 @@ and is retried after ten minutes, not on every write.
937
977
  The Stop nudge asks the session to continue once (`decision: block`, skipped
938
978
  when `stop_hook_active`); Codex passes it through its managed dispatcher.
939
979
  Before that, in a lane session (a `lane-*.json` identity), the same hook holds
940
- the stop while the lane's done check is not recorded and the lane is not
941
- BLOCKED (NODII-DEV-CLI-8), even after an earlier block: it reads the lane from
942
- Dev and answers `Lane KEY: done check not recorded. Continue the Task: STEP.`
943
- Recording the done check (`lane done-check`), a blocked step (`lane step
944
- --block`) or a heartbeat note that starts with "blocked" lets the stop
945
- through, and so does a lane Dev can't serve. A block on an open issue that no
946
- live lane holds does not let it through (MW1-12.6, DEC-0486): the hook answers
947
- with the `lane leeway` command to run, and while a leeway is asked or granted
948
- it keeps the session on what is not blocked, or on the fix. One session is held at most
949
- `NODII_DEV_STOP_BLOCK_LIMIT` times in a row (default 5), as a cost backstop. A
950
- nudge hook skips the amendment guard and the waiting-results banner, since
951
- either would corrupt its JSON; it cannot run a Delta gather.
980
+ the stop while the lane's done check is not recorded (NODII-DEV-CLI-8), even
981
+ after an earlier block: it reads the lane from Dev and answers `Lane KEY: done
982
+ check not recorded. Continue the Task: STEP.` By default a blocked step, an
983
+ open wait or a heartbeat note that starts "blocked" or "WAITING:" lets the stop
984
+ through, except where leeway covers the blocker (MW1-12.6). With
985
+ `NODII_DEV_BLOCKED_NOT_EXIT=1` (NODII-DEV-CLI-191; off until its clause is in
986
+ force, as lane questions were, DEC-0503), BLOCKED is not an exit:
987
+ only a typed human gate lets the stop through. The gates
988
+ are a wait on an operator question (`Q-n`), a decision (`DEC-n`), a release or
989
+ launch day; a wait on a `steward-*` lane; or a latest heartbeat typed
990
+ `WAITING: human-gate totp|steward|spend: WHAT THE PERSON MUST DO`. Any other
991
+ blocker, such as a blocked step, a PR, an issue, another lane, or an untyped
992
+ BLOCKED or WAITING note, holds the stop. The answer then names the blocker and
993
+ says to claim it with `lane leeway` (MW1-12.6), fix it with the lane's
994
+ subagents, and keep working what is not blocked. Recording the done check
995
+ (`lane done-check`) lets the stop through, and so does a lane Dev can't serve.
996
+ While a leeway is asked or granted (MW1-12.6, DEC-0486), the hook keeps the
997
+ session on what is not blocked, or on the fix. A no-progress guard lets the
998
+ stop through after `NODII_DEV_STOP_STALL_LIMIT` holds in a row (default 2) with
999
+ no new tool call in the session's transcript. One session is held at most
1000
+ `NODII_DEV_STOP_BLOCK_LIMIT` times in a row (default 40), as a cost backstop.
1001
+ The Stop nudge answers "I couldn't …" with "fix it, or claim it and fix it with
1002
+ your subagents", and citing an issue key no longer silences it. No
1003
+ hook (the managed dispatcher included) runs the amendment guard or the
1004
+ waiting-results banner: a hook cannot enforce them, since it exits 0 on
1005
+ anything but a policy denial, either would corrupt its JSON, and it cannot run
1006
+ a Delta gather (NODII-DEV-CLI-191). Every command the agent runs still reads
1007
+ both fresh from Dev; `hook nudge-pre` tells the lane of a pending amendment.
1008
+
1009
+ The managed dispatcher runs `safety-pre`, `nudge-pre`, `tooling-docs` and
1010
+ `safety-post` inside its own process, with the same code and the same answers
1011
+ as `nodii-dev hook ACTION`; every other handler still gets its own process. A
1012
+ `Bash` call in a lane was two dispatcher processes plus five handler processes;
1013
+ it is now the two dispatchers.
952
1014
 
953
1015
  ### DS conformance: `design roles` and `design check` (DEC-0034)
954
1016
 
@@ -1700,9 +1762,31 @@ which it also ends blocked from the lane's record. Every report carries `exit_co
1700
1762
  `lane_status` evidence. `process.pid` still holds the runtime's own PID, and the
1701
1763
  supervisor appends its report line to `process.log`.
1702
1764
 
1765
+ **Account failures (NODII-DEV-CLI-191).** When the newest line the runtime
1766
+ itself wrote (not a tool's output) names a usage limit (`Claude AI usage limit
1767
+ reached|EPOCH`, `You've hit your limit`, `5-hour limit reached ∙ resets 3pm`,
1768
+ a weekly limit, Codex's `You've hit your usage limit ... try again at|in ...`),
1769
+ an auth failure (`OAuth token has expired`, `401`, `authentication_error`,
1770
+ `Invalid API key`, `Please run /login`) or a network one (Cloudflare,
1771
+ `cf-ray`, `ECONNRESET`, `ETIMEDOUT`, `fetch failed`, `529 Overloaded`), the
1772
+ unfinished exit is reported with the fingerprint `lane-account-usage_limit`,
1773
+ `lane-account-auth` or `lane-account-network`, and the reason names the kind,
1774
+ the reset time and the line. The run's profile (`CLAUDE_CONFIG_DIR` or
1775
+ `CODEX_HOME`, else `~/.claude` or `~/.codex`) is recorded exhausted until the
1776
+ reset the line gives, else for 5 hours (a limit) or 1 hour (auth, network), in
1777
+ `exhausted-accounts.json` in the nodii-dev config directory (beside
1778
+ `config.json`). A later, shorter failure never shortens a recorded wait. The
1779
+ supervisor does not continue the same session on such a failure. `lane resume`
1780
+ reads the file before it asks Dev for a run: a lane whose recorded profile is
1781
+ still exhausted moves to another registered account of its runtime on this
1782
+ machine whose fresh band admits the lane's class (`ok` for long work), or is
1783
+ refused `ACCOUNT_EXHAUSTED` naming the reset; `--profile` naming the exhausted
1784
+ profile is refused too.
1785
+
1703
1786
  Before it reports an unfinished exit, the supervisor continues the same session
1704
- (NODII-DEV-CLI-9) when the lane is not BLOCKED (a lane with leeway asked,
1705
- granted or open to ask is not) and the run log names the
1787
+ (NODII-DEV-CLI-9) unless the lane waits on a typed human gate
1788
+ (NODII-DEV-CLI-191; a fixable blocker is continued with the claim-and-fix
1789
+ directive) and the run log names the
1706
1790
  session (Claude's `session_id`, Codex's `thread_id`). It runs `claude ...
1707
1791
  --resume SESSION`, or `codex exec resume ... SESSION -`, with the same model
1708
1792
  and permission mode, and sends `Lane KEY: done check not recorded. Continue the
@@ -1713,9 +1797,20 @@ Task: STEP.` on stdin. It does this up to `NODII_DEV_LANE_CONTINUATIONS` times
1713
1797
  continuations included, the supervisor heartbeats its run every
1714
1798
  `NODII_DEV_RUN_HEARTBEAT_SECONDS` (60) as the lane identity that owns it. A
1715
1799
  runtime that is alive but writes nothing gets no heartbeat from the supervisor,
1716
- so it goes Quiet at 15 minutes and Orphan at 45 (MW1-36). `lane heartbeat` and
1717
- the harness's `hook lane-heartbeat` (on `PostToolUse` and `Stop`) also heartbeat
1718
- the run. The hook beats at most once a minute per worktree. It reads its lane
1800
+ so it goes Quiet at 15 minutes and Orphan at 45 (MW1-36). A write to the
1801
+ session's Claude transcript (`<profile>/projects/<worktree>/<session>.jsonl`,
1802
+ and its subagents' transcripts) counts as one too. The same tick is the lane's
1803
+ harness heartbeat (`POST /lanes/KEY/heartbeat` with `source: "hook"`), which
1804
+ keeps Dev's `stuck` meaningful, and the run heartbeat's note ends `last
1805
+ activity <time>` (NODII-DEV-CLI-191). This lighter tracking is off unless
1806
+ `NODII_DEV_STOP_ONLY_HEARTBEAT=1` (see "Switched-off lane features"); off, the
1807
+ harness registers `hook lane-heartbeat` on every tool call and on `Stop` as
1808
+ before, and neither the supervisor nor `hook nudge-pre` beats the lane. On, no
1809
+ hook process runs on every tool call for it: the harness's `hook lane-heartbeat`
1810
+ is registered on `Stop` only. A session with no lane supervisor (no `NODII_DEV_RUN_ID`) is beaten from
1811
+ `hook nudge-pre`, which already runs on each tool call, at most once every 5
1812
+ minutes. `lane heartbeat` also heartbeats the run. The Stop hook beats at most
1813
+ once a minute per worktree. It reads its lane
1719
1814
  from Dev once and keeps the lane's id and key in the worktree's git private dir
1720
1815
  (`nodii-lane-heartbeat-lane.json`, beside `nodii-lane.json`), so a beat is one
1721
1816
  small POST. It reads the lane again when the branch or the lane key changes,
@@ -1889,6 +1984,66 @@ ask and, when Dev has no such ask, as a leeway, and `--leeway` goes straight to
1889
1984
  the leeway. `lane asks [--machine M] [--state S] [--lane KEY] [--cursor C]
1890
1985
  [--limit N]` lists asks.
1891
1986
 
1987
+ ### Switched-off lane features (NODII-DEV-CLI-191)
1988
+
1989
+ These features ship off by default until their clauses are in Dev's rulebook,
1990
+ as lane questions (`NODII_DEV_LANE_ASKS`) and BLOCKED is not an exit
1991
+ (`NODII_DEV_BLOCKED_NOT_EXIT`) did. The clauses are drafted on NODII-DEV-CLI-191
1992
+ for a Docs change; when a clause is in force, its default flips. Each
1993
+ is on only when its variable is exactly `1`, in the environment of the process
1994
+ that does the work: the coordinator's for `lane resume`, the lane session's for
1995
+ hooks, and the harness's for `harness update`.
1996
+
1997
+ | Variable | Feature | Off (the default) |
1998
+ |---|---|---|
1999
+ | `NODII_DEV_RESUME_IF_CHANGED` | `lane resume --if-changed` | Refused `RESUME_IF_CHANGED_OFF`; resume without it |
2000
+ | `NODII_DEV_SAME_SESSION_RESUME` | A resume continues the dead run's own session with a short prompt | A resume starts a fresh session with the Freeze |
2001
+ | `NODII_DEV_SUBAGENT_BUDGET` | The subagent budget line in the launch brief, gather's next step and the resume prompt | No line |
2002
+ | `NODII_DEV_ISSUES_GROUP` | `issues group` | Refused `ISSUES_GROUP_OFF` |
2003
+ | `NODII_DEV_LANE_MESSAGES` | `lane message`, `lane inbox` and their delivery by the Stop hook, `hook nudge-pre` and the supervisor | Refused `LANE_MESSAGES_OFF`; no hook delivers from an inbox file |
2004
+ | `NODII_DEV_STOP_ONLY_HEARTBEAT` | The harness beats a lane on `Stop` only; the supervisor and `hook nudge-pre` beat it while it acts | The harness's `hook lane-heartbeat` runs on every tool call and on `Stop`, as before |
2005
+
2006
+ ### Lane messages (NODII-DEV-CLI-191)
2007
+
2008
+ Off unless `NODII_DEV_LANE_MESSAGES=1` (see "Switched-off lane features").
2009
+
2010
+ Lanes, stewards and the coordinator message each other by name: a lane by its
2011
+ lane key, a steward by its steward lane key, the machine's coordinator as
2012
+ `coordinator`. `lane message TO --body TEXT` (or `--body-file FILE`) sends one.
2013
+ The sender is the session's own identity: the current lane (`NODII_DEV_LANE_KEY`),
2014
+ else this agent's Dev name. No flag names it (P-8). A delivered message is
2015
+ marked as information from another session, not an instruction: the lane's
2016
+ Freeze and the rulebook still govern.
2017
+
2018
+ Dev keeps the durable record: the message is a comment on the recipient lane's
2019
+ issue (a message to the coordinator goes on the sending lane's issue; `--issue
2020
+ ISSUE` names another), headed
2021
+
2022
+ ```
2023
+ [lane message] from <sender> to <recipient> · <ISO time> · id <short id>
2024
+ ```
2025
+
2026
+ A local inbox file is the doorbell. When the recipient runs on this machine (a
2027
+ lane provisioned here, a lane Dev places on this agent's machine, or the
2028
+ coordinator), the message is also appended as one JSON line,
2029
+ `{id, from, to, at, body, issueKey, commentRef}`, to
2030
+ `<config dir>/inbox/<key>.jsonl` (file 0600, folder 0700). A read marker beside
2031
+ it (`<key>.read.json`) records how far it has been delivered.
2032
+
2033
+ A lane gets an unread message without polling, Claude and Codex alike:
2034
+
2035
+ - at its next tool call, `hook nudge-pre` adds it to the call's context, from
2036
+ the inbox file alone (no Dev call);
2037
+ - at its next stop, the Stop hook holds the session with the messages (sender,
2038
+ time and body, oldest first, about 1,500 characters at most; the rest wait
2039
+ for the next delivery);
2040
+ - at its next continuation, the lane supervisor puts them ahead of the
2041
+ continuation prompt, so a headless lane takes them as its next turn.
2042
+
2043
+ Each message is delivered once. `lane inbox [KEY]` prints the unread messages
2044
+ and marks them read (`--all` prints every one); it also reads Dev's record and
2045
+ adds the messages sent from another machine that the local file lacks.
2046
+
1892
2047
  Dev counts what lanes wait on a machine's coordinator for (questions, leeway
1893
2048
  asks, waits and escalations) and names the count in the `x-dev-lane-items`
1894
2049
  response header. Every command the coordinator runs prints one line for it
@@ -1922,7 +2077,8 @@ shown and never gate. When Dev cannot be read the call passes with the note
1922
2077
  same check as a direct hook. The governor's launch gate is separate and still
1923
2078
  fails closed.
1924
2079
 
1925
- On the lane side, `hook nudge-pre` and `hook lane-heartbeat` tell a
2080
+ On the lane side, `hook nudge-pre` (and `hook lane-heartbeat`, where an older
2081
+ harness still runs it after a tool call) tell a
1926
2082
  long-running session about an amendment it has not acknowledged (an answer, a
1927
2083
  leeway grant, a rebrief) as `additionalContext`, through the 15-second read
1928
2084
  cache and at most once a minute per newest version, so it learns of it without
@@ -1987,6 +2143,7 @@ missing base ref, an invalid policy file or a git error lets the call through.
1987
2143
  | `ASK_DECIDED` | The ask is already answered or withdrawn | Read it with lane asks --lane KEY |
1988
2144
  | `LANE_NOT_FINISHED` | lane close before the lane's own agent marked the lane finished | The lane runs lane finish KEY --reason TEXT --evidence REF |
1989
2145
  | `LANE_ALREADY_FINISHED` | The lane is already marked finished | Nothing; its coordinator closes it with lane close |
2146
+ | `ACCOUNT_EXHAUSTED` | lane resume would relaunch onto a profile whose last run died on a usage limit, auth or the network, before its recorded reset, and no other account of its runtime has headroom | Resume after the reset it names, or move it with lane resume KEY --profile PATH --class CLASS |
1990
2147
  | `HARNESS_INVALID` | A managed hook entry is not one nodii-dev command, or Dev's harness cannot take the managed hooks | Run harness update, and file the entry as a harness issue |
1991
2148
  | `HARNESS_SETTINGS_INVALID` | .claude/settings.json or .codex/hooks.json is not valid JSON | Repair the file, then run harness update |
1992
2149
  | `TOOLING_DOCS_STALE` | The change touches a tooling file named in nodii.repo.yaml (toolingDocs.tooling) and no docs file (toolingDocs.docs), so hook tooling-docs refuses the push or the pull request (MW1-46.3) | Update the docs in the same change, or add the commit trailer Docs-unaffected: <reason>; check with repo docs-check |
package/dist/accounts.js CHANGED
@@ -7,7 +7,7 @@ import { execFile as execFileCallback } from "node:child_process";
7
7
  import { createHash } from "node:crypto";
8
8
  import { mkdir, readFile, readdir, rename, writeFile } from "node:fs/promises";
9
9
  import { homedir, tmpdir } from "node:os";
10
- import { dirname, isAbsolute, join, normalize } from "node:path";
10
+ import { basename, dirname, isAbsolute, join, normalize } from "node:path";
11
11
  import { promisify } from "node:util";
12
12
  import { CliError, configPath, devRequest, encode, readConfig } from "./core.js";
13
13
 
@@ -224,6 +224,67 @@ async function resolveCodexBin(run = execFile) {
224
224
  catch { return null; }
225
225
  }
226
226
 
227
+ // NODII-DEV-CLI-191: a profile whose lane run died on a usage limit, auth
228
+ // or the network, until its reset. The lane supervisor writes it (as the
229
+ // lane's identity, whose config sits under agents/) and lane resume reads it
230
+ // (as the coordinator's), so it lives in the nodii-dev dir both share.
231
+ export function exhaustedAccountsPath(path = configPath()) {
232
+ const dir = dirname(path);
233
+ return join(basename(dir) === "agents" ? dirname(dir) : dir, "exhausted-accounts.json");
234
+ }
235
+
236
+ async function readExhausted(path, read = readFile) {
237
+ try {
238
+ const entries = JSON.parse(await read(path, "utf8"));
239
+ return entries && typeof entries === "object" && !Array.isArray(entries) ? entries : {};
240
+ } catch { return {}; }
241
+ }
242
+
243
+ const liveOnly = (entries, now) => Object.fromEntries(Object.entries(entries)
244
+ .filter(([, entry]) => Date.parse(entry?.until) > now.getTime()));
245
+
246
+ /** The profiles recorded exhausted whose reset has not come yet, by profile. */
247
+ export async function liveExhaustedAccounts({ path = exhaustedAccountsPath(), now = () => new Date(), read = readFile } = {}) {
248
+ return liveOnly(await readExhausted(path, read), now());
249
+ }
250
+
251
+ /**
252
+ * Record a profile exhausted until `entry.until`. A later failure with an
253
+ * earlier time never shortens a recorded wait; entries past their time are
254
+ * dropped.
255
+ */
256
+ export async function recordExhaustedAccount(profile, entry, { path = exhaustedAccountsPath(), now = () => new Date() } = {}) {
257
+ const key = profilePath(profile);
258
+ const entries = liveOnly(await readExhausted(path), now());
259
+ const recorded = entries[key];
260
+ if (!recorded || Date.parse(entry.until) >= Date.parse(recorded.until)) entries[key] = entry;
261
+ await mkdir(dirname(path), { recursive: true, mode: 0o700 });
262
+ const temporary = `${path}.tmp-${process.pid}`;
263
+ await writeFile(temporary, `${JSON.stringify(entries, null, 2)}\n`, { mode: 0o600 });
264
+ await rename(temporary, path);
265
+ return entries[key];
266
+ }
267
+
268
+ // MW1-38.2: a long resume needs an ok band; a short or no-build one fits up to short_only.
269
+ const ADMITS = { long: ["ok"], short: ["ok", "warning", "short_only"], no_build: ["ok", "warning", "short_only"] };
270
+
271
+ /**
272
+ * Another registered account of the runtime on this machine whose freshly
273
+ * read band admits the work class, skipping `from` and the `exclude`d
274
+ * (recorded exhausted) profiles; null when none has headroom or the
275
+ * accounts cannot be listed.
276
+ */
277
+ export async function pickHeadroomAccount({ runtime, from, exclude = [], workClass = "long" }, { request = devRequest, config = readConfig, readers = READERS } = {}) {
278
+ let accounts;
279
+ try { accounts = await listAccounts(await coordinatorMachineId({ request, config }), { request }); }
280
+ catch { return null; }
281
+ const skip = new Set([from, ...exclude].map(profilePath));
282
+ const candidates = accounts.filter((account) => account.runtime === runtime && !skip.has(profilePath(account.profile)));
283
+ if (!candidates.length) return null;
284
+ const results = await reportUsage(candidates, { request, readers });
285
+ return results.find((result) => !result.error && (ADMITS[workClass] ?? ADMITS.long).includes(result.band)) ?? null;
286
+ }
287
+
227
288
  const READERS = { claude: readClaudeUsage, codex: readCodexUsage };
228
289
 
229
290
  /** The usage ceiling body Dev stores for one reading. */