toolroll 0.9.129 → 0.9.131

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.
Files changed (155) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/dist/agent-fence.js +0 -2
  3. package/dist/agent-state.js +1 -1
  4. package/dist/agents-board.js +0 -3
  5. package/dist/assignment.js +4 -3
  6. package/dist/backup-ui.js +6 -3
  7. package/dist/bounded-spawn.js +0 -1
  8. package/dist/browser/manifest.json +57 -0
  9. package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.br +0 -0
  10. package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.gz +0 -0
  11. package/dist/browser/{terminal.js → terminal.8978c8cbb268e1006f4b0be371c2401b58078e6d9f1380f4a5c2a12da843909f.js} +1 -1
  12. package/dist/browser/terminal.8978c8cbb268e1006f4b0be371c2401b58078e6d9f1380f4a5c2a12da843909f.js.br +0 -0
  13. package/dist/browser/terminal.8978c8cbb268e1006f4b0be371c2401b58078e6d9f1380f4a5c2a12da843909f.js.gz +0 -0
  14. package/dist/browser/{workspace.js → workspace.4f5935dc80574baadf1adafd39a08c567fb1fac1323052ca355546622f9b06d2.js} +25 -25
  15. package/dist/browser/workspace.4f5935dc80574baadf1adafd39a08c567fb1fac1323052ca355546622f9b06d2.js.br +0 -0
  16. package/dist/browser/workspace.4f5935dc80574baadf1adafd39a08c567fb1fac1323052ca355546622f9b06d2.js.gz +0 -0
  17. package/dist/browser/{workspace.css → workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css} +1 -1
  18. package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.br +0 -0
  19. package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.gz +0 -0
  20. package/dist/browser-shell.js +271 -25
  21. package/dist/builder-restart.js +13 -4
  22. package/dist/builder.js +4 -1
  23. package/dist/canvas-freshness.js +122 -0
  24. package/dist/canvas-fs.js +273 -0
  25. package/dist/canvas-reads.js +747 -0
  26. package/dist/canvas-state-key.js +45 -0
  27. package/dist/canvas-state-worker.js +12 -0
  28. package/dist/central-recovery-stage.js +3 -3
  29. package/dist/chat-actions.js +26 -0
  30. package/dist/chat-channel.js +4 -3
  31. package/dist/chat-follow.js +4 -0
  32. package/dist/chat-task-actions.js +1 -1
  33. package/dist/chat-warmth.js +0 -1
  34. package/dist/check-report.js +0 -6
  35. package/dist/commit-extract.js +79 -40
  36. package/dist/companion/access.js +1 -0
  37. package/dist/companion/actions.js +79 -16
  38. package/dist/companion/canvas.js +141 -0
  39. package/dist/companion/chat-origin.js +92 -0
  40. package/dist/companion/chat.js +571 -0
  41. package/dist/companion/daemon.js +46 -5
  42. package/dist/companion/finish.js +51 -0
  43. package/dist/companion/reads.js +5 -1
  44. package/dist/companion/schema.js +64 -0
  45. package/dist/companion/service.js +1 -0
  46. package/dist/companion/store.js +1 -0
  47. package/dist/companion/webauthn.js +11 -2
  48. package/dist/console-instance.js +0 -4
  49. package/dist/contracts/console-api.js +1 -1
  50. package/dist/converse.js +0 -1
  51. package/dist/coordinator.js +1 -1
  52. package/dist/db-tuning.js +16 -0
  53. package/dist/desktop-config.js +156 -0
  54. package/dist/desktop-host.js +6 -152
  55. package/dist/desktop-update.js +1 -1
  56. package/dist/discord.js +9 -3
  57. package/dist/dispatch.js +32 -4
  58. package/dist/doctor.js +30 -0
  59. package/dist/evidence.js +49 -3
  60. package/dist/exec.js +3 -0
  61. package/dist/finished-task-sql.js +14 -0
  62. package/dist/flexible-answers.js +0 -1
  63. package/dist/git-views.js +7 -1
  64. package/dist/github-read.js +41 -0
  65. package/dist/guides.js +4 -3
  66. package/dist/invoke.js +12 -2
  67. package/dist/lead-doors.js +50 -3
  68. package/dist/lead-queue-runtime.js +18 -6
  69. package/dist/lead-queue.js +25 -0
  70. package/dist/lead-status.js +3 -3
  71. package/dist/live-backup-change.js +93 -0
  72. package/dist/live-backup.js +75 -31
  73. package/dist/live-bus.js +3 -2
  74. package/dist/live-records.js +0 -1
  75. package/dist/local-owner.js +0 -1
  76. package/dist/loop-recovery.js +40 -0
  77. package/dist/maintenance.js +29 -1
  78. package/dist/model-choices.js +0 -11
  79. package/dist/needs-you.js +2 -0
  80. package/dist/nightly-check.js +0 -3
  81. package/dist/nightly-github.js +1 -2
  82. package/dist/notification-retention.js +119 -0
  83. package/dist/observations.js +0 -1
  84. package/dist/operate.js +393 -56
  85. package/dist/planner.js +4 -1
  86. package/dist/pull-request-flow.js +56 -15
  87. package/dist/qr.js +0 -25
  88. package/dist/query-index-schema.js +10 -0
  89. package/dist/read-executor-worker.js +9 -7
  90. package/dist/read-executor.js +18 -1
  91. package/dist/read-worker-connection.js +7 -0
  92. package/dist/release-adapters.js +63 -48
  93. package/dist/release-auto.js +17 -13
  94. package/dist/release-card.js +0 -4
  95. package/dist/release-cli.js +58 -10
  96. package/dist/release-next.js +1 -1
  97. package/dist/release-stops.js +71 -0
  98. package/dist/release-supersede.js +125 -0
  99. package/dist/release-supervise.js +90 -0
  100. package/dist/release.js +265 -42
  101. package/dist/restore-retirement.js +4 -1
  102. package/dist/result-review.js +0 -3
  103. package/dist/retention-ui.js +1 -1
  104. package/dist/retention.js +14 -21
  105. package/dist/review-context.js +0 -5
  106. package/dist/route-rules.js +1 -5
  107. package/dist/runner.js +36 -1
  108. package/dist/sandbox-runtime.js +0 -1
  109. package/dist/scope.js +1 -13
  110. package/dist/scout.js +4 -7
  111. package/dist/serve.js +122 -12
  112. package/dist/server/kit.js +1 -0
  113. package/dist/server/live.js +9 -2
  114. package/dist/server/pages.js +3 -2
  115. package/dist/server/phone-engine-api.js +22 -2
  116. package/dist/server/render-pages.js +1 -11
  117. package/dist/server/render-results.js +0 -1
  118. package/dist/server/render-tasks.js +0 -68
  119. package/dist/server/route-table.js +1 -1
  120. package/dist/server/settings-phone.js +32 -9
  121. package/dist/server/settings-scripts.js +1 -1
  122. package/dist/server/settings-section-table.js +0 -3
  123. package/dist/server/settings-sections.js +4 -2
  124. package/dist/server/settings.js +20 -5
  125. package/dist/server/terminals.js +2 -1
  126. package/dist/shutdown-proof.js +29 -0
  127. package/dist/slack.js +9 -3
  128. package/dist/sqlite-telemetry.js +26 -14
  129. package/dist/statement-cache.js +137 -0
  130. package/dist/store.js +86 -14
  131. package/dist/structured-output.js +0 -1
  132. package/dist/style-asset.js +20 -7
  133. package/dist/tailnet.js +0 -3
  134. package/dist/task-control.js +5 -0
  135. package/dist/task-sizing.js +0 -1
  136. package/dist/task-status.js +1 -1
  137. package/dist/teams.js +9 -3
  138. package/dist/teardown-git.js +10 -1
  139. package/dist/telegram.js +4 -1
  140. package/dist/toolroll-update-cli.js +19 -3
  141. package/dist/toolroll-update-ui.js +2 -2
  142. package/dist/toolroll-update.js +95 -15
  143. package/dist/up-processes.js +16 -1
  144. package/dist/up-role-child.js +2 -0
  145. package/dist/wake-hub.js +15 -6
  146. package/dist/work-index.js +101 -44
  147. package/dist/work-reads.js +53 -9
  148. package/dist/work-snapshots.js +185 -0
  149. package/dist/worker-statement-cache.js +83 -0
  150. package/dist/workspace-revision.js +145 -21
  151. package/package.json +1 -1
  152. package/dist/companion/webauthn-test-support.js +0 -44
  153. package/dist/contracts/contract-test.js +0 -55
  154. package/dist/unfenced-test-support.js +0 -6
  155. /package/dist/browser/{terminal.css → terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css} +0 -0
