export const SENTINEL_KILL_ERROR_PREFIX = 'Run killed by Sentinel:';
export function parsePolicyFromKillReason(reason) {
    const match = reason.match(/^policy_violation:([^:]+)$/);
    return match?.[1];
}
export function buildSentinelFailureOutput(reason) {
    const policy = parsePolicyFromKillReason(reason);
    return {
        error: 'Run terminated by Sentinel',
        source: 'sentinel',
        action: 'deny',
        reason,
        ...(policy !== undefined && { policy }),
    };
}
export function buildSentinelFailureLog(reason) {
    return `Sentinel denied run: ${reason}`;
}
export function formatSentinelKillError(reason) {
    return `${SENTINEL_KILL_ERROR_PREFIX} ${reason}`;
}
export function extractSentinelKillReason(errorMessage) {
    if (!errorMessage.startsWith(SENTINEL_KILL_ERROR_PREFIX)) {
        return null;
    }
    const reason = errorMessage.slice(SENTINEL_KILL_ERROR_PREFIX.length).trim();
    return reason.length > 0 ? reason : null;
}
export function isSentinelKillError(errorMessage) {
    return extractSentinelKillReason(errorMessage) !== null;
}
export function appendRunLog(existing, entry) {
    return [...(existing ?? []), entry];
}
export function emitSentinelKilledSse(connection, runId, reason) {
    const output = buildSentinelFailureOutput(reason);
    connection.write(`event: end\ndata: ${JSON.stringify({
        type: 'run_killed',
        runId,
        status: 'killed',
        error: output.error,
        output,
        timestamp: new Date().toISOString(),
    })}\n\n`);
}
//# sourceMappingURL=sentinel-run-failure.js.map