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
45
46
47
48
49
50
51
52
53
54
55
56
import fs from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import {spawnSync} from 'node:child_process';
import {pathToFileURL} from 'node:url';
import * as runtime from './detail_strengthening_p0_open_gap_evidence_acquisition_runtime_20260731.mjs';
import {parseAuthorityCsv,serializeCsv,serializeManifestDataRows,validateAppendStableManifest} from './artifact_manifest_append_stable_parser_repair001_20260730.mjs';
 
const {ROOT,CASE_ID,BASE,AUDIT_REPORT,MANIFEST,ANOMALY_AUTHORITY,ORIGINAL_TRIAGE_RUN,DESIGN_ID,DESIGN_RUN,DESIGN_BATCH,EXEC_RUN,SCRIPT,GAP_AUTHORITY,QUERY_AUTHORITY,SOURCE_POLICY,METHOD_AUTHORITY,FORMAL_ENTRY_AUTHORITY,CONTRACT,SCHEMA,EXEC_SET,PROTECTED_INVENTORY,NEXT_PLAN,DECISION,MAP}=runtime;
const BUILDER='dev/ana-dev/detail_strengthening_p0_open_gap_evidence_acquisition_design_20260731.mjs';
const FORMAL_DESIGN='ana-doc/机器人案例/案例分析设计.md';
const NEGATIVE=`${BASE}/evidence/detail_strengthening_p0_open_gap_evidence_acquisition_negative_results_20260731.csv`;
const DESIGN_BASELINE=`${BASE}/evidence/detail_strengthening_p0_open_gap_evidence_acquisition_design_baseline_20260731.csv`;
const DESIGN_REQUEST=`${BASE}/manifest/review_request_detail_strengthening_p0_open_gap_evidence_acquisition_design_20260731.md`;
const DESIGN_SET=`${BASE}/manifest/detail_strengthening_p0_open_gap_evidence_acquisition_design_artifact_set_20260731.csv`;
const DESIGN_VALIDATION=`${BASE}/manifest/detail_strengthening_p0_open_gap_evidence_acquisition_design_validation_20260731.csv`;
const SOURCE_AUDIT=runtime.SOURCE_AUDIT;
const SOURCE_AUDIT_EXPECTED={lines:55,bytes:7049,sha256:'d92c8859526cc74ef6518fe109c87b4ed189799be17f0b469305b7980dc683a6'};
const DESIGN_PATHS=[SCRIPT,BUILDER,GAP_AUTHORITY,QUERY_AUTHORITY,SOURCE_POLICY,METHOD_AUTHORITY,FORMAL_ENTRY_AUTHORITY,CONTRACT,SCHEMA,EXEC_SET,NEGATIVE,DESIGN_BASELINE,DESIGN_REQUEST,DESIGN_SET,DESIGN_VALIDATION];
const DESIGN_SET_COLUMNS=['artifact_id','batch_id','run_id','generation_order','artifact_path','artifact_role','identity_policy'];
const NEGATIVE_COLUMNS=['test_id','mutation_applied','oracle','expected','expected_code','actual','actual_codes','result'];
const DESIGN_BASELINE_COLUMNS=['baseline_id','identity_type','artifact_path','rows','bytes','sha256','mtime','required_state'];
const VALIDATION_COLUMNS=runtime.VALIDATION_COLUMNS;
const abs=value=>path.join(ROOT,...value.split('/'));
const sha=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:sha(buffer),mtime:new Date(Math.trunc(stat.mtimeMs)).toISOString(),mtimeMs:Math.trunc(stat.mtimeMs)};};
const write=(file,text)=>{fs.mkdirSync(path.dirname(abs(file)),{recursive:true});fs.writeFileSync(abs(file),text,'utf8');};
 
function auditEntry(auditId){const all=normalize(fs.readFileSync(abs(AUDIT_REPORT))).split('\n'),start=all.findIndex(line=>line.startsWith('### ')&&line.includes(auditId));if(start<0)return null;let end=all.length;for(let i=start+1;i<all.length;i++)if(all[i].startsWith('### ')){end=i;break;}while(end>start&&all[end-1]==='')end--;const buffer=Buffer.from(`${all.slice(start,end).join('\n')}\n`);return{start:start+1,end,lines:end-start,bytes:buffer.length,sha256:sha(buffer),text:buffer.toString('utf8')};}
 
