(
benchDashboard: BenchDashboard,
store: PromotionStore,
opts: { promotionName?: string } = {}
)
| 338 | } |
| 339 | |
| 340 | export async function syncPromotionsFromBench( |
| 341 | benchDashboard: BenchDashboard, |
| 342 | store: PromotionStore, |
| 343 | opts: { promotionName?: string } = {} |
| 344 | ): Promise<SyncResult> { |
| 345 | const rawPromotions = opts.promotionName |
| 346 | ? await getNamedPromotion(benchDashboard, opts.promotionName) |
| 347 | : await listNewPromotions(benchDashboard, store); |
| 348 | const promotions = rawPromotions.map(promotion => PromotionRecordSchema.parse(promotion)); |
| 349 | |
| 350 | let inserted = 0; |
| 351 | let alreadyHad = 0; |
| 352 | const tuplesToRecompute = new Map<string, PromotionTuple>(); |
| 353 | const fetchedModels = [...new Set(promotions.map(promotion => promotion.model))]; |
| 354 | const terminalModels = promotions |
| 355 | .filter(promotion => promotion.task_source === 'terminal-bench') |
| 356 | .map(promotion => promotion.model); |
| 357 | const orphanedModels = await store.listOrphanedTerminalBenchModels(); |
| 358 | const models = [...new Set([...fetchedModels, ...orphanedModels])]; |
| 359 | await store.ensureModelStatsTargets([...terminalModels, ...orphanedModels]); |
| 360 | const modelStatsTargets = await store.findModelStatsTargets(models); |
| 361 | const orphanedTargets = new Map( |
| 362 | orphanedModels.flatMap(model => { |
| 363 | const target = modelStatsTargets.get(model); |
| 364 | return target ? [[model, target]] : []; |
| 365 | }) |
| 366 | ); |
| 367 | const repaired = await store.linkOrphanedTerminalBenchPromotions(orphanedTargets); |
| 368 | for (const tuple of repaired) tuplesToRecompute.set(tupleKey(tuple), tuple); |
| 369 | const promotionsToInsert = promotions.map(promotion => { |
| 370 | const modelStatsTarget = modelStatsTargets.get(promotion.model); |
| 371 | return { |
| 372 | promotion, |
| 373 | modelStatsId: modelStatsTarget?.id ?? null, |
| 374 | } satisfies PromotionInsert; |
| 375 | }); |
| 376 | const insertedPromotionNames = await store.insertPromotions(promotionsToInsert); |
| 377 | if (opts.promotionName != null) { |
| 378 | const promotionToRefresh = promotionsToInsert.find( |
| 379 | ({ promotion }) => !insertedPromotionNames.has(promotion.bench_eval_name) |
| 380 | ); |
| 381 | if (promotionToRefresh) { |
| 382 | await store.refreshPromotion(promotionToRefresh); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | for (const promotion of promotions) { |
| 387 | const modelStatsTarget = modelStatsTargets.get(promotion.model); |
| 388 | const wasInserted = insertedPromotionNames.has(promotion.bench_eval_name); |
| 389 | if (wasInserted) { |
| 390 | inserted++; |
| 391 | } else { |
| 392 | alreadyHad++; |
| 393 | } |
| 394 | |
| 395 | // Named admin re-pulls refresh the model cache even when the audit row is already present. |
| 396 | if (modelStatsTarget && (wasInserted || opts.promotionName != null)) { |
| 397 | const tuple = { |
no test coverage detected