@cotal-ai/cli 0.91.0 → 0.92.1

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 (222) hide show
  1. package/dist/chat/chat-view.d.ts.map +1 -1
  2. package/dist/chat/chat-view.js +3 -3
  3. package/dist/chat/chat-view.js.map +1 -1
  4. package/dist/chat/controller.d.ts +2 -1
  5. package/dist/chat/controller.d.ts.map +1 -1
  6. package/dist/chat/controller.js +29 -13
  7. package/dist/chat/controller.js.map +1 -1
  8. package/dist/chat/engine.d.ts +12 -10
  9. package/dist/chat/engine.d.ts.map +1 -1
  10. package/dist/chat/engine.js +17 -19
  11. package/dist/chat/engine.js.map +1 -1
  12. package/dist/chat/frontend.d.ts.map +1 -1
  13. package/dist/chat/frontend.js +17 -2
  14. package/dist/chat/frontend.js.map +1 -1
  15. package/dist/chat/native-session.d.ts +25 -3
  16. package/dist/chat/native-session.d.ts.map +1 -1
  17. package/dist/chat/native-session.js +72 -31
  18. package/dist/chat/native-session.js.map +1 -1
  19. package/dist/chat/picker.d.ts +4 -3
  20. package/dist/chat/picker.d.ts.map +1 -1
  21. package/dist/chat/picker.js +7 -5
  22. package/dist/chat/picker.js.map +1 -1
  23. package/dist/chat/plain.d.ts.map +1 -1
  24. package/dist/chat/plain.js +16 -9
  25. package/dist/chat/plain.js.map +1 -1
  26. package/dist/chat/session.d.ts +6 -1
  27. package/dist/chat/session.d.ts.map +1 -1
  28. package/dist/chat/stdio-io.d.ts +3 -2
  29. package/dist/chat/stdio-io.d.ts.map +1 -1
  30. package/dist/chat/stdio-io.js +3 -2
  31. package/dist/chat/stdio-io.js.map +1 -1
  32. package/dist/chat/stdio.d.ts.map +1 -1
  33. package/dist/chat/stdio.js +2 -1
  34. package/dist/chat/stdio.js.map +1 -1
  35. package/dist/chat/view.d.ts +37 -6
  36. package/dist/chat/view.d.ts.map +1 -1
  37. package/dist/chat/view.js +69 -22
  38. package/dist/chat/view.js.map +1 -1
  39. package/dist/command.d.ts.map +1 -1
  40. package/dist/command.js +5 -2
  41. package/dist/command.js.map +1 -1
  42. package/dist/commands/agents.d.ts.map +1 -1
  43. package/dist/commands/agents.js +5 -0
  44. package/dist/commands/agents.js.map +1 -1
  45. package/dist/commands/backup-maintenance.d.ts.map +1 -1
  46. package/dist/commands/backup-maintenance.js +4 -3
  47. package/dist/commands/backup-maintenance.js.map +1 -1
  48. package/dist/commands/backup-read.d.ts.map +1 -1
  49. package/dist/commands/backup-read.js +4 -1
  50. package/dist/commands/backup-read.js.map +1 -1
  51. package/dist/commands/backup-restore.d.ts.map +1 -1
  52. package/dist/commands/backup-restore.js +7 -10
  53. package/dist/commands/backup-restore.js.map +1 -1
  54. package/dist/commands/backup.d.ts.map +1 -1
  55. package/dist/commands/backup.js +22 -9
  56. package/dist/commands/backup.js.map +1 -1
  57. package/dist/commands/describe.d.ts.map +1 -1
  58. package/dist/commands/describe.js +10 -4
  59. package/dist/commands/describe.js.map +1 -1
  60. package/dist/commands/doctor.d.ts.map +1 -1
  61. package/dist/commands/doctor.js +1 -1
  62. package/dist/commands/doctor.js.map +1 -1
  63. package/dist/commands/down.d.ts.map +1 -1
  64. package/dist/commands/down.js +119 -13
  65. package/dist/commands/down.js.map +1 -1
  66. package/dist/commands/endpoint-authoring.d.ts +2 -0
  67. package/dist/commands/endpoint-authoring.d.ts.map +1 -1
  68. package/dist/commands/endpoint-authoring.js +6 -1
  69. package/dist/commands/endpoint-authoring.js.map +1 -1
  70. package/dist/commands/history-clear.d.ts.map +1 -1
  71. package/dist/commands/history-clear.js +3 -2
  72. package/dist/commands/history-clear.js.map +1 -1
  73. package/dist/commands/history.d.ts.map +1 -1
  74. package/dist/commands/history.js +1 -7
  75. package/dist/commands/history.js.map +1 -1
  76. package/dist/commands/meshes-add.d.ts.map +1 -1
  77. package/dist/commands/meshes-add.js +3 -1
  78. package/dist/commands/meshes-add.js.map +1 -1
  79. package/dist/commands/mint-endpoint-caller.js +2 -2
  80. package/dist/commands/mint-endpoint-caller.js.map +1 -1
  81. package/dist/commands/reset-space-apply.d.ts.map +1 -1
  82. package/dist/commands/reset-space-apply.js +43 -56
  83. package/dist/commands/reset-space-apply.js.map +1 -1
  84. package/dist/commands/reset-store-apply.d.ts +24 -0
  85. package/dist/commands/reset-store-apply.d.ts.map +1 -1
  86. package/dist/commands/reset-store-apply.js +115 -53
  87. package/dist/commands/reset-store-apply.js.map +1 -1
  88. package/dist/commands/reset.d.ts.map +1 -1
  89. package/dist/commands/reset.js +30 -18
  90. package/dist/commands/reset.js.map +1 -1
  91. package/dist/commands/service-actions.d.ts.map +1 -1
  92. package/dist/commands/service-actions.js +13 -5
  93. package/dist/commands/service-actions.js.map +1 -1
  94. package/dist/commands/service-log-follow.d.ts.map +1 -1
  95. package/dist/commands/service-log-follow.js +3 -2
  96. package/dist/commands/service-log-follow.js.map +1 -1
  97. package/dist/commands/service-registry.js +1 -1
  98. package/dist/commands/service-registry.js.map +1 -1
  99. package/dist/commands/service-set-stop.d.ts.map +1 -1
  100. package/dist/commands/service-set-stop.js +5 -2
  101. package/dist/commands/service-set-stop.js.map +1 -1
  102. package/dist/commands/space-delete.d.ts.map +1 -1
  103. package/dist/commands/space-delete.js +50 -5
  104. package/dist/commands/space-delete.js.map +1 -1
  105. package/dist/commands/space-registration.d.ts.map +1 -1
  106. package/dist/commands/space-registration.js +14 -8
  107. package/dist/commands/space-registration.js.map +1 -1
  108. package/dist/commands/spawn.d.ts +4 -4
  109. package/dist/commands/spawn.d.ts.map +1 -1
  110. package/dist/commands/spawn.js +20 -13
  111. package/dist/commands/spawn.js.map +1 -1
  112. package/dist/commands/stack-down.d.ts +15 -1
  113. package/dist/commands/stack-down.d.ts.map +1 -1
  114. package/dist/commands/stack-down.js +156 -49
  115. package/dist/commands/stack-down.js.map +1 -1
  116. package/dist/commands/stack-up.d.ts +7 -2
  117. package/dist/commands/stack-up.d.ts.map +1 -1
  118. package/dist/commands/stack-up.js +63 -24
  119. package/dist/commands/stack-up.js.map +1 -1
  120. package/dist/commands/status.d.ts +12 -0
  121. package/dist/commands/status.d.ts.map +1 -1
  122. package/dist/commands/status.js +71 -17
  123. package/dist/commands/status.js.map +1 -1
  124. package/dist/commands/team-boot-controller.js +1 -1
  125. package/dist/commands/team-boot-controller.js.map +1 -1
  126. package/dist/commands/team.js +4 -3
  127. package/dist/commands/team.js.map +1 -1
  128. package/dist/commands/up.d.ts +2 -1
  129. package/dist/commands/up.d.ts.map +1 -1
  130. package/dist/commands/up.js +117 -16
  131. package/dist/commands/up.js.map +1 -1
  132. package/dist/commands/update.d.ts +2 -0
  133. package/dist/commands/update.d.ts.map +1 -1
  134. package/dist/commands/update.js +20 -9
  135. package/dist/commands/update.js.map +1 -1
  136. package/dist/index.d.ts +1 -0
  137. package/dist/index.d.ts.map +1 -1
  138. package/dist/index.js +2 -1
  139. package/dist/index.js.map +1 -1
  140. package/dist/lib/backup-artifact.d.ts +7 -0
  141. package/dist/lib/backup-artifact.d.ts.map +1 -1
  142. package/dist/lib/backup-artifact.js +29 -5
  143. package/dist/lib/backup-artifact.js.map +1 -1
  144. package/dist/lib/broker-service.d.ts +9 -0
  145. package/dist/lib/broker-service.d.ts.map +1 -1
  146. package/dist/lib/broker-service.js +64 -24
  147. package/dist/lib/broker-service.js.map +1 -1
  148. package/dist/lib/confirmation-required.d.ts +13 -0
  149. package/dist/lib/confirmation-required.d.ts.map +1 -0
  150. package/dist/lib/confirmation-required.js +10 -0
  151. package/dist/lib/confirmation-required.js.map +1 -0
  152. package/dist/lib/credential-authority.d.ts.map +1 -1
  153. package/dist/lib/credential-authority.js +5 -4
  154. package/dist/lib/credential-authority.js.map +1 -1
  155. package/dist/lib/delivery-proc.d.ts.map +1 -1
  156. package/dist/lib/delivery-proc.js +36 -5
  157. package/dist/lib/delivery-proc.js.map +1 -1
  158. package/dist/lib/endpoint-client.d.ts +3 -0
  159. package/dist/lib/endpoint-client.d.ts.map +1 -1
  160. package/dist/lib/endpoint-client.js +4 -0
  161. package/dist/lib/endpoint-client.js.map +1 -1
  162. package/dist/lib/endpoint-errors.d.ts +9 -0
  163. package/dist/lib/endpoint-errors.d.ts.map +1 -1
  164. package/dist/lib/endpoint-errors.js +17 -1
  165. package/dist/lib/endpoint-errors.js.map +1 -1
  166. package/dist/lib/reset-space-plan.d.ts.map +1 -1
  167. package/dist/lib/reset-space-plan.js +25 -4
  168. package/dist/lib/reset-space-plan.js.map +1 -1
  169. package/dist/lib/restore.d.ts +10 -0
  170. package/dist/lib/restore.d.ts.map +1 -1
  171. package/dist/lib/restore.js +35 -1
  172. package/dist/lib/restore.js.map +1 -1
  173. package/dist/lib/service-census.d.ts +10 -10
  174. package/dist/lib/service-install-intent.d.ts +2 -2
  175. package/dist/lib/service-instances.d.ts +1 -1
  176. package/dist/lib/service-instances.d.ts.map +1 -1
  177. package/dist/lib/service-instances.js +2 -2
  178. package/dist/lib/service-instances.js.map +1 -1
  179. package/dist/lib/service-journal.d.ts.map +1 -1
  180. package/dist/lib/service-journal.js +4 -4
  181. package/dist/lib/service-journal.js.map +1 -1
  182. package/dist/lib/service-presets.d.ts.map +1 -1
  183. package/dist/lib/service-presets.js +3 -2
  184. package/dist/lib/service-presets.js.map +1 -1
  185. package/dist/lib/service-process.d.ts +26 -1
  186. package/dist/lib/service-process.d.ts.map +1 -1
  187. package/dist/lib/service-process.js +140 -20
  188. package/dist/lib/service-process.js.map +1 -1
  189. package/dist/lib/service-supervision.d.ts +1 -1
  190. package/dist/lib/service-systemd-attempt-read.d.ts +3 -3
  191. package/dist/lib/service-uninstall-intent.d.ts +3 -3
  192. package/dist/lib/shown-time.d.ts +5 -0
  193. package/dist/lib/shown-time.d.ts.map +1 -0
  194. package/dist/lib/shown-time.js +9 -0
  195. package/dist/lib/shown-time.js.map +1 -0
  196. package/dist/lib/stack-journal.d.ts +75 -1
  197. package/dist/lib/stack-journal.d.ts.map +1 -1
  198. package/dist/lib/stack-journal.js +226 -4
  199. package/dist/lib/stack-journal.js.map +1 -1
  200. package/dist/lib/stdout-owner.d.ts +17 -0
  201. package/dist/lib/stdout-owner.d.ts.map +1 -0
  202. package/dist/lib/stdout-owner.js +44 -0
  203. package/dist/lib/stdout-owner.js.map +1 -0
  204. package/dist/lib/team-additive-attempt.d.ts +1 -1
  205. package/dist/lib/up-report.d.ts +2 -0
  206. package/dist/lib/up-report.d.ts.map +1 -1
  207. package/dist/lib/up-report.js +3 -1
  208. package/dist/lib/up-report.js.map +1 -1
  209. package/dist/managed-handoff.d.ts +3 -0
  210. package/dist/managed-handoff.d.ts.map +1 -1
  211. package/dist/managed-handoff.js +5 -0
  212. package/dist/managed-handoff.js.map +1 -1
  213. package/dist/ui.d.ts +4 -2
  214. package/dist/ui.d.ts.map +1 -1
  215. package/dist/ui.js +17 -6
  216. package/dist/ui.js.map +1 -1
  217. package/dist/watch-data.js +1 -1
  218. package/dist/watch-data.js.map +1 -1
  219. package/dist/watch-json.d.ts.map +1 -1
  220. package/dist/watch-json.js +3 -2
  221. package/dist/watch-json.js.map +1 -1
  222. package/package.json +4 -4
