// src/meta-eval/outcome-store.ts
var InMemoryOutcomeStore = class {
  items = [];
  async append(outcome) {
    this.items.push({ ...outcome });
  }
  async forRun(runId) {
    return this.items.filter((o) => o.runId === runId).map((o) => ({ ...o }));
  }
  async list(filter = {}) {
    return this.items.filter((o) => matches(o, filter)).map((o) => ({ ...o }));
  }
};
var FileSystemOutcomeStore = class {
  dir;
  maxBytes;
  memo;
  loaded = false;
  constructor(options) {
    this.dir = options.dir;
    this.maxBytes = options.maxBytes ?? 32 * 1024 * 1024;
  }
  async ensureDir() {
    const fs = await import("fs/promises");
    await fs.mkdir(this.dir, { recursive: true });
  }
  async append(outcome) {
    await this.ensureDir();
    const fs = await import("fs/promises");
    const path = await import("path");
    const active = path.join(this.dir, "outcomes.ndjson");
    try {
      const stat = await fs.stat(active);
      if (stat.size >= this.maxBytes) {
        await fs.rename(active, path.join(this.dir, `outcomes.${Date.now()}.ndjson`));
      }
    } catch {
    }
    await fs.appendFile(active, `${JSON.stringify(outcome)}
`, "utf8");
    if (this.memo) await this.memo.append(outcome);
  }
  async load() {
    if (this.loaded && this.memo) return this.memo;
    const fs = await import("fs/promises");
    const path = await import("path");
    const memo = new InMemoryOutcomeStore();
    try {
      const entries = await fs.readdir(this.dir);
      for (const file of entries) {
        if (!file.endsWith(".ndjson")) continue;
        const content = await fs.readFile(path.join(this.dir, file), "utf8");
        for (const line of content.split("\n")) {
          if (!line.trim()) continue;
          await memo.append(JSON.parse(line));
        }
      }
    } catch {
    }
    this.memo = memo;
    this.loaded = true;
    return memo;
  }
  async forRun(runId) {
    return (await this.load()).forRun(runId);
  }
  async list(filter) {
    return (await this.load()).list(filter);
  }
};
function matches(o, f) {
  if (f.runIds && !f.runIds.includes(o.runId)) return false;
  if (f.since !== void 0 && o.capturedAt < f.since) return false;
  if (f.until !== void 0 && o.capturedAt > f.until) return false;
  if (f.source && o.source !== f.source) return false;
  if (f.label && o.labels?.[f.label.key] !== f.label.value) return false;
  return true;
}

export {
  InMemoryOutcomeStore,
  FileSystemOutcomeStore
};
//# sourceMappingURL=chunk-3RF76KTD.js.map