/**
 * Session capture utility for debugging compaction failures.
 *
 * Sessions are saved to the app's .superblocks/captured-sessions folder.
 * Captured sessions can be replayed in integration tests to verify
 * summarization quality using LLM-as-judge.
 */
import { writeFileSync, mkdirSync, existsSync } from "node:fs";
import { join } from "node:path";
import { getPrefixedLogger } from "../../../util/logger.js";
const logger = getPrefixedLogger("[session-capture]");
/** Set to true to capture sessions for debugging compaction failures */
const CAPTURE_ENABLED = false;
function isTestEnvironment() {
    return process.env.VITEST === "true" || process.env.NODE_ENV === "test";
}
function isEnabled() {
    if (isTestEnvironment()) {
        return false;
    }
    return CAPTURE_ENABLED;
}
function getCaptureDir(rootDir) {
    return join(rootDir, ".superblocks", "captured-sessions");
}
function ensureDir(captureDir) {
    if (!existsSync(captureDir)) {
        mkdirSync(captureDir, { recursive: true });
    }
}
function generateFilename() {
    const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
    return `session-${timestamp}.json`;
}
/**
 * Capture full session data for debugging compaction failures.
 * Sessions are saved to {rootDir}/.superblocks/captured-sessions/
 *
 * @param data - The session data to capture
 * @param rootDir - The app root directory
 */
export function captureSession(data, rootDir) {
    if (!isEnabled()) {
        return null;
    }
    try {
        const captureDir = getCaptureDir(rootDir);
        ensureDir(captureDir);
        const session = {
            messages: data.messages,
            usage: data.usage,
        };
        const filename = generateFilename();
        const filepath = join(captureDir, filename);
        writeFileSync(filepath, JSON.stringify(session, null, 2));
        logger.info(`Captured session to ${filepath}`);
        return filepath;
    }
    catch (err) {
        logger.error(`Failed to capture session: ${err}`);
        return null;
    }
}
//# sourceMappingURL=session-capture.js.map