MB-X Bilibili Pipeline
7 days ago 873dca205129f123d5ea9dd768bfa1c2e5452830
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import fs from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import {pathToFileURL} from 'node:url';
import {spawnSync} from 'node:child_process';
import * as runtime from './detail_strengthening_p0_eligible6_formal_evidence_integration_runtime_20260730.mjs';
import {parseAuthorityCsv,serializeCsv,serializeManifestDataRows,validateAppendStableManifest} from './artifact_manifest_append_stable_parser_repair001_20260730.mjs';
 
const ROOT=runtime.ROOT,BASE=runtime.BASE,CASE_ID=runtime.CASE_ID,MANIFEST=runtime.MANIFEST;
const SCRIPT='dev/ana-dev/detail_strengthening_p0_eligible6_formal_evidence_integration_design_20260730.mjs';
const DESIGN_DOC='ana-doc/机器人案例/案例分析设计.md';
const DESIGN_SET=`${BASE}/manifest/detail_strengthening_p0_eligible6_formal_evidence_integration_design_artifact_set_20260730.csv`;
const DESIGN_BASELINE=`${BASE}/evidence/detail_strengthening_p0_eligible6_formal_evidence_integration_design_baseline_20260730.csv`;
const NEGATIVE_RESULTS=`${BASE}/evidence/detail_strengthening_p0_eligible6_formal_evidence_integration_negative_results_20260730.csv`;
const DESIGN_VALIDATION=`${BASE}/manifest/detail_strengthening_p0_eligible6_formal_evidence_integration_design_validation_20260730.csv`;
const REVIEW_REQUEST=`${BASE}/manifest/review_request_detail_strengthening_p0_eligible6_formal_evidence_integration_design_20260730.md`;
const DESIGN_SET_COLUMNS=['artifact_id','batch_id','run_id','generation_order','artifact_path','artifact_role','identity_policy'];
const BASELINE_COLUMNS=runtime.BASELINE_COLUMNS,VALIDATION_COLUMNS=runtime.VALIDATION_COLUMNS;
const NEGATIVE_COLUMNS=['test_id','mutation_applied','oracle','expected','expected_code','actual','actual_codes','result'];
const abs=value=>path.join(ROOT,...value.split('/'));
const hash=value=>crypto.createHash('sha256').update(value).digest('hex');
const normalize=value=>value.toString('utf8').replace(/^\uFEFF/,'').replace(/\r\n/g,'\n').replace(/\r/g,'\n');
const lineCount=value=>{const text=normalize(value);return text.split('\n').length-(text.endsWith('\n')?1:0);};
const readCsv=file=>{const parsed=parseAuthorityCsv(normalize(fs.readFileSync(abs(file))));if(parsed.errors.length)throw new Error(`CSV:${file}:${parsed.errors.join('|')}`);return parsed;};
const identity=file=>{const buffer=fs.readFileSync(abs(file)),stat=fs.statSync(abs(file));return{rows:file.endsWith('.csv')?readCsv(file).rows.length:lineCount(buffer),bytes:buffer.length,sha256:hash(buffer),mtime:new Date(Math.trunc(stat.mtimeMs)).toISOString(),mtimeMs:Math.trunc(stat.mtimeMs)};};
const exactColumns=(parsed,columns,code)=>parsed.header.join('|')===columns.join('|')?[]:[`${code}_COLUMNS`];
const compareRows=(actual,expected,columns,code)=>{const errors=[];if(actual.length!==expected.length)return[`${code}_COUNT:${actual.length}:${expected.length}`];for(let i=0;i<expected.length;i++)for(const column of columns)if(String(actual[i]?.[column]??'')!==String(expected[i]?.[column]??'')){errors.push(`${code}_FIELD:${i+1}:${column}`);break;}return errors;};
const write=(file,text)=>{fs.mkdirSync(path.dirname(abs(file)),{recursive:true});fs.writeFileSync(abs(file),text,'utf8');};
 