@@ -3,7 +3,7 @@ import { createHash, randomUUID } from "node:crypto";
3
3
  import { createConnection, createServer } from "node:net";
4
4
  import { homedir, hostname } from "node:os";
5
5
  import { mkdirSync, readFileSync, existsSync, openSync, fstatSync, statSync, readSync, closeSync, lstatSync, realpathSync, readdirSync, } from "node:fs";
6
- import { dirname, join, resolve } from "node:path";
6
+ import { dirname, isAbsolute, join, resolve } from "node:path";
7
7
  import { isReachable, DEFAULT_SERVER, createSpaceAuth, serverConfig, openServerConfig, validateTlsMaterial, mintCreds, mintLifecycleUid, DEV_OWNER, mintConnectionEvictorCreds, mintMembershipObserverCreds, newIdentity, setupSpaceStreams, reconcileSpaceTtls, ttlBuckets, standaloneConnectOpts, requireBrokerFloor, parseServerVersion, presenceBucket, seedChannelRegistry, ensureDefaultDeliveryClass, LEASE_TTL_MS, mkSecretDir, openPrivateAppend, realDirNoSymlink, writeSecretFile, } from "@cotal-ai/core";
8
8
  import { connect } from "@nats-io/transport-node";
9
9
  import { jetstreamManager, StorageType } from "@nats-io/jetstream";
