# Import Bot Import Scripts

> use this only as the source the copy command reads: import-tools.py, the Windows collector, and the Windows ChatGPT pull. Never read it into the conversation

- Skill: `brokep/import-bot-import-scripts` (Agent Skill)
- Install (CLI): `npx get-skill add brokep/import-bot-import-scripts`
- Raw SKILL.md: https://api.skillmd.com/api/skills/brokep/import-bot-import-scripts/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: brokep (https://skillmd.com/u/brokep)
- Updated: 2026-11-22
- Page: https://skillmd.com/skills/brokep/import-bot-import-scripts

---

# import-scripts

# Import scripts

The scripts other skills copy out with the command in import-bot-setup ("Copying a script out of a skill"). Copy the one you need by its heading. Never read this skill into the conversation.

## import-tools.py

```python
#!/usr/bin/env python3
"""Import Bot's tools on its own computer, in one script.

    python3 import-tools.py parse <archive-or-dir> [out-dir]
    python3 import-tools.py normalize <migration-dir>
    python3 import-tools.py search <migration-dir> "<pattern>" [--from <harness>] [-n 20]
    python3 import-tools.py view <migration-dir> <id-prefix> [--max 80]
    python3 import-tools.py seed <migration-dir> <slug> <id,id,...> [--max-sessions 12] [--max-messages 40]

parse, in order: extract into <out>/raw and drop AppleDouble sidecars, reduce
credential-bearing configs to names and delete them, delete anything on the
never-copy list, split account bundles, redact secret-looking strings value-only,
write <out>/inventory.json and <out>/sources.md, then normalize.

normalize writes one text-only document per session to <out>/pack/sessions/ plus
index.json: {"id","harness","title","timestamp","last_activity","cwd","model","source",
"messages":[{"role","text","timestamp"}],"tools":{name:count},"connectors":[...]}.
Tool calls are counted, never carried.

Sources: Claude Cowork, Claude Code, Codex, OpenClaw, Hermes, and the account pulls
placed under raw/accounts/{chatgpt,claude}/ (official exports and live conversation
JSON). Unknown keys are kept under "raw" so nothing is lost.
"""
import json
import os
import re
import shutil
import sqlite3
import sys
import tarfile
import time
import zipfile
from collections import Counter
from pathlib import Path


# ---------------------------------------------------------------- redaction
SECRET_PATTERNS = [
    r"sk-[A-Za-z0-9_-]{16,}",
    r"xox[abprs]-[A-Za-z0-9-]{10,}",
    r"gh[pousr]_[A-Za-z0-9]{20,}",
    r"AKIA[0-9A-Z]{16}",
    r"AIza[0-9A-Za-z_-]{30,}",
    r"ya29\.[A-Za-z0-9_-]{20,}",
    r"eyJ[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{10,}",
    r"-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----",
]
RX = [re.compile(p) for p in SECRET_PATTERNS]
RX_KV = re.compile(
    r"(?i)((?:api[_-]?key|secret|token|password|passwd|authorization|bearer|cookie|session_key)[\"']?\s*[:=]\s*[\"']?)([^\s'\",;}]{8,})"
)
TEXT_EXT = {".md", ".json", ".jsonl", ".toml", ".yaml", ".yml", ".txt"}

# High-entropy tokens: long, no spaces, mixed letters and digits, not a UUID, path, or URL.
# Catches secrets in formats the pattern list does not know. Shannon entropy over 4.0 bits
# per character on 32+ characters is rare in prose and common in keys.
RX_TOKEN = re.compile(r"(?<![A-Za-z0-9/:._\-])([A-Za-z0-9_\-+/=]{32,})(?![A-Za-z0-9/:._\-])")
RX_UUID = 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}")


def looks_like_secret(tok: str) -> bool:
    if RX_UUID.search(tok) or "/" in tok:
        return False
    if not (any(c.isalpha() for c in tok) and any(c.isdigit() for c in tok)):
        return False
    import math
    counts = {}
    for c in tok:
        counts[c] = counts.get(c, 0) + 1
    ent = -sum(n / len(tok) * math.log2(n / len(tok)) for n in counts.values())
    return ent > 4.0


def redact_text(text: str):
    n = 0
    for r in RX:
        text, k = r.subn("[redacted]", text)
        n += k
    text, k = RX_KV.subn(r"\1[redacted]", text)
    n += k
    def _tok(m):
        nonlocal n
        if looks_like_secret(m.group(1)):
            n += 1
            return "[redacted]"
        return m.group(0)
    text = RX_TOKEN.sub(_tok, text)
    return text, n


NEVER_COPY_DIRS = {"uploads", "outputs", "ccd-session-secrets", "vm_bundles", "claude-code-vm", "skills-plugin", "rpm", "node_modules", ".git", "credentials", "debug", "backups"}
NEVER_COPY_NAMES = {"claude_desktop_config.json", ".claude.json", "config.toml", "settings.json", "openclaw.json", "auth.json", "gateway.json"}
NEVER_COPY_PREFIXES = ("host-creds-",)
NEVER_COPY_SUFFIXES = (".audit-key", ".pem", ".key")
UUIDISH = re.compile(r"^[0-9a-fA-F-]{20,}$")


def extract(src: Path, raw: Path):
    raw.mkdir(parents=True, exist_ok=True)
    if src.is_dir():
        shutil.copytree(src, raw, dirs_exist_ok=True)
    elif zipfile.is_zipfile(src):
        with zipfile.ZipFile(src) as z:
            z.extractall(raw)
    else:
        with tarfile.open(src) as t:
            t.extractall(raw)
    for p in list(raw.rglob("._*")):
        if p.is_file():
            p.unlink()
    for junk in list(raw.rglob(".DS_Store")):
        junk.unlink()


def enforce_allowlist(raw: Path) -> int:
    removed = 0
    for p in list(raw.rglob("*")):
        if not p.exists():
            continue
        parts = set(p.relative_to(raw).parts)
        if p.is_dir() and p.name in NEVER_COPY_DIRS:
            removed += sum(1 for f in p.rglob("*") if f.is_file())
            shutil.rmtree(p, ignore_errors=True)
            continue
        if p.is_file() and (parts & NEVER_COPY_DIRS or p.name in NEVER_COPY_NAMES
                            or p.name.startswith(NEVER_COPY_PREFIXES) or p.name.endswith(NEVER_COPY_SUFFIXES)):
            p.unlink()
            removed += 1
    return removed


def redact_tree(root: Path) -> int:
    n = 0
    for p in root.rglob("*"):
        if not p.is_file() or p.suffix.lower() not in TEXT_EXT:
            continue
        try:
            data = p.read_text(encoding="utf-8", errors="replace")
        except Exception:
            continue
        new, k = redact_text(data)
        n += k
        if new != data:
            p.write_text(new, encoding="utf-8")
    return n


SCAFFOLD_PREFIXES = ("<environment_context>", "<user_instructions>", "<system-reminder>", "<command-name>", "<local-command", "<bash-input>", "<bash-stdout", "<uploaded_files>")


# ---------------------------------------------------------------- helpers
def read_text(p: Path, limit=200_000) -> str:
    try:
        return p.read_text(encoding="utf-8", errors="replace")[:limit]
    except Exception:
        return ""


def load_json(p: Path):
    try:
        return json.loads(read_text(p, 50_000_000))
    except Exception:
        return None


def first(d, *keys, default=None):
    for k in keys:
        if isinstance(d, dict) and k in d and d[k] not in (None, ""):
            return d[k]
    return default


def iso(ts):
    if ts is None:
        return None
    try:
        ts = float(ts)
        if ts > 1e12:
            ts /= 1000
        return time.strftime("%Y-%m-%d", time.gmtime(ts))
    except Exception:
        return str(ts)[:10]


def md_notes(folder: Path):
    notes = []
    if folder.is_dir():
        for p in sorted(folder.rglob("*.md")):
            notes.append({"file": p.name, "modified": iso(p.stat().st_mtime), "text": read_text(p, 20_000)})
    return notes


def skill_files(folder: Path, raw: Path):
    return [str(p.relative_to(raw)) for p in folder.rglob("SKILL.md")] if folder.is_dir() else []


def user_turns(jsonl: Path, cap=8):
    """First user messages from a Claude Code or Codex style JSONL transcript."""
    turns = []
    try:
        with open(jsonl, encoding="utf-8", errors="replace") as f:
            for line in f:
                try:
                    o = json.loads(line)
                except Exception:
                    continue
                msg = o.get("message") if isinstance(o.get("message"), dict) else o
                role = msg.get("role") or o.get("type")
                if role != "user" or o.get("isMeta"):
                    continue
                c = msg.get("content")
                if isinstance(c, list):
                    c = " ".join(b.get("text", "") for b in c if isinstance(b, dict) and b.get("type") == "text")
                if isinstance(c, str) and c.strip() and not c.lstrip().startswith("<"):
                    turns.append(c.strip()[:400])
                if len(turns) >= cap:
                    break
    except Exception:
        pass
    return turns


def audit_tools(audit: Path) -> Counter:
    tools = Counter()
    for line in read_text(audit, 5_000_000).splitlines():
        try:
            o = json.loads(line)
        except Exception:
            continue
        # Agent SDK stream: assistant messages carry tool_use blocks
        msg = o.get("message") if isinstance(o.get("message"), dict) else None
        if msg and isinstance(msg.get("content"), list):
            for b in msg["content"]:
                if isinstance(b, dict) and b.get("type") == "tool_use" and isinstance(b.get("name"), str):
                    tools[b["name"]] += 1
        name = first(o, "tool", "toolName", "tool_name")
        if isinstance(name, str):
            tools[name] += 1
    return tools


def connectors_from_tools(tools) -> list:
    names = set()
    for n in (tools.keys() if isinstance(tools, Counter) else [t for t, _ in tools]):
        if isinstance(n, str) and n.startswith("mcp__") and len(n.split("__")) > 1:
            names.add(n.split("__")[1])
    return sorted(names)


def mcp_names_from_json(p: Path, per_project=False):
    cfg = load_json(p) or {}
    out = sorted((cfg.get("mcpServers") or {}).keys())
    if per_project:
        for proj, pc in (cfg.get("projects") or {}).items():
            out += [f"{n} (project {proj})" for n in (pc.get("mcpServers") or {}).keys()]
    return out


def mcp_names_from_toml(p: Path):
    names = []
    for line in read_text(p).splitlines():
        m = re.match(r"\s*\[mcp_servers\.([^\].]+)", line)
        if m:
            names.append(m.group(1))
    return sorted(set(names))


def iter_jsonl(p: Path):
    try:
        with open(p, encoding="utf-8", errors="replace") as f:
            for line in f:
                line = line.strip()
                if not line:
                    continue
                try:
                    yield json.loads(line)
                except Exception:
                    continue
    except Exception:
        return


def iso_ts(ts):
    if ts is None or ts == "":
        return None
    if isinstance(ts, str):
        return ts[:19]
    try:
        ts = float(ts)
        if ts > 1e12:
            ts /= 1000
        return time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(ts))
    except Exception:
        return str(ts)[:19]


def blocks_text(content, tools: Counter):
    """Anthropic-style content: string, or list of blocks. Returns text, counting tool_use names."""
    if isinstance(content, str):
        return content
    out = []
    if isinstance(content, list):
        for b in content:
            if not isinstance(b, dict):
                continue
            t = b.get("type")
            if t in ("text", "input_text", "output_text") and isinstance(b.get("text"), str):
                out.append(b["text"])
            elif t in ("tool_use", "function_call") and isinstance(b.get("name"), str):
                tools[b["name"]] += 1
    return "\n".join(out)


def is_scaffold(text: str) -> bool:
    s = text.lstrip()
    return any(s.startswith(p) for p in SCAFFOLD_PREFIXES)


def connectors_from(tools: Counter, extra=()):
    names = set()
    for n in list(tools.keys()) + list(extra):
        if isinstance(n, str) and n.startswith("mcp__") and len(n.split("__")) > 1:
            names.add(n.split("__")[1])
    return sorted(names)


def doc(harness, sid, title, timestamp, messages, cwd=None, model=None, source=None, tools=None, connectors=None, last=None):
    tools = tools or Counter()
    for m in messages:
        m["text"], _ = redact_text(m["text"])
    return {
        "id": f"{harness}-{sid}",
        "harness": harness,
        "title": (title or (messages[0]["text"][:60] if messages else sid)).strip() if isinstance(title or "", str) else sid,
        "timestamp": timestamp,
        "last_activity": last or (messages[-1]["timestamp"] if messages and messages[-1].get("timestamp") else timestamp),
        "cwd": cwd,
        "model": model,
        "source": str(source) if source else None,
        "messages": messages,
        "tools": dict(tools.most_common(40)),
        "connectors": connectors if connectors is not None else connectors_from(tools),
    }


# ---------------------------------------------------------------- configs
def reduce_configs(raw: Path) -> dict:
    """Names out of files that can carry credentials, then delete those files.
    Runs before redaction (files still parse) and before parsing (nothing
    downstream sees them)."""
    out = {"desktop_mcp": [], "claude_code_mcp": [], "claude_code_settings": {}, "codex_mcp": [], "openclaw_channels": [], "hermes_config": []}
    for names in raw.rglob("connector-names.txt"):      # bot path, written on the Mac
        section = None
        for line in read_text(names).splitlines():
            line = line.strip().lstrip("\ufeff")
            if line.startswith("## "):
                section = line[3:]
                continue
            if not line or section is None:
                continue
            if section.endswith("claude_desktop_config.json"):
                out["desktop_mcp"].append(line)
            elif section.endswith(".claude.json"):
                out["claude_code_mcp"].append(line)
            elif section.endswith("config.toml"):
                out["codex_mcp"].append(line)
            elif section.endswith("settings.json"):
                m = re.match(r'"([a-zA-Z]*[cC]leanupPeriodDays)"\s*:\s*(\d+)', line)
                if m:
                    out["claude_code_settings"][m.group(1)] = int(m.group(2))
            elif section.endswith("openclaw.json"):
                out["openclaw_channels"].append(line)
            elif section.endswith("config.yaml"):
                out["hermes_config"].append(line)
    for cfg in list(raw.rglob("claude_desktop_config.json")):
        out["desktop_mcp"] += mcp_names_from_json(cfg)
        cfg.unlink()
    for cfg in list(raw.rglob(".claude.json")):
        out["claude_code_mcp"] += mcp_names_from_json(cfg, per_project=True)
        cfg.unlink()
    for cfg in list(raw.rglob("settings.json")):
        if cfg.parent.name not in (".claude", "claude-code"):
            continue
        settings = load_json(cfg) or {}
        for k in ("cleanupPeriodDays", "desktopSessionCleanupPeriodDays"):
            if k in settings:
                out["claude_code_settings"][k] = settings[k]
        cfg.unlink()
    for cfg in list(raw.rglob("config.toml")):
        if cfg.parent.name in (".codex", "codex"):
            out["codex_mcp"] += mcp_names_from_toml(cfg)
            cfg.unlink()
    for cfg in list(raw.rglob("openclaw.json")):
        data = load_json(cfg) or {}
        for key in ("channels", "connectors", "plugins", "mcpServers", "tools"):
            v = data.get(key)
            if isinstance(v, dict):
                out["openclaw_channels"] += list(v.keys())
            elif isinstance(v, list):
                out["openclaw_channels"] += [x.get("name", x.get("id", "")) if isinstance(x, dict) else str(x) for x in v]
        cfg.unlink()
    for cfg in list(raw.rglob("config.yaml")):
        if cfg.parent.name in (".hermes", "hermes"):
            keep = [l.rstrip() for l in read_text(cfg).splitlines() if l.strip() and not re.search(r"(?i)key|token|secret|password", l)]
            out["hermes_config"] += keep[:60]
            cfg.unlink()
    for k in ("desktop_mcp", "claude_code_mcp", "codex_mcp", "openclaw_channels"):
        out[k] = sorted({x for x in out[k] if x})
    return out


def find_dir(raw: Path, *names):
    for name in names:
        for d in raw.rglob(name):
            if d.is_dir():
                yield d


# ---------------------------------------------------------------- Cowork
def parse_cowork(raw: Path, configs: dict):
    result = {"found": False, "accounts": [], "plugins": [], "mcp_servers": configs["desktop_mcp"], "scheduled_skill_files": []}
    roots = list(find_dir(raw, "local-agent-mode-sessions")) + list(find_dir(raw, "claude-code-sessions"))
    for root in roots:
        for acct in sorted(d for d in root.glob("*/*") if d.is_dir()):
            # An account tree is <org-uuid>/<account-uuid>. skills-plugin and app state sit beside it.
            if not (UUIDISH.match(acct.parent.name) and UUIDISH.match(acct.name)):
                continue
            a = {"path": str(acct.relative_to(raw)), "layout": root.name}
            settings = load_json(acct / "cowork_account_settings.json") or {}
            a["display_name"] = first(settings, "displayName", "display_name")
            a["memory_enabled"] = first(settings, "memoryEnabled", "memory_enabled")
            a["global_instructions"] = read_text(acct / "memory" / "CLAUDE.md")
            a["global_memory_notes"] = md_notes(acct / "memory" / "memory")

            # Projects (spaces). Schema inferred, kept tolerant.
            spaces_raw = load_json(acct / "spaces.json")
            items = []
            if isinstance(spaces_raw, dict):
                items = spaces_raw.get("spaces") if isinstance(spaces_raw.get("spaces"), list) else \
                    [dict(v, id=k) if isinstance(v, dict) else {"id": k} for k, v in spaces_raw.items() if isinstance(v, dict)]
            elif isinstance(spaces_raw, list):
                items = spaces_raw
            spaces = []
            for s in items:
                sid = first(s, "id", "uuid", "spaceId")
                spaces.append({
                    "id": sid,
                    "name": first(s, "name", "title"),
                    "instructions": first(s, "instructions", "prompt", "systemPrompt"),
                    "folders": first(s, "folders", "paths", default=[]),
                    "created": iso(first(s, "createdAt", "created_at")),
                    "memory_notes": md_notes(acct / "spaces" / str(sid) / "memory") if sid else [],
                    "raw": {k: v for k, v in s.items() if k not in {"instructions", "folders"}},
                })
            a["spaces"] = spaces

            # Scheduled tasks. Top-level key "scheduledTasks" verified 2026-09-12.
            st = load_json(acct / "scheduled-tasks.json") or {}
            tasks = st.get("scheduledTasks") if isinstance(st, dict) else st
            a["scheduled_tasks"] = []
            for t in tasks or []:
                if isinstance(t, dict):
                    a["scheduled_tasks"].append({
                        "name": first(t, "name", "title"),
                        "schedule": first(t, "schedule", "cron", "rrule"),
                        "timezone": first(t, "timezone", "tz"),
                        "prompt": first(t, "prompt", "instructions", "description"),
                        "space_id": first(t, "spaceId", "space_id", "projectId"),
                        "enabled": first(t, "enabled", "isEnabled", default=None),
                        "skill_path": first(t, "skillPath", "skill_path"),
                        "raw": t,
                    })

            # Sessions. Record local_<uuid>.json + storage dir local_<uuid>/ (same stem),
            # agent-type sessions under agent/. Per txcript docs/formats/cowork.md.
            sessions = []
            cards = sorted(acct.glob("local_*.json")) + sorted((acct / "agent").glob("local_*.json")) if (acct / "agent").is_dir() else sorted(acct.glob("local_*.json"))
            for card in cards:
                c = load_json(card) or {}
                sid = first(c, "sessionId", "id", default=card.stem)
                sdir = None
                for cand in (card.parent / card.stem, card.parent / str(sid), acct / card.stem, acct / str(sid)):
                    if cand.is_dir():
                        sdir = cand
                        break
                transcripts, turns, session_tools = [], [], Counter()
                if sdir:
                    transcripts = [t for t in sdir.rglob("*.jsonl") if t.name != "audit.jsonl" and "subagents" not in t.parts]
                    for t in transcripts[:2]:
                        turns += user_turns(t, cap=4)
                    for audit in sdir.rglob("audit.jsonl"):
                        session_tools.update(audit_tools(audit))
                enabled_mcp = first(c, "enabledMcpTools", default=[]) or []
                connectors = set(connectors_from_tools(session_tools))
                for tool in enabled_mcp if isinstance(enabled_mcp, list) else []:
                    if isinstance(tool, str) and tool.startswith("mcp__") and len(tool.split("__")) > 1:
                        connectors.add(tool.split("__")[1])
                sessions.append({
                    "id": sid,
                    "record": card.name,
                    "title": first(c, "title", "name", "summary"),
                    "created": iso(first(c, "createdAt", "created_at", "startedAt")),
                    "last_activity": iso(first(c, "lastActivityAt", "updatedAt")),
                    "cwd": first(c, "cwd"),
                    "model": first(c, "model"),
                    "cli_session_id": first(c, "cliSessionId"),
                    "space_id": first(c, "spaceId", "space_id", "projectId"),
                    "archived": first(c, "isArchived", "archived", default=False),
                    "agent_type": card.parent.name == "agent",
                    "has_transcript": bool(transcripts),
                    "sample_user_turns": turns,
                    "tool_usage": session_tools.most_common(30),
                    "connectors_seen": sorted(connectors),
                    "raw": {k: v for k, v in c.items() if k not in {"systemPrompt", "enabledMcpTools", "egressAllowedDomains", "initialMessage"}},
                })
            sessions.sort(key=lambda s: s["created"] or "")
            a["sessions"] = sessions
            a["session_count"] = len(sessions)
            a["sessions_with_transcript"] = sum(1 for s in sessions if s["has_transcript"])
            a["oldest_session"] = sessions[0]["created"] if sessions else None
            a["newest_session"] = sessions[-1]["created"] if sessions else None
            with_t = [s["created"] for s in sessions if s["has_transcript"] and s["created"]]
            a["oldest_transcript"] = min(with_t) if with_t else None

            merged_all = Counter()
            for s in sessions:
                merged_all.update(dict(s["tool_usage"]))
            a["tool_usage"] = merged_all.most_common(60)
            a["connectors_seen"] = sorted({c for s in sessions for c in s["connectors_seen"]})
            for sp in a["spaces"]:
                own = [s for s in sessions if s["space_id"] and sp["id"] and str(s["space_id"]) == str(sp["id"])]
                merged = Counter()
                for s in own:
                    merged.update(dict(s["tool_usage"]))
                sp["session_count"] = len(own)
                sp["tool_usage"] = merged.most_common(30)
                sp["connectors_seen"] = sorted({c for s in own for c in s["connectors_seen"]})
            unassigned = [s for s in sessions if not s["space_id"]]
            a["connectors_seen_outside_projects"] = sorted({c for s in unassigned for c in s["connectors_seen"]})
            a["sessions_outside_projects"] = len(unassigned)
            result["accounts"].append(a)
            result["found"] = True

    for pdir in find_dir(raw, "cowork_plugins"):
        for plug in sorted(d for d in pdir.iterdir() if d.is_dir()):
            manifest = load_json(plug / "plugin.json") or load_json(plug / ".claude-plugin" / "plugin.json") or {}
            result["plugins"].append({
                "name": first(manifest, "name", default=plug.name),
                "version": first(manifest, "version"),
                "skills": [p.parent.name for p in plug.rglob("SKILL.md")],
                "skill_files": [str(p.relative_to(raw)) for p in plug.rglob("SKILL.md")],
            })
    result["scheduled_skill_files"] = [str(p.relative_to(raw)) for p in raw.rglob("Scheduled/**/SKILL.md")]
    return result


# ---------------------------------------------------------------- Claude Code
def parse_claude_code(raw: Path, configs: dict):
    r = {"found": False}
    homes = [d for d in find_dir(raw, ".claude", "claude-code") if any((d / x).exists() for x in ("CLAUDE.md", "projects", "skills", "rules"))]
    if not homes:
        return r
    h = homes[0]
    r["found"] = True
    r["instructions"] = read_text(h / "CLAUDE.md")
    r["settings"] = configs["claude_code_settings"]
    r["skills"] = skill_files(h / "skills", raw)
    r["rules"] = [str(p.relative_to(raw)) for p in (h / "rules").rglob("*.md")] if (h / "rules").is_dir() else []
    r["commands"] = [p.stem for p in (h / "commands").rglob("*.md")] if (h / "commands").is_dir() else []
    r["agents"] = [p.stem for p in (h / "agents").rglob("*.md")] if (h / "agents").is_dir() else []
    r["projects"] = []
    if (h / "projects").is_dir():
        for proj in sorted(d for d in (h / "projects").iterdir() if d.is_dir()):
            transcripts = list(proj.glob("*.jsonl"))
            turns = []
            for t in sorted(transcripts, key=lambda x: x.stat().st_mtime, reverse=True)[:3]:
                turns += user_turns(t, cap=3)
            r["projects"].append({"dir": proj.name, "transcripts": len(transcripts), "memory_notes": md_notes(proj / "memory"), "sample_user_turns": turns})
    r["mcp_servers"] = configs["claude_code_mcp"]
    return r


# ---------------------------------------------------------------- Codex
def parse_codex(raw: Path, configs: dict):
    r = {"found": False}
    homes = [d for d in find_dir(raw, ".codex", "codex") if any((d / x).exists() for x in ("AGENTS.md", "memories", "sessions", "skills"))]
    if not homes:
        return r
    h = homes[0]
    r["found"] = True
    r["instructions"] = read_text(h / "AGENTS.md")
    r["instructions_override"] = read_text(h / "AGENTS.override.md")
    r["memories"] = md_notes(h / "memories")
    r["memories_enabled"] = bool(r["memories"])
    r["skills"] = skill_files(h / "skills", raw)
    r["shared_skills"] = [str(p.relative_to(raw)) for p in raw.rglob(".agents/skills/**/SKILL.md")] + [str(p.relative_to(raw)) for p in raw.rglob("agents-skills/**/SKILL.md")]
    r["mcp_servers"] = configs["codex_mcp"]
    sessions = list((h / "sessions").rglob("*.jsonl")) if (h / "sessions").is_dir() else []
    r["session_count"] = len(sessions)
    turns = []
    for t in sorted(sessions, key=lambda x: x.stat().st_mtime, reverse=True)[:10]:
        turns += user_turns(t, cap=2)
    r["sample_user_turns"] = turns
    return r


# ---------------------------------------------------------------- OpenClaw
def parse_openclaw(raw: Path, configs: dict):
    r = {"found": False}
    homes = [d for d in find_dir(raw, ".openclaw", "openclaw") if any((d / x).exists() for x in ("workspace", "agents", "skills"))]
    if not homes:
        return r
    h = homes[0]
    r["found"] = True
    ws = h / "workspace"
    r["bootstrap"] = {name: read_text(ws / f"{name}.md") for name in ("AGENTS", "SOUL", "IDENTITY", "USER", "MEMORY") if (ws / f"{name}.md").exists()}
    r["memory_notes"] = md_notes(ws / "memory")
    r["skills"] = skill_files(h / "skills", raw) + skill_files(ws / "skills", raw)
    r["channels"] = configs["openclaw_channels"]
    r["agents"] = []
    if (h / "agents").is_dir():
        for ag in sorted(d for d in (h / "agents").iterdir() if d.is_dir()):
            info = {"id": ag.name, "session_count": 0, "sample_user_turns": []}
            for db in ag.rglob("*.sqlite"):
                info.update(sqlite_summary(db))
            legacy = list((ag / "sessions").rglob("*.jsonl")) if (ag / "sessions").is_dir() else []
            info["session_count"] += len(legacy)
            for t in legacy[:3]:
                info["sample_user_turns"] += user_turns(t, cap=2)
            r["agents"].append(info)
    return r


def sqlite_summary(db: Path) -> dict:
    """Read-only look at an agent session database: table names, row counts,
    and a few user-authored texts. Never writes."""
    out = {"db": db.name, "tables": {}, "session_count": 0, "sample_user_turns": []}
    try:
        con = sqlite3.connect(f"file:{db}?mode=ro", uri=True)
        cur = con.cursor()
        tables = [r[0] for r in cur.execute("select name from sqlite_master where type='table'")]
        for t in tables:
            try:
                out["tables"][t] = cur.execute(f'select count(*) from "{t}"').fetchone()[0]
            except Exception:
                pass
        for t in tables:
            if "session" in t.lower():
                out["session_count"] = max(out["session_count"], out["tables"].get(t, 0))
        for t in tables:
            cols = [c[1] for c in cur.execute(f'pragma table_info("{t}")')]
            if "role" in cols and "content" in cols:
                rows = cur.execute(f'select content from "{t}" where role='user' order by rowid desc limit 12').fetchall()
                for (c,) in rows:
                    if isinstance(c, str) and c.strip():
                        out["sample_user_turns"].append(c.strip()[:300])
                break
        con.close()
    except Exception as e:
        out["error"] = str(e)[:200]
    return out


# ---------------------------------------------------------------- Hermes
def parse_hermes(raw: Path, configs: dict):
    r = {"found": False}
    homes = [d for d in find_dir(raw, ".hermes", "hermes") if any((d / x).exists() for x in ("state.db", "memories", "skills"))]
    if not homes:
        return r
    h = homes[0]
    r["found"] = True
    r["memory"] = read_text(h / "memories" / "MEMORY.md")
    r["user_profile"] = read_text(h / "memories" / "USER.md")
    r["skills"] = skill_files(h / "skills", raw)
    r["config_lines"] = configs["hermes_config"]
    r["sessions"] = []
    db = h / "state.db"
    if db.exists():
        summary = sqlite_summary(db)
        r["db"] = summary
        try:
            con = sqlite3.connect(f"file:{db}?mode=ro", uri=True)
            rows = con.execute("select id, title, started_at, model, cwd from sessions where coalesce(archived,0)=0 order by started_at desc limit 200").fetchall()
            for sid, title, started, model, cwd in rows:
                r["sessions"].append({"id": sid, "title": title, "started": iso(started), "model": model, "cwd": cwd})
            con.close()
        except Exception as e:
            r["sessions_error"] = str(e)[:200]
    r["session_count"] = len(r["sessions"])
    return r


# ---------------------------------------------------------------- account bundles
def ingest_bundles(raw: Path) -> dict:
    """Split the one-file bundles the page scripts download (claude-pull-*.json,
    chatgpt-pull-*.json) into the per-conversation layout the account parsers read.
    Idempotent: existing files are not rewritten."""
    stats = {"claude": 0, "chatgpt": 0}
    for f in list(raw.rglob("claude-pull-*.json")) + list(raw.rglob("chatgpt-pull-*.json")):
        b = load_json(f) or {}
        service = b.get("service") or ("claude" if f.name.startswith("claude") else "chatgpt")
        base = raw / "accounts" / service
        (base / "conversations").mkdir(parents=True, exist_ok=True)
        for conv in b.get("conversations") or []:
            cid = conv.get("uuid") or conv.get("conversation_id") or conv.get("id")
            if not cid:
                continue
            out = base / "conversations" / f"{cid}.json"
            if not out.exists():
                out.write_text(json.dumps(conv))
                stats[service] += 1
        if service == "claude" and b.get("projects"):
            (base / "projects-live.json").write_text(json.dumps(b["projects"], indent=1))
        if service == "chatgpt" and b.get("custom_instructions") and not (base / "custom-instructions.md").exists():
            ci = b["custom_instructions"]
            if isinstance(ci, dict):
                text = "\n\n".join(str(ci.get(k) or "") for k in ("about_user_message", "about_model_message") if ci.get(k))
                if text.strip():
                    (base / "custom-instructions.md").write_text(text + "\n")
    return stats


def parse_chatgpt_account(raw: Path):
    r = {"found": False, "conversations": [], "memories": "", "custom_instructions": "", "projects": "", "gpts": ""}
    base = None
    for d in find_dir(raw, "chatgpt"):
        if "accounts" in d.parts:
            base = d
            break
    if base is None:
        return r
    r["found"] = True
    for name in ("memories", "custom-instructions", "projects", "gpts"):
        p = base / f"{name}.md"
        if p.exists():
            r[name.replace("-", "_")] = read_text(p, 60_000)
    convs = []
    exp = base / "export" / "conversations.json"
    if exp.exists():
        data = load_json(exp) or []
        for conv in data if isinstance(data, list) else []:
            turns = chatgpt_turns(conv)
            convs.append({"id": first(conv, "conversation_id", "id"), "title": conv.get("title"), "created": iso(conv.get("create_time")),
                          "updated": iso(conv.get("update_time")), "turns": len(turns), "sample_user_turns": [t["text"][:300] for t in turns if t["role"] == "user"][:4], "source": "export"})
    live = base / "conversations"
    if live.is_dir():
        for p in sorted(live.glob("*.json")):
            conv = load_json(p) or {}
            if not conv.get("mapping"):
                continue
            turns = chatgpt_turns(conv)
            convs.append({"id": first(conv, "conversation_id", "id", default=p.stem), "title": conv.get("title"), "created": iso(conv.get("create_time")),
                          "updated": iso(conv.get("update_time")), "turns": len(turns), "sample_user_turns": [t["text"][:300] for t in turns if t["role"] == "user"][:4], "source": "live", "file": str(p.relative_to(raw))})
    convs.sort(key=lambda c: c["updated"] or c["created"] or "")
    r["conversations"] = convs
    r["conversation_count"] = len(convs)
    r["export_present"] = exp.exists()
    return r


def parse_claude_account(raw: Path):
    r = {"found": False, "conversations": [], "memory_text": "", "projects": [], "projects_text": ""}
    base = None
    for d in find_dir(raw, "claude"):
        if "accounts" in d.parts:
            base = d
            break
    if base is None:
        return r
    r["found"] = True
    if (base / "memory.md").exists():
        r["memory_text"] = read_text(base / "memory.md", 60_000)
    if (base / "projects.md").exists():
        r["projects_text"] = read_text(base / "projects.md", 60_000)
    exp = base / "export"
    mem = load_json(exp / "memories.json") if (exp / "memories.json").exists() else None
    if isinstance(mem, list) and mem:
        m0 = mem[0] if isinstance(mem[0], dict) else {}
        r["export_memory"] = {"conversations_memory": m0.get("conversations_memory", ""), "project_memories": m0.get("project_memories", {})}
    projs = load_json(exp / "projects.json") if (exp / "projects.json").exists() else None
    live = load_json(base / "projects-live.json") if (base / "projects-live.json").exists() else None
    seen = set()
    for p in (projs if isinstance(projs, list) else []) + (live if isinstance(live, list) else []):
        if p.get("uuid") in seen:
            continue
        seen.add(p.get("uuid"))
        r["projects"].append({"uuid": p.get("uuid"), "name": p.get("name"), "description": p.get("description"), "prompt_template": p.get("prompt_template"),
                              "docs": [d.get("filename") or d.get("file_name") for d in (p.get("docs") or []) if isinstance(d, dict)]})
    convs = []
    data = load_json(exp / "conversations.json") if (exp / "conversations.json").exists() else None
    for conv in data if isinstance(data, list) else []:
        msgs = conv.get("chat_messages") or []
        humans = [m.get("text", "") for m in msgs if m.get("sender") == "human" and m.get("text")]
        convs.append({"id": conv.get("uuid"), "title": conv.get("name"), "created": (conv.get("created_at") or "")[:10], "turns": len(msgs),
                      "sample_user_turns": [h[:300] for h in humans[:4]], "source": "export"})
    live = base / "conversations"
    if live.is_dir():
        for p in sorted(live.glob("*.json")):
            conv = load_json(p) or {}
            msgs = conv.get("chat_messages") or []
            if not msgs:
                continue
            humans = [m.get("text", "") for m in msgs if m.get("sender") == "human" and m.get("text")]
            convs.append({"id": conv.get("uuid", p.stem), "title": conv.get("name"), "created": (conv.get("created_at") or "")[:10], "turns": len(msgs),
                          "sample_user_turns": [h[:300] for h in humans[:4]], "source": "live", "file": str(p.relative_to(raw))})
    r["conversations"] = convs
    r["conversation_count"] = len(convs)
    r["export_present"] = (exp / "conversations.json").exists()
    return r


# ---------------------------------------------------------------- report
def sources_md(inv: dict) -> str:
    L = ["# Sources", "", "| Source | Found | What |", "|---|---|---|"]
    cw = inv["cowork"]
    if cw["found"]:
        for a in cw["accounts"]:
            L.append(f"| Claude Cowork ({a['layout']}) | yes | {len(a['global_memory_notes'])} memory notes, {len(a['spaces'])} projects, "
                     f"{len(a['scheduled_tasks'])} scheduled tasks, {a['session_count']} sessions ({a['sessions_with_transcript']} with transcript, "
                     f"{a['oldest_session']} to {a['newest_session']}), memory {'on' if a['memory_enabled'] else 'off or unknown'}, connectors {', '.join(a['connectors_seen']) or 'none'} |")
        if cw["plugins"]:
            L.append(f"| Cowork plugins | yes | {', '.join(p['name'] for p in cw['plugins'])} |")
        if cw["mcp_servers"]:
            L.append(f"| Claude Desktop MCP servers | yes | {', '.join(cw['mcp_servers'])} |")
    else:
        L.append("| Claude Cowork | no | not installed or never used on this Mac |")
    cc = inv["claude_code"]
    L.append(f"| Claude Code | {'yes' if cc['found'] else 'no'} | " + (f"{len(cc['skills'])} skills, {len(cc['rules'])} rules, {len(cc['projects'])} project dirs, MCP {', '.join(cc['mcp_servers']) or 'none'}, retention {cc['settings'] or 'defaults'} |" if cc["found"] else " |"))
    cx = inv["codex"]
    L.append(f"| Codex | {'yes' if cx['found'] else 'no'} | " + (f"memories {'on' if cx['memories_enabled'] else 'off'} ({len(cx['memories'])} files), {len(cx['skills']) + len(cx['shared_skills'])} skills, {cx['session_count']} sessions, MCP {', '.join(cx['mcp_servers']) or 'none'} |" if cx["found"] else " |"))
    oc = inv["openclaw"]
    L.append(f"| OpenClaw | {'yes' if oc['found'] else 'no'} | " + (f"bootstrap files {', '.join(oc['bootstrap'].keys()) or 'none'}, {len(oc['memory_notes'])} memory notes, {len(oc['skills'])} skills, {len(oc['agents'])} agents, channels {', '.join(oc['channels']) or 'none'} |" if oc["found"] else " |"))
    hm = inv["hermes"]
    L.append(f"| Hermes | {'yes' if hm['found'] else 'no'} | " + (f"MEMORY.md {'yes' if hm['memory'] else 'no'}, USER.md {'yes' if hm['user_profile'] else 'no'}, {len(hm['skills'])} skills, {hm['session_count']} sessions |" if hm["found"] else " |"))
    gpt = inv["accounts"]["chatgpt"]
    L.append(f"| ChatGPT (account) | {'yes' if gpt['found'] else 'no'} | " + (f"{gpt['conversation_count']} conversations ({'export' if gpt['export_present'] else 'live pull'}), memories {'yes' if gpt['memories'] else 'no'}, custom instructions {'yes' if gpt['custom_instructions'] else 'no'}, projects {'yes' if gpt['projects'] else 'no'}, GPTs {'yes' if gpt['gpts'] else 'no'} |" if gpt["found"] else " |"))
    cl = inv["accounts"]["claude"]
    L.append(f"| Claude.ai (account) | {'yes' if cl['found'] else 'no'} | " + (f"{cl['conversation_count']} conversations ({'export' if

…(truncated)
