mlld
Version:
mlld: llm scripting language
2,687 lines • 114 kB
JavaScript
import { normalizeTransformerResult, handleExecGuardDenial, createParameterVariable } from './chunk-JRTTHLAJ.mjs';
import { CommandUtils, prepareValueForShadow } from './chunk-ONLT7ZR3.mjs';
import { wrapExecResult, wrapPipelineResult, coerceValueForStdin, runWithGuardRetry, resolveStreamFormatValue, loadStreamAdapter, getAdapter, interpolate, materializeGuardInputsWithMapping, getGuardTransformedInputs, handleGuardDecision, resolveWorkingDirectory, evaluateExeBlock, extractSection } from './chunk-IA26UJYI.mjs';
import { AutoUnwrapManager } from './chunk-QTYWAF2L.mjs';
import { InterpolationContext } from './chunk-PHUTH3LV.mjs';
import { logger } from './chunk-M3R2H5KU.mjs';
import { MlldInterpreterError, CircularReferenceError, MlldCommandExecutionError } from './chunk-V5XE5YB5.mjs';
import { astLocationToSourceLocation, isTemplateExecutable, isDataExecutable, isPipelineExecutable, isCommandExecutable, isCodeExecutable, isCommandRefExecutable, isSectionExecutable, isResolverExecutable } from './chunk-6PZVQRSR.mjs';
import { asText, isStructuredValue, extractSecurityDescriptor, VariableMetadataUtils, asData, varMxToSecurityDescriptor, inheritExpressionProvenance, isExecutableVariable, collectAndMergeParameterDescriptors, parseAndWrapJson, wrapStructured, normalizeWhenShowEffect, applySecurityDescriptorToStructuredValue } from './chunk-RKGZ44GZ.mjs';
import { __name, __publicField } from './chunk-NJQT543K.mjs';
import * as fs from 'fs';
// interpreter/eval/with-clause.ts
async function applyWithClause(input, withClause, env) {
let result = wrapExecResult(input);
if (withClause.pipeline && withClause.pipeline.length > 0) {
const { processPipeline } = await import('./unified-processor-GTJC5G2K.mjs');
const pipelineResult = await processPipeline({
value: result,
env,
pipeline: withClause.pipeline,
format: withClause.format,
isRetryable: false,
stream: withClause.stream === true
});
result = wrapPipelineResult(pipelineResult);
}
return {
value: result,
env,
stdout: asText(result),
stderr: "",
exitCode: 0
};
}
__name(applyWithClause, "applyWithClause");
// core/types/structured-value/StructuredValue.ts
var JSON_PRIMITIVES = /* @__PURE__ */ new Set([
"true",
"false",
"null"
]);
var _StructuredValue = class _StructuredValue {
constructor(raw, options = {}) {
__publicField(this, "raw");
__publicField(this, "format");
__publicField(this, "origin");
__publicField(this, "jsonKind");
__publicField(this, "recognizedReason");
__publicField(this, "jsonEvaluated", false);
__publicField(this, "jsonValue");
__publicField(this, "jsonError");
this.raw = raw;
const detection = detectStructuredValue(raw, options);
this.format = detection.format;
this.jsonKind = detection.jsonKind;
this.recognizedReason = detection.reason;
this.origin = options.origin;
if (process.env.MLLD_DEBUG === "true" && detection.shouldWrap) {
const originSummary = options.origin ? `${options.origin.source}${options.origin.identifier ? `:${options.origin.identifier}` : ""}` : "unknown";
const preview = raw.length > 160 ? `${raw.slice(0, 160)}\u2026` : raw;
console.debug("[StructuredValue] recognized candidate", {
origin: originSummary,
format: detection.format,
reason: detection.reason,
kind: detection.jsonKind,
preview
});
}
}
static create(raw, options = {}) {
return new _StructuredValue(raw, options);
}
static detect(raw, options = {}) {
const detection = detectStructuredValue(raw, options);
return detection.shouldWrap ? new _StructuredValue(raw, options) : void 0;
}
static isStructuredValue(value) {
return value instanceof _StructuredValue;
}
toString() {
return this.raw;
}
valueOf() {
return this.raw;
}
[Symbol.toPrimitive]() {
return this.raw;
}
toJSON() {
return this.raw;
}
get text() {
return this.raw;
}
get data() {
if (this.format === "json") {
return this.asJson();
}
return this.raw;
}
hasJsonCandidate() {
return this.format === "json" || Boolean(this.jsonKind);
}
asJson() {
const result = this.tryParseJson();
if (!result.ok) {
throw result.error;
}
return result.value;
}
tryJson() {
return this.tryParseJson();
}
isJsonArray() {
const result = this.tryParseJson();
return result.ok && Array.isArray(result.value);
}
isJsonObject() {
const result = this.tryParseJson();
return result.ok && typeof result.value === "object" && result.value !== null && !Array.isArray(result.value);
}
asArray() {
const value = this.asJson();
if (!Array.isArray(value)) {
throw new Error("StructuredValue contains JSON that is not an array");
}
return value;
}
entries() {
const value = this.asJson();
if (!value || typeof value !== "object" || Array.isArray(value)) {
throw new Error("StructuredValue contains JSON that is not an object");
}
return Object.entries(value);
}
getJsonParseError() {
return this.jsonError;
}
tryParseJson() {
if (!this.hasJsonCandidate()) {
return {
ok: false,
error: new Error("StructuredValue is not recognized as JSON")
};
}
if (!this.jsonEvaluated) {
this.jsonEvaluated = true;
try {
const trimmed = this.raw.trim();
this.jsonValue = JSON.parse(trimmed);
this.jsonError = void 0;
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
this.jsonError = err;
if (process.env.MLLD_DEBUG === "true") {
const originSummary = this.origin ? `${this.origin.source}${this.origin.identifier ? `:${this.origin.identifier}` : ""}` : "unknown";
console.debug("[StructuredValue] JSON parse failed", {
origin: originSummary,
message: err.message
});
}
return {
ok: false,
error: err
};
}
}
if (this.jsonError) {
return {
ok: false,
error: this.jsonError
};
}
return {
ok: true,
value: this.jsonValue
};
}
};
__name(_StructuredValue, "StructuredValue");
var StructuredValue = _StructuredValue;
function detectStructuredValue(raw, options = {}) {
const formatHint = normalizeFormatHint(options.formatHint);
const trimmed = raw.trim();
const allowPrimaries = options.allowPrimaries ?? formatHint === "json";
if (!trimmed) {
return {
shouldWrap: false,
format: formatHint ?? "text"
};
}
if (formatHint === "json") {
return {
shouldWrap: true,
format: "json",
jsonKind: classifyStructure(trimmed) ?? classifyPrimitive(trimmed, allowPrimaries),
reason: "hint"
};
}
const structuralKind = classifyStructure(trimmed);
if (structuralKind) {
return {
shouldWrap: true,
format: "json",
jsonKind: structuralKind,
reason: "structure"
};
}
const primitiveKind = classifyPrimitive(trimmed, allowPrimaries);
if (primitiveKind) {
return {
shouldWrap: true,
format: "json",
jsonKind: primitiveKind,
reason: "primitive"
};
}
return {
shouldWrap: false,
format: formatHint ?? "text"
};
}
__name(detectStructuredValue, "detectStructuredValue");
function normalizeFormatHint(formatHint) {
if (!formatHint) return void 0;
switch (formatHint) {
case "json":
case "csv":
case "xml":
case "text":
return formatHint;
default:
return "unknown";
}
}
__name(normalizeFormatHint, "normalizeFormatHint");
function classifyStructure(trimmed) {
if (!trimmed) return void 0;
const first = trimmed[0];
const last = trimmed[trimmed.length - 1];
if (first === "{" && last === "}") {
return "object";
}
if (first === "[" && last === "]") {
return "array";
}
return void 0;
}
__name(classifyStructure, "classifyStructure");
function classifyPrimitive(trimmed, allowPrimaries = true) {
if (!allowPrimaries) return void 0;
if (JSON_PRIMITIVES.has(trimmed)) {
return "primitive";
}
if (/^-?\d+$/.test(trimmed)) {
const asNumber = Number(trimmed);
if (Number.isSafeInteger(asNumber)) {
return "primitive";
}
return void 0;
}
if (/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
return "primitive";
}
if (trimmed.startsWith('"') && trimmed.endsWith('"')) {
return "primitive";
}
return void 0;
}
__name(classifyPrimitive, "classifyPrimitive");
// interpreter/eval/exec-invocation.ts
async function resolveStdinInput(stdinSource, env) {
if (stdinSource === null || stdinSource === void 0) {
return "";
}
const { evaluate } = await import('./interpreter-MW7QI3FC.mjs');
const result = await evaluate(stdinSource, env, {
isExpression: true
});
let value = result.value;
const { isVariable, resolveValue, ResolutionContext } = await import('./variable-resolution-HFG3FTZK.mjs');
if (isVariable(value)) {
value = await resolveValue(value, env, ResolutionContext.CommandExecution);
}
return coerceValueForStdin(value);
}
__name(resolveStdinInput, "resolveStdinInput");
var chainDebugEnabled = process.env.MLLD_DEBUG_CHAINING === "1";
function chainDebug(message, payload) {
if (!chainDebugEnabled) {
return;
}
try {
const serialized = payload ? ` ${JSON.stringify(payload)}` : "";
process.stdout.write(`[CHAIN] ${message}${serialized}
`);
} catch {
process.stdout.write(`[CHAIN] ${message}
`);
}
}
__name(chainDebug, "chainDebug");
function cloneVariableWithNewValue(source, value, fallback) {
const cloned = {
...source,
value: value ?? fallback,
mx: source.mx ? {
...source.mx
} : void 0,
internal: {
...source.internal ?? {}
}
};
if (cloned.mx?.mxCache) {
delete cloned.mx.mxCache;
}
return cloned;
}
__name(cloneVariableWithNewValue, "cloneVariableWithNewValue");
function cloneGuardCandidateForParameter(name, candidate, argValue, fallback) {
const cloned = {
...candidate,
name,
value: argValue ?? fallback ?? candidate.value,
mx: candidate.mx ? {
...candidate.mx
} : void 0,
internal: {
...candidate.internal ?? {},
isSystem: true,
isParameter: true
}
};
if (cloned.mx?.mxCache) {
delete cloned.mx.mxCache;
}
return cloned;
}
__name(cloneGuardCandidateForParameter, "cloneGuardCandidateForParameter");
function stringifyGuardArg(value) {
if (isStructuredValue(value)) {
return asText(value);
}
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
return String(value);
}
if (value === void 0) {
return "undefined";
}
if (value === null) {
return "null";
}
try {
return JSON.stringify(value);
} catch {
return String(value);
}
}
__name(stringifyGuardArg, "stringifyGuardArg");
function applyGuardTransformsToExecArgs(options) {
const { guardInputEntries, transformedInputs, guardVariableCandidates, evaluatedArgs, evaluatedArgStrings } = options;
const limit = Math.min(transformedInputs.length, guardInputEntries.length);
for (let i = 0; i < limit; i++) {
const entry = guardInputEntries[i];
const replacement = transformedInputs[i];
if (!entry || !replacement) {
continue;
}
const argIndex = entry.index;
guardVariableCandidates[argIndex] = replacement;
const normalizedValue = isStructuredValue(replacement.value) ? replacement.value.data : replacement.value;
evaluatedArgs[argIndex] = normalizedValue;
evaluatedArgStrings[argIndex] = stringifyGuardArg(normalizedValue);
}
}
__name(applyGuardTransformsToExecArgs, "applyGuardTransformsToExecArgs");
var resolveVariableIndexValue = /* @__PURE__ */ __name(async (fieldValue, env) => {
const { evaluateDataValue } = await import('./data-value-evaluator-6G4NQGOF.mjs');
const node = typeof fieldValue === "object" ? fieldValue : {
type: "VariableReference",
valueType: "varIdentifier",
identifier: String(fieldValue)
};
return evaluateDataValue(node, env);
}, "resolveVariableIndexValue");
function ensureStringTarget(method, target) {
if (typeof target === "string") {
return target;
}
throw new MlldInterpreterError(`Cannot call .${method}() on ${typeof target}`);
}
__name(ensureStringTarget, "ensureStringTarget");
function ensureArrayTarget(method, target) {
if (Array.isArray(target)) {
return target;
}
throw new MlldInterpreterError(`Cannot call .${method}() on ${typeof target}`);
}
__name(ensureArrayTarget, "ensureArrayTarget");
function handleStringBuiltin(method, target, args = []) {
const value = ensureStringTarget(method, target);
switch (method) {
case "toLowerCase":
return value.toLowerCase();
case "toUpperCase":
return value.toUpperCase();
case "trim":
return value.trim();
case "slice": {
const start = args.length > 0 ? Number(args[0]) : void 0;
const end = args.length > 1 ? Number(args[1]) : void 0;
return value.slice(start ?? void 0, end ?? void 0);
}
case "substring": {
const start = args.length > 0 ? Number(args[0]) : 0;
const end = args.length > 1 ? Number(args[1]) : void 0;
return value.substring(start, end ?? void 0);
}
case "substr": {
const start = args.length > 0 ? Number(args[0]) : 0;
const length = args.length > 1 && args[1] !== void 0 ? Number(args[1]) : void 0;
return length !== void 0 ? value.substr(start, length) : value.substr(start);
}
case "replace": {
const searchValue = args[0] instanceof RegExp ? args[0] : String(args[0] ?? "");
const replaceValue = String(args[1] ?? "");
return value.replace(searchValue, replaceValue);
}
case "replaceAll": {
const searchValue = args[0] instanceof RegExp ? args[0] : String(args[0] ?? "");
const replaceValue = String(args[1] ?? "");
if (searchValue instanceof RegExp && !searchValue.global) {
return value.replace(new RegExp(searchValue.source, `${searchValue.flags}g`), replaceValue);
}
return value.replaceAll(searchValue, replaceValue);
}
case "padStart": {
const targetLength = args.length > 0 ? Number(args[0]) : value.length;
const padStringArg = args.length > 1 ? String(args[1]) : " ";
return value.padStart(targetLength, padStringArg);
}
case "padEnd": {
const targetLength = args.length > 0 ? Number(args[0]) : value.length;
const padStringArg = args.length > 1 ? String(args[1]) : " ";
return value.padEnd(targetLength, padStringArg);
}
case "repeat": {
const count = args.length > 0 ? Number(args[0]) : 0;
return value.repeat(count);
}
}
throw new MlldInterpreterError(`Unsupported string builtin: ${method}`);
}
__name(handleStringBuiltin, "handleStringBuiltin");
function handleArrayBuiltin(method, target, args = []) {
const array = ensureArrayTarget(method, target);
switch (method) {
case "slice": {
const start = args.length > 0 ? Number(args[0]) : void 0;
const end = args.length > 1 ? Number(args[1]) : void 0;
return array.slice(start ?? void 0, end ?? void 0);
}
case "concat":
return array.concat(...args);
case "reverse":
return [
...array
].reverse();
case "sort": {
const cloned = [
...array
];
const comparator = args[0];
if (typeof comparator === "function") {
return cloned.sort(comparator);
}
return cloned.sort();
}
}
throw new MlldInterpreterError(`Unsupported array builtin: ${method}`);
}
__name(handleArrayBuiltin, "handleArrayBuiltin");
function handleLengthBuiltin(target) {
if (typeof target === "string" || Array.isArray(target)) {
return target.length;
}
throw new MlldInterpreterError(`Cannot call .length() on ${typeof target}`);
}
__name(handleLengthBuiltin, "handleLengthBuiltin");
function handleJoinBuiltin(target, separator) {
const value = ensureArrayTarget("join", target);
const joiner = separator !== void 0 ? String(separator) : ",";
return value.join(joiner);
}
__name(handleJoinBuiltin, "handleJoinBuiltin");
function handleSplitBuiltin(target, separator) {
const value = ensureStringTarget("split", target);
const splitOn = separator !== void 0 ? String(separator) : "";
return value.split(splitOn);
}
__name(handleSplitBuiltin, "handleSplitBuiltin");
function handleTypeCheckingBuiltin(method, target) {
switch (method) {
case "isArray":
return Array.isArray(target);
case "isObject":
return typeof target === "object" && target !== null && !Array.isArray(target);
case "isString":
return typeof target === "string";
case "isNumber":
return typeof target === "number";
case "isBoolean":
return typeof target === "boolean";
case "isNull":
return target === null;
case "isDefined":
return target !== null && target !== void 0;
}
throw new MlldInterpreterError(`Unsupported type checking builtin: ${method}`);
}
__name(handleTypeCheckingBuiltin, "handleTypeCheckingBuiltin");
function handleSearchBuiltin(method, target, arg) {
if (Array.isArray(target)) {
if (method === "includes") {
return target.includes(arg);
}
if (method === "indexOf") {
return target.indexOf(arg);
}
throw new MlldInterpreterError(`Cannot call .${method}() on array targets`);
}
const value = ensureStringTarget(method, target);
const searchValue = String(arg ?? "");
switch (method) {
case "includes":
return value.includes(searchValue);
case "indexOf":
return value.indexOf(searchValue);
case "startsWith":
return value.startsWith(searchValue);
case "endsWith":
return value.endsWith(searchValue);
}
throw new MlldInterpreterError(`Unsupported search builtin: ${method}`);
}
__name(handleSearchBuiltin, "handleSearchBuiltin");
function handleMatchBuiltin(target, arg) {
const value = ensureStringTarget("match", target);
const pattern = arg instanceof RegExp ? arg : new RegExp(String(arg ?? ""));
return value.match(pattern);
}
__name(handleMatchBuiltin, "handleMatchBuiltin");
async function evaluateExecInvocation(node, env) {
const operationPreview = buildExecOperationPreview(node);
return await runWithGuardRetry({
env,
operationContext: operationPreview,
sourceRetryable: true,
execute: /* @__PURE__ */ __name(() => evaluateExecInvocationInternal(node, env), "execute")
});
}
__name(evaluateExecInvocation, "evaluateExecInvocation");
function buildExecOperationPreview(node) {
const identifier = node.commandRef?.identifier;
if (typeof identifier === "string" && identifier.length > 0) {
return {
type: "exe",
name: identifier,
location: node.location ?? null,
metadata: {
sourceRetryable: true
}
};
}
return void 0;
}
__name(buildExecOperationPreview, "buildExecOperationPreview");
async function evaluateExecInvocationInternal(node, env) {
let commandName;
const builtinMethods = [
"includes",
"match",
"length",
"indexOf",
"join",
"split",
"toLowerCase",
"toUpperCase",
"trim",
"slice",
"substring",
"substr",
"replace",
"replaceAll",
"padStart",
"padEnd",
"repeat",
"startsWith",
"endsWith",
"concat",
"reverse",
"sort",
"isArray",
"isObject",
"isString",
"isNumber",
"isBoolean",
"isNull",
"isDefined"
];
const normalizeFields = /* @__PURE__ */ __name((fields) => (fields || []).map((field) => {
if (!field || typeof field !== "object") return field;
if (field.type === "Field") {
return {
...field,
type: "field"
};
}
return field;
}), "normalizeFields");
let streamingOptions = env.getStreamingOptions();
let streamingRequested = node.stream === true || node.withClause?.stream === true || node.meta?.withClause?.stream === true;
let streamingEnabled = streamingOptions.enabled !== false && streamingRequested;
const hasStreamFormat = node.withClause?.streamFormat !== void 0 || node.meta?.withClause?.streamFormat !== void 0;
const rawStreamFormat = node.withClause?.streamFormat || node.meta?.withClause?.streamFormat;
const streamFormatValue = hasStreamFormat ? await resolveStreamFormatValue(rawStreamFormat, env) : void 0;
const pipelineId = `exec-${Date.now().toString(36)}-${Math.floor(Math.random() * 1e4)}`;
let lastEmittedChunk;
if (hasStreamFormat) {
env.setStreamingOptions({
...streamingOptions,
streamFormat: streamFormatValue,
skipDefaultSinks: true,
suppressTerminal: true
});
streamingOptions = env.getStreamingOptions();
}
const activeStreamingOptions = streamingOptions;
const streamingManager = env.getStreamingManager();
if (streamingEnabled) {
let adapter;
if (hasStreamFormat && streamFormatValue) {
adapter = await loadStreamAdapter(streamFormatValue);
}
if (!adapter) {
adapter = await getAdapter("ndjson");
}
streamingManager.configure({
env,
streamingEnabled: true,
streamingOptions: activeStreamingOptions,
adapter
});
}
const chunkEffect = /* @__PURE__ */ __name((chunk, source) => {
if (!streamingEnabled) return;
const trimmed = chunk.trim();
if (trimmed && trimmed === lastEmittedChunk) {
return;
}
lastEmittedChunk = trimmed || chunk;
const withSpacing = chunk.endsWith("\n") ? `${chunk}
` : `${chunk}
`;
if (!streamingOptions.skipDefaultSinks) {
return;
}
if (source === "stdout") {
env.emitEffect("doc", withSpacing);
} else {
env.emitEffect("stderr", chunk);
}
}, "chunkEffect");
try {
let resultSecurityDescriptor;
const mergeResultDescriptor = /* @__PURE__ */ __name((descriptor) => {
if (!descriptor) {
return;
}
resultSecurityDescriptor = resultSecurityDescriptor ? env.mergeSecurityDescriptors(resultSecurityDescriptor, descriptor) : descriptor;
}, "mergeResultDescriptor");
const interpolateWithResultDescriptor = /* @__PURE__ */ __name((nodes, targetEnv = env, interpolationContext = InterpolationContext.Default) => {
return interpolate(nodes, targetEnv, interpolationContext, {
collectSecurityDescriptor: /* @__PURE__ */ __name((descriptor) => {
if (descriptor) {
mergeResultDescriptor(descriptor);
}
}, "collectSecurityDescriptor")
});
}, "interpolateWithResultDescriptor");
const createParameterMetadata = /* @__PURE__ */ __name((value) => {
const descriptor = extractSecurityDescriptor(value);
const metadata = descriptor ? VariableMetadataUtils.applySecurityMetadata(void 0, {
existingDescriptor: descriptor
}) : void 0;
return {
metadata,
internal: {
isSystem: true,
isParameter: true
}
};
}, "createParameterMetadata");
const createEvalResult = /* @__PURE__ */ __name((value, targetEnv, options) => {
const wrapped = wrapExecResult(value, options);
if (resultSecurityDescriptor) {
const existing = getStructuredSecurityDescriptor(wrapped);
const merged = existing ? env.mergeSecurityDescriptors(existing, resultSecurityDescriptor) : resultSecurityDescriptor;
setStructuredSecurityDescriptor(wrapped, merged);
}
return {
value: wrapped,
env: targetEnv,
stdout: asText(wrapped),
stderr: "",
exitCode: 0
};
}, "createEvalResult");
const toPipelineInput = /* @__PURE__ */ __name((value, options) => {
const structured = wrapExecResult(value, options);
if (resultSecurityDescriptor) {
setStructuredSecurityDescriptor(structured, resultSecurityDescriptor);
}
return structured;
}, "toPipelineInput");
if (process.env.MLLD_DEBUG === "true") {
console.error("[evaluateExecInvocation] Entry:", {
hasCommandRef: !!node.commandRef,
hasWithClause: !!node.withClause,
hasPipeline: !!node.withClause?.pipeline,
pipelineLength: node.withClause?.pipeline?.length
});
}
if (process.env.DEBUG_WHEN || process.env.DEBUG_EXEC) {
logger.debug("evaluateExecInvocation called with:", {
commandRef: node.commandRef
});
}
const nodeSourceLocation = astLocationToSourceLocation(node.location, env.getCurrentFilePath());
let args = [];
if (!node.commandRef && node.name) {
commandName = node.name;
args = node.arguments || [];
} else if (node.commandRef) {
if (node.commandRef.name) {
commandName = node.commandRef.name;
args = node.commandRef.args || [];
} else if (typeof node.commandRef.identifier === "string") {
commandName = node.commandRef.identifier;
args = node.commandRef.args || [];
} else if (Array.isArray(node.commandRef.identifier) && node.commandRef.identifier.length > 0) {
const identifierNode2 = node.commandRef.identifier[0];
if (identifierNode2.type === "VariableReference" && identifierNode2.identifier) {
commandName = identifierNode2.identifier;
} else if (identifierNode2.type === "Text" && identifierNode2.content) {
commandName = identifierNode2.content;
} else {
throw new Error("Unable to extract command name from identifier array");
}
args = node.commandRef.args || [];
} else {
throw new Error("CommandReference missing both name and identifier");
}
} else {
throw new Error("ExecInvocation node missing both commandRef and name");
}
const identifierNode = node.commandRef?.identifier?.[0];
const identifierFields = normalizeFields(identifierNode?.fields);
const lastField = identifierFields[identifierFields.length - 1];
if (lastField?.type === "variableIndex") {
const resolvedName = await resolveVariableIndexValue(lastField.value, env);
commandName = String(resolvedName);
}
if (!commandName) {
throw new MlldInterpreterError("ExecInvocation has no command identifier");
}
const isBuiltinMethod = builtinMethods.includes(commandName);
const isReservedName = env.hasVariable(commandName) && env.getVariable(commandName)?.internal?.isReserved;
const shouldTrackResolution = !isBuiltinMethod && !isReservedName;
if (shouldTrackResolution && env.isResolving(commandName)) {
throw new CircularReferenceError(`Circular reference detected: executable '@${commandName}' calls itself recursively without a terminating condition`, {
identifier: commandName,
location: nodeSourceLocation
});
}
if (shouldTrackResolution) {
env.beginResolving(commandName);
}
let variable;
const commandRefWithObject = node.commandRef;
if (node.commandRef && (commandRefWithObject.objectReference || commandRefWithObject.objectSource)) {
const typeCheckingMethods = [
"isArray",
"isObject",
"isString",
"isNumber",
"isBoolean",
"isNull",
"isDefined"
];
const isTypeCheckingBuiltin = typeCheckingMethods.includes(commandName);
if (builtinMethods.includes(commandName)) {
let objectValue2;
let objectVar2;
let sourceDescriptor;
if (commandRefWithObject.objectReference) {
const objectRef2 = commandRefWithObject.objectReference;
objectVar2 = env.getVariable(objectRef2.identifier);
if (!objectVar2) {
if (isTypeCheckingBuiltin) {
const typeCheckResult = handleTypeCheckingBuiltin(commandName, void 0);
return createEvalResult(typeCheckResult, env);
}
throw new MlldInterpreterError(`Object not found: ${objectRef2.identifier}`);
}
const { extractVariableValue: extractVariableValue2, isVariable } = await import('./variable-resolution-HFG3FTZK.mjs');
objectValue2 = await extractVariableValue2(objectVar2, env);
if (isVariable(objectValue2)) {
objectValue2 = await extractVariableValue2(objectValue2, env);
}
if (objectRef2.fields && objectRef2.fields.length > 0) {
const normalizedFields = normalizeFields(objectRef2.fields);
for (const field of normalizedFields) {
let targetValue = objectValue2;
let key = field.value;
if (field.type === "variableIndex") {
if (isStructuredValue(targetValue)) {
targetValue = asData(targetValue);
}
key = await resolveVariableIndexValue(field.value, env);
}
if (isStructuredValue(targetValue) && typeof key === "string" && key in targetValue) {
objectValue2 = targetValue[key];
} else if (typeof targetValue === "object" && targetValue !== null) {
objectValue2 = targetValue[key];
} else {
if (isTypeCheckingBuiltin) {
const typeCheckResult = handleTypeCheckingBuiltin(commandName, void 0);
return createEvalResult(typeCheckResult, env);
}
throw new MlldInterpreterError(`Cannot access field ${String(key)} on non-object`);
}
}
}
} else if (commandRefWithObject.objectSource) {
const srcResult = await evaluateExecInvocation(commandRefWithObject.objectSource, env);
if (srcResult && typeof srcResult === "object") {
sourceDescriptor = extractSecurityDescriptor(srcResult.value);
if (srcResult.value !== void 0) {
const { resolveValue, ResolutionContext } = await import('./variable-resolution-HFG3FTZK.mjs');
objectValue2 = await resolveValue(srcResult.value, env, ResolutionContext.Display);
} else if (typeof srcResult.stdout === "string") {
objectValue2 = srcResult.stdout;
}
}
}
if (typeof objectValue2 === "undefined") {
if (isTypeCheckingBuiltin) {
const typeCheckResult = handleTypeCheckingBuiltin(commandName, objectValue2);
return createEvalResult(typeCheckResult, env);
}
if (process.env.DEBUG_EXEC) {
logger.debug("Builtin invocation unresolved object value", {
commandName,
objectIdentifier: commandRefWithObject.objectReference?.identifier,
fields: commandRefWithObject.objectReference?.fields
});
}
throw new MlldInterpreterError("Unable to resolve object value for builtin method invocation");
}
if (process.env.DEBUG_EXEC) {
logger.debug("Builtin invocation object value", {
commandName,
objectType: Array.isArray(objectValue2) ? "array" : typeof objectValue2,
objectPreview: typeof objectValue2 === "string" ? objectValue2.slice(0, 80) : Array.isArray(objectValue2) ? `[array length=${objectValue2.length}]` : objectValue2 && typeof objectValue2 === "object" ? Object.keys(objectValue2) : objectValue2
});
}
chainDebug("builtin invocation start", {
commandName,
hasObjectSource: Boolean(commandRefWithObject.objectSource),
hasObjectReference: Boolean(commandRefWithObject.objectReference)
});
const targetDescriptor = sourceDescriptor || objectVar2 && varMxToSecurityDescriptor(objectVar2.mx) || extractSecurityDescriptor(objectValue2);
mergeResultDescriptor(targetDescriptor);
if (isStructuredValue(objectValue2)) {
objectValue2 = objectValue2.type === "array" ? objectValue2.data : asText(objectValue2);
} else if (StructuredValue.isStructuredValue?.(objectValue2)) {
objectValue2 = objectValue2.text;
}
chainDebug("resolved object value", {
commandName,
objectType: Array.isArray(objectValue2) ? "array" : typeof objectValue2,
preview: typeof objectValue2 === "string" ? objectValue2.slice(0, 80) : Array.isArray(objectValue2) ? `[array length=${objectValue2.length}]` : objectValue2 && typeof objectValue2 === "object" ? "[object]" : objectValue2
});
const evaluatedArgs2 = [];
for (const arg of args) {
const { evaluateDataValue } = await import('./data-value-evaluator-6G4NQGOF.mjs');
const evaluatedArg = await evaluateDataValue(arg, env);
evaluatedArgs2.push(evaluatedArg);
}
const quantifierEvaluator = objectValue2?.__mlldQuantifierEvaluator;
if (quantifierEvaluator && typeof quantifierEvaluator === "function") {
const quantifierResult = quantifierEvaluator(commandName, evaluatedArgs2);
return createEvalResult(quantifierResult, env);
}
let result;
const propagateResult = /* @__PURE__ */ __name((output) => {
inheritExpressionProvenance(output, objectVar2 ?? objectValue2);
const sourceDescriptor2 = objectVar2 && varMxToSecurityDescriptor(objectVar2.mx) || extractSecurityDescriptor(objectValue2);
if (sourceDescriptor2) {
resultSecurityDescriptor = resultSecurityDescriptor ? env.mergeSecurityDescriptors(resultSecurityDescriptor, sourceDescriptor2) : sourceDescriptor2;
}
}, "propagateResult");
switch (commandName) {
case "toLowerCase":
case "toUpperCase":
case "trim":
case "substring":
case "substr":
case "replace":
case "replaceAll":
case "padStart":
case "padEnd":
case "repeat":
result = handleStringBuiltin(commandName, objectValue2, evaluatedArgs2);
propagateResult(result);
break;
case "slice":
if (Array.isArray(objectValue2)) {
result = handleArrayBuiltin("slice", objectValue2, evaluatedArgs2);
} else {
result = handleStringBuiltin("slice", objectValue2, evaluatedArgs2);
}
propagateResult(result);
break;
case "concat":
case "reverse":
case "sort":
result = handleArrayBuiltin(commandName, objectValue2, evaluatedArgs2);
propagateResult(result);
break;
case "length":
result = handleLengthBuiltin(objectValue2);
break;
case "join":
result = handleJoinBuiltin(objectValue2, evaluatedArgs2[0]);
propagateResult(result);
break;
case "split":
result = handleSplitBuiltin(objectValue2, evaluatedArgs2[0]);
propagateResult(result);
break;
case "includes":
case "indexOf":
case "startsWith":
case "endsWith":
result = handleSearchBuiltin(commandName, objectValue2, evaluatedArgs2[0]);
break;
case "match":
result = handleMatchBuiltin(objectValue2, evaluatedArgs2[0]);
propagateResult(result);
break;
case "isArray":
case "isObject":
case "isString":
case "isNumber":
case "isBoolean":
case "isNull":
case "isDefined":
result = handleTypeCheckingBuiltin(commandName, objectValue2);
break;
default:
throw new MlldInterpreterError(`Unknown builtin method: ${commandName}`);
}
const postFieldsBuiltin = node.fields || [];
if (postFieldsBuiltin && postFieldsBuiltin.length > 0) {
const { accessField } = await import('./field-access-MJ6PMBJX.mjs');
for (const f of postFieldsBuiltin) {
result = await accessField(result, f, {
env,
sourceLocation: nodeSourceLocation
});
}
}
const normalized = normalizeTransformerResult(commandName, result);
const resolvedValue = normalized.value;
const wrapOptions = normalized.options;
inheritExpressionProvenance(resolvedValue, objectVar2 ?? objectValue2);
if (node.withClause) {
if (node.withClause.pipeline) {
const { processPipeline } = await import('./unified-processor-GTJC5G2K.mjs');
const pipelineInputValue = toPipelineInput(resolvedValue, wrapOptions);
chainDebug("applying builtin pipeline", {
commandName,
pipelineLength: node.withClause.pipeline.length
});
const pipelineResult = await processPipeline({
value: pipelineInputValue,
env,
node,
identifier: node.identifier,
descriptorHint: resultSecurityDescriptor
});
return applyWithClause(pipelineResult, {
...node.withClause,
pipeline: void 0
}, env);
} else {
return applyWithClause(resolvedValue, node.withClause, env);
}
}
return createEvalResult(resolvedValue, env, wrapOptions);
}
if (commandRefWithObject.objectSource && !commandRefWithObject.objectReference) {
throw new MlldInterpreterError(`Only builtin methods are supported on exec results (got: ${commandName})`);
}
const objectRef = commandRefWithObject.objectReference;
const objectVar = env.getVariable(objectRef.identifier);
if (!objectVar) {
throw new MlldInterpreterError(`Object not found: ${objectRef.identifier}`);
}
const { extractVariableValue } = await import('./variable-resolution-HFG3FTZK.mjs');
const objectValue = await extractVariableValue(objectVar, env);
if (objectRef.fields && objectRef.fields.length > 0) {
const { accessFields } = await import('./field-access-MJ6PMBJX.mjs');
const accessedObject = await accessFields(objectValue, normalizeFields(objectRef.fields), {
env,
preserveContext: false,
returnUndefinedForMissing: true,
sourceLocation: objectRef.location
});
if (typeof accessedObject === "object" && accessedObject !== null) {
const fieldValue = accessedObject[commandName];
variable = fieldValue;
}
} else {
if (typeof objectValue === "object" && objectValue !== null) {
let fieldValue;
if (objectValue.type === "object" && objectValue.properties) {
fieldValue = objectValue.properties[commandName];
} else {
fieldValue = objectValue[commandName];
}
variable = fieldValue;
}
}
if (!variable) {
throw new MlldInterpreterError(`Method not found: ${commandName} on ${objectRef.identifier}`);
}
if (typeof variable === "object" && variable !== null && "__executable" in variable && variable.__executable) {
let serializedInternal = variable.internal ?? {};
if (serializedInternal.capturedShadowEnvs && typeof serializedInternal.capturedShadowEnvs === "object") {
const needsDeserialization = Object.entries(serializedInternal.capturedShadowEnvs).some(([lang, env2]) => env2 && !(env2 instanceof Map));
if (needsDeserialization) {
serializedInternal = {
...serializedInternal,
capturedShadowEnvs: deserializeShadowEnvs(serializedInternal.capturedShadowEnvs)
};
}
}
if (serializedInternal.capturedModuleEnv && !(serializedInternal.capturedModuleEnv instanceof Map)) {
const { VariableImporter } = await import('./VariableImporter-7E4PKQ75.mjs');
const importer = new VariableImporter(null);
const moduleEnvMap = importer.deserializeModuleEnv(serializedInternal.capturedModuleEnv);
for (const [_, variable2] of moduleEnvMap) {
if (variable2.type === "executable") {
variable2.internal = {
...variable2.internal ?? {},
capturedModuleEnv: moduleEnvMap
};
}
}
serializedInternal = {
...serializedInternal,
capturedModuleEnv: moduleEnvMap
};
}
const { createExecutableVariable } = await import('./VariableFactories-JRELL2UW.mjs');
variable = createExecutableVariable(commandName, "command", "", variable.paramNames || [], void 0, {
directive: "exe",
syntax: "braces",
hasInterpolation: false,
isMultiLine: false
}, {
internal: {
executableDef: variable.executableDef,
...serializedInternal
}
});
}
} else {
variable = env.getVariable(commandName);
if (!variable) {
if (commandName.includes(".")) {
const parts = commandName.split(".");
const baseName = parts[0];
const variantName = parts.slice(1).join(".");
const baseVar = env.getVariable(baseName);
if (baseVar) {
const variants = baseVar.internal?.transformerVariants;
if (variants && variantName in variants) {
variable = variants[variantName];
}
}
}
if (!variable) {
throw new MlldInterpreterError(`Command not found: ${commandName}`);
}
}
if (isExecutableVariable(variable) && node.commandRef) {
const varRef = node.commandRef.identifier?.[0] || node.commandRef;
if (varRef && varRef.type === "VariableReference" && varRef.fields && varRef.fields.length > 0) {
const field = varRef.fields[0];
if (field.type === "field") {
const variants = variable.internal?.transformerVariants;
if (variants && field.value in variants) {
variable = variants[field.value];
}
}
}
}
}
if (!isExecutableVariable(variable)) {
throw new MlldInterpreterError(`Variable ${commandName} is not executable (type: ${variable.type})`);
}
if (variable.internal?.isBuiltinTransformer && variable.internal?.transformerImplementation) {
if (commandName === "typeof" || commandName === "TYPEOF") {
if (args.length > 0) {
const arg = args[0];
if (arg && typeof arg === "object" && "type" in arg && arg.type === "VariableReference") {
const varRef = arg;
const varName = varRef.identifier;
const varObj = env.getVariable(varName);
if (varObj) {
let typeInfo = varObj.type;
if (varObj.type === "simple-text" && "subtype" in varObj) {
const subtype = varObj.subtype;
if (subtype && subtype !== "simple" && subtype !== "interpolated-text") {
typeInfo = subtype;
}
} else if (varObj.type === "primitive" && "primitiveType" in varObj) {
typeInfo = `primitive (${varObj.primitiveType})`;
} else if (varObj.type === "object") {
const objValue = varObj.value;
if (objValue && typeof objValue === "object") {
const keys = Object.keys(objValue);
typeInfo = `object (${keys.length} properties)`;
}
} else if (varObj.type === "array") {
const arrValue = varObj.value;
if (Array.isArray(arrValue)) {
typeInfo = `array (${arrValue.length} items)`;
}
} else if (varObj.type === "executable") {
const execDef = varObj.internal?.executableDef;
if (execDef && "type" in execDef) {
typeInfo = `executable (${execDef.type})`;
}
}
if (varObj.source?.directive) {
typeInfo += ` [from /${varObj.source.directive}]`;
}
const result2 = await variable.internal.transformerImplementation(`__MLLD_VARIABLE_OBJECT__:${typeInfo}`);
const normalized2 = normalizeTransformerResult(commandName, result2);
const resolvedValue2 = normalized2.value;
const wrapOptions2 = normalized2.options;
if (commandName && shouldTrackResolution) {
env.endResolving(commandName);
}
if (node.withClause) {
if (node.withClause.pipeline) {
const { processPipeline } = await import('./unified-processor-GTJC5G2K.mjs');
const pipelineInputValue = toPipelineInput(resolvedValue2, wrapOptions2);
const pipelineResult = await processPipeline({
value: pipelineInputValue,
env,
node,
identifier: node.identifier,
descriptorHint: resultSecurityDescriptor
});
return applyWithClause(pipelineResult, {
...node.withClause,
pipeline: void 0
}, env);
} else {
return applyWithClause(resolvedValue2, node.withClause, env);
}
}
return createEvalResult(resolvedValue2, env, wrapOptions2);
}
}
}
}
if (commandName === "exists" || commandName === "EXISTS") {
const arg = args[0];
const isGlobPattern = /* @__PURE__ */ __name((value) => /[\*\?\{\}\[\]]/.test(value), "isGlobPattern");
const isEmptyLoadArray = /* @__PURE__ */ __name((value) => {
if (isStructuredValue(value)) {
return value.type === "array" && Array.isArray(value.data) && value.data.length === 0;
}
return Array.isArray(value) && value.length === 0;
}, "isEmptyLoadArray");
const resolveLoadContentSource = /* @__PURE__ */ __name(async (loadNode) => {
const source = loadNode?.source;
if (!source || typeof source !== "object") {
return void 0;
}
const actualSource = source.type === "path" || source.type === "url" ? source : source.segments && source.raw !== void 0 ? {
...source,
type: "path"
} : source;
if (actualSource.type === "path") {
if (actualSource.meta?.hasVariables && Array.isArray(actualSource.segments)) {
try {
return (await interpolateWithResultDescriptor(actualSource.segments, env)).trim();
} catch {
return typeof actualSource.raw === "string" ? actualSource.raw.trim() : void 0;
}
}
if (typeof actualSource.raw === "string") {
return actualSource.raw.trim();
}
}
if (actualSource.type === "url") {
if (typeof actualSource.raw === "string") {
return actualSource.raw.trim();
}
if (typeof actualSource.protocol === "string" && typeof actualSource.host === "string") {
return `${actualSource.protocol}://${actualSource.host}${actualSource.path || ""}`;
}
}
return void 0;
}, "resolveLoadContentSource");
const isStringPathArgument = /* @__PURE__ */ __name((value) => {
if (Array.isArray(value)) {
return true;
}
if (!value || typeof value !== "object") {
return false;
}
if ("wrapperType" in value && Array.isArray(value.content)) {
return true;
}
if (value.type === "Text") {
return true;
}
if (value.type === "Literal" && value.valueType === "string") {
return true;
}
return false;
}, "isStringPathArgument");
const finalizeExistsResult = /* @__PURE__ */ __name(async (existsResult) => {
if (commandName && shouldTrackResolution) {
env.endResolving(commandName);
}
if (node.withClause) {
if (node.withClause.pipeline) {
const { processPipeline } = await import('./unified-processor-GTJC5G2K.mjs');
const pipelineInputValue = toPipelineInput(existsResult);
const pipelineResult = await processPipeline({
value: pipelineInputValue,
env,
node,
identifier: node.identifier,
descriptorHint: resultSecurityDescriptor
});
return applyWithClause(pipelineResult, {
...node.withClause,
pipeline: void 0
}, env);
}
return applyWithClause(existsResult, node.withClause, env);
}
return createEvalResult(existsResult, env);
}, "finalizeExistsResult");
if (!arg) {
return finalizeExistsResult(false);
}
try {
if (isStringPathArgument(arg)) {
const resolvePathString = /* @__PURE__ */ __name(async () => {
if (Array.isArray(arg)) {
return interpolateWithResultDescriptor(arg, env, InterpolationContext.Default);
}
if (arg && typeof arg === "object" && arg.type === "Text") {
return String(arg.content ?? "");
}
if (arg && typeof arg === "object" && arg.type === "Literal") {
return String(arg.value ?? "");
}
if (arg && typeof arg === "object" && "wrapperType" in arg && Array.isArray(arg.content)) {
return interpolateWithResultDescriptor(arg.content, env, InterpolationContext.Default);
}
return String(arg ?? "");
}, "resolvePathString");
const trimmedPath = (await resolvePathString()).trim();
if (!trimmedPath) {
return finalizeExistsResult(false);
}
const { processContentLoader } = await import('./content-loader-QADYRQX5.mjs');
const loadNode = {
type: "load-content",
source: {
type: "path",
raw: trimmedPath
}
};
const loadResult = await processContentLoader(loadNode, env);
if (isGlobPattern(trimmedPath) && isEmptyLoadArray(loadResult)) {
return finalizeExistsResult(false);
}
return finalizeExistsResult(true);
}
if (arg && typeof arg === "object" && arg.type === "load-content") {
const { processContentLoader } = await import('./content-loader-QADYRQX5.mjs');
const loadResult = await processContentLoader(arg, env);
const sourceString = await resolveLoadContentSource(arg);
if (sourceString && isGlobPattern(sourceString) && isEmptyLoadArray(loadResult)) {
return finalizeExistsResult(false);
}
return finalizeExistsResult(true);
}
if (arg && typeof arg === "object" && arg.type === "ExecInvocation") {
await evaluateExecInvocation(arg, env);
return finalizeExistsResult(true);
}
if (arg && typeof arg === "object" && arg.type === "VariableReference") {
const varRef = arg;
let targetVar = env.getVariable(varRef.identifier);
if (!targetVar && env.hasVariable(varRef.identifier)) {
targetVar = await env.getResolverVariable(varRef.identifier);
}
if (!targetVar) {
return finalizeExistsResult(false);
}
const { resolveVariable, ResolutionContext } = await import('./variable-resolution-HFG3FTZK.mjs');
let resolvedValue2 = await resolveVariable(targetVar, env, ResolutionContext.FieldAccess);
if (varRef.fields && varRef.fields.length > 0) {
const { accessField } = await import('./field-access-MJ6PMBJX.mjs');
const normalized2 = normalizeFields(varRef.fields);
for (const field of normalized2) {
const fieldResult = await accessField(resolvedValue2, field, {
preserveContext: true,
env,
sourceLocation: nodeSourceLocation
});
resolvedValue2 = fieldResult.value;
if (resolvedValue2 === void 0) {
break;
}
}
}
if (varRef.pipes && varRef.pipes.length > 0) {
const { processPipeline } = await import('./unified-processor-GTJC5G2K.mjs');
await processPipeline({
value: resolvedValue2,
env,
node: varRef,
identifier: varRef.identifier
});
}
return finalizeExistsResult(true);
}
return finalizeExistsResult(true);
} catch {
return finalizeExistsResult(false);
}
}
let inputValue = "";
if (args.length > 0) {
let arg = args[0];
if (arg && typeof arg === "object" && "type" in arg) {
const { evaluateDataValue } = await import('./data-value-evaluator-6G4NQGOF.mjs');
arg = await evaluateDataValue(arg, env);
}
const transformerName = (variable.name ?? commandName ?? "").toLowerCase();
if (transformerName === "keep" || transformerName === "keepstructured") {
inputValue = arg;
} else if (typeof arg === "string") {
inputValue = arg;
} else if (arg && typeof arg === "object") {
inputValue = await interpolateWithResultDescriptor([
arg
], env);
} else {
inputValue = String(arg);
}
}
const result = await variable.internal.transformerImplementation(inputValue);
const normalized = normalizeTransformerResult(commandName, result);
const resolvedValue = normalized.value;
const wrapOptions = normalized.options;
if (commandName && shouldTrackResolution) {
env.endResolving(commandName);
}
if (node.withClause) {
if (node.withClause.pipeline) {
const { processPipeline } = await import('./unified-processor-GTJC5G2K.mjs');
const pipelineInputValue = toPipelineInput(resolvedValue, wrapOptions);
const pipelineResult = await processPipeline({
value: pipelineInputValue,
env,
node,
identifier: node.identifier,
descriptorHint: resultSecurityDescriptor
});
return applyWithClause(pipelineResult, {
...node.withClause,
pipeline: void 0
}, env);
} else {
return applyWithClause(resolvedValue, node.withClause, env);
}
}
return createEvalResult(resolvedValue, env, wrapOptions);
}
const definition = variable.internal?.executableDef;
if (!definition) {
throw new MlldInterpreterError(`Executable ${commandName} has no definition in metadata`);
}
if (process.env.DEBUG_EXEC === "true" && commandName === "pipe") {
console.error("[debug-exec] definition for @pipe:", JSON.stringify(definition, null, 2));
}
const definitionHasStreamFormat = definition.withClause?.streamFormat !== void 0 || definition.meta?.withClause?.streamFormat !== void 0;
streamingRequested = streamingRequested || definition.withClause?.stream === true || definition.meta?.withClause?.stream === true || definition.meta?.isStream === true;
streamingEnabled = streamingOptions.enabled !== false && streamingRequested;
let whenExprNode = null;
if (definition.language === "mlld-when") {
const candidate = Array.isArray(definition.codeTemplate) && definition.codeTemplate.length > 0 ? definition.codeTemplate[0] : void 0;
if (!candidate || candidate.type !== "WhenExpression") {
throw new MlldInterpreterError("mlld-when executable missing WhenExpression node");
}
whenExprNode = candidate;
}
let execEnv = env.createChild();
if (variable?.internal?.capturedModuleEnv instanceof Map) {
execEnv.setCapturedModuleEnv(variable.internal.capturedModuleEnv);
}
const params = definition.paramNames || [];
const evaluatedArgStrings = [];
const evaluatedArgs = [];
for (const arg of args) {
let argValue;
let argValueAny;
if (process.env.MLLD_DEBUG === "true") {
console.error("[DEBUG ARG] Processing arg:", {
isStructured: isStructuredValue(arg),
argType: typeof arg,
argKeys: arg && typeof arg === "object" ? Object.keys(arg).slice(0, 5) : null
});
}
if (isStructuredValue(arg)) {
if (process.env.MLLD_DEBUG === "true") {
console.error("[DEBUG ARG] StructuredValue detected:", {
type: arg.type,
dataType: typeof arg.data,
isArray: Array.isArray(arg.data)
});
}
argValueAny = arg;
argValue = asText(arg);
} else if (arg && typeof arg === "object" && arg.type === "RegexLiteral") {
const pattern = arg.pattern || "";
const flags = arg.flags || "";
const regex = new RegExp(pattern, flags);
argValueAny = regex;
argValue = regex.toString();
} else if (typeof arg === "string" || typeof arg === "number" || typeof arg === "boolean") {
argValue = String(arg);
argValueAny = arg;
} else if (arg && typeof arg === "object" && "type" in arg) {
switch (arg.type) {
case "WhenExpression": {
const { evaluateWhenExpression } = await import('./when-expression-ZW53U2K2.mjs');
const whenRes = await evaluateWhenExpression(arg, env);
argValueAny = whenRes.value;
if (argValueAny === void 0) {
argValue = "undefined";
} else if (typeof argValueAny === "object") {
try {
argValue = JSON.stringify(argValueAny);
} catch {
argValue = String(argValueAny);
}
} else {
argValue = String(argValueAny);
}
break;
}
case "foreach":
case "foreach-command": {
const { evaluateForeachCommand } = await import('./foreach-USOCOKPZ.mjs');
const arr = await evaluateForeachCommand(arg, env);
argValueAny = arr;
argValue = JSON.stringify(arr);
break;
}
case "object":
const { evaluateDataValue } = await import('./data-value-evaluator-6G4NQGOF.mjs');
argValueAny = await evaluateDataValue(arg, env);
argValue = JSON.stringify(argValueAny);
break;
case "array":
const { evaluateDataValue: evalArray } = await import('./data-value-evaluator-6G4NQGOF.mjs');
argValueAny = await evalArray(arg, env);
argValue = JSON.stringify(argValueAny);
break;
case "VariableReference":
const varRef = arg;
const varName = varRef.identifier;
const variable2 = env.getVariable(varName);
if (variable2) {
let value = variable2.value;
const { isTemplate } = await import('./variable-FNPDYIEH.mjs');
if (isTemplate(variable2)) {
if (Array.isArray(value)) {
value = await interpolateWithResultDescriptor(value, env);
} else if (variable2.internal?.templateAst && Array.isArray(variable2.internal.templateAst)) {
value = await interpolateWithResultDescriptor(variable2.internal.templateAst, env);
}
}
if (varRef.fields && varRef.fields.length > 0) {
const { accessFields } = await import('./field-access-MJ6PMBJX.mjs');
const accessed = await accessFields(value, varRef.fields, {
env,
preserveContext: false,
sourceLocation: varRef.location
});
value = accessed;
}
if (isStructuredValue(value)) {
argValueAny = value;
argValue = asText(value);
} else {
argValueAny = value;
if (value === void 0) {
argValue = "undefined";
} else if (typeof value === "object" && value !== null) {
try {
argValue = JSON.stringify(value);
} catch (e) {
argValue = String(value);
}
} else {
argValue = String(value);
}
}
} else {
argValue = await interpolateWithResultDescriptor([
arg
], env, InterpolationContext.Default);
argValueAny = argValue;
}
break;
case "load-content": {
const { processContentLoader } = await import('./content-loader-QADYRQX5.mjs');
const { wrapLoadContentValue } = await import('./load-content-structured-FVMWENVK.mjs');
const loadResult = await processContentLoader(arg, env);
const structured = wrapLoadContentValue(loadResult);
argValueAny = structured;
argValue = asText(structured);
break;
}
case "ExecInvocation": {
const nestedResult = await evaluateExecInvocation(arg, env);
if (nestedResult && nestedResult.value !== void 0) {
argValueAny = nestedResult.value;
} else if (nestedResult && nestedResult.stdout !== void 0) {
argValueAny = nestedResult.stdout;
} else {
argValueAny = void 0;
}
if (argValueAny === void 0) {
argValue = "undefined";
} else if (isStructuredValue(argValueAny)) {
argValue = asText(argValueAny);
} else if (typeof argValueAny === "object") {
try {
argValue = JSON.stringify(argValueAny);
} catch {
argValue = String(argValueAny);
}
} else {
argValue = String(argValueAny);
}
break;
}
case "Text":
argValue = await interpolateWithResultDescriptor([
arg
], env, InterpolationContext.Default);
argValueAny = argValue;
break;
default:
argValue = await interpolateWithResultDescriptor([
arg
], env, InterpolationContext.Default);
try {
argValueAny = JSON.parse(argValue);
} catch {
argValueAny = argValue;
}
break;
}
} else {
argValue = String(arg);
argValueAny = arg;
}
evaluatedArgStrings.push(argValue);
evaluatedArgs.push(argValueAny);
}
if (process.env.MLLD_DEBUG_FIX === "true") {
console.error("[evaluateExecInvocation] evaluated args", {
commandName,
argCount: evaluatedArgs.length,
argTypes: evaluatedArgs.map((a) => a === null ? "null" : Array.isArray(a) ? "array" : typeof a),
argPreview: evaluatedArgs.map((a) => {
if (isStructuredValue(a)) return {
structured: true,
type: a.type,
dataType: typeof a.data
};
if (a && typeof a === "object") return {
keys: Object.keys(a).slice(0, 5)
};
return a;
})
});
}
const originalVariables = new Array(args.length);
const guardVariableCandidates = new Array(args.length);
const expressionSourceVariables = new Array(args.length);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg && typeof arg === "object" && "type" in arg && arg.type === "VariableReference") {
const varRef = arg;
const varName = varRef.identifier;
const variable2 = env.getVariable(varName);
if (variable2 && !varRef.fields) {
const { isTemplate } = await import('./variable-FNPDYIEH.mjs');
if (isTemplate(variable2) && typeof evaluatedArgs[i] === "string") {
originalVariables[i] = void 0;
} else {
originalVariables[i] = variable2;
guardVariableCandidates[i] = variable2;
}
if (process.env.MLLD_DEBUG === "true") {
const subtype = variable2.type === "primitive" && "primitiveType" in variable2 ? variable2.primitiveType : variable2.subtype;
logger.debug(`Preserving original Variable for arg ${i}:`, {
varName,
variableType: variable2.type,
variableSubtype: subtype,
isPrimitive: typeof variable2.value !== "object" || variable2.value === null
});
}
}
} else if (arg && typeof arg === "object" && "type" in arg && arg.type === "ExecInvocation") {
const objectRef = arg.commandRef?.objectReference;
if (objectRef && typeof objectRef === "object" && objectRef.type === "VariableReference" && objectRef.identifier) {
const baseVariable = env.getVariable(objectRef.identifier);
if (baseVariable) {
expressionSourceVariables[i] = baseVariable;
}
}
}
}
const guardHelperImpl = variable.internal?.guardHelperImplementation;
if (variable.internal?.isGuardHelper && typeof guardHelperImpl === "function") {
const impl = guardHelperImpl;
const helperResult = await impl(evaluatedArgs);
return createEvalResult(helperResult, env);
}
for (let i = 0; i < args.length; i++) {
if (!guardVariableCandidates[i] && expressionSourceVariables[i]) {
const source = expressionSourceVariables[i];
const cloned = cloneVariableWithNewValue(source, evaluatedArgs[i], evaluatedArgStrings[i]);
guardVariableCandidates[i] = cloned;
}
}
const guardInputsWithMapping = materializeGuardInputsWithMapping(Array.from({
length: guardVariableCandidates.length
}, (_unused, index) => guardVariableCandidates[index] ?? evaluatedArgs[index]), {
nameHint: "__guard_input__"
});
const guardInputs = guardInputsWithMapping.map((entry) => entry.variable);
let postHookInputs = guardInputs;
const hookManager = env.getHookManager();
const execDescriptor = getVariableSecurityDescriptor(variable);
const operationContext = {
type: "exe",
name: variable.name ?? commandName,
labels: execDescriptor?.labels,
location: node.location ?? null,
metadata: {
executableType: definition.type,
command: commandName,
sourceRetryable: true
}
};
const finalizeResult = /* @__PURE__ */ __name(async (result) => {
try {
return await hookManager.runPost(node, result, postHookInputs, env, operationContext);
} catch (error) {
if (whenExprNode) {
const handled = await handleExecGuardDenial(error, {
execEnv,
env,
whenExprNode
});
if (handled) {
return handled;
}
}
throw error;
}
}, "finalizeResult");
return await env.withOpContext(operationContext, async () => {
return AutoUnwrapManager.executeWithPreservation(async () => {
const preDecision = await hookManager.runPre(node, guardInputs, env, operationContext);
const transformedGuardInputs = getGuardTransformedInputs(preDecision, guardInputs);
const transformedGuardSet = transformedGuardInputs && transformedGuardInputs.length > 0 ? new Set(transformedGuardInputs) : null;
if (transformedGuardInputs && transformedGuardInputs.length > 0) {
postHookInputs = transformedGuardInputs;
applyGuardTransformsToExecArgs({
guardInputEntries: guardInputsWithMapping,
transformedInputs: transformedGuardInputs,
guardVariableCandidates,
evaluatedArgs,
evaluatedArgStrings
});
}
for (let i = 0; i < params.length; i++) {
const paramName = params[i];
const argValue = evaluatedArgs[i];
const argStringValue = evaluatedArgStrings[i];
const originalVar = originalVariables[i];
const guardCandidate = guardVariableCandidates[i];
const isShellCode = definition.type === "code" && typeof definition.language === "string" && (definition.language === "bash" || definition.language === "sh");
const preferGuardReplacement = transformedGuardSet?.has(guardCandidate) ?? false;
const allowOriginalReuse = !preferGuardReplacement && Boolean(originalVar) && !isShellCode && definition.type !== "command";
if (guardCandidate && (!originalVar || !allowOriginalReuse || preferGuardReplacement)) {
const candidateClone = cloneGuardCandidateForParameter(paramName, guardCandidate, argValue, argStringValue);
execEnv.setParameterVariable(paramName, candidateClone);
continue;
}
if (argValue !== void 0) {
const paramVar = createParameterVariable({
name: paramName,
value: evaluatedArgs[i],
stringValue: argStringValue,
originalVariable: originalVar,
allowOriginalReuse,
metadataFactory: createParameterMetadata,
origin: "exec-param"
});
if (paramVar) {
execEnv.setParameterVariable(paramName, paramVar);
}
}
}
const descriptorPieces = [];
const variableDescriptor = getVariableSecurityDescriptor(variable);
if (variableDescriptor) {
descriptorPieces.push(variableDescriptor);
}
const mergedParamDescriptor = collectAndMergeParameterDescriptors(params, execEnv);
if (mergedParamDescriptor) {
descriptorPieces.push(mergedParamDescriptor);
}
if (descriptorPieces.length > 0) {
resultSecurityDescriptor = descriptorPieces.length === 1 ? descriptorPieces[0] : env.mergeSecurityDescriptors(...descriptorPieces);
env.recordSecurityDescriptor(resultSecurityDescriptor);
}
const guardInputVariable = preDecision && preDecision.metadata && preDecision.metadata.guardInput;
try {
await handleGuardDecision(preDecision, node, env, operationContext);
} catch (error) {
if (guardInputVariable) {
const existingInput = execEnv.getVariable("input");
if (!existingInput) {
const clonedInput = {
...guardInputVariable,
name: "input",
mx: {
...guardInputVariable.mx
},
internal: {
...guardInputVariable.internal ?? {},
isSystem: true,
isParameter: true
}
};
execEnv.setParameterVariable("input", clonedInput);
}
}
if (whenExprNode) {
const handled = await handleExecGuardDenial(error, {
execEnv,
env,
whenExprNode
});
if (handled) {
return finalizeResult(handled);
}
}
throw error;
}
let result;
let workingDirectory;
if ("workingDir" in definition && definition.workingDir) {
workingDirectory = await resolveWorkingDirectory(definition.workingDir, execEnv, {
sourceLocation: node.location ?? void 0,
directiveType: "exec"
});
}
if (isTemplateExecutable(definition)) {
const templateResult = await interpolateWithResultDescriptor(definition.template, execEnv);
if (isStructuredValue(templateResult)) {
result = templateResult;
} else if (typeof templateResult === "string") {
const parsed = parseAndWrapJson(templateResult, {
metadata: resultSecurityDescriptor ? {
security: resultSecurityDescriptor
} : void 0,
preserveText: true
});
result = parsed ?? templateResult;
} else {
result = templateResult;
}
if (!isStructuredValue(result) && result && typeof result === "object") {
const templateType = Array.isArray(result) ? "array" : "object";
const metadata = resultSecurityDescriptor ? {
security: resultSecurityDescriptor
} : void 0;
result = wrapStructured(result, templateType, void 0, metadata);
}
const templateWithClause = definition.withClause;
if (templateWithClause) {
if (templateWithClause.pipeline && templateWithClause.pipeline.length > 0) {
const { processPipeline } = await import('./unified-processor-GTJC5G2K.mjs');
const pipelineInputValue = toPipelineInput(result);
const pipelineResult = await processPipeline({
value: pipelineInputValue,
env: execEnv,
pipeline: templateWithClause.pipeline,
format: templateWithClause.format,
isRetryable: false,
identifier: commandName,
location: node.location,
descriptorHint: resultSecurityDescriptor
});
result = pipelineResult;
} else {
const withClauseResult = await applyWithClause(result, templateWithClause, execEnv);
result = withClauseResult.value ?? withClauseResult;
}
}
} else if (isDataExecutable(definition)) {
const { evaluateDataValue } = await import('./data-value-evaluator-6G4NQGOF.mjs');
const dataValue = await evaluateDataValue(definition.dataTemplate, execEnv);
const text = typeof dataValue === "string" ? dataValue : JSON.stringify(dataValue);
const dataDescriptor = extractSecurityDescriptor(dataValue, {
recursive: true,
mergeArrayElements: true
});
const mergedDescriptor = dataDescriptor && resultSecurityDescriptor ? execEnv.mergeSecurityDescriptors(dataDescriptor, resultSecurityDescriptor) : dataDescriptor || resultSecurityDescriptor || void 0;
result = wrapStructured(dataValue, Array.isArray(dataValue) ? "array" : "object", text, mergedDescriptor ? {
security: mergedDescriptor
} : void 0);
} else if (isPipelineExecutable(definition)) {
const { processPipeline } = await import('./unified-processor-GTJC5G2K.mjs');
const pipelineInputValue = evaluatedArgs.length > 0 ? toPipelineInput(evaluatedArgs[0]) : "";
const pipelineResult = await processPipeline({
value: pipelineInputValue,
env: execEnv,
pipeline: definition.pipeline,
format: definition.format,
identifier: commandName,
location: node.location,
isRetryable: false,
descriptorHint: resultSecurityDescriptor
});
result = typeof pipelineResult === "string" ? pipelineResult : String(pipelineResult ?? "");
} else if (isCommandExecutable(definition)) {
const referencedInTemplate = /* @__PURE__ */ new Set();
try {
const nodes = definition.commandTemplate;
if (Array.isArray(nodes)) {
for (const n of nodes) {
if (n && typeof n === "object" && n.type === "VariableReference" && typeof n.identifier === "string") {
referencedInTemplate.add(n.identifier);
} else if (n && typeof n === "object" && n.type === "Text" && typeof n.content === "string") {
for (const pname of params) {
const re = new RegExp(`@${pname}(?![A-Za-z0-9_])`);
if (re.test(n.content)) {
referencedInTemplate.add(pname);
}
}
}
}
}
} catch {
}
let command = await interpolateWithResultDescriptor(definition.commandTemplate, execEnv, InterpolationContext.ShellCommand);
if (process.env.DEBUG_WHEN || process.env.DEBUG_EXEC) {
logger.debug("Executing command", {
command,
commandTemplate: definition.commandTemplate
});
}
const envVars = {};
const escapeRegex = /* @__PURE__ */ __name((s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "escapeRegex");
const paramRegexCache = {};
const referencesParam = /* @__PURE__ */ __name((cmd, name) => {
if (referencedInTemplate.has(name)) return true;
if (!paramRegexCache[name]) {
const n = escapeRegex(name);
paramRegexCache[name] = {
simple: new RegExp(`(^|[^\\\\])\\$${n}(?![A-Za-z0-9_])`),
braced: new RegExp(`\\$\\{${n}\\}`)
};
}
const { simple, braced } = paramRegexCache[name];
return simple.test(cmd) || braced.test(cmd);
}, "referencesParam");
for (let i = 0; i < params.length; i++) {
const paramName = params[i];
if (!referencesParam(command, paramName)) continue;
const paramVar = execEnv.getVariable(paramName);
if (paramVar && typeof paramVar.value === "object" && paramVar.value !== null) {
try {
envVars[paramName] = JSON.stringify(paramVar.value);
} catch {
envVars[paramName] = evaluatedArgStrings[i];
}
} else {
envVars[paramName] = evaluatedArgStrings[i];
}
}
const perVarMax = (() => {
const v = process.env.MLLD_MAX_SHELL_ENV_VAR_SIZE;
if (!v) return 128 * 1024;
const n = Number(v);
return Number.isFinite(n) && n > 0 ? Math.floor(n) : 128 * 1024;
})();
const needsBashFallback = Object.values(envVars).some((v) => Buffer.byteLength(v || "", "utf8") > perVarMax);
const fallbackDisabled = (() => {
const v = (process.env.MLLD_DISABLE_COMMAND_BASH_FALLBACK || "").toLowerCase();
return v === "1" || v === "true" || v === "yes" || v === "on";
})();
if (needsBashFallback && !fallbackDisabled) {
let fallbackCommand = "";
try {
const nodes = definition.commandTemplate;
if (Array.isArray(nodes)) {
for (const n of nodes) {
if (n && typeof n === "object" && n.type === "VariableReference" && typeof n.identifier === "string" && params.includes(n.identifier)) {
fallbackCommand += `"$${n.identifier}"`;
} else if (n && typeof n === "object" && "content" in n) {
fallbackCommand += String(n.content || "");
} else if (typeof n === "string") {
fallbackCommand += n;
} else {
fallbackCommand += await interpolateWithResultDescriptor([
n
], execEnv, InterpolationContext.ShellCommand);
}
}
} else {
fallbackCommand = command;
}
} catch {
fallbackCommand = command;
}
try {
CommandUtils.validateAndParseCommand(fallbackCommand);
} catch (error) {
throw new MlldCommandExecutionError(error instanceof Error ? error.message : String(error), context?.sourceLocation, {
command: fallbackCommand,
exitCode: 1,
duration: 0,
stderr: error instanceof Error ? error.message : String(error),
workingDirectory: execEnv.getProjectRoot?.() || "",
directiveType: context?.directiveType || "run"
});
}
const codeParams = {};
for (let i = 0; i < params.length; i++) {
const paramName = params[i];
if (!referencesParam(command, paramName)) continue;
codeParams[paramName] = evaluatedArgs[i];
}
if (process.env.MLLD_DEBUG === "true") {
console.error("[exec-invocation] Falling back to bash heredoc for oversized command params", {
fallbackSnippet: fallbackCommand.slice(0, 120),
paramCount: Object.keys(codeParams).length
});
}
const commandOutput = await execEnv.executeCode(fallbackCommand, "sh", codeParams, void 0, workingDirectory ? {
workingDirectory
} : void 0, {
directiveType: "exec",
sourceLocation: node.location,
workingDirectory
});
if (typeof commandOutput === "string") {
const parsed = parseAndWrapJson(commandOutput);
result = parsed ?? commandOutput;
} else {
result = commandOutput;
}
} else {
let stdinInput;
if (definition.withClause && "stdin" in definition.withClause) {
stdinInput = await resolveStdinInput(definition.withClause.stdin, execEnv);
}
const commandOptions = stdinInput !== void 0 ? {
env: envVars,
input: stdinInput
} : {
env: envVars
};
if (workingDirectory) {
commandOptions.workingDirectory = workingDirectory;
}
const commandOutput = await execEnv.executeCommand(command, commandOptions, {
directiveType: "exec",
streamingEnabled,
pipelineId,
stageIndex: 0,
sourceLocation: node.location,
emitEffect: chunkEffect,
workingDirectory,
suppressTerminal: hasStreamFormat || streamingOptions.suppressTerminal === true
});
if (typeof commandOutput === "string") {
const parsed = parseAndWrapJson(commandOutput);
result = parsed ?? commandOutput;
} else {
result = commandOutput;
}
}
if (definition.withClause) {
if (definition.withClause.pipeline && definition.withClause.pipeline.length > 0) {
const { processPipeline } = await import('./unified-processor-GTJC5G2K.mjs');
const pipelineInput = typeof result === "string" ? result : result === void 0 || result === null ? "" : isStructuredValue(result) ? asText(result) : JSON.stringify(result);
const pipelineResult = await processPipeline({
value: pipelineInput,
env: execEnv,
pipeline: definition.withClause.pipeline,
format: definition.withClause.format,
isRetryable: false,
identifier: commandName,
location: variable.mx?.definedAt || node.location,
descriptorHint: resultSecurityDescriptor
});
if (typeof pipelineResult === "string") {
const parsed = parseAndWrapJson(pipelineResult);
result = parsed ?? pipelineResult;
} else {
result = pipelineResult;
}
}
}
} else if (isCodeExecutable(definition)) {
if (definition.language === "mlld-when") {
const activeWhenExpr = whenExprNode;
if (!activeWhenExpr) {
throw new MlldInterpreterError("mlld-when executable missing WhenExpression node");
}
const { evaluateWhenExpression } = await import('./when-expression-ZW53U2K2.mjs');
let whenResult;
try {
whenResult = await evaluateWhenExpression(activeWhenExpr, execEnv);
} catch (error) {
const handled = await handleExecGuardDenial(error, {
execEnv,
env,
whenExprNode: activeWhenExpr
});
if (handled) {
const finalHandled = await finalizeResult(handled);
return finalHandled;
}
throw error;
}
const normalization = normalizeWhenShowEffect(whenResult.value);
result = normalization.normalized;
execEnv = whenResult.env;
} else if (definition.language === "mlld-foreach") {
const foreachNode = definition.codeTemplate[0];
const { evaluateForeachCommand } = await import('./foreach-USOCOKPZ.mjs');
result = await evaluateForeachCommand(foreachNode, execEnv);
} else if (definition.language === "mlld-for") {
const forExprNode = definition.codeTemplate[0];
if (!forExprNode || forExprNode.type !== "ForExpression") {
throw new MlldInterpreterError("mlld-for executable missing ForExpression node");
}
const { evaluateForExpression } = await import('./for-FC7J7F6X.mjs');
result = await evaluateForExpression(forExprNode, execEnv);
} else if (definition.language === "mlld-exe-block") {
const blockNode = Array.isArray(definition.codeTemplate) ? definition.codeTemplate[0] : void 0;
if (!blockNode || !blockNode.values) {
throw new MlldInterpreterError("mlld-exe-block executable missing block content");
}
const blockResult = await evaluateExeBlock(blockNode, execEnv);
result = blockResult.value;
execEnv = blockResult.env;
} else {
let code;
if (definition.language === "bash" || definition.language === "sh") {
if (Array.isArray(definition.codeTemplate)) {
code = definition.codeTemplate.map((node2) => {
if (typeof node2 === "string") return node2;
if (node2 && typeof node2 === "object" && "content" in node2) return node2.content || "";
return "";
}).join("");
} else if (typeof definition.codeTemplate === "string") {
code = definition.codeTemplate;
} else {
code = "";
}
} else {
code = await interpolateWithResultDescriptor(definition.codeTemplate, execEnv);
}
const { ASTEvaluator } = await import('./ast-evaluator-TR7L7WV6.mjs');
const codeParams = {};
const variableMetadata = {};
for (let i = 0; i < params.length; i++) {
const paramName = params[i];
const paramVar = execEnv.getVariable(paramName);
if (process.env.MLLD_DEBUG === "true") {
logger.debug("Checking parameter:", {
paramName,
hasParamVar: !!paramVar,
paramVarType: paramVar?.type,
isPipelineInput: paramVar?.type === "pipeline-input"
});
}
if (paramVar && paramVar.type === "pipeline-input") {
codeParams[paramName] = paramVar.value;
} else if (paramVar) {
if (definition.language === "bash" || definition.language === "sh") {
const rawValue = paramVar.value;
if (typeof rawValue === "string") {
codeParams[paramName] = rawValue;
} else if (isStructuredValue(rawValue)) {
codeParams[paramName] = asText(rawValue);
} else {
codeParams[paramName] = prepareValueForShadow(paramVar);
}
} else {
if (env.shouldSuppressGuards() && paramVar.internal?.isSystem && paramVar.internal?.isParameter) {
const rawValue = isStructuredValue(paramVar.value) ? paramVar.value.data : paramVar.value;
codeParams[paramName] = rawValue;
} else {
let variableForShadow = paramVar;
if (paramVar.isComplex && paramVar.value && typeof paramVar.value === "object" && "type" in paramVar.value) {
const { extractVariableValue: extractVal } = await import('./variable-resolution-HFG3FTZK.mjs');
const resolvedValue = await extractVal(paramVar, execEnv);
const resolvedVar = {
...paramVar,
value: resolvedValue,
isComplex: false
};
variableForShadow = resolvedVar;
} else {
const unwrappedValue = AutoUnwrapManager.unwrap(paramVar.value);
if (unwrappedValue !== paramVar.value) {
const unwrappedVar = {
...paramVar,
value: unwrappedValue,
// Update type based on unwrapped value
type: Array.isArray(unwrappedValue) ? "array" : "text"
};
variableForShadow = unwrappedVar;
} else {
variableForShadow = paramVar;
}
}
codeParams[paramName] = variableForShadow;
}
}
if (definition.language !== "bash" && definition.language !== "sh" && (paramVar.value === null || typeof paramVar.value !== "object")) {
const subtype = paramVar.type === "primitive" && "primitiveType" in paramVar ? paramVar.primitiveType : paramVar.subtype;
variableMetadata[paramName] = {
type: paramVar.type,
subtype,
mx: paramVar.mx,
internal: paramVar.internal,
isVariable: true
};
}
if (process.env.DEBUG_EXEC || process.env.MLLD_DEBUG === "true") {
const subtype = paramVar.type === "primitive" && "primitiveType" in paramVar ? paramVar.primitiveType : paramVar.subtype;
logger.debug(`Variable passing for ${paramName}:`, {
variableType: paramVar.type,
variableSubtype: subtype,
hasInternal: !!paramVar.internal,
isPrimitive: paramVar.value === null || typeof paramVar.value !== "object",
language: definition.language
});
}
} else {
const argValue = evaluatedArgs[i];
codeParams[paramName] = await ASTEvaluator.evaluateToRuntime(argValue, execEnv);
if (process.env.DEBUG_EXEC) {
logger.debug(`Code parameter ${paramName}:`, {
argValue,
type: typeof argValue,
isNumber: typeof argValue === "number",
evaluatedArgs_i: evaluatedArgs[i],
evaluatedArgStrings_i: evaluatedArgStrings[i]
});
}
}
}
const capturedModuleEnv = variable.internal?.capturedModuleEnv;
if (capturedModuleEnv instanceof Map && (definition.language === "js" || definition.language === "javascript" || definition.language === "node" || definition.language === "nodejs")) {
for (const [capturedName, capturedVar] of capturedModuleEnv) {
if (codeParams[capturedName] !== void 0) {
continue;
}
if (params.includes(capturedName)) {
continue;
}
if (capturedVar.type === "executable") {
continue;
}
codeParams[capturedName] = capturedVar;
if ((capturedVar.value === null || typeof capturedVar.value !== "object") && capturedVar.type !== "executable") {
const subtype = capturedVar.type === "primitive" && "primitiveType" in capturedVar ? capturedVar.primitiveType : capturedVar.subtype;
variableMetadata[capturedName] = {
type: capturedVar.type,
subtype,
mx: capturedVar.mx,
isVariable: true
};
}
}
}
const capturedEnvs = variable.internal?.capturedShadowEnvs;
if (capturedEnvs && (definition.language === "js" || definition.language === "javascript" || definition.language === "node" || definition.language === "nodejs")) {
codeParams.__capturedShadowEnvs = capturedEnvs;
}
const codeResult = await execEnv.executeCode(code, definition.language || "javascript", codeParams, Object.keys(variableMetadata).length > 0 ? variableMetadata : void 0, workingDirectory ? {
workingDirectory
} : void 0, workingDirectory ? {
directiveType: "exec",
sourceLocation: node.location,
workingDirectory
} : {
directiveType: "exec",
sourceLocation: node.location
});
let processedResult;
if (typeof codeResult === "string" && (codeResult.startsWith('"') || codeResult.startsWith("{") || codeResult.startsWith("[") || codeResult === "null" || codeResult === "true" || codeResult === "false" || /^-?\d+(\.\d+)?$/.test(codeResult))) {
try {
const parsed = JSON.parse(codeResult);
processedResult = parsed;
} catch {
processedResult = codeResult;
}
} else {
processedResult = codeResult;
}
result = AutoUnwrapManager.restore(processedResult);
if (process.env.MLLD_DEBUG_STRUCTURED === "true" && result && typeof result === "object") {
try {
const debugData = result.data;
console.error("[exec-invocation] rehydrate candidate", {
hasType: "type" in result,
hasText: "text" in result,
dataType: typeof debugData,
dataKeys: debugData && typeof debugData === "object" ? Object.keys(debugData) : void 0
});
} catch {
}
}
if (result && typeof result === "object" && !isStructuredValue(result) && "type" in result && "text" in result && "data" in result) {
const payload = result.data;
result = wrapStructured(payload, result.type, result.text, result.metadata);
}
if (definition.withClause) {
if (definition.withClause.pipeline && definition.withClause.pipeline.length > 0) {
const { processPipeline } = await import('./unified-processor-GTJC5G2K.mjs');
const pipelineInput = toPipelineInput(result);
const pipelineResult = await processPipeline({
value: pipelineInput,
env: execEnv,
pipeline: definition.withClause.pipeline,
format: definition.withClause.format,
isRetryable: false,
identifier: commandName,
location: node.location,
descriptorHint: resultSecurityDescriptor
});
result = pipelineResult;
} else {
const withClauseResult = await applyWithClause(result, definition.withClause, execEnv);
result = withClauseResult.value ?? withClauseResult;
}
}
const inputDescriptors = Object.values(variableMetadata).map((meta) => getSecurityDescriptorFromCarrier(meta)).filter((descriptor) => Boolean(descriptor));
if (inputDescriptors.length > 0) {
const mergedInputDescriptor = inputDescriptors.length === 1 ? inputDescriptors[0] : env.mergeSecurityDescriptors(...inputDescriptors);
env.recordSecurityDescriptor(mergedInputDescriptor);
mergeResultDescriptor(mergedInputDescriptor);
}
}
} else if (isCommandRefExecutable(definition)) {
const refName = definition.commandRef;
if (!refName) {
throw new MlldInterpreterError(`Command reference ${commandName} has no target command`);
}
let refCommand = null;
if (variable?.internal?.capturedModuleEnv) {
const capturedEnv = variable.internal?.capturedModuleEnv;
if (capturedEnv instanceof Map) {
refCommand = capturedEnv.get(refName);
} else if (capturedEnv && typeof capturedEnv === "object") {
refCommand = capturedEnv[refName];
}
}
if (!refCommand) {
refCommand = env.getVariable(refName);
}
if (!refCommand) {
throw new MlldInterpreterError(`Referenced command not found: ${refName}`);
}
if (definition.commandArgs && definition.commandArgs.length > 0) {
if (process.env.MLLD_DEBUG === "true") {
try {
console.error("[EXEC INVOC] commandRef args shape:", definition.commandArgs.map((a) => Array.isArray(a) ? "array" : a && typeof a === "object" && a.type || typeof a));
} catch {
}
}
let refArgs = [];
const { evaluate } = await import('./interpreter-MW7QI3FC.mjs');
for (const argNode of definition.commandArgs) {
let value;
if (Array.isArray(argNode)) {
value = await interpolateWithResultDescriptor(argNode, execEnv, InterpolationContext.Default);
} else {
const argResult = await evaluate(argNode, execEnv, {
isExpression: true
});
value = argResult?.value;
}
if (typeof value === "string") {
const paramVar = execEnv.getVariable(value);
if (paramVar?.internal?.isParameter) {
value = isStructuredValue(paramVar.value) ? paramVar.value : paramVar.value;
}
}
if (value !== void 0) {
refArgs.push(value);
}
}
const refEnv = env.createChild();
if (variable?.internal?.capturedModuleEnv instanceof Map) {
const captured = variable.internal?.capturedModuleEnv;
if (captured instanceof Map) {
refEnv.setCapturedModuleEnv(captured);
}
}
const refInvocation = {
type: "ExecInvocation",
commandRef: {
identifier: refName,
args: refArgs
// Pass values directly like foreach does
},
// Pass along the pipeline if present
...definition.withClause ? {
withClause: definition.withClause
} : {}
};
const refResult = await evaluateExecInvocation(refInvocation, refEnv);
result = refResult.value;
} else {
const refEnv = env.createChild();
if (variable?.internal?.capturedModuleEnv instanceof Map) {
refEnv.setCapturedModuleEnv(variable.internal.capturedModuleEnv);
}
const refInvocation = {
type: "ExecInvocation",
commandRef: {
identifier: refName,
args: evaluatedArgs
// Pass values directly like foreach does
},
// Pass along the pipeline if present
...definition.withClause ? {
withClause: definition.withClause
} : {}
};
const refResult = await evaluateExecInvocation(refInvocation, refEnv);
result = refResult.value;
}
} else if (isSectionExecutable(definition)) {
const filePath = await interpolateWithResultDescriptor(definition.pathTemplate, execEnv);
const sectionName = await interpolateWithResultDescriptor(definition.sectionTemplate, execEnv);
const fileContent = await execEnv.readFile(filePath);
const llmxmlInstance = env.getLlmxml();
let sectionContent;
try {
const titleWithoutHash = sectionName.replace(/^#+\s*/, "");
sectionContent = await llmxmlInstance.getSection(fileContent, titleWithoutHash, {
includeNested: true
});
} catch (error) {
sectionContent = extractSection(fileContent, sectionName);
}
if (definition.renameTemplate) {
const newTitle = await interpolateWithResultDescriptor(definition.renameTemplate, execEnv);
const lines = sectionContent.split("\n");
if (lines.length > 0 && lines[0].match(/^#+\s/)) {
const newTitleTrimmed = newTitle.trim();
const newHeadingMatch = newTitleTrimmed.match(/^(#+)(\s+(.*))?$/);
if (newHeadingMatch) {
if (!newHeadingMatch[3]) {
const originalText = lines[0].replace(/^#+\s*/, "");
lines[0] = `${newHeadingMatch[1]} ${originalText}`;
} else {
lines[0] = newTitleTrimmed;
}
} else {
const originalLevel = lines[0].match(/^#+/)?.[0] || "#";
lines[0] = `${originalLevel} ${newTitleTrimmed}`;
}
sectionContent = lines.join("\n");
}
}
result = sectionContent;
} else if (isResolverExecutable(definition)) {
let resolverPath = definition.resolverPath;
for (let i = 0; i < params.length; i++) {
const paramName = params[i];
const argValue = evaluatedArgs[i];
if (argValue !== void 0) {
resolverPath = resolverPath.replace(new RegExp(`@${paramName}\\b`, "g"), argValue);
}
}
let payload = void 0;
if (definition.payloadTemplate) {
const payloadStr = await interpolateWithResultDescriptor(definition.payloadTemplate, execEnv);
try {
payload = JSON.parse(payloadStr);
} catch {
payload = payloadStr;
}
}
const resolverManager = env.getResolverManager();
if (!resolverManager) {
throw new MlldInterpreterError("Resolver manager not available");
}
const resolverResult = await resolverManager.resolve(resolverPath, {
context: "exec-invocation",
basePath: env.getBasePath(),
payload
});
if (resolverResult && typeof resolverResult === "object" && "content" in resolverResult) {
result = resolverResult.content;
} else if (typeof resolverResult === "string") {
result = resolverResult;
} else if (resolverResult && typeof resolverResult === "object") {
result = JSON.stringify(resolverResult, null, 2);
} else {
result = String(resolverResult);
}
} else {
throw new MlldInterpreterError(`Unknown executable type: ${definition.type}`);
}
const postFields = node.fields || [];
if (postFields && postFields.length > 0) {
try {
const { accessField } = await import('./field-access-MJ6PMBJX.mjs');
let current = result;
for (const f of postFields) {
current = await accessField(current, f, {
env,
sourceLocation: nodeSourceLocation
});
}
result = current;
} catch (e) {
throw e;
}
}
if (result && typeof result === "object") {
const { isVariable, extractVariableValue } = await import('./variable-resolution-HFG3FTZK.mjs');
if (isVariable(result)) {
const extracted = await extractVariableValue(result, execEnv);
const typeHint = Array.isArray(extracted) ? "array" : typeof extracted === "object" && extracted !== null ? "object" : "text";
const structured = wrapStructured(extracted, typeHint);
result = structured;
}
}
mergeResultDescriptor(extractSecurityDescriptor(result));
if (resultSecurityDescriptor) {
const structured = wrapExecResult(result);
const existing = getStructuredSecurityDescriptor(structured);
const merged = existing ? env.mergeSecurityDescriptors(existing, resultSecurityDescriptor) : resultSecurityDescriptor;
setStructuredSecurityDescriptor(structured, merged);
result = structured;
}
const cleanupShouldTrack = !builtinMethods.includes(commandName) && !(env.hasVariable(commandName) && env.getVariable(commandName)?.internal?.isReserved);
if (commandName && cleanupShouldTrack) {
env.endResolving(commandName);
}
if (process.env.MLLD_DEBUG_FIX === "true") {
try {
const summary = {
commandName,
type: typeof result,
isStructured: isStructuredValue(result),
keys: result && typeof result === "object" ? Object.keys(result).slice(0, 5) : void 0,
preview: isStructuredValue(result) && typeof result.data === "object" ? Object.keys(result.data || {}).slice(0, 5) : void 0,
text: isStructuredValue(result) && typeof result.text === "string" ? String(result.text).slice(0, 120) : void 0
};
console.error("[evaluateExecInvocation] result summary", summary);
if (commandName === "needsMeta" || commandName === "jsDefault" || commandName === "jsKeep" || commandName === "agentsContext") {
try {
fs.appendFileSync("/tmp/mlld-debug.log", JSON.stringify(summary) + "\n");
} catch {
}
}
} catch {
}
}
if (node.withClause) {
if (node.withClause.pipeline) {
if (process.env.MLLD_DEBUG === "true") {
console.error("[exec-invocation] Handling pipeline:", {
pipelineLength: node.withClause.pipeline.length,
stages: node.withClause.pipeline.map((p) => Array.isArray(p) ? "[parallel]" : p.rawIdentifier || "unknown")
});
}
const { executePipeline } = await import('./pipeline-6P6GNLL2.mjs');
const sourceFunction = /* @__PURE__ */ __name(async () => {
if (process.env.MLLD_DEBUG === "true") {
console.error("[exec-invocation] sourceFunction called - re-executing ExecInvocation");
}
const nodeWithoutPipeline = {
...node,
withClause: void 0
};
const freshResult = await evaluateExecInvocation(nodeWithoutPipeline, execEnv);
return wrapExecResult(freshResult.value);
}, "sourceFunction");
const SOURCE_STAGE = {
rawIdentifier: "__source__",
identifier: [],
args: [],
fields: [],
rawArgs: []
};
let normalizedPipeline = [
SOURCE_STAGE,
...node.withClause.pipeline
];
try {
const { attachBuiltinEffects } = await import('./effects-attachment-AJ6HEGO3.mjs');
const { functionalPipeline } = attachBuiltinEffects(normalizedPipeline);
normalizedPipeline = functionalPipeline;
} catch {
}
if (process.env.MLLD_DEBUG === "true") {
console.error("[exec-invocation] Creating pipeline with synthetic source:", {
originalLength: node.withClause.pipeline.length,
normalizedLength: normalizedPipeline.length,
stages: normalizedPipeline.map((p) => Array.isArray(p) ? "[parallel]" : p.rawIdentifier || "unknown")
});
}
const pipelineInput = wrapExecResult(result);
const pipelineResult = await executePipeline(pipelineInput, normalizedPipeline, execEnv, node.location, node.withClause.format, true, sourceFunction, true, void 0, void 0, {
returnStructured: true
});
let pipelineValue = wrapPipelineResult(pipelineResult);
const pipelineDescriptor = getStructuredSecurityDescriptor(pipelineValue);
const combinedDescriptor = pipelineDescriptor ? resultSecurityDescriptor ? env.mergeSecurityDescriptors(pipelineDescriptor, resultSecurityDescriptor) : pipelineDescriptor : resultSecurityDescriptor;
if (combinedDescriptor) {
setStructuredSecurityDescriptor(pipelineValue, combinedDescriptor);
mergeResultDescriptor(combinedDescriptor);
}
const withClauseResult = await applyWithClause(pipelineValue, {
...node.withClause,
pipeline: void 0
}, execEnv);
const finalWithClauseResult = await finalizeResult(withClauseResult);
return finalWithClauseResult;
} else {
const withClauseResult = await applyWithClause(result, node.withClause, execEnv);
const finalWithClauseResult = await finalizeResult(withClauseResult);
return finalWithClauseResult;
}
}
if (process.env.MLLD_DEBUG === "true") {
try {
console.log("[exec-invocation] returning result", {
commandName,
typeofResult: typeof result,
isArrayResult: Array.isArray(result)
});
} catch {
}
}
const finalEvalResult = await finalizeResult(createEvalResult(result, execEnv));
return finalEvalResult;
});
});
} finally {
if (commandName) {
const wasTracked = !builtinMethods.includes(commandName) && !(env.hasVariable(commandName) && env.getVariable(commandName)?.internal?.isReserved);
if (wasTracked) {
env.endResolving(commandName);
}
}
const finalizedStreaming = streamingManager.finalizeResults();
env.setStreamingResult(finalizedStreaming.streaming);
}
}
__name(evaluateExecInvocationInternal, "evaluateExecInvocationInternal");
function getVariableSecurityDescriptor(variable) {
if (!variable) {
return void 0;
}
return getSecurityDescriptorFromCarrier({
mx: variable.mx
});
}
__name(getVariableSecurityDescriptor, "getVariableSecurityDescriptor");
function getStructuredSecurityDescriptor(value) {
return getSecurityDescriptorFromCarrier(value);
}
__name(getStructuredSecurityDescriptor, "getStructuredSecurityDescriptor");
function setStructuredSecurityDescriptor(value, descriptor) {
if (!descriptor || !value || typeof value !== "object") {
return;
}
applySecurityDescriptorToStructuredValue(value, descriptor);
}
__name(setStructuredSecurityDescriptor, "setStructuredSecurityDescriptor");
function getSecurityDescriptorFromCarrier(carrier) {
if (!carrier) {
return void 0;
}
return descriptorFromVarMx(carrier.mx);
}
__name(getSecurityDescriptorFromCarrier, "getSecurityDescriptorFromCarrier");
function descriptorFromVarMx(mx) {
if (!mx) {
return void 0;
}
const labels = Array.isArray(mx.labels) ? mx.labels : [];
const sources = Array.isArray(mx.sources) ? mx.sources : [];
const taint = mx.taint ?? "unknown";
if (labels.length === 0 && sources.length === 0 && taint === "unknown") {
return void 0;
}
return varMxToSecurityDescriptor(mx);
}
__name(descriptorFromVarMx, "descriptorFromVarMx");
function deserializeShadowEnvs(envs) {
const result = {};
for (const [lang, shadowObj] of Object.entries(envs)) {
if (shadowObj && typeof shadowObj === "object") {
const map = /* @__PURE__ */ new Map();
for (const [name, func] of Object.entries(shadowObj)) {
map.set(name, func);
}
result[lang] = map;
}
}
return result;
}
__name(deserializeShadowEnvs, "deserializeShadowEnvs");
export { evaluateExecInvocation };
//# sourceMappingURL=chunk-KZFFCWXR.mjs.map
//# sourceMappingURL=chunk-KZFFCWXR.mjs.map