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

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-CiXdMoVa.js";
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
2
  import { ReactorAccessDeniedError, ReactorAccessDeniedError as ReactorAccessDeniedError$1, ReactorActionsFailedError, ReactorJobFailedError, ReactorJobPendingError as ReactorJobPendingError$1, WebhookHandshakeStrategy } 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,7 @@ 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, defaultReadModelIndexingConfig, deleteDocumentAction, isPurgeMarker } from "@powerhousedao/reactor";
22
+ import { BaseReadModel, JobStatus, PropagationMode, createEmptyDocument, defaultReadModelIndexingConfig, deleteDocumentAction, isPurgeMarker } from "@powerhousedao/reactor";
23
23
  import { DOCUMENT_SCOPE_ACTION_TYPES, initializeAuth, normalizeDocumentModelVersion, withSignaturePolicy } from "@powerhousedao/shared/document-model";
24
24
  import { performance as performance$1 } from "node:perf_hooks";
25
25
  //#region src/pieces/activepieces/piece-versions.ts
@@ -170,6 +170,23 @@ function createAttachmentPort(client, access) {
170
170
  //#region src/reactor/package-name.ts
171
171
  const WORKFLOW_PACKAGE_NAME = "@powerhousedao/workflow";
172
172
  //#endregion
173
+ //#region src/reactor/id-token.ts
174
+ /** The verified email in a connection auth's `data.id_token`, if there is one. */
175
+ function idTokenEmail(auth) {
176
+ const token = (auth?.data)?.id_token;
177
+ if (typeof token !== "string") return void 0;
178
+ const payload = token.split(".")[1];
179
+ if (!payload) return void 0;
180
+ let claims;
181
+ try {
182
+ claims = JSON.parse(Buffer.from(payload, "base64url").toString("utf8"));
183
+ } catch {
184
+ return;
185
+ }
186
+ if (claims.email_verified === false || claims.email_verified === "false") return;
187
+ return typeof claims.email === "string" && claims.email !== "" ? claims.email : void 0;
188
+ }
189
+ //#endregion
173
190
  //#region src/reactor/oauth.ts
174
191
  const OAUTH_CLIENT_ID = "client_id";
175
192
  const OAUTH_CLIENT_SECRET = "client_secret";
@@ -221,14 +238,15 @@ var OAuthAttemptStore = class OAuthAttemptStore {
221
238
  await this.db.deleteFrom("oauth_attempt").where("expires_at", "<", (/* @__PURE__ */ new Date(now.getTime() - 864e5)).toISOString()).execute();
222
239
  const { method } = input;
223
240
  const state = base64url(randomBytes(32));
224
- const verifier = method.pkce === true ? base64url(randomBytes(48)) : null;
241
+ const verifier = method.pkce === true || input.pkce === true ? base64url(randomBytes(48)) : null;
225
242
  const authUrl = new URL(fillOAuthUrl(method.authUrl, input.props));
226
243
  const params = authUrl.searchParams;
227
244
  params.set("response_type", "code");
228
245
  params.set("client_id", input.clientId);
229
246
  params.set("redirect_uri", input.redirectUri);
230
247
  params.set("state", state);
231
- if (method.scope.length > 0) params.set("scope", method.scope.join(" "));
248
+ const scope = input.scopes ?? method.scope;
249
+ if (scope.length > 0) params.set("scope", scope.join(" "));
232
250
  params.set("access_type", "offline");
233
251
  const prompt = method.prompt ?? "consent";
234
252
  if (prompt !== "omit") params.set("prompt", prompt);
@@ -401,13 +419,15 @@ function tokenSetFrom(response, context, previous) {
401
419
  token_url: context.token_url,
402
420
  ...context.authorization_method ? { authorization_method: context.authorization_method } : {},
403
421
  redirect_url: context.redirect_url,
422
+ ...context.client_id ?? previous?.client_id ? { client_id: context.client_id ?? previous?.client_id } : {},
423
+ ...context.client_secret_ref ?? previous?.client_secret_ref ? { client_secret_ref: context.client_secret_ref ?? previous?.client_secret_ref } : {},
404
424
  data: {
405
425
  ...previous?.data,
406
426
  ...response
407
427
  }
408
428
  };
409
429
  }
410
- async function exchangeCode(attempt, code, clientSecret, egress) {
430
+ async function exchangeCode(attempt, code, clientSecret, egress, fromRegistry = false) {
411
431
  const method = authorizationMethod(attempt.authorization_method);
412
432
  const auth = clientAuth$1(method, attempt.client_id, clientSecret);
413
433
  return tokenSetFrom(await postTokenForm(attempt.token_url, {
@@ -419,7 +439,11 @@ async function exchangeCode(attempt, code, clientSecret, egress) {
419
439
  }, auth.headers, egress), {
420
440
  token_url: attempt.token_url,
421
441
  authorization_method: method,
422
- redirect_url: attempt.redirect_uri
442
+ redirect_url: attempt.redirect_uri,
443
+ ...fromRegistry ? {
444
+ client_id: attempt.client_id,
445
+ client_secret_ref: attempt.client_secret_ref
446
+ } : {}
423
447
  });
424
448
  }
425
449
  function parseTokenSet(value) {
@@ -447,20 +471,27 @@ var StoreTokenRefresher = class {
447
471
  refreshIfDue(source) {
448
472
  const refOf = (name) => source.secretRefs?.find((entry) => entry.name === name)?.ref;
449
473
  const tokenRef = refOf(OAUTH_TOKEN);
474
+ if (!tokenRef) return Promise.resolve();
450
475
  const secretRef = refOf(OAUTH_CLIENT_SECRET);
451
- const clientId = source.config?.[OAUTH_CLIENT_ID];
452
- if (!tokenRef || !secretRef || typeof clientId !== "string") return Promise.resolve();
476
+ const configId = source.config?.[OAUTH_CLIENT_ID];
477
+ const own = secretRef && typeof configId === "string" ? {
478
+ secretRef,
479
+ clientId: configId
480
+ } : void 0;
453
481
  let pending = this.inFlight.get(tokenRef);
454
482
  if (!pending) {
455
- pending = this.refresh(tokenRef, secretRef, clientId).finally(() => this.inFlight.delete(tokenRef));
483
+ pending = this.refresh(tokenRef, own).finally(() => this.inFlight.delete(tokenRef));
456
484
  this.inFlight.set(tokenRef, pending);
457
485
  }
458
486
  return pending;
459
487
  }
460
- async refresh(tokenRef, secretRef, clientId) {
488
+ async refresh(tokenRef, own) {
461
489
  const store = await this.secrets();
462
490
  const tokens = parseTokenSet(await store.get(tokenRef));
463
491
  if (!tokenDue(tokens, Math.round(Date.now() / 1e3))) return;
492
+ const clientId = own?.clientId ?? tokens.client_id;
493
+ const secretRef = own?.secretRef ?? tokens.client_secret_ref;
494
+ if (!clientId || !secretRef) return;
464
495
  const auth = clientAuth$1(tokens.authorization_method, clientId, await store.get(secretRef));
465
496
  let response;
466
497
  try {
@@ -2094,23 +2125,34 @@ var RunScopedReactorClient = class {
2094
2125
  async find(search, view, paging, signal) {
2095
2126
  return this.documents(await this.inner.find(search, this.readView(view), this.paging(paging), this.signal(signal)));
2096
2127
  }
2128
+ async query(request, signal) {
2129
+ const queryIndex = this.host.queryIndex;
2130
+ if (!queryIndex) throw accessDenied("This host serves no read-model indexes to pieces (reactor.query)");
2131
+ this.assertAccess();
2132
+ return queryIndex(request, this.scope.runUser, this.signal(signal));
2133
+ }
2097
2134
  async create(document, parentIdentifier, signal) {
2098
2135
  this.assertWritable("create");
2099
2136
  const abort = this.signal(signal);
2100
2137
  const parentId = parentIdentifier === void 0 ? void 0 : await this.resolveReference(parentIdentifier, "main", abort);
2101
2138
  return this.createDocument(document, parentId, abort);
2102
2139
  }
2140
+ async newDocument(documentModelType, options, signal) {
2141
+ this.assertWritable("newDocument");
2142
+ const abort = this.signal(signal);
2143
+ 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);
2145
+ const base = await this.inner.getCreateProtocolVersions({
2146
+ documentType: documentModelType,
2147
+ parentIdentifier: parentId
2148
+ }, abort);
2149
+ return createEmptyDocument(module, options, await this.inner.getCreateSignaturePolicy(), base);
2150
+ }
2103
2151
  async createEmpty(documentModelType, options, signal) {
2104
2152
  this.assertWritable("createEmpty");
2105
2153
  const abort = this.signal(signal);
2106
- const module = await this.modelModule(documentModelType, options?.documentModelVersion, abort);
2154
+ const document = await this.newDocument(documentModelType, options, abort);
2107
2155
  const parentId = options?.parentIdentifier === void 0 ? void 0 : await this.resolveReference(options.parentIdentifier, "main", abort);
2108
- const base = await this.inner.getCreateProtocolVersions(parentId, abort);
2109
- const document = withSignaturePolicy(module.utils.createDocument(), await this.inner.getCreateSignaturePolicy(), { protocolVersions: {
2110
- ...base,
2111
- ...options?.protocolVersions
2112
- } });
2113
- document.state.document.version = normalizeDocumentModelVersion(module.version);
2114
2156
  return this.createDocument(document, parentId, abort);
2115
2157
  }
2116
2158
  async execute(documentIdentifier, branch, actions, signal, subject) {
@@ -2378,11 +2420,13 @@ const SERVED = {
2378
2420
  get: true,
2379
2421
  resolveIdOrSlug: true,
2380
2422
  find: true,
2423
+ query: true,
2381
2424
  getOperations: true,
2382
2425
  getOutgoingRelationships: true,
2383
2426
  getIncomingRelationships: true,
2384
2427
  getOutgoingRelationshipEdges: true,
2385
2428
  getIncomingRelationshipEdges: true,
2429
+ newDocument: true,
2386
2430
  create: true,
2387
2431
  createEmpty: true,
2388
2432
  execute: true,
@@ -9193,6 +9237,28 @@ var TriggerSupervisor = class {
9193
9237
  else await store.clearWorkflowPark(workflowId);
9194
9238
  });
9195
9239
  }
9240
+ /**
9241
+ * Makes an armed trigger due now and runs the due check at once, so a
9242
+ * schedule fires and a poll polls without waiting for its interval ("sync
9243
+ * now"). The next one follows the interval from there. False when the
9244
+ * workflow has no armed trigger (disabled, parked, erroring, unknown).
9245
+ */
9246
+ async pollNow(workflowId) {
9247
+ if (this.stopped) return false;
9248
+ const store = await this.store();
9249
+ if (!store) return false;
9250
+ const row = await store.getTriggerState(workflowId);
9251
+ if (!row || row.status !== "ENABLED") return false;
9252
+ await store.upsertTriggerState({
9253
+ ...row,
9254
+ next_poll_at: this.now().toISOString()
9255
+ });
9256
+ this.tick().catch((error) => {
9257
+ if (isShutdownRefusal(error)) return;
9258
+ logger$2.error("Trigger tick after poll-now failed: @error", error);
9259
+ });
9260
+ return true;
9261
+ }
9196
9262
  /** A re-publish past the parked version: on this lane, a park older than
9197
9263
  * `publishedVersion` goes and a row it parked is ENABLED again, as it was
9198
9264
  * before (only an ENABLED row is ever parked); a park as new stands. The
@@ -9710,6 +9776,7 @@ var TriggerSupervisor = class {
9710
9776
  started.push(this.startOnLane(row.workflow_id, () => store.setTriggerStatus(row.workflow_id, "DISABLED"), false));
9711
9777
  continue;
9712
9778
  }
9779
+ if (!isSchedule(binding) && this.options.pollHeld?.(row.workflow_id)) continue;
9713
9780
  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));
9714
9781
  }
9715
9782
  this.throwIfStopped();
@@ -10065,6 +10132,125 @@ function matchesLifecycleFilter(filter, documentType, driveId) {
10065
10132
  return ok(filter.documentType, documentType ?? void 0) && ok(filter.driveId, driveId ?? void 0);
10066
10133
  }
10067
10134
  //#endregion
10135
+ //#region src/reactor/oauth-apps.ts
10136
+ /** The connection config key naming an app of the registry. */
10137
+ const OAUTH_APP = "oauth_app";
10138
+ /**
10139
+ * The connection config key for the scopes to ask for instead of the piece's
10140
+ * own: a list, or one space-separated string. A read-only integration asks
10141
+ * for `calendar.readonly` where the piece would ask to edit too.
10142
+ */
10143
+ const OAUTH_SCOPES = "oauth_scopes";
10144
+ var StaticOAuthAppRegistry = class {
10145
+ entries;
10146
+ constructor(entries) {
10147
+ this.entries = entries;
10148
+ }
10149
+ client(appId, kind) {
10150
+ return this.entries.find((e) => e.appId === appId && e.kind === kind);
10151
+ }
10152
+ clients(appId) {
10153
+ return this.entries.filter((e) => e.appId === appId);
10154
+ }
10155
+ };
10156
+ const LOOPBACK = /* @__PURE__ */ new Set([
10157
+ "localhost",
10158
+ "127.0.0.1",
10159
+ "[::1]",
10160
+ "::1"
10161
+ ]);
10162
+ /** Desktop for a callback to this machine, web for one the provider reaches over the internet. */
10163
+ function clientKindFor(redirectUri) {
10164
+ let host;
10165
+ try {
10166
+ host = new URL(redirectUri).hostname;
10167
+ } catch {
10168
+ return "web";
10169
+ }
10170
+ return LOOPBACK.has(host) ? "desktop" : "web";
10171
+ }
10172
+ /**
10173
+ * Config key the runtime writes after a sign-in: the scopes the provider
10174
+ * granted (its token response's `scope`, else the ones asked for). A
10175
+ * connection whose `oauth_scopes` reach beyond them needs a new sign-in.
10176
+ */
10177
+ const OAUTH_GRANTED_SCOPES = "oauth_granted_scopes";
10178
+ /** What a sign-in was granted: the token's own `scope`, else what the connection asked for. */
10179
+ function grantedScopes(tokenScope, config) {
10180
+ const granted = (tokenScope ?? "").split(/[\s,]+/).filter(Boolean);
10181
+ return granted.length > 0 ? granted : scopesOverride(config);
10182
+ }
10183
+ /** The scopes a connection asks for instead of the piece's, or undefined to keep the piece's. */
10184
+ function scopesOverride(config) {
10185
+ const raw = config[OAUTH_SCOPES];
10186
+ const scopes = (Array.isArray(raw) ? raw.filter((s) => typeof s === "string") : typeof raw === "string" ? raw.split(/\s+/) : []).map((s) => s.trim()).filter(Boolean);
10187
+ return scopes.length > 0 ? scopes : void 0;
10188
+ }
10189
+ /**
10190
+ * The client id and secret in a provider's client file: Google's
10191
+ * `{ installed | web: { client_id, client_secret } }`, or the two keys at the
10192
+ * top level.
10193
+ */
10194
+ function parseClientFile(text) {
10195
+ let parsed;
10196
+ try {
10197
+ parsed = JSON.parse(text);
10198
+ } catch {
10199
+ throw new Error("not JSON");
10200
+ }
10201
+ const root = parsed ?? {};
10202
+ const inner = root.installed ?? root.web ?? root;
10203
+ const clientId = inner.client_id;
10204
+ const clientSecret = inner.client_secret;
10205
+ if (typeof clientId !== "string" || !clientId) throw new Error("no client_id");
10206
+ if (typeof clientSecret !== "string" || !clientSecret) throw new Error("no client_secret");
10207
+ return {
10208
+ clientId,
10209
+ clientSecret
10210
+ };
10211
+ }
10212
+ /** The label a client secret carries in the store, so a restart finds it again. */
10213
+ function clientSecretLabel(appId, kind) {
10214
+ return `oauth-app:${appId}:${kind}`;
10215
+ }
10216
+ /**
10217
+ * Reads each configured client file and keeps its secret in the store under a
10218
+ * fixed label: created the first time, rotated when the file changed, left
10219
+ * alone otherwise. A client whose file cannot be read is skipped with the
10220
+ * reason, so one bad file does not take the others down.
10221
+ */
10222
+ async function loadOAuthApps(definitions, store, options = {}) {
10223
+ 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]));
10225
+ const entries = [];
10226
+ for (const app of definitions) for (const kind of ["desktop", "web"]) {
10227
+ const path = app.clients[kind];
10228
+ if (!path) continue;
10229
+ let client;
10230
+ try {
10231
+ client = parseClientFile(await read(path));
10232
+ } catch (error) {
10233
+ options.onSkip?.(app.id, kind, `${path}: ${error instanceof Error ? error.message : String(error)}`);
10234
+ continue;
10235
+ }
10236
+ const label = clientSecretLabel(app.id, kind);
10237
+ let ref = existing.get(label);
10238
+ if (ref) {
10239
+ if (await store.get(ref) !== client.clientSecret) await store.rotate(ref, client.clientSecret);
10240
+ } else ref = (await store.create({
10241
+ value: client.clientSecret,
10242
+ label
10243
+ })).ref;
10244
+ entries.push({
10245
+ appId: app.id,
10246
+ kind,
10247
+ clientId: client.clientId,
10248
+ clientSecretRef: ref
10249
+ });
10250
+ }
10251
+ return new StaticOAuthAppRegistry(entries);
10252
+ }
10253
+ //#endregion
10068
10254
  //#region src/reactor/service.ts
10069
10255
  function targetOf(block) {
10070
10256
  return {
@@ -10287,6 +10473,7 @@ var WorkflowRuntimeService = class {
10287
10473
  secretsPromise;
10288
10474
  oauthAttemptsPromise;
10289
10475
  tokenRefresher;
10476
+ oauthAppsPromise;
10290
10477
  registry = /* @__PURE__ */ new Map();
10291
10478
  seedPromise;
10292
10479
  seedError;
@@ -10382,6 +10569,11 @@ var WorkflowRuntimeService = class {
10382
10569
  this.oauthAttemptsPromise ??= OAuthAttemptStore.create(this.host.relationalDb);
10383
10570
  return this.oauthAttemptsPromise;
10384
10571
  }
10572
+ /** The host's OAuth2 apps, their client secrets loaded into the store once. */
10573
+ oauthApps() {
10574
+ 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}`) }));
10575
+ return this.oauthAppsPromise;
10576
+ }
10385
10577
  oauthRefresher() {
10386
10578
  return this.tokenRefresher ??= new StoreTokenRefresher(() => this.secrets(), this.designEgress);
10387
10579
  }
@@ -11123,6 +11315,7 @@ var WorkflowRuntimeService = class {
11123
11315
  fire: (workflowId, payload, kind, runId) => {
11124
11316
  this.startFiring(workflowId, payload, kind, runId);
11125
11317
  },
11318
+ pollHeld: (workflowId) => this.pollHeld(workflowId),
11126
11319
  webhookUrlFor: async (workflowId) => (await this.mintWebhookEndpoint(workflowId))?.url,
11127
11320
  reactorAccess: (binding, requireReactor, runUser) => this.triggerReactorAccess(binding, requireReactor, runUser),
11128
11321
  models: this.models,
@@ -11193,6 +11386,44 @@ var WorkflowRuntimeService = class {
11193
11386
  this.unobservePool = void 0;
11194
11387
  this.designWorker = void 0;
11195
11388
  }
11389
+ /**
11390
+ * Runs a workflow's trigger now, as its schedule or poll would (a sync
11391
+ * now): a schedule fires the workflow, a polling trigger polls its source.
11392
+ * Never a backfill's pace. False when it has no armed trigger.
11393
+ */
11394
+ async pollNow(workflowId, ctx) {
11395
+ if (this.closed) return false;
11396
+ await this.assertCanReadWorkflow(workflowId, ctx, void 0);
11397
+ return this.supervisor().pollNow(workflowId);
11398
+ }
11399
+ /**
11400
+ * How one workflow's trigger stands, for host-side code that keeps
11401
+ * workflows running (a processor reconciling the workflows it built): the
11402
+ * trigger row, and the park that holds it unarmed. Not caller-filtered:
11403
+ * GraphQL callers go through triggerStates.
11404
+ */
11405
+ async workflowHealth(workflowId) {
11406
+ const store = await this.store();
11407
+ if (!store) return {
11408
+ trigger: null,
11409
+ park: null
11410
+ };
11411
+ const [row, park] = await Promise.all([store.getTriggerState(workflowId), store.getWorkflowPark(workflowId)]);
11412
+ return {
11413
+ trigger: row ? {
11414
+ status: row.status,
11415
+ lastError: row.last_error,
11416
+ lastPollAt: row.last_poll_at,
11417
+ nextPollAt: row.next_poll_at,
11418
+ consecutiveFailures: row.consecutive_failures
11419
+ } : null,
11420
+ park: park ? {
11421
+ reason: park.reason,
11422
+ parkedAt: park.parked_at,
11423
+ publishedVersion: park.published_version
11424
+ } : null
11425
+ };
11426
+ }
11196
11427
  async triggerStates(ctx) {
11197
11428
  const store = await this.store();
11198
11429
  if (!store) return [];
@@ -11715,7 +11946,7 @@ var WorkflowRuntimeService = class {
11715
11946
  return this.recordCheckResult(document, {
11716
11947
  ok: true,
11717
11948
  detail: outcome.declared ? null : "piece declares no auth.validate; credentials resolved",
11718
- accountLabel: outcome.accountLabel ?? accountLabel
11949
+ accountLabel: outcome.accountLabel ?? idTokenEmail(shapedAuth) ?? accountLabel
11719
11950
  });
11720
11951
  }
11721
11952
  async startOAuth(connectionId, ctx, options) {
@@ -11723,10 +11954,25 @@ var WorkflowRuntimeService = class {
11723
11954
  await this.assertCanWriteDocument(connectionId, ctx);
11724
11955
  const state = (await this.connectionDocument(connectionId)).state.global;
11725
11956
  if (state.authType !== "OAUTH2") throw new OAuthError("This connection does not sign in with OAuth2");
11726
- const { [OAUTH_CLIENT_ID]: clientId, ...props } = state.config ?? {};
11727
- if (typeof clientId !== "string" || clientId === "") throw new OAuthError("Set the client ID before connecting");
11728
- const clientSecretRef = state.secretRefs.find((entry) => entry.name === OAUTH_CLIENT_SECRET)?.ref;
11729
- if (!clientSecretRef) throw new OAuthError("Save the client secret before connecting");
11957
+ const { [OAUTH_CLIENT_ID]: ownClientId, [OAUTH_APP]: appId, [OAUTH_SCOPES]: _scopes, [OAUTH_GRANTED_SCOPES]: _granted, ...props } = state.config ?? {};
11958
+ const scopes = scopesOverride(state.config ?? {});
11959
+ let clientId;
11960
+ let clientSecretRef;
11961
+ let pkce = false;
11962
+ if (typeof appId === "string" && appId !== "") {
11963
+ const kind = clientKindFor(options.redirectUri);
11964
+ const client = (await this.oauthApps()).client(appId, kind);
11965
+ if (!client) throw new OAuthError(`The app "${appId}" has no ${kind} client on this host; configure it in workflows.oauthApps`);
11966
+ clientId = client.clientId;
11967
+ clientSecretRef = client.clientSecretRef;
11968
+ pkce = kind === "desktop";
11969
+ } else {
11970
+ if (typeof ownClientId !== "string" || ownClientId === "") throw new OAuthError("Set the client ID before connecting");
11971
+ const ref = state.secretRefs.find((entry) => entry.name === OAUTH_CLIENT_SECRET)?.ref;
11972
+ if (!ref) throw new OAuthError("Save the client secret before connecting");
11973
+ clientId = ownClientId;
11974
+ clientSecretRef = ref;
11975
+ }
11730
11976
  const packageName = packageFromConnectorId(state.connectorId);
11731
11977
  const found = await this.blockResolver.latest(packageName);
11732
11978
  if (found.version === void 0) throw new OAuthError(`Could not resolve a version for piece "${packageName}"` + (found.unreachable ? `: ${found.unreachable}` : ""));
@@ -11745,7 +11991,9 @@ var WorkflowRuntimeService = class {
11745
11991
  clientId,
11746
11992
  clientSecretRef,
11747
11993
  redirectUri: options.redirectUri,
11748
- ...options.returnUrl ? { returnUrl: options.returnUrl } : {}
11994
+ ...options.returnUrl ? { returnUrl: options.returnUrl } : {},
11995
+ ...scopes ? { scopes } : {},
11996
+ ...pkce ? { pkce } : {}
11749
11997
  });
11750
11998
  }
11751
11999
  async oauthAttempt(state, ctx) {
@@ -11775,9 +12023,11 @@ var WorkflowRuntimeService = class {
11775
12023
  const document = await this.connectionDocument(attempt.connection_id);
11776
12024
  const state = document.state.global;
11777
12025
  const config = state.config ?? {};
11778
- if (state.authType !== "OAUTH2" || config["client_id"] !== attempt.client_id) return await fail("The connection changed during sign-in; try again");
12026
+ const appId = config[OAUTH_APP];
12027
+ const fromRegistry = typeof appId === "string" && appId !== "" && (await this.oauthApps()).clients(appId).some((c) => c.clientId === attempt.client_id);
12028
+ if (state.authType !== "OAUTH2" || !fromRegistry && config["client_id"] !== attempt.client_id) return await fail("The connection changed during sign-in; try again");
11779
12029
  const secrets = await this.secrets();
11780
- const tokens = await exchangeCode(attempt, callback.code, await secrets.get(attempt.client_secret_ref), this.designEgress);
12030
+ const tokens = await exchangeCode(attempt, callback.code, await secrets.get(attempt.client_secret_ref), this.designEgress, fromRegistry);
11781
12031
  const value = JSON.stringify(tokens);
11782
12032
  const existing = state.secretRefs.find((entry) => entry.name === OAUTH_TOKEN);
11783
12033
  let ref = existing?.ref;
@@ -11797,6 +12047,11 @@ var WorkflowRuntimeService = class {
11797
12047
  ref
11798
12048
  })]);
11799
12049
  }
12050
+ const granted = grantedScopes(tokens.scope, config);
12051
+ if (granted) await this.host.reactorClient.execute(document.header.id, "main", [actions.setConfigValue({
12052
+ name: OAUTH_GRANTED_SCOPES,
12053
+ value: granted
12054
+ })]);
11800
12055
  const result = await this.checkConnectionDocument(await this.connectionDocument(attempt.connection_id));
11801
12056
  if (!result.ok) return await fail(result.detail ?? "Connection check failed");
11802
12057
  await attempts.finish(attempt.state, null);
@@ -12327,7 +12582,8 @@ var WorkflowRuntimeService = class {
12327
12582
  "trigger.kind": triggerKind,
12328
12583
  ...resume ? { "run.rerun_of": resume.rerunOf } : {}
12329
12584
  };
12330
- return this.telemetry.span("workflow.run", attributes, async (span) => {
12585
+ this.unsettledFirings.set(workflowId, (this.unsettledFirings.get(workflowId) ?? 0) + 1);
12586
+ const traced = this.telemetry.span("workflow.run", attributes, async (span) => {
12331
12587
  const run = {
12332
12588
  span,
12333
12589
  refused: false
@@ -12347,6 +12603,11 @@ var WorkflowRuntimeService = class {
12347
12603
  this.telemetry.recordRun(performance.now() - started, status, triggerKind);
12348
12604
  }
12349
12605
  });
12606
+ try {
12607
+ return await traced;
12608
+ } finally {
12609
+ this.settleFiring(workflowId);
12610
+ }
12350
12611
  }
12351
12612
  async runFired(run, workflowId, triggerPayload, triggerKind, resume, ctx, enqueuedRunId) {
12352
12613
  const firedAt = Date.now();
@@ -12409,6 +12670,7 @@ var WorkflowRuntimeService = class {
12409
12670
  const executor = this.blockExecutor(store);
12410
12671
  const runnable = runnableDefinition(state);
12411
12672
  const policy = effectiveRunPolicy(runnable);
12673
+ this.runLimits.set(workflowId, concurrencyLimit(policy));
12412
12674
  const deadline = policy.runTimeoutSeconds ? firedAt + policy.runTimeoutSeconds * 1e3 : void 0;
12413
12675
  const skipped = (reason, refusal) => this.skipFiring(store, workflowId, enqueuedRunId, {
12414
12676
  reason,
@@ -12582,6 +12844,28 @@ var WorkflowRuntimeService = class {
12582
12844
  }
12583
12845
  }
12584
12846
  runGate = new WorkflowRunGate();
12847
+ unsettledFirings = /* @__PURE__ */ new Map();
12848
+ runLimits = /* @__PURE__ */ new Map();
12849
+ /**
12850
+ * Whether a due poll of this workflow's trigger should wait: its runs are
12851
+ * serialised (QUEUE, SINGLETON, a bounded PARALLEL) and every slot is taken
12852
+ * by a firing that has not settled. A polling trigger that hands out work
12853
+ * advances its own position when polled (the backfill's for-every moves its
12854
+ * cursor), so a poll now would only add a firing that waits in the queue,
12855
+ * where it can expire past `runTimeoutSeconds` or be skipped by a park,
12856
+ * with the position already gone. The supervisor leaves the poll due and
12857
+ * looks again on its next tick.
12858
+ */
12859
+ pollHeld(workflowId) {
12860
+ const limit = this.runLimits.get(workflowId);
12861
+ if (limit === void 0 || limit === null) return false;
12862
+ return (this.unsettledFirings.get(workflowId) ?? 0) >= limit;
12863
+ }
12864
+ settleFiring(workflowId) {
12865
+ const left = (this.unsettledFirings.get(workflowId) ?? 1) - 1;
12866
+ if (left > 0) this.unsettledFirings.set(workflowId, left);
12867
+ else this.unsettledFirings.delete(workflowId);
12868
+ }
12585
12869
  heldReplies = /* @__PURE__ */ new Map();
12586
12870
  /** Why a trigger's firing of a PARKED workflow is refused; undefined when
12587
12871
  * it is not parked, or when an operator started the run. */
@@ -13333,6 +13617,9 @@ var WorkflowTriggersReadModel = class extends BaseReadModel {
13333
13617
  });
13334
13618
  this.runtime = runtime;
13335
13619
  }
13620
+ health(workflowId) {
13621
+ return this.runtime.workflowHealth(workflowId);
13622
+ }
13336
13623
  async commitOperations(items) {
13337
13624
  const markers = items.filter((item) => isPurgeMarker(item.operation));
13338
13625
  if (markers.length === 0) {
@@ -13344,6 +13631,6 @@ var WorkflowTriggersReadModel = class extends BaseReadModel {
13344
13631
  }
13345
13632
  };
13346
13633
  //#endregion
13347
- 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 };
13634
+ 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 };
13348
13635
 
13349
13636
  //# sourceMappingURL=index.js.map