@powerhousedao/reactor-workflow 6.2.3-dev.80 → 6.2.3-dev.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { $ as rememberSecrets, A as AttachmentCache, B as SecretNotFoundError, C as blockLabel, D as describeBuiltinPiece, E as builtinPiece, F as shapeConnection, G as PieceWorkerPool, H as secretRefFromId, I as resolveStepInput, J as PieceWorkerError, K as PieceWorker, L as expressionsIn, N as declaredConnectionIds, O as CORE_PIECE_NAME, P as declaredReactorConnectionIds, Q as redactMessage, S as storeHandlers, T as blockPorts, U as stepBlock, V as parseSecretRef, W as triggerBlock, X as containsRedactedMarker, Y as PieceWorkerTimeoutError, Z as redact, _ as effectiveRetryPolicy, _t as npmRegistryBaseUrl, a as PieceRegistry, at as FileTooLargeError, b as isHostBound, bt as isIndeterminateError, c as packagePieces, ct as pieceModuleRef, d as runWorkflow, dt as UnsupportedPieceFeatureError, et as secretsFor, f as canonicalJson, ft as authMethodFor, g as checkDynamicProperties, h as DynamicPropertiesError, i as tracedSteps, it as extractDedupeKey, j as BoundConnectionResolver, k as CORE_PIECE_VERSION, lt as sourcedResolver, m as stepConfigHash, mt as unsupportedTrigger, n as WorkflowTelemetry, nt as isPrivateAddress, o as installedPiece, ot as maxFileBytes, p as hashOf, pt as unsupportedAuth, q as PieceWorkerDisposedError, r as tracedMethods, rt as parseAddress, s as installedPieces, st as bundleResolver, t as TELEMETRY_SCOPE, tt as DEFAULT_EGRESS_POLICY, u as errorNameOf, v as ActivepiecesBlockExecutor, vt as pieceRegistrySource, w as unpinnedNote, x as resolvedBlock, y as CompositeBlockExecutor, yt as setPieceRegistryUrl, z as SecretDeletedError } from "./telemetry-BCXPCbfB.js";
2
- import { ReactorAccessDeniedError, ReactorAccessDeniedError as ReactorAccessDeniedError$1, ReactorActionsFailedError, ReactorJobFailedError, ReactorJobPendingError as ReactorJobPendingError$1, WebhookHandshakeStrategy } from "@powerhousedao/pieces-framework";
1
+ import { $ as redact, A as AttachmentCache, B as SecretDeletedError, C as blockLabel, D as describeBuiltinPiece, E as builtinPiece, F as shapeConnection, G as stepBlock, H as SecretOwnerMismatchError, I as resolveStepInput, J as PieceWorker, K as triggerBlock, L as expressionsIn, N as declaredConnectionIds, O as CORE_PIECE_NAME, P as declaredReactorConnectionIds, Q as containsRedactedMarker, S as storeHandlers, St as isIndeterminateError, T as blockPorts, U as parseSecretRef, V as SecretNotFoundError, W as secretRefFromId, X as PieceWorkerError, Y as PieceWorkerDisposedError, Z as PieceWorkerTimeoutError, _ as effectiveRetryPolicy, a as PieceRegistry, at as parseAddress, b as isHostBound, bt as pieceRegistrySource, c as packagePieces, ct as maxFileBytes, d as runWorkflow, dt as sourcedResolver, et as redactMessage, f as canonicalJson, g as checkDynamicProperties, gt as unsupportedTrigger, h as DynamicPropertiesError, ht as unsupportedAuth, i as tracedSteps, it as isPrivateAddress, j as BoundConnectionResolver, k as CORE_PIECE_VERSION, lt as bundleResolver, m as stepConfigHash, mt as authMethodFor, n as WorkflowTelemetry, nt as secretsFor, o as installedPiece, ot as extractDedupeKey, p as hashOf, pt as UnsupportedPieceFeatureError, q as PieceWorkerPool, r as tracedMethods, rt as DEFAULT_EGRESS_POLICY, s as installedPieces, st as FileTooLargeError, t as TELEMETRY_SCOPE, tt as rememberSecrets, u as errorNameOf, ut as pieceModuleRef, v as ActivepiecesBlockExecutor, w as unpinnedNote, x as resolvedBlock, xt as setPieceRegistryUrl, y as CompositeBlockExecutor, yt as npmRegistryBaseUrl, z as SecretCreatorMismatchError } from "./telemetry-DE0ALzOJ.js";
2
+ import { DENIED_ACTION_INDEXES, ReactorAccessDeniedError, ReactorAccessDeniedError as ReactorAccessDeniedError$1, ReactorJobFailedError, ReactorJobPendingError as ReactorJobPendingError$1, WebhookHandshakeStrategy, assertActionsApplied } from "@powerhousedao/pieces-framework";
3
3
  import { mkdirSync, openAsBlob, readFileSync, writeFileSync } from "node:fs";
4
4
  import { open, readFile, readdir, rm, stat } from "node:fs/promises";
5
5
  import { dirname, join } from "node:path";
@@ -19,7 +19,8 @@ import { actions as actions$1 } from "@powerhousedao/workflow/document-models/wo
19
19
  import { Kind, parse } from "graphql";
20
20
  import { ReactorHostServer } from "@powerhousedao/reactor/rpc";
21
21
  import { SpanStatusCode, context } from "@opentelemetry/api";
22
- import { BaseReadModel, JobStatus, PropagationMode, createEmptyDocument, defaultReadModelIndexingConfig, deleteDocumentAction, isPurgeMarker } from "@powerhousedao/reactor";
22
+ import { BaseReadModel, JobStatus, PropagationMode, WriteHandleFactory, createEmptyDocument, defaultReadModelIndexingConfig, deleteDocumentAction, isPurgeMarker, jobIdsOf, placeholderJob, remoteStagePort } from "@powerhousedao/reactor";
23
+ import { isFolderNode } from "@powerhousedao/shared/document-drive";
23
24
  import { DOCUMENT_SCOPE_ACTION_TYPES, initializeAuth, normalizeDocumentModelVersion, withSignaturePolicy } from "@powerhousedao/shared/document-model";
24
25
  import { performance as performance$1 } from "node:perf_hooks";
25
26
  //#region src/pieces/activepieces/piece-versions.ts
@@ -204,7 +205,10 @@ var OAuthError = class extends Error {
204
205
  }
205
206
  };
206
207
  async function up$2(db) {
207
- await db.schema.createTable("oauth_attempt").addColumn("state", "text", (col) => col.primaryKey()).addColumn("connection_id", "text", (col) => col.notNull()).addColumn("client_id", "text", (col) => col.notNull()).addColumn("client_secret_ref", "text", (col) => col.notNull()).addColumn("code_verifier", "text").addColumn("redirect_uri", "text", (col) => col.notNull()).addColumn("token_url", "text", (col) => col.notNull()).addColumn("authorization_method", "text").addColumn("return_url", "text").addColumn("status", "text", (col) => col.notNull()).addColumn("error", "text").addColumn("created_at", "text", (col) => col.notNull()).addColumn("expires_at", "text", (col) => col.notNull()).ifNotExists().execute();
208
+ await db.schema.createTable("oauth_attempt").addColumn("state", "text", (col) => col.primaryKey()).addColumn("connection_id", "text", (col) => col.notNull()).addColumn("client_id", "text", (col) => col.notNull()).addColumn("client_secret_ref", "text", (col) => col.notNull()).addColumn("code_verifier", "text").addColumn("redirect_uri", "text", (col) => col.notNull()).addColumn("token_url", "text", (col) => col.notNull()).addColumn("authorization_method", "text").addColumn("return_url", "text").addColumn("created_by", "text").addColumn("status", "text", (col) => col.notNull()).addColumn("error", "text").addColumn("created_at", "text", (col) => col.notNull()).addColumn("expires_at", "text", (col) => col.notNull()).ifNotExists().execute();
209
+ try {
210
+ await db.schema.alterTable("oauth_attempt").addColumn("created_by", "text").execute();
211
+ } catch {}
208
212
  }