function designEntry(){const lines=normalize(fs.readFileSync(abs(DESIGN_DOC))).split('\n'),start=lines.findIndex(line=>line.startsWith('## ')&&line.includes(runtime.DESIGN_ID));if(start<0)throw new Error('FORMAL_DESIGN_ENTRY_MISSING');let end=lines.length;for(let i=start+1;i<lines.length;i++)if(lines[i].startsWith('## ')){end=i;break;}while(end>start&&lines[end-1]==='')end--;const buffer=Buffer.from(`${lines.slice(start,end).join('\n')}\n`);return{start:start+1,end,lines:end-start,bytes:buffer.length,sha256:hash(buffer)};}
function formalEntryRow(){const entry=designEntry();return{authority_id:'P0INT-FORMAL-DESIGN-001',design_id:runtime.DESIGN_ID,artifact_path:DESIGN_DOC,start_line:String(entry.start),end_line:String(entry.end),lines:String(entry.lines),bytes:String(entry.bytes),sha256:entry.sha256,normalization:'UTF8_NO_BOM_LF_JOIN_LINES_WITH_TERMINAL_LF_V1',required_markers:[runtime.DESIGN_ID,'EMAP-046','25 项','FROZEN_LOCAL_INPUTS_ONLY_NO_NETWORK','Eligible6_formal_evidence_acceptance_or_integration_execution_release=NOT_RELEASED','NO_UPGRADE'].join('|')};}
function designSetRows(){const paths=[runtime.SCRIPT,SCRIPT,runtime.SOURCE_ENTRY_AUTHORITY,runtime.CRITERIA,runtime.ELIGIBLE_AUTHORITY,runtime.MAP_BASELINE,runtime.MAP_TARGET,runtime.LEDGER_SCHEMA,runtime.CONTRACT,runtime.SCHEMA,runtime.EXEC_SET,DESIGN_SET,runtime.FORMAL_ENTRY_AUTHORITY,DESIGN_BASELINE,NEGATIVE_RESULTS,REVIEW_REQUEST,DESIGN_VALIDATION],roles=['EXECUTION_RUNTIME','DESIGN_BUILDER','SOURCE_AUDIT_AUTHORITY','INTEGRATION_CRITERIA','ELIGIBLE6_AUTHORITY','FORMAL_MAP_BASELINE','FORMAL_MAP_TARGET','CHANGE_LEDGER_SCHEMA','RUNTIME_CONTRACT','OUTPUT_SCHEMA','EXECUTION_EXACT_SET','DESIGN_EXACT_SET','FORMAL_DESIGN_ENTRY_AUTHORITY','DESIGN_BASELINE','NEGATIVE_RESULTS','DESIGN_REVIEW_REQUEST','DESIGN_VALIDATION_LAST'];return paths.map((artifactPath,index)=>({artifact_id:`P0INT-DESIGN-SET-${String(index+1).padStart(3,'0')}`,batch_id:runtime.DESIGN_BATCH,run_id:runtime.DESIGN_RUN,generation_order:String(index+1),artifact_path:artifactPath,artifact_role:roles[index],identity_policy:'EXACT_SHA256_BYTES_FILESYSTEM_MTIME_MS'}));}
function protectedRows(){const rows=readCsv(runtime.PROTECTED_INVENTORY).rows;if(rows.length!==49)throw new Error(`PROTECTED_COUNT:${rows.length}`);return rows.map(row=>({path:row.artifact_path,...identity(row.artifact_path)}));}
function manifestState(buffer=fs.readFileSync(abs(MANIFEST)),options={}){return validateAppendStableManifest(buffer,{anomalyRows:readCsv(runtime.ANOMALY_AUTHORITY).rows,originalRunId:runtime.ORIGINAL_TRIAGE_RUN,originalRunExpectedRows:11,...options});}
function designBaselineRows(manifestBefore){const source=runtime.auditEntry(runtime.SOURCE_AUDIT),mapId=identity(runtime.CASE_MAP),rows=[{baseline_id:'P0INT-DESIGN-BASE-001',identity_type:'SOURCE_EXECUTION_AUDIT_ENTRY',artifact_path:`${runtime.AUDIT_REPORT}#L${source.start}-L${source.end}`,rows:String(source.lines),bytes:String(source.bytes),sha256:source.sha256,mtime:'INFO_APPENDABLE_SHARED_FILE',required_state:'EXACT_SOURCE_ENTRY'},{baseline_id:'P0INT-DESIGN-BASE-002',identity_type:'MANIFEST_PREFIX_BEFORE_DESIGN',artifact_path:MANIFEST,rows:String(manifestState(manifestBefore).logicalDataRows),bytes:String(manifestBefore.length),sha256:hash(manifestBefore),mtime:new Date(Math.trunc(fs.statSync(abs(MANIFEST)).mtimeMs)).toISOString(),required_state:'EXACT_APPEND_ONLY_PREFIX'},{baseline_id:'P0INT-DESIGN-BASE-003',identity_type:'FORMAL_MAP_CURRENT_BASELINE',artifact_path:runtime.CASE_MAP,rows:String(mapId.rows),bytes:String(mapId.bytes),sha256:mapId.sha256,mtime:mapId.mtime,required_state:'EXACT45_NO_DESIGN_MUTATION'}];for(const file of designSetRows().slice(0,13).map(row=>row.artifact_path)){const id=identity(file);rows.push({baseline_id:`P0INT-DESIGN-BASE-${String(rows.length+1).padStart(3,'0')}`,identity_type:'FROZEN_DESIGN_INPUT',artifact_path:file,rows:String(id.rows),bytes:String(id.bytes),sha256:id.sha256,mtime:id.mtime,required_state:'EXACT_NO_DRIFT'});}for(const item of protectedRows())rows.push({baseline_id:`P0INT-DESIGN-BASE-${String(rows.length+1).padStart(3,'0')}`,identity_type:'PROTECTED49_CURRENT_IDENTITY',artifact_path:item.path,rows:String(item.rows),bytes:String(item.bytes),sha256:item.sha256,mtime:item.mtime,required_state:'NO_MUTATION'});return rows;}
function requestText(){return`# Eligible6 formal evidence integration detailed-design review request\n\ncase_id=${CASE_ID}\naction_id=NEXT-ROBOT-043-DESIGN-001\ndesign_id=${runtime.DESIGN_ID}\nsource_audit=${runtime.SOURCE_AUDIT}\nrequested_release_audit=${runtime.RELEASE_AUDIT}\nproposed_batch_id=${runtime.EXEC_BATCH}\nproposed_run_id=${runtime.EXEC_RUN}\nreview_scope=DETAILED_DESIGN_ONLY_NO_EXECUTION\neligible_references=6\nheld_candidate=P0DISCR1-CAND-005\nformal_map_before=45\nformal_map_after=46\nmap_change=APPEND_ONE_REGISTER_ROW_EMAP_046_ONLY\nexecution_exact_set=25\ndesign_exact_set=17\nnetwork_scope=FROZEN_LOCAL_INPUTS_ONLY_NO_NETWORK\nintegration_ceiling=FORMAL_REFERENCE_REGISTRATION_ONLY_GAPS_REMAIN_OPEN_NO_UPGRADE\ngap_state=OPEN_RETAINED_GAP\nclaim_strength_ceiling=PENDING_VERIFICATION_MAX_NO_UPGRADE\nformal_pool_effect=NO_AUTOMATIC_FORMAL_POOL_CHANGE\nevidence_strength_effect=NO_UPGRADE\nexecution_release=NOT_RELEASED_PENDING_INDEPENDENT_DESIGN_REVIEW\n`;} 
function validationRows(manifestBefore){const preview=spawnSync(process.execPath,[abs(runtime.SCRIPT),'--preview'],{cwd:ROOT,encoding:'utf8'}),self=runtime.negativeRows(),inputErrors=runtime.designInputErrors({requireRelease:false,expectNoExecution:true}),checks=[['NODE_CHECK_RUNTIME','0',String(spawnSync(process.execPath,['--check',abs(runtime.SCRIPT)],{cwd:ROOT}).status??1)],['NODE_CHECK_BUILDER','0',String(spawnSync(process.execPath,['--check',abs(SCRIPT)],{cwd:ROOT}).status??1)],['SOURCE_AUTHORITY_ERRORS','0',String(runtime.sourceAuthorityErrors().length)],['DESIGN_INPUT_ERRORS','0',String(inputErrors.length)],['PREVIEW_EXIT','0',String(preview.status??1)],['NEGATIVE_PASS','19',String(self.filter(row=>row.result==='PASS').length)],['ELIGIBLE','6',String(runtime.eligibleRows().length)],['HELD_CAND005_EXCLUDED','0',String(runtime.eligibleRows().filter(row=>row.candidate_id==='P0DISCR1-CAND-005').length)],['MAP_BASELINE','45',String(readCsv(runtime.MAP_BASELINE).rows.length)],['MAP_TARGET','46',String(readCsv(runtime.MAP_TARGET).rows.length)],['CRITERIA','10',String(readCsv(runtime.CRITERIA).rows.length)],['CONTRACTS','35',String(readCsv(runtime.CONTRACT).rows.length)],['SCHEMAS','8',String(readCsv(runtime.SCHEMA).rows.length)],['EXECUTION_EXACT_SET','25',String(readCsv(runtime.EXEC_SET).rows.length)],['DESIGN_EXACT_SET','17',String(designSetRows().length)],['DESIGN_BASELINE','65','65'],['PROTECTED49','49',String(protectedRows().length)],['EXECUTION_OUTPUTS_EXIST','0',String(runtime.GENERATED.filter(file=>fs.existsSync(abs(file))).length)],['EXECUTION_RUN_ROWS','0',String(manifestState(manifestBefore).logicalRows.filter(row=>row.run_id===runtime.EXEC_RUN).length)]];const rows=checks.map((item,index)=>({check_id:`P0INT-DESIGN-VAL-${String(index+1).padStart(3,'0')}`,check_description:item[0],expected:item[1],actual:item[2],status:item[1]===item[2]?'PASS':'FAIL',evidence:'ELIGIBLE6_FORMAL_EVIDENCE_INTEGRATION_DESIGN_FAIL_CLOSED'}));rows.push({check_id:'P0INT-DESIGN-VAL-020',check_description:'SELF_REFERENCE_EXCLUDED',expected:'SELF_REFERENCE_EXCLUDED',actual:'SELF_REFERENCE_EXCLUDED',status:'INFO',evidence:'design validation and manifest identities postchecked'});return rows;}
function manifestRowsFor(paths){return paths.map(file=>{const id=identity(file);return{case_id:CASE_ID,run_id:runtime.DESIGN_RUN,batch_id:runtime.DESIGN_BATCH,artifact_path:file,sha256:id.sha256,bytes:String(id.bytes),mtime:id.mtime};});}
function baselineErrors(rows,manifestPrefix){const errors=[];if(rows.length!==65)return[`DESIGN_BASELINE_COUNT:${rows.length}:65`];const source=runtime.auditEntry(runtime.SOURCE_AUDIT);if(rows[0].rows!==String(source.lines)||rows[0].bytes!==String(source.bytes)||rows[0].sha256!==source.sha256)errors.push('DESIGN_BASELINE_SOURCE');if(rows[1].bytes!==String(manifestPrefix.length)||rows[1].sha256!==hash(manifestPrefix))errors.push('DESIGN_BASELINE_MANIFEST');const mapId=identity(runtime.CASE_MAP);if(rows[2].rows!=='45'||rows[2].bytes!==String(mapId.bytes)||rows[2].sha256!==mapId.sha256)errors.push('DESIGN_BASELINE_MAP');for(const row of rows.slice(3)){if(!fs.existsSync(abs(row.artifact_path))){errors.push(`DESIGN_BASELINE_MISSING:${row.artifact_path}`);continue;}const id=identity(row.artifact_path);if(row.rows!==String(id.rows)||row.bytes!==String(id.bytes)||row.sha256!==id.sha256||Date.parse(row.mtime)!==id.mtimeMs)errors.push(`DESIGN_BASELINE_IDENTITY:${row.artifact_path}`);}return errors;}
function validateDesign({postManifest=false}={}){const errors=[];const checks=[[runtime.SOURCE_ENTRY_AUTHORITY,runtime.SOURCE_ENTRY_COLUMNS,[runtime.sourceAuthorityRow()],'SOURCE_AUTHORITY'],[runtime.CRITERIA,runtime.CRITERIA_COLUMNS,runtime.criteriaRows(),'CRITERIA'],[runtime.ELIGIBLE_AUTHORITY,runtime.ELIGIBLE_COLUMNS,runtime.eligibleRows(),'ELIGIBLE'],[runtime.MAP_TARGET,runtime.MAP_TARGET_COLUMNS,runtime.mapTargetRows(readCsv(runtime.MAP_BASELINE).rows),'MAP_TARGET'],[runtime.LEDGER_SCHEMA,runtime.LEDGER_SCHEMA_COLUMNS,runtime.ledgerSchemaRows(),'LEDGER_SCHEMA'],[runtime.CONTRACT,runtime.CONTRACT_COLUMNS,runtime.contractRows(),'CONTRACT'],[runtime.SCHEMA,runtime.SCHEMA_COLUMNS,runtime.schemaRows(),'SCHEMA'],[runtime.EXEC_SET,runtime.EXEC_SET_COLUMNS,runtime.executionSetRows(),'EXEC_SET'],[DESIGN_SET,DESIGN_SET_COLUMNS,designSetRows(),'DESIGN_SET'],[runtime.FORMAL_ENTRY_AUTHORITY,runtime.FORMAL_ENTRY_COLUMNS,[formalEntryRow()],'FORMAL_ENTRY'],[NEGATIVE_RESULTS,NEGATIVE_COLUMNS,runtime.negativeRows(),'NEGATIVE']];for(const [file,columns,expected,code] of checks){if(!fs.existsSync(abs(file))){errors.push(`${code}_MISSING`);continue;}const parsed=readCsv(file);errors.push(...exactColumns(parsed,columns,code),...compareRows(parsed.rows,expected,columns,code));}if(!fs.readFileSync(abs(runtime.MAP_BASELINE)).equals(fs.readFileSync(abs(runtime.CASE_MAP))))errors.push('MAP_BASELINE_BYTES');const baseline=readCsv(DESIGN_BASELINE),prefixBytes=Number(baseline.rows[1]?.bytes??0),manifest=fs.readFileSync(abs(MANIFEST)),prefix=manifest.subarray(0,prefixBytes);errors.push(...baselineErrors(baseline.rows,prefix),...runtime.designInputErrors({requireRelease:false,expectNoExecution:true}));if(normalize(fs.readFileSync(abs(REVIEW_REQUEST)))!==requestText())errors.push('REVIEW_REQUEST_TEXT');const validation=readCsv(DESIGN_VALIDATION);if(validation.rows.length!==20||validation.rows.some(row=>row.status==='FAIL'))errors.push('DESIGN_VALIDATION_STATUS');if(postManifest){const expected=manifestRowsFor(designSetRows().map(row=>row.artifact_path)),state=manifestState(manifest,{frozenPrefix:{bytes:prefixBytes,sha256:hash(prefix)},expectedRunRows:expected,expectedRunId:runtime.DESIGN_RUN,expectedPhysicalDelta:17,filesystemIdentity:{runIds:[runtime.DESIGN_RUN],get:file=>fs.existsSync(abs(file))?identity(file):null}});errors.push(...state.errors.map(code=>`MANIFEST:${code}`));}else if(manifestState(manifest).logicalRows.some(row=>row.run_id===runtime.DESIGN_RUN))errors.push('DESIGN_RUN_ROWS_EXIST');return[...new Set(errors)];}
 
