367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
64 lines
3.0 KiB
Python
64 lines
3.0 KiB
Python
"""Preserve a closed generation's diagnostics before a fresh, manual-only Login.
|
|
|
|
Native retirement owns all transport/account/state changes. This coordinator may
|
|
request it and validate its receipt; it never authenticates, types, or retries.
|
|
"""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import stat
|
|
import time
|
|
|
|
|
|
def uint(value,maximum=0xffffffff):
|
|
return type(value)is int and 0<=value<=maximum
|
|
|
|
|
|
def closed_receipt(pid,pipe,account):
|
|
if not uint(pid,0x7fffffff)or not pid or not isinstance(pipe,dict)or pipe.get('closed')!=1:
|
|
raise ValueError('An actually closed owned generation is required')
|
|
result={'kind':'closed-native-account-generation-v1','pid':pid,'observed_ns':time.time_ns(),
|
|
'automatic_authentication_attempted':False,'credential_input_reused':False}
|
|
fields={}
|
|
for label,source,keys in [('pipe',pipe,('error','closed','world_auth')),
|
|
('world',pipe.get('world',{}),('login_sent','self_create')),
|
|
('account',account,('state','error','auth_result','discovery_result','transport_result'))]:
|
|
if not isinstance(source,dict):raise ValueError('Malformed closed generation metadata')
|
|
row={}
|
|
for key in keys:
|
|
value=source.get(key)
|
|
if type(value)is bool:value=int(value)
|
|
if type(value)is not int or not -0x80000000<=value<=0xffffffff:
|
|
raise ValueError('Malformed closed generation numeric metadata')
|
|
row[key]=value
|
|
fields[label]=row
|
|
result['previous']=fields
|
|
return result
|
|
|
|
|
|
def save_closed_receipt(root,receipt):
|
|
root=Path(root);directory=root/'runtime'
|
|
before=directory.lstat()
|
|
if not stat.S_ISDIR(before.st_mode)or before.st_uid!=os.getuid()or before.st_mode&0o022 or directory.resolve()!=directory:
|
|
raise ValueError('Owned nonsymlink runtime required')
|
|
name='closed-account-generation-'+str(receipt['observed_ns'])+'.json'
|
|
target=directory/name
|
|
fd=os.open(target,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
|
with os.fdopen(fd,'w')as out:
|
|
json.dump(receipt,out,separators=(',',':'));out.write('\n');out.flush();os.fsync(out.fileno())
|
|
return target
|
|
|
|
|
|
def require_retirement_ack(request,ack):
|
|
"""Validate native-owned completion; no readiness follows from this alone."""
|
|
expected=request.get('expected_credentials_generation')
|
|
if not uint(expected,63)or not expected:
|
|
raise ValueError('Bounded actual previous generation required')
|
|
if (any(type(ack.get(key))is not int for key in ('state','result','completed_credentials_generation','next_credentials_generation','owner'))or
|
|
ack.get('state')!=2 or ack.get('result')!=0 or
|
|
ack.get('completed_credentials_generation')!=expected or
|
|
ack.get('next_credentials_generation')!=expected+1 or
|
|
ack.get('owner')!=request.get('owner')or not request.get('owner')):
|
|
raise ValueError('Native retirement not verified for this exact generation')
|
|
return expected+1
|