@yu_robotics/remote-cli 1.6.247 → 1.6.252

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 (110) hide show
  1. package/dist/client/MessageHandler.d.ts +6 -2
  2. package/dist/client/MessageHandler.d.ts.map +1 -1
  3. package/dist/client/MessageHandler.js +103 -32
  4. package/dist/client/MessageHandler.js.map +1 -1
  5. package/dist/client/WebSocketClient.d.ts +3 -0
  6. package/dist/client/WebSocketClient.d.ts.map +1 -1
  7. package/dist/client/WebSocketClient.js +3 -1
  8. package/dist/client/WebSocketClient.js.map +1 -1
  9. package/dist/commands/start.d.ts +5 -0
  10. package/dist/commands/start.d.ts.map +1 -1
  11. package/dist/commands/start.js +37 -0
  12. package/dist/commands/start.js.map +1 -1
  13. package/dist/executor/ClaudePersistentExecutor.d.ts.map +1 -1
  14. package/dist/executor/ClaudePersistentExecutor.js +61 -9
  15. package/dist/executor/ClaudePersistentExecutor.js.map +1 -1
  16. package/dist/files/FileInbox.d.ts +1 -0
  17. package/dist/files/FileInbox.d.ts.map +1 -1
  18. package/dist/files/FileInbox.js +49 -4
  19. package/dist/files/FileInbox.js.map +1 -1
  20. package/dist/index.js +2 -0
  21. package/dist/index.js.map +1 -1
  22. package/dist/maintenance/release-notes.json +1 -1
  23. package/dist/scheduling/ReviewedNativeCleanup.d.ts +1 -1
  24. package/dist/scheduling/ReviewedNativeCleanup.d.ts.map +1 -1
  25. package/dist/scheduling/ScheduleCleanup.d.ts +1 -0
  26. package/dist/scheduling/ScheduleCleanup.d.ts.map +1 -1
  27. package/dist/scheduling/ScheduleCleanup.js +2 -1
  28. package/dist/scheduling/ScheduleCleanup.js.map +1 -1
  29. package/dist/scheduling/SchedulePurgeVerification.js +4 -4
  30. package/dist/scheduling/SchedulePurgeVerification.js.map +1 -1
  31. package/dist/scheduling/ScheduleService.d.ts +22 -2
  32. package/dist/scheduling/ScheduleService.d.ts.map +1 -1
  33. package/dist/scheduling/ScheduleService.js +222 -62
  34. package/dist/scheduling/ScheduleService.js.map +1 -1
  35. package/dist/scheduling/ScheduleToolScope.d.ts.map +1 -1
  36. package/dist/scheduling/ScheduleToolScope.js +7 -0
  37. package/dist/scheduling/ScheduleToolScope.js.map +1 -1
  38. package/dist/scheduling/claude/ClaudeScheduleOwnership.d.ts +15 -3
  39. package/dist/scheduling/claude/ClaudeScheduleOwnership.d.ts.map +1 -1
  40. package/dist/scheduling/claude/ClaudeScheduleOwnership.js +111 -7
  41. package/dist/scheduling/claude/ClaudeScheduleOwnership.js.map +1 -1
  42. package/dist/scheduling/claude/ClaudeScheduleProcesses.d.ts +14 -0
  43. package/dist/scheduling/claude/ClaudeScheduleProcesses.d.ts.map +1 -0
  44. package/dist/scheduling/claude/ClaudeScheduleProcesses.js +86 -0
  45. package/dist/scheduling/claude/ClaudeScheduleProcesses.js.map +1 -0
  46. package/dist/scheduling/claude/ClaudeSessionSchedules.d.ts +13 -0
  47. package/dist/scheduling/claude/ClaudeSessionSchedules.d.ts.map +1 -1
  48. package/dist/scheduling/claude/ClaudeSessionSchedules.js +156 -40
  49. package/dist/scheduling/claude/ClaudeSessionSchedules.js.map +1 -1
  50. package/dist/scheduling/types.d.ts +4 -0
  51. package/dist/scheduling/types.d.ts.map +1 -1
  52. package/dist/scheduling/types.js +6 -1
  53. package/dist/scheduling/types.js.map +1 -1
  54. package/dist/service/SessionHostSupervisor.d.ts +63 -0
  55. package/dist/service/SessionHostSupervisor.d.ts.map +1 -0
  56. package/dist/service/SessionHostSupervisor.js +323 -0
  57. package/dist/service/SessionHostSupervisor.js.map +1 -0
  58. package/dist/sessionHost/HostClient.d.ts +49 -0
  59. package/dist/sessionHost/HostClient.d.ts.map +1 -0
  60. package/dist/sessionHost/HostClient.js +101 -0
  61. package/dist/sessionHost/HostClient.js.map +1 -0
  62. package/dist/sessionHost/HostProtocol.d.ts +65 -0
  63. package/dist/sessionHost/HostProtocol.d.ts.map +1 -0
  64. package/dist/sessionHost/HostProtocol.js +78 -0
  65. package/dist/sessionHost/HostProtocol.js.map +1 -0
  66. package/dist/sessionHost/HostServer.d.ts +52 -0
  67. package/dist/sessionHost/HostServer.d.ts.map +1 -0
  68. package/dist/sessionHost/HostServer.js +413 -0
  69. package/dist/sessionHost/HostServer.js.map +1 -0
  70. package/dist/sessionHost/HostStore.d.ts +43 -0
  71. package/dist/sessionHost/HostStore.d.ts.map +1 -0
  72. package/dist/sessionHost/HostStore.js +206 -0
  73. package/dist/sessionHost/HostStore.js.map +1 -0
  74. package/dist/sessionHost/RetainedFrontend.d.ts +63 -0
  75. package/dist/sessionHost/RetainedFrontend.d.ts.map +1 -0
  76. package/dist/sessionHost/RetainedFrontend.js +244 -0
  77. package/dist/sessionHost/RetainedFrontend.js.map +1 -0
  78. package/dist/sessionHost/RetainedHostLauncher.d.ts +25 -0
  79. package/dist/sessionHost/RetainedHostLauncher.d.ts.map +1 -0
  80. package/dist/sessionHost/RetainedHostLauncher.js +132 -0
  81. package/dist/sessionHost/RetainedHostLauncher.js.map +1 -0
  82. package/dist/sessionHost/RetainedRuntime.d.ts +62 -0
  83. package/dist/sessionHost/RetainedRuntime.d.ts.map +1 -0
  84. package/dist/sessionHost/RetainedRuntime.js +478 -0
  85. package/dist/sessionHost/RetainedRuntime.js.map +1 -0
  86. package/dist/sessionHost/RuntimeJournal.d.ts +47 -0
  87. package/dist/sessionHost/RuntimeJournal.d.ts.map +1 -0
  88. package/dist/sessionHost/RuntimeJournal.js +103 -0
  89. package/dist/sessionHost/RuntimeJournal.js.map +1 -0
  90. package/dist/sessionHost/entry.d.ts +13 -0
  91. package/dist/sessionHost/entry.d.ts.map +1 -0
  92. package/dist/sessionHost/entry.js +107 -0
  93. package/dist/sessionHost/entry.js.map +1 -0
  94. package/dist/update/AutomaticUpdater.d.ts +2 -1
  95. package/dist/update/AutomaticUpdater.d.ts.map +1 -1
  96. package/dist/update/AutomaticUpdater.js +26 -12
  97. package/dist/update/AutomaticUpdater.js.map +1 -1
  98. package/dist/update/PinnedHostBundle.d.ts +49 -0
  99. package/dist/update/PinnedHostBundle.d.ts.map +1 -0
  100. package/dist/update/PinnedHostBundle.js +309 -0
  101. package/dist/update/PinnedHostBundle.js.map +1 -0
  102. package/native/prebuilds/darwin-arm64/managed_native.node +0 -0
  103. package/native/prebuilds/darwin-arm64/manifest.json +1 -1
  104. package/native/prebuilds/darwin-x64/managed_native.node +0 -0
  105. package/native/prebuilds/darwin-x64/manifest.json +1 -1
  106. package/native/prebuilds/win32-arm64/managed_native.node +0 -0
  107. package/native/prebuilds/win32-arm64/manifest.json +1 -1
  108. package/native/prebuilds/win32-x64/managed_native.node +0 -0
  109. package/native/prebuilds/win32-x64/manifest.json +1 -1
  110. package/package.json +1 -1
