"""Stabile IDs (PLAN.md §3.6, §10.1). - UUIDs für alle Show-Objekte. - Persistente node_id: einmal erzeugt, dauerhaft gespeichert; unabhängig von IP-Adresse und Hostname (§6.3). """ from __future__ import annotations import os import uuid from pathlib import Path def new_uuid() -> str: """Stabile UUID für Show-Objekte (Layer, Effekte, Outputs, ...).""" return str(uuid.uuid4()) def _machine_independent_seed() -> bytes: """Einstreu ohne IP/Hostname: OS-Urandom hat Priorität (§6.3).""" return os.urandom(16) def new_node_id() -> str: """Erzeugt eine neue, netzwerkunabhängige node_id (UUIDv4).""" return str(uuid.UUID(bytes=_machine_independent_seed(), version=4)) def persistent_node_id(identity_file: Path) -> str: """Lädt die node_id aus identity_file oder erzeugt sie genau einmal. IP-Wechsel ändern die node_id nicht; doppelte Vergabe über die Datei wird durch exklusives Erzeugen (O_EXCL) verhindert. """ identity_file = Path(identity_file) if identity_file.exists(): existing = identity_file.read_text(encoding="utf-8").strip() if existing: uuid.UUID(existing) # Validierung: muss UUID sein return existing identity_file.parent.mkdir(parents=True, exist_ok=True) candidate = new_node_id() try: fd = os.open(identity_file, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600) with os.fdopen(fd, "w", encoding="utf-8") as fh: fh.write(candidate) return candidate except FileExistsError: existing = identity_file.read_text(encoding="utf-8").strip() if not existing: raise return existing