"""Capture verified source files and private model settings for a Windows migration.""" from __future__ import annotations import argparse from datetime import datetime, timezone import json import os from pathlib import Path import re import shutil import sqlite3 import sys import tarfile import uuid from migration_common import filter_models, relative_name, sha256 ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / 'scripts')) import backup def private_json(path, data): fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) with os.fdopen(fd, 'w', encoding='utf-8') as output: json.dump(data, output, ensure_ascii=False, indent=2) output.write('\n') def native_copy(source, target): source_db = sqlite3.connect(source.resolve().as_uri() + '?mode=ro', uri=True) destination = sqlite3.connect(target) try: source_db.backup(destination) destination.execute('PRAGMA foreign_keys=OFF') destination.execute('PRAGMA secure_delete=ON') tables = {row[0] for row in destination.execute("SELECT name FROM sqlite_master WHERE type='table'")} # Access sessions, external integrations and cached provider keys are # recreated on Windows. Historical chats and their ownership stay intact. for name in ('session', 'verification', 'api_key', 'jwks', 'oauth_access_token', 'oauth_refresh_token', 'oauth_consent', 'oauth_client', 'mcp_user_token', 'mcp_oauth_client', 'sandbox_secret', 'log', 'mcp_call_log'): if name in tables: destination.execute('DELETE FROM "' + name + '"') if 'project_llm_config' in tables: destination.execute('DELETE FROM project_llm_config') for name in ('user', 'account', 'organization'): if name not in tables: continue columns = list(destination.execute('PRAGMA table_info("' + name + '")')) for column in columns: key = column[1] if key in ('github_access_token', 'gitlab_access_token', 'access_token', 'refresh_token', 'id_token', 'google_client_secret'): replacement = "''" if column[3] else 'NULL' destination.execute('UPDATE "' + name + '" SET "' + key + '"=' + replacement) destination.commit() destination.execute('VACUUM') assert destination.execute('PRAGMA integrity_check').fetchone()[0] == 'ok' finally: destination.close() source_db.close() target.chmod(0o600) def main(): parser = argparse.ArgumentParser() parser.add_argument('--work', type=Path, required=True) args = parser.parse_args() work = args.work.resolve() payload = work / 'migration' manifest = json.loads((payload / 'source-snapshot.json').read_text()) database = manifest['sourceDatabase'] query = lambda sql: backup.client(sql, database) archive_root = ROOT / 'runtimedata/archive' files, sources, digest_index = {}, {}, {} for path in sorted(archive_root.rglob('*')): if path.is_symlink(): raise RuntimeError('来源归档包含符号链接。') if not path.is_file(): continue rel = relative_name(path.relative_to(archive_root).as_posix()).as_posix() digest = sha256(path) files[rel] = {'sha256': digest, 'bytes': path.stat().st_size} sources[rel] = path digest_index.setdefault(digest, rel) bindings = [] missing_optional = 0 for table, column, state in ( ('source_revision', 'archive_path', "'REQUIRED'"), ('source_candidate', 'source_path', 'status'), ('source_candidate', 'archive_path', 'status'), ('source_upload_draft', 'source_path', "IF(expires_at