#!/usr/bin/env python3 """Equipo's only device/Keychain mutation boundary is lifetime-guarded commit.""" from pathlib import Path import re root = Path(__file__).resolve().parents[1] sources = root / 'apple/Sources/EquipoKit' account = sources / 'Pairing/DeviceAccount.swift' text = account.read_text() assert 'func commit(' in text, 'DeviceAccounts must expose one guarded commit function' start = text.index('{', text.index('func commit(')) depth = 1 end = start + 1 while depth: depth += (text[end] == '{') - (text[end] == '}') end += 1 commit = text[start:end] outside = text[:start] + text[end:] assert not re.search(r'store\.(?:write|remove)\s*\(', outside), 'persistence outside commit' assert 'try lifetimes.require(lifetime)' in commit, 'commit must reject an ended or foreign lifetime' assert commit.index('try lifetimes.require(lifetime)') < commit.index('store.write('), 'guard must precede persistence' for path in [*sources.rglob('*.swift'), *(root / 'ios').rglob('*.swift'), *(root / 'macos').rglob('*.swift')]: value = path.read_text() if path != account: assert not re.search(r'\b(?:accounts|self\.accounts)\.(?:save|remove)\s*\(', value), f'unguarded account write: {path}' assert not re.search(r'\.write\s*\(', value), f'raw secret-store write: {path}' assert not re.search(r'\b\w*(?:store|Store)\.remove\s*\(', value), f'raw secret-store removal: {path}' if path.name != 'SecretStore.swift': assert not re.search(r'\bSecItem(?:Add|Update|Delete)\s*\(', value), f'raw Keychain write: {path}' assert 'private let store: SecretStore' in text, 'raw store must stay private' assert not re.search(r'func (?:save|remove)\(', text), 'do not add a persistence bypass' print('Sign-in lifetime: device and Keychain writes use the guarded commit')