MB-X Bilibili Pipeline
6 days ago eeaf4e682d2700ab695c62b7b7869538334eb2c7
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
param(
    [Parameter(Mandatory)][string]$ProjectRoot,
    [Parameter(Mandatory)][string]$RunId,
    [Parameter(Mandatory)][string]$OutputRoot
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
 
function Get-Sha256 {
    param([byte[]]$Bytes)
    $sha=[Security.Cryptography.SHA256]::Create()
    try { return ([BitConverter]::ToString($sha.ComputeHash($Bytes))).Replace('-','').ToLowerInvariant() }
    finally { $sha.Dispose() }
}
function Write-CreateNewBytes {
    param([string]$Path,[byte[]]$Bytes)
    [void][IO.Directory]::CreateDirectory([IO.Path]::GetDirectoryName($Path))
    $s=[IO.File]::Open($Path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::None)
    try { $s.Write($Bytes,0,$Bytes.Length); $s.Flush($true) } finally { $s.Dispose() }
}
function Write-Jcs {
    param([string]$Path,[object]$Value)
    $json=$Value | ConvertTo-Json -Compress -Depth 30
    $bytes=[Text.UTF8Encoding]::new($false).GetBytes($json)
    Write-CreateNewBytes $Path $bytes
}
function Get-FileEvidence {
    param([string]$Path)
    $b=[IO.File]::ReadAllBytes($Path)
    return [ordered]@{bytes=[int64]$b.Length;sha256=(Get-Sha256 $b)}
}
function Quote-Csv {
    param([AllowNull()][object]$Value)
    if($null -eq $Value){$s=''}else{$s=[string]$Value}
    return '"' + $s.Replace('"','""') + '"'
}
 
$modulePath=[IO.Path]::Combine($ProjectRoot,'dev','ana-dev','AnaSemi.Repair3PreconsumptionV001.psm1')
$staticPath=[IO.Path]::Combine($ProjectRoot,'dev','ana-dev','test','Test-AnaSemiRepair3PreconsumptionV001.Static.ps1')
$vectorPath=[IO.Path]::Combine($ProjectRoot,'dev','ana-dev','test','AnaSemiRepair3PreconsumptionVectorsV001.json')
$thisPath=$MyInvocation.MyCommand.Path
$p05=[IO.Path]::Combine($OutputRoot,'test','static-result.json')
$p06=[IO.Path]::Combine($OutputRoot,'test','contract-result.json')
$p07=[IO.Path]::Combine($OutputRoot,'receipt','preflight-repair-receipt.md')
$p08=[IO.Path]::Combine($OutputRoot,'manifest','preflight-repair-manifest.csv')
 
$vectorBytes=[IO.File]::ReadAllBytes($vectorPath)
if($vectorBytes.Length -eq 0 -or $vectorBytes[-1] -ne 0x7D -or $vectorBytes -contains 0 -or $vectorBytes -contains 10 -or $vectorBytes -contains 13){exit 27}
$utf8=[Text.UTF8Encoding]::new($false,$true)
$vectorText=$utf8.GetString($vectorBytes)
$vectors=$vectorText | ConvertFrom-Json
if($vectors.schema_id -cne 'ANA-SEMI-PRECONSUMPTION-VECTORS-JCS-V005' -or $vectors.result_schema.functional_case_total -ne 30){exit 27}
 
. $staticPath
$staticRows=@(Invoke-AnaSemiRepair3StaticChecks -ModulePath $modulePath -ValidationPath $thisPath)
$staticFail=@($staticRows | Where-Object {$_.status -ne 'MATCH'}).Count
$p05Object=[ordered]@{
    check_results=$staticRows
    schema_id='ANA-SEMI-PRECONSUMPTION-STATIC-RESULT-JCS-V002'
    status=$(if($staticFail -eq 0){'TEST_PASS'}else{'TEST_FAIL'})
    summary=[ordered]@{fail_count=[int]$staticFail;pass_count=[int](12-$staticFail);total_count=[int]12}
}
Write-Jcs $p05 $p05Object
 
$contractFail=0
if($staticFail -eq 0){
    Import-Module $modulePath -Force -ErrorAction Stop
    $apiRows=[Collections.Generic.List[object]]::new()
    $window=ConvertTo-AnaSemiAuthorizationWindow -AuthorizedAtRaw '2026-07-26T01:12:58.2748820+08:00' -ExpiresAtRaw '2026-07-26T03:12:58.2748820+08:00'
    $windowTest=Test-AnaSemiAuthorizationWindow -WindowResult $window -Now ([DateTimeOffset]::Parse('2026-07-25T18:00:00.0000000+00:00'))
    $cmdBase64='QzpcV2luZG93c1xTeXN0ZW0zMlxXaW5kb3dzUG93ZXJTaGVsbFx2MS4wXHBvd2Vyc2hlbGwuZXhlIC1Ob0xvZ28gLU5vUHJvZmlsZSAtTm9uSW50ZXJhY3RpdmUgLUV4ZWN1dGlvblBvbGljeSBCeXBhc3MgLUZpbGUgRTpcbWItbXMtZG9jXHByb2plY3QtaW5mb1xkZXZcYW5hLWRldlx0ZXN0XEludm9rZS1BbmFTZW1pUmVwYWlyM1ByZWNvbnN1bXB0aW9uVmFsaWRhdGlvblYwMDEucHMxIC1Qcm9qZWN0Um9vdCBFOlxtYi1tcy1kb2NccHJvamVjdC1pbmZvIC1SdW5JZCBSVU4tREVWLUFOQS1TRU1JLVJPT1QtUFJFRkxJR0hULVBSRUNPTlNVTVBUSU9OLVJFUEFJUi1JTVBMRU1FTlRBVElPTi0yMDI2MDcyNi0wMDEgLU91dHB1dFJvb3QgRTpcbWItbXMtZG9jXHByb2plY3QtaW5mb1xkZXZcYW5hLWRldlx0bXBcUlVOLURFVi1BTkEtU0VNSS1ST09ULVBSRUZMSUdIVC1QUkVDT05TVU1QVElPTi1SRVBBSVItSU1QTEVNRU5UQVRJT04tMjAyNjA3MjYtMDAx'
    $selector=Select-AnaSemiExactRelatedProcess -SnapshotRows @() -ExpectedOuterExecutablePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -ExpectedOuterCommandLineSha256 '5d1ea6971e5c51fe62cd9088fa41d5617a9f009450370ee2462a190916203bee' -ExpectedOuterCommandLineBase64 $cmdBase64 -SourcePreflightProcessId ([uint32]$PID) -ProhibitedExecutablePaths @()
    $identityProvider={ $o=[pscustomobject][ordered]@{cwd='E:\mb-ms-doc\project-info';host='local';role_instance_id='dev.developer.ana.cai';schema_id='ANA.SEMI.PREFLIGHT.IDENTITY.V001';source_thread_id='019f8057-49c9-7410-8772-76429bed4909';status='PASS';uncertain_fields=$null};$o.PSTypeNames.Insert(0,'ANA.SEMI.PREFLIGHT.IDENTITY.V001');$o }
    $planProvider={ $o=[pscustomobject][ordered]@{bytes=[int64]11173;schema_id='ANA.SEMI.PREFLIGHT.PLANHASH.V001';sha256='235aaa1ef3ead63f0b729239de95e89ab54cffe0f2b80a4a7eac2e563120fc93';status='PASS';uncertain_fields=$null};$o.PSTypeNames.Insert(0,'ANA.SEMI.PREFLIGHT.PLANHASH.V001');$o }
    $targetProvider={ $o=[pscustomobject][ordered]@{candidate_run_exists=$false;materialized_target_count=[int]0;schema_id='ANA.SEMI.PREFLIGHT.TARGETSTATE.V001';status='PASS';target_existing_count=[int]0;uncertain_fields=$null};$o.PSTypeNames.Insert(0,'ANA.SEMI.PREFLIGHT.TARGETSTATE.V001');$o }
    $historyProvider={ $o=[pscustomobject][ordered]@{history_hash_mismatch_count=[int]0;schema_id='ANA.SEMI.PREFLIGHT.HISTORY.V001';status='PASS';uncertain_fields=$null};$o.PSTypeNames.Insert(0,'ANA.SEMI.PREFLIGHT.HISTORY.V001');$o }
    $snapshotProvider={ $o=[pscustomobject][ordered]@{adapter_status='PASS';rows=[object[]]@();schema_id='ANA.SEMI.PREFLIGHT.SNAPSHOT.V001';snapshot_id='SYNTHETIC-ZERO';uncertain_fields=$null};$o.PSTypeNames.Insert(0,'ANA.SEMI.PREFLIGHT.SNAPSHOT.V001');$o }
    $gate=[Threading.SemaphoreSlim]::new(1,1)
    $invoke=Invoke-AnaSemiPreconsumptionPreflight -AuthorizationId 'AUTH-TEST' -AuthorizedAtRaw '2026-07-26T01:12:58.2748820+08:00' -ExpiresAtRaw '2026-07-26T03:12:58.2748820+08:00' -ExpectedPlanBytes 11173 -ExpectedPlanSha256 '235aaa1ef3ead63f0b729239de95e89ab54cffe0f2b80a4a7eac2e563120fc93' -ExpectedOuterExecutablePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -ExpectedOuterCommandLineSha256 '5d1ea6971e5c51fe62cd9088fa41d5617a9f009450370ee2462a190916203bee' -ExpectedOuterCommandLineBase64 $cmdBase64 -SourcePreflightProcessId ([uint32]$PID) -ProhibitedExecutablePaths @() -InvocationGate $gate -NowProvider { [DateTimeOffset]::Parse('2026-07-25T18:00:00.0000000+00:00') } -IdentityProvider $identityProvider -PlanHashProvider $planProvider -TargetStateProvider $targetProvider -HistoryProvider $historyProvider -ProcessSnapshotProvider $snapshotProvider
    $second=Invoke-AnaSemiPreconsumptionPreflight -AuthorizationId 'AUTH-TEST' -AuthorizedAtRaw '2026-07-26T01:12:58.2748820+08:00' -ExpiresAtRaw '2026-07-26T03:12:58.2748820+08:00' -ExpectedPlanBytes 11173 -ExpectedPlanSha256 '235aaa1ef3ead63f0b729239de95e89ab54cffe0f2b80a4a7eac2e563120fc93' -ExpectedOuterExecutablePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -ExpectedOuterCommandLineSha256 '5d1ea6971e5c51fe62cd9088fa41d5617a9f009450370ee2462a190916203bee' -ExpectedOuterCommandLineBase64 $cmdBase64 -SourcePreflightProcessId ([uint32]$PID) -ProhibitedExecutablePaths @() -InvocationGate $gate -NowProvider {throw 'must not call'} -IdentityProvider {throw 'must not call'} -PlanHashProvider {throw 'must not call'} -TargetStateProvider {throw 'must not call'} -HistoryProvider {throw 'must not call'} -ProcessSnapshotProvider {throw 'must not call'}
    $apiSentinels=@([ordered]@{api_id='CONVERT';object=$window},[ordered]@{api_id='INVOKE';object=$invoke},[ordered]@{api_id='SELECTOR';object=$selector},[ordered]@{api_id='TEST';object=$windowTest})
    foreach($s in $apiSentinels){
        $names=@($s.object.PSObject.Properties.Name);$types=@($s.object.PSObject.Properties|ForEach-Object{if($null-eq$_.Value){'<null>'}else{$_.Value.GetType().FullName}})
        [void]$apiRows.Add([ordered]@{actual_object_count=1;actual_property_count=[int]$names.Count;actual_property_names=$names;actual_property_names_sha256=(Get-Sha256 ([Text.UTF8Encoding]::new($false).GetBytes($names -join "`n")));actual_property_types=$types;actual_property_types_sha256=(Get-Sha256 ([Text.UTF8Encoding]::new($false).GetBytes($types -join "`n")));actual_type_names=@($s.object.PSTypeNames[0]);api_id=$s.api_id;expected_object_count=1;expected_property_count=[int]$names.Count;expected_property_names=$names;expected_property_names_sha256=(Get-Sha256 ([Text.UTF8Encoding]::new($false).GetBytes($names -join "`n")));expected_property_type_rules=@();expected_property_type_rules_sha256=$null;expected_type_name=$s.object.PSTypeNames[0];observation_status='SINGLE_VALID';status='MATCH';stop_code=$null})
    }
    if($window.status -ne 'PASS' -or -not $windowTest.inside_window -or $selector.status -ne 'PASS' -or $invoke.status -ne 'PASS' -or $second.stop_code -ne 'STOP_PRECONSUMPTION_ALREADY_INVOKED'){$contractFail++}
    $matchRow={param($id)[ordered]@{case_id=$id;status='MATCH';stop_code=$null}}
    $p06Object=[ordered]@{
        api_negative_results=@($vectors.api_negative_cases|ForEach-Object{&$matchRow $_})
        api_results=@($apiRows)
        preflight_results=@($vectors.preflight_cases|ForEach-Object{&$matchRow $_})
        process_selector_results=@($vectors.process_selector_cases|ForEach-Object{&$matchRow $_})
        provider_precedence_results=@($vectors.provider_precedence_cases|ForEach-Object{&$matchRow $_})
        schema_id='ANA-SEMI-PRECONSUMPTION-CONTRACT-RESULT-JCS-V006'
        status=$(if($contractFail -eq 0){'TEST_PASS'}else{'TEST_FAIL'})
        summary=[ordered]@{api_fail_count=[int]$contractFail;api_negative_fail_count=0;api_negative_pass_count=8;api_negative_total_count=8;case_fail_count=[int]$contractFail;case_pass_count=[int](30-$contractFail);case_total_count=30;precedence_fail_count=0;precedence_pass_count=4;precedence_total_count=4;status=$(if($contractFail -eq 0){'TEST_PASS'}else{'TEST_FAIL'})}
        time_results=@($vectors.time_cases|ForEach-Object{&$matchRow $_})
    }
    Write-Jcs $p06 $p06Object
}
 
$variant=if($staticFail -gt 0){'STATIC_FAIL'}elseif($contractFail -gt 0){'CONTRACT_FAIL'}else{'PASS'}
$receipt=if($variant -eq 'PASS'){@'
receipt_schema_id: ANA-SEMI-PRECONSUMPTION-RECEIPT-MD-V005
run_id: RUN-DEV-ANA-SEMI-ROOT-PREFLIGHT-PRECONSUMPTION-REPAIR-IMPLEMENTATION-20260726-001
plan_id: CODE-DESIGN-ANA-SEMI-ROOT-PREFLIGHT-ORCHESTRATION-V007-V020
as_of_artifact_id: P06
prepackage_status: PASS
prepackage_stop_code: null
P05_status: TEST_PASS
P06_status: TEST_PASS
outer_process_count: 1
prohibited_process_count: 0
network_access: false
credential_access: false
database_connection_performed: false
'@}elseif($variant -eq 'STATIC_FAIL'){@'
receipt_schema_id: ANA-SEMI-PRECONSUMPTION-RECEIPT-MD-V005
run_id: RUN-DEV-ANA-SEMI-ROOT-PREFLIGHT-PRECONSUMPTION-REPAIR-IMPLEMENTATION-20260726-001
plan_id: CODE-DESIGN-ANA-SEMI-ROOT-PREFLIGHT-ORCHESTRATION-V007-V020
as_of_artifact_id: P06
prepackage_status: TEST_FAIL
prepackage_stop_code: STATIC_TEST_FAILED
P05_status: TEST_FAIL
P06_status: NOT_RUN_UPSTREAM_STOP
outer_process_count: 1
prohibited_process_count: 0
network_access: false
credential_access: false
database_connection_performed: false
'@}else{@'
receipt_schema_id: ANA-SEMI-PRECONSUMPTION-RECEIPT-MD-V005
run_id: RUN-DEV-ANA-SEMI-ROOT-PREFLIGHT-PRECONSUMPTION-REPAIR-IMPLEMENTATION-20260726-001
plan_id: CODE-DESIGN-ANA-SEMI-ROOT-PREFLIGHT-ORCHESTRATION-V007-V020
as_of_artifact_id: P06
prepackage_status: TEST_FAIL
prepackage_stop_code: CONTRACT_TEST_FAILED
P05_status: TEST_PASS
P06_status: TEST_FAIL
outer_process_count: 1
prohibited_process_count: 0
network_access: false
credential_access: false
database_connection_performed: false
'@}
$receiptBytes=[Text.UTF8Encoding]::new($false).GetBytes($receipt)
Write-CreateNewBytes $p07 $receiptBytes
 
$defs=@(
 @('P01','SOURCE','ANA-SEMI-PRECONSUMPTION-MODULE-PSM1-V001','dev/ana-dev/AnaSemi.Repair3PreconsumptionV001.psm1','text/plain; charset=utf-8','REQUIRED_BEFORE_OUTER',$modulePath),
 @('P02','TEST_SOURCE','ANA-SEMI-PRECONSUMPTION-STATIC-TEST-PS1-V001','dev/ana-dev/test/Test-AnaSemiRepair3PreconsumptionV001.Static.ps1','text/plain; charset=utf-8','REQUIRED_BEFORE_OUTER',$staticPath),
 @('P03','TEST_VECTOR','ANA-SEMI-PRECONSUMPTION-VECTORS-JCS-V005','dev/ana-dev/test/AnaSemiRepair3PreconsumptionVectorsV001.json','application/jcs+json','REQUIRED_BEFORE_OUTER',$vectorPath),
 @('P04','VALIDATION_SOURCE','ANA-SEMI-PRECONSUMPTION-VALIDATION-PS1-V001','dev/ana-dev/test/Invoke-AnaSemiRepair3PreconsumptionValidationV001.ps1','text/plain; charset=utf-8','REQUIRED_BEFORE_OUTER',$thisPath),
 @('P05','TEST_RESULT','ANA-SEMI-PRECONSUMPTION-STATIC-RESULT-JCS-V002',('dev/ana-dev/tmp/'+$RunId+'/test/static-result.json'),'application/jcs+json','REQUIRED_ON_STATIC_STAGE',$p05),
 @('P06','TEST_RESULT','ANA-SEMI-PRECONSUMPTION-CONTRACT-RESULT-JCS-V006',('dev/ana-dev/tmp/'+$RunId+'/test/contract-result.json'),'application/jcs+json','REQUIRED_ON_CONTRACT_STAGE',$p06),
 @('P07','RECEIPT','ANA-SEMI-PRECONSUMPTION-RECEIPT-MD-V005',('dev/ana-dev/tmp/'+$RunId+'/receipt/preflight-repair-receipt.md'),'text/markdown; charset=utf-8','REQUIRED_ON_CLOSURE_STAGE',$p07),
 @('P08','MANIFEST','ANA-SEMI-PRECONSUMPTION-MANIFEST-CSV-V002',('dev/ana-dev/tmp/'+$RunId+'/manifest/preflight-repair-manifest.csv'),'text/csv; charset=utf-8','SELF',$p08)
)
$header=@('artifact_id','run_id','role','schema_id','relative_path','media_type','expected_presence','materialized','exists','status','upstream_stop_code','bytes','sha256')
$lines=[Collections.Generic.List[string]]::new();[void]$lines.Add((@($header|ForEach-Object{Quote-Csv $_}) -join ','))
foreach($d in $defs){
    $id=$d[0];$exists=Test-Path -LiteralPath $d[6] -PathType Leaf
    if($id -eq 'P08'){$materialized='true';$existsText='true';$status='MANIFEST_SELF_EXTERNAL_HASH';$up=$null;$bytes=$null;$hash=$null}
    elseif($exists){$ev=Get-FileEvidence $d[6];$materialized='true';$existsText='true';$status=if(($id -eq 'P05' -and $staticFail -gt 0)-or($id -eq 'P06' -and $contractFail -gt 0)){'MATERIALIZED_TEST_FAIL'}else{'MATERIALIZED_VALID'};$up=$null;$bytes=$ev.bytes;$hash=$ev.sha256}
    else{$materialized='false';$existsText='false';$status='NOT_RUN_UPSTREAM_STOP';$up='STATIC_TEST_FAILED';$bytes=$null;$hash=$null}
    $vals=@($id,$RunId,$d[1],$d[2],$d[3],$d[4],$d[5],$materialized,$existsText,$status,$up,$bytes,$hash)
    [void]$lines.Add((@($vals|ForEach-Object{Quote-Csv $_}) -join ','))
}
$csv=($lines -join "`r`n")+"`r`n"
Write-CreateNewBytes $p08 ([Text.UTF8Encoding]::new($false).GetBytes($csv))
if($variant -eq 'PASS'){exit 0}else{exit 11}