set -euo pipefail
runtime_subset_validate() {
  python3 - "$@" <<'OD_CLAUDE_RUNTIME_SUBSET'
import hashlib
import os
import stat
import sys

def fail(msg):
    print(msg, file=sys.stderr)
    sys.exit(1)

def symlink_resolves_inside(root, link_path, link_text):
    parent = os.path.dirname(os.path.abspath(link_path))
    resolved = os.path.normpath(os.path.join(parent, link_text))
    root = os.path.abspath(root)
    return resolved == root or resolved.startswith(root + os.sep)

def file_sha256(path):
    digest = hashlib.sha256()
    with open(path, "rb") as fh:
        while True:
            chunk = fh.read(1024 * 1024)
            if not chunk:
                return digest.hexdigest()
            digest.update(chunk)

def validate(quarantine_root, target_roots, legacy):
    qroot = os.path.abspath(quarantine_root)
    troots = [os.path.abspath(root) for root in target_roots]
    if not os.path.isdir(qroot):
        fail("subset:not-a-directory")
    if not troots or any(not os.path.exists(root) for root in troots):
        fail("subset:target-missing")
    for dirpath, dirnames, filenames in os.walk(qroot, topdown=True, followlinks=False):
        rel_dir = os.path.relpath(dirpath, qroot)
        if rel_dir == ".":
            rel_dir = ""
        descend = []
        for name in sorted(set(dirnames) | set(filenames)):
            # Python bytecode caches regenerate wherever a shipped script runs;
            # they are derived state, never payload, and never block takeover.
            if name == "__pycache__" or name.endswith(".pyc"):
                continue
            rel = name if not rel_dir else f"{rel_dir}/{name}"
            qpath = os.path.join(dirpath, name)
            try:
                qst = os.lstat(qpath)
            except OSError:
                fail(f"subset:unreadable:{rel}")
            targets = []
            target_error = False
            for troot in troots:
                tpath = os.path.join(troot, rel)
                try:
                    targets.append((tpath, os.lstat(tpath)))
                except FileNotFoundError:
                    pass
                except OSError:
                    target_error = True
            if not targets:
                fail(f"subset:target-unreadable:{rel}" if target_error else f"subset:unknown:{rel}")
            qmode = stat.S_IFMT(qst.st_mode)
            typed = [(path, tst) for path, tst in targets if stat.S_IFMT(tst.st_mode) == qmode]
            if not typed:
                fail(f"subset:type-mismatch:{rel}")
            if qmode == stat.S_IFREG:
                matched = False
                for tpath, tst in typed:
                    if qst.st_size != tst.st_size:
                        continue
                    with open(qpath, "rb") as qf, open(tpath, "rb") as tf:
                        while True:
                            qb = qf.read(1024 * 1024)
                            tb = tf.read(1024 * 1024)
                            if qb != tb:
                                break
                            if not qb:
                                matched = True
                                break
                    if matched:
                        break
                if not matched and file_sha256(qpath) in legacy.get(rel, set()):
                    matched = True
                if not matched:
                    fail(f"subset:content-mismatch:{rel}")
            elif qmode == stat.S_IFLNK:
                try:
                    qlink = os.readlink(qpath)
                except OSError:
                    fail(f"subset:symlink-unreadable:{rel}")
                if not symlink_resolves_inside(qroot, qpath, qlink):
                    fail(f"subset:unsafe-symlink:{rel}")
                matched = False
                for tpath, _tst in typed:
                    try:
                        if qlink == os.readlink(tpath):
                            matched = True
                            break
                    except OSError:
                        continue
                if not matched:
                    fail(f"subset:symlink-mismatch:{rel}")
            elif qmode == stat.S_IFDIR:
                descend.append(name)
            else:
                fail(f"subset:special-file:{rel}")
        dirnames[:] = descend

if __name__ == "__main__":
    if len(sys.argv) < 3:
        fail("subset:usage")
    args = sys.argv[2:]
    legacy = {}
    if "--legacy" in args:
        split = args.index("--legacy")
        target_roots, specs = args[:split], args[split + 1:]
        for spec in specs:
            try:
                rel, digest = spec.rsplit("=", 1)
            except ValueError:
                fail("subset:legacy-usage")
            if not rel or os.path.isabs(rel) or ".." in rel or len(digest) != 64 or any(c not in "0123456789abcdef" for c in digest):
                fail("subset:legacy-usage")
            legacy.setdefault(rel, set()).add(digest)
    else:
        target_roots = args
    validate(sys.argv[1], target_roots, legacy)
OD_CLAUDE_RUNTIME_SUBSET
}
dest="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53"
home="$HOME/.claude"
managed_root="$HOME/.local/share/overdeck/claude-runtime"/
mkdir -p "$home" "$dest"
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/skills"
link="$HOME/.claude/skills"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:skills >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:skills >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:skills >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}skills"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:skills >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:skills >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:skills >&2; exit 1
    fi
  else
    echo link-collision:skills >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:skills >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:skills >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/hooks"
