File size: 12,304 Bytes
54903c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env python3
"""Export the whole measurement corpus as a HuggingFace-ready dataset.

Three files, because three different questions are asked of this corpus and one table cannot serve
all of them:

  runs.jsonl      one object per agent run - the unit a pass/fail verdict attaches to. This is the
                  table for "which models pass what", and it carries the verdict, the shortfall, the
                  tool histogram, the refusal histogram and the guidance histogram inline.

  events.jsonl    one object per ledger event, ~127k rows. This is the raw record and the reason the
                  dataset is worth releasing at all: it lets a reader recompute every number in the
                  paper, and disagree with the taxonomy without re-running anything. The taxonomy in
                  the paper is an interpretation OF this file, not a property of it.

  refusals.jsonl  every refused or declined tool call, with the tool, the reason code and the
                  validator's field paths. Split out because it is the paper's central evidence and
                  is otherwise buried under the event stream.

Redaction: the ledger is server-authored and holds no credentials by construction, but this export
leaves the machine, so `scrub` below drops the two fields that could carry a key if a future event
shape ever did, and every value is passed through a secret pattern check by the caller. The sample
database is the synthetic employee/department/salary schema shipped with the product; no real data
was ever connected during these runs.
"""
import glob
import json
import os
import sys
from collections import Counter, defaultdict

WORKTREES = [
    "/home/researcher/projects/libredb/lb-guncel",
    "/home/researcher/projects/libredb/lb-yeni",
    "/home/researcher/projects/libredb/lb-main-test",
    "/home/researcher/projects/libredb/lb-fix",
    "/home/researcher/projects/libredb/libredb-studio",
]

# Never exported, whatever a future event shape puts in them.
DROP_KEYS = {"apiKey", "api_key", "authorization", "token", "password", "secret", "connectionString"}

CLOCK = {"model-timeout", "turn-limit", "deadline-exceeded"}


def scrub(value):
    if isinstance(value, dict):
        return {k: scrub(v) for k, v in value.items() if k not in DROP_KEYS}
    if isinstance(value, list):
        return [scrub(v) for v in value]
    return value


def read_event(path):
    try:
        with open(path, "rb") as handle:
            raw = handle.read()
        return json.loads(raw[1:].decode("utf-8", "replace")).get("event") or {}
    except Exception:
        return {}


def collect(worktree):
    runs = defaultdict(list)
    pattern = os.path.join(worktree, ".workflow-data/streams/chunks/agent-ledger-*.bin")
    for path in glob.glob(pattern):
        base = os.path.basename(path)
        run_id = base.split("agent-ledger-", 1)[1].rsplit("-chnk_", 1)[0]
        runs[run_id].append(path)
    return runs


def classify(stop_reason, tools_invoked, outcome):
    """The paper's four-class taxonomy, computed here so the dataset carries it as a column.

    Stated as code rather than prose so a reader can disagree with it precisely: the column is an
    interpretation, `events.jsonl` is the evidence, and swapping this function reclassifies the
    corpus without touching the data.
    """
    if outcome != "unanswered":
        return ""
    if stop_reason in CLOCK:
        return "clock"
    if tools_invoked == 0:
        return "capability"
    if stop_reason == "report-composed":
        return "verification"
    return "transport"


