@powerhousedao/reactor-workflow 6.2.3-dev.88 → 6.2.3-dev.90

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.
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import { $ as containsRedactedMarker, A as AttachmentCache, B as SecretCreatorMismatchError, C as blockLabel, Ct as isIndeterminateError, D as describeBuiltinPiece, E as builtinPiece, F as shapeConnection, G as secretRefFromId, H as SecretNotFoundError, I as resolveStepInput, J as PieceWorkerPool, K as stepBlock, L as expressionsIn, N as declaredConnectionIds, O as CORE_PIECE_NAME, P as declaredReactorConnectionIds, Q as PieceWorkerTimeoutError, R as unavailableValue, S as storeHandlers, St as setPieceRegistryUrl, T as blockPorts, U as SecretOwnerMismatchError, V as SecretDeletedError, W as parseSecretRef, X as PieceWorkerDisposedError, Y as PieceWorker, Z as PieceWorkerError, _ as effectiveRetryPolicy, _t as unsupportedTrigger, a as PieceRegistry, at as isPrivateAddress, b as isHostBound, bt as npmRegistryBaseUrl, c as packagePieces, ct as FileTooLargeError, d as runWorkflow, dt as pieceModuleRef, et as redact, f as canonicalJson, ft as sourcedResolver, g as checkDynamicProperties, gt as unsupportedAuth, h as DynamicPropertiesError, ht as authMethodFor, i as tracedSteps, it as DEFAULT_EGRESS_POLICY, j as BoundConnectionResolver, k as CORE_PIECE_VERSION, lt as maxFileBytes, m as stepConfigHash, mt as UnsupportedPieceFeatureError, n as WorkflowTelemetry, nt as rememberSecrets, o as installedPiece, ot as parseAddress, p as hashOf, q as triggerBlock, r as tracedMethods, rt as secretsFor, s as installedPieces, st as extractDedupeKey, t as TELEMETRY_SCOPE, tt as redactMessage, u as errorNameOf, ut as bundleResolver, v as ActivepiecesBlockExecutor, w as unpinnedNote, x as resolvedBlock, xt as pieceRegistrySource, y as CompositeBlockExecutor } from "./telemetry-BYDhITFO.js";
1
+ import { $ as PieceWorkerPool, A as builtinPiece, At as isIndeterminateError, B as shapeConnection, C as flowsHandlers, Ct as unsupportedAuth, D as blockLabel, Dt as npmRegistryBaseUrl, E as storeHandlers, F as AttachmentCache, G as SecretCreatorMismatchError, H as expressionsIn, I as BoundConnectionResolver, J as SecretOwnerMismatchError, K as SecretDeletedError, M as CORE_PIECE_NAME, N as CORE_PIECE_VERSION, O as unpinnedNote, Ot as pieceRegistrySource, P as loopItems, Q as triggerBlock, R as declaredConnectionIds, S as CompositeBlockExecutor, St as authMethodFor, T as resolvedBlock, U as unavailableValue, V as resolveStepInput, X as secretRefFromId, Y as parseSecretRef, Z as stepBlock, _ as stepConfigHash, _t as bundleResolver, a as PieceRegistry, at as redact, b as effectiveRetryPolicy, c as packagePieces, ct as secretsFor, d as runWorkflow, dt as parseAddress, et as PieceWorker, f as isLoopStep, ft as extractDedupeKey, g as hashOf, gt as HOST_ORIGIN, h as canonicalJson, ht as FLOWS_LIST, i as tracedSteps, it as containsRedactedMarker, j as describeBuiltinPiece, k as blockPorts, kt as setPieceRegistryUrl, lt as DEFAULT_EGRESS_POLICY, m as loopStructure, mt as maxFileBytes, n as WorkflowTelemetry, nt as PieceWorkerError, o as installedPiece, ot as redactMessage, p as loopConfigHash, pt as FileTooLargeError, q as SecretNotFoundError, r as tracedMethods, rt as PieceWorkerTimeoutError, s as installedPieces, st as rememberSecrets, t as TELEMETRY_SCOPE, tt as PieceWorkerDisposedError, u as errorNameOf, ut as isPrivateAddress, v as DynamicPropertiesError, vt as pieceModuleRef, w as isHostBound, wt as unsupportedTrigger, x as ActivepiecesBlockExecutor, xt as UnsupportedPieceFeatureError, y as checkDynamicProperties, yt as sourcedResolver, z as declaredReactorConnectionIds } from "./telemetry-BOS6FOtP.js";
2
2
  import { DENIED_ACTION_INDEXES, ReactorAccessDeniedError, ReactorAccessDeniedError as ReactorAccessDeniedError$1, ReactorJobFailedError, ReactorJobPendingError as ReactorJobPendingError$1, WebhookHandshakeStrategy, assertActionsApplied } from "@powerhousedao/pieces-framework";
3
3
  import { mkdirSync, openAsBlob, readFileSync, writeFileSync } from "node:fs";
4
4
  import { open, readFile, readdir, rm, stat } from "node:fs/promises";
5
5
  import { dirname, join } from "node:path";
6
6
  import { childLogger, garbageCollect, generateId, hashDocumentStateForScope, isDenied, sortOperations } from "document-model";
7
- import { DEFAULT_TIMEZONE, DOCUMENT_REF_KEY, ScheduleConfigError, checkTriggerStrategy, coerceManualPayload, containsDocumentRef, documentRefsIn, expandDocumentRefs, isDocumentRefMarker, manualInputTypeName, parseManualInputs, parseScheduleConfig, referenceDocuments } from "@powerhousedao/pieces-framework/workflow";
7
+ import { CALL_FLOW_ACTION as CALL_FLOW_ACTION$1, DEFAULT_TIMEZONE, DOCUMENT_REF_KEY, SUBFLOWS_PIECE as SUBFLOWS_PIECE$1, ScheduleConfigError, checkTriggerStrategy, coerceManualPayload, containsDocumentRef, documentRefsIn, expandDocumentRefs, isCallableFlow, isDocumentRefMarker, manualInputTypeName, parseManualInputs, parseScheduleConfig, referenceDocuments } from "@powerhousedao/pieces-framework/workflow";
8
8
  import { Cron } from "croner";
9
9
  import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes, randomUUID } from "node:crypto";
10
10
  import dns from "node:dns";
@@ -18,6 +18,7 @@ import http, { validateHeaderName, validateHeaderValue } from "node:http";
18
18
  import https from "node:https";
19
19
  import { actions as actions$1, reducer, utils } from "@powerhousedao/workflow/document-models/workflow";
20
20
  import { Kind, parse } from "graphql";
21
+ import { resolvePhDir } from "@powerhousedao/shared/clis/ph-dir";
21
22
  import { ReactorHostServer } from "@powerhousedao/reactor/rpc";
22
23
  import { SpanStatusCode, context } from "@opentelemetry/api";
23
24
  import { isFolderNode } from "@powerhousedao/shared/document-drive";
@@ -829,8 +830,7 @@ const SERVER_ONLY_PIECES = /* @__PURE__ */ new Set([
829
830
  "@activepieces/piece-utility-ai",
830
831
  "@activepieces/piece-agent",
831
832
  "@activepieces/piece-todos",
832
- "@activepieces/piece-tables",
833
- "@activepieces/piece-subflows"
833
+ "@activepieces/piece-tables"
834
834
  ]);
835
835
  //#endregion
836
836
  //#region src/reactor/piece-catalog.ts