function build(){const existing=designSetRows().slice(2).map(row=>row.artifact_path).filter(file=>fs.existsSync(abs(file)));if(existing.length)throw new Error(`DESIGN_TARGETS_EXIST:${existing.join('|')}`);const manifestBefore=fs.readFileSync(abs(MANIFEST)),created=[];try{const emit=(file,text)=>{write(file,text);created.push(file);};emit(runtime.SOURCE_ENTRY_AUTHORITY,serializeCsv([runtime.sourceAuthorityRow()],runtime.SOURCE_ENTRY_COLUMNS));emit(runtime.CRITERIA,serializeCsv(runtime.criteriaRows(),runtime.CRITERIA_COLUMNS));emit(runtime.ELIGIBLE_AUTHORITY,serializeCsv(runtime.eligibleRows(),runtime.ELIGIBLE_COLUMNS));fs.writeFileSync(abs(runtime.MAP_BASELINE),fs.readFileSync(abs(runtime.CASE_MAP)));created.push(runtime.MAP_BASELINE);emit(runtime.MAP_TARGET,serializeCsv(runtime.mapTargetRows(readCsv(runtime.MAP_BASELINE).rows),runtime.MAP_TARGET_COLUMNS));emit(runtime.LEDGER_SCHEMA,serializeCsv(runtime.ledgerSchemaRows(),runtime.LEDGER_SCHEMA_COLUMNS));emit(runtime.CONTRACT,serializeCsv(runtime.contractRows(),runtime.CONTRACT_COLUMNS));emit(runtime.SCHEMA,serializeCsv(runtime.schemaRows(),runtime.SCHEMA_COLUMNS));emit(runtime.EXEC_SET,serializeCsv(runtime.executionSetRows(),runtime.EXEC_SET_COLUMNS));emit(DESIGN_SET,serializeCsv(designSetRows(),DESIGN_SET_COLUMNS));emit(runtime.FORMAL_ENTRY_AUTHORITY,serializeCsv([formalEntryRow()],runtime.FORMAL_ENTRY_COLUMNS));emit(DESIGN_BASELINE,serializeCsv(designBaselineRows(manifestBefore),BASELINE_COLUMNS));emit(NEGATIVE_RESULTS,serializeCsv(runtime.negativeRows(),NEGATIVE_COLUMNS));emit(REVIEW_REQUEST,requestText());const validation=validationRows(manifestBefore);emit(DESIGN_VALIDATION,serializeCsv(validation,VALIDATION_COLUMNS));if(validation.some(row=>row.status==='FAIL'))throw new Error('DESIGN_VALIDATION_FAIL');const pre=validateDesign();if(pre.length)throw new Error(`DESIGN_PREWRITE:${pre.join('|')}`);const rows=manifestRowsFor(designSetRows().map(row=>row.artifact_path)),candidate=Buffer.concat([manifestBefore,Buffer.from(serializeManifestDataRows(rows),'utf8')]),candidateState=manifestState(candidate,{frozenPrefix:{bytes:manifestBefore.length,sha256:hash(manifestBefore)},expectedRunRows:rows,expectedRunId:runtime.DESIGN_RUN,expectedPhysicalDelta:17});if(candidateState.errors.length)throw new Error(`MANIFEST_CANDIDATE:${candidateState.errors.join('|')}`);fs.writeFileSync(abs(MANIFEST),candidate);const post=validateDesign({postManifest:true});if(post.length)throw new Error(`DESIGN_POSTWRITE:${post.join('|')}`);return{status:'DESIGN_BUILT_PENDING_INDEPENDENT_REVIEW',eligible:6,held:1,map_before:45,map_after:46,execution_exact_set:25,design_exact_set:17,negative_tests:runtime.negativeRows().length,manifest_rows:17};}catch(error){fs.writeFileSync(abs(MANIFEST),manifestBefore);for(const file of created.reverse())if(fs.existsSync(abs(file)))fs.rmSync(abs(file));throw error;}}
 
if(process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href){const mode=process.argv[2]??'--validate-design';if(mode==='--build-design')console.log(JSON.stringify(build(),null,2));else if(mode==='--validate-design'){const errors=validateDesign({postManifest:true});console.log(JSON.stringify({status:errors.length?'FAIL':'PASS_DESIGN_PACKAGE',errors},null,2));if(errors.length)process.exitCode=1;}else if(mode==='--self-test'){const rows=runtime.negativeRows();console.log(JSON.stringify({status:rows.every(row=>row.result==='PASS')?'SELF_TEST_PASS':'FAIL',tests:rows.length,rows},null,2));if(rows.some(row=>row.result!=='PASS'))process.exitCode=1;}else if(mode==='--validate-package-manifest'){const errors=validateDesign({postManifest:true}).filter(error=>error.startsWith('MANIFEST:'));console.log(JSON.stringify({status:errors.length?'FAIL':'PASS_PACKAGE_MANIFEST',errors},null,2));if(errors.length)process.exitCode=1;}else throw new Error(`UNKNOWN_MODE:${mode}`);}