Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

11.1 KB
Raw Blame
import { rm } from 'node:fs/promises'
import { join } from 'node:path'
import { ActivepiecesError, ErrorCode, isNil, tryCatch } from '@activepieces/core-utils'
import { type ApLogger, apVersionUtil, wideEvent } from '@activepieces/server-utils'
import { PrewarmScopeFileContent, WorkerToApiContract } from '@activepieces/shared'
import { cacheUtils } from './cache/cache-paths'
import { localExecutionCache } from './cache/local-execution-cache'
import { engineRpcHandlers } from './engine-rpc-handlers'
import { createResolver } from './resolver'
import { createSandboxManager, SandboxManager } from './sandbox-manager'
import {
    ExecuteParams,
    PreWarmSandboxParams,
    ProvisionInput,
    Resolver,
    Runtime,
    RuntimeExecutionResult,
    RuntimeExecutorInfo,
    SandboxSettings,
} from './types'
import { bundleHttp } from './utils/bundle-http'

// One box per worker at the destination (concurrency 1), or N independent boxes in the transitional
// compatibility mode that honors AP_WORKER_CONCURRENCY. Each box is its own manager, holding one
// in-flight operation at a time; the worker runs one poll loop per box and routes each execute to its
// box by workerIndex. The boxes share the on-disk caches, which are already concurrency-safe
// (threadSafeMkdir / cache-state), so there is no per-key provision dedup here. execute owns the slot
// lifecycle: acquire -> provision -> run -> release on success / invalidate on throw, re-raising the
// sandbox ActivepiecesError codes (timeout / memory / log-size) that handlers already catch. See ADR 0004.
export function createSandboxRuntime({ concurrency = 1, basePath, getSettings }: CreateSandboxRuntimeParams): Runtime {
    const managers: SandboxManager[] = Array.from({ length: concurrency }, (_, index) =>
        createSandboxManager({ boxId: index + 1, basePath, getSettings }),
    )

    return {
        async execute({ workerIndex, log, operationType, operation, timeoutInSeconds, expiresAt, provision }: ExecuteParams): Promise<RuntimeExecutionResult> {
            const manager = managers[workerIndex]
            if (isNil(manager)) {
                throw new ActivepiecesError({
                    code: ErrorCode.VALIDATION,
                    params: { message: `No sandbox manager for worker index ${workerIndex} (concurrency=${concurrency})` },
                })
            }
            const sandbox = manager.acquire({ log })

            const provisionStartedAt = Date.now()
            const { error: provisionError } = await tryCatch(() => localExecutionCache(log, basePath, getSettings).provision({
                pieces: provision.pieces,
                codeSteps: provision.codes,
                internalApiUrl: provision.internalApiUrl,
                engineToken: provision.engineToken,
            }))
            if (provisionError) {
                await manager.invalidate(log)
                throw provisionError
            }
            const provisionMs = Date.now() - provisionStartedAt

            try {
                let bootMs = 0
                let runMs = 0
                // Break the engine timeline into its two worker-observable phases:
                //   sandboxStart = fork the engine child + Node boot + parse main.js (V8-cached) +
                //                  isolated-vm init + socket connect handshake.
                //   sandboxRun   = send the operation + the engine runs the flow steps + returns.
                // executionMs wraps both (total), so the report shows execution = start + run.
                const result = await wideEvent.timed({
                    name: 'execution',
                    fn: async () => {
                        const bootStartedAt = Date.now()
                        await wideEvent.timed({
                            name: 'sandboxStart',
                            fn: () => sandbox.start({
                                flowVersionId: provision.flowVersionId,
                                platformId: provision.platformId,
                                mounts: [],
                            }),
                        })
                        bootMs = Date.now() - bootStartedAt
                        const runTimeoutInSeconds = remainingTimeoutInSeconds({ timeoutInSeconds, expiresAt })
                        if (runTimeoutInSeconds <= 0) {
                            throw new ActivepiecesError({
                                code: ErrorCode.SANDBOX_EXECUTION_TIMEOUT,
                                params: {
                                    standardOutput: '',
                                    standardError: `Caller deadline passed while starting the sandbox (provisionMs=${provisionMs}, bootMs=${bootMs}), the operation was never started`,
                                    neverStarted: true,
                                },
                            })
                        }
                        const runStartedAt = Date.now()
                        const runResult = await wideEvent.timed({
                            name: 'sandboxRun',
                            fn: () => sandbox.execute(operationType, operation, {
                                timeoutInSeconds: runTimeoutInSeconds,
                                engineRpc: engineRpcHandlers({ log, basePath, getSettings, provision }),
                            }),
                        })
                        runMs = Date.now() - runStartedAt
                        return runResult
                    },
                })
                await manager.release(log)
                return { ...result, timings: { provisionMs, bootMs, runMs } }
            }
            catch (error) {
                await manager.invalidate(log)
                throw error
            }
        },
        getActiveExecutors(): RuntimeExecutorInfo[] {
            return managers
                .map((manager) => manager.getActiveSandbox())
                .filter((info) => !isNil(info))
                .map((info) => ({
                    sandboxId: info.sandboxId,
                    boxId: info.boxId,
                    pid: info.pid,
                    busy: info.busy,
                }))
        },
        async prewarm({ log, apiClient, internalApiUrl, flow }: PreWarmSandboxParams): Promise<void> {
            if (isNil(apiClient) || isNil(internalApiUrl)) {
                return
            }
            const startedAt = Date.now()
            const { error } = await tryCatch(async () => {
                if (!isNil(flow)) {
                    // The version JSON may have been updated in place (e.g. admin deno migration):
                    // drop the local copies so the resolve below refetches fresh data.
                    await evictFlowVersionCaches({ basePath, flowVersionId: flow.versionId })
                }
                const prewarmData = await apiClient.getPrewarmData({
                    workerGroupId: getSettings().WORKER_GROUP_ID,
                    projectWorker: getSettings().PROJECT_WORKER,
                    workerVersion: apVersionUtil.getCurrentRelease(),
                    flow,
                })
                const { platformId, engineToken } = prewarmData
                // Platform-wide prewarm uses the distinct piece/code set the app computed in one pass.
                // The targeted (flowPublished) prewarm still resolves worker-side, which also publishes
                // the flow bundle.
                const { pieces, codeSteps } = isNil(flow)
                    ? await fetchScopeFile({ apiClient, scopeFileId: prewarmData.scopeFileId })
                    : await resolveFlowsForPrewarm({
                        resolver: createResolver({ apiClient, basePath, getSettings, log }),
                        flows: prewarmData.flows ?? [],
                        platformId,
                        internalApiUrl,
                        engineToken,
                        log,
                    })
                await localExecutionCache(log, basePath, getSettings).provision({ pieces, codeSteps, internalApiUrl, engineToken, bestEffort: true })
                log.info({ pieceCount: pieces.length, codeStepCount: codeSteps.length, durationMs: Date.now() - startedAt }, 'Prewarmed sandbox cache')
            })
            if (error) {
                log.warn({ error: String(error) }, 'Cache prewarm failed')
            }
        },
        async shutdown(shutdownLog: ApLogger): Promise<void> {
            await Promise.all(managers.map((manager) => manager.shutdown(shutdownLog)))
        },
    }
}

