"""OpenCode 1.18.31 native export -> shared preservation/review pipeline. CLI events corroborate the native parts; they never replace the session export. The native entry is retained beside the mapped message so the mapping is reversible. """ import re from export.trajectory import TERMINAL_STATES, _events, _json, _observation from harness.runners.opencode import _allowed_tool_definitions VERSION = "1.18.31" # A runner upgrade must explicitly revalidate this native format. SESSION = "agent/raw-session.json" SYSTEM = "agent/raw-system.json" EVENT_TYPES = {"step_start": "step-start", "step_finish": "step-finish", "tool_use": "tool", "text": "text", "reasoning": "reasoning"} def location(source, name): return source.manifest.get(name, {}).get("sha256", "missing") + ":0" def observations(source): """Independent denominator: enumerate raw containers, not converted messages.""" raw = _json(source, SESSION, {}) loc = location(source, SESSION) result = [] for i, entry in enumerate(raw.get("messages", [])): result.append(f"{loc}:/messages/{i}") for j, part in enumerate(entry.get("parts", [])): if part.get("type") == "tool": result.append(f"{loc}:/messages/{i}/parts/{j}/state") result += [f"{loc}:/event" for loc, _ in _events(source)] if SYSTEM in source.files: result.append(f"{location(source, SYSTEM)}:/system") return result def inventory(source): errors = list(source.errors) meta = _json(source, "rollout.json", {}) raw = _json(source, SESSION, {}) identity = _json(source, "agent/session-id.json", {}) if (not all(isinstance(obj, dict) for obj in (meta, raw, identity)) or not isinstance(raw.get("info", {}), dict) or not isinstance(raw.get("messages", []), list)): raise ValueError("invalid OpenCode session envelope") sid = identity.get("session_id") if (meta.get("runner_version") != VERSION or identity.get("version") != VERSION or identity.get("runner") != "opencode"): errors.append("UNSUPPORTED_SOURCE") if meta.get("state") not in TERMINAL_STATES: errors.append("SOURCE_NOT_TERMINAL") if not isinstance(sid, str) or not re.fullmatch(r"ses_[a-zA-Z0-9]+", sid) or raw.get("info", {}).get("id") != sid: errors.append("IDENTITY_PAYLOAD_CONFLICT") messages, by_part, native_ids = [], {}, set() loc = location(source, SESSION) def add(payload, ident, pointer, original, event_type="opencode_message"): item = {"message_id": ident, "payload": payload, "event_type": event_type, "observations": [_observation(loc, pointer, original)]} messages.append(item) return item system = _json(source, SYSTEM, None) tools = _json(source, "agent/tools.json", []) context = {"status": "UNKNOWN", "source": "runner-raw-system"} try: if not isinstance(system, list) or not system or not all(isinstance(s, str) for s in system) or not any(system): raise ValueError("missing system") if tools != _allowed_tool_definitions(_json(source, "agent/raw-tools.json", None)): raise ValueError("tool snapshot mismatch") context["status"] = "COMPLETE" except ValueError: errors.append("CONTEXT_SNAPSHOT_MISSING") if isinstance(system, list) and all(isinstance(s, str) for s in system): item = add({"role": "system", "content": "\n\n".join(system), "opencode_system": system}, f"{location(source, SYSTEM)}:/system", "/system", system, "system_snapshot") item["observations"] = [_observation(location(source, SYSTEM), "/system", system)] entries = raw.get("messages", []) previous_user = None for i, entry in enumerate(entries): if (not isinstance(entry, dict) or not isinstance(entry.get("info"), dict) or not isinstance(entry.get("parts"), list) or not isinstance(entry["info"].get("time", {}), dict) or any(not isinstance(p, dict) or not isinstance(p.get("state", {}), dict) or not isinstance(p.get("state", {}).get("time", {}), dict) for p in entry["parts"])): raise ValueError("invalid OpenCode message or part") info, parts = entry["info"], entry["parts"] mid, role = info.get("id"), info.get("role") if (not isinstance(mid, str) or not re.fullmatch(r"msg_[a-zA-Z0-9]+", mid) or mid in native_ids or info.get("sessionID") != sid): errors.append("IDENTITY_PAYLOAD_CONFLICT") native_ids.add(mid) if role == "user": previous_user = mid elif role == "assistant": if not previous_user or info.get("parentID") != previous_user: errors.append("ORDER_AND_CAUSALITY") if info.get("error") or not info.get("time", {}).get("completed"): errors.append("INCOMPLETE_GENERATION") if info.get("finish") not in ("stop", "tool-calls"): errors.append("INCOMPLETE_GENERATION") else: errors.append("UNSUPPORTED_SOURCE") content = [] payload = {"role": role, "content": content, "opencode": entry} if role == "assistant": payload["stopReason"] = {"tool-calls": "toolUse"}.get(info.get("finish"), info.get("finish")) item = add(payload, f"{sid}/{mid}", f"/messages/{i}", entry) for j, part in enumerate(parts): pid, kind = part.get("id"), part.get("type") if (not isinstance(pid, str) or not re.fullmatch(r"prt_[a-zA-Z0-9]+", pid) or pid in by_part or part.get("messageID") != mid or part.get("sessionID") != sid): errors.append("IDENTITY_PAYLOAD_CONFLICT") by_part[pid] = (part, item, role, (i, j)) if kind in ("text", "reasoning"): if not isinstance(part.get("text"), str) or part.get("ignored") or part.get("synthetic"): errors.append("UNSUPPORTED_SOURCE") content.append({"type": "text" if kind == "text" else "thinking", "text" if kind == "text" else "thinking": part.get("text")}) elif kind == "tool": state = part.get("state", {}) status = state.get("status") if (role != "assistant" or status not in ("completed", "error") or not isinstance(state.get("input"), dict) or not isinstance(state.get("output" if status == "completed" else "error"), str) or state.get("attachments") or state.get("time", {}).get("compacted")): errors.append("INCOMPLETE_TOOL_RESULT") content.append({"type": "toolCall", "id": part.get("callID"), "name": part.get("tool"), "arguments": state.get("input")}) result = {"role": "toolResult", "toolCallId": part.get("callID"), "toolName": part.get("tool"), "isError": status == "error", "content": [{"type": "text", "text": state.get("error" if status == "error" else "output", "")}]} add(result, f"{sid}/{mid}/{pid}/result", f"/messages/{i}/parts/{j}/state", state, "opencode_tool_result") elif kind not in ("step-start", "step-finish"): errors.append("UNSUPPORTED_SOURCE") if role == "assistant": starts = [p for p in parts if p.get("type") == "step-start"] ends = [p for p in parts if p.get("type") == "step-finish"] if (len(starts) != 1 or len(ends) != 1 or ends[0].get("reason") != info.get("finish") or parts[0].get("type") != "step-start" or parts[-1].get("type") != "step-finish"): errors.append("INCOMPLETE_GENERATION") seen, event_order = set(), [] for event_loc, event in _events(source): part = event.get("part", {}) if not isinstance(part, dict): errors.append("SOURCE_EVENT_UNKNOWN") continue candidate = by_part.get(part.get("id")) if (event.get("sessionID") != sid or EVENT_TYPES.get(event.get("type")) != part.get("type") or event.get("type") not in EVENT_TYPES or candidate is None): errors.append("SOURCE_EVENT_UNKNOWN") continue original, item, role, index = candidate if original != part or part["id"] in seen or role != "assistant": errors.append("IDENTITY_PAYLOAD_CONFLICT") seen.add(part["id"]) event_order.append(index) item["observations"].append(_observation(event_loc, "/event", event)) # Reasoning is not emitted by the CLI unless --thinking is set; native parts retain it. required = {pid for pid, (part, _, role, _) in by_part.items() if role == "assistant" and part.get("type") != "reasoning"} if not required <= seen or event_order != sorted(event_order): errors.append("INCOMPLETE_TRAJECTORY") if not entries or entries[0].get("info", {}).get("role") != "user": errors.append("INITIAL_CONTEXT_MISSING") ended = bool(entries and entries[-1].get("info", {}).get("finish") == "stop") if not ended: errors.append("INCOMPLETE_TRAJECTORY") return {"messages": messages, "excluded": [], "operations": [], "compactions": [], "errors": sorted(set(errors)), "tools": tools, "agent_end": ended, "context": context, "format": "opencode-native-messages/1"}