-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalidate-core-target-native.py
More file actions
executable file
·354 lines (317 loc) · 12.9 KB
/
Copy pathvalidate-core-target-native.py
File metadata and controls
executable file
·354 lines (317 loc) · 12.9 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
"""Credential-free Claude/Codex native resume checks over readable sessions.
The script prints aggregate anonymous counts only. It imports selected real
sessions into private temporary homes, resumes them in the pinned Docker image
with networking disabled, proves append-only behavior, and removes the homes.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import subprocess
import tempfile
import uuid
from collections import Counter
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from session_migrate.conversion import (
ConversionOptions,
convert_session,
target_import_paths,
)
from session_migrate.errors import SessionMigrateError
from session_migrate.formats import claude, codex, copilot, opencode, pi
from session_migrate.jsonl import write_private_atomic
from session_migrate.model import AgentFormat, EventKind, Role, Session, TargetFormat
PINNED_IMAGE_ID = "sha256:8f170f660813ac358f347fa8a3580139972f3ea7a9fb087834f1da44669d9392"
@dataclass(frozen=True, slots=True)
class SessionSummary:
ordinal: int
source_bytes: int
features: tuple[str, ...]
def main() -> int:
parser = argparse.ArgumentParser()
sources = parser.add_mutually_exclusive_group(required=True)
sources.add_argument("--claude-root", type=Path)
sources.add_argument("--codex-root", type=Path)
sources.add_argument("--pi-root", type=Path)
sources.add_argument("--opencode-export-root", type=Path)
sources.add_argument("--copilot-root", type=Path)
parser.add_argument(
"--include-same-format",
action="store_true",
help="also cold-resume the portable same-format rewrite",
)
parser.add_argument("--count", type=int, default=10)
parser.add_argument("--image", default="basic-claude-uv:latest")
args = parser.parse_args()
source_format, source_root = selected_source(args)
files = source_files(source_format, source_root)
inventory: list[SessionSummary] = []
rejected: Counter[str] = Counter()
for ordinal, path in enumerate(files, start=1):
try:
session = load_source(path, source_format)
except SessionMigrateError as exc:
reason = expected_rejection(source_format, exc)
if reason:
rejected[reason] += 1
continue
raise RuntimeError(f"source parse failed at anonymous session {ordinal}") from None
size = path.stat().st_size
inventory.append(SessionSummary(ordinal, size, classify(session, size)))
selected = select_cases(inventory, args.count)
target_formats = tuple(
target
for target in (TargetFormat.CLAUDE, TargetFormat.CODEX)
if args.include_same_format or target.value != source_format.value
)
image_id = resolved_image_id(args.image)
feature_counts: Counter[str] = Counter()
target_counts: Counter[str] = Counter()
workspace: Path | None = None
with tempfile.TemporaryDirectory(prefix="session-migrate-core-native-") as directory:
workspace = Path(directory)
os.chmod(workspace, 0o700)
for summary in selected:
session = load_source(files[summary.ordinal - 1], source_format)
feature_counts.update(summary.features)
for target in target_formats:
check_one(
session,
ordinal=summary.ordinal,
target=target,
image_id=image_id,
workspace=workspace,
)
target_counts[target.value] += 1
if workspace is None or workspace.exists():
raise RuntimeError("private native validation workspace was not removed")
result: dict[str, Any] = {
"source_format": source_format.value,
"source_files": len(files),
"parsed_sessions": len(inventory),
"expected_rejections": dict(sorted(rejected.items())),
"selected_sessions": len(selected),
"target_checks": dict(sorted(target_counts.items())),
"native_resumes": sum(target_counts.values()),
"exact_generated_prefixes": sum(target_counts.values()),
"claude_append_ancestry_verified": target_counts[TargetFormat.CLAUDE.value],
"codex_sqlite_indexes_rebuilt": target_counts[TargetFormat.CODEX.value],
"feature_counts": dict(sorted(feature_counts.items())),
"docker_image_id": image_id,
"network_enabled": False,
"credentials_mounted": False,
"private_workspace_removed": True,
}
print(json.dumps(result, indent=2, sort_keys=True))
return 0
def selected_source(args: argparse.Namespace) -> tuple[AgentFormat, Path]:
if args.claude_root:
return AgentFormat.CLAUDE, args.claude_root
if args.codex_root:
return AgentFormat.CODEX, args.codex_root
if args.pi_root:
return AgentFormat.PI, args.pi_root
if args.opencode_export_root:
return AgentFormat.OPENCODE, args.opencode_export_root
assert args.copilot_root
return AgentFormat.COPILOT, args.copilot_root
def source_files(source_format: AgentFormat, root: Path) -> list[Path]:
if source_format == AgentFormat.CLAUDE:
return sorted((root / "projects").glob("*/*.jsonl"))
if source_format == AgentFormat.CODEX:
return sorted(
[
*(root / "sessions").glob("*/*/*/rollout-*.jsonl"),
*(root / "archived_sessions").glob("rollout-*.jsonl"),
]
)
if source_format == AgentFormat.PI:
return sorted((root / "sessions").glob("*/*.jsonl"))
if source_format == AgentFormat.OPENCODE:
return sorted(root.glob("*.json"))
return sorted(root.glob("*/events.jsonl"))
def load_source(path: Path, source_format: AgentFormat) -> Session:
if source_format == AgentFormat.CLAUDE:
return claude.parse(path)
if source_format == AgentFormat.CODEX:
return codex.parse(path)
if source_format == AgentFormat.PI:
return pi.parse_session(path)
if source_format == AgentFormat.OPENCODE:
return opencode.parse_session(path)
return copilot.parse_session(path)
def expected_rejection(source_format: AgentFormat, exc: SessionMigrateError) -> str | None:
if source_format != AgentFormat.CODEX:
return None
message = str(exc)
if "history mode" in message and "not supported" in message:
return "codex_history_mode"
if "history_base lineage is not supported" in message:
return "codex_history_base"
return None
def classify(session: Session, source_bytes: int) -> tuple[str, ...]:
kinds = Counter(event.kind for event in session.events)
features: list[str] = []
if kinds[EventKind.TOOL_CALL] or kinds[EventKind.TOOL_RESULT]:
features.append("tools")
if kinds[EventKind.COMPACTION]:
features.append("compaction")
if any(
event.kind == EventKind.CONTEXT and event.payload.get("block_type") == "image"
for event in session.events
) or any(
event.kind == EventKind.TOOL_RESULT
and any(
isinstance(block, dict) and block.get("type") in {"image", "input_image"}
for block in event.payload.get("content_blocks", [])
)
for event in session.events
):
features.append("images")
portable = [
event
for event in session.events
if event.kind in {EventKind.MESSAGE, EventKind.TOOL_CALL, EventKind.TOOL_RESULT}
]
if portable and (portable[-1].role == Role.USER or portable[-1].kind == EventKind.TOOL_CALL):
features.append("interrupted")
if source_bytes >= 1024 * 1024:
features.append("large")
return tuple(features or ["basic"])
def select_cases(sessions: list[SessionSummary], count: int) -> list[SessionSummary]:
count = min(max(count, 0), len(sessions))
selected: list[SessionSummary] = []
for feature in ("compaction", "images", "interrupted", "tools", "large", "basic"):
matches = [item for item in sessions if feature in item.features and item not in selected]
if matches:
selected.append(min(matches, key=lambda item: (item.source_bytes, item.ordinal)))
remaining = sorted(
(item for item in sessions if item not in selected),
key=lambda item: (item.source_bytes, item.ordinal),
)
selected.extend(remaining[: max(0, count - len(selected))])
return selected[:count]
def resolved_image_id(image: str) -> str:
completed = subprocess.run(
["docker", "image", "inspect", image, "--format", "{{.Id}}"],
check=False,
capture_output=True,
text=True,
timeout=20,
)
image_id = completed.stdout.strip()
if completed.returncode != 0 or image_id != PINNED_IMAGE_ID:
raise RuntimeError("native validation requires the pinned Docker image ID")
return image_id
def check_one(
session: Session,
*,
ordinal: int,
target: TargetFormat,
image_id: str,
workspace: Path,
) -> None:
case = workspace / f"case-{ordinal}-{target.value}"
target_home = case / "target"
os_home = case / "home"
for directory in (case, target_home, os_home):
directory.mkdir(mode=0o700)
target_id = str(uuid.uuid5(uuid.NAMESPACE_URL, f"core-native-{target.value}-{ordinal}"))
artifact = convert_session(
session,
ConversionOptions(target_format=target, session_id=target_id, cwd=Path("/work")),
)
native_path, _manifest = target_import_paths(artifact, target_home)
native_path.parent.mkdir(parents=True, mode=0o700)
write_private_atomic(native_path, artifact.native_bytes)
before = native_path.read_bytes()
if before != artifact.native_bytes:
raise RuntimeError(f"native setup mismatch at anonymous session {ordinal}")
command = claude_command() if target == TargetFormat.CLAUDE else codex_command()
completed = subprocess.run(
[
"docker",
"run",
"--rm",
"--network",
"none",
"--user",
f"{os.getuid()}:{os.getgid()}",
"-e",
f"TARGET_ID={target_id}",
"-v",
f"{case}:/state",
"-w",
"/work",
image_id,
"bash",
"-lc",
command,
],
check=False,
capture_output=True,
timeout=40,
)
if completed.returncode != 0:
raise RuntimeError(f"native resume failed at anonymous session {ordinal}")
after = native_path.read_bytes()
if len(after) <= len(before) or not after.startswith(before):
raise RuntimeError(f"native append changed source prefix at anonymous session {ordinal}")
if hashlib.sha256(after[: len(before)]).digest() != hashlib.sha256(before).digest():
raise RuntimeError(f"native prefix hash mismatch at anonymous session {ordinal}")
if target == TargetFormat.CLAUDE:
assert_claude_append(native_path, len(before), ordinal)
elif not (target_home / "state_5.sqlite").is_file():
raise RuntimeError(f"Codex index was not rebuilt at anonymous session {ordinal}")
def claude_command() -> str:
return """
set -eu
log=/state/resume.log
HOME=/state/home CLAUDE_CONFIG_DIR=/state/target timeout 20s \
claude -p --resume "$TARGET_ID" \
"Synthetic offline native-resume validation probe." >"$log" 2>&1 || true
grep -q "Not logged in" "$log"
"""
def codex_command() -> str:
return """
set -eu
log=/state/resume.log
HOME=/state/home CODEX_HOME=/state/target timeout 20s \
codex exec resume --skip-git-repo-check "$TARGET_ID" \
"Synthetic offline native-resume validation probe." >"$log" 2>&1 || true
grep -q "session id: $TARGET_ID" "$log"
test -s /state/target/state_5.sqlite
"""
def assert_claude_append(path: Path, before: int, ordinal: int) -> None:
data = path.read_bytes()
head = [json.loads(line) for line in data[:before].splitlines() if line.strip()]
tail = [json.loads(line) for line in data[before:].splitlines() if line.strip()]
leaf = next(
(
record.get("uuid")
for record in reversed(head)
if record.get("type") in {"user", "assistant"} and record.get("uuid")
),
None,
)
appended = next((record for record in tail if record.get("type") == "user"), None)
if not leaf or not isinstance(appended, dict):
raise RuntimeError(f"Claude append missing at anonymous session {ordinal}")
nodes = {
record["uuid"]: record
for record in tail
if isinstance(record, dict) and isinstance(record.get("uuid"), str)
}
cursor = appended.get("parentUuid")
seen: set[str] = set()
while isinstance(cursor, str) and cursor in nodes and cursor not in seen:
seen.add(cursor)
cursor = nodes[cursor].get("parentUuid")
if cursor != leaf:
raise RuntimeError(f"Claude append ancestry mismatch at anonymous session {ordinal}")
if __name__ == "__main__":
raise SystemExit(main())