async function evictFlowVersionCaches({ basePath, flowVersionId }: { basePath: string, flowVersionId: string }): Promise<void> {
    const paths = cacheUtils(basePath)
    await Promise.all([
        rm(join(paths.getGlobalCacheFlowsPath(), flowVersionId), { recursive: true, force: true }),
        rm(join(paths.getGlobalCacheBundlesPath(), flowVersionId), { recursive: true, force: true }),
    ])
}

async function fetchScopeFile({ apiClient, scopeFileId }: FetchScopeFileParams): Promise<ResolvedPrewarmInputs> {
    if (isNil(scopeFileId)) {
        return { pieces: [], codeSteps: [] }
    }
    const response = await apiClient.getPrewarmScopeFile({ fileId: scopeFileId })
    if (isNil(response)) {
        return { pieces: [], codeSteps: [] }
    }
    const data = response.kind === 'url' ? await bundleHttp.getBuffer(response.url) : response.data
    const content = JSON.parse(data.toString('utf8')) as PrewarmScopeFileContent
    return { pieces: content.pieces, codeSteps: content.codes }
}

async function resolveFlowsForPrewarm({ resolver, flows, platformId, internalApiUrl, engineToken, log }: ResolveFlowsForPrewarmParams): Promise<ResolvedPrewarmInputs> {
    const resolvedFlows = await Promise.all(flows.map(async (flow) => {
        const { data: resolved, error: flowError } = await tryCatch(() => resolver.resolve({ flow, platformId, internalApiUrl, engineToken }))
        if (flowError) {
            log.warn({ error: String(flowError), flow: { id: flow.id } }, 'Failed to resolve flow for prewarm')
            return null
        }
        return resolved.kind === 'ready' ? resolved.provision : null
    }))
    const provisions = resolvedFlows.filter((provision) => !isNil(provision))
    return {
        pieces: provisions.flatMap((provision) => provision.pieces),
        codeSteps: provisions.flatMap((provision) => provision.codes),
    }
}


function remainingTimeoutInSeconds({ timeoutInSeconds, expiresAt }: { timeoutInSeconds: number, expiresAt?: number }): number {
    if (isNil(expiresAt)) {
        return timeoutInSeconds
    }
    return Math.min(timeoutInSeconds, Math.floor((expiresAt - Date.now()) / 1000))
}

type FetchScopeFileParams = {
    apiClient: WorkerToApiContract
    scopeFileId: string | undefined
}

type ResolveFlowsForPrewarmParams = {
    resolver: Resolver
    flows: { id: string, versionId: string, projectId: string }[]
    platformId: string
    internalApiUrl: string
    engineToken: string
    log: ApLogger
}

type ResolvedPrewarmInputs = {
    pieces: ProvisionInput['pieces']
    codeSteps: ProvisionInput['codes']
}

type CreateSandboxRuntimeParams = {
    concurrency?: number
    basePath: string
    getSettings: () => SandboxSettings

}