209
213
  function fillOAuthUrl(template, props) {
210
214
  const filled = template.replace(/\{(\w+)\}/g, (_match, name) => {
@@ -267,6 +271,7 @@ var OAuthAttemptStore = class OAuthAttemptStore {
267
271
  token_url: fillOAuthUrl(method.tokenUrl, input.props),
268
272
  authorization_method: method.authorizationMethod ?? null,
269
273
  return_url: input.returnUrl ?? null,
274
+ created_by: input.createdBy?.toLowerCase() ?? null,
270
275
  status: "PENDING",
271
276
  error: null,
272
277
  created_at: now.toISOString(),
@@ -468,7 +473,7 @@ var StoreTokenRefresher = class {
468
473
  this.secrets = secrets;
469
474
  this.egress = egress;
470
475
  }
471
- refreshIfDue(source) {
476
+ refreshIfDue(source, connectionId) {
472
477
  const refOf = (name) => source.secretRefs?.find((entry) => entry.name === name)?.ref;
473
478
  const tokenRef = refOf(OAUTH_TOKEN);
474
479
  if (!tokenRef) return Promise.resolve();
@@ -480,19 +485,24 @@ var StoreTokenRefresher = class {
480
485
  } : void 0;
481
486
  let pending = this.inFlight.get(tokenRef);
482
487
  if (!pending) {
483
- pending = this.refresh(tokenRef, own).finally(() => this.inFlight.delete(tokenRef));
488
+ const owner = {
489
+ documentId: connectionId,
490
+ kind: "connection"
491
+ };
492
+ pending = this.refresh(tokenRef, own, owner).finally(() => this.inFlight.delete(tokenRef));
484
493
  this.inFlight.set(tokenRef, pending);
485
494
  }
486
495
  return pending;
487
496
  }
488
- async refresh(tokenRef, own) {
497
+ async refresh(tokenRef, own, owner) {
489
498
  const store = await this.secrets();
490
- const tokens = parseTokenSet(await store.get(tokenRef));
499
+ const read = (ref) => store.get(ref, owner);
500
+ const tokens = parseTokenSet(await read(tokenRef));
491
501
  if (!tokenDue(tokens, Math.round(Date.now() / 1e3))) return;
492
502
  const clientId = own?.clientId ?? tokens.client_id;
493
503
  const secretRef = own?.secretRef ?? tokens.client_secret_ref;
494
504
  if (!clientId || !secretRef) return;
495
- const auth = clientAuth$1(tokens.authorization_method, clientId, await store.get(secretRef));
505
+ const auth = clientAuth$1(tokens.authorization_method, clientId, own ? await read(secretRef) : await store.get(secretRef, null));
496
506
  let response;
497
507
  try {
498
508
  response = await postTokenForm(tokens.token_url, {
@@ -508,7 +518,7 @@ var StoreTokenRefresher = class {
508
518
  }
509
519
  throw new OAuthError(`Refreshing the OAuth2 token failed; reconnect: ${detail}`);
510
520
  }
511
- const current = parseTokenSet(await store.get(tokenRef));
521
+ const current = parseTokenSet(await read(tokenRef));
512
522
  if (current.access_token !== tokens.access_token || current.claimed_at !== tokens.claimed_at) return;
513
523
  await store.rotate(tokenRef, JSON.stringify(tokenSetFrom(response, tokens, tokens)));
514
524
  }
@@ -1106,6 +1116,8 @@ function detailResult(descriptor, pieceName, version) {
1106
1116
  props: action.props,
1107
1117
  requireAuth: action.requireAuth,
1108
1118
  ...action.requireReactor ? { requireReactor: action.requireReactor } : {},
1119
+ ...action.defaultTimeoutSeconds !== void 0 ? { defaultTimeoutSeconds: action.defaultTimeoutSeconds } : {},
1120
+ ...action.defaultRetry ? { defaultRetry: action.defaultRetry } : {},
1109
1121
  outputSchema: action.outputSchema,
1110
1122
  audience: action.audience
1111
1123
  }])),
@@ -1652,8 +1664,8 @@ async function resolveConnectionWithSecrets(document, secrets, request, oauth) {
1652
1664
  config: state.config ?? {},
1653
1665
  secretRefs: state.secretRefs
1654
1666
  };
1655
- if (source.authType === "OAUTH2") await oauth?.refreshIfDue(source);
1656
- return shapeConnection(source, secrets);
1667
+ if (source.authType === "OAUTH2") await oauth?.refreshIfDue(source, document.header.id);
1668
+ return shapeConnection(source, secrets, document.header.id);
1657
1669
  }
1658
1670
  const EGRESS_ALLOW_ENV = "PH_WORKFLOWS_EGRESS_ALLOW_ADDRESSES";
1659
1671
  function asCidr(entry) {
@@ -1904,6 +1916,18 @@ var ReactorError = class extends Error {
1904
1916
  function accessDenied(message) {
1905
1917
  return new ReactorError(ReactorAccessDeniedError, message);
1906
1918
  }
1919
+ var ReactorActionsDeniedError = class extends ReactorError {
1920
+ denied;
1921
+ constructor(documentId, denied) {
1922
+ super(ReactorAccessDeniedError, deniedMessage(documentId, denied));
1923
+ this.denied = denied;
1924
+ }
1925
+ };
1926
+ function deniedMessage(documentId, denied) {
1927
+ const actions = denied.map(({ type, reason }) => `${type} (${reason})`);
1928
+ const indexes = denied.map(({ index }) => index).join(",");
1929
+ return `The run user may not apply ${actions.join(", ")} to document ${documentId}; ${DENIED_ACTION_INDEXES}[${indexes}]`;
1930
+ }
1907
1931
  function refusedMethod(method) {
1908
1932
  return accessDenied(`${method} is not available to workflow pieces`);
1909
1933
  }
@@ -1998,27 +2022,9 @@ function jobPending(jobId, status) {
1998
2022
  const AUTHORIZATION_DENIED = "AuthorizationDeniedError";
1999
2023
  function jobFailed(job) {
2000
2024
  const error = job.error;
2001
- if (error?.name === AUTHORIZATION_DENIED) return accessDenied(`The Switchboard has no grant for this operation: ${error.message}`);
2025
+ if (error?.name === AUTHORIZATION_DENIED || error?.message.startsWith("Authorization denied")) return accessDenied(`The Switchboard has no grant for this operation: ${error.message}`);
2002
2026
  return new ReactorError(ReactorJobFailedError, `Reactor job ${job.id} failed: ${job.error?.message ?? "unknown error"}`);
2003
2027
  }
2004
- function assertActionsApplied(job, actions) {
2005
- const outcomes = job.result?.actions ?? [];
2006
- const byId = new Map(outcomes.map((outcome) => [outcome.actionId, outcome]));
2007
- const failed = (actions?.map((action) => ({
2008
- id: action.id,
2009
- label: action.type
2010
- })) ?? outcomes.map((outcome) => ({
2011
- id: outcome.actionId,
2012
- label: outcome.actionId
2013
- }))).flatMap(({ id, label }) => {
2014
- const outcome = byId.get(id);
2015
- if (!outcome) return [`Action ${label} produced no operation`];
2016
- if (outcome.kind === "reducer-error") return [`Action ${label} failed: ${outcome.message}`];
2017
- if (outcome.kind === "denied") return [`Action ${label} was denied: ${outcome.reason}`];
2018
- return [];
2019
- });
2020
- if (failed.length > 0) throw new ReactorError(ReactorActionsFailedError, failed.join("; "));
2021
- }
2022
2028
  const DOCUMENT_DRIVE = "powerhouse/document-drive";
2023
2029
  const REACTOR_DRIVE = "powerhouse/reactor-drive";
2024
2030
  function isDriveType(documentType) {
@@ -2075,6 +2081,8 @@ var RunScopedReactorClient = class {
2075
2081
  options;
2076
2082
  stopAt;
2077
2083
  maxPageLimit;
2084
+ jobs = /* @__PURE__ */ new Set();
2085
+ handles;
2078
2086
  constructor(inner, scope, host, options = {}) {
2079
2087
  this.inner = inner;
2080
2088
  this.scope = scope;
@@ -2082,6 +2090,7 @@ var RunScopedReactorClient = class {
2082
2090
  this.options = options;
2083
2091
  this.stopAt = scope.deadline - deadlineMargin(scope.deadline - Date.now());
2084
2092
  this.maxPageLimit = options.maxPageLimit ?? 100;
2093
+ this.handles = new WriteHandleFactory(remoteStagePort(this.read));
2085
2094
  }
2086
2095
  async getDocumentModelModules(namespace, paging, signal) {
2087
2096
  this.assertAccess();
@@ -2095,8 +2104,8 @@ var RunScopedReactorClient = class {
2095
2104
  this.assertAccess();
2096
2105
  return this.inner.getDocumentModelModuleForDocument(document);
2097
2106
  }
2098
- async get(identifier, view, signal) {
2099
- const document = await this.inner.get(identifier, this.readView(view), this.signal(signal));
2107
+ async get(identifier, view, signal, consistencyToken) {
2108
+ const document = await this.inner.get(identifier, this.readView(view), this.signal(signal), consistencyToken);
2100
2109
  await this.scope.journal.recordDocuments([document.header.id]);
2101
2110
  return document;
2102
2111
  }
@@ -2131,17 +2140,39 @@ var RunScopedReactorClient = class {
2131
2140
  this.assertAccess();
2132
2141
  return queryIndex(request, this.scope.runUser, this.signal(signal));
2133
2142
  }
2143
+ write = {
2144
+ create: (document, parentIdentifier, signal) => this.writeCreate(document, parentIdentifier, signal),
2145
+ createEmpty: (documentModelType, options, signal) => this.writeCreateEmpty("write.createEmpty", documentModelType, options, this.signal(signal)),
2146
+ execute: (documentIdentifier, branch, actions, signal) => this.submitExecute("write.execute", documentIdentifier, branch, actions, this.signal(signal)),
2147
+ deleteDocument: (identifier, propagate, signal) => this.submitDelete("write.deleteDocument", identifier, propagate, this.signal(signal)),
2148
+ drives: {
2149
+ addFile: (driveIdentifier, document, parentFolder, signal) => this.submitAddFile("write.drives.addFile", driveIdentifier, document, parentFolder, this.signal(signal)),
2150
+ addFolder: (driveIdentifier, name, parentFolder, signal) => this.submitAddFolder("write.drives.addFolder", driveIdentifier, name, parentFolder, this.signal(signal))
2151
+ }
2152
+ };
2153
+ read = {
2154
+ waitForJob: async (jobId, signal) => {
2155
+ this.assertOwnJob(jobId);
2156
+ return this.settle(jobId, this.signal(signal));
2157
+ },
2158
+ getJobStatus: async (jobId, signal) => {
2159
+ this.assertOwnJob(jobId);
2160
+ return this.inner.getJobStatus(jobId, this.signal(signal));
2161
+ },
2162
+ get: (identifier, view, signal, consistencyToken) => this.get(identifier, view, signal, consistencyToken)
2163
+ };
2134
2164
  async create(document, parentIdentifier, signal) {
2135
2165
  this.assertWritable("create");
2136
2166
  const abort = this.signal(signal);
2137
- const parentId = parentIdentifier === void 0 ? void 0 : await this.resolveReference(parentIdentifier, "main", abort);
2138
- return this.createDocument(document, parentId, abort);
2167
+ const parentId = await this.parentOf(parentIdentifier, abort);
2168
+ const handle = await this.submitCreate(document, parentId, abort);
2169
+ return this.createdBack(handle, abort);
2139
2170
  }
2140
2171
  async newDocument(documentModelType, options, signal) {
2141
2172
  this.assertWritable("newDocument");
2142
2173
  const abort = this.signal(signal);
2143
2174
  const module = await this.modelModule(documentModelType, options?.documentModelVersion, abort);
2144
- const parentId = options?.parentIdentifier === void 0 ? void 0 : await this.resolveReference(options.parentIdentifier, "main", abort);
2175
+ const parentId = await this.parentOf(options?.parentIdentifier, abort);
2145
2176
  const base = await this.inner.getCreateProtocolVersions({
2146
2177
  documentType: documentModelType,
2147
2178
  parentIdentifier: parentId
@@ -2149,24 +2180,40 @@ var RunScopedReactorClient = class {
2149
2180
  return createEmptyDocument(module, options, await this.inner.getCreateSignaturePolicy(), base);
2150
2181
  }
2151
2182
  async createEmpty(documentModelType, options, signal) {
2152
- this.assertWritable("createEmpty");
2153
2183
  const abort = this.signal(signal);
2154
- const document = await this.newDocument(documentModelType, options, abort);
2155
- const parentId = options?.parentIdentifier === void 0 ? void 0 : await this.resolveReference(options.parentIdentifier, "main", abort);
2156
- return this.createDocument(document, parentId, abort);
2184
+ const handle = await this.writeCreateEmpty("createEmpty", documentModelType, options, abort);
2185
+ return this.createdBack(handle, abort);
2157
2186
  }
2158
2187
  async execute(documentIdentifier, branch, actions, signal, subject) {
2159
2188
  if (subject !== void 0) throw accessDenied("A piece may not choose the subject it reads as");
2160
2189
  const abort = this.signal(signal);
2161
- const { id } = await this.apply("execute", documentIdentifier, branch, actions, abort);
2162
- return this.readBack(id, branch, abort);
2190
+ const handle = await this.submitExecute("execute", documentIdentifier, branch, actions, abort);
2191
+ await this.landed(handle, abort, actions);
2192
+ return this.readBack(handle.documentId, branch, abort);
2163
2193
  }
2164
2194
  async executeAsync(documentIdentifier, branch, actions, signal) {
2165
2195
  const abort = this.signal(signal);
2166
- const { job } = await this.apply("executeAsync", documentIdentifier, branch, actions, abort);
2167
- return job;
2196
+ const handle = await this.submitExecute("executeAsync", documentIdentifier, branch, actions, abort);
2197
+ return this.landed(handle, abort, actions);
2198
+ }
2199
+ async deleteDocument(identifier, propagate, signal) {
2200
+ const abort = this.signal(signal);
2201
+ const handle = await this.submitDelete("deleteDocument", identifier, propagate, abort);
2202
+ await this.landed(handle, abort);
2168
2203
  }
2169
- async apply(method, documentIdentifier, branch, actions, abort) {
2204
+ async writeCreate(document, parentIdentifier, signal) {
2205
+ this.assertWritable("write.create");
2206
+ const abort = this.signal(signal);
2207
+ const parentId = await this.parentOf(parentIdentifier, abort);
2208
+ return this.submitCreate(document, parentId, abort);
2209
+ }
2210
+ async writeCreateEmpty(method, documentModelType, options, abort) {
2211
+ this.assertWritable(method);
2212
+ const document = await this.newDocument(documentModelType, options, abort);
2213
+ const parentId = await this.parentOf(options?.parentIdentifier, abort);
2214
+ return this.submitCreate(document, parentId, abort);
2215
+ }
2216
+ async submitExecute(method, documentIdentifier, branch, actions, abort) {
2170
2217
  this.assertWritable(method);
2171
2218
  for (const action of actions) {
2172
2219
  if (action.context?.signer) throw accessDenied(`Action ${action.type} carries a signer; the host signs every action`);
@@ -2174,26 +2221,17 @@ var RunScopedReactorClient = class {
2174
2221
  }
2175
2222
  const id = await this.resolveReference(documentIdentifier, branch, abort);
2176
2223
  await this.gate(id, branch, actions, abort);
2177
- return {
2178
- id,
2179
- job: await this.run(id, branch, actions, abort)
2180
- };
2224
+ return this.submit(id, branch, actions, abort);
2181
2225
  }
2182
- async deleteDocument(identifier, propagate, signal) {
2183
- this.assertWritable("deleteDocument");
2226
+ async submitDelete(method, identifier, propagate, abort) {
2227
+ this.assertWritable(method);
2184
2228
  if (propagate === PropagationMode.Cascade) throw accessDenied("Cascade delete is not offered to pieces");
2185
- const abort = this.signal(signal);
2186
2229
  const id = await this.resolveReference(identifier, "main", abort);
2187
2230
  await this.gate(id, "main", [deleteDocumentAction(id)], abort);
2188
2231
  this.assertTimeLeft("a delete");
2189
2232
  await this.scope.journal.recordDocuments([id]);
2190
- try {
2191
- await this.inner.deleteDocument(id, void 0, abort);
2192
- } catch (error) {
2193
- const message = messageOf(error);
2194
- if (message.startsWith("Authorization denied")) throw accessDenied(`The Switchboard has no grant for this operation: ${message}`);
2195
- throw error;
2196
- }
2233
+ const submitted = await this.inner.write.deleteDocument(id, void 0, abort);
2234
+ return this.handle(submitted, await this.track(submitted, () => [id]));
2197
2235
  }
2198
2236
  drives = {
2199
2237
  getNode: (driveIdentifier, nodeId, signal) => this.getNode(driveIdentifier, nodeId, signal),
@@ -2212,31 +2250,39 @@ var RunScopedReactorClient = class {
2212
2250
  return this.inner.drives.listNodes(drive.id, parentFolder, this.paging(paging), abort);
2213
2251
  }
2214
2252
  async addFile(driveIdentifier, document, parentFolder, signal) {
2215
- this.assertWritable("drives.addFile");
2216
2253
  const abort = this.signal(signal);
2254
+ const handle = await this.submitAddFile("drives.addFile", driveIdentifier, document, parentFolder, abort);
2255
+ return this.createdBack(handle, abort, true);
2256
+ }
2257
+ async addFolder(driveIdentifier, name, parentFolder, signal) {
2258
+ const abort = this.signal(signal);
2259
+ const handle = await this.submitAddFolder("drives.addFolder", driveIdentifier, name, parentFolder, abort);
2260
+ await this.landed(handle, abort);
2261
+ const node = await this.inner.drives.getNode(handle.documentId, handle.nodeId, abort);
2262
+ if (!isFolderNode(node)) throw new Error("Folder creation failed");
2263
+ return node;
2264
+ }
2265
+ async submitAddFile(method, driveIdentifier, document, parentFolder, abort) {
2266
+ this.assertWritable(method);
2217
2267
  const drive = await this.readDrive(driveIdentifier, abort);
2218
2268
  const prepared = withSignaturePolicy(document, await this.inner.getCreateSignaturePolicy());
2219
2269
  const documentId = prepared.header.id;
2220
2270
  await this.gateCandidates(drive.id, "main", [fileCandidate(drive, prepared, parentFolder)], abort);
2221
2271
  this.assertTimeLeft(`a ${prepared.header.documentType} document`);
2222
2272
  await this.scope.journal.recordDocuments([drive.id, documentId]);
2223
- await this.inner.drives.addFile(drive.id, prepared, parentFolder, abort);
2224
- try {
2225
- await this.grantRunUser(documentId, abort);
2226
- return await this.readBack(documentId, prepared.header.branch || "main", abort);
2227
- } catch (error) {
2228
- if (error instanceof Error) error.message = `Document ${documentId} was created, but: ${error.message}`;
2229
- throw error;
2230
- }
2273
+ const submitted = await this.inner.write.drives.addFile(drive.id, prepared, parentFolder, abort);
2274
+ const jobIds = await this.track(submitted, () => [drive.id, documentId]);
2275
+ return this.withGrant(submitted, jobIds, abort, true);
2231
2276
  }
2232
- async addFolder(driveIdentifier, name, parentFolder, signal) {
2233
- this.assertWritable("drives.addFolder");
2234
- const abort = this.signal(signal);
2277
+ async submitAddFolder(method, driveIdentifier, name, parentFolder, abort) {
2278
+ this.assertWritable(method);
2235
2279
  const drive = await this.readDrive(driveIdentifier, abort);
2236
2280
  await this.gateCandidates(drive.id, "main", [folderCandidate(drive, name, parentFolder)], abort);
2237
2281
  this.assertTimeLeft("a folder");
2238
2282
  await this.scope.journal.recordDocuments([drive.id]);
2239
- return this.inner.drives.addFolder(drive.id, name, parentFolder, abort);
2283
+ const submitted = await this.inner.write.drives.addFolder(drive.id, name, parentFolder, abort);
2284
+ const jobIds = await this.track(submitted, () => [drive.id]);
2285
+ return Object.assign(this.handle(submitted, jobIds), { nodeId: submitted.nodeId });
2240
2286
  }
2241
2287
  async readDrive(driveIdentifier, signal) {
2242
2288
  const { id, documentType } = (await this.inner.get(driveIdentifier, this.readView({ scopes: ["document"] }), signal)).header;
@@ -2291,21 +2337,66 @@ var RunScopedReactorClient = class {
2291
2337
  } catch (error) {
2292
2338
  throw accessDenied(`Could not check the run user's access to document ${documentId}: ${messageOf(error)}`);
2293
2339
  }
2294
- const denied = answer.evaluations.flatMap((evaluation, index) => evaluation.decision === "deny" ? [`${candidates[index].type} (${evaluation.reason})`] : []);
2295
- if (denied.length > 0) throw accessDenied(`The run user may not apply ${denied.join(", ")} to document ${documentId}`);
2340
+ const denied = answer.evaluations.flatMap((evaluation, index) => evaluation.decision === "deny" ? [{
2341
+ index,
2342
+ type: candidates[index].type,
2343
+ reason: evaluation.reason
2344
+ }] : []);
2345
+ if (denied.length > 0) throw new ReactorActionsDeniedError(documentId, denied);
2296
2346
  }
2297
- async run(documentId, branch, actions, signal) {
2347
+ async submit(documentId, branch, actions, signal) {
2298
2348
  this.assertTimeLeft(`${actions.length} action(s)`);
2299
- const info = await this.inner.executeAsync(documentId, branch, actions, signal);
2300
- await this.scope.journal.recordJob(info.id, [documentId]);
2301
- const settled = await this.settle(info.id, signal);
2302
- if (settled.status === JobStatus.FAILED) throw jobFailed(settled);
2303
- assertActionsApplied(settled, actions);
2304
- return settled;
2349
+ const submitted = await this.inner.write.execute(documentId, branch, actions, signal);
2350
+ return this.handle(submitted, await this.track(submitted, () => [documentId]));
2351
+ }
2352
+ async run(documentId, branch, actions, signal) {
2353
+ const handle = await this.submit(documentId, branch, actions, signal);
2354
+ return this.landed(handle, signal, actions);
2355
+ }
2356
+ async track(submitted, documentsOf) {
2357
+ const jobIds = [submitted.jobId, ...jobIdsOf(submitted).filter((jobId) => jobId !== submitted.jobId)].filter((jobId) => jobId !== "");
2358
+ for (const jobId of jobIds) {
2359
+ this.jobs.add(jobId);
2360
+ await this.scope.journal.recordJob(jobId, documentsOf(jobId));
2361
+ }
2362
+ return jobIds;
2363
+ }
2364
+ handle(submitted, jobIds) {
2365
+ const { documentId, branch } = submitted;
2366
+ const reader = (receipt, view, signal) => this.get(documentId, {
2367
+ ...view,
2368
+ branch: receipt.branch
2369
+ }, signal, receipt.token);
2370
+ return jobIds.length === 0 ? this.handles.settled(documentId, branch, reader) : this.handles.forJobs(submitted.jobId, jobIds.map((jobId) => placeholderJob(jobId, documentId)), documentId, branch, void 0, reader);
2371
+ }
2372
+ async landed(handle, signal, actions, onLanded) {
2373
+ let own;
2374
+ for (const jobId of jobIdsOf(handle)) {
2375
+ const job = await this.settle(jobId, signal);
2376
+ if (job.status === JobStatus.FAILED) throw jobFailed(job);
2377
+ assertActionsApplied(job, jobId === handle.jobId ? actions : void 0);
2378
+ if (jobId === handle.jobId) own = job;
2379
+ onLanded?.(jobId);
2380
+ }
2381
+ return own ?? (await handle.readable).job;
2382
+ }
2383
+ async createdBack(handle, signal, created = false) {
2384
+ try {
2385
+ await this.landed(handle, signal, void 0, (jobId) => {
2386
+ if (jobId === handle.jobId) created = true;
2387
+ });
2388
+ return await this.readBack(handle.documentId, handle.branch, signal);
2389
+ } catch (error) {
2390
+ if (created && error instanceof Error) error.message = `Document ${handle.documentId} was created, but: ${error.message}`;
2391
+ throw error;
2392
+ }
2305
2393
  }
2306
2394
  assertTimeLeft(what) {
2307
2395
  if (Date.now() >= this.stopAt) throw new Error(`No time was left before the step deadline to submit ${what}; nothing was submitted`);
2308
2396
  }
2397
+ assertOwnJob(jobId) {
2398
+ if (!this.jobs.has(jobId)) throw accessDenied(`Job ${String(jobId)} was not submitted by this step`);
2399
+ }
2309
2400
  async settle(jobId, signal) {
2310
2401
  let status = JobStatus.PENDING;
2311
2402
  for (;;) {
@@ -2316,7 +2407,7 @@ var RunScopedReactorClient = class {
2316
2407
  try {
2317
2408
  job = await this.inner.waitForJob(jobId, wait);
2318
2409
  } catch (error) {
2319
- if (!wait.aborted) throw error;
2410
+ if (!wait.aborted || signal.aborted) throw error;
2320
2411
  job = await this.inner.getJobStatus(jobId).catch(() => {
2321
2412
  throw jobPending(jobId, status);
2322
2413
  });
@@ -2338,32 +2429,32 @@ var RunScopedReactorClient = class {
2338
2429
  await this.scope.journal.recordDocuments([documentId]);
2339
2430
  return document;
2340
2431
  }
2341
- async createDocument(document, parentId, signal) {
2432
+ async submitCreate(document, parentId, signal) {
2342
2433
  const prepared = withSignaturePolicy(document, await this.inner.getCreateSignaturePolicy());
2343
2434
  const documentId = prepared.header.id;
2344
2435
  this.assertTimeLeft(`a ${prepared.header.documentType} document`);
2345
- const { jobs } = await this.inner.createAsync(prepared, parentId, signal);
2346
- const ordered = Object.entries(jobs).sort(([a], [b]) => Number(b === "create") - Number(a === "create"));
2347
- for (const [key, job] of ordered) await this.scope.journal.recordJob(job.id, key === "create" || parentId === void 0 ? [documentId] : [parentId, documentId]);
2348
- let landed = false;
2436
+ const submitted = await this.inner.write.create(prepared, parentId, signal);
2437
+ const jobIds = await this.track(submitted, (jobId) => jobId === submitted.jobId || parentId === void 0 ? [documentId] : [parentId, documentId]);
2438
+ return this.withGrant(submitted, jobIds, signal);
2439
+ }
2440
+ async withGrant(submitted, jobIds, signal, created = false) {
2441
+ if (!this.scope.runUser) return this.handle(submitted, jobIds);
2349
2442
  try {
2350
- for (const [, job] of ordered) {
2351
- const settled = await this.settle(job.id, signal);
2352
- if (settled.status === JobStatus.FAILED) throw jobFailed(settled);
2353
- assertActionsApplied(settled);
2354
- landed = true;
2443
+ for (const jobId of jobIds) {
2444
+ if ((await this.settle(jobId, signal)).status === JobStatus.FAILED) return this.handle(submitted, jobIds);
2445
+ created = true;
2355
2446
  }
2356
- await this.grantRunUser(documentId, signal);
2357
- return await this.readBack(documentId, prepared.header.branch || "main", signal);
2447
+ const grant = await this.grantRunUser(submitted.documentId, signal);
2448
+ return this.handle(submitted, grant ? [...jobIds, grant.id] : jobIds);
2358
2449
  } catch (error) {
2359
- if (landed && error instanceof Error) error.message = `Document ${documentId} was created, but: ${error.message}`;
2450
+ if (created && error instanceof Error) error.message = `Document ${submitted.documentId} was created, but: ${error.message}`;
2360
2451
  throw error;
2361
2452
  }
2362
2453
  }
2363
2454
  async grantRunUser(documentId, signal) {
2364
2455
  const runUser = this.scope.runUser;
2365
- if (!runUser) return;
2366
- if ((await this.inner.get(documentId, void 0, signal)).state.auth.version !== 0) return;
2456
+ if (!runUser) return void 0;
2457
+ if ((await this.inner.get(documentId, void 0, signal)).state.auth.version !== 0) return void 0;
2367
2458
  const grants = [{
2368
2459
  id: "run-user",
2369
2460
  description: "The user whose workflow run created this document",
@@ -2378,11 +2469,14 @@ var RunScopedReactorClient = class {
2378
2469
  principal: this.options.hostPrincipal,
2379
2470
  capability: { can: "execute" }
2380
2471
  });
2381
- await this.run(documentId, "main", [initializeAuth({
2472
+ return this.run(documentId, "main", [initializeAuth({
2382
2473
  version: 1,
2383
2474
  grants
2384
2475
  })], signal);
2385
2476
  }
2477
+ parentOf(identifier, signal) {
2478
+ return identifier === void 0 ? Promise.resolve(void 0) : this.resolveReference(identifier, "main", signal);
2479
+ }
2386
2480
  async resolveReference(identifier, branch, signal) {
2387
2481
  try {
2388
2482
  return await this.inner.resolveIdOrSlug(identifier, { branch }, signal);
@@ -2447,6 +2541,36 @@ const REFUSED_DRIVES = {
2447
2541
  copyNode: true,
2448
2542
  setPreferredEditorOnNode: true
2449
2543
  };
2544
+ const SERVED_WRITE = {
2545
+ create: true,
2546
+ createEmpty: true,
2547
+ execute: true,
2548
+ deleteDocument: true
2549
+ };
2550
+ const SERVED_DRIVE_WRITES = {
2551
+ addFile: true,
2552
+ addFolder: true
2553
+ };
2554
+ const REFUSED_WRITE = {
2555
+ isDocumentIdTaken: true,
2556
+ describe: true,
2557
+ evaluateActions: true,
2558
+ upgradeDocument: true,
2559
+ executeBatch: true,
2560
+ rename: true,
2561
+ setPreferredEditor: true,
2562
+ addRelationship: true,
2563
+ updateRelationship: true,
2564
+ removeRelationship: true,
2565
+ moveRelationship: true,
2566
+ deleteDocuments: true,
2567
+ loadBatch: true
2568
+ };
2569
+ const SERVED_READ = {
2570
+ waitForJob: true,
2571
+ getJobStatus: true,
2572
+ get: true
2573
+ };
2450
2574
  const REFUSED = {
2451
2575
  getMany: true,
2452
2576
  createDocumentInDrive: true,
@@ -2474,6 +2598,10 @@ const REFUSED = {
2474
2598
  function servedClient(client) {
2475
2599
  const facade = served(client, SERVED, REFUSED, "");
2476
2600
  facade.drives = served(client.drives, SERVED_DRIVES, REFUSED_DRIVES, "drives.");
2601
+ const write = served(client.write, SERVED_WRITE, REFUSED_WRITE, "write.");
2602
+ write.drives = served(client.write.drives, SERVED_DRIVE_WRITES, REFUSED_DRIVES, "write.drives.");
2603
+ facade.write = write;
2604
+ if (client.read) facade.read = served(client.read, SERVED_READ, {}, "read.");
2477
2605
  return facade;
2478
2606
  }
2479
2607
  function served(methods, serve, refuse, prefix) {
@@ -2509,7 +2637,11 @@ function reactorTap(host, base, journal, options = {}, telemetry) {
2509
2637
  journal
2510
2638
  }, host, clientOptions));
2511
2639
  const parent = context.active();
2512
- const server = new ReactorHostServer(telemetry ? tracedMethods(served, telemetry, "reactor", parent, ["drives"]) : served, transport);
2640
+ const server = new ReactorHostServer(telemetry ? tracedMethods(served, telemetry, "reactor", parent, [
2641
+ "drives",
2642
+ "write",
2643
+ "read"
2644
+ ]) : served, transport);
2513
2645
  server.start();
2514
2646
  return () => server.stop();
2515
2647
  }
@@ -2554,7 +2686,10 @@ const LEGACY_MASTER_KEY_ENV = "PH_SECRETS_MASTER_KEY";
2554
2686
  const KEY_CHECK_ID = "master";
2555
2687
  const KEY_CHECK_LABEL = "reactor-workflow/secrets/key-check/v1";
2556
2688
  async function up$1(db) {
2557
- await db.schema.createTable("secret").addColumn("id", "text", (col) => col.primaryKey()).addColumn("label", "text").addColumn("version", "integer", (col) => col.notNull()).addColumn("enc", "text").addColumn("status", "text", (col) => col.notNull()).addColumn("created_at", "text", (col) => col.notNull()).addColumn("updated_at", "text", (col) => col.notNull()).ifNotExists().execute();
2689
+ await db.schema.createTable("secret").addColumn("id", "text", (col) => col.primaryKey()).addColumn("label", "text").addColumn("owner_id", "text").addColumn("created_by", "text").addColumn("version", "integer", (col) => col.notNull()).addColumn("enc", "text").addColumn("status", "text", (col) => col.notNull()).addColumn("created_at", "text", (col) => col.notNull()).addColumn("updated_at", "text", (col) => col.notNull()).ifNotExists().execute();
2690
+ for (const column of ["owner_id", "created_by"]) try {
2691
+ await db.schema.alterTable("secret").addColumn(column, "text").execute();
2692
+ } catch {}
2558
2693
  await db.schema.createTable("secret_key_check").addColumn("id", "text", (col) => col.primaryKey()).addColumn("fingerprint", "text", (col) => col.notNull()).addColumn("created_at", "text", (col) => col.notNull()).ifNotExists().execute();
2559
2694
  }
2560
2695
  function legacyKeyHint() {
@@ -2666,6 +2801,8 @@ var LocalEncryptedSecretStore = class LocalEncryptedSecretStore {
2666
2801
  return {
2667
2802
  ref: secretRefFromId(row.id),
2668
2803
  label: row.label,
2804
+ ownerId: row.owner_id,
2805
+ createdBy: row.created_by,
2669
2806
  version: row.version,
2670
2807
  status: row.status,
2671
2808
  createdAt: row.created_at,
@@ -2673,10 +2810,13 @@ var LocalEncryptedSecretStore = class LocalEncryptedSecretStore {
2673
2810
  };
2674
2811
  }
2675
2812
  async create(input) {
2813
+ if (input.ownerId === "") throw new Error("A secret needs an owning document");
2676
2814
  const now = (/* @__PURE__ */ new Date()).toISOString();
2677
2815
  const row = {
2678
2816
  id: randomBytes(16).toString("hex"),
2679
2817
  label: input.label ?? null,
2818
+ owner_id: input.ownerId,
2819
+ created_by: input.createdBy?.toLowerCase() ?? null,
2680
2820
  version: 1,
2681
2821
  enc: this.encrypt(input.value),
2682
2822
  status: "ACTIVE",
@@ -2702,8 +2842,11 @@ var LocalEncryptedSecretStore = class LocalEncryptedSecretStore {
2702
2842
  }).where("id", "=", row.id).execute();
2703
2843
  return this.toStat(updated);
2704
2844
  }
2705
- async get(ref) {
2845
+ async get(ref, owner) {
2706
2846
  const row = await this.row(ref);
2847
+ if (owner === null) {
2848
+ if (row.owner_id !== null) throw new SecretOwnerMismatchError("host");
2849
+ } else if (row.owner_id === null || row.owner_id !== owner.documentId) throw new SecretOwnerMismatchError(owner.kind);
2707
2850
  if (row.status !== "ACTIVE" || row.enc === null) throw new SecretDeletedError(ref);
2708
2851
  return this.decrypt(row.enc);
2709
2852
  }
@@ -2766,8 +2909,9 @@ function coerce(variable) {
2766
2909
  }
2767
2910
  }
2768
2911
  /** Coerces typed variables and resolves SECRET ones through the secret store.
2769
- * The error for a SECRET names its ref, never a value. */
2770
- async function resolveVariables(variables, secrets) {
2912
+ * A SECRET resolves only as the workflow it was minted for. The error for a
2913
+ * SECRET names its ref, never a value. */
2914
+ async function resolveVariables(variables, secrets, workflowId) {
2771
2915
  const secretValues = [];
2772
2916
  const resolved = [];
2773
2917
  for (const variable of variables) {
@@ -2788,7 +2932,10 @@ async function resolveVariables(variables, secrets) {
2788
2932
  if (typeof variable.value !== "string") throw new VariableTypeError(`Variable "${variable.key}" is a SECRET, but its value is not a secret reference`);
2789
2933
  let value;
2790
2934
  try {
2791
- value = await secrets.get(variable.value);
2935
+ value = await secrets.get(variable.value, {
2936
+ documentId: workflowId,
2937
+ kind: "workflow"
2938
+ });
2792
2939
  } catch (error) {
2793
2940
  const detail = error instanceof Error ? error.message : String(error);
2794
2941
  throw new VariableTypeError(`Secret variable "${variable.key}" could not be resolved: ${detail}`);
@@ -10221,7 +10368,7 @@ function clientSecretLabel(appId, kind) {
10221
10368
  */
10222
10369
  async function loadOAuthApps(definitions, store, options = {}) {
10223
10370
  const read = options.read ?? ((path) => readFile(path, "utf8"));
10224
- const existing = new Map((await store.list()).filter((s) => s.status === "ACTIVE" && s.label).map((s) => [s.label, s.ref]));
10371
+ const existing = new Map((await store.list()).filter((s) => s.status === "ACTIVE" && s.label && s.ownerId === null).map((s) => [s.label, s.ref]));
10225
10372
  const entries = [];
10226
10373
  for (const app of definitions) for (const kind of ["desktop", "web"]) {
10227
10374
  const path = app.clients[kind];
@@ -10236,10 +10383,11 @@ async function loadOAuthApps(definitions, store, options = {}) {
10236
10383
  const label = clientSecretLabel(app.id, kind);
10237
10384
  let ref = existing.get(label);
10238
10385
  if (ref) {
10239
- if (await store.get(ref) !== client.clientSecret) await store.rotate(ref, client.clientSecret);
10386
+ if (await store.get(ref, null) !== client.clientSecret) await store.rotate(ref, client.clientSecret);
10240
10387
  } else ref = (await store.create({
10241
10388
  value: client.clientSecret,
10242
- label
10389
+ label,
10390
+ ownerId: null
10243
10391
  })).ref;
10244
10392
  entries.push({
10245
10393
  appId: app.id,
@@ -10293,6 +10441,7 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms).unref());
10293
10441
  const logger$1 = childLogger(["workflow", "runtime"]);
10294
10442
  const DRIVE_DOCUMENT_TYPE = "powerhouse/document-drive";
10295
10443
  const WORKFLOW_DOCUMENT_TYPE = "powerhouse/workflow";
10444
+ const CONNECTION_DOCUMENT_TYPE = "powerhouse/connection";
10296
10445
  const DOCUMENT_SCOPE = "document";
10297
10446
  const CHILD_RELATIONSHIP = "child";
10298
10447
  function operationKey(op) {
@@ -10563,7 +10712,56 @@ var WorkflowRuntimeService = class {
10563
10712
  return this.secretsPromise;
10564
10713
  }
10565
10714
  secretProvider() {
10566
- return { get: (ref) => this.secrets().then((store) => store.get(ref)) };
10715
+ return { get: (ref, owner) => this.secrets().then((s) => s.get(ref, owner)) };
10716
+ }
10717
+ callerAddress(ctx) {
10718
+ return (ctx ? this.host.subjectOf?.(ctx) : void 0)?.address?.toLowerCase() ?? null;
10719
+ }
10720
+ async createSecret(input, ctx) {
10721
+ const client = this.host.reactorClient;
10722
+ if (await isNotHereYet(client, input.ownerId)) await waitForSync(client, input.ownerId, () => Promise.resolve(), this.host.syncWaitMs ?? 1e4);
10723
+ await this.assertCanReadDocument(input.ownerId, ctx);
10724
+ await this.assertCanWriteDocument(input.ownerId, ctx);
10725
+ const type = (await client.get(input.ownerId)).header.documentType;
10726
+ if (type !== CONNECTION_DOCUMENT_TYPE && type !== WORKFLOW_DOCUMENT_TYPE) throw new Error("A secret belongs to a connection or a workflow");
10727
+ return (await this.secrets()).create({
10728
+ ...input,
10729
+ createdBy: this.callerAddress(ctx)
10730
+ });
10731
+ }
10732
+ async assertCanManageSecret(stat, ctx) {
10733
+ if (stat.ownerId === null) throw new Error("This secret has no owner");
10734
+ await this.assertCanReadDocument(stat.ownerId, ctx);
10735
+ await this.assertCanWriteDocument(stat.ownerId, ctx);
10736
+ if (stat.createdBy !== null && stat.createdBy !== this.callerAddress(ctx)) throw new SecretCreatorMismatchError();
10737
+ }
10738
+ async canManageSecret(stat, ctx) {
10739
+ return this.assertCanManageSecret(stat, ctx).then(() => true, () => false);
10740
+ }
10741
+ async rotateSecret(ref, value, ctx) {
10742
+ const store = await this.secrets();
10743
+ await this.assertCanManageSecret(await store.stat(ref), ctx);
10744
+ return store.rotate(ref, value);
10745
+ }
10746
+ async deleteSecret(ref, ctx) {
10747
+ const store = await this.secrets();
10748
+ await this.assertCanManageSecret(await store.stat(ref), ctx);
10749
+ await store.delete(ref);
10750
+ }
10751
+ async secretStat(ref, ctx) {
10752
+ let stat;
10753
+ try {
10754
+ stat = await (await this.secrets()).stat(ref);
10755
+ } catch {
10756
+ return null;
10757
+ }
10758
+ return (await this.readableSecrets([stat], ctx)).at(0) ?? null;
10759
+ }
10760
+ async listSecrets(ctx) {
10761
+ return this.readableSecrets(await (await this.secrets()).list(), ctx);
10762
+ }
10763
+ readableSecrets(stats, ctx) {
10764
+ return this.readableRows(stats.filter((stat) => stat.ownerId !== null), (stat) => stat.ownerId, ctx);
10567
10765
  }
10568
10766
  oauthAttempts() {
10569
10767
  this.oauthAttemptsPromise ??= OAuthAttemptStore.create(this.host.relationalDb);
@@ -11644,7 +11842,10 @@ var WorkflowRuntimeService = class {
11644
11842
  async webhookSecret(config, workflowId) {
11645
11843
  if (!config.secretRef) return void 0;
11646
11844
  try {
11647
- return await (await this.secrets()).get(config.secretRef);
11845
+ return await (await this.secrets()).get(config.secretRef, {
11846
+ documentId: workflowId,
11847
+ kind: "workflow"
11848
+ });
11648
11849
  } catch (error) {
11649
11850
  const message = error instanceof Error ? error.message : String(error);
11650
11851
  this.logger.error(`Webhook secret unavailable for ${workflowId}: @error`, message);
@@ -11669,6 +11870,19 @@ var WorkflowRuntimeService = class {
11669
11870
  const descriptor = await this.pieceDescriptor(targetOf(block));
11670
11871
  return (block.kind === "trigger" ? descriptor.triggers : descriptor.actions).find((candidate) => candidate.name === block.name)?.requireReactor;
11671
11872
  }
11873
+ async blockDefaults(block) {
11874
+ if (block.kind !== "action" || builtinPiece(block.pieceName)) return;
11875
+ const parsed = resolvedBlock(await this.resolveBlock(block));
11876
+ if (!parsed) return void 0;
11877
+ const action = (await this.pieceDescriptor(targetOf(parsed))).actions.find((candidate) => candidate.name === parsed.name);
11878
+ if (!action) return void 0;
11879
+ const { defaultTimeoutSeconds, defaultRetry } = action;
11880
+ if (defaultTimeoutSeconds === void 0 && defaultRetry === void 0) return;
11881
+ return {
11882
+ ...defaultTimeoutSeconds !== void 0 ? { timeoutSeconds: defaultTimeoutSeconds } : {},
11883
+ ...defaultRetry !== void 0 ? { retry: defaultRetry } : {}
11884
+ };
11885
+ }
11672
11886
  callerRunUser(ctx) {
11673
11887
  const subject = ctx ? this.host.subjectOf?.(ctx) : void 0;
11674
11888
  return subject?.address ? {
@@ -11993,7 +12207,8 @@ var WorkflowRuntimeService = class {
11993
12207
  redirectUri: options.redirectUri,
11994
12208
  ...options.returnUrl ? { returnUrl: options.returnUrl } : {},
11995
12209
  ...scopes ? { scopes } : {},
11996
- ...pkce ? { pkce } : {}
12210
+ ...pkce ? { pkce } : {},
12211
+ createdBy: this.callerAddress(ctx)
11997
12212
  });
11998
12213
  }
11999
12214
  async oauthAttempt(state, ctx) {
@@ -12027,19 +12242,27 @@ var WorkflowRuntimeService = class {
12027
12242
  const fromRegistry = typeof appId === "string" && appId !== "" && (await this.oauthApps()).clients(appId).some((c) => c.clientId === attempt.client_id);
12028
12243
  if (state.authType !== "OAUTH2" || !fromRegistry && config["client_id"] !== attempt.client_id) return await fail("The connection changed during sign-in; try again");
12029
12244
  const secrets = await this.secrets();
12030
- const tokens = await exchangeCode(attempt, callback.code, await secrets.get(attempt.client_secret_ref), this.designEgress, fromRegistry);
12245
+ const connectionId = document.header.id;
12246
+ const tokens = await exchangeCode(attempt, callback.code, await secrets.get(attempt.client_secret_ref, fromRegistry ? null : {
12247
+ documentId: connectionId,
12248
+ kind: "connection"
12249
+ }), this.designEgress, fromRegistry);
12031
12250
  const value = JSON.stringify(tokens);
12032
12251
  const existing = state.secretRefs.find((entry) => entry.name === OAUTH_TOKEN);
12033
12252
  let ref = existing?.ref;
12034
12253
  try {
12035
- if (ref) await secrets.rotate(ref, value);
12254
+ const stat = ref ? await secrets.stat(ref) : void 0;
12255
+ if (ref && stat?.ownerId === connectionId && (stat.createdBy === null || stat.createdBy === attempt.created_by)) await secrets.rotate(ref, value);
12256
+ else ref = void 0;
12036
12257
  } catch {
12037
12258
  ref = void 0;
12038
12259
  }
12039
12260
  if (!ref) {
12040
12261
  ref = (await secrets.create({
12041
12262
  value,
12042
- label: `${state.name || "connection"} · OAuth2 token`
12263
+ label: `${state.name || "connection"} · OAuth2 token`,
12264
+ ownerId: connectionId,
12265
+ createdBy: attempt.created_by
12043
12266
  })).ref;
12044
12267
  await this.host.reactorClient.execute(document.header.id, "main", [actions.setSecretRef({
12045
12268
  id: existing?.id ?? generateId(),
@@ -12779,7 +13002,7 @@ var WorkflowRuntimeService = class {
12779
13002
  if (runId) run.span.setAttribute("run.id", runId);
12780
13003
  if (this.closed) throw new WorkflowRuntimeClosedError(workflowId);
12781
13004
  session = this.workers().session();
12782
- const { variables, secretValues } = await resolveVariables(runnable.variables, this.secretProvider());
13005
+ const { variables, secretValues } = await resolveVariables(runnable.variables, this.secretProvider(), workflowId);
12783
13006
  const journal = store;
12784
13007
  const journaledRunId = runId;
12785
13008
  const handed = /* @__PURE__ */ new Set();
@@ -12801,6 +13024,7 @@ var WorkflowRuntimeService = class {
12801
13024
  },
12802
13025
  executor,
12803
13026
  declaredPorts: blockPorts,
13027
+ blockDefaults: (block) => this.blockDefaults(block),
12804
13028
  ...secretValues.length > 0 ? { redactValues: secretValues } : {},
12805
13029
  triggerPayload,
12806
13030
  completedSteps: resume?.completedSteps,
@@ -13053,7 +13277,7 @@ var WorkflowRuntimeService = class {
13053
13277
  if ("error" in upstream) return refused(upstream.error);
13054
13278
  let resolved;
13055
13279
  try {
13056
- resolved = await resolveVariables(state.variables, this.secretProvider());
13280
+ resolved = await resolveVariables(state.variables, this.secretProvider(), workflowId);
13057
13281
  } catch (error) {
13058
13282
  return refused(error instanceof Error ? error.message : String(error));
13059
13283
  }
@@ -13114,6 +13338,10 @@ var WorkflowRuntimeService = class {
13114
13338
  }, () => runWorkflow({
13115
13339
  definition,
13116
13340
  executor,
13341
+ blockDefaults: async (block) => {
13342
+ const timeoutSeconds = (await this.blockDefaults(block))?.timeoutSeconds;
13343
+ return timeoutSeconds ? { timeoutSeconds } : void 0;
13344
+ },
13117
13345
  ...secretValues.length > 0 ? { redactValues: secretValues } : {},
13118
13346
  triggerPayload: upstream.triggerPayload,
13119
13347
  priorSteps: upstream.priorSteps,