@@ -1,15 +1,18 @@
1
+ import { spawn } from "node:child_process";
2
+ import { randomUUID } from "node:crypto";
1
3
  import { existsSync, readFileSync } from "node:fs";
2
4
  import { homedir } from "node:os";
3
5
  import { dirname, join, resolve } from "node:path";
4
6
  import { createRequire } from "node:module";
5
7
  const sqlite = () => createRequire(import.meta.url)("node:sqlite");
6
8
  import { checkedEnvelopeJson } from "./contracts/cli.js";
7
- import { RELEASE_LIMITS, runRelease } from "./release.js";
9
+ import { readJournal, RELEASE_LIMITS, RELEASE_STEPS, runRelease, writeJournal } from "./release.js";
10
+ import { superviseRelease } from "./release-supervise.js";
8
11
  import { exec, prepareRelease, processes, readMainVersion, recordedReleaseRepo, releaseAdapters, releaseTools } from "./release-adapters.js";
9
12
  import { mainRunKey, releaseBranchFor } from "./release-main.js";
10
13
  import { journals, releaseGate } from "./release-gate.js";
11
14
  import { rescueDeployment } from "./deploy-rescue.js";
12
- import { settleDeployment, takeReleaseRun } from "./release-next.js";
15
+ import { RELEASE_WAIT_MS, settleDeployment, takeReleaseRun } from "./release-next.js";
13
16
  import { databasePath, elapsedSeconds, latestReleaseStepTask, openStore, readSchemaVersion, releaseProgressFor, revokeRelease, SCHEMA_VERSION } from "./store.js";
