package com.correx.core.kernel.orchestration

import com.correx.core.artifacts.kind.KindContractTable
import com.correx.core.artifacts.kind.KindInference
import com.correx.core.events.types.SessionId
import com.correx.core.events.types.StageId

/**
 * Recovers the produced kind from this stage's recorded file manifest, then maps it to the
 * toolchain-specific build command. File writes are event facts, so this remains replay-safe.
 */
internal fun SessionOrchestrator.stageProducedToolchain(
    sessionId: SessionId,
    stageId: StageId,
): KindContractTable.Toolchain? = toolchainForPaths(stageWrittenPaths(sessionId, stageId))

internal fun toolchainForPaths(paths: List<String>): KindContractTable.Toolchain? =
    paths.asReversed().firstNotNullOfOrNull { path ->
        KindInference.kindFor(path)?.let(KindContractTable::toolchainFor)
    }