def main():
    out_dir = sys.argv[1] if len(sys.argv) > 1 else "/home/researcher/Desktop/makale/dataset"
    os.makedirs(out_dir, exist_ok=True)
    runs_path = os.path.join(out_dir, "runs.jsonl")
    events_path = os.path.join(out_dir, "events.jsonl")
    refusals_path = os.path.join(out_dir, "refusals.jsonl")

    n_runs = n_events = n_ref = 0
    with (
        open(runs_path, "w") as runs_out,
        open(events_path, "w") as events_out,
        open(refusals_path, "w") as refusals_out,
    ):
        for worktree in WORKTREES:
            tree = os.path.basename(worktree)
            grouped = collect(worktree)
            print(f"{tree}: {len(grouped)} run", flush=True)
            for run_id, paths in grouped.items():
                events = [e for e in (read_event(p) for p in paths) if e]
                if not events:
                    continue
                events.sort(key=lambda e: e.get("atMs") or 0)
                base_ms = events[0].get("atMs") or 0

                record = {
                    "run_id": run_id,
                    "worktree": tree,
                    "model": "",
                    "provider": "",
                    "tuning_origin": "",
                    "mode": "",
                    "surface": "",
                    "verifier": "",
                    "status": "",
                    "stop_reason": "",
                    "outcome": "",
                    "unmet": [],
                    "elapsed_ms": (events[-1].get("atMs") or 0) - base_ms,
                    "event_count": len(events),
                    "tools": {},
                    "refusals": {},
                    "guidance": {},
                    "stopped_saying_chars": 0,
                    "reports_composed": 0,
                    "answers_composed": 0,
                    "recommendations": 0,
                    "loss_class": "",
                    # Rank of this run's start within its worktree, filled after the pass below.
                    #
                    # The wall clock is deliberately NOT exported - when this machine ran a sweep is
                    # not part of the finding - but ORDER is load-bearing: the protocol scores a cell
                    # as five CONSECUTIVE passes, and consecutiveness cannot be recovered from a
                    # pooled corpus without it. A rank gives a reader the sequence and not the date.
                    "order": -1,
                    "started_at_ms_raw": base_ms,
                }
                tools, refusals, guidance = Counter(), Counter(), Counter()

                for index, event in enumerate(events):
                    kind = event.get("kind")
                    clean = scrub(event)
                    # Offsets rather than wall-clock: the absolute timestamps say when this machine
                    # ran a sweep, which is not part of the finding, and an offset is what a reader
                    # needs to see the shape of a run.
                    clean.pop("atMs", None)
                    events_out.write(
                        json.dumps(
                            {
                                "run_id": run_id,
                                "worktree": tree,
                                "seq": index,
                                "offset_ms": (event.get("atMs") or 0) - base_ms,
                                "kind": kind,
                                "event": clean,
                            },
                            ensure_ascii=False,
                        )
                        + "\n"
                    )
                    n_events += 1

                    if kind == "driver-resolved":
                        record["model"] = event.get("modelId") or ""
                        record["provider"] = event.get("provider") or ""
                        record["tuning_origin"] = (event.get("tuning") or {}).get("origin") or ""
                    elif kind == "run-started":
                        record["mode"] = event.get("mode") or ""
                    elif kind == "run-finished":
                        record["status"] = event.get("status") or ""
                        record["stop_reason"] = event.get("stopReason") or ""
                        verdict = event.get("goalVerdict") or {}
                        record["outcome"] = verdict.get("outcome") or ""
                        record["unmet"] = verdict.get("unmet") or []
                        verifier = verdict.get("verifier") or ""
                        record["verifier"] = verifier
                        stem = verifier.rsplit(".", 1)[0]
                        record["surface"] = stem.removeprefix("agent-") if stem else ""
                    elif kind == "tool-invoked":
                        tools[event.get("tool") or event.get("toolName") or "?"] += 1
                    elif kind == "guidance-issued":
                        guidance[event.get("notice") or "?"] += 1
                    elif kind == "model-stopped-saying":
                        record["stopped_saying_chars"] += len(event.get("text") or "")
                    elif kind == "report-composed":
                        record["reports_composed"] += 1
                    elif kind == "answer-composed":
                        record["answers_composed"] += 1
                    elif kind == "recommendation":
                        record["recommendations"] += 1

                    if kind == "call-declined":
                        key = f"{event.get('tool')}:{event.get('reasonCode')}"
                        refusals[key] += 1
                        refusals_out.write(
                            json.dumps(
                                {
                                    "run_id": run_id,
                                    "worktree": tree,
                                    "offset_ms": (event.get("atMs") or 0) - base_ms,
                                    "channel": "call-declined",
                                    "tool": event.get("tool") or "",
                                    "reason_code": event.get("reasonCode") or "",
                                    "detail": event.get("detail") or "",
                                },
                                ensure_ascii=False,
                            )
                            + "\n"
                        )
                        n_ref += 1
                    elif kind == "tool-refused":
                        refusal = event.get("refusal") or {}
                        key = f"tool:{refusal.get('class', '?')}"
                        refusals[key] += 1
                        refusals_out.write(
                            json.dumps(
                                {
                                    "run_id": run_id,
                                    "worktree": tree,
                                    "offset_ms": (event.get("atMs") or 0) - base_ms,
                                    "channel": "tool-refused",
                                    "tool": "",
                                    "reason_code": refusal.get("class") or "",
                                    "detail": refusal.get("message") or "",
                                },
                                ensure_ascii=False,
                            )
                            + "\n"
                        )
                        n_ref += 1

                record["tools"] = dict(tools)
                record["refusals"] = dict(refusals)
                record["guidance"] = dict(guidance)
                record["loss_class"] = classify(
                    record["stop_reason"], sum(tools.values()), record["outcome"]
                )
                runs_out.write(json.dumps(record, ensure_ascii=False) + "\n")
                n_runs += 1

    # Second pass over runs.jsonl only: rank within worktree, then drop the raw clock.
    by_tree = defaultdict(list)
    with open(runs_path) as handle:
        records = [json.loads(line) for line in handle]
    for record in records:
        by_tree[record["worktree"]].append(record)
    for tree_records in by_tree.values():
        tree_records.sort(key=lambda r: r["started_at_ms_raw"])
        for rank, record in enumerate(tree_records):
            record["order"] = rank
    with open(runs_path, "w") as handle:
        for record in records:
            record.pop("started_at_ms_raw", None)
            handle.write(json.dumps(record, ensure_ascii=False) + "\n")

    print(f"runs={n_runs} events={n_events} refusals={n_ref}", flush=True)
    print(f"-> {runs_path}\n-> {events_path}\n-> {refusals_path}", flush=True)


if __name__ == "__main__":
    main()