14
17
  import { dur, plural } from "./design/kit.js";
15
18
  import { carryRefusalWords, proveCarry, setAskEveryReleaseCommit, setUnsandboxedCarry } from "./release-carry.js";
@@ -46,10 +49,15 @@ toolroll release <branch> — the earlier way: release from a pushed branch with
46
49
  squash-merges, deletes the matching remote branch, verifies the merged tree and tags. Both macOS legs gate npm
47
50
  publication; Homebrew waits for its checks to register and all pass before merging.
48
51
 
49
- Run the same command to go on after any stop. It reads where the release got to and does the one thing that's next:
50
- finishes a deployment forward, puts one back, lifts a pause left before a swap, waits for another run of the release
51
- (naming its computer and process), or waits for your approval. It says what it did. A branch behind main is brought up
52
- to date for you; that new commit needs its own approval, CI and check.
52
+ A stop that clears on its own (GitHub not answering, Toolroll's own git busy on the checkout, CI, npm or Homebrew
53
+ catching up, an earlier operation's process ending) doesn't end the release: it goes on by itself from its journal,
54
+ within a budget the journal keeps. A failed CI job whose runner failed before the tests is run again once; any other red check waits for
55
+ you to rerun it or push a fix. Run the same command to go on after any other stop. It reads where the release got to
56
+ and does the one thing that's next: finishes a deployment forward, puts one back, lifts a pause left before a swap,
57
+ follows another run of the release (naming its computer and process), or waits for your approval. It says what it
58
+ did. A branch behind main is brought up to date for you; that new commit needs its own approval, CI and check.
59
+ --new starts over for a new commit and sets the earlier release's task aside (stopped, put away, its approval
60
+ withdrawn), refused once it began merging.
53
61
 
54
62
  A branch whose name is one of this command's own words (approve, status, nightly, carry…) is named with --branch <name>
