@powerhousedao/reactor-workflow 6.2.3-dev.49 → 6.2.3-dev.51

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.
@@ -1,16 +1,59 @@
1
- import { L as jsonSafe, O as rewriteFileRefs, R as buildDescriptor, S as STORE_PUT, _ as MODEL_MANIFEST, a as redact, b as STORE_DELETE, c as rememberSecrets, h as LOG_WRITE, i as containsRedactedMarker, j as configuredMaxFileBytes, l as secretsFor, o as redactError, q as getActions, r as collectSecretValues, s as redactMessage, t as createIpcTransport, u as DEFAULT_EGRESS_POLICY, v as OUTPUT_UPDATE, x as STORE_GET } from "./reactor-rpc-BqAjCK1p.js";
2
- import { AppConnectionType, PieceCategory, Property, TriggerStrategy, createAction, createPiece, createTrigger } from "@powerhousedao/pieces-framework";
1
+ import { AppConnectionType, DEDUPE_KEY_PROPERTY, PieceCategory, Property, ReactorJobPendingError, TriggerStrategy, createAction, createPiece, createTrigger } from "@powerhousedao/pieces-framework";
3
2
  import { execFile, fork } from "node:child_process";
4
- import { existsSync } from "node:fs";
5
- import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
3
+ import { accessSync, constants, existsSync, lstatSync, mkdirSync } from "node:fs";
4
+ import { copyFile, lstat, mkdir, readFile, readdir, realpath, rename, rm, stat, utimes, writeFile } from "node:fs/promises";
6
5
  import path from "node:path";
7
6
  import { promisify } from "node:util";
8
7
  import { gunzip } from "node:zlib";
9
8
  import { childLogger } from "document-model";
10
9
  import { fileURLToPath } from "node:url";
11
- import { BRANCH_PORTS, CORE_PIECE_NAME, CORE_PIECE_NAME as CORE_PIECE_NAME$1, NEXT_PORT, PIECE_ACTION_PORTS, SCHEDULE_MODES, SCHEDULE_UNIT_MS, TRIGGER_PORTS, referenceDocuments, undeclaredPortEdges } from "@powerhousedao/pieces-framework/workflow";
10
+ import { BRANCH_PORTS, CORE_PIECE_NAME, CORE_PIECE_NAME as CORE_PIECE_NAME$1, NEXT_PORT, PIECE_ACTION_PORTS, SCHEDULE_MODES, SCHEDULE_UNIT_MS, TRIGGER_PORTS, checkTriggerStrategy, referenceDocuments, undeclaredPortEdges } from "@powerhousedao/pieces-framework/workflow";
11
+ import { Cron } from "croner";
12
12
  import { createHash, randomUUID } from "node:crypto";
13
+ import "node:stream";
14
+ import { ssrfIpClassifier } from "@powerhousedao/pieces-framework/host";
15
+ import dns from "node:dns";
16
+ import net, { isIP } from "node:net";
17
+ import { AsyncLocalStorage } from "node:async_hooks";
18
+ import { tmpdir, userInfo } from "node:os";
13
19
  import { blockKey, isExactVersion } from "@powerhousedao/pieces-framework/block-type";
20
+ //#region src/pieces/activepieces/types.ts
21
+ function getActions(piece) {
22
+ return (typeof piece.actions === "function" ? piece.actions() : piece.actions) ?? {};
23
+ }
24
+ function getTriggers(piece) {
25
+ return (typeof piece.triggers === "function" ? piece.triggers() : piece.triggers) ?? {};
26
+ }
27
+ //#endregion
28
+ //#region src/pieces/activepieces/indeterminate.ts
29
+ /** The property name, so both sides spell it once. */
30
+ const INDETERMINATE_FLAG = "indeterminate";
31
+ const INDETERMINATE_NAMES = /* @__PURE__ */ new Set([ReactorJobPendingError]);
32
+ function named(value) {
33
+ if (typeof value !== "object" || value === null) return false;
34
+ const name = value.name;
35
+ return typeof name === "string" && INDETERMINATE_NAMES.has(name);
36
+ }
37
+ function flagged(value) {
38
+ if (typeof value !== "object" || value === null) return false;
39
+ return value[INDETERMINATE_FLAG] === true;
40
+ }
41
+ /**
42
+ * Whether this error means "the write may have landed".
43
+ *
44
+ * Three places to look, in the order the error can reach us: the error itself
45
+ * (thrown in the reactor process), the `properties` record of the
46
+ * `SerializedPieceError` a `PieceWorkerError` carries (thrown in the child),
47
+ * and the `cause` chain (a piece that wrapped it).
48
+ */
49
+ function isIndeterminateError(error) {
50
+ if (flagged(error) || named(error)) return true;
51
+ const serialized = error?.serialized;
52
+ if (flagged(serialized?.properties) || named(serialized)) return true;
53
+ const cause = error?.cause;
54
+ return cause !== void 0 && cause !== error && (flagged(cause) || named(cause));
55
+ }
56
+ //#endregion
14
57
  //#region src/pieces/activepieces/registry-source.ts
15
58
  const logger$2 = childLogger(["workflow", "piece-registry"]);
16
59
  function source(baseUrl) {
@@ -32,6 +75,10 @@ function setPieceRegistryUrl(url) {
32
75
  configured = source(trimmed);
33
76
  logger$2.info(`Pieces may be fetched from ${configured.baseUrl}`);
34
77
  }
78
+ /** The registry at `baseUrl`, whichever one this host is configured with. */
79
+ function pieceRegistrySourceAt(baseUrl) {
80
+ return source(baseUrl.replace(/\/+$/, ""));
81
+ }
35
82
  function pieceRegistrySource() {
36
83
  return configured;
37
84
  }
@@ -113,12 +160,11 @@ async function readBoundedBody(response, url) {
113
160
  }
114
161
  return Buffer.concat(chunks);
115
162
  }
