@songsid/agend 2.3.5-alpha.5 → 2.3.5-alpha.6

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 (326) hide show
  1. package/THIRD_PARTY_NOTICES.md +0 -1
  2. package/dist/accounts-profiles.d.ts +96 -0
  3. package/dist/accounts-profiles.js +178 -0
  4. package/dist/accounts-profiles.js.map +1 -0
  5. package/dist/accounts-signout.d.ts +59 -0
  6. package/dist/accounts-signout.js +184 -0
  7. package/dist/accounts-signout.js.map +1 -0
  8. package/dist/accounts-view.d.ts +54 -0
  9. package/dist/accounts-view.js +55 -11
  10. package/dist/accounts-view.js.map +1 -1
  11. package/dist/agent-workspace.d.ts +11 -0
  12. package/dist/agent-workspace.js +28 -0
  13. package/dist/agent-workspace.js.map +1 -1
  14. package/dist/auth-api.js +27 -5
  15. package/dist/auth-api.js.map +1 -1
  16. package/dist/authority/session-authorizations.d.ts +2 -0
  17. package/dist/authority/session-authorizations.js +7 -1
  18. package/dist/authority/session-authorizations.js.map +1 -1
  19. package/dist/backend/codex.js +1 -0
  20. package/dist/backend/codex.js.map +1 -1
  21. package/dist/backend/types.d.ts +2 -0
  22. package/dist/backend/types.js.map +1 -1
  23. package/dist/build-provenance.json +1 -1
  24. package/dist/bundled-tmux-guard.d.ts +19 -0
  25. package/dist/bundled-tmux-guard.js +83 -0
  26. package/dist/bundled-tmux-guard.js.map +1 -0
  27. package/dist/classic-bindings.d.ts +13 -0
  28. package/dist/classic-bindings.js +28 -2
  29. package/dist/classic-bindings.js.map +1 -1
  30. package/dist/classic-channel-manager.d.ts +2 -0
  31. package/dist/classic-channel-manager.js +5 -1
  32. package/dist/classic-channel-manager.js.map +1 -1
  33. package/dist/cli.js +116 -11
  34. package/dist/cli.js.map +1 -1
  35. package/dist/config-validator.js +11 -0
  36. package/dist/config-validator.js.map +1 -1
  37. package/dist/config.js +3 -1
  38. package/dist/config.js.map +1 -1
  39. package/dist/daemon.d.ts +27 -2
  40. package/dist/daemon.js +107 -6
  41. package/dist/daemon.js.map +1 -1
  42. package/dist/doctor.js +3 -4
  43. package/dist/doctor.js.map +1 -1
  44. package/dist/e2e-isolation.js +5 -2
  45. package/dist/e2e-isolation.js.map +1 -1
  46. package/dist/fleet-control-audit.d.ts +1 -1
  47. package/dist/fleet-manager.d.ts +129 -0
  48. package/dist/fleet-manager.js +725 -53
  49. package/dist/fleet-manager.js.map +1 -1
  50. package/dist/general-knowledge/skills/credential-profiles/SKILL.md +10 -0
  51. package/dist/hub/compose/server.js +1 -0
  52. package/dist/hub/compose/server.js.map +1 -1
  53. package/dist/hub/directory/directory.d.ts +1 -1
  54. package/dist/hub/directory/directory.js +115 -3
  55. package/dist/hub/directory/directory.js.map +1 -1
  56. package/dist/hub/directory/groups.d.ts +29 -0
  57. package/dist/hub/directory/groups.js +29 -0
  58. package/dist/hub/directory/groups.js.map +1 -0
  59. package/dist/hub/directory/index.d.ts +1 -0
  60. package/dist/hub/directory/types.d.ts +4 -0
  61. package/dist/hub/directory/types.js.map +1 -1
  62. package/dist/hub/group-policy/model.d.ts +56 -0
  63. package/dist/hub/group-policy/model.js +46 -0
  64. package/dist/hub/group-policy/model.js.map +1 -0
  65. package/dist/hub/group-policy/resources.d.ts +52 -0
  66. package/dist/hub/group-policy/resources.js +166 -0
  67. package/dist/hub/group-policy/resources.js.map +1 -0
  68. package/dist/hub/group-policy/store.d.ts +52 -0
  69. package/dist/hub/group-policy/store.js +149 -0
  70. package/dist/hub/group-policy/store.js.map +1 -0
  71. package/dist/hub/http/compose.js +4 -3
  72. package/dist/hub/http/compose.js.map +1 -1
  73. package/dist/hub/pairing.d.ts +3 -1
  74. package/dist/hub/pairing.js +6 -3
  75. package/dist/hub/pairing.js.map +1 -1
  76. package/dist/hub/server.d.ts +2 -0
  77. package/dist/hub/server.js +12 -2
  78. package/dist/hub/server.js.map +1 -1
  79. package/dist/instance-config-impact.js +4 -3
  80. package/dist/instance-config-impact.js.map +1 -1
  81. package/dist/instance-lifecycle.d.ts +17 -0
  82. package/dist/instance-lifecycle.js +38 -1
  83. package/dist/instance-lifecycle.js.map +1 -1
  84. package/dist/locale.js +21 -0
  85. package/dist/locale.js.map +1 -1
  86. package/dist/login-controller.d.ts +5 -0
  87. package/dist/login-controller.js +11 -0
  88. package/dist/login-controller.js.map +1 -1
  89. package/dist/login-window-lock.d.ts +2 -1
  90. package/dist/login-window-lock.js.map +1 -1
  91. package/dist/ls-rows.d.ts +1 -1
  92. package/dist/ls-rows.js +1 -1
  93. package/dist/ls-rows.js.map +1 -1
  94. package/dist/machine-accounts.d.ts +110 -0
  95. package/dist/machine-accounts.js +229 -0
  96. package/dist/machine-accounts.js.map +1 -0
  97. package/dist/machine-signins.d.ts +89 -0
  98. package/dist/machine-signins.js +182 -0
  99. package/dist/machine-signins.js.map +1 -0
  100. package/dist/muse-usage-relay.d.ts +2 -0
  101. package/dist/muse-usage-relay.js +5 -3
  102. package/dist/muse-usage-relay.js.map +1 -1
  103. package/dist/needs-you-hub.d.ts +3 -0
  104. package/dist/needs-you-hub.js +13 -3
  105. package/dist/needs-you-hub.js.map +1 -1
  106. package/dist/needs-you.d.ts +35 -2
  107. package/dist/needs-you.js +19 -0
  108. package/dist/needs-you.js.map +1 -1
  109. package/dist/quickstart.js +7 -18
  110. package/dist/quickstart.js.map +1 -1
  111. package/dist/quiet-run.d.ts +31 -0
  112. package/dist/quiet-run.js +50 -0
  113. package/dist/quiet-run.js.map +1 -0
  114. package/dist/quota-notice-config.d.ts +22 -0
  115. package/dist/quota-notice-config.js +41 -0
  116. package/dist/quota-notice-config.js.map +1 -0
  117. package/dist/runtime-api.d.ts +13 -0
  118. package/dist/runtime-api.js +125 -1
  119. package/dist/runtime-api.js.map +1 -1
  120. package/dist/runtime-build-info.d.ts +2 -1
  121. package/dist/runtime-build-info.js +7 -3
  122. package/dist/runtime-build-info.js.map +1 -1
  123. package/dist/runtime-cli-install.d.ts +153 -0
  124. package/dist/runtime-cli-install.js +675 -0
  125. package/dist/runtime-cli-install.js.map +1 -0
  126. package/dist/runtime-cli.js +157 -12
  127. package/dist/runtime-cli.js.map +1 -1
  128. package/dist/runtime-client.d.ts +64 -2
  129. package/dist/runtime-client.js +277 -20
  130. package/dist/runtime-client.js.map +1 -1
  131. package/dist/runtime-coordinator.d.ts +10 -1
  132. package/dist/runtime-coordinator.js +93 -2
  133. package/dist/runtime-coordinator.js.map +1 -1
  134. package/dist/runtime-credential-profiles.d.ts +11 -2
  135. package/dist/runtime-credential-profiles.js +28 -4
  136. package/dist/runtime-credential-profiles.js.map +1 -1
  137. package/dist/runtime-install.js +9 -6
  138. package/dist/runtime-install.js.map +1 -1
  139. package/dist/runtime-pane-link.js +2 -1
  140. package/dist/runtime-pane-link.js.map +1 -1
  141. package/dist/runtime-pane-service.js +2 -2
  142. package/dist/runtime-pane-service.js.map +1 -1
  143. package/dist/runtime-pane-store.d.ts +10 -0
  144. package/dist/runtime-pane-store.js +13 -1
  145. package/dist/runtime-pane-store.js.map +1 -1
  146. package/dist/runtime-profile-ownership.d.ts +66 -0
  147. package/dist/runtime-profile-ownership.js +391 -0
  148. package/dist/runtime-profile-ownership.js.map +1 -0
  149. package/dist/runtime-protocol.d.ts +88 -2
  150. package/dist/runtime-protocol.js +40 -0
  151. package/dist/runtime-protocol.js.map +1 -1
  152. package/dist/runtime-remote-accounts.d.ts +197 -0
  153. package/dist/runtime-remote-accounts.js +663 -0
  154. package/dist/runtime-remote-accounts.js.map +1 -0
  155. package/dist/runtime-remote-setup.d.ts +105 -0
  156. package/dist/runtime-remote-setup.js +529 -0
  157. package/dist/runtime-remote-setup.js.map +1 -0
  158. package/dist/runtime-signin-transport.d.ts +39 -0
  159. package/dist/runtime-signin-transport.js +110 -0
  160. package/dist/runtime-signin-transport.js.map +1 -0
  161. package/dist/runtime-signin.d.ts +132 -0
  162. package/dist/runtime-signin.js +289 -0
  163. package/dist/runtime-signin.js.map +1 -0
  164. package/dist/runtime-update-attention.d.ts +4 -0
  165. package/dist/runtime-update-attention.js +11 -0
  166. package/dist/runtime-update-attention.js.map +1 -0
  167. package/dist/runtime-update-controller.d.ts +75 -0
  168. package/dist/runtime-update-controller.js +691 -0
  169. package/dist/runtime-update-controller.js.map +1 -0
  170. package/dist/runtime-update-ipc.d.ts +42 -0
  171. package/dist/runtime-update-ipc.js +144 -0
  172. package/dist/runtime-update-ipc.js.map +1 -0
  173. package/dist/runtime-update-journal.d.ts +5 -0
  174. package/dist/runtime-update-journal.js +7 -0
  175. package/dist/runtime-update-journal.js.map +1 -1
  176. package/dist/runtime-update-lock.js +17 -2
  177. package/dist/runtime-update-lock.js.map +1 -1
  178. package/dist/runtime-update-status.d.ts +30 -0
  179. package/dist/runtime-update-status.js +31 -0
  180. package/dist/runtime-update-status.js.map +1 -0
  181. package/dist/settings-api.js +3 -0
  182. package/dist/settings-api.js.map +1 -1
  183. package/dist/settings-change.js +8 -0
  184. package/dist/settings-change.js.map +1 -1
  185. package/dist/settings-instance-patch.js +1 -1
  186. package/dist/settings-instance-patch.js.map +1 -1
  187. package/dist/setup-wizard.js +3 -5
  188. package/dist/setup-wizard.js.map +1 -1
  189. package/dist/signin-keepalive.d.ts +203 -0
  190. package/dist/signin-keepalive.js +652 -0
  191. package/dist/signin-keepalive.js.map +1 -0
  192. package/dist/structured-signin-transport.js +4 -0
  193. package/dist/structured-signin-transport.js.map +1 -1
  194. package/dist/structured-signin.d.ts +7 -0
  195. package/dist/structured-signin.js +1 -1
  196. package/dist/structured-signin.js.map +1 -1
  197. package/dist/tmux-binary.d.ts +23 -23
  198. package/dist/tmux-binary.js +35 -46
  199. package/dist/tmux-binary.js.map +1 -1
  200. package/dist/tmux-exec-helper.js +3 -3
  201. package/dist/tmux-exec-helper.js.map +1 -1
  202. package/dist/tmux-legacy.d.ts +94 -0
  203. package/dist/tmux-legacy.js +309 -0
  204. package/dist/tmux-legacy.js.map +1 -0
  205. package/dist/tmux-manager.d.ts +7 -1
  206. package/dist/tmux-manager.js +21 -11
  207. package/dist/tmux-manager.js.map +1 -1
  208. package/dist/tmux-rollback.d.ts +31 -0
  209. package/dist/tmux-rollback.js +72 -0
  210. package/dist/tmux-rollback.js.map +1 -0
  211. package/dist/tmux-server.d.ts +102 -0
  212. package/dist/tmux-server.js +248 -0
  213. package/dist/tmux-server.js.map +1 -0
  214. package/dist/transcript-monitor.js +43 -5
  215. package/dist/transcript-monitor.js.map +1 -1
  216. package/dist/transcript-records.d.ts +2 -1
  217. package/dist/transcript-records.js +3 -0
  218. package/dist/transcript-records.js.map +1 -1
  219. package/dist/transcript-sources.js +3 -0
  220. package/dist/transcript-sources.js.map +1 -1
  221. package/dist/types.d.ts +3 -0
  222. package/dist/ui/accounts-keepalive.js +112 -0
  223. package/dist/ui/accounts-manage.js +237 -0
  224. package/dist/ui/accounts-view.js +175 -31
  225. package/dist/ui/agent-runtime-settings.js +2 -1
  226. package/dist/ui/app.html +8 -1
  227. package/dist/ui/hub-attachments.js +90 -0
  228. package/dist/ui/hub-chat-store.js +9 -4
  229. package/dist/ui/hub-client.js +20 -1
  230. package/dist/ui/hub-preview.js +101 -0
  231. package/dist/ui/machine-signin.js +56 -0
  232. package/dist/ui/panel-fleet.js +1 -1
  233. package/dist/ui/panel-hub.js +126 -31
  234. package/dist/ui/panel-needs.js +14 -8
  235. package/dist/ui/panel-notices.js +4 -3
  236. package/dist/ui/panel-runtimes.js +19 -6
  237. package/dist/ui/panel-settings.js +12 -6
  238. package/dist/ui/panel-windows-agent.js +2 -1
  239. package/dist/ui/preview-pages.js +3 -41
  240. package/dist/ui/preview-panel-actions.js +3 -161
  241. package/dist/ui/preview-panel.js +10 -557
  242. package/dist/ui/preview-versions.js +3 -155
  243. package/dist/ui/runtime-cli-install.js +114 -0
  244. package/dist/ui/runtime-update-commands.d.ts +2 -0
  245. package/dist/ui/runtime-update-commands.js +11 -0
  246. package/dist/ui/runtime-update-status.js +53 -0
  247. package/dist/ui/search-dialog.js +3 -2
  248. package/dist/ui/settings-guests.js +3 -2
  249. package/dist/ui/settings-hub-browser-certificate.js +2 -0
  250. package/dist/ui/settings-hub-membership.js +12 -6
  251. package/dist/ui/settings-hub-retention.js +2 -0
  252. package/dist/ui/settings-hub.js +11 -3
  253. package/dist/ui/settings-quota-notice.js +100 -0
  254. package/dist/ui/settings-registry.js +1 -0
  255. package/dist/ui/settings-strings.js +2 -2
  256. package/dist/ui/settings-web-authority.js +2 -1
  257. package/dist/ui/shared/accounts-keepalive.css +7 -0
  258. package/dist/ui/shared/accounts.css +13 -0
  259. package/dist/ui/shared/app-route.js +1 -1
  260. package/dist/ui/shared/app-shell.js +9 -3
  261. package/dist/ui/shared/app-store.js +2 -2
  262. package/dist/ui/shared/app.css +13 -0
  263. package/dist/ui/shared/app.js +2 -1
  264. package/dist/ui/shared/hub-chat.css +1 -0
  265. package/dist/ui/shared/hub-owner.css +1 -1
  266. package/dist/ui/shared/loading.css +37 -0
  267. package/dist/ui/shared/preview-pages.js +41 -0
  268. package/dist/ui/shared/preview-panel-actions.js +168 -0
  269. package/dist/ui/shared/preview-panel.js +565 -0
  270. package/dist/ui/shared/preview-versions.js +155 -0
  271. package/dist/ui/shared/runtime-install.css +4 -0
  272. package/dist/ui/shared/signin.js +68 -6
  273. package/dist/ui/shared/ui-states.js +14 -3
  274. package/dist/ui/shared/workspace-follow.css +19 -0
  275. package/dist/ui/signin.html +7 -3
  276. package/dist/ui/structured-signin.js +31 -7
  277. package/dist/ui/workspace-follow-strings.js +28 -0
  278. package/dist/ui/workspace-panel.js +225 -36
  279. package/dist/update-install.d.ts +7 -0
  280. package/dist/update-install.js +23 -0
  281. package/dist/update-install.js.map +1 -1
  282. package/dist/usage/providers.d.ts +5 -0
  283. package/dist/usage/providers.js +5 -0
  284. package/dist/usage/providers.js.map +1 -1
  285. package/dist/usage/quota-notices.d.ts +38 -0
  286. package/dist/usage/quota-notices.js +160 -0
  287. package/dist/usage/quota-notices.js.map +1 -0
  288. package/dist/usage/usage-api.d.ts +1 -0
  289. package/dist/usage/usage-api.js +16 -0
  290. package/dist/usage/usage-api.js.map +1 -1
  291. package/dist/web-api.d.ts +33 -0
  292. package/dist/web-api.js +342 -8
  293. package/dist/web-api.js.map +1 -1
  294. package/dist/web-login.d.ts +35 -1
  295. package/dist/web-login.js +51 -5
  296. package/dist/web-login.js.map +1 -1
  297. package/dist/web-session.d.ts +11 -0
  298. package/dist/web-session.js +38 -1
  299. package/dist/web-session.js.map +1 -1
  300. package/dist/web-shell-routes.d.ts +1 -1
  301. package/dist/web-shell-routes.js +1 -1
  302. package/dist/web-shell-routes.js.map +1 -1
  303. package/dist/web-signin-link.d.ts +42 -0
  304. package/dist/web-signin-link.js +110 -0
  305. package/dist/web-signin-link.js.map +1 -0
  306. package/dist/windows-cli-api.d.ts +6 -0
  307. package/dist/windows-cli-api.js +11 -0
  308. package/dist/windows-cli-api.js.map +1 -1
  309. package/dist/windows-cli-worker.d.ts +3 -1
  310. package/dist/windows-cli-worker.js +18 -3
  311. package/dist/windows-cli-worker.js.map +1 -1
  312. package/dist/workspace-follow-parse.d.ts +36 -0
  313. package/dist/workspace-follow-parse.js +208 -0
  314. package/dist/workspace-follow-parse.js.map +1 -0
  315. package/dist/workspace-follow-records.d.ts +17 -0
  316. package/dist/workspace-follow-records.js +88 -0
  317. package/dist/workspace-follow-records.js.map +1 -0
  318. package/dist/workspace-follow.d.ts +41 -0
  319. package/dist/workspace-follow.js +143 -0
  320. package/dist/workspace-follow.js.map +1 -0
  321. package/dist/wsl-detect.d.ts +1 -0
  322. package/dist/wsl-detect.js +20 -0
  323. package/dist/wsl-detect.js.map +1 -0
  324. package/launcher/tmux-bundle.cjs +26 -10
  325. package/package.json +6 -2
  326. package/templates/runtime-install/install.ps1 +154 -13