link="$HOME/.claude/hooks"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:hooks >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:hooks >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:hooks >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}hooks"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:hooks >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:hooks >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:hooks >&2; exit 1
    fi
  else
    echo link-collision:hooks >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:hooks >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:hooks >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/agents"
link="$HOME/.claude/agents"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:agents >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:agents >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:agents >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}agents"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:agents >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:agents >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:agents >&2; exit 1
    fi
  else
    echo link-collision:agents >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:agents >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:agents >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/commands"
link="$HOME/.claude/commands"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:commands >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:commands >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:commands >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}commands"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:commands >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:commands >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:commands >&2; exit 1
    fi
  else
    echo link-collision:commands >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:commands >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:commands >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/output-styles"
link="$HOME/.claude/output-styles"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:output-styles >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:output-styles >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:output-styles >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}output-styles"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:output-styles >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:output-styles >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:output-styles >&2; exit 1
    fi
  else
    echo link-collision:output-styles >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:output-styles >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:output-styles >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/lib"
link="$HOME/.claude/lib"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:lib >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:lib >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:lib >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}lib"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:lib >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:lib >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:lib >&2; exit 1
    fi
  else
    echo link-collision:lib >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:lib >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:lib >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/bin"
link="$HOME/.claude/bin"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:bin >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:bin >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:bin >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}bin"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}" --legacy 'sandbox-provision=7a0ab3b65f7885ee2f516421b0cd1ae79e212424178df00050d2b8041acec09c'; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:bin >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:bin >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:bin >&2; exit 1
    fi
  else
    echo link-collision:bin >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:bin >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:bin >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/workflows/lib"
link="$HOME/.claude/workflows/lib"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:workflows/lib >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:workflows/lib >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:workflows/lib >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}workflows/lib"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:workflows/lib >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:workflows/lib >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:workflows/lib >&2; exit 1
    fi
  else
    echo link-collision:workflows/lib >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:workflows/lib >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:workflows/lib >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/workflows/security-sweep.js"
link="$HOME/.claude/workflows/security-sweep.js"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:workflows/security-sweep.js >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:workflows/security-sweep.js >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:workflows/security-sweep.js >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}workflows/security-sweep.js"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:workflows/security-sweep.js >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:workflows/security-sweep.js >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:workflows/security-sweep.js >&2; exit 1
    fi
  else
    echo link-collision:workflows/security-sweep.js >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:workflows/security-sweep.js >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:workflows/security-sweep.js >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/systemd"
link="$HOME/.claude/systemd"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:systemd >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:systemd >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:systemd >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}systemd"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:systemd >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:systemd >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:systemd >&2; exit 1
    fi
  else
    echo link-collision:systemd >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:systemd >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:systemd >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/scripts"
link="$HOME/.claude/scripts"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:scripts >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:scripts >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:scripts >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}scripts"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:scripts >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:scripts >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:scripts >&2; exit 1
    fi
  else
    echo link-collision:scripts >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:scripts >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:scripts >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/tests"
link="$HOME/.claude/tests"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:tests >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:tests >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:tests >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}tests"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:tests >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:tests >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:tests >&2; exit 1
    fi
  else
    echo link-collision:tests >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:tests >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:tests >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/docs"
link="$HOME/.claude/docs"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:docs >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:docs >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:docs >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}docs"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:docs >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:docs >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:docs >&2; exit 1
    fi
  else
    echo link-collision:docs >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:docs >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:docs >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/command-assets"
link="$HOME/.claude/command-assets"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:command-assets >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:command-assets >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:command-assets >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}command-assets"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:command-assets >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:command-assets >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:command-assets >&2; exit 1
    fi
  else
    echo link-collision:command-assets >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:command-assets >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:command-assets >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/security-gate"
link="$HOME/.claude/security-gate"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:security-gate >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:security-gate >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:security-gate >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}security-gate"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:security-gate >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:security-gate >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:security-gate >&2; exit 1
    fi
  else
    echo link-collision:security-gate >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:security-gate >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:security-gate >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/statusline-command.sh"
