UNPKG

@mondaydotcomorg/atp-compiler

Version:

Production-ready compiler for transforming async iteration patterns into resumable operations with checkpoint-based state management

1,641 lines (1,630 loc) 86.3 kB
import { parse } from '@babel/parser'; import _traverse from '@babel/traverse'; import _generate from '@babel/generator'; import * as t7 from '@babel/types'; import { getCallSequenceNumber, getCachedResult, nextSequenceNumber } from '@mondaydotcomorg/atp-runtime'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/types.ts var PAUSABLE_CALL_PATTERNS = [ { namespace: "atp.llm", method: "call" }, { namespace: "atp.llm", method: "extract" }, { namespace: "atp.llm", method: "classify" }, { namespace: "atp.llm", method: "stream" }, { namespace: "atp.llm", method: "generate" }, { namespace: "atp.approval", method: "request" }, { namespace: "atp.approval", method: "confirm" }, { namespace: "atp.approval", method: "verify" }, { namespace: "atp.embedding", method: "embed" }, { namespace: "atp.embedding", method: "search" }, { namespace: "atp.embedding", method: "create" }, { namespace: "atp.embedding", method: "generate" }, { namespace: "atp.embedding", method: "encode" } ]; var DEFAULT_COMPILER_CONFIG = { enableBatchParallel: true, maxLoopNesting: 10, checkpointInterval: 1, debugMode: false, batchSizeThreshold: 10 }; function isPausableCall(node) { if (!t7.isAwaitExpression(node)) { return false; } const argument = node.argument; if (!t7.isCallExpression(argument)) { return false; } return isPausableCallExpression(argument); } __name(isPausableCall, "isPausableCall"); function isPausableCallExpression(node) { const callee = node.callee; if (!t7.isMemberExpression(callee)) { return false; } const fullPath = getMemberExpressionPath(callee); return PAUSABLE_CALL_PATTERNS.some((pattern) => fullPath === `${pattern.namespace}.${pattern.method}`); } __name(isPausableCallExpression, "isPausableCallExpression"); function getMemberExpressionPath(node) { const parts = []; let current = node; while (t7.isMemberExpression(current)) { if (t7.isIdentifier(current.property)) { parts.unshift(current.property.name); } current = current.object; } if (t7.isIdentifier(current)) { parts.unshift(current.name); } return parts.join("."); } __name(getMemberExpressionPath, "getMemberExpressionPath"); function containsAwait(node) { let hasAwait = false; const checkNode = /* @__PURE__ */ __name((n) => { if (t7.isAwaitExpression(n)) { hasAwait = true; return; } if (hasAwait) return; Object.keys(n).forEach((key) => { const value = n[key]; if (Array.isArray(value)) { value.forEach((item) => { if (item && typeof item === "object" && item.type) { checkNode(item); } }); } else if (value && typeof value === "object" && value.type) { checkNode(value); } }); }, "checkNode"); checkNode(node); return hasAwait; } __name(containsAwait, "containsAwait"); function containsPausableCall(node) { let hasPausable = false; const checkNode = /* @__PURE__ */ __name((n) => { if (t7.isAwaitExpression(n) && isPausableCall(n)) { hasPausable = true; return; } if (hasPausable) return; Object.keys(n).forEach((key) => { const value = n[key]; if (Array.isArray(value)) { value.forEach((item) => { if (item && typeof item === "object" && item.type) { checkNode(item); } }); } else if (value && typeof value === "object" && value.type) { checkNode(value); } }); }, "checkNode"); checkNode(node); return hasPausable; } __name(containsPausableCall, "containsPausableCall"); function isAsyncFunction(node) { return (t7.isFunctionDeclaration(node) || t7.isFunctionExpression(node) || t7.isArrowFunctionExpression(node)) && node.async === true; } __name(isAsyncFunction, "isAsyncFunction"); function getNodeLocation(node) { if (node.loc) { return { line: node.loc.start.line, column: node.loc.start.column }; } return void 0; } __name(getNodeLocation, "getNodeLocation"); function createRuntimeCall(fnName, args) { return t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier(fnName)), args)); } __name(createRuntimeCall, "createRuntimeCall"); function wrapInAsyncFunction(body) { return t7.functionExpression(null, [], t7.blockStatement(body), false, true); } __name(wrapInAsyncFunction, "wrapInAsyncFunction"); function isArrayMethod(node, methodName) { if (!t7.isCallExpression(node)) { return false; } const callee = node.callee; if (!t7.isMemberExpression(callee)) { return false; } return t7.isIdentifier(callee.property) && callee.property.name === methodName; } __name(isArrayMethod, "isArrayMethod"); function extractForOfParamName(left) { if (t7.isVariableDeclaration(left)) { const id = left.declarations[0]?.id; return t7.isIdentifier(id) ? id.name : "item"; } else if (t7.isIdentifier(left)) { return left.name; } else { return "item"; } } __name(extractForOfParamName, "extractForOfParamName"); // src/transformer/detector.ts var traverse = typeof _traverse.default === "function" ? _traverse.default : _traverse; var AsyncIterationDetector = class { static { __name(this, "AsyncIterationDetector"); } detect(code) { const patterns = []; let batchableParallel = false; try { const ast = parse(code, { sourceType: "module", plugins: [ "typescript" ], allowAwaitOutsideFunction: true, allowReturnOutsideFunction: true }); traverse(ast, { ForOfStatement: /* @__PURE__ */ __name((path) => { if (containsAwait(path.node.body)) { patterns.push("for-of-await"); } }, "ForOfStatement"), WhileStatement: /* @__PURE__ */ __name((path) => { if (containsAwait(path.node.body)) { patterns.push("while-await"); } }, "WhileStatement"), CallExpression: /* @__PURE__ */ __name((path) => { const node = path.node; if (isArrayMethod(node, "map")) { const callback = node.arguments[0]; if (callback && t7.isFunction(callback) && callback.async) { patterns.push("map-async"); } } if (isArrayMethod(node, "forEach")) { const callback = node.arguments[0]; if (callback && t7.isFunction(callback) && callback.async) { patterns.push("forEach-async"); } } if (isArrayMethod(node, "filter")) { const callback = node.arguments[0]; if (callback && t7.isFunction(callback) && callback.async) { patterns.push("filter-async"); } } if (isArrayMethod(node, "reduce")) { const callback = node.arguments[0]; if (callback && t7.isFunction(callback) && callback.async) { patterns.push("reduce-async"); } } if (isArrayMethod(node, "find")) { const callback = node.arguments[0]; if (callback && t7.isFunction(callback) && callback.async) { patterns.push("find-async"); } } if (isArrayMethod(node, "some")) { const callback = node.arguments[0]; if (callback && t7.isFunction(callback) && callback.async) { patterns.push("some-async"); } } if (isArrayMethod(node, "every")) { const callback = node.arguments[0]; if (callback && t7.isFunction(callback) && callback.async) { patterns.push("every-async"); } } if (isArrayMethod(node, "flatMap")) { const callback = node.arguments[0]; if (callback && t7.isFunction(callback) && callback.async) { patterns.push("flatMap-async"); } } if (this.isPromiseAll(node)) { patterns.push("promise-all"); if (this.canBatchPromiseAll(node)) { batchableParallel = true; } } if (this.isPromiseAllSettled(node)) { patterns.push("promise-allSettled"); } }, "CallExpression") }); return { needsTransform: patterns.length > 0, patterns: [ ...new Set(patterns) ], batchableParallel }; } catch (error) { return { needsTransform: false, patterns: [], batchableParallel: false }; } } isPromiseAll(node) { const callee = node.callee; return t7.isMemberExpression(callee) && t7.isIdentifier(callee.object, { name: "Promise" }) && t7.isIdentifier(callee.property, { name: "all" }); } isPromiseAllSettled(node) { const callee = node.callee; return t7.isMemberExpression(callee) && t7.isIdentifier(callee.object, { name: "Promise" }) && t7.isIdentifier(callee.property, { name: "allSettled" }); } canBatchPromiseAll(node) { const arrayArg = node.arguments[0]; if (!t7.isArrayExpression(arrayArg)) { return false; } if (arrayArg.elements.length === 0) { return false; } return arrayArg.elements.every((el) => { if (!el || t7.isSpreadElement(el)) { return false; } return this.isDirectPausableCall(el); }); } isDirectPausableCall(node) { if (t7.isAwaitExpression(node)) { node = node.argument; } if (!t7.isCallExpression(node)) { return false; } return isPausableCallExpression(node); } }; // src/runtime/context.ts var contextStack = []; function setRuntimeContext(context) { contextStack.push(context); } __name(setRuntimeContext, "setRuntimeContext"); function getRuntimeContext() { const context = contextStack[contextStack.length - 1]; if (!context) { throw new Error("No runtime context available. Compiler runtime not properly initialized."); } return context; } __name(getRuntimeContext, "getRuntimeContext"); function clearRuntimeContext() { contextStack.pop(); } __name(clearRuntimeContext, "clearRuntimeContext"); function hasRuntimeContext() { return contextStack.length > 0; } __name(hasRuntimeContext, "hasRuntimeContext"); var idCounter = 0; function generateUniqueId(prefix) { return `${prefix}_${Date.now()}_${idCounter++}`; } __name(generateUniqueId, "generateUniqueId"); function resetIdCounter() { idCounter = 0; } __name(resetIdCounter, "resetIdCounter"); var BatchOptimizer = class { static { __name(this, "BatchOptimizer"); } arrayMethodsWithEarlyExit = [ "find", "some", "every" ]; canBatchArrayMethod(callback) { if (!callback.async) { return { canBatch: false, reason: "Not async" }; } const body = callback.body; if (!t7.isBlockStatement(body)) { if (t7.isAwaitExpression(body)) { if (this.isDirectPausableCall(body.argument)) { return { canBatch: true, llmCallPattern: "single", hasConditionals: false }; } } return { canBatch: false, reason: "Non-block body without direct call" }; } const statements = body.body; if (statements.length === 0) { return { canBatch: false, reason: "Empty body" }; } let hasConditionals = false; let hasLoops = false; let hasTryCatch = false; for (const stmt of statements) { if (t7.isIfStatement(stmt) || t7.isSwitchStatement(stmt)) { hasConditionals = true; } if (t7.isTryStatement(stmt)) { hasTryCatch = true; } if (t7.isForStatement(stmt) || t7.isForOfStatement(stmt) || t7.isForInStatement(stmt) || t7.isWhileStatement(stmt) || t7.isDoWhileStatement(stmt)) { hasLoops = true; } if (t7.isBreakStatement(stmt) || t7.isContinueStatement(stmt)) { return { canBatch: false, reason: "Contains break/continue" }; } if (t7.isReturnStatement(stmt) && stmt !== statements[statements.length - 1]) { return { canBatch: false, reason: "Early return" }; } } if (hasLoops) { return { canBatch: false, reason: "Contains loops", hasLoops: true }; } if (hasTryCatch) { return { canBatch: false, reason: "Contains try-catch" }; } const pausableCalls = this.countPausableCalls(body); if (pausableCalls === 0) { return { canBatch: false, reason: "No pausable calls" }; } if (pausableCalls > 1) { return { canBatch: false, reason: "Multiple pausable calls", llmCallPattern: "multiple" }; } if (hasConditionals) { return { canBatch: true, llmCallPattern: "conditional", hasConditionals: true, reason: "Simple conditional - can batch but consider array size" }; } return { canBatch: true, llmCallPattern: "single", hasConditionals: false }; } /** * Smart decision: Should we batch based on array size and method type? */ makeSmartBatchDecision(methodName, batchResult, arrayNode, threshold = 10) { if (!batchResult.canBatch) { return { shouldBatch: false, reason: "Complex callback - use sequential", strategy: "never-batch" }; } if (!batchResult.hasConditionals) { return { shouldBatch: true, reason: "Simple callback - batching is faster", strategy: "always-batch" }; } const hasEarlyExitBenefit = this.arrayMethodsWithEarlyExit.includes(methodName); if (!hasEarlyExitBenefit) { const arraySize2 = this.estimateArraySize(arrayNode); if (arraySize2 !== null && arraySize2 < threshold) { return { shouldBatch: true, reason: `Small array (${arraySize2} < ${threshold}) - batch despite conditionals`, strategy: "size-dependent" }; } return { shouldBatch: false, reason: "Conditionals + large/unknown array - sequential for safety", strategy: "size-dependent" }; } const arraySize = this.estimateArraySize(arrayNode); if (arraySize !== null && arraySize < threshold) { return { shouldBatch: true, reason: `Small array (${arraySize} < ${threshold}) - batch for speed`, strategy: "size-dependent" }; } if (arraySize !== null && arraySize >= threshold) { return { shouldBatch: false, reason: `Large array (${arraySize} >= ${threshold}) + conditionals - sequential for early-exit savings`, strategy: "size-dependent" }; } if (t7.isArrayExpression(arrayNode)) { return { shouldBatch: true, reason: "Array literal (likely small) - batch", strategy: "size-dependent" }; } return { shouldBatch: false, reason: "Unknown array size + conditionals - sequential for safety", strategy: "size-dependent" }; } estimateArraySize(arrayNode) { if (t7.isArrayExpression(arrayNode)) { return arrayNode.elements.length; } return null; } canBatchForOfLoop(loopNode) { const body = loopNode.body; if (!t7.isBlockStatement(body)) { return { canBatch: false, reason: "Loop body not a block" }; } const statements = body.body; if (statements.length === 0) { return { canBatch: false, reason: "Empty loop body" }; } const hasBreakOrContinue = this.containsBreakOrContinue(body); if (hasBreakOrContinue) { return { canBatch: false, reason: "Contains break/continue" }; } let hasConditionals = false; for (const stmt of statements) { if (t7.isIfStatement(stmt) || t7.isSwitchStatement(stmt)) { hasConditionals = true; } if (t7.isForStatement(stmt) || t7.isForOfStatement(stmt) || t7.isForInStatement(stmt) || t7.isWhileStatement(stmt) || t7.isDoWhileStatement(stmt)) { return { canBatch: false, reason: "Contains nested loops", hasLoops: true }; } } const pausableCalls = this.countPausableCalls(body); if (pausableCalls === 0) { return { canBatch: false, reason: "No pausable calls" }; } if (pausableCalls > 1) { return { canBatch: false, reason: "Multiple pausable calls", llmCallPattern: "multiple" }; } if (hasConditionals) { return { canBatch: true, llmCallPattern: "conditional", hasConditionals: true, reason: "Simple conditional - can batch but consider array size" }; } return { canBatch: true, llmCallPattern: "single", hasConditionals: false }; } containsBreakOrContinue(node) { let found = false; const visit = /* @__PURE__ */ __name((n) => { if (found) return; if (t7.isBreakStatement(n) || t7.isContinueStatement(n)) { found = true; return; } if (t7.isForStatement(n) || t7.isForOfStatement(n) || t7.isForInStatement(n) || t7.isWhileStatement(n) || t7.isDoWhileStatement(n)) { return; } Object.keys(n).forEach((key) => { const value = n[key]; if (Array.isArray(value)) { value.forEach((item) => { if (item && typeof item === "object" && item.type) { visit(item); } }); } else if (value && typeof value === "object" && value.type) { visit(value); } }); }, "visit"); visit(node); return found; } isDirectPausableCall(node) { if (!t7.isCallExpression(node)) { return false; } return isPausableCallExpression(node); } countPausableCalls(body) { let count = 0; const visit = /* @__PURE__ */ __name((node) => { if (t7.isAwaitExpression(node) && this.isDirectPausableCall(node.argument)) { count++; return; } Object.keys(node).forEach((key) => { const value = node[key]; if (Array.isArray(value)) { value.forEach((item) => { if (item && typeof item === "object" && item.type) { visit(item); } }); } else if (value && typeof value === "object" && value.type) { visit(value); } }); }, "visit"); visit(body); return count; } }; var BatchParallelDetector = class { static { __name(this, "BatchParallelDetector"); } canBatch(promiseAllNode) { const arrayArg = promiseAllNode.arguments[0]; if (!t7.isArrayExpression(arrayArg)) { return false; } if (arrayArg.elements.length === 0) { return false; } return arrayArg.elements.every((el) => { if (!el || t7.isSpreadElement(el)) { return false; } return this.isDirectPausableCall(el); }); } isDirectPausableCall(node) { if (t7.isAwaitExpression(node)) { node = node.argument; } if (!t7.isCallExpression(node)) { return false; } return isPausableCallExpression(node); } extractBatchCalls(arrayNode) { const calls = []; for (const el of arrayNode.elements) { if (!el || t7.isSpreadElement(el)) { continue; } let callNode = el; if (t7.isAwaitExpression(callNode)) { callNode = callNode.argument; } if (!t7.isCallExpression(callNode)) { continue; } const callInfo = this.extractCallInfo(callNode); if (callInfo) { calls.push(callInfo); } } return calls; } extractCallInfo(callNode) { if (!t7.isMemberExpression(callNode.callee)) { return null; } const path = getMemberExpressionPath(callNode.callee); const parts = path.split("."); if (parts.length < 3) { return null; } const [namespace, service, method] = parts; if (namespace !== "atp" || !method) { return null; } const type = service; const payload = this.extractPayload(callNode.arguments); return { type, operation: method, payload }; } /** * Extract payload AST node directly */ extractPayloadNode(callNode) { if (callNode.arguments.length === 0) { return t7.objectExpression([]); } const firstArg = callNode.arguments[0]; if (!firstArg || t7.isSpreadElement(firstArg) || !t7.isExpression(firstArg)) { return null; } return firstArg; } extractPayload(args) { if (args.length === 0) { return {}; } const firstArg = args[0]; if (t7.isSpreadElement(firstArg)) { return {}; } if (t7.isObjectExpression(firstArg)) { return this.objectExpressionToRecord(firstArg); } if (t7.isStringLiteral(firstArg)) { return { message: firstArg.value }; } return {}; } objectExpressionToRecord(obj) { const record = {}; for (const prop of obj.properties) { if (t7.isObjectProperty(prop) && !prop.computed) { const key = t7.isIdentifier(prop.key) ? prop.key.name : String(prop.key); const value = this.extractValue(prop.value); record[key] = value; } } return record; } extractValue(node) { if (t7.isStringLiteral(node)) { return node.value; } if (t7.isNumericLiteral(node)) { return node.value; } if (t7.isBooleanLiteral(node)) { return node.value; } if (t7.isNullLiteral(node)) { return null; } if (t7.isArrayExpression(node)) { return node.elements.map((el) => el && !t7.isSpreadElement(el) ? this.extractValue(el) : null); } if (t7.isObjectExpression(node)) { return this.objectExpressionToRecord(node); } return void 0; } }; function findLLMCallExpression(body) { let found = null; const visit = /* @__PURE__ */ __name((node) => { if (found) return; if (t7.isAwaitExpression(node) && t7.isCallExpression(node.argument)) { const call = node.argument; if (t7.isMemberExpression(call.callee)) { found = call; return; } } Object.keys(node).forEach((key) => { const value = node[key]; if (Array.isArray(value)) { value.forEach((item) => { if (item && typeof item === "object" && item.type) { visit(item); } }); } else if (value && typeof value === "object" && value.type) { visit(value); } }); }, "visit"); visit(body); return found; } __name(findLLMCallExpression, "findLLMCallExpression"); function getArrayMethodName(node) { const arrayMethods = [ "map", "forEach", "filter", "reduce", "find", "some", "every", "flatMap" ]; for (const method of arrayMethods) { if (isArrayMethod(node, method)) { return method; } } return null; } __name(getArrayMethodName, "getArrayMethodName"); function getRuntimeMethodName(arrayMethod) { const mapping = { map: "resumableMap", forEach: "resumableForEach", filter: "resumableFilter", reduce: "resumableReduce", find: "resumableFind", some: "resumableSome", every: "resumableEvery", flatMap: "resumableFlatMap" }; return mapping[arrayMethod] || null; } __name(getRuntimeMethodName, "getRuntimeMethodName"); function canUseBatchParallel(methodName) { return [ "map", "forEach", "filter", "find", "some", "every" ].includes(methodName); } __name(canUseBatchParallel, "canUseBatchParallel"); // src/transformer/loop-transformer.ts var LoopTransformer = class { static { __name(this, "LoopTransformer"); } transformCount = 0; batchOptimizer; batchDetector; batchSizeThreshold; constructor(batchSizeThreshold = 10) { this.batchOptimizer = new BatchOptimizer(); this.batchDetector = new BatchParallelDetector(); this.batchSizeThreshold = batchSizeThreshold; } transformForOfLoop(path) { const node = path.node; if (!containsAwait(node.body)) { return false; } const batchResult = this.batchOptimizer.canBatchForOfLoop(node); if (batchResult.canBatch) { const decision = this.batchOptimizer.makeSmartBatchDecision("for...of", batchResult, node.right, this.batchSizeThreshold); if (decision.shouldBatch) { return this.transformForOfToBatch(path, node); } } return this.transformForOfToSequential(path, node); } /** * Transform simple for...of to batch parallel */ transformForOfToBatch(path, node) { const loopId = generateUniqueId("for_of_batch"); const right = node.right; const paramName = extractForOfParamName(node.left); const llmCall = findLLMCallExpression(node.body); if (!llmCall) { return this.transformForOfToSequential(path, node); } const callInfo = this.batchDetector.extractCallInfo(llmCall); if (!callInfo) { return this.transformForOfToSequential(path, node); } const payloadNode = this.batchDetector.extractPayloadNode(llmCall); if (!payloadNode) { return this.transformForOfToSequential(path, node); } const batchCallsArray = t7.callExpression(t7.memberExpression(right, t7.identifier("map")), [ t7.arrowFunctionExpression([ t7.identifier(paramName) ], t7.objectExpression([ t7.objectProperty(t7.identifier("type"), t7.stringLiteral(callInfo.type)), t7.objectProperty(t7.identifier("operation"), t7.stringLiteral(callInfo.operation)), t7.objectProperty(t7.identifier("payload"), payloadNode) ])) ]); const batchCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier("batchParallel")), [ batchCallsArray, t7.stringLiteral(loopId) ])); path.replaceWith(t7.expressionStatement(batchCall)); this.transformCount++; return true; } /** * Transform for...of to sequential with checkpoints (fallback) */ transformForOfToSequential(path, node) { const loopId = generateUniqueId("for_of"); const right = node.right; const paramName = extractForOfParamName(node.left); const bodyStatements = t7.isBlockStatement(node.body) ? node.body.body : [ node.body ]; const callbackFn = t7.arrowFunctionExpression([ t7.identifier(paramName), t7.identifier("__index") ], t7.blockStatement(bodyStatements), true); const runtimeCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier("resumableForOf")), [ right, callbackFn, t7.stringLiteral(loopId) ])); path.replaceWith(t7.expressionStatement(runtimeCall)); this.transformCount++; return true; } transformWhileLoop(path) { const node = path.node; if (!containsAwait(node.body)) { return false; } const loopId = generateUniqueId("while"); const conditionFn = t7.arrowFunctionExpression([], node.test, false); const bodyStatements = t7.isBlockStatement(node.body) ? node.body.body : [ node.body ]; const bodyFn = t7.arrowFunctionExpression([ t7.identifier("__iteration") ], t7.blockStatement(bodyStatements), true); const runtimeCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier("resumableWhile")), [ conditionFn, bodyFn, t7.stringLiteral(loopId) ])); path.replaceWith(t7.expressionStatement(runtimeCall)); this.transformCount++; return true; } transformForLoop(path) { const node = path.node; if (!containsAwait(node.body)) { return false; } if (!node.init || !node.test || !node.update) { return false; } const loopId = generateUniqueId("for"); let initValue = t7.numericLiteral(0); let loopVar = "__i"; if (t7.isVariableDeclaration(node.init)) { const decl = node.init.declarations[0]; if (decl && t7.isIdentifier(decl.id) && decl.init) { loopVar = decl.id.name; initValue = decl.init; } } const conditionFn = t7.arrowFunctionExpression([ t7.identifier(loopVar) ], node.test, false); const bodyStatements = t7.isBlockStatement(node.body) ? node.body.body : [ node.body ]; const bodyFn = t7.arrowFunctionExpression([ t7.identifier(loopVar) ], t7.blockStatement(bodyStatements), true); let incrementFn; if (t7.isUpdateExpression(node.update)) { if (node.update.operator === "++") { incrementFn = t7.arrowFunctionExpression([ t7.identifier(loopVar) ], t7.binaryExpression("+", t7.identifier(loopVar), t7.numericLiteral(1)), false); } else if (node.update.operator === "--") { incrementFn = t7.arrowFunctionExpression([ t7.identifier(loopVar) ], t7.binaryExpression("-", t7.identifier(loopVar), t7.numericLiteral(1)), false); } else { return false; } } else { return false; } const runtimeCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier("resumableForLoop")), [ initValue, conditionFn, incrementFn, bodyFn, t7.stringLiteral(loopId) ])); path.replaceWith(t7.expressionStatement(runtimeCall)); this.transformCount++; return true; } getTransformCount() { return this.transformCount; } resetTransformCount() { this.transformCount = 0; } }; function wrapFilterResult(batchCall, array, methodId) { const resultsVar = `__filter_results_${methodId}`; const indexVar = `__i_${methodId}`; const arrayClone = t7.cloneNode(array, true); return t7.awaitExpression(t7.callExpression(t7.arrowFunctionExpression([], t7.blockStatement([ t7.variableDeclaration("const", [ t7.variableDeclarator(t7.identifier(resultsVar), batchCall.argument) ]), t7.returnStatement(t7.callExpression(t7.memberExpression(arrayClone, t7.identifier("filter")), [ t7.arrowFunctionExpression([ t7.identifier("_"), t7.identifier(indexVar) ], t7.callExpression(t7.identifier("Boolean"), [ t7.memberExpression(t7.identifier(resultsVar), t7.identifier(indexVar), true) ])) ])) ]), true), [])); } __name(wrapFilterResult, "wrapFilterResult"); function wrapFindResult(batchCall, array, methodId) { const resultsVar = `__find_results_${methodId}`; const arrayClone = t7.cloneNode(array, true); return t7.awaitExpression(t7.callExpression(t7.arrowFunctionExpression([], t7.blockStatement([ t7.variableDeclaration("const", [ t7.variableDeclarator(t7.identifier(resultsVar), batchCall.argument) ]), t7.returnStatement(t7.callExpression(t7.memberExpression(arrayClone, t7.identifier("find")), [ t7.arrowFunctionExpression([ t7.identifier("_"), t7.identifier("__i") ], t7.callExpression(t7.identifier("Boolean"), [ t7.memberExpression(t7.identifier(resultsVar), t7.identifier("__i"), true) ])) ])) ]), true), [])); } __name(wrapFindResult, "wrapFindResult"); function wrapSomeResult(batchCall, methodId) { const resultsVar = `__some_results_${methodId}`; return t7.awaitExpression(t7.callExpression(t7.arrowFunctionExpression([], t7.blockStatement([ t7.variableDeclaration("const", [ t7.variableDeclarator(t7.identifier(resultsVar), batchCall.argument) ]), t7.returnStatement(t7.callExpression(t7.memberExpression(t7.identifier(resultsVar), t7.identifier("some")), [ t7.arrowFunctionExpression([ t7.identifier("r") ], t7.callExpression(t7.identifier("Boolean"), [ t7.identifier("r") ])) ])) ]), true), [])); } __name(wrapSomeResult, "wrapSomeResult"); function wrapEveryResult(batchCall, methodId) { const resultsVar = `__every_results_${methodId}`; return t7.awaitExpression(t7.callExpression(t7.arrowFunctionExpression([], t7.blockStatement([ t7.variableDeclaration("const", [ t7.variableDeclarator(t7.identifier(resultsVar), batchCall.argument) ]), t7.returnStatement(t7.callExpression(t7.memberExpression(t7.identifier(resultsVar), t7.identifier("every")), [ t7.arrowFunctionExpression([ t7.identifier("r") ], t7.callExpression(t7.identifier("Boolean"), [ t7.identifier("r") ])) ])) ]), true), [])); } __name(wrapEveryResult, "wrapEveryResult"); function wrapBatchResultIfNeeded(batchCall, methodName, array, methodId) { switch (methodName) { case "filter": return wrapFilterResult(batchCall, array, methodId); case "find": return wrapFindResult(batchCall, array, methodId); case "some": return wrapSomeResult(batchCall, methodId); case "every": return wrapEveryResult(batchCall, methodId); case "forEach": return batchCall; default: return batchCall; } } __name(wrapBatchResultIfNeeded, "wrapBatchResultIfNeeded"); // src/transformer/array-transformer-batch.ts function extractBatchCallInfo(callback, batchDetector) { const paramName = callback.params[0]; if (!t7.isIdentifier(paramName)) { return null; } const param = paramName.name; const llmCall = findLLMCallExpression(callback.body); if (!llmCall) { return null; } const callInfo = batchDetector.extractCallInfo(llmCall); if (!callInfo) { return null; } const payloadNode = batchDetector.extractPayloadNode(llmCall); if (!payloadNode) { return null; } const mapperFunction = t7.arrowFunctionExpression([ t7.identifier(param) ], t7.objectExpression([ t7.objectProperty(t7.identifier("type"), t7.stringLiteral(callInfo.type)), t7.objectProperty(t7.identifier("operation"), t7.stringLiteral(callInfo.operation)), t7.objectProperty(t7.identifier("payload"), payloadNode) ])); return { mapperFunction }; } __name(extractBatchCallInfo, "extractBatchCallInfo"); function transformToBatchParallel(path, node, methodName, callback, batchDetector, onTransform, fallbackTransform) { const methodId = generateUniqueId(`${methodName}_batch`); const array = node.callee.object; const callInfo = extractBatchCallInfo(callback, batchDetector); if (!callInfo) { return fallbackTransform(); } const batchCallsArray = t7.callExpression(t7.memberExpression(array, t7.identifier("map")), [ callInfo.mapperFunction ]); const batchCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier("batchParallel")), [ batchCallsArray, t7.stringLiteral(methodId) ])); const finalCall = wrapBatchResultIfNeeded(batchCall, methodName, array, methodId); path.replaceWith(finalCall); onTransform(); return true; } __name(transformToBatchParallel, "transformToBatchParallel"); function transformToSequential(path, node, methodName, callback, onTransform) { const runtimeMethod = getRuntimeMethodName(methodName); if (!runtimeMethod) { return false; } const methodId = generateUniqueId(methodName); const array = node.callee.object; const args = [ array, callback, t7.stringLiteral(methodId) ]; if (methodName === "reduce" && node.arguments[1]) { args.push(node.arguments[1]); } const runtimeCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier(runtimeMethod)), args)); path.replaceWith(runtimeCall); onTransform(); return true; } __name(transformToSequential, "transformToSequential"); // src/transformer/array-transformer.ts var ArrayTransformer = class { static { __name(this, "ArrayTransformer"); } transformCount = 0; batchOptimizer; batchDetector; batchSizeThreshold; constructor(batchSizeThreshold = 10) { this.batchOptimizer = new BatchOptimizer(); this.batchDetector = new BatchParallelDetector(); this.batchSizeThreshold = batchSizeThreshold; } transformArrayMethod(path) { const node = path.node; const methodName = getArrayMethodName(node); if (!methodName) { return false; } const callback = node.arguments[0]; if (!callback || !t7.isFunction(callback) || !callback.async) { return false; } const batchResult = this.batchOptimizer.canBatchArrayMethod(callback); if (batchResult.canBatch && canUseBatchParallel(methodName)) { const array = node.callee.object; const decision = this.batchOptimizer.makeSmartBatchDecision(methodName, batchResult, array, this.batchSizeThreshold); if (decision.shouldBatch) { return transformToBatchParallel(path, node, methodName, callback, this.batchDetector, () => this.transformCount++, () => this.doTransformToSequential(path, node, methodName, callback)); } } return this.doTransformToSequential(path, node, methodName, callback); } doTransformToSequential(path, node, methodName, callback) { return transformToSequential(path, node, methodName, callback, () => this.transformCount++); } getTransformCount() { return this.transformCount; } resetTransformCount() { this.transformCount = 0; } }; // src/runtime/runtime-functions.ts var RuntimeFunction = { // Promise operations RESUMABLE_PROMISE_ALL: "resumablePromiseAll", RESUMABLE_PROMISE_ALL_SETTLED: "resumablePromiseAllSettled", BATCH_PARALLEL: "batchParallel", // Loop operations RESUMABLE_FOR_OF: "resumableForOf", RESUMABLE_FOR_LOOP: "resumableForLoop", RESUMABLE_WHILE: "resumableWhile", // Array method operations RESUMABLE_MAP: "resumableMap", RESUMABLE_FOR_EACH: "resumableForEach", RESUMABLE_FILTER: "resumableFilter", RESUMABLE_REDUCE: "resumableReduce", RESUMABLE_FIND: "resumableFind", RESUMABLE_SOME: "resumableSome", RESUMABLE_EVERY: "resumableEvery", RESUMABLE_FLAT_MAP: "resumableFlatMap" }; var IN_ISOLATE_RUNTIME_FUNCTIONS = [ RuntimeFunction.RESUMABLE_PROMISE_ALL, RuntimeFunction.RESUMABLE_PROMISE_ALL_SETTLED, RuntimeFunction.RESUMABLE_FOR_OF, RuntimeFunction.RESUMABLE_FOR_LOOP, RuntimeFunction.RESUMABLE_WHILE, RuntimeFunction.RESUMABLE_MAP, RuntimeFunction.RESUMABLE_FOR_EACH, RuntimeFunction.RESUMABLE_FILTER, RuntimeFunction.RESUMABLE_REDUCE, RuntimeFunction.RESUMABLE_FIND, RuntimeFunction.RESUMABLE_SOME, RuntimeFunction.RESUMABLE_EVERY, RuntimeFunction.RESUMABLE_FLAT_MAP ]; function isInIsolateRuntimeFunction(name) { return IN_ISOLATE_RUNTIME_FUNCTIONS.includes(name); } __name(isInIsolateRuntimeFunction, "isInIsolateRuntimeFunction"); // src/transformer/promise-transformer.ts var PromiseTransformer = class { static { __name(this, "PromiseTransformer"); } transformCount = 0; batchDetector; enableBatchParallel; constructor(enableBatchParallel = true) { this.batchDetector = new BatchParallelDetector(); this.enableBatchParallel = enableBatchParallel; } transformPromiseAll(path) { const node = path.node; if (!this.isPromiseAll(node)) { return false; } const arrayArg = node.arguments[0]; if (this.enableBatchParallel && this.batchDetector.canBatch(node)) { return this.transformToBatchParallel(path, node); } if (t7.isArrayExpression(arrayArg)) { return this.transformToSequential(path, node); } return false; } transformPromiseAllSettled(path) { const node = path.node; if (!this.isPromiseAllSettled(node)) { return false; } const arrayArg = node.arguments[0]; if (t7.isArrayExpression(arrayArg)) { const parallelId = generateUniqueId("allSettled"); const runtimeCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier(RuntimeFunction.RESUMABLE_PROMISE_ALL_SETTLED)), [ arrayArg, t7.stringLiteral(parallelId) ])); path.replaceWith(runtimeCall); this.transformCount++; return true; } return false; } transformToBatchParallel(path, node) { const arrayArg = node.arguments[0]; if (!t7.isArrayExpression(arrayArg)) { return false; } const batchId = generateUniqueId("batch"); const batchCallsArray = t7.arrayExpression(arrayArg.elements.map((el) => { if (!el || t7.isSpreadElement(el)) { return t7.nullLiteral(); } let callNode = el; if (t7.isAwaitExpression(callNode)) { callNode = callNode.argument; } if (!t7.isCallExpression(callNode) || !t7.isMemberExpression(callNode.callee)) { return t7.nullLiteral(); } const callInfo = this.batchDetector.extractCallInfo(callNode); if (!callInfo) { return t7.nullLiteral(); } const payloadArg = callNode.arguments[0]; return t7.objectExpression([ t7.objectProperty(t7.identifier("type"), t7.stringLiteral(callInfo.type)), t7.objectProperty(t7.identifier("operation"), t7.stringLiteral(callInfo.operation)), t7.objectProperty(t7.identifier("payload"), payloadArg && t7.isExpression(payloadArg) ? payloadArg : t7.objectExpression([])) ]); })); const runtimeCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier(RuntimeFunction.BATCH_PARALLEL)), [ batchCallsArray, t7.stringLiteral(batchId) ])); path.replaceWith(runtimeCall); this.transformCount++; return true; } transformToSequential(path, node) { const arrayArg = node.arguments[0]; if (!t7.isArrayExpression(arrayArg)) { return false; } const parallelId = generateUniqueId("parallel"); const runtimeCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier(RuntimeFunction.RESUMABLE_PROMISE_ALL)), [ arrayArg, t7.stringLiteral(parallelId) ])); path.replaceWith(runtimeCall); this.transformCount++; return true; } payloadToExpression(payload) { const properties = []; for (const [key, value] of Object.entries(payload)) { properties.push(t7.objectProperty(t7.identifier(key), this.valueToExpression(value))); } return t7.objectExpression(properties); } valueToExpression(value) { if (typeof value === "string") { return t7.stringLiteral(value); } if (typeof value === "number") { return t7.numericLiteral(value); } if (typeof value === "boolean") { return t7.booleanLiteral(value); } if (value === null) { return t7.nullLiteral(); } if (Array.isArray(value)) { return t7.arrayExpression(value.map((v) => this.valueToExpression(v))); } if (typeof value === "object") { return this.payloadToExpression(value); } return t7.identifier("undefined"); } isPromiseAll(node) { const callee = node.callee; return t7.isMemberExpression(callee) && t7.isIdentifier(callee.object, { name: "Promise" }) && t7.isIdentifier(callee.property, { name: "all" }); } isPromiseAllSettled(node) { const callee = node.callee; return t7.isMemberExpression(callee) && t7.isIdentifier(callee.object, { name: "Promise" }) && t7.isIdentifier(callee.property, { name: "allSettled" }); } getTransformCount() { return this.transformCount; } resetTransformCount() { this.transformCount = 0; } }; // src/runtime/errors.ts var CheckpointOperation; (function(CheckpointOperation2) { CheckpointOperation2["SAVE"] = "save"; CheckpointOperation2["LOAD"] = "load"; CheckpointOperation2["CLEAR"] = "clear"; })(CheckpointOperation || (CheckpointOperation = {})); var BatchPauseExecutionError = class extends Error { static { __name(this, "BatchPauseExecutionError"); } calls; batchId; sequenceNumber; constructor(calls, batchId, sequenceNumber) { super(`Batch pause for parallel execution (${calls.length} callbacks)`); this.name = "BatchPauseExecutionError"; this.calls = calls; this.batchId = batchId; this.sequenceNumber = sequenceNumber; } }; var CheckpointError = class extends Error { static { __name(this, "CheckpointError"); } checkpointId; operation; constructor(message, checkpointId, operation) { super(`Checkpoint ${operation} failed for ${checkpointId}: ${message}`); this.name = "CheckpointError"; this.checkpointId = checkpointId; this.operation = operation; } }; var TransformationError = class extends Error { static { __name(this, "TransformationError"); } code; pattern; location; constructor(message, code, pattern, location) { const loc = location ? ` at line ${location.line}:${location.column}` : ""; super(`Transformation failed for ${pattern}${loc}: ${message}`); this.name = "TransformationError"; this.code = code; this.pattern = pattern; this.location = location; } }; var InfiniteLoopDetectionError = class extends Error { static { __name(this, "InfiniteLoopDetectionError"); } loopId; iterationCount; constructor(loopId, iterationCount) { super(`Infinite loop detected: ${loopId} exceeded ${iterationCount} iterations without completing`); this.name = "InfiniteLoopDetectionError"; this.loopId = loopId; this.iterationCount = iterationCount; } }; function isBatchPauseError(error) { return error instanceof BatchPauseExecutionError; } __name(isBatchPauseError, "isBatchPauseError"); function isCheckpointError(error) { return error instanceof CheckpointError; } __name(isCheckpointError, "isCheckpointError"); function isTransformationError(error) { return error instanceof TransformationError; } __name(isTransformationError, "isTransformationError"); // src/transformer/index.ts var traverse2 = _traverse.default || _traverse; var generate = _generate.default || _generate; var ATPCompiler = class { static { __name(this, "ATPCompiler"); } config; detector; loopTransformer; arrayTransformer; promiseTransformer; constructor(config = {}) { this.config = { ...DEFAULT_COMPILER_CONFIG, ...config }; this.detector = new AsyncIterationDetector(); this.loopTransformer = new LoopTransformer(this.config.batchSizeThreshold); this.arrayTransformer = new ArrayTransformer(this.config.batchSizeThreshold); this.promiseTransformer = new PromiseTransformer(this.config.enableBatchParallel); } detect(code) { return this.detector.detect(code); } transform(code) { resetIdCounter(); const detection = this.detector.detect(code); if (!detection.needsTransform) { return { code, transformed: false, patterns: [], metadata: { loopCount: 0, arrayMethodCount: 0, parallelCallCount: 0, batchableCount: 0 } }; } try { const ast = parse(code, { sourceType: "module", plugins: [ "typescript" ], allowAwaitOutsideFunction: true, allowReturnOutsideFunction: true }); this.loopTransformer.resetTransformCount(); this.arrayTransformer.resetTransformCount(); this.promiseTransformer.resetTransformCount(); traverse2(ast, { ForOfStatement: /* @__PURE__ */ __name((path) => { this.loopTransformer.transformForOfLoop(path); }, "ForOfStatement"), WhileStatement: /* @__PURE__ */ __name((path) => { this.loopTransformer.transformWhileLoop(path); }, "WhileStatement"), ForStatement: /* @__PURE__ */ __name((path) => { this.loopTransformer.transformForLoop(path); }, "ForStatement"), CallExpression: /* @__PURE__ */ __name((path) => { if (this.isArrayMethodCall(path.node)) { this.arrayTransformer.transformArrayMethod(path); } else if (this.isPromiseAllCall(path.node)) { this.promiseTransformer.transformPromiseAll(path); } else if (this.isPromiseAllSettledCall(path.node)) { this.promiseTransformer.transformPromiseAllSettled(path); } }, "CallExpression") }); const output = generate(ast, { sourceMaps: false, retainLines: true, comments: true }); const metadata = { loopCount: this.loopTransformer.getTransformCount(), arrayMethodCount: this.arrayTransformer.getTransformCount(), parallelCallCount: this.promiseTransformer.getTransformCount(), batchableCount: detection.batchableParallel ? 1 : 0 }; return { code: output.code, transformed: true, patterns: detection.patterns, metadata }; } catch (error) { const message = error instanceof Error ? error.message : String(error); throw new TransformationError(message, code, "unknown"); } } isArrayMethodCall(node) { if (!t7.isMemberExpression(node.callee)) { return false; } const property = node.callee.property; if (!t7.isIdentifier(property)) { return false; } const arrayMethods = [ "map", "forEach", "filter", "reduce", "find", "some", "every", "flatMap" ]; return arrayMethods.includes(property.name); } isPromiseAllCall(node) { const callee = node.callee; return t7.isMemberExpression(callee) && t7.isIdentifier(callee.object, { name: "Promise" }) && t7.isIdentifier(callee.property, { name: "all" }); } isPromis