// engine/telemetry/error-reporter.ts — SPEC-276: Remote error reporting for tool failures.
// SPEC-327: Distinguishes tool_blocked (gate/validation blocks) from tool_error (exceptions).
// Fire-and-forget. Sends sanitized error info to Supabase on unhandled tool exceptions.
// Opt-out: set PLANU_TELEMETRY=off to disable remote reporting.
// No PII: only tool name, sanitized error message, version, and Node version are sent.
import { sendTelemetryEvent } from './telemetry-client.js';
import { PLANU_VERSION } from '../../config/version.js';
/**
 * Returns true when remote error reporting is active (default: on).
 * Disabled by setting env var PLANU_TELEMETRY=off.
 */
export function isErrorReportingEnabled() {
    return process.env.PLANU_TELEMETRY !== 'off';
}
/**
 * Sanitizes an error message by removing filesystem paths and home-dir fragments
 * so no user-specific data is transmitted.
 */
export function sanitizeErrorMessage(message) {
    return message
        .replace(/\/Users\/[^\s/]+/g, '/[user]')
        .replace(/\/home\/[^\s/]+/g, '/[user]')
        .replace(/C:\\Users\\[^\s\\]+/g, 'C:\\[user]')
        .slice(0, 300);
}
/** Patterns in error messages that indicate a gate/policy block (not an exception). */
const BLOCKED_PATTERNS = [
    'Blocked:',
    'is locked',
    'is already',
    'Invalid transition',
    'actuals_required',
    'does not meet',
];
/**
 * Classifies a telemetry event as tool_blocked or tool_error.
 * Gate blocks (DoD, validate score, spec locked, invalid transition) → tool_blocked.
 * Unhandled exceptions → tool_error.
 */
export function classifyToolEvent(errorMessage, errorType) {
    if (errorType === 'validation' || errorType === 'gate') {
        return 'tool_blocked';
    }
    if (BLOCKED_PATTERNS.some((pattern) => errorMessage.includes(pattern))) {
        return 'tool_blocked';
    }
    return 'tool_error';
}
/**
 * Sends a tool_error event to Supabase. Fire-and-forget — never throws.
 * Call this from safe-handler catch blocks after returning isError:true to the user.
 */
export function reportToolError(toolName, error) {
    if (!isErrorReportingEnabled()) {
        return;
    }
    const rawMessage = error instanceof Error ? error.message : String(error);
    const message = sanitizeErrorMessage(rawMessage);
    sendTelemetryEvent({
        event: 'tool_error',
        properties: {
            tool: toolName,
            message,
            errorType: 'exception',
            planVersion: PLANU_VERSION,
            nodeVersion: process.version,
        },
        installationId: 'anonymous',
    });
}
/**
 * Sends a tool_error or tool_blocked event for business-logic validation failures.
 * Gate/policy blocks (DoD, spec locked, invalid transitions) emit tool_blocked.
 * Other expected errors (missing params, not-found, etc.) emit tool_error.
 * Fire-and-forget — never throws.
 */
export function reportToolValidationError(toolName, message) {
    if (!isErrorReportingEnabled()) {
        return;
    }
    const sanitized = sanitizeErrorMessage(message);
    const event = classifyToolEvent(sanitized, 'validation');
    sendTelemetryEvent({
        event,
        properties: {
            tool: toolName,
            message: sanitized,
            errorType: 'validation',
            planVersion: PLANU_VERSION,
            nodeVersion: process.version,
        },
        installationId: 'anonymous',
    });
}
//# sourceMappingURL=error-reporter.js.map