@agimon-ai/po-runner 0.0.0-alpha.169 → 0.0.0-alpha.170

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.
@@ -389,6 +389,29 @@ function createRunnerSequenceService(options) {
389
389
  await store.save(record);
390
390
  options.onChange();
391
391
  };
392
+ let recovery;
393
+ const recover = () => {
394
+ recovery ??= (async () => {
395
+ for (const record of await store.list(true)) {
396
+ if (disposed) return;
397
+ if (record.state === "running") {
398
+ record.state = "completed";
399
+ record.outcome = "interrupted";
400
+ record.notificationPending = false;
401
+ record.result = textResult("Sequence interrupted because its owning runtime ended. Pending commands were not replayed.", {
402
+ id: record.id,
403
+ sequenceId: record.id,
404
+ sequence: true
405
+ }, true);
406
+ await save(record);
407
+ } else if (record.notificationPending) active.set(record.id, { record });
408
+ }
409
+ })().catch((error) => {
410
+ recovery = void 0;
411
+ throw error;
412
+ });
413
+ return recovery;
414
+ };
392
415
  const notify = (record) => {
393
416
  const pending = notifications.get(record.id);
394
417
  if (pending) return pending;
@@ -396,8 +419,12 @@ function createRunnerSequenceService(options) {
396
419
  const work = (async () => {
397
420
  try {
398
421
  await options.onComplete?.(record);
422
+ await store.save({
423
+ ...record,
424
+ notificationPending: false
425
+ });
399
426
  record.notificationPending = false;
400
- await save(record);
427
+ options.onChange();
401
428
  } catch (error) {
402
429
  options.onWarning(`Could not report sequence ${record.id}: ${String(error)}`);
403
430
  }
@@ -544,6 +571,9 @@ function createRunnerSequenceService(options) {
544
571
  validateTimeout(params.timeout);
545
572
  if (!context.toolContinuation) throw new Error("This host does not support guarded Bash command sequences.");
546
573
  signal?.throwIfAborted();
574
+ await recover();
575
+ assertAvailable();
576
+ signal?.throwIfAborted();
547
577
  const controller = new AbortController();
548
578
  const abort = () => controller.abort(signal?.reason);
549
579
  signal?.addEventListener("abort", abort, { once: true });
@@ -601,7 +631,7 @@ function createRunnerSequenceService(options) {
601
631
  },
602
632
  async stop(id, reason) {
603
633
  const requested = await store.requestStop(id, reason);
604
- active.get(id)?.controller.abort(new Error(reason ?? "Sequence stop requested."));
634
+ active.get(id)?.controller?.abort(new Error(reason ?? "Sequence stop requested."));
605
635
  return requested;
606
636
  },
607
637
  list: (includeCompleted) => store.list(includeCompleted),
@@ -615,24 +645,14 @@ function createRunnerSequenceService(options) {
615
645
  }));
616
646
  },
617
647
  refresh() {
648
+ if (disposed) return Promise.resolve();
618
649
  refreshing ??= (async () => {
619
- for (const record of await store.list(true)) {
620
- const current = active.get(record.id);
621
- if (current) {
622
- if (record.state === "running" && await store.stopRequested(record.id)) current.controller.abort(/* @__PURE__ */ new Error("Sequence stop requested."));
623
- await notify(current.record);
624
- if (current.record.state === "completed" && !current.record.notificationPending) active.delete(record.id);
625
- } else if (record.state === "running") {
626
- record.state = "completed";
627
- record.outcome = "interrupted";
628
- record.notificationPending = false;
629
- record.result = textResult("Sequence interrupted because its owning runtime ended. Pending commands were not replayed.", {
630
- id: record.id,
631
- sequenceId: record.id,
632
- sequence: true
633
- }, true);
634
- await save(record);
635
- } else if (record.notificationPending) await notify(record);
650
+ await recover();
651
+ for (const { record, controller } of active.values()) {
652
+ if (disposed) return;
653
+ if (record.state === "running" && controller && await store.stopRequested(record.id)) controller.abort(/* @__PURE__ */ new Error("Sequence stop requested."));
654
+ await notify(record);
655
+ if (record.state === "completed" && !record.notificationPending) active.delete(record.id);
636
656
  }
637
657
  })().finally(() => {
638
658
  refreshing = void 0;
@@ -645,8 +665,12 @@ function createRunnerSequenceService(options) {
645
665
  async dispose() {
646
666
  if (disposed) return;
647
667
  disposed = true;
648
- for (const { controller } of active.values()) controller.abort(/* @__PURE__ */ new Error("Runner session ended."));
649
- await Promise.allSettled([...active.values()].map(({ work }) => work));
668
+ for (const { controller } of active.values()) controller?.abort(/* @__PURE__ */ new Error("Runner session ended."));
669
+ await Promise.allSettled([
670
+ recovery,
671
+ refreshing,
672
+ ...[...active.values()].map(({ work }) => work)
673
+ ]);
650
674
  }
651
675
  };
652
676
  }