@powerhousedao/reactor-workflow 6.2.3-dev.79 → 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,7 +1,7 @@
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-CJd29LsN.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
- import { open, readdir, rm, stat } from "node:fs/promises";
4
+ import { open, readFile, readdir, rm, stat } from "node:fs/promises";
5
5
  import { dirname, join } from "node:path";
6
6
  import { childLogger, generateId } from "document-model";
7
7
  import { DEFAULT_TIMEZONE, DOCUMENT_REF_KEY, ScheduleConfigError, checkTriggerStrategy, containsDocumentRef, documentRefsIn, expandDocumentRefs, isDocumentRefMarker, parseScheduleConfig, referenceDocuments } from "@powerhousedao/pieces-framework/workflow";
@@ -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
@@ -170,6 +171,23 @@ function createAttachmentPort(client, access) {
170
171
  //#region src/reactor/package-name.ts
171
172
  const WORKFLOW_PACKAGE_NAME = "@powerhousedao/workflow";
172
173
  //#endregion
174
+ //#region src/reactor/id-token.ts
175
+ /** The verified email in a connection auth's `data.id_token`, if there is one. */
176
+ function idTokenEmail(auth) {
177
+ const token = (auth?.data)?.id_token;
178
+ if (typeof token !== "string") return void 0;
179
+ const payload = token.split(".")[1];
180
+ if (!payload) return void 0;
181
+ let claims;
182
+ try {
183
+ claims = JSON.parse(Buffer.from(payload, "base64url").toString("utf8"));
184
+ } catch {
185
+ return;
186
+ }
187
+ if (claims.email_verified === false || claims.email_verified === "false") return;
188
+ return typeof claims.email === "string" && claims.email !== "" ? claims.email : void 0;
189
+ }
190
+ //#endregion
173
191
  //#region src/reactor/oauth.ts
174
192
  const OAUTH_CLIENT_ID = "client_id";
175
193
  const OAUTH_CLIENT_SECRET = "client_secret";
@@ -187,7 +205,10 @@ var OAuthError = class extends Error {
187
205
  }
188
206
  };
189
207
  async function up$2(db) {
190
- 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 {}
191
212
  }
