Coverage for src/retrospeedlab/lake/sources/lab_traces.py: 100%
34 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-04 00:01 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-04 00:01 +0000
1from datetime import datetime
3from pyspark.sql import DataFrame, SparkSession
4from pyspark.sql import functions as F
5from pyspark.sql.types import DoubleType, IntegerType, LongType, StringType, StructField, StructType, TimestampType
7from retrospeedlab.lake.batch.lab_runs import served_lab_runs
8from retrospeedlab.lake.lake.catalog import LAB_ROI, LAB_RUNS, Table
9from retrospeedlab.lake.lake.tables import exists, read
10from retrospeedlab.lake.lake.write import replace_counted
11from retrospeedlab.lake.quality.gate import Gate
12from retrospeedlab.lake.settings import LakeSettings
13from retrospeedlab.lake.sources.catalog import LAB_RUN_ROUNDS, LAB_RUN_TRACES, LAB_TRACE_DAILY, SILVER_LAB_ROUNDS
15COUNTS = (
16 "cost_usd",
17 "minutes",
18 "turns",
19 "tool_calls",
20 "tool_errors",
21 "subagents",
22 "tokens",
23 "input_tokens",
24 "output_tokens",
25 "cache_read_tokens",
26 "cache_creation_tokens",
27)
28LAB_ROUND = StructType(
29 [
30 StructField("game", StringType(), False),
31 StructField("stamp", StringType(), False),
32 StructField("round", IntegerType(), False),
33 StructField("outcome", StringType(), False),
34 StructField("started_at", TimestampType(), False),
35 StructField("minutes", DoubleType(), False),
36 StructField("model", StringType(), True),
37 StructField("cost_usd", DoubleType(), False),
38 *(StructField(name, LongType(), False) for name in COUNTS[2:]),
39 ]
40)
41NO_RUNS = "game string, stamp string, commits int"
42NO_ROI = "game string, stamp string, frames_saved double, frames_per_dollar double"
45def totals() -> list[F.Column]:
46 return [F.sum(name).alias(name) for name in COUNTS]
49def run_traces(rounds: DataFrame, runs: DataFrame, roi: DataFrame) -> DataFrame:
50 first = F.min(F.struct("round", "model")).alias("first")
51 traced = rounds.groupBy("game", "stamp", "outcome").agg(
52 F.count("*").alias("rounds"), F.min("started_at").alias("started_at"), first, *totals()
53 )
54 return (
55 traced.select("*", F.col("first.model").alias("model"))
56 .drop("first")
57 .join(runs.select("game", "stamp", "commits"), ["game", "stamp"], "left")
58 .join(roi.select("game", "stamp", "frames_saved", "frames_per_dollar"), ["game", "stamp"], "left")
59 )
62def trace_days(runs: DataFrame) -> DataFrame:
63 return runs.groupBy(F.to_date("started_at").alias("date"), "game").agg(
64 F.count("*").alias("runs"), F.sum("rounds").alias("rounds"), *totals()
65 )
68def optional(spark: SparkSession, lake: LakeSettings, table: Table, empty: str) -> DataFrame:
69 return read(spark, table, lake) if exists(spark, table, lake) else spark.createDataFrame([], empty)
72def build_lab_traces_gold(spark: SparkSession, lake: LakeSettings, gate: Gate, now: datetime) -> int:
73 if not exists(spark, SILVER_LAB_ROUNDS, lake):
74 return 0
75 stamped = F.lit(now).alias("computed_at")
76 rounds = read(spark, SILVER_LAB_ROUNDS, lake).drop("measured_at")
77 served = served_lab_runs(spark, lake) if exists(spark, LAB_RUNS, lake) else spark.createDataFrame([], NO_RUNS)
78 runs = run_traces(rounds, served, optional(spark, lake, LAB_ROI, NO_ROI))
79 tables = ((LAB_RUN_ROUNDS, rounds), (LAB_RUN_TRACES, runs), (LAB_TRACE_DAILY, trace_days(runs)))
80 return sum(replace_counted(table, lake, rows.select("*", stamped), gate) for table, rows in tables)