273 lines
15 KiB
Python
273 lines
15 KiB
Python
#!/usr/bin/env python3
|
||
"""Offline completeness gate, NOT medical/SEO approval or live verification.
|
||
No network, credentials, publisher-state operations or article mutations.
|
||
"""
|
||
import argparse
|
||
import hashlib
|
||
import json
|
||
from pathlib import Path
|
||
import re
|
||
from html.parser import HTMLParser
|
||
from urllib.parse import urlsplit
|
||
|
||
SOURCES = {
|
||
'admin': 'frontend/admin-panel/src/pages/Blogs.tsx',
|
||
'client': 'frontend/admin-panel/src/services/api.ts',
|
||
'controller': 'backend/src/admin/blogs.controller.ts',
|
||
'service': 'backend/src/admin/blogs.service.ts',
|
||
'schema': 'backend/prisma/schema.prisma',
|
||
}
|
||
SYSTEM = {'id', 'viewCount', 'createdAt', 'updatedAt'}
|
||
RELATIONS = {'author', 'category', 'tags', 'comments'}
|
||
DISPOSITIONS = {'verified_value', 'system_derived', 'not_applicable', 'evidence_unavailable_empty'}
|
||
|
||
|
||
def sha(path):
|
||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||
|
||
|
||
def manifest(root):
|
||
root = Path(root)
|
||
sources = {k: {'path': v, 'sha256': sha(root / v)} for k, v in SOURCES.items()}
|
||
schema = (root / SOURCES['schema']).read_text()
|
||
block = re.search(r'model Blog\s*\{(.*?)\n\}', schema, re.S)
|
||
if not block:
|
||
raise ValueError('Blog model missing')
|
||
fields = {}
|
||
for line in block.group(1).splitlines():
|
||
m = re.match(r'\s*(\w+)\s+([\w\[\]?]+)', line)
|
||
if not m:
|
||
continue
|
||
name, typ = m.groups()
|
||
fields[name] = {'type': typ, 'kind': 'system' if name in SYSTEM else 'relation' if name in RELATIONS else 'scalar'}
|
||
# tagNames is a transport alias, not a Prisma field; do not send tags/tagIds.
|
||
fields['tagNames'] = {'type': 'String[]', 'kind': 'transport_alias', 'readback': 'tags[].name'}
|
||
fields['markContentUpdated'] = {'type': 'Boolean', 'kind': 'transport_control', 'readback': 'contentUpdatedAt'}
|
||
# Optional UPDATE concurrency precondition; never a persisted Blog scalar.
|
||
fields['expectedUpdatedAt'] = {'type': 'String', 'kind': 'transport_control', 'actions': ['UPDATE'], 'persisted': False}
|
||
for role, needles in {
|
||
'admin': ["api.post('/admin/blogs', payload)", 'tagNames', 'relatedBlogIds'],
|
||
'controller': ['@Controller("admin/blogs")', '@Get("tags")', '@Post()'],
|
||
'service': ['tagNames', 'connectOrCreate', 'relatedBlogIds', 'contentUpdatedAt'],
|
||
'client': ['axios'],
|
||
}.items():
|
||
text = (root / SOURCES[role]).read_text()
|
||
if any(n not in text for n in needles):
|
||
raise ValueError(f'{role} contract changed: review parser and manifest manually')
|
||
return {'scope': 'LOCAL_SOURCE_CONTRACT_NOT_DEPLOYMENT_PROOF', 'sources': sources, 'fields': fields}
|
||
|
||
|
||
class Links(HTMLParser):
|
||
def __init__(self):
|
||
super().__init__()
|
||
self.hrefs = set()
|
||
def handle_starttag(self, tag, attrs):
|
||
if tag == 'a':
|
||
href = dict(attrs).get('href', '')
|
||
u = urlsplit(href)
|
||
if (not u.netloc and href.startswith('/') and not href.startswith('//')) or u.netloc == 'canina.ir':
|
||
self.hrefs.add(href)
|
||
|
||
|
||
CITATION = re.compile(r'\[\s*([0-9۰-۹٠-٩]+(?:\s*[,،;؛–-]\s*[0-9۰-۹٠-٩]+)*)\s*\]')
|
||
|
||
|
||
class CitationHTML(HTMLParser):
|
||
def __init__(self):
|
||
super().__init__(convert_charrefs=True)
|
||
self.stack = []
|
||
self.anchors = []
|
||
self.raw = []
|
||
self.targets = {}
|
||
def handle_starttag(self, tag, attrs):
|
||
attrs = dict(attrs)
|
||
self.stack.append((tag, attrs))
|
||
if attrs.get('id'):
|
||
self.targets.setdefault(attrs['id'], set())
|
||
if tag == 'a':
|
||
self.anchors.append({'href': attrs.get('href', ''), 'text': ''})
|
||
attrs['_anchor'] = self.anchors[-1]
|
||
for _, parent in self.stack:
|
||
if parent.get('id'):
|
||
self.targets.setdefault(parent['id'], set()).add(attrs.get('href', ''))
|
||
if tag in {'br', 'img', 'hr', 'input', 'meta', 'link'}:
|
||
self.stack.pop()
|
||
def handle_endtag(self, tag):
|
||
for i in range(len(self.stack)-1, -1, -1):
|
||
if self.stack[i][0] == tag:
|
||
del self.stack[i:]
|
||
break
|
||
def handle_data(self, text):
|
||
anchors = [attrs['_anchor'] for _, attrs in self.stack if '_anchor' in attrs]
|
||
if anchors:
|
||
anchors[-1]['text'] += text
|
||
else:
|
||
self.raw.append(text)
|
||
|
||
|
||
def validate_citations(content, sources):
|
||
"""Map numeric inline anchors to retrieved source URLs; no network claims."""
|
||
parser = CitationHTML()
|
||
parser.feed(content)
|
||
errors = []
|
||
if CITATION.search(''.join(parser.raw)):
|
||
errors.append('citations: raw unlinked numbered bracket citation')
|
||
for anchor in parser.anchors:
|
||
for match in CITATION.finditer(anchor['text']):
|
||
numbers = re.split(r'\s*[,،;؛–-]\s*', match[1])
|
||
for number in numbers:
|
||
key = str(int(number))
|
||
source = sources.get(key, {})
|
||
url = source.get('url', '')
|
||
href = anchor['href']
|
||
if not (urlsplit(url).scheme in {'http', 'https'} and urlsplit(url).netloc and source.get('retrieved') is True):
|
||
errors.append('citations: missing retrieved source mapping ' + key)
|
||
elif href != url and not (href == '#ref-' + key and url in parser.targets.get('ref-' + key, set())):
|
||
errors.append('citations: anchor does not resolve to mapped source ' + key)
|
||
return errors
|
||
|
||
|
||
def validate(packet, root, base):
|
||
"""Errors are cumulative. Evidence assertions still require independent review."""
|
||
errors = []
|
||
def require(ok, message):
|
||
if not ok:
|
||
errors.append(message)
|
||
expected = manifest(root)
|
||
require(packet.get('manifest') == expected, 'current complete source-derived manifest/hash mismatch')
|
||
payload = packet.get('payload', {})
|
||
errors.extend(validate_citations(payload.get('content', ''), packet.get('citation_sources', {})))
|
||
action = packet.get('action')
|
||
require(action in {'NEW', 'UPDATE', 'READ_ONLY_ACCEPTANCE'}, 'explicit action required')
|
||
if action in {'UPDATE', 'READ_ONLY_ACCEPTANCE'}:
|
||
require(bool(packet.get('record_id')), 'existing-record action requires record_id')
|
||
fields = packet.get('field_dispositions', {})
|
||
require(set(fields) == set(expected['fields']), 'every manifest field/control needs exactly one disposition')
|
||
allowed = set(expected['fields']) - SYSTEM - RELATIONS
|
||
require(not (set(payload) - allowed), 'unknown/system/readback-only payload keys: ' + ','.join(sorted(set(payload) - allowed)))
|
||
for key, field in fields.items():
|
||
require(field.get('disposition') in DISPOSITIONS, f'{key}: invalid disposition')
|
||
require(bool(field.get('reason')), f'{key}: reason required')
|
||
require(bool(field.get('evidence')), f'{key}: evidence references required')
|
||
if field.get('disposition') == 'verified_value':
|
||
require('value' in field, f'{key}: verified_value needs explicit value')
|
||
if key in allowed:
|
||
require(key in payload, f'{key}: verified writable value must be in payload; omission needs explicit disposition')
|
||
if key in payload:
|
||
require(field.get('value') == payload[key], f'{key}: disposition value differs from payload')
|
||
if field.get('disposition') in {'not_applicable', 'evidence_unavailable_empty'} and key in payload:
|
||
require(payload[key] in (None, '', []), f'{key}: deliberately empty disposition has nonempty payload')
|
||
if expected['fields'].get(key, {}).get('kind') == 'transport_control':
|
||
require(field.get('disposition') != 'verified_value', f'{key}: transport control is not a fetched scalar; use an explicit no-mutation/system-derived disposition')
|
||
if key in SYSTEM:
|
||
require(field.get('disposition') == 'system_derived', f'{key}: must be system-derived, not authored')
|
||
evidence = packet.get('evidence', {})
|
||
for key, entry in evidence.items():
|
||
path = Path(base) / entry.get('path', '')
|
||
require(path.is_file(), f'{key}: missing evidence file')
|
||
if path.is_file():
|
||
require(sha(path) == entry.get('sha256'), f'{key}: evidence hash mismatch')
|
||
require(bool(entry.get('readAt')), f'{key}: evidence readAt required')
|
||
def refs(refs, context):
|
||
require(bool(refs) and all(ref in evidence for ref in refs), context + ': unresolved evidence references')
|
||
for key, field in fields.items():
|
||
refs(field.get('evidence', []), key)
|
||
refs(packet.get('artifact_evidence', []), 'immutable artifact')
|
||
# Acceptance must bind dispositions to actual saved authenticated JSON, not
|
||
# a reconstructed payload that invents absent relations or transport fields.
|
||
if action == 'READ_ONLY_ACCEPTANCE':
|
||
binding = packet.get('acceptance_readback', {})
|
||
ref = binding.get('evidence')
|
||
require(ref in evidence, 'acceptance_readback: authenticated record evidence required')
|
||
if ref in evidence:
|
||
try:
|
||
fetched = json.loads((Path(base) / evidence[ref]['path']).read_text())
|
||
require(fetched.get('status') == 200, 'acceptance_readback: authenticated 200 required')
|
||
record = fetched['body']['data']
|
||
require(record.get('id') == packet.get('record_id'), 'acceptance_readback: record identity mismatch')
|
||
for key, field in fields.items():
|
||
kind = expected['fields'].get(key, {}).get('kind')
|
||
if kind == 'transport_control':
|
||
require(key not in record, key + ': unexpected transport readback requires contract review')
|
||
continue
|
||
if key == 'tagNames':
|
||
actual = [tag['name'] for tag in record.get('tags', [])]
|
||
require(field.get('value') == actual, key + ': readback alias differs')
|
||
elif key not in record:
|
||
require(field.get('disposition') in {'not_applicable', 'evidence_unavailable_empty'}, key + ': absent from fetched record; cannot claim fetched/system-derived value')
|
||
require(field.get('value') in (None, '', []), key + ': absent fetched field must be explicitly empty')
|
||
else:
|
||
require(field.get('value') == record[key], key + ': disposition differs from actual authenticated record')
|
||
except (ValueError, OSError, KeyError, TypeError):
|
||
require(False, 'acceptance_readback: invalid authenticated record evidence')
|
||
# Bind reviewed article to exact payload bytes, not merely a nearby draft.
|
||
artifact = packet.get('artifact', {})
|
||
path = Path(base) / artifact.get('path', '')
|
||
require(path.is_file(), 'immutable payload artifact missing')
|
||
if path.is_file():
|
||
require(sha(path) == artifact.get('sha256'), 'immutable payload artifact hash mismatch')
|
||
try:
|
||
require(json.loads(path.read_text()) == payload, 'immutable artifact differs from payload')
|
||
except (ValueError, OSError):
|
||
require(False, 'immutable artifact is not payload JSON')
|
||
relations = packet.get('relations', {})
|
||
for key, value in [('authorId', payload.get('authorId')), ('categoryId', payload.get('categoryId'))]:
|
||
item = relations.get(key, {})
|
||
require(item.get('selected') == value, key + ': selection mismatch')
|
||
require(bool(item.get('reason')), key + ': attribution/category rationale missing')
|
||
refs(item.get('evidence', []), key + ' authenticated read')
|
||
require(item.get('authenticated') is True and item.get('status') == 200, key + ': authenticated 200 read required')
|
||
for key in ['tagNames', 'relatedBlogIds', 'relatedProductIds']:
|
||
item = relations.get(key, {})
|
||
selected = payload.get(key, [])
|
||
require(item.get('selected') == selected, key + ': selection mismatch')
|
||
require(bool(item.get('reason')), key + ': deliberate disposition required even when empty')
|
||
refs(item.get('evidence', []), key + ' inventory/destination review')
|
||
require(item.get('authenticated') is True and item.get('status') == 200, key + ': authenticated inventory read required')
|
||
require(item.get('inventory_complete') is True, key + ': complete inventory required')
|
||
require(isinstance(selected, list) and len(selected) == len(set(selected)), key + ': unique array required')
|
||
require(set(selected or []) <= set(item.get('available', [])), key + ': selection absent from verified inventory')
|
||
if key == 'relatedBlogIds':
|
||
require(packet.get('record_id') not in selected, 'self related-article forbidden')
|
||
parser = Links()
|
||
parser.feed(payload.get('content', ''))
|
||
links = packet.get('internal_links', {})
|
||
candidates = links.get('reviewed_destinations', [])
|
||
require(bool(candidates), 'contextual link destination shortlist not reviewed')
|
||
for item in candidates:
|
||
refs(item.get('evidence', []), 'destination ' + item.get('href', '?'))
|
||
require(bool(item.get('reason')), 'destination suitability/medical-safety rationale required')
|
||
require(item.get('identity_verified') is True and item.get('content_reviewed') is True, 'destination content/identity not verified (HTTP 200 insufficient)')
|
||
selected_links = links.get('selected', [])
|
||
require(set(selected_links) == parser.hrefs, 'contextual body links differ from reviewed selections')
|
||
suitable = {item.get('href') for item in candidates if item.get('suitable') is True}
|
||
require(set(selected_links) <= suitable, 'selected link has no suitable reviewed destination')
|
||
if not selected_links:
|
||
require(bool(links.get('zero_link_reason')) and links.get('disclose_in_report') is True, 'zero contextual links need justified exception and report disclosure')
|
||
require(not suitable, 'zero-link exception contradicts suitable destinations')
|
||
return errors
|
||
|
||
|
||
def main():
|
||
parser = argparse.ArgumentParser(description=__doc__)
|
||
parser.add_argument('action', choices=['manifest', 'validate'])
|
||
parser.add_argument('--source-root', required=True)
|
||
parser.add_argument('--packet')
|
||
args = parser.parse_args()
|
||
if args.action == 'manifest':
|
||
print(json.dumps(manifest(args.source_root), ensure_ascii=False, indent=2))
|
||
return 0
|
||
if not args.packet:
|
||
parser.error('validate requires --packet')
|
||
path = Path(args.packet)
|
||
try:
|
||
errors = validate(json.loads(path.read_text()), args.source_root, path.parent)
|
||
except (ValueError, OSError, KeyError, TypeError, AttributeError) as exc:
|
||
errors = ['invalid/incomplete evidence packet: ' + str(exc)]
|
||
print(json.dumps({'result': 'EDITORIAL_EVIDENCE_FAILED' if errors else 'EDITORIAL_EVIDENCE_COMPLETE', 'errors': errors, 'not_approval': 'Independent RD/Vet, SEO/Editorial, final QA, current live identity/relations, backup and public browser verification still mandatory.'}, ensure_ascii=False, indent=2))
|
||
return 1 if errors else 0
|
||
|
||
|
||
if __name__ == '__main__':
|
||
raise SystemExit(main())
|