/**
 * ContentLakeCacheAdapter — decorator that adds remote fingerprint lookup
 * on local cache miss.
 *
 * On local miss, queries the Content Lake for a report with a matching
 * eval fingerprint. If found, the eval + calculate-scores steps can be
 * skipped entirely (the cached score-summary.json is reused).
 *
 * This decorator wraps any CacheStore (typically FilesystemCache) and
 * only intercepts `lookup()` — all other operations delegate directly.
 *
 * @see docs/design-docs/content-lake-eval-caching.md
 */
export class ContentLakeCacheAdapter {
    local;
    reportStore;
    constructor(local, reportStore) {
        this.local = local;
        this.reportStore = reportStore;
    }
    async computeKey(inputPaths, context) {
        return this.local.computeKey(inputPaths, context);
    }
    async lookup(step, key) {
        // Try local first
        const local = await this.local.lookup(step, key);
        if (local.hit)
            return local;
        // Try remote — only for eval steps (the expensive ones)
        if (!step.startsWith("eval")) {
            return { hit: false };
        }
        try {
            const report = await this.reportStore.findByFingerprint(key);
            if (report) {
                return {
                    entry: {
                        durationMs: 0,
                        outputPaths: [],
                        summary: `Remote cache hit — reusing report from ${report.completedAt ?? "unknown date"}`,
                        timestamp: new Date().toISOString(),
                    },
                    hit: true,
                };
            }
        }
        catch {
            // Remote lookup failure is non-fatal — fall through to miss
        }
        return { hit: false };
    }
    async record(step, key, metadata) {
        // Only record locally — remote recording happens via the publish step
        await this.local.record(step, key, metadata);
    }
    async clear() {
        await this.local.clear();
    }
}
