|
| 1 | +import path from "path"; |
| 2 | +import fs from "fs/promises"; |
| 3 | +import { fileURLToPath } from "url"; |
| 4 | +import { |
| 5 | + AccuracyRunStatus, |
| 6 | + AccuracySnapshotEntry, |
| 7 | + AccuracySnapshotEntrySchema, |
| 8 | + AccuracySnapshotStorage, |
| 9 | +} from "./snapshot-storage.js"; |
| 10 | +const __dirname = fileURLToPath(import.meta.url); |
| 11 | +const rootDir = path.resolve(__dirname, "..", "..", "..", "..", ".."); |
| 12 | +const snapshotsDir = path.resolve(rootDir, ".accuracy-snapshots"); |
| 13 | +export const snapshotFilePath = path.resolve(snapshotsDir, "snapshots.json"); |
| 14 | + |
| 15 | +export class DiskSnapshotStorage implements AccuracySnapshotStorage { |
| 16 | + private constructor( |
| 17 | + private readonly accuracyRunId: string, |
| 18 | + private readonly commitSHA: string |
| 19 | + ) {} |
| 20 | + |
| 21 | + async createSnapshotEntry( |
| 22 | + snapshotEntry: Pick< |
| 23 | + AccuracySnapshotEntry, |
| 24 | + | "provider" |
| 25 | + | "requestedModel" |
| 26 | + | "test" |
| 27 | + | "prompt" |
| 28 | + | "toolCallingAccuracy" |
| 29 | + | "expectedToolCalls" |
| 30 | + | "actualToolCalls" |
| 31 | + | "llmResponseTime" |
| 32 | + | "tokensUsage" |
| 33 | + | "respondingModel" |
| 34 | + | "text" |
| 35 | + | "messages" |
| 36 | + > |
| 37 | + ): Promise<void> { |
| 38 | + const snapshotWithMeta: AccuracySnapshotEntry = { |
| 39 | + ...snapshotEntry, |
| 40 | + commitSHA: this.commitSHA, |
| 41 | + accuracyRunId: this.accuracyRunId, |
| 42 | + accuracyRunStatus: AccuracyRunStatus.InProgress, |
| 43 | + createdOn: Date.now(), |
| 44 | + }; |
| 45 | + |
| 46 | + await this.appendAccuracySnapshot(snapshotWithMeta); |
| 47 | + } |
| 48 | + |
| 49 | + async getLatestSnapshotsForCommit(commit: string): Promise<AccuracySnapshotEntry[]> { |
| 50 | + const snapshot = await this.readSnapshot(); |
| 51 | + const entries = snapshot |
| 52 | + .filter((entry) => { |
| 53 | + return entry.commitSHA === commit && entry.accuracyRunStatus === AccuracyRunStatus.Done; |
| 54 | + }) |
| 55 | + .sort((a, b) => b.createdOn - a.createdOn); |
| 56 | + const latestRunId = entries[0]?.accuracyRunId; |
| 57 | + return latestRunId ? snapshot.filter((entry) => entry.accuracyRunId === latestRunId) : []; |
| 58 | + } |
| 59 | + |
| 60 | + async accuracyRunFinished(): Promise<void> { |
| 61 | + const snapshot = await this.readSnapshot(); |
| 62 | + const updatedSnapshot = snapshot.map((entry) => { |
| 63 | + if (entry.accuracyRunId === this.accuracyRunId) { |
| 64 | + return { |
| 65 | + ...entry, |
| 66 | + accuracyRunStatus: AccuracyRunStatus.Done, |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + return entry; |
| 71 | + }); |
| 72 | + await this.writeSnapshot(updatedSnapshot); |
| 73 | + } |
| 74 | + |
| 75 | + close(): Promise<void> { |
| 76 | + return Promise.resolve(); |
| 77 | + } |
| 78 | + |
| 79 | + private async appendAccuracySnapshot(entry: AccuracySnapshotEntry): Promise<void> { |
| 80 | + for (let attempt = 0; attempt < 5; attempt++) { |
| 81 | + try { |
| 82 | + const snapshot = await this.readSnapshot(); |
| 83 | + snapshot.unshift(entry); |
| 84 | + await this.writeSnapshot(snapshot); |
| 85 | + return; |
| 86 | + } catch (e) { |
| 87 | + if (attempt < 4) { |
| 88 | + await this.waitFor(100 + Math.random() * 200); |
| 89 | + } else { |
| 90 | + throw e; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private async writeSnapshot(snapshot: AccuracySnapshotEntry[]): Promise<void> { |
| 97 | + const tmp = `${snapshotFilePath}~${Date.now()}`; |
| 98 | + await fs.writeFile(tmp, JSON.stringify(snapshot, null, 2)); |
| 99 | + await fs.rename(tmp, snapshotFilePath); |
| 100 | + } |
| 101 | + |
| 102 | + private async readSnapshot(): Promise<AccuracySnapshotEntry[]> { |
| 103 | + try { |
| 104 | + const raw = await fs.readFile(snapshotFilePath, "utf8"); |
| 105 | + return AccuracySnapshotEntrySchema.array().parse(JSON.parse(raw)); |
| 106 | + } catch (e: unknown) { |
| 107 | + if ((e as { code: string }).code === "ENOENT") { |
| 108 | + return []; |
| 109 | + } |
| 110 | + throw e; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private waitFor(ms: number) { |
| 115 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 116 | + } |
| 117 | + |
| 118 | + static async getStorage(commitSHA: string, accuracyRunId: string) { |
| 119 | + await fs.mkdir(snapshotsDir, { recursive: true }); |
| 120 | + return new DiskSnapshotStorage(commitSHA, accuracyRunId); |
| 121 | + } |
| 122 | +} |
0 commit comments