-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_real_session_fixtures.py
More file actions
272 lines (246 loc) · 8.71 KB
/
Copy pathgen_real_session_fixtures.py
File metadata and controls
272 lines (246 loc) · 8.71 KB
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
"""One-off generator for Tuesday real_session_*.jsonl fixtures. Run from repo root:
python scripts/gen_real_session_fixtures.py
Each entry includes top-level ``sessionId`` (real Claude Code JSONL shape). The parser
currently ignores it and uses the filename for ``session_id``.
"""
from __future__ import annotations
import json
import os
FIXTURES = os.path.join(os.path.dirname(__file__), "..", "tests", "fixtures")
CWD = "/sanitized/project/path"
TS = "2026-05-26T10:{:02d}:00Z"
# Sanitized sessionId values (one per fixture file).
SESSION_IDS = {
"minimal": "session-sanitized-minimal",
"all_tool_types": "session-sanitized-all-tool-types",
"nested_tools": "session-sanitized-nested-tools",
"unknown_fields": "session-sanitized-unknown-fields",
"malformed_lines": "session-sanitized-malformed-lines",
}
def _line(obj: dict) -> str:
return json.dumps(obj, ensure_ascii=False) + "\n"
def _stamp(entry: dict, session_id: str) -> dict:
entry["sessionId"] = session_id
return entry
def _user(
minute: int,
text: str = "",
tool_result: dict | None = None,
*,
session_id: str,
sidechain: bool = False,
extra: dict | None = None,
) -> dict:
entry: dict = {
"type": "user",
"timestamp": TS.format(minute),
"cwd": CWD,
"message": {"content": [{"type": "text", "text": text}] if text else []},
}
if tool_result is not None:
entry["toolUseResult"] = tool_result
if sidechain:
entry["isSidechain"] = True
if extra:
entry.update(extra)
return _stamp(entry, session_id)
def _assistant(
minute: int,
content: list,
*,
session_id: str,
model: str = "claude-sanitized",
) -> dict:
return _stamp(
{
"type": "assistant",
"timestamp": TS.format(minute),
"message": {
"model": model,
"content": content,
"usage": {"input_tokens": 10, "output_tokens": 5},
},
},
session_id,
)
def write_minimal() -> None:
sid = SESSION_IDS["minimal"]
lines = [
_user(0, "Sanitized minimal real-shaped session opener", session_id=sid),
_assistant(1, [{"type": "text", "text": "Acknowledged."}], session_id=sid),
_user(
2,
tool_result={"stdout": "sanitized output\n", "stderr": "", "exitCode": 0},
session_id=sid,
),
]
path = os.path.join(FIXTURES, "real_session_minimal.jsonl")
with open(path, "w", encoding="utf-8", newline="\n") as f:
f.writelines(_line(x) for x in lines)
def write_all_tool_types() -> None:
# 16 tool-result user entries: 15 to cover each dispatch predicate once, plus 1
# overlap blob (agentId + totalDurationMs + message) for task_message-beats-
# task_completed dispatch-order regression. Total lines: 2 opener + 16 = 18 messages.
sid = SESSION_IDS["all_tool_types"]
tool_results = [
{"stdout": "ok\n", "stderr": "", "exitCode": 0},
{"filePath": f"{CWD}/a.py", "structuredPatch": "@@ sanitized"},
{"filePath": f"{CWD}/b.txt", "content": "sanitized body"},
{"filenames": ["sanitized.py"], "numFiles": 1, "truncated": False},
{"mode": "content", "numFiles": 1, "numLines": 2, "content": "sanitized match"},
{
"file": {
"filePath": f"{CWD}/readme.md",
"numLines": 3,
"content": "sanitized read",
}
},
{"query": "sanitized query", "results": [{"url": "https://example.com/a"}]},
{"url": "https://example.com/doc", "code": 200, "durationMs": 40},
{"task_id": "task-sanitized-msg", "task_type": "sub"},
{
"retrieval_status": "found",
"task": {"task_id": "task-sanitized-ret", "description": "REDACTED"},
},
{
"agentId": "agent-sanitized-done",
"totalDurationMs": 1000,
"status": "completed",
},
{
"agentId": "agent-sanitized-async",
"isAsync": True,
"status": "running",
"description": "REDACTED background task",
},
{"newTodos": [{"id": "1", "content": "sanitized todo"}]},
{"questions": [{"id": "q1"}], "answers": {"q1": "sanitized answer"}},
{"plan": [], "filePath": f"{CWD}/plan.md"},
# Dispatch-order overlap: message key present — locks task_message winning over completed
{
"agentId": "agent-sanitized-overlap",
"totalDurationMs": 500,
"status": "completed",
"message": "status update sanitized",
},
]
lines = [
_user(0, "Exercise all fifteen tool-result dispatch predicates", session_id=sid),
_assistant(
1,
[{"type": "tool_use", "id": "tu-1", "name": "Bash", "input": {"command": "echo ok"}}],
session_id=sid,
),
]
for i, tr in enumerate(tool_results):
lines.append(_user(2 + i, tool_result=tr, session_id=sid))
path = os.path.join(FIXTURES, "real_session_all_tool_types.jsonl")
with open(path, "w", encoding="utf-8", newline="\n") as f:
f.writelines(_line(x) for x in lines)
def write_nested_tools() -> None:
sid = SESSION_IDS["nested_tools"]
lines = [
_user(0, "Parent turn with nested subagent tool activity", session_id=sid),
_assistant(
1,
[
{
"type": "tool_use",
"id": "parent-tool-1",
"name": "Task",
"input": {"description": "sanitized subagent task"},
}
],
session_id=sid,
),
_user(
2,
tool_result={"stdout": "sidechain output\n", "stderr": "", "exitCode": 0},
session_id=sid,
sidechain=True,
),
_stamp(
{
"type": "progress",
"timestamp": TS.format(3),
"toolUseID": "child-tool-1",
"parentToolUseID": "parent-tool-1",
"isSidechain": True,
"data": {"type": "bash_progress", "output": "sanitized streaming chunk"},
},
sid,
),
_assistant(
4,
[
{
"type": "tool_use",
"id": "nested-read-1",
"name": "Read",
"input": {"file_path": f"{CWD}/nested.txt"},
}
],
session_id=sid,
),
]
path = os.path.join(FIXTURES, "real_session_nested_tools.jsonl")
with open(path, "w", encoding="utf-8", newline="\n") as f:
f.writelines(_line(x) for x in lines)
def write_unknown_fields() -> None:
sid = SESSION_IDS["unknown_fields"]
lines = [
_user(
0,
"Forward-compat entry with unknown top-level keys",
session_id=sid,
extra={"_futureSchemaVersion": 2, "experimentalFlag": True},
),
_assistant(1, [{"type": "text", "text": "Handled unknown fields."}], session_id=sid),
_user(
2,
tool_result={
"stdout": "still bash\n",
"stderr": "",
"exitCode": 0,
"_unknownToolMeta": {"vendor": "sanitized"},
},
session_id=sid,
),
]
path = os.path.join(FIXTURES, "real_session_unknown_fields.jsonl")
with open(path, "w", encoding="utf-8", newline="\n") as f:
f.writelines(_line(x) for x in lines)
def write_malformed_lines() -> None:
sid = SESSION_IDS["malformed_lines"]
valid = [
_user(0, "Valid line before malformed section", session_id=sid),
_assistant(1, [{"type": "text", "text": "Recovered after bad lines."}], session_id=sid),
]
path = os.path.join(FIXTURES, "real_session_malformed_lines.jsonl")
with open(path, "w", encoding="utf-8", newline="\n") as f:
for obj in valid:
f.write(_line(obj))
f.write("\n")
f.write("{not valid json\n")
f.write('{"type": "user", "timestamp": "2026-05-26T10:02:00Z", "message": {"content": ')
f.write("\n")
f.write(
_line(
_user(
3,
"Valid line after malformed section",
tool_result={"stderr": "warn only", "exitCode": 1},
session_id=sid,
)
)
)
def main() -> None:
os.makedirs(FIXTURES, exist_ok=True)
write_minimal()
write_all_tool_types()
write_nested_tools()
write_unknown_fields()
write_malformed_lines()
print("Wrote 5 fixtures to", FIXTURES)
if __name__ == "__main__":
main()