diffscore 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffscore/__init__.py +3 -0
- diffscore/api.py +729 -0
- diffscore/calibration.py +374 -0
- diffscore/cli.py +867 -0
- diffscore/config.py +88 -0
- diffscore/db/__init__.py +27 -0
- diffscore/db/base.py +25 -0
- diffscore/db/migrate.py +61 -0
- diffscore/db/models.py +257 -0
- diffscore/db/session.py +37 -0
- diffscore/diffing.py +292 -0
- diffscore/graders.py +281 -0
- diffscore/jobs.py +161 -0
- diffscore/judge.py +355 -0
- diffscore/labeling.py +167 -0
- diffscore/llm.py +120 -0
- diffscore/migrations/README +1 -0
- diffscore/migrations/env.py +84 -0
- diffscore/migrations/script.py.mako +28 -0
- diffscore/migrations/versions/2026_11_30-03d94780829e_run_error_and_cache_latency.py +34 -0
- diffscore/migrations/versions/2026_11_30-8c8961902e49_run_spend.py +34 -0
- diffscore/migrations/versions/2026_11_30-da2445f19a6c_initial_schema.py +280 -0
- diffscore/migrations/versions/2026_12_01-053ee29c31c6_run_case_keys.py +55 -0
- diffscore/migrations/versions/2026_12_01-9dc97334c9d9_judge_calibration.py +69 -0
- diffscore/migrations/versions/2026_12_01-a85ad738bd12_run_provider_keys.py +32 -0
- diffscore/queries.py +165 -0
- diffscore/runner.py +393 -0
- diffscore/secrets.py +130 -0
- diffscore/stats.py +83 -0
- diffscore/suite.py +329 -0
- diffscore/templates/env.example +14 -0
- diffscore/templates/example-prompt.md +14 -0
- diffscore/templates/example.yaml +75 -0
- diffscore-0.1.0.dist-info/METADATA +351 -0
- diffscore-0.1.0.dist-info/RECORD +38 -0
- diffscore-0.1.0.dist-info/WHEEL +4 -0
- diffscore-0.1.0.dist-info/entry_points.txt +2 -0
- diffscore-0.1.0.dist-info/licenses/LICENSE +21 -0
diffscore/llm.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""The one place Diffscore talks to a model provider.
|
|
2
|
+
|
|
3
|
+
Everything goes through LiteLLM, so swapping providers is a model string, not
|
|
4
|
+
an adapter. The runner never calls this directly: it takes a `CompleteFn`, so
|
|
5
|
+
tests can hand it a fake model and run offline.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import re
|
|
12
|
+
import time
|
|
13
|
+
from collections.abc import Callable
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from diffscore.config import get_settings
|
|
18
|
+
|
|
19
|
+
Message = dict[str, str]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True)
|
|
23
|
+
class Completion:
|
|
24
|
+
text: str
|
|
25
|
+
model: str
|
|
26
|
+
input_tokens: int
|
|
27
|
+
output_tokens: int
|
|
28
|
+
latency_ms: int
|
|
29
|
+
cost_usd: float | None
|
|
30
|
+
cached: bool = False
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
CompleteFn = Callable[[str, list[Message], dict[str, Any]], Completion]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def complete(
|
|
37
|
+
model: str, messages: list[Message], params: dict[str, Any] | None = None
|
|
38
|
+
) -> Completion:
|
|
39
|
+
"""One chat completion, with usage and cost accounting.
|
|
40
|
+
|
|
41
|
+
`params` go straight to LiteLLM (temperature, max_tokens, ...). Transient
|
|
42
|
+
provider errors (429, 503) are retried with backoff by LiteLLM.
|
|
43
|
+
"""
|
|
44
|
+
import litellm # slow import; keep it off the CLI's startup path
|
|
45
|
+
|
|
46
|
+
get_settings().export_provider_keys()
|
|
47
|
+
litellm.suppress_debug_info = True
|
|
48
|
+
|
|
49
|
+
started = time.perf_counter()
|
|
50
|
+
response = litellm.completion(model=model, messages=messages, num_retries=3, **(params or {}))
|
|
51
|
+
latency_ms = round((time.perf_counter() - started) * 1000)
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
cost: float | None = float(litellm.completion_cost(completion_response=response))
|
|
55
|
+
except Exception: # unknown pricing for local/free models is normal
|
|
56
|
+
cost = None
|
|
57
|
+
|
|
58
|
+
usage = response.usage
|
|
59
|
+
return Completion(
|
|
60
|
+
text=response.choices[0].message.content or "",
|
|
61
|
+
model=model,
|
|
62
|
+
input_tokens=usage.prompt_tokens,
|
|
63
|
+
output_tokens=usage.completion_tokens,
|
|
64
|
+
latency_ms=latency_ms,
|
|
65
|
+
cost_usd=cost,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def throttled(
|
|
70
|
+
fn: CompleteFn,
|
|
71
|
+
rpm: float | None,
|
|
72
|
+
*,
|
|
73
|
+
per_model: dict[str, float | None] | None = None,
|
|
74
|
+
clock: Callable[[], float] = time.monotonic,
|
|
75
|
+
sleep: Callable[[float], None] = time.sleep,
|
|
76
|
+
) -> CompleteFn:
|
|
77
|
+
"""Space calls to each model at least 60/rpm seconds apart. `None` or 0: no limit.
|
|
78
|
+
|
|
79
|
+
Free tiers count requests per minute per model (Gemini's flash-lite allows
|
|
80
|
+
15, flash 5), and LiteLLM's short retries give up long before the window
|
|
81
|
+
resets, so pace instead of retrying. `per_model` overrides `rpm` for named
|
|
82
|
+
models, e.g. the judge.
|
|
83
|
+
"""
|
|
84
|
+
overrides = {k: v for k, v in (per_model or {}).items()}
|
|
85
|
+
if not rpm and not any(overrides.values()):
|
|
86
|
+
return fn
|
|
87
|
+
last: dict[str, float] = {}
|
|
88
|
+
|
|
89
|
+
def call(model: str, messages: list[Message], params: dict[str, Any]) -> Completion:
|
|
90
|
+
limit = overrides.get(model, rpm)
|
|
91
|
+
if limit:
|
|
92
|
+
if model in last:
|
|
93
|
+
wait = last[model] + 60.0 / limit - clock()
|
|
94
|
+
if wait > 0:
|
|
95
|
+
sleep(wait)
|
|
96
|
+
last[model] = clock()
|
|
97
|
+
return fn(model, messages, params)
|
|
98
|
+
|
|
99
|
+
return call
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def describe_error(exc: BaseException, limit: int = 300) -> str:
|
|
103
|
+
"""The useful part of a LiteLLM error, on one line.
|
|
104
|
+
|
|
105
|
+
LiteLLM wraps the provider's JSON body and appends a traceback and a
|
|
106
|
+
bug-report URL, so prefer the provider's own "message" field, then fall
|
|
107
|
+
back to the first line.
|
|
108
|
+
"""
|
|
109
|
+
text = str(exc)
|
|
110
|
+
found = re.search(r'"message":\s*"((?:[^"\\]|\\.)*)"', text)
|
|
111
|
+
if found:
|
|
112
|
+
try:
|
|
113
|
+
message = str(json.loads(f'"{found.group(1)}"'))
|
|
114
|
+
except json.JSONDecodeError:
|
|
115
|
+
message = found.group(1)
|
|
116
|
+
else:
|
|
117
|
+
lines = text.strip().splitlines()
|
|
118
|
+
message = lines[0] if lines else type(exc).__name__
|
|
119
|
+
message = " ".join(message.split())
|
|
120
|
+
return message if len(message) <= limit else message[: limit - 3] + "..."
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Generic single-database configuration.
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
from logging.config import fileConfig
|
|
2
|
+
|
|
3
|
+
from alembic import context
|
|
4
|
+
from sqlalchemy import engine_from_config, pool
|
|
5
|
+
|
|
6
|
+
# this is the Alembic Config object, which provides
|
|
7
|
+
# access to the values within the .ini file in use.
|
|
8
|
+
config = context.config
|
|
9
|
+
|
|
10
|
+
# Interpret the config file for Python logging.
|
|
11
|
+
# This line sets up loggers basically.
|
|
12
|
+
if config.config_file_name is not None:
|
|
13
|
+
fileConfig(config.config_file_name)
|
|
14
|
+
|
|
15
|
+
# add your model's MetaData object here
|
|
16
|
+
# for 'autogenerate' support
|
|
17
|
+
# from myapp import mymodel
|
|
18
|
+
from diffscore.config import get_settings # noqa: E402
|
|
19
|
+
from diffscore.db import Base # noqa: E402
|
|
20
|
+
|
|
21
|
+
target_metadata = Base.metadata
|
|
22
|
+
# A caller (diffscore.db.migrate) may pass the URL; otherwise use DATABASE_URL.
|
|
23
|
+
_url = config.attributes.get("url") or get_settings().database_url
|
|
24
|
+
config.set_main_option("sqlalchemy.url", _url.replace("%", "%%"))
|
|
25
|
+
|
|
26
|
+
# other values from the config, defined by the needs of env.py,
|
|
27
|
+
# can be acquired:
|
|
28
|
+
# my_important_option = config.get_main_option("my_important_option")
|
|
29
|
+
# ... etc.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def run_migrations_offline() -> None:
|
|
33
|
+
"""Run migrations in 'offline' mode.
|
|
34
|
+
|
|
35
|
+
This configures the context with just a URL
|
|
36
|
+
and not an Engine, though an Engine is acceptable
|
|
37
|
+
here as well. By skipping the Engine creation
|
|
38
|
+
we don't even need a DBAPI to be available.
|
|
39
|
+
|
|
40
|
+
Calls to context.execute() here emit the given string to the
|
|
41
|
+
script output.
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
url = config.get_main_option("sqlalchemy.url")
|
|
45
|
+
context.configure(
|
|
46
|
+
url=url,
|
|
47
|
+
target_metadata=target_metadata,
|
|
48
|
+
literal_binds=True,
|
|
49
|
+
dialect_opts={"paramstyle": "named"},
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
with context.begin_transaction():
|
|
53
|
+
context.run_migrations()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def run_migrations_online() -> None:
|
|
57
|
+
"""Run migrations in 'online' mode.
|
|
58
|
+
|
|
59
|
+
In this scenario we need to create an Engine
|
|
60
|
+
and associate a connection with the context.
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
connectable = engine_from_config(
|
|
64
|
+
config.get_section(config.config_ini_section, {}),
|
|
65
|
+
prefix="sqlalchemy.",
|
|
66
|
+
poolclass=pool.NullPool,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
with connectable.connect() as connection:
|
|
70
|
+
context.configure(
|
|
71
|
+
connection=connection,
|
|
72
|
+
target_metadata=target_metadata,
|
|
73
|
+
# SQLite can't ALTER most things; batch mode rebuilds the table instead.
|
|
74
|
+
render_as_batch=connection.dialect.name == "sqlite",
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
with context.begin_transaction():
|
|
78
|
+
context.run_migrations()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
if context.is_offline_mode():
|
|
82
|
+
run_migrations_offline()
|
|
83
|
+
else:
|
|
84
|
+
run_migrations_online()
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""${message}
|
|
2
|
+
|
|
3
|
+
Revision ID: ${up_revision}
|
|
4
|
+
Revises: ${down_revision | comma,n}
|
|
5
|
+
Create Date: ${create_date}
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
from typing import Sequence, Union
|
|
9
|
+
|
|
10
|
+
from alembic import op
|
|
11
|
+
import sqlalchemy as sa
|
|
12
|
+
${imports if imports else ""}
|
|
13
|
+
|
|
14
|
+
# revision identifiers, used by Alembic.
|
|
15
|
+
revision: str = ${repr(up_revision)}
|
|
16
|
+
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
|
|
17
|
+
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
|
18
|
+
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def upgrade() -> None:
|
|
22
|
+
"""Upgrade schema."""
|
|
23
|
+
${upgrades if upgrades else "pass"}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def downgrade() -> None:
|
|
27
|
+
"""Downgrade schema."""
|
|
28
|
+
${downgrades if downgrades else "pass"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""run error and cache latency
|
|
2
|
+
|
|
3
|
+
Revision ID: 03d94780829e
|
|
4
|
+
Revises: da2445f19a6c
|
|
5
|
+
Create Date: 2026-11-30 15:58:21.540640
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
|
|
11
|
+
import sqlalchemy as sa
|
|
12
|
+
from alembic import op
|
|
13
|
+
|
|
14
|
+
# revision identifiers, used by Alembic.
|
|
15
|
+
revision: str = "03d94780829e"
|
|
16
|
+
down_revision: str | Sequence[str] | None = "da2445f19a6c"
|
|
17
|
+
branch_labels: str | Sequence[str] | None = None
|
|
18
|
+
depends_on: str | Sequence[str] | None = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def upgrade() -> None:
|
|
22
|
+
"""Upgrade schema."""
|
|
23
|
+
# ### commands auto generated by Alembic - please adjust! ###
|
|
24
|
+
op.add_column("cache", sa.Column("latency_ms", sa.Integer(), nullable=True))
|
|
25
|
+
op.add_column("run", sa.Column("error", sa.Text(), nullable=True))
|
|
26
|
+
# ### end Alembic commands ###
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def downgrade() -> None:
|
|
30
|
+
"""Downgrade schema."""
|
|
31
|
+
# ### commands auto generated by Alembic - please adjust! ###
|
|
32
|
+
op.drop_column("run", "error")
|
|
33
|
+
op.drop_column("cache", "latency_ms")
|
|
34
|
+
# ### end Alembic commands ###
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""run spend
|
|
2
|
+
|
|
3
|
+
Revision ID: 8c8961902e49
|
|
4
|
+
Revises: 03d94780829e
|
|
5
|
+
Create Date: 2026-11-30 22:10:05.991891
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
|
|
11
|
+
import sqlalchemy as sa
|
|
12
|
+
from alembic import op
|
|
13
|
+
|
|
14
|
+
# revision identifiers, used by Alembic.
|
|
15
|
+
revision: str = "8c8961902e49"
|
|
16
|
+
down_revision: str | Sequence[str] | None = "03d94780829e"
|
|
17
|
+
branch_labels: str | Sequence[str] | None = None
|
|
18
|
+
depends_on: str | Sequence[str] | None = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def upgrade() -> None:
|
|
22
|
+
"""Upgrade schema."""
|
|
23
|
+
# ### commands auto generated by Alembic - please adjust! ###
|
|
24
|
+
op.add_column("run", sa.Column("tokens_spent", sa.Integer(), nullable=True))
|
|
25
|
+
op.add_column("run", sa.Column("cost_spent", sa.Numeric(precision=12, scale=6), nullable=True))
|
|
26
|
+
# ### end Alembic commands ###
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def downgrade() -> None:
|
|
30
|
+
"""Downgrade schema."""
|
|
31
|
+
# ### commands auto generated by Alembic - please adjust! ###
|
|
32
|
+
op.drop_column("run", "cost_spent")
|
|
33
|
+
op.drop_column("run", "tokens_spent")
|
|
34
|
+
# ### end Alembic commands ###
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
"""initial schema
|
|
2
|
+
|
|
3
|
+
Revision ID: da2445f19a6c
|
|
4
|
+
Revises:
|
|
5
|
+
Create Date: 2026-11-30 15:05:20.488999
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
|
|
11
|
+
import sqlalchemy as sa
|
|
12
|
+
from alembic import op
|
|
13
|
+
from sqlalchemy.dialects import postgresql
|
|
14
|
+
|
|
15
|
+
# revision identifiers, used by Alembic.
|
|
16
|
+
revision: str = "da2445f19a6c"
|
|
17
|
+
down_revision: str | Sequence[str] | None = None
|
|
18
|
+
branch_labels: str | Sequence[str] | None = None
|
|
19
|
+
depends_on: str | Sequence[str] | None = None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def upgrade() -> None:
|
|
23
|
+
"""Upgrade schema."""
|
|
24
|
+
# ### commands auto generated by Alembic - please adjust! ###
|
|
25
|
+
op.create_table(
|
|
26
|
+
"cache",
|
|
27
|
+
sa.Column("key", sa.String(length=64), nullable=False),
|
|
28
|
+
sa.Column("model", sa.String(length=200), nullable=False),
|
|
29
|
+
sa.Column("output", sa.Text(), nullable=False),
|
|
30
|
+
sa.Column("input_tokens", sa.Integer(), nullable=True),
|
|
31
|
+
sa.Column("output_tokens", sa.Integer(), nullable=True),
|
|
32
|
+
sa.Column("cost_usd", sa.Numeric(precision=12, scale=6), nullable=True),
|
|
33
|
+
sa.Column("hit_count", sa.Integer(), nullable=False),
|
|
34
|
+
sa.Column(
|
|
35
|
+
"created_at",
|
|
36
|
+
sa.DateTime(timezone=True),
|
|
37
|
+
server_default=sa.text("now()"),
|
|
38
|
+
nullable=False,
|
|
39
|
+
),
|
|
40
|
+
sa.PrimaryKeyConstraint("key", name=op.f("pk_cache")),
|
|
41
|
+
)
|
|
42
|
+
op.create_table(
|
|
43
|
+
"project",
|
|
44
|
+
sa.Column("id", sa.Integer(), nullable=False),
|
|
45
|
+
sa.Column("name", sa.String(length=200), nullable=False),
|
|
46
|
+
sa.Column("default_model", sa.String(length=200), nullable=True),
|
|
47
|
+
sa.Column("provider_keys_encrypted", sa.LargeBinary(), nullable=True),
|
|
48
|
+
sa.Column(
|
|
49
|
+
"created_at",
|
|
50
|
+
sa.DateTime(timezone=True),
|
|
51
|
+
server_default=sa.text("now()"),
|
|
52
|
+
nullable=False,
|
|
53
|
+
),
|
|
54
|
+
sa.PrimaryKeyConstraint("id", name=op.f("pk_project")),
|
|
55
|
+
sa.UniqueConstraint("name", name=op.f("uq_project_name")),
|
|
56
|
+
)
|
|
57
|
+
op.create_table(
|
|
58
|
+
"prompt_version",
|
|
59
|
+
sa.Column("id", sa.Integer(), nullable=False),
|
|
60
|
+
sa.Column("project_id", sa.Integer(), nullable=False),
|
|
61
|
+
sa.Column("template", sa.Text(), nullable=False),
|
|
62
|
+
sa.Column(
|
|
63
|
+
"params",
|
|
64
|
+
sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), "postgresql"),
|
|
65
|
+
nullable=False,
|
|
66
|
+
),
|
|
67
|
+
sa.Column("content_hash", sa.String(length=64), nullable=False),
|
|
68
|
+
sa.Column("label", sa.String(length=200), nullable=True),
|
|
69
|
+
sa.Column(
|
|
70
|
+
"created_at",
|
|
71
|
+
sa.DateTime(timezone=True),
|
|
72
|
+
server_default=sa.text("now()"),
|
|
73
|
+
nullable=False,
|
|
74
|
+
),
|
|
75
|
+
sa.ForeignKeyConstraint(
|
|
76
|
+
["project_id"],
|
|
77
|
+
["project.id"],
|
|
78
|
+
name=op.f("fk_prompt_version_project_id_project"),
|
|
79
|
+
ondelete="CASCADE",
|
|
80
|
+
),
|
|
81
|
+
sa.PrimaryKeyConstraint("id", name=op.f("pk_prompt_version")),
|
|
82
|
+
sa.UniqueConstraint(
|
|
83
|
+
"project_id", "content_hash", name=op.f("uq_prompt_version_project_id_content_hash")
|
|
84
|
+
),
|
|
85
|
+
)
|
|
86
|
+
op.create_table(
|
|
87
|
+
"suite",
|
|
88
|
+
sa.Column("id", sa.Integer(), nullable=False),
|
|
89
|
+
sa.Column("project_id", sa.Integer(), nullable=False),
|
|
90
|
+
sa.Column("name", sa.String(length=200), nullable=False),
|
|
91
|
+
sa.Column("baseline_run_id", sa.Integer(), nullable=True),
|
|
92
|
+
sa.Column(
|
|
93
|
+
"created_at",
|
|
94
|
+
sa.DateTime(timezone=True),
|
|
95
|
+
server_default=sa.text("now()"),
|
|
96
|
+
nullable=False,
|
|
97
|
+
),
|
|
98
|
+
sa.ForeignKeyConstraint(
|
|
99
|
+
["project_id"],
|
|
100
|
+
["project.id"],
|
|
101
|
+
name=op.f("fk_suite_project_id_project"),
|
|
102
|
+
ondelete="CASCADE",
|
|
103
|
+
),
|
|
104
|
+
sa.PrimaryKeyConstraint("id", name=op.f("pk_suite")),
|
|
105
|
+
sa.UniqueConstraint("project_id", "name", name=op.f("uq_suite_project_id_name")),
|
|
106
|
+
)
|
|
107
|
+
op.create_table(
|
|
108
|
+
"case",
|
|
109
|
+
sa.Column("id", sa.Integer(), nullable=False),
|
|
110
|
+
sa.Column("suite_id", sa.Integer(), nullable=False),
|
|
111
|
+
sa.Column("key", sa.String(length=200), nullable=False),
|
|
112
|
+
sa.Column(
|
|
113
|
+
"input",
|
|
114
|
+
sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), "postgresql"),
|
|
115
|
+
nullable=False,
|
|
116
|
+
),
|
|
117
|
+
sa.Column("reference_output", sa.Text(), nullable=True),
|
|
118
|
+
sa.Column(
|
|
119
|
+
"graders",
|
|
120
|
+
sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), "postgresql"),
|
|
121
|
+
nullable=False,
|
|
122
|
+
),
|
|
123
|
+
sa.Column(
|
|
124
|
+
"created_at",
|
|
125
|
+
sa.DateTime(timezone=True),
|
|
126
|
+
server_default=sa.text("now()"),
|
|
127
|
+
nullable=False,
|
|
128
|
+
),
|
|
129
|
+
sa.ForeignKeyConstraint(
|
|
130
|
+
["suite_id"], ["suite.id"], name=op.f("fk_case_suite_id_suite"), ondelete="CASCADE"
|
|
131
|
+
),
|
|
132
|
+
sa.PrimaryKeyConstraint("id", name=op.f("pk_case")),
|
|
133
|
+
sa.UniqueConstraint("suite_id", "key", name=op.f("uq_case_suite_id_key")),
|
|
134
|
+
)
|
|
135
|
+
op.create_table(
|
|
136
|
+
"run",
|
|
137
|
+
sa.Column("id", sa.Integer(), nullable=False),
|
|
138
|
+
sa.Column("suite_id", sa.Integer(), nullable=False),
|
|
139
|
+
sa.Column("prompt_version_id", sa.Integer(), nullable=False),
|
|
140
|
+
sa.Column("model", sa.String(length=200), nullable=False),
|
|
141
|
+
sa.Column(
|
|
142
|
+
"params",
|
|
143
|
+
sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), "postgresql"),
|
|
144
|
+
nullable=False,
|
|
145
|
+
),
|
|
146
|
+
sa.Column("status", sa.String(length=20), nullable=False),
|
|
147
|
+
sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
|
|
148
|
+
sa.Column(
|
|
149
|
+
"created_at",
|
|
150
|
+
sa.DateTime(timezone=True),
|
|
151
|
+
server_default=sa.text("now()"),
|
|
152
|
+
nullable=False,
|
|
153
|
+
),
|
|
154
|
+
sa.CheckConstraint(
|
|
155
|
+
"status IN ('pending', 'running', 'completed', 'failed', 'cancelled')",
|
|
156
|
+
name=op.f("ck_run_status"),
|
|
157
|
+
),
|
|
158
|
+
sa.ForeignKeyConstraint(
|
|
159
|
+
["prompt_version_id"],
|
|
160
|
+
["prompt_version.id"],
|
|
161
|
+
name=op.f("fk_run_prompt_version_id_prompt_version"),
|
|
162
|
+
),
|
|
163
|
+
sa.ForeignKeyConstraint(
|
|
164
|
+
["suite_id"], ["suite.id"], name=op.f("fk_run_suite_id_suite"), ondelete="CASCADE"
|
|
165
|
+
),
|
|
166
|
+
sa.PrimaryKeyConstraint("id", name=op.f("pk_run")),
|
|
167
|
+
)
|
|
168
|
+
op.create_index(op.f("ix_run_suite_id"), "run", ["suite_id"], unique=False)
|
|
169
|
+
# suite.baseline_run_id -> run.id is a cycle (run -> suite), so it is added
|
|
170
|
+
# once both tables exist rather than inline in create_table.
|
|
171
|
+
op.create_foreign_key(
|
|
172
|
+
op.f("fk_suite_baseline_run_id_run"),
|
|
173
|
+
"suite",
|
|
174
|
+
"run",
|
|
175
|
+
["baseline_run_id"],
|
|
176
|
+
["id"],
|
|
177
|
+
ondelete="SET NULL",
|
|
178
|
+
)
|
|
179
|
+
op.create_table(
|
|
180
|
+
"result",
|
|
181
|
+
sa.Column("id", sa.Integer(), nullable=False),
|
|
182
|
+
sa.Column("run_id", sa.Integer(), nullable=False),
|
|
183
|
+
sa.Column("case_id", sa.Integer(), nullable=False),
|
|
184
|
+
sa.Column("output", sa.Text(), nullable=True),
|
|
185
|
+
sa.Column("error", sa.Text(), nullable=True),
|
|
186
|
+
sa.Column("input_tokens", sa.Integer(), nullable=True),
|
|
187
|
+
sa.Column("output_tokens", sa.Integer(), nullable=True),
|
|
188
|
+
sa.Column("latency_ms", sa.Integer(), nullable=True),
|
|
189
|
+
sa.Column("cost_usd", sa.Numeric(precision=12, scale=6), nullable=True),
|
|
190
|
+
sa.Column("cache_hit", sa.Boolean(), nullable=False),
|
|
191
|
+
sa.Column(
|
|
192
|
+
"created_at",
|
|
193
|
+
sa.DateTime(timezone=True),
|
|
194
|
+
server_default=sa.text("now()"),
|
|
195
|
+
nullable=False,
|
|
196
|
+
),
|
|
197
|
+
sa.ForeignKeyConstraint(
|
|
198
|
+
["case_id"], ["case.id"], name=op.f("fk_result_case_id_case"), ondelete="CASCADE"
|
|
199
|
+
),
|
|
200
|
+
sa.ForeignKeyConstraint(
|
|
201
|
+
["run_id"], ["run.id"], name=op.f("fk_result_run_id_run"), ondelete="CASCADE"
|
|
202
|
+
),
|
|
203
|
+
sa.PrimaryKeyConstraint("id", name=op.f("pk_result")),
|
|
204
|
+
sa.UniqueConstraint("run_id", "case_id", name=op.f("uq_result_run_id_case_id")),
|
|
205
|
+
)
|
|
206
|
+
op.create_table(
|
|
207
|
+
"grade",
|
|
208
|
+
sa.Column("id", sa.Integer(), nullable=False),
|
|
209
|
+
sa.Column("result_id", sa.Integer(), nullable=False),
|
|
210
|
+
sa.Column("grader_name", sa.String(length=100), nullable=False),
|
|
211
|
+
sa.Column("grader_version", sa.String(length=50), nullable=False),
|
|
212
|
+
sa.Column("score", sa.Float(), nullable=False),
|
|
213
|
+
sa.Column("verdict", sa.String(length=10), nullable=False),
|
|
214
|
+
sa.Column("reason", sa.Text(), nullable=True),
|
|
215
|
+
sa.Column(
|
|
216
|
+
"details",
|
|
217
|
+
sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), "postgresql"),
|
|
218
|
+
nullable=False,
|
|
219
|
+
),
|
|
220
|
+
sa.Column(
|
|
221
|
+
"created_at",
|
|
222
|
+
sa.DateTime(timezone=True),
|
|
223
|
+
server_default=sa.text("now()"),
|
|
224
|
+
nullable=False,
|
|
225
|
+
),
|
|
226
|
+
sa.CheckConstraint("verdict IN ('pass', 'fail')", name=op.f("ck_grade_verdict")),
|
|
227
|
+
sa.CheckConstraint("score >= 0 AND score <= 1", name=op.f("ck_grade_score_range")),
|
|
228
|
+
sa.ForeignKeyConstraint(
|
|
229
|
+
["result_id"], ["result.id"], name=op.f("fk_grade_result_id_result"), ondelete="CASCADE"
|
|
230
|
+
),
|
|
231
|
+
sa.PrimaryKeyConstraint("id", name=op.f("pk_grade")),
|
|
232
|
+
)
|
|
233
|
+
op.create_index(op.f("ix_grade_result_id"), "grade", ["result_id"], unique=False)
|
|
234
|
+
op.create_table(
|
|
235
|
+
"human_label",
|
|
236
|
+
sa.Column("id", sa.Integer(), nullable=False),
|
|
237
|
+
sa.Column("result_id", sa.Integer(), nullable=False),
|
|
238
|
+
sa.Column("grader_name", sa.String(length=100), nullable=True),
|
|
239
|
+
sa.Column("verdict", sa.String(length=10), nullable=False),
|
|
240
|
+
sa.Column("note", sa.Text(), nullable=True),
|
|
241
|
+
sa.Column(
|
|
242
|
+
"created_at",
|
|
243
|
+
sa.DateTime(timezone=True),
|
|
244
|
+
server_default=sa.text("now()"),
|
|
245
|
+
nullable=False,
|
|
246
|
+
),
|
|
247
|
+
sa.CheckConstraint("verdict IN ('pass', 'fail')", name=op.f("ck_human_label_verdict")),
|
|
248
|
+
sa.ForeignKeyConstraint(
|
|
249
|
+
["result_id"],
|
|
250
|
+
["result.id"],
|
|
251
|
+
name=op.f("fk_human_label_result_id_result"),
|
|
252
|
+
ondelete="CASCADE",
|
|
253
|
+
),
|
|
254
|
+
sa.PrimaryKeyConstraint("id", name=op.f("pk_human_label")),
|
|
255
|
+
sa.UniqueConstraint(
|
|
256
|
+
"result_id",
|
|
257
|
+
"grader_name",
|
|
258
|
+
name=op.f("uq_human_label_result_id_grader_name"),
|
|
259
|
+
postgresql_nulls_not_distinct=True,
|
|
260
|
+
),
|
|
261
|
+
)
|
|
262
|
+
# ### end Alembic commands ###
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def downgrade() -> None:
|
|
266
|
+
"""Downgrade schema."""
|
|
267
|
+
op.drop_constraint(op.f("fk_suite_baseline_run_id_run"), "suite", type_="foreignkey")
|
|
268
|
+
# ### commands auto generated by Alembic - please adjust! ###
|
|
269
|
+
op.drop_table("human_label")
|
|
270
|
+
op.drop_index(op.f("ix_grade_result_id"), table_name="grade")
|
|
271
|
+
op.drop_table("grade")
|
|
272
|
+
op.drop_table("result")
|
|
273
|
+
op.drop_index(op.f("ix_run_suite_id"), table_name="run")
|
|
274
|
+
op.drop_table("run")
|
|
275
|
+
op.drop_table("case")
|
|
276
|
+
op.drop_table("suite")
|
|
277
|
+
op.drop_table("prompt_version")
|
|
278
|
+
op.drop_table("project")
|
|
279
|
+
op.drop_table("cache")
|
|
280
|
+
# ### end Alembic commands ###
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""run case keys
|
|
2
|
+
|
|
3
|
+
Revision ID: 053ee29c31c6
|
|
4
|
+
Revises: 9dc97334c9d9
|
|
5
|
+
Create Date: 2026-12-01 11:23:56.078911
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
|
|
11
|
+
import sqlalchemy as sa
|
|
12
|
+
from alembic import op
|
|
13
|
+
from sqlalchemy.dialects import postgresql
|
|
14
|
+
|
|
15
|
+
# revision identifiers, used by Alembic.
|
|
16
|
+
revision: str = "053ee29c31c6"
|
|
17
|
+
down_revision: str | Sequence[str] | None = "9dc97334c9d9"
|
|
18
|
+
branch_labels: str | Sequence[str] | None = None
|
|
19
|
+
depends_on: str | Sequence[str] | None = None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def upgrade() -> None:
|
|
23
|
+
"""Upgrade schema."""
|
|
24
|
+
op.add_column(
|
|
25
|
+
"run",
|
|
26
|
+
sa.Column(
|
|
27
|
+
"case_keys",
|
|
28
|
+
sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), "postgresql"),
|
|
29
|
+
nullable=False,
|
|
30
|
+
server_default=sa.text("'[]'"),
|
|
31
|
+
),
|
|
32
|
+
)
|
|
33
|
+
# Existing runs: recover each run's case list, in order, from its results.
|
|
34
|
+
# (Postgres only: SQLite databases are created at the latest schema.)
|
|
35
|
+
if op.get_bind().dialect.name != "postgresql":
|
|
36
|
+
return
|
|
37
|
+
op.execute(
|
|
38
|
+
"""
|
|
39
|
+
UPDATE run SET case_keys = sub.keys
|
|
40
|
+
FROM (
|
|
41
|
+
SELECT r.run_id, jsonb_agg(c.key ORDER BY r.id) AS keys
|
|
42
|
+
FROM result r JOIN "case" c ON c.id = r.case_id
|
|
43
|
+
GROUP BY r.run_id
|
|
44
|
+
) AS sub
|
|
45
|
+
WHERE sub.run_id = run.id
|
|
46
|
+
"""
|
|
47
|
+
)
|
|
48
|
+
op.alter_column("run", "case_keys", server_default=None)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def downgrade() -> None:
|
|
52
|
+
"""Downgrade schema."""
|
|
53
|
+
# ### commands auto generated by Alembic - please adjust! ###
|
|
54
|
+
op.drop_column("run", "case_keys")
|
|
55
|
+
# ### end Alembic commands ###
|