1284 lines · 39.3 KB · TypeScript
RawDownload
import { randomUUID } from "node:crypto";import { dirname, join } from "node:path";import { FileSystem } from "@effect/platform";import { NodeFileSystem } from "@effect/platform-node"; import {  AuthArtifactSchema,  AuthLeaseSchema,  type AuthArtifact,  type AuthLease,  type Execution,  type ExecutionInteraction,  type ExecutionStep,  ExecutionInteractionSchema,  ExecutionSchema,  ExecutionStepSchema,  type ProviderAuthGrant,  ProviderAuthGrantSchema,  SecretMaterialSchema,  type SecretMaterial,  type SourceAuthSession,  SourceAuthSessionSchema,  type ScopeOauthClient,  ScopeOauthClientSchema,  type ScopedSourceOauthClient,  ScopedSourceOauthClientSchema,} from "@executor/platform-sdk/schema";import * as Effect from "effect/Effect";import * as Option from "effect/Option";import * as Schema from "effect/Schema"; import type { ResolvedLocalWorkspaceContext } from "./config";import { deriveLocalInstallation } from "./installation";import {  LocalFileSystemError,  unknownLocalErrorDetails,} from "./errors"; const LOCAL_EXECUTOR_STATE_VERSION = 1 as const;const LOCAL_EXECUTOR_STATE_BASENAME = "executor-state.json"; const LocalExecutorStateSnapshotSchema = Schema.Struct({  version: Schema.Literal(LOCAL_EXECUTOR_STATE_VERSION),  authArtifacts: Schema.Array(AuthArtifactSchema),  authLeases: Schema.Array(AuthLeaseSchema),  sourceOauthClients: Schema.Array(ScopedSourceOauthClientSchema),  scopeOauthClients: Schema.Array(ScopeOauthClientSchema),  providerAuthGrants: Schema.Array(ProviderAuthGrantSchema),  sourceAuthSessions: Schema.Array(SourceAuthSessionSchema),  secretMaterials: Schema.Array(SecretMaterialSchema),  executions: Schema.Array(ExecutionSchema),  executionInteractions: Schema.Array(ExecutionInteractionSchema),  executionSteps: Schema.Array(ExecutionStepSchema),}); export type LocalExecutorStateSnapshot = typeof LocalExecutorStateSnapshotSchema.Type; export type LocalExecutorStatePersistence = {  executorState: LocalExecutorStateStore;  close: () => Promise<void>;}; const decodeLocalExecutorStateSnapshot = Schema.decodeUnknown(  LocalExecutorStateSnapshotSchema,); const defaultLocalExecutorStateSnapshot = (): LocalExecutorStateSnapshot => ({  version: LOCAL_EXECUTOR_STATE_VERSION,  authArtifacts: [],  authLeases: [],  sourceOauthClients: [],  scopeOauthClients: [],  providerAuthGrants: [],  sourceAuthSessions: [],  secretMaterials: [],  executions: [],  executionInteractions: [],  executionSteps: [],}); const cloneValue = <T>(value: T): T =>  JSON.parse(JSON.stringify(value)) as T; const asRecord = (  value: unknown,): Record<string, unknown> | null =>  value !== null && typeof value === "object" && !Array.isArray(value)    ? (value as Record<string, unknown>)    : null; const migrateLegacyExecutorStateValue = (  value: unknown,): {  value: unknown;  migrated: boolean;} => {  if (Array.isArray(value)) {    let migrated = false;    const next = value.map((item) => {      const result = migrateLegacyExecutorStateValue(item);      migrated = migrated || result.migrated;      return result.value;    });    return { value: next, migrated };  }   const record = asRecord(value);  if (record === null) {    return { value, migrated: false };  }   let migrated = false;  const next: Record<string, unknown> = {};   for (const [key, entry] of Object.entries(record)) {    let nextKey = key;    if (key === "workspaceId") {      nextKey = "scopeId";      migrated = true;    } else if (key === "actorAccountId") {      nextKey = "actorScopeId";      migrated = true;    } else if (key === "createdByAccountId") {      nextKey = "createdByScopeId";      migrated = true;    } else if (key === "workspaceOauthClients") {      nextKey = "scopeOauthClients";      migrated = true;    }     const migratedEntry = migrateLegacyExecutorStateValue(entry);    migrated = migrated || migratedEntry.migrated;    next[nextKey] = migratedEntry.value;  }   return { value: next, migrated };}; const mapFileSystemError = (path: string, action: string) => (cause: unknown) =>  new LocalFileSystemError({    message: `Failed to ${action} ${path}: ${unknownLocalErrorDetails(cause)}`,    action,    path,    details: unknownLocalErrorDetails(cause),  }); const actorEquals = (  left: string | null | undefined,  right: string | null | undefined,): boolean => (left ?? null) === (right ?? null); const sortByUpdatedAtAndIdAsc = <T extends { updatedAt: number; id: string }>(  values: readonly T[],): T[] =>  [...values].sort((left, right) =>    left.updatedAt - right.updatedAt || left.id.localeCompare(right.id),  ); const sortByUpdatedAtAndIdDesc = <T extends { updatedAt: number; id: string }>(  values: readonly T[],): T[] =>  [...values].sort((left, right) =>    right.updatedAt - left.updatedAt || right.id.localeCompare(left.id),  ); const localExecutorStatePath = (  context: ResolvedLocalWorkspaceContext,): string =>  join(    context.homeStateDirectory,    "workspaces",    deriveLocalInstallation(context).scopeId,    LOCAL_EXECUTOR_STATE_BASENAME,  ); const bindFileSystem = <A, E>(  fileSystem: FileSystem.FileSystem,  effect: Effect.Effect<A, E, FileSystem.FileSystem>,): Effect.Effect<A, E, never> =>  effect.pipe(Effect.provideService(FileSystem.FileSystem, fileSystem)); const bindNodeFileSystem = <A, E>(  effect: Effect.Effect<A, E, FileSystem.FileSystem>,): Effect.Effect<A, E, never> =>  effect.pipe(Effect.provide(NodeFileSystem.layer)); const readStateFromDisk = (  context: ResolvedLocalWorkspaceContext,): Effect.Effect<LocalExecutorStateSnapshot, LocalFileSystemError, FileSystem.FileSystem> =>  Effect.gen(function* () {    const fs = yield* FileSystem.FileSystem;    const path = localExecutorStatePath(context);    const exists = yield* fs.exists(path).pipe(      Effect.mapError(mapFileSystemError(path, "check executor state path")),    );    if (!exists) {      return defaultLocalExecutorStateSnapshot();    }     const content = yield* fs.readFileString(path, "utf8").pipe(      Effect.mapError(mapFileSystemError(path, "read executor state")),    );    const parsed = yield* Effect.try({      try: () => JSON.parse(content) as unknown,      catch: mapFileSystemError(path, "parse executor state"),    });    const migrated = migrateLegacyExecutorStateValue(parsed);    const decoded = yield* decodeLocalExecutorStateSnapshot(migrated.value).pipe(      Effect.mapError(mapFileSystemError(path, "decode executor state")),    );    if (migrated.migrated) {      yield* writeStateToDisk(context, decoded);    }    return decoded;  }); const writeStateToDisk = (  context: ResolvedLocalWorkspaceContext,  state: LocalExecutorStateSnapshot,): Effect.Effect<void, LocalFileSystemError, FileSystem.FileSystem> =>  Effect.gen(function* () {    const fs = yield* FileSystem.FileSystem;    const path = localExecutorStatePath(context);    const tempPath = `${path}.${randomUUID()}.tmp`;     yield* fs.makeDirectory(dirname(path), { recursive: true }).pipe(      Effect.mapError(mapFileSystemError(dirname(path), "create executor state directory")),    );    yield* fs.writeFileString(tempPath, `${JSON.stringify(state, null, 2)}\n`, {      mode: 0o600,    }).pipe(      Effect.mapError(mapFileSystemError(tempPath, "write executor state")),    );    yield* fs.rename(tempPath, path).pipe(      Effect.mapError(mapFileSystemError(path, "replace executor state")),    );  }); export const loadLocalExecutorStateSnapshot = (  context: ResolvedLocalWorkspaceContext,): Effect.Effect<LocalExecutorStateSnapshot, LocalFileSystemError> =>  bindNodeFileSystem(readStateFromDisk(context)); export const writeLocalExecutorStateSnapshot = (input: {  context: ResolvedLocalWorkspaceContext;  state: LocalExecutorStateSnapshot;}): Effect.Effect<void, LocalFileSystemError> =>  bindNodeFileSystem(writeStateToDisk(input.context, input.state)); const mergeById = <T extends { id: string }>(  current: readonly T[],  imported: readonly T[],): T[] => {  const merged = new Map<string, T>();   for (const item of imported) {    merged.set(item.id, cloneValue(item));  }   for (const item of current) {    merged.set(item.id, cloneValue(item));  }   return [...merged.values()];}; const mergeAuthLeases = (  current: readonly AuthLease[],  imported: readonly AuthLease[],): AuthLease[] => {  const merged = new Map<string, AuthLease>();   for (const lease of imported) {    merged.set(lease.authArtifactId, cloneValue(lease));  }   for (const lease of current) {    merged.set(lease.authArtifactId, cloneValue(lease));  }   return [...merged.values()];}; const mergeAuthArtifacts = (  current: readonly AuthArtifact[],  imported: readonly AuthArtifact[],): AuthArtifact[] => {  const merged = new Map<string, AuthArtifact>();   for (const artifact of imported) {    merged.set(      [        artifact.scopeId,        artifact.sourceId,        artifact.actorScopeId ?? "",        artifact.slot,      ].join("::"),      cloneValue(artifact),    );  }   for (const artifact of current) {    merged.set(      [        artifact.scopeId,        artifact.sourceId,        artifact.actorScopeId ?? "",        artifact.slot,      ].join("::"),      cloneValue(artifact),    );  }   return [...merged.values()];}; const mergeSourceOauthClients = (  current: readonly ScopedSourceOauthClient[],  imported: readonly ScopedSourceOauthClient[],): ScopedSourceOauthClient[] => {  const merged = new Map<string, ScopedSourceOauthClient>();   for (const oauthClient of imported) {    merged.set(      [oauthClient.scopeId, oauthClient.sourceId, oauthClient.providerKey].join(        "::",      ),      cloneValue(oauthClient),    );  }   for (const oauthClient of current) {    merged.set(      [oauthClient.scopeId, oauthClient.sourceId, oauthClient.providerKey].join(        "::",      ),      cloneValue(oauthClient),    );  }   return [...merged.values()];}; const mergeWorkspaceOauthClients = (  current: readonly ScopeOauthClient[],  imported: readonly ScopeOauthClient[],): ScopeOauthClient[] => {  const merged = new Map<string, ScopeOauthClient>();   for (const oauthClient of imported) {    merged.set(      [oauthClient.scopeId, oauthClient.providerKey, oauthClient.id].join("::"),      cloneValue(oauthClient),    );  }   for (const oauthClient of current) {    merged.set(      [oauthClient.scopeId, oauthClient.providerKey, oauthClient.id].join("::"),      cloneValue(oauthClient),    );  }   return [...merged.values()];}; const mergeProviderAuthGrants = (  current: readonly ProviderAuthGrant[],  imported: readonly ProviderAuthGrant[],): ProviderAuthGrant[] => mergeById(current, imported); export const mergeImportedLocalExecutorStateSnapshot = (input: {  current: LocalExecutorStateSnapshot;  imported: Partial<Omit<LocalExecutorStateSnapshot, "version">>;}): LocalExecutorStateSnapshot => ({  version: LOCAL_EXECUTOR_STATE_VERSION,  authArtifacts: mergeAuthArtifacts(    input.current.authArtifacts,    input.imported.authArtifacts ?? [],  ),  authLeases: mergeAuthLeases(    input.current.authLeases,    input.imported.authLeases ?? [],  ),  sourceOauthClients: mergeSourceOauthClients(    input.current.sourceOauthClients,    input.imported.sourceOauthClients ?? [],  ),  scopeOauthClients: mergeWorkspaceOauthClients(    input.current.scopeOauthClients,    input.imported.scopeOauthClients ?? [],  ),  providerAuthGrants: mergeProviderAuthGrants(    input.current.providerAuthGrants,    input.imported.providerAuthGrants ?? [],  ),  sourceAuthSessions: mergeById(    input.current.sourceAuthSessions,    input.imported.sourceAuthSessions ?? [],  ),  secretMaterials: mergeById(    input.current.secretMaterials,    input.imported.secretMaterials ?? [],  ),  executions: mergeById(input.current.executions, input.imported.executions ?? []),  executionInteractions: mergeById(    input.current.executionInteractions,    input.imported.executionInteractions ?? [],  ),  executionSteps: mergeById(    input.current.executionSteps,    input.imported.executionSteps ?? [],  ),}); type StateMutationResult<A> = {  state: LocalExecutorStateSnapshot;  value: A;}; const createStateManager = (  context: ResolvedLocalWorkspaceContext,  fileSystem: FileSystem.FileSystem,) => {  let cache: LocalExecutorStateSnapshot | null = null;  let mutationQueue: Promise<void> = Promise.resolve();  const run = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem>) =>    Effect.runPromise(bindFileSystem(fileSystem, effect));   const ensureLoaded = async (): Promise<LocalExecutorStateSnapshot> => {    if (cache !== null) {      return cache;    }     cache = await run(readStateFromDisk(context));    return cache;  };   const read = <A>(    operation: (state: LocalExecutorStateSnapshot) => A | Promise<A>,  ): Effect.Effect<A, LocalFileSystemError> =>    Effect.tryPromise({      try: async () => {        await mutationQueue;        return operation(cloneValue(await ensureLoaded()));      },      catch: mapFileSystemError(        localExecutorStatePath(context),        "read executor state",      ),    });   const mutate = <A>(    operation: (      state: LocalExecutorStateSnapshot,    ) => StateMutationResult<A> | Promise<StateMutationResult<A>>,  ): Effect.Effect<A, LocalFileSystemError> =>    Effect.tryPromise({      try: async () => {        let value!: A;        let failure: unknown = null;         mutationQueue = mutationQueue.then(async () => {          try {            const current = cloneValue(await ensureLoaded());            const result = await operation(current);            cache = result.state;            value = result.value;            await run(writeStateToDisk(context, cache));          } catch (cause) {            failure = cause;          }        });         await mutationQueue;         if (failure !== null) {          throw failure;        }         return value;      },      catch: mapFileSystemError(        localExecutorStatePath(context),        "write executor state",      ),    });   return {    read,    mutate,  };}; export const createLocalExecutorStateStore = (  context: ResolvedLocalWorkspaceContext,  fileSystem: FileSystem.FileSystem,) => {  const stateManager = createStateManager(context, fileSystem); 

Lines 1–500 of 1284.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t