]>
dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/validate-slice-22k.py
2 """Slice 22K acceptance audit over the existing offline collector and case records.
3 --audit-only checks saved executions without rerunning tests. Output is outside Git.
5 import argparse
, datetime
, hashlib
, importlib
. util
, json
, re
, subprocess
, sys
6 from pathlib
import Path
7 sys
. dont_write_bytecode
= True
8 ROOT
= Path ( __file__
). resolve (). parents
[ 1 ]
9 D
= ROOT
/ 'reports/slice-22k' ; PRIOR
= ROOT
/ 'reports/slice-22j/after'
10 BASE
= 'cc1a0e02803a5705c97fb5457f6e93ad22f8b347'
11 ALLOWED
={ 'src/xtm/reader.lisp' , 'topic-maps.asd' , 'tests/xtm-identity.lisp' , 'tests/xtm-occurrences.lisp' , 'tests/xtm-associations.lisp' }
12 SOURCES
= ALLOWED|
{ 'tests/xtm-item-identities.lisp' , 'scripts/validate-slice-22d.py' , 'reports/slice-22d/failure-controls.lisp' }
13 def read ( p
): return json
. loads ( p
. read_text ())
14 def sha ( p
): return hashlib
. sha256 ( p
. read_bytes ()). hexdigest ()
15 def save ( p
, x
): p
. write_text ( json
. dumps ( x
, indent
= 2 , ensure_ascii
= False )+ ' \n ' )
16 def git (* args
): return subprocess
. check_output ([ 'git' ,* args
], cwd
= ROOT
, text
= True ). strip ()
17 def diff ( a
, b
, path
= '' ):
19 if isinstance ( a
, dict ) and isinstance ( b
, dict ): return [ d
for k
in sorted ( a
. keys () |b
. keys ()) for d
in diff ( a
. get ( k
), b
. get ( k
), path
+ '/' + k
)]
20 if isinstance ( a
, list ) and isinstance ( b
, list ) and len ( a
)== len ( b
): return [ d
for i
,( x
, y
) in enumerate ( zip ( a
, b
)) for d
in diff ( x
, y
, path
+ '/' + str ( i
))]
21 return [{ 'path' : path
, 'before' : a
, 'after' : b
}]
23 initial
= read ( D
/ 'initial-state.json' ); assert initial
[ 'baseCommit' ]== BASE
and initial
[ 'initialIndexAndWorktreeClean' ]
24 assert set ( git ( 'diff' , '--name-only' , BASE
). splitlines ())& set ( initial
[ 'trackedFileSHA256' ])<= ALLOWED
25 protected
={ p
: h
for p
, h
in initial
[ 'trackedFileSHA256' ]. items () if p
not in ALLOWED
}
26 assert protected
=={ p
: sha ( ROOT
/ p
) for p
in protected
}
27 run
= read ( out
/ 'runs.json' ); assert len ( run
[ 'runs' ])== 119
28 assert all ( x
[ 'exitCode' ]== x
[ 'expectedExitCode' ] and x
[ 'network' ]== x
[ 'repositoryWrites' ]== 'denied' and sha ( out
/( x
[ 'label' ]+ '.log' ))== x
[ 'logSHA256' ] for x
in run
[ 'runs' ])
29 assert run
[ 'executionSourcesSHA256' ]=={ p
: sha ( ROOT
/ p
) for p
in run
[ 'executionSourcesSHA256' ]}
30 assert run
[ 'rawReportSHA256' ]=={ p
: sha ( out
/ p
) for p
in run
[ 'rawReportSHA256' ]}
31 old
= read ( PRIOR
/ 'xtm.json' ); new
= read ( out
/ 'xtm.json' ); before
={ x
[ 'id' ]: x
for x
in old
[ 'cases' ]}; after
={ x
[ 'id' ]: x
for x
in new
[ 'cases' ]}
32 inputs
={ x
[ 'id' ]: x
for x
in read ( D
/ 'originals-before.json' )}; targets
= set ( inputs
)
33 assert old
[ 'statuses' ]=={ 'pass' : 98 , 'fail' : 0 , 'unsupported' : 0 , 'not-run' : 0 }
34 assert new
[ 'statuses' ]=={ 'pass' : 107 , 'fail' : 0 , 'unsupported' : 0 , 'not-run' : 0 } and new
[ 'otherCorpusInputs' ]=={ 'positiveUnexecuted' : 15 , 'negativeUnregisteredUnexecuted' : 2 }
35 assert old
[ 'cases' ]== new
[ 'cases' ][: len ( old
[ 'cases' ])] and set ( after
)- set ( before
)== targets
36 assert len ( after
)== len ( new
[ 'cases' ]) and all ( after
[ id ][ 'status' ]== 'pass' for id in targets
)
37 assert [ c
for c
in new
[ 'checks' ] if c
in set ( old
[ 'checks' ])]== old
[ 'checks' ]
38 labels
= re
. findall ( r
'^XTM-CHECK-PASS (.+)$' ,( out
/ 'xtm.log' ). read_text (), re
. M
)
39 assert labels
== new
[ 'checks' ] and len ( labels
)== len ( set ( labels
))== 3339
40 permitted
={ 'scope' , 'statuses' , 'otherCorpusInputs' , 'checks' , 'cases' , 'occurrenceControls' , 'associationControls' , 'mergemapControls' }
42 if key
not in permitted
: assert old
[ key
]== new
[ key
], key
44 for key
in [ 'occurrenceControls' , 'associationControls' ]:
45 assert len ( old
[ key
])== len ( new
[ key
])
46 changed
=[( a
, b
) for a
, b
in zip ( old
[ key
], new
[ key
]) if a
!= b
]; assert len ( changed
)==( 1 if key
== 'occurrenceControls' else 2 )
48 assert a
[ 'label' ]== b
[ 'label' ] and b
[ 'condition' ][ 'class' ]== 'XTM-UNSUPPORTED' and b
[ 'condition' ][ 'code' ]== 'unsupported-construct'
49 retargets
. append ({ 'reportKey' : key
, 'label' : a
[ 'label' ], 'before' : a
, 'after' : b
, 'differences' : diff ( a
, b
)})
50 assert new
[ 'mergemapControls' ][ 1 :]== old
[ 'mergemapControls' ]
51 imported
= new
[ 'mergemapControls' ][ 0 ]; assert imported
[ 'label' ]== 'item-identities-native-import' and imported
[ 'sourceMapsPreserved' ] and imported
[ 'qualifiedParserCalls' ]== 2
52 assert len ( imported
[ 'resolverCalls' ])== 1 and imported
[ 'model' ][ 'canonicalNativeReferencesEQ' ]
53 save ( out
/ 'retargeted-control-observations.json' , retargets
)
54 originals
=[];( out
/ 'actual' ). mkdir ( exist_ok
= True )
55 assert { x
[ 'id' ] for x
in new
[ 'itemIdentityOriginals' ]}== targets
56 for row
in new
[ 'itemIdentityOriginals' ]:
57 source
= inputs
[ row
[ 'id' ]]; raw
= bytes
. fromhex ( row
[ 'sourceHex' ])
58 assert raw
==( ROOT
/ 'vendor/cxtm' / row
[ 'id' ]). read_bytes () and hashlib
. sha256 ( raw
). hexdigest ()== source
[ 'sourceSHA256' ]
59 assert row
[ 'status' ]== 'pass'
60 assert row
[ 'condition' ] is None and row
[ 'firstDifferentByte' ] is None and row
[ 'originalOctetsEQ' ] and row
[ 'qualifiedParserCalls' ]== 1 and row
[ 'oldHandlesCanonicalAndLive' ]
61 assert row
[ 'model' ][ 'globalItemIdentifiersUnique' ] and row
[ 'model' ][ 'nativeModel' ][ 'canonicalAssociationRoleReferencesEQ' ]
62 actual
= row
[ 'actualCXTM' ]. encode (); expected
=( ROOT
/ 'vendor/cxtm/xtm2/baseline' /( Path ( row
[ 'id' ]). name
+ '.cxtm' )). read_bytes ()
63 assert actual
== expected
== row
[ 'expectedCXTM' ]. encode () and len ( actual
)== row
[ 'actualBytes' ]== row
[ 'expectedBytes' ]== source
[ 'baselineBytes' ]
64 assert hashlib
. sha256 ( expected
). hexdigest ()== source
[ 'baselineSHA256' ] and after
[ row
[ 'id' ]][ 'actualOutcome' ]== 'cxtm-match'
65 p
= out
/ 'actual' /( Path ( row
[ 'id' ]). name
+ '.cxtm' ); p
. write_bytes ( actual
)
66 row
= dict ( row
, actualSHA256
= sha ( p
), completeByteExact
= True )
67 originals
. append ( dict ( row
, sourceSHA256
= source
[ 'sourceSHA256' ], baselineSHA256
= source
[ 'baselineSHA256' ]))
68 save ( out
/ 'original-contracts.json' , originals
)
69 # Compare all original reader diagnoses, not only successful registrations.
70 oldd
={ x
[ 'id' ]: x
for x
in read ( PRIOR
/ 'after-diagnostics.json' )}; newd
={ x
[ 'id' ]: x
for x
in read ( out
/ 'after-diagnostics.json' )}
71 inventory
= sorted ( 'xtm2/' + sub
+ '/' + p
. name
for sub
in [ 'in' , 'invalid' ] for p
in ( ROOT
/ 'vendor/cxtm/xtm2' / sub
). glob ( '*.xtm' ))
72 assert len ( inventory
)== 124 and set ( inventory
)== set ( oldd
)== set ( newd
)
75 a
, b
= oldd
[ id ], newd
[ id ]; ds
= diff ( a
, b
)
78 assert b
[ 'byteExact' ] and b
[ 'condition' ] is None
79 reason
= 'new Item Identity original execution reaches native canonical identifiers and byte-exact CXTM'
81 assert id == 'xtm2/in/occurrence-duplicate-iid2.xtm'
82 assert a
[ 'condition' ][ 'class' ]== 'XTM-UNSUPPORTED' and b
[ 'condition' ][ 'class' ]== 'XTM-REJECTION'
83 assert b
[ 'condition' ][ 'code' ]== 'duplicate-id' and b
[ 'condition' ][ 'line' ]== 11 and b
[ 'condition' ][ 'column' ]== 21
84 assert b
[ 'actualCXTM' ] is None and not b
[ 'byteExact' ] and id not in after
85 reason
= 'excluded original now reaches unchanged XML-ID uniqueness check; no normative qualification or registration is added'
86 changed
. append ({ 'id' : id , 'reason' : reason
, 'differences' : ds
})
87 elif id in targets
: raise AssertionError ( id )
88 bs
= before
. get ( id ,{}). get ( 'status' , 'not-run' ); ns
= after
. get ( id ,{}). get ( 'status' , 'not-run' ); assert ( bs
!= ns
)==( id in targets
)
89 matrix
. append ({ 'id' : id , 'before' : bs
, 'after' : ns
, 'registeredStatusChanged' : bs
!= ns
, 'diagnosisChanged' : bool ( ds
), 'beforeDiagnosis' : a
, 'afterDiagnosis' : b
})
90 assert all ( newd
[ id ]== oldd
[ id ] for id in before
)
91 assert { x
[ 'id' ] for x
in changed
}== targets|
{ 'xtm2/in/occurrence-duplicate-iid2.xtm' }
92 save ( out
/ 'diagnostic-changes.json' ,{ 'originalDiagnoses' : 124 , 'changed' : len ( changed
), 'unchanged' : 124 - len ( changed
), 'changes' : changed
})
93 save ( out
/ 'all-124-original-statuses.json' ,{ 'cases' : matrix
, 'registeredChanges' : 9 , 'previous98OriginalPASSUnchanged' : True })
96 if '-control-' not in x
[ 'label' ]: continue
97 a
= read ( PRIOR
/( x
[ 'label' ]+ '.json' )); b
= read ( out
/( x
[ 'label' ]+ '.json' )); changes
= diff ( a
, b
)
98 assert a
[ 'cases' ]== b
[ 'cases' ] and a
[ 'statuses' ]== b
[ 'statuses' ] and a
[ 'checks' ]== b
[ 'checks' ] and x
[ 'exitCode' ]== 1
100 for k
in [ 'scope' , 'itemIdentityOriginals' , 'itemIdentityControls' ]: aa
. pop ( k
, None ); bb
. pop ( k
, None )
102 key
= rt
[ 'reportKey' ]; aa
[ key
]=[ rt
[ 'after' ] if row
== rt
[ 'before' ] else row
for row
in aa
[ key
]]
103 assert aa
== bb
,( x
[ 'label' ], diff ( aa
, bb
))
104 assert not b
[ 'itemIdentityOriginals' ] and not b
[ 'itemIdentityControls' ]
105 faults
. append ({ 'label' : x
[ 'label' ], 'expectedASDFFailureObserved' : True , 'exitCode' : 1 , 'caseRecordsOutcomesAndChecksIdentical' : True , 'faultCountedAsOriginalPASS' : False , 'reportDifferences' : changes
})
106 assert len ( faults
)== 113
107 save ( out
/ 'failure-controls.json' ,{ 'retained' : 113 , 'new' : 0 , 'controls' : faults
, 'allTargetCasesAndChecksUnchanged' : True , 'failureDriverUnchanged' : True })
109 for p
in sorted (( PRIOR
/ 'retained' ). glob ( '*.json' )):
110 rel
= p
. relative_to ( PRIOR
)
111 assert p
. read_bytes ()==( out
/ rel
). read_bytes (), str ( rel
)
112 retained
. append ({ 'report' : str ( rel
), 'sha256' : sha ( p
), 'byteIdentical' : True })
113 for name
in [ 'ctm.validation.json' , 'ctm.cxtm.json' , 'ctm.encoding.json' ]:
114 assert ( out
/ name
). read_bytes ()==( PRIOR
/ name
). read_bytes (); retained
. append ({ 'report' : name
, 'sha256' : sha ( out
/ name
), 'byteIdentical' : True })
115 assert len ( retained
)== 49 ; save ( out
/ 'retained-reports.json' , retained
)
116 eqpath
= next (( out
/ 'retained' ). glob ( '*.equivalence.json' )); eqb
= read ( eqpath
)
117 assert ( PRIOR
/ eqpath
. relative_to ( out
)). read_bytes ()== eqpath
. read_bytes ()
118 assert len ( eqb
[ 'checks' ])== 501 and sum ( x
[ 'kind' ]== 'direct-reader-equivalence-pair' for x
in eqb
[ 'pairs' ])== 28
119 prior
= read ( PRIOR
/ 'execution.json' ); log
=( out
/ 'regression.log' ). read_text ()
120 for marker
, count
in dict ( prior
[ 'retainedChecks' ],** prior
[ 'stageChecks' ]). items (): assert int ( re
. search ( r
'(?m)^' + marker
+ r
'-PASS: (\d+)' , log
). group ( 1 ))== count
121 assert f
"CTM-XTM-EQUIVALENCE-PASS: {len(eqb['checks'])} checks; 28 actual reader pairs;" in log
122 assert 'Ran 2 tests' in log
and 'NFC-ORACLE-PASS: 16294' in log
and 'CXML-SUMMARY 146 PASS 0 FAIL' in ( out
/ 'xml.log' ). read_text ()
123 lock
= read ( ROOT
/ 'references/corpus-lock.json' )
124 for f
in lock
[ 'files' ]:
125 p
= ROOT
/ 'vendor/cxtm' / f
[ 'path' ]; assert p
. stat (). st_size
== f
[ 'size' ] and sha ( p
)== f
[ 'sha256' ]
126 assert protected
=={ p
: sha ( ROOT
/ p
) for p
in protected
}
127 save ( out
/ 'integrity.json' ,{ 'protectedBaseFilesSHA256' : protected
, 'pinnedCorpusFilesVerified' : len ( lock
[ 'files' ]), 'historicalReportFilesVerified' : sum ( p
. startswith ( 'reports/' ) for p
in protected
),
128 'originalsBaselinesHistoricalReportsAndCorpusLockUnchanged' : True , 'productionChanges' :[ 'src/xtm/reader.lisp' ], 'TMDM_CTM_CXTM_XMLUnchanged' : True })
129 save ( out
/ 'execution.json' ,{ 'kind' : 'slice-22k-XTM-item-identities' , 'baseCommit' : BASE
, 'branch' : git ( 'branch' , '--show-current' ), 'runtime' : 'SBCL 2.5.4' ,
130 'startedAtUTC' : run
[ 'startedAtUTC' ], 'finishedAtUTC' : run
[ 'finishedAtUTC' ], 'auditedAtUTC' : datetime
. datetime
. now ( datetime
. timezone
. utc
). isoformat (), 'runs' : run
[ 'runs' ],
131 'collector' : 'scripts/validate-slice-22d.py::collect (unchanged)' , 'auditToolSHA256' : sha ( Path ( __file__
)), 'standaloneXTMChecks' : len ( labels
), 'retainedStandaloneXTMChecks' : len ( old
[ 'checks' ]),
132 'newXTMChecks' : len ( labels
)- len ( old
[ 'checks' ]), 'retainedLispChecks' : 12100 , 'retainedChecks' : prior
[ 'retainedChecks' ], 'stageChecks' : prior
[ 'stageChecks' ],
133 'equivalenceChecks' : len ( eqb
[ 'checks' ]), 'equivalenceReaderPairs' : 28 , 'faultControls' : 113 , 'allOriginalStatusesCompared' : 124 , 'diagnosticChanges' : len ( changed
), 'registeredStatusChanges' : 9 ,
134 'statusesBefore' : prior
[ 'statusesAfter' ], 'statusesAfter' :{ 'XTM' :{ 'pass' : 107 , 'fail' : 0 , 'not-run' : 17 }, 'CTM' :{ 'pass' : 262 , 'fail' : 4 , 'not-run' : 7 }, 'total' :{ 'pass' : 369 , 'fail' : 4 , 'not-run' : 24 }},
135 'originalRegistrationsAdded' : 9 , 'xmlQualificationChecks' : 146 , 'pythonUnitTests' : 2 , 'nfcOracleComparisons' : 16294 , 'pinnedCorpusFilesVerified' : len ( lock
[ 'files' ]),
136 'historicalReportFilesVerified' : sum ( p
. startswith ( 'reports/' ) for p
in protected
), 'knownCTMFailuresUnchanged' : prior
[ 'knownCTMFailuresUnchanged' ],
137 'productionChanges' :[ 'src/xtm/reader.lisp' ], 'newDependencyDownloads' : 0 , 'persistentImagesTouched' : False , 'acceptancePassed' : True })
138 print ( json
. dumps ({ 'acceptancePassed' : True , 'XTMChecks' : len ( labels
), 'newOriginalPASS' : 9 , 'allOriginalStatusesCompared' : 124 , 'diagnosticChanges' : len ( changed
), 'faultControls' : 113 }))
140 ap
= argparse
. ArgumentParser ( description
= __doc__
); ap
. add_argument ( '--output' , type = Path
, required
= True ); ap
. add_argument ( '--audit-only' , action
= 'store_true' ); a
= ap
. parse_args (); out
= a
. output
. resolve (); assert not out
. is_relative_to ( ROOT
); out
. mkdir ( parents
= True , exist_ok
= True )
142 spec
= importlib
. util
. spec_from_file_location ( 'v22d' , ROOT
/ 'scripts/validate-slice-22d.py' ); v
= importlib
. util
. module_from_spec ( spec
); spec
. loader
. exec_module ( v
); v
. EXECUTION_FILES
= SOURCES
; v
. collect ( out
, ROOT
/ 'reports/slice-22d/baseline' )
144 if __name__
== '__main__' : main ()