@@ -96,9 +96,10 @@ function sameOwner(left, right) {
96
96
  function sameCleanupOwner(left, right) {
97
97
  return left.threadId === right.threadId && left.openId === right.openId;
98
98
  }
99
- /** Terminal intent prevents recreation, but uncertain native cleanup still owns admission. */
99
+ /** Failed future-only history is inactive; live runtimes still own admission.
100
+ * Other providers retain their existing native cleanup obligations. */
100
101
  function recoveryOwnsWork(record) {
101
- return !!record.recovery && (!['completed', 'cancelled'].includes(record.state)
102
+ return !!record.recovery && !(record.state === 'failed' && record.recovery.futureOnly) && (!['completed', 'cancelled'].includes(record.state)
102
103
  || !!record.error?.startsWith('Native process exit unconfirmed:'));
103
104
  }
104
105
  function deliveryId(scheduleId, occurrenceId) {
@@ -143,6 +144,8 @@ class ScheduleService {
143
144
  suspending = false;
144
145
  handoffUnresolved = false;
145
146
  suspension;
147
+ hostRetirement;
148
+ hostsRetired = false;
146
149
  constructor(options) {
147
150
  this.options = { ...options };
148
151
  this.now = options.now ?? Date.now;
@@ -182,6 +185,16 @@ class ScheduleService {
182
185
  }
183
186
  continue; // Terminal tombstones always win over a stale recovery checkpoint.
184
187
  }
188
+ // A consumed future-only attempt or an already blocked interrupted row
189
+ // cannot regain automatic admission after another service restart.
190
+ if (record.recovery?.futureOnly && (record.state === 'creating'
191
+ || record.state === 'interrupted' && record.recovery.status === 'blocked')) {
192
+ record.state = 'failed';
193
+ record.recovery.status = 'blocked';
194
+ record.error ??= 'Interrupted native restoration quarantined; original session and cleanup evidence retained.';
195
+ changed = true;
196
+ continue;
197
+ }
185
198
  if (LIVE.has(record.state) || record.recovery?.status === 'pending'
186
199
  || record.state === 'interrupted' && record.recovery?.futureOnly && record.rule.kind === 'cron') {
187
200
  const futureOnly = record.recovery?.futureOnly && record.rule.kind === 'cron';
@@ -260,6 +273,15 @@ class ScheduleService {
260
273
  runtime.completed = new Set(record.completedOccurrences);
261
274
  const controller = new AbortController();
262
275
  runtime.recoveryAbort = controller;
276
+ // Admission contention has not consumed this job's clean checkpoint.
277
+ // Leave it pending so the exact earlier runtime can release the thread.
278
+ try {
279
+ runtime.releaseAdmission = this.options.acquireRecovery?.(structuredClone(record));
280
+ }
281
+ catch (error) {
282
+ await this.recoveryDiagnostic(record, errorText(error));
283
+ continue;
284
+ }
263
285
  this.runtimes.set(id, runtime);
264
286
  const current = () => !controller.signal.aborted && !this.closing && !this.fault && !record.cancelRequested
265
287
  && this.current(record) && this.options.canRecover?.(structuredClone(record)) === true;
@@ -277,7 +299,6 @@ class ScheduleService {
277
299
  let admitted = false;
278
300
  let succeeded = false;
279
301
  try {
280
- runtime.releaseAdmission = this.options.acquireRecovery?.(structuredClone(record));
281
302
  await this.serial(async () => {
282
303
  if (!current())
283
304
  throw new Error('Recovery admission expired.');
@@ -288,6 +309,7 @@ class ScheduleService {
288
309
  await this.persist();
289
310
  });
290
311
  admitted = true;
312
+ runtime.recoveryAdmitted = true;
291
313
  const admission = Promise.resolve().then(() => provider.reconcile(restoreRecord, { notBefore: this.now(), signal: controller.signal, isCurrent: current,
292
314
  isOwnerCurrent: () => !record.cancelRequested && !this.fault && this.current(record),
293
315
  canAdmitOccurrence: () => this.canAdmitOccurrence(id, runtime) }, emit));
@@ -314,12 +336,28 @@ class ScheduleService {
314
336
  await this.stop(id, runtime).catch(() => { });
315
337
  }
316
338
  runtime.resolveReady(runtime.handle);
339
+ if (!runtime.handle && (error instanceof types_1.NativeScheduleRecoverySettledError
340
+ || error instanceof types_1.NativeScheduleNotAdmittedError || error instanceof types_1.NativeScheduleAbsentError)) {
341
+ runtime.stopped = true;
342
+ this.releaseRecoveryAdmission(runtime);
343
+ if (this.runtimes.get(id) === runtime)
344
+ this.runtimes.delete(id);
345
+ }
317
346
  // A rejection without a handle is not proof that native admission never happened.
318
347
  throw error;
319
348
  });
320
349
  const handle = await Promise.race([receipt, new Promise((_, reject) => {
321
- timer = setTimeout(() => { controller.abort(); reject(new Error('Native recovery timed out; ownership retained, do not recreate.')); }, this.options.recoveryTimeoutMs ?? 30_000);
350
+ timer = setTimeout(() => {
351
+ controller.abort();
352
+ reject(new Error(record.recovery?.futureOnly
353
+ ? 'Native recovery timed out; this job will not restart automatically. Native settlement and cleanup remain separate.'
354
+ : 'Native recovery timed out; ownership retained, do not recreate.'));
355
+ }, this.options.recoveryTimeoutMs ?? 30_000);
322
356
  })]);
357
+ // Native recovery and inventory validation settled. A later deadline
358
+ // must not abort the adopted handle while its active checkpoint is saved.
359
+ clearTimeout(timer);
360
+ timer = undefined;
323
361
  text(handle.nativeId);
324
362
  // The provider's reconcile contract includes inventory validation; list is
325
363
  // additionally required to verify the returned handle before activating it.
@@ -329,6 +367,10 @@ class ScheduleService {
329
367
  if (!current())
330
368
  throw new Error('Recovery owner retired before receipt persistence.');
331
369
  record.nativeId = handle.nativeId;
370
+ if (handle.recoveryIdentity) {
371
+ text(handle.recoveryIdentity, 8192);
372
+ record.recovery.identity = handle.recoveryIdentity;
373
+ }
332
374
  if (record.state === 'creating')
333
375
  record.state = 'active';
334
376
  record.recovery.status = 'eligible';
@@ -339,12 +381,15 @@ class ScheduleService {
339
381
  }
340
382
  catch (error) {
341
383
  controller.abort();
342
- if (!admitted || error instanceof types_1.NativeScheduleAbsentError || error instanceof types_1.NativeScheduleNotAdmittedError) {
384
+ if (!admitted || error instanceof types_1.NativeScheduleAbsentError || error instanceof types_1.NativeScheduleNotAdmittedError || error instanceof types_1.NativeScheduleRecoverySettledError) {
343
385
  runtime.stopped = true;
344
386
  runtime.resolveReady(undefined);
345
387
  }
346
388
  // Do not retry an ambiguous mutation or release its handle on a timeout.
347
- await this.blockRecovery(record, errorText(error));
389
+ if (admitted)
390
+ await this.blockRecovery(record, errorText(error), !!record.recovery?.futureOnly);
391
+ else
392
+ await this.recoveryDiagnostic(record, errorText(error));
348
393
  if (runtime.handle)
349
394
  await this.stop(id, runtime).catch(() => { });
350
395
  else if (runtime.stopped)
@@ -354,8 +399,7 @@ class ScheduleService {
354
399
  if (timer)
355
400
  clearTimeout(timer);
356
401
  if (succeeded || runtime.stopped) {
357
- runtime.releaseAdmission?.();
358
- runtime.releaseAdmission = undefined;
402
+ this.releaseRecoveryAdmission(runtime);
359
403
  }
360
404
  }
361
405
  }
@@ -374,6 +418,20 @@ class ScheduleService {
374
418
  released.add(record.threadId);
375
419
  }
376
420
  }
421
+ releaseRecoveryAdmission(runtime) {
422
+ const release = runtime.releaseAdmission;
423
+ runtime.releaseAdmission = undefined;
424
+ release?.();
425
+ if (!release || !runtime.recoveryAdmitted || !runtime.recoveryAbort?.signal.aborted)
426
+ return;
427
+ // Join the current pass before retrying untouched siblings. Failed rows are
428
+ // never candidates, and canRecover/acquireRecovery recheck newer admission.
429
+ const retry = Promise.resolve(this.recoveryRun).catch(() => { }).then(() => {
430
+ if (!this.closing && !this.fault && this.hasPendingRecovery())
431
+ return this.recover();
432
+ });
433
+ this.track(retry);
434
+ }
377
435
  checkpoint(value) {
378
436
  return { version: 1, provider: value.provider, strategy: value.strategy, identity: value.identity,
379
437
  ...(value.orderlyOnly && { orderlyOnly: true }), ...(value.futureOnly && { futureOnly: true }), ...(value.handoffPolicy && { handoffPolicy: value.handoffPolicy }), ...(value.handoff && { handoff: value.handoff }),
@@ -394,12 +452,13 @@ class ScheduleService {
394
452
  text(value.identity, 8192);
395
453
  }
396
454
  async recoveryDiagnostic(record, reason) {
397
- await this.serial(async () => { record.error = reason; await this.persist(); });
455
+ await this.serial(async () => { record.error = reason; if (!this.fault)
456
+ await this.persist(); });
398
457
  }
399
- async blockRecovery(record, reason) {
458
+ async blockRecovery(record, reason, quarantine = false) {
400
459
  await this.serial(async () => {
401
460
  if (!['completed', 'cancelled'].includes(record.state))
402
- record.state = 'interrupted';
461
+ record.state = quarantine ? 'failed' : 'interrupted';
403
462
  record.recovery.status = 'blocked';
404
463
  record.error = reason;
405
464
  if (!this.fault)
@@ -858,11 +917,87 @@ class ScheduleService {
858
917
  return erase(evidence);
859
918
  });
860
919
  }
920
+ /** Durable outputs still awaiting their external delivery receipt. */
921
+ hasPendingResults() { return this.pending.size > 0; }
861
922
  /** Includes terminal jobs whose native process exit is still unconfirmed. */
862
923
  hasActiveSchedules(threadId) {
863
924
  return [...this.records.values()].some(record => (!threadId || record.threadId === threadId)
864
925
  && (LIVE.has(record.state) || recoveryOwnsWork(record) || (this.runtimes.has(record.id) && !this.runtimes.get(record.id).stopped)));
865
926
  }
927
+ /** Admission to explicit process retirement, NOT proof of native exit or queue
928
+ * reconstruction. Only the original live handles can supply deletion receipts.
929
+ */
930
+ canRetireDrainedHosts() {
931
+ if (!this.initialized || this.closing || this.fault || this.hostRetirement || this.suspending
932
+ || this.handoffUnresolved || this.recoveryRun || this.operations.size || this.tasks.size
933
+ || this.recordOperations.size || this.pending.size || this.delivering.size || this.refreshes
934
+ || this.retainedCancellations.size || this.purges.size || this.cleanup.size)
935
+ return false;
936
+ try {
937
+ this.catalogGate?.verify();
938
+ return [...this.records.values()].every(record => {
939
+ if (record.error || record.recovery || !this.current(record) || this.options.isNativeMutationPending?.(ownerOf(record)))
940
+ return false;
941
+ const runtime = this.runtimes.get(record.id);
942
+ if (!runtime)
943
+ return record.state === 'cancelled' && record.cancelRequested === true && record.cleanupConfirmed === true;
944
+ if (!runtime.handle || runtime.running.size || runtime.heldDeliveries.size || runtime.cancelling || runtime.releasing)
945
+ return false;
946
+ if (record.state === 'completed')
947
+ return record.rule.kind === 'once' && runtime.completed.size > 0;
948
+ return record.state === 'cancelled' && record.cancelRequested === true && runtime.removalConfirmed === true
949
+ && (runtime.handle.nativeDefinitionAbsent === true || runtime.stopped && record.cleanupConfirmed === true);
950
+ });
951
+ }
952
+ catch {
953
+ return false;
954
+ }
955
+ }
956
+ /** Synchronous fence: no asynchronous gap between checking and revoking new
957
+ * schedule operations. Retain exact pre-exit deletion receipts for finalization.
958
+ */
959
+ beginDrainedHostRetirement() {
960
+ if (this.hostRetirement)
961
+ return;
962
+ if (!this.canRetireDrainedHosts())
963
+ throw new Error('Native schedules are not drained for explicit host retirement.');
964
+ this.hostRetirement = new Map([...this.runtimes].map(([id, runtime]) => [id,
965
+ { runtime, cancelled: this.records.get(id)?.cancelRequested === true && runtime.removalConfirmed === true }]));
966
+ }
967
+ /** Called only after the owner joined its original executor groups and drained
968
+ * native callback ingestion. Never calls stop/cancel/releaseForRestart on a
969
+ * provider. Failure preserves the same retirement fence, handles and catalog.
970
+ */
971
+ async finishDrainedHostRetirement() {
972
+ if (!this.hostRetirement)
973
+ throw new Error('Explicit host retirement was not admitted.');
974
+ while (this.tasks.size)
975
+ await Promise.allSettled([...this.tasks]);
976
+ await this.deliveryTail;
977
+ await this.serial(async () => {
978
+ if (this.fault)
979
+ throw this.fault;
980
+ if (this.pending.size || this.operations.size || this.recordOperations.size || this.delivering.size)
981
+ throw new Error('Native retirement still owns pending work or output.');
982
+ for (const [id, proof] of this.hostRetirement) {
983
+ const runtime = this.runtimes.get(id), record = this.records.get(id);
984
+ if (runtime !== proof.runtime || !record || runtime.running.size || runtime.heldDeliveries.size
985
+ || !(runtime.stopped || runtime.handle?.retirementConfirmed === true))
986
+ throw new Error('Exact native host retirement is unconfirmed.');
987
+ }
988
+ for (const [id, proof] of this.hostRetirement) {
989
+ proof.runtime.stopped = true;
990
+ proof.runtime.releaseAdmission?.();
991
+ proof.runtime.releaseAdmission = undefined;
992
+ // AGY invalidates its inventory view after exit. Preserve the exact
993
+ // deletion receipt captured BEFORE exit rather than querying a new host.
994
+ if (proof.cancelled)
995
+ this.records.get(id).cleanupConfirmed = true;
996
+ }
997
+ await this.persist();
998
+ this.hostsRetired = true;
999
+ });
1000
+ }
866
1001
  /** Card eligibility includes retryable cleanup even for a terminal catalog record. */
867
1002
  canCancel(owner, id) {
868
1003
  this.checkReady();
@@ -870,7 +1005,7 @@ class ScheduleService {
870
1005
  const runtime = this.runtimes.get(id);
871
1006
  if (record.state === 'cancelled' && runtime?.handle?.cancellationKeepsRunning && !record.error)
872
1007
  return false;
873
- return LIVE.has(record.state) || recoveryOwnsWork(record) || record.state === 'interrupted' && !!record.nativeId && this.supportsRetainedCancellation(record)
1008
+ return LIVE.has(record.state) || recoveryOwnsWork(record) || ['interrupted', 'failed'].includes(record.state) && !!record.nativeId && this.supportsRetainedCancellation(record)
874
1009
  || Boolean(runtime && !runtime.stopped);
875
1010
  }
876
1011
  /** Newly observed cleanup facts never supply historical recovery proof. The optional
@@ -895,7 +1030,7 @@ class ScheduleService {
895
1030
  record.error = absent ? 'Owner-reviewed native definition absent on current host; predecessor work remains unknown.'
896
1031
  : 'Owner-reviewed native deletion requested; absence and predecessor work remain unconfirmed.';
897
1032
  const runtime = this.runtimes.get(id);
898
- if (absent && retirementConfirmed?.() === true && (!runtime || runtime.stopped && !runtime.running.size)
1033
+ if (absent && retirementConfirmed?.(record) === true && (!runtime || runtime.stopped && !runtime.running.size)
899
1034
  && !this.recordOperations.has(id) && !this.recoveryRun && !this.refreshes && !this.retainedCancellations.has(id)) {
900
1035
  record.state = 'cancelled';
901
1036
  record.cleanupConfirmed = true;
@@ -923,50 +1058,60 @@ class ScheduleService {
923
1058
  await this.persist();
924
1059
  });
925
1060
  }
926
- /** User-requested native reconciliation only; no background polling is installed. */
1061
+ /** Observe native inventory without deleting definitions or retiring native work. */
927
1062
  refresh(owner) {
928
1063
  this.refreshes++;
929
- return this.operation(this.refreshNative(owner).finally(() => { this.refreshes--; }));
1064
+ return this.operation(this.refreshNative(structuredClone(owner)).finally(() => { this.refreshes--; }));
930
1065
  }
931
1066
  async refreshNative(owner) {
932
1067
  this.checkReady();
933
1068
  validateOwner(owner);
934
1069
  const candidates = await this.serial(async () => [...this.records.values()]
935
1070
  .filter(record => sameOwner(record, owner)).map(record => record.id));
1071
+ let failure;
936
1072
  for (const id of candidates) {
1073
+ const record = this.records.get(id);
937
1074
  const runtime = this.runtimes.get(id);
938
- if (!runtime?.handle || runtime.stopped || runtime.cancelling || runtime.running.size || !this.current(owner))
1075
+ const handle = runtime?.handle;
1076
+ if (!record || !runtime || !handle)
939
1077
  continue;
940
- if (!LIVE.has(this.records.get(id).state)) {
941
- await this.stop(id, runtime);
1078
+ const nativeId = record.nativeId;
1079
+ const completed = runtime.completed.size;
1080
+ // Recheck after every read: cancellation, execution or rebound may have
1081
+ // advanced while inventory was awaiting its native receipt.
1082
+ const readable = () => !this.closing && !this.fault && !this.suspending && this.current(owner)
1083
+ && this.records.get(id) === record && record.nativeId === nativeId && record.state === 'active' && !record.cancelRequested
1084
+ && this.runtimes.get(id) === runtime && runtime.handle === handle && !runtime.stopped && !runtime.cancelling
1085
+ && !runtime.running.size && runtime.completed.size === completed && !runtime.stopPromise && !runtime.releaseAdmission
1086
+ && !this.recordOperations.has(id) && !this.retainedCancellations.has(id);
1087
+ if (!readable())
942
1088
  continue;
943
- }
1089
+ let present;
944
1090
  try {
945
- const present = await runtime.handle.list();
946
- if (present)
947
- continue;
948
- await this.serial(async () => {
949
- const record = this.records.get(id);
950
- if (LIVE.has(record.state)) {
951
- record.state = 'interrupted';
952
- record.error = 'Native schedule is no longer listed; execution completion is not confirmed.';
953
- await this.persist();
954
- }
955
- });
956
- await this.stop(id, runtime);
1091
+ present = await handle.list();
1092
+ if (typeof present !== 'boolean')
1093
+ throw new Error('Native inventory returned no verified presence or absence.');
957
1094
  }
958
1095
  catch (error) {
959
- await this.serial(async () => {
960
- const record = this.records.get(id);
961
- if (LIVE.has(record.state))
962
- record.state = 'interrupted';
963
- record.error = errorText(error);
964
- if (!this.fault)
965
- await this.persist();
966
- });
967
- await this.stop(id, runtime);
1096
+ // Read uncertainty is not absence or cancellation authority. Leave the
1097
+ // durable record, recovery fences and future occurrence admission intact.
1098
+ if (readable())
1099
+ failure ??= new Error(`Native inventory could not be verified for ${id}: ${errorText(error)}`);
1100
+ continue;
968
1101
  }
1102
+ if (present)
1103
+ continue;
1104
+ await this.serial(async () => {
1105
+ if (!readable())
1106
+ return;
1107
+ record.state = 'interrupted';
1108
+ const detail = 'Native schedule is no longer listed; execution completion and cleanup are not confirmed.';
1109
+ record.error = record.error ? `${bounded(record.error, ERROR_BYTES - Buffer.byteLength(detail) - 1)}\n${detail}` : detail;
1110
+ await this.persist();
1111
+ });
969
1112
  }
1113
+ if (failure)
1114
+ throw failure;
970
1115
  }
971
1116
  cancel(owner, id) {
972
1117
  return this.operation(this.recordOperation(id, this.cancelSchedule(owner, id)));
@@ -996,8 +1141,8 @@ class ScheduleService {
996
1141
  }
997
1142
  const retainedCleanup = !runtime || runtime.stopped && !runtime.handle?.nativeDefinitionAbsent
998
1143
  && this.supportsRetainedCancellation(record);
999
- if (retainedCleanup && (recoveryOwnsWork(record)
1000
- || record.state === 'interrupted' && !!record.nativeId && this.supportsRetainedCancellation(record))) {
1144
+ if (retainedCleanup && (recoveryOwnsWork(record) || record.state === 'failed' && !!record.recovery
1145
+ || ['interrupted', 'failed'].includes(record.state) && !!record.nativeId && this.supportsRetainedCancellation(record))) {
1001
1146
  operation = Promise.resolve().then(() => this.cancelRetained(record));
1002
1147
  this.retainedCancellations.set(id, operation);
1003
1148
  return;
@@ -1078,15 +1223,15 @@ class ScheduleService {
1078
1223
  }
1079
1224
  hasUnresolvedHandoff() { return this.handoffUnresolved; }
1080
1225
  hasUnresolvedRecovery() {
1081
- return [...this.records.values()].some(record => recoveryOwnsWork(record)
1082
- && record.recovery?.status !== 'eligible');
1226
+ return [...this.runtimes.values()].some(runtime => !!runtime.recoveryAbort && !runtime.stopped && runtime.recoveryAbort.signal.aborted)
1227
+ || [...this.records.values()].some(record => recoveryOwnsWork(record) && record.recovery?.status !== 'eligible');
1083
1228
  }
1084
1229
  hasPendingRecovery() {
1085
1230
  return [...this.records.values()].some(record => record.state === 'interrupted' && record.recovery?.status === 'pending');
1086
1231
  }
1087
1232
  /** Snapshot eligibility is only admission to a handoff, never permission to install. */
1088
1233
  canSuspendForRestart() {
1089
- if (!this.initialized || this.closing || this.fault || this.suspending || this.recoveryRun || this.operations.size || this.pending.size)
1234
+ if (!this.initialized || this.closing || this.hostRetirement || this.fault || this.suspending || this.recoveryRun || this.operations.size || this.pending.size)
1090
1235
  return false;
1091
1236
  return [...this.records.values()].filter(record => LIVE.has(record.state) || recoveryOwnsWork(record)
1092
1237
  || !!this.runtimes.get(record.id) && !this.runtimes.get(record.id).stopped).every(record => {
@@ -1225,9 +1370,12 @@ class ScheduleService {
1225
1370
  }
1226
1371
  /** Providers with durable intent must complete handoff before unrelated host teardown. */
1227
1372
  requiresShutdownHandoff() {
1228
- return [...this.records.values()].some(record => !!record.recovery);
1373
+ return [...this.records.values()].some(record => !!record.recovery
1374
+ && (!(record.state === 'failed' && record.recovery.futureOnly) || !!this.runtimes.get(record.id) && !this.runtimes.get(record.id).stopped));
1229
1375
  }
1230
1376
  destroy() {
1377
+ if (this.hostRetirement && !this.hostsRetired)
1378
+ return Promise.reject(new Error('Explicit native host retirement remains unresolved.'));
1231
1379
  if (this.destruction)
1232
1380
  return this.destruction;
1233
1381
  const preserve = !this.closing && this.hasActiveSchedules() && this.canSuspendForRestart();
@@ -1370,6 +1518,10 @@ class ScheduleService {
1370
1518
  if (this.fault)
1371
1519
  throw this.fault;
1372
1520
  const record = this.records.get(id);
1521
+ if (this.hostRetirement && (event.type === 'started' || event.type === 'rebound'
1522
+ || (event.type === 'completed' || event.type === 'skipped') && !runtime.completed.has(event.occurrenceId))) {
1523
+ throw new Error('Unexpected native work during explicit retirement; retain the native event journal.');
1524
+ }
1373
1525
  if (this.suspending && event.type === 'stopped') {
1374
1526
  if (event.exitConfirmed === false)
1375
1527
  throw new Error('Native handoff exit unconfirmed.');
@@ -1382,8 +1534,7 @@ class ScheduleService {
1382
1534
  // after a failed cancellation cleanup. Never downgrade confirmed exit.
1383
1535
  if (event.type === 'stopped' && event.exitConfirmed !== false) {
1384
1536
  runtime.stopped = true;
1385
- runtime.releaseAdmission?.();
1386
- runtime.releaseAdmission = undefined;
1537
+ this.releaseRecoveryAdmission(runtime);
1387
1538
  if (record.error?.startsWith('Native process exit unconfirmed:')) {
1388
1539
  delete record.error;
1389
1540
  await this.persist();
@@ -1422,9 +1573,10 @@ class ScheduleService {
1422
1573
  await this.persist();
1423
1574
  return;
1424
1575
  }
1576
+ const finishingFailed = record.state === 'failed' && event.type === 'completed' && runtime.running.has(event.occurrenceId);
1425
1577
  const finishingCancelled = record.state === 'cancelled' && runtime.handle?.cancellationKeepsRunning
1426
1578
  && event.type === 'completed' && runtime.running.has(event.occurrenceId);
1427
- if ((!finishingCancelled && record.state === 'cancelled') || record.state === 'failed' || record.state === 'completed')
1579
+ if ((!finishingCancelled && record.state === 'cancelled') || record.state === 'failed' && !finishingFailed || record.state === 'completed')
1428
1580
  return;
1429
1581
  if (event.type === 'stopped') {
1430
1582
  if (LIVE.has(record.state)) {
@@ -1484,7 +1636,7 @@ class ScheduleService {
1484
1636
  record.state = 'completed';
1485
1637
  cleanup = true;
1486
1638
  }
1487
- else if (record.state !== 'interrupted') {
1639
+ else if (record.state !== 'interrupted' && record.state !== 'failed') {
1488
1640
  record.state = runtime.running.size ? 'running' : 'active';
1489
1641
  if (record.recovery && !runtime.running.size)
1490
1642
  record.recovery.status = 'eligible';
@@ -1502,8 +1654,13 @@ class ScheduleService {
1502
1654
  // Persistence failures still set the service fault inside persist().
1503
1655
  if (cleanup && !runtime.cancelling)
1504
1656
  this.track(this.stop(id, runtime).catch(() => { }));
1505
- if (delivery)
1506
- await this.attemptDeliveries([delivery], true);
1657
+ if (delivery) {
1658
+ const delivering = this.attemptDeliveries([delivery], true);
1659
+ if (this.options.deferDelivery)
1660
+ this.track(delivering);
1661
+ else
1662
+ await delivering;
1663
+ }
1507
1664
  }
1508
1665
  attemptDeliveries(ids, firstAttempt = false) {
1509
1666
  const run = this.deliveryTail.then(async () => {
@@ -1582,8 +1739,7 @@ class ScheduleService {
1582
1739
  if (handle?.retirementConfirmed !== false && (!handle?.cancellationKeepsRunning || !runtime.running.size))
1583
1740
  runtime.stopped = true;
1584
1741
  if (runtime.stopped) {
1585
- runtime.releaseAdmission?.();
1586
- runtime.releaseAdmission = undefined;
1742
+ this.releaseRecoveryAdmission(runtime);
1587
1743
  await this.rememberCleanup(id, runtime);
1588
1744
  }
1589
1745
  }
@@ -1627,8 +1783,8 @@ class ScheduleService {
1627
1783
  }
1628
1784
  canAdmitOccurrence(id, runtime) {
1629
1785
  const record = this.records.get(id);
1630
- return !!record && this.runtimes.get(id) === runtime && !this.closing && !this.fault
1631
- && !runtime.stopped && !runtime.cancelling && !record.cancelRequested
1786
+ return !!record && this.runtimes.get(id) === runtime && !this.closing && !this.hostRetirement && !this.fault
1787
+ && !runtime.stopped && !runtime.cancelling && !runtime.recoveryAbort?.signal.aborted && !record.cancelRequested
1632
1788
  && LIVE.has(record.state) && this.current(record);
1633
1789
  }
1634
1790
  newRuntime() {
@@ -1641,8 +1797,9 @@ class ScheduleService {
1641
1797
  void task.catch(error => {
1642
1798
  // Persistence/protocol failures fail closed and stop native producers.
1643
1799
  this.fault ??= error instanceof Error ? error : new Error('Schedule service failed.');
1644
- for (const [id, runtime] of this.runtimes)
1645
- void this.stop(id, runtime).catch(() => { });
1800
+ if (!this.hostRetirement)
1801
+ for (const [id, runtime] of this.runtimes)
1802
+ void this.stop(id, runtime).catch(() => { });
1646
1803
  }).finally(() => this.tasks.delete(task));
1647
1804
  }
1648
1805
  operation(task) {
@@ -1682,7 +1839,7 @@ class ScheduleService {
1682
1839
  checkReady() {
1683
1840
  if (this.fault)
1684
1841
  throw this.fault;
1685
- if (!this.initialized || this.closing || this.suspending)
1842
+ if (!this.initialized || this.closing || this.hostRetirement || this.suspending)
1686
1843
  throw new Error('Schedule service is not initialized or is closed.');
1687
1844
  }
1688
1845
  serial(operation) {
@@ -1844,8 +2001,10 @@ class ScheduleService {
1844
2001
  if (entry.nativeId !== undefined)
1845
2002
  text(entry.nativeId);
1846
2003
  if (entry.attribution !== undefined) {
1847
- const a = object(entry.attribution, ['sessionId', 'marker', 'promptHash', 'rule']);
2004
+ const a = object(entry.attribution, ['sessionId', 'marker', 'ownerToken', 'promptHash', 'rule']);
1848
2005
  text(a.sessionId);
2006
+ if (a.ownerToken !== undefined && (entry.backend !== 'claude' || typeof a.ownerToken !== 'string' || !/^[a-f0-9-]{36}$/.test(a.ownerToken)))
2007
+ throw new Error('Invalid cleanup owner incarnation.');
1849
2008
  if (typeof a.promptHash !== 'string' || !/^[a-f0-9]{64}$/.test(a.promptHash)
1850
2009
  || (entry.backend === 'claude' ? typeof a.marker !== 'string' || !/^[a-f0-9-]{36}$/.test(a.marker) : a.marker !== undefined))
1851
2010
  throw new Error('Invalid cleanup attribution.');
@@ -1914,8 +2073,9 @@ class ScheduleService {
1914
2073
  }
1915
2074
  catch (error) {
1916
2075
  this.fault = error instanceof Error ? error : new Error('Schedule catalog write failed.');
1917
- for (const [id, runtime] of this.runtimes)
1918
- void this.stop(id, runtime).catch(() => { });
2076
+ if (!this.hostRetirement)
2077
+ for (const [id, runtime] of this.runtimes)
2078
+ void this.stop(id, runtime).catch(() => { });
1919
2079
  throw this.fault;
1920
2080
  }
1921
2081
  finally {