File size: 9,551 Bytes
9589849
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
"""Convert public mini-swe-agent trajectories to Pi/Qwen structured tool chats."""

from __future__ import annotations

import argparse
import json
import re
from pathlib import Path

from datasets import Dataset, load_dataset

SOURCE = "Kwai-Klear/SWE-smith-mini_swe_agent_plus-trajectories-66k"
BASH_BLOCK = re.compile(r"```(?:bash|sh)\s*\n(.*?)\n```", re.DOTALL | re.IGNORECASE)
FINAL_MARKER = "MINI_SWE_AGENT_FINAL_OUTPUT"

PI_SYSTEM_PROMPT = """You are an expert coding assistant operating inside pi, a coding agent harness. You help users by reading files, executing commands, editing code, and writing new files.

Available tools:
- read: Read file contents
- bash: Execute bash commands
- edit: Make precise file edits with exact text replacement
- write: Create or overwrite files

Guidelines:
- Inspect the repository and relevant instructions before changing code.
- Use tools to make the requested change; do not merely describe commands.
- Prefer focused searches and reads over dumping large files.
- Make minimal, targeted edits and preserve unrelated behavior.
- Run relevant focused tests after editing.
- When the task is complete, stop using tools and give a concise final summary.
- Be concise in your responses.
- Show file paths clearly when working with files."""

PI_TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "read",
            "description": "Read the contents of a file.",
            "parameters": {
                "type": "object",
                "properties": {
                    "path": {"type": "string"},
                    "offset": {"type": "number"},
                    "limit": {"type": "number"},
                },
                "required": ["path"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "bash",
            "description": "Execute a bash command in the current working directory.",
            "parameters": {
                "type": "object",
                "properties": {
                    "command": {"type": "string"},
                    "timeout": {"type": "number"},
                },
                "required": ["command"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "edit",
            "description": "Edit one file using exact text replacement.",
            "parameters": {
                "type": "object",
                "properties": {
                    "path": {"type": "string"},
                    "edits": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "oldText": {"type": "string"},
                                "newText": {"type": "string"},
                            },
                            "required": ["oldText", "newText"],
                        },
                    },
                },
                "required": ["path", "edits"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "write",
            "description": "Create or overwrite a file.",
            "parameters": {
                "type": "object",
                "properties": {
                    "path": {"type": "string"},
                    "content": {"type": "string"},
                },
                "required": ["path", "content"],
            },
        },
    },
]

TOOLS_JSON = json.dumps(PI_TOOLS, separators=(",", ":"))


def message(
    role: str,
    content: str,
    *,
    reasoning_content: str | None = None,
    tool_calls: list[dict] | None = None,
    tool_call_id: str | None = None,
    name: str | None = None,
) -> dict:
    return {
        "role": role,
        "content": content,
        "reasoning_content": reasoning_content,
        "tool_calls": tool_calls,
        "tool_call_id": tool_call_id,
        "name": name,
    }


def clean_reasoning(text: str) -> str:
    text = text.strip()
    text = re.sub(r"^THOUGHT:\s*", "", text, flags=re.IGNORECASE)
    return text.strip()


def convert(example: dict) -> dict:
    source_messages = example["messages"]
    converted = [message("system", PI_SYSTEM_PROMPT)]
    pending_call_id: str | None = None
    call_index = 0
    initial_user_seen = False
    malformed_actions = 0

    for source in source_messages:
        role = source["role"]
        content = source.get("content") or ""

        if role == "system":
            continue

        if role == "user":
            if pending_call_id is not None:
                converted.append(
                    message(
                        "tool",
                        content,
                        tool_call_id=pending_call_id,
                        name="bash",
                    )
                )
                pending_call_id = None
            else:
                converted.append(message("user", content))
                initial_user_seen = True
            continue

        if role != "assistant":
            continue

        match = BASH_BLOCK.search(content)
        is_final = FINAL_MARKER in content
        if is_final:
            converted.append(
                message(
                    "assistant",
                    "Implemented the requested change and verified it with focused checks.",
                )
            )
            pending_call_id = None
            continue

        if match is None:
            malformed_actions += 1
            converted.append(message("assistant", content.strip()))
            pending_call_id = None
            continue

        reasoning = clean_reasoning(content[: match.start()])
        command = match.group(1).strip()
        call_index += 1
        pending_call_id = f"call_{call_index:04d}"
        converted.append(
            message(
                "assistant",
                "",
                reasoning_content=reasoning,
                tool_calls=[
                    {
                        "id": pending_call_id,
                        "type": "function",
                        "function": {
                            "name": "bash",
                            "arguments": json.dumps(
                                {"command": command}, separators=(",", ":")
                            ),
                        },
                    }
                ],
            )
        )

    if not initial_user_seen:
        raise ValueError(f"trajectory {example['instance_id']} has no user request")

    if converted[-1]["role"] != "assistant" or converted[-1]["tool_calls"]:
        converted.append(
            message(
                "assistant",
                "Implemented the requested change and verified it with focused checks.",
            )
        )

    return {
        "instance_id": example["instance_id"],
        "messages": converted,
        "tools": TOOLS_JSON,
        "source": SOURCE,
        "num_tool_calls": call_index,
        "malformed_actions": malformed_actions,
    }


def evaluation_instance_ids(path: Path) -> set[str]:
    """Exact identifiers only, used solely as a decontamination blacklist."""
    if not path.exists():
        return set()
    return {entry.name for entry in path.iterdir() if entry.is_dir()}


def write_shards(dataset: Dataset, output_dir: Path, shards: int) -> None:
    output_dir.mkdir(parents=True, exist_ok=True)
    for index in range(shards):
        shard = dataset.shard(num_shards=shards, index=index, contiguous=True)
        path = output_dir / f"train-{index:05d}-of-{shards:05d}.parquet"
        shard.to_parquet(path)


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--output", type=Path, required=True)
    parser.add_argument("--limit", type=int)
    parser.add_argument("--num-proc", type=int, default=32)
    parser.add_argument("--shards", type=int, default=16)
    parser.add_argument(
        "--eval-id-dir",
        type=Path,
        default=Path("/root/work/shared/tasksets/swe-bench-verified"),
    )
    args = parser.parse_args()

    split = "train" if args.limit is None else f"train[:{args.limit}]"
    raw = load_dataset(SOURCE, split=split)
    blacklist = evaluation_instance_ids(args.eval_id_dir)
    before = len(raw)
    raw = raw.filter(
        lambda instance_id: instance_id not in blacklist,
        input_columns=["instance_id"],
        num_proc=args.num_proc,
        desc="Removing exact evaluation identifiers",
    )
    removed = before - len(raw)
    converted = raw.map(
        convert,
        remove_columns=raw.column_names,
        num_proc=args.num_proc,
        desc="Converting trajectories to Pi structured calls",
    )

    write_shards(converted, args.output, min(args.shards, len(converted)))
    report = {
        "source": SOURCE,
        "source_revision": "750b2c11239fd5e32f97e6cfb9bf80fb9a9a2983",
        "source_rows": before,
        "rows_removed_as_exact_eval_ids": removed,
        "output_rows": len(converted),
        "tool_calls": sum(converted["num_tool_calls"]),
        "malformed_actions": sum(converted["malformed_actions"]),
        "transformation": "fenced bash -> Pi structured bash calls; terminal marker -> concise final response",
    }
    (args.output / "provenance.json").write_text(json.dumps(report, indent=2) + "\n")
    print(json.dumps(report, indent=2))


if __name__ == "__main__":
    main()