@@ -0,0 +1,691 @@
1
+ /** Managed Runtime code replacement. Source of truth is the stored paired origin; no CLI installation or task replay. */
2
+ import { randomUUID } from "node:crypto";
3
+ import { mkdirSync } from "node:fs";
4
+ import { basename, dirname, join, resolve } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import { spawn } from "./child-process.js";
7
+ import { RuntimeClient, describeRuntimeStop } from "./runtime-client.js";
8
+ import { RuntimeClientStore, validateRuntimeServer } from "./runtime-client-store.js";
9
+ import { credentialProfilesRoot } from "./runtime-credential-profiles.js";
10
+ import { nativeCliEnvironment } from "./windows-cli-worker.js";
11
+ import { fetchRuntimeUpdateArchive, fetchRuntimeUpdateMetadata, runtimeUpdateNeeded, RuntimeUpdateArtifactError } from "./runtime-update-artifact.js";
12
+ import { RuntimeUpdateJournal } from "./runtime-update-journal.js";
13
+ import { RuntimeUpdateIpc, parseRuntimeUpdateIpc } from "./runtime-update-ipc.js";
14
+ import { acquireRuntimeInstallLock, assertRuntimeInstallDirectory, observeRuntimeProcess, runtimeProcessMatches } from "./runtime-update-lock.js";
15
+ import { readRuntimeInstallReceipt, readRuntimeInstalledProgram, readRuntimeSelectedProgram, selectRuntimeProgram, stageRuntimeUpdate } from "./runtime-update-install.js";
16
+ import { adoptRuntimeInstallOwner, assertRuntimeInstallExclusive, attachRuntimeInstallChild, readRuntimeInstallOwner, registerRuntimeInstallOwner, releaseRuntimeInstallOwner, RuntimeInstallOwnerError } from "./runtime-update-owners.js";
17
+ const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
18
+ const sleep = (ms) => new Promise(done => setTimeout(done, ms));
19
+ const REINSTALL = "Automatic updates need a fresh, separate install. Use the one-line command in AgEnD Settings > Runtime. Your pairing is kept.";
20
+ function journalAt(store, binding) {
21
+ // A fresh separate installation keeps the pairing, but must not inherit another installation's failed/recovery state.
22
+ let directory = store.directory;
23
+ assertRuntimeInstallDirectory(directory);
24
+ for (const part of ["runtime-updates", binding.installId, binding.machineId]) {
25
+ const path = join(directory, part);
26
+ try {
27
+ mkdirSync(path, { mode: 0o700 });
28
+ }
29
+ catch (error) {
30
+ if (error.code !== "EEXIST")
31
+ throw error;
32
+ }
33
+ assertRuntimeInstallDirectory(path);
34
+ directory = path;
35
+ }
36
+ return new RuntimeUpdateJournal(directory, binding);
37
+ }
38
+ function layout(moduleDirectory) {
39
+ const programDirectory = dirname(resolve(moduleDirectory)), root = dirname(programDirectory), folder = basename(programDirectory);
40
+ if (basename(moduleDirectory) !== "dist")
41
+ return undefined;
42
+ const receipt = readRuntimeInstallReceipt(root);
43
+ if (!receipt)
44
+ return undefined;
45
+ let program;
46
+ try {
47
+ program = readRuntimeInstalledProgram(root, folder);
48
+ }
49
+ catch { /* Still register a running managed client; it must not become invisible to another updater. */ }
50
+ return { root, folder, installId: receipt.id, ...(program ? { program } : {}) };
51
+ }
52
+ function processPeer(value) {
53
+ return { get connected() { return value.connected; }, on: (event, listener) => value.on(event, listener), off: (event, listener) => value.off(event, listener),
54
+ send: (message, callback) => value.send(message, callback) };
55
+ }
56
+ class RuntimeUpdateCancelled extends Error {
57
+ constructor() { super("Runtime update controller closed"); }
58
+ }
59
+ export class RuntimeUpdateController {
60
+ options;
61
+ install;
62
+ self;
63
+ observe;
64
+ report;
65
+ binding;
66
+ journal;
67
+ owner;
68
+ client;
69
+ authentication;
70
+ work;
71
+ lifecycle;
72
+ timer;
73
+ closed = false;
74
+ handedOff = false;
75
+ intervalAt = 0;
76
+ lastReport = "";
77
+ trialPending = false;
78
+ closePromise;
79
+ retainedLease;
80
+ ownerReleased = false;
81
+ constructor(options, install, self, owner, binding) {
82
+ this.options = options;
83
+ this.install = install;
84
+ this.self = self;
85
+ this.owner = structuredClone(owner);
86
+ this.binding = structuredClone(binding);
87
+ this.observe = options.observe ?? observeRuntimeProcess;
88
+ this.report = options.report ?? (() => { });
89
+ this.journal = journalAt(options.store, this.binding);
90
+ }
91
+ /** Registers before RuntimeClient.start; valid managed installs never run an untracked client. */
92
+ static async open(options) {
93
+ const report = options.report ?? (() => { });
94
+ let install;
95
+ try {
96
+ install = layout(options.moduleDirectory ?? dirname(fileURLToPath(import.meta.url)));
97
+ }
98
+ catch { /* legacy/unverifiable layout */ }
99
+ if (!install) {
100
+ report(REINSTALL);
101
+ return undefined;
102
+ }
103
+ if (!install.program)
104
+ report(REINSTALL);
105
+ const credentials = options.store.credentials();
106
+ if (!credentials)
107
+ throw Error("Pair this Runtime before starting it");
108
+ const binding = { origin: validateRuntimeServer(credentials.server).origin, machineId: credentials.machineId, installId: install.installId };
109
+ const observe = options.observe ?? observeRuntimeProcess, observed = await observe(process.pid);
110
+ if (observed.state !== "alive" || observed.proof.pid !== process.pid || runtimeProcessMatches(observed.proof, observed) !== true)
111
+ throw Error("Runtime process ownership is unconfirmed; no managed client was started");
112
+ const self = { ...observed.proof }, lock = await acquireRuntimeInstallLock(install.root, { ownProof: self, observe });
113
+ let owner;
114
+ try {
115
+ owner = await registerRuntimeInstallOwner(install.root, binding.origin, lock, observe);
116
+ const controller = new RuntimeUpdateController(options, install, self, owner, binding);
117
+ if (controller.journal.read().attempt)
118
+ throw Error(`Runtime update recovery is unfinished. ${REINSTALL}`);
119
+ return controller;
120
+ }
121
+ catch (error) {
122
+ if (owner)
123
+ await releaseRuntimeInstallOwner(install.root, owner, lock, observe);
124
+ throw error;
125
+ }
126
+ finally {
127
+ lock.release();
128
+ }
129
+ }
130
+ /** The new controller is initially only a helper. It cannot start a client before its exact old parent exits. */
131
+ static async resume(options, init, channel, cancelled = () => false) {
132
+ const snapshot = parseRuntimeUpdateIpc(init);
133
+ if (snapshot.type !== "init" || snapshot.profile !== options.store.directory)
134
+ throw Error("Runtime handoff profile changed");
135
+ init = snapshot;
136
+ const install = layout(options.moduleDirectory ?? dirname(fileURLToPath(import.meta.url)));
137
+ if (!install?.program)
138
+ throw Error("Runtime update program is unverified");
139
+ const store = options.store, credentials = store.credentials();
140
+ if (!credentials)
141
+ throw Error("Runtime pairing is unavailable");
142
+ const binding = { origin: validateRuntimeServer(credentials.server).origin, machineId: credentials.machineId, installId: install.installId };
143
+ const observe = options.observe ?? observeRuntimeProcess, observed = await observe(process.pid);
144
+ if (observed.state !== "alive" || observed.proof.pid !== process.pid || runtimeProcessMatches(observed.proof, observed) !== true)
145
+ throw Error("Runtime child identity is unconfirmed");
146
+ const self = { ...observed.proof }, expected = readRuntimeInstallOwner(install.root, init.ownerId), journal = journalAt(store, binding), attempt = journal.read().attempt;
147
+ if (!attempt || attempt.id !== init.id || attempt.ownerId !== expected.id || expected.installId !== install.installId || expected.origin !== binding.origin
148
+ || expected.parent.pid !== process.ppid || !expected.child || runtimeProcessMatches(expected.child, { state: "alive", proof: self }) !== true
149
+ || !same(install.program, init.role === "helper" ? attempt.next : attempt.old)
150
+ || attempt.phase !== (init.role === "helper" ? "handoff" : "rollback"))
151
+ throw Error("Runtime handoff binding changed");
152
+ if (cancelled())
153
+ throw Error("Runtime handoff was cancelled");
154
+ await channel.send({ v: 1, id: init.id, type: "armed" });
155
+ const deadline = Date.now() + 30_000;
156
+ for (;;) {
157
+ if (cancelled())
158
+ throw Error("Runtime handoff was cancelled");
159
+ if (runtimeProcessMatches(expected.parent, await observe(expected.parent.pid)) === false)
160
+ break;
161
+ if (Date.now() >= deadline)
162
+ throw Error("Previous Runtime process exit is unconfirmed");
163
+ await sleep(250);
164
+ }
165
+ channel.close();
166
+ const lock = await acquireRuntimeInstallLock(install.root, { ownProof: self, observe });
167
+ let owner;
168
+ try {
169
+ owner = await adoptRuntimeInstallOwner(install.root, expected, lock, observe);
170
+ const controller = new RuntimeUpdateController(options, install, self, owner, binding);
171
+ controller.current(lock);
172
+ if (init.role === "rollback")
173
+ journal.fail(init.id, () => controller.current(lock));
174
+ return controller;
175
+ }
176
+ finally {
177
+ lock.release();
178
+ }
179
+ }
180
+ bindingCurrent() {
181
+ const value = this.options.store.credentials(), receipt = readRuntimeInstallReceipt(this.install.root);
182
+ if (!value || validateRuntimeServer(value.server).origin !== this.binding.origin || value.machineId !== this.binding.machineId || receipt?.id !== this.binding.installId)
183
+ throw Error("Runtime update binding changed");
184
+ }
185
+ current(lock) {
186
+ lock.assert();
187
+ this.bindingCurrent();
188
+ if (!same(readRuntimeInstallOwner(this.install.root, this.owner.id), this.owner) || runtimeProcessMatches(this.owner.parent, { state: "alive", proof: this.self }) !== true)
189
+ throw Error("Runtime update ownership changed");
190
+ }
191
+ running(lock) {
192
+ if (this.closed)
193
+ throw new RuntimeUpdateCancelled();
194
+ this.current(lock);
195
+ }
196
+ async locked(job) {
197
+ const lock = this.retainedLease ?? await acquireRuntimeInstallLock(this.install.root, { ownProof: this.self, observe: this.observe });
198
+ this.retainedLease = lock;
199
+ try {
200
+ this.current(lock);
201
+ return await job(lock);
202
+ }
203
+ finally {
204
+ lock.release();
205
+ if (this.retainedLease === lock)
206
+ this.retainedLease = undefined;
207
+ }
208
+ }
209
+ say(message) { if (message !== this.lastReport) {
210
+ this.lastReport = message;
211
+ this.report(message);
212
+ } }
213
+ status(value) { if (!this.closed)
214
+ this.client?.setUpdateStatus(value); }
215
+ initialStatus() {
216
+ if (!this.install.program)
217
+ return { state: "manual", reason: "damaged" };
218
+ const state = this.journal.read(), target = state.attempt?.next ?? state.failed;
219
+ return target ? { state: state.attempt ? "pending" : "failed", target: { version: target.version, bundleSha256: target.sha256 } } : { state: "unknown" };
220
+ }
221
+ newClient(trialId) {
222
+ this.authentication = undefined;
223
+ this.trialPending = !!trialId;
224
+ return (this.options.makeClient ?? (value => new RuntimeClient(value)))({ store: this.options.store, credentialProfilesRoot: credentialProfilesRoot(this.options.store.directory), report: this.report, updateStatus: this.initialStatus(),
225
+ managedUpdate: { ...(trialId ? { trialId } : {}), authenticated: value => { this.authentication = structuredClone(value); if (!this.trialPending)
226
+ this.checkSoon(); } } });
227
+ }
228
+ ownLifecycle(job) {
229
+ if (this.lifecycle)
230
+ return Promise.reject(Error("Runtime lifecycle work is already pending"));
231
+ const work = Promise.resolve().then(job);
232
+ this.lifecycle = work;
233
+ void work.finally(() => { if (this.lifecycle === work)
234
+ this.lifecycle = undefined; }).catch(() => { });
235
+ return work;
236
+ }
237
+ start() { return this.ownLifecycle(() => this.startOwned()); }
238
+ async startOwned() {
239
+ if (this.closed || this.client)
240
+ throw Error("Runtime controller cannot start twice");
241
+ this.client = this.newClient();
242
+ if (this.closed)
243
+ return;
244
+ await this.client.start();
245
+ if (this.closed)
246
+ return;
247
+ this.timer = setInterval(() => this.checkSoon(), this.options.pollMs ?? 5 * 60_000);
248
+ this.timer.unref();
249
+ }
250
+ startTrial(id) { return this.ownLifecycle(() => this.startTrialOwned(id)); }
251
+ async startTrialOwned(id) {
252
+ if (this.closed || this.client || !this.install.program)
253
+ throw Error("Runtime trial cannot start");
254
+ let activationIntent = false;
255
+ this.client = this.newClient(id);
256
+ try {
257
+ if (this.closed)
258
+ return;
259
+ await this.locked(async (lock) => {
260
+ this.running(lock);
261
+ const attempt = this.journal.read().attempt;
262
+ if (!attempt || attempt.id !== id || attempt.ownerId !== this.owner.id || attempt.phase !== "handoff")
263
+ throw Error("Runtime trial binding changed");
264
+ await assertRuntimeInstallExclusive(this.install.root, this.owner, lock, this.observe);
265
+ this.running(lock);
266
+ this.journal.advance(id, "trial", () => this.running(lock));
267
+ selectRuntimeProgram(this.install.root, attempt.old, attempt.next, lock);
268
+ });
269
+ if (this.closed)
270
+ return;
271
+ await this.client.start();
272
+ const deadline = Date.now() + 30_000;
273
+ while (!this.authentication && !this.closed && !this.client.stopReason) {
274
+ if (Date.now() >= deadline)
275
+ throw Error("Runtime trial connection failed");
276
+ await sleep(100);
277
+ }
278
+ const auth = this.authentication, program = this.install.program;
279
+ if (this.closed)
280
+ return;
281
+ if (!auth || auth.updateProtocol !== 1 || auth.bootId !== this.client.bootId || !Number.isSafeInteger(auth.generation) || auth.generation < 1
282
+ || auth.runtimeInfo.version !== program.version || auth.runtimeInfo.buildId !== program.buildId)
283
+ throw Error("Runtime trial identity or health failed");
284
+ // Set before the durable write: a write error can have an unknown committed outcome.
285
+ activationIntent = true;
286
+ await this.locked(async (lock) => { this.journal.advance(id, "activating", () => this.running(lock)); });
287
+ while (!this.closed && !this.client.stopReason) {
288
+ try {
289
+ const activated = await this.client.activateTrial(id);
290
+ if (this.closed)
291
+ return;
292
+ if (activated) {
293
+ await this.locked(async (lock) => {
294
+ this.running(lock);
295
+ const state = this.journal.read();
296
+ if (state.attempt?.phase === "activating")
297
+ this.journal.advance(id, "active", () => this.running(lock));
298
+ if (this.journal.read().attempt)
299
+ this.journal.complete(id, () => this.running(lock));
300
+ });
301
+ if (this.closed)
302
+ return;
303
+ this.trialPending = false;
304
+ this.say("Runtime update connected and activated.");
305
+ if (this.closed)
306
+ return;
307
+ this.status({ state: "current" });
308
+ this.timer = setInterval(() => this.checkSoon(), this.options.pollMs ?? 5 * 60_000);
309
+ this.timer.unref();
310
+ this.checkSoon();
311
+ return;
312
+ }
313
+ }
314
+ catch { /* Exact-ID retry; a lost acknowledgement never grants rollback. */ }
315
+ if (this.closed)
316
+ return;
317
+ this.say("Runtime activation is unconfirmed. Keeping the selected build and retrying the same attempt.");
318
+ await sleep(1000);
319
+ }
320
+ }
321
+ catch (error) {
322
+ if (error instanceof RuntimeUpdateCancelled && this.closed)
323
+ return;
324
+ if (!activationIntent && !this.closed) {
325
+ await this.rollback(id);
326
+ return;
327
+ }
328
+ throw error;
329
+ }
330
+ }
331
+ async rollback(id) {
332
+ // This method is reachable only while the candidate is still gated, before any activation intent.
333
+ await this.client?.stop();
334
+ if (this.closed)
335
+ return;
336
+ let child, transferred = false;
337
+ try {
338
+ await this.locked(async (lock) => {
339
+ this.running(lock);
340
+ const attempt = this.journal.read().attempt;
341
+ if (!attempt || attempt.id !== id || !["prepared", "handoff", "trial"].includes(attempt.phase))
342
+ throw Error("Runtime rollback is unconfirmed");
343
+ this.journal.advance(id, "rollback", () => this.running(lock));
344
+ const selected = readRuntimeSelectedProgram(this.install.root);
345
+ if (same(selected, attempt.next))
346
+ selectRuntimeProgram(this.install.root, attempt.next, attempt.old, lock);
347
+ // A lockless manual installer wins. Never overwrite a third selection while returning to the verified old code.
348
+ child = await this.spawnChild(attempt.old, id, lock);
349
+ });
350
+ if (this.closed)
351
+ return;
352
+ if (!child)
353
+ throw Error("Runtime rollback did not start");
354
+ await this.arm(child, { v: 1, id, type: "init", role: "rollback", ownerId: this.owner.id, profile: this.options.store.directory });
355
+ if (this.closed)
356
+ return;
357
+ this.say("Runtime update failed. Returning to the previous verified build; pairing and task records are kept.");
358
+ if (this.closed)
359
+ return;
360
+ this.handedOff = true;
361
+ transferred = true;
362
+ child.channel.close();
363
+ if (child.process.connected)
364
+ child.process.disconnect();
365
+ child.process.unref();
366
+ }
367
+ catch (error) {
368
+ if (!(error instanceof RuntimeUpdateCancelled && this.closed))
369
+ throw error;
370
+ }
371
+ finally {
372
+ // A helper cannot admit while this parent lives. Shutdown must positively stop it,
373
+ // never leave it armed to adopt the row when this parent exits.
374
+ if (child && !transferred)
375
+ await this.stopUnadmitted(child);
376
+ }
377
+ }
378
+ checkSoon() {
379
+ if (this.closed || this.work || this.handedOff || Date.now() < this.intervalAt)
380
+ return;
381
+ // Own the promise before any callback can re-enter stop/start.
382
+ const work = Promise.resolve().then(() => this.check());
383
+ this.work = work;
384
+ void work.catch(error => {
385
+ if (error instanceof RuntimeInstallOwnerError && ["shared", "owned", "unknown"].includes(error.reason))
386
+ this.status({ state: "manual", reason: error.reason === "shared" ? "shared" : "another-runtime" });
387
+ this.say("Runtime update check is unavailable; it will retry from the paired AgEnD.");
388
+ }).finally(() => { if (this.work === work)
389
+ this.work = undefined; });
390
+ }
391
+ async checkNow() {
392
+ this.intervalAt = 0;
393
+ this.checkSoon();
394
+ const work = this.work;
395
+ try {
396
+ await work;
397
+ }
398
+ finally {
399
+ if (this.work === work)
400
+ this.work = undefined;
401
+ }
402
+ }
403
+ async check() {
404
+ this.intervalAt = Date.now() + 30_000;
405
+ const client = this.client, program = this.install.program, authentication = this.authentication;
406
+ if (this.closed || this.trialPending || !client?.online() || this.authentication?.updateProtocol !== 1 || !program)
407
+ return;
408
+ this.bindingCurrent();
409
+ const state = this.journal.read();
410
+ if (state.attempt) {
411
+ this.say("Runtime update recovery is pending; the selected build and journal were preserved.");
412
+ return;
413
+ }
414
+ await this.locked(async (lock) => {
415
+ await assertRuntimeInstallExclusive(this.install.root, this.owner, lock, this.observe);
416
+ if (!same(readRuntimeSelectedProgram(this.install.root), program))
417
+ throw Error("Runtime selection changed");
418
+ });
419
+ const metadata = await fetchRuntimeUpdateMetadata(this.binding.origin);
420
+ if (this.closed || client !== this.client || !client.online() || this.authentication !== authentication)
421
+ return;
422
+ if (!runtimeUpdateNeeded(program, metadata, state.failed?.sha256)) {
423
+ if (state.failed?.sha256 === metadata.sha256) {
424
+ this.status({ state: "failed", target: { version: metadata.version, bundleSha256: metadata.sha256 } });
425
+ this.say(`Runtime update failed: ${metadata.version}/${metadata.sha256.slice(0, 8)}. Waiting for a different server build.`);
426
+ }
427
+ else
428
+ this.status({ state: "current" });
429
+ return;
430
+ }
431
+ this.status({ state: "pending", target: { version: metadata.version, bundleSha256: metadata.sha256 } });
432
+ this.say(`Runtime update pending: ${metadata.version}/${metadata.sha256.slice(0, 8)}. Waiting for idle.`);
433
+ let bytes;
434
+ try {
435
+ bytes = await fetchRuntimeUpdateArchive(this.binding.origin, metadata);
436
+ }
437
+ catch (error) {
438
+ // Transport interruptions/429 are backoff, never a failed-build tombstone.
439
+ if (error instanceof RuntimeUpdateArtifactError && error.reason === "checksum") {
440
+ await this.locked(async (lock) => this.journal.rememberFailure(metadata, () => this.current(lock)));
441
+ this.status({ state: "failed", target: { version: metadata.version, bundleSha256: metadata.sha256 } });
442
+ }
443
+ throw error;
444
+ }
445
+ if (this.closed)
446
+ return;
447
+ let child;
448
+ const id = randomUUID();
449
+ let retired = false;
450
+ try {
451
+ await this.locked(async (lock) => {
452
+ await assertRuntimeInstallExclusive(this.install.root, this.owner, lock, this.observe);
453
+ if (this.closed || !same(readRuntimeSelectedProgram(this.install.root), program))
454
+ return;
455
+ let next;
456
+ try {
457
+ next = stageRuntimeUpdate(this.install.root, bytes, metadata, lock);
458
+ }
459
+ catch (error) {
460
+ if (error instanceof RuntimeUpdateArtifactError && ["archive", "checksum"].includes(error.reason)) {
461
+ this.journal.rememberFailure(metadata, () => this.current(lock));
462
+ this.status({ state: "failed", target: { version: metadata.version, bundleSha256: metadata.sha256 } });
463
+ }
464
+ throw error;
465
+ }
466
+ if (!await client.prepareUpdate(id))
467
+ return;
468
+ retired = true;
469
+ if (this.closed)
470
+ return;
471
+ this.current(lock);
472
+ await assertRuntimeInstallExclusive(this.install.root, this.owner, lock, this.observe);
473
+ this.running(lock);
474
+ this.journal.begin(id, this.owner.id, program, next, () => this.running(lock));
475
+ child = await this.spawnChild(next, id, lock);
476
+ this.journal.advance(id, "handoff", () => this.running(lock));
477
+ });
478
+ }
479
+ catch (error) {
480
+ if (child)
481
+ await this.stopUnadmitted(child);
482
+ if (retired && !this.closed)
483
+ await this.resumeOld(id, metadata);
484
+ throw error;
485
+ }
486
+ if (!child)
487
+ return;
488
+ if (this.closed) {
489
+ await this.stopUnadmitted(child);
490
+ return;
491
+ }
492
+ try {
493
+ await this.arm(child, { v: 1, id, type: "init", role: "helper", ownerId: this.owner.id, profile: this.options.store.directory });
494
+ }
495
+ catch (error) {
496
+ // This parent is still alive: the helper cannot yet adopt or admit any task.
497
+ await this.stopUnadmitted(child);
498
+ if (this.closed)
499
+ return;
500
+ await this.resumeOld(id, metadata);
501
+ throw error;
502
+ }
503
+ if (this.closed) {
504
+ await this.stopUnadmitted(child);
505
+ return;
506
+ }
507
+ this.handedOff = true;
508
+ if (this.timer)
509
+ clearInterval(this.timer);
510
+ this.timer = undefined;
511
+ child.channel.close();
512
+ if (child.process.connected)
513
+ child.process.disconnect();
514
+ child.process.unref();
515
+ }
516
+ async resumeOld(id, target) {
517
+ await this.locked(async (lock) => {
518
+ if (this.owner.child && runtimeProcessMatches(this.owner.child, await this.observe(this.owner.child.pid)) !== false)
519
+ throw Error("Runtime helper exit is unconfirmed");
520
+ const attempt = this.journal.read().attempt;
521
+ if (attempt)
522
+ this.journal.fail(id, () => this.current(lock));
523
+ else
524
+ this.journal.rememberFailure(target, () => this.current(lock));
525
+ });
526
+ if (!this.closed) {
527
+ this.client = this.newClient();
528
+ if (!this.closed)
529
+ await this.client.start();
530
+ }
531
+ }
532
+ async spawnChild(program, id, lock) {
533
+ this.running(lock);
534
+ if (!same(readRuntimeInstalledProgram(this.install.root, program.folder), program))
535
+ throw Error("Runtime target changed");
536
+ const child = (this.options.spawn ?? spawn)(process.execPath, [join(this.install.root, program.folder, "dist", "runtime-cli.js"), "--internal-update"],
537
+ // On Windows an attached console child can die with the outgoing cmd/console owner.
538
+ // This is still an exact registered, unadmitted child; unref follows only its positive arm.
539
+ { shell: false, detached: true, stdio: ["inherit", "inherit", "inherit", "ipc"], env: nativeCliEnvironment(), windowsHide: true });
540
+ const exited = new Promise(done => {
541
+ child.once("close", () => done());
542
+ child.once("error", () => { });
543
+ if (child.exitCode !== null || child.signalCode !== null)
544
+ done();
545
+ });
546
+ let result;
547
+ try {
548
+ result = { process: child, exited, channel: new RuntimeUpdateIpc(processPeer(child), id) };
549
+ if (!child.pid)
550
+ throw Error("Runtime update child did not start");
551
+ const observed = await this.observe(child.pid);
552
+ if (observed.state !== "alive" || observed.proof.pid !== child.pid || runtimeProcessMatches(observed.proof, observed) !== true)
553
+ throw Error("Runtime update child ownership is unconfirmed");
554
+ result.proof = { ...observed.proof };
555
+ this.running(lock);
556
+ this.owner = await attachRuntimeInstallChild(this.install.root, this.owner, result.proof, lock, this.observe);
557
+ this.running(lock);
558
+ return result;
559
+ }
560
+ catch (error) {
561
+ if (result)
562
+ await this.stopUnadmitted(result);
563
+ else {
564
+ if (child.exitCode === null && child.signalCode === null)
565
+ child.kill();
566
+ await exited;
567
+ }
568
+ throw error;
569
+ }
570
+ }
571
+ async arm(child, init) {
572
+ const reply = await child.channel.exchange(init, ["armed", "failure"]);
573
+ if (reply.type !== "armed" || !child.proof || runtimeProcessMatches(child.proof, await this.observe(child.proof.pid)) !== true)
574
+ throw Error("Runtime helper readiness is unconfirmed");
575
+ }
576
+ async stopUnadmitted(child) {
577
+ child.channel.close();
578
+ if (child.process.exitCode === null && child.process.signalCode === null)
579
+ child.process.kill();
580
+ await child.exited;
581
+ if (child.proof && runtimeProcessMatches(child.proof, await this.observe(child.proof.pid)) !== false)
582
+ throw Error("Runtime child exit is unconfirmed");
583
+ }
584
+ async wait() {
585
+ if (!this.client)
586
+ throw Error("Runtime controller was not started");
587
+ for (;;) {
588
+ const client = this.client;
589
+ await client.waitUntilStopped();
590
+ if (this.work)
591
+ await this.work.catch(() => { });
592
+ if (this.client !== client)
593
+ continue;
594
+ if (this.handedOff)
595
+ return;
596
+ if (client.stopReason)
597
+ throw Error(describeRuntimeStop(client.stopReason));
598
+ return;
599
+ }
600
+ }
601
+ close() {
602
+ if (this.closePromise)
603
+ return this.closePromise;
604
+ let accept, reject;
605
+ const owned = new Promise((done, fail) => { accept = done; reject = fail; });
606
+ this.closePromise = owned;
607
+ this.closed = true;
608
+ if (this.timer)
609
+ clearInterval(this.timer);
610
+ this.timer = undefined;
611
+ void (async () => {
612
+ await this.client?.stop();
613
+ if (this.lifecycle)
614
+ await this.lifecycle.catch(() => { });
615
+ if (this.work)
616
+ await this.work.catch(() => { });
617
+ if (!this.handedOff) {
618
+ if (this.ownerReleased) {
619
+ this.retainedLease?.release();
620
+ this.retainedLease = undefined;
621
+ }
622
+ else
623
+ await this.locked(async (lock) => { await releaseRuntimeInstallOwner(this.install.root, this.owner, lock, this.observe); this.ownerReleased = true; });
624
+ }
625
+ })().then(accept, error => { if (this.closePromise === owned)
626
+ this.closePromise = undefined; reject(error); });
627
+ return owned;
628
+ }
629
+ }
630
+ /** Public run/connect use this only after resolving the existing local pairing/profile normally. */
631
+ export async function runRuntimeWithUpdates(options) {
632
+ let controller, cancelled = false;
633
+ const stop = () => { cancelled = true; void controller?.close().catch(() => { process.exitCode = 1; }); };
634
+ process.once("SIGINT", stop);
635
+ process.once("SIGTERM", stop);
636
+ try {
637
+ controller = await RuntimeUpdateController.open(options);
638
+ if (!controller)
639
+ return false;
640
+ if (!cancelled) {
641
+ await controller.start();
642
+ await controller.wait();
643
+ }
644
+ return true;
645
+ }
646
+ finally {
647
+ try {
648
+ await controller?.close();
649
+ }
650
+ finally {
651
+ process.off("SIGINT", stop);
652
+ process.off("SIGTERM", stop);
653
+ }
654
+ }
655
+ }
656
+ /** No public command can supply an init payload. It must arrive over the inherited Node IPC channel. */
657
+ export async function runtimeUpdateInternalMain() {
658
+ if (process.platform !== "win32" || !process.send || !process.connected)
659
+ throw Error("Inherited native Runtime update channel required");
660
+ const channel = new RuntimeUpdateIpc({ get connected() { return process.connected === true; },
661
+ on: (event, listener) => process.on(event, listener), off: (event, listener) => process.off(event, listener),
662
+ send: (message, callback) => process.send(message, callback) });
663
+ let controller, cancelled = false;
664
+ const stop = () => { cancelled = true; void controller?.close().catch(() => { process.exitCode = 1; }); };
665
+ process.once("SIGINT", stop);
666
+ process.once("SIGTERM", stop);
667
+ try {
668
+ const init = await channel.next(["init"]);
669
+ if (init.type !== "init")
670
+ throw Error("Runtime update initialization required");
671
+ controller = await RuntimeUpdateController.resume({ store: new RuntimeClientStore(resolve(init.profile)), report: message => process.stdout.write(`${message}\n`) }, init, channel, () => cancelled);
672
+ if (cancelled)
673
+ return;
674
+ if (init.role === "helper")
675
+ await controller.startTrial(init.id);
676
+ else
677
+ await controller.start();
678
+ await controller.wait();
679
+ }
680
+ finally {
681
+ channel.close();
682
+ try {
683
+ await controller?.close();
684
+ }
685
+ finally {
686
+ process.off("SIGINT", stop);
687
+ process.off("SIGTERM", stop);
688
+ }
689
+ }
690
+ }
691
+ //# sourceMappingURL=runtime-update-controller.js.map