55
63
  or after --: \`toolroll release --branch status\`.
@@ -289,7 +297,8 @@ export async function runReleaseCommand(args, write, deps = {}) {
289
297
  const lockKey = version === undefined ? branch : mainRunKey(version);
290
298
  const taken = await takeReleaseRun(stateDir, lockKey, say, deps.runLock);
291
299
  if (!taken.ok)
292
- return refuse("running", `Another release of ${version ?? branch} is still running on ${taken.holder.host} (process ${taken.holder.pid}). Nothing was changed.`, 3);
300
+ return refuse("running", `The release of ${version ?? branch} is being run by ${taken.holder.host} (process ${taken.holder.pid}), which goes on by itself; this command followed it for ${Math.max(1, Math.round((deps.runLock?.waitMs ?? RELEASE_WAIT_MS) / 60_000))} min and changed nothing. \`toolroll release status\` shows where it is.`, 3);
301
+ let next = null;
293
302
  try {
294
303
  const world = deps.settle ?? (deps.adapters === undefined ? { rescue: installedRescueWorld(checkout), gate: installedGateWorld() } : null);
295
304
  if (world !== null) {
@@ -297,13 +306,26 @@ export async function runReleaseCommand(args, write, deps = {}) {
297
306
  if (!settled.ok)
298
307
  return refuse("deployment", settled.message, 3);
299
308
  }
300
- const outcome = await runRelease({ branch: version === undefined ? branch : releaseBranchFor(version), checkout, stateDir, limits, fresh: bare.has("--new"), noCarry: bare.has("--no-carry"),
301
- ...(version === undefined ? {} : { version, full: bare.has("--full") }) }, adapters);
302
- return report(outcome, write, json, lines);
309
+ const base = { branch: version === undefined ? branch : releaseBranchFor(version), checkout, stateDir, limits, noCarry: bare.has("--no-carry"), leaseOwner: randomUUID(),
310
+ ...(version === undefined ? {} : { version, full: bare.has("--full") }) };
311
+ const outcome = await superviseRelease(await runRelease({ ...base, fresh: bare.has("--new") }, adapters), {
312
+ again: () => runRelease(base, adapters), now: () => adapters.now(), sleep: ms => adapters.sleep(ms), say, read: readJournal, write: writeJournal,
313
+ handOver: done => {
314
+ if (deps.adapters === undefined)
315
+ next = deployedBuildToContinue(done, deps.database ?? databasePath(process.env, homedir()));
316
+ return next !== null;
317
+ },
318
+ });
319
+ if (next === null)
320
+ return report(outcome, write, json, lines);
303
321
  }
304
322
  finally {
305
323
  taken.release();
306
324
  }
325
+ const bin = next;
326
+ if (!json)
327
+ write(` This build (database schema ${SCHEMA_VERSION}) can't open the database the deploy upgraded; the deployed build goes on: ${bin}`);
328
+ return continueWith(bin, args.filter(arg => arg !== "--new"), deps.cwd ?? process.cwd());
307
329
  }
