2026-10-17 14:59:30 -06:00
"""Check repository JSON, document links, manifest paths, and local schema references."""
import ast
import json
import re
from pathlib import Path
from urllib.parse import unquote
2026-10-18 09:21:12 -06:00
from jsonschema import Draft202012Validator , FormatChecker
2026-10-17 14:59:30 -06:00
ROOT = Path ( __file__ ) . resolve () . parents [ 1 ]
SKIP = { 'build' , '.git' , '__pycache__' , '.test-work' , '.venv' }
2026-10-18 09:21:12 -06:00
def validate_contracts ( root = ROOT ):
def load ( name ):
return json . loads (( root / name ) . read_text ( encoding = 'utf-8-sig' ))
for document , schema in [( 'repository.json' , 'repository' ),( 'repository-variables.json' , 'repository-variables' ),( 'content-index.json' , 'content-index' )]:
Draft202012Validator ( load ( 'schemas/' + schema + '.schema.json' ), format_checker = FormatChecker ()) . validate ( load ( document ))
manifest , variables , index = load ( 'repository.json' ), load ( 'repository-variables.json' ), load ( 'content-index.json' )
assert variables [ 'identity' ][ 'authors' ] == manifest [ 'authors' ], 'Author mismatch'
assert variables [ 'identity' ][ 'publisher' ] == manifest [ 'publisher' ], 'Publisher mismatch'
assert variables [ 'identity' ][ 'maintainer' ][ 'name' ] == manifest [ 'maintenance' ][ 'maintainer' ], 'Maintainer mismatch'
for key , value in variables [ 'repository' ] . items ():
assert manifest . get ( key ) == value , 'Repository identity mismatch: ' + key
for key , value in variables [ 'rights' ] . items ():
assert manifest [ 'rights' ][ key ] == value , 'Rights mismatch: ' + key
version = ( root / 'VERSION' ) . read_text ( encoding = 'utf-8-sig' ) . strip ()
assert manifest [ 'version' ] == version , 'Manifest/version mismatch'
citation = ( root / 'CITATION.cff' ) . read_text ( encoding = 'utf-8' )
assert re . search ( r '(?m)^version:\s*[\"\x27]?' + re . escape ( version ) + r '[\"\x27]?\s*$' , citation ), 'Citation version mismatch'
assert 'license: MIT' in citation and manifest [ 'rights' ][ 'license' ] == 'MIT' , 'Citation/license mismatch'
assert manifest [ 'canonical_url' ] in citation , 'Citation URL mismatch'
for record in manifest [ 'authors' ]:
assert record [ 'name' ] in ( root / 'AUTHOR.md' ) . read_text ( encoding = 'utf-8' ), 'Author notice mismatch'
2026-10-19 12:56:31 -06:00
license_text = ( root / manifest [ 'rights' ][ 'license_file' ]) . read_text ( encoding = 'utf-8' )
assert re . search ( r 'Copyright \(c\) \d {4} ' , license_text ), 'Missing copyright notice'
assert any ( record [ 'name' ] in license_text for record in manifest [ 'authors' ]), 'License attribution mismatch'
2026-10-18 09:21:12 -06:00
ids = [ item [ 'content_id' ] for item in index [ 'items' ]]
paths = [ item [ 'path' ] for item in index [ 'items' ]]
assert len ( ids ) == len ( set ( ids )) and len ( paths ) == len ( set ( paths )), 'Duplicate content identity/path'
for item in index [ 'items' ]:
target = ( root / item [ 'path' ]) . resolve ()
assert target . is_relative_to ( root . resolve ()) and target . is_file (), 'Missing/unsafe indexed path'
assert set ( item . get ( 'related_content_ids' ,[])) <= set ( ids ), 'Unresolved content relation'
for declared in [ v for v in manifest [ 'entrypoints' ] . values () if v ] + [ c [ 'path' ] for c in manifest [ 'contents' ]] + manifest [ 'ai_retrieval' ][ 'read_first' ] + manifest [ 'ai_retrieval' ][ 'retrieve_as_needed' ]:
target = ( root / declared ) . resolve ()
assert target . is_relative_to ( root . resolve ()) and target . exists (), 'Missing/unsafe declared path: ' + declared
return { 'version' : version , 'license' : manifest [ 'rights' ][ 'license' ], 'indexed_documents' : len ( ids )}
2026-10-19 12:56:31 -06:00
def validate_public_template ( root = ROOT ):
"""Bounded repository hygiene checks; not a general PII detector."""
operational_roots = { 'tasks' , 'task-records' , 'runtime' , 'private' , 'recordings' , 'structured' }
generated_names = { 'media-intake.json' , 'structure-plan.json' , 'build-receipt.json' , 'approval.json' }
text_suffixes = { '.md' , '.py' , '.json' , '.txt' , '.vtt' , '.cff' }
machine_path = re . compile ( r '(?<![A-Za-z0-9])[A-Za-z]:[ \\ /]|/(?:Users|home)/[A-Za-z0-9_.-]+/' )
checked = 0
for path in root . rglob ( '*' ):
rel = path . relative_to ( root )
if not path . is_file () or set ( rel . parts ) & SKIP :
continue
assert rel . parts [ 0 ] not in operational_roots and not rel . parts [ 0 ] . startswith ( 'structured-final' ), 'Operational records in public template: ' + rel . as_posix ()
assert path . name not in generated_names and not path . name . endswith (( '.ticket.json' , '.receipt.json' , '.log' )), 'Recording-specific output in public template: ' + rel . as_posix ()
if path . suffix in text_suffixes or path . name in { 'LICENSE' , '.gitignore' , '.gitattributes' , 'VERSION' }:
text = path . read_text ( encoding = 'utf-8-sig' )
assert not machine_path . search ( text ), 'Absolute machine path in public template: ' + rel . as_posix ()
checked += 1
return { 'public_template_files' : checked }
2026-10-17 14:59:30 -06:00
def check ():
files = [ p for p in ROOT . rglob ( '*' ) if p . is_file () and not ( set ( p . relative_to ( ROOT ) . parts ) & SKIP )]
count = { 'json' : 0 , 'markdown_links' : 0 , 'schema_refs' : 0 , 'python' : 0 }
errors = []
def local_ref ( base , ref ):
filename , _ , pointer = ref . partition ( '#' )
path = ( base . parent / filename ) . resolve () if filename else base
assert path . is_relative_to ( ROOT ) and path . is_file (), f 'Missing/outside reference { ref } '
doc = json . loads ( path . read_text ( encoding = 'utf-8-sig' ))
for key in pointer . lstrip ( '/' ) . split ( '/' ) if pointer else []:
key = unquote ( key ) . replace ( '~1' , '/' ) . replace ( '~0' , '~' )
doc = doc [ int ( key )] if isinstance ( doc , list ) else doc [ key ]
def walk_refs ( value , base ):
if isinstance ( value , dict ):
for key , item in value . items ():
if key == '$ref' and not item . startswith (( 'https:' , 'http:' )):
local_ref ( base , item )
count [ 'schema_refs' ] += 1
else :
walk_refs ( item , base )
elif isinstance ( value , list ):
for item in value :
walk_refs ( item , base )
for path in files :
try :
if path . suffix in { '.md' , '.py' , '.json' , '.txt' , '.vtt' } or path . name in { '.gitignore' , '.gitattributes' , 'VERSION' }:
text = path . read_text ( encoding = 'utf-8-sig' )
assert all ( line == line . rstrip ( ' \t ' ) for line in text . splitlines ()), 'Trailing whitespace'
assert not re . search ( r '-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----|AKIA[0-9A-Z] {16} |gh[pousr]_[A-Za-z0-9]{30,}' , text ), 'Potential credential pattern'
if path . suffix == '.json' :
value = json . loads ( path . read_text ( encoding = 'utf-8-sig' ))
count [ 'json' ] += 1
walk_refs ( value , path )
if path . name . endswith ( '.schema.json' ):
Draft202012Validator . check_schema ( value )
elif path . suffix == '.py' :
ast . parse ( path . read_text ( encoding = 'utf-8' ), filename = str ( path ))
count [ 'python' ] += 1
elif path . suffix == '.md' :
for target in re . findall ( r '\[[^\]]*\]\(([^)]+)\)' , path . read_text ( encoding = 'utf-8' )):
if target . startswith (( 'http:' , 'https:' , '#' , 'mailto:' )):
continue
linked = ( path . parent / unquote ( target . split ( '#' )[ 0 ])) . resolve ()
assert linked . is_relative_to ( ROOT ) and linked . exists (), f 'Missing local link { target } '
count [ 'markdown_links' ] += 1
except Exception as error :
errors . append ( f ' { path . relative_to ( ROOT ) } : { error } ' )
manifest = json . loads (( ROOT / 'repository.json' ) . read_text ( encoding = 'utf-8' ))
2026-10-18 09:21:12 -06:00
for name in [ v for v in manifest [ 'entrypoints' ] . values () if v ] + [ v [ 'path' ] for v in manifest [ 'contents' ]]:
2026-10-17 14:59:30 -06:00
if not ( ROOT / name ) . exists ():
errors . append ( 'Missing manifest path: ' + name )
if errors :
raise ValueError ( ' \n ' . join ( errors ))
2026-10-19 12:56:31 -06:00
print ( json . dumps ({ 'valid' : True , ** count , ** validate_contracts ( ROOT ), ** validate_public_template ( ROOT )}, indent = 2 ))
2026-10-17 14:59:30 -06:00
if __name__ == '__main__' :
check ()