#!/usr/bin/env python3
"""Extract a compact Oracle-ready supplement from a Codex rollout JSONL."""

from __future__ import annotations

import argparse
import glob
import json
import os
import re
import sys
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any


UUID_RE = re.compile(
    r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
)
UUID_SEARCH_RE = re.compile(
    r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
)
HOME = str(Path.home())


SECRET_PATTERNS = [
    (re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----.*?-----END [A-Z ]*PRIVATE KEY-----", re.S), "<REDACTED:PRIVATE_KEY>"),
    (re.compile(r"sk-[A-Za-z0-9_-]{20,}"), "<REDACTED:OPENAI_KEY>"),
    (re.compile(r"gh[pousr]_[A-Za-z0-9_]{20,}"), "<REDACTED:GITHUB_TOKEN>"),
    (re.compile(r"xox[baprs]-[A-Za-z0-9-]{20,}"), "<REDACTED:SLACK_TOKEN>"),
    (re.compile(r"\bAKIA[0-9A-Z]{16}\b"), "<REDACTED:AWS_ACCESS_KEY>"),
    (re.compile(r"(?i)(Authorization:\s*Bearer\s+)[A-Za-z0-9._~+/=-]+"), r"\1<REDACTED:TOKEN>"),
    (re.compile(r"(?i)(Cookie:\s*)[^\n\r]+"), r"\1<REDACTED:COOKIE>"),
    (re.compile(r"(?i)(https?://[^/\s:@]+:)[^@\s/]+(@)"), r"\1<REDACTED:CREDENTIAL>\2"),
    (re.compile(r'(?i)("([^"]*(?:api[_-]?key|token|secret|password|passwd)[^"]*)"\s*:\s*)"[^"]*"'), r'\1"<REDACTED:SECRET>"'),
    (re.compile(r"(?i)\b((?![A-Z0-9_]*(?:TOKEN[_-]?COUNT|COUNT[_-]?TOKEN))[A-Z0-9_]*(?:API[_-]?KEY|TOKEN|SECRET|PASSWORD|PASSWD)[A-Z0-9_]*)\s*=\s*([^\s\"']+)"), r"\1=<REDACTED:SECRET>"),
    (re.compile(r"(?i)\b((?![A-Z0-9_]*(?:TOKEN[_-]?COUNT|COUNT[_-]?TOKEN))[A-Z0-9_]*(?:API[_-]?KEY|TOKEN|SECRET|PASSWORD|PASSWD)[A-Z0-9_]*)\s*:\s*([^\s\"',}]+)"), r"\1: <REDACTED:SECRET>"),
    (re.compile(r"(?i)(password|passwd|token|secret)=([^\s&]+)"), r"\1=<REDACTED:SECRET>"),
]

PRIVATE_MARKER_PATTERNS = [
    (re.compile(r"base_instructions", re.I), "<REDACTED:PRIVATE_MARKER>"),
    (re.compile(r"encrypted_content", re.I), "<REDACTED:PRIVATE_MARKER>"),
    (re.compile(r"encrypted_reasoning", re.I), "<REDACTED:PRIVATE_MARKER>"),
    (re.compile(r"system prompt", re.I), "<REDACTED:PRIVATE_MARKER>"),
    (re.compile(r"developer prompt", re.I), "<REDACTED:PRIVATE_MARKER>"),
    (re.compile(r"developer instructions", re.I), "<REDACTED:PRIVATE_MARKER>"),
    (re.compile(r"OPENAI_API_KEY", re.I), "<REDACTED:SECRET_NAME>"),
    (re.compile(r"Authorization:\s*Bearer", re.I), "<REDACTED:AUTH_HEADER>"),
    (re.compile(r"PRIVATE KEY", re.I), "<REDACTED:PRIVATE_KEY_LABEL>"),
]

PRIVATE_CONTROL_PATTERNS = [
    r"<environment_context\b",
    r"</environment_context>",
    r"<permissions instructions>",
    r"</permissions instructions>",
    r"<collaboration_mode>",
    r"</collaboration_mode>",
    r"\bdeveloper_instructions\b",
    r"\bbase_instructions\b",
    r"\bsandbox_permissions\b",
    r"\bapproval_policy\b",
    r"\bsandbox_mode\b",
    r"\bpermission_profile\b",
    r'"filesystem"\s*:',
    r'"file_system"\s*:',
    r"<filesystem\b",
    r"</filesystem>",
    r"\bencrypted_content\b",
    r"\bencrypted_reasoning\b",
]
PRIVATE_CONTROL_RE = re.compile("|".join(f"(?:{pattern})" for pattern in PRIVATE_CONTROL_PATTERNS), re.I | re.S)


@dataclass
class Snapshot:
    records: list[dict[str, Any]]
    invalid_lines: int
    skipped_partial_trailing_line: bool
    bytes_read: int


@dataclass
class Item:
    index: int
    kind: str
    title: str
    text: str
    score: int


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--slug")
    parser.add_argument("--out")
    parser.add_argument("--session-id")
    parser.add_argument("--surface-id")
    parser.add_argument("--transcript")
    parser.add_argument("--registry", default=str(Path.home() / ".codex/cmux-codex-sessions.json"))
    parser.add_argument("--packx-bundle")
    parser.add_argument("--max-chars", type=int, default=1_200_000)
    parser.add_argument("--max-total-chars", type=int, default=3_600_000)
    parser.add_argument("--reserve-chars", type=int, default=200_000)
    parser.add_argument("--max-tool-output-chars", type=int, default=12_000)
    parser.add_argument("--max-failed-output-chars", type=int, default=30_000)
    parser.add_argument("--recent-turns", type=int, default=12)
    parser.add_argument("--include-success-output", action="store_true")
    parser.add_argument("--dry-run", action="store_true")
    parser.add_argument("--print-stats", action="store_true")
    parser.add_argument("--strict", action="store_true")
    return parser.parse_args()


def expand_path(value: str | None) -> Path | None:
    if not value:
        return None
    return Path(value).expanduser()


def load_registry(path: Path) -> dict[str, Any]:
    try:
        return json.loads(path.read_text(encoding="utf-8"))
    except Exception:
        return {}


def normalize_surface(value: str | None) -> str:
    value = (value or "").strip()
    if not value:
        return ""
    return value if value.startswith("surface:") else f"surface:{value}"


def path_related_to_cwd(cwd: str, current: Path) -> bool:
    if not cwd:
        return False
    try:
        candidate = Path(cwd).expanduser().resolve()
        current = current.resolve()
    except Exception:
        return False
    return candidate == current or candidate in current.parents or current in candidate.parents


def resolve_session_id(args: argparse.Namespace, registry: dict[str, Any]) -> tuple[str | None, str]:
    if args.session_id:
        return args.session_id, "cli --session-id"
    env_thread = os.environ.get("CODEX_THREAD_ID", "").strip()
    if env_thread:
        return env_thread, "CODEX_THREAD_ID"

    surface = normalize_surface(args.surface_id or os.environ.get("CMUX_SURFACE_ID"))
    if surface:
        entry = registry.get("by_surface", {}).get(surface, {})
        session_id = str(entry.get("session_id") or "").strip()
        if session_id:
            return session_id, "registry by surface"

    current = Path.cwd()
    candidates = []
    for entry in registry.get("by_session", {}).values():
        if path_related_to_cwd(str(entry.get("cwd") or ""), current):
            candidates.append(entry)
    candidates.sort(key=lambda e: str(e.get("updated_at") or ""), reverse=True)
    if candidates:
        session_id = str(candidates[0].get("session_id") or "").strip()
        if session_id:
            return session_id, "newest registry session for cwd"

    return None, "unresolved"


def resolve_transcript_path(
    args: argparse.Namespace, registry: dict[str, Any], session_id: str
) -> tuple[Path | None, str]:
    explicit = expand_path(args.transcript)
    if explicit:
        return explicit, "cli --transcript"

    by_session = registry.get("by_session", {}).get(session_id, {})
    path_value = str(by_session.get("transcript_path") or "").strip()
    if path_value:
        return Path(path_value).expanduser(), "registry by session"

    surface = normalize_surface(args.surface_id or os.environ.get("CMUX_SURFACE_ID"))
    if surface:
        entry = registry.get("by_surface", {}).get(surface, {})
        if str(entry.get("session_id") or "") == session_id:
            path_value = str(entry.get("transcript_path") or "").strip()
            if path_value:
                return Path(path_value).expanduser(), "registry by surface"

    pattern = str(Path.home() / f".codex/sessions/**/rollout-*{session_id}.jsonl")
    matches = [Path(p) for p in glob.glob(pattern, recursive=True)]
    matches = [p for p in matches if p.is_file()]
    matches.sort(key=lambda p: p.stat().st_mtime, reverse=True)
    if matches:
        return matches[0], "glob fallback"
    return None, "unresolved"


def read_jsonl_snapshot(path: Path) -> Snapshot:
    raw = path.read_bytes()
    text = raw.decode("utf-8", errors="replace")
    lines = text.splitlines()
    records: list[dict[str, Any]] = []
    invalid = 0
    skipped_partial = False
    for idx, line in enumerate(lines):
        if not line.strip():
            continue
        try:
            records.append(json.loads(line))
        except json.JSONDecodeError:
            invalid += 1
            if idx == len(lines) - 1:
                skipped_partial = True
    return Snapshot(records, invalid, skipped_partial, len(raw))


def redact(text: str) -> str:
    text = text.replace(HOME, "~")
    for pattern, replacement in SECRET_PATTERNS:
        text = pattern.sub(replacement, text)
    for pattern, replacement in PRIVATE_MARKER_PATTERNS:
        text = pattern.sub(replacement, text)
    return text


def record_has_private_control(record: dict[str, Any]) -> bool:
    try:
        text = json.dumps(record, sort_keys=True, ensure_ascii=False)
    except Exception:
        text = str(record)
    return bool(PRIVATE_CONTROL_RE.search(text))


def content_to_text(content: Any) -> str:
    if isinstance(content, str):
        return content
    if isinstance(content, list):
        parts = []
        for item in content:
            if isinstance(item, dict):
                value = item.get("text") or item.get("content") or item.get("input_text") or item.get("output_text")
                if isinstance(value, str):
                    parts.append(value)
        return "\n".join(parts)
    return ""


def payload_item(record: dict[str, Any]) -> dict[str, Any]:
    payload = record.get("payload")
    if isinstance(payload, dict) and isinstance(payload.get("item"), dict):
        return payload["item"]
    return payload if isinstance(payload, dict) else {}


def score_text(role: str, text: str) -> int:
    score = 20
    if role == "user":
        score += 70
    if re.search(r"(?i)\b(must|never|required|goal|deliverable|verify|test|blocker|failed|scope|decision|oracle|packx|skill)\b", text):
        score += 25
    if len(text) > 250:
        score += 5
    return score


def summarize_tool_output(text: str, args: argparse.Namespace) -> tuple[str, int, bool]:
    if PRIVATE_CONTROL_RE.search(text) or re.search(r'(?i)("type":"session_meta"|"session_meta")', text):
        return (
            f"[raw transcript/private instruction output omitted; original_chars={len(text)}]",
            40,
            True,
        )
    failed = bool(re.search(r"(?i)(process exited with code [1-9]|error|traceback|failed|exception|fatal)", text))
    cap = args.max_failed_output_chars if failed else args.max_tool_output_chars
    if not failed and not args.include_success_output and len(text) > 1600:
        return (
            f"[large successful output compacted; original_chars={len(text)}]",
            55,
            True,
        )
    if len(text) > cap:
        return text[:cap] + f"\n...[truncated {len(text) - cap} chars]...", 75 if failed else 45, True
    return text, 90 if failed else 45, False


def classify_record(record: dict[str, Any], index: int, args: argparse.Namespace) -> Item | None:
    record_type = str(record.get("type") or "")
    item = payload_item(record)
    item_type = str(item.get("type") or record_type)

    if record_type == "turn_context" or record_has_private_control(record):
        return Item(index, "omitted_private_control", "Omitted Private Control Context", "", 0)

    if item_type in {"reasoning", "encrypted_reasoning"}:
        return Item(index, "omitted_reasoning", "Omitted Reasoning", "", 0)

    if record_type == "session_meta":
        payload = record.get("payload") if isinstance(record.get("payload"), dict) else {}
        summary = {
            "id": payload.get("id"),
            "cwd": payload.get("cwd"),
            "cli_version": payload.get("cli_version"),
            "source": payload.get("source"),
        }
        return Item(index, "session_meta", "Session Metadata", json.dumps(summary, indent=2), 30)

    if record_type == "event_msg":
        payload = record.get("payload") if isinstance(record.get("payload"), dict) else {}
        if payload.get("type") == "token_count":
            return Item(index, "omitted_token_count", "Omitted Token Count", "", 0)
        message = payload.get("message") or payload.get("text") or ""
        if message:
            return Item(index, "event_notice", "Agent Notice", str(message), 45)
        return None

    if item_type == "message":
        role = str(item.get("role") or "unknown")
        text = content_to_text(item.get("content"))
        if not text.strip():
            return None
        title = "User Message" if role == "user" else "Assistant Message"
        return Item(index, f"message:{role}", title, text, score_text(role, text))

    if item_type == "function_call":
        name = item.get("name") or item.get("recipient_name") or "tool"
        args_text = item.get("arguments") or item.get("args") or ""
        if not isinstance(args_text, str):
            args_text = json.dumps(args_text, sort_keys=True)
        if len(args_text) > 2400:
            args_text = args_text[:2400] + "\n...[arguments truncated]..."
        return Item(index, "tool_call", f"Tool Call: {name}", args_text, 50)

    if item_type == "function_call_output":
        output = str(item.get("output") or "")
        text, score, _ = summarize_tool_output(output, args)
        return Item(index, "tool_output", "Tool Output", text, score)

    return None


def compute_budget(args: argparse.Namespace) -> tuple[int, str]:
    budget = args.max_chars
    warning = ""
    bundle = expand_path(args.packx_bundle)
    if bundle and bundle.exists():
        remaining = args.max_total_chars - bundle.stat().st_size - args.reserve_chars
        if remaining < budget:
            budget = max(60_000, remaining)
        if remaining < 200_000:
            warning = "packx bundle leaves less than the preferred conversation budget"
    return max(20_000, budget), warning


def one_line(text: str, limit: int) -> str:
    collapsed = " ".join(text.split())
    return collapsed if len(collapsed) <= limit else collapsed[:limit] + "..."


def fence(text: str) -> str:
    text = text.strip().replace("```", "`\u200b``")
    return f"```text\n{text}\n```"


def render_markdown(
    args: argparse.Namespace,
    session_id: str,
    session_source: str,
    transcript_path: Path,
    transcript_source: str,
    snapshot: Snapshot,
    items: list[Item],
    budget: int,
    budget_warning: str,
) -> str:
    useful = [item for item in items if item.text.strip()]
    user_items = [item for item in useful if item.kind == "message:user"]
    decisions = [
        item for item in useful
        if re.search(r"(?i)\b(decision|scope|narrow|must|never|required|deliverable|goal|blocker|verify|validation|failed)\b", item.text)
    ]
    tools = [item for item in useful if item.kind in {"tool_call", "tool_output", "event_notice"}]
    included = sorted(useful, key=lambda item: (item.score, item.index), reverse=True)
    recent_cutoff = max(0, len(useful) - args.recent_turns)
    recent = useful[recent_cutoff:]
    selected_by_index = {item.index: item for item in included[:140]}
    for item in recent:
        selected_by_index[item.index] = item
    selected = [selected_by_index[idx] for idx in sorted(selected_by_index)]

    sections = [
        "# Codex Conversation Supplement",
        "",
        "## Extraction Summary",
        f"- generated_at: {datetime.now(timezone.utc).isoformat()}",
        f"- session_id: {session_id}",
        f"- session_id_source: {session_source}",
        f"- transcript_path: {redact(str(transcript_path))}",
        f"- transcript_path_source: {transcript_source}",
        f"- records_read: {len(snapshot.records)}",
        f"- invalid_json_lines: {snapshot.invalid_lines}",
        f"- skipped_partial_trailing_line: {str(snapshot.skipped_partial_trailing_line).lower()}",
        f"- bytes_read: {snapshot.bytes_read}",
        f"- output_budget_chars: {budget}",
    ]
    if budget_warning:
        sections.append(f"- budget_warning: {budget_warning}")
    sections.extend([
        "",
        "## User Requirements Preserved",
    ])
    for item in user_items[-40:]:
        sections.append(f"- #{item.index}: {one_line(redact(item.text), 900)}")
    if not user_items:
        sections.append("- No user messages were extracted.")

    sections.extend(["", "## Decisions, Scope, Blockers, and Verification"])
    for item in decisions[:100]:
        sections.append(f"- **{item.title} #{item.index}**: {one_line(redact(item.text), 600)}")
    if not decisions:
        sections.append("- No explicit decision/blocker/verification records were detected.")

    sections.extend(["", "## Tool Receipt Index"])
    for item in tools[:160]:
        sections.append(f"- **{item.title} #{item.index}** score={item.score}: {one_line(redact(item.text), 360)}")
    if not tools:
        sections.append("- No tool receipts were extracted.")

    sections.extend(["", "## Chronological Conversation Highlights"])
    for item in selected:
        text = redact(item.text)
        if not text.strip():
            continue
        sections.append(f"### #{item.index} {item.title}")
        sections.append("")
        sections.append(fence(text) if "\n" in text or len(text) > 240 else f"> {text}")
        sections.append("")

    omitted_reasoning = sum(1 for item in items if item.kind == "omitted_reasoning")
    omitted_tokens = sum(1 for item in items if item.kind == "omitted_token_count")
    omitted_private_control = sum(1 for item in items if item.kind == "omitted_private_control")
    sections.extend([
        "## Omissions and Redactions",
        f"- omitted_reasoning_records: {omitted_reasoning}",
        f"- omitted_token_count_events: {omitted_tokens}",
        f"- omitted_private_control_records: {omitted_private_control}",
        f"- selected_highlight_records: {len(selected)}",
        "- redacted: API keys, bearer tokens, private keys, credential-like assignments, and home paths",
        "- omitted: private instruction blocks, hidden reasoning, token-count chatter, large successful outputs, raw environment dumps, and raw transcript JSONL",
    ])

    body = "\n".join(sections) + "\n"
    if len(body) <= budget:
        return body
    head = body[: int(budget * 0.72)]
    tail = body[-int(budget * 0.22):]
    omitted = len(body) - len(head) - len(tail)
    return head + f"\n\n...[conversation supplement budget trim omitted {omitted} chars]...\n\n" + tail


def failure_supplement(args: argparse.Namespace, out_path: Path, reason: str) -> int:
    if args.strict:
        print(f"ERROR: {reason}", file=sys.stderr)
        return 2
    text = "\n".join([
        "# Codex Conversation Supplement",
        "",
        "## Extraction Summary",
        f"- generated_at: {datetime.now(timezone.utc).isoformat()}",
        f"- extraction_failed: {reason}",
        "",
        "## User Requirements Preserved",
        "- Conversation extraction failed before transcript parsing.",
        "",
        "## Omissions and Redactions",
        "- No raw transcript content was included.",
        "",
    ])
    if not args.dry_run:
        out_path.parent.mkdir(parents=True, exist_ok=True)
        out_path.write_text(text, encoding="utf-8")
    print(f"Conversation extraction failed: {reason}", file=sys.stderr)
    return 0


def main() -> int:
    args = parse_args()
    if not args.slug and not args.out:
        print("ERROR: provide --slug or --out", file=sys.stderr)
        return 2
    out_path = expand_path(args.out) or (Path.home() / ".oracle" / "bundles" / f"{args.slug}-conversation.md")
    registry = load_registry(Path(args.registry).expanduser())

    session_id, session_source = resolve_session_id(args, registry)
    explicit_transcript = expand_path(args.transcript)
    if explicit_transcript and not args.session_id:
        match = UUID_SEARCH_RE.search(explicit_transcript.name)
        if match:
            session_id = match.group(0)
            session_source = "transcript filename"
        else:
            session_id = "explicit-transcript"
            session_source = "cli --transcript"
    if not session_id or (not UUID_RE.match(session_id) and not explicit_transcript):
        return failure_supplement(args, out_path, f"could not resolve valid Codex session id ({session_source})")

    transcript_path, transcript_source = resolve_transcript_path(args, registry, session_id)
    if not transcript_path:
        return failure_supplement(args, out_path, f"could not resolve transcript path for session {session_id}")
    if not transcript_path.exists() or not transcript_path.is_file():
        return failure_supplement(args, out_path, f"transcript path is not a file: {transcript_path}")

    snapshot = read_jsonl_snapshot(transcript_path)
    items = []
    for index, record in enumerate(snapshot.records, start=1):
        item = classify_record(record, index, args)
        if item is not None:
            items.append(item)

    budget, budget_warning = compute_budget(args)
    body = render_markdown(
        args,
        session_id,
        session_source,
        transcript_path,
        transcript_source,
        snapshot,
        items,
        budget,
        budget_warning,
    )
    body = redact(body)

    if args.print_stats or args.dry_run:
        print(f"session_id={session_id}", file=sys.stderr)
        print(f"session_id_source={session_source}", file=sys.stderr)
        print(f"transcript_path={transcript_path}", file=sys.stderr)
        print(f"records={len(snapshot.records)}", file=sys.stderr)
        print(f"items={len(items)}", file=sys.stderr)
        print(f"output_chars={len(body)}", file=sys.stderr)
        print(f"estimated_tokens={len(body) // 4}", file=sys.stderr)

    if args.dry_run:
        return 0
    out_path.parent.mkdir(parents=True, exist_ok=True)
    out_path.write_text(body, encoding="utf-8")
    print(f"Wrote conversation supplement: {out_path}", file=sys.stderr)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