192
213
  function fillOAuthUrl(template, props) {
193
214
  const filled = template.replace(/\{(\w+)\}/g, (_match, name) => {
@@ -221,14 +242,15 @@ var OAuthAttemptStore = class OAuthAttemptStore {
221
242
  await this.db.deleteFrom("oauth_attempt").where("expires_at", "<", (/* @__PURE__ */ new Date(now.getTime() - 864e5)).toISOString()).execute();
222
243
  const { method } = input;
223
244
  const state = base64url(randomBytes(32));
224
- const verifier = method.pkce === true ? base64url(randomBytes(48)) : null;
245
+ const verifier = method.pkce === true || input.pkce === true ? base64url(randomBytes(48)) : null;
225
246
  const authUrl = new URL(fillOAuthUrl(method.authUrl, input.props));
226
247
  const params = authUrl.searchParams;
227
248
  params.set("response_type", "code");
228
249
  params.set("client_id", input.clientId);
229
250
  params.set("redirect_uri", input.redirectUri);
230
251
  params.set("state", state);
231
- if (method.scope.length > 0) params.set("scope", method.scope.join(" "));
252
+ const scope = input.scopes ?? method.scope;
253
+ if (scope.length > 0) params.set("scope", scope.join(" "));
232
254
  params.set("access_type", "offline");
233
255
  const prompt = method.prompt ?? "consent";
234
256
  if (prompt !== "omit") params.set("prompt", prompt);
@@ -249,6 +271,7 @@ var OAuthAttemptStore = class OAuthAttemptStore {
249
271
  token_url: fillOAuthUrl(method.tokenUrl, input.props),
250
272
  authorization_method: method.authorizationMethod ?? null,
251
273
  return_url: input.returnUrl ?? null,
274
+ created_by: input.createdBy?.toLowerCase() ?? null,
252
275
  status: "PENDING",
253
276
  error: null,
254
277
  created_at: now.toISOString(),
@@ -401,13 +424,15 @@ function tokenSetFrom(response, context, previous) {
401
424
  token_url: context.token_url,
402
425
  ...context.authorization_method ? { authorization_method: context.authorization_method } : {},
403
426
  redirect_url: context.redirect_url,
427
+ ...context.client_id ?? previous?.client_id ? { client_id: context.client_id ?? previous?.client_id } : {},
428
+ ...context.client_secret_ref ?? previous?.client_secret_ref ? { client_secret_ref: context.client_secret_ref ?? previous?.client_secret_ref } : {},
404
429
  data: {
405
430
  ...previous?.data,
406
431
  ...response
407
432
  }
408
433
  };
409
434
  }
410
- async function exchangeCode(attempt, code, clientSecret, egress) {
435
+ async function exchangeCode(attempt, code, clientSecret, egress, fromRegistry = false) {
411
436
  const method = authorizationMethod(attempt.authorization_method);
412
437
  const auth = clientAuth$1(method, attempt.client_id, clientSecret);
413
438
  return tokenSetFrom(await postTokenForm(attempt.token_url, {
@@ -419,7 +444,11 @@ async function exchangeCode(attempt, code, clientSecret, egress) {
419
444
  }, auth.headers, egress), {
420
445
  token_url: attempt.token_url,
421
446
  authorization_method: method,
422
- redirect_url: attempt.redirect_uri
447
+ redirect_url: attempt.redirect_uri,
448
+ ...fromRegistry ? {
449
+ client_id: attempt.client_id,
450
+ client_secret_ref: attempt.client_secret_ref
451
+ } : {}
423
452
  });
424
453
  }
425
454
  function parseTokenSet(value) {
@@ -444,24 +473,36 @@ var StoreTokenRefresher = class {
444
473
  this.secrets = secrets;
445
474
  this.egress = egress;
446
475
  }
447
- refreshIfDue(source) {
476
+ refreshIfDue(source, connectionId) {
448
477
  const refOf = (name) => source.secretRefs?.find((entry) => entry.name === name)?.ref;
449
478
  const tokenRef = refOf(OAUTH_TOKEN);
479
+ if (!tokenRef) return Promise.resolve();
450
480
  const secretRef = refOf(OAUTH_CLIENT_SECRET);
451
- const clientId = source.config?.[OAUTH_CLIENT_ID];
452
- if (!tokenRef || !secretRef || typeof clientId !== "string") return Promise.resolve();
481
+ const configId = source.config?.[OAUTH_CLIENT_ID];
482
+ const own = secretRef && typeof configId === "string" ? {
483
+ secretRef,
484
+ clientId: configId
485
+ } : void 0;
453
486
  let pending = this.inFlight.get(tokenRef);
454
487
  if (!pending) {
455
- pending = this.refresh(tokenRef, secretRef, clientId).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));
456
493
  this.inFlight.set(tokenRef, pending);
457
494
  }
458
495
  return pending;
459
496
  }
460
- async refresh(tokenRef, secretRef, clientId) {
497
+ async refresh(tokenRef, own, owner) {
461
498
  const store = await this.secrets();
462
- const tokens = parseTokenSet(await store.get(tokenRef));
499
+ const read = (ref) => store.get(ref, owner);
500
+ const tokens = parseTokenSet(await read(tokenRef));
463
501
  if (!tokenDue(tokens, Math.round(Date.now() / 1e3))) return;
464
- const auth = clientAuth$1(tokens.authorization_method, clientId, await store.get(secretRef));
502
+ const clientId = own?.clientId ?? tokens.client_id;
503
+ const secretRef = own?.secretRef ?? tokens.client_secret_ref;
504
+ if (!clientId || !secretRef) return;
505
+ const auth = clientAuth$1(tokens.authorization_method, clientId, own ? await read(secretRef) : await store.get(secretRef, null));
465
506
  let response;
466
507
  try {
467
508
  response = await postTokenForm(tokens.token_url, {
@@ -477,7 +518,7 @@ var StoreTokenRefresher = class {
477
518
  }
478
519
  throw new OAuthError(`Refreshing the OAuth2 token failed; reconnect: ${detail}`);
479
520
  }
480
- const current = parseTokenSet(await store.get(tokenRef));
521
+ const current = parseTokenSet(await read(tokenRef));
481
522
  if (current.access_token !== tokens.access_token || current.claimed_at !== tokens.claimed_at) return;
482
523
  await store.rotate(tokenRef, JSON.stringify(tokenSetFrom(response, tokens, tokens)));
483
524
  }
@@ -1075,6 +1116,8 @@ function detailResult(descriptor, pieceName, version) {
1075
1116
  props: action.props,
1076
1117
  requireAuth: action.requireAuth,
1077
1118
  ...action.requireReactor ? { requireReactor: action.requireReactor } : {},
1119
+ ...action.defaultTimeoutSeconds !== void 0 ? { defaultTimeoutSeconds: action.defaultTimeoutSeconds } : {},
1120
+ ...action.defaultRetry ? { defaultRetry: action.defaultRetry } : {},
1078
1121
  outputSchema: action.outputSchema,
1079
1122
  audience: action.audience
1080
1123
  }])),
@@ -1621,8 +1664,8 @@ async function resolveConnectionWithSecrets(document, secrets, request, oauth) {
1621
1664
  config: state.config ?? {},
1622
1665
  secretRefs: state.secretRefs
1623
1666
  };
1624
- if (source.authType === "OAUTH2") await oauth?.refreshIfDue(source);
1625
- return shapeConnection(source, secrets);
1667
+ if (source.authType === "OAUTH2") await oauth?.refreshIfDue(source, document.header.id);
1668
+ return shapeConnection(source, secrets, document.header.id);
1626
1669
  }
1627
1670
  const EGRESS_ALLOW_ENV = "PH_WORKFLOWS_EGRESS_ALLOW_ADDRESSES";
1628
1671
  function asCidr(entry) {
@@ -1873,6 +1916,18 @@ var ReactorError = class extends Error {
1873
1916
  function accessDenied(message) {
1874
1917
  return new ReactorError(ReactorAccessDeniedError, message);
1875
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
+ }
1876
1931
  function refusedMethod(method) {
1877
1932
  return accessDenied(`${method} is not available to workflow pieces`);
1878
1933
  }
@@ -1967,27 +2022,9 @@ function jobPending(jobId, status) {
1967
2022
  const AUTHORIZATION_DENIED = "AuthorizationDeniedError";
1968
2023
  function jobFailed(job) {
1969
2024
  const error = job.error;
1970
- 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}`);
1971
2026
  return new ReactorError(ReactorJobFailedError, `Reactor job ${job.id} failed: ${job.error?.message ?? "unknown error"}`);
1972
2027
  }
1973
- function assertActionsApplied(job, actions) {
1974
- const outcomes = job.result?.actions ?? [];
1975
- const byId = new Map(outcomes.map((outcome) => [outcome.actionId, outcome]));
1976
- const failed = (actions?.map((action) => ({
1977
- id: action.id,
1978
- label: action.type
1979
- })) ?? outcomes.map((outcome) => ({
1980
- id: outcome.actionId,
1981
- label: outcome.actionId
1982
- }))).flatMap(({ id, label }) => {
1983
- const outcome = byId.get(id);
1984
- if (!outcome) return [`Action ${label} produced no operation`];
1985
- if (outcome.kind === "reducer-error") return [`Action ${label} failed: ${outcome.message}`];
1986
- if (outcome.kind === "denied") return [`Action ${label} was denied: ${outcome.reason}`];
1987
- return [];
1988
- });
1989
- if (failed.length > 0) throw new ReactorError(ReactorActionsFailedError, failed.join("; "));
1990
- }
1991
2028
  const DOCUMENT_DRIVE = "powerhouse/document-drive";
1992
2029
  const REACTOR_DRIVE = "powerhouse/reactor-drive";
1993
2030
  function isDriveType(documentType) {
@@ -2044,6 +2081,8 @@ var RunScopedReactorClient = class {
2044
2081
  options;
2045
2082
  stopAt;
2046
2083
  maxPageLimit;
2084
+ jobs = /* @__PURE__ */ new Set();
2085
+ handles;
2047
2086
  constructor(inner, scope, host, options = {}) {
2048
2087
  this.inner = inner;
2049
2088
  this.scope = scope;
@@ -2051,6 +2090,7 @@ var RunScopedReactorClient = class {
2051
2090
  this.options = options;
2052
2091
  this.stopAt = scope.deadline - deadlineMargin(scope.deadline - Date.now());
2053
2092
  this.maxPageLimit = options.maxPageLimit ?? 100;
2093
+ this.handles = new WriteHandleFactory(remoteStagePort(this.read));
2054
2094
  }
2055
2095
  async getDocumentModelModules(namespace, paging, signal) {
2056
2096
  this.assertAccess();
@@ -2064,8 +2104,8 @@ var RunScopedReactorClient = class {
2064
2104
  this.assertAccess();
2065
2105
  return this.inner.getDocumentModelModuleForDocument(document);
2066
2106
  }
2067
- async get(identifier, view, signal) {
2068
- 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);
2069
2109
  await this.scope.journal.recordDocuments([document.header.id]);
2070
2110
  return document;
2071
2111
  }
@@ -2094,17 +2134,45 @@ var RunScopedReactorClient = class {
2094
2134
  async find(search, view, paging, signal) {
2095
2135
  return this.documents(await this.inner.find(search, this.readView(view), this.paging(paging), this.signal(signal)));
2096
2136
  }
2137
+ async query(request, signal) {
2138
+ const queryIndex = this.host.queryIndex;
2139
+ if (!queryIndex) throw accessDenied("This host serves no read-model indexes to pieces (reactor.query)");
2140
+ this.assertAccess();
2141
+ return queryIndex(request, this.scope.runUser, this.signal(signal));
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
+ };
2097
2164
  async create(document, parentIdentifier, signal) {
2098
2165
  this.assertWritable("create");
2099
2166
  const abort = this.signal(signal);
2100
- const parentId = parentIdentifier === void 0 ? void 0 : await this.resolveReference(parentIdentifier, "main", abort);
2101
- 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);
2102
2170
  }
2103
2171
  async newDocument(documentModelType, options, signal) {
2104
2172
  this.assertWritable("newDocument");
2105
2173
  const abort = this.signal(signal);
2106
2174
  const module = await this.modelModule(documentModelType, options?.documentModelVersion, abort);
2107
- const parentId = options?.parentIdentifier === void 0 ? void 0 : await this.resolveReference(options.parentIdentifier, "main", abort);
2175
+ const parentId = await this.parentOf(options?.parentIdentifier, abort);
2108
2176
  const base = await this.inner.getCreateProtocolVersions({
2109
2177
  documentType: documentModelType,
2110
2178
  parentIdentifier: parentId
@@ -2112,24 +2180,40 @@ var RunScopedReactorClient = class {
2112
2180
  return createEmptyDocument(module, options, await this.inner.getCreateSignaturePolicy(), base);
2113
2181
  }
2114
2182
  async createEmpty(documentModelType, options, signal) {
2115
- this.assertWritable("createEmpty");
2116
2183
  const abort = this.signal(signal);
2117
- const document = await this.newDocument(documentModelType, options, abort);
2118
- const parentId = options?.parentIdentifier === void 0 ? void 0 : await this.resolveReference(options.parentIdentifier, "main", abort);
2119
- return this.createDocument(document, parentId, abort);
2184
+ const handle = await this.writeCreateEmpty("createEmpty", documentModelType, options, abort);
2185
+ return this.createdBack(handle, abort);
2120
2186
  }
2121
2187
  async execute(documentIdentifier, branch, actions, signal, subject) {
2122
2188
  if (subject !== void 0) throw accessDenied("A piece may not choose the subject it reads as");
2123
2189
  const abort = this.signal(signal);
2124
- const { id } = await this.apply("execute", documentIdentifier, branch, actions, abort);
2125
- 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);
2126
2193
  }
2127
2194
  async executeAsync(documentIdentifier, branch, actions, signal) {
2128
2195
  const abort = this.signal(signal);
2129
- const { job } = await this.apply("executeAsync", documentIdentifier, branch, actions, abort);
2130
- 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);
2203
+ }
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);
2131
2209
  }
2132
- async apply(method, documentIdentifier, branch, actions, abort) {
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) {
2133
2217
  this.assertWritable(method);
2134
2218
  for (const action of actions) {
2135
2219
  if (action.context?.signer) throw accessDenied(`Action ${action.type} carries a signer; the host signs every action`);
@@ -2137,26 +2221,17 @@ var RunScopedReactorClient = class {
2137
2221
  }
2138
2222
  const id = await this.resolveReference(documentIdentifier, branch, abort);
2139
2223
  await this.gate(id, branch, actions, abort);
2140
- return {
2141
- id,
2142
- job: await this.run(id, branch, actions, abort)
2143
- };
2224
+ return this.submit(id, branch, actions, abort);
2144
2225
  }
2145
- async deleteDocument(identifier, propagate, signal) {
2146
- this.assertWritable("deleteDocument");
2226
+ async submitDelete(method, identifier, propagate, abort) {
2227
+ this.assertWritable(method);
2147
2228
  if (propagate === PropagationMode.Cascade) throw accessDenied("Cascade delete is not offered to pieces");
2148
- const abort = this.signal(signal);
2149
2229
  const id = await this.resolveReference(identifier, "main", abort);
2150
2230
  await this.gate(id, "main", [deleteDocumentAction(id)], abort);
2151
2231
  this.assertTimeLeft("a delete");
2152
2232
  await this.scope.journal.recordDocuments([id]);
2153
- try {
2154
- await this.inner.deleteDocument(id, void 0, abort);
2155
- } catch (error) {
2156
- const message = messageOf(error);
2157
- if (message.startsWith("Authorization denied")) throw accessDenied(`The Switchboard has no grant for this operation: ${message}`);
2158
- throw error;
2159
- }
2233
+ const submitted = await this.inner.write.deleteDocument(id, void 0, abort);
2234
+ return this.handle(submitted, await this.track(submitted, () => [id]));
2160
2235
  }
2161
2236
  drives = {
2162
2237
  getNode: (driveIdentifier, nodeId, signal) => this.getNode(driveIdentifier, nodeId, signal),
@@ -2175,31 +2250,39 @@ var RunScopedReactorClient = class {
2175
2250
  return this.inner.drives.listNodes(drive.id, parentFolder, this.paging(paging), abort);
2176
2251
  }
2177
2252
  async addFile(driveIdentifier, document, parentFolder, signal) {
2178
- this.assertWritable("drives.addFile");
2179
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);
2180
2267
  const drive = await this.readDrive(driveIdentifier, abort);
2181
2268
  const prepared = withSignaturePolicy(document, await this.inner.getCreateSignaturePolicy());
2182
2269
  const documentId = prepared.header.id;
2183
2270
  await this.gateCandidates(drive.id, "main", [fileCandidate(drive, prepared, parentFolder)], abort);
2184
2271
  this.assertTimeLeft(`a ${prepared.header.documentType} document`);
2185
2272
  await this.scope.journal.recordDocuments([drive.id, documentId]);
2186
- await this.inner.drives.addFile(drive.id, prepared, parentFolder, abort);
2187
- try {
2188
- await this.grantRunUser(documentId, abort);
2189
- return await this.readBack(documentId, prepared.header.branch || "main", abort);
2190
- } catch (error) {
2191
- if (error instanceof Error) error.message = `Document ${documentId} was created, but: ${error.message}`;
2192
- throw error;
2193
- }
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);
2194
2276
  }
2195
- async addFolder(driveIdentifier, name, parentFolder, signal) {
2196
- this.assertWritable("drives.addFolder");
2197
- const abort = this.signal(signal);
2277
+ async submitAddFolder(method, driveIdentifier, name, parentFolder, abort) {
2278
+ this.assertWritable(method);
2198
2279
  const drive = await this.readDrive(driveIdentifier, abort);
2199
2280
  await this.gateCandidates(drive.id, "main", [folderCandidate(drive, name, parentFolder)], abort);
2200
2281
  this.assertTimeLeft("a folder");
2201
2282
  await this.scope.journal.recordDocuments([drive.id]);
2202
- 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 });
2203
2286
  }
2204
2287
  async readDrive(driveIdentifier, signal) {
2205
2288
  const { id, documentType } = (await this.inner.get(driveIdentifier, this.readView({ scopes: ["document"] }), signal)).header;
@@ -2254,21 +2337,66 @@ var RunScopedReactorClient = class {
2254
2337
  } catch (error) {
2255
2338
  throw accessDenied(`Could not check the run user's access to document ${documentId}: ${messageOf(error)}`);
2256
2339
  }
2257
- const denied = answer.evaluations.flatMap((evaluation, index) => evaluation.decision === "deny" ? [`${candidates[index].type} (${evaluation.reason})`] : []);
2258
- 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);
2259
2346
  }
2260
- async run(documentId, branch, actions, signal) {
2347
+ async submit(documentId, branch, actions, signal) {
2261
2348
  this.assertTimeLeft(`${actions.length} action(s)`);
2262
- const info = await this.inner.executeAsync(documentId, branch, actions, signal);
2263
- await this.scope.journal.recordJob(info.id, [documentId]);
2264
- const settled = await this.settle(info.id, signal);
2265
- if (settled.status === JobStatus.FAILED) throw jobFailed(settled);
2266
- assertActionsApplied(settled, actions);
2267
- 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
+ }
2268
2393
  }
2269
2394
  assertTimeLeft(what) {
2270
2395
  if (Date.now() >= this.stopAt) throw new Error(`No time was left before the step deadline to submit ${what}; nothing was submitted`);
2271
2396
  }
2397
+ assertOwnJob(jobId) {
2398
+ if (!this.jobs.has(jobId)) throw accessDenied(`Job ${String(jobId)} was not submitted by this step`);
2399
+ }
2272
2400
  async settle(jobId, signal) {
2273
2401
  let status = JobStatus.PENDING;
2274
2402
  for (;;) {
@@ -2279,7 +2407,7 @@ var RunScopedReactorClient = class {
2279
2407
  try {
2280
2408
  job = await this.inner.waitForJob(jobId, wait);
2281
2409
  } catch (error) {
2282
- if (!wait.aborted) throw error;
2410
+ if (!wait.aborted || signal.aborted) throw error;
2283
2411
  job = await this.inner.getJobStatus(jobId).catch(() => {
2284
2412
  throw jobPending(jobId, status);
2285
2413
  });
@@ -2301,32 +2429,32 @@ var RunScopedReactorClient = class {
2301
2429
  await this.scope.journal.recordDocuments([documentId]);
2302
2430
  return document;
2303
2431
  }
2304
- async createDocument(document, parentId, signal) {
2432
+ async submitCreate(document, parentId, signal) {
2305
2433
  const prepared = withSignaturePolicy(document, await this.inner.getCreateSignaturePolicy());
2306
2434
  const documentId = prepared.header.id;
2307
2435
  this.assertTimeLeft(`a ${prepared.header.documentType} document`);
2308
- const { jobs } = await this.inner.createAsync(prepared, parentId, signal);
2309
- const ordered = Object.entries(jobs).sort(([a], [b]) => Number(b === "create") - Number(a === "create"));
2310
- for (const [key, job] of ordered) await this.scope.journal.recordJob(job.id, key === "create" || parentId === void 0 ? [documentId] : [parentId, documentId]);
2311
- 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);
2312
2442
  try {
2313
- for (const [, job] of ordered) {
2314
- const settled = await this.settle(job.id, signal);
2315
- if (settled.status === JobStatus.FAILED) throw jobFailed(settled);
2316
- assertActionsApplied(settled);
2317
- 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;
2318
2446
  }
2319
- await this.grantRunUser(documentId, signal);
2320
- 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);
2321
2449
  } catch (error) {
2322
- 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}`;
2323
2451
  throw error;
2324
2452
  }
2325
2453
  }
2326
2454
  async grantRunUser(documentId, signal) {
2327
2455
  const runUser = this.scope.runUser;
2328
- if (!runUser) return;
2329
- 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;
2330
2458
  const grants = [{
2331
2459
  id: "run-user",
2332
2460
  description: "The user whose workflow run created this document",
@@ -2341,11 +2469,14 @@ var RunScopedReactorClient = class {
2341
2469
  principal: this.options.hostPrincipal,
2342
2470
  capability: { can: "execute" }
2343
2471
  });
2344
- await this.run(documentId, "main", [initializeAuth({
2472
+ return this.run(documentId, "main", [initializeAuth({
2345
2473
  version: 1,
2346
2474
  grants
2347
2475
  })], signal);
2348
2476
  }
2477
+ parentOf(identifier, signal) {
2478
+ return identifier === void 0 ? Promise.resolve(void 0) : this.resolveReference(identifier, "main", signal);
2479
+ }
2349
2480
  async resolveReference(identifier, branch, signal) {
2350
2481
  try {
2351
2482
  return await this.inner.resolveIdOrSlug(identifier, { branch }, signal);
@@ -2383,6 +2514,7 @@ const SERVED = {
2383
2514
  get: true,
2384
2515
  resolveIdOrSlug: true,
2385
2516
  find: true,
2517
+ query: true,
2386
2518
  getOperations: true,
2387
2519
  getOutgoingRelationships: true,
2388
2520
  getIncomingRelationships: true,
@@ -2409,6 +2541,36 @@ const REFUSED_DRIVES = {
2409
2541
  copyNode: true,
2410
2542
  setPreferredEditorOnNode: true
2411
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
+ };
2412
2574
  const REFUSED = {
2413
2575
  getMany: true,
2414
2576
  createDocumentInDrive: true,
@@ -2436,6 +2598,10 @@ const REFUSED = {
2436
2598
  function servedClient(client) {
2437
2599
  const facade = served(client, SERVED, REFUSED, "");
2438
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.");
2439
2605
  return facade;
2440
2606
  }
2441
2607
  function served(methods, serve, refuse, prefix) {
@@ -2471,7 +2637,11 @@ function reactorTap(host, base, journal, options = {}, telemetry) {
2471
2637
  journal
2472
2638
  }, host, clientOptions));
2473
2639
  const parent = context.active();
2474
- 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);
2475
2645
  server.start();
2476
2646
  return () => server.stop();
2477
2647
  }
@@ -2516,7 +2686,10 @@ const LEGACY_MASTER_KEY_ENV = "PH_SECRETS_MASTER_KEY";
2516
2686
  const KEY_CHECK_ID = "master";
2517
2687
  const KEY_CHECK_LABEL = "reactor-workflow/secrets/key-check/v1";
2518
2688
  async function up$1(db) {
2519
- 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 {}
2520
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();
2521
2694
  }
2522
2695
  function legacyKeyHint() {
@@ -2628,6 +2801,8 @@ var LocalEncryptedSecretStore = class LocalEncryptedSecretStore {
2628
2801
  return {
2629
2802
  ref: secretRefFromId(row.id),
2630
2803
  label: row.label,
2804
+ ownerId: row.owner_id,
2805
+ createdBy: row.created_by,
2631
2806
  version: row.version,
2632
2807
  status: row.status,
2633
2808
  createdAt: row.created_at,
@@ -2635,10 +2810,13 @@ var LocalEncryptedSecretStore = class LocalEncryptedSecretStore {
2635
2810
  };
2636
2811
  }
2637
2812
  async create(input) {
2813
+ if (input.ownerId === "") throw new Error("A secret needs an owning document");
2638
2814
  const now = (/* @__PURE__ */ new Date()).toISOString();
2639
2815
  const row = {
2640
2816
  id: randomBytes(16).toString("hex"),
2641
2817
  label: input.label ?? null,
2818
+ owner_id: input.ownerId,
2819
+ created_by: input.createdBy?.toLowerCase() ?? null,
2642
2820
  version: 1,
2643
2821
  enc: this.encrypt(input.value),
2644
2822
  status: "ACTIVE",
@@ -2664,8 +2842,11 @@ var LocalEncryptedSecretStore = class LocalEncryptedSecretStore {
2664
2842
  }).where("id", "=", row.id).execute();
2665
2843
  return this.toStat(updated);
2666
2844
  }
2667
- async get(ref) {
2845
+ async get(ref, owner) {
2668
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);
2669
2850
  if (row.status !== "ACTIVE" || row.enc === null) throw new SecretDeletedError(ref);
2670
2851
  return this.decrypt(row.enc);
2671
2852
  }
@@ -2728,8 +2909,9 @@ function coerce(variable) {
2728
2909
  }
2729
2910
  }
2730
2911
  /** Coerces typed variables and resolves SECRET ones through the secret store.
2731
- * The error for a SECRET names its ref, never a value. */
2732
- 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) {
2733
2915
  const secretValues = [];
2734
2916
  const resolved = [];
2735
2917
  for (const variable of variables) {
@@ -2750,7 +2932,10 @@ async function resolveVariables(variables, secrets) {
2750
2932
  if (typeof variable.value !== "string") throw new VariableTypeError(`Variable "${variable.key}" is a SECRET, but its value is not a secret reference`);
2751
2933
  let value;
2752
2934
  try {
2753
- value = await secrets.get(variable.value);
2935
+ value = await secrets.get(variable.value, {
2936
+ documentId: workflowId,
2937
+ kind: "workflow"
2938
+ });
2754
2939
  } catch (error) {
2755
2940
  const detail = error instanceof Error ? error.message : String(error);
2756
2941
  throw new VariableTypeError(`Secret variable "${variable.key}" could not be resolved: ${detail}`);
@@ -9199,6 +9384,28 @@ var TriggerSupervisor = class {
9199
9384
  else await store.clearWorkflowPark(workflowId);
9200
9385
  });
9201
9386
  }
9387
+ /**
9388
+ * Makes an armed trigger due now and runs the due check at once, so a
9389
+ * schedule fires and a poll polls without waiting for its interval ("sync
9390
+ * now"). The next one follows the interval from there. False when the
9391
+ * workflow has no armed trigger (disabled, parked, erroring, unknown).
9392
+ */
9393
+ async pollNow(workflowId) {
9394
+ if (this.stopped) return false;
9395
+ const store = await this.store();
9396
+ if (!store) return false;
9397
+ const row = await store.getTriggerState(workflowId);
9398
+ if (!row || row.status !== "ENABLED") return false;
9399
+ await store.upsertTriggerState({
9400
+ ...row,
9401
+ next_poll_at: this.now().toISOString()
9402
+ });
9403
+ this.tick().catch((error) => {
9404
+ if (isShutdownRefusal(error)) return;
9405
+ logger$2.error("Trigger tick after poll-now failed: @error", error);
9406
+ });
9407
+ return true;
9408
+ }
9202
9409
  /** A re-publish past the parked version: on this lane, a park older than
9203
9410
  * `publishedVersion` goes and a row it parked is ENABLED again, as it was
9204
9411
  * before (only an ENABLED row is ever parked); a park as new stands. The
@@ -9716,6 +9923,7 @@ var TriggerSupervisor = class {
9716
9923
  started.push(this.startOnLane(row.workflow_id, () => store.setTriggerStatus(row.workflow_id, "DISABLED"), false));
9717
9924
  continue;
9718
9925
  }
9926
+ if (!isSchedule(binding) && this.options.pollHeld?.(row.workflow_id)) continue;
9719
9927
  started.push(isSchedule(binding) ? this.startOnLane(row.workflow_id, this.whileBound(binding, () => this.fireSchedule(store, row, binding)), false) : this.startOnLane(row.workflow_id, this.whileBound(binding, () => this.poll(store, row, binding)), true));
9720
9928
  }
9721
9929
  this.throwIfStopped();
@@ -10071,6 +10279,126 @@ function matchesLifecycleFilter(filter, documentType, driveId) {
10071
10279
  return ok(filter.documentType, documentType ?? void 0) && ok(filter.driveId, driveId ?? void 0);
10072
10280
  }
10073
10281
  //#endregion
10282
+ //#region src/reactor/oauth-apps.ts
10283
+ /** The connection config key naming an app of the registry. */
10284
+ const OAUTH_APP = "oauth_app";
10285
+ /**
10286
+ * The connection config key for the scopes to ask for instead of the piece's
10287
+ * own: a list, or one space-separated string. A read-only integration asks
10288
+ * for `calendar.readonly` where the piece would ask to edit too.
10289
+ */
10290
+ const OAUTH_SCOPES = "oauth_scopes";
10291
+ var StaticOAuthAppRegistry = class {
10292
+ entries;
10293
+ constructor(entries) {
10294
+ this.entries = entries;
10295
+ }
10296
+ client(appId, kind) {
10297
+ return this.entries.find((e) => e.appId === appId && e.kind === kind);
10298
+ }
10299
+ clients(appId) {
10300
+ return this.entries.filter((e) => e.appId === appId);
10301
+ }
10302
+ };
10303
+ const LOOPBACK = /* @__PURE__ */ new Set([
10304
+ "localhost",
10305
+ "127.0.0.1",
10306
+ "[::1]",
10307
+ "::1"
10308
+ ]);
10309
+ /** Desktop for a callback to this machine, web for one the provider reaches over the internet. */
10310
+ function clientKindFor(redirectUri) {
10311
+ let host;
10312
+ try {
10313
+ host = new URL(redirectUri).hostname;
10314
+ } catch {
10315
+ return "web";
10316
+ }
10317
+ return LOOPBACK.has(host) ? "desktop" : "web";
10318
+ }
10319
+ /**
10320
+ * Config key the runtime writes after a sign-in: the scopes the provider
10321
+ * granted (its token response's `scope`, else the ones asked for). A
10322
+ * connection whose `oauth_scopes` reach beyond them needs a new sign-in.
10323
+ */
10324
+ const OAUTH_GRANTED_SCOPES = "oauth_granted_scopes";
10325
+ /** What a sign-in was granted: the token's own `scope`, else what the connection asked for. */
10326
+ function grantedScopes(tokenScope, config) {
10327
+ const granted = (tokenScope ?? "").split(/[\s,]+/).filter(Boolean);
10328
+ return granted.length > 0 ? granted : scopesOverride(config);
10329
+ }
10330
+ /** The scopes a connection asks for instead of the piece's, or undefined to keep the piece's. */
10331
+ function scopesOverride(config) {
10332
+ const raw = config[OAUTH_SCOPES];
10333
+ const scopes = (Array.isArray(raw) ? raw.filter((s) => typeof s === "string") : typeof raw === "string" ? raw.split(/\s+/) : []).map((s) => s.trim()).filter(Boolean);
10334
+ return scopes.length > 0 ? scopes : void 0;
10335
+ }
10336
+ /**
10337
+ * The client id and secret in a provider's client file: Google's
10338
+ * `{ installed | web: { client_id, client_secret } }`, or the two keys at the
10339
+ * top level.
10340
+ */
10341
+ function parseClientFile(text) {
10342
+ let parsed;
10343
+ try {
10344
+ parsed = JSON.parse(text);
10345
+ } catch {
10346
+ throw new Error("not JSON");
10347
+ }
10348
+ const root = parsed ?? {};
10349
+ const inner = root.installed ?? root.web ?? root;
10350
+ const clientId = inner.client_id;
10351
+ const clientSecret = inner.client_secret;
10352
+ if (typeof clientId !== "string" || !clientId) throw new Error("no client_id");
10353
+ if (typeof clientSecret !== "string" || !clientSecret) throw new Error("no client_secret");
10354
+ return {
10355
+ clientId,
10356
+ clientSecret
10357
+ };
10358
+ }
10359
+ /** The label a client secret carries in the store, so a restart finds it again. */
10360
+ function clientSecretLabel(appId, kind) {
10361
+ return `oauth-app:${appId}:${kind}`;
10362
+ }
10363
+ /**
10364
+ * Reads each configured client file and keeps its secret in the store under a
10365
+ * fixed label: created the first time, rotated when the file changed, left
10366
+ * alone otherwise. A client whose file cannot be read is skipped with the
10367
+ * reason, so one bad file does not take the others down.
10368
+ */
10369
+ async function loadOAuthApps(definitions, store, options = {}) {
10370
+ const read = options.read ?? ((path) => readFile(path, "utf8"));
10371
+ const existing = new Map((await store.list()).filter((s) => s.status === "ACTIVE" && s.label && s.ownerId === null).map((s) => [s.label, s.ref]));
10372
+ const entries = [];
10373
+ for (const app of definitions) for (const kind of ["desktop", "web"]) {
10374
+ const path = app.clients[kind];
10375
+ if (!path) continue;
10376
+ let client;
10377
+ try {
10378
+ client = parseClientFile(await read(path));
10379
+ } catch (error) {
10380
+ options.onSkip?.(app.id, kind, `${path}: ${error instanceof Error ? error.message : String(error)}`);
10381
+ continue;
10382
+ }
10383
+ const label = clientSecretLabel(app.id, kind);
10384
+ let ref = existing.get(label);
10385
+ if (ref) {
10386
+ if (await store.get(ref, null) !== client.clientSecret) await store.rotate(ref, client.clientSecret);
10387
+ } else ref = (await store.create({
10388
+ value: client.clientSecret,
10389
+ label,
10390
+ ownerId: null
10391
+ })).ref;
10392
+ entries.push({
10393
+ appId: app.id,
10394
+ kind,
10395
+ clientId: client.clientId,
10396
+ clientSecretRef: ref
10397
+ });
10398
+ }
10399
+ return new StaticOAuthAppRegistry(entries);
10400
+ }
10401
+ //#endregion
10074
10402
  //#region src/reactor/service.ts
10075
10403
  function targetOf(block) {
10076
10404
  return {
@@ -10113,6 +10441,7 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms).unref());
10113
10441
  const logger$1 = childLogger(["workflow", "runtime"]);
10114
10442
  const DRIVE_DOCUMENT_TYPE = "powerhouse/document-drive";
10115
10443
  const WORKFLOW_DOCUMENT_TYPE = "powerhouse/workflow";
10444
+ const CONNECTION_DOCUMENT_TYPE = "powerhouse/connection";
10116
10445
  const DOCUMENT_SCOPE = "document";
10117
10446
  const CHILD_RELATIONSHIP = "child";
10118
10447
  function operationKey(op) {
@@ -10293,6 +10622,7 @@ var WorkflowRuntimeService = class {
10293
10622
  secretsPromise;
10294
10623
  oauthAttemptsPromise;
10295
10624
  tokenRefresher;
10625
+ oauthAppsPromise;
10296
10626
  registry = /* @__PURE__ */ new Map();
10297
10627
  seedPromise;
10298
10628
  seedError;
@@ -10382,12 +10712,66 @@ var WorkflowRuntimeService = class {
10382
10712
  return this.secretsPromise;
10383
10713
  }
10384
10714
  secretProvider() {
10385
- 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);
10386
10765
  }
10387
10766
  oauthAttempts() {
10388
10767
  this.oauthAttemptsPromise ??= OAuthAttemptStore.create(this.host.relationalDb);
10389
10768
  return this.oauthAttemptsPromise;
10390
10769
  }
10770
+ /** The host's OAuth2 apps, their client secrets loaded into the store once. */
10771
+ oauthApps() {
10772
+ this.oauthAppsPromise ??= this.secrets().then((store) => loadOAuthApps(this.host.oauthApps ?? [], store, { onSkip: (appId, kind, reason) => logger$1.warn(`OAuth2 app "${appId}" has no usable ${kind} client: ${reason}`) }));
10773
+ return this.oauthAppsPromise;
10774
+ }
10391
10775
  oauthRefresher() {
10392
10776
  return this.tokenRefresher ??= new StoreTokenRefresher(() => this.secrets(), this.designEgress);
10393
10777
  }
@@ -11129,6 +11513,7 @@ var WorkflowRuntimeService = class {
11129
11513
  fire: (workflowId, payload, kind, runId) => {
11130
11514
  this.startFiring(workflowId, payload, kind, runId);
11131
11515
  },
11516
+ pollHeld: (workflowId) => this.pollHeld(workflowId),
11132
11517
  webhookUrlFor: async (workflowId) => (await this.mintWebhookEndpoint(workflowId))?.url,
11133
11518
  reactorAccess: (binding, requireReactor, runUser) => this.triggerReactorAccess(binding, requireReactor, runUser),
11134
11519
  models: this.models,
@@ -11199,6 +11584,44 @@ var WorkflowRuntimeService = class {
11199
11584
  this.unobservePool = void 0;
11200
11585
  this.designWorker = void 0;
11201
11586
  }
11587
+ /**
11588
+ * Runs a workflow's trigger now, as its schedule or poll would (a sync
11589
+ * now): a schedule fires the workflow, a polling trigger polls its source.
11590
+ * Never a backfill's pace. False when it has no armed trigger.
11591
+ */
11592
+ async pollNow(workflowId, ctx) {
11593
+ if (this.closed) return false;
11594
+ await this.assertCanReadWorkflow(workflowId, ctx, void 0);
11595
+ return this.supervisor().pollNow(workflowId);
11596
+ }
11597
+ /**
11598
+ * How one workflow's trigger stands, for host-side code that keeps
11599
+ * workflows running (a processor reconciling the workflows it built): the
11600
+ * trigger row, and the park that holds it unarmed. Not caller-filtered:
11601
+ * GraphQL callers go through triggerStates.
11602
+ */
11603
+ async workflowHealth(workflowId) {
11604
+ const store = await this.store();
11605
+ if (!store) return {
11606
+ trigger: null,
11607
+ park: null
11608
+ };
11609
+ const [row, park] = await Promise.all([store.getTriggerState(workflowId), store.getWorkflowPark(workflowId)]);
11610
+ return {
11611
+ trigger: row ? {
11612
+ status: row.status,
11613
+ lastError: row.last_error,
11614
+ lastPollAt: row.last_poll_at,
11615
+ nextPollAt: row.next_poll_at,
11616
+ consecutiveFailures: row.consecutive_failures
11617
+ } : null,
11618
+ park: park ? {
11619
+ reason: park.reason,
11620
+ parkedAt: park.parked_at,
11621
+ publishedVersion: park.published_version
11622
+ } : null
11623
+ };
11624
+ }
11202
11625
  async triggerStates(ctx) {
11203
11626
  const store = await this.store();
11204
11627
  if (!store) return [];
@@ -11419,7 +11842,10 @@ var WorkflowRuntimeService = class {
11419
11842
  async webhookSecret(config, workflowId) {
11420
11843
  if (!config.secretRef) return void 0;
11421
11844
  try {
11422
- return await (await this.secrets()).get(config.secretRef);
11845
+ return await (await this.secrets()).get(config.secretRef, {
11846
+ documentId: workflowId,
11847
+ kind: "workflow"
11848
+ });
11423
11849
  } catch (error) {
11424
11850
  const message = error instanceof Error ? error.message : String(error);
11425
11851
  this.logger.error(`Webhook secret unavailable for ${workflowId}: @error`, message);
@@ -11444,6 +11870,19 @@ var WorkflowRuntimeService = class {
11444
11870
  const descriptor = await this.pieceDescriptor(targetOf(block));
11445
11871
  return (block.kind === "trigger" ? descriptor.triggers : descriptor.actions).find((candidate) => candidate.name === block.name)?.requireReactor;
11446
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
+ }
11447
11886
  callerRunUser(ctx) {
11448
11887
  const subject = ctx ? this.host.subjectOf?.(ctx) : void 0;
11449
11888
  return subject?.address ? {
@@ -11721,7 +12160,7 @@ var WorkflowRuntimeService = class {
11721
12160
  return this.recordCheckResult(document, {
11722
12161
  ok: true,
11723
12162
  detail: outcome.declared ? null : "piece declares no auth.validate; credentials resolved",
11724
- accountLabel: outcome.accountLabel ?? accountLabel
12163
+ accountLabel: outcome.accountLabel ?? idTokenEmail(shapedAuth) ?? accountLabel
11725
12164
  });
11726
12165
  }
11727
12166
  async startOAuth(connectionId, ctx, options) {
@@ -11729,10 +12168,25 @@ var WorkflowRuntimeService = class {
11729
12168
  await this.assertCanWriteDocument(connectionId, ctx);
11730
12169
  const state = (await this.connectionDocument(connectionId)).state.global;
11731
12170
  if (state.authType !== "OAUTH2") throw new OAuthError("This connection does not sign in with OAuth2");
11732
- const { [OAUTH_CLIENT_ID]: clientId, ...props } = state.config ?? {};
11733
- if (typeof clientId !== "string" || clientId === "") throw new OAuthError("Set the client ID before connecting");
11734
- const clientSecretRef = state.secretRefs.find((entry) => entry.name === OAUTH_CLIENT_SECRET)?.ref;
11735
- if (!clientSecretRef) throw new OAuthError("Save the client secret before connecting");
12171
+ const { [OAUTH_CLIENT_ID]: ownClientId, [OAUTH_APP]: appId, [OAUTH_SCOPES]: _scopes, [OAUTH_GRANTED_SCOPES]: _granted, ...props } = state.config ?? {};
12172
+ const scopes = scopesOverride(state.config ?? {});
12173
+ let clientId;
12174
+ let clientSecretRef;
12175
+ let pkce = false;
12176
+ if (typeof appId === "string" && appId !== "") {
12177
+ const kind = clientKindFor(options.redirectUri);
12178
+ const client = (await this.oauthApps()).client(appId, kind);
12179
+ if (!client) throw new OAuthError(`The app "${appId}" has no ${kind} client on this host; configure it in workflows.oauthApps`);
12180
+ clientId = client.clientId;
12181
+ clientSecretRef = client.clientSecretRef;
12182
+ pkce = kind === "desktop";
12183
+ } else {
12184
+ if (typeof ownClientId !== "string" || ownClientId === "") throw new OAuthError("Set the client ID before connecting");
12185
+ const ref = state.secretRefs.find((entry) => entry.name === OAUTH_CLIENT_SECRET)?.ref;
12186
+ if (!ref) throw new OAuthError("Save the client secret before connecting");
12187
+ clientId = ownClientId;
12188
+ clientSecretRef = ref;
12189
+ }
11736
12190
  const packageName = packageFromConnectorId(state.connectorId);
11737
12191
  const found = await this.blockResolver.latest(packageName);
11738
12192
  if (found.version === void 0) throw new OAuthError(`Could not resolve a version for piece "${packageName}"` + (found.unreachable ? `: ${found.unreachable}` : ""));
@@ -11751,7 +12205,10 @@ var WorkflowRuntimeService = class {
11751
12205
  clientId,
11752
12206
  clientSecretRef,
11753
12207
  redirectUri: options.redirectUri,
11754
- ...options.returnUrl ? { returnUrl: options.returnUrl } : {}
12208
+ ...options.returnUrl ? { returnUrl: options.returnUrl } : {},
12209
+ ...scopes ? { scopes } : {},
12210
+ ...pkce ? { pkce } : {},
12211
+ createdBy: this.callerAddress(ctx)
11755
12212
  });
11756
12213
  }
11757
12214
  async oauthAttempt(state, ctx) {
@@ -11781,21 +12238,31 @@ var WorkflowRuntimeService = class {
11781
12238
  const document = await this.connectionDocument(attempt.connection_id);
11782
12239
  const state = document.state.global;
11783
12240
  const config = state.config ?? {};
11784
- if (state.authType !== "OAUTH2" || config["client_id"] !== attempt.client_id) return await fail("The connection changed during sign-in; try again");
12241
+ const appId = config[OAUTH_APP];
12242
+ const fromRegistry = typeof appId === "string" && appId !== "" && (await this.oauthApps()).clients(appId).some((c) => c.clientId === attempt.client_id);
12243
+ if (state.authType !== "OAUTH2" || !fromRegistry && config["client_id"] !== attempt.client_id) return await fail("The connection changed during sign-in; try again");
11785
12244
  const secrets = await this.secrets();
11786
- const tokens = await exchangeCode(attempt, callback.code, await secrets.get(attempt.client_secret_ref), this.designEgress);
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);
11787
12250
  const value = JSON.stringify(tokens);
11788
12251
  const existing = state.secretRefs.find((entry) => entry.name === OAUTH_TOKEN);
11789
12252
  let ref = existing?.ref;
11790
12253
  try {
11791
- 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;
11792
12257
  } catch {
11793
12258
  ref = void 0;
11794
12259
  }
11795
12260
  if (!ref) {
11796
12261
  ref = (await secrets.create({
11797
12262
  value,
11798
- label: `${state.name || "connection"} · OAuth2 token`
12263
+ label: `${state.name || "connection"} · OAuth2 token`,
12264
+ ownerId: connectionId,
12265
+ createdBy: attempt.created_by
11799
12266
  })).ref;
11800
12267
  await this.host.reactorClient.execute(document.header.id, "main", [actions.setSecretRef({
11801
12268
  id: existing?.id ?? generateId(),
@@ -11803,6 +12270,11 @@ var WorkflowRuntimeService = class {
11803
12270
  ref
11804
12271
  })]);
11805
12272
  }
12273
+ const granted = grantedScopes(tokens.scope, config);
12274
+ if (granted) await this.host.reactorClient.execute(document.header.id, "main", [actions.setConfigValue({
12275
+ name: OAUTH_GRANTED_SCOPES,
12276
+ value: granted
12277
+ })]);
11806
12278
  const result = await this.checkConnectionDocument(await this.connectionDocument(attempt.connection_id));
11807
12279
  if (!result.ok) return await fail(result.detail ?? "Connection check failed");
11808
12280
  await attempts.finish(attempt.state, null);
@@ -12333,7 +12805,8 @@ var WorkflowRuntimeService = class {
12333
12805
  "trigger.kind": triggerKind,
12334
12806
  ...resume ? { "run.rerun_of": resume.rerunOf } : {}
12335
12807
  };
12336
- return this.telemetry.span("workflow.run", attributes, async (span) => {
12808
+ this.unsettledFirings.set(workflowId, (this.unsettledFirings.get(workflowId) ?? 0) + 1);
12809
+ const traced = this.telemetry.span("workflow.run", attributes, async (span) => {
12337
12810
  const run = {
12338
12811
  span,
12339
12812
  refused: false
@@ -12353,6 +12826,11 @@ var WorkflowRuntimeService = class {
12353
12826
  this.telemetry.recordRun(performance.now() - started, status, triggerKind);
12354
12827
  }
12355
12828
  });
12829
+ try {
12830
+ return await traced;
12831
+ } finally {
12832
+ this.settleFiring(workflowId);
12833
+ }
12356
12834
  }
12357
12835
  async runFired(run, workflowId, triggerPayload, triggerKind, resume, ctx, enqueuedRunId) {
12358
12836
  const firedAt = Date.now();
@@ -12415,6 +12893,7 @@ var WorkflowRuntimeService = class {
12415
12893
  const executor = this.blockExecutor(store);
12416
12894
  const runnable = runnableDefinition(state);
12417
12895
  const policy = effectiveRunPolicy(runnable);
12896
+ this.runLimits.set(workflowId, concurrencyLimit(policy));
12418
12897
  const deadline = policy.runTimeoutSeconds ? firedAt + policy.runTimeoutSeconds * 1e3 : void 0;
12419
12898
  const skipped = (reason, refusal) => this.skipFiring(store, workflowId, enqueuedRunId, {
12420
12899
  reason,
@@ -12523,7 +13002,7 @@ var WorkflowRuntimeService = class {
12523
13002
  if (runId) run.span.setAttribute("run.id", runId);
12524
13003
  if (this.closed) throw new WorkflowRuntimeClosedError(workflowId);
12525
13004
  session = this.workers().session();
12526
- const { variables, secretValues } = await resolveVariables(runnable.variables, this.secretProvider());
13005
+ const { variables, secretValues } = await resolveVariables(runnable.variables, this.secretProvider(), workflowId);
12527
13006
  const journal = store;
12528
13007
  const journaledRunId = runId;
12529
13008
  const handed = /* @__PURE__ */ new Set();
@@ -12545,6 +13024,7 @@ var WorkflowRuntimeService = class {
12545
13024
  },
12546
13025
  executor,
12547
13026
  declaredPorts: blockPorts,
13027
+ blockDefaults: (block) => this.blockDefaults(block),
12548
13028
  ...secretValues.length > 0 ? { redactValues: secretValues } : {},
12549
13029
  triggerPayload,
12550
13030
  completedSteps: resume?.completedSteps,
@@ -12588,6 +13068,28 @@ var WorkflowRuntimeService = class {
12588
13068
  }
12589
13069
  }
12590
13070
  runGate = new WorkflowRunGate();
13071
+ unsettledFirings = /* @__PURE__ */ new Map();
13072
+ runLimits = /* @__PURE__ */ new Map();
13073
+ /**
13074
+ * Whether a due poll of this workflow's trigger should wait: its runs are
13075
+ * serialised (QUEUE, SINGLETON, a bounded PARALLEL) and every slot is taken
13076
+ * by a firing that has not settled. A polling trigger that hands out work
13077
+ * advances its own position when polled (the backfill's for-every moves its
13078
+ * cursor), so a poll now would only add a firing that waits in the queue,
13079
+ * where it can expire past `runTimeoutSeconds` or be skipped by a park,
13080
+ * with the position already gone. The supervisor leaves the poll due and
13081
+ * looks again on its next tick.
13082
+ */
13083
+ pollHeld(workflowId) {
13084
+ const limit = this.runLimits.get(workflowId);
13085
+ if (limit === void 0 || limit === null) return false;
13086
+ return (this.unsettledFirings.get(workflowId) ?? 0) >= limit;
13087
+ }
13088
+ settleFiring(workflowId) {
13089
+ const left = (this.unsettledFirings.get(workflowId) ?? 1) - 1;
13090
+ if (left > 0) this.unsettledFirings.set(workflowId, left);
13091
+ else this.unsettledFirings.delete(workflowId);
13092
+ }
12591
13093
  heldReplies = /* @__PURE__ */ new Map();
12592
13094
  /** Why a trigger's firing of a PARKED workflow is refused; undefined when
12593
13095
  * it is not parked, or when an operator started the run. */
@@ -12775,7 +13277,7 @@ var WorkflowRuntimeService = class {
12775
13277
  if ("error" in upstream) return refused(upstream.error);
12776
13278
  let resolved;
12777
13279
  try {
12778
- resolved = await resolveVariables(state.variables, this.secretProvider());
13280
+ resolved = await resolveVariables(state.variables, this.secretProvider(), workflowId);
12779
13281
  } catch (error) {
12780
13282
  return refused(error instanceof Error ? error.message : String(error));
12781
13283
  }
@@ -12836,6 +13338,10 @@ var WorkflowRuntimeService = class {
12836
13338
  }, () => runWorkflow({
12837
13339
  definition,
12838
13340
  executor,
13341
+ blockDefaults: async (block) => {
13342
+ const timeoutSeconds = (await this.blockDefaults(block))?.timeoutSeconds;
13343
+ return timeoutSeconds ? { timeoutSeconds } : void 0;
13344
+ },
12839
13345
  ...secretValues.length > 0 ? { redactValues: secretValues } : {},
12840
13346
  triggerPayload: upstream.triggerPayload,
12841
13347
  priorSteps: upstream.priorSteps,
@@ -13339,6 +13845,9 @@ var WorkflowTriggersReadModel = class extends BaseReadModel {
13339
13845
  });
13340
13846
  this.runtime = runtime;
13341
13847
  }
13848
+ health(workflowId) {
13849
+ return this.runtime.workflowHealth(workflowId);
13850
+ }
13342
13851
  async commitOperations(items) {
13343
13852
  const markers = items.filter((item) => isPurgeMarker(item.operation));
13344
13853
  if (markers.length === 0) {
@@ -13350,6 +13859,6 @@ var WorkflowTriggersReadModel = class extends BaseReadModel {
13350
13859
  }
13351
13860
  };
13352
13861
  //#endregion
13353
- export { CORE_PIECE_NAME, CORE_PIECE_VERSION, InvalidRunCursorError, PieceRegistry, SINGLETON_HEARTBEAT_MS, SINGLETON_LEASE_TTL_MS, SINGLETON_STALE_HEARTBEATS, TELEMETRY_SCOPE, WORKFLOW_PACKAGE_NAME, WORKFLOW_SINGLETON_OWNER_ENV, WORKFLOW_SYNCING_MESSAGE, WORKFLOW_TRIGGERS_READ_MODEL, WORKFLOW_TRIGGERS_READ_MODEL_STAGE, WorkflowRuntimeService, WorkflowSingletonConflictError, WorkflowSyncingError, WorkflowTriggersReadModel, acquireWorkflowSingletonLease, createAttachmentPort, createWorkflowRuntime, packagePieces, publishRunUser, setPieceRegistryUrl, singletonOwnerName };
13862
+ export { CORE_PIECE_NAME, CORE_PIECE_VERSION, InvalidRunCursorError, OAUTH_APP, OAUTH_GRANTED_SCOPES, OAUTH_SCOPES, PieceRegistry, SINGLETON_HEARTBEAT_MS, SINGLETON_LEASE_TTL_MS, SINGLETON_STALE_HEARTBEATS, StaticOAuthAppRegistry, TELEMETRY_SCOPE, WORKFLOW_PACKAGE_NAME, WORKFLOW_SINGLETON_OWNER_ENV, WORKFLOW_SYNCING_MESSAGE, WORKFLOW_TRIGGERS_READ_MODEL, WORKFLOW_TRIGGERS_READ_MODEL_STAGE, WorkflowRuntimeService, WorkflowSingletonConflictError, WorkflowSyncingError, WorkflowTriggersReadModel, acquireWorkflowSingletonLease, clientKindFor, createAttachmentPort, createWorkflowRuntime, grantedScopes, loadOAuthApps, packagePieces, parseClientFile, publishRunUser, scopesOverride, setPieceRegistryUrl, singletonOwnerName };
13354
13863
 
13355
13864
  //# sourceMappingURL=index.js.map