291 lines
13 KiB
Python
291 lines
13 KiB
Python
#!/usr/bin/env python3
|
|
"""Config-only installer. No process, network, config.yaml or scheduler changes."""
|
|
import argparse
|
|
import base64
|
|
import fcntl
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
PROFILES = ('pmo', 'canina-competitor-worker', 'canina-seo-worker',
|
|
'canina-content-worker', 'canina-vet-review-worker', 'canina-qa-worker')
|
|
ALLOWED = {'editorial-v4/status.json'} | {
|
|
f'profiles/{p}/skills/canina-editorial-v4/{f}'
|
|
for p in PROFILES for f in ('SKILL.md', 'references/policy.md', 'references/role.json')}
|
|
|
|
|
|
def digest(data):
|
|
return hashlib.sha256(data).hexdigest()
|
|
|
|
|
|
def safe(home, relative):
|
|
path = home / relative
|
|
for part in (path, *path.parents):
|
|
if part.is_symlink():
|
|
raise ValueError(f'symlink refused: {part}')
|
|
if relative not in ALLOWED and relative not in (
|
|
'editorial-v4/.installer/transaction.json', 'editorial-v4/.installer/lock'):
|
|
raise ValueError('path not allowlisted')
|
|
if path.exists() and not path.is_file():
|
|
raise ValueError(f'not a regular file: {path}')
|
|
return path
|
|
|
|
|
|
def atomic(path, data):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
fd, temp = tempfile.mkstemp(prefix='.editorial-', dir=path.parent)
|
|
try:
|
|
with os.fdopen(fd, 'wb') as stream:
|
|
stream.write(data)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
os.chmod(temp, 0o644)
|
|
os.replace(temp, path)
|
|
finally:
|
|
if os.path.exists(temp):
|
|
os.unlink(temp)
|
|
|
|
|
|
def bundle():
|
|
manifest = json.loads((ROOT / 'bundle.json').read_text())
|
|
if set(manifest['files']) != ALLOWED:
|
|
raise ValueError('bundle allowlist mismatch')
|
|
result = {}
|
|
for relative, expected in manifest['files'].items():
|
|
path = ROOT / 'payload' / relative
|
|
if path.is_symlink():
|
|
raise ValueError('source symlink refused')
|
|
data = path.read_bytes()
|
|
if digest(data) != expected:
|
|
raise ValueError(f'bundle digest mismatch: {relative}')
|
|
result[relative] = data
|
|
return result
|
|
|
|
|
|
def recover(journal, paths):
|
|
if not journal.exists():
|
|
return
|
|
try:
|
|
state = json.loads(journal.read_text())
|
|
except Exception:
|
|
raise ValueError('invalid journal json')
|
|
if state.get('phase') != 'applying':
|
|
return
|
|
if (set(state.get('before', {})) != ALLOWED or
|
|
set(state.get('installed', {})) != ALLOWED):
|
|
raise ValueError('invalid in-flight journal allowlist')
|
|
if state.get('previous_installed') is not None and set(state['previous_installed']) != ALLOWED:
|
|
raise ValueError('invalid in-flight journal previous_installed allowlist')
|
|
if state.get('previous_before') is not None and set(state['previous_before']) != ALLOWED:
|
|
raise ValueError('invalid in-flight journal previous_before allowlist')
|
|
for r, p in paths.items():
|
|
curr = p.read_bytes() if p.exists() else None
|
|
curr_d = digest(curr) if curr is not None else None
|
|
if curr_d == state['installed'][r]:
|
|
continue
|
|
if state.get('previous_installed') is not None:
|
|
if curr_d == state['previous_installed'][r]:
|
|
continue
|
|
else:
|
|
old = state['before'][r]
|
|
old_data = base64.b64decode(old['data']) if old else None
|
|
if curr == old_data:
|
|
continue
|
|
raise ValueError(f'interrupted transaction conflict: {r}')
|
|
restore_target = state.get('previous_before') or state['before']
|
|
restore(paths, restore_target)
|
|
if state.get('previous_journal'):
|
|
atomic(journal, base64.b64decode(state['previous_journal']))
|
|
elif state.get('previous_installed'):
|
|
atomic(journal, json.dumps({
|
|
'phase': 'installed',
|
|
'before': state['before'],
|
|
'installed': state['previous_installed']
|
|
}, sort_keys=True).encode())
|
|
else:
|
|
journal.unlink()
|
|
|
|
|
|
def execute(action, home):
|
|
if not home.is_absolute() or not home.is_dir():
|
|
raise ValueError('--target-home must be an existing absolute directory')
|
|
files = bundle()
|
|
paths = {r: safe(home, r) for r in files}
|
|
journal = safe(home, 'editorial-v4/.installer/transaction.json')
|
|
if action == 'verify':
|
|
bad = [r for r, p in paths.items() if not p.exists() or p.read_bytes() != files[r]]
|
|
if bad:
|
|
raise ValueError('verification mismatch: ' + ', '.join(bad))
|
|
if journal.exists():
|
|
jstate = json.loads(journal.read_text())
|
|
if jstate.get('phase', 'installed') != 'installed':
|
|
raise ValueError('verification refused: in-flight transaction requires recovery')
|
|
return {'status': 'configured_only', 'files': len(files), 'workflow_verified': False}
|
|
lock = safe(home, 'editorial-v4/.installer/lock')
|
|
lock.parent.mkdir(parents=True, exist_ok=True)
|
|
with lock.open('a') as stream:
|
|
fcntl.flock(stream, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
recover(journal, paths)
|
|
if action == 'rollback':
|
|
if not journal.exists():
|
|
return {'status': 'nothing_to_rollback'}
|
|
state = json.loads(journal.read_text())
|
|
if set(state['before']) != ALLOWED or set(state['installed']) != ALLOWED:
|
|
raise ValueError('invalid rollback allowlist')
|
|
# Refuse external edits rather than destroying another writer's work.
|
|
for r, p in paths.items():
|
|
current = p.read_bytes() if p.exists() else None
|
|
before = state['before'][r]
|
|
old = base64.b64decode(before['data']) if before else None
|
|
if current != old and (current is None or digest(current) != state['installed'][r]):
|
|
raise ValueError(f'rollback conflict: {r}')
|
|
restore(paths, state['before'])
|
|
journal.unlink()
|
|
return {'status': 'rolled_back'}
|
|
changes = [r for r, p in paths.items() if not p.exists() or p.read_bytes() != files[r]]
|
|
if not changes:
|
|
return {'status': 'unchanged', 'files': len(files)}
|
|
previous_journal = journal.read_bytes() if journal.exists() else None
|
|
previous_state = None
|
|
if previous_journal is not None:
|
|
previous_state = json.loads(previous_journal)
|
|
if (set(previous_state) not in ({'before', 'installed'}, {'phase', 'before', 'installed'}) or
|
|
set(previous_state['before']) != ALLOWED or
|
|
set(previous_state['installed']) != ALLOWED):
|
|
raise ValueError('invalid upgrade journal allowlist')
|
|
if previous_state.get('phase', 'installed') != 'installed':
|
|
raise ValueError('invalid upgrade journal phase')
|
|
for r, p in paths.items():
|
|
old = previous_state['before'][r]
|
|
if old is not None:
|
|
if (set(old) != {'data', 'mode'} or type(old['mode']) is not int or
|
|
not 0 <= old['mode'] <= 0o777):
|
|
raise ValueError('invalid upgrade rollback entry')
|
|
base64.b64decode(old['data'], validate=True)
|
|
# Only a completely installed, verified previous transaction may
|
|
# advance. Never recover partial writes or overwrite live drift.
|
|
if not p.exists() or digest(p.read_bytes()) != previous_state['installed'][r]:
|
|
raise ValueError(f'existing transaction conflict: {r}')
|
|
current_snapshot = {r: {'data': base64.b64encode(p.read_bytes()).decode(),
|
|
'mode': p.stat().st_mode & 0o777} if p.exists() else None
|
|
for r, p in paths.items()}
|
|
original_before = previous_state['before'] if previous_state else current_snapshot
|
|
incoming_installed = {r: digest(v) for r, v in files.items()}
|
|
in_flight_state = {
|
|
'phase': 'applying',
|
|
'before': original_before,
|
|
'installed': incoming_installed,
|
|
'previous_installed': previous_state['installed'] if previous_state else None,
|
|
'previous_before': current_snapshot,
|
|
'previous_journal': base64.b64encode(previous_journal).decode() if previous_journal else None,
|
|
}
|
|
atomic(journal, json.dumps(in_flight_state, sort_keys=True).encode())
|
|
try:
|
|
for r in changes:
|
|
atomic(paths[r], files[r])
|
|
except BaseException:
|
|
try:
|
|
restore(paths, current_snapshot)
|
|
if previous_journal is None:
|
|
journal.unlink()
|
|
else:
|
|
atomic(journal, previous_journal)
|
|
except BaseException:
|
|
pass
|
|
raise
|
|
committed_state = {
|
|
'phase': 'installed',
|
|
'before': original_before,
|
|
'installed': incoming_installed,
|
|
}
|
|
atomic(journal, json.dumps(committed_state, sort_keys=True).encode())
|
|
return {'status': 'installed_config_only', 'files': len(files)}
|
|
|
|
|
|
def restore(paths, before):
|
|
for r, p in paths.items():
|
|
old = before[r]
|
|
if old is None:
|
|
p.unlink(missing_ok=True)
|
|
else:
|
|
atomic(p, base64.b64decode(old['data']))
|
|
os.chmod(p, old['mode'])
|
|
|
|
|
|
def configure_models(home):
|
|
"""CI-only narrow source policy; never serialize provider secrets or .env.
|
|
|
|
Surgical scalar edits preserve every unrelated byte (including auth/gates).
|
|
Preflight the whole registry and every destination before the first write.
|
|
"""
|
|
import re
|
|
import stat
|
|
policy = json.loads((ROOT/'model-policy.json').read_text())
|
|
profiles = policy['profiles']
|
|
if policy['model'] != 'Canina-content' or len(set(profiles)) != 29:
|
|
raise ValueError('Invalid canonical model policy')
|
|
def checked(relative):
|
|
path = home/relative
|
|
if not home.is_absolute() or any(p.is_symlink() for p in (path, *path.parents)):
|
|
raise ValueError('Unsafe model destination')
|
|
st = path.stat()
|
|
if not stat.S_ISREG(st.st_mode) or st.st_nlink != 1 or st.st_uid != os.getuid() or st.st_size > 65536:
|
|
raise ValueError('Unsafe model file')
|
|
return path
|
|
registry = checked(policy['registry']).read_text()
|
|
registered = re.findall(r'\bprofile:\s*([a-z0-9-]+)', registry)
|
|
if len(registered) != len(set(registered)) or set(registered) | set(policy['additionalProfiles']) != set(profiles):
|
|
raise ValueError('Canonical registry mismatch')
|
|
pending = []
|
|
for profile in profiles:
|
|
if not re.fullmatch(r'(?:canina-[a-z0-9-]+-worker|pmo)', profile):
|
|
raise ValueError('Noncanonical profile')
|
|
path = checked(f'profiles/{profile}/config.yaml')
|
|
old = path.read_text()
|
|
updated = old
|
|
for section, key, value in [('model','default','Canina-content'), ('agent','max_turns','0'), ('agent','run_budget_seconds','0')]:
|
|
block = re.search(r'(?m)^'+section+r':[^\n]*\n(?:[ \t]+[^\n]*\n|\n)*', updated)
|
|
if block is None:
|
|
if section == 'model': raise ValueError('Model mapping required')
|
|
updated += '\nagent:\n max_turns: 0\n run_budget_seconds: 0\n'
|
|
continue
|
|
content = block.group()
|
|
pattern = r'(?m)^ '+key+r':[^\n]*$'
|
|
count = len(re.findall(pattern, content))
|
|
if count > 1: raise ValueError('Ambiguous model setting')
|
|
replacement = ' '+key+': '+value
|
|
content = re.sub(pattern, replacement, content) if count else content+replacement+'\n'
|
|
updated = updated[:block.start()]+content+updated[block.end():]
|
|
pending.append((path, old, updated, path.stat().st_mode & 0o777))
|
|
for path, old, updated, mode in pending:
|
|
if path.read_text() != old: raise ValueError('Concurrent model config edit')
|
|
if old != updated:
|
|
# Private same-directory atomic replacement; no secret-bearing journal.
|
|
fd, temporary = tempfile.mkstemp(prefix='.model-policy-', dir=path.parent)
|
|
try:
|
|
with os.fdopen(fd, 'w') as stream:
|
|
stream.write(updated); stream.flush(); os.fsync(stream.fileno())
|
|
os.chmod(temporary, mode)
|
|
os.replace(temporary, path)
|
|
finally:
|
|
if os.path.exists(temporary): os.unlink(temporary)
|
|
return len(pending)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('action', choices=['install', 'verify', 'rollback'])
|
|
parser.add_argument('--target-home', type=Path, required=True)
|
|
args = parser.parse_args()
|
|
try:
|
|
print(json.dumps(execute(args.action, args.target_home), sort_keys=True))
|
|
except (OSError, ValueError, KeyError) as error:
|
|
parser.exit(1, f'{error}\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|