/**
 * Trace storage — port interface for persisting evaluation traces.
 *
 * Full trace payloads go to blob storage (GCP Cloud Storage or local
 * filesystem). Content Lake documents store only sanitized summaries
 * with a `traceDataUri` pointing to the full payload.
 *
 * CRITICAL SECURITY: The `next` dataset is publicly accessible. Full
 * trace data (which may contain tool call arguments with sensitive data)
 * MUST go to blob storage. Only sanitized summaries in Content Lake.
 *
 * @see docs/design-docs/architecture-overhaul/observability-telemetry.md
 * @see docs/design-docs/architecture-overhaul/storage-schema.md
 */
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
import { simpleHash } from "../hash.js";
// ---------------------------------------------------------------------------
// Local filesystem implementation
// ---------------------------------------------------------------------------
/** Local filesystem trace store — for development and CI */
export class LocalTraceStore {
    baseDir;
    constructor(baseDir) {
        this.baseDir = baseDir;
    }
    async store(trace) {
        mkdirSync(this.baseDir, { recursive: true });
        const filename = `${trace.traceId}.json`;
        const filepath = resolve(this.baseDir, filename);
        const content = JSON.stringify(trace, null, 2);
        writeFileSync(filepath, content);
        return {
            uri: `file://${filepath}`,
            contentHash: simpleHash(content),
            sizeBytes: Buffer.byteLength(content),
        };
    }
    async retrieve(uri) {
        const filepath = uri.startsWith("file://") ? uri.slice(7) : uri;
        if (!existsSync(filepath))
            return null;
        try {
            const content = readFileSync(filepath, "utf-8");
            return JSON.parse(content);
        }
        catch {
            return null;
        }
    }
}
// ---------------------------------------------------------------------------
// Summary extraction
// ---------------------------------------------------------------------------
/**
 * Extract a sanitized summary from a full trace.
 *
 * This summary is safe for Content Lake storage (no tool call arguments,
 * no file contents, no potentially sensitive data).
 */
export function extractTraceSummary(trace, traceDataUri) {
    // Count tool calls by category
    const toolCallCategories = {};
    for (const call of trace.toolCalls) {
        toolCallCategories[call.category] =
            (toolCallCategories[call.category] ?? 0) + 1;
    }
    return {
        traceId: trace.traceId,
        traceDataUri,
        taskId: trace.taskId,
        modelId: trace.modelId,
        toolCallCount: trace.toolCalls.length,
        toolCallCategories,
        totalTokens: trace.tokensUsed.totalTokens,
        costEstimate: trace.costEstimate,
        durationMs: trace.durationMs,
        urlsVisitedCount: trace.urlsVisited.length,
        filesReadCount: trace.filesRead.length,
        filesWrittenCount: trace.filesWritten.length,
    };
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