link="$HOME/.claude/statusline-command.sh"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:statusline-command.sh >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:statusline-command.sh >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:statusline-command.sh >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}statusline-command.sh"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:statusline-command.sh >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:statusline-command.sh >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:statusline-command.sh >&2; exit 1
    fi
  else
    echo link-collision:statusline-command.sh >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:statusline-command.sh >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:statusline-command.sh >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/statusline-cost.sh"
link="$HOME/.claude/statusline-cost.sh"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:statusline-cost.sh >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:statusline-cost.sh >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:statusline-cost.sh >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}statusline-cost.sh"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:statusline-cost.sh >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:statusline-cost.sh >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:statusline-cost.sh >&2; exit 1
    fi
  else
    echo link-collision:statusline-cost.sh >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:statusline-cost.sh >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:statusline-cost.sh >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/statusline-router.sh"
link="$HOME/.claude/statusline-router.sh"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:statusline-router.sh >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:statusline-router.sh >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:statusline-router.sh >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}statusline-router.sh"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:statusline-router.sh >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:statusline-router.sh >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:statusline-router.sh >&2; exit 1
    fi
  else
    echo link-collision:statusline-router.sh >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:statusline-router.sh >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:statusline-router.sh >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/tools.json"
link="$HOME/.claude/tools.json"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:tools.json >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:tools.json >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:tools.json >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}tools.json"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:tools.json >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:tools.json >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:tools.json >&2; exit 1
    fi
  else
    echo link-collision:tools.json >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:tools.json >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:tools.json >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/build-priority.conf"
link="$HOME/.claude/build-priority.conf"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:build-priority.conf >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:build-priority.conf >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:build-priority.conf >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}build-priority.conf"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:build-priority.conf >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:build-priority.conf >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:build-priority.conf >&2; exit 1
    fi
  else
    echo link-collision:build-priority.conf >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:build-priority.conf >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:build-priority.conf >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/build-remote.json"
link="$HOME/.claude/build-remote.json"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:build-remote.json >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:build-remote.json >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:build-remote.json >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}build-remote.json"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:build-remote.json >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:build-remote.json >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:build-remote.json >&2; exit 1
    fi
  else
    echo link-collision:build-remote.json >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:build-remote.json >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:build-remote.json >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/buildbox-hosts.json"
link="$HOME/.claude/buildbox-hosts.json"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:buildbox-hosts.json >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:buildbox-hosts.json >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:buildbox-hosts.json >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}buildbox-hosts.json"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:buildbox-hosts.json >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:buildbox-hosts.json >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:buildbox-hosts.json >&2; exit 1
    fi
  else
    echo link-collision:buildbox-hosts.json >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:buildbox-hosts.json >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:buildbox-hosts.json >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/cloudflare-token-targets.json"
link="$HOME/.claude/cloudflare-token-targets.json"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:cloudflare-token-targets.json >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:cloudflare-token-targets.json >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:cloudflare-token-targets.json >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}cloudflare-token-targets.json"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:cloudflare-token-targets.json >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:cloudflare-token-targets.json >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:cloudflare-token-targets.json >&2; exit 1
    fi
  else
    echo link-collision:cloudflare-token-targets.json >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:cloudflare-token-targets.json >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:cloudflare-token-targets.json >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/local-gate.json"
link="$HOME/.claude/local-gate.json"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:local-gate.json >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:local-gate.json >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:local-gate.json >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}local-gate.json"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:local-gate.json >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:local-gate.json >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:local-gate.json >&2; exit 1
    fi
  else
    echo link-collision:local-gate.json >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:local-gate.json >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:local-gate.json >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/claudex-anthropic-settings.json"
link="$HOME/.claude/claudex-anthropic-settings.json"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:claudex-anthropic-settings.json >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:claudex-anthropic-settings.json >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:claudex-anthropic-settings.json >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}claudex-anthropic-settings.json"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:claudex-anthropic-settings.json >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:claudex-anthropic-settings.json >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:claudex-anthropic-settings.json >&2; exit 1
    fi
  else
    echo link-collision:claudex-anthropic-settings.json >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:claudex-anthropic-settings.json >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:claudex-anthropic-settings.json >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/coding_best_practices.md"
