File size: 6,245 Bytes
80cd1f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""Export REAL agent traces for the Hub (Build Small "Sharing is Caring" badge).

Captures two genuine traces from the live, fully-local stack:
1. CASE GENERATION - every prompt the pipeline sends to the in-process llama.cpp model
   and the raw completion that came back, for one complete authored case;
2. LIVE INTERROGATION - a short playthrough against the served case: questions (one with
   evidence presented), the suspect's spoken reply, and the server-authoritative
   suspicion/flags, with wall-clock latency per turn.

Writes ``traces/case0_traces.jsonl`` + ``traces/README.md``. Upload with:

    python scripts/export_traces.py            # produce the files
    python scripts/export_traces.py --push     # produce AND push to the Hub dataset
"""

from __future__ import annotations

import json
import sys
import time
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / "src"))

from case_zero.config import get_settings  # noqa: E402
from case_zero.generator.pipeline import generate_case  # noqa: E402
from case_zero.llm.backend import GenParams, LLMBackend, make_backend  # noqa: E402

DATASET_ID = "HusseinEid/case0-traces"
OUT_DIR = ROOT / "traces"

_DATASET_README = """---
license: apache-2.0
tags:
  - build-small-hackathon
  - agent-trace
  - text-generation
pretty_name: Case Zero agent traces
---

# Case Zero - agent traces

Real traces from [Case Zero](https://huggingface.co/spaces/build-small-hackathon/case0),
a procedural detective game where a single **Qwen2.5-1.5B** model (in-process llama.cpp,
CPU-only, no cloud APIs) authors a complete mystery and then role-plays every suspect
live under interrogation.

`case0_traces.jsonl` - one JSON object per line:

- `type: "generation_call"` - one pipeline LLM call while authoring a case: the exact
  `prompt`, the raw `completion`, sampling params, and latency. Two calls author a full
  case (world+cast, then mystery); deterministic Python assembles and solver-checks it.
- `type: "interrogation_turn"` - one live turn against the running game server: the
  player's `question` (optionally `presented_clue`), the suspect's spoken `reply`, and
  the server-authoritative `suspicion` / `flags` that came back, with latency.

Everything was produced by the shipped game code - no hand-editing, no cloud calls.
"""


class _TracingBackend:
    """Wraps the real backend and records every (prompt, completion) pair."""

    def __init__(self, inner: LLMBackend) -> None:
        self._inner = inner
        self.calls: list[dict] = []

    def generate(self, prompt: str, params: GenParams) -> str:
        t0 = time.time()
        out = self._inner.generate(prompt, params)
        self.calls.append({
            "type": "generation_call",
            "prompt": prompt,
            "completion": out,
            "temperature": params.temperature,
            "max_tokens": params.max_tokens,
            "constrained": bool(params.grammar or params.json_schema),
            "latency_s": round(time.time() - t0, 2),
        })
        return out

    def stream(self, prompt: str, params: GenParams):
        yield self.generate(prompt, params)


def _generation_trace(records: list[dict]) -> None:
    backend = _TracingBackend(make_backend(get_settings()))
    result = generate_case(backend, seed=77321)
    for call in backend.calls:
        records.append(call)
    records.append({
        "type": "generation_result",
        "case_id": result.case.case_id,
        "crime_kind": result.case.crime_kind.value,
        "title": result.case.title,
        "solvable": result.report.ok,
        "attempts": result.attempts,
        "n_suspects": len(result.case.suspects),
        "n_clues": len(result.case.clues),
    })


def _interrogation_trace(records: list[dict]) -> None:
    from starlette.testclient import TestClient

    from case_zero.api.server import build_server

    client = TestClient(build_server())
    case = client.post("/api/case", json={}).json()
    run_id = case["runId"]
    pub = case["case"]
    records.append({
        "type": "case_served",
        "case_id": pub["id"],
        "kind": pub.get("kind", "homicide"),
        "title": pub["title"],
    })
    sus = pub["suspects"]
    breaking = pub["evidence"][0]["id"]
    plan = [
        (sus[0]["id"], "Where were you when it happened?", None),
        (sus[0]["id"], "Did you have any quarrel with the victim?", None),
        (sus[1]["id"], "Walk me through your evening, minute by minute.", None),
        (sus[1]["id"], "Explain this.", breaking),
    ]
    for sus_id, question, clue in plan:
        t0 = time.time()
        body: dict = {"freeText": question}
        if clue:
            body["presentEvidenceId"] = clue
        r = client.post(f"/api/run/{run_id}/interrogate/{sus_id}", json=body).json()
        records.append({
            "type": "interrogation_turn",
            "suspect": sus_id,
            "question": question,
            "presented_clue": clue,
            "reply": r.get("reply"),
            "suspicion": r.get("suspicion"),
            "suspicion_delta": r.get("suspicionDelta"),
            "flags": r.get("flags"),
            "latency_s": round(time.time() - t0, 2),
        })


def main() -> int:
    OUT_DIR.mkdir(parents=True, exist_ok=True)
    records: list[dict] = []
    print("tracing one full case generation (two model calls)...")
    _generation_trace(records)
    print("tracing a live interrogation playthrough...")
    _interrogation_trace(records)

    path = OUT_DIR / "case0_traces.jsonl"
    with path.open("w", encoding="utf-8") as fh:
        for rec in records:
            fh.write(json.dumps(rec, ensure_ascii=False) + "\n")
    (OUT_DIR / "README.md").write_text(_DATASET_README, encoding="utf-8")
    print(f"wrote {len(records)} records -> {path}")

    if "--push" in sys.argv:
        from huggingface_hub import HfApi

        api = HfApi()
        api.create_repo(DATASET_ID, repo_type="dataset", exist_ok=True)
        api.upload_folder(repo_id=DATASET_ID, repo_type="dataset", folder_path=str(OUT_DIR))
        print(f"pushed -> https://huggingface.co/datasets/{DATASET_ID}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())