File size: 3,696 Bytes
fd11263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from __future__ import annotations

import hashlib
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Callable, Mapping


@dataclass
class CallableLLMBackend:
    """Test-only structured completion adapter."""

    kind = "callable"
    provider = "callable"

    call: Callable[[str, Mapping[str, Any]], Mapping[str, Any]]
    model: str = "configured-api-model"
    name: str = "callable"

    def complete(
        self,
        prompt: str,
        *,
        output_schema: Mapping[str, Any],
    ) -> Mapping[str, Any]:
        return self.call(prompt, output_schema)


def write_canonical_dataset(
    root: Path,
    cases: list[dict[str, Any]],
    *,
    metadata_schema: dict[str, Any] | None = None,
    dataset_id: str = "demo",
    dataset_version: str = "v1",
) -> Path:
    root.mkdir()
    schema = metadata_schema or {"type": "object"}
    raw = b"".join((json.dumps(case, ensure_ascii=False) + "\n").encode("utf-8") for case in cases)
    schema_bytes = (json.dumps(schema, ensure_ascii=False, sort_keys=True, separators=(",", ":")) + "\n").encode("utf-8")
    (root / "cases.jsonl").write_bytes(raw)
    (root / "manifest.json").write_text(json.dumps({
        "schema_version": "dataset/v3",
        "dataset_id": dataset_id,
        "dataset_version": dataset_version,
        "case_count": len(cases),
        "case_ids": [case.get("case_id") for case in cases],
        "cases_file": "cases.jsonl",
        "cases_sha256": hashlib.sha256(raw).hexdigest(),
        "source_root": ".",
        "loader": "tests.support:load_cases",
        "dataset_config_sha256": "0" * 64,
        "metadata_schema_sha256": hashlib.sha256(schema_bytes).hexdigest(),
        "metadata_schema": schema,
        "builder_version": "test-builder-v1",
    }, ensure_ascii=False) + "\n", encoding="utf-8")
    return root


def profile_text(evaluators: str, *, protocol: str = "eval-events/v1", event_carriers: str = "[llm_message, tool_call]", preserve_static: bool = False) -> str:
    # Keep test call sites concise while emitting the real v3 profile shape.
    # Individual tests can still override malformed instrumentation or unknown
    # evaluator IDs to exercise the parser/registry errors.
    import yaml

    parsed = yaml.safe_load("evaluators:\n" + evaluators) or {"evaluators": {}}
    for family, entries in parsed.get("evaluators", {}).items():
        if not isinstance(entries, list):
            continue
        for entry in entries:
            if not isinstance(entry, dict):
                continue
            evaluator_id = entry.get("id", "")
            entry.setdefault("params", {})
            # Profiles select an evaluator; static contract fields are owned
            # by the registry. Keep the helper's generated fixtures aligned
            # with the minimal v3 format.
            if not preserve_static:
                for key in ("family", "engine", "result_kind", "scope", "expansion",
                            "aggregation", "direction", "version"):
                    entry.pop(key, None)
            if isinstance(entry.get("llm"), dict):
                entry["llm"] = {
                    key: value for key, value in entry["llm"].items()
                    if key in {"backend", "model"}
                }
    body = yaml.safe_dump(parsed.get("evaluators", {}), sort_keys=False).splitlines()
    return (
        "schema_version: skill-evaluation/v3\n"
        "evaluation:\n"
        "  instrumentation:\n"
        f"    protocol: {protocol}\n"
        f"    event_carriers: {event_carriers}\n"
        "  evaluators:\n"
        + "\n".join("    " + line for line in body)
        + "\n"
    )