308
330
  function report(outcome, write, json, lines) {
309
331
  const release = outcome.journal === null ? null : {
@@ -662,3 +684,29 @@ async function runPrepareCommand(version, flags, write, json, deps) {
662
684
  return answer(false, { reason: "refused", message: `The release pull request for ${version} wasn't opened: ${made.why}.` }, `The release pull request for ${version} wasn't opened: ${made.why}.`, 1);
663
685
  return answer(true, { version, pullRequest: made.pr, branch: made.branch, commit: made.commit }, `Opened pull request #${made.pr} (${made.branch}): the version and changelog for ${version}. Merge it like any pull request once its checks pass, then run \`toolroll release ${version}\`.`, 0);
664
686
  }
687
+ export function deployedBuildToContinue(outcome, database) {
688
+ const runtime = outcome.journal?.deploy?.runtime;
689
+ if (outcome.ok || runtime === undefined || outcome.step === "preflight" || RELEASE_STEPS.indexOf(outcome.step) <= RELEASE_STEPS.indexOf("link"))
690
+ return null;
691
+ const bin = join(runtime, "bin.js");
692
+ if (!existsSync(bin) || !existsSync(database))
693
+ return null;
694
+ try {
695
+ const db = new (sqlite().DatabaseSync)(database, { readOnly: true });
696
+ try {
697
+ const rows = db.prepare("SELECT typeof(version) AS kind, version FROM schema_version").all();
698
+ return rows.length === 1 && rows[0].kind === "integer" && Math.abs(Number(rows[0].version)) > SCHEMA_VERSION ? bin : null;
699
+ }
700
+ finally {
701
+ db.close();
702
+ }
703
+ }
704
+ catch {
705
+ return null;
706
+ }
707
+ }
708
+ const continueWith = (bin, args, cwd) => new Promise(done => {
709
+ const child = spawn(process.execPath, [bin, "release", ...args], { cwd, stdio: "inherit", env: process.env });
710
+ child.on("error", () => done(3));
711
+ child.on("exit", code => done(code ?? 3));
712
+ });
@@ -57,7 +57,7 @@ export async function takeReleaseRun(stateDir, branch, say, seams = {}) {
57
57
  continue;
58
58
  }
59
59
  if (!said) {
60
- say(`Waiting: another release of ${branch} is running on ${holder.host} (process ${holder.pid}).`);
60
+ say(`Following: the release of ${branch} is already being run by ${holder.host} (process ${holder.pid}), which goes on by itself; this command waits for it to finish (up to ${Math.max(1, Math.round((seams.waitMs ?? RELEASE_WAIT_MS) / 60_000))} min).`);
61
61
  said = true;
62
62
  }
63
63
  if (now() >= deadline)
@@ -0,0 +1,71 @@
1
+ export const TIMEOUT_POLICY = {
2
+ gate: "auto-wait",
3
+ approval: "person",
4
+ check: "person",
5
+ quiet: "auto-wait",
6
+ deploy: "person",
7
+ ci: "auto-wait",
8
+ publish: "auto-wait",
9
+ homebrew: "auto-wait",
10
+ };
11
+ export const STOP_POLICY = {
12
+ "network": "auto-resume",
13
+ "checkout-busy": "auto-resume",
14
+ "github-unreadable": "auto-resume",
15
+ "operation-in-flight": "auto-wait",
16
+ "ci-missing": "auto-wait",
17
+ "deploy-busy": "auto-wait",
18
+ "superseding": "auto-wait",
19
+ "timeout": "by-limit",
20
+ "approval": "person",
21
+ "base": "person",
22
+ "base-moved": "person",
23
+ "base-unprotected": "person",
24
+ "changelog": "person",
25
+ "check-failed": "person",
26
+ "check-level": "person",
27
+ "checkout": "person",
28
+ "ci-failed": "person",
29
+ "complete": "person",
30
+ "conflict": "person",
31
+ "deploy": "person",
32
+ "deploy-recovery": "person",
33
+ "drift": "person",
34
+ "error": "person",
35
+ "gate-held": "person",
36
+ "github-auth": "person",
37
+ "lease": "person",
38
+ "link": "person",
39
+ "merge-unknown": "person",
40
+ "merging": "person",
41
+ "mixed-runtime": "person",
42
+ "nightly-debt": "person",
43
+ "no-branch": "person",
44
+ "no-pull-request": "person",
45
+ "no-release-commit": "person",
46
+ "not-merged": "person",
47
+ "proof": "person",
48
+ "publish-failed": "person",
49
+ "release": "person",
50
+ "scope-changed": "person",
51
+ "tagging": "person",
52
+ "tree-mismatch": "person",
53
+ "usage": "person",
54
+ "version": "person",
55
+ "worktree": "person",
56
+ "write-unknown": "person",
57
+ };
58
+ export function stopPolicy(reason, waited = null) {
59
+ if (!Object.hasOwn(STOP_POLICY, reason))
60
+ return "person";
61
+ const policy = STOP_POLICY[reason];
62
+ if (policy !== "by-limit")
63
+ return policy;
64
+ return waited === null ? "person" : TIMEOUT_POLICY[waited];
65
+ }
66
+ export class NotYetPublished extends Error {
67
+ constructor(message) {
68
+ super(message);
69
+ this.name = "NotYetPublished";
70
+ }
71
+ }
@@ -0,0 +1,125 @@
1
+ import { requestTaskStop, taskControlOf } from "./task-control.js";
2
+ import { archiveTaskFamily, LIVE_RELEASE_STATES, outstandingReleaseOperations, releaseAuthority, releaseAuthorityOfTask, revokeRelease, withdrawApproval } from "./store.js";
3
+ const SUPERSEDED = "superseded by a newer release (toolroll release --new)";
4
+ class Refusal extends Error {
5
+ answer;
6
+ constructor(answer) {
7
+ super(answer.why);
8
+ this.answer = answer;
9
+ }
10
+ }
11
+ export function supersedeReleaseTask(store, input, now) {
12
+ const ref = store.lookupRef(input.taskId);
13
+ if (ref === null || store.getTask(input.taskId) === null)
14
+ return { state: "done", steps: [] };
15
+ const unbound = () => {
16
+ const now = store.lookupRef(input.taskId);
17
+ if (now === null || now.id !== ref.id)
18
+ return { state: "refused", why: `task ${input.taskId} is no longer the one this release recorded`, merging: false };
19
+ const current = releaseAuthorityOfTask(store, input.taskId)?.authority ?? null;
20
+ const recorded = input.authority === null ? null : releaseAuthority(store, input.authority.id);
21
+ for (const one of [current, recorded]) {
22
+ if (one !== null && (one.state === "merge-reserved" || one.state === "merged"))
23
+ return { state: "refused", why: `release ${one.id} already began merging`, merging: true };
24
+ }
25
+ if ((store.getScope(input.taskId)?.digest ?? null) !== input.scopeDigest)
26
+ return { state: "refused", why: `task ${input.taskId}'s scope changed since this release recorded it`, merging: false };
27
+ if ((current?.id ?? null) !== (input.authority?.id ?? null) || (current !== null && current.generation !== input.authority.generation)) {
28
+ return { state: "refused", why: `task ${input.taskId}'s release is no longer the one this release recorded`, merging: false };
29
+ }
30
+ const open = current === null ? [] : outstandingReleaseOperations(store, current.id);
31
+ if (open.length > 0)
32
+ return { state: "refused", why: `release ${current.id} still has a ${open[0].kind} in flight (operation ${open[0].id})`, merging: open.some(one => one.kind === "merge") };
33
+ return null;
34
+ };
35
+ const guarded = (write) => {
36
+ try {
37
+ return store.transact(() => {
38
+ const refused = unbound();
39
+ if (refused !== null)
40
+ throw new Refusal(refused);
41
+ return write();
42
+ });
43
+ }
44
+ catch (error) {
45
+ if (error instanceof Refusal)
46
+ return error.answer;
47
+ throw error;
48
+ }
49
+ };
50
+ const refusedNow = (value) => typeof value === "object" && value !== null && value.state === "refused";
51
+ const steps = [];
52
+ const held = guarded(() => {
53
+ if (store.handle.prepare("SELECT 1 FROM hold WHERE owner_kind = 'operator' AND owner_id = ?").get(String(ref.id)) !== undefined)
54
+ return false;
55
+ store.hold(ref.id, SUPERSEDED, null, now);
56
+ return true;
57
+ });
58
+ if (refusedNow(held))
59
+ return held;
60
+ if (held)
61
+ steps.push("held");
62
+ const control = taskControlOf(store, ref.id, now);
63
+ if (control.kind === "stop") {
64
+ let signal = null;
65
+ const asked = guarded(() => requestTaskStop(store, { taskId: input.taskId, runId: control.run, by: input.by, via: "cli", deferSignal: run => { signal = run; } }, now));
66
+ if (refusedNow(asked))
67
+ return asked;
68
+ signal?.();
69
+ if (!asked.ok && asked.reason !== "finished")
70
+ return asked.reason === "publication" ? { state: "refused", why: asked.detail, merging: false } : { state: "stopping", steps, why: asked.detail };
71
+ if (asked.ok && !asked.repeated)
72
+ steps.push("stop-requested");
73
+ return { state: "stopping", steps, why: `build #${control.run} of ${input.taskId} is stopping` };
74
+ }
75
+ if (control.kind === "stopping")
76
+ return { state: "stopping", steps, why: control.detail ?? `build #${control.run} of ${input.taskId} is still stopping` };
77
+ if (store.hasLiveClaim(ref.id, now))
78
+ return { state: "stopping", steps, why: `${input.taskId} still holds a claim` };
79
+ class Again extends Error {
80
+ why;
81
+ constructor(why) {
82
+ super(why);
83
+ this.why = why;
84
+ }
85
+ }
86
+ try {
87
+ const done = guarded(() => {
88
+ const made = [];
89
+ const cancelled = store.cancelTask(input.taskId, now, SUPERSEDED);
90
+ if (!cancelled.ok && cancelled.reason === "claimed")
91
+ throw new Again(`${input.taskId} was claimed again`);
92
+ if (cancelled.ok)
93
+ made.push("cancelled");
94
+ else if (cancelled.reason !== "already-terminal")
95
+ throw new Refusal({ state: "refused", why: `${input.taskId} couldn't be cancelled (${cancelled.reason})`, merging: false });
96
+ if (withdrawApproval(store, { where: "WHERE task_id = ? AND approved_at IS NOT NULL", args: [input.taskId] }, { basisAfter: null, clearRoute: false, clearModeDigest: false }) > 0)
97
+ made.push("approval-withdrawn");
98
+ const authority = input.authority === null ? null : releaseAuthority(store, input.authority.id);
99
+ if (authority !== null && LIVE_RELEASE_STATES.includes(authority.state)) {
100
+ const revoked = revokeRelease(store, authority.id, input.by, now);
101
+ if (!revoked.ok)
102
+ throw new Refusal({ state: "refused", why: revoked.why, merging: false });
103
+ made.push("authority-revoked");
104
+ }
105
+ const put = archiveTaskFamily(store, input.taskId, { by: input.by, byKind: "person", rule: null, reason: SUPERSEDED }, now);
106
+ if (put.ok)
107
+ made.push("archived");
108
+ else if (put.reason === "live")
109
+ throw new Again(put.message);
110
+ else if (put.reason !== "archived")
111
+ throw new Refusal({ state: "refused", why: put.message, merging: false });
112
+ if (made.length > 0)
113
+ store.recordAction({ at: now.toISOString(), actor: input.by, repo: ref.repo, taskId: input.taskId, runId: null, action: "release superseded", outcome: "done", source: "work", detail: [...steps, ...made].join(", ") });
114
+ return made;
115
+ });
116
+ if (refusedNow(done))
117
+ return done;
118
+ return { state: "done", steps: [...steps, ...done] };
119
+ }
120
+ catch (error) {
121
+ if (error instanceof Again)
122
+ return { state: "stopping", steps, why: error.why };
123
+ throw error;
124
+ }
125
+ }
@@ -0,0 +1,90 @@
1
+ import { stopPolicy } from "./release-stops.js";
2
+ export const HEAL_DEADLINE_MS = 6 * 60 * 60_000;
3
+ export const HEAL_MAX_TOTAL = 30;
4
+ export const HEAL_MAX = {
5
+ "network": 10, "checkout-busy": 12, "github-unreadable": 5,
6
+ "operation-in-flight": 6, "ci-missing": 4, "deploy-busy": 8, "superseding": 6, "timeout": 4,
7
+ "homebrew-pending": 6,
8
+ };
9
+ export const HEAL_PREFLIGHT_MAX = 5;
10
+ const PAUSE_MS = {
11
+ "auto-resume": [30_000, 60_000, 120_000, 300_000],
12
+ "auto-wait": [120_000, 300_000],
13
+ };
14
+ export function nextRun(outcome) {
15
+ if (outcome.ok) {
16
+ const j = outcome.journal;
17
+ return j.step === "homebrew" && j.published !== null && j.homebrew?.state === "pending" ? { key: "homebrew-pending", policy: "auto-wait" } : null;
18
+ }
19
+ const policy = stopPolicy(outcome.reason, outcome.waited ?? null);
20
+ return policy === "person" ? null : { key: outcome.reason, policy };
21
+ }
22
+ export function budgetFor(journal, now) {
23
+ const kept = journal.heal;
24
+ if (kept != null && kept.journal === journal.id && kept.candidate === journal.candidate.sha)
25
+ return kept;
26
+ return { journal: journal.id, candidate: journal.candidate.sha, since: now.toISOString(), deadline: new Date(now.getTime() + HEAL_DEADLINE_MS).toISOString(), counts: {}, total: 0, last: null };
27
+ }
28
+ export function exhausted(budget, key, now) {
29
+ if (now.getTime() >= Date.parse(budget.deadline))
30
+ return `it has been running itself again since ${budget.since}, up to its ${Math.round(HEAL_DEADLINE_MS / 3_600_000)}-hour limit`;
31
+ if (budget.total >= HEAL_MAX_TOTAL)
32
+ return `it already ran itself again ${budget.total} times`;
33
+ const max = HEAL_MAX[key] ?? 0;
34
+ if ((budget.counts[key] ?? 0) >= max)
35
+ return max === 0 ? "this stop is never run again by itself" : `it already ran itself again ${max} times for this`;
36
+ return null;
37
+ }
38
+ export async function superviseRelease(first, deps) {
39
+ let outcome = first;
40
+ let preflight = 0;
41
+ for (;;) {
42
+ if (deps.handOver?.(outcome) === true)
43
+ return outcome;
44
+ const next = nextRun(outcome);
45
+ if (next === null)
46
+ return outcome;
47
+ const now = deps.now();
48
+ let tries, deadline = null;
49
+ if (outcome.journal === null) {
50
+ if (preflight >= HEAL_PREFLIGHT_MAX) {
51
+ deps.say(` Toolroll stopped running it again by itself: it already tried ${preflight} times before anything began.`);
52
+ return outcome;
53
+ }
54
+ tries = ++preflight;
55
+ }
56
+ else {
57
+ const journal = deps.read(outcome.file);
58
+ if (journal === null || journal.id !== outcome.journal.id)
59
+ return outcome;
60
+ const budget = budgetFor(journal, now);
61
+ const why = exhausted(budget, next.key, now);
62
+ if (why !== null) {
63
+ deps.say(` Toolroll stopped running it again by itself: ${why}.`);
64
+ return outcome;
65
+ }
66
+ budget.counts = { ...budget.counts, [next.key]: (budget.counts[next.key] ?? 0) + 1 };
67
+ budget.total += 1;
68
+ budget.last = { reason: next.key, step: journal.step, at: now.toISOString() };
69
+ journal.heal = budget;
70
+ deps.write(outcome.file, journal);
71
+ tries = budget.counts[next.key];
72
+ deadline = Date.parse(budget.deadline);
73
+ }
74
+ const pauses = PAUSE_MS[next.policy];
75
+ const pause = Math.max(0, Math.min(pauses[Math.min(tries - 1, pauses.length - 1)], deadline === null ? Infinity : deadline - now.getTime()));
76
+ deps.say(outcome.ok
77
+ ? ` Homebrew is still pending; Toolroll finishes it by itself in ${Math.round(pause / 60_000)} min.`
78
+ : ` ${outcome.message.split("\n")[0]} Toolroll goes on by itself from ${outcome.step} in ${pause >= 60_000 ? `${Math.round(pause / 60_000)} min` : `${Math.round(pause / 1000)} s`}.`);
79
+ await deps.sleep(pause);
80
+ if (outcome.journal !== null) {
81
+ const kept = deps.read(outcome.file)?.heal;
82
+ const until = kept != null && kept.journal === outcome.journal.id ? Date.parse(kept.deadline) : deadline;
83
+ if (until !== null && !(deps.now().getTime() < until)) {
84
+ deps.say(` Toolroll stopped running it again by itself: its ${Math.round(HEAL_DEADLINE_MS / 3_600_000)}-hour limit passed.`);
85
+ return outcome;
86
+ }
87
+ }
88
+ outcome = await deps.again();
89
+ }
90
+ }