@cotal-ai/workspace 0.100.1 → 0.101.0

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 (171) hide show
  1. package/build/Release/darwin-arm64/build.json +20 -0
  2. package/build/Release/darwin-arm64/process.node +0 -0
  3. package/build/Release/darwin-x64/build.json +20 -0
  4. package/build/Release/darwin-x64/process.node +0 -0
  5. package/build/Release/win32-arm64/build.json +4 -3
  6. package/build/Release/win32-x64/build.json +4 -3
  7. package/dist/agent-grants.d.ts +21 -0
  8. package/dist/agent-grants.d.ts.map +1 -0
  9. package/dist/agent-grants.js +21 -0
  10. package/dist/agent-grants.js.map +1 -0
  11. package/dist/auth-paths.d.ts +34 -0
  12. package/dist/auth-paths.d.ts.map +1 -1
  13. package/dist/auth-paths.js +99 -3
  14. package/dist/auth-paths.js.map +1 -1
  15. package/dist/broker-display.d.ts +12 -0
  16. package/dist/broker-display.d.ts.map +1 -1
  17. package/dist/broker-display.js +14 -0
  18. package/dist/broker-display.js.map +1 -1
  19. package/dist/connect.d.ts +21 -7
  20. package/dist/connect.d.ts.map +1 -1
  21. package/dist/connect.js +152 -22
  22. package/dist/connect.js.map +1 -1
  23. package/dist/control-target.d.ts.map +1 -1
  24. package/dist/control-target.js +12 -10
  25. package/dist/control-target.js.map +1 -1
  26. package/dist/cotal-config.d.ts +7 -0
  27. package/dist/cotal-config.d.ts.map +1 -1
  28. package/dist/cotal-config.js +29 -2
  29. package/dist/cotal-config.js.map +1 -1
  30. package/dist/cotal-home.d.ts +23 -0
  31. package/dist/cotal-home.d.ts.map +1 -0
  32. package/dist/cotal-home.js +36 -0
  33. package/dist/cotal-home.js.map +1 -0
  34. package/dist/credential-actions.d.ts +17 -3
  35. package/dist/credential-actions.d.ts.map +1 -1
  36. package/dist/credential-actions.js +62 -10
  37. package/dist/credential-actions.js.map +1 -1
  38. package/dist/darwin-process.d.ts +20 -0
  39. package/dist/darwin-process.d.ts.map +1 -0
  40. package/dist/darwin-process.js +24 -0
  41. package/dist/darwin-process.js.map +1 -0
  42. package/dist/deprecated-daemon-entries.d.ts +3 -0
  43. package/dist/deprecated-daemon-entries.d.ts.map +1 -1
  44. package/dist/deprecated-daemon-entries.js +6 -1
  45. package/dist/deprecated-daemon-entries.js.map +1 -1
  46. package/dist/extensions.d.ts +4 -2
  47. package/dist/extensions.d.ts.map +1 -1
  48. package/dist/extensions.js +7 -2
  49. package/dist/extensions.js.map +1 -1
  50. package/dist/flags.d.ts +2 -0
  51. package/dist/flags.d.ts.map +1 -1
  52. package/dist/flags.js +1 -1
  53. package/dist/flags.js.map +1 -1
  54. package/dist/index.d.ts +7 -1
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +7 -1
  57. package/dist/index.js.map +1 -1
  58. package/dist/join-target.d.ts +3 -1
  59. package/dist/join-target.d.ts.map +1 -1
  60. package/dist/join-target.js +15 -3
  61. package/dist/join-target.js.map +1 -1
  62. package/dist/maintenance.d.ts +61 -2
  63. package/dist/maintenance.d.ts.map +1 -1
  64. package/dist/maintenance.js +153 -14
  65. package/dist/maintenance.js.map +1 -1
  66. package/dist/manager-name.d.ts +15 -0
  67. package/dist/manager-name.d.ts.map +1 -0
  68. package/dist/manager-name.js +54 -0
  69. package/dist/manager-name.js.map +1 -0
  70. package/dist/mesh-registry.d.ts +39 -35
  71. package/dist/mesh-registry.d.ts.map +1 -1
  72. package/dist/mesh-registry.js +57 -36
  73. package/dist/mesh-registry.js.map +1 -1
  74. package/dist/mesh-target.d.ts +120 -3
  75. package/dist/mesh-target.d.ts.map +1 -1
  76. package/dist/mesh-target.js +319 -48
  77. package/dist/mesh-target.js.map +1 -1
  78. package/dist/official-connectors.d.ts +3 -0
  79. package/dist/official-connectors.d.ts.map +1 -1
  80. package/dist/official-connectors.js +5 -0
  81. package/dist/official-connectors.js.map +1 -1
  82. package/dist/output.d.ts +34 -6
  83. package/dist/output.d.ts.map +1 -1
  84. package/dist/output.js +38 -5
  85. package/dist/output.js.map +1 -1
  86. package/dist/pid.d.ts +42 -7
  87. package/dist/pid.d.ts.map +1 -1
  88. package/dist/pid.js +189 -26
  89. package/dist/pid.js.map +1 -1
  90. package/dist/preferences.d.ts +25 -3
  91. package/dist/preferences.d.ts.map +1 -1
  92. package/dist/preferences.js +77 -14
  93. package/dist/preferences.js.map +1 -1
  94. package/dist/preflight.d.ts +17 -5
  95. package/dist/preflight.d.ts.map +1 -1
  96. package/dist/preflight.js +22 -32
  97. package/dist/preflight.js.map +1 -1
  98. package/dist/prepared-spaces.d.ts +20 -2
  99. package/dist/prepared-spaces.d.ts.map +1 -1
  100. package/dist/prepared-spaces.js +90 -15
  101. package/dist/prepared-spaces.js.map +1 -1
  102. package/dist/present.d.ts +2 -12
  103. package/dist/present.d.ts.map +1 -1
  104. package/dist/present.js +24 -40
  105. package/dist/present.js.map +1 -1
  106. package/dist/protected-records.d.ts +22 -3
  107. package/dist/protected-records.d.ts.map +1 -1
  108. package/dist/protected-records.js +198 -27
  109. package/dist/protected-records.js.map +1 -1
  110. package/dist/registration-policy.d.ts +2 -1
  111. package/dist/registration-policy.d.ts.map +1 -1
  112. package/dist/render.d.ts +2 -1
  113. package/dist/render.d.ts.map +1 -1
  114. package/dist/render.js +42 -11
  115. package/dist/render.js.map +1 -1
  116. package/dist/renewal.d.ts +4 -0
  117. package/dist/renewal.d.ts.map +1 -1
  118. package/dist/renewal.js +20 -1
  119. package/dist/renewal.js.map +1 -1
  120. package/dist/service-adapter.d.ts +1 -2
  121. package/dist/service-adapter.d.ts.map +1 -1
  122. package/dist/service-adapter.js +9 -4
  123. package/dist/service-adapter.js.map +1 -1
  124. package/dist/service-custody.js +2 -2
  125. package/dist/service-custody.js.map +1 -1
  126. package/dist/service-definition.d.ts +3 -0
  127. package/dist/service-definition.d.ts.map +1 -1
  128. package/dist/service-definition.js +56 -2
  129. package/dist/service-definition.js.map +1 -1
  130. package/dist/service-definition.schema.json +2 -1
  131. package/dist/service-identity.js +4 -4
  132. package/dist/service-identity.js.map +1 -1
  133. package/dist/service-stop.js +3 -3
  134. package/dist/service-stop.js.map +1 -1
  135. package/dist/service-target.d.ts.map +1 -1
  136. package/dist/service-target.js +9 -2
  137. package/dist/service-target.js.map +1 -1
  138. package/dist/signer-export.d.ts +1 -0
  139. package/dist/signer-export.d.ts.map +1 -1
  140. package/dist/signer-export.js +20 -4
  141. package/dist/signer-export.js.map +1 -1
  142. package/dist/space-streams.d.ts +18 -0
  143. package/dist/space-streams.d.ts.map +1 -0
  144. package/dist/space-streams.js +41 -0
  145. package/dist/space-streams.js.map +1 -0
  146. package/dist/space.d.ts +5 -0
  147. package/dist/space.d.ts.map +1 -1
  148. package/dist/space.js +5 -0
  149. package/dist/space.js.map +1 -1
  150. package/dist/store-entries.d.ts +6 -0
  151. package/dist/store-entries.d.ts.map +1 -0
  152. package/dist/store-entries.js +17 -0
  153. package/dist/store-entries.js.map +1 -0
  154. package/dist/system-rotation.d.ts +3 -1
  155. package/dist/system-rotation.d.ts.map +1 -1
  156. package/dist/system-rotation.js +19 -4
  157. package/dist/system-rotation.js.map +1 -1
  158. package/dist/telemetry.d.ts +4 -3
  159. package/dist/telemetry.d.ts.map +1 -1
  160. package/dist/telemetry.js +13 -6
  161. package/dist/telemetry.js.map +1 -1
  162. package/dist/text-wrap.d.ts +18 -0
  163. package/dist/text-wrap.d.ts.map +1 -0
  164. package/dist/text-wrap.js +60 -0
  165. package/dist/text-wrap.js.map +1 -0
  166. package/native/README.md +5 -3
  167. package/native/darwin-process.c +173 -0
  168. package/package.json +2 -2
  169. package/scripts/assert-shipped-natives.mjs +3 -3
  170. package/scripts/build-native.mjs +39 -25
  171. package/scripts/native-contract.mjs +36 -16
