import { BestAgentError } from "@best-agent/foundation";
import { TRACE_PREVIEW_LIMIT, TRACE_TOOL_OUTPUT_LIMIT } from "./runtime-limits.js";
export function shouldPersistToolTrace(toolName, output, executionClass) {
    if (output.length > TRACE_TOOL_OUTPUT_LIMIT) {
        return true;
    }
    return (executionClass === "read-only" && toolName === "shell" && output.length > TRACE_PREVIEW_LIMIT);
}
export function createTracePreview(content, summary) {
    if (content.length <= TRACE_PREVIEW_LIMIT) {
        return content;
    }
    return `${content.slice(0, TRACE_PREVIEW_LIMIT)}\n\n[truncated] ${summary}`;
}
export function buildTraceArtifactName(prefix) {
    return `${sanitizeTraceName(prefix)}-${Date.now()}-${Math.random().toString(16).slice(2, 8)}.txt`;
}
export function renderRuntimeFailureTrace(error, message) {
    if (!(error instanceof Error) || typeof error.stack !== "string") {
        return message;
    }
    if (error instanceof BestAgentError) {
        return message;
    }
    const stack = error.stack.trim();
    if (!stack || stack === message) {
        return message;
    }
    return stack.includes(message) ? stack : `${message}\n\n${stack}`;
}
export function createAbortError(message) {
    const error = new Error(message);
    error.name = "AbortError";
    return error;
}
function sanitizeTraceName(value) {
    return value.replace(/[^a-zA-Z0-9._-]+/g, "-");
}
//# sourceMappingURL=runtime-trace.js.map