function formalEntryRow(){const all=normalize(fs.readFileSync(abs(FORMAL_DESIGN))).split('\n'),heading=`## ${DESIGN_ID}(2026-07-31)`,start=all.findIndex(line=>line===heading);if(start<0)throw new Error('FORMAL_DESIGN_HEADING_MISSING');let end=all.length;for(let i=start+1;i<all.length;i++)if(all[i].startsWith('## ')){end=i;break;}while(end>start&&all[end-1]==='')end--;const buffer=Buffer.from(`${all.slice(start,end).join('\n')}\n`);return{entry_id:'P0ACQ-FORMAL-001',design_id:DESIGN_ID,artifact_path:FORMAL_DESIGN,start_line:String(start+1),end_line:String(end),lines:String(end-start),bytes:String(buffer.length),sha256:sha(buffer),normalization:'UTF8_NO_BOM_LF_JOIN_LINES_WITH_TERMINAL_LF_V1',required_markers:'CODEX_WEB_SEARCH_QUERY_AND_OPEN|14 条新查询|DISCOVERY_ONLY_GAP_REMAINS_OPEN|PENDING_VERIFICATION_MAX_NO_UPGRADE|NO_AUTOMATIC_FORMAL_POOL_CHANGE|NO_UPGRADE|network=0|NOT_RELEASED'};}
 
function protectedRows(){const paths=readCsv(PROTECTED_INVENTORY).rows.map(row=>row.artifact_path);if(paths.length!==49||new Set(paths).size!==49)throw new Error('PROTECTED_PATH_SET');return paths.map(artifact_path=>({artifact_path,...identity(artifact_path)}));}
function currentManifest(){const buffer=fs.readFileSync(abs(MANIFEST)),state=validateAppendStableManifest(buffer,{anomalyRows:readCsv(ANOMALY_AUTHORITY).rows,originalRunId:ORIGINAL_TRIAGE_RUN,originalRunExpectedRows:11});if(state.errors.length)throw new Error(`MANIFEST_CURRENT:${state.errors.join('|')}`);return{buffer,state,identity:{bytes:buffer.length,sha256:sha(buffer),mtime:new Date(Math.trunc(fs.statSync(abs(MANIFEST)).mtimeMs)).toISOString()}};}
 
function baselineRows(){const source=auditEntry(SOURCE_AUDIT),manifest=currentManifest(),rows=[];if(!source)throw new Error('SOURCE_AUDIT_MISSING');rows.push({baseline_id:'P0ACQ-DESIGN-BASE-001',identity_type:'SOURCE_PASS_AUDIT_ENTRY',artifact_path:`${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_PASS_ORACLE_REPAIR_EXECUTION_ENTRY'});rows.push({baseline_id:'P0ACQ-DESIGN-BASE-002',identity_type:'RAW_MANIFEST_PREFIX_BEFORE_DESIGN',artifact_path:MANIFEST,rows:String(manifest.state.logicalDataRows),bytes:String(manifest.identity.bytes),sha256:manifest.identity.sha256,mtime:manifest.identity.mtime,required_state:'EXACT_APPEND_ONLY_PREFIX'});for(const file of [runtime.TARGET_AUTHORITY,runtime.PRIOR_GAP_AUTHORITY,NEXT_PLAN,DECISION,MAP,PROTECTED_INVENTORY,ANOMALY_AUTHORITY,SCRIPT,BUILDER,FORMAL_DESIGN]){const id=identity(file);rows.push({baseline_id:`P0ACQ-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:`P0ACQ-DESIGN-BASE-${String(rows.length+1).padStart(3,'0')}`,identity_type:'PROTECTED49_CURRENT_IDENTITY',artifact_path:item.artifact_path,rows:String(item.rows),bytes:String(item.bytes),sha256:item.sha256,mtime:item.mtime,required_state:'NO_MUTATION'});return rows;}
 
function designSetRows(){const roles=['RUNTIME','DESIGN_BUILDER','GAP_AUTHORITY','QUERY_AUTHORITY','SOURCE_POLICY','METHOD_AUTHORITY','FORMAL_ENTRY_AUTHORITY','RUNTIME_CONTRACT','SCHEMA_AUTHORITY','EXECUTION_EXACT_SET','NEGATIVE_RESULTS','DESIGN_BASELINE','DESIGN_REVIEW_REQUEST','DESIGN_EXACT_SET','VALIDATION_LAST'];return DESIGN_PATHS.map((artifactPath,index)=>({artifact_id:`P0ACQ-DESIGN-SET-${String(index+1).padStart(3,'0')}`,batch_id:DESIGN_BATCH,run_id:DESIGN_RUN,generation_order:String(index+1),artifact_path:artifactPath,artifact_role:roles[index],identity_policy:'EXACT_SHA256_BYTES_FILESYSTEM_MTIME_MS'}));}
 
