@powerhousedao/reactor-workflow 6.2.3-dev.79 → 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-CJd29LsN.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";
@@ -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,6 +2125,12 @@ 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);
@@ -2383,6 +2420,7 @@ const SERVED = {
2383
2420
  get: true,
2384
2421
  resolveIdOrSlug: true,
2385
2422
  find: true,
2423
+ query: true,
2386
2424
  getOperations: true,
2387
2425
  getOutgoingRelationships: true,
2388
2426
  getIncomingRelationships: true,
@@ -9199,6 +9237,28 @@ var TriggerSupervisor = class {
9199
9237
  else await store.clearWorkflowPark(workflowId);
9200
9238
  });
9201
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
+ }
9202
9262
  /** A re-publish past the parked version: on this lane, a park older than
9203
9263
  * `publishedVersion` goes and a row it parked is ENABLED again, as it was
9204
9264
  * before (only an ENABLED row is ever parked); a park as new stands. The
@@ -9716,6 +9776,7 @@ var TriggerSupervisor = class {
9716
9776
  started.push(this.startOnLane(row.workflow_id, () => store.setTriggerStatus(row.workflow_id, "DISABLED"), false));
9717
9777
  continue;
9718
9778
  }
9779
+ if (!isSchedule(binding) && this.options.pollHeld?.(row.workflow_id)) continue;
9719
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));
9720
9781
  }
9721
9782
  this.throwIfStopped();
@@ -10071,6 +10132,125 @@ function matchesLifecycleFilter(filter, documentType, driveId) {
10071
10132
  return ok(filter.documentType, documentType ?? void 0) && ok(filter.driveId, driveId ?? void 0);
10072
10133
  }
10073
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
10074
10254
  //#region src/reactor/service.ts
10075
10255
  function targetOf(block) {
10076
10256
  return {
@@ -10293,6 +10473,7 @@ var WorkflowRuntimeService = class {
10293
10473
  secretsPromise;
10294
10474
  oauthAttemptsPromise;
10295
10475
  tokenRefresher;
10476
+ oauthAppsPromise;
10296
10477
  registry = /* @__PURE__ */ new Map();
10297
10478
  seedPromise;
10298
10479
  seedError;
@@ -10388,6 +10569,11 @@ var WorkflowRuntimeService = class {
10388
10569
  this.oauthAttemptsPromise ??= OAuthAttemptStore.create(this.host.relationalDb);
10389
10570
  return this.oauthAttemptsPromise;
10390
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
+ }
10391
10577
  oauthRefresher() {
10392
10578
  return this.tokenRefresher ??= new StoreTokenRefresher(() => this.secrets(), this.designEgress);
10393
10579
  }
@@ -11129,6 +11315,7 @@ var WorkflowRuntimeService = class {
11129
11315
  fire: (workflowId, payload, kind, runId) => {
11130
11316
  this.startFiring(workflowId, payload, kind, runId);
11131
11317
  },
11318
+ pollHeld: (workflowId) => this.pollHeld(workflowId),
11132
11319
  webhookUrlFor: async (workflowId) => (await this.mintWebhookEndpoint(workflowId))?.url,
11133
11320
  reactorAccess: (binding, requireReactor, runUser) => this.triggerReactorAccess(binding, requireReactor, runUser),
11134
11321
  models: this.models,
@@ -11199,6 +11386,44 @@ var WorkflowRuntimeService = class {
11199
11386
  this.unobservePool = void 0;
11200
11387
  this.designWorker = void 0;
11201
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
+ }
11202
11427
  async triggerStates(ctx) {
11203
11428
  const store = await this.store();
11204
11429
  if (!store) return [];
@@ -11721,7 +11946,7 @@ var WorkflowRuntimeService = class {
11721
11946
  return this.recordCheckResult(document, {
11722
11947
  ok: true,
11723
11948
  detail: outcome.declared ? null : "piece declares no auth.validate; credentials resolved",
11724
- accountLabel: outcome.accountLabel ?? accountLabel
11949
+ accountLabel: outcome.accountLabel ?? idTokenEmail(shapedAuth) ?? accountLabel
11725
11950
  });
11726
11951
  }
11727
11952
  async startOAuth(connectionId, ctx, options) {
@@ -11729,10 +11954,25 @@ var WorkflowRuntimeService = class {
11729
11954
  await this.assertCanWriteDocument(connectionId, ctx);
11730
11955
  const state = (await this.connectionDocument(connectionId)).state.global;
11731
11956
  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");
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
+ }
11736
11976
  const packageName = packageFromConnectorId(state.connectorId);
11737
11977
  const found = await this.blockResolver.latest(packageName);
11738
11978
  if (found.version === void 0) throw new OAuthError(`Could not resolve a version for piece "${packageName}"` + (found.unreachable ? `: ${found.unreachable}` : ""));
@@ -11751,7 +11991,9 @@ var WorkflowRuntimeService = class {
11751
11991
  clientId,
11752
11992
  clientSecretRef,
11753
11993
  redirectUri: options.redirectUri,
11754
- ...options.returnUrl ? { returnUrl: options.returnUrl } : {}
11994
+ ...options.returnUrl ? { returnUrl: options.returnUrl } : {},
11995
+ ...scopes ? { scopes } : {},
11996
+ ...pkce ? { pkce } : {}
11755
11997
  });
11756
11998
  }
11757
11999
  async oauthAttempt(state, ctx) {
@@ -11781,9 +12023,11 @@ var WorkflowRuntimeService = class {
11781
12023
  const document = await this.connectionDocument(attempt.connection_id);
11782
12024
  const state = document.state.global;
11783
12025
  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");
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");
11785
12029
  const secrets = await this.secrets();
11786
- 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);
11787
12031
  const value = JSON.stringify(tokens);
11788
12032
  const existing = state.secretRefs.find((entry) => entry.name === OAUTH_TOKEN);
11789
12033
  let ref = existing?.ref;
@@ -11803,6 +12047,11 @@ var WorkflowRuntimeService = class {
11803
12047
  ref
11804
12048
  })]);
