7336 lines · 324.3 KB · TypeScript
RawDownload
import {  Cause,  Deferred,  Duration,  Effect,  Exit,  Fiber,  Inspectable,  Layer,  Option,  Predicate,  Ref,  Schema,  Semaphore,} from "effect";import { FetchHttpClient, type HttpClient } from "effect/unstable/http";import { fumadb } from "@executor-js/fumadb";import { memoryAdapter } from "@executor-js/fumadb/adapters/memory";import { withQueryContext, type Condition, type ConditionBuilder } from "@executor-js/fumadb/query";import { schema as fumaSchema, type RelationsMap } from "@executor-js/fumadb/schema";import type { AnyColumn } from "@executor-js/fumadb/schema";import {  StorageError,  CredentialWriteIncompleteError,  afterCommit,  afterCommitRequired,  isStorageFailure,  makeFumaClient,  type FumaDb,  type FumaRow,  type FumaTables,  type StorageFailure,} from "./fuma-runtime";import { makeFumaBlobStore, pluginBlobStore, type BlobStore, type OwnerPartitions } from "./blob";import { makePendingApprovalStore, type PendingApprovalStore } from "./pending-approval";import { coreToolsPlugin } from "./core-tools";import type {  Connection,  ConnectionInputOrigin,  ConnectionRef,  CreateConnectionInput,  ConnectionValueInput,  UpdateConnectionInput,  ValidateConnectionInput,} from "./connection";import {  HealthCheckReason,  HealthCheckResult,  HealthCheckSpec,  isToolSyncHealth,  toolSyncHealthDetailPrefix,} from "./health-check";import type { HealthCheckCandidate } from "./health-check";import {  ARTIFACT_SUMMARY_COLUMNS,  coreSchema,  isToolPolicyAction,  TOOL_INVOCATION_COLUMNS,  type ConnectionRow,  type CoreSchema,  type IntegrationRow,  type OAuthClientRow,  type ToolInvocationRow,  type ToolRow,  type ToolPolicyRow,} from "./core-schema";import {  ElicitationDeclinedError,  ElicitationResponse,  FormElicitation,  type ElicitationHandler,  type ElicitationRequest,  type OnElicitation,  type InvokeOptions,} from "./elicitation";import { currentOrgWriteAccess, type OrgWriteAccess } from "./org-write-access";import {  restoreCredentialSnapshotsWithRecheck,  snapshotCredentialWrites,  type CredentialWriteSnapshot,} from "./credential-compensation";import {  credentialAttemptItemId,  makeCredentialWriteAttempt,  parseCredentialWriteAttempt,} from "./credential-item-reference"; export type { OnElicitation, InvokeOptions } from "./elicitation";import {  rowToArtifact,  rowToArtifactSummary,  type Artifact,  type ArtifactSummary,  type RemoveArtifactInput,  type RenameArtifactInput,  type SaveArtifactInput,  type SetArtifactPreviewInput,} from "./artifact";import {  ArtifactNotFoundError,  ConnectionAlreadyExistsError,  ConnectionNotFoundError,  CredentialProviderNotRegisteredError,  CredentialResolutionError,  IntegrationNotFoundError,  InvalidConnectionInputError,  IntegrationRemovalNotAllowedError,  NoHandlerError,  OrgWriteDeniedError,  PluginNotLoadedError,  ToolBlockedError,  ToolInvocationError,  ToolNotFoundError,  type ExecuteError,} from "./errors";import {  ArtifactId,  AuthTemplateSlug,  ConnectionAddress,  ConnectionName,  IntegrationSlug,  NO_AUTH_TEMPLATE,  OAuthClientSlug,  Owner,  PolicyId,  ProviderItemId,  ProviderKey,  Subject,  Tenant,  ToolAddress,  ToolName,} from "./ids";import type {  AuthMethodDescriptor,  Integration,  IntegrationChangeEvent,  IntegrationConfig,  IntegrationDisplayDescriptor,  RegisterIntegrationInput,} from "./integration";import {  makeOAuthService,  STORE_WRITABILITY_PROBE_VALUE,  storeWritabilityProbeItemIdFor,  type MintOAuthConnectionInput,  type OAuthScopePolicy,} from "./oauth-service";import {  isFirstPartyOAuthClientSlug,  parseStoredTokenEndpointAuthMethod,  type OAuthService,  type TokenEndpointAuthMethod,} from "./oauth-client";import type { FirstPartyOAuthClientConfig } from "./oauth-client";import {  comparePolicyRow,  isUnboundedDynamicToolScope,  isValidPattern,  matchPattern,  positionForNewPattern,  resolveEffectivePolicy,  rowToToolPolicy,  type CreateToolPolicyInput,  type EffectivePolicy,  type RemoveToolPolicyInput,  type ToolPolicy,  type UpdateToolPolicyInput,} from "./policies";import type { CredentialProvider, ProviderEntry } from "./provider";import { touchSubject } from "./subject-registry";import type {  AnyPlugin,  Elicit,  IntegrationConfigureSchema,  IntegrationPresetCatalogEntry,  IntegrationRecord,  OwnerBinding,  PluginCtx,  PluginExtensions,  ResolveToolsResult,  StaticIntegrationDecl,  StaticToolDecl,  StorageDeps,  PreparedToolPolicy,  ToolPolicyProvider,  ToolPolicyProviderRule,  ToolInvocationCredential,} from "./plugin";import {  pluginStorageId,  type PluginStorageCollectionData,  type PluginStorageCollectionDefinition,  type PluginStorageCollectionQueryInput,  type PluginStorageEntry,  type PluginStorageFacade,  type PluginStorageRuntimeCollectionDefinition,  type PluginStorageRuntimeIndexSpec,} from "./plugin-storage";import {  assertExecutorOwnerPolicyTable,  ORG_SUBJECT,  type ExecutorOwnerPolicyContext,} from "./owner-policy";import { ToolAnnotationsView, ToolSchemaView, type IntegrationDetectionResult } from "./types";import { type Tool, type ToolAnnotations, type ToolDef, type ToolListFilter } from "./tool";import { buildToolTypeScriptPreview } from "./schema-types";import { collectReferencedDefinitions } from "./schema-refs";import {  refreshAccessToken,  exchangeClientCredentials,  isPermanentTokenRejection,  isUnusableSuccessTokenResponse,  optionalScopesFromAuthorizationUrl,  shouldRefreshToken,  type OAuth2TokenResponse,  type OAuthEndpointUrlPolicy,} from "./oauth-helpers";import {  ENTERPRISE_MANAGED_PROVIDER_STATE_KEY,  enterpriseManagedStateFrom,  mintEnterpriseManagedAccessToken,  type EnterpriseManagedMintError,  type EnterpriseManagedRollout,} from "./oauth-ema";import { connectionIdentifier } from "./connection-name-identifier";import { annotateToolResultOutcome, isToolResult } from "./tool-result";import { makeShapeMemory, observedShapeToJsonSchema, SHAPE_MEMORY_PLUGIN_ID } from "./shape-memory";import { isUnauthorizedToolFailure } from "./auth-tool-failure"; const PLUGIN_STORAGE_DELETE_KEY_BATCH_SIZE = 90;const MAX_APPROVAL_ARGUMENT_PREVIEW_CHARS = 4_000; // ---------------------------------------------------------------------------// In-flight OAuth refresh gate — a CROSS-STACK resource.//// Concurrent resolves of one connection must share a single refresh-token// grant: the authorization server rotates the refresh token, so a second// grant redeems a token the first already consumed, and a provider that// detects reuse revokes the whole token family. The first refresh cycle still// succeeds, so the fault hides until a later expiry.//// The gate therefore cannot live on a single execution stack. A host builds a// fresh scoped executor per MCP session (and, since request-scoped stack// builds, per request), so a per-`createExecutor` map put every session in its// own gate and deduplicated nothing. Hanging it off the root DB handle instead// converges every stack over one handle on one map — the same object hosts// already treat as their shared, process-lived resource.//// SCOPE OF THE GUARANTEE: dedup reaches exactly as far as one root DB handle// in one process. A host that hands every scoped executor a FRESH handle keys// a different map each time and gets no dedup — silently, because an unshared// gate still behaves correctly for the one caller holding it. Multi-instance// deployments are outside it for the same reason: a process-local map cannot// see a peer isolate or replica. Both need database-backed coordination// (compare-and-swap on the stored refresh token) rather than a wider map.//// Weakly keyed so the map dies with the handle and a host that opens and drops// handles does not leak one gate per handle.type RefreshGate = Map<  string,  Deferred.Deferred<string | null, StorageFailure | CredentialResolutionError>>; const refreshGateByRootDb = new WeakMap<object, RefreshGate>(); const refreshGateFor = (rootDb: object): RefreshGate => {  const existing = refreshGateByRootDb.get(rootDb);  if (existing) return existing;  const created: RefreshGate = new Map();  refreshGateByRootDb.set(rootDb, created);  return created;}; // ---------------------------------------------------------------------------// Elicitation handler — resolved once at `createExecutor({ onElicitation })`// and overridable per `execute`. A tool that requests user input mid-execution// suspends the fiber and the handler decides how to respond. The "accept-all"// sentinel auto-accepts (tests / non-interactive hosts).// --------------------------------------------------------------------------- const acceptAllHandler: ElicitationHandler = () =>  Effect.succeed(ElicitationResponse.make({ action: "accept" })); const resolveElicitationHandler = (onElicitation: OnElicitation): ElicitationHandler =>  onElicitation === "accept-all" ? acceptAllHandler : onElicitation; // ---------------------------------------------------------------------------// Address scheme — `tools.<integration>.<owner>.<connection>.<tool>`.// --------------------------------------------------------------------------- const ADDRESS_PREFIX = "tools"; export interface ParsedToolAddress {  readonly integration: IntegrationSlug;  readonly owner: Owner;  readonly connection: ConnectionName;  readonly tool: ToolName;} const isOwner = (value: string): value is Owner => value === "org" || value === "user"; /** Parse a callable address; null when it's not a well-formed *  `tools.<integration>.<owner>.<connection>.<tool>`. * *  The four leading segments (prefix, integration, owner, connection) are *  slug-like and never contain a `.`; the `<tool>` segment is the *entire* *  remainder after the 4th dot, so it may itself contain dots. That lets a tool *  whose name is a structured `group.leaf` path (e.g. an OpenAPI *  `aliases.deleteAlias`) address naturally as *  `tools.<integration>.<owner>.<connection>.aliases.deleteAlias` — the same *  dotted nesting the sandbox `tools` proxy produces from property access. */export const parseToolAddress = (address: string): ParsedToolAddress | null => {  // Walk to the 4th dot; everything past it is the tool (dots and all).  let cut = -1;  for (let i = 0; i < 4; i++) {    cut = address.indexOf(".", cut + 1);    if (cut === -1) return null;  }  const [prefix, integration, owner, connection] = address.slice(0, cut).split(".") as [    string,    string,    string,    string,  ];  const tool = address.slice(cut + 1);  if (prefix !== ADDRESS_PREFIX) return null;  if (!isOwner(owner)) return null;  if (integration.length === 0 || connection.length === 0 || tool.length === 0) {    return null;  }  return {    integration: IntegrationSlug.make(integration),    owner,    connection: ConnectionName.make(connection),    tool: ToolName.make(tool),  };}; export const connectionAddress = (  owner: Owner,  integration: IntegrationSlug,  connection: ConnectionName,): ConnectionAddress =>  ConnectionAddress.make(`${ADDRESS_PREFIX}.${integration}.${owner}.${connection}`); export const toolAddress = (  owner: Owner,  integration: IntegrationSlug,  connection: ConnectionName,  tool: ToolName,): ToolAddress =>  ToolAddress.make(`${ADDRESS_PREFIX}.${integration}.${owner}.${connection}.${tool}`); // ---------------------------------------------------------------------------// Owner key helpers — every owned-row write stamps `tenant`, `owner`,// `subject` (org → subject="").// --------------------------------------------------------------------------- interface OwnedKeys {  readonly tenant: string;  readonly owner: Owner;  readonly subject: string;} // ---------------------------------------------------------------------------// Executor — public surface. Every list/execute/schema call is a direct// core-table query unioned with the in-memory static pool.// --------------------------------------------------------------------------- export type Executor<TPlugins extends readonly AnyPlugin[] = readonly []> = {  readonly integrations: {    readonly list: () => Effect.Effect<readonly Integration[], StorageFailure>;    readonly get: (slug: IntegrationSlug) => Effect.Effect<Integration | null, StorageFailure>;    readonly update: (      slug: IntegrationSlug,      patch: { readonly name?: string; readonly description?: string },    ) => Effect.Effect<void, IntegrationNotFoundError | OrgWriteDeniedError | StorageFailure>;    readonly remove: (      slug: IntegrationSlug,    ) => Effect.Effect<      void,      IntegrationRemovalNotAllowedError | OrgWriteDeniedError | StorageFailure    >;    readonly detect: (      url: string,    ) => Effect.Effect<readonly IntegrationDetectionResult[], StorageFailure>;    /** The integration's declared health check: which authenticated operation a     *  connection runs to prove its credential is alive and surface whose     *  account it is. Configured by the user the same way auth methods are. */    readonly healthCheck: {      /** The currently declared check, or null when none is configured (or the       *  owning plugin has no health-check capability). */      readonly get: (        slug: IntegrationSlug,      ) => Effect.Effect<HealthCheckSpec | null, StorageFailure>;      /** The operations a user can pick from, ranked non-destructive-first then       *  fewest required arguments. Empty when the plugin has no candidates. */      readonly candidates: (        slug: IntegrationSlug,      ) => Effect.Effect<        readonly HealthCheckCandidate[],        IntegrationNotFoundError | StorageFailure      >;      /** Declare (or clear, with `null`) the health check for the integration. */      readonly set: (        slug: IntegrationSlug,        spec: HealthCheckSpec | null,      ) => Effect.Effect<void, IntegrationNotFoundError | OrgWriteDeniedError | StorageFailure>;    };  };   readonly connections: {    readonly create: (      input: CreateConnectionInput,    ) => Effect.Effect<      Connection,      | IntegrationNotFoundError      | ConnectionAlreadyExistsError      | CredentialProviderNotRegisteredError      | InvalidConnectionInputError      | OrgWriteDeniedError      | StorageFailure    >;    readonly list: (filter?: {      readonly integration?: IntegrationSlug;      readonly owner?: Owner;    }) => Effect.Effect<readonly Connection[], StorageFailure>;    readonly get: (ref: ConnectionRef) => Effect.Effect<Connection | null, StorageFailure>;    /** Edit user-curated metadata (description, identityLabel). Credentials and     *  OAuth lifecycle fields are not editable here. */    readonly update: (      ref: ConnectionRef,      input: UpdateConnectionInput,    ) => Effect.Effect<Connection, ConnectionNotFoundError | OrgWriteDeniedError | StorageFailure>;    readonly remove: (      ref: ConnectionRef,    ) => Effect.Effect<void, ConnectionNotFoundError | OrgWriteDeniedError | StorageFailure>;    readonly refresh: (      ref: ConnectionRef,    ) => Effect.Effect<      readonly Tool[],      ConnectionNotFoundError | IntegrationNotFoundError | OrgWriteDeniedError | StorageFailure    >;    /** Run the integration's declared health check against a saved connection:     *  classify the credential (healthy / expired / degraded / unknown) and     *  extract its identity for display. Never throws on an auth wall or upstream     *  error: those come back as a `HealthCheckResult` with the matching status. */    readonly checkHealth: (      ref: ConnectionRef,      options?: {        /** Return the persisted verdict when younger than this; probe         *  otherwise. Omit to always probe. */        readonly ifStaleMs?: number;      },    ) => Effect.Effect<      HealthCheckResult,      ConnectionNotFoundError | IntegrationNotFoundError | StorageFailure    >;    /** Validate an in-flight credential WITHOUT saving it (key-first connect):     *  resolve the pasted value(s), run the health check, and return the result     *  so the caller can confirm the key works and derive a name from the     *  identity before creating the connection. */    readonly validate: (      input: ValidateConnectionInput,    ) => Effect.Effect<HealthCheckResult, IntegrationNotFoundError | StorageFailure>;  };   /** Shared OAuth service. Hosts use this through the core HTTP OAuth group;   *  plugins see the same service as `ctx.oauth`. */  readonly oauth: OAuthService;   readonly tools: {    readonly list: (filter?: ToolListFilter) => Effect.Effect<readonly Tool[], StorageFailure>;    readonly schema: (address: ToolAddress) => Effect.Effect<ToolSchemaView | null, StorageFailure>;  };   readonly providers: {    readonly list: () => Effect.Effect<readonly ProviderKey[]>;    readonly items: (key: ProviderKey) => Effect.Effect<readonly ProviderEntry[], StorageFailure>;  };   readonly policies: {    readonly list: () => Effect.Effect<readonly ToolPolicy[], StorageFailure>;    readonly create: (      input: CreateToolPolicyInput,    ) => Effect.Effect<ToolPolicy, OrgWriteDeniedError | StorageFailure>;    readonly update: (      input: UpdateToolPolicyInput,    ) => Effect.Effect<ToolPolicy, OrgWriteDeniedError | StorageFailure>;    readonly remove: (      input: RemoveToolPolicyInput,    ) => Effect.Effect<void, OrgWriteDeniedError | StorageFailure>;    readonly resolve: (address: ToolAddress) => Effect.Effect<EffectivePolicy, StorageFailure>;  };   /**   * The PLATFORM VIEW: read-only, tenant-wide reads across every subject.   * Present only when the executor was built with `platformView: true`   * (default off) — every other surface on this executor stays bound to the   * single `{ tenant, subject }` product view and is unaffected by this one.

Lines 1–500 of 7336.