@@ -36,7 +36,7 @@ import { assertUserStackTransport, preflightUserStack } from "./stack-user-auth.
36
36
  import { stackDown } from "./stack-down.js";
37
37
  import { assertControllerCoupling, assertControllerMembersLive, assertControllerResumeEligible, beginControllerResume, bindControllerBroker, controllerAttemptBrokerInstance, currentControllerAttempt, managerResumeAttempt, relaunchCommittedManager, settleControllerResume, startControllerMember, startControllerMembers, } from "../lib/controller-resume.js";
38
38
  import { removeRetiredBrokerInstance } from "../lib/controller-restore.js";
39
- import { admittedStackRestore, readStackJournal, refuseControllerCut } from "../lib/stack-journal.js";
39
+ import { acquireStackOperationLock, admittedStackRestore, readStackJournal, refuseControllerCut } from "../lib/stack-journal.js";
40
40
  import { serviceInstances } from "../lib/service-instances.js";
41
41
  import { serviceSupervisorStore } from "../lib/service-supervision.js";
42
42
  import { serviceInstallIntents } from "../lib/service-install-intent.js";
@@ -46,7 +46,7 @@ import { extensionNames, preflightRuntime } from "../ext-loader.js";
46
46
  import { completingFlagValue } from "../lib/completion.js";
