@powerhousedao/reactor-workflow 6.2.3-dev.48 → 6.2.3-dev.50

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) {
@@ -337,6 +380,295 @@ async function readDependencies(dir) {
337
380
  return JSON.parse(raw).dependencies ?? {};
338
381
  }
339
382
  //#endregion
383
+ //#region src/pieces/activepieces/unsupported.ts
384
+ const ISSUES_URL = "https://github.com/powerhouse-inc/powerhouse/issues/";
385
+ function unsupported(feature, issue) {
386
+ return {
387
+ feature,
388
+ issue,
389
+ reason: `${feature} is not supported yet (${ISSUES_URL}${issue})`
390
+ };
391
+ }
392
+ function isRecord$1(value) {
393
+ return value !== null && typeof value === "object" && !Array.isArray(value);
394
+ }
395
+ function unsupportedAuth(auth) {
396
+ if (Array.isArray(auth)) {
397
+ const reasons = auth.map(unsupportedMethod);
398
+ return reasons.some((reason) => reason === void 0) ? void 0 : reasons[0];
399
+ }
400
+ return unsupportedMethod(auth);
401
+ }
402
+ function authMethodFor(auth, type) {
403
+ if (!Array.isArray(auth)) return auth;
404
+ return auth.find((entry) => isRecord$1(entry) && entry.type === type);
405
+ }
406
+ function unsupportedMethod(auth) {
407
+ if (!isRecord$1(auth)) return void 0;
408
+ if (auth.type === "OAUTH2" && auth.grantType === "client_credentials") return unsupported("OAuth2 client credentials", 3091);
409
+ if (auth.type === "OIDC") return unsupported("OIDC auth", 3091);
410
+ if (auth.type === "CUSTOM_AUTH" && auth.refresh != null) return unsupported("CustomAuth refresh", 3091);
411
+ }
412
+ function unsupportedTrigger(trigger) {
413
+ if (trigger.type === "MANUAL") return unsupported("TriggerStrategy.MANUAL", 3091);
414
+ const strategy = checkTriggerStrategy(trigger.type);
415
+ if ("issue" in strategy) {
416
+ const issue = trigger.type === "APP_WEBHOOK" ? 3081 : 3091;
417
+ return {
418
+ feature: `TriggerStrategy ${String(trigger.type)}`,
419
+ issue,
420
+ reason: `${strategy.issue} (${ISSUES_URL}${issue})`
421
+ };
422
+ }
423
+ const problem = renewProblem(trigger.renewConfiguration);
424
+ if (problem) return {
425
+ feature: "renewConfiguration",
426
+ issue: 3090,
427
+ reason: `renewConfiguration ${problem} (${ISSUES_URL}3090)`
428
+ };
429
+ }
430
+ function cronFires(cron) {
431
+ try {
432
+ return new Cron(cron, {
433
+ timezone: "UTC",
434
+ legacyMode: false
435
+ }).nextRun() !== null;
436
+ } catch {
437
+ return false;
438
+ }
439
+ }
440
+ function renewProblem(renew) {
441
+ if (renew === void 0 || renew === null) return void 0;
442
+ if (!isRecord$1(renew)) return "is not an object";
443
+ if (renew.strategy === "NONE") return void 0;
444
+ if (renew.strategy !== "CRON") return `strategy ${String(renew.strategy)} is not supported`;
445
+ const cron = renew.cronExpression;
446
+ if (typeof cron !== "string" || !cronFires(cron)) return `cron ${JSON.stringify(cron)} is invalid`;
447
+ }
448
+ function triggerRenew(trigger) {
449
+ const renew = trigger.renewConfiguration;
450
+ if (!isRecord$1(renew) || renew.strategy !== "CRON") return void 0;
451
+ if (renewProblem(renew)) return void 0;
452
+ return {
453
+ strategy: "CRON",
454
+ cronExpression: renew.cronExpression
455
+ };
456
+ }
457
+ var UnsupportedPieceFeatureError = class extends Error {
458
+ feature;
459
+ issue;
460
+ constructor(subject, unsupportedFeature) {
461
+ super(`${subject}: ${unsupportedFeature.reason}`);
462
+ this.name = "UnsupportedPieceFeatureError";
463
+ this.feature = unsupportedFeature.feature;
464
+ this.issue = unsupportedFeature.issue;
465
+ }
466
+ };
467
+ //#endregion
468
+ //#region src/pieces/activepieces/descriptor.ts
469
+ function describeOAuth2(raw) {
470
+ if (typeof raw.authUrl !== "string" || typeof raw.tokenUrl !== "string") return;
471
+ const strings = (value) => Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
472
+ const extra = raw.extra && typeof raw.extra === "object" ? Object.fromEntries(Object.entries(raw.extra).filter((entry) => typeof entry[1] === "string")) : void 0;
473
+ return {
474
+ authUrl: raw.authUrl,
475
+ tokenUrl: raw.tokenUrl,
476
+ scope: strings(raw.scope),
477
+ ...typeof raw.prompt === "string" ? { prompt: raw.prompt } : {},
478
+ ...typeof raw.pkce === "boolean" ? { pkce: raw.pkce } : {},
479
+ ...raw.pkceMethod === "plain" || raw.pkceMethod === "S256" ? { pkceMethod: raw.pkceMethod } : {},
480
+ ...raw.authorizationMethod === "HEADER" || raw.authorizationMethod === "BODY" ? { authorizationMethod: raw.authorizationMethod } : {},
481
+ ...typeof raw.grantType === "string" ? { grantType: raw.grantType } : {},
482
+ ...extra && Object.keys(extra).length > 0 ? { extra } : {}
483
+ };
484
+ }
485
+ function describeHandshake(trigger) {
486
+ const strategy = trigger.handshakeConfiguration?.strategy;
487
+ if (!strategy || strategy === "NONE") return void 0;
488
+ return {
489
+ strategy,
490
+ paramName: trigger.handshakeConfiguration?.paramName
491
+ };
492
+ }
493
+ function optional(key, value) {
494
+ return value === void 0 ? {} : { [key]: value };
495
+ }
496
+ function describeOption(option) {
497
+ return {
498
+ label: option.label,
499
+ value: option.value,
500
+ ...typeof option.description === "string" && option.description ? { description: option.description } : {},
501
+ ...typeof option.icon === "string" && option.icon ? { icon: option.icon } : {}
502
+ };
503
+ }
504
+ function describeHints(prop) {
505
+ const hints = {};
506
+ if (prop.width === "half" || prop.width === "full") hints.width = prop.width;
507
+ for (const key of [
508
+ "icon",
509
+ "variant",
510
+ "display",
511
+ "formatProperty"
512
+ ]) {
513
+ const value = prop[key];
514
+ if (typeof value === "string" && value !== "") hints[key] = value;
515
+ }
516
+ for (const key of [
517
+ "min",
518
+ "max",
519
+ "step"
520
+ ]) {
521
+ const value = prop[key];
522
+ if (typeof value === "number" && Number.isFinite(value)) hints[key] = value;
523
+ }
524
+ if (Array.isArray(prop.reveals)) {
525
+ const reveals = prop.reveals.filter((name) => typeof name === "string");
526
+ if (reveals.length > 0) hints.reveals = reveals;
527
+ }
528
+ if (prop.refreshOnSearch === true) hints.refreshOnSearch = true;
529
+ const showWhen = prop.showWhen;
530
+ if (typeof showWhen?.prop === "string" && Array.isArray(showWhen.oneOf)) {
531
+ const oneOf = showWhen.oneOf;
532
+ hints.showWhen = {
533
+ prop: showWhen.prop,
534
+ oneOf: [...oneOf]
535
+ };
536
+ }
537
+ if (typeof prop.emptyChoice === "string" && prop.emptyChoice !== "") hints.emptyChoice = prop.emptyChoice;
538
+ return hints;
539
+ }
540
+ function describeGroups(groups) {
541
+ if (!Array.isArray(groups)) return void 0;
542
+ const described = groups.filter((group) => typeof group.key === "string" && typeof group.display === "string" && Array.isArray(group.props)).map((group) => ({
543
+ key: group.key,
544
+ display: group.display,
545
+ ...group.label ? { label: group.label } : {},
546
+ ...group.description ? { description: group.description } : {},
547
+ ...group.icon ? { icon: group.icon } : {},
548
+ props: group.props.filter((name) => typeof name === "string")
549
+ }));
550
+ return described.length > 0 ? described : void 0;
551
+ }
552
+ function hasResolver(prop) {
553
+ return typeof prop.options === "function" || typeof prop.props === "function";
554
+ }
555
+ function toPropDescriptor(name, prop, resolverId) {
556
+ const dynamic = hasResolver(prop);
557
+ const descriptor = {
558
+ name,
559
+ displayName: prop.displayName ?? name,
560
+ type: prop.type ?? "UNKNOWN",
561
+ required: prop.required ?? false,
562
+ hasDynamicResolver: dynamic
563
+ };
564
+ if (typeof prop.description === "string" && prop.description !== "") descriptor.description = prop.description;
565
+ if (typeof prop.placeholder === "string" && prop.placeholder !== "") descriptor.placeholder = prop.placeholder;
566
+ if (prop.defaultValue !== void 0) descriptor.defaultValue = prop.defaultValue;
567
+ if (prop.advanced === true) descriptor.advanced = true;
568
+ if (dynamic && resolverId) descriptor.dynamicResolverId = resolverId;
569
+ if (dynamic && Array.isArray(prop.refreshers)) descriptor.refreshers = prop.refreshers.filter((entry) => typeof entry === "string");
570
+ if (typeof prop.options === "object" && Array.isArray(prop.options.options)) {
571
+ descriptor.staticOptions = prop.options.options.map(describeOption);
572
+ if (prop.options.disabled === true) descriptor.staticDisabled = true;
573
+ if (typeof prop.options.placeholder === "string") descriptor.staticPlaceholder = prop.options.placeholder;
574
+ }
575
+ Object.assign(descriptor, describeHints(prop));
576
+ if (prop.properties && typeof prop.properties === "object") {
577
+ const nested = describeProperties(prop.properties);
578
+ if (nested.length > 0) descriptor.properties = nested;
579
+ }
580
+ return descriptor;
581
+ }
582
+ function describeProperties(props, resolverIdFor) {
583
+ if (!props || typeof props !== "object") return [];
584
+ return Object.entries(props).filter((entry) => isPropertyObject(entry[1])).map(([propName, prop]) => toPropDescriptor(propName, prop, resolverIdFor?.(propName)));
585
+ }
586
+ function isPropertyObject(value) {
587
+ return value !== null && typeof value === "object";
588
+ }
589
+ function withUnsupported(feature) {
590
+ return feature ? { unsupported: feature } : {};
591
+ }
592
+ function describeAuthMethod(auth) {
593
+ if (!auth || typeof auth !== "object") return void 0;
594
+ const method = auth;
595
+ const props = method.props && typeof method.props === "object" ? describeProperties(method.props) : [];
596
+ const oauth2 = method.type === "OAUTH2" ? describeOAuth2(auth) : void 0;
597
+ return {
598
+ type: method.type ?? "UNKNOWN",
599
+ displayName: method.displayName,
600
+ description: method.description,
601
+ required: method.required,
602
+ ...props.length > 0 ? { props } : {},
603
+ ...oauth2 ? { oauth2 } : {},
604
+ ...withUnsupported(unsupportedAuth(method))
605
+ };
606
+ }
607
+ function declaredPorts(ports) {
608
+ if (!Array.isArray(ports) || ports.length === 0) return void 0;
609
+ return ports.every((port) => typeof port === "string") ? ports : void 0;
610
+ }
611
+ function declaredReactor(value) {
612
+ return value === "read" || value === "write" ? value : void 0;
613
+ }
614
+ function buildDescriptor(piece, source, options = {}) {
615
+ const actions = Object.entries(getActions(piece)).map(([actionName, action]) => ({
616
+ name: action.name ?? actionName,
617
+ displayName: action.displayName ?? actionName,
618
+ description: action.description,
619
+ requireAuth: action.requireAuth ?? false,
620
+ ...optional("requireReactor", declaredReactor(action.requireReactor)),
621
+ ports: (options.routed ? declaredPorts(action.ports) : void 0) ?? PIECE_ACTION_PORTS,
622
+ props: describeProperties(action.props, (propName) => `activepieces:${source.packageName}#${actionName}.${propName}`),
623
+ ...action.outputSchema !== void 0 ? { outputSchema: action.outputSchema } : {},
624
+ ...action.audience !== void 0 ? { audience: action.audience } : {},
625
+ ...optional("propertyGroups", describeGroups(action.propertyGroups)),
626
+ ...optional("classification", typeof action.classification === "string" ? action.classification : void 0),
627
+ ...optional("errorHandlingOptions", action.errorHandlingOptions)
628
+ }));
629
+ const triggers = Object.entries(getTriggers(piece)).map(([triggerName, trigger]) => ({
630
+ name: trigger.name ?? triggerName,
631
+ displayName: trigger.displayName ?? triggerName,
632
+ description: trigger.description,
633
+ strategy: trigger.type ?? "UNKNOWN",
634
+ testStrategy: trigger.testStrategy,
635
+ ...typeof trigger.display === "string" && trigger.display !== "" ? { display: trigger.display } : {},
636
+ requireAuth: trigger.requireAuth ?? false,
637
+ ...optional("requireReactor", declaredReactor(trigger.requireReactor)),
638
+ ports: TRIGGER_PORTS,
639
+ props: describeProperties(trigger.props, (propName) => `activepieces:${source.packageName}#${triggerName}.${propName}`),
640
+ ...trigger.outputSchema !== void 0 ? { outputSchema: trigger.outputSchema } : {},
641
+ hasSampleData: trigger.sampleData !== void 0 && trigger.sampleData !== null,
642
+ ...trigger.sampleData !== void 0 && trigger.sampleData !== null ? { sampleData: trigger.sampleData } : {},
643
+ handshake: describeHandshake(trigger),
644
+ ...optional("renew", triggerRenew(trigger)),
645
+ ...optional("propertyGroups", describeGroups(trigger.propertyGroups)),
646
+ ...options.hostFed ? {} : withUnsupported(unsupportedTrigger(trigger))
647
+ }));
648
+ const descriptor = {
649
+ id: `activepieces:${source.packageName}`,
650
+ source,
651
+ displayName: piece.displayName,
652
+ description: piece.description,
653
+ logoUrl: piece.logoUrl,
654
+ categories: piece.categories,
655
+ ...piece.deprecated === true ? { deprecated: true } : {},
656
+ minimumSupportedRelease: piece.minimumSupportedRelease,
657
+ maximumSupportedRelease: piece.maximumSupportedRelease,
658
+ actions,
659
+ triggers,
660
+ ...withUnsupported(unsupportedAuth(piece.auth))
661
+ };
662
+ if (Array.isArray(piece.auth)) {
663
+ const methods = piece.auth.map(describeAuthMethod).filter((method) => method !== void 0);
664
+ if (methods.length > 0) descriptor.auth = methods;
665
+ } else {
666
+ const method = describeAuthMethod(piece.auth);
667
+ if (method) descriptor.auth = method;
668
+ }
669
+ return descriptor;
670
+ }
671
+ //#endregion
340
672
  //#region src/pieces/activepieces/resolver.ts
341
673
  const DOWNLOADS = {
342
674
  registry: ["registry"],
@@ -400,6 +732,330 @@ function pieceModuleRef(piece) {
400
732
  return piece.entryPath ? { entryPath: piece.entryPath } : { bundleDir: piece.bundleDir };
401
733
  }
402
734
  //#endregion
735
+ //#region src/pieces/activepieces/worker/json-safe.ts
736
+ function jsonSafe(value) {
737
+ try {
738
+ return JSON.parse(JSON.stringify(value));
739
+ } catch {
740
+ return String(value);
741
+ }
742
+ }
743
+ //#endregion
744
+ //#region src/pieces/activepieces/worker/protocol.ts
745
+ const REACTOR_RPC = "reactor-rpc";
746
+ const MODEL_MANIFEST = "model-manifest";
747
+ const LOG_WRITE = "log.write";
748
+ const OUTPUT_UPDATE = "output.update";
749
+ const STORE_GET = "store.get";
750
+ const STORE_PUT = "store.put";
751
+ const STORE_DELETE = "store.delete";
752
+ //#endregion
753
+ //#region src/pieces/activepieces/worker/host-call.ts
754
+ const DEFAULT_HOST_CALL_TIMEOUT_MS = 1e4;
755
+ function configuredMaxFileBytes() {
756
+ const raw = process.env.PH_WORKFLOWS_PIECE_MAX_FILE_BYTES;
757
+ if (raw === void 0) return void 0;
758
+ const parsed = Number(raw);
759
+ return Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : void 0;
760
+ }
761
+ function maxFileBytes() {
762
+ return configuredMaxFileBytes() ?? 8388608;
763
+ }
764
+ /** How long a piece's call of its host may take before the step gives up on
765
+ * it. Read per call, like the file ceiling, and travelling on the wire for the
766
+ * same reason: the child is forked with no environment. */
767
+ const HOST_CALL_TIMEOUT_ENV = "PH_WORKFLOWS_HOST_CALL_TIMEOUT_MS";
768
+ /**
769
+ * The host-call cap for one step.
770
+ *
771
+ * The 10s default outran reality for a reactor dispatch under load and the
772
+ * step was reported FAILED for a write that had been committed (backlog item
773
+ * 6). So it is configurable, and it is never shorter than the step's OWN
774
+ * timeout: a step the author gave two minutes should not have its host calls
775
+ * cut off after ten seconds. The child still clips each call to end before
776
+ * the step's kill deadline (`worker/host-call.ts`).
777
+ */
778
+ function hostCallTimeoutForStep(stepTimeoutMs, env = process.env) {
779
+ const configured = Number(env[HOST_CALL_TIMEOUT_ENV]);
780
+ return Math.max(Number.isFinite(configured) && configured > 0 ? Math.floor(configured) : DEFAULT_HOST_CALL_TIMEOUT_MS, stepTimeoutMs ?? 0);
781
+ }
782
+ var FileTooLargeError = class extends Error {
783
+ size;
784
+ limit;
785
+ constructor(size, limit = maxFileBytes()) {
786
+ super(`File of ${size} bytes exceeds the ${limit} byte limit (raise PH_WORKFLOWS_PIECE_MAX_FILE_BYTES to allow more)`);
787
+ this.name = "FileTooLargeError";
788
+ this.size = size;
789
+ this.limit = limit;
790
+ }
791
+ };
792
+ //#endregion
793
+ //#region src/pieces/activepieces/context/files.ts
794
+ function rewriteFileRefs(value, refs) {
795
+ if (refs.size === 0) return value;
796
+ if (typeof value === "string") return refs.get(value) ?? value;
797
+ if (Array.isArray(value)) return value.map((entry) => rewriteFileRefs(entry, refs));
798
+ if (typeof value === "object" && value !== null) {
799
+ const out = {};
800
+ for (const [key, entry] of Object.entries(value)) out[key] = rewriteFileRefs(entry, refs);
801
+ return out;
802
+ }
803
+ return value;
804
+ }
805
+ //#endregion
806
+ //#region src/pieces/activepieces/context/trigger.ts
807
+ function extractDedupeKey(payload) {
808
+ if (typeof payload !== "object" || payload === null) return void 0;
809
+ const value = payload[DEDUPE_KEY_PROPERTY];
810
+ return typeof value === "string" ? value : void 0;
811
+ }
812
+ //#endregion
813
+ //#region src/pieces/activepieces/worker/egress.ts
814
+ const DEFAULT_EGRESS_POLICY = {};
815
+ const IPV4_COMPATIBLE = new net.BlockList();
816
+ IPV4_COMPATIBLE.addSubnet("::", 96, "ipv6");
817
+ function familyOf(address) {
818
+ return isIP(address) === 6 ? "ipv6" : "ipv4";
819
+ }
820
+ function parseAddress(value) {
821
+ const bare = value.split("%")[0];
822
+ return isIP(bare) === 0 ? void 0 : bare;
823
+ }
824
+ originalOf(dns, "lookup");
825
+ function isPrivateAddress(value) {
826
+ const address = parseAddress(value);
827
+ if (!address) return true;
828
+ if (IPV4_COMPATIBLE.check(address, familyOf(address))) return true;
829
+ return ssrfIpClassifier.isBlockedIp({
830
+ ip: address,
831
+ allowList: []
832
+ });
833
+ }
834
+ new AsyncLocalStorage();
835
+ function originalOf(target, name) {
836
+ return Object.getOwnPropertyDescriptor(target, name)?.value;
837
+ }
838
+ const SECRET_MARKER = "[redacted:secret]";
839
+ const CIRCULAR_MARKER = "[circular]";
840
+ const TRUNCATED_MARKER = "[truncated]";
841
+ const ERROR_MAX_DEPTH = 8;
842
+ const ERROR_MAX_NODES = 1e3;
843
+ const STACK_GUARD_DEPTH = 200;
844
+ const SENSITIVE_NAMES = /* @__PURE__ */ new Set([
845
+ "auth",
846
+ "authorization",
847
+ "proxyauthorization",
848
+ "wwwauthenticate",
849
+ "authtoken",
850
+ "xauthtoken",
851
+ "apikey",
852
+ "xapikey",
853
+ "apisecret",
854
+ "bearer",
855
+ "cookie",
856
+ "setcookie",
857
+ "credential",
858
+ "credentials",
859
+ "password",
860
+ "passwd",
861
+ "passphrase",
862
+ "pwd",
863
+ "privatekey",
864
+ "secret",
865
+ "secrettext",
866
+ "sessionid",
867
+ "signature",
868
+ "token"
869
+ ]);
870
+ const SENSITIVE_SUFFIXES = [
871
+ "apikey",
872
+ "credentials",
873
+ "password",
874
+ "privatekey",
875
+ "secret",
876
+ "token"
877
+ ];
878
+ 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");
879
+ const AUTH_SCHEME = /\b(Bearer|Basic|Token)\s+([A-Za-z0-9._~+/=-]{8,})/g;
880
+ const QUERY_PARAM = /([?&])([A-Za-z0-9_.%[\]-]+)=([^&\s"'<>]+)/g;
881
+ const URL_USERINFO = /:\/\/(?<=[a-z][a-z0-9+.-]*:\/\/)([^/\s:@]+):([^/\s@]+)@/gi;
882
+ function normalizeName(name) {
883
+ return name.toLowerCase().replace(/[^a-z0-9]/g, "");
884
+ }
885
+ function isSensitiveName(name) {
886
+ const normalized = normalizeName(name);
887
+ if (!normalized) return false;
888
+ if (SENSITIVE_NAMES.has(normalized)) return true;
889
+ if (normalized.includes("signature")) return true;
890
+ return SENSITIVE_SUFFIXES.some((suffix) => normalized.length > suffix.length && normalized.endsWith(suffix));
891
+ }
892
+ function marker(name) {
893
+ return `[redacted:${name.toLowerCase()}]`;
894
+ }
895
+ function shannonEntropy(text) {
896
+ const counts = /* @__PURE__ */ new Map();
897
+ for (const char of text) counts.set(char, (counts.get(char) ?? 0) + 1);
898
+ let entropy = 0;
899
+ for (const count of counts.values()) {
900
+ const p = count / text.length;
901
+ entropy -= p * Math.log2(p);
902
+ }
903
+ return entropy;
904
+ }
905
+ const MIN_VALUE_LENGTH = 8;
906
+ const MIN_DISTINCT_CHARS = 5;
907
+ const MIN_ENTROPY_BITS = 2;
908
+ const MIN_DECLARED_LENGTH = 4;
909
+ function isRedactableValue(value) {
910
+ if (typeof value !== "string") return false;
911
+ if (value.length < MIN_VALUE_LENGTH) return false;
912
+ if (new Set(value).size < MIN_DISTINCT_CHARS) return false;
913
+ return shannonEntropy(value) >= MIN_ENTROPY_BITS;
914
+ }
915
+ function isDeclaredValue(value) {
916
+ return typeof value === "string" && value.length >= MIN_DECLARED_LENGTH;
917
+ }
918
+ const STRUCTURAL_NAMES = /* @__PURE__ */ new Set(["type", "authtype"]);
919
+ const URL_LIKE = /^[a-z][a-z0-9+.-]*:\/\//i;
920
+ function collectSecretValues(value, into = /* @__PURE__ */ new Set(), depth = 0) {
921
+ if (depth > ERROR_MAX_DEPTH) return into;
922
+ if (isRedactableValue(value)) {
923
+ if (!URL_LIKE.test(value)) into.add(value);
924
+ return into;
925
+ }
926
+ if (Array.isArray(value)) {
927
+ for (const entry of value) collectSecretValues(entry, into, depth + 1);
928
+ return into;
929
+ }
930
+ if (typeof value === "object" && value !== null) for (const [key, entry] of Object.entries(value)) {
931
+ if (STRUCTURAL_NAMES.has(normalizeName(key))) continue;
932
+ collectSecretValues(entry, into, depth + 1);
933
+ }
934
+ return into;
935
+ }
936
+ const thrownSecrets = /* @__PURE__ */ new WeakMap();
937
+ function rememberSecrets(error, values) {
938
+ if (typeof error === "object" && error !== null && values.length > 0) thrownSecrets.set(error, values);
939
+ return error;
940
+ }
941
+ function secretsFor(error) {
942
+ if (typeof error !== "object" || error === null) return [];
943
+ return thrownSecrets.get(error) ?? [];
944
+ }
945
+ function preparePass(options) {
946
+ return {
947
+ values: [...options?.values ?? []].filter(isDeclaredValue).sort((a, b) => b.length - a.length),
948
+ maxDepth: options?.maxDepth ?? STACK_GUARD_DEPTH,
949
+ maxNodes: options?.maxNodes ?? Number.POSITIVE_INFINITY,
950
+ nodes: 0
951
+ };
952
+ }
953
+ function safeDecode(name) {
954
+ try {
955
+ return decodeURIComponent(name);
956
+ } catch {
957
+ return name;
958
+ }
959
+ }
960
+ function replaceValues(text, values) {
961
+ let result = text;
962
+ for (const value of values) {
963
+ if (result.includes(value)) result = result.split(value).join(SECRET_MARKER);
964
+ const encoded = encodeURIComponent(value);
965
+ if (encoded !== value && result.includes(encoded)) result = result.split(encoded).join(SECRET_MARKER);
966
+ }
967
+ return result;
968
+ }
969
+ function redactText(text, pass) {
970
+ let result = replaceValues(text, pass.values);
971
+ result = result.replace(URL_USERINFO, (_match, user) => `://${user}:${marker("password")}@`);
972
+ result = result.replace(QUERY_PARAM, (match, sep, name) => isSensitiveName(safeDecode(name)) ? `${sep}${name}=${marker(name)}` : match);
973
+ result = result.replace(AUTH_SCHEME, (_match, scheme) => `${scheme} ${marker(scheme)}`);
974
+ return result.replace(TEXT_FIELD, (match, name, signaturePrefix, nameQuote, sep, quote, value) => value.startsWith("[redacted:") ? match : `${name}${nameQuote}${sep}${quote}${marker(`${signaturePrefix ?? ""}${name}`)}${quote}`);
975
+ }
976
+ function isScopeRevision(parent, key, entry) {
977
+ 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));
978
+ }
979
+ function walk(value, pass, depth, seen) {
980
+ if (typeof value === "string") return redactText(value, pass);
981
+ if (typeof value !== "object" || value === null) return value;
982
+ if (seen.has(value)) return CIRCULAR_MARKER;
983
+ if (depth >= pass.maxDepth) return TRUNCATED_MARKER;
984
+ if (pass.nodes >= pass.maxNodes) return TRUNCATED_MARKER;
985
+ pass.nodes += 1;
986
+ seen.add(value);
987
+ try {
988
+ if (Array.isArray(value)) return value.map((entry) => walk(entry, pass, depth + 1, seen));
989
+ const result = {};
990
+ const record = value;
991
+ 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);
992
+ return result;
993
+ } finally {
994
+ seen.delete(value);
995
+ }
996
+ }
997
+ function redact(value, options) {
998
+ return walk(value, preparePass(options), 0, /* @__PURE__ */ new Set());
999
+ }
1000
+ function redactError(value, options) {
1001
+ return redact(value, {
1002
+ maxDepth: ERROR_MAX_DEPTH,
1003
+ maxNodes: ERROR_MAX_NODES,
1004
+ ...options
1005
+ });
1006
+ }
1007
+ function containsRedactedMarker(value, depth = 0) {
1008
+ if (typeof value === "string") return value.includes("[redacted:") || value === "[truncated]";
1009
+ if (typeof value !== "object" || value === null) return false;
1010
+ if (depth >= STACK_GUARD_DEPTH) return false;
1011
+ return Object.values(value).some((entry) => containsRedactedMarker(entry, depth + 1));
1012
+ }
1013
+ function redactMessage(text, options) {
1014
+ return redactText(text, preparePass(options));
1015
+ }
1016
+ //#endregion
1017
+ //#region src/pieces/activepieces/worker/reactor-rpc.ts
1018
+ function isReactorRpcEnvelope(value, requestId) {
1019
+ if (typeof value !== "object" || value === null) return false;
1020
+ const envelope = value;
1021
+ return envelope.type === "reactor-rpc" && envelope.requestId === requestId;
1022
+ }
1023
+ function isRpcMessage(value) {
1024
+ return typeof value === "object" && value !== null && typeof value.k === "string";
1025
+ }
1026
+ function createIpcTransport(worker, requestId) {
1027
+ let closed = false;
1028
+ const detachers = /* @__PURE__ */ new Set();
1029
+ return {
1030
+ post(message) {
1031
+ if (closed || !worker.connected) return;
1032
+ worker.send({
1033
+ type: REACTOR_RPC,
1034
+ requestId,
1035
+ message
1036
+ });
1037
+ },
1038
+ onMessage(listener) {
1039
+ const handler = (value) => {
1040
+ if (closed || !isReactorRpcEnvelope(value, requestId)) return;
1041
+ if (!isRpcMessage(value.message)) return;
1042
+ listener(value.message);
1043
+ };
1044
+ worker.on("message", handler);
1045
+ const detach = () => {
1046
+ worker.off("message", handler);
1047
+ detachers.delete(detach);
1048
+ };
1049
+ detachers.add(detach);
1050
+ return detach;
1051
+ },
1052
+ close() {
1053
+ closed = true;
1054
+ for (const detach of [...detachers]) detach();
1055
+ }
1056
+ };
1057
+ }
1058
+ //#endregion
403
1059
  //#region src/pieces/activepieces/worker/transport.ts
404
1060
  function defaultEntryPath() {
405
1061
  let dir = path.dirname(fileURLToPath(import.meta.url));
@@ -412,10 +1068,50 @@ function defaultEntryPath() {
412
1068
  if (!existsSync(entry)) throw new Error(`Worker entry not built at ${entry} — run pnpm build`);
413
1069
  return entry;
414
1070
  }
1071
+ const PROFILE_EXIT_MS = 2e3;
1072
+ function workerProfileDir() {
1073
+ return process.env.PH_WORKFLOWS_WORKER_CPU_PROF_DIR || void 0;
1074
+ }
1075
+ let compileCache;
1076
+ function privateDirectory(dir) {
1077
+ const stat = lstatSync(dir);
1078
+ if (!stat.isDirectory()) return false;
1079
+ if (typeof process.getuid !== "function") return true;
1080
+ return stat.uid === process.getuid() && (stat.mode & 63) === 0;
1081
+ }
1082
+ function writable(file) {
1083
+ try {
1084
+ accessSync(file, constants.W_OK);
1085
+ return true;
1086
+ } catch {
1087
+ return false;
1088
+ }
1089
+ }
1090
+ function workerCompileCache(entryPath) {
1091
+ if (compileCache === void 0) {
1092
+ const configured = process.env.PH_WORKFLOWS_WORKER_COMPILE_CACHE;
1093
+ try {
1094
+ if (configured === "off") throw new Error("disabled");
1095
+ if (!configured && !writable(entryPath)) throw new Error("read-only");
1096
+ const base = configured ? void 0 : path.join(tmpdir(), `ph-workflows-${userInfo().uid}`);
1097
+ const dir = configured || path.join(base, "compile-cache");
1098
+ mkdirSync(dir, {
1099
+ recursive: true,
1100
+ mode: 448
1101
+ });
1102
+ compileCache = privateDirectory(dir) && (!base || privateDirectory(base)) ? dir : null;
1103
+ } catch {
1104
+ compileCache = null;
1105
+ }
1106
+ }
1107
+ return compileCache ?? void 0;
1108
+ }
415
1109
  function createForkTransport(entryPath) {
1110
+ const profileDir = workerProfileDir();
1111
+ const cacheDir = workerCompileCache(entryPath);
416
1112
  const child = fork(entryPath, [], {
417
- env: {},
418
- execArgv: [],
1113
+ env: cacheDir ? { NODE_COMPILE_CACHE: cacheDir } : {},
1114
+ execArgv: profileDir ? ["--cpu-prof", `--cpu-prof-dir=${profileDir}`] : [],
419
1115
  serialization: "advanced",
420
1116
  stdio: [
421
1117
  "ignore",
@@ -456,11 +1152,25 @@ function createForkTransport(entryPath) {
456
1152
  child.off("message", listener);
457
1153
  },
458
1154
  kill() {
459
- child.kill("SIGKILL");
1155
+ if (!profileDir) {
1156
+ child.kill("SIGKILL");
1157
+ return;
1158
+ }
1159
+ child.kill("SIGTERM");
1160
+ const force = setTimeout(() => child.kill("SIGKILL"), PROFILE_EXIT_MS);
1161
+ force.unref();
1162
+ child.once("exit", () => clearTimeout(force));
460
1163
  }
461
1164
  };
462
1165
  }
463
1166
  //#endregion
1167
+ //#region src/pieces/activepieces/worker/timings.ts
1168
+ function epochNow() {
1169
+ return performance.timeOrigin + performance.now();
1170
+ }
1171
+ new AsyncLocalStorage();
1172
+ new AsyncLocalStorage();
1173
+ //#endregion
464
1174
  //#region src/pieces/activepieces/worker/host.ts
465
1175
  var PieceWorkerError = class extends Error {
466
1176
  serialized;
@@ -494,7 +1204,9 @@ var PieceWorker = class {
494
1204
  defaultTimeoutMs;
495
1205
  hostCallTimeoutMs;
496
1206
  models;
1207
+ telemetry;
497
1208
  worker;
1209
+ forkedAt = 0;
498
1210
  queue = Promise.resolve();
499
1211
  nextId = 1;
500
1212
  constructor(options = {}) {
@@ -503,6 +1215,7 @@ var PieceWorker = class {
503
1215
  this.defaultTimeoutMs = options.defaultTimeoutMs ?? 3e4;
504
1216
  this.hostCallTimeoutMs = options.hostCallTimeoutMs;
505
1217
  this.models = options.models;
1218
+ this.telemetry = options.telemetry;
506
1219
  }
507
1220
  runAction(request, options = {}) {
508
1221
  return this.enqueue("run", request, options.timeoutMs, options);
@@ -520,7 +1233,13 @@ var PieceWorker = class {
520
1233
  return this.enqueue("trigger-hook", request, options.timeoutMs, options);
521
1234
  }
522
1235
  enqueue(type, request, timeoutMs, taps = {}) {
523
- const run = this.queue.then(() => this.execute(type, request, timeoutMs ?? this.defaultTimeoutMs, taps));
1236
+ const run = this.queue.then(() => {
1237
+ const execute = (onTimings) => this.execute(type, request, timeoutMs ?? this.defaultTimeoutMs, taps, onTimings);
1238
+ const telemetry = this.telemetry;
1239
+ if (!telemetry) return execute();
1240
+ const cold = !this.worker;
1241
+ return telemetry.phase(`worker.${type}${cold ? ".cold" : ""}`, { "worker.cold": cold }, (span) => execute((timings, ipc) => telemetry.workerTimings(span, timings, this.forkedAt, ipc)));
1242
+ });
524
1243
  this.queue = run.catch(() => void 0);
525
1244
  return run;
526
1245
  }
@@ -530,6 +1249,7 @@ var PieceWorker = class {
530
1249
  }
531
1250
  spawn() {
532
1251
  if (this.worker) return this.worker;
1252
+ this.forkedAt = epochNow();
533
1253
  const worker = this.connect();
534
1254
  this.sendModels(worker);
535
1255
  const forget = () => {
@@ -577,9 +1297,11 @@ var PieceWorker = class {
577
1297
  }
578
1298
  };
579
1299
  }
580
- execute(type, request, timeoutMs, taps) {
1300
+ execute(type, request, timeoutMs, taps, onTimings) {
581
1301
  const worker = this.spawn();
582
1302
  const id = this.nextId++;
1303
+ const hostCallTimeoutMs = this.hostCallTimeoutMs ?? taps.hostCallTimeoutMs ?? hostCallTimeoutForStep(timeoutMs);
1304
+ let sentAt = 0;
583
1305
  return new Promise((resolve, reject) => {
584
1306
  const deadline = Date.now() + timeoutMs;
585
1307
  let reactor;
@@ -600,6 +1322,7 @@ var PieceWorker = class {
600
1322
  if (response.type === "host-call" || response.type === "host-notify" || response.type === "reactor-rpc") return;
601
1323
  if (response.id !== id) return;
602
1324
  cleanup();
1325
+ const received = epochNow();
603
1326
  if (response.type === "result") resolve({
604
1327
  output: response.output,
605
1328
  touched: response.touched,
@@ -610,6 +1333,12 @@ var PieceWorker = class {
610
1333
  listeners: response.listeners
611
1334
  });
612
1335
  else reject(new PieceWorkerError(response.error));
1336
+ if (response.timings && onTimings) try {
1337
+ onTimings(response.timings, {
1338
+ sent: sentAt,
1339
+ received
1340
+ });
1341
+ } catch {}
613
1342
  };
614
1343
  const onExit = ({ code, signal }) => {
615
1344
  cleanup();
@@ -634,13 +1363,14 @@ var PieceWorker = class {
634
1363
  worker.on("message", onMessage);
635
1364
  worker.on("message", onHostCall);
636
1365
  worker.on("exit", onExit);
1366
+ sentAt = epochNow();
637
1367
  worker.send(jsonSafe({
638
1368
  id,
639
1369
  type,
640
1370
  request: {
641
1371
  maxFileBytes: configuredMaxFileBytes(),
642
1372
  deadline,
643
- ...this.hostCallTimeoutMs ? { hostCallTimeoutMs: this.hostCallTimeoutMs } : {},
1373
+ ...hostCallTimeoutMs ? { hostCallTimeoutMs } : {},
644
1374
  ...request,
645
1375
  ...reactor ? { reactor: reactor.binding } : {}
646
1376
  }
@@ -730,7 +1460,8 @@ var PieceWorkerSession = class {
730
1460
  return this.taking;
731
1461
  }
732
1462
  async acquire() {
733
- await this.pool.acquire(this);
1463
+ const { telemetry } = this.pool;
1464
+ await (telemetry ? telemetry.phase("worker.acquire", {}, () => this.pool.acquire(this)) : this.pool.acquire(this));
734
1465
  this.held = true;
735
1466
  if (this.closed) {
736
1467
  this.giveBack();
@@ -747,6 +1478,8 @@ var PieceWorkerSession = class {
747
1478
  };
748
1479
  var PieceWorkerPool = class {
749
1480
  size;
1481
+ /** @internal */
1482
+ telemetry;
750
1483
  maxQueueDepth;
751
1484
  createWorker;
752
1485
  workerOptions;
@@ -760,6 +1493,7 @@ var PieceWorkerPool = class {
760
1493
  this.maxQueueDepth = Math.max(0, Math.trunc(maxQueueDepth ?? 0));
761
1494
  this.createWorker = createWorker ?? ((o) => new PieceWorker(o));
762
1495
  this.workerOptions = workerOptions;
1496
+ this.telemetry = workerOptions.telemetry;
763
1497
  }
764
1498
  session() {
765
1499
  if (this.disposed) throw new Error("Piece worker pool is disposed");
@@ -899,6 +1633,65 @@ var UnresolvedReferenceError = class extends Error {
899
1633
  this.name = "UnresolvedReferenceError";
900
1634
  }
901
1635
  };
1636
+ /**
1637
+ * A value that exists but cannot be read, with the reason attached.
1638
+ *
1639
+ * The case it was written for: a rerun replaying a SUCCEEDED step whose
1640
+ * journaled output the payload cap truncated. The step must NOT run again — it
1641
+ * had side effects — but its output is genuinely gone, so a downstream step
1642
+ * that reads it has to be told, by name, rather than handed a truncation
1643
+ * marker or quietly made to re-run the step that produced it.
1644
+ *
1645
+ * The reason hangs off a SYMBOL key, so `JSON.stringify` drops it and nothing
1646
+ * can leak the wrapper into a payload as data.
1647
+ */
1648
+ const UNAVAILABLE = Symbol("unavailable");
1649
+ var UnavailableValueError = class extends Error {
1650
+ constructor(reason) {
1651
+ super(reason);
1652
+ this.name = "UnavailableValueError";
1653
+ }
1654
+ };
1655
+ function unavailableValue(reason) {
1656
+ return { [UNAVAILABLE]: reason };
1657
+ }
1658
+ function unavailableReason(value) {
1659
+ if (typeof value !== "object" || value === null) return void 0;
1660
+ const reason = value[UNAVAILABLE];
1661
+ return typeof reason === "string" ? reason : void 0;
1662
+ }
1663
+ /**
1664
+ * The reason of the first {@link unavailableValue} anywhere inside a value,
1665
+ * however deep, or undefined when there is none.
1666
+ *
1667
+ * A shallow check is not enough, and that was the hole: `{{steps.charge}}`
1668
+ * lands on the step's whole entry rather than on the wrapper, so the wrapper
1669
+ * sits one level down as `{ output: <wrapper> }`. The path check saw an
1670
+ * ordinary object, handed it over as data, and `JSON.stringify` then dropped
1671
+ * the symbol on the way to the piece — the downstream step received `{}` and
1672
+ * the reason was gone. Refusing by name is the whole point of the wrapper.
1673
+ */
1674
+ function unavailableValueReason(value) {
1675
+ const own = unavailableReason(value);
1676
+ if (own !== void 0) return own;
1677
+ if (Array.isArray(value)) {
1678
+ for (const item of value) {
1679
+ const nested = unavailableValueReason(item);
1680
+ if (nested !== void 0) return nested;
1681
+ }
1682
+ return;
1683
+ }
1684
+ if (typeof value === "object" && value !== null) for (const entry of Object.values(value)) {
1685
+ const nested = unavailableValueReason(entry);
1686
+ if (nested !== void 0) return nested;
1687
+ }
1688
+ }
1689
+ /** Throws {@link UnavailableValueError} when a resolved value holds an
1690
+ * unavailable wrapper at any depth; the gate every resolved input passes. */
1691
+ function assertNoUnavailableValue(value) {
1692
+ const reason = unavailableValueReason(value);
1693
+ if (reason !== void 0) throw new UnavailableValueError(reason);
1694
+ }
902
1695
  const IDENT = /[A-Za-z0-9_$-]/;
903
1696
  function parseExpression(source) {
904
1697
  let i = 0;
@@ -1042,10 +1835,13 @@ function lookupPath(scope, segments) {
1042
1835
  let current = scope;
1043
1836
  for (const segment of segments) {
1044
1837
  if (current === null || typeof current !== "object") return MISSING;
1838
+ const blocked = unavailableReason(current);
1839
+ if (blocked !== void 0) throw new UnavailableValueError(blocked);
1045
1840
  const record = current;
1046
1841
  if (!Object.prototype.hasOwnProperty.call(record, segment)) return MISSING;
1047
1842
  current = record[segment];
1048
1843
  }
1844
+ assertNoUnavailableValue(current);
1049
1845
  return current === void 0 ? MISSING : current;
1050
1846
  }
1051
1847
  const isEmpty = (value) => value === null || value === "";
@@ -1230,7 +2026,22 @@ function evaluateBranch(config) {
1230
2026
  }
1231
2027
  //#endregion
1232
2028
  //#region src/pieces/engine/step-input.ts
2029
+ /**
2030
+ * A step's resolved input, and the last gate before it crosses to the piece.
2031
+ *
2032
+ * The unavailable wrapper carries its reason on a symbol, so anything that
2033
+ * serializes it loses the reason — the worker boundary included. Checking the
2034
+ * whole resolved input here, at any depth, is what guarantees that no route
2035
+ * through resolution can hand a piece the wrapper as data: the path checks
2036
+ * inside `lookupPath` catch the ways an expression reaches one, and this
2037
+ * catches whatever they do not.
2038
+ */
1233
2039
  function resolveStepInput(step, scope) {
2040
+ const input = resolveStepInputRaw(step, scope);
2041
+ assertNoUnavailableValue(input);
2042
+ return input;
2043
+ }
2044
+ function resolveStepInputRaw(step, scope) {
1234
2045
  const { config } = step;
1235
2046
  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
2047
  const record = config;
@@ -1416,6 +2227,81 @@ var BoundConnectionResolver = class {
1416
2227
  }
1417
2228
  };
1418
2229
  //#endregion
2230
+ //#region src/pieces/engine/attachment-cache.ts
2231
+ const META = ".json";
2232
+ var AttachmentCache = class {
2233
+ options;
2234
+ constructor(options) {
2235
+ this.options = options;
2236
+ }
2237
+ entry(ref) {
2238
+ const match = /^attachment:\/\/(v\d+):([a-f0-9]{64})$/i.exec(ref);
2239
+ if (!match) throw new Error(`Not a content-addressed reference: ${ref}`);
2240
+ return path.join(this.options.dir, `${match[1]}-${match[2].toLowerCase()}`);
2241
+ }
2242
+ static cacheable(ref) {
2243
+ return /^attachment:\/\/v\d+:[a-f0-9]{64}$/i.test(ref);
2244
+ }
2245
+ async get(ref, destPath) {
2246
+ const file = this.entry(ref);
2247
+ let meta;
2248
+ try {
2249
+ meta = JSON.parse(await readFile(file + META, "utf8"));
2250
+ if ((await stat(file)).size !== meta.size) {
2251
+ await this.remove(file);
2252
+ return;
2253
+ }
2254
+ } catch {
2255
+ return;
2256
+ }
2257
+ await copyFile(file, destPath, constants.COPYFILE_FICLONE);
2258
+ const now = /* @__PURE__ */ new Date();
2259
+ await utimes(file, now, now).catch(() => void 0);
2260
+ return meta;
2261
+ }
2262
+ async put(ref, srcPath, meta) {
2263
+ if (this.options.maxBytes <= 0) return;
2264
+ const file = this.entry(ref);
2265
+ const size = (await stat(srcPath)).size;
2266
+ if (size > this.options.maxBytes) return;
2267
+ await mkdir(this.options.dir, { recursive: true });
2268
+ const tmp = `${file}.${randomUUID()}.tmp`;
2269
+ try {
2270
+ await copyFile(srcPath, tmp, constants.COPYFILE_FICLONE);
2271
+ await writeFile(file + META, JSON.stringify({
2272
+ ...meta,
2273
+ size
2274
+ }));
2275
+ await rename(tmp, file);
2276
+ } catch (error) {
2277
+ await rm(tmp, { force: true });
2278
+ throw error;
2279
+ }
2280
+ await this.evict();
2281
+ }
2282
+ async remove(file) {
2283
+ await rm(file, { force: true });
2284
+ await rm(file + META, { force: true });
2285
+ }
2286
+ async evict() {
2287
+ const names = await readdir(this.options.dir).catch(() => []);
2288
+ const live = (await Promise.all(names.filter((name) => !name.endsWith(META) && !name.endsWith(".tmp")).map(async (name) => {
2289
+ const file = path.join(this.options.dir, name);
2290
+ const info = await stat(file).catch(() => void 0);
2291
+ return info ? {
2292
+ file,
2293
+ size: info.size,
2294
+ used: info.mtimeMs
2295
+ } : void 0;
2296
+ }))).filter((e) => e !== void 0).sort((a, b) => b.used - a.used);
2297
+ let total = 0;
2298
+ for (const entry of live) {
2299
+ total += entry.size;
2300
+ if (total > this.options.maxBytes) await this.remove(entry.file);
2301
+ }
2302
+ }
2303
+ };
2304
+ //#endregion
1419
2305
  //#region src/pieces/core/hints.ts
1420
2306
  function withHints(property, hints) {
1421
2307
  return {
@@ -1746,7 +2632,7 @@ const manualTrigger = createTrigger({
1746
2632
  });
1747
2633
  //#endregion
1748
2634
  //#region src/pieces/core/version.ts
1749
- const CORE_PIECE_VERSION = "6.2.3-dev.48";
2635
+ const CORE_PIECE_VERSION = "6.2.3-dev.50";
1750
2636
  //#endregion
1751
2637
  //#region src/pieces/core/index.ts
1752
2638
  const corePiece = createPiece({
@@ -1961,7 +2847,7 @@ var ActivepiecesBlockExecutor = class {
1961
2847
  const worker = supplied();
1962
2848
  if (worker) return worker;
1963
2849
  } else if (supplied) return supplied;
1964
- return this.own ??= new PieceWorker();
2850
+ return this.own ??= new PieceWorker({ telemetry: this.options.telemetry });
1965
2851
  }
1966
2852
  async execute(execution) {
1967
2853
  const resolution = this.options.resolveBlock ? await this.options.resolveBlock(execution.block) : pinnedResolution(execution.block);
@@ -1983,11 +2869,13 @@ var ActivepiecesBlockExecutor = class {
1983
2869
  const runSecrets = execution.redactValues ?? [];
1984
2870
  let redactValues = [...runSecrets];
1985
2871
  try {
1986
- const piece = await this.resolver.resolve({
2872
+ const ref = {
1987
2873
  name: parsed.packageName,
1988
2874
  version: parsed.version,
1989
2875
  ...parsed.source ? { source: parsed.source } : {}
1990
- });
2876
+ };
2877
+ const telemetry = this.options.telemetry;
2878
+ const piece = await (telemetry ? telemetry.phase("piece.resolve", {}, () => this.resolver.resolve(ref)) : this.resolver.resolve(ref));
1991
2879
  const connection = await this.resolveConnection(execution.connectionId, {
1992
2880
  piecePackage: parsed.packageName,
1993
2881
  stepId: execution.step.id,
@@ -1996,7 +2884,10 @@ var ActivepiecesBlockExecutor = class {
1996
2884
  const auth = connection?.auth;
1997
2885
  redactValues = [...runSecrets, ...connection?.secretValues ?? []];
1998
2886
  const timeoutMs = execution.step.timeoutSeconds ? execution.step.timeoutSeconds * 1e3 : this.options.defaultTimeoutMs;
1999
- const stagedInputs = await this.stageInputs(execution.config, stagingDir);
2887
+ const deadline = timeoutMs ? Date.now() + timeoutMs : void 0;
2888
+ const signal = deadline ? AbortSignal.timeout(timeoutMs) : void 0;
2889
+ const stagedInputs = await this.stageInputs(execution.config, stagingDir, signal);
2890
+ const remainingMs = deadline ? Math.max(1, deadline - Date.now()) : void 0;
2000
2891
  const pieceStore = this.options.pieceStore;
2001
2892
  const notifications = stepTaps(this.options, execution, redactValues);
2002
2893
  const egress = this.options.egress === void 0 ? DEFAULT_EGRESS_POLICY : this.options.egress;
@@ -2022,13 +2913,14 @@ var ActivepiecesBlockExecutor = class {
2022
2913
  ...this.options.onPartialOutput ? { liveOutput: true } : {},
2023
2914
  ...egress ? { egress } : {}
2024
2915
  }, {
2025
- ...timeoutMs ? { timeoutMs } : {},
2916
+ ...remainingMs ? { timeoutMs: remainingMs } : {},
2917
+ hostCallTimeoutMs: hostCallTimeoutForStep(timeoutMs),
2026
2918
  ...pieceStore ? { hostCalls: storeHandlers(pieceStore) } : {},
2027
2919
  ...notifications ? { notifications } : {},
2028
2920
  ...reactorTap ? { reactor: reactorTap } : {}
2029
2921
  });
2030
2922
  return {
2031
- output: await this.ingestFiles(result.output, result.files),
2923
+ output: await this.ingestFiles(result.output, result.files, stagingDir, signal),
2032
2924
  redactValues
2033
2925
  };
2034
2926
  } catch (error) {
@@ -2050,7 +2942,7 @@ var ActivepiecesBlockExecutor = class {
2050
2942
  secretValues: [...collectSecretValues(auth)]
2051
2943
  };
2052
2944
  }
2053
- async stageInputs(config, stagingDir) {
2945
+ async stageInputs(config, stagingDir, signal) {
2054
2946
  const port = this.options.attachments;
2055
2947
  if (!stagingDir || !port) return void 0;
2056
2948
  const refs = /* @__PURE__ */ new Set();
@@ -2062,29 +2954,63 @@ var ActivepiecesBlockExecutor = class {
2062
2954
  for (const ref of refs) {
2063
2955
  const destPath = path.join(stagingDir, `in-${index++}`);
2064
2956
  try {
2065
- const meta = await port.read(ref, destPath);
2957
+ const meta = await this.readAttachment(port, ref, destPath, signal);
2066
2958
  staged.push({
2067
2959
  ref,
2068
2960
  path: destPath,
2069
2961
  ...meta
2070
2962
  });
2071
2963
  } catch (error) {
2964
+ if (signal?.aborted) throw error;
2965
+ staged.push({
2966
+ ref,
2967
+ path: "",
2968
+ error: error instanceof Error ? error.message : String(error)
2969
+ });
2072
2970
  logger$1.debug("Left @ref unstaged: @error", ref, error instanceof Error ? error.message : String(error));
2073
2971
  }
2074
2972
  }
2075
2973
  return staged;
2076
2974
  }
2077
- async ingestFiles(output, files) {
2975
+ async readAttachment(port, ref, destPath, signal) {
2976
+ const cache = this.options.attachmentCache;
2977
+ if (!cache || !port.authorize || !AttachmentCache.cacheable(ref)) return port.read(ref, destPath, signal);
2978
+ await port.authorize(ref);
2979
+ const hit = await cache.get(ref, destPath).catch(() => void 0);
2980
+ if (hit) return {
2981
+ ...hit.fileName !== void 0 ? { fileName: hit.fileName } : {},
2982
+ ...hit.contentType !== void 0 ? { contentType: hit.contentType } : {}
2983
+ };
2984
+ const meta = await port.read(ref, destPath, signal);
2985
+ await cache.put(ref, destPath, meta).catch((error) => {
2986
+ logger$1.debug("Could not cache @ref: @error", ref, error instanceof Error ? error.message : String(error));
2987
+ });
2988
+ return meta;
2989
+ }
2990
+ async checkStagedFile(file, stagingDir) {
2991
+ if (!stagingDir) throw new Error(`Refusing to ingest "${file.fileName}": no staging directory`);
2992
+ const root = await realpath(stagingDir);
2993
+ const resolved = path.resolve(file.path);
2994
+ const info = await lstat(resolved).catch(() => void 0);
2995
+ 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`);
2996
+ const limit = maxFileBytes();
2997
+ if (info.size > limit) throw new FileTooLargeError(info.size, limit);
2998
+ return info.size;
2999
+ }
3000
+ async ingestFiles(output, files, stagingDir, signal) {
2078
3001
  if (!files || files.length === 0) return output;
2079
3002
  const port = this.options.attachments;
2080
3003
  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
3004
  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
- }));
3005
+ for (const file of files) {
3006
+ const size = await this.checkStagedFile(file, stagingDir);
3007
+ refs.set(file.token, await port.write({
3008
+ path: path.resolve(file.path),
3009
+ fileName: file.fileName,
3010
+ size,
3011
+ contentType: file.contentType
3012
+ }, signal));
3013
+ }
2088
3014
  return rewriteFileRefs(output, refs);
2089
3015
  }
2090
3016
  dispose() {
@@ -2105,6 +3031,64 @@ var CompositeBlockExecutor = class {
2105
3031
  return this.pieces.execute(execution);
2106
3032
  }
2107
3033
  };
3034
+ /** The ceiling on one backoff wait, whatever `maxDelaySeconds` says: the wait
3035
+ * holds the run's worker slot, and the run deadline bounds the rest. */
3036
+ const MAX_RETRY_DELAY_MS = 3e5;
3037
+ function positive(value) {
3038
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : null;
3039
+ }
3040
+ /** Resolves an author-written retry block; null when it asks for one attempt,
3041
+ * which is the same thing as no retry policy and saves every reader a branch. */
3042
+ function effectiveRetryPolicy(retry) {
3043
+ if (typeof retry !== "object" || retry === null) return null;
3044
+ const record = retry;
3045
+ const asked = positive(record.maxAttempts) ?? 1;
3046
+ const maxAttempts = Math.min(Math.trunc(asked), 10);
3047
+ if (maxAttempts <= 1) return null;
3048
+ const initial = positive(record.initialDelaySeconds) ?? 0;
3049
+ const max = positive(record.maxDelaySeconds) ?? initial;
3050
+ return {
3051
+ maxAttempts,
3052
+ backoff: record.backoff === "EXPONENTIAL" ? "EXPONENTIAL" : "FIXED",
3053
+ initialDelaySeconds: initial,
3054
+ maxDelaySeconds: Math.max(initial, max),
3055
+ retryOn: Array.isArray(record.retryOn) ? record.retryOn.filter((entry) => typeof entry === "string") : []
3056
+ };
3057
+ }
3058
+ /** How long to wait before `attempt` (1-based: attempt 2 is the first retry).
3059
+ * EXPONENTIAL doubles from the initial delay, capped both by the author's
3060
+ * `maxDelaySeconds` and by {@link MAX_RETRY_DELAY_MS}. */
3061
+ function retryDelayMs(retry, attempt) {
3062
+ const initial = retry.initialDelaySeconds * 1e3;
3063
+ const raw = retry.backoff === "EXPONENTIAL" ? initial * 2 ** Math.max(0, attempt - 2) : initial;
3064
+ return Math.max(0, Math.min(raw, retry.maxDelaySeconds * 1e3, MAX_RETRY_DELAY_MS));
3065
+ }
3066
+ /**
3067
+ * Whether `retryOn` admits this error.
3068
+ *
3069
+ * The schema calls its entries "error classes", which in a workflow is a loose
3070
+ * notion: a piece throws whatever it likes, and what reaches here may be an
3071
+ * `Error` subclass of the engine's, an HTTP failure formatted by the piece
3072
+ * framework, or a string. So an entry matches the error's CLASS NAME exactly,
3073
+ * or appears anywhere in its message — case-insensitively in both cases. That
3074
+ * makes `["HostCallTimeoutError"]` and `["429"]` both work, which is what an
3075
+ * author writing one of these actually means.
3076
+ *
3077
+ * An EMPTY list admits everything. The schema's own reading ("everything else
3078
+ * fails terminally on attempt 1") would make the shipped default — an empty
3079
+ * `retryOn` — turn every `maxAttempts` into a lie, so empty is read as
3080
+ * "unrestricted" and a non-empty list as the filter it is.
3081
+ */
3082
+ function isRetryableError(retry, error) {
3083
+ if (retry.retryOn.length === 0) return true;
3084
+ const name = (error instanceof Error ? error.name : "").toLowerCase();
3085
+ const message = (error instanceof Error ? error.message : String(error)).toLowerCase();
3086
+ return retry.retryOn.some((entry) => {
3087
+ const needle = entry.trim().toLowerCase();
3088
+ if (needle === "") return false;
3089
+ return needle === name || message.includes(needle);
3090
+ });
3091
+ }
2108
3092
  //#endregion
2109
3093
  //#region src/pieces/engine/dynamic-props.ts
2110
3094
  var DynamicPropertiesError = class extends Error {
@@ -2208,6 +3192,11 @@ function errorNameOf(error) {
2208
3192
  function journaled(value, values) {
2209
3193
  return value === void 0 ? void 0 : redact(value, { values });
2210
3194
  }
3195
+ /** What a run that passed `policy.runTimeoutSeconds` ends CANCELLED with. */
3196
+ /** The error name a run its deadline cancelled is journaled with; it is what
3197
+ * marks the run as resumable, unlike a firing that was refused. */
3198
+ const RUN_DEADLINE_ERROR_NAME = "RunDeadlineExceeded";
3199
+ const RUN_DEADLINE_REASON = "Run exceeded its runTimeoutSeconds and was cancelled";
2211
3200
  async function runWorkflow(options) {
2212
3201
  const { definition, executor } = options;
2213
3202
  const runSecrets = options.redactValues ?? [];
@@ -2219,8 +3208,17 @@ async function runWorkflow(options) {
2219
3208
  const records = /* @__PURE__ */ new Map();
2220
3209
  const edgeDecisions = /* @__PURE__ */ new Map();
2221
3210
  let runFailed;
3211
+ let runCancelled;
2222
3212
  let runFailedName;
2223
3213
  let executedCount = 0;
3214
+ const sleep = options.sleep ?? ((ms) => new Promise((resolve) => {
3215
+ setTimeout(resolve, ms).unref();
3216
+ }));
3217
+ const outOfTime = () => {
3218
+ if (options.deadline === void 0) return void 0;
3219
+ if (Date.now() < options.deadline) return void 0;
3220
+ return RUN_DEADLINE_REASON;
3221
+ };
2224
3222
  const journal = async (record) => {
2225
3223
  if (!options.onStep) return;
2226
3224
  const ordinal = executedCount++;
@@ -2293,114 +3291,218 @@ async function runWorkflow(options) {
2293
3291
  return;
2294
3292
  }
2295
3293
  const port = replay.port ?? "next";
3294
+ 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
3295
  const record = {
2297
3296
  stepId: step.id,
2298
3297
  key: step.key,
2299
3298
  pieceName: step.pieceName,
2300
3299
  blockName: step.actionName,
2301
3300
  status: "REPLAYED",
2302
- output: replay.output,
3301
+ output: replay.outputTruncated ? void 0 : replay.output,
3302
+ ...replay.outputTruncated && replay.truncatedOutput !== void 0 ? { journaledOutput: replay.truncatedOutput } : {},
2303
3303
  port,
2304
3304
  configHash: stepConfigHash(step)
2305
3305
  };
2306
3306
  records.set(step.id, record);
2307
3307
  await journal(record);
2308
- scope.steps[step.key] = { output: replay.output };
3308
+ scope.steps[step.key] = { output };
2309
3309
  decideOutgoing(step.id, port);
2310
3310
  return;
2311
3311
  }
3312
+ const retry = step.retry === void 0 || step.retry === null ? options.defaultRetry ?? null : effectiveRetryPolicy(step.retry);
3313
+ const maxAttempts = retry?.maxAttempts ?? 1;
2312
3314
  const startedAt = (/* @__PURE__ */ new Date()).toISOString();
2313
3315
  let input;
2314
3316
  try {
2315
3317
  input = resolveStepInput(step, scope);
2316
3318
  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
3319
  } 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 } : {},
3320
+ await endStep(step, {
3321
+ error,
3322
+ input,
2358
3323
  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;
3324
+ attempt: 1
3325
+ });
3326
+ return;
3327
+ }
3328
+ for (let attempt = 1;; attempt++) {
3329
+ const attempts = attempt > 1 ? { attempts: attempt } : {};
3330
+ try {
3331
+ const result = await executor.execute({
3332
+ block: stepBlock(step),
3333
+ config: input,
3334
+ connectionId: step.connectionId,
3335
+ reactorConnectionId: step.reactorConnectionId,
3336
+ step,
3337
+ ...runSecrets.length > 0 ? { redactValues: runSecrets } : {}
3338
+ });
3339
+ const port = result.port ?? "next";
3340
+ const values = [...runSecrets, ...result.redactValues ?? []];
3341
+ const record = {
3342
+ stepId: step.id,
3343
+ key: step.key,
3344
+ pieceName: step.pieceName,
3345
+ blockName: step.actionName,
3346
+ status: "SUCCEEDED",
3347
+ input: journaled(input, values),
3348
+ output: journaled(referenceDocuments(result.output), values),
3349
+ port,
3350
+ startedAt,
3351
+ endedAt: (/* @__PURE__ */ new Date()).toISOString(),
3352
+ ...withPiece(pieceRecord(result.resolution)),
3353
+ configHash: stepConfigHash(step),
3354
+ ...attempts
3355
+ };
3356
+ records.set(step.id, record);
3357
+ await journal(record);
3358
+ scope.steps[step.key] = { output: result.output };
3359
+ decideOutgoing(step.id, port);
3360
+ return;
3361
+ } catch (error) {
3362
+ const indeterminate = isIndeterminateError(error);
3363
+ if (retry !== null && attempt < maxAttempts && !indeterminate && !(error instanceof UnavailableValueError) && isRetryableError(retry, error)) {
3364
+ const stopped = await waitForRetry(retryDelayMs(retry, attempt + 1));
3365
+ if (stopped !== void 0) {
3366
+ await endStep(step, {
3367
+ error,
3368
+ input,
3369
+ startedAt,
3370
+ attempt,
3371
+ cancelled: stopped
3372
+ });
3373
+ return;
3374
+ }
3375
+ continue;
3376
+ }
3377
+ await endStep(step, {
3378
+ error,
3379
+ input,
3380
+ startedAt,
3381
+ attempt
3382
+ });
3383
+ return;
2370
3384
  }
2371
3385
  }
2372
3386
  };
3387
+ /**
3388
+ * A retry wait, clipped to the run's deadline.
3389
+ *
3390
+ * Undefined when there is room for both the wait and another attempt.
3391
+ * Otherwise the reason the run is over: the wait is served out only as far
3392
+ * as the deadline, never past it, and the caller runs no further attempt.
3393
+ * Sleeping the full backoff and leaving the deadline to "the next attempt's
3394
+ * own check" meant that attempt ran its side effect after the run had
3395
+ * expired, and the run then ended FAILED rather than CANCELLED.
3396
+ */
3397
+ async function waitForRetry(waitMs) {
3398
+ const expired = outOfTime();
3399
+ if (expired !== void 0) return expired;
3400
+ if (options.deadline === void 0) {
3401
+ if (waitMs > 0) await sleep(waitMs);
3402
+ return;
3403
+ }
3404
+ const remaining = options.deadline - Date.now();
3405
+ if (waitMs >= remaining) {
3406
+ if (remaining > 0) await sleep(remaining);
3407
+ return RUN_DEADLINE_REASON;
3408
+ }
3409
+ if (waitMs > 0) await sleep(waitMs);
3410
+ return outOfTime();
3411
+ }
3412
+ /**
3413
+ * The terminal end of a step that threw: its record, its error port, and
3414
+ * what that means for the run.
3415
+ *
3416
+ * One place for it, because three paths reach it — a deterministic
3417
+ * resolution failure before the first attempt, an exhausted or refused
3418
+ * retry, and a retry the run's deadline cut short.
3419
+ */
3420
+ async function endStep(step, outcome) {
3421
+ const { error, input, startedAt, attempt } = outcome;
3422
+ const values = [...runSecrets, ...secretsFor(error)];
3423
+ const detail = redactMessage(errorMessage(error), { values });
3424
+ const indeterminate = isIndeterminateError(error);
3425
+ const errorName = errorNameOf(error);
3426
+ const record = {
3427
+ stepId: step.id,
3428
+ key: step.key,
3429
+ pieceName: step.pieceName,
3430
+ blockName: step.actionName,
3431
+ status: indeterminate ? "INDETERMINATE" : "FAILED",
3432
+ input: journaled(input, values),
3433
+ error: detail,
3434
+ ...errorName ? { errorName } : {},
3435
+ startedAt,
3436
+ endedAt: (/* @__PURE__ */ new Date()).toISOString(),
3437
+ ...withPiece(pieceRecord(resolutionOf(error))),
3438
+ configHash: stepConfigHash(step),
3439
+ ...attempt > 1 ? { attempts: attempt } : {}
3440
+ };
3441
+ records.set(step.id, record);
3442
+ await journal(record);
3443
+ if (indeterminate) {
3444
+ runFailed = `Step "${step.key}" is INDETERMINATE: ${detail}`;
3445
+ runFailedName = errorName;
3446
+ return;
3447
+ }
3448
+ if (outcome.cancelled !== void 0) {
3449
+ runCancelled = outcome.cancelled;
3450
+ return;
3451
+ }
3452
+ scope.steps[step.key] = { error: detail };
3453
+ decideOutgoing(step.id, "error");
3454
+ if (!definition.edges.some((edge) => edge.from === step.id && edgeDecisions.get(edge.id))) {
3455
+ const failedAfter = attempt > 1 ? ` after ${attempt} attempts` : "";
3456
+ runFailed = `Step "${step.key}" failed${failedAfter}: ${detail}`;
3457
+ runFailedName = errorName;
3458
+ }
3459
+ }
3460
+ const expiredBeforeStep = () => {
3461
+ runCancelled ??= outOfTime();
3462
+ return runCancelled !== void 0;
3463
+ };
2373
3464
  let progressed = true;
2374
- while (progressed && !runFailed) {
3465
+ while (progressed && !runFailed && !runCancelled) {
2375
3466
  progressed = false;
2376
3467
  for (const step of definition.steps) {
2377
3468
  if (records.has(step.id)) continue;
2378
3469
  const inbound = inboundEdges(step);
2379
3470
  if (isEntryStep(step)) {
3471
+ if (expiredBeforeStep()) break;
2380
3472
  await executeStep(step);
2381
3473
  progressed = true;
2382
- if (runFailed) break;
3474
+ if (runFailed || runCancelled) break;
2383
3475
  continue;
2384
3476
  }
2385
3477
  if (inbound.length === 0) continue;
2386
3478
  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);
3479
+ if (inbound.some((edge) => edgeDecisions.get(edge.id))) {
3480
+ if (expiredBeforeStep()) break;
3481
+ await executeStep(step);
3482
+ } else skipStep(step);
2389
3483
  progressed = true;
2390
- if (runFailed) break;
3484
+ if (runFailed || runCancelled) break;
2391
3485
  }
2392
3486
  }
2393
3487
  for (const step of definition.steps) if (!records.has(step.id)) skipStep(step);
2394
3488
  const steps = definition.steps.map((step) => records.get(step.id));
2395
3489
  const warnings = options.declaredPorts ? deadPortWarnings(definition, options.declaredPorts) : [];
2396
3490
  const noted = warnings.length > 0 ? { warnings } : {};
2397
- return runFailed ? {
3491
+ if (runFailed) return {
2398
3492
  status: "FAILED",
2399
3493
  steps,
2400
3494
  error: runFailed,
2401
3495
  ...runFailedName ? { errorName: runFailedName } : {},
2402
3496
  ...noted
2403
- } : {
3497
+ };
3498
+ if (runCancelled) return {
3499
+ status: "CANCELLED",
3500
+ steps,
3501
+ error: runCancelled,
3502
+ errorName: RUN_DEADLINE_ERROR_NAME,
3503
+ ...noted
3504
+ };
3505
+ return {
2404
3506
  status: "SUCCEEDED",
2405
3507
  steps,
2406
3508
  ...noted
@@ -2453,6 +3555,6 @@ function installedPieces() {
2453
3555
  return [...builtinLocalPieces(), ...packagePieces.entries().filter((piece) => !isBuiltinPiece(piece.name))];
2454
3556
  }
2455
3557
  //#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 };
3558
+ export { parseAddress as $, declaredConnectionIds as A, stepBlock as B, builtinPiece as C, AttachmentCache as D, CORE_PIECE_VERSION as E, InMemorySecretProvider as F, PieceWorkerTimeoutError as G, PieceWorkerPool as H, SecretDeletedError as I, redactMessage as J, containsRedactedMarker as K, SecretNotFoundError as L, shapeConnection as M, resolveStepInput as N, BoundConnectionResolver as O, expressionsIn as P, isPrivateAddress as Q, parseSecretRef as R, blockPorts as S, CORE_PIECE_NAME$1 as T, PieceWorker as U, triggerBlock as V, PieceWorkerError as W, secretsFor as X, rememberSecrets as Y, DEFAULT_EGRESS_POLICY as Z, resolvedBlock as _, RUN_DEADLINE_ERROR_NAME as a, sourcedResolver as at, resolutionOf as b, canonicalJson as c, authMethodFor as ct, DynamicPropertiesError as d, ensurePieceBundle as dt, extractDedupeKey as et, checkDynamicProperties as f, fetchPieceBundle as ft, isHostBound as g, isIndeterminateError as gt, CompositeBlockExecutor as h, setPieceRegistryUrl as ht, packagePieces as i, pieceModuleRef as it, declaredReactorConnectionIds as j, StaticConnectionResolver as k, hashOf as l, unsupportedAuth as lt, ActivepiecesBlockExecutor as m, pieceRegistrySource as mt, installedPiece as n, maxFileBytes as nt, errorNameOf as o, buildDescriptor as ot, effectiveRetryPolicy as p, npmRegistryBaseUrl as pt, redact as q, installedPieces as r, bundleResolver as rt, runWorkflow as s, UnsupportedPieceFeatureError as st, PieceRegistry as t, FileTooLargeError as tt, stepConfigHash as u, unsupportedTrigger as ut, storeHandlers as v, describeBuiltinPiece as w, unpinnedNote as x, blockLabel as y, secretRefFromId as z };
2457
3559
 
2458
- //# sourceMappingURL=piece-registry-e-vFPyTd.js.map
3560
+ //# sourceMappingURL=piece-registry-BKRLK7Kv.js.map