@@ -1534,9 +1534,23 @@ var BlockResolver = class {
1534
1534
  //#endregion
1535
1535
  //#region src/reactor/run-scope.ts
1536
1536
  const storage = new AsyncLocalStorage();
1537
+ let scopesOpened = 0;
1538
+ let scopesCollected = 0;
1539
+ const collectedScopes = new FinalizationRegistry(() => {
1540
+ scopesCollected += 1;
1541
+ });
1537
1542
  function withRunScope(scope, fn) {
1543
+ scopesOpened += 1;
1544
+ collectedScopes.register(scope, void 0);
1538
1545
  return storage.run(scope, fn);
1539
1546
  }
1547
+ function runScopeStats() {
1548
+ return {
1549
+ opened: scopesOpened,
1550
+ collected: scopesCollected,
1551
+ held: scopesOpened - scopesCollected
1552
+ };
1553
+ }
1540
1554
  function currentWorkflowId() {
1541
1555
  return storage.getStore()?.workflowId;
1542
1556
  }
@@ -1564,6 +1578,9 @@ function currentRunUser() {
1564
1578
  function currentWaitpoints() {
1565
1579
  return storage.getStore()?.waitpoints;
1566
1580
  }
1581
+ function currentFlows() {
1582
+ return storage.getStore()?.flows;
1583
+ }
1567
1584
  //#endregion
1568
1585
  //#region src/reactor/piece-store-port.ts
1569
1586
  const logger$5 = childLogger(["workflow", "piece-store"]);
@@ -1706,12 +1723,12 @@ function configuredEgress() {
1706
1723
  pieceLogger.info(`Egress policy widened by ${EGRESS_ALLOW_ENV}: ${allowAddresses.join(", ")}`);
1707
1724
  return { allowAddresses };
1708
1725
  }
1709
- let bundleCache = join(process.cwd(), ".ph", "ap-bundles");
1726
+ let bundleCache = join(resolvePhDir(), "ap-bundles");
1710
1727
  function bundleCacheDir() {
1711
1728
  return bundleCache;
1712
1729
  }
1713
- const ATTACHMENT_STAGING_DIR = join(process.cwd(), ".ph", "ap-attachment-staging");
1714
- const ATTACHMENT_CACHE_DIR = join(process.cwd(), ".ph", "ap-attachment-cache");
1730
+ const ATTACHMENT_STAGING_DIR = join(resolvePhDir(), "ap-attachment-staging");
1731
+ const ATTACHMENT_CACHE_DIR = join(resolvePhDir(), "ap-attachment-cache");
1715
1732
  const DEFAULT_ATTACHMENT_CACHE_BYTES = 1073741824;
1716
1733
  function attachmentCacheBytes() {
1717
1734
  const raw = process.env.PH_WORKFLOWS_ATTACHMENT_CACHE_BYTES;
@@ -1760,6 +1777,7 @@ function createBlockExecutor(host, secrets, attachments, pieceStore, resolveBloc
1760
1777
  worker: currentPieceWorker,
1761
1778
  stepTest: currentStepTest,
1762
1779
  waitpoints: currentWaitpoints,
1780
+ flows: currentFlows,
1763
1781
  identity: () => ({
1764
1782
  runId: currentRunId(),
1765
1783
  flowId: currentWorkflowId(),
@@ -2708,6 +2726,8 @@ const MANUAL_BLOCK = trigger("manual");
2708
2726
  const SCHEDULE_BLOCK = trigger("schedule");
2709
2727
  const WEBHOOK_BLOCK = trigger("webhook");
2710
2728
  const BRANCH_BLOCK = action("branch");
2729
+ const ROUTER_BLOCK = action("router");
2730
+ const LOOP_BLOCK = action("loop");
2711
2731
  const ASSERT_BLOCK = action("assert");
2712
2732
  const DELAY_BLOCK = action("delay");
2713
2733
  const APPROVAL_BLOCK = action("approval");
@@ -2746,22 +2766,27 @@ var MasterKeyMismatchError = class extends Error {
2746
2766
  this.name = "MasterKeyMismatchError";
2747
2767
  }
2748
2768
  };
2749
- function loadKey(options) {
2750
- const hex = options.masterKeyHex ?? process.env[MASTER_KEY_ENV];
2751
- if (hex !== void 0) {
2752
- if (!/^[0-9a-f]{64}$/i.test(hex)) throw new Error("Secrets master key must be 64 hex chars (32 bytes)");
2753
- return Buffer.from(hex, "hex");
2754
- }
2755
- if (options.keyFile === false) throw new MasterKeyRequiredError();
2756
- const file = options.keyFile ?? join(process.cwd(), ".ph", "secrets.key");
2769
+ function readKeyFile(file) {
2757
2770
  try {
2758
2771
  const key = Buffer.from(readFileSync(file, "utf8").trim(), "hex");
2759
2772
  if (key.length !== KEY_BYTES) throw new Error(`Key file "${file}" is not 32 bytes of hex`);
2760
2773
  return key;
2761
2774
  } catch (error) {
2762
2775
  if (error.code !== "ENOENT") throw error;
2776
+ return;
2763
2777
  }
2764
- const key = randomBytes(KEY_BYTES);
2778
+ }
2779
+ function loadKey(options) {
2780
+ const hex = options.masterKeyHex ?? process.env[MASTER_KEY_ENV];
2781
+ if (hex !== void 0) {
2782
+ if (!/^[0-9a-f]{64}$/i.test(hex)) throw new Error("Secrets master key must be 64 hex chars (32 bytes)");
2783
+ return Buffer.from(hex, "hex");
2784
+ }
2785
+ if (options.keyFile === false) throw new MasterKeyRequiredError();
2786
+ const file = options.keyFile ?? join(resolvePhDir(), "secrets.key");
2787
+ const kept = readKeyFile(file);
2788
+ if (kept) return kept;
2789
+ const key = (options.keyFile === void 0 ? readKeyFile(join(resolvePhDir(process.cwd(), {}), "secrets.key")) : void 0) ?? randomBytes(KEY_BYTES);
2765
2790
  mkdirSync(dirname(file), { recursive: true });
2766
2791
  writeFileSync(file, key.toString("hex") + "\n", { mode: 384 });
2767
2792
  return key;
@@ -7729,6 +7754,7 @@ function triggerBlockColumns(block) {
7729
7754
  }
7730
7755
  const FIRED_WITHOUT_RUN_ID = "fired-without-run";
7731
7756
  const DEDUPE_CLAIM_MIGRATION = "trigger_dedupe.run_id-null-is-unclaimed";
7757
+ const STEP_PATH_MIGRATION = "step_execution.step_path";
7732
7758
  function abandonedAdmissionError(attempts) {
7733
7759
  return `This run was admitted at ${attempts} boots and the reactor stopped each time before it started, so it is no longer admitted on boot. Fix the cause, then rerun it to fire the workflow with the same payload.`;
7734
7760
  }
@@ -7833,7 +7859,9 @@ async function up(db) {
7833
7859
  for (const column of ["started_at", "ended_at"]) try {
7834
7860
  await db.schema.alterTable("step_execution").addColumn(column, "text").execute();
7835
7861
  } catch {}
7836
- try {
7862
+ await db.schema.createTable("migration_mark").addColumn("name", "text", (col) => col.primaryKey()).addColumn("applied_at", "text", (col) => col.notNull()).ifNotExists().execute();
7863
+ const stepPathMigrated = await db.selectFrom("migration_mark").select("name").where("name", "=", STEP_PATH_MIGRATION).executeTakeFirst();
7864
+ if (!stepPathMigrated) try {
7837
7865
  await db.schema.alterTable("step_execution").addUniqueConstraint("step_execution_run_step", ["run_id", "step_id"]).execute();
7838
7866
  } catch (error) {
7839
7867
  if (!isDuplicateObject(error)) throw new Error(`Could not add the step_execution (run_id, step_id) unique constraint, which per-step journaling upserts against: ${errorMessage(error)}`, { cause: error });
@@ -7850,7 +7878,7 @@ async function up(db) {
7850
7878
  try {
7851
7879
  await db.schema.alterTable("trigger_dedupe").addColumn("attempts", "integer", (col) => col.notNull().defaultTo(1)).execute();
7852
7880
  } catch {}
7853
- await db.schema.createTable("migration_mark").addColumn("name", "text", (col) => col.primaryKey()).addColumn("applied_at", "text", (col) => col.notNull()).ifNotExists().execute();
7881
+ if (!stepPathMigrated) await migrateStepPath(db);
7854
7882
  await db.transaction().execute(async (trx) => {
7855
7883
  if (await trx.selectFrom("migration_mark").select("name").where("name", "=", DEDUPE_CLAIM_MIGRATION).executeTakeFirst()) return;
7856
7884
  await trx.updateTable("trigger_dedupe").set({ run_id: FIRED_WITHOUT_RUN_ID }).where("run_id", "is", null).execute();
@@ -7914,7 +7942,9 @@ async function up(db) {
7914
7942
  ["definition_hash", "text"],
7915
7943
  ["definition_op_index", "integer"],
7916
7944
  ["definition_scope_hash", "text"],
7917
- ["waiting_on", "text"]
7945
+ ["waiting_on", "text"],
7946
+ ["parent_run_id", "text"],
7947
+ ["parent_waitpoint_id", "text"]
7918
7948
  ]) try {
7919
7949
  await db.schema.alterTable("run").addColumn(column, type).execute();
7920
7950
  } catch {}
@@ -7956,6 +7986,21 @@ function legacyBlockIdentity(blockType) {
7956
7986
  name
7957
7987
  };
7958
7988
  }
7989
+ async function migrateStepPath(db) {
7990
+ await db.schema.alterTable("step_execution").addColumn("step_path", "text", (col) => col.ifNotExists()).execute();
7991
+ await db.updateTable("step_execution").set({ step_path: sql.ref("step_id") }).where("step_path", "is", null).execute();
7992
+ await db.schema.alterTable("step_execution").alterColumn("step_path", (col) => col.setNotNull()).execute();
7993
+ try {
7994
+ await db.schema.alterTable("step_execution").addUniqueConstraint("step_execution_run_path", ["run_id", "step_path"]).execute();
7995
+ } catch (error) {
7996
+ if (!isDuplicateObject(error)) throw error;
7997
+ }
7998
+ await db.schema.alterTable("step_execution").dropConstraint("step_execution_run_step").ifExists().execute();
7999
+ await db.insertInto("migration_mark").values({
8000
+ name: STEP_PATH_MIGRATION,
8001
+ applied_at: (/* @__PURE__ */ new Date()).toISOString()
8002
+ }).onConflict((oc) => oc.column("name").doNothing()).execute();
8003
+ }
7959
8004
  async function migrateBlockType(db, table, nameColumn) {
7960
8005
  const legacy = db;
7961
8006
  try {
@@ -8180,6 +8225,7 @@ function stepValues(runId, ordinal, step) {
8180
8225
  run_id: runId,
8181
8226
  ordinal,
8182
8227
  step_id: step.stepId,
8228
+ step_path: step.path ?? step.stepId,
8183
8229
  step_key: step.key,
8184
8230
  piece_name: step.pieceName,
8185
8231
  block_name: step.blockName,
@@ -8271,6 +8317,7 @@ const STEP_COLUMNS_WITHOUT_DATA = [
8271
8317
  "run_id",
8272
8318
  "ordinal",
8273
8319
  "step_id",
8320
+ "step_path",
8274
8321
  "step_key",
8275
8322
  "piece_name",
8276
8323
  "block_name",
@@ -8292,6 +8339,7 @@ var WorkflowRunStore = class WorkflowRunStore {
8292
8339
  db;
8293
8340
  unmigrated;
8294
8341
  runsInFlight = /* @__PURE__ */ new Set();
8342
+ orphanedRunIds = [];
8295
8343
  journaledSteps = /* @__PURE__ */ new Map();
8296
8344
  pausingRuns = /* @__PURE__ */ new Set();
8297
8345
  resumedLegs = /* @__PURE__ */ new Set();
@@ -8325,10 +8373,11 @@ var WorkflowRunStore = class WorkflowRunStore {
8325
8373
  const ids = (await query.returning("id").execute()).map((row) => row.id);
8326
8374
  await failWaitingSteps(trx, ids, ORPHANED_RUN_ERROR, null, endedAt);
8327
8375
  await closeLiveWaitpoints(trx, ids, "CANCELLED", endedAt);
8328
- return ids.length;
8376
+ return ids;
8329
8377
  });
8330
- if (recovered > 0) logger$4.warn(`Recovered ${recovered} workflow run(s) left RUNNING by a stopped reactor; they are now FAILED and rerunnable`);
8331
- return recovered;
8378
+ this.orphanedRunIds = recovered;
8379
+ if (recovered.length > 0) logger$4.warn(`Recovered ${recovered.length} workflow run(s) left RUNNING by a stopped reactor; they are now FAILED and rerunnable`);
8380
+ return recovered.length;
8332
8381
  }
8333
8382
  async admitAbandonedRuns(options = {}) {
8334
8383
  const admitted = await this.db.transaction().execute(async (trx) => {
@@ -8610,9 +8659,21 @@ var WorkflowRunStore = class WorkflowRunStore {
8610
8659
  ended_at: null,
8611
8660
  rerun_of: null,
8612
8661
  warnings: 0,
8613
- warning_notes: null
8662
+ warning_notes: null,
8663
+ ...options.parent ? {
8664
+ parent_run_id: options.parent.runId,
8665
+ parent_waitpoint_id: options.parent.waitpointId
8666
+ } : {}
8614
8667
  }).execute();
8615
8668
  }
8669
+ async getRunParent(runId) {
8670
+ const row = await this.db.selectFrom("run").select(["parent_run_id", "parent_waitpoint_id"]).where("id", "=", runId).executeTakeFirst();
8671
+ if (!row?.parent_run_id) return void 0;
8672
+ return {
8673
+ runId: row.parent_run_id,
8674
+ waitpointId: row.parent_waitpoint_id ?? null
8675
+ };
8676
+ }
8616
8677
  async beginRun(runId, details) {
8617
8678
  this.runsInFlight.add(runId);
8618
8679
  this.journaledSteps.set(runId, /* @__PURE__ */ new Map());
@@ -8652,12 +8713,12 @@ var WorkflowRunStore = class WorkflowRunStore {
8652
8713
  async recordStep(runId, ordinal, step) {
8653
8714
  if (erasedRuns.has(runId)) return;
8654
8715
  const values = stepValues(runId, ordinal, step);
8655
- const { run_id: _run, step_id: _step, ...mutable } = values;
8716
+ const { run_id: _run, step_id: _step, step_path: _path, ...mutable } = values;
8656
8717
  if (!await this.withRunRow(runId, (trx) => trx.insertInto("step_execution").values({
8657
8718
  id: randomUUID(),
8658
8719
  ...values
8659
- }).onConflict((oc) => oc.columns(["run_id", "step_id"]).doUpdateSet(mutable)).execute())) return;
8660
- this.journaledSteps.get(runId)?.set(step.stepId, {
8720
+ }).onConflict((oc) => oc.columns(["run_id", "step_path"]).doUpdateSet(mutable)).execute())) return;
8721
+ this.journaledSteps.get(runId)?.set(values.step_path, {
8661
8722
  ordinal,
8662
8723
  hash: rowHash(values)
8663
8724
  });
@@ -8728,7 +8789,7 @@ var WorkflowRunStore = class WorkflowRunStore {
8728
8789
  return waiting || paused ? this.db.transaction().execute(close) : close(this.db);
8729
8790
  }
8730
8791
  async sweepSteps(runId, result, executionOrder) {
8731
- const journaled = await this.db.selectFrom("step_execution").select(["step_id", "ordinal"]).where("run_id", "=", runId).execute();
8792
+ const journaled = await this.db.selectFrom("step_execution").select(["step_id", "ordinal"]).where("run_id", "=", runId).whereRef("step_path", "=", "step_id").execute();
8732
8793
  const ordinals = new Map(journaled.map((row) => [row.step_id, row.ordinal]));
8733
8794
  let nextOrdinal = journaled.reduce((max, row) => Math.max(max, row.ordinal + 1), 0);
8734
8795
  for (const step of result.steps) {
@@ -8741,7 +8802,7 @@ var WorkflowRunStore = class WorkflowRunStore {
8741
8802
  await this.withRunRow(runId, (trx) => trx.insertInto("step_execution").values(result.steps.map((step) => ({
8742
8803
  id: randomUUID(),
8743
8804
  ...stepValues(runId, ordinalFor(step), step)
8744
- }))).onConflict((oc) => oc.columns(["run_id", "step_id"]).doUpdateSet((eb) => ({
8805
+ }))).onConflict((oc) => oc.columns(["run_id", "step_path"]).doUpdateSet((eb) => ({
8745
8806
  ordinal: eb.ref("excluded.ordinal"),
8746
8807
  step_key: eb.ref("excluded.step_key"),
8747
8808
  piece_name: eb.ref("excluded.piece_name"),
@@ -8782,13 +8843,22 @@ var WorkflowRunStore = class WorkflowRunStore {
8782
8843
  async getRun(id) {
8783
8844
  return this.db.selectFrom("run").selectAll().where("id", "=", id).executeTakeFirst();
8784
8845
  }
8785
- async getSteps(runId) {
8786
- return this.db.selectFrom("step_execution").selectAll().where("run_id", "=", runId).orderBy("ordinal", "asc").execute();
8846
+ async getSteps(runId, options = {}) {
8847
+ const base = this.db.selectFrom("step_execution").selectAll().where("run_id", "=", runId).orderBy("ordinal", "asc");
8848
+ const own = base.whereRef("step_path", "=", "step_id");
8849
+ if (!options.withIterations) return own.execute();
8850
+ if (options.iterationLimit === void 0) return base.execute();
8851
+ const [steps, iterations] = await Promise.all([own.execute(), base.whereRef("step_path", "!=", "step_id").limit(options.iterationLimit).execute()]);
8852
+ return [...steps, ...iterations].sort((a, b) => a.ordinal - b.ordinal);
8853
+ }
8854
+ async countIterations(runId) {
8855
+ const row = await this.db.selectFrom("step_execution").select((eb) => eb.fn.countAll().as("count")).where("run_id", "=", runId).whereRef("step_path", "!=", "step_id").executeTakeFirst();
8856
+ return Number(row?.count ?? 0);
8787
8857
  }
8788
8858
  async getStepsForRuns(runIds, options = {}) {
8789
8859
  const byRun = new Map(runIds.map((id) => [id, []]));
8790
8860
  if (runIds.length === 0) return byRun;
8791
- const base = this.db.selectFrom("step_execution").where("run_id", "in", [...new Set(runIds)]).orderBy("run_id").orderBy("ordinal", "asc");
8861
+ const base = this.db.selectFrom("step_execution").where("run_id", "in", [...new Set(runIds)]).whereRef("step_path", "=", "step_id").orderBy("run_id").orderBy("ordinal", "asc");
8792
8862
  const rows = options.withData === false ? (await base.select(STEP_COLUMNS_WITHOUT_DATA).execute()).map((row) => ({
8793
8863
  ...row,
8794
8864
  input: null,
@@ -9259,6 +9329,77 @@ var ParkState = class {
9259
9329
  }
9260
9330
  };
9261
9331
  //#endregion
9332
+ //#region src/reactor/subflows.ts
9333
+ const PARENT_RUN_HEADER = "ap-parent-run-id";
9334
+ const WAITPOINTS_PATH = "/v1/waitpoints/";
9335
+ const HOST_WAITPOINTS_URL = `${HOST_ORIGIN}/v1/waitpoints`;
9336
+ function hostRoute(request) {
9337
+ if (request.method.toUpperCase() !== "POST") return void 0;
9338
+ const { pathname } = new URL(request.path, HOST_ORIGIN);
9339
+ const [version, kind, id, ...rest] = pathname.split("/").filter(Boolean).map(decodeURIComponent);
9340
+ if (version !== "v1" || !id || rest.length > 0) return void 0;
9341
+ if (kind === "webhooks") return {
9342
+ kind: "webhook",
9343
+ workflowId: id
9344
+ };
9345
+ if (kind === "waitpoints") return {
9346
+ kind: "callback",
9347
+ token: id
9348
+ };
9349
+ }
9350
+ function hostBody(request) {
9351
+ if (request.body === void 0 || request.body === "") return void 0;
9352
+ if (!(request.headers["content-type"] ?? "").includes("json")) return request.body;
9353
+ try {
9354
+ return JSON.parse(request.body);
9355
+ } catch {
9356
+ return request.body;
9357
+ }
9358
+ }
9359
+ function jsonReply(status, body) {
9360
+ return {
9361
+ status,
9362
+ headers: { "content-type": "application/json" },
9363
+ body: JSON.stringify(body)
9364
+ };
9365
+ }
9366
+ function localCallback(callbackUrl, resumeBaseUrl) {
9367
+ const base = callbackUrl.startsWith(`http://reactor.invalid${WAITPOINTS_PATH}`) ? `${HOST_ORIGIN}${WAITPOINTS_PATH}` : resumeBaseUrl ? `${resumeBaseUrl.replace(/\/+$/, "")}/` : void 0;
9368
+ if (!base || !callbackUrl.startsWith(base)) return void 0;
9369
+ const token = callbackUrl.slice(base.length).split(/[/?#]/)[0];
9370
+ if (!token) return void 0;
9371
+ return {
9372
+ url: `${HOST_ORIGIN}${WAITPOINTS_PATH}${token}`,
9373
+ token
9374
+ };
9375
+ }
9376
+ function populatedFlow(workflowId, state, documentName) {
9377
+ const runnable = runnableDefinition(state);
9378
+ const trigger = runnable.trigger;
9379
+ const config = trigger?.config;
9380
+ return {
9381
+ id: workflowId,
9382
+ externalId: workflowId,
9383
+ status: state.status === "ENABLED" && runnable.published ? "ENABLED" : "DISABLED",
9384
+ version: {
9385
+ id: `${workflowId}@${runnable.version}`,
9386
+ displayName: state.name || documentName || workflowId,
9387
+ trigger: trigger ? {
9388
+ type: "PIECE_TRIGGER",
9389
+ settings: {
9390
+ pieceName: trigger.pieceName,
9391
+ pieceVersion: trigger.pieceVersion,
9392
+ triggerName: trigger.triggerName,
9393
+ input: config && typeof config === "object" && !Array.isArray(config) ? config : {}
9394
+ }
9395
+ } : {
9396
+ type: "EMPTY",
9397
+ settings: { input: {} }
9398
+ }
9399
+ }
9400
+ };
9401
+ }
9402
+ //#endregion
9262
9403
  //#region src/reactor/run-cursor.ts
9263
9404
  var InvalidRunCursorError = class extends Error {
9264
9405
  constructor() {
@@ -9877,6 +10018,8 @@ var TriggerSupervisor = class {
9877
10018
  if (!store) return false;
9878
10019
  const row = await store.getTriggerState(workflowId);
9879
10020
  if (!row || row.status !== "ENABLED") return false;
10021
+ const binding = this.bindings.get(workflowId);
10022
+ if (binding && !isSchedule(binding) && isCallableFlow(binding)) return false;
9880
10023
  await store.upsertTriggerState({
9881
10024
  ...row,
9882
10025
  next_poll_at: this.now().toISOString()
@@ -9985,12 +10128,12 @@ var TriggerSupervisor = class {
9985
10128
  handshake(binding, payload) {
9986
10129
  return this.enqueue(binding.workflowId, () => this.hook(binding, "onHandshake", { payload }));
9987
10130
  }
9988
- deliverWebhook(workflowId, payload) {
10131
+ deliverWebhook(workflowId, payload, parent) {
9989
10132
  return this.enqueue(workflowId, async () => {
9990
10133
  const binding = this.bindings.get(workflowId);
9991
10134
  if (!binding || isSchedule(binding)) {
9992
10135
  logger$2.warn(`Webhook delivery for unknown workflow ${workflowId}`);
9993
- return;
10136
+ return [];
9994
10137
  }
9995
10138
  const store = await this.store();
9996
10139
  if (!store) throw new MissingJournalError("Webhook delivery");
@@ -10009,13 +10152,23 @@ var TriggerSupervisor = class {
10009
10152
  storeState: VESTIGIAL_STORE_STATE,
10010
10153
  nextPollAtIso: new Date(now.getTime() + row.interval_ms).toISOString()
10011
10154
  } : void 0;
10012
- await this.fireItems(store, binding, result.output, now, rewind, success);
10155
+ return await this.fireItems(store, binding, result.output, now, rewind, success, parent);
10013
10156
  } catch (error) {
10014
10157
  if (!refused) await rewind();
10015
10158
  throw error;
10016
10159
  }
10017
10160
  });
10018
10161
  }
10162
+ pieceBinding(workflowId) {
10163
+ const binding = this.bindings.get(workflowId);
10164
+ return binding && !isSchedule(binding) ? binding : void 0;
10165
+ }
10166
+ onStart(binding, runId, payload) {
10167
+ return this.hook(binding, "onStart", {
10168
+ payload,
10169
+ runId
10170
+ });
10171
+ }
10019
10172
  async cursorRewind(store, workflowId) {
10020
10173
  const before = await store.listPieceStore("FLOW", workflowId);
10021
10174
  let rewound;
@@ -10044,6 +10197,11 @@ var TriggerSupervisor = class {
10044
10197
  const auth = await this.options.resolveAuth(binding.connectionId, { piecePackage: binding.packageName });
10045
10198
  const redactValues = secretsFor(auth);
10046
10199
  const reactor = await this.reactorFor(binding, hook, options.runUser);
10200
+ const flows = hook === "test" || hook === "onDisable" ? void 0 : this.options.flowsFor?.(binding.workflowId, options.runId);
10201
+ const hostCalls = pieceStore || flows ? {
10202
+ ...pieceStore ? storeHandlers(pieceStore) : {},
10203
+ ...flows ? flowsHandlers(flows) : {}
10204
+ } : void 0;
10047
10205
  this.throwIfStopped();
10048
10206
  return this.worker.runTriggerHook({
10049
10207
  ...pieceModuleRef(piece),
@@ -10055,15 +10213,17 @@ var TriggerSupervisor = class {
10055
10213
  ...pieceStore ? { durableStore: true } : {},
10056
10214
  identity: {
10057
10215
  flowId: binding.workflowId,
10058
- projectId: PROJECT_SCOPE_KEY
10216
+ projectId: PROJECT_SCOPE_KEY,
10217
+ ...options.runId ? { runId: options.runId } : {}
10059
10218
  },
10060
10219
  isRepublish: options.isRepublish,
10061
10220
  payload: options.payload,
10062
10221
  webhookUrl: options.webhookUrl ?? `http://localhost:0/v1/webhooks/${binding.workflowId}`,
10063
- ...this.egress ? { egress: this.egress } : {}
10222
+ ...this.egress ? { egress: this.egress } : {},
10223
+ ...flows ? { flows: flows.publicUrl ? { publicUrl: flows.publicUrl } : {} } : {}
10064
10224
  }, {
10065
10225
  timeoutMs: this.hookTimeoutMs,
10066
- ...pieceStore ? { hostCalls: storeHandlers(pieceStore) } : {},
10226
+ ...hostCalls ? { hostCalls } : {},
10067
10227
  ...reactor ? { reactor } : {}
10068
10228
  });
10069
10229
  }
@@ -10138,7 +10298,8 @@ var TriggerSupervisor = class {
10138
10298
  let reachedProvider = false;
10139
10299
  try {
10140
10300
  const runtime = await this.deliveryFor(binding);
10141
- const webhook = runtime.delivery === "webhook";
10301
+ const inProcess = isCallableFlow(binding);
10302
+ const webhook = runtime.delivery === "webhook" && !inProcess;
10142
10303
  const webhookUrl = webhook ? await this.webhookUrlOrThrow(binding.workflowId) : void 0;
10143
10304
  if (webhook && !webhookUrl) throw new Error("This trigger delivers by webhook, but no public webhook endpoint is configured for the reactor");
10144
10305
  reachedProvider = true;
@@ -10156,7 +10317,7 @@ var TriggerSupervisor = class {
10156
10317
  status: await this.armedStatus(store, binding.workflowId),
10157
10318
  store_state: VESTIGIAL_STORE_STATE,
10158
10319
  interval_ms: intervalMs,
10159
- next_poll_at: new Date(now.getTime() + intervalMs).toISOString(),
10320
+ next_poll_at: inProcess ? null : new Date(now.getTime() + intervalMs).toISOString(),
10160
10321
  last_poll_at: null,
10161
10322
  last_error: null,
10162
10323
  consecutive_failures: 0,
@@ -10502,10 +10663,10 @@ var TriggerSupervisor = class {
10502
10663
  logger$2.warn(`Poll failed for workflow ${row.workflow_id} (${failures}x): @error`, message);
10503
10664
  }
10504
10665
  }
10505
- async fireItems(store, binding, items, now, rewind, success) {
10666
+ async fireItems(store, binding, items, now, rewind, success, parent) {
10506
10667
  if (items.length === 0) {
10507
10668
  if (success) await store.recordPollSuccess(success.workflowId, success.storeState, now.toISOString(), success.nextPollAtIso);
10508
- return;
10669
+ return [];
10509
10670
  }
10510
10671
  const kind = pieceTriggerKind(binding.block);
10511
10672
  const journaled = (await store.journalTriggerItems(items.map((item) => ({
@@ -10513,7 +10674,8 @@ var TriggerSupervisor = class {
10513
10674
  options: {
10514
10675
  workflowId: binding.workflowId,
10515
10676
  triggerKind: kind,
10516
- triggerPayload: item
10677
+ triggerPayload: item,
10678
+ ...parent ? { parent } : {}
10517
10679
  }
10518
10680
  })), DEDUPE_TTL_MS, now.toISOString(), success)).flatMap((runId, index) => runId ? [{
10519
10681
  runId,
@@ -10525,6 +10687,7 @@ var TriggerSupervisor = class {
10525
10687
  throw error;
10526
10688
  }
10527
10689
  for (const { runId, item } of journaled) this.fire(binding.workflowId, item, kind, runId);
10690
+ return journaled.map(({ runId }) => runId);
10528
10691
  }
10529
10692
  async redeliver(runIds, rewind, error) {
10530
10693
  if (!await rewind(runIds)) await this.failRefused(runIds, error);
@@ -10962,6 +11125,8 @@ function bindingTarget(binding) {
10962
11125
  ...binding.source ? { source: binding.source } : {}
10963
11126
  };
10964
11127
  }
11128
+ /** A journal row with the steps that belong to it, as the subgraph serves it. */
11129
+ const RUN_ITERATIONS_LIMIT = 1e3;
10965
11130
  const RUNS_SCAN_LIMIT = 1e3;
10966
11131
  const WEBHOOK_TEST_TIMEOUT_MS = 3e5;
10967
11132
  const PENDING_SWEEP_INTERVAL_MS = 6e4;
@@ -11293,12 +11458,13 @@ var WorkflowRuntimeService = class {
11293
11458
  openStore() {
11294
11459
  this.storeOpening = true;
11295
11460
  const opening = WorkflowRunStore.create(this.host.relationalDb);
11296
- opening.then(() => {
11461
+ opening.then((store) => {
11297
11462
  this.storeOpening = false;
11298
11463
  if (this.storeError !== void 0) this.logger.info("Workflow run store opened after failing to");
11299
11464
  this.storeError = void 0;
11300
11465
  this.storeReopenMs = STORE_REOPEN_MS;
11301
11466
  this.retentionSweep?.();
11467
+ for (const runId of store.orphanedRunIds) this.endSubflow(runId);
11302
11468
  }, (error) => {
11303
11469
  this.storeOpening = false;
11304
11470
  this.storeError = error;
@@ -12119,6 +12285,7 @@ var WorkflowRuntimeService = class {
12119
12285
  },
12120
12286
  pollHeld: (workflowId) => this.pollHeld(workflowId),
12121
12287
  webhookUrlFor: async (workflowId) => (await this.mintWebhookEndpoint(workflowId))?.url,
12288
+ flowsFor: (workflowId, runId) => this.flowsPort(workflowId, runId ?? null),
12122
12289
  reactorAccess: (binding, requireReactor, runUser) => this.triggerReactorAccess(binding, requireReactor, runUser),
12123
12290
  models: this.models,
12124
12291
  worker: this.workers().session("trigger"),
@@ -12469,6 +12636,7 @@ var WorkflowRuntimeService = class {
12469
12636
  }
12470
12637
  }
12471
12638
  async deliverToPiece(binding, request) {
12639
+ if (isCallableFlow(binding)) return UNAUTHORIZED;
12472
12640
  const probe = await this.pieceHandshake(binding, request);
12473
12641
  if (probe) return probe;
12474
12642
  const payload = webhookPayload(request);
@@ -12603,14 +12771,17 @@ var WorkflowRuntimeService = class {
12603
12771
  }
12604
12772
  async driveWorkflowIds(driveId, ctx) {
12605
12773
  await this.assertCanReadDocument(driveId, ctx);
12774
+ const ids = await this.workflowNodeIds(driveId);
12775
+ return this.readableRows(ids, (id) => id, ctx);
12776
+ }
12777
+ async workflowNodeIds(driveId) {
12606
12778
  let page = await this.host.reactorClient.drives.listNodes(driveId);
12607
12779
  const nodes = [...page.results];
12608
12780
  while (page.next) {
12609
12781
  page = await page.next();
12610
12782
  nodes.push(...page.results);
12611
12783
  }
12612
- const ids = nodes.filter((node) => "documentType" in node && node.documentType === WORKFLOW_DOCUMENT_TYPE).map((node) => node.id);
12613
- return this.readableRows(ids, (id) => id, ctx);
12784
+ return nodes.filter((node) => "documentType" in node && node.documentType === WORKFLOW_DOCUMENT_TYPE).map((node) => node.id);
12614
12785
  }
12615
12786
  async runs(args, ctx) {
12616
12787
  return (await this.runsPage(args, ctx)).records;
@@ -12687,9 +12858,16 @@ var WorkflowRuntimeService = class {
12687
12858
  if (!row) return null;
12688
12859
  if (!await this.canReadDocument(row.workflow_id, ctx)) return null;
12689
12860
  if ((await this.servedRuns([row], ctx)).length === 0) return null;
12861
+ const steps = await store.getSteps(row.id, {
12862
+ withIterations: true,
12863
+ iterationLimit: RUN_ITERATIONS_LIMIT
12864
+ });
12865
+ const shown = steps.filter((step) => step.step_path !== step.step_id);
12866
+ const total = shown.length < 1e3 ? shown.length : await store.countIterations(row.id);
12690
12867
  return {
12691
12868
  row,
12692
- steps: await store.getSteps(row.id)
12869
+ steps,
12870
+ ...total > shown.length ? { iterationsTotal: total } : {}
12693
12871
  };
12694
12872
  }
12695
12873
  async connections(ctx) {
@@ -13114,9 +13292,10 @@ var WorkflowRuntimeService = class {
13114
13292
  if (!ctx) throw new Error("Connection access requires an authenticated request");
13115
13293
  await this.host.assertCanWrite(documentId, ctx);
13116
13294
  }
13117
- async blockOptions(block, propName, input, connectionId, ctx, searchValue, reactorConnectionId) {
13295
+ async blockOptions(block, propName, input, connectionId, ctx, searchValue, reactorConnectionId, editedWorkflowId) {
13118
13296
  if (connectionId) await this.assertCanReadDocument(connectionId, ctx);
13119
13297
  if (reactorConnectionId) await this.assertCanReadDocument(reactorConnectionId, ctx);
13298
+ const workflowId = editedWorkflowId && await this.assertCanReadDocument(editedWorkflowId, ctx).then(() => true, () => false) ? editedWorkflowId : void 0;
13120
13299
  const resolution = await this.resolveBlock(block);
13121
13300
  const parsed = resolvedBlock(resolution);
13122
13301
  if (!parsed) throw new Error(resolution.note ?? `${blockLabel(block)} does not resolve`);
@@ -13135,8 +13314,12 @@ var WorkflowRuntimeService = class {
13135
13314
  ...searchValue !== void 0 ? { searchValue } : {},
13136
13315
  auth,
13137
13316
  projectId: PROJECT_SCOPE_KEY,
13138
- ...this.designEgress ? { egress: this.designEgress } : {}
13139
- }, reactor ? { reactor } : {})).output;
13317
+ ...this.designEgress ? { egress: this.designEgress } : {},
13318
+ ...workflowId ? { flows: true } : {}
13319
+ }, {
13320
+ ...reactor ? { reactor } : {},
13321
+ ...workflowId ? { hostCalls: { [FLOWS_LIST]: (payload) => this.listFlows(workflowId, payload?.externalIds, ctx) } } : {}
13322
+ })).output;
13140
13323
  }
13141
13324
  async blockOutputTree(block, config) {
13142
13325
  const record = config ?? {};
@@ -13177,6 +13360,57 @@ var WorkflowRuntimeService = class {
13177
13360
  }
13178
13361
  ]
13179
13362
  };
13363
+ case ROUTER_BLOCK: return {
13364
+ source: "static",
13365
+ nodes: [{
13366
+ name: "branch",
13367
+ type: "String",
13368
+ description: "The matched branch's name; null for otherwise"
13369
+ }, {
13370
+ name: "index",
13371
+ type: "Int",
13372
+ description: "The matched branch, from 1; null for otherwise"
13373
+ }]
13374
+ };
13375
+ case LOOP_BLOCK: return {
13376
+ source: "static",
13377
+ nodes: [
13378
+ {
13379
+ name: "item",
13380
+ type: "value",
13381
+ description: "The current item; the last one after the loop"
13382
+ },
13383
+ {
13384
+ name: "index",
13385
+ type: "Int!",
13386
+ description: "From 1"
13387
+ },
13388
+ {
13389
+ name: "count",
13390
+ type: "Int",
13391
+ description: "Set after the loop"
13392
+ },
13393
+ {
13394
+ name: "iterations",
13395
+ type: "array",
13396
+ description: "Set after the loop: each item's step outputs",
13397
+ children: [
13398
+ {
13399
+ name: "index",
13400
+ type: "Int!"
13401
+ },
13402
+ {
13403
+ name: "item",
13404
+ type: "value"
13405
+ },
13406
+ {
13407
+ name: "steps",
13408
+ type: "JSONObject!"
13409
+ }
13410
+ ]
13411
+ }
13412
+ ]
13413
+ };
13180
13414
  case ASSERT_BLOCK: return {
13181
13415
  source: "static",
13182
13416
  nodes: [{
@@ -13496,9 +13730,11 @@ var WorkflowRuntimeService = class {
13496
13730
  }
13497
13731
  const unobservePool = this.telemetry.observeWorkerPool(() => pool.stats());
13498
13732
  const unregisterGauge = registerTelemetryGauge("workflow.pool", () => pool.memoryStats());
13733
+ const unregisterRuns = registerTelemetryGauge("workflow.runs", runScopeStats);
13499
13734
  this.unobservePool = () => {
13500
13735
  unobservePool();
13501
13736
  unregisterGauge();
13737
+ unregisterRuns();
13502
13738
  };
13503
13739
  return this.pieceWorkers = pool;
13504
13740
  }
@@ -13555,6 +13791,7 @@ var WorkflowRuntimeService = class {
13555
13791
  return await traced;
13556
13792
  } finally {
13557
13793
  this.settleFiring(workflowId);
13794
+ if (enqueuedRunId) this.endSubflow(enqueuedRunId);
13558
13795
  }
13559
13796
  }
13560
13797
  async runFired(run, workflowId, triggerPayload, triggerKind, resume, ctx, enqueuedRunId) {
@@ -13751,6 +13988,7 @@ var WorkflowRuntimeService = class {
13751
13988
  runId = journaled.runId;
13752
13989
  if (runId) run.span.setAttribute("run.id", runId);
13753
13990
  if (this.closed) throw new WorkflowRuntimeClosedError(workflowId);
13991
+ if (runId && !resume) await this.startPieceTrigger(workflowId, runId, triggerPayload);
13754
13992
  session = this.workers().session();
13755
13993
  const { result, handed } = await this.executeLeg({
13756
13994
  workflowId,
@@ -13766,6 +14004,7 @@ var WorkflowRuntimeService = class {
13766
14004
  triggerPayload,
13767
14005
  executor,
13768
14006
  completedSteps: resume?.completedSteps,
14007
+ loopItems: resume?.loopItems,
13769
14008
  replayed: resume?.completedSteps,
13770
14009
  pin: () => ({
13771
14010
  definition_hash: this.definitionHash(workflowId, state),
@@ -13822,7 +14061,8 @@ var WorkflowRuntimeService = class {
13822
14061
  for (const documentId of documentIds) handed.add(documentId);
13823
14062
  if (store && runId) await store.recordRunDocuments(runId, documentIds);
13824
14063
  },
13825
- ...waitpoints ? { waitpoints: waitpoints.port } : {}
14064
+ ...waitpoints ? { waitpoints: waitpoints.port } : {},
14065
+ flows: this.flowsPort(workflowId, runId)
13826
14066
  }, () => runWorkflow({
13827
14067
  definition: {
13828
14068
  ...leg.definition,
@@ -13834,6 +14074,7 @@ var WorkflowRuntimeService = class {
13834
14074
  ...secretValues.length > 0 ? { redactValues: secretValues } : {},
13835
14075
  triggerPayload: leg.triggerPayload,
13836
14076
  completedSteps: leg.completedSteps,
14077
+ ...leg.loopItems ? { loopItems: leg.loopItems } : {},
13837
14078
  ...leg.carriedSteps ? { carriedSteps: leg.carriedSteps } : {},
13838
14079
  ...leg.resume ? { resume: leg.resume } : {},
13839
14080
  ...leg.firstOrdinal !== void 0 ? { firstOrdinal: leg.firstOrdinal } : {},
@@ -13841,7 +14082,7 @@ var WorkflowRuntimeService = class {
13841
14082
  ...policy.defaultRetry ? { defaultRetry: policy.defaultRetry } : {},
13842
14083
  ...leg.deadline !== void 0 ? { deadline: leg.deadline } : {},
13843
14084
  onStep: store && runId ? async (record, ordinal) => {
13844
- executionOrder.set(record.stepId, ordinal);
14085
+ if (!record.path) executionOrder.set(record.stepId, ordinal);
13845
14086
  try {
13846
14087
  await this.telemetry.phase("journal.step", {}, () => store.recordStep(runId, ordinal, record));
13847
14088
  } catch (error) {
@@ -13854,7 +14095,7 @@ var WorkflowRuntimeService = class {
13854
14095
  if (store && runId) {
13855
14096
  let waiting = result.status === WAITING_RUN_STATUS;
13856
14097
  try {
13857
- const { held, closed } = await this.telemetry.phase("journal.finish", {}, () => store.finishRun(runId, result, executionOrder, waiting ? leg.pin?.() : void 0));
14098
+ const { held, closed } = await this.journalFinish(store, runId, result, executionOrder, waiting ? leg.pin?.() : void 0);
13858
14099
  this.answerClosed(closed, runId, {
13859
14100
  status: waiting ? CANCELLED_RUN_STATUS : result.status,
13860
14101
  error: waiting ? "The step that created this waitpoint no longer waits on it" : result.error ?? "The run ended without resuming on it"
@@ -13877,6 +14118,9 @@ var WorkflowRuntimeService = class {
13877
14118
  handed
13878
14119
  };
13879
14120
  }
14121
+ journalFinish(store, runId, result, executionOrder, pin) {
14122
+ return this.telemetry.phase("journal.finish", {}, () => store.finishRun(runId, result, executionOrder, pin));
14123
+ }
13880
14124
  async endUnheldWait(store, runId, result) {
13881
14125
  const on = result.waiting;
13882
14126
  const step = result.steps.find((record) => record.stepId === on?.stepId);
@@ -14084,31 +14328,62 @@ var WorkflowRuntimeService = class {
14084
14328
  const triggerPayload = run.trigger_payload === null ? void 0 : JSON.parse(run.trigger_payload);
14085
14329
  if (isTruncatedStepPayload(triggerPayload)) throw new Error(`Trigger payload of run "${runId}" was truncated by the journal and cannot be replayed; fire the workflow again instead of rerunning it`);
14086
14330
  if (containsRedactedMarker(triggerPayload)) throw new Error(`Trigger payload of run "${runId}" was redacted and cannot be replayed; fire the workflow again instead of rerunning it`);
14087
- const document = await this.host.reactorClient.get(run.workflow_id);
14088
- const currentSteps = new Map(runnableDefinition(document.state.global).steps.map((step) => [step.id, stepDefinition(step)]));
14331
+ const runnable = runnableDefinition((await this.host.reactorClient.get(run.workflow_id)).state.global);
14332
+ const currentSteps = new Map(runnable.steps.map((step) => [step.id, stepDefinition(step)]));
14333
+ const current = {
14334
+ steps: [...currentSteps.values()],
14335
+ edges: runnable.edges
14336
+ };
14337
+ const structure = loopStructure(current);
14338
+ const hashes = /* @__PURE__ */ new Map();
14339
+ const hashNow = (step) => {
14340
+ let hash = hashes.get(step.id);
14341
+ if (hash === void 0) {
14342
+ hash = isLoopStep(step) ? loopConfigHash(step, current, structure) : stepConfigHash(step);
14343
+ hashes.set(step.id, hash);
14344
+ }
14345
+ return hash;
14346
+ };
14347
+ const readers = /* @__PURE__ */ new Map();
14348
+ const rereads = (step) => {
14349
+ let reads = readers.get(step.id);
14350
+ if (!reads) readers.set(step.id, reads = this.rereads(step));
14351
+ return reads;
14352
+ };
14089
14353
  const completedSteps = /* @__PURE__ */ new Map();
14090
- for (const row of await store.getSteps(runId)) {
14354
+ const loopItemsByPath = /* @__PURE__ */ new Map();
14355
+ const rows = await store.getSteps(runId, { withIterations: true });
14356
+ for (const row of rows) {
14357
+ const step = currentSteps.get(row.step_id);
14358
+ if (!step || step.key !== row.step_key || row.config_hash === null || hashNow(step) !== row.config_hash) continue;
14359
+ if (isLoopStep(step)) {
14360
+ const input = row.input === null ? void 0 : JSON.parse(row.input);
14361
+ if (input === void 0 || isTruncatedStepPayload(input)) continue;
14362
+ try {
14363
+ loopItemsByPath.set(row.step_path, loopItems(input));
14364
+ } catch {}
14365
+ continue;
14366
+ }
14091
14367
  if (row.status !== "SUCCEEDED" && row.status !== "REPLAYED") continue;
14092
- const current = currentSteps.get(row.step_id);
14093
- if (!current || current.key !== row.step_key || row.config_hash === null || stepConfigHash(current) !== row.config_hash) continue;
14094
14368
  const output = row.output === null ? void 0 : JSON.parse(row.output);
14095
14369
  if (isTruncatedStepPayload(output)) {
14096
- if (await this.rereads(current)) continue;
14097
- completedSteps.set(row.step_id, {
14370
+ if (await rereads(step)) continue;
14371
+ completedSteps.set(row.step_path, {
14098
14372
  port: row.port,
14099
14373
  outputTruncated: true,
14100
14374
  truncatedOutput: output
14101
14375
  });
14102
14376
  continue;
14103
14377
  }
14104
- if (containsDocumentRef(output) && await this.rereads(current)) continue;
14105
- completedSteps.set(row.step_id, {
14378
+ if (containsDocumentRef(output) && await rereads(step)) continue;
14379
+ completedSteps.set(row.step_path, {
14106
14380
  output,
14107
14381
  port: row.port
14108
14382
  });
14109
14383
  }
14110
14384
  return this.fire(run.workflow_id, triggerPayload, "rerun", {
14111
14385
  completedSteps,
14386
+ loopItems: loopItemsByPath,
14112
14387
  rerunOf: runId
14113
14388
  }, ctx);
14114
14389
  }
@@ -14116,6 +14391,151 @@ var WorkflowRuntimeService = class {
14116
14391
  const parsed = resolvedBlock(await this.resolveBlock(stepBlock(step)));
14117
14392
  return parsed ? await this.declaredReactor(parsed) === "read" : false;
14118
14393
  }
14394
+ flowsPort(workflowId, runId) {
14395
+ const base = this.resumeBaseUrl();
14396
+ return {
14397
+ list: (params) => this.listFlows(workflowId, params.externalIds),
14398
+ request: (request) => this.hostRequest(workflowId, runId, request),
14399
+ ...base ? { publicUrl: new URL(base).origin } : {}
14400
+ };
14401
+ }
14402
+ resumeBaseUrl() {
14403
+ const configured = this.host.waitpointBaseUrl?.replace(/\/+$/, "");
14404
+ return configured && URL.canParse(configured) ? configured : void 0;
14405
+ }
14406
+ async drivesOf(workflowId) {
14407
+ try {
14408
+ return (await this.host.reactorClient.getIncomingRelationships(workflowId, CHILD_RELATIONSHIP)).results.filter((parent) => parent.header.documentType === DRIVE_DOCUMENT_TYPE).map((parent) => parent.header.id);
14409
+ } catch (error) {
14410
+ this.logger.debug("Could not read the drives of workflow @workflow: @error", workflowId, error);
14411
+ return [];
14412
+ }
14413
+ }
14414
+ async callableWorkflowIds(workflowId) {
14415
+ const ids = /* @__PURE__ */ new Set([workflowId]);
14416
+ for (const driveId of await this.drivesOf(workflowId)) for (const id of await this.workflowNodeIds(driveId)) ids.add(id);
14417
+ return [...ids];
14418
+ }
14419
+ async callableFlow(id, reads) {
14420
+ try {
14421
+ const document = await this.host.reactorClient.get(id);
14422
+ if (document.header.documentType !== WORKFLOW_DOCUMENT_TYPE) return;
14423
+ const trigger = runnableDefinition(document.state.global).trigger;
14424
+ if (!trigger || !isCallableFlow(trigger) || !await reads(id)) return;
14425
+ return populatedFlow(id, document.state.global, document.header.name);
14426
+ } catch {
14427
+ return;
14428
+ }
14429
+ }
14430
+ /** `ctx.flows.list`, as Activepieces' PopulatedFlow records: the Callable
14431
+ * Flow workflows the caller (else the calling workflow's run user) reads. */
14432
+ async listFlows(workflowId, externalIds, ctx) {
14433
+ const ids = (await this.callableWorkflowIds(workflowId)).filter((id) => !externalIds || externalIds.includes(id));
14434
+ const reads = await this.flowReader(workflowId, ctx);
14435
+ return { data: (await Promise.all(ids.map((id) => this.callableFlow(id, reads)))).filter((flow) => flow !== void 0) };
14436
+ }
14437
+ async flowReader(workflowId, ctx) {
14438
+ if (ctx) return (id) => this.canReadDocument(id, ctx);
14439
+ const runUser = currentRunUser() ?? await publishRunUser(this.host.reactorClient, workflowId, this.host.hostIdentity).catch(() => null);
14440
+ if (!runUser) {
14441
+ const open = !authEnforced(this.host);
14442
+ return () => Promise.resolve(open);
14443
+ }
14444
+ return (id) => this.host.reactorClient.isServed(id, { subject: runUser.subject }).catch(() => false);
14445
+ }
14446
+ async hostRequest(workflowId, runId, request) {
14447
+ const route = hostRoute(request);
14448
+ if (!route) return jsonReply(404, { message: `Nothing is served at ${request.method} ${request.path}` });
14449
+ const body = hostBody(request);
14450
+ if (route.kind === "callback") {
14451
+ const { status } = await this.resumeByToken(route.token, {
14452
+ body,
14453
+ headers: request.headers,
14454
+ queryParams: {}
14455
+ });
14456
+ return jsonReply(200, {
14457
+ status,
14458
+ discarded: status === "gone"
14459
+ });
14460
+ }
14461
+ return this.dispatchSubflow(workflowId, runId, route.workflowId, request.headers, body);
14462
+ }
14463
+ async flowCallableFrom(callerId, targetId) {
14464
+ if (targetId !== callerId) {
14465
+ const [callerDrives, targetDrives] = await Promise.all([this.drivesOf(callerId), this.drivesOf(targetId)]);
14466
+ if (!targetDrives.some((drive) => callerDrives.includes(drive))) return;
14467
+ }
14468
+ return this.callableFlow(targetId, await this.flowReader(callerId, void 0));
14469
+ }
14470
+ async dispatchSubflow(callerId, runId, targetId, headers, body) {
14471
+ const flow = await this.flowCallableFrom(callerId, targetId);
14472
+ if (!flow) return jsonReply(404, { message: `No workflow "${targetId}" is callable from workflow "${callerId}"` });
14473
+ const registration = this.registry.get(targetId);
14474
+ const binding = registration?.kind === "piece-webhook" ? registration.binding : void 0;
14475
+ if (!binding || !isCallableFlow(binding)) return jsonReply(409, { message: `Workflow "${flow.version.displayName}" is not enabled with a Callable Flow trigger` });
14476
+ const claimed = headers[PARENT_RUN_HEADER];
14477
+ if (runId && claimed && claimed !== runId) return jsonReply(403, { message: `Run "${runId}" cannot call on behalf of run "${claimed}"` });
14478
+ const request = body && typeof body === "object" ? body : { data: body };
14479
+ const callback = typeof request.callbackUrl === "string" ? localCallback(request.callbackUrl, this.resumeBaseUrl()) : void 0;
14480
+ let waitpointId = null;
14481
+ if (runId && callback && headers["ap-fail-parent-on-failure"] === "true") {
14482
+ const waitpoint = await (await this.store())?.getWaitpointByTokenHash(tokenHash(callback.token));
14483
+ if (waitpoint?.run_id === runId) waitpointId = waitpoint.id;
14484
+ }
14485
+ const payload = {
14486
+ method: "POST",
14487
+ path: `/v1/webhooks/${targetId}`,
14488
+ headers,
14489
+ queryParams: {},
14490
+ body: {
14491
+ data: request.data ?? null,
14492
+ ...request.callbackUrl !== void 0 ? { callbackUrl: callback?.url ?? request.callbackUrl } : {}
14493
+ }
14494
+ };
14495
+ try {
14496
+ return jsonReply(200, { runId: (await this.supervisor().deliverWebhook(targetId, payload, runId ? {
14497
+ runId,
14498
+ waitpointId
14499
+ } : void 0)).at(0) ?? null });
14500
+ } catch (error) {
14501
+ return jsonReply(500, { message: error instanceof Error ? error.message : String(error) });
14502
+ }
14503
+ }
14504
+ async startPieceTrigger(workflowId, runId, payload) {
14505
+ const registration = this.registry.get(workflowId);
14506
+ const binding = registration?.kind === "piece-webhook" ? registration.binding : registration?.kind === "piece" ? this.supervisor().pieceBinding(workflowId) : void 0;
14507
+ if (!binding) return;
14508
+ if (!(await this.pieceDescriptor(bindingTarget(binding))).triggers.find((entry) => entry.name === binding.triggerName)?.onStart) return;
14509
+ await this.supervisor().onStart(binding, runId, payload);
14510
+ }
14511
+ async endSubflow(runId) {
14512
+ try {
14513
+ const store = await this.store();
14514
+ if (!store) return;
14515
+ const parent = await store.getRunParent(runId);
14516
+ if (!parent) return;
14517
+ const run = await store.getRun(runId);
14518
+ if (!run) return;
14519
+ const failed = FAILED_SUBFLOW_STATUSES.has(run.status);
14520
+ if (!failed && run.status !== "SUCCEEDED") return;
14521
+ await store.deletePieceStoreValue("FLOW", run.workflow_id, `${CALLABLE_FLOW_KEY_PREFIX}${runId}`);
14522
+ if (!parent.waitpointId || !failed) return;
14523
+ const completed = await store.completeWaitpoint(parent.waitpointId, { payload: {
14524
+ body: {
14525
+ status: "error",
14526
+ data: {
14527
+ message: run.error ?? `The called workflow's run ended ${run.status}`,
14528
+ runId
14529
+ }
14530
+ },
14531
+ headers: {},
14532
+ queryParams: {}
14533
+ } });
14534
+ if (completed) await this.deliverHeld(completed.run_id);
14535
+ } catch (error) {
14536
+ this.logger.warn("Could not fail the caller of run @run: @error", runId, error);
14537
+ }
14538
+ }
14119
14539
  resuming = /* @__PURE__ */ new Set();
14120
14540
  definitionHashes = /* @__PURE__ */ new Map();
14121
14541
  /** Our hash of the runnable definition: computed once per published version. */
@@ -14133,13 +14553,13 @@ var WorkflowRuntimeService = class {
14133
14553
  }
14134
14554
  waitpointPort(store, workflowId, runId, suspendMs) {
14135
14555
  const responses = /* @__PURE__ */ new Map();
14136
- const tokens = /* @__PURE__ */ new Map();
14137
- const configured = this.host.waitpointBaseUrl?.replace(/\/+$/, "");
14138
- const baseUrl = configured && URL.canParse(configured) ? configured : void 0;
14556
+ const urls = /* @__PURE__ */ new Map();
14557
+ const resumeBaseUrl = this.resumeBaseUrl();
14139
14558
  return {
14140
14559
  port: {
14141
- ...baseUrl ? { resumeBaseUrl: baseUrl } : {},
14560
+ ...resumeBaseUrl ? { resumeBaseUrl } : {},
14142
14561
  create: async (request) => {
14562
+ const baseUrl = request.pieceName === SUBFLOWS_PIECE$1 && request.blockName === CALL_FLOW_ACTION$1 ? HOST_WAITPOINTS_URL : resumeBaseUrl;
14143
14563
  if (!baseUrl && request.type === "WEBHOOK" && !request.approval) throw new Error("This host serves no resume URLs: resume URLs need an HTTP host");
14144
14564
  const createdAt = /* @__PURE__ */ new Date();
14145
14565
  const resumeAt = request.resumeDateTime ? new Date(Date.parse(request.resumeDateTime)).toISOString() : null;
@@ -14173,20 +14593,21 @@ var WorkflowRuntimeService = class {
14173
14593
  response_to_send: request.responseToSend ? JSON.stringify(request.responseToSend) : null
14174
14594
  });
14175
14595
  if (!inserted) {
14176
- const known = tokens.get(waitpoint.id);
14596
+ const known = urls.get(waitpoint.id);
14177
14597
  if (waitpoint.token_hash !== null && known === void 0) throw new Error(`Step "${request.stepKey}" has a waitpoint from an earlier leg, whose resume URL cannot be issued again`);
14178
14598
  return {
14179
14599
  id: waitpoint.id,
14180
- resumeUrl: known ? `${baseUrl}/${known}` : ""
14600
+ resumeUrl: known ?? ""
14181
14601
  };
14182
14602
  }
14183
- if (token) tokens.set(id, token);
14603
+ const resumeUrl = token ? `${baseUrl}/${token}` : "";
14604
+ if (token) urls.set(id, resumeUrl);
14184
14605
  if (request.responseToSend) responses.set(id, request.responseToSend);
14185
14606
  const due = request.type === "DELAY" ? resumeAt : expiresAt;
14186
14607
  if (due) this.waitpointPoller.nudge(due);
14187
14608
  return {
14188
14609
  id,
14189
- resumeUrl: token ? `${baseUrl}/${token}` : ""
14610
+ resumeUrl
14190
14611
  };
14191
14612
  }
14192
14613
  },
@@ -14221,10 +14642,13 @@ var WorkflowRuntimeService = class {
14221
14642
  waitpointPoller = new WaitpointPoller({
14222
14643
  store: () => this.store(),
14223
14644
  resume: (runId) => this.deliverHeld(runId),
14224
- ended: (end) => Promise.resolve(this.answerClosed(end.closed, end.runId, {
14225
- status: CANCELLED_RUN_STATUS,
14226
- error: end.reason
14227
- })),
14645
+ ended: (end) => {
14646
+ this.answerClosed(end.closed, end.runId, {
14647
+ status: CANCELLED_RUN_STATUS,
14648
+ error: end.reason
14649
+ });
14650
+ return this.endSubflow(end.runId);
14651
+ },
14228
14652
  logger: { warn: (message, ...args) => this.logger.warn(message, ...args) }
14229
14653
  });
14230
14654
  unobserveSuspended;
@@ -14291,6 +14715,7 @@ var WorkflowRuntimeService = class {
14291
14715
  let status = "REFUSED";
14292
14716
  try {
14293
14717
  const result = await this.runResumed(runId, workflowId);
14718
+ this.endSubflow(runId);
14294
14719
  if (result) status = result.status;
14295
14720
  if (status === "FAILED") span.setStatus({
14296
14721
  code: SpanStatusCode.ERROR,
@@ -14767,6 +15192,7 @@ var WorkflowRuntimeService = class {
14767
15192
  const reason = by ? `Cancelled by ${by}` : "Cancelled";
14768
15193
  if (!await store.cancelIdleRun(runId, reason)) throw refuse((await store.getRun(runId))?.status ?? "gone");
14769
15194
  await this.answerHeldResumes(store, runId, reason);
15195
+ await this.endSubflow(runId);
14770
15196
  const row = await store.getRun(runId);
14771
15197
  this.queuedReplies.get(runId)?.({
14772
15198
  status: row.status,
@@ -15098,6 +15524,12 @@ var WorkflowRuntimeService = class {
15098
15524
  function scopeHash(state) {
15099
15525
  return hashDocumentStateForScope({ state: { global: state } }, "global");
15100
15526
  }
15527
+ const CALLABLE_FLOW_KEY_PREFIX = "callableFlow_";
15528
+ const FAILED_SUBFLOW_STATUSES = /* @__PURE__ */ new Set([
15529
+ "FAILED",
15530
+ "CANCELLED",
15531
+ "INDETERMINATE"
15532
+ ]);
15101
15533
  function tokenHash(token) {
15102
15534
  return createHash("sha256").update(token).digest("hex");
15103
15535
  }
@@ -15370,7 +15802,7 @@ async function acquireWorkflowSingletonLease(options) {
15370
15802
  instance,
15371
15803
  acquired_at: dbNow(),
15372
15804
  ...stamps()
15373
- }).where("id", "=", LEASE_ID).where((eb) => eb.or([eb("expires_at", "<=", dbNow()), eb.and([eb("owner", "=", owner), eb("heartbeat_at", "<=", dbNowPlus(-staleMs))])])).returning("owner").executeTakeFirst() !== void 0;
15805
+ }).where("id", "=", LEASE_ID).where((eb) => eb.or([eb("expires_at", "<=", dbNow()), eb.and([eb("owner", "=", owner), eb("heartbeat_at", "<=", dbNowPlus(options.exclusive ? 0 : -staleMs))])])).returning("owner").executeTakeFirst() !== void 0;
15374
15806
  };
15375
15807
  const claimSentAt = performance.now();
15376
15808
  if (!await claim()) {