link="$HOME/.claude/coding_best_practices.md"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:coding_best_practices.md >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:coding_best_practices.md >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:coding_best_practices.md >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}coding_best_practices.md"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:coding_best_practices.md >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:coding_best_practices.md >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:coding_best_practices.md >&2; exit 1
    fi
  else
    echo link-collision:coding_best_practices.md >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:coding_best_practices.md >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:coding_best_practices.md >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/claude-home.md"
link="$HOME/.claude/CLAUDE.md"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:CLAUDE.md >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:CLAUDE.md >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:CLAUDE.md >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}CLAUDE.md"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:CLAUDE.md >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:CLAUDE.md >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:CLAUDE.md >&2; exit 1
    fi
  else
    echo link-collision:CLAUDE.md >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:CLAUDE.md >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:CLAUDE.md >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/mcp.json"
link="$HOME/.claude/mcp.json"
mkdir -p "$(dirname "$link")"
[ -e "$target" ] || { echo missing-runtime:mcp.json >&2; exit 1; }
quarantine=""
if [ -L "$link" ]; then
  case "$(readlink "$link")" in "$managed_root"*) ;; *) echo link-collision:mcp.json >&2; exit 1 ;; esac
elif [ -e "$link" ]; then
  if [ -d "$link" ]; then
    if rmdir -- "$link" 2>/dev/null; then
      :
    else
      quarantine="${link}.quarantine.$$"
      if ! mv -T "$link" "$quarantine"; then
        echo link-collision:mcp.json >&2; exit 1
      fi
      _subset_targets=("$target")
      for _rev_dir in "$managed_root"*/; do
        [ -f "${_rev_dir}.runtime-complete" ] || continue
        _rev_target="${_rev_dir}mcp.json"
        [ -e "$_rev_target" ] || continue
        _subset_targets+=("$_rev_target")
      done
      if ! runtime_subset_validate "$quarantine" "${_subset_targets[@]}"; then
        if [ ! -e "$link" ]; then
          mv -T "$quarantine" "$link"
        fi
        echo link-collision:mcp.json >&2
        exit 1
      fi
    fi
  elif [ -f "$link" ] && [ -f "$target" ]; then
    quarantine="${link}.quarantine.$$"
    if ! mv -T "$link" "$quarantine"; then
      echo link-collision:mcp.json >&2; exit 1
    fi
    if ! cmp -s "$quarantine" "$target"; then
      mv -T "$quarantine" "$link" || true
      echo link-collision:mcp.json >&2; exit 1
    fi
  else
    echo link-collision:mcp.json >&2; exit 1
  fi
fi
if [ -n "$quarantine" ] && [ -e "$link" ]; then
  echo link-collision:mcp.json >&2; exit 1
fi
if ! ln -sfnT "$target" "$link"; then
  if [ -n "$quarantine" ] && [ -e "$quarantine" ] && [ ! -e "$link" ]; then
    mv -T "$quarantine" "$link" || true
  fi
  echo link-collision:mcp.json >&2; exit 1
fi
if [ -n "$quarantine" ] && [ -e "$quarantine" ]; then
  rm -rf "$quarantine"
fi
target="$HOME/.local/share/overdeck/claude-runtime/content-35eb1b643a53/settings.json"
link="$HOME/.claude/settings.json"
mkdir -p "$(dirname "$link")"
prov="$HOME/.local/state/overdeck/claude-runtime-copy/e9e1aabe0aae56cb1eba125e053b355aaf43680cf243ee7b518e1c897e5868c1.sha256"
[ -f "$target" ] || { echo missing-runtime:settings.json >&2; exit 1; }
if [ -d "$link" ] || [ -L "$link" ]; then echo link-collision:settings.json >&2; exit 1; fi
if [ -f "$link" ] && ! cmp -s "$target" "$link"; then
  if [ ! -f "$prov" ]; then echo link-collision:settings.json >&2; exit 1; fi
  link_fp=$(sha256sum "$link" | awk '{print $1}')
  prov_fp=$(tr -d '\n' <"$prov")
  [ "$link_fp" = "$prov_fp" ] || { echo link-collision:settings.json >&2; exit 1; }
fi
publish_tmp="${link}.publish.$$"
cp -a "$target" "$publish_tmp"
mv -T "$publish_tmp" "$link"
mkdir -p "$(dirname "$prov")"
link_fp=$(sha256sum "$link" | awk '{print $1}')
prov_tmp="${prov}.tmp.$$"
printf '%s\n' "$link_fp" >"$prov_tmp"
mv -T "$prov_tmp" "$prov"