/**
 * FilesystemCache — wraps the existing pipeline/cache.ts behind the CacheStore port.
 *
 * This adapter delegates to the battle-tested hashing, manifest read/write,
 * and output-existence checks in pipeline/cache.ts. It adds no new logic —
 * just adapts the function-based API to the CacheStore interface.
 *
 * @see packages/eval/src/pipeline/cache.ts — underlying implementation
 */
import { existsSync, rmSync } from "fs";
import { resolve } from "path";
import { hashFiles, readManifest, recordCache } from "../../pipeline/cache.js";
export class FilesystemCache {
    rootDir;
    constructor(rootDir) {
        this.rootDir = rootDir;
    }
    async computeKey(inputPaths, context) {
        return hashFiles(inputPaths, context);
    }
    async lookup(step, key) {
        const manifest = readManifest(this.rootDir, step);
        if (!manifest?.entry) {
            return { hit: false };
        }
        // Hash must match
        if (manifest.entry.inputHash !== key) {
            return { hit: false };
        }
        // All output files must still exist (guard against manual deletion)
        const outputsExist = manifest.entry.outputPaths.every((p) => existsSync(resolve(this.rootDir, p)));
        if (!outputsExist) {
            return { hit: false };
        }
        return {
            entry: {
                durationMs: manifest.entry.durationMs,
                outputPaths: manifest.entry.outputPaths,
                summary: manifest.entry.summary,
                timestamp: manifest.entry.timestamp,
            },
            hit: true,
        };
    }
    async record(step, key, metadata) {
        recordCache(this.rootDir, step, key, metadata.summary, metadata.durationMs, metadata.outputPaths);
    }
    async clear() {
        const cacheDir = resolve(this.rootDir, "results", "cache");
        if (existsSync(cacheDir)) {
            rmSync(cacheDir, { recursive: true, force: true });
        }
    }
}