function requestText(){return`# P0 开放缺口定向主来源证据采集详细设计审核请求\n\ncase_id=${CASE_ID}\naction_id=NEXT-ROBOT-045-DESIGN-001\ndesign_id=${DESIGN_ID}\nsource_audit=${SOURCE_AUDIT}\nrequested_release_audit=${runtime.RELEASE_AUDIT}\nrequested_batch=${runtime.EXEC_BATCH}\nrequested_run=${runtime.EXEC_RUN}\n\n设计冻结 7 gap、10 occurrence、14 targeted queries、15 source/network/boundary policies、39 runtime contracts、12 schemas、25-item execution exact-set 和 18 项真实 mutation self-test。只在独立审核精确 PASS/release 后先生成 baseline+method preflight,再允许公共网页只读 search/open;网络前 staging=0、execution outputs=0、execution manifest rows=0。候选效果恒为 DISCOVERY_ONLY_GAP_REMAINS_OPEN / PENDING_VERIFICATION_MAX_NO_UPGRADE / NO_AUTOMATIC_FORMAL_POOL_CHANGE / NO_UPGRADE,formal map46 只读。\n`;}
 
function validationRows(){const source=auditEntry(SOURCE_AUDIT),negative=runtime.negativeRows(),preview=spawnSync(process.execPath,[abs(SCRIPT),'--preview'],{cwd:ROOT,encoding:'utf8'}),self=spawnSync(process.execPath,[abs(SCRIPT),'--self-test'],{cwd:ROOT,encoding:'utf8'}),manifest=currentManifest(),checks=[
 ['SOURCE_AUDIT',`${SOURCE_AUDIT_EXPECTED.lines}/${SOURCE_AUDIT_EXPECTED.bytes}/${SOURCE_AUDIT_EXPECTED.sha256}`,source?`${source.lines}/${source.bytes}/${source.sha256}`:'MISSING'],
 ['GAPS','7',String(runtime.gapRows().length)],['OCCURRENCES','10',String(readCsv(runtime.TARGET_AUTHORITY).rows.length)],['NEXT_PLAN','7',String(readCsv(NEXT_PLAN).rows.length)],['DECISION','7',String(readCsv(DECISION).rows.length)],['MAP','46',String(readCsv(MAP).rows.length)],['QUERIES','14',String(runtime.queryRows().length)],['POLICIES','15',String(runtime.policyRows().length)],['METHODS','1',String(runtime.methodRows().length)],['CONTRACTS','39',String(runtime.contractRows().length)],['SCHEMAS','12',String(runtime.schemaRows().length)],['EXEC_SET','25',String(runtime.executionSetRows().length)],['NEGATIVE_PASS','18',String(negative.filter(row=>row.result==='PASS').length)],['PROTECTED','49',String(protectedRows().length)],['RUNTIME_NODE','0',String(spawnSync(process.execPath,['--check',abs(SCRIPT)]).status??1)],['BUILDER_NODE','0',String(spawnSync(process.execPath,['--check',abs(BUILDER)]).status??1)],['PREVIEW','0',String(preview.status??1)],['SELF_TEST','0',String(self.status??1)],['MANIFEST_GLOBAL_ERRORS','0',String(manifest.state.errors.length)],['EXECUTION_FILES','0',String(runtime.GENERATED.filter(file=>fs.existsSync(abs(file))).length)],['EXECUTION_MANIFEST_ROWS','0',String(manifest.state.logicalRows.filter(row=>row.run_id===EXEC_RUN).length)],['BOUNDARY','NO_UPGRADE','NO_UPGRADE']
];const rows=checks.map((item,index)=>({check_id:`P0ACQ-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:'P0ACQ_FAIL_CLOSED_DESIGN_ORACLE'}));rows.push({check_id:'P0ACQ-DESIGN-VAL-023',check_description:'SELF_REFERENCE_EXCLUDED',expected:'SELF_REFERENCE_EXCLUDED',actual:'SELF_REFERENCE_EXCLUDED',status:'INFO',evidence:'validation and manifest identity postchecked'});return rows;}
 
function manifestRows(paths){return paths.map(file=>{const id=identity(file);return{case_id:CASE_ID,run_id:DESIGN_RUN,batch_id:DESIGN_BATCH,artifact_path:file,sha256:id.sha256,bytes:String(id.bytes),mtime:id.mtime};});}
 
function build(){const pre=currentManifest(),targets=DESIGN_PATHS.slice(2);if(targets.some(file=>fs.existsSync(abs(file))))throw new Error('DESIGN_TARGET_EXISTS');write(GAP_AUTHORITY,serializeCsv(runtime.gapRows(),runtime.GAP_COLUMNS));write(QUERY_AUTHORITY,serializeCsv(runtime.queryRows(),runtime.QUERY_COLUMNS));write(SOURCE_POLICY,serializeCsv(runtime.policyRows(),runtime.POLICY_COLUMNS));write(METHOD_AUTHORITY,serializeCsv(runtime.methodRows(),runtime.METHOD_COLUMNS));write(FORMAL_ENTRY_AUTHORITY,serializeCsv([formalEntryRow()],runtime.FORMAL_ENTRY_COLUMNS));write(CONTRACT,serializeCsv(runtime.contractRows(),runtime.CONTRACT_COLUMNS));write(SCHEMA,serializeCsv(runtime.schemaRows(),runtime.SCHEMA_COLUMNS));write(EXEC_SET,serializeCsv(runtime.executionSetRows(),runtime.EXEC_SET_COLUMNS));write(NEGATIVE,serializeCsv(runtime.negativeRows(),NEGATIVE_COLUMNS));write(DESIGN_BASELINE,serializeCsv(baselineRows(),DESIGN_BASELINE_COLUMNS));write(DESIGN_REQUEST,requestText());write(DESIGN_SET,serializeCsv(designSetRows(),DESIGN_SET_COLUMNS));const validation=validationRows();write(DESIGN_VALIDATION,serializeCsv(validation,VALIDATION_COLUMNS));if(validation.some(row=>row.status==='FAIL'))throw new Error(`DESIGN_VALIDATION_FAIL:${validation.filter(row=>row.status==='FAIL').map(row=>row.check_description).join('|')}`);const rows=manifestRows(DESIGN_PATHS),candidate=Buffer.concat([pre.buffer,Buffer.from(serializeManifestDataRows(rows),'utf8')]),state=validateAppendStableManifest(candidate,{anomalyRows:readCsv(ANOMALY_AUTHORITY).rows,frozenPrefix:{bytes:pre.buffer.length,sha256:sha(pre.buffer)},originalRunId:ORIGINAL_TRIAGE_RUN,originalRunExpectedRows:11,expectedRunRows:rows,expectedRunId:DESIGN_RUN,expectedPhysicalDelta:15,filesystemIdentity:{runIds:[DESIGN_RUN],get:file=>identity(file)}});if(state.errors.length)throw new Error(`MANIFEST_CANDIDATE:${state.errors.join('|')}`);fs.writeFileSync(abs(MANIFEST),candidate);const errors=validate();if(errors.length)throw new Error(`POSTWRITE:${errors.join('|')}`);return{status:'DESIGN_BUILT_PENDING_INDEPENDENT_REVIEW',gaps:7,occurrences:10,queries:14,contracts:39,schemas:12,execution_exact_set:25,negative_pass:18,design_manifest_rows:15,execution_outputs:0};}
 
function validate(){const errors=[],source=auditEntry(SOURCE_AUDIT);if(!source||source.lines!==SOURCE_AUDIT_EXPECTED.lines||source.bytes!==SOURCE_AUDIT_EXPECTED.bytes||source.sha256!==SOURCE_AUDIT_EXPECTED.sha256)errors.push('SOURCE_AUDIT');errors.push(...runtime.designInputErrors({requireRelease:false,expectNoExecution:true,expectDesignManifest:true}));const baseline=readCsv(DESIGN_BASELINE).rows,prefix=baseline.find(row=>row.identity_type==='RAW_MANIFEST_PREFIX_BEFORE_DESIGN'),current=fs.readFileSync(abs(MANIFEST));if(!prefix||current.length<Number(prefix.bytes)||sha(current.subarray(0,Number(prefix.bytes)))!==prefix.sha256)errors.push('MANIFEST_PREFIX');const rows=currentManifest().state.logicalRows.filter(row=>row.run_id===DESIGN_RUN);if(rows.length!==15||new Set(rows.map(row=>row.artifact_path)).size!==15)errors.push(`DESIGN_ROWS:${rows.length}`);for(const row of rows){const id=identity(row.artifact_path);if(row.case_id!==CASE_ID||row.batch_id!==DESIGN_BATCH||row.sha256!==id.sha256||row.bytes!==String(id.bytes)||Date.parse(row.mtime)!==id.mtimeMs)errors.push(`DESIGN_IDENTITY:${row.artifact_path}`);}const negative=readCsv(NEGATIVE).rows,validation=readCsv(DESIGN_VALIDATION).rows;if(negative.length!==18||negative.some(row=>row.result!=='PASS'))errors.push('NEGATIVE');if(validation.length!==23||validation.some(row=>row.status==='FAIL'))errors.push('VALIDATION');return[...new Set(errors)];}
 
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=validate();console.log(JSON.stringify({status:errors.length?'FAIL':'PASS_DESIGN_PACKAGE',errors},null,2));if(errors.length)process.exitCode=1;}else if(mode==='--validate-package-manifest'){const errors=currentManifest().state.errors;console.log(JSON.stringify({status:errors.length?'FAIL':'PASS_APPEND_STABLE_MANIFEST',errors},null,2));if(errors.length)process.exitCode=1;}else throw new Error(`UNKNOWN_MODE:${mode}`);}