function asRecord(value) {
    return value && typeof value === 'object' ? value : undefined;
}
function asString(value) {
    return typeof value === 'string' && value.length > 0 ? value : undefined;
}
function asNumber(value) {
    return typeof value === 'number' ? value : undefined;
}
function extractObservedFacts(journal) {
    const writtenFiles = new Set();
    const updatedFiles = new Set();
    const n8nacActions = [];
    for (const entry of journal) {
        if (entry.type !== 'step' || !entry.step) {
            continue;
        }
        const step = entry.step;
        for (let index = 0; index < step.toolCalls.length; index += 1) {
            const toolCall = step.toolCalls[index];
            const toolResult = step.toolResults[index];
            const args = asRecord(toolCall.args);
            const result = asRecord(toolResult?.result);
            if (toolCall.toolName === 'writeWorkspaceFile') {
                const filePath = asString(result?.path) ?? asString(args?.path);
                if (filePath) {
                    writtenFiles.add(filePath);
                }
                continue;
            }
            if (toolCall.toolName === 'replaceInWorkspaceFile') {
                const filePath = asString(result?.path) ?? asString(args?.path);
                if (filePath) {
                    updatedFiles.add(filePath);
                }
                continue;
            }
            if (toolCall.toolName === 'n8nac') {
                const action = asString(args?.action) ?? 'unknown';
                if (action === 'setup_check') {
                    continue;
                }
                n8nacActions.push({
                    action,
                    success: (asNumber(result?.exitCode) ?? 1) === 0,
                    filename: asString(args?.filename),
                    workflowId: asString(args?.workflowId),
                    validateFile: asString(args?.validateFile),
                    exitCode: asNumber(result?.exitCode),
                });
            }
        }
    }
    return {
        writtenFiles: [...writtenFiles],
        updatedFiles: [...updatedFiles],
        n8nacActions,
    };
}
function findSuccessfulAction(actions, actionName) {
    return actions.find((action) => action.action === actionName && action.success);
}
function actionKey(action) {
    return `${action.action}::${action.filename ?? action.validateFile ?? action.workflowId ?? ''}`;
}
export function formatObservedAction(action) {
    const target = action.filename ?? action.validateFile ?? action.workflowId;
    return target ? `${action.action} (${target})` : action.action;
}
export function analyzeRunOutcome(journal) {
    const facts = extractObservedFacts(journal);
    const successfulActions = facts.n8nacActions.filter((action) => action.success);
    const failedActions = facts.n8nacActions.filter((action) => !action.success);
    const resolvedFailureKeys = new Set(successfulActions.map(actionKey));
    const unresolvedFailedActions = failedActions.filter((action) => !resolvedFailureKeys.has(actionKey(action)));
    const hasWorkflowWrites = [...facts.writtenFiles, ...facts.updatedFiles].some((filePath) => filePath.endsWith('.workflow.ts'));
    const successfulPush = findSuccessfulAction(facts.n8nacActions, 'push');
    const successfulValidate = findSuccessfulAction(facts.n8nacActions, 'validate') ?? successfulPush;
    const successfulVerify = findSuccessfulAction(facts.n8nacActions, 'verify') ?? successfulPush;
    return {
        writtenFiles: facts.writtenFiles,
        updatedFiles: facts.updatedFiles,
        successfulActions,
        failedActions,
        unresolvedFailedActions,
        successfulValidate,
        successfulPush,
        successfulVerify,
        hasWorkflowWrites,
    };
}
//# sourceMappingURL=outcome.js.map