11805
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
+ })]);
11806
12055
  const result = await this.checkConnectionDocument(await this.connectionDocument(attempt.connection_id));
11807
12056
  if (!result.ok) return await fail(result.detail ?? "Connection check failed");
11808
12057
  await attempts.finish(attempt.state, null);
@@ -12333,7 +12582,8 @@ var WorkflowRuntimeService = class {
12333
12582
  "trigger.kind": triggerKind,
12334
12583
  ...resume ? { "run.rerun_of": resume.rerunOf } : {}
12335
12584
  };
12336
- 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) => {
12337
12587
  const run = {
12338
12588
  span,
12339
12589
  refused: false
@@ -12353,6 +12603,11 @@ var WorkflowRuntimeService = class {
12353
12603
  this.telemetry.recordRun(performance.now() - started, status, triggerKind);
12354
12604
  }
12355
12605
  });
12606
+ try {
12607
+ return await traced;
12608
+ } finally {
12609
+ this.settleFiring(workflowId);
12610
+ }
12356
12611
  }
12357
12612
  async runFired(run, workflowId, triggerPayload, triggerKind, resume, ctx, enqueuedRunId) {
12358
12613
  const firedAt = Date.now();
@@ -12415,6 +12670,7 @@ var WorkflowRuntimeService = class {
12415
12670
  const executor = this.blockExecutor(store);
12416
12671
  const runnable = runnableDefinition(state);
12417
12672
  const policy = effectiveRunPolicy(runnable);
12673
+ this.runLimits.set(workflowId, concurrencyLimit(policy));
12418
12674
  const deadline = policy.runTimeoutSeconds ? firedAt + policy.runTimeoutSeconds * 1e3 : void 0;
12419
12675
  const skipped = (reason, refusal) => this.skipFiring(store, workflowId, enqueuedRunId, {
12420
12676
  reason,
@@ -12588,6 +12844,28 @@ var WorkflowRuntimeService = class {
12588
12844
  }
12589
12845
  }
12590
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
+ }
12591
12869
  heldReplies = /* @__PURE__ */ new Map();
12592
12870
  /** Why a trigger's firing of a PARKED workflow is refused; undefined when
12593
12871
  * it is not parked, or when an operator started the run. */
@@ -13339,6 +13617,9 @@ var WorkflowTriggersReadModel = class extends BaseReadModel {
13339
13617
  });
13340
13618
  this.runtime = runtime;
13341
13619
  }
13620
+ health(workflowId) {
13621
+ return this.runtime.workflowHealth(workflowId);
13622
+ }
13342
13623
  async commitOperations(items) {
13343
13624
  const markers = items.filter((item) => isPurgeMarker(item.operation));
13344
13625
  if (markers.length === 0) {
@@ -13350,6 +13631,6 @@ var WorkflowTriggersReadModel = class extends BaseReadModel {
13350
13631
  }
13351
13632
  };
13352
13633
  //#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 };
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 };
13354
13635
 
13355
13636
  //# sourceMappingURL=index.js.map