116
- function tarballSources(name, version, only) {
117
- const all = allTarballSources(name, version);
163
+ function tarballSources(name, version, only, registry = pieceRegistrySource()) {
164
+ const all = allTarballSources(name, version, registry);
118
165
  return only ? all.filter(({ source }) => only.includes(source)) : all;
119
166
  }
120
- function allTarballSources(name, version) {
121
- const registry = pieceRegistrySource();
167
+ function allTarballSources(name, version, registry) {
122
168
  return [
123
169
  ...registry ? [{
124
170
  source: "registry",
@@ -134,8 +180,8 @@ function allTarballSources(name, version) {
134
180
  }
135
181
  ];
136
182
  }
137
- async function downloadTarball(name, version, timeoutMs, only) {
138
- const sources = tarballSources(name, version, only);
183
+ async function downloadTarball(name, version, timeoutMs, only, registry) {
184
+ const sources = tarballSources(name, version, only, registry);
139
185
  let lastError;
140
186
  for (const { source, url } of sources) try {
141
187
  const response = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) });
@@ -243,7 +289,7 @@ async function installPieceBundle(options, fetched) {
243
289
  };
244
290
  try {
245
291
  await stageInstallWorkspace(workspace, options);
246
- await runInstall(workspace, timeoutMs);
292
+ await runInstall(workspace, timeoutMs, packageRegistry(options)?.baseUrl);
247
293
  await writeFile(path.join(workspace, INSTALL_READY_MARKER), "true");
248
294
  } catch (error) {
249
295
  await rm(workspace, {
@@ -259,8 +305,17 @@ async function installPieceBundle(options, fetched) {
259
305
  installed: true
260
306
  };
261
307
  }
308
+ function registryOf(entryUrl) {
309
+ const at = entryUrl.indexOf("/-/cdn/");
310
+ return at > 0 ? entryUrl.slice(0, at) : void 0;
311
+ }
312
+ function packageRegistry(options) {
313
+ if (!options.entryUrl) return void 0;
314
+ const base = registryOf(options.entryUrl);
315
+ return base ? pieceRegistrySourceAt(base) : pieceRegistrySource();
316
+ }
262
317
  async function stageInstallWorkspace(workspace, options) {
263
- const { tgz } = await downloadTarball(options.name, options.version, options.timeoutMs ?? 3e4, options.sources);
318
+ const { tgz } = await downloadTarball(options.name, options.version, options.timeoutMs ?? 3e4, options.sources, packageRegistry(options));
264
319
  await rm(workspace, {
265
320
  recursive: true,
266
321
  force: true
@@ -277,7 +332,7 @@ async function stageInstallWorkspace(workspace, options) {
277
332
  const MAX_LISTED_DEPENDENCIES = 5;
278
333
  const INSTALL_READY_MARKER = "ready";
279
334
  const INSTALL_TIMEOUT_MS = Number(process.env.PH_WORKFLOWS_PIECE_INSTALL_TIMEOUT_MS) || 12e4;
280
- function installCommand() {
335
+ function installCommand(registry) {
281
336
  const windows = process.platform === "win32";
282
337
  return {
283
338
  file: windows ? "npm.cmd" : "npm",
@@ -287,13 +342,14 @@ function installCommand() {
287
342
  "--no-audit",
288
343
  "--no-fund",
289
344
  "--omit=dev",
290
- "--loglevel=error"
345
+ "--loglevel=error",
346
+ ...registry ? [`--registry=${registry}/`] : []
291
347
  ],
292
348
  shell: windows
293
349
  };
294
350
  }
295
- async function runInstall(cwd, timeoutMs) {
296
- const { file, args, shell } = installCommand();
351
+ async function runInstall(cwd, timeoutMs, registry) {
352
+ const { file, args, shell } = installCommand(registry);
297
353
  await new Promise((resolve, reject) => {
298
354
  execFile(file, args, {
299
355
  cwd,
@@ -337,6 +393,295 @@ async function readDependencies(dir) {
337
393
  return JSON.parse(raw).dependencies ?? {};
338
394
  }
339
395
  //#endregion
396
+ //#region src/pieces/activepieces/unsupported.ts
397
+ const ISSUES_URL = "https://github.com/powerhouse-inc/powerhouse/issues/";
398
+ function unsupported(feature, issue) {
399
+ return {
400
+ feature,
401
+ issue,
402
+ reason: `${feature} is not supported yet (${ISSUES_URL}${issue})`
403
+ };
404
+ }
405
+ function isRecord$1(value) {
406
+ return value !== null && typeof value === "object" && !Array.isArray(value);
407
+ }
408
+ function unsupportedAuth(auth) {
409
+ if (Array.isArray(auth)) {
410
+ const reasons = auth.map(unsupportedMethod);
411
+ return reasons.some((reason) => reason === void 0) ? void 0 : reasons[0];
412
+ }
413
+ return unsupportedMethod(auth);
414
+ }
415
+ function authMethodFor(auth, type) {
416
+ if (!Array.isArray(auth)) return auth;
417
+ return auth.find((entry) => isRecord$1(entry) && entry.type === type);
418
+ }
419
+ function unsupportedMethod(auth) {
420
+ if (!isRecord$1(auth)) return void 0;
421
+ if (auth.type === "OAUTH2" && auth.grantType === "client_credentials") return unsupported("OAuth2 client credentials", 3091);
422
+ if (auth.type === "OIDC") return unsupported("OIDC auth", 3091);
423
+ if (auth.type === "CUSTOM_AUTH" && auth.refresh != null) return unsupported("CustomAuth refresh", 3091);
424
+ }
425
+ function unsupportedTrigger(trigger) {
426
+ if (trigger.type === "MANUAL") return unsupported("TriggerStrategy.MANUAL", 3091);
427
+ const strategy = checkTriggerStrategy(trigger.type);
428
+ if ("issue" in strategy) {
429
+ const issue = trigger.type === "APP_WEBHOOK" ? 3081 : 3091;
430
+ return {
431
+ feature: `TriggerStrategy ${String(trigger.type)}`,
432
+ issue,
433
+ reason: `${strategy.issue} (${ISSUES_URL}${issue})`
434
+ };
435
+ }
436
+ const problem = renewProblem(trigger.renewConfiguration);
437
+ if (problem) return {
438
+ feature: "renewConfiguration",
439
+ issue: 3090,
440
+ reason: `renewConfiguration ${problem} (${ISSUES_URL}3090)`
441
+ };
442
+ }
443
+ function cronFires(cron) {
444
+ try {
445
+ return new Cron(cron, {
446
+ timezone: "UTC",
447
+ legacyMode: false
448
+ }).nextRun() !== null;
449
+ } catch {
450
+ return false;
451
+ }
452
+ }
453
+ function renewProblem(renew) {
454
+ if (renew === void 0 || renew === null) return void 0;
455
+ if (!isRecord$1(renew)) return "is not an object";
456
+ if (renew.strategy === "NONE") return void 0;
457
+ if (renew.strategy !== "CRON") return `strategy ${String(renew.strategy)} is not supported`;
458
+ const cron = renew.cronExpression;
459
+ if (typeof cron !== "string" || !cronFires(cron)) return `cron ${JSON.stringify(cron)} is invalid`;
460
+ }
461
+ function triggerRenew(trigger) {
462
+ const renew = trigger.renewConfiguration;
463
+ if (!isRecord$1(renew) || renew.strategy !== "CRON") return void 0;
464
+ if (renewProblem(renew)) return void 0;
465
+ return {
466
+ strategy: "CRON",
467
+ cronExpression: renew.cronExpression
468
+ };
469
+ }
470
+ var UnsupportedPieceFeatureError = class extends Error {
471
+ feature;
472
+ issue;
473
+ constructor(subject, unsupportedFeature) {
474
+ super(`${subject}: ${unsupportedFeature.reason}`);
475
+ this.name = "UnsupportedPieceFeatureError";
476
+ this.feature = unsupportedFeature.feature;
477
+ this.issue = unsupportedFeature.issue;
478
+ }
479
+ };
480
+ //#endregion
481
+ //#region src/pieces/activepieces/descriptor.ts
482
+ function describeOAuth2(raw) {
483
+ if (typeof raw.authUrl !== "string" || typeof raw.tokenUrl !== "string") return;
484
+ const strings = (value) => Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
485
+ const extra = raw.extra && typeof raw.extra === "object" ? Object.fromEntries(Object.entries(raw.extra).filter((entry) => typeof entry[1] === "string")) : void 0;
486
+ return {
487
+ authUrl: raw.authUrl,
488
+ tokenUrl: raw.tokenUrl,
489
+ scope: strings(raw.scope),
490
+ ...typeof raw.prompt === "string" ? { prompt: raw.prompt } : {},
491
+ ...typeof raw.pkce === "boolean" ? { pkce: raw.pkce } : {},
492
+ ...raw.pkceMethod === "plain" || raw.pkceMethod === "S256" ? { pkceMethod: raw.pkceMethod } : {},
493
+ ...raw.authorizationMethod === "HEADER" || raw.authorizationMethod === "BODY" ? { authorizationMethod: raw.authorizationMethod } : {},
494
+ ...typeof raw.grantType === "string" ? { grantType: raw.grantType } : {},
495
+ ...extra && Object.keys(extra).length > 0 ? { extra } : {}
496
+ };
497
+ }
498
+ function describeHandshake(trigger) {
499
+ const strategy = trigger.handshakeConfiguration?.strategy;
500
+ if (!strategy || strategy === "NONE") return void 0;
501
+ return {
502
+ strategy,
503
+ paramName: trigger.handshakeConfiguration?.paramName
504
+ };
505
+ }
506
+ function optional(key, value) {
507
+ return value === void 0 ? {} : { [key]: value };
508
+ }
509
+ function describeOption(option) {
510
+ return {
511
+ label: option.label,
512
+ value: option.value,
513
+ ...typeof option.description === "string" && option.description ? { description: option.description } : {},
514
+ ...typeof option.icon === "string" && option.icon ? { icon: option.icon } : {}
515
+ };
516
+ }
517
+ function describeHints(prop) {
518
+ const hints = {};
519
+ if (prop.width === "half" || prop.width === "full") hints.width = prop.width;
520
+ for (const key of [
521
+ "icon",
522
+ "variant",
523
+ "display",
524
+ "formatProperty"
525
+ ]) {
526
+ const value = prop[key];
527
+ if (typeof value === "string" && value !== "") hints[key] = value;
528
+ }
529
+ for (const key of [
530
+ "min",
531
+ "max",
532
+ "step"
533
+ ]) {
534
+ const value = prop[key];
535
+ if (typeof value === "number" && Number.isFinite(value)) hints[key] = value;
536
+ }
537
+ if (Array.isArray(prop.reveals)) {
538
+ const reveals = prop.reveals.filter((name) => typeof name === "string");
539
+ if (reveals.length > 0) hints.reveals = reveals;
540
+ }
541
+ if (prop.refreshOnSearch === true) hints.refreshOnSearch = true;
542
+ const showWhen = prop.showWhen;
543
+ if (typeof showWhen?.prop === "string" && Array.isArray(showWhen.oneOf)) {
544
+ const oneOf = showWhen.oneOf;
545
+ hints.showWhen = {
546
+ prop: showWhen.prop,
547
+ oneOf: [...oneOf]
548
+ };
549
+ }
550
+ if (typeof prop.emptyChoice === "string" && prop.emptyChoice !== "") hints.emptyChoice = prop.emptyChoice;
551
+ return hints;
552
+ }
553
+ function describeGroups(groups) {
554
+ if (!Array.isArray(groups)) return void 0;
555
+ const described = groups.filter((group) => typeof group.key === "string" && typeof group.display === "string" && Array.isArray(group.props)).map((group) => ({
556
+ key: group.key,
557
+ display: group.display,
558
+ ...group.label ? { label: group.label } : {},
559
+ ...group.description ? { description: group.description } : {},
560
+ ...group.icon ? { icon: group.icon } : {},
561
+ props: group.props.filter((name) => typeof name === "string")
562
+ }));
563
+ return described.length > 0 ? described : void 0;
564
+ }
565
+ function hasResolver(prop) {
566
+ return typeof prop.options === "function" || typeof prop.props === "function";
567
+ }
568
+ function toPropDescriptor(name, prop, resolverId) {
569
+ const dynamic = hasResolver(prop);
570
+ const descriptor = {
571
+ name,
572
+ displayName: prop.displayName ?? name,
573
+ type: prop.type ?? "UNKNOWN",
574
+ required: prop.required ?? false,
575
+ hasDynamicResolver: dynamic
576
+ };
577
+ if (typeof prop.description === "string" && prop.description !== "") descriptor.description = prop.description;
578
+ if (typeof prop.placeholder === "string" && prop.placeholder !== "") descriptor.placeholder = prop.placeholder;
579
+ if (prop.defaultValue !== void 0) descriptor.defaultValue = prop.defaultValue;
580
+ if (prop.advanced === true) descriptor.advanced = true;
581
+ if (dynamic && resolverId) descriptor.dynamicResolverId = resolverId;
582
+ if (dynamic && Array.isArray(prop.refreshers)) descriptor.refreshers = prop.refreshers.filter((entry) => typeof entry === "string");
583
+ if (typeof prop.options === "object" && Array.isArray(prop.options.options)) {
584
+ descriptor.staticOptions = prop.options.options.map(describeOption);
585
+ if (prop.options.disabled === true) descriptor.staticDisabled = true;
586
+ if (typeof prop.options.placeholder === "string") descriptor.staticPlaceholder = prop.options.placeholder;
587
+ }
588
+ Object.assign(descriptor, describeHints(prop));
589
+ if (prop.properties && typeof prop.properties === "object") {
590
+ const nested = describeProperties(prop.properties);
591
+ if (nested.length > 0) descriptor.properties = nested;
592
+ }
593
+ return descriptor;
594
+ }
595
+ function describeProperties(props, resolverIdFor) {
596
+ if (!props || typeof props !== "object") return [];
597
+ return Object.entries(props).filter((entry) => isPropertyObject(entry[1])).map(([propName, prop]) => toPropDescriptor(propName, prop, resolverIdFor?.(propName)));
598
+ }
599
+ function isPropertyObject(value) {
600
+ return value !== null && typeof value === "object";
601
+ }
602
+ function withUnsupported(feature) {
603
+ return feature ? { unsupported: feature } : {};
604
+ }
605
+ function describeAuthMethod(auth) {
606
+ if (!auth || typeof auth !== "object") return void 0;
607
+ const method = auth;
608
+ const props = method.props && typeof method.props === "object" ? describeProperties(method.props) : [];
609
+ const oauth2 = method.type === "OAUTH2" ? describeOAuth2(auth) : void 0;
610
+ return {
611
+ type: method.type ?? "UNKNOWN",
612
+ displayName: method.displayName,
613
+ description: method.description,
614
+ required: method.required,
615
+ ...props.length > 0 ? { props } : {},
616
+ ...oauth2 ? { oauth2 } : {},
617
+ ...withUnsupported(unsupportedAuth(method))
618
+ };
619
+ }
620
+ function declaredPorts(ports) {
621
+ if (!Array.isArray(ports) || ports.length === 0) return void 0;
622
+ return ports.every((port) => typeof port === "string") ? ports : void 0;
623
+ }
624
+ function declaredReactor(value) {
625
+ return value === "read" || value === "write" ? value : void 0;
626
+ }
627
+ function buildDescriptor(piece, source, options = {}) {
628
+ const actions = Object.entries(getActions(piece)).map(([actionName, action]) => ({
629
+ name: action.name ?? actionName,
630
+ displayName: action.displayName ?? actionName,
631
+ description: action.description,
632
+ requireAuth: action.requireAuth ?? false,
633
+ ...optional("requireReactor", declaredReactor(action.requireReactor)),
634
+ ports: (options.routed ? declaredPorts(action.ports) : void 0) ?? PIECE_ACTION_PORTS,
635
+ props: describeProperties(action.props, (propName) => `activepieces:${source.packageName}#${actionName}.${propName}`),
636
+ ...action.outputSchema !== void 0 ? { outputSchema: action.outputSchema } : {},
637
+ ...action.audience !== void 0 ? { audience: action.audience } : {},
638
+ ...optional("propertyGroups", describeGroups(action.propertyGroups)),
639
+ ...optional("classification", typeof action.classification === "string" ? action.classification : void 0),
640
+ ...optional("errorHandlingOptions", action.errorHandlingOptions)
641
+ }));
642
+ const triggers = Object.entries(getTriggers(piece)).map(([triggerName, trigger]) => ({
643
+ name: trigger.name ?? triggerName,
644
+ displayName: trigger.displayName ?? triggerName,
645
+ description: trigger.description,
646
+ strategy: trigger.type ?? "UNKNOWN",
647
+ testStrategy: trigger.testStrategy,
648
+ ...typeof trigger.display === "string" && trigger.display !== "" ? { display: trigger.display } : {},
649
+ requireAuth: trigger.requireAuth ?? false,
650
+ ...optional("requireReactor", declaredReactor(trigger.requireReactor)),
651
+ ports: TRIGGER_PORTS,
652
+ props: describeProperties(trigger.props, (propName) => `activepieces:${source.packageName}#${triggerName}.${propName}`),
653
+ ...trigger.outputSchema !== void 0 ? { outputSchema: trigger.outputSchema } : {},
654
+ hasSampleData: trigger.sampleData !== void 0 && trigger.sampleData !== null,
655
+ ...trigger.sampleData !== void 0 && trigger.sampleData !== null ? { sampleData: trigger.sampleData } : {},
656
+ handshake: describeHandshake(trigger),
657
+ ...optional("renew", triggerRenew(trigger)),
658
+ ...optional("propertyGroups", describeGroups(trigger.propertyGroups)),
659
+ ...options.hostFed ? {} : withUnsupported(unsupportedTrigger(trigger))
660
+ }));
661
+ const descriptor = {
662
+ id: `activepieces:${source.packageName}`,
663
+ source,
664
+ displayName: piece.displayName,
665
+ description: piece.description,
666
+ logoUrl: piece.logoUrl,
667
+ categories: piece.categories,
668
+ ...piece.deprecated === true ? { deprecated: true } : {},
669
+ minimumSupportedRelease: piece.minimumSupportedRelease,
670
+ maximumSupportedRelease: piece.maximumSupportedRelease,
671
+ actions,
672
+ triggers,
673
+ ...withUnsupported(unsupportedAuth(piece.auth))
674
+ };
675
+ if (Array.isArray(piece.auth)) {
676
+ const methods = piece.auth.map(describeAuthMethod).filter((method) => method !== void 0);
677
+ if (methods.length > 0) descriptor.auth = methods;
678
+ } else {
679
+ const method = describeAuthMethod(piece.auth);
680
+ if (method) descriptor.auth = method;
681
+ }
682
+ return descriptor;
683
+ }
684
+ //#endregion
340
685
  //#region src/pieces/activepieces/resolver.ts
341
686
  const DOWNLOADS = {
342
687
  registry: ["registry"],
@@ -400,6 +745,330 @@ function pieceModuleRef(piece) {
400
745
  return piece.entryPath ? { entryPath: piece.entryPath } : { bundleDir: piece.bundleDir };
401
746
  }
402
747
  //#endregion
748
+ //#region src/pieces/activepieces/worker/json-safe.ts
749
+ function jsonSafe(value) {
750
+ try {
751
+ return JSON.parse(JSON.stringify(value));
752
+ } catch {
753
+ return String(value);
754
+ }
755
+ }
756
+ //#endregion
757
+ //#region src/pieces/activepieces/worker/protocol.ts
758
+ const REACTOR_RPC = "reactor-rpc";
759
+ const MODEL_MANIFEST = "model-manifest";
760
+ const LOG_WRITE = "log.write";
761
+ const OUTPUT_UPDATE = "output.update";
762
+ const STORE_GET = "store.get";
763
+ const STORE_PUT = "store.put";
764
+ const STORE_DELETE = "store.delete";
765
+ //#endregion
766
+ //#region src/pieces/activepieces/worker/host-call.ts
767
+ const DEFAULT_HOST_CALL_TIMEOUT_MS = 1e4;
768
+ function configuredMaxFileBytes() {
769
+ const raw = process.env.PH_WORKFLOWS_PIECE_MAX_FILE_BYTES;
770
+ if (raw === void 0) return void 0;
771
+ const parsed = Number(raw);
772
+ return Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : void 0;
773
+ }
774
+ function maxFileBytes() {
775
+ return configuredMaxFileBytes() ?? 8388608;
776
+ }
777
+ /** How long a piece's call of its host may take before the step gives up on
778
+ * it. Read per call, like the file ceiling, and travelling on the wire for the
779
+ * same reason: the child is forked with no environment. */
780
+ const HOST_CALL_TIMEOUT_ENV = "PH_WORKFLOWS_HOST_CALL_TIMEOUT_MS";
781
+ /**
782
+ * The host-call cap for one step.
783
+ *
784
+ * The 10s default outran reality for a reactor dispatch under load and the
785
+ * step was reported FAILED for a write that had been committed (backlog item
786
+ * 6). So it is configurable, and it is never shorter than the step's OWN
787
+ * timeout: a step the author gave two minutes should not have its host calls
788
+ * cut off after ten seconds. The child still clips each call to end before
789
+ * the step's kill deadline (`worker/host-call.ts`).
790
+ */
791
+ function hostCallTimeoutForStep(stepTimeoutMs, env = process.env) {
792
+ const configured = Number(env[HOST_CALL_TIMEOUT_ENV]);
793
+ return Math.max(Number.isFinite(configured) && configured > 0 ? Math.floor(configured) : DEFAULT_HOST_CALL_TIMEOUT_MS, stepTimeoutMs ?? 0);
794
+ }
795
+ var FileTooLargeError = class extends Error {
796
+ size;
797
+ limit;
798
+ constructor(size, limit = maxFileBytes()) {
799
+ super(`File of ${size} bytes exceeds the ${limit} byte limit (raise PH_WORKFLOWS_PIECE_MAX_FILE_BYTES to allow more)`);
800
+ this.name = "FileTooLargeError";
801
+ this.size = size;
802
+ this.limit = limit;
803
+ }
804
+ };
805
+ //#endregion
806
+ //#region src/pieces/activepieces/context/files.ts
807
+ function rewriteFileRefs(value, refs) {
808
+ if (refs.size === 0) return value;
809
+ if (typeof value === "string") return refs.get(value) ?? value;
810
+ if (Array.isArray(value)) return value.map((entry) => rewriteFileRefs(entry, refs));
811
+ if (typeof value === "object" && value !== null) {
812
+ const out = {};
813
+ for (const [key, entry] of Object.entries(value)) out[key] = rewriteFileRefs(entry, refs);
814
+ return out;
815
+ }
816
+ return value;
817
+ }
818
+ //#endregion
819
+ //#region src/pieces/activepieces/context/trigger.ts
820
+ function extractDedupeKey(payload) {
821
+ if (typeof payload !== "object" || payload === null) return void 0;
822
+ const value = payload[DEDUPE_KEY_PROPERTY];
823
+ return typeof value === "string" ? value : void 0;
824
+ }
825
+ //#endregion
826
+ //#region src/pieces/activepieces/worker/egress.ts
827
+ const DEFAULT_EGRESS_POLICY = {};
828
+ const IPV4_COMPATIBLE = new net.BlockList();
829
+ IPV4_COMPATIBLE.addSubnet("::", 96, "ipv6");
830
+ function familyOf(address) {
831
+ return isIP(address) === 6 ? "ipv6" : "ipv4";
832
+ }
833
+ function parseAddress(value) {
834
+ const bare = value.split("%")[0];
835
+ return isIP(bare) === 0 ? void 0 : bare;
836
+ }
837
+ originalOf(dns, "lookup");
838
+ function isPrivateAddress(value) {
839
+ const address = parseAddress(value);
840
+ if (!address) return true;
841
+ if (IPV4_COMPATIBLE.check(address, familyOf(address))) return true;
842
+ return ssrfIpClassifier.isBlockedIp({
843
+ ip: address,
844
+ allowList: []
845
+ });
846
+ }
847
+ new AsyncLocalStorage();
848
+ function originalOf(target, name) {
849
+ return Object.getOwnPropertyDescriptor(target, name)?.value;
850
+ }
851
+ const SECRET_MARKER = "[redacted:secret]";
852
+ const CIRCULAR_MARKER = "[circular]";
853
+ const TRUNCATED_MARKER = "[truncated]";
854
+ const ERROR_MAX_DEPTH = 8;
855
+ const ERROR_MAX_NODES = 1e3;
856
+ const STACK_GUARD_DEPTH = 200;
857
+ const SENSITIVE_NAMES = /* @__PURE__ */ new Set([
858
+ "auth",
859
+ "authorization",
860
+ "proxyauthorization",
861
+ "wwwauthenticate",
862
+ "authtoken",
863
+ "xauthtoken",
864
+ "apikey",
865
+ "xapikey",
866
+ "apisecret",
867
+ "bearer",
868
+ "cookie",
869
+ "setcookie",
870
+ "credential",
871
+ "credentials",
872
+ "password",
873
+ "passwd",
874
+ "passphrase",
875
+ "pwd",
876
+ "privatekey",
877
+ "secret",
878
+ "secrettext",
879
+ "sessionid",
880
+ "signature",
881
+ "token"
882
+ ]);
883
+ const SENSITIVE_SUFFIXES = [
884
+ "apikey",
885
+ "credentials",
886
+ "password",
887
+ "privatekey",
888
+ "secret",
889
+ "token"
890
+ ];
891
+ const TEXT_FIELD = new RegExp(String.raw`(\b(?:authorization|proxy-authorization|api[-_]?key|x-api-key|access[-_]?token|refresh[-_]?token|client[-_]?secret|set-cookie|cookie|password|secret|token)|signature(?<=\b((?:[a-z0-9]+[-_])*)signature)(?:[-_][a-z0-9]+)*)\b(["']?)(\s*[:=]\s*)(["']?)((?:(?:Bearer|Basic|Token)\s+)?[^\s",;&)}]+)\5`, "gi");
892
+ const AUTH_SCHEME = /\b(Bearer|Basic|Token)\s+([A-Za-z0-9._~+/=-]{8,})/g;
893
+ const QUERY_PARAM = /([?&])([A-Za-z0-9_.%[\]-]+)=([^&\s"'<>]+)/g;
894
+ const URL_USERINFO = /:\/\/(?<=[a-z][a-z0-9+.-]*:\/\/)([^/\s:@]+):([^/\s@]+)@/gi;
895
+ function normalizeName(name) {
896
+ return name.toLowerCase().replace(/[^a-z0-9]/g, "");
897
+ }
898
+ function isSensitiveName(name) {
899
+ const normalized = normalizeName(name);
900
+ if (!normalized) return false;
901
+ if (SENSITIVE_NAMES.has(normalized)) return true;
902
+ if (normalized.includes("signature")) return true;
903
+ return SENSITIVE_SUFFIXES.some((suffix) => normalized.length > suffix.length && normalized.endsWith(suffix));
904
+ }
905
+ function marker(name) {
906
+ return `[redacted:${name.toLowerCase()}]`;
907
+ }
908
+ function shannonEntropy(text) {
909
+ const counts = /* @__PURE__ */ new Map();
910
+ for (const char of text) counts.set(char, (counts.get(char) ?? 0) + 1);
911
+ let entropy = 0;
912
+ for (const count of counts.values()) {
913
+ const p = count / text.length;
914
+ entropy -= p * Math.log2(p);
915
+ }
916
+ return entropy;
917
+ }
918
+ const MIN_VALUE_LENGTH = 8;
919
+ const MIN_DISTINCT_CHARS = 5;
920
+ const MIN_ENTROPY_BITS = 2;
921
+ const MIN_DECLARED_LENGTH = 4;
922
+ function isRedactableValue(value) {
923
+ if (typeof value !== "string") return false;
924
+ if (value.length < MIN_VALUE_LENGTH) return false;
925
+ if (new Set(value).size < MIN_DISTINCT_CHARS) return false;
926
+ return shannonEntropy(value) >= MIN_ENTROPY_BITS;
927
+ }
928
+ function isDeclaredValue(value) {
929
+ return typeof value === "string" && value.length >= MIN_DECLARED_LENGTH;
930
+ }
931
+ const STRUCTURAL_NAMES = /* @__PURE__ */ new Set(["type", "authtype"]);
932
+ const URL_LIKE = /^[a-z][a-z0-9+.-]*:\/\//i;
933
+ function collectSecretValues(value, into = /* @__PURE__ */ new Set(), depth = 0) {
934
+ if (depth > ERROR_MAX_DEPTH) return into;
935
+ if (isRedactableValue(value)) {
936
+ if (!URL_LIKE.test(value)) into.add(value);
937
+ return into;
938
+ }
939
+ if (Array.isArray(value)) {
940
+ for (const entry of value) collectSecretValues(entry, into, depth + 1);
941
+ return into;
942
+ }
943
+ if (typeof value === "object" && value !== null) for (const [key, entry] of Object.entries(value)) {
944
+ if (STRUCTURAL_NAMES.has(normalizeName(key))) continue;
945
+ collectSecretValues(entry, into, depth + 1);
946
+ }
947
+ return into;
948
+ }
949
+ const thrownSecrets = /* @__PURE__ */ new WeakMap();
950
+ function rememberSecrets(error, values) {
951
+ if (typeof error === "object" && error !== null && values.length > 0) thrownSecrets.set(error, values);
952
+ return error;
953
+ }
954
+ function secretsFor(error) {
955
+ if (typeof error !== "object" || error === null) return [];
956
+ return thrownSecrets.get(error) ?? [];
957
+ }
958
+ function preparePass(options) {
959
+ return {
960
+ values: [...options?.values ?? []].filter(isDeclaredValue).sort((a, b) => b.length - a.length),
961
+ maxDepth: options?.maxDepth ?? STACK_GUARD_DEPTH,
962
+ maxNodes: options?.maxNodes ?? Number.POSITIVE_INFINITY,
963
+ nodes: 0
964
+ };
965
+ }
966
+ function safeDecode(name) {
967
+ try {
968
+ return decodeURIComponent(name);
969
+ } catch {
970
+ return name;
971
+ }
972
+ }
973
+ function replaceValues(text, values) {
974
+ let result = text;
975
+ for (const value of values) {
976
+ if (result.includes(value)) result = result.split(value).join(SECRET_MARKER);
977
+ const encoded = encodeURIComponent(value);
978
+ if (encoded !== value && result.includes(encoded)) result = result.split(encoded).join(SECRET_MARKER);
979
+ }
980
+ return result;
981
+ }
982
+ function redactText(text, pass) {
983
+ let result = replaceValues(text, pass.values);
984
+ result = result.replace(URL_USERINFO, (_match, user) => `://${user}:${marker("password")}@`);
985
+ result = result.replace(QUERY_PARAM, (match, sep, name) => isSensitiveName(safeDecode(name)) ? `${sep}${name}=${marker(name)}` : match);
986
+ result = result.replace(AUTH_SCHEME, (_match, scheme) => `${scheme} ${marker(scheme)}`);
987
+ return result.replace(TEXT_FIELD, (match, name, signaturePrefix, nameQuote, sep, quote, value) => value.startsWith("[redacted:") ? match : `${name}${nameQuote}${sep}${quote}${marker(`${signaturePrefix ?? ""}${name}`)}${quote}`);
988
+ }
989
+ function isScopeRevision(parent, key, entry) {
990
+ return key === "revision" && typeof parent.documentType === "string" && typeof entry === "object" && entry !== null && !Array.isArray(entry) && Object.values(entry).every((count) => typeof count === "number" && Number.isFinite(count));
991
+ }
992
+ function walk(value, pass, depth, seen) {
993
+ if (typeof value === "string") return redactText(value, pass);
994
+ if (typeof value !== "object" || value === null) return value;
995
+ if (seen.has(value)) return CIRCULAR_MARKER;
996
+ if (depth >= pass.maxDepth) return TRUNCATED_MARKER;
997
+ if (pass.nodes >= pass.maxNodes) return TRUNCATED_MARKER;
998
+ pass.nodes += 1;
999
+ seen.add(value);
1000
+ try {
1001
+ if (Array.isArray(value)) return value.map((entry) => walk(entry, pass, depth + 1, seen));
1002
+ const result = {};
1003
+ const record = value;
1004
+ for (const [key, entry] of Object.entries(record)) result[key] = isSensitiveName(key) ? marker(key) : isScopeRevision(record, key, entry) ? { ...entry } : walk(entry, pass, depth + 1, seen);
1005
+ return result;
1006
+ } finally {
1007
+ seen.delete(value);
1008
+ }
1009
+ }
1010
+ function redact(value, options) {
1011
+ return walk(value, preparePass(options), 0, /* @__PURE__ */ new Set());
1012
+ }
1013
+ function redactError(value, options) {
1014
+ return redact(value, {
1015
+ maxDepth: ERROR_MAX_DEPTH,
1016
+ maxNodes: ERROR_MAX_NODES,
1017
+ ...options
1018
+ });
1019
+ }
1020
+ function containsRedactedMarker(value, depth = 0) {
1021
+ if (typeof value === "string") return value.includes("[redacted:") || value === "[truncated]";
1022
+ if (typeof value !== "object" || value === null) return false;
1023
+ if (depth >= STACK_GUARD_DEPTH) return false;
1024
+ return Object.values(value).some((entry) => containsRedactedMarker(entry, depth + 1));
1025
+ }
1026
+ function redactMessage(text, options) {
1027
+ return redactText(text, preparePass(options));
1028
+ }
1029
+ //#endregion
1030
+ //#region src/pieces/activepieces/worker/reactor-rpc.ts
1031
+ function isReactorRpcEnvelope(value, requestId) {
1032
+ if (typeof value !== "object" || value === null) return false;
1033
+ const envelope = value;
1034
+ return envelope.type === "reactor-rpc" && envelope.requestId === requestId;
1035
+ }
1036
+ function isRpcMessage(value) {
1037
+ return typeof value === "object" && value !== null && typeof value.k === "string";
1038
+ }
1039
+ function createIpcTransport(worker, requestId) {
1040
+ let closed = false;
1041
+ const detachers = /* @__PURE__ */ new Set();
1042
+ return {
1043
+ post(message) {
1044
+ if (closed || !worker.connected) return;
1045
+ worker.send({
1046
+ type: REACTOR_RPC,
1047
+ requestId,
1048
+ message
1049
+ });
1050
+ },
1051
+ onMessage(listener) {
1052
+ const handler = (value) => {
1053
+ if (closed || !isReactorRpcEnvelope(value, requestId)) return;
1054
+ if (!isRpcMessage(value.message)) return;
1055
+ listener(value.message);
1056
+ };
1057
+ worker.on("message", handler);
1058
+ const detach = () => {
1059
+ worker.off("message", handler);
1060
+ detachers.delete(detach);
1061
+ };
1062
+ detachers.add(detach);
1063
+ return detach;
1064
+ },
1065
+ close() {
1066
+ closed = true;
1067
+ for (const detach of [...detachers]) detach();
1068
+ }
1069
+ };
1070
+ }
1071
+ //#endregion
403
1072
  //#region src/pieces/activepieces/worker/transport.ts
404
1073
  function defaultEntryPath() {
405
1074
  let dir = path.dirname(fileURLToPath(import.meta.url));
@@ -412,10 +1081,50 @@ function defaultEntryPath() {
412
1081
  if (!existsSync(entry)) throw new Error(`Worker entry not built at ${entry} — run pnpm build`);
413
1082
  return entry;
414
1083
  }
1084
+ const PROFILE_EXIT_MS = 2e3;
1085
+ function workerProfileDir() {
1086
+ return process.env.PH_WORKFLOWS_WORKER_CPU_PROF_DIR || void 0;
1087
+ }
1088
+ let compileCache;
1089
+ function privateDirectory(dir) {
1090
+ const stat = lstatSync(dir);
1091
+ if (!stat.isDirectory()) return false;
1092
+ if (typeof process.getuid !== "function") return true;
1093
+ return stat.uid === process.getuid() && (stat.mode & 63) === 0;
1094
+ }
1095
+ function writable(file) {
1096
+ try {
1097
+ accessSync(file, constants.W_OK);
1098
+ return true;
1099
+ } catch {
1100
+ return false;
1101
+ }
1102
+ }
1103
+ function workerCompileCache(entryPath) {
1104
+ if (compileCache === void 0) {
1105
+ const configured = process.env.PH_WORKFLOWS_WORKER_COMPILE_CACHE;
1106
+ try {
1107
+ if (configured === "off") throw new Error("disabled");
1108
+ if (!configured && !writable(entryPath)) throw new Error("read-only");
1109
+ const base = configured ? void 0 : path.join(tmpdir(), `ph-workflows-${userInfo().uid}`);
1110
+ const dir = configured || path.join(base, "compile-cache");
1111
+ mkdirSync(dir, {
1112
+ recursive: true,
1113
+ mode: 448
1114
+ });
1115
+ compileCache = privateDirectory(dir) && (!base || privateDirectory(base)) ? dir : null;
1116
+ } catch {
1117
+ compileCache = null;
1118
+ }
1119
+ }
1120
+ return compileCache ?? void 0;
1121
+ }
415
1122
  function createForkTransport(entryPath) {
1123
+ const profileDir = workerProfileDir();
1124
+ const cacheDir = workerCompileCache(entryPath);
416
1125
  const child = fork(entryPath, [], {
417
- env: {},
418
- execArgv: [],
1126
+ env: cacheDir ? { NODE_COMPILE_CACHE: cacheDir } : {},
1127
+ execArgv: profileDir ? ["--cpu-prof", `--cpu-prof-dir=${profileDir}`] : [],
419
1128
  serialization: "advanced",
420
1129
  stdio: [
421
1130
  "ignore",
@@ -456,11 +1165,25 @@ function createForkTransport(entryPath) {
456
1165
  child.off("message", listener);
457
1166
  },
458
1167
  kill() {
459
- child.kill("SIGKILL");
1168
+ if (!profileDir) {
1169
+ child.kill("SIGKILL");
1170
+ return;
1171
+ }
1172
+ child.kill("SIGTERM");
1173
+ const force = setTimeout(() => child.kill("SIGKILL"), PROFILE_EXIT_MS);
1174
+ force.unref();
1175
+ child.once("exit", () => clearTimeout(force));
460
1176
  }
461
1177
  };
462
1178
  }
463
1179
  //#endregion
1180
+ //#region src/pieces/activepieces/worker/timings.ts
1181
+ function epochNow() {
1182
+ return performance.timeOrigin + performance.now();
1183
+ }
1184
+ new AsyncLocalStorage();
1185
+ new AsyncLocalStorage();
1186
+ //#endregion
464
1187
  //#region src/pieces/activepieces/worker/host.ts
465
1188
  var PieceWorkerError = class extends Error {
466
1189
  serialized;
@@ -482,6 +1205,12 @@ var PieceWorkerExitError = class extends Error {
482
1205
  this.name = "PieceWorkerExitError";
483
1206
  }
484
1207
  };
1208
+ var PieceWorkerDisposedError = class extends Error {
1209
+ constructor() {
1210
+ super("This piece worker is disposed");
1211
+ this.name = "PieceWorkerDisposedError";
1212
+ }
1213
+ };
485
1214
  function serveNotify(message, handlers) {
486
1215
  const handler = handlers?.[message.method];
487
1216
  if (!handler) return;
@@ -494,15 +1223,19 @@ var PieceWorker = class {
494
1223
  defaultTimeoutMs;
495
1224
  hostCallTimeoutMs;
496
1225
  models;
1226
+ telemetry;
497
1227
  worker;
1228
+ forkedAt = 0;
498
1229
  queue = Promise.resolve();
499
1230
  nextId = 1;
1231
+ disposed = false;
500
1232
  constructor(options = {}) {
501
1233
  const entryPath = options.entryPath;
502
1234
  this.connect = options.transport ?? (() => createForkTransport(entryPath ?? defaultEntryPath()));
503
1235
  this.defaultTimeoutMs = options.defaultTimeoutMs ?? 3e4;
504
1236
  this.hostCallTimeoutMs = options.hostCallTimeoutMs;
505
1237
  this.models = options.models;
1238
+ this.telemetry = options.telemetry;
506
1239
  }
507
1240
  runAction(request, options = {}) {
508
1241
  return this.enqueue("run", request, options.timeoutMs, options);
@@ -520,16 +1253,24 @@ var PieceWorker = class {
520
1253
  return this.enqueue("trigger-hook", request, options.timeoutMs, options);
521
1254
  }
522
1255
  enqueue(type, request, timeoutMs, taps = {}) {
523
- const run = this.queue.then(() => this.execute(type, request, timeoutMs ?? this.defaultTimeoutMs, taps));
1256
+ const run = this.queue.then(() => {
1257
+ const execute = (onTimings) => this.execute(type, request, timeoutMs ?? this.defaultTimeoutMs, taps, onTimings);
1258
+ const telemetry = this.telemetry;
1259
+ if (!telemetry) return execute();
1260
+ const cold = !this.worker;
1261
+ return telemetry.phase(`worker.${type}${cold ? ".cold" : ""}`, { "worker.cold": cold }, (span) => execute((timings, ipc) => telemetry.workerTimings(span, timings, this.forkedAt, ipc)));
1262
+ });
524
1263
  this.queue = run.catch(() => void 0);
525
1264
  return run;
526
1265
  }
527
1266
  dispose() {
1267
+ this.disposed = true;
528
1268
  this.worker?.kill();
529
1269
  this.worker = void 0;
530
1270
  }
531
1271
  spawn() {
532
1272
  if (this.worker) return this.worker;
1273
+ this.forkedAt = epochNow();
533
1274
  const worker = this.connect();
534
1275
  this.sendModels(worker);
535
1276
  const forget = () => {
@@ -577,9 +1318,12 @@ var PieceWorker = class {
577
1318
  }
578
1319
  };
579
1320
  }
580
- execute(type, request, timeoutMs, taps) {
1321
+ execute(type, request, timeoutMs, taps, onTimings) {
1322
+ if (this.disposed) return Promise.reject(new PieceWorkerDisposedError());
581
1323
  const worker = this.spawn();
582
1324
  const id = this.nextId++;
1325
+ const hostCallTimeoutMs = this.hostCallTimeoutMs ?? taps.hostCallTimeoutMs ?? hostCallTimeoutForStep(timeoutMs);
1326
+ let sentAt = 0;
583
1327
  return new Promise((resolve, reject) => {
584
1328
  const deadline = Date.now() + timeoutMs;
585
1329
  let reactor;
@@ -600,6 +1344,7 @@ var PieceWorker = class {
600
1344
  if (response.type === "host-call" || response.type === "host-notify" || response.type === "reactor-rpc") return;
601
1345
  if (response.id !== id) return;
602
1346
  cleanup();
1347
+ const received = epochNow();
603
1348
  if (response.type === "result") resolve({
604
1349
  output: response.output,
605
1350
  touched: response.touched,
@@ -610,6 +1355,12 @@ var PieceWorker = class {
610
1355
  listeners: response.listeners
611
1356
  });
612
1357
  else reject(new PieceWorkerError(response.error));
1358
+ if (response.timings && onTimings) try {
1359
+ onTimings(response.timings, {
1360
+ sent: sentAt,
1361
+ received
1362
+ });
1363
+ } catch {}
613
1364
  };
614
1365
  const onExit = ({ code, signal }) => {
615
1366
  cleanup();
@@ -634,13 +1385,14 @@ var PieceWorker = class {
634
1385
  worker.on("message", onMessage);
635
1386
  worker.on("message", onHostCall);
636
1387
  worker.on("exit", onExit);
1388
+ sentAt = epochNow();
637
1389
  worker.send(jsonSafe({
638
1390
  id,
639
1391
  type,
640
1392
  request: {
641
1393
  maxFileBytes: configuredMaxFileBytes(),
642
1394
  deadline,
643
- ...this.hostCallTimeoutMs ? { hostCallTimeoutMs: this.hostCallTimeoutMs } : {},
1395
+ ...hostCallTimeoutMs ? { hostCallTimeoutMs } : {},
644
1396
  ...request,
645
1397
  ...reactor ? { reactor: reactor.binding } : {}
646
1398
  }
@@ -730,7 +1482,8 @@ var PieceWorkerSession = class {
730
1482
  return this.taking;
731
1483
  }
732
1484
  async acquire() {
733
- await this.pool.acquire(this);
1485
+ const { telemetry } = this.pool;
1486
+ await (telemetry ? telemetry.phase("worker.acquire", {}, () => this.pool.acquire(this)) : this.pool.acquire(this));
734
1487
  this.held = true;
735
1488
  if (this.closed) {
736
1489
  this.giveBack();
@@ -747,6 +1500,8 @@ var PieceWorkerSession = class {
747
1500
  };
748
1501
  var PieceWorkerPool = class {
749
1502
  size;
1503
+ /** @internal */
1504
+ telemetry;
750
1505
  maxQueueDepth;
751
1506
  createWorker;
752
1507
  workerOptions;
@@ -760,6 +1515,7 @@ var PieceWorkerPool = class {
760
1515
  this.maxQueueDepth = Math.max(0, Math.trunc(maxQueueDepth ?? 0));
761
1516
  this.createWorker = createWorker ?? ((o) => new PieceWorker(o));
762
1517
  this.workerOptions = workerOptions;
1518
+ this.telemetry = workerOptions.telemetry;
763
1519
  }
764
1520
  session() {
765
1521
  if (this.disposed) throw new Error("Piece worker pool is disposed");
@@ -899,6 +1655,65 @@ var UnresolvedReferenceError = class extends Error {
899
1655
  this.name = "UnresolvedReferenceError";
900
1656
  }
901
1657
  };
1658
+ /**
1659
+ * A value that exists but cannot be read, with the reason attached.
1660
+ *
1661
+ * The case it was written for: a rerun replaying a SUCCEEDED step whose
1662
+ * journaled output the payload cap truncated. The step must NOT run again — it
1663
+ * had side effects — but its output is genuinely gone, so a downstream step
1664
+ * that reads it has to be told, by name, rather than handed a truncation
1665
+ * marker or quietly made to re-run the step that produced it.
1666
+ *
1667
+ * The reason hangs off a SYMBOL key, so `JSON.stringify` drops it and nothing
1668
+ * can leak the wrapper into a payload as data.
1669
+ */
1670
+ const UNAVAILABLE = Symbol("unavailable");
1671
+ var UnavailableValueError = class extends Error {
1672
+ constructor(reason) {
1673
+ super(reason);
1674
+ this.name = "UnavailableValueError";
1675
+ }
1676
+ };
1677
+ function unavailableValue(reason) {
1678
+ return { [UNAVAILABLE]: reason };
1679
+ }
1680
+ function unavailableReason(value) {
1681
+ if (typeof value !== "object" || value === null) return void 0;
1682
+ const reason = value[UNAVAILABLE];
1683
+ return typeof reason === "string" ? reason : void 0;
1684
+ }
1685
+ /**
1686
+ * The reason of the first {@link unavailableValue} anywhere inside a value,
1687
+ * however deep, or undefined when there is none.
1688
+ *
1689
+ * A shallow check is not enough, and that was the hole: `{{steps.charge}}`
1690
+ * lands on the step's whole entry rather than on the wrapper, so the wrapper
1691
+ * sits one level down as `{ output: <wrapper> }`. The path check saw an
1692
+ * ordinary object, handed it over as data, and `JSON.stringify` then dropped
1693
+ * the symbol on the way to the piece — the downstream step received `{}` and
1694
+ * the reason was gone. Refusing by name is the whole point of the wrapper.
1695
+ */
1696
+ function unavailableValueReason(value) {
1697
+ const own = unavailableReason(value);
1698
+ if (own !== void 0) return own;
1699
+ if (Array.isArray(value)) {
1700
+ for (const item of value) {
1701
+ const nested = unavailableValueReason(item);
1702
+ if (nested !== void 0) return nested;
1703
+ }
1704
+ return;
1705
+ }
1706
+ if (typeof value === "object" && value !== null) for (const entry of Object.values(value)) {
1707
+ const nested = unavailableValueReason(entry);
1708
+ if (nested !== void 0) return nested;
1709
+ }
1710
+ }
1711
+ /** Throws {@link UnavailableValueError} when a resolved value holds an
1712
+ * unavailable wrapper at any depth; the gate every resolved input passes. */
1713
+ function assertNoUnavailableValue(value) {
1714
+ const reason = unavailableValueReason(value);
1715
+ if (reason !== void 0) throw new UnavailableValueError(reason);
1716
+ }
902
1717
  const IDENT = /[A-Za-z0-9_$-]/;
903
1718
  function parseExpression(source) {
904
1719
  let i = 0;
@@ -1042,10 +1857,13 @@ function lookupPath(scope, segments) {
1042
1857
  let current = scope;
1043
1858
  for (const segment of segments) {
1044
1859
  if (current === null || typeof current !== "object") return MISSING;
1860
+ const blocked = unavailableReason(current);
1861
+ if (blocked !== void 0) throw new UnavailableValueError(blocked);
1045
1862
  const record = current;
1046
1863
  if (!Object.prototype.hasOwnProperty.call(record, segment)) return MISSING;
1047
1864
  current = record[segment];
1048
1865
  }
1866
+ assertNoUnavailableValue(current);
1049
1867
  return current === void 0 ? MISSING : current;
1050
1868
  }
1051
1869
  const isEmpty = (value) => value === null || value === "";
@@ -1230,7 +2048,22 @@ function evaluateBranch(config) {
1230
2048
  }
1231
2049
  //#endregion
1232
2050
  //#region src/pieces/engine/step-input.ts
2051
+ /**
2052
+ * A step's resolved input, and the last gate before it crosses to the piece.
2053
+ *
2054
+ * The unavailable wrapper carries its reason on a symbol, so anything that
2055
+ * serializes it loses the reason — the worker boundary included. Checking the
2056
+ * whole resolved input here, at any depth, is what guarantees that no route
2057
+ * through resolution can hand a piece the wrapper as data: the path checks
2058
+ * inside `lookupPath` catch the ways an expression reaches one, and this
2059
+ * catches whatever they do not.
2060
+ */
1233
2061
  function resolveStepInput(step, scope) {
2062
+ const input = resolveStepInputRaw(step, scope);
2063
+ assertNoUnavailableValue(input);
2064
+ return input;
2065
+ }
2066
+ function resolveStepInputRaw(step, scope) {
1234
2067
  const { config } = step;
1235
2068
  if (step.pieceName !== CORE_PIECE_NAME || step.actionName !== "branch" || config === null || typeof config !== "object" || Array.isArray(config) || !Object.hasOwn(config, "left")) return resolveExpressions(config, scope);
1236
2069
  const record = config;
@@ -1416,6 +2249,81 @@ var BoundConnectionResolver = class {
1416
2249
  }
1417
2250
  };
1418
2251
  //#endregion
2252
+ //#region src/pieces/engine/attachment-cache.ts
2253
+ const META = ".json";
2254
+ var AttachmentCache = class {
2255
+ options;
2256
+ constructor(options) {
2257
+ this.options = options;
2258
+ }
2259
+ entry(ref) {
2260
+ const match = /^attachment:\/\/(v\d+):([a-f0-9]{64})$/i.exec(ref);
2261
+ if (!match) throw new Error(`Not a content-addressed reference: ${ref}`);
2262
+ return path.join(this.options.dir, `${match[1]}-${match[2].toLowerCase()}`);
2263
+ }
2264
+ static cacheable(ref) {
2265
+ return /^attachment:\/\/v\d+:[a-f0-9]{64}$/i.test(ref);
2266
+ }
2267
+ async get(ref, destPath) {
2268
+ const file = this.entry(ref);
2269
+ let meta;
2270
+ try {
2271
+ meta = JSON.parse(await readFile(file + META, "utf8"));
2272
+ if ((await stat(file)).size !== meta.size) {
2273
+ await this.remove(file);
2274
+ return;
2275
+ }
2276
+ } catch {
2277
+ return;
2278
+ }
2279
+ await copyFile(file, destPath, constants.COPYFILE_FICLONE);
2280
+ const now = /* @__PURE__ */ new Date();
2281
+ await utimes(file, now, now).catch(() => void 0);
2282
+ return meta;
2283
+ }
2284
+ async put(ref, srcPath, meta) {
2285
+ if (this.options.maxBytes <= 0) return;
2286
+ const file = this.entry(ref);
2287
+ const size = (await stat(srcPath)).size;
2288
+ if (size > this.options.maxBytes) return;
2289
+ await mkdir(this.options.dir, { recursive: true });
2290
+ const tmp = `${file}.${randomUUID()}.tmp`;
2291
+ try {
2292
+ await copyFile(srcPath, tmp, constants.COPYFILE_FICLONE);
2293
+ await writeFile(file + META, JSON.stringify({
2294
+ ...meta,
2295
+ size
2296
+ }));
2297
+ await rename(tmp, file);
2298
+ } catch (error) {
2299
+ await rm(tmp, { force: true });
2300
+ throw error;
2301
+ }
2302
+ await this.evict();
2303
+ }
2304
+ async remove(file) {
2305
+ await rm(file, { force: true });
2306
+ await rm(file + META, { force: true });
2307
+ }
2308
+ async evict() {
2309
+ const names = await readdir(this.options.dir).catch(() => []);
2310
+ const live = (await Promise.all(names.filter((name) => !name.endsWith(META) && !name.endsWith(".tmp")).map(async (name) => {
2311
+ const file = path.join(this.options.dir, name);
2312
+ const info = await stat(file).catch(() => void 0);
2313
+ return info ? {
2314
+ file,
2315
+ size: info.size,
2316
+ used: info.mtimeMs
2317
+ } : void 0;
2318
+ }))).filter((e) => e !== void 0).sort((a, b) => b.used - a.used);
2319
+ let total = 0;
2320
+ for (const entry of live) {
2321
+ total += entry.size;
2322
+ if (total > this.options.maxBytes) await this.remove(entry.file);
2323
+ }
2324
+ }
2325
+ };
2326
+ //#endregion
1419
2327
  //#region src/pieces/core/hints.ts
1420
2328
  function withHints(property, hints) {
1421
2329
  return {
@@ -1746,7 +2654,7 @@ const manualTrigger = createTrigger({
1746
2654
  });
1747
2655
  //#endregion
1748
2656
  //#region src/pieces/core/version.ts
1749
- const CORE_PIECE_VERSION = "6.2.3-dev.49";
2657
+ const CORE_PIECE_VERSION = "6.2.3-dev.51";
1750
2658
  //#endregion
1751
2659
  //#region src/pieces/core/index.ts
1752
2660
  const corePiece = createPiece({
@@ -1961,7 +2869,7 @@ var ActivepiecesBlockExecutor = class {
1961
2869
  const worker = supplied();
1962
2870
  if (worker) return worker;
1963
2871
  } else if (supplied) return supplied;
1964
- return this.own ??= new PieceWorker();
2872
+ return this.own ??= new PieceWorker({ telemetry: this.options.telemetry });
1965
2873
  }
1966
2874
  async execute(execution) {
1967
2875
  const resolution = this.options.resolveBlock ? await this.options.resolveBlock(execution.block) : pinnedResolution(execution.block);
@@ -1983,11 +2891,13 @@ var ActivepiecesBlockExecutor = class {
1983
2891
  const runSecrets = execution.redactValues ?? [];
1984
2892
  let redactValues = [...runSecrets];
1985
2893
  try {
1986
- const piece = await this.resolver.resolve({
2894
+ const ref = {
1987
2895
  name: parsed.packageName,
1988
2896
  version: parsed.version,
1989
2897
  ...parsed.source ? { source: parsed.source } : {}
1990
- });
2898
+ };
2899
+ const telemetry = this.options.telemetry;
2900
+ const piece = await (telemetry ? telemetry.phase("piece.resolve", {}, () => this.resolver.resolve(ref)) : this.resolver.resolve(ref));
1991
2901
  const connection = await this.resolveConnection(execution.connectionId, {
1992
2902
  piecePackage: parsed.packageName,
1993
2903
  stepId: execution.step.id,
@@ -1996,7 +2906,10 @@ var ActivepiecesBlockExecutor = class {
1996
2906
  const auth = connection?.auth;
1997
2907
  redactValues = [...runSecrets, ...connection?.secretValues ?? []];
1998
2908
  const timeoutMs = execution.step.timeoutSeconds ? execution.step.timeoutSeconds * 1e3 : this.options.defaultTimeoutMs;
1999
- const stagedInputs = await this.stageInputs(execution.config, stagingDir);
2909
+ const deadline = timeoutMs ? Date.now() + timeoutMs : void 0;
2910
+ const signal = deadline ? AbortSignal.timeout(timeoutMs) : void 0;
2911
+ const stagedInputs = await this.stageInputs(execution.config, stagingDir, signal);
2912
+ const remainingMs = deadline ? Math.max(1, deadline - Date.now()) : void 0;
2000
2913
  const pieceStore = this.options.pieceStore;
2001
2914
  const notifications = stepTaps(this.options, execution, redactValues);
2002
2915
  const egress = this.options.egress === void 0 ? DEFAULT_EGRESS_POLICY : this.options.egress;
@@ -2022,13 +2935,14 @@ var ActivepiecesBlockExecutor = class {
2022
2935
  ...this.options.onPartialOutput ? { liveOutput: true } : {},
2023
2936
  ...egress ? { egress } : {}
2024
2937
  }, {
2025
- ...timeoutMs ? { timeoutMs } : {},
2938
+ ...remainingMs ? { timeoutMs: remainingMs } : {},
2939
+ hostCallTimeoutMs: hostCallTimeoutForStep(timeoutMs),
2026
2940
  ...pieceStore ? { hostCalls: storeHandlers(pieceStore) } : {},
2027
2941
  ...notifications ? { notifications } : {},
2028
2942
  ...reactorTap ? { reactor: reactorTap } : {}
2029
2943
  });
2030
2944
  return {
2031
- output: await this.ingestFiles(result.output, result.files),
2945
+ output: await this.ingestFiles(result.output, result.files, stagingDir, signal),
2032
2946
  redactValues
2033
2947
  };
2034
2948
  } catch (error) {
@@ -2050,7 +2964,7 @@ var ActivepiecesBlockExecutor = class {
2050
2964
  secretValues: [...collectSecretValues(auth)]
2051
2965
  };
2052
2966
  }
2053
- async stageInputs(config, stagingDir) {
2967
+ async stageInputs(config, stagingDir, signal) {
2054
2968
  const port = this.options.attachments;
2055
2969
  if (!stagingDir || !port) return void 0;
2056
2970
  const refs = /* @__PURE__ */ new Set();
@@ -2062,29 +2976,63 @@ var ActivepiecesBlockExecutor = class {
2062
2976
  for (const ref of refs) {
2063
2977
  const destPath = path.join(stagingDir, `in-${index++}`);
2064
2978
  try {
2065
- const meta = await port.read(ref, destPath);
2979
+ const meta = await this.readAttachment(port, ref, destPath, signal);
2066
2980
  staged.push({
2067
2981
  ref,
2068
2982
  path: destPath,
2069
2983
  ...meta
2070
2984
  });
2071
2985
  } catch (error) {
2986
+ if (signal?.aborted) throw error;
2987
+ staged.push({
2988
+ ref,
2989
+ path: "",
2990
+ error: error instanceof Error ? error.message : String(error)
2991
+ });
2072
2992
  logger$1.debug("Left @ref unstaged: @error", ref, error instanceof Error ? error.message : String(error));
2073
2993
  }
2074
2994
  }
2075
2995
  return staged;
2076
2996
  }
2077
- async ingestFiles(output, files) {
2997
+ async readAttachment(port, ref, destPath, signal) {
2998
+ const cache = this.options.attachmentCache;
2999
+ if (!cache || !port.authorize || !AttachmentCache.cacheable(ref)) return port.read(ref, destPath, signal);
3000
+ await port.authorize(ref);
3001
+ const hit = await cache.get(ref, destPath).catch(() => void 0);
3002
+ if (hit) return {
3003
+ ...hit.fileName !== void 0 ? { fileName: hit.fileName } : {},
3004
+ ...hit.contentType !== void 0 ? { contentType: hit.contentType } : {}
3005
+ };
3006
+ const meta = await port.read(ref, destPath, signal);
3007
+ await cache.put(ref, destPath, meta).catch((error) => {
3008
+ logger$1.debug("Could not cache @ref: @error", ref, error instanceof Error ? error.message : String(error));
3009
+ });
3010
+ return meta;
3011
+ }
3012
+ async checkStagedFile(file, stagingDir) {
3013
+ if (!stagingDir) throw new Error(`Refusing to ingest "${file.fileName}": no staging directory`);
3014
+ const root = await realpath(stagingDir);
3015
+ const resolved = path.resolve(file.path);
3016
+ const info = await lstat(resolved).catch(() => void 0);
3017
+ if (!info?.isFile() || info.nlink !== 1 || path.dirname(await realpath(resolved)) !== root || path.dirname(resolved) !== path.resolve(stagingDir)) throw new Error(`Refusing to ingest "${file.fileName}": it is not a file the step wrote`);
3018
+ const limit = maxFileBytes();
3019
+ if (info.size > limit) throw new FileTooLargeError(info.size, limit);
3020
+ return info.size;
3021
+ }
3022
+ async ingestFiles(output, files, stagingDir, signal) {
2078
3023
  if (!files || files.length === 0) return output;
2079
3024
  const port = this.options.attachments;
2080
3025
  if (!port) throw new Error(`The action wrote ${files.length} file(s) through ctx.files, but no attachment store is configured for this reactor`);
2081
3026
  const refs = /* @__PURE__ */ new Map();
2082
- for (const file of files) refs.set(file.token, await port.write({
2083
- path: file.path,
2084
- fileName: file.fileName,
2085
- size: file.size,
2086
- contentType: file.contentType
2087
- }));
3027
+ for (const file of files) {
3028
+ const size = await this.checkStagedFile(file, stagingDir);
3029
+ refs.set(file.token, await port.write({
3030
+ path: path.resolve(file.path),
3031
+ fileName: file.fileName,
3032
+ size,
3033
+ contentType: file.contentType
3034
+ }, signal));
3035
+ }
2088
3036
  return rewriteFileRefs(output, refs);
2089
3037
  }
2090
3038
  dispose() {
@@ -2105,6 +3053,64 @@ var CompositeBlockExecutor = class {
2105
3053
  return this.pieces.execute(execution);
2106
3054
  }
2107
3055
  };
3056
+ /** The ceiling on one backoff wait, whatever `maxDelaySeconds` says: the wait
3057
+ * holds the run's worker slot, and the run deadline bounds the rest. */
3058
+ const MAX_RETRY_DELAY_MS = 3e5;
3059
+ function positive(value) {
3060
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : null;
3061
+ }
3062
+ /** Resolves an author-written retry block; null when it asks for one attempt,
3063
+ * which is the same thing as no retry policy and saves every reader a branch. */
3064
+ function effectiveRetryPolicy(retry) {
3065
+ if (typeof retry !== "object" || retry === null) return null;
3066
+ const record = retry;
3067
+ const asked = positive(record.maxAttempts) ?? 1;
3068
+ const maxAttempts = Math.min(Math.trunc(asked), 10);
3069
+ if (maxAttempts <= 1) return null;
3070
+ const initial = positive(record.initialDelaySeconds) ?? 0;
3071
+ const max = positive(record.maxDelaySeconds) ?? initial;
3072
+ return {
3073
+ maxAttempts,
3074
+ backoff: record.backoff === "EXPONENTIAL" ? "EXPONENTIAL" : "FIXED",
3075
+ initialDelaySeconds: initial,
3076
+ maxDelaySeconds: Math.max(initial, max),
3077
+ retryOn: Array.isArray(record.retryOn) ? record.retryOn.filter((entry) => typeof entry === "string") : []
3078
+ };
3079
+ }
3080
+ /** How long to wait before `attempt` (1-based: attempt 2 is the first retry).
3081
+ * EXPONENTIAL doubles from the initial delay, capped both by the author's
3082
+ * `maxDelaySeconds` and by {@link MAX_RETRY_DELAY_MS}. */
3083
+ function retryDelayMs(retry, attempt) {
3084
+ const initial = retry.initialDelaySeconds * 1e3;
3085
+ const raw = retry.backoff === "EXPONENTIAL" ? initial * 2 ** Math.max(0, attempt - 2) : initial;
3086
+ return Math.max(0, Math.min(raw, retry.maxDelaySeconds * 1e3, MAX_RETRY_DELAY_MS));
3087
+ }
3088
+ /**
3089
+ * Whether `retryOn` admits this error.
3090
+ *
3091
+ * The schema calls its entries "error classes", which in a workflow is a loose
3092
+ * notion: a piece throws whatever it likes, and what reaches here may be an
3093
+ * `Error` subclass of the engine's, an HTTP failure formatted by the piece
3094
+ * framework, or a string. So an entry matches the error's CLASS NAME exactly,
3095
+ * or appears anywhere in its message — case-insensitively in both cases. That
3096
+ * makes `["HostCallTimeoutError"]` and `["429"]` both work, which is what an
3097
+ * author writing one of these actually means.
3098
+ *
3099
+ * An EMPTY list admits everything. The schema's own reading ("everything else
3100
+ * fails terminally on attempt 1") would make the shipped default — an empty
3101
+ * `retryOn` — turn every `maxAttempts` into a lie, so empty is read as
3102
+ * "unrestricted" and a non-empty list as the filter it is.
3103
+ */
3104
+ function isRetryableError(retry, error) {
3105
+ if (retry.retryOn.length === 0) return true;
3106
+ const name = (error instanceof Error ? error.name : "").toLowerCase();
3107
+ const message = (error instanceof Error ? error.message : String(error)).toLowerCase();
3108
+ return retry.retryOn.some((entry) => {
3109
+ const needle = entry.trim().toLowerCase();
3110
+ if (needle === "") return false;
3111
+ return needle === name || message.includes(needle);
3112
+ });
3113
+ }
2108
3114
  //#endregion
2109
3115
  //#region src/pieces/engine/dynamic-props.ts
2110
3116
  var DynamicPropertiesError = class extends Error {
@@ -2208,6 +3214,11 @@ function errorNameOf(error) {
2208
3214
  function journaled(value, values) {
2209
3215
  return value === void 0 ? void 0 : redact(value, { values });
2210
3216
  }
3217
+ /** What a run that passed `policy.runTimeoutSeconds` ends CANCELLED with. */
3218
+ /** The error name a run its deadline cancelled is journaled with; it is what
3219
+ * marks the run as resumable, unlike a firing that was refused. */
3220
+ const RUN_DEADLINE_ERROR_NAME = "RunDeadlineExceeded";
3221
+ const RUN_DEADLINE_REASON = "Run exceeded its runTimeoutSeconds and was cancelled";
2211
3222
  async function runWorkflow(options) {
2212
3223
  const { definition, executor } = options;
2213
3224
  const runSecrets = options.redactValues ?? [];
@@ -2219,8 +3230,17 @@ async function runWorkflow(options) {
2219
3230
  const records = /* @__PURE__ */ new Map();
2220
3231
  const edgeDecisions = /* @__PURE__ */ new Map();
2221
3232
  let runFailed;
3233
+ let runCancelled;
2222
3234
  let runFailedName;
2223
3235
  let executedCount = 0;
3236
+ const sleep = options.sleep ?? ((ms) => new Promise((resolve) => {
3237
+ setTimeout(resolve, ms).unref();
3238
+ }));
3239
+ const outOfTime = () => {
3240
+ if (options.deadline === void 0) return void 0;
3241
+ if (Date.now() < options.deadline) return void 0;
3242
+ return RUN_DEADLINE_REASON;
3243
+ };
2224
3244
  const journal = async (record) => {
2225
3245
  if (!options.onStep) return;
2226
3246
  const ordinal = executedCount++;
@@ -2293,114 +3313,218 @@ async function runWorkflow(options) {
2293
3313
  return;
2294
3314
  }
2295
3315
  const port = replay.port ?? "next";
3316
+ const output = replay.outputTruncated ? unavailableValue(`Output of step "${step.key}" is unavailable: it succeeded in the run being rerun, but the run journal truncated its output past the payload cap. The step is not re-run, because it had side effects. Fire the workflow again instead of rerunning it.`) : replay.output;
2296
3317
  const record = {
2297
3318
  stepId: step.id,
2298
3319
  key: step.key,
2299
3320
  pieceName: step.pieceName,
2300
3321
  blockName: step.actionName,
2301
3322
  status: "REPLAYED",
2302
- output: replay.output,
3323
+ output: replay.outputTruncated ? void 0 : replay.output,
3324
+ ...replay.outputTruncated && replay.truncatedOutput !== void 0 ? { journaledOutput: replay.truncatedOutput } : {},
2303
3325
  port,
2304
3326
  configHash: stepConfigHash(step)
2305
3327
  };
2306
3328
  records.set(step.id, record);
2307
3329
  await journal(record);
2308
- scope.steps[step.key] = { output: replay.output };
3330
+ scope.steps[step.key] = { output };
2309
3331
  decideOutgoing(step.id, port);
2310
3332
  return;
2311
3333
  }
3334
+ const retry = step.retry === void 0 || step.retry === null ? options.defaultRetry ?? null : effectiveRetryPolicy(step.retry);
3335
+ const maxAttempts = retry?.maxAttempts ?? 1;
2312
3336
  const startedAt = (/* @__PURE__ */ new Date()).toISOString();
2313
3337
  let input;
2314
3338
  try {
2315
3339
  input = resolveStepInput(step, scope);
2316
3340
  checkDynamicProperties(input, step.propertySettings);
2317
- const result = await executor.execute({
2318
- block: stepBlock(step),
2319
- config: input,
2320
- connectionId: step.connectionId,
2321
- reactorConnectionId: step.reactorConnectionId,
2322
- step,
2323
- ...runSecrets.length > 0 ? { redactValues: runSecrets } : {}
2324
- });
2325
- const port = result.port ?? "next";
2326
- const values = [...runSecrets, ...result.redactValues ?? []];
2327
- const record = {
2328
- stepId: step.id,
2329
- key: step.key,
2330
- pieceName: step.pieceName,
2331
- blockName: step.actionName,
2332
- status: "SUCCEEDED",
2333
- input: journaled(input, values),
2334
- output: journaled(referenceDocuments(result.output), values),
2335
- port,
2336
- startedAt,
2337
- endedAt: (/* @__PURE__ */ new Date()).toISOString(),
2338
- ...withPiece(pieceRecord(result.resolution)),
2339
- configHash: stepConfigHash(step)
2340
- };
2341
- records.set(step.id, record);
2342
- await journal(record);
2343
- scope.steps[step.key] = { output: result.output };
2344
- decideOutgoing(step.id, port);
2345
3341
  } catch (error) {
2346
- const values = [...runSecrets, ...secretsFor(error)];
2347
- const detail = redactMessage(errorMessage(error), { values });
2348
- const errorName = errorNameOf(error);
2349
- const record = {
2350
- stepId: step.id,
2351
- key: step.key,
2352
- pieceName: step.pieceName,
2353
- blockName: step.actionName,
2354
- status: "FAILED",
2355
- input: journaled(input, values),
2356
- error: detail,
2357
- ...errorName ? { errorName } : {},
3342
+ await endStep(step, {
3343
+ error,
3344
+ input,
2358
3345
  startedAt,
2359
- endedAt: (/* @__PURE__ */ new Date()).toISOString(),
2360
- ...withPiece(pieceRecord(resolutionOf(error))),
2361
- configHash: stepConfigHash(step)
2362
- };
2363
- records.set(step.id, record);
2364
- await journal(record);
2365
- scope.steps[step.key] = { error: detail };
2366
- decideOutgoing(step.id, "error");
2367
- if (!definition.edges.some((edge) => edge.from === step.id && edgeDecisions.get(edge.id))) {
2368
- runFailed = `Step "${step.key}" failed: ${detail}`;
2369
- runFailedName = errorName;
3346
+ attempt: 1
3347
+ });
3348
+ return;
3349
+ }
3350
+ for (let attempt = 1;; attempt++) {
3351
+ const attempts = attempt > 1 ? { attempts: attempt } : {};
3352
+ try {
3353
+ const result = await executor.execute({
3354
+ block: stepBlock(step),
3355
+ config: input,
3356
+ connectionId: step.connectionId,
3357
+ reactorConnectionId: step.reactorConnectionId,
3358
+ step,
3359
+ ...runSecrets.length > 0 ? { redactValues: runSecrets } : {}
3360
+ });
3361
+ const port = result.port ?? "next";
3362
+ const values = [...runSecrets, ...result.redactValues ?? []];
3363
+ const record = {
3364
+ stepId: step.id,
3365
+ key: step.key,
3366
+ pieceName: step.pieceName,
3367
+ blockName: step.actionName,
3368
+ status: "SUCCEEDED",
3369
+ input: journaled(input, values),
3370
+ output: journaled(referenceDocuments(result.output), values),
3371
+ port,
3372
+ startedAt,
3373
+ endedAt: (/* @__PURE__ */ new Date()).toISOString(),
3374
+ ...withPiece(pieceRecord(result.resolution)),
3375
+ configHash: stepConfigHash(step),
3376
+ ...attempts
3377
+ };
3378
+ records.set(step.id, record);
3379
+ await journal(record);
3380
+ scope.steps[step.key] = { output: result.output };
3381
+ decideOutgoing(step.id, port);
3382
+ return;
3383
+ } catch (error) {
3384
+ const indeterminate = isIndeterminateError(error);
3385
+ if (retry !== null && attempt < maxAttempts && !indeterminate && !(error instanceof UnavailableValueError) && isRetryableError(retry, error)) {
3386
+ const stopped = await waitForRetry(retryDelayMs(retry, attempt + 1));
3387
+ if (stopped !== void 0) {
3388
+ await endStep(step, {
3389
+ error,
3390
+ input,
3391
+ startedAt,
3392
+ attempt,
3393
+ cancelled: stopped
3394
+ });
3395
+ return;
3396
+ }
3397
+ continue;
3398
+ }
3399
+ await endStep(step, {
3400
+ error,
3401
+ input,
3402
+ startedAt,
3403
+ attempt
3404
+ });
3405
+ return;
2370
3406
  }
2371
3407
  }
2372
3408
  };
3409
+ /**
3410
+ * A retry wait, clipped to the run's deadline.
3411
+ *
3412
+ * Undefined when there is room for both the wait and another attempt.
3413
+ * Otherwise the reason the run is over: the wait is served out only as far
3414
+ * as the deadline, never past it, and the caller runs no further attempt.
3415
+ * Sleeping the full backoff and leaving the deadline to "the next attempt's
3416
+ * own check" meant that attempt ran its side effect after the run had
3417
+ * expired, and the run then ended FAILED rather than CANCELLED.
3418
+ */
3419
+ async function waitForRetry(waitMs) {
3420
+ const expired = outOfTime();
3421
+ if (expired !== void 0) return expired;
3422
+ if (options.deadline === void 0) {
3423
+ if (waitMs > 0) await sleep(waitMs);
3424
+ return;
3425
+ }
3426
+ const remaining = options.deadline - Date.now();
3427
+ if (waitMs >= remaining) {
3428
+ if (remaining > 0) await sleep(remaining);
3429
+ return RUN_DEADLINE_REASON;
3430
+ }
3431
+ if (waitMs > 0) await sleep(waitMs);
3432
+ return outOfTime();
3433
+ }
3434
+ /**
3435
+ * The terminal end of a step that threw: its record, its error port, and
3436
+ * what that means for the run.
3437
+ *
3438
+ * One place for it, because three paths reach it — a deterministic
3439
+ * resolution failure before the first attempt, an exhausted or refused
3440
+ * retry, and a retry the run's deadline cut short.
3441
+ */
3442
+ async function endStep(step, outcome) {
3443
+ const { error, input, startedAt, attempt } = outcome;
3444
+ const values = [...runSecrets, ...secretsFor(error)];
3445
+ const detail = redactMessage(errorMessage(error), { values });
3446
+ const indeterminate = isIndeterminateError(error);
3447
+ const errorName = errorNameOf(error);
3448
+ const record = {
3449
+ stepId: step.id,
3450
+ key: step.key,
3451
+ pieceName: step.pieceName,
3452
+ blockName: step.actionName,
3453
+ status: indeterminate ? "INDETERMINATE" : "FAILED",
3454
+ input: journaled(input, values),
3455
+ error: detail,
3456
+ ...errorName ? { errorName } : {},
3457
+ startedAt,
3458
+ endedAt: (/* @__PURE__ */ new Date()).toISOString(),
3459
+ ...withPiece(pieceRecord(resolutionOf(error))),
3460
+ configHash: stepConfigHash(step),
3461
+ ...attempt > 1 ? { attempts: attempt } : {}
3462
+ };
3463
+ records.set(step.id, record);
3464
+ await journal(record);
3465
+ if (indeterminate) {
3466
+ runFailed = `Step "${step.key}" is INDETERMINATE: ${detail}`;
3467
+ runFailedName = errorName;
3468
+ return;
3469
+ }
3470
+ if (outcome.cancelled !== void 0) {
3471
+ runCancelled = outcome.cancelled;
3472
+ return;
3473
+ }
3474
+ scope.steps[step.key] = { error: detail };
3475
+ decideOutgoing(step.id, "error");
3476
+ if (!definition.edges.some((edge) => edge.from === step.id && edgeDecisions.get(edge.id))) {
3477
+ const failedAfter = attempt > 1 ? ` after ${attempt} attempts` : "";
3478
+ runFailed = `Step "${step.key}" failed${failedAfter}: ${detail}`;
3479
+ runFailedName = errorName;
3480
+ }
3481
+ }
3482
+ const expiredBeforeStep = () => {
3483
+ runCancelled ??= outOfTime();
3484
+ return runCancelled !== void 0;
3485
+ };
2373
3486
  let progressed = true;
2374
- while (progressed && !runFailed) {
3487
+ while (progressed && !runFailed && !runCancelled) {
2375
3488
  progressed = false;
2376
3489
  for (const step of definition.steps) {
2377
3490
  if (records.has(step.id)) continue;
2378
3491
  const inbound = inboundEdges(step);
2379
3492
  if (isEntryStep(step)) {
3493
+ if (expiredBeforeStep()) break;
2380
3494
  await executeStep(step);
2381
3495
  progressed = true;
2382
- if (runFailed) break;
3496
+ if (runFailed || runCancelled) break;
2383
3497
  continue;
2384
3498
  }
2385
3499
  if (inbound.length === 0) continue;
2386
3500
  if (!inbound.every((edge) => edgeDecisions.has(edge.id))) continue;
2387
- if (inbound.some((edge) => edgeDecisions.get(edge.id))) await executeStep(step);
2388
- else skipStep(step);
3501
+ if (inbound.some((edge) => edgeDecisions.get(edge.id))) {
3502
+ if (expiredBeforeStep()) break;
3503
+ await executeStep(step);
3504
+ } else skipStep(step);
2389
3505
  progressed = true;
2390
- if (runFailed) break;
3506
+ if (runFailed || runCancelled) break;
2391
3507
  }
2392
3508
  }
2393
3509
  for (const step of definition.steps) if (!records.has(step.id)) skipStep(step);
2394
3510
  const steps = definition.steps.map((step) => records.get(step.id));
2395
3511
  const warnings = options.declaredPorts ? deadPortWarnings(definition, options.declaredPorts) : [];
2396
3512
  const noted = warnings.length > 0 ? { warnings } : {};
2397
- return runFailed ? {
3513
+ if (runFailed) return {
2398
3514
  status: "FAILED",
2399
3515
  steps,
2400
3516
  error: runFailed,
2401
3517
  ...runFailedName ? { errorName: runFailedName } : {},
2402
3518
  ...noted
2403
- } : {
3519
+ };
3520
+ if (runCancelled) return {
3521
+ status: "CANCELLED",
3522
+ steps,
3523
+ error: runCancelled,
3524
+ errorName: RUN_DEADLINE_ERROR_NAME,
3525
+ ...noted
3526
+ };
3527
+ return {
2404
3528
  status: "SUCCEEDED",
2405
3529
  steps,
2406
3530
  ...noted
@@ -2453,6 +3577,6 @@ function installedPieces() {
2453
3577
  return [...builtinLocalPieces(), ...packagePieces.entries().filter((piece) => !isBuiltinPiece(piece.name))];
2454
3578
  }
2455
3579
  //#endregion
2456
- export { expressionsIn as A, PieceWorkerError as B, CORE_PIECE_VERSION as C, declaredReactorConnectionIds as D, declaredConnectionIds as E, secretRefFromId as F, ensurePieceBundle as G, bundleResolver as H, stepBlock as I, pieceRegistrySource as J, fetchPieceBundle as K, triggerBlock as L, SecretDeletedError as M, SecretNotFoundError as N, shapeConnection as O, parseSecretRef as P, PieceWorkerPool as R, CORE_PIECE_NAME$1 as S, StaticConnectionResolver as T, pieceModuleRef as U, PieceWorkerTimeoutError as V, sourcedResolver as W, setPieceRegistryUrl as Y, blockLabel as _, errorNameOf as a, builtinPiece as b, hashOf as c, checkDynamicProperties as d, ActivepiecesBlockExecutor as f, storeHandlers as g, resolvedBlock as h, packagePieces as i, InMemorySecretProvider as j, resolveStepInput as k, stepConfigHash as l, isHostBound as m, installedPiece as n, runWorkflow as o, CompositeBlockExecutor as p, npmRegistryBaseUrl as q, installedPieces as r, canonicalJson as s, PieceRegistry as t, DynamicPropertiesError as u, unpinnedNote as v, BoundConnectionResolver as w, describeBuiltinPiece as x, blockPorts as y, PieceWorker as z };
3580
+ export { isPrivateAddress as $, declaredConnectionIds as A, stepBlock as B, builtinPiece as C, AttachmentCache as D, CORE_PIECE_VERSION as E, InMemorySecretProvider as F, PieceWorkerError as G, PieceWorkerPool as H, SecretDeletedError as I, redact as J, PieceWorkerTimeoutError as K, SecretNotFoundError as L, shapeConnection as M, resolveStepInput as N, BoundConnectionResolver as O, expressionsIn as P, DEFAULT_EGRESS_POLICY as Q, parseSecretRef as R, blockPorts as S, CORE_PIECE_NAME$1 as T, PieceWorker as U, triggerBlock as V, PieceWorkerDisposedError as W, rememberSecrets as X, redactMessage as Y, secretsFor as Z, resolvedBlock as _, isIndeterminateError as _t, RUN_DEADLINE_ERROR_NAME as a, pieceModuleRef as at, resolutionOf as b, canonicalJson as c, UnsupportedPieceFeatureError as ct, DynamicPropertiesError as d, unsupportedTrigger as dt, parseAddress as et, checkDynamicProperties as f, ensurePieceBundle as ft, isHostBound as g, setPieceRegistryUrl as gt, CompositeBlockExecutor as h, pieceRegistrySource as ht, packagePieces as i, bundleResolver as it, declaredReactorConnectionIds as j, StaticConnectionResolver as k, hashOf as l, authMethodFor as lt, ActivepiecesBlockExecutor as m, npmRegistryBaseUrl as mt, installedPiece as n, FileTooLargeError as nt, errorNameOf as o, sourcedResolver as ot, effectiveRetryPolicy as p, fetchPieceBundle as pt, containsRedactedMarker as q, installedPieces as r, maxFileBytes as rt, runWorkflow as s, buildDescriptor as st, PieceRegistry as t, extractDedupeKey as tt, stepConfigHash as u, unsupportedAuth as ut, storeHandlers as v, describeBuiltinPiece as w, unpinnedNote as x, blockLabel as y, secretRefFromId as z };
2457
3581
 
2458
- //# sourceMappingURL=piece-registry-CpiVMWNv.js.map
3582
+ //# sourceMappingURL=piece-registry-D0pa4v0T.js.map