mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
- Require the exact config-group and weights key sets the pinned assembler writes, with exact JSON types, so per-group format overrides, block_structure, and unknown nested keys fail closed. - Restore and back up prepared files via staged temp files and os.replace with the recorded mode, so read-only originals roll back cleanly and retries are not blocked by retained backups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1093 lines
43 KiB
Python
1093 lines
43 KiB
Python
"""Pinned external GSQ source record and fail-closed prepare adapter.
|
|
|
|
The adapter is textual and two-phase: every expected upstream fragment must
|
|
match exactly the expected count before any file is changed, and re-running
|
|
it over an already-prepared tree is a no-op. Anything deployment-specific
|
|
from prior art (lease brokers, supervisor hooks, service users, installer
|
|
paths) is intentionally not carried over.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import os
|
|
import subprocess
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
|
|
GSQ_REPOSITORY = "https://github.com/IST-DASLab/GSQ.git"
|
|
GSQ_REVISION = "03fc16484c369e3127225615d5e03e8d3a6043e3"
|
|
EXPECTED_PREPARED_TREE_SHA256 = (
|
|
"6f56d133aa95116f9ec7ab76154d8af5ab830187bbb37ceed5d0d73d84d0d1d9"
|
|
)
|
|
GUARD_FILENAME = "gsq_numerics_guard.py"
|
|
OPEN_THOUGHTS_REVISION = "bd093c3994fd54d2390985b66988ddf282a55eb6"
|
|
# Staging temp files (removed on both paths) and original-content backups
|
|
# (removed on success, kept on failure for inspection) for atomic prepare.
|
|
_PREPARE_TMP_SUFFIX = ".obliteratus-tmp"
|
|
_PREPARE_BACKUP_SUFFIX = ".obliteratus-backup"
|
|
|
|
_ENABLED_REMOTE_CODE = "trust_remote_code=" + "True"
|
|
_DISABLED_REMOTE_CODE = "trust_remote_code=False"
|
|
_EXCLUDED_PARTS = frozenset({".git", ".venv", "venv", "__pycache__"})
|
|
|
|
|
|
# (relative path, expected old fragment, replacement, expected match count)
|
|
TEXT_PATCHES: tuple[tuple[str, str, str, int], ...] = (
|
|
(
|
|
"main.py",
|
|
" name_lower = model_name.lower()\n if 'llama' in name_lower:\n",
|
|
" name_lower = model_name.lower()\n"
|
|
' declared_model_type = ""\n'
|
|
" try:\n"
|
|
" from transformers import AutoConfig as _AC\n"
|
|
" _cfg = _AC.from_pretrained(model_name, trust_remote_code=False)\n"
|
|
' _tc = getattr(_cfg, "text_config", None) or _cfg\n'
|
|
' declared_model_type ='
|
|
' str(getattr(_tc, "model_type", "") or "").lower()\n'
|
|
" except Exception:\n"
|
|
' declared_model_type = ""\n'
|
|
" # Config-based dispatch for certified dense families: the declared\n"
|
|
" # model_type (top-level or text_config) selects the branch\n"
|
|
" # regardless of the path name, so neutral paths such as\n"
|
|
" # /models/export work. Unknown or remote-code types leave\n"
|
|
" # name-based dispatch unchanged. MoE detection inside the qwen\n"
|
|
" # branches stays config-based (hasattr(_tc, 'num_experts') or\n"
|
|
" # hasattr(_tc, 'num_local_experts')).\n"
|
|
' if declared_model_type == "llama":\n'
|
|
' name_lower = "llama"\n'
|
|
' elif declared_model_type == "qwen3":\n'
|
|
' name_lower = "qwen3"\n'
|
|
" elif declared_model_type in {'qwen3_5', 'qwen3_5_text'}:\n"
|
|
' name_lower = "qwen3.5"\n'
|
|
" if 'llama' in name_lower:\n",
|
|
1,
|
|
),
|
|
(
|
|
"main.py",
|
|
"elif 'qwen3.5' in name_lower or 'qwen3.6' in name_lower:",
|
|
"elif (\n"
|
|
" 'qwen3.5' in name_lower\n"
|
|
" or 'qwen3.6' in name_lower\n"
|
|
" or declared_model_type in {'qwen3_5', 'qwen3_5_text'}\n"
|
|
" ):",
|
|
1,
|
|
),
|
|
(
|
|
"main.py",
|
|
" inps = torch.zeros((num_samples, seqlen, hidden_size),"
|
|
" dtype=dtype, device='cpu')\n",
|
|
" # The caller fills every sample before use; avoid eagerly faulting\n"
|
|
" # and zeroing large anonymous activation buffers.\n"
|
|
" inps = torch.empty((num_samples, seqlen, hidden_size),"
|
|
" dtype=dtype, device='cpu')\n",
|
|
1,
|
|
),
|
|
(
|
|
"src/models/qwen35.py",
|
|
"class Qwen35Wrapper(BaseModelWrapper):\n",
|
|
"def detect_qwen35_model_prefix(checkpoint_names):\n"
|
|
' """Return the transformer prefix for text-only or multimodal'
|
|
' checkpoints."""\n'
|
|
' if any(name.startswith("model.layers.")'
|
|
" for name in checkpoint_names):\n"
|
|
' return "model"\n'
|
|
' if any(name.startswith("model.language_model.layers.")'
|
|
" for name in checkpoint_names):\n"
|
|
' return "model.language_model"\n'
|
|
" raise ValueError(\n"
|
|
' "Unsupported Qwen3.5 checkpoint layout: expected model.layers.* "\n'
|
|
' "or model.language_model.layers.* tensors"\n'
|
|
" )\n"
|
|
"\n"
|
|
"\n"
|
|
"class Qwen35Wrapper(BaseModelWrapper):\n",
|
|
1,
|
|
),
|
|
(
|
|
"src/models/qwen35.py",
|
|
'self.layer_prefix = "model.language_model.layers"',
|
|
"_names_for_prefix = dict(self._name_to_shard or {})\n"
|
|
" if not _names_for_prefix and self._single_shard:\n"
|
|
" from safetensors import safe_open as _safe_open\n"
|
|
" with _safe_open(self._single_shard, framework=\"pt\","
|
|
' device="cpu") as _shard:\n'
|
|
" _names_for_prefix = {key: None for key in _shard.keys()}\n"
|
|
" self.model_prefix ="
|
|
" detect_qwen35_model_prefix(_names_for_prefix)\n"
|
|
' self.layer_prefix = f"{self.model_prefix}.layers"',
|
|
1,
|
|
),
|
|
(
|
|
"src/models/qwen35.py",
|
|
'self._names_from_ckpt(["model.language_model.embed_tokens"])',
|
|
'self._names_from_ckpt([f"{self.model_prefix}.embed_tokens"])',
|
|
1,
|
|
),
|
|
(
|
|
"src/models/qwen35.py",
|
|
'self._names_from_ckpt("model.language_model.norm")',
|
|
'self._names_from_ckpt(f"{self.model_prefix}.norm")',
|
|
1,
|
|
),
|
|
(
|
|
"src/models/qwen35.py",
|
|
' subset[f"{base_prefix}.in_proj_qkv"]'
|
|
" = current_layer.linear_attn.in_proj_qkv\n"
|
|
' subset[f"{base_prefix}.in_proj_z"]'
|
|
" = current_layer.linear_attn.in_proj_z\n"
|
|
' subset[f"{base_prefix}.out_proj"]'
|
|
" = current_layer.linear_attn.out_proj\n",
|
|
" for name, module in"
|
|
" current_layer.linear_attn.named_modules():\n"
|
|
" if isinstance(module, torch.nn.Linear):\n"
|
|
' subset[f"{base_prefix}.{name}"] = module\n',
|
|
1,
|
|
),
|
|
(
|
|
"src/models/qwen35.py",
|
|
" compressed_attn_layers = ['in_proj_qkv', 'in_proj_z',"
|
|
" 'out_proj']\n"
|
|
" non_compressed_attn_layers = ['conv1d', 'in_proj_a',"
|
|
" 'in_proj_b', 'norm']\n",
|
|
" compressed_attn_layers = [\n"
|
|
" 'in_proj_qkv', 'in_proj_z', 'in_proj_a', 'in_proj_b',"
|
|
" 'out_proj'\n"
|
|
" ]\n"
|
|
" non_compressed_attn_layers = ['conv1d', 'norm']\n",
|
|
1,
|
|
),
|
|
(
|
|
"src/models/qwen35.py",
|
|
" compressed = self.compressor.compress_weight(\n"
|
|
" weight=module.weight.data.detach().cpu(),\n"
|
|
' scale=self.temp_weights[f"{base}.scale"].detach().cpu(),\n'
|
|
" quantization_args=self.quantization_config.config_groups.group_0.weights\n" # noqa: E501
|
|
" )\n"
|
|
' to_save[base + ".weight_packed"] = compressed["weight_packed"]\n'
|
|
' to_save[base + ".weight_scale"] ='
|
|
' self.temp_weights[f"{base}.scale"].detach().cpu()\n',
|
|
" scale_cpu ="
|
|
' self.temp_weights[f"{base}.scale"].detach().cpu()\n'
|
|
" compressed = self._compress_weight(\n"
|
|
" module.weight.data.detach().cpu(),\n"
|
|
" scale_cpu,\n"
|
|
" )\n"
|
|
' to_save[base + ".weight_packed"] = compressed["weight_packed"]\n'
|
|
' to_save[base + ".weight_scale"] = scale_cpu\n',
|
|
1,
|
|
),
|
|
(
|
|
"src/models/qwen3.py",
|
|
" compressed = self.compressor.compress_weight(\n"
|
|
" weight=module.weight.data.detach().cpu(),\n"
|
|
' scale=self.temp_weights[f"{base}.scale"].detach().cpu(),\n'
|
|
" quantization_args=self.quantization_config.config_groups.group_0.weights\n" # noqa: E501
|
|
" )\n"
|
|
' to_save[base + ".weight_packed"] = compressed["weight_packed"]\n'
|
|
' to_save[base + ".weight_scale"] ='
|
|
' self.temp_weights[f"{base}.scale"].detach().cpu()\n',
|
|
" scale_cpu ="
|
|
' self.temp_weights[f"{base}.scale"].detach().cpu()\n'
|
|
" compressed = self._compress_weight(\n"
|
|
" module.weight.data.detach().cpu(),\n"
|
|
" scale_cpu,\n"
|
|
" )\n"
|
|
' to_save[base + ".weight_packed"] = compressed["weight_packed"]\n'
|
|
' to_save[base + ".weight_scale"] = scale_cpu\n',
|
|
1,
|
|
),
|
|
(
|
|
"src/models/base.py",
|
|
" handle.remove()\n"
|
|
"\n"
|
|
' self.kwargs["position_embeddings"] ='
|
|
' (self.kwargs["position_embeddings"][0][0].unsqueeze(0),'
|
|
' self.kwargs["position_embeddings"][1][0].unsqueeze(0))\n',
|
|
" handle.remove()\n"
|
|
" if cache['index'] != total_batches:\n"
|
|
" raise RuntimeError(\n"
|
|
' f"Activation capture incomplete: {cache[\'index\']}/'
|
|
'{total_batches} batches filled"\n'
|
|
" )\n"
|
|
"\n"
|
|
' self.kwargs["position_embeddings"] ='
|
|
' (self.kwargs["position_embeddings"][0][0].unsqueeze(0),'
|
|
' self.kwargs["position_embeddings"][1][0].unsqueeze(0))\n',
|
|
1,
|
|
),
|
|
(
|
|
"src/prior/gptq.py",
|
|
" damp = percdamp * torch.mean(torch.diag(self.H))\n"
|
|
" diag = torch.arange(self.columns, device=self.dev)\n"
|
|
" self.H[diag, diag] += damp\n"
|
|
" self.H = torch.linalg.cholesky(self.H)\n",
|
|
" from gsq_numerics_guard import damped_cholesky\n"
|
|
" self.H = damped_cholesky(self.H, percdamp, self.name)\n",
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
"def _build_ignore_list(model_config):",
|
|
"def _build_ignore_list(model_config, *, quantize_attention=False):",
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
' if "vision_config" in model_config or "vision_tower" in model_type:\n'
|
|
' ignore.append("re:.*vision_tower.*")\n'
|
|
' ignore.append("re:.*mm_projector.*")\n',
|
|
' if "vision_config" in model_config or "vision_tower" in model_type:\n'
|
|
' ignore.append("re:.*vision_tower.*")\n'
|
|
' ignore.append("re:.*mm_projector.*")\n'
|
|
" # Qwen3.5-style checkpoints keep dense vision and multi-token\n"
|
|
" # prediction Linear weights outside vision_tower/mm_projector\n"
|
|
" # (model.visual.* and mtp.* stay dense while the exported\n"
|
|
" # metadata targets all Linear modules), so exclude them too.\n"
|
|
' ignore.append("re:.*visual.*")\n'
|
|
' ignore.append("re:.*mtp.*")\n',
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
' "re:.*embed_tokens.*",\n'
|
|
' "re:.*self_attn.*",\n'
|
|
' "re:.*input_layernorm.*",\n',
|
|
' "re:.*embed_tokens.*",\n'
|
|
' "re:.*input_layernorm.*",\n',
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
' text_config = model_config.get("text_config", {})\n',
|
|
" if not quantize_attention:\n"
|
|
' ignore.append("re:.*self_attn.*")\n'
|
|
"\n"
|
|
' text_config = model_config.get("text_config", {})\n',
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
'if has_linear_attn:\n ignore.append("re:.*linear_attn.*")',
|
|
'if has_linear_attn and not quantize_attention:\n'
|
|
' ignore.append("re:.*linear_attn.*")',
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
"def inject_quantization_config(out_dir, groupsize, wbits=4, num_layers=None,"
|
|
" quantized_layer_indices=None):",
|
|
"def inject_quantization_config(out_dir, groupsize, wbits=4, num_layers=None,"
|
|
" quantized_layer_indices=None, *, quantize_attention=False):",
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
"ignore = _build_ignore_list(config)",
|
|
"ignore = _build_ignore_list(config,"
|
|
" quantize_attention=quantize_attention)",
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
' parser.add_argument("--out-dir", type=str, default=None,\n'
|
|
' help="Output directory. Defaults to'
|
|
' <checkpoint_dir>/<run_id>/assembled")\n',
|
|
' parser.add_argument("--out-dir", type=str, default=None,\n'
|
|
' help="Output directory. Defaults to'
|
|
' <checkpoint_dir>/<run-id>/assembled")\n'
|
|
' parser.add_argument("--allow-partial", action="store_true",\n'
|
|
' help="Explicitly allow non-release assembly of'
|
|
" incomplete layer runs\")\n"
|
|
' parser.add_argument("--run-dir", type=str, default=None,\n'
|
|
' help="Resolved run directory. Overrides'
|
|
" --run-id and the config checkpoint directory\")\n",
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
' print(f"\\nInjecting quantization_config into config.json'
|
|
' (group_size={groupsize})...")\n'
|
|
" if quantized_layer_indices and len(quantized_layer_indices) < num_layers:\n", # noqa: E501
|
|
' print(f"\\nInjecting quantization_config into config.json'
|
|
' (group_size={groupsize})...")\n'
|
|
" if set(quantized_layer_indices) != set(range(num_layers))"
|
|
" and not args.allow_partial:\n"
|
|
" raise RuntimeError(\n"
|
|
' f"Refusing partial assembly:'
|
|
' {len(quantized_layer_indices)}/{num_layers} layers present"\n'
|
|
" )\n"
|
|
" if quantized_layer_indices and len(quantized_layer_indices) < num_layers:\n", # noqa: E501
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
" quantized_layer_indices=quantized_layer_indices,\n"
|
|
" )\n",
|
|
" quantized_layer_indices=quantized_layer_indices,\n"
|
|
" quantize_attention=cfg.quantization.self_attn,\n"
|
|
" )\n",
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
" if args.run_id is not None:\n"
|
|
" run_id = args.run_id\n"
|
|
" else:\n"
|
|
" run_id = find_latest_run(checkpoint_dir)\n"
|
|
" if run_id is None:\n"
|
|
" raise RuntimeError(\n"
|
|
' f"No completed runs found in \'{checkpoint_dir}\'. "\n'
|
|
' "Pass --run-id explicitly or complete a training run'
|
|
' first.")\n'
|
|
"\n"
|
|
" run_dir = os.path.join(checkpoint_dir, run_id)\n"
|
|
" if not os.path.isdir(run_dir):\n"
|
|
' raise FileNotFoundError(f"Run directory not found: {run_dir}")\n',
|
|
" if args.run_dir is not None:\n"
|
|
" run_dir = args.run_dir\n"
|
|
" if not os.path.isdir(run_dir):\n"
|
|
' raise FileNotFoundError('
|
|
'f"Run directory not found: {run_dir}")\n'
|
|
" run_id = os.path.basename(os.path.normpath(run_dir))\n"
|
|
" else:\n"
|
|
" if args.run_id is not None:\n"
|
|
" run_id = args.run_id\n"
|
|
" else:\n"
|
|
" run_id = find_latest_run(checkpoint_dir)\n"
|
|
" if run_id is None:\n"
|
|
" raise RuntimeError(\n"
|
|
' f"No completed runs found in \'{checkpoint_dir}\'. "\n'
|
|
' "Pass --run-id explicitly or complete a training'
|
|
' run first.")\n'
|
|
"\n"
|
|
" run_dir = os.path.join(checkpoint_dir, run_id)\n"
|
|
" if not os.path.isdir(run_dir):\n"
|
|
' raise FileNotFoundError('
|
|
'f"Run directory not found: {run_dir}")\n',
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
" if not quantized_entries:\n"
|
|
" raise RuntimeError(\n"
|
|
' f"No quantized shard files found in {run_dir}. "\n'
|
|
' "Make sure the training run completed at least one'
|
|
' layer.")\n',
|
|
" if not quantized_entries:\n"
|
|
" raise RuntimeError(\n"
|
|
' f"No quantized shard files found in {run_dir}. "\n'
|
|
' "Make sure the training run completed at least one'
|
|
' layer.")\n'
|
|
" _assembled_layer_indices = sorted({\n"
|
|
" e[\"layer_idx\"] for e in quantized_entries.values()\n"
|
|
' if e["layer_idx"] is not None\n'
|
|
" })\n"
|
|
" if _assembled_layer_indices != list(range(num_layers))"
|
|
" and not args.allow_partial:\n"
|
|
" raise RuntimeError(\n"
|
|
' f"Refusing partial assembly:'
|
|
' {len(_assembled_layer_indices)}/{num_layers} layers present"\n'
|
|
" )\n",
|
|
1,
|
|
),
|
|
(
|
|
"src/data/dataset.py",
|
|
'load_dataset("open-thoughts/OpenThoughts-114k", split="train")',
|
|
'load_dataset(\n'
|
|
' "open-thoughts/OpenThoughts-114k",\n'
|
|
' split="train",\n'
|
|
' revision="' + OPEN_THOUGHTS_REVISION + '",\n'
|
|
" )",
|
|
2,
|
|
),
|
|
(
|
|
"src/data/dataset.py",
|
|
" messages.append({\n"
|
|
' "role": "system",\n'
|
|
' "content": (\n'
|
|
' "You are Kimi, an AI assistant created by Moonshot AI."\n'
|
|
" ),\n"
|
|
" })\n",
|
|
" # Calibration carries no system identity prompt.\n",
|
|
1,
|
|
),
|
|
(
|
|
"main.py",
|
|
"def main():\n load_dotenv()\n",
|
|
"def main():\n"
|
|
" # The launcher controls the environment: do not load .env\n"
|
|
" # files, which could re-inject GSQ_* overrides the launcher\n"
|
|
" # stripped.\n",
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
' has_linear_attn = "layer_types" in merged\n',
|
|
" # Mirror transformers Qwen3_5TextConfig: without layer_types,\n"
|
|
" # Qwen3.5 derives hybrid topology from full_attention_interval,\n"
|
|
" # so a declared qwen3_5/qwen3_5_text model has linear attention\n"
|
|
" # even when layer_types is omitted (top-level or text_config,\n"
|
|
" # merged above).\n"
|
|
' _has_qwen35_linear = str(\n'
|
|
' merged.get("model_type", "") or "").lower() in {\n'
|
|
' "qwen3_5", "qwen3_5_text"}\n'
|
|
' has_linear_attn = "layer_types" in merged'
|
|
" or _has_qwen35_linear\n",
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
' "tokenizer.model", "tiktoken.model",\n'
|
|
' "preprocessor_config.json",\n',
|
|
' "tokenizer.model", "tiktoken.model",\n'
|
|
' "preprocessor_config.json",\n'
|
|
' "chat_template.jinja", "chat_template.json",\n'
|
|
' "added_tokens.json", "processor_config.json",\n'
|
|
' "video_preprocessor_config.json",\n',
|
|
1,
|
|
),
|
|
(
|
|
"save_model.py",
|
|
" copied = []\n"
|
|
" for fname in os.listdir(base_model_dir):\n"
|
|
' if fname in patterns or fname.endswith(".py"):\n'
|
|
" src = os.path.join(base_model_dir, fname)\n"
|
|
" dst = os.path.join(out_dir, fname)\n"
|
|
" if os.path.isfile(src):\n"
|
|
" shutil.copy2(src, dst)\n"
|
|
" copied.append(fname)\n"
|
|
" return copied\n",
|
|
" copied = []\n"
|
|
" for fname in os.listdir(base_model_dir):\n"
|
|
' if fname in patterns or fname.endswith(".py"):\n'
|
|
" src = os.path.join(base_model_dir, fname)\n"
|
|
" dst = os.path.join(out_dir, fname)\n"
|
|
" if os.path.isfile(src):\n"
|
|
" shutil.copy2(src, dst)\n"
|
|
" copied.append(fname)\n"
|
|
' chat_templates_src = os.path.join(base_model_dir,'
|
|
' "chat_templates")\n'
|
|
" if os.path.isdir(chat_templates_src):\n"
|
|
' chat_templates_dst = os.path.join(out_dir, "chat_templates")\n'
|
|
" os.makedirs(chat_templates_dst, exist_ok=True)\n"
|
|
" for fname in sorted(os.listdir(chat_templates_src)):\n"
|
|
' if fname.endswith(".jinja"):\n'
|
|
" src = os.path.join(chat_templates_src, fname)\n"
|
|
" dst = os.path.join(chat_templates_dst, fname)\n"
|
|
" if os.path.isfile(src):\n"
|
|
" shutil.copy2(src, dst)\n"
|
|
' copied.append(os.path.join("chat_templates",'
|
|
" fname))\n"
|
|
" return copied\n",
|
|
1,
|
|
),
|
|
)
|
|
|
|
|
|
def _iter_python_files(root: Path):
|
|
"""Yield every relevant ``*.py`` file under root in sorted order.
|
|
|
|
Exclusions match checkout-relative components only: an absolute
|
|
ancestor such as ``/work/venv/GSQ`` must not exclude the
|
|
checkout's own files.
|
|
"""
|
|
for path in sorted(root.rglob("*.py")):
|
|
if any(part in _EXCLUDED_PARTS for part in path.relative_to(root).parts):
|
|
continue
|
|
yield path
|
|
|
|
|
|
def _git(root: Path, *args: str) -> str:
|
|
return subprocess.check_output(
|
|
["git", "-C", str(root), *args], text=True, stderr=subprocess.STDOUT
|
|
).strip()
|
|
|
|
|
|
def verify_checkout(path: str | Path) -> str:
|
|
"""Fail closed unless the checkout is pinned to :data:`GSQ_REVISION`."""
|
|
root = Path(path)
|
|
try:
|
|
head = _git(root, "rev-parse", "HEAD")
|
|
except (OSError, subprocess.CalledProcessError) as exc:
|
|
raise RuntimeError(
|
|
f"cannot verify GSQ checkout at {root}: {exc}"
|
|
) from exc
|
|
if head != GSQ_REVISION:
|
|
raise RuntimeError(
|
|
f"GSQ checkout must be pinned to {GSQ_REVISION}, found {head}"
|
|
)
|
|
return head
|
|
|
|
|
|
def tree_digest(path: str | Path) -> str:
|
|
"""Hash the prepared tree's relevant ``*.py`` files deterministically.
|
|
|
|
Relative paths are hashed in canonical POSIX form (``as_posix``),
|
|
so the digest is identical on Windows and POSIX checkouts of the
|
|
same tree: ``str(relative_path)`` would hash backslashes on
|
|
Windows. On POSIX ``as_posix`` is byte-identical to ``str``.
|
|
"""
|
|
root = Path(path)
|
|
digest = hashlib.sha256()
|
|
for file_path in _iter_python_files(root):
|
|
relative = file_path.relative_to(root).as_posix()
|
|
digest.update(relative.encode())
|
|
digest.update(b"\0")
|
|
digest.update(file_path.read_bytes())
|
|
return digest.hexdigest()
|
|
|
|
|
|
def _guard_source_bytes() -> bytes:
|
|
source = Path(__file__).with_name("numerics_guard.py")
|
|
try:
|
|
return source.read_bytes()
|
|
except OSError as exc:
|
|
raise RuntimeError(
|
|
f"cannot read numerics guard source at {source}: {exc}"
|
|
) from exc
|
|
|
|
|
|
def _patch_state(
|
|
target: Path, text: str, old: str, new: str, count: int
|
|
) -> str:
|
|
"""Classify one patch as ``pending`` or ``applied``, failing closed.
|
|
|
|
A fully applied patch holds the replacement exactly ``count`` times;
|
|
the old fragment may only remain as a substring of the replacement
|
|
itself (some replacements extend the fragment they replace). Any other
|
|
mixture -- a replacement present without its old fragment fully
|
|
replaced (half-applied), old and new side by side, or a tampered
|
|
fragment -- raises instead of reporting success.
|
|
"""
|
|
new_count = text.count(new)
|
|
if new_count == 0:
|
|
actual = text.count(old)
|
|
if actual != count:
|
|
raise RuntimeError(
|
|
f"{target}: expected {count} source fragment(s), found {actual}"
|
|
)
|
|
return "pending"
|
|
if new_count != count:
|
|
raise RuntimeError(
|
|
f"{target}: half-applied patch: replacement present"
|
|
f" {new_count}/{count} times; refusing to report success"
|
|
)
|
|
residual = new.count(old) * count
|
|
if text.count(old) != residual:
|
|
raise RuntimeError(
|
|
f"{target}: half-applied patch: old and replacement fragments"
|
|
" both present; refusing to report success"
|
|
)
|
|
return "applied"
|
|
|
|
|
|
def _read_staged_texts(
|
|
root: Path,
|
|
) -> tuple[dict[Path, str], dict[Path, bytes]]:
|
|
"""Read and validate every patch target in memory without writing.
|
|
|
|
Runs Phase 1 of :func:`prepare`: every ``TEXT_PATCHES`` fragment must
|
|
match exactly its expected count (pending or fully applied;
|
|
half-applied raises), plus the ``trust_remote_code`` sweep. Returns
|
|
``(current, originals)`` where ``current`` holds the prospective file
|
|
texts for files needing a change and ``originals`` holds the original
|
|
bytes for every relevant ``*.py`` file read. No file is written.
|
|
"""
|
|
current: dict[Path, str] = {}
|
|
originals: dict[Path, bytes] = {}
|
|
for relative, old, new, count in TEXT_PATCHES:
|
|
target = root / relative
|
|
if target not in current:
|
|
try:
|
|
raw = target.read_bytes()
|
|
except OSError as exc:
|
|
raise RuntimeError(
|
|
f"GSQ checkout is missing expected file {relative}: {exc}"
|
|
) from exc
|
|
try:
|
|
current[target] = raw.decode("utf-8")
|
|
except UnicodeDecodeError as exc:
|
|
raise RuntimeError(
|
|
f"GSQ checkout file {relative} is not UTF-8: {exc}"
|
|
) from exc
|
|
originals[target] = raw
|
|
state = _patch_state(target, current[target], old, new, count)
|
|
if state == "pending":
|
|
current[target] = current[target].replace(old, new, count)
|
|
final_new = current[target].count(new)
|
|
if final_new != count:
|
|
raise RuntimeError(
|
|
f"{target}: patch did not reach its complete state"
|
|
f" ({final_new}/{count}); refusing to write"
|
|
)
|
|
|
|
for file_path in _iter_python_files(root):
|
|
if file_path in current:
|
|
text = current[file_path]
|
|
else:
|
|
try:
|
|
raw = file_path.read_bytes()
|
|
except OSError as exc:
|
|
raise RuntimeError(
|
|
f"cannot read GSQ file {file_path}: {exc}"
|
|
) from exc
|
|
try:
|
|
text = raw.decode("utf-8")
|
|
except UnicodeDecodeError as exc:
|
|
raise RuntimeError(
|
|
f"GSQ file {file_path} is not UTF-8: {exc}"
|
|
) from exc
|
|
originals[file_path] = raw
|
|
if _ENABLED_REMOTE_CODE in text:
|
|
current[file_path] = text.replace(
|
|
_ENABLED_REMOTE_CODE, _DISABLED_REMOTE_CODE
|
|
)
|
|
return current, originals
|
|
|
|
|
|
def _build_prepare_jobs(
|
|
root: Path,
|
|
current: dict[Path, str],
|
|
originals: dict[Path, bytes],
|
|
guard_bytes: bytes,
|
|
) -> dict[Path, bytes]:
|
|
"""Build the promotion jobs without writing anything.
|
|
|
|
Re-reads each staged target to refuse a tree that changed during
|
|
planning, then returns ``{target: prospective bytes}`` for every file
|
|
whose contents would change (including the guard file when stale or
|
|
missing). No temp, backup, or target file is created or modified.
|
|
"""
|
|
jobs: dict[Path, bytes] = {}
|
|
for target, text in current.items():
|
|
try:
|
|
disk = target.read_bytes()
|
|
except OSError as exc:
|
|
raise RuntimeError(
|
|
f"cannot re-read GSQ file {target}: {exc}"
|
|
) from exc
|
|
if disk != originals[target]:
|
|
raise RuntimeError(
|
|
f"{target}: tree changed during prepare; refusing to write"
|
|
)
|
|
data = text.encode("utf-8")
|
|
if data != disk:
|
|
jobs[target] = data
|
|
guard_target = root / GUARD_FILENAME
|
|
try:
|
|
guard_disk = guard_target.read_bytes()
|
|
except OSError:
|
|
guard_disk = None
|
|
guard_changed = guard_disk != guard_bytes
|
|
if guard_changed:
|
|
jobs[guard_target] = guard_bytes
|
|
return jobs
|
|
|
|
|
|
def _prospective_digest_from_plan(
|
|
root: Path,
|
|
jobs: dict[Path, bytes],
|
|
originals: dict[Path, bytes],
|
|
) -> str:
|
|
"""Hash the tree as it would look after promoting ``jobs``.
|
|
|
|
Combines the in-memory staged contents (``jobs``) with the unchanged
|
|
files (``originals``) in the same deterministic order :func:`tree_digest`
|
|
uses (sorted relative path, NUL, bytes). Only relevant ``*.py`` files
|
|
outside :data:`_EXCLUDED_PARTS` affect the digest; the guard file is
|
|
included via ``jobs`` when new or stale. Reads nothing from disk.
|
|
"""
|
|
relevant: dict[Path, bytes] = dict(originals)
|
|
for target, data in jobs.items():
|
|
if target.suffix != ".py":
|
|
continue
|
|
try:
|
|
relative_parts = target.relative_to(root).parts
|
|
except ValueError:
|
|
continue
|
|
if any(part in _EXCLUDED_PARTS for part in relative_parts):
|
|
continue
|
|
relevant[target] = data
|
|
digest = hashlib.sha256()
|
|
for file_path in sorted(relevant):
|
|
# Canonical POSIX form, matching tree_digest: identical bytes on
|
|
# Windows and POSIX checkouts of the same tree.
|
|
relative = file_path.relative_to(root).as_posix()
|
|
digest.update(relative.encode())
|
|
digest.update(b"\0")
|
|
digest.update(relevant[file_path])
|
|
return digest.hexdigest()
|
|
|
|
|
|
def prospective_prepared_digest(path: str | Path) -> str:
|
|
"""Return the prepared-tree digest prepare would produce, without writing.
|
|
|
|
Computes the prospective digest from the in-memory staged contents
|
|
plus unchanged files and the guard file, before promoting anything.
|
|
The tree is left byte-identical (no temp or backup files); patch
|
|
validation failures raise exactly as :func:`prepare` would.
|
|
"""
|
|
root = Path(path).resolve()
|
|
verify_checkout(root)
|
|
guard_bytes = _guard_source_bytes()
|
|
current, originals = _read_staged_texts(root)
|
|
jobs = _build_prepare_jobs(root, current, originals, guard_bytes)
|
|
return _prospective_digest_from_plan(root, jobs, originals)
|
|
|
|
|
|
def prepare(path: str | Path) -> list[str]:
|
|
"""Apply the portable adapter to a pinned GSQ checkout.
|
|
|
|
Two-phase and fail-closed: every expected upstream fragment must match
|
|
exactly the expected count before any file is changed. Each patch is
|
|
classified as pending or fully applied; half-applied trees (a
|
|
replacement present while its old fragment remains, or old and new
|
|
side by side) raise before anything is written. Writes are atomic:
|
|
all new contents are computed in memory, staged to fsync'd temp files
|
|
in the same directory, then promoted with os.replace; any staging or
|
|
promotion failure restores originals (keeping backups on ``OSError``)
|
|
and removes temps, leaving the tree byte-identical for an idempotent
|
|
retry. Interrupts (``KeyboardInterrupt``/``SystemExit``) restore
|
|
originals and remove temps and backups, then re-raise. Permission
|
|
bits are preserved: staged replacements keep the original file's
|
|
mode (new files get ``0o644 & ~umask``), and backups/restores carry
|
|
it too. Re-running over an already-prepared tree changes nothing
|
|
and returns an empty list.
|
|
"""
|
|
root = Path(path).resolve()
|
|
verify_checkout(root)
|
|
guard_bytes = _guard_source_bytes()
|
|
|
|
# Phase 1: read every target and validate complete per-file states in
|
|
# memory. No file is written until all states below are established.
|
|
current, originals = _read_staged_texts(root)
|
|
|
|
# Phase 2: all states validated; atomically apply the in-memory
|
|
# results. Every new content is staged to a temp file in the same
|
|
# directory (fsync), originals are backed up, then all temps are
|
|
# promoted with os.replace. If any staging or promotion step fails,
|
|
# already-promoted files are restored from backups (which are kept),
|
|
# temps are removed, and the tree stays byte-identical to before, so a
|
|
# retry is idempotent.
|
|
jobs = _build_prepare_jobs(root, current, originals, guard_bytes)
|
|
changed = [str(target.relative_to(root)) for target in jobs]
|
|
if jobs:
|
|
_promote_atomically(root, jobs)
|
|
else:
|
|
# Nothing to write, but a stale temp from a killed run must not
|
|
# linger and confuse a later failure inspection.
|
|
_remove_prepare_temps(root)
|
|
|
|
remaining: list[str] = []
|
|
for file_path in _iter_python_files(root):
|
|
if _ENABLED_REMOTE_CODE in file_path.read_text(encoding="utf-8"):
|
|
remaining.append(str(file_path.relative_to(root)))
|
|
if remaining:
|
|
raise RuntimeError(f"remote-code trust remains enabled in: {remaining}")
|
|
return sorted(set(changed))
|
|
|
|
|
|
def _remove_prepare_temps(root: Path) -> None:
|
|
"""Remove leftover staging temp files from a previous attempt."""
|
|
for temp in sorted(root.rglob(f"*{_PREPARE_TMP_SUFFIX}")):
|
|
try:
|
|
if temp.is_file():
|
|
temp.unlink()
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
def _stage_temp(target: Path, data: bytes) -> Path:
|
|
"""Write ``data`` to a unique temp file next to ``target`` (fsync).
|
|
|
|
``mkstemp`` creates the temp with mode ``0600``; the caller chmods it
|
|
to its final mode before promotion, so the promoted file never
|
|
appears with a discarded mode.
|
|
"""
|
|
fd, tmp_name = tempfile.mkstemp(
|
|
dir=str(target.parent),
|
|
prefix=target.name + ".",
|
|
suffix=_PREPARE_TMP_SUFFIX,
|
|
)
|
|
try:
|
|
with os.fdopen(fd, "wb") as handle:
|
|
handle.write(data)
|
|
handle.flush()
|
|
os.fsync(handle.fileno())
|
|
except BaseException:
|
|
try:
|
|
os.unlink(tmp_name)
|
|
except OSError:
|
|
pass
|
|
raise
|
|
return Path(tmp_name)
|
|
|
|
|
|
def _current_umask() -> int:
|
|
"""Return the process umask without changing it."""
|
|
mask = os.umask(0)
|
|
os.umask(mask)
|
|
return mask
|
|
|
|
|
|
def _default_new_file_mode() -> int:
|
|
"""Return the mode for files prepare creates (honoring umask)."""
|
|
return 0o644 & ~_current_umask()
|
|
|
|
|
|
def _record_target_modes(jobs: dict[Path, bytes]) -> dict[Path, int | None]:
|
|
"""Record each promotion target's permission bits, if it exists.
|
|
|
|
Existing files map to their ``stat.S_IMODE`` bits (restored onto the
|
|
staged replacement and onto backups/restores); new files map to
|
|
``None`` (staged with :func:`_default_new_file_mode`). An existing
|
|
file that cannot be stated fails closed instead of promoting with a
|
|
guessed mode.
|
|
"""
|
|
import stat
|
|
|
|
modes: dict[Path, int | None] = {}
|
|
for target in jobs:
|
|
if target.is_file():
|
|
try:
|
|
modes[target] = stat.S_IMODE(target.stat().st_mode)
|
|
except OSError as exc:
|
|
raise RuntimeError(
|
|
f"cannot stat GSQ file {target}: {exc}"
|
|
) from exc
|
|
else:
|
|
modes[target] = None
|
|
return modes
|
|
|
|
|
|
def _chmod_or_raise(path: Path, mode: int, *, what: str) -> None:
|
|
"""Chmod ``path``, failing closed with a RuntimeError on OSError."""
|
|
try:
|
|
os.chmod(path, mode)
|
|
except OSError as exc:
|
|
raise RuntimeError(f"cannot set mode on {what} {path}: {exc}") from exc
|
|
|
|
|
|
def _restore_from_backup(target: Path, backup: Path, mode: int | None) -> None:
|
|
"""Restore ``target`` from ``backup`` via staged replacement.
|
|
|
|
The backup bytes are written to a temp file in the same directory,
|
|
chmodded to the recorded mode, then promoted with ``os.replace`` --
|
|
the original is never written through, so restoring a read-only
|
|
(``0444``) original cannot raise ``PermissionError`` (replacement
|
|
needs only directory write permission). Raises ``OSError`` when the
|
|
read, stage, chmod, or replace fails, so both rollback paths treat
|
|
a mode failure like a byte failure (the backup is kept for
|
|
inspection on ``OSError``).
|
|
"""
|
|
staged = _stage_temp(target, backup.read_bytes())
|
|
try:
|
|
if mode is not None:
|
|
os.chmod(staged, mode)
|
|
os.replace(str(staged), str(target))
|
|
except BaseException:
|
|
try:
|
|
if staged.is_file():
|
|
staged.unlink()
|
|
except OSError:
|
|
pass
|
|
raise
|
|
|
|
|
|
def _promote_atomically(root: Path, jobs: dict[Path, bytes]) -> None:
|
|
"""Stage every new content to a temp file, then promote all at once.
|
|
|
|
Originals are backed up before promotion; on any staging or promotion
|
|
failure, promoted files are restored from backups (kept for
|
|
inspection on ``OSError``), temps are removed, and a RuntimeError is
|
|
raised with the tree byte-identical to before the call. Interrupts
|
|
(``BaseException`` such as ``KeyboardInterrupt``/``SystemExit``) also
|
|
restore originals and remove temps and backups, then re-raise the
|
|
original so a Ctrl-C between promotions leaves the tree byte-identical
|
|
for an idempotent retry. Restores and backups never write through a
|
|
read-only file: both stage a temp in the same directory and promote
|
|
it with ``os.replace``, so a ``0444`` original in a writable
|
|
directory rolls back without ``PermissionError`` and a retained
|
|
read-only backup never obstructs the retry. Permission bits survive
|
|
the round trip:
|
|
staged replacements are chmodded to the original file's mode before
|
|
promotion (new files get ``0o644 & ~umask``), backups carry the
|
|
original mode, and restores re-apply it -- so an ``0755`` checkout
|
|
file never comes back as ``0600``. The prepared-tree digest hashes
|
|
paths and bytes only, so modes never affect it.
|
|
"""
|
|
modes = _record_target_modes(jobs)
|
|
temps: dict[Path, Path] = {}
|
|
try:
|
|
for target, data in jobs.items():
|
|
try:
|
|
temps[target] = _stage_temp(target, data)
|
|
except OSError as exc:
|
|
raise RuntimeError(
|
|
f"cannot stage GSQ file {target}: {exc}"
|
|
) from exc
|
|
except BaseException:
|
|
for temp in temps.values():
|
|
try:
|
|
temp.unlink()
|
|
except OSError:
|
|
pass
|
|
raise
|
|
try:
|
|
for target in jobs:
|
|
final = modes[target]
|
|
if final is None:
|
|
final = _default_new_file_mode()
|
|
_chmod_or_raise(temps[target], final, what="staged GSQ file")
|
|
except BaseException:
|
|
for temp in temps.values():
|
|
try:
|
|
temp.unlink()
|
|
except OSError:
|
|
pass
|
|
raise
|
|
backups: dict[Path, Path] = {}
|
|
try:
|
|
for target in jobs:
|
|
if target.is_file():
|
|
backup = target.with_name(target.name + _PREPARE_BACKUP_SUFFIX)
|
|
# Stage the backup next to it and replace atomically: a
|
|
# retained read-only backup from a previous failed run
|
|
# must never be written through (that PermissionError
|
|
# would obstruct the retry), and a failed replace leaves
|
|
# the previous backup intact, so it is kept, not
|
|
# unlinked, on failure.
|
|
try:
|
|
staged = _stage_temp(backup, target.read_bytes())
|
|
except OSError as exc:
|
|
for temp in temps.values():
|
|
try:
|
|
temp.unlink()
|
|
except OSError:
|
|
pass
|
|
raise RuntimeError(
|
|
f"cannot back up GSQ file {target}: {exc}"
|
|
) from exc
|
|
# Intent is recorded before the replace so an interrupt
|
|
# landing after os.replace succeeds but before
|
|
# bookkeeping still cleans the backup up (mirroring the
|
|
# promotion loop below).
|
|
backups[target] = backup
|
|
try:
|
|
if modes[target] is not None:
|
|
os.chmod(staged, modes[target])
|
|
os.replace(str(staged), str(backup))
|
|
except OSError as exc:
|
|
try:
|
|
if staged.is_file():
|
|
staged.unlink()
|
|
except OSError:
|
|
pass
|
|
for temp in temps.values():
|
|
try:
|
|
temp.unlink()
|
|
except OSError:
|
|
pass
|
|
raise RuntimeError(
|
|
f"cannot back up GSQ file {target}: {exc}"
|
|
) from exc
|
|
except BaseException:
|
|
try:
|
|
if staged.is_file():
|
|
staged.unlink()
|
|
except OSError:
|
|
pass
|
|
raise
|
|
except BaseException as exc:
|
|
if isinstance(exc, Exception):
|
|
raise
|
|
for temp in temps.values():
|
|
try:
|
|
if temp.is_file():
|
|
temp.unlink()
|
|
except OSError:
|
|
pass
|
|
for backup in backups.values():
|
|
try:
|
|
if backup.is_file():
|
|
backup.unlink()
|
|
except OSError:
|
|
pass
|
|
raise
|
|
# Intent is recorded before each replace so an interrupt landing
|
|
# after os.replace succeeds but before bookkeeping still rolls back:
|
|
# rollback restores from backup for every intended entry whose
|
|
# backup exists (tolerating entries whose replace never happened by
|
|
# rewriting identical bytes) and unlinks new files with no backup.
|
|
# A backup is deleted only after its restore succeeded.
|
|
promoted: list[Path] = []
|
|
try:
|
|
for target in jobs:
|
|
promoted.append(target)
|
|
os.replace(str(temps[target]), str(target))
|
|
except BaseException as exc:
|
|
if not isinstance(exc, OSError):
|
|
failed: list[Path] = []
|
|
for target in promoted:
|
|
backup = backups.get(target)
|
|
if backup is not None:
|
|
try:
|
|
if backup.is_file():
|
|
_restore_from_backup(
|
|
target, backup, modes[target]
|
|
)
|
|
# A missing backup means nothing was backed up for
|
|
# this entry; nothing to restore.
|
|
except OSError:
|
|
failed.append(target)
|
|
else:
|
|
try:
|
|
if target.is_file():
|
|
target.unlink()
|
|
except OSError:
|
|
pass
|
|
for temp in temps.values():
|
|
try:
|
|
if temp.is_file():
|
|
temp.unlink()
|
|
except OSError:
|
|
pass
|
|
for target, backup in backups.items():
|
|
if target in failed:
|
|
continue
|
|
try:
|
|
if backup.is_file():
|
|
backup.unlink()
|
|
except OSError:
|
|
pass
|
|
raise
|
|
failures: list[str] = [f"cannot promote GSQ files: {exc}"]
|
|
for target in promoted:
|
|
backup = backups.get(target)
|
|
if backup is not None:
|
|
try:
|
|
if backup.is_file():
|
|
_restore_from_backup(target, backup, modes[target])
|
|
except OSError as restore_exc:
|
|
failures.append(f"cannot restore {target}: {restore_exc}")
|
|
else:
|
|
try:
|
|
if target.is_file():
|
|
target.unlink()
|
|
except OSError as restore_exc:
|
|
failures.append(f"cannot remove {target}: {restore_exc}")
|
|
for temp in temps.values():
|
|
try:
|
|
if temp.is_file():
|
|
temp.unlink()
|
|
except OSError:
|
|
pass
|
|
raise RuntimeError("; ".join(failures)) from exc
|
|
for backup in backups.values():
|
|
try:
|
|
backup.unlink()
|
|
except OSError:
|
|
pass
|