added
This commit is contained in:
1 parent
4eaec52904
commit
df7571392e
50 files changed
+3390
-258
No files matched your search
@@ -0,0 +1,141 @@
|
||||
"""Explicit local content approval, final-package integrity, and draft preservation.
|
||||
|
||||
This is workflow evidence, not authenticated signatures, source fact-checking,
|
||||
third-party media licensing, or publication authority.
|
||||
"""
|
||||
from pathlib import Path
|
||||
import copy
|
||||
import shutil
|
||||
import tempfile
|
||||
from datetime import datetime
|
||||
|
||||
import media_builder as core
|
||||
import folder_builder
|
||||
|
||||
|
||||
def semantic_plan(plan):
|
||||
return {key:value for key,value in plan.items() if key != 'review'}
|
||||
|
||||
|
||||
def verify(output, check_sources=True):
|
||||
output=Path(output).resolve()
|
||||
receipt=core.load(output/'build-receipt.json')
|
||||
approval=core.load(output/'approval.json')
|
||||
core.require(receipt['status']=='approved' and receipt['lifecycle']=='final', 'Not a final approved package')
|
||||
core.require(approval['status']=='approved' and approval['reviewer'].strip() and approval['approval_evidence'].strip(), 'Explicit approval record missing')
|
||||
core.require(approval['input_sha256']==receipt['input_sha256'], 'Approval/receipt fingerprint mismatch')
|
||||
actual={p.relative_to(output).as_posix():folder_builder.sha_file(p) for p in output.rglob('*')
|
||||
if p.is_file() and p != output/'build-receipt.json'}
|
||||
core.require(actual==receipt['files'], 'Final output differs from receipt')
|
||||
for name in ('transcript.json','chunks.json','qa.json','ai-index.json'):
|
||||
value=core.load(output/name)
|
||||
core.validate(value, 'folder-export')
|
||||
core.require(value['status']=='approved','Artifact not approved')
|
||||
if check_sources:
|
||||
for record in receipt['source_files'].values():
|
||||
path=core.relative_file(output.parent,record['path'])
|
||||
core.require(folder_builder.sha_file(path)==record['sha256'],'Original source changed')
|
||||
intake=core.relative_file(output.parent,approval['inputs']['intake'])
|
||||
plan=core.relative_file(output.parent,approval['inputs']['plan'])
|
||||
core.require(folder_builder.sha_file(intake)==approval['intake_sha256'],'Intake changed since approval')
|
||||
plan_value=core.load(plan)
|
||||
core.require(core.digest(core.encoded(semantic_plan(plan_value)))==approval['plan_content_sha256'],
|
||||
'Plan content changed since approval')
|
||||
core.require(plan_value['review']['status']=='approved','Source plan not approved')
|
||||
core.require(plan_value['review']['reviewer']==approval['reviewer'] and plan_value['review']['reviewed_at']==approval['approved_at'], 'Approval identity/time differs from source plan')
|
||||
core.require(plan_value['review']['input_sha256']==receipt['input_sha256'],'Plan approval fingerprint mismatch')
|
||||
media=core.load(output/'media.json')
|
||||
core.require(media['human_approval'] and not media['publication_ready'], 'Approval scope mismatch')
|
||||
return {'valid':True,'status':'approved','lifecycle':'final','source_files_checked':check_sources,
|
||||
'source_files_unchanged':True if check_sources else None,
|
||||
'reviewer':approval['reviewer'],**receipt['counts']}
|
||||
|
||||
|
||||
def finalize(source,output,intake_path,plan_path,reviewer,approved_at,evidence):
|
||||
source,output=Path(source).resolve(),Path(output).resolve()
|
||||
intake_path,plan_path=Path(intake_path).resolve(),Path(plan_path).resolve()
|
||||
core.require(source.parent==output.parent and output!=source,'Final must be a sibling of the reviewed draft')
|
||||
core.require(not output.exists(),'Final output exists; do not overwrite it')
|
||||
core.require(intake_path.parent==source.parent and plan_path.parent==source.parent,'Inputs must be beside the media sources')
|
||||
core.require(reviewer.strip() and evidence.strip(),'Explicit reviewer and approval evidence required')
|
||||
moment=datetime.fromisoformat(approved_at.replace('Z','+00:00'))
|
||||
core.require(moment.tzinfo is not None,'Approval timestamp must include a timezone')
|
||||
folder_builder.verify(source)
|
||||
draft_receipt=core.load(source/'build-receipt.json')
|
||||
plan=core.load(plan_path)
|
||||
core.require(plan['review']['status']=='draft','Expected the reviewed draft source plan')
|
||||
expected=core.digest(core.encoded({'builder_version':draft_receipt['builder_version'], 'intake':core.load(intake_path), 'plan':plan, 'source_files':draft_receipt['source_files']}))
|
||||
core.require(expected==draft_receipt['input_sha256'], 'Draft inputs changed since build; rebuild and review before approval')
|
||||
old_review=copy.deepcopy(plan['review'])
|
||||
approval={
|
||||
'schema_version':'1.0','status':'approved','lifecycle':'final',
|
||||
'reviewer':reviewer,'approved_at':approved_at,'approval_evidence':evidence,
|
||||
'scope':'Owner approval of the generated structured content and metadata as final, with documented limitations retained.',
|
||||
'publication_ready':False,'input_sha256':draft_receipt['input_sha256'],
|
||||
'inputs':{'intake':intake_path.name,'plan':plan_path.name},
|
||||
'intake_sha256':folder_builder.sha_file(intake_path),
|
||||
'plan_content_sha256':core.digest(core.encoded(semantic_plan(plan))),
|
||||
'reviewed_draft_directory':source.name,'reviewed_draft_receipt':draft_receipt,
|
||||
'notes':['The original reviewed draft remains available as history.',
|
||||
'The approval timestamp records when this run recorded the user approval.',
|
||||
'Approval does not claim independent fact-checking, audio/slide verification, a recording license, or deployment.',
|
||||
'Unknown metadata and original source wording are retained.']}
|
||||
pending=Path(tempfile.mkdtemp(prefix='.final-media-',dir=output.parent))
|
||||
plan_updated=False
|
||||
try:
|
||||
for path in source.iterdir():
|
||||
core.require(path.is_file(),'Unexpected draft subdirectory')
|
||||
shutil.copyfile(path,pending/path.name)
|
||||
for path in pending.glob('*.json'):
|
||||
if path.name=='build-receipt.json':continue
|
||||
value=core.load(path)
|
||||
if value.get('profile')=='local-review':value['status']='approved'
|
||||
if path.name=='media.json':
|
||||
value['human_approval']=True
|
||||
if value['metadata']['description']:
|
||||
value['metadata']['description']=value['metadata']['description'].replace(
|
||||
'This local draft preserves','This approved structured package preserves')
|
||||
core.write(path,value)
|
||||
page=(pending/'README.md').read_text(encoding='utf-8')
|
||||
page=page.replace('**Draft for human review.** Built from the supplied recording, TXT, and VTT.',
|
||||
f'**Final — approved by {reviewer} on {moment.date().isoformat()}.** '
|
||||
'Built from the supplied recording, TXT, and VTT; documented limitations remain in the review record.')
|
||||
page=page.replace('This local draft preserves','This approved structured package preserves')
|
||||
page=page.replace('Draft credit','Approved credit')
|
||||
core.write(pending/'README.md',page)
|
||||
review=(pending/'review.md').read_text(encoding='utf-8')
|
||||
review=review.replace('Status: **draft — not human approved or publication ready**.',
|
||||
f'Status: **final and approved**. Approved by {reviewer} on {moment.date().isoformat()}.\n\n'
|
||||
f'Approval evidence: “{evidence}”\n\n'
|
||||
'This records approval of the generated structure with its stated limitations. '
|
||||
'Publication prerequisites and source-verification notes below remain informational follow-up items.')
|
||||
review=review.replace('## Please review','## Follow-up checks retained after approval')
|
||||
review=review.replace('not factual accuracy or human approval.','not independent factual accuracy. Human approval is recorded separately in approval.json.')
|
||||
core.write(pending/'review.md',review)
|
||||
guide=(pending/'llms.txt').read_text(encoding='utf-8').replace(
|
||||
'> Local draft for review; publication details may be unknown.',
|
||||
'> Final, owner-approved structured content; publication details may remain unknown.')
|
||||
core.write(pending/'llms.txt',guide)
|
||||
core.write(pending/'approval.json',approval)
|
||||
receipt={**draft_receipt,'status':'approved','lifecycle':'final','human_approval':True,
|
||||
'publication_ready':False,'approval_record':'approval.json',
|
||||
'files':{p.name:folder_builder.sha_file(p) for p in sorted(pending.iterdir())
|
||||
if p.is_file() and p.name!='build-receipt.json'}}
|
||||
core.write(pending/'build-receipt.json',receipt)
|
||||
plan['review']={'status':'approved','reviewer':reviewer,'reviewed_at':approved_at,
|
||||
'input_sha256':draft_receipt['input_sha256']}
|
||||
core.validate(plan,'plan')
|
||||
core.write(plan_path,plan)
|
||||
plan_updated=True
|
||||
# Validate all content/state and original hashes before exposing the final path.
|
||||
verify(pending)
|
||||
pending.rename(output)
|
||||
return verify(output)
|
||||
except Exception:
|
||||
if plan_updated and not output.exists():
|
||||
plan['review']=old_review
|
||||
core.write(plan_path,plan)
|
||||
raise
|
||||
finally:
|
||||
if pending.exists():shutil.rmtree(pending)
|
||||
|
||||
Reference in new issue
Block a user