Separate Greif scoring from independently gated Drive archival

This commit is contained in:
Codex committed 2026-11-19 21:43:44 +08:00
1 parent dbb03c7dfa
commit 18e0e7298f
8 files changed
+559 -150

No files matched your search

+58 -12
View File
@@ -1,4 +1,4 @@
"""Drive-backed Greif safekeeping and one guarded custody read per test case."""
"""Local single-read Greif scoring and independently verified Drive safekeeping."""
from collections import defaultdict
import hashlib
@@ -16,7 +16,7 @@ from phase2_h2_data import cached
from s2_custody_common import prediction_inputs
from s2_custody_statistics import part_metrics
import track_b_gino_greif_entry as entry
from track_b_gino_greif_drive_acl import prove as prove_drive_acl
from track_b_gino_greif_drive_acl import prove as prove_drive_acl, prove_folder
from track_b_gino_greif_prepare import PILOT, QUERY, PINS
from track_b_gino_shapenet_entry import BASE, checked, read, save, sha
@@ -70,7 +70,7 @@ def pack(c):
return 0
def verify_drive(auth):
def local_bundle():
root=entry.ROOT
bundle=read(root/'drive-upload-index.json')
path=checked(bundle['path'],bundle['sha256'])
@@ -80,6 +80,12 @@ def verify_drive(auth):
or bundle['model_index_sha256']!=sha(root/'models-frozen.json')
or bundle['prediction_index_sha256']!=sha(root/'predictions-frozen.json')):
raise ValueError('Local archive no longer matches the fixed models and forecasts')
return bundle
def verify_drive(auth):
root=entry.ROOT
bundle=local_bundle()
receipt=read(root/'drive-readback.json')
# Transfer bookkeeping binds the file ID to the expected archive. The
# actual ACL and remote file identity come from the live provider.
@@ -91,6 +97,50 @@ def verify_drive(auth):
return prove_drive_acl(receipt.get('file_id'),auth['private_drive_folder_id'],bundle['sha256'],bundle['bytes'])
def record_drive_hold(path, reason, scope):
if not path.exists():
save(path,dict(status='HOLD_FURTHER_DRIVE_UPLOADS',stage=scope,reason=str(reason),
unix=time.time(),permissions_changed=False,files_deleted=False))
def drive_preflight(c, auth):
root=entry.ROOT
if any((root/n).exists() for n in ('drive-upload-hold.json','drive-readback.json','drive-upload-verified.json')):
raise PermissionError('Existing Drive attempt or HOLD requires disposition before any further upload')
try:
frozen(c)
bundle=local_bundle()
evidence=prove_folder(auth.get('private_drive_folder_id'))
except (OSError, ValueError, KeyError) as exc:
record_drive_hold(root/'drive-preflight-hold.json',exc,'BEFORE_UPLOAD')
save(root/'drive-preflight.json',dict(status='HOLD_FURTHER_DRIVE_UPLOADS',unix=time.time()))
return 1
save(root/'drive-preflight.json',dict(status='PROJECT_FOLDER_ACL_VERIFIED',unix=time.time(),
sha256=bundle['sha256'],contract_sha256=bundle['contract_sha256'],drive=evidence,
does_not_authorize_upload=True))
return 0
def drive_readback(c, auth):
root=entry.ROOT
if any((root/n).exists() for n in ('drive-upload-hold.json','drive-upload-verified.json')):
raise PermissionError('Existing Drive disposition is preserved')
try:
frozen(c)
bundle=local_bundle()
preflight=read(root/'drive-preflight.json')
if (preflight['status'],preflight['sha256'],preflight['contract_sha256'],preflight['drive']['parent_id']) != (
'PROJECT_FOLDER_ACL_VERIFIED',bundle['sha256'],bundle['contract_sha256'],auth.get('private_drive_folder_id')):
raise PermissionError('No matching pre-upload project-folder ACL evidence')
evidence=verify_drive(auth)
except (OSError, ValueError, KeyError) as exc:
record_drive_hold(root/'drive-upload-hold.json',exc,'AFTER_UPLOAD')
return 1
save(root/'drive-upload-verified.json',dict(status='PRIVATE_DRIVE_ARCHIVE_VERIFIED',unix=time.time(),
**evidence, preflight_sha256=sha(root/'drive-preflight.json')))
return 0
def bootstrap(rows, metric, fixed_support=None):
values=defaultdict(list)
unavailable=[]
@@ -172,7 +222,7 @@ def one_case(pilot, meta, row, forecasts, reference, log):
def score(c):
auth,_=entry.authorize(c)
entry.authorize(c)
if os.environ.get('CUDA_VISIBLE_DEVICES')!='':
raise PermissionError('Custody scoring must run with CUDA hidden')
ceiling=resource.getrlimit(resource.RLIMIT_CPU)[1]
@@ -181,7 +231,6 @@ def score(c):
limit=min(limit,ceiling)
resource.setrlimit(resource.RLIMIT_CPU,(limit,limit))
_,predictions=frozen(c)
drive=verify_drive(auth)
compare=read(checked(c['comparison_contract'],c['comparison_contract_sha256']))
rosters={(r['fold'],r['case']) for r in compare['roster']}
new={(r['fold'],r['seed'],r['case']):r for r in predictions}
@@ -195,16 +244,13 @@ def score(c):
query={r['case']:r for r in read(checked(QUERY,PINS[str(QUERY)]))['rows'] if r['included']}
out=entry.ROOT/'score'
out.mkdir(exist_ok=False)
evidence=out/'drive-acl-evidence.json'
save(evidence,dict(drive,observed_before_target_reads=True))
save(out/'custody-start.json',dict(unix=time.time(),drive_file_id=drive['file_id'],
drive_acl_evidence_sha256=sha(evidence), manifest_sha256=sha(entry.ROOT/'predictions-frozen.json'),
before_test_target_reads=True))
save(out/'custody-start.json',dict(unix=time.time(),
model_manifest_sha256=sha(entry.ROOT/'models-frozen.json'),
manifest_sha256=sha(entry.ROOT/'predictions-frozen.json'),before_test_target_reads=True))
results=[]
with (out/'heldout-target-access.jsonl').open('x') as log:
for row in compare['roster']:
results.append(one_case(pilot,query[row['case']],row,new,reference,log))
save(out/'result.json',dict(status='COMPLETE',summary=summary(results,compare),rows=results,
unique_test_target_reads=len(results),no_new_decode=True,drive_file_id=drive['file_id'],
drive_acl_evidence_sha256=sha(evidence)))
unique_test_target_reads=len(results),no_new_decode=True))
return 0
+22 -9
View File
@@ -100,14 +100,9 @@ def _member_evidence(item, token):
return matches
def prove(file_id, folder_id, digest, size):
if (folder_id != PROJECT_FOLDER or not isinstance(file_id, str) or not ID.fullmatch(file_id)
or file_id in (PROJECT_FOLDER, PROJECT_MY_DRIVE_ROOT, 'root')):
raise PermissionError('Archive must be in the fixed project folder')
def _prove_chain(lookup, archive=None):
token = _credential()
query = dict(fields=FILE_FIELDS, supportsAllDrives='true')
lookup = ((file_id, file_id, [PROJECT_FOLDER]), (PROJECT_FOLDER, PROJECT_FOLDER, [PROJECT_MY_DRIVE_ROOT]),
('root', PROJECT_MY_DRIVE_ROOT, []))
observations = []
for locator, expected_id, parents in lookup:
item = _get_json(locator, query, token)
@@ -115,9 +110,9 @@ def prove(file_id, folder_id, digest, size):
or item.get('ownedByMe') is not True or item.get('trashed') is not False
or not isinstance(item.get('version'), str) or not item['version'].isdecimal()):
raise PermissionError('Drive archive or inheritance chain does not match the fixed project')
if locator == file_id:
if archive and locator == archive['file_id']:
if (item.get('mimeType') not in ('application/zip','application/x-zip','application/x-zip-compressed')
or (item.get('sha256Checksum'),item.get('size')) != (digest,str(size))):
or (item.get('sha256Checksum'),item.get('size')) != (archive['sha256'],str(archive['bytes']))):
raise PermissionError('Authoritative Drive archive bytes do not match the frozen bundle')
elif item.get('mimeType') != FOLDER_TYPE:
raise PermissionError('Drive inheritance parent is not a folder')
@@ -127,6 +122,24 @@ def prove(file_id, folder_id, digest, size):
if _get_json(locator, query, token) != item:
raise PermissionError('Drive metadata changed during ACL verification')
return dict(source='live Google Drive v3 files.get and exhaustive permissions.list',
file_id=file_id,parent_id=folder_id,bytes=size,sha256=digest,
chain=[dict(file_id=item['id'],version=item['version'],parents=item.get('parents', []),
effective_permissions=permissions) for _,item,permissions in observations])
def _fixed_parent_chain(folder_id):
if folder_id != PROJECT_FOLDER:
raise PermissionError('Drive destination must be the fixed project folder')
return ((PROJECT_FOLDER, PROJECT_FOLDER, [PROJECT_MY_DRIVE_ROOT]), ('root', PROJECT_MY_DRIVE_ROOT, []))
def prove_folder(folder_id):
return dict(_prove_chain(_fixed_parent_chain(folder_id)), parent_id=folder_id)
def prove(file_id, folder_id, digest, size):
parents = _fixed_parent_chain(folder_id)
if (not isinstance(file_id, str) or not ID.fullmatch(file_id)
or file_id in (PROJECT_FOLDER, PROJECT_MY_DRIVE_ROOT, 'root')):
raise PermissionError('Archive must be in the fixed project folder')
archive = dict(file_id=file_id,parent_id=folder_id,bytes=size,sha256=digest)
return dict(_prove_chain(((file_id, file_id, [PROJECT_FOLDER]),) + parents, archive), **archive)
+10 -6
View File
@@ -73,9 +73,10 @@ def expected_contract():
per_prediction=PRED_SECONDS,train_setup_reserve=FIT_SETUP_RESERVE,score_cpu_seconds=3600),
run_order=['two first epochs as part of seed0 fits','reviewed cost gate','complete all four fits',
'freeze models','form four target-free prediction sets','freeze predictions',
'verified private Drive transfer','one controlled custody pass'],
'one controlled local custody pass; independent optional verified private Drive transfer'],
drive={'purpose':'immutable model and prediction bundle','git':'small SHA indexes and receipts only',
'acl_revision':'h-17e62811 live exhaustive Drive ACL and fixed owner-only inheritance chain'},
'acl_revision':'h-c3d10e10 pre-upload parent ACL and post-upload file ACL',
'required_for_local_score':False},
no_retry=True)
@@ -92,8 +93,7 @@ def authorize(c):
if (a.get('decision'),a.get('mode'),a.get('contract_sha256'),a.get('review_sha256')) != (
'APPROVED',c['mode'],sha(ROOT/'contract.json'),sha(ROOT/'review.json')):
raise PermissionError('Exact review plus separate stage authorization required')
if (not a.get('yanqi_direct_reply_id') or not a.get('claude_shape_verdict_id')
or not a.get('private_drive_folder_id')):
if not a.get('yanqi_direct_reply_id') or not a.get('claude_shape_verdict_id'):
raise PermissionError('Direct user launch and ShapeNet verdict are required')
verdict=read(checked(a['shape_verdict']['path'],a['shape_verdict']['sha256']))
if (verdict['contract_sha256']!=prep.reference.sha(prep.reference.ROOT/'contract.json')
@@ -247,13 +247,17 @@ def run(c, auth, phase_booked):
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('action',choices=('run','pack','score'))
parser.add_argument('action',choices=('run','pack','score','drive-preflight','drive-readback'))
args=parser.parse_args()
c=contract()
auth,booked=authorize(c)
if args.action=='run':
return run(c,auth,booked)
from track_b_gino_greif_custody import pack, score
from track_b_gino_greif_custody import drive_preflight, drive_readback, pack, score
if args.action in ('drive-preflight','drive-readback'):
with (ROOT/'drive-custody.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
return drive_preflight(c,auth) if args.action=='drive-preflight' else drive_readback(c,auth)
return pack(c) if args.action=='pack' else score(c)