Banaxi-Tech's picture
Release Klein-Code-Bench v0.1.0: 260 validated project-generation tasks
072246b verified
Raw History Blame Contribute Delete
15.4 kB
"""Acceptance scenarios: every scenario gets an independent project/state copy."""
from __future__ import annotations
import copy
import json
import random
from .catalog import FAMILIES
from .fuzz import generated_inputs
from .protocol import TIERS
from .reference.service import Service
def scenarios(family, tier, seed=1729):
f = FAMILIES[family]
level = list(TIERS).index(tier)
rng = random.Random(f"{seed}/{family}/{tier}")
a, b, c = [
prefix + str(rng.randrange(10**8)) for prefix in ("doc/", "doc/", "other/")
]
a, b = sorted((a, b))
value, other = copy.deepcopy(f.inputs[0]), copy.deepcopy(f.inputs[-1])
traces = []
def add(name, *sessions):
traces.append(
{"name": name, "sessions": [{"requests": list(s)} for s in sessions]}
)
add(
"generated-domain",
[{"op": "solve", "input": v} for v in generated_inputs(family, seed=seed)],
)
add("domain-boundaries", [{"op": "solve", "input": v} for v in f.inputs])
add(
"protocol-errors",
[
"{broken",
"",
"null",
"[]",
"42",
{},
{"op": 1},
{"op": "unknown"},
{"op": "solve"},
{"op": "solve", "input": []},
{"op": "solve", "input": {}},
{"op": "solve", "input": value},
],
)
add(
"required-fields",
[
{"op": "solve", "input": {k: v for k, v in value.items() if k != missing}}
for missing in f.required
]
+ [{"op": "solve", "input": dict(value, ignored_extra={"nested": True})}],
)
if level == 0:
add(
"unsupported-store",
[
{"op": "put", "key": a, "value": value},
{"op": "status"},
{"op": "batch", "commands": []},
],
)
if level >= 1:
add(
"document-lifecycle",
[
{"op": "status"},
{"op": "list"},
{"op": "get", "key": a},
{"op": "put", "key": a, "value": value},
{"op": "evaluate", "key": a},
{"op": "get", "key": a},
{"op": "put", "key": a, "value": other},
{"op": "evaluate", "key": a},
{"op": "put", "key": b, "value": value},
{"op": "delete", "key": a},
{"op": "get", "key": a},
{"op": "delete", "key": a},
{"op": "get", "key": b},
{"op": "status"},
],
)
add(
"listing-and-json-values",
[
{"op": "put", "key": c, "value": None},
{"op": "put", "key": b, "value": [True, False, {"x": [1, None]}]},
{"op": "put", "key": a, "value": {}},
{"op": "put", "key": "../é/😀", "value": "value"},
{"op": "list"},
{"op": "list", "prefix": "doc/"},
{"op": "list", "prefix": "missing"},
{"op": "get", "key": c},
{"op": "evaluate", "key": c},
{"op": "evaluate", "key": a},
{"op": "get", "key": b},
{"op": "put", "key": c, "value": None},
{"op": "status"},
],
)
add(
"store-errors",
[
{"op": "put", "key": "", "value": 1},
{"op": "put", "key": a},
{"op": "put", "key": 1, "value": None},
{"op": "get"},
{"op": "delete", "key": []},
{"op": "list", "prefix": None},
{"op": "evaluate", "key": "missing"},
{"op": "status"},
],
)
if level == 1:
add(
"unsupported-transactions",
[
{"op": "begin"},
{"op": "batch", "commands": []},
{"op": "history"},
{"op": "status"},
],
)
if level >= 2:
add(
"compare-and-swap",
[
{"op": "put", "key": a, "value": value, "if_revision": 0},
{"op": "put", "key": b, "value": value, "if_revision": 0},
{"op": "delete", "key": "missing", "if_revision": 0},
{"op": "delete", "key": a, "if_revision": True},
{"op": "put", "key": a, "value": other, "if_revision": -1},
{"op": "put", "key": a, "value": other, "if_revision": 1},
{"op": "get", "key": a},
{"op": "delete", "key": a, "if_revision": 2},
{"op": "status"},
],
)
add(
"atomic-batches",
[
{"op": "batch", "commands": []},
{
"op": "batch",
"commands": [
{"op": "put", "key": a, "value": value},
{"op": "evaluate", "key": a},
{"op": "status"},
],
},
{
"op": "batch",
"commands": [
{"op": "put", "key": a, "value": other},
{"op": "delete", "key": "missing"},
],
},
{"op": "get", "key": a},
{"op": "status"},
{
"op": "batch",
"commands": [
{"op": "delete", "key": a},
{"op": "put", "key": b, "value": other, "if_revision": 2},
{"op": "list"},
],
},
{"op": "list"},
{"op": "status"},
],
)
add(
"batch-errors",
[
{
"op": "batch",
"commands": [
{"op": "put", "key": a, "value": value},
{"op": "begin"},
],
},
{
"op": "batch",
"commands": [
{"op": "put", "key": a, "value": value},
{"op": "batch", "commands": []},
],
},
{"op": "batch", "commands": [None]},
{"op": "batch", "commands": {}},
{"op": "batch", "commands": [{"op": "status"}] * 33},
{
"op": "batch",
"commands": [
{"op": "put", "key": a, "value": {}},
{"op": "evaluate", "key": a},
],
},
{"op": "status"},
{"op": "list"},
],
)
add(
"nested-transactions",
[
{"op": "commit"},
{"op": "rollback"},
{"op": "put", "key": a, "value": value},
{"op": "begin"},
{"op": "put", "key": a, "value": other},
{"op": "begin"},
{"op": "delete", "key": a},
{"op": "put", "key": b, "value": value},
{"op": "commit"},
{"op": "status"},
{"op": "rollback"},
{"op": "get", "key": a},
{"op": "get", "key": b},
{"op": "status"},
{"op": "begin"},
{"op": "delete", "key": a},
{"op": "rollback"},
{"op": "get", "key": a},
{"op": "begin"},
{"op": "put", "key": b, "value": other},
{"op": "commit"},
{"op": "status"},
],
)
add(
"transaction-limits",
[{"op": "begin"}] * 9
+ [{"op": "put", "key": a, "value": value}]
+ [{"op": "rollback"}] * 9
+ [{"op": "status"}],
)
if level == 2:
add(
"unsupported-durability",
[{"op": "history"}, {"op": "scan"}, {"op": "status"}],
)
if level == 3:
add(
"durable-writes",
[
{"op": "put", "key": a, "value": value},
{"op": "put", "key": b, "value": other},
],
[
{"op": "status"},
{"op": "get", "key": a},
{"op": "evaluate", "key": b},
{"op": "delete", "key": a},
],
[
{"op": "list"},
{"op": "get", "key": a},
{"op": "history"},
{"op": "status"},
],
)
add(
"durable-commit",
[
{"op": "begin"},
{"op": "put", "key": a, "value": value},
{"op": "begin"},
{"op": "put", "key": b, "value": other},
{"op": "commit"},
{"op": "commit"},
],
[
{"op": "get", "key": a},
{"op": "get", "key": b},
{"op": "history"},
{"op": "status"},
],
)
add(
"discard-open-transactions",
[
{"op": "put", "key": a, "value": value},
{"op": "begin"},
{"op": "put", "key": a, "value": other},
{"op": "begin"},
{"op": "put", "key": b, "value": value},
{"op": "commit"},
{"op": "status"},
],
[
{"op": "get", "key": a},
{"op": "get", "key": b},
{"op": "history"},
{"op": "status"},
{"op": "rollback"},
],
)
add(
"pagination-and-history",
[
{"op": "put", "key": b, "value": other},
{"op": "put", "key": a, "value": value},
{"op": "put", "key": c, "value": None},
{"op": "scan", "prefix": "doc/", "limit": 1},
{"op": "scan", "prefix": "doc/", "after": a, "limit": 1},
{"op": "scan", "limit": 0},
{"op": "scan", "after": "zzzz"},
{"op": "scan"},
{"op": "history", "after": 1, "limit": 1},
{"op": "delete", "key": a},
{"op": "history", "after": 2},
{"op": "begin"},
{"op": "put", "key": c, "value": {}},
{"op": "history"},
{"op": "rollback"},
{"op": "history"},
],
)
add(
"durable-errors",
[
{"op": "history", "after": True},
{"op": "history", "limit": 101},
{"op": "history", "limit": -1},
{"op": "scan", "prefix": None},
{"op": "scan", "after": 2},
{"op": "scan", "limit": False},
{"op": "get", "key": a, "token": "bad"},
{"op": "put", "key": a, "value": 1, "token": ""},
{
"op": "batch",
"commands": [
{"op": "put", "key": a, "value": value, "token": "nested"}
],
},
{"op": "status"},
{"op": "history"},
],
)
first = {
"op": "put",
"key": a,
"value": value,
"if_revision": 0,
"token": "request/α",
}
add(
"idempotency-and-restart",
[
first,
dict(reversed(list(first.items()))),
dict(first, value=other if other != value else None),
{"op": "status"},
],
[
first,
{"op": "history"},
{"op": "status"},
{"op": "delete", "key": a, "token": "delete"},
{"op": "delete", "key": a, "token": "delete"},
],
[
{"op": "delete", "key": a, "token": "delete"},
{"op": "history"},
{"op": "status"},
],
)
add(
"failed-tokens-and-rollback",
[
{"op": "delete", "key": a, "token": "retry"},
{"op": "put", "key": a, "value": value},
{"op": "delete", "key": a, "token": "retry"},
{"op": "begin"},
{"op": "put", "key": b, "value": other, "token": "tx"},
{"op": "rollback"},
{"op": "put", "key": b, "value": value, "token": "tx"},
{
"op": "batch",
"token": "atomic",
"commands": [
{"op": "put", "key": c, "value": other},
{"op": "delete", "key": b},
],
},
{
"op": "batch",
"token": "atomic",
"commands": [
{"op": "put", "key": c, "value": other},
{"op": "delete", "key": b},
],
},
{"op": "history"},
{"op": "status"},
],
[
{"op": "history"},
{"op": "status"},
{"op": "put", "key": b, "value": value, "token": "tx"},
],
)
add(
"volatile-token-discard",
[
{"op": "begin"},
{"op": "put", "key": a, "value": value, "token": "volatile"},
{"op": "begin"},
{"op": "commit"},
],
[
{"op": "put", "key": a, "value": other, "token": "volatile"},
{"op": "history"},
{"op": "status"},
],
)
return traces
def expected_scenarios(family, tier, seed=1729):
"""Materialize static expectations once; grading never loads reference code."""
traces = scenarios(family, tier, seed)
for trace in traces:
service = Service(family, tier)
durable = copy.deepcopy(service.state)
for session in trace["sessions"]:
service = Service(family, tier)
if tier == "max":
service.state = copy.deepcopy(durable)
expected = []
for request in session["requests"]:
try:
parsed = (
json.loads(request) if isinstance(request, str) else request
)
except json.JSONDecodeError:
response = {"ok": False, "error": "invalid_request"}
else:
response = service.request(parsed)
expected.append(response)
if tier == "max" and not service.stack:
durable = copy.deepcopy(service.state)
session["expected"] = expected
return traces