@bian-womp/spark-graph 0.2.86 → 0.2.88

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.
@@ -58,6 +58,12 @@ export declare class GraphRuntime {
58
58
  resume(): void;
59
59
  invalidateDownstream(nodeId: string): void;
60
60
  scheduleInputsChanged(nodeId: string): void;
61
+ /**
62
+ * Cancel all active runs for a node and emit cancellation events
63
+ * @param node - The node to cancel runs for
64
+ * @param reason - The cancellation reason ("snapshot" | "node-deleted")
65
+ */
66
+ private cancelNodeActiveRuns;
61
67
  cancelNodeRuns(nodeIds: string[]): void;
62
68
  hydrate(payload: {
63
69
  inputs?: Record<string, Record<string, unknown>>;
@@ -1 +1 @@
1
- {"version":3,"file":"GraphRuntime.d.ts","sourceRoot":"","sources":["../../../../src/runtime/GraphRuntime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,eAAe,EAKhB,MAAM,2BAA2B,CAAC;AAEnC,OAAO,KAAK,EAIV,gBAAgB,EACjB,MAAM,eAAe,CAAC;AAQvB,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE/C,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,gBAAgB,IAAI,CAC7D,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,KAClB,IAAI,CAAC;AAUV,MAAM,MAAM,gBAAgB,GAAG,OAAO,GAAG,YAAY,GAAG,OAAO,GAAG,OAAO,CAAC;AAE1E,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,YAAY,CAAC;IACpB,UAAU,EAAE,iBAAiB,CAAC;IAC9B,KAAK,EAAE,YAAY,CAAC;IACpB,KAAK,EAAE,YAAY,CAAC;CACrB;AAuCD,qBAAa,YAAY;IACvB,OAAO,CAAC,KAAK,CAAuC;IACpD,OAAO,CAAC,KAAK,CAAqB;IAElC,OAAO,CAAC,QA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gB,CAAC;KAC1B,GACD,SAAS;IAYb,cAAc,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAIzC,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;IASlD,WAAW,IAAI,eAAe;IAsBxB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAkB/B,KAAK,IAAI,IAAI;IAIb,QAAQ,IAAI,OAAO;IAInB,MAAM,IAAI,IAAI;IAId,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAI1C,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAI3C;;;;OAIG;IACH,OAAO,CAAC,oBAAoB;IAmC5B,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI;IAsCvC,OAAO,CACL,OAAO,EAAE;QACP,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;QACjD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;KACnD,EACD,IAAI,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,GAC1B,IAAI;IA6CP,MAAM,CAAC,GAAG,EAAE,eAAe,EAAE,QAAQ,EAAE,QAAQ,GAAG,IAAI;IA4PtD,OAAO,CAAC,wBAAwB;YAWlB,uBAAuB;CA6DtC"}
package/lib/esm/index.js CHANGED
@@ -758,6 +758,7 @@ class GraphRuntime {
758
758
  this.emit("stats", {
759
759
  kind: "node-progress",
760
760
  nodeId,
761
+ typeId: node.typeId,
761
762
  runId,
762
763
  progress: node.stats.progress,
763
764
  });
@@ -774,15 +775,16 @@ class GraphRuntime {
774
775
  await node.runtime.onInputsChanged?.(capturedInputs, ctx);
775
776
  }
776
777
  catch (err) {
777
- // Suppress errors caused by expected cancellations (switch, snapshot)
778
+ // Suppress errors caused by expected cancellations
778
779
  if (controller.signal.aborted) {
779
780
  const reason = controller.signal.reason;
780
- if (reason === "switch" || reason === "snapshot") {
781
- return; // ignore switched/snapshot-cancelled runs
781
+ if (reason === "switch" ||
782
+ reason === "snapshot" ||
783
+ reason === "node-deleted") {
784
+ return; // Cancellation events are emitted separately, skip error handling
782
785
  }
783
786
  }
784
787
  hadError = true;
785
- // Record last error for node
786
788
  node.stats.lastError = err;
787
789
  const retry = policy.retry;
788
790
  if (retry && attempt < (retry.attempts ?? 0)) {
@@ -793,6 +795,10 @@ class GraphRuntime {
793
795
  this.emit("error", { kind: "node-run", nodeId, runId, err });
794
796
  }
795
797
  finally {
798
+ // Skip cleanup if node was deleted (cleanup already handled)
799
+ if (!this.nodes.has(nodeId)) {
800
+ return;
801
+ }
796
802
  if (timeoutId)
797
803
  clearTimeout(timeoutId);
798
804
  node.activeControllers.delete(controller);
@@ -803,15 +809,21 @@ class GraphRuntime {
803
809
  node.stats.lastStartAt && node.stats.lastEndAt
804
810
  ? node.stats.lastEndAt - node.stats.lastStartAt
805
811
  : undefined;
806
- // Clear lastError upon successful completion
807
812
  if (!hadError)
808
813
  node.stats.lastError = undefined;
809
- this.emit("stats", {
810
- kind: "node-done",
811
- nodeId,
812
- runId,
813
- durationMs: node.stats.lastDurationMs,
814
- });
814
+ // Only emit node-done if not cancelled (cancellation events emitted separately)
815
+ const isCancelled = controller.signal.aborted &&
816
+ (controller.signal.reason === "snapshot" ||
817
+ controller.signal.reason === "node-deleted");
818
+ if (!isCancelled) {
819
+ this.emit("stats", {
820
+ kind: "node-done",
821
+ nodeId,
822
+ typeId: node.typeId,
823
+ runId,
824
+ durationMs: node.stats.lastDurationMs,
825
+ });
826
+ }
815
827
  ctx.log("debug", "node-done", {
816
828
  durationMs: node.stats.lastDurationMs,
817
829
  hadError,
@@ -821,7 +833,12 @@ class GraphRuntime {
821
833
  }
822
834
  };
823
835
  // fire node-start event
824
- this.emit("stats", { kind: "node-start", nodeId, runId });
836
+ this.emit("stats", {
837
+ kind: "node-start",
838
+ nodeId,
839
+ typeId: node.typeId,
840
+ runId,
841
+ });
825
842
  ctx.log("debug", "node-start");
826
843
  exec(0);
827
844
  };
@@ -1017,6 +1034,7 @@ class GraphRuntime {
1017
1034
  this.emit("stats", {
1018
1035
  kind: "edge-start",
1019
1036
  edgeId: e.id,
1037
+ typeId: e.typeId,
1020
1038
  source: { nodeId: e.source.nodeId, handle: e.source.handle },
1021
1039
  target: { nodeId: e.target.nodeId, handle: e.target.handle },
1022
1040
  });
@@ -1037,6 +1055,7 @@ class GraphRuntime {
1037
1055
  this.emit("stats", {
1038
1056
  kind: "edge-done",
1039
1057
  edgeId: e.id,
1058
+ typeId: e.typeId,
1040
1059
  source: { nodeId: e.source.nodeId, handle: e.source.handle },
1041
1060
  target: { nodeId: e.target.nodeId, handle: e.target.handle },
1042
1061
  durationMs: e.stats.lastDurationMs,
@@ -1350,12 +1369,50 @@ class GraphRuntime {
1350
1369
  scheduleInputsChanged(nodeId) {
1351
1370
  this.scheduleInputsChangedInternal(nodeId);
1352
1371
  }
1372
+ /**
1373
+ * Cancel all active runs for a node and emit cancellation events
1374
+ * @param node - The node to cancel runs for
1375
+ * @param reason - The cancellation reason ("snapshot" | "node-deleted")
1376
+ */
1377
+ cancelNodeActiveRuns(node, reason) {
1378
+ for (const controller of Array.from(node.activeControllers)) {
1379
+ const runId = node.controllerRunIds.get(controller);
1380
+ if (runId) {
1381
+ // Track cancelled runIds for snapshot operations
1382
+ if (reason === "snapshot") {
1383
+ if (!node.snapshotCancelledRunIds) {
1384
+ node.snapshotCancelledRunIds = new Set();
1385
+ }
1386
+ node.snapshotCancelledRunIds.add(runId);
1387
+ }
1388
+ // Emit cancellation event
1389
+ this.emit("stats", {
1390
+ kind: "node-done",
1391
+ nodeId: node.nodeId,
1392
+ typeId: node.typeId,
1393
+ runId,
1394
+ cancelled: true,
1395
+ });
1396
+ }
1397
+ try {
1398
+ controller.abort(reason);
1399
+ }
1400
+ catch {
1401
+ // ignore abort errors
1402
+ }
1403
+ }
1404
+ node.activeControllers.clear();
1405
+ node.controllerRunIds.clear();
1406
+ node.stats.active = 0;
1407
+ node.queue = [];
1408
+ }
1353
1409
  cancelNodeRuns(nodeIds) {
1354
1410
  if (nodeIds.length === 0)
1355
1411
  return;
1356
1412
  const toCancel = new Set(nodeIds);
1357
1413
  const visited = new Set();
1358
1414
  const queue = [...nodeIds];
1415
+ // Collect all downstream nodes to cancel
1359
1416
  for (let i = 0; i < queue.length; i++) {
1360
1417
  const nodeId = queue[i];
1361
1418
  if (visited.has(nodeId))
@@ -1371,29 +1428,12 @@ class GraphRuntime {
1371
1428
  }
1372
1429
  }
1373
1430
  }
1431
+ // Cancel runs for all affected nodes
1374
1432
  for (const nodeId of toCancel) {
1375
1433
  const node = this.nodes.get(nodeId);
1376
1434
  if (!node)
1377
1435
  continue;
1378
- if (!node.snapshotCancelledRunIds) {
1379
- node.snapshotCancelledRunIds = new Set();
1380
- }
1381
- for (const controller of Array.from(node.activeControllers)) {
1382
- const rid = node.controllerRunIds.get(controller);
1383
- if (rid) {
1384
- node.snapshotCancelledRunIds.add(rid);
1385
- }
1386
- try {
1387
- controller.abort("snapshot");
1388
- }
1389
- catch {
1390
- // ignore
1391
- }
1392
- }
1393
- node.activeControllers.clear();
1394
- node.controllerRunIds.clear();
1395
- node.stats.active = 0;
1396
- node.queue = [];
1436
+ this.cancelNodeActiveRuns(node, "snapshot");
1397
1437
  node.runSeq += 1;
1398
1438
  const now = Date.now();
1399
1439
  node.latestRunId = `${nodeId}:${node.runSeq}:${now}:snapshot`;
@@ -1456,6 +1496,9 @@ class GraphRuntime {
1456
1496
  for (const nodeId of Array.from(currentIds)) {
1457
1497
  if (!desiredIds.has(nodeId)) {
1458
1498
  const node = this.nodes.get(nodeId);
1499
+ // Cancel all active runs and emit cancellation events
1500
+ this.cancelNodeActiveRuns(node, "node-deleted");
1501
+ // Cleanup node resources
1459
1502
  node.runtime.onDeactivated?.();
1460
1503
  node.runtime.dispose?.();
1461
1504
  node.lifecycle?.dispose?.({
@@ -1463,7 +1506,6 @@ class GraphRuntime {
1463
1506
  setState: (next) => Object.assign(node.state, next),
1464
1507
  });
1465
1508
  this.nodes.delete(nodeId);
1466
- // Clear any array buckets for this node
1467
1509
  this.arrayInputBuckets.delete(nodeId);
1468
1510
  }
1469
1511
  }