@@ -1,8 +1,10 @@
1
- import { closeSync, chmodSync, constants, existsSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, realpathSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
1
+ import { closeSync, chmodSync, constants, existsSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, realpathSync, renameSync, rmdirSync, rmSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
2
2
  import { hostname } from "node:os";
3
3
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
4
4
  import { createHash, randomUUID } from "node:crypto";
5
- import { hardenPrivate } from "@cotal-ai/core";
5
+ import { hardenPrivate, wfjStreamName } from "@cotal-ai/core";
6
+ import { recordOperationWarning } from "./output.js";
7
+ import { foreignStoreEntries, STORE_ENTRY_NAMES } from "./store-entries.js";
6
8
  /** Version 1 lives in `<project>/.cotal/maintenance/v1`; the lock is shared across versions. */
7
9
  export const MAINTENANCE_JOURNAL_VERSION = 1;
8
10
  export const MAINTENANCE_RESUME_DOCUMENT_VERSION = 1;
@@ -88,8 +90,16 @@ function fsyncDirectory(path) {
88
90
  }
89
91
  function ensureDirectory(path, harden) {
90
92
  if (!existsSync(path)) {
91
- mkdirSync(path, { mode: 0o700 });
92
- fsyncDirectory(dirname(path));
93
+ // A concurrent first operation on this root can create it between the check and the mkdir; the
94
+ // checks below verify what is there, and the lock then names that operation.
95
+ try {
96
+ mkdirSync(path, { mode: 0o700 });
97
+ fsyncDirectory(dirname(path));
98
+ }
99
+ catch (error) {
100
+ if (error.code !== "EEXIST")
101
+ throw error;
102
+ }
93
103
  }
94
104
  const stat = lstatSync(path);
95
105
  if (!stat.isDirectory() || stat.isSymbolicLink())
@@ -323,7 +333,19 @@ function readLockFile(path) {
323
333
  const lock = parsed;
324
334
  if (!lock || typeof lock !== "object" || typeof lock.token !== "string" || !lock.token)
325
335
  maintenanceError("journal-corrupt", "maintenance lock has no token", { paths: [path] });
326
- return { token: lock.token, owner: parseOwner(lock.owner, "lock") };
336
+ const operation = typeof lock.operation === "string" && /^[a-z][a-z -]{0,63}$/.test(lock.operation) ? lock.operation : undefined;
337
+ return { token: lock.token, owner: parseOwner(lock.owner, "lock"), ...(operation ? { operation } : {}) };
338
+ }
339
+ /** The process that holds this root's maintenance lock, and the operation it recorded, for a refused
340
+ * contender to name. Undefined when no lock is held or it cannot be read. */
341
+ export function readMaintenanceLockHolder(root) {
342
+ try {
343
+ const { owner, operation } = readLockFile(maintenancePaths(root).lock);
344
+ return { owner, ...(operation ? { operation } : {}) };
345
+ }
346
+ catch {
347
+ return undefined;
348
+ }
327
349
  }
328
350
  function removeReaper(path, token) {
329
351
  try {
@@ -338,13 +360,25 @@ function removeReaper(path, token) {
338
360
  throw error;
339
361
  }
340
362
  }
363
+ /** Whether a live process holds `root`'s maintenance lock, read without taking it: the holder's
364
+ * status as {@link acquireMaintenanceLock} would judge it, or `undefined` when no lock is held. */
365
+ export function maintenanceLockHolder(root) {
366
+ try {
367
+ return localProcessOwnerStatus(readLockFile(maintenancePaths(root).lock).owner);
368
+ }
369
+ catch (error) {
370
+ if (errno(error) === "ENOENT")
371
+ return undefined;
372
+ throw error;
373
+ }
374
+ }
341
375
  export function acquireMaintenanceLock(root, options = {}) {
342
376
  const paths = maintenancePaths(root);
343
377
  ensureLayout(paths);
344
378
  const owner = options.owner ?? defaultOwner();
345
379
  parseOwner(owner, "requested lock");
346
380
  const token = randomUUID();
347
- const body = `${JSON.stringify({ token, owner }, null, 2)}\n`;
381
+ const body = `${JSON.stringify({ token, owner, ...(options.operation ? { operation: options.operation } : {}) }, null, 2)}\n`;
348
382
  try {
349
383
  writePrivateExclusive(paths.lock, body);
350
384
  return { root: paths.root, path: paths.lock, token, owner };
@@ -389,6 +423,7 @@ export function acquireMaintenanceLock(root, options = {}) {
389
423
  }
390
424
  throw error;
391
425
  }
426
+ let takenOver;
392
427
  try {
393
428
  let existing;
394
429
  try {
@@ -418,6 +453,7 @@ export function acquireMaintenanceLock(root, options = {}) {
418
453
  });
419
454
  unlinkSync(paths.lock);
420
455
  fsyncDirectory(paths.maintenanceDir);
456
+ takenOver = existing.owner;
421
457
  }
422
458
  }
423
459
  finally {
@@ -435,6 +471,13 @@ export function acquireMaintenanceLock(root, options = {}) {
435
471
  });
436
472
  throw error;
437
473
  }
474
+ // A lock is taken over only from an owner proven dead, and the run that takes it says so.
475
+ if (takenOver)
476
+ recordOperationWarning({
477
+ code: "MAINTENANCE_LOCK_TAKEN_OVER",
478
+ target: { root: paths.root, path: paths.lock, owner: { pid: takenOver.pid, host: takenOver.host, startedAt: takenOver.startedAt } },
479
+ message: `took over the maintenance lock in ${paths.root} that process ${takenOver.pid} (started ${takenOver.startedAt}) left; that process is no longer running, so the command it ran there ended without releasing it`,
480
+ });
438
481
  return { root: paths.root, path: paths.lock, token, owner };
439
482
  }
440
483
  function assertLock(lock) {
@@ -1190,7 +1233,8 @@ function validateRestoreContext(record, path) {
1190
1233
  const restore = record.restore;
1191
1234
  if (!restore || typeof restore.attemptId !== "string" || !ATTEMPT_ID.test(restore.attemptId) ||
1192
1235
  !["same-path", "alternate", "disaster"].includes(restore.method) ||
1193
- typeof restore.targetPath !== "string" || !isAbsolute(restore.targetPath))
1236
+ typeof restore.targetPath !== "string" || !isAbsolute(restore.targetPath) ||
1237
+ (restore.selection !== undefined && !["full", "registry"].includes(restore.selection)))
1194
1238
  maintenanceError("journal-corrupt", "restore context is invalid", { paths: [path] });
1195
1239
  if (restore.target && (!validIdentity(restore.target) || restore.target.path !== restore.targetPath))
1196
1240
  maintenanceError("journal-corrupt", "restore target identity is invalid", { paths: [path] });
@@ -2463,10 +2507,10 @@ export function consumeRetiredMaintenance(lock) {
2463
2507
  fsyncDirectory(paths.versionDir);
2464
2508
  return record;
2465
2509
  }
2466
- function restoreBase(ready, attemptId, method, targetPath) {
2510
+ function restoreBase(ready, attemptId, method, targetPath, selection) {
2467
2511
  if (!ATTEMPT_ID.test(attemptId))
2468
2512
  maintenanceError("invalid-transition", "restore attempt id must be a safe token", { root: ready.source.path, attemptId });
2469
- return { attemptId, method, targetPath };
2513
+ return { attemptId, method, targetPath, ...(selection ? { selection } : {}) };
2470
2514
  }
2471
2515
  function newRestoreClaim(input) {
2472
2516
  if (!Number.isFinite(Date.parse(input.deadline)))
@@ -2547,7 +2591,7 @@ export function prepareSamePathRestore(lock, input) {
2547
2591
  });
2548
2592
  const { claim, ownedPaths } = newRestoreClaim(input.claim);
2549
2593
  const restore = {
2550
- ...restoreBase(ready, input.attemptId, "same-path", targetPath), fallbackPath,
2594
+ ...restoreBase(ready, input.attemptId, "same-path", targetPath, input.selection), fallbackPath,
2551
2595
  ...(ownedPaths ? { ownedPaths } : {}),
2552
2596
  };
2553
2597
  return writeJournal(lock, ready, { ...readyFrom(ready), state: "restore-ready", phase: "move-pending", restore, claim });
@@ -2607,7 +2651,7 @@ export function prepareAlternateRestore(lock, input) {
2607
2651
  });
2608
2652
  const { claim, ownedPaths } = newRestoreClaim(input.claim);
2609
2653
  const restore = {
2610
- ...restoreBase(ready, input.attemptId, "alternate", targetPath),
2654
+ ...restoreBase(ready, input.attemptId, "alternate", targetPath, input.selection),
2611
2655
  previousSource: { kind: "retained", identity: ready.source },
2612
2656
  ...(ownedPaths ? { ownedPaths } : {}),
2613
2657
  };
@@ -2630,7 +2674,7 @@ export function prepareMissingSourceRestore(lock, input) {
2630
2674
  });
2631
2675
  const { claim, ownedPaths } = newRestoreClaim(input.claim);
2632
2676
  const restore = {
2633
- ...restoreBase(record, input.attemptId, "disaster", targetPath),
2677
+ ...restoreBase(record, input.attemptId, "disaster", targetPath, input.selection),
2634
2678
  ...(ownedPaths ? { ownedPaths } : {}),
2635
2679
  };
2636
2680
  return writeJournal(lock, record, { ...readyFrom(record), state: "restore-ready", phase: "disaster-source-missing", restore, claim });
@@ -3021,7 +3065,10 @@ function continueCleanup(lock, record) {
3021
3065
  });
3022
3066
  if (record.state === "active")
3023
3067
  assertStoreIdentity(record.restore.target);
3024
- rmSync(cleanup.tombPath, { recursive: true });
3068
+ if (cleanup.kind === "previous-source")
3069
+ removeStoreTomb(lock, record, cleanup);
3070
+ else
3071
+ rmSync(cleanup.tombPath, { recursive: true });
3025
3072
  fsyncDirectory(dirname(cleanup.tombPath));
3026
3073
  }
