"use strict";
// src/orchestrator/workflow/CatalogStepExecutor.ts
// Phase 5 of catalog→execution: a real WorkflowStepExecutor that runs each
// step through the agent it names (via CatalogAgentExecutor), instead of the
// SimulatedStepExecutor that reports every step "completed" without doing work.
//
// Resolution + graceful degradation:
//   - elicit step            → awaiting_input (engine pauses for human)
//   - step names an agent     → run it; map ok→completed, fail→failed
//   - agent can't be resolved → completed-as-structural (note recorded, the
//                               workflow keeps progressing rather than dying)
//   - no agent / no squad     → completed-as-structural (marker/file step)
Object.defineProperty(exports, "__esModule", { value: true });
exports.CatalogStepExecutor = void 0;
class CatalogStepExecutor {
    agentExecutor;
    constructor(agentExecutor) {
        this.agentExecutor = agentExecutor;
    }
    async execute(inv) {
        const step = inv.step;
        const artifacts = [
            ...(Array.isArray(step.creates) ? step.creates : []),
            ...(Array.isArray(step.updates) ? step.updates : []),
        ];
        // Human-input steps pause the engine, same as the simulated executor.
        if (step.elicit) {
            return { status: 'awaiting_input' };
        }
        // Steps with no named agent are structural (phase markers, file ops).
        const agentId = step.agent;
        if (!agentId) {
            return {
                status: 'completed',
                artifacts,
                result: JSON.stringify({ structural: true, stepId: step.id, squad: step.squad }),
            };
        }
        // Resolve + run the real agent.
        const def = this.agentExecutor.loadDefinition(agentId);
        if (!def) {
            // Unresolvable agent — don't kill the run; record it and move on.
            return {
                status: 'completed',
                artifacts,
                result: JSON.stringify({ skipped: 'agent_not_found', stepId: step.id, agent: agentId }),
            };
        }
        const task = this.buildTask(step, inv.context);
        const res = await this.agentExecutor.execute(agentId, task);
        if (!res.ok) {
            return { status: 'failed', artifacts, error: res.error };
        }
        return {
            status: 'completed',
            artifacts,
            result: JSON.stringify({ agent: agentId, stepId: step.id, output: res.output }),
        };
    }
    /** Compose the task prompt for the agent from the step + run context. */
    buildTask(step, context) {
        const parts = [];
        if (step.action)
            parts.push(`Action: ${step.action}`);
        if (Array.isArray(step.creates) && step.creates.length)
            parts.push(`Produce: ${step.creates.join(', ')}`);
        if (Array.isArray(step.updates) && step.updates.length)
            parts.push(`Update: ${step.updates.join(', ')}`);
        const ctxKeys = context ? Object.keys(context) : [];
        if (ctxKeys.length) {
            const ctxStr = JSON.stringify(context);
            parts.push(`Workflow context: ${ctxStr.length > 600 ? ctxStr.slice(0, 600) + '…' : ctxStr}`);
        }
        return parts.join('\n') || `Execute workflow step "${step.id}".`;
    }
}
exports.CatalogStepExecutor = CatalogStepExecutor;
