[CmdletBinding()] param() $ErrorActionPreference = 'Stop' $backendRoot = Split-Path -Parent $PSScriptRoot $workspaceRoot = Split-Path -Parent $backendRoot $failures = [System.Collections.Generic.List[string]]::new() $checked = 0 function Add-Failure([string]$message) { $script:failures.Add($message) } function Read-RequiredText([string]$path) { $script:checked++ if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { Add-Failure "缺少索引文件: $path" return '' } return Get-Content -LiteralPath $path -Raw } function Assert-Contains([string]$text, [string]$needle, [string]$scope) { $script:checked++ if (-not $text.Contains($needle)) { Add-Failure "$scope 未索引: $needle" } } function Assert-Matches([string]$text, [string]$pattern, [string]$scope) { $script:checked++ if ($text -notmatch $pattern) { Add-Failure "$scope 未匹配合同: $pattern" } } function Assert-Path([string]$relativePath) { $script:checked++ $absolutePath = Join-Path $workspaceRoot $relativePath if (-not (Test-Path -LiteralPath $absolutePath)) { Add-Failure "索引入口不存在: $relativePath" } } function Assert-MarkdownTableUnique([string]$path, [string]$heading, [int]$keyColumn, [string]$scope) { $script:checked++ $lines = Get-Content -LiteralPath $path $inside = $false $keys = [System.Collections.Generic.List[string]]::new() foreach ($line in $lines) { if ($line -eq $heading) { $inside = $true continue } if ($inside -and $line.StartsWith('## ')) { break } if (-not $inside -or -not $line.StartsWith('|')) { continue } $cells = @($line.Trim('|').Split('|') | ForEach-Object { $_.Trim() }) if ($keyColumn -ge $cells.Count -or $cells[$keyColumn] -match '^:?-{3,}:?$') { continue } $keys.Add($cells[$keyColumn]) } $keys | Group-Object | Where-Object Count -gt 1 | ForEach-Object { Add-Failure "$scope 存在重复索引键: $($_.Name)($($_.Count) 次)" } } $rootAgentsPath = Join-Path $workspaceRoot 'AGENTS.md' $adminAgentsPath = Join-Path $workspaceRoot 'apps/admin-web/AGENTS.md' $clientAgentsPath = Join-Path $workspaceRoot 'apps/client-web/AGENTS.md' $pdaAgentsPath = Join-Path $workspaceRoot 'apps/pda-uniapp/AGENTS.md' $overseasPdaAgentsPath = Join-Path $workspaceRoot 'apps/pda-overseas-uniapp/AGENTS.md' $backendAgentsPath = Join-Path $backendRoot 'AGENTS.md' $rootAgents = Read-RequiredText $rootAgentsPath $adminAgents = Read-RequiredText $adminAgentsPath $clientAgents = Read-RequiredText $clientAgentsPath $pdaAgents = Read-RequiredText $pdaAgentsPath $overseasPdaAgents = Read-RequiredText $overseasPdaAgentsPath $backendAgents = Read-RequiredText $backendAgentsPath # 部署单元必须能从根索引进入。 Get-ChildItem -LiteralPath (Join-Path $workspaceRoot 'apps') -Directory | Sort-Object Name | ForEach-Object { Assert-Contains $rootAgents "apps/$($_.Name)/" '根 AGENTS.md' } Assert-Contains $rootAgents 'baiyue-firstleg/' '根 AGENTS.md' Assert-MarkdownTableUnique $rootAgentsPath '## 项目快速入口' 0 '根项目表' Assert-MarkdownTableUnique $rootAgentsPath '## 项目快速入口' 1 '根项目目录' Assert-MarkdownTableUnique $rootAgentsPath '## 业务快速路由表' 0 '根业务路由表' Assert-MarkdownTableUnique $rootAgentsPath '## 原型入口索引' 0 '根原型表' # 前端当前全部业务模块必须能从应用级索引命中。 $applications = @( @{ Root = 'apps/admin-web/src/modules'; Text = $adminAgents; Scope = 'Admin AGENTS.md' }, @{ Root = 'apps/client-web/src/modules'; Text = $clientAgents; Scope = 'Client AGENTS.md' } ) foreach ($application in $applications) { foreach ($moduleDirectory in (Get-ChildItem -LiteralPath (Join-Path $workspaceRoot $application.Root) -Directory | Sort-Object Name)) { Assert-Contains $application.Text "modules/$($moduleDirectory.Name)/" $application.Scope } } # 国内/海外 PDA 只检查 pages.json 正式注册页面,不把空目录或未注册原型纳入索引。 $pdaApplications = @( @{ Pages = 'apps/pda-uniapp/pages.json'; Text = $pdaAgents; Scope = 'PDA AGENTS.md'; Prefix = '' }, @{ Pages = 'apps/pda-overseas-uniapp/src/pages.json'; Text = $overseasPdaAgents; Scope = 'Overseas PDA AGENTS.md'; Prefix = 'src/' } ) foreach ($pdaApplication in $pdaApplications) { $pagesJsonPath = Join-Path $workspaceRoot $pdaApplication.Pages $pagesJson = Get-Content -LiteralPath $pagesJsonPath -Raw | ConvertFrom-Json $registeredPageRoots = $pagesJson.pages.path | ForEach-Object { $parts = $_ -split '/'; "$($pdaApplication.Prefix)$($parts[0])/$($parts[1])/" } | Sort-Object -Unique foreach ($pageRoot in $registeredPageRoots) { Assert-Contains $pdaApplication.Text $pageRoot $pdaApplication.Scope } } # 文档声明为正式开放的 Client 海外仓页面必须仍是 active 路由。 $clientOverseasRoutesPath = Join-Path $workspaceRoot 'apps/client-web/src/modules/overseas-warehouse/routes.ts' $clientOverseasRoutes = Read-RequiredText $clientOverseasRoutesPath foreach ($route in @( 'overseas-warehouse/labeling', 'overseas-warehouse/storage-transfer', 'overseas-warehouse/cargo-disposal', 'overseas-warehouse/return-relabel' )) { $routePattern = "(?s)path:\s*'$([regex]::Escape($route))'.{0,500}?release:\s*'active'" Assert-Matches $clientOverseasRoutes $routePattern "Client 正式路由 $route" } # 根索引中的海外浏览器 API 必须被海外 Gateway 显式覆盖。 $overseasGatewayConfigPath = Join-Path $backendRoot 'gateway-overseas/src/main/resources/application-overseas.yml' $overseasGatewayConfig = Read-RequiredText $overseasGatewayConfigPath foreach ($gatewayPath in @( '/api/merchant/v1/last-leg/**', '/api/platform/v1/last-leg/**', '/api/merchant/v1/warehouses/**', '/api/platform/v1/warehouse-admin/**' )) { Assert-Contains $overseasGatewayConfig $gatewayPath 'Overseas Gateway 路由' } Assert-Contains $overseasGatewayConfig 'uri: lb://lastleg-service' 'Overseas Gateway Lastleg 路由' Assert-Contains $overseasGatewayConfig 'uri: lb://warehouse-overseas-service' 'Overseas Gateway Warehouse 路由' # Maven 聚合声明的全部历史/当前模块必须在后端根指引中可定位。 [xml]$pom = Get-Content -LiteralPath (Join-Path $backendRoot 'pom.xml') -Raw foreach ($module in $pom.project.modules.module) { Assert-Contains $backendAgents "$module/" '后端 AGENTS.md' } # 对本轮高变化链路做精确文件门禁,防止改名或删除后留下悬空索引。 $criticalPaths = @( 'apps/admin-web/src/modules/overseas-warehouse/api/inboundOrders.ts', 'apps/admin-web/src/modules/overseas-warehouse/api/inboundPlans.ts', 'apps/admin-web/src/modules/overseas-warehouse/api/inventory.ts', 'apps/admin-web/src/modules/overseas-warehouse/api/pallets.ts', 'apps/admin-web/src/modules/overseas-warehouse/api/dispatches.ts', 'apps/admin-web/src/modules/overseas-warehouse/pages/DispatchesView.vue', 'apps/admin-web/src/modules/overseas-warehouse/pages/DispatchDetailView.vue', 'apps/admin-web/src/modules/overseas-warehouse/components/SmartDispatchDialog.vue', 'apps/admin-web/src/modules/container-loading/pages/SmartLoadingWorkbenchView.vue', 'apps/admin-web/src/modules/container-loading/api/platformContainerLoading.ts', 'apps/client-web/src/api/overseasHttp.ts', 'apps/client-web/src/modules/overseas-warehouse/routes.ts', 'apps/client-web/src/modules/overseas-warehouse/api/warehouseServices.ts', 'apps/client-web/src/modules/overseas-warehouse/views/WarehouseServiceView.vue', 'apps/admin-web/src/modules/overseas-business/api/platformOverseasBusiness.ts', 'apps/admin-web/src/modules/overseas-master-data/api/overseasMasterData.ts', 'apps/pda-uniapp/pages/tasks/tasks.vue', 'apps/pda-uniapp/utils/evidence-upload-queue.js', 'apps/pda-overseas-uniapp/package.json', 'apps/pda-overseas-uniapp/src/pages.json', 'apps/pda-overseas-uniapp/src/data/overseas-workbench.js', 'apps/pda-overseas-uniapp/src/pages/operation/operation.vue', 'apps/pda-overseas-uniapp/src/config/runtime.js', 'apps/pda-overseas-uniapp/src/api/auth.js', 'apps/pda-overseas-uniapp/src/utils/scan-broadcast.js', 'baiyue-firstleg/firstleg/src/main/java/com/baiyue/firstleg/service/FeePaymentSnapshotValidator.java', 'baiyue-firstleg/firstleg/src/main/java/com/baiyue/firstleg/client/FeePaymentVoucherFileClient.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanQueryMapper.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/service/InboundFbaAppointmentService.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/gateway/UnloadPhotoFileGateway.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/controller/DispatchQueryController.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/controller/SmartDispatchController.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/entity/DispatchDraftContainer.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/mapper/DispatchDraftContainerMapper.java', 'baiyue-firstleg/firstleg/src/main/java/com/baiyue/firstleg/controller/PlatformSmartLoadingController.java', 'baiyue-firstleg/firstleg/src/main/java/com/baiyue/firstleg/service/SmartLoadingService.java', 'baiyue-firstleg/auth/src/main/java/com/baiyue/auth/service/OverseasTokenService.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/controller/MerchantLastlegOrderController.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/model/LastlegOrderType.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/model/LastlegOrderStatus.java', 'baiyue-firstleg/firstleg/src/main/java/com/baiyue/firstleg/portschedule/service/CarrierScheduleQuerySlots.java', 'baiyue-firstleg/firstleg/src/main/java/com/baiyue/firstleg/portschedule/entity/CarrierScheduleExceptionFactEntity.java', 'baiyue-firstleg/integration-service/src/main/java/com/baiyue/integration/domain/CarrierScheduleStandardNode.java', 'baiyue-firstleg/integration-service/src/main/java/com/baiyue/integration/domain/CarrierScheduleExceptionCode.java', 'baiyue-firstleg/gateway-overseas/src/test/java/com/baiyue/gateway/config/OverseasLastlegRouteContractTest.java', 'baiyue-firstleg/event-bridge-service/src/main/java/com/baiyue/eventbridge/service/FirstlegEventClient.java', 'baiyue-firstleg/common-event-contract/src/main/java/com/baiyue/common/event/FirstLegLoadingPlanCompletedV2.java', 'baiyue-firstleg/firstleg/src/test/java/com/baiyue/firstleg/service/TaillegRealProducerAcceptanceTest.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegInboundOrder.java', 'baiyue-firstleg/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegInboundOrderMapper.java', 'baiyue-firstleg/lastleg-service/src/test/java/com/baiyue/lastleg/service/TaillegRealConsumerAcceptanceTest.java', 'baiyue-firstleg/deploy/scripts/deploy-built-service.sh', 'baiyue-firstleg/scripts/deploy-built-service-rollback.test.mjs', 'docs/四期/船节点标准化契约.md' ) $criticalPaths | ForEach-Object { Assert-Path $_ } # 只检查明确维护的快速索引表,避免把排除规则正文中的目录名误判为业务入口。 $indexedAgentFiles = @( $rootAgentsPath, $adminAgentsPath, $clientAgentsPath, $pdaAgentsPath, $overseasPdaAgentsPath, $backendAgentsPath, (Join-Path $backendRoot 'firstleg/AGENTS.md'), (Join-Path $backendRoot 'finance/AGENTS.md'), (Join-Path $backendRoot 'lastleg-service/AGENTS.md'), (Join-Path $backendRoot 'event-bridge-service/AGENTS.md'), (Join-Path $backendRoot 'file/AGENTS.md'), (Join-Path $backendRoot 'common-event-contract/AGENTS.md') ) $excludedTargetPattern = '(?i)`(?:[^`]*/)?(node_modules|node_modules\.broken-by-path|dist|target|reports|output|outputs|uni_modules|\.git|\.codex[^/`]*|\.runtime[^/`]*)(?:/|`)' $excludedPatternSamples = @( '| 错误入口 | `node_modules/pkg/` |', '| 错误入口 | `src/target/classes/` |', '| 错误入口 | `.codex-temp/task/` |', '| 错误入口 | `.runtime-tailleg/DbQuery.java` |' ) foreach ($sample in $excludedPatternSamples) { $script:checked++ if ($sample -notmatch $excludedTargetPattern) { Add-Failure "排除目录检测器自检失败: $sample" } } foreach ($agentFile in $indexedAgentFiles) { $script:checked++ if (-not (Test-Path -LiteralPath $agentFile -PathType Leaf)) { Add-Failure "缺少受检索引文件: $agentFile" continue } $lineNumber = 0 foreach ($line in Get-Content -LiteralPath $agentFile) { $lineNumber++ if ($line.StartsWith('|') -and $line -match $excludedTargetPattern) { Add-Failure "快速索引引用了排除目录: $agentFile`:$lineNumber" } } } if ($failures.Count -gt 0) { Write-Host "AGENTS 索引合同失败,共 $($failures.Count) 项:" -ForegroundColor Red $failures | ForEach-Object { Write-Host "- $_" -ForegroundColor Red } exit 1 } Write-Host "AGENTS 索引合同通过:$checked 项检查,覆盖快速表唯一性、部署单元、前端模块、Client active 路由、Gateway 覆盖、PDA 注册页面、Maven 模块、关键入口和排除目录。" -ForegroundColor Green exit 0