3027
3074
  const restore = { ...record.restore, cleanup: { ...cleanup, status: "complete" } };
@@ -3036,6 +3083,36 @@ function continueCleanup(lock, record) {
3036
3083
  activeAt: record.activeAt, details: record.details,
3037
3084
  });
3038
3085
  }
3086
+ /** Remove an old store's tomb by its store entries, then the folder once it is empty. An entry no
3087
+ * Cotal store writes, there before the removal or put there since, moves the folder back to where it
3088
+ * was and refuses, so the old source loses nothing but its store. */
3089
+ function removeStoreTomb(lock, record, cleanup) {
3090
+ const refuse = (entries) => {
3091
+ const back = !existsSync(cleanup.originalPath);
3092
+ if (back) {
3093
+ renameSync(cleanup.tombPath, cleanup.originalPath);
3094
+ fsyncDirectory(dirname(cleanup.originalPath));
3095
+ }
3096
+ const path = back ? cleanup.originalPath : cleanup.tombPath;
3097
+ return maintenanceError("store-holds-foreign-entries", `the restore's old source ${path} also holds ${entries.join(", ")}, which no Cotal store writes`, {
3098
+ root: lock.root, attemptId: record.restore.attemptId, paths: [path],
3099
+ recourse: [{ action: "inspect", description: "Move those entries out of the old source, then run the cleanup again.", paths: [path] }],
3100
+ });
3101
+ };
3102
+ const foreign = foreignStoreEntries(cleanup.tombPath);
3103
+ if (foreign.length)
3104
+ refuse(foreign);
3105
+ for (const entry of STORE_ENTRY_NAMES)
3106
+ rmSync(join(cleanup.tombPath, entry), { recursive: true, force: true });
3107
+ try {
3108
+ rmdirSync(cleanup.tombPath);
3109
+ }
3110
+ catch (error) {
3111
+ if (errno(error) !== "ENOTEMPTY" && errno(error) !== "EEXIST")
3112
+ throw error;
3113
+ refuse(foreignStoreEntries(cleanup.tombPath));
3114
+ }
3115
+ }
3039
3116
  function cleanupAttemptTarget(lock, record) {
3040
3117
  const target = record.restore.target;
3041
3118
  if (!target)
@@ -3167,7 +3244,53 @@ export function rollbackRestore(lock, options = {}) {
3167
3244
  removeOwnedPathsOrFail(lock, record.restore.attemptId, record.restore.ownedPaths);
3168
3245
  return writeJournal(lock, record, readyFrom(record));
3169
3246
  }
3170
- export function cleanupRestoreFallback(lock, attemptId) {
3247
+ /** Whether a restore's launch records that the cut it replaced was preserved after its artifact was
3248
+ * made, so the store it set aside holds history newer than the artifact. */
3249
+ export function restoreReplacedNewerCut(record) {
3250
+ return record.launch?.previousCutNewer === true;
3251
+ }
3252
+ /** What a restore left only in the store it set aside. A registry restore left out every other
3253
+ * stream, and a full restore whose launch names no workflow journal, of a user-mode artifact or one
3254
+ * made before full backups held runs, left out the workflow runs. A full restore that replaced a
3255
+ * newer cut ({@link restoreReplacedNewerCut}) also left out what was written after its artifact.
3256
+ * Undefined when it left out nothing. */
3257
+ export function restoreLeftOut(record) {
3258
+ if (record.restore.selection === "registry")
3259
+ return "chat and DM history and workflow runs";
3260
+ const restored = record.launch?.restoredStreams;
3261
+ const runs = Array.isArray(restored) && restored.includes(wfjStreamName(record.space));
3262
+ if (restoreReplacedNewerCut(record))
3263
+ return runs ? "newer chat and DM history and workflow runs" : "workflow runs and newer chat and DM history";
3264
+ return runs ? undefined : "workflow runs";
3265
+ }
3266
+ /** The runs a restore's launch records it rolled back, and how many more it counted than it listed.
3267
+ * Empty when it recorded none. */
3268
+ export function restoreRolledBackRuns(record) {
3269
+ const recorded = record.launch?.rolledBackRuns, more = record.launch?.rolledBackRunsMore;
3270
+ const runs = Array.isArray(recorded) ? recorded.flatMap((row) => {
3271
+ const value = row;
3272
+ if (!value || typeof value !== "object" || typeof value.run !== "string" || !Array.isArray(value.reopened) || !value.reopened.every((key) => typeof key === "string"))
3273
+ return [];
3274
+ return [{ run: value.run, reopened: value.reopened, ...(value.missing === true ? { missing: true } : {}) }];
3275
+ }) : [];
3276
+ return { runs, more: typeof more === "number" && Number.isSafeInteger(more) && more > 0 ? more : 0 };
3277
+ }
3278
+ /** Why a restore left something only in the store it set aside ({@link restoreLeftOut}), as a clause
3279
+ * about the restore. Undefined when it left out nothing. */
3280
+ export function restoreLeftOutReason(record) {
3281
+ const leftOut = restoreLeftOut(record);
3282
+ if (!leftOut)
3283
+ return undefined;
3284
+ if (record.restore.selection === "registry")
3285
+ return "this restore was registry-only";
3286
+ const why = [...(restoreReplacedNewerCut(record) ? ["was made before the state it replaced was preserved"] : []), ...(leftOut.startsWith("workflow runs") ? ["held no workflow runs"] : [])];
3287
+ return `this restore's artifact ${why.join(" and ")}`;
3288
+ }
3289
+ /** Delete the old store a healthy active restore set aside, then retire its journal. When the
3290
+ * restore left something out ({@link restoreLeftOut}), that store holds its only copy and is
3291
+ * deleted only with `discardHistory`. A store that also holds an entry no Cotal store writes
3292
+ * ({@link foreignStoreEntries}) is not deleted at all. */
3293
+ export function cleanupRestoreFallback(lock, attemptId, options = {}) {
3171
3294
  let record = currentJournal(lock);
3172
3295
  if (record.state !== "active" || record.restore.attemptId !== attemptId)
3173
3296
  maintenanceError("cleanup-forbidden", "old-source cleanup requires the matching healthy active restore", {
@@ -3193,6 +3316,22 @@ export function cleanupRestoreFallback(lock, attemptId) {
3193
3316
  });
3194
3317
  if (record.restore.cleanup?.status === "complete")
3195
3318
  return retireCleanedRestore(lock, record);
3319
+ // A cleanup already under way had this consent when it started.
3320
+ const leftOut = restoreLeftOut(record);
3321
+ if (!record.restore.cleanup && leftOut && !options.discardHistory)
3322
+ maintenanceError("cleanup-forbidden", `the restore's old source holds the only copy of the ${leftOut} it left out`, {
3323
+ root: lock.root, attemptId, paths: [previous.identity.path],
3324
+ recourse: [{ action: "cleanup", description: "Keep the old source, or repeat the cleanup with explicit consent to discard that history.", paths: [previous.identity.path] }],
3325
+ });
3326
+ // A file in the old source that no Cotal store writes is someone's, so nothing there is deleted.
3327
+ if (!record.restore.cleanup && existsSync(previous.identity.path)) {
3328
+ const foreign = foreignStoreEntries(previous.identity.path);
3329
+ if (foreign.length)
3330
+ maintenanceError("store-holds-foreign-entries", `the restore's old source ${previous.identity.path} also holds ${foreign.join(", ")}, which no Cotal store writes`, {
3331
+ root: lock.root, attemptId, paths: [previous.identity.path],
3332
+ recourse: [{ action: "inspect", description: "Move those entries out of the old source, then run the cleanup again.", paths: [previous.identity.path] }],
3333
+ });
3334
+ }
3196
3335
  if (!record.restore.cleanup)
3197
3336
  record = startCleanup(lock, record, "previous-source", previous.identity);
3198
3337
  if (record.restore.cleanup?.kind !== "previous-source")