47
47
  import { metadataPositionals } from "../lib/command-path.js";
48
48
  import { askManager } from "../lib/control.js";
49
- import { bindPreparedRestoreListener, isManagerCommitResult, isManagerFinalizeResult, isManagerCommittedRestore, markPreparedRestoreActive, markPreparedRestoreDegraded, prepareRestore, recordPreparedRestoreManagerCommit, rehydratePreparedRestore, replacePreparedDeadRestoreListener, } from "../lib/restore.js";
49
+ import { bindPreparedRestoreListener, isManagerCommitResult, isManagerFinalizeResult, isManagerCommittedRestore, markPreparedRestoreActive, markPreparedRestoreDegraded, heldRestoreAttempt, prepareRestore, recordPreparedRestoreManagerCommit, rehydratePreparedRestore, replacePreparedDeadRestoreListener, } from "../lib/restore.js";
50
50
  const pendingRestores = new Map();
51
51
  const restoreSinks = new Map();
52
52
  /** Run the guarded `up --restore` startup and return its result instead of printing it. The
@@ -54,8 +54,10 @@ const restoreSinks = new Map();
54
54
  export async function startRestore(values) {
55
55
  let returned;
56
56
  await up({ values: { ...values }, positionals: [], raw: [] }, undefined, false, (result) => { returned = result; });
57
- if (!returned)
58
- throw new CommandFailure("unknown", "OPERATION_UNKNOWN", "the guarded restore returned without its activation result; the journal records its state and no step was repeated", { remedy: "inspect the root's maintenance journal; a stale attempt is recovered by `cotal backup recover <attempt-id>`" });
57
+ if (!returned) {
58
+ const root = cotalRoot(), held = heldRestoreAttempt(root);
59
+ throw new CommandFailure("unknown", "OPERATION_UNKNOWN", "the guarded restore returned without its activation result; the journal records its state and no step was repeated", { target: { root, ...(held ? { attemptId: held.attemptId, journalState: held.journalState } : {}) }, remedy: held?.remedy ?? "this root's maintenance journal names no restore attempt; check it with cotal status" });
60
+ }
59
61
  return returned;
60
62
  }
61
63
  const pendingOrdinaryResumes = new Map();
@@ -125,6 +127,10 @@ export function validateUpArguments(args) {
125
127
  throw new CommandFailure("refused", "USAGE_INVALID", "--no-manager cannot be combined with --max-sessions because no manager starts", { remedy: "drop --max-sessions, or drop --no-manager to set the manager session ceiling" });
126
128
  if (args.values.foreground === true && args.values.detach === true)
127
129
  throw new CommandFailure("refused", "USAGE_INVALID", "choose --foreground or --detach, not both");
130
+ // A manifest boot checks its own `--server` override against `broker.servers`.
131
+ const serverProblem = args.values.server !== undefined && !args.values.file ? upServerProblem(String(args.values.server)) : undefined;
132
+ if (serverProblem)
133
+ throw new CommandFailure("refused", "USAGE_INVALID", `${serverProblem}; nothing was started or written`, { remedy: "pass the address the local broker listens on, for example --server nats://127.0.0.1:4222" });
128
134
  if (args.values.file !== undefined && args.values.foreground === true)
129
135
  throw new CommandFailure("refused", "UNSUPPORTED", "manifest boot has no native foreground custody path; a detached broker was not substituted for the requested terminal-bound lifetime");
130
136
  if (args.values.foreground === true && (args.values.json === true || args.values.plain === true))
@@ -152,6 +158,30 @@ export function validateUpArguments(args) {
152
158
  if (args.values.restore !== undefined)
153
159
  refuseControllerCut(args.values.root !== undefined ? preparedUpRoot(String(args.values.root)) : cotalRoot(), "up --restore");
154
160
  }
161
+ /** Why `--server` cannot be the address a local broker listens on, or undefined when it can. Both
162
+ * launch routes need one nats:// or tls:// URL with a host and a TCP port, and without this check
163
+ * a typo reached their first write. The message never repeats the value, which may carry a
164
+ * credential. */
165
+ function upServerProblem(server) {
166
+ let url;
167
+ try {
168
+ url = new URL(server);
169
+ }
170
+ catch {
171
+ return "--server is not a URL";
172
+ }
173
+ if (url.protocol !== "nats:" && url.protocol !== "tls:")
174
+ return "--server must be a nats:// or tls:// URL";
175
+ if (url.username || url.password)
176
+ return "--server must not embed credentials";
177
+ if (url.search || url.hash || (url.pathname && url.pathname !== "/") || server.includes(","))
178
+ return "--server must be one broker URL without a path, query or fragment";
179
+ if (!url.hostname)
180
+ return "--server names no host";
181
+ if (url.port === "0")
182
+ return "--server needs a TCP port from 1 to 65535";
183
+ return undefined;
184
+ }
155
185
  function inheritedMachineHomeRoot() {
156
186
  const root = cotalRoot();
157
187
  const machineHome = canonicalRoot(root) === canonicalRoot(homedir()) || canonicalRoot(join(root, ".cotal")) === canonicalRoot(homeCotalDir());
@@ -261,18 +291,39 @@ function assertFreshTeamBootOwnership(root, space, mode, own) {
261
291
  * or unknown stop is reported with its retained members; nothing is force-killed. */
262
292
  export function holdForegroundStack(root) {
263
293
  return new Promise((resolveHold) => {
264
- // A pending promise and signal listeners do not keep Node alive after its last socket closes.
265
- const lifetime = setInterval(() => { }, 60_000);
294
+ // The hold ends on its own when another command completes a whole-stack stop or preserve of this
295
+ // root: a later operation recorded as completed is that evidence. Any other later operation,
296
+ // such as a refresh, keeps the hold. The poll also keeps Node alive, which a pending promise and
297
+ // signal listeners alone do not.
298
+ const recorded = () => { try {
299
+ return readStackJournal(root)?.journal.operation;
300
+ }
301
+ catch {
302
+ return undefined;
303
+ } };
304
+ const held = recorded()?.id;
266
305
  let stopping = false;
306
+ const lifetime = setInterval(() => {
307
+ const operation = recorded();
308
+ if (stopping || !operation || operation.id === held || operation.phase !== "completed" || operation.kind !== "down" && operation.kind !== "preserve")
309
+ return;
310
+ stopping = true;
311
+ process.off("SIGINT", interrupt);
312
+ process.off("SIGTERM", terminate);
313
+ clearInterval(lifetime);
314
+ process.stderr.write(`${renderReport({ sections: [{ title: "Foreground stack released", blocks: [{ kind: "lines", lines: [`Another command completed a ${operation.kind === "down" ? "stop" : "preserve"} of this stack (operation ${operation.id}). This terminal no longer holds it.`] }] }] }, "rich", terminalWidth(process.stderr))}\n`);
315
+ resolveHold();
316
+ }, 1000);
267
317
  const stop = (signal) => {
268
318
  if (stopping)
269
319
  return;
270
320
  stopping = true;
271
321
  process.off("SIGINT", interrupt);
272
322
  process.off("SIGTERM", terminate);
323
+ clearInterval(lifetime);
273
324
  void stackDown(root, { yes: true }).catch((error) => {
274
325
  process.stderr.write(`${renderReport(commandFailureReport(isCommandFailure(error) ? error : new CommandFailure("unknown", "OPERATION_UNKNOWN", "the foreground stack stop did not confirm its outcome; its members were retained")), "rich", terminalWidth(process.stderr))}\n`);
275
- }).finally(() => { clearInterval(lifetime); process.exitCode = signal === "SIGINT" ? 130 : 143; resolveHold(); });
326
+ }).finally(() => { process.exitCode = signal === "SIGINT" ? 130 : 143; resolveHold(); });
276
327
  };
277
328
  const interrupt = () => stop("SIGINT");
278
329
  const terminate = () => stop("SIGTERM");
@@ -814,12 +865,12 @@ async function runUp(args, inheritedLock, onAdopt, freshTeamBoot = false, restor
814
865
  const stackServer = values.server ?? readStackJournal(meshRoot)?.journal.server ?? localMeshesForRoot(meshRoot).find((m) => m.space === stackSpace)?.server ?? (values.host ? reconcileHostAndServer(values.host, undefined) : DEFAULT_SERVER);
815
866
  const stackMode = requestedStackMode(values);
816
867
  const presentation = presentationOf(values);
817
- const stackLock = acquireMaintenanceLock(meshRoot);
868
+ const stackLock = acquireStackOperationLock(meshRoot);
818
869
  const native = controllerStackNative();
819
870
  try {
820
871
  if (values.foreground && presentation !== "json")
821
872
  stackUpForegroundNotice(presentation, stackSpace);
822
- await stackUp({ root: meshRoot, space: stackSpace, server: stackServer, transport, runtime: values.runtime ?? "pty", noManager, ...(stackMode ? { mode: stackMode } : {}), ...(values.idp ? { idp: values.idp } : {}), ...(values.host ? { host: values.host } : {}), ...(values["store-dir"] ? { storeDir: values["store-dir"] } : {}), ...(maxFileStore !== undefined ? { maxFileStore } : {}), ...(maxSessions !== undefined ? { maxSessions } : {}), ...(values.channels ? { channels: values.channels } : {}), foreground: values.foreground === true, presentation }, native);
873
+ await stackUp({ maintenanceLock: stackLock, root: meshRoot, space: stackSpace, server: stackServer, transport, runtime: values.runtime ?? "pty", noManager, ...(stackMode ? { mode: stackMode } : {}), ...(values.idp ? { idp: values.idp } : {}), ...(values.host ? { host: values.host } : {}), ...(values["store-dir"] ? { storeDir: values["store-dir"] } : {}), ...(maxFileStore !== undefined ? { maxFileStore } : {}), ...(maxSessions !== undefined ? { maxSessions } : {}), ...(values.channels ? { channels: values.channels } : {}), foreground: values.foreground === true, presentation }, native);
823
874
  }
824
875
  finally {
825
876
  releaseMaintenanceLock(stackLock);
@@ -1299,7 +1350,7 @@ async function runUp(args, inheritedLock, onAdopt, freshTeamBoot = false, restor
1299
1350
  // Transport policy is committed inside startMeshDetached before delivery launch (S5+S9).
1300
1351
  if (!quietRestore) {
1301
1352
  console.log(c.dim(`Started nats-server (${source}).`));
1302
- console.log(c.green(renderDetachedSummary({ pid, delivery, authService: wantUser && authService, manager, ...(values.root ? { root: cotalRoot() } : {}) })));
1353
+ console.log(c.green(renderDetachedSummary({ pid, delivery, authService: wantUser && authService, manager, machineHome: canonicalRoot(cotalRoot()) === canonicalRoot(homedir()), ...(values.root ? { root: cotalRoot() } : {}) })));
1303
1354
  }
1304
1355
  if (restored && process.env.COTAL_SMOKE_FAIL_AFTER_RESTORE_LISTENER_READY === "1")
1305
1356
  throw new Error("smoke-injected failure after restore listener readiness");
@@ -3172,27 +3223,77 @@ function manualClaimRemedy(space, server) {
3172
3223
  return `\`cotal supervise --space ${space} --server ${server}\` to run its control plane from here (and \`cotal deliver --space ${space} --server ${server}\` for the daemon)`;
3173
3224
  return `\`cotal meshes rm ${space}\` to drop that record first, or start this one under a different \`--space\``;
3174
3225
  }
3226
+ /** Where a prior holder's absolute root physically resolves, or undefined when it is proven gone: it
3227
+ * does not resolve (ENOENT, ENOTDIR) or resolves to something other than a directory. Any other
3228
+ * failure, such as EACCES, EPERM, EIO or ELOOP, proves nothing about the root or its journal, so the
3229
+ * name is kept and the refusal says why. No lexical spelling stands in for the physical path: under
3230
+ * an unreadable ancestor, `<root>/link/..` resolves lexically to `<root>` while it names another. */
3231
+ function priorRootPath(space, root) {
3232
+ let real;
3233
+ let kind;
3234
+ try {
3235
+ real = realpathSync.native(root);
3236
+ kind = statSync(real);
3237
+ }
3238
+ catch (e) {
3239
+ const code = e.code;
3240
+ if (code === "ENOENT" || code === "ENOTDIR")
3241
+ return undefined;
3242
+ throw new CommandFailure("refused", code === "EACCES" || code === "EPERM" ? "PERMISSION_DENIED" : "TARGET_UNAVAILABLE", `space "${space}" is registered to ${root}, which could not be resolved (${code ?? String(e)}) - \`cotal up\` did not take the name`, { target: { space, root }, remedy: `restore access to ${root} and run this again, or start this stack under a different \`--space\`` });
3243
+ }
3244
+ return kind.isDirectory() ? real : undefined;
3245
+ }
3175
3246
  /** A space name maps to one mesh in the registry (the key `--space`/`use`/`down` act on). Before
3176
3247
  * starting a broker, refuse to reuse a space already claimed by a DIFFERENT live mesh — a stale/dead
3177
- * holder is reclaimed. Re-`up`ping the same mesh (same server + root) is a refresh (port-reachable
3248
+ * holder is reclaimed, and said so when it was another root's, unless that root still holds a
3249
+ * maintenance journal. Re-`up`ping the same mesh (same server + root) is a refresh (port-reachable
3178
3250
  * path). NOTE: this is a best-effort sequential guard — two `cotal up --space X` racing from
3179
3251
  * different roots within the same instant can both pass the check before either records; that
3180
3252
  * concurrent case is out of scope (a single-operator CLI action), not synchronized with a lock. */
3181
3253
  export async function claimSpace(space, server, root) {
3182
3254
  const existing = findMesh(space);
3183
- if (!existing || (existing.server === server && existing.root === root))
3255
+ if (!existing)
3184
3256
  return;
3257
+ // A relative recorded root names no directory without a guess against the cwd, so it is never
3258
+ // taken as this root, compared with it or reclaimed. Its record stays until it is removed by hand.
3259
+ const relative = !isAbsolute(existing.root);
3260
+ if (!relative && existing.server === server && existing.root === root)
3261
+ return;
3262
+ // Every refusal below is made before the registry changes, so it is typed as refused with its own
3263
+ // remedy, and the controller and legacy routes both show the reason and exit 2.
3264
+ const target = { space, root: existing.root, server: existing.server };
3185
3265
  // An OPERATOR-REGISTERED holder is decided FIRST, before liveness, because liveness changes
3186
3266
  // nothing about it and the two outcomes would otherwise print the wrong remedy. It is never
3187
3267
  // reclaimed: unreachable is not proof the mesh is gone (the record describes a broker on another
3188
3268
  // machine), the reclaim runs BEFORE this launch starts anything, and `cotal down` — what the
3189
3269
  // liveness branch below would advise — cannot stop a mesh this machine does not run.
3190
3270
  if (existing.origin === "manual" || existing.origin === "catalog")
3191
- throw new Error(`space "${space}" is registered to a mesh at ${existing.server} (${existing.root}) - it is ${existing.origin === "catalog" ? "owned by a signed-in space catalog" : "registered by hand"}, so \`cotal up\` neither takes it over nor reclaims the name: ${existing.origin === "catalog" ? "use a different `--space`, or remove access at the IdP and run `cotal sync`" : manualClaimRemedy(space, existing.server)}`);
3192
- if (await isReachable(existing.server)) {
3193
- throw new Error(`space "${space}" is already in use by a mesh at ${existing.server} (${existing.root}) - pick a different \`--space\`, or \`cotal down\` it first`);
3271
+ throw new CommandFailure("refused", "CONFLICT", `space "${space}" is registered to a mesh at ${existing.server} (${existing.root}) - it is ${existing.origin === "catalog" ? "owned by a signed-in space catalog" : "registered by hand"}, so \`cotal up\` neither takes it over nor reclaims the name`, { target, remedy: existing.origin === "catalog" ? "use a different `--space`, or remove access at the IdP and run `cotal sync`" : manualClaimRemedy(space, existing.server) });
3272
+ if (relative)
3273
+ throw new CommandFailure("refused", "CONFLICT", `space "${space}" is registered to ${existing.root}, which is not an absolute path - \`cotal up\` did not take the name`, { target, remedy: `drop that record with \`cotal meshes rm ${space}\`, or start this stack under a different \`--space\`` });
3274
+ if (await isReachable(existing.server))
3275
+ throw new CommandFailure("refused", "CONFLICT", `space "${space}" is already in use by a mesh at ${existing.server} (${existing.root})`, { target, remedy: "pick a different `--space`, or `cotal down` it first" });
3276
+ // The recorded root is this root only where it physically resolves, so it is resolved before any
3277
+ // comparison, and one that cannot be resolved refuses. A dead holder at another root keeps its name
3278
+ // while that root holds a preserved cut or a restore: taking it would cut that root's recovery off
3279
+ // from its space. A journal that cannot be read keeps it too. A root proven gone or not a directory
3280
+ // has no journal to read; a readable root without one is reclaimed.
3281
+ const prior = priorRootPath(space, existing.root);
3282
+ const otherRoot = prior !== canonicalRoot(root);
3283
+ if (otherRoot && prior !== undefined) {
3284
+ let held;
3285
+ try {
3286
+ held = readMaintenanceJournal(existing.root)?.state;
3287
+ }
3288
+ catch {
3289
+ held = "unreadable";
3290
+ }
3291
+ if (held)
3292
+ throw new CommandFailure("refused", "LIFECYCLE_BLOCKED", `space "${space}" is registered to ${existing.root}, whose maintenance journal holds a preserved cut or a restore (state ${held}) - \`cotal up\` did not take the name`, { target, remedy: `finish that attempt from ${existing.root}, or start this stack under a different \`--space\`` });
3194
3293
  }
3195
3294
  removeMesh(space); // the prior holder's broker is gone — reclaim the name
3295
+ if (otherRoot)
3296
+ console.error(`• space "${space}" was registered to ${existing.root}, whose broker at ${existing.server} is not running - this up takes the name over`);
3196
3297
  }
3197
3298
  /**
3198
3299
  * The manager attach/console BIND host for a launch into `space`.