bailinghub 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dockerignore +42 -0
- package/.env.example +83 -0
- package/.gitignore +44 -0
- package/.npmignore +56 -0
- package/CODE_OF_CONDUCT.md +24 -0
- package/CONTRIBUTING.md +74 -0
- package/Dockerfile +16 -0
- package/LICENSE +176 -0
- package/LICENSING.md +9 -0
- package/NOTICE +23 -0
- package/README.en.md +264 -0
- package/README.md +279 -0
- package/SECURITY.md +30 -0
- package/THIRD_PARTY_NOTICES.md +254 -0
- package/VISION.md +41 -0
- package/assets/architecture-overview.en-dark.png +0 -0
- package/assets/architecture-overview.en-dark.svg +57 -0
- package/assets/architecture-overview.en.png +0 -0
- package/assets/architecture-overview.en.svg +57 -0
- package/assets/architecture-overview.zh-CN-dark.png +0 -0
- package/assets/architecture-overview.zh-CN-dark.svg +57 -0
- package/assets/architecture-overview.zh-CN.png +0 -0
- package/assets/architecture-overview.zh-CN.svg +57 -0
- package/assets/bailinghub-lockup-dark.png +0 -0
- package/assets/bailinghub-lockup-dark.svg +8 -0
- package/assets/bailinghub-lockup.png +0 -0
- package/assets/bailinghub-lockup.svg +8 -0
- package/brain/README.en.md +23 -0
- package/brain/README.md +45 -0
- package/brain/agents/code-review.md +41 -0
- package/brain/agents/triage.md +37 -0
- package/brain/profiles.json +114 -0
- package/brain/runbooks/ticket-triage.md +24 -0
- package/config.example.json +58 -0
- package/contracts/client-api/consumers.json +28 -0
- package/contracts/client-api/v1/error-response.schema.json +21 -0
- package/contracts/client-api/v1/health-response.schema.json +13 -0
- package/contracts/client-api/v1/job-response.schema.json +49 -0
- package/contracts/client-api/v1/manifest.json +63 -0
- package/contracts/client-api/v1/run-request.schema.json +42 -0
- package/contracts/client-api/v1/submit-response.schema.json +35 -0
- package/contracts/client-api/v1/vectors.json +153 -0
- package/demo/business/Dockerfile +8 -0
- package/demo/business/server.mjs +483 -0
- package/docker-compose.images.yml +67 -0
- package/docker-compose.yml +79 -0
- package/docs/AI_FRIENDLY_TOOLS.en.md +69 -0
- package/docs/AI/345/217/213/345/245/275/345/267/245/345/205/267/350/256/276/350/256/241/346/214/207/345/215/227.md +198 -0
- package/docs/ARCHITECTURE.en.md +81 -0
- package/docs/ARCHITECTURE.md +795 -0
- package/docs/CHANGELOG.en.md +275 -0
- package/docs/CHANGELOG.md +279 -0
- package/docs/CHANNELS.en.md +35 -0
- package/docs/CHANNELS.md +72 -0
- package/docs/CLIENT_API.md +89 -0
- package/docs/COMPATIBILITY.en.md +103 -0
- package/docs/CONTRACT.en.md +254 -0
- package/docs/CONTRACT.md +653 -0
- package/docs/DEMO.en.md +90 -0
- package/docs/DEMO.md +297 -0
- package/docs/ECOSYSTEM.en.md +39 -0
- package/docs/ECOSYSTEM.md +39 -0
- package/docs/INDEPENDENT_VALIDATION.en.md +184 -0
- package/docs/INDEPENDENT_VALIDATION.md +184 -0
- package/docs/INTEGRATION.en.md +120 -0
- package/docs/KERNEL_HOST_API.en.md +59 -0
- package/docs/KERNEL_HOST_API.md +59 -0
- package/docs/OPERATIONS.en.md +182 -0
- package/docs/OPERATIONS.md +231 -0
- package/docs/PIPELINE.en.md +74 -0
- package/docs/PIPELINE.md +67 -0
- package/docs/QUICKSTART.en.md +297 -0
- package/docs/QUICKSTART.md +152 -0
- package/docs/README.en.md +103 -0
- package/docs/README.md +119 -0
- package/docs/RELEASE_NOTES_v0.1.0.en.md +46 -0
- package/docs/RELEASE_NOTES_v0.1.0.md +66 -0
- package/docs/RELEASE_NOTES_v0.1.1.en.md +38 -0
- package/docs/RELEASE_NOTES_v0.1.1.md +38 -0
- package/docs/RELEASE_NOTES_v0.1.10.en.md +102 -0
- package/docs/RELEASE_NOTES_v0.1.10.md +102 -0
- package/docs/RELEASE_NOTES_v0.1.11.en.md +99 -0
- package/docs/RELEASE_NOTES_v0.1.11.md +99 -0
- package/docs/RELEASE_NOTES_v0.1.12.en.md +64 -0
- package/docs/RELEASE_NOTES_v0.1.12.md +64 -0
- package/docs/RELEASE_NOTES_v0.1.13.en.md +46 -0
- package/docs/RELEASE_NOTES_v0.1.13.md +46 -0
- package/docs/RELEASE_NOTES_v0.1.14.en.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.14.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.15.en.md +50 -0
- package/docs/RELEASE_NOTES_v0.1.15.md +50 -0
- package/docs/RELEASE_NOTES_v0.1.16.en.md +106 -0
- package/docs/RELEASE_NOTES_v0.1.16.md +105 -0
- package/docs/RELEASE_NOTES_v0.1.2.en.md +39 -0
- package/docs/RELEASE_NOTES_v0.1.2.md +39 -0
- package/docs/RELEASE_NOTES_v0.1.3.en.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.3.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.4.en.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.4.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.5.en.md +49 -0
- package/docs/RELEASE_NOTES_v0.1.5.md +49 -0
- package/docs/RELEASE_NOTES_v0.1.6.en.md +41 -0
- package/docs/RELEASE_NOTES_v0.1.6.md +41 -0
- package/docs/RELEASE_NOTES_v0.1.7.en.md +39 -0
- package/docs/RELEASE_NOTES_v0.1.7.md +39 -0
- package/docs/RELEASE_NOTES_v0.1.8.en.md +88 -0
- package/docs/RELEASE_NOTES_v0.1.8.md +88 -0
- package/docs/RELEASE_NOTES_v0.1.9.en.md +127 -0
- package/docs/RELEASE_NOTES_v0.1.9.md +127 -0
- package/docs/RELEASE_NOTES_v0.2.0.en.md +95 -0
- package/docs/RELEASE_NOTES_v0.2.0.md +96 -0
- package/docs/RELEASE_NOTES_v0.3.0.en.md +77 -0
- package/docs/RELEASE_NOTES_v0.3.0.md +77 -0
- package/docs/SDK.en.md +177 -0
- package/docs/STREAMING.en.md +78 -0
- package/docs/STREAMING.md +109 -0
- package/docs/TOOLS.en.md +205 -0
- package/docs/TOOLS_DESIGN.en.md +142 -0
- package/docs/TOOLS_DESIGN.md +466 -0
- package/docs/TOOLS_MODEL.en.md +56 -0
- package/docs/TOOLS_MODEL.md +903 -0
- package/docs/WIDGET_RENDERERS.en.md +133 -0
- package/docs/WIDGET_RENDERERS.md +139 -0
- package/docs/examples/bailing-tool-verify.mjs +94 -0
- package/docs/examples/bailing-tool-verify.php +154 -0
- package/docs/examples/bailing-tool-verify.py +107 -0
- package/docs/examples/custom-streaming-chat-client.mjs +108 -0
- package/docs/examples/widget-chart-renderer-adapter.mjs +44 -0
- package/docs/integrations/bailian/README.en.md +181 -0
- package/docs/integrations/bailian/README.md +165 -0
- package/docs/integrations/bailian/bailian-mcp-config.json +17 -0
- package/docs/integrations/bailian/verify_recipe.py +97 -0
- package/docs/integrations/deepseek/README.en.md +225 -0
- package/docs/integrations/deepseek/README.md +225 -0
- package/docs/integrations/deepseek/verify_e2e.py +131 -0
- package/docs/integrations/dify/README.en.md +131 -0
- package/docs/integrations/dify/README.md +131 -0
- package/docs/integrations/dify/bailinghub-control-plane.openapi.json +129 -0
- package/docs/integrations/dify/verify_contract.py +88 -0
- package/docs/integrations/dify/verify_e2e.py +117 -0
- package/docs/integrations/jeecgboot/README.en.md +193 -0
- package/docs/integrations/jeecgboot/README.md +161 -0
- package/docs/integrations/jeecgboot/adapter-contract.v1.json +94 -0
- package/docs/integrations/jeecgboot/jeecgboot-user-adapter.openapi.json +183 -0
- package/docs/integrations/jeecgboot/verify_recipe.mjs +97 -0
- package/docs/integrations/ruoyi-vue-pro/README.en.md +91 -0
- package/docs/integrations/ruoyi-vue-pro/README.md +155 -0
- package/docs/integrations/ruoyi-vue-pro/adapter-contract.v1.json +71 -0
- package/docs/integrations/ruoyi-vue-pro/ruoyi-vue-pro-trade-adapter.openapi.json +148 -0
- package/docs/integrations/ruoyi-vue-pro/verify_recipe.mjs +71 -0
- package/docs/user-guide/README.en.md +39 -0
- package/docs/user-guide/README.md +40 -0
- package/docs/user-guide/concepts.en.md +69 -0
- package/docs/user-guide/concepts.md +225 -0
- package/docs/user-guide/console-map.en.md +58 -0
- package/docs/user-guide/console-map.md +473 -0
- package/docs/user-guide/overview.en.md +61 -0
- package/docs/user-guide/overview.md +156 -0
- package/docs/user-guide/scenarios.en.md +70 -0
- package/docs/user-guide/scenarios.md +398 -0
- package/docs//345/205/274/345/256/271/346/200/247/344/270/216/345/215/207/347/272/247.md +134 -0
- package/docs//347/254/254/344/270/211/346/226/271/345/257/271/346/216/245/346/214/207/345/215/227.md +625 -0
- package/package.json +119 -0
- package/schemas/agent-capability-contract.schema.json +104 -0
- package/schemas/api/approval-decision.schema.json +28 -0
- package/schemas/api/approval-request.schema.json +31 -0
- package/schemas/api/delivery-callback.schema.json +28 -0
- package/schemas/api/job-response.schema.json +6 -0
- package/schemas/api/kb-doc-upsert.schema.json +21 -0
- package/schemas/api/run-request.schema.json +6 -0
- package/schemas/api/run-response.schema.json +6 -0
- package/schemas/api/tool-spec.schema.json +38 -0
- package/schemas/config/alert-rule.schema.json +22 -0
- package/schemas/config/channel.schema.json +91 -0
- package/schemas/config/chat-entry.schema.json +47 -0
- package/schemas/config/client.schema.json +56 -0
- package/schemas/config/common.schema.json +215 -0
- package/schemas/config/credential.schema.json +48 -0
- package/schemas/config/executor-token.schema.json +20 -0
- package/schemas/config/page-context.schema.json +21 -0
- package/schemas/config/route.schema.json +152 -0
- package/schemas/config/storage-bucket.schema.json +31 -0
- package/schemas/config/target.schema.json +17 -0
- package/schemas/config/tool-provider.schema.json +60 -0
- package/schemas/tool-definition.schema.json +188 -0
- package/scripts/check-client-api-ecosystem.mjs +69 -0
- package/scripts/check-doc-links.mjs +136 -0
- package/scripts/check-image-tags.sh +43 -0
- package/scripts/check-package-artifact.mjs +185 -0
- package/scripts/check-release-consistency.mjs +199 -0
- package/scripts/check-release-consistency.test.mjs +63 -0
- package/scripts/client-api-contract-test.ts +127 -0
- package/scripts/create-admin.ts +27 -0
- package/scripts/demo-e2e.ts +133 -0
- package/scripts/docker-entrypoint.sh +33 -0
- package/scripts/doctor.ts +107 -0
- package/scripts/e2e-console.mjs +214 -0
- package/scripts/export-oss.mjs +204 -0
- package/scripts/generate-readme-assets.mjs +181 -0
- package/scripts/generate-third-party-notices.mjs +93 -0
- package/scripts/image-release-policy.mjs +75 -0
- package/scripts/image-release-policy.test.mjs +58 -0
- package/scripts/init-db.ts +21 -0
- package/scripts/install.sh +461 -0
- package/scripts/oss-boundary-check.mjs +183 -0
- package/scripts/publish-images.sh +63 -0
- package/scripts/publish-mysql-image.sh +41 -0
- package/scripts/release-audit.mjs +348 -0
- package/scripts/sdk-contract-test.ts +62 -0
- package/scripts/sdk-p1-check.mjs +58 -0
- package/scripts/sdk-runtime-test.mjs +106 -0
- package/scripts/security-scan.mjs +110 -0
- package/scripts/seed-demo.ts +116 -0
- package/scripts/selftest.ts +226 -0
- package/scripts/smoke.ts +31 -0
- package/scripts/validate-examples.mjs +93 -0
- package/scripts/verify-bootstrap-admin-restart.ts +66 -0
- package/scripts/verify-github-repo.mjs +195 -0
- package/scripts/verify-oss-export.mjs +192 -0
- package/scripts/verify-routing.ts +29 -0
- package/scripts/widget-renderer-test.mjs +206 -0
- package/scripts/write-build-info.mjs +29 -0
- package/sdk/dotnet/Bailing.Connect/Bailing.Connect.csproj +12 -0
- package/sdk/dotnet/Bailing.Connect/BailingConnect.cs +260 -0
- package/sdk/dotnet/README.md +32 -0
- package/sdk/dotnet/examples/BuildSpec/BuildSpec.csproj +11 -0
- package/sdk/dotnet/examples/BuildSpec/Program.cs +36 -0
- package/sdk/go/README.md +33 -0
- package/sdk/go/bailingconnect/connect.go +367 -0
- package/sdk/go/examples/build-spec/main.go +39 -0
- package/sdk/go/go.mod +3 -0
- package/sdk/java/README.md +43 -0
- package/sdk/java/examples/BuildSpec.java +31 -0
- package/sdk/java/src/main/java/com/bailing/connect/BailingConnect.java +376 -0
- package/sdk/node/README.md +107 -0
- package/sdk/node/examples/build-spec.mjs +84 -0
- package/sdk/node/package.json +22 -0
- package/sdk/node/src/index.mjs +250 -0
- package/sdk/php/README.md +204 -0
- package/sdk/php/composer.json +18 -0
- package/sdk/php/examples/DemoStaffController.php +108 -0
- package/sdk/php/examples/build-spec.php +27 -0
- package/sdk/php/examples/thinkphp-integration.php +81 -0
- package/sdk/php/src/Attributes/AiParam.php +40 -0
- package/sdk/php/src/Attributes/AiTool.php +75 -0
- package/sdk/php/src/HubClient.php +122 -0
- package/sdk/php/src/SpecBuilder.php +328 -0
- package/sdk/php/src/SpecServer.php +209 -0
- package/sdk/php/src/Ticket.php +37 -0
- package/sdk/php/src/Verify.php +178 -0
- package/sdk/php7/README.md +191 -0
- package/sdk/php7/composer.json +18 -0
- package/sdk/php7/examples/build-spec.php +68 -0
- package/sdk/php7/examples/well-known.php +33 -0
- package/sdk/php7/src/HubClient.php +111 -0
- package/sdk/php7/src/SpecServer.php +203 -0
- package/sdk/php7/src/Ticket.php +33 -0
- package/sdk/php7/src/ToolDef.php +169 -0
- package/sdk/php7/src/ToolSpec.php +334 -0
- package/sdk/php7/src/Verify.php +165 -0
- package/sdk/python/README.md +107 -0
- package/sdk/python/bailing_connect/__init__.py +307 -0
- package/sdk/python/examples/build_spec.py +90 -0
- package/sdk/python/pyproject.toml +24 -0
- package/sql/001_init_state.sql +66 -0
- package/sql/002_routes.sql +44 -0
- package/sql/003_executor.sql +15 -0
- package/sql/004_clients.sql +22 -0
- package/sql/005_admins.sql +25 -0
- package/sql/006_ledger.sql +57 -0
- package/sql/007_delivery.sql +8 -0
- package/sql/008_kb.sql +66 -0
- package/sql/009_rbac.sql +5 -0
- package/sql/010_targets.sql +28 -0
- package/sql/011_observability.sql +11 -0
- package/sql/012_generic.sql +5 -0
- package/sql/013_tools.sql +24 -0
- package/sql/014_approvals.sql +27 -0
- package/sql/015_chat.sql +21 -0
- package/sql/016_chat_feedback.sql +19 -0
- package/sql/017_provider_autorefresh.sql +6 -0
- package/sql/018_kb_ingest.sql +11 -0
- package/sql/019_kb_datasources.sql +32 -0
- package/sql/022_storage_buckets.sql +25 -0
- package/sql/024_channels.sql +15 -0
- package/sql/025_chat_appearance.sql +7 -0
- package/sql/026_thread_memory.sql +10 -0
- package/sql/027_page_contexts.sql +23 -0
- package/sql/028_executor_capabilities.sql +4 -0
- package/sql/029_executor_tokens.sql +13 -0
- package/sql/030_route_permission.sql +5 -0
- package/sql/031_tool_embeddings.sql +20 -0
- package/sql/032_tool_provider_embed.sql +7 -0
- package/sql/033_alert_rules.sql +21 -0
- package/sql/034_client_allowed_channels.sql +6 -0
- package/sql/035_tool_idempotency.sql +15 -0
- package/sql/036_delivery_dlq.sql +15 -0
- package/sql/038_audit_trace_shape.sql +15 -0
- package/sql/039_runtime_locks.sql +9 -0
- package/sql/040_budget_policy.sql +3 -0
- package/sql/041_approval_intent.sql +14 -0
- package/sql/042_approval_decision_contract.sql +11 -0
- package/sql/043_rate_limits.sql +7 -0
- package/sql/044_job_run_after.sql +3 -0
- package/sql/045_job_claim_lease.sql +7 -0
- package/sql/046_tool_authz_probe.sql +5 -0
- package/sql/047_route_audience.sql +4 -0
- package/sql/048_llm_target.sql +17 -0
- package/sql/049_llm_input_shape.sql +11 -0
- package/sql/050_operational_metrics_indexes.sql +8 -0
- package/sql/051_instance_branding.sql +17 -0
- package/sql/052_tool_execution_journal.sql +19 -0
- package/sql/053_tool_spec_access_policy.sql +9 -0
- package/sql/README.en.md +41 -0
- package/sql/README.md +40 -0
- package/src/adapters/channels/wecom-api.test.ts +77 -0
- package/src/adapters/channels/wecom-api.ts +224 -0
- package/src/adapters/channels/wecom-crypto.test.ts +85 -0
- package/src/adapters/channels/wecom-crypto.ts +77 -0
- package/src/adapters/executors/claudecode.ts +39 -0
- package/src/adapters/executors/profiles.ts +86 -0
- package/src/adapters/executors/report.ts +35 -0
- package/src/adapters/executors/runner.ts +114 -0
- package/src/adapters/executors/wecomnotify.ts +63 -0
- package/src/adapters/llm/file.test.ts +81 -0
- package/src/adapters/llm/file.ts +250 -0
- package/src/adapters/llm/openai-chat-stream.test.ts +74 -0
- package/src/adapters/llm/openai-chat-stream.ts +154 -0
- package/src/adapters/llm/perception.ts +159 -0
- package/src/adapters/llm/speech.test.ts +75 -0
- package/src/adapters/llm/speech.ts +146 -0
- package/src/adapters/storage/object-storage.test.ts +38 -0
- package/src/adapters/storage/object-storage.ts +140 -0
- package/src/adapters/targets/demo-agent.ts +88 -0
- package/src/adapters/targets/llm.test.ts +733 -0
- package/src/adapters/targets/llm.ts +862 -0
- package/src/app/admin-bootstrap.test.ts +112 -0
- package/src/app/admin-bootstrap.ts +48 -0
- package/src/app/auth-default.ts +26 -0
- package/src/app/auth.test.ts +57 -0
- package/src/app/auth.ts +207 -0
- package/src/app/builtin-tools-default.ts +17 -0
- package/src/app/builtin-tools.test.ts +202 -0
- package/src/app/builtin-tools.ts +380 -0
- package/src/app/channels-default.ts +8 -0
- package/src/app/channels.ts +141 -0
- package/src/app/delivery-default.ts +14 -0
- package/src/app/delivery.test.ts +46 -0
- package/src/app/delivery.ts +215 -0
- package/src/app/engine-default.ts +54 -0
- package/src/app/engine.ts +427 -0
- package/src/app/http-mount.test.ts +27 -0
- package/src/app/http-server.test.ts +114 -0
- package/src/app/http-server.ts +127 -0
- package/src/app/http.test.ts +27 -0
- package/src/app/http.ts +118 -0
- package/src/app/kernel.test.ts +191 -0
- package/src/app/kernel.ts +544 -0
- package/src/app/monitor-default.ts +9 -0
- package/src/app/monitor.ts +46 -0
- package/src/app/operational-metrics.test.ts +73 -0
- package/src/app/operational-metrics.ts +124 -0
- package/src/app/oss-edition.test.ts +71 -0
- package/src/app/oss-edition.ts +108 -0
- package/src/app/outbound-default.ts +57 -0
- package/src/app/outbound.ts +133 -0
- package/src/app/readiness.test.ts +37 -0
- package/src/app/readiness.ts +39 -0
- package/src/app/runtime-composition.test.ts +51 -0
- package/src/app/runtime-composition.ts +73 -0
- package/src/app/runtime-context-default.ts +30 -0
- package/src/app/runtime-context.test.ts +60 -0
- package/src/app/runtime-context.ts +79 -0
- package/src/app/runtime-lifecycle-default.ts +48 -0
- package/src/app/runtime-lifecycle.test.ts +322 -0
- package/src/app/runtime-lifecycle.ts +440 -0
- package/src/app/runtime.ts +36 -0
- package/src/app/tool-approvals-default.ts +16 -0
- package/src/app/tool-approvals.ts +214 -0
- package/src/app/tool-assembly-default.ts +11 -0
- package/src/app/tool-assembly.test.ts +112 -0
- package/src/app/tool-assembly.ts +83 -0
- package/src/app/tool-context-default.ts +7 -0
- package/src/app/tool-context.test.ts +82 -0
- package/src/app/tool-context.ts +109 -0
- package/src/app/tool-debug.test.ts +111 -0
- package/src/app/tool-debug.ts +152 -0
- package/src/app/tool-proxy-default.ts +35 -0
- package/src/app/tool-proxy.ts +173 -0
- package/src/app/tool-specs-default.ts +33 -0
- package/src/app/tool-specs.test.ts +322 -0
- package/src/app/tool-specs.ts +555 -0
- package/src/app/tools-runtime-default.ts +14 -0
- package/src/app/tools-runtime.ts +23 -0
- package/src/architecture-boundary.test.ts +119 -0
- package/src/core/config/config-codec.test.ts +105 -0
- package/src/core/config/config-codec.ts +231 -0
- package/src/core/config/config-diagnostics.test.ts +285 -0
- package/src/core/config/config-diagnostics.ts +436 -0
- package/src/core/config/config-models.test.ts +584 -0
- package/src/core/config/config-models.ts +392 -0
- package/src/core/config/config-schema.test.ts +153 -0
- package/src/core/config/config.test.ts +314 -0
- package/src/core/config/config.ts +324 -0
- package/src/core/config/route-config.test.ts +232 -0
- package/src/core/config/route-config.ts +246 -0
- package/src/core/config/target-config.test.ts +139 -0
- package/src/core/config/target-config.ts +219 -0
- package/src/core/config/tools-config.test.ts +124 -0
- package/src/core/config/tools-config.ts +126 -0
- package/src/core/contracts/approval-decision.test.ts +67 -0
- package/src/core/contracts/approval-decision.ts +93 -0
- package/src/core/contracts/openapi-tools.ts +201 -0
- package/src/core/contracts/tool-annotations.ts +121 -0
- package/src/core/contracts/tool-definition.test.ts +147 -0
- package/src/core/contracts/tool-definition.ts +304 -0
- package/src/core/contracts/tools-idempotency.test.ts +435 -0
- package/src/core/contracts/tools.test.ts +487 -0
- package/src/core/contracts/tools.ts +831 -0
- package/src/core/contracts/types.ts +408 -0
- package/src/core/edition/capabilities.ts +6 -0
- package/src/core/edition/context.test.ts +20 -0
- package/src/core/edition/context.ts +78 -0
- package/src/core/edition/index.ts +4 -0
- package/src/core/edition/scope-resolver.ts +24 -0
- package/src/core/edition/store-factory.ts +6 -0
- package/src/core/observability/openmetrics.test.ts +57 -0
- package/src/core/observability/openmetrics.ts +103 -0
- package/src/core/platform/content.test.ts +46 -0
- package/src/core/platform/content.ts +90 -0
- package/src/core/platform/fence.test.ts +31 -0
- package/src/core/platform/fence.ts +11 -0
- package/src/core/platform/instance-branding.test.ts +65 -0
- package/src/core/platform/instance-branding.ts +202 -0
- package/src/core/platform/metrics-token.test.ts +31 -0
- package/src/core/platform/metrics-token.ts +42 -0
- package/src/core/platform/pagecontext.ts +0 -0
- package/src/core/platform/password.test.ts +13 -0
- package/src/core/platform/password.ts +38 -0
- package/src/core/platform/queue.test.ts +59 -0
- package/src/core/platform/queue.ts +75 -0
- package/src/core/platform/serial.test.ts +73 -0
- package/src/core/platform/serial.ts +64 -0
- package/src/core/platform/server-token.test.ts +46 -0
- package/src/core/platform/server-token.ts +60 -0
- package/src/core/platform/signing.test.ts +24 -0
- package/src/core/platform/signing.ts +22 -0
- package/src/core/platform/time.ts +73 -0
- package/src/core/platform/version.test.ts +19 -0
- package/src/core/platform/version.ts +90 -0
- package/src/core/runtime/budget-runtime.test.ts +93 -0
- package/src/core/runtime/budget-runtime.ts +110 -0
- package/src/core/runtime/context-runtime.test.ts +106 -0
- package/src/core/runtime/context-runtime.ts +95 -0
- package/src/core/runtime/credential-resolver.test.ts +129 -0
- package/src/core/runtime/credential-resolver.ts +93 -0
- package/src/core/runtime/credential-verify.test.ts +76 -0
- package/src/core/runtime/credential-verify.ts +161 -0
- package/src/core/runtime/debug-report.test.ts +20 -0
- package/src/core/runtime/debug-report.ts +100 -0
- package/src/core/runtime/execution-runtime.test.ts +350 -0
- package/src/core/runtime/execution-runtime.ts +197 -0
- package/src/core/runtime/finish-runtime.test.ts +168 -0
- package/src/core/runtime/finish-runtime.ts +83 -0
- package/src/core/runtime/identity-runtime.test.ts +38 -0
- package/src/core/runtime/identity-runtime.ts +104 -0
- package/src/core/runtime/inhub-runtime.test.ts +297 -0
- package/src/core/runtime/inhub-runtime.ts +198 -0
- package/src/core/runtime/job-stream.test.ts +55 -0
- package/src/core/runtime/job-stream.ts +187 -0
- package/src/core/runtime/knowledge-runtime.test.ts +120 -0
- package/src/core/runtime/knowledge-runtime.ts +98 -0
- package/src/core/runtime/launch-runtime.test.ts +187 -0
- package/src/core/runtime/launch-runtime.ts +182 -0
- package/src/core/runtime/memory-runtime.test.ts +85 -0
- package/src/core/runtime/memory-runtime.ts +32 -0
- package/src/core/runtime/memory.ts +141 -0
- package/src/core/runtime/redaction-runtime.test.ts +35 -0
- package/src/core/runtime/redaction-runtime.ts +60 -0
- package/src/core/runtime/routing-runtime.test.ts +89 -0
- package/src/core/runtime/routing-runtime.ts +146 -0
- package/src/core/runtime/smoke-runtime.test.ts +65 -0
- package/src/core/runtime/smoke-runtime.ts +237 -0
- package/src/core/runtime/summary-runtime.test.ts +234 -0
- package/src/core/runtime/summary-runtime.ts +153 -0
- package/src/core/runtime/trace-runtime.test.ts +234 -0
- package/src/core/runtime/trace-runtime.ts +330 -0
- package/src/core/state/audit-observability.test.ts +51 -0
- package/src/core/state/audit-observability.ts +74 -0
- package/src/core/state/state-codec.ts +44 -0
- package/src/core/state/state-contracts.ts +59 -0
- package/src/core/targets/adapter.ts +69 -0
- package/src/core/targets/registry.test.ts +65 -0
- package/src/core/targets/registry.ts +153 -0
- package/src/core/targets/resolve.ts +21 -0
- package/src/executor.ts +201 -0
- package/src/extension-api.test.ts +84 -0
- package/src/extension-api.ts +194 -0
- package/src/infrastructure/config/config-admin-repository.test.ts +193 -0
- package/src/infrastructure/config/config-admin-repository.ts +175 -0
- package/src/infrastructure/config/config-alert-rule-repository.ts +38 -0
- package/src/infrastructure/config/config-approval-ledger.ts +74 -0
- package/src/infrastructure/config/config-channel-repository.ts +31 -0
- package/src/infrastructure/config/config-chat-repository.ts +83 -0
- package/src/infrastructure/config/config-client-repository.ts +46 -0
- package/src/infrastructure/config/config-conversation-ledger.ts +222 -0
- package/src/infrastructure/config/config-credential-repository.test.ts +32 -0
- package/src/infrastructure/config/config-credential-repository.ts +47 -0
- package/src/infrastructure/config/config-delivery-dlq-ledger.ts +49 -0
- package/src/infrastructure/config/config-executor-ledger.ts +30 -0
- package/src/infrastructure/config/config-executor-token-repository.ts +44 -0
- package/src/infrastructure/config/config-instance-branding-repository.ts +75 -0
- package/src/infrastructure/config/config-knowledge-repository.ts +249 -0
- package/src/infrastructure/config/config-observability-ledger.ts +262 -0
- package/src/infrastructure/config/config-project-repository.ts +30 -0
- package/src/infrastructure/config/config-rate-limit-ledger.ts +56 -0
- package/src/infrastructure/config/config-route-repository.ts +41 -0
- package/src/infrastructure/config/config-storage-bucket-repository.ts +43 -0
- package/src/infrastructure/config/config-target-repository.ts +25 -0
- package/src/infrastructure/config/config-tool-call-ledger.ts +142 -0
- package/src/infrastructure/config/config-tool-embedding-repository.test.ts +124 -0
- package/src/infrastructure/config/config-tool-embedding-repository.ts +78 -0
- package/src/infrastructure/config/config-tool-provider-repository.test.ts +71 -0
- package/src/infrastructure/config/config-tool-provider-repository.ts +50 -0
- package/src/infrastructure/config/configstore.ts +130 -0
- package/src/infrastructure/config/local-instance-branding-provider.test.ts +94 -0
- package/src/infrastructure/config/local-instance-branding-provider.ts +79 -0
- package/src/infrastructure/mysql/pool-owner.test.ts +36 -0
- package/src/infrastructure/mysql/pool-owner.ts +56 -0
- package/src/infrastructure/schema/core-schema-migrator.test.ts +340 -0
- package/src/infrastructure/schema/core-schema-migrator.ts +461 -0
- package/src/infrastructure/schema/migration-files.test.ts +18 -0
- package/src/infrastructure/schema/migration-files.ts +27 -0
- package/src/infrastructure/schema/retired-migrations.ts +35 -0
- package/src/infrastructure/state/operational-metrics-storage.test.ts +107 -0
- package/src/infrastructure/state/state-jsonl.ts +268 -0
- package/src/infrastructure/state/state-mysql-update-plan.test.ts +54 -0
- package/src/infrastructure/state/state-mysql-update-plan.ts +63 -0
- package/src/infrastructure/state/state-mysql.ts +359 -0
- package/src/infrastructure/state/state.test.ts +319 -0
- package/src/infrastructure/state/state.ts +11 -0
- package/src/kernel-api/v1/artifact.test.ts +46 -0
- package/src/kernel-api/v1/artifact.ts +71 -0
- package/src/kernel-api/v1/contracts.ts +168 -0
- package/src/kernel-api/v1/execution-gate.test.ts +25 -0
- package/src/kernel-api/v1/execution-gate.ts +18 -0
- package/src/kernel-api/v1/index.ts +51 -0
- package/src/openclaw-stdio.test.ts +60 -0
- package/src/routes/admin-access-default.ts +20 -0
- package/src/routes/admin-access.ts +94 -0
- package/src/routes/admin-branding.test.ts +116 -0
- package/src/routes/admin-branding.ts +48 -0
- package/src/routes/admin-chat-default.ts +12 -0
- package/src/routes/admin-chat.ts +84 -0
- package/src/routes/admin-default.ts +35 -0
- package/src/routes/admin-dispatch-config-default.ts +13 -0
- package/src/routes/admin-dispatch-config.ts +121 -0
- package/src/routes/admin-format.ts +12 -0
- package/src/routes/admin-host.test.ts +56 -0
- package/src/routes/admin-infra-default.ts +12 -0
- package/src/routes/admin-infra.ts +142 -0
- package/src/routes/admin-kb-default.ts +13 -0
- package/src/routes/admin-kb.ts +160 -0
- package/src/routes/admin-runtime-default.ts +31 -0
- package/src/routes/admin-runtime.ts +444 -0
- package/src/routes/admin-tool-providers-default.ts +20 -0
- package/src/routes/admin-tool-providers.test.ts +114 -0
- package/src/routes/admin-tool-providers.ts +186 -0
- package/src/routes/admin.ts +246 -0
- package/src/routes/approvals-default.ts +32 -0
- package/src/routes/approvals.ts +120 -0
- package/src/routes/chat-default.ts +61 -0
- package/src/routes/chat-mount.test.ts +58 -0
- package/src/routes/chat.test.ts +222 -0
- package/src/routes/chat.ts +478 -0
- package/src/routes/critical-routes.test.ts +292 -0
- package/src/routes/executor-default.ts +45 -0
- package/src/routes/executor.ts +226 -0
- package/src/routes/kb-default.ts +23 -0
- package/src/routes/kb.ts +92 -0
- package/src/routes/private-default.ts +38 -0
- package/src/routes/private-host-auth.test.ts +84 -0
- package/src/routes/private.ts +210 -0
- package/src/routes/public-default.ts +46 -0
- package/src/routes/public.test.ts +504 -0
- package/src/routes/public.ts +313 -0
- package/src/routes/run-default.ts +25 -0
- package/src/routes/run.ts +188 -0
- package/src/routes/send-default.ts +24 -0
- package/src/routes/send.ts +143 -0
- package/src/routes/wecom-default.ts +27 -0
- package/src/routes/wecom.ts +307 -0
- package/src/server.ts +13 -0
- package/src/services/embedding.test.ts +185 -0
- package/src/services/embedding.ts +138 -0
- package/src/services/kb-repository.ts +79 -0
- package/src/services/kb.ts +328 -0
- package/src/services/kbsync.ts +166 -0
- package/src/services/tool-index-repository.ts +50 -0
- package/src/services/tools-index.test.ts +756 -0
- package/src/services/tools-index.ts +427 -0
- package/tsconfig.json +17 -0
- package/web/connect/bailing-connect-php.tgz +0 -0
- package/web/connect/bailing-connect-php7.tgz +0 -0
- package/web/connect/executor.mjs +152 -0
- package/web/connect/openclaw-stdio.mjs +145 -0
- package/web/connect/skills/connect-bailinghub-executor/SKILL.md +97 -0
- package/web/connect/skills/connect-bailinghub-executor/agents/openai.yaml +4 -0
- package/web/connect/skills/connect-bailinghub-executor/references/direct-protocol.md +99 -0
- package/web/connect/skills/connect-bailinghub-executor/references/openclaw.md +39 -0
- package/web/console/assets/Accounts-B4ePH5jp.css +1 -0
- package/web/console/assets/Accounts-Dwzm4D_V.js +3 -0
- package/web/console/assets/Approvals-BxhuI0ku.js +1 -0
- package/web/console/assets/Approvals-DlTSHl_7.css +1 -0
- package/web/console/assets/Audit-CSu6_3oD.css +1 -0
- package/web/console/assets/Audit-dFAbGynl.js +1 -0
- package/web/console/assets/BrandLockup-BtZyvkM5.js +1 -0
- package/web/console/assets/BrandLockup-DKYBYzKU.css +1 -0
- package/web/console/assets/Channels-CCn0L1jg.css +1 -0
- package/web/console/assets/Channels-Dqg4-_yr.js +105 -0
- package/web/console/assets/ChatEntries-BweMx_CU.css +1 -0
- package/web/console/assets/ChatEntries-CEGZ3NnJ.js +7 -0
- package/web/console/assets/Clients-C0QD1x7g.js +3 -0
- package/web/console/assets/Clients-DcTYN-Js.css +1 -0
- package/web/console/assets/Cost-BAJqLhpK.js +1 -0
- package/web/console/assets/Cost-CjT39DSZ.css +1 -0
- package/web/console/assets/Credentials-D_GDRgWi.js +1 -0
- package/web/console/assets/Credentials-ioLP0qYN.css +1 -0
- package/web/console/assets/Diagnostics-BWYnDxRj.js +1 -0
- package/web/console/assets/Diagnostics-Ya05PTDp.css +1 -0
- package/web/console/assets/Executors-CjhEM54c.css +1 -0
- package/web/console/assets/Executors-Cmryk4Vk.js +1 -0
- package/web/console/assets/HelpTip-CA6Bm7Nx.css +1 -0
- package/web/console/assets/HelpTip-CT07o3kB.js +1 -0
- package/web/console/assets/Kb-BSXQE0Fa.js +5 -0
- package/web/console/assets/Kb-CzIzd9xr.css +1 -0
- package/web/console/assets/Login-BnBPqC8Z.js +1 -0
- package/web/console/assets/Login-xtPwDB5_.css +1 -0
- package/web/console/assets/Projects-Bm8AjsNx.css +1 -0
- package/web/console/assets/Projects-CNHx4L_7.js +1 -0
- package/web/console/assets/Routes-DWGQN5Pv.css +1 -0
- package/web/console/assets/Routes-u5cEAEXQ.js +124 -0
- package/web/console/assets/Runs-BeFUR77_.js +3 -0
- package/web/console/assets/Runs-CUtSsPW-.css +1 -0
- package/web/console/assets/Setup-BjZfrmfN.js +1 -0
- package/web/console/assets/Setup-QjlHZKdB.css +1 -0
- package/web/console/assets/Shell-CbGNTN91.css +1 -0
- package/web/console/assets/Shell-EJuYf3OO.js +1 -0
- package/web/console/assets/StorageBuckets-DMXRJxUA.js +1 -0
- package/web/console/assets/StorageBuckets-GnXnVvF0.css +1 -0
- package/web/console/assets/SystemSettings-B72F-loY.js +1 -0
- package/web/console/assets/SystemSettings-BjiHyrXO.css +1 -0
- package/web/console/assets/SystemStatus-I11I67rw.css +1 -0
- package/web/console/assets/SystemStatus-hOj_TPYE.js +1 -0
- package/web/console/assets/Targets-DTb5tn6z.css +1 -0
- package/web/console/assets/Targets-UVVf7twC.js +1 -0
- package/web/console/assets/Tools-A20oaMaQ.css +1 -0
- package/web/console/assets/Tools-ihkr9R9x.js +2 -0
- package/web/console/assets/_plugin-vue_export-helper-DlAUqK2U.js +1 -0
- package/web/console/assets/docs-DPUOCa7R.js +1 -0
- package/web/console/assets/index-DZEQL_yy.js +1 -0
- package/web/console/assets/index-DxvruykG.js +2 -0
- package/web/console/assets/index-Fi9gkGxV.css +1 -0
- package/web/console/assets/llm-catalog-HwRIrS94.js +1 -0
- package/web/console/assets/request-BisL2T9B.js +1 -0
- package/web/console/assets/schema-CzpUpoLk.js +1 -0
- package/web/console/assets/util-SiQC2MJh.js +1 -0
- package/web/console/assets/vendor-element-D7WRzHhy.js +37 -0
- package/web/console/assets/vendor-mammoth-DvgsHdlx.js +231 -0
- package/web/console/assets/vendor-turndown-kLzRgxYD.js +61 -0
- package/web/console/assets/vendor-vue-B2uhHUfE.js +29 -0
- package/web/console/index.html +16 -0
- package/web/widget/widget.js +1490 -0
- package/web-admin/index.html +13 -0
- package/web-admin/package-lock.json +1854 -0
- package/web-admin/package.json +25 -0
- package/web-admin/src/App.vue +9 -0
- package/web-admin/src/branding.ts +90 -0
- package/web-admin/src/components/BrandLockup.vue +40 -0
- package/web-admin/src/components/HelpTip.vue +51 -0
- package/web-admin/src/components/RichText.vue +35 -0
- package/web-admin/src/docs.ts +16 -0
- package/web-admin/src/layout/Shell.vue +356 -0
- package/web-admin/src/llm-catalog.ts +129 -0
- package/web-admin/src/main.ts +116 -0
- package/web-admin/src/pages/Accounts.vue +162 -0
- package/web-admin/src/pages/Approvals.vue +179 -0
- package/web-admin/src/pages/Audit.vue +110 -0
- package/web-admin/src/pages/Channels.vue +738 -0
- package/web-admin/src/pages/ChatEntries.vue +778 -0
- package/web-admin/src/pages/Clients.vue +353 -0
- package/web-admin/src/pages/Cost.vue +314 -0
- package/web-admin/src/pages/Credentials.vue +418 -0
- package/web-admin/src/pages/Diagnostics.vue +440 -0
- package/web-admin/src/pages/Executors.vue +398 -0
- package/web-admin/src/pages/Kb.vue +698 -0
- package/web-admin/src/pages/Login.vue +314 -0
- package/web-admin/src/pages/Projects.vue +106 -0
- package/web-admin/src/pages/Routes.vue +1661 -0
- package/web-admin/src/pages/Runs.vue +1113 -0
- package/web-admin/src/pages/Setup.vue +354 -0
- package/web-admin/src/pages/StorageBuckets.vue +182 -0
- package/web-admin/src/pages/SystemSettings.vue +281 -0
- package/web-admin/src/pages/SystemStatus.vue +185 -0
- package/web-admin/src/pages/Targets.vue +193 -0
- package/web-admin/src/pages/Tools.vue +1051 -0
- package/web-admin/src/request.ts +18 -0
- package/web-admin/src/router.ts +142 -0
- package/web-admin/src/runtime-path.ts +27 -0
- package/web-admin/src/schema.ts +52 -0
- package/web-admin/src/store.ts +35 -0
- package/web-admin/src/styles/base.css +272 -0
- package/web-admin/src/table-widths.ts +603 -0
- package/web-admin/src/util.ts +16 -0
- package/web-admin/tsconfig.json +15 -0
- package/web-admin/vite.config.ts +35 -0
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { readdirSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import type { MysqlConfig } from '../../core/config/config';
|
|
6
|
+
import { assertOfficialMigrationFilenames } from './migration-files';
|
|
7
|
+
import { BAILINGHUB_RETIRED_CORE_MIGRATIONS_V1 } from './retired-migrations';
|
|
8
|
+
|
|
9
|
+
const IDEMPOTENT_MYSQL_ERRNOS = new Set([1060, 1061]);
|
|
10
|
+
const DEFAULT_LOCK_TIMEOUT_SECONDS = 30;
|
|
11
|
+
const BAILINGHUB_CORE_ROOT = fileURLToPath(new URL('../../../', import.meta.url));
|
|
12
|
+
|
|
13
|
+
const CREATE_LEDGER_SQL =
|
|
14
|
+
'CREATE TABLE IF NOT EXISTS `bz_schema_migrations` (' +
|
|
15
|
+
'`filename` VARCHAR(190) NOT NULL PRIMARY KEY,' +
|
|
16
|
+
'`checksum_sha256` CHAR(64) NULL,' +
|
|
17
|
+
'`applied_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP' +
|
|
18
|
+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=\'已应用的 sql 结构文件账本\'';
|
|
19
|
+
|
|
20
|
+
const ADD_LEDGER_CHECKSUM_SQL =
|
|
21
|
+
'ALTER TABLE `bz_schema_migrations` ' +
|
|
22
|
+
'ADD COLUMN `checksum_sha256` CHAR(64) NULL AFTER `filename`';
|
|
23
|
+
|
|
24
|
+
export interface CoreSchemaMigrationConnectionV1 {
|
|
25
|
+
query(sql: string, params?: unknown[]): Promise<[unknown, unknown]>;
|
|
26
|
+
end(): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type CoreSchemaMigrationConnectionFactoryV1 = (
|
|
30
|
+
mysql: MysqlConfig,
|
|
31
|
+
) => Promise<CoreSchemaMigrationConnectionV1>;
|
|
32
|
+
|
|
33
|
+
export interface CoreSchemaMigrationLoggerV1 {
|
|
34
|
+
log(message: string): void;
|
|
35
|
+
warn(message: string): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface MigrateBailingHubCoreSchemaInputV1 {
|
|
39
|
+
mysql: MysqlConfig;
|
|
40
|
+
/** 测试或受控宿主可注入单连接;传入后连接生命周期仍由调用方负责。 */
|
|
41
|
+
connection?: CoreSchemaMigrationConnectionV1;
|
|
42
|
+
/** 未直接注入连接时可替换连接工厂;生产缺省使用 mysql2/promise。 */
|
|
43
|
+
connectionFactory?: CoreSchemaMigrationConnectionFactoryV1;
|
|
44
|
+
lockName?: string;
|
|
45
|
+
lockTimeoutSeconds?: number;
|
|
46
|
+
logger?: CoreSchemaMigrationLoggerV1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface BailingHubCoreSchemaMigrationResultV1 {
|
|
50
|
+
sourceDirectory: string;
|
|
51
|
+
lockName: string;
|
|
52
|
+
totalFiles: number;
|
|
53
|
+
appliedFiles: string[];
|
|
54
|
+
skippedFiles: string[];
|
|
55
|
+
checksumBackfilledFiles: string[];
|
|
56
|
+
executedStatements: number;
|
|
57
|
+
toleratedStatements: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface MigrationFile {
|
|
61
|
+
filename: string;
|
|
62
|
+
checksum: string;
|
|
63
|
+
statements: string[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface LedgerRow {
|
|
67
|
+
filename: string;
|
|
68
|
+
checksum_sha256: string | null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 显式同步 BailingHub Core 状态库结构。
|
|
73
|
+
*
|
|
74
|
+
* 该函数不会被 Kernel 启动隐式调用;宿主或运维命令必须在自己的发布阶段主动调用。
|
|
75
|
+
* 业务结构的唯一来源始终是 Core 自带的 sql/*.sql,账本表仅是迁移器自举元数据。
|
|
76
|
+
*/
|
|
77
|
+
export async function migrateBailingHubCoreSchema(
|
|
78
|
+
input: MigrateBailingHubCoreSchemaInputV1,
|
|
79
|
+
): Promise<BailingHubCoreSchemaMigrationResultV1> {
|
|
80
|
+
if (input.connection && input.connectionFactory) {
|
|
81
|
+
throw new Error('connection 与 connectionFactory 只能传入一个');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const files = loadOfficialMigrationFiles();
|
|
85
|
+
const sourceDirectory = join(BAILINGHUB_CORE_ROOT, 'sql');
|
|
86
|
+
const lockName = resolveLockName(input);
|
|
87
|
+
const lockTimeoutSeconds = resolveLockTimeout(input.lockTimeoutSeconds);
|
|
88
|
+
const ownsConnection = !input.connection;
|
|
89
|
+
const factory = input.connectionFactory ?? defaultConnectionFactory;
|
|
90
|
+
const connection = input.connection ?? await factory(input.mysql);
|
|
91
|
+
let lockAcquired = false;
|
|
92
|
+
let failed = false;
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const [lockPayload] = await connection.query(
|
|
96
|
+
'SELECT GET_LOCK(?, ?) AS `acquired`',
|
|
97
|
+
[lockName, lockTimeoutSeconds],
|
|
98
|
+
);
|
|
99
|
+
if (numericField(firstRow(lockPayload), 'acquired') !== 1) {
|
|
100
|
+
throw new Error(`无法在 ${lockTimeoutSeconds} 秒内取得 Core Schema 迁移锁`);
|
|
101
|
+
}
|
|
102
|
+
lockAcquired = true;
|
|
103
|
+
|
|
104
|
+
await ensureChecksumLedger(connection);
|
|
105
|
+
const ledger = await loadLedger(connection);
|
|
106
|
+
const retiredMigrations = BAILINGHUB_RETIRED_CORE_MIGRATIONS_V1;
|
|
107
|
+
const knownMigrations = [
|
|
108
|
+
...files.map((file) => ({ filename: file.filename, checksum: file.checksum })),
|
|
109
|
+
...retiredMigrations.map((file) => ({
|
|
110
|
+
filename: file.filename,
|
|
111
|
+
checksum: file.checksumSha256,
|
|
112
|
+
})),
|
|
113
|
+
].sort((left, right) => left.filename.localeCompare(right.filename));
|
|
114
|
+
const knownFiles = new Set(knownMigrations.map((file) => file.filename));
|
|
115
|
+
if (knownFiles.size !== knownMigrations.length) {
|
|
116
|
+
throw new Error('Core Schema 活动迁移与退役迁移证据存在重名文件');
|
|
117
|
+
}
|
|
118
|
+
const unknownFiles = [...ledger.keys()].filter((filename) => !knownFiles.has(filename)).sort();
|
|
119
|
+
if (unknownFiles.length > 0) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
`Core Schema 账本包含当前 artifact 不认识的迁移:${unknownFiles.join(', ')};` +
|
|
122
|
+
'拒绝用较旧或错误的 Core artifact 启动/迁移',
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 先校验所有已有非空摘要,再做补录或新迁移;发现发布文件漂移时不产生后续写入。
|
|
127
|
+
for (const file of knownMigrations) {
|
|
128
|
+
const recorded = ledger.get(file.filename);
|
|
129
|
+
if (recorded === undefined || recorded === null) continue;
|
|
130
|
+
if (normalizeChecksum(recorded) !== file.checksum) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`Core Schema 校验失败:${file.filename} 与账本摘要不一致;` +
|
|
133
|
+
'已发布的 sql 文件不可修改,请新增编号迁移文件',
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const checksumBackfilledFiles: string[] = [];
|
|
139
|
+
for (const file of knownMigrations) {
|
|
140
|
+
if (ledger.get(file.filename) !== null) continue;
|
|
141
|
+
await connection.query(
|
|
142
|
+
'UPDATE `bz_schema_migrations` SET `checksum_sha256`=? ' +
|
|
143
|
+
'WHERE `filename`=? AND `checksum_sha256` IS NULL',
|
|
144
|
+
[file.checksum, file.filename],
|
|
145
|
+
);
|
|
146
|
+
const verified = await loadSingleLedgerChecksum(connection, file.filename);
|
|
147
|
+
if (normalizeChecksum(verified) !== file.checksum) {
|
|
148
|
+
throw new Error(`旧迁移账本摘要补录失败:${file.filename}`);
|
|
149
|
+
}
|
|
150
|
+
ledger.set(file.filename, file.checksum);
|
|
151
|
+
checksumBackfilledFiles.push(file.filename);
|
|
152
|
+
input.logger?.log(`↺ ${file.filename}(旧账本摘要已安全补录,不重放 SQL)`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const appliedFiles: string[] = [];
|
|
156
|
+
const skippedFiles: string[] = [];
|
|
157
|
+
let executedStatements = 0;
|
|
158
|
+
let toleratedStatements = 0;
|
|
159
|
+
|
|
160
|
+
for (const file of files) {
|
|
161
|
+
if (ledger.has(file.filename)) {
|
|
162
|
+
skippedFiles.push(file.filename);
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let fileExecuted = 0;
|
|
167
|
+
let fileTolerated = 0;
|
|
168
|
+
for (const statement of file.statements) {
|
|
169
|
+
try {
|
|
170
|
+
await connection.query(statement);
|
|
171
|
+
fileExecuted++;
|
|
172
|
+
executedStatements++;
|
|
173
|
+
} catch (error) {
|
|
174
|
+
if (
|
|
175
|
+
IDEMPOTENT_MYSQL_ERRNOS.has(mysqlErrno(error))
|
|
176
|
+
&& await verifyDuplicateObject(connection, statement, mysqlErrno(error))
|
|
177
|
+
) {
|
|
178
|
+
fileTolerated++;
|
|
179
|
+
toleratedStatements++;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
throw migrationStatementError(file.filename, statement, error);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
await connection.query(
|
|
187
|
+
'INSERT INTO `bz_schema_migrations` (`filename`, `checksum_sha256`) VALUES (?, ?)',
|
|
188
|
+
[file.filename, file.checksum],
|
|
189
|
+
);
|
|
190
|
+
ledger.set(file.filename, file.checksum);
|
|
191
|
+
appliedFiles.push(file.filename);
|
|
192
|
+
input.logger?.log(
|
|
193
|
+
`✓ ${file.filename}(执行 ${fileExecuted},过渡容错跳过 ${fileTolerated})→ 已记账`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
sourceDirectory,
|
|
199
|
+
lockName,
|
|
200
|
+
totalFiles: files.length,
|
|
201
|
+
appliedFiles,
|
|
202
|
+
skippedFiles,
|
|
203
|
+
checksumBackfilledFiles,
|
|
204
|
+
executedStatements,
|
|
205
|
+
toleratedStatements,
|
|
206
|
+
};
|
|
207
|
+
} catch (error) {
|
|
208
|
+
failed = true;
|
|
209
|
+
throw error;
|
|
210
|
+
} finally {
|
|
211
|
+
let cleanupError: unknown;
|
|
212
|
+
if (lockAcquired) {
|
|
213
|
+
try {
|
|
214
|
+
const [releasePayload] = await connection.query(
|
|
215
|
+
'SELECT RELEASE_LOCK(?) AS `released`',
|
|
216
|
+
[lockName],
|
|
217
|
+
);
|
|
218
|
+
if (numericField(firstRow(releasePayload), 'released') !== 1) {
|
|
219
|
+
throw new Error('Core Schema 迁移锁释放结果异常');
|
|
220
|
+
}
|
|
221
|
+
} catch (error) {
|
|
222
|
+
cleanupError = error;
|
|
223
|
+
if (failed) input.logger?.warn(`迁移失败后的锁释放也失败:${errorMessage(error)}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (ownsConnection) {
|
|
227
|
+
try {
|
|
228
|
+
await connection.end();
|
|
229
|
+
} catch (error) {
|
|
230
|
+
cleanupError ??= error;
|
|
231
|
+
if (failed) input.logger?.warn(`迁移失败后的数据库连接关闭也失败:${errorMessage(error)}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (!failed && cleanupError !== undefined) throw cleanupError;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function verifyDuplicateObject(
|
|
239
|
+
connection: CoreSchemaMigrationConnectionV1,
|
|
240
|
+
statement: string,
|
|
241
|
+
errno: number,
|
|
242
|
+
): Promise<boolean> {
|
|
243
|
+
const table = /\bALTER\s+TABLE\s+`?([a-zA-Z0-9_]+)`?/i.exec(statement)?.[1];
|
|
244
|
+
if (!table) return false;
|
|
245
|
+
if (errno === 1060) return await verifyDuplicateColumns(connection, table, statement);
|
|
246
|
+
if (errno === 1061) return await verifyDuplicateIndexes(connection, table, statement);
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function verifyDuplicateColumns(
|
|
251
|
+
connection: CoreSchemaMigrationConnectionV1,
|
|
252
|
+
table: string,
|
|
253
|
+
statement: string,
|
|
254
|
+
): Promise<boolean> {
|
|
255
|
+
const additions = [...statement.matchAll(
|
|
256
|
+
/\bADD\s+COLUMN\s+`?([a-zA-Z0-9_]+)`?\s+([\s\S]*?)(?=,\s*ADD\s+COLUMN\b|$)/gi,
|
|
257
|
+
)];
|
|
258
|
+
if (additions.length === 0) return false;
|
|
259
|
+
const [payload] = await connection.query(`SHOW FULL COLUMNS FROM \`${table}\``);
|
|
260
|
+
const columns = new Map(rows(payload).map((row) => [String(row['Field'] ?? row['field'] ?? ''), row]));
|
|
261
|
+
return additions.every((addition) => {
|
|
262
|
+
const name = addition[1]!;
|
|
263
|
+
const definition = addition[2]!.trim();
|
|
264
|
+
const actual = columns.get(name);
|
|
265
|
+
if (!actual) return false;
|
|
266
|
+
const expectedType = /^([a-zA-Z]+(?:\s*\([^)]*\))?(?:\s+UNSIGNED)?)/i.exec(definition)?.[1];
|
|
267
|
+
if (!expectedType) return false;
|
|
268
|
+
if (normalizeSqlType(actual['Type'] ?? actual['type']) !== normalizeSqlType(expectedType)) return false;
|
|
269
|
+
const expectedNull = /\bNOT\s+NULL\b/i.test(definition) ? 'NO' : 'YES';
|
|
270
|
+
if (String(actual['Null'] ?? actual['null'] ?? '').toUpperCase() !== expectedNull) return false;
|
|
271
|
+
const defaultMatch = /\bDEFAULT\s+(NULL|'(?:''|[^'])*'|[^\s,]+)/i.exec(definition);
|
|
272
|
+
if (!defaultMatch) return true;
|
|
273
|
+
return normalizeSqlDefault(actual['Default'] ?? actual['default']) === normalizeSqlDefault(defaultMatch[1]);
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async function verifyDuplicateIndexes(
|
|
278
|
+
connection: CoreSchemaMigrationConnectionV1,
|
|
279
|
+
table: string,
|
|
280
|
+
statement: string,
|
|
281
|
+
): Promise<boolean> {
|
|
282
|
+
const additions = [...statement.matchAll(
|
|
283
|
+
/\bADD\s+(UNIQUE\s+)?(?:KEY|INDEX)\s+`?([a-zA-Z0-9_]+)`?\s*\(([^)]+)\)/gi,
|
|
284
|
+
)];
|
|
285
|
+
if (additions.length === 0) return false;
|
|
286
|
+
for (const addition of additions) {
|
|
287
|
+
const unique = Boolean(addition[1]);
|
|
288
|
+
const name = addition[2]!;
|
|
289
|
+
const expectedColumns = addition[3]!.split(',').map((part) => part.replace(/`/g, '').trim().split(/\s+/)[0]!);
|
|
290
|
+
const [payload] = await connection.query(`SHOW INDEX FROM \`${table}\` WHERE Key_name = ?`, [name]);
|
|
291
|
+
const indexRows = rows(payload).sort((a, b) => Number(a['Seq_in_index'] ?? 0) - Number(b['Seq_in_index'] ?? 0));
|
|
292
|
+
if (indexRows.length !== expectedColumns.length) return false;
|
|
293
|
+
if (indexRows.some((row) => Number(row['Non_unique'] ?? 1) !== (unique ? 0 : 1))) return false;
|
|
294
|
+
if (indexRows.some((row, index) => String(row['Column_name'] ?? '') !== expectedColumns[index])) return false;
|
|
295
|
+
}
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function normalizeSqlType(value: unknown): string {
|
|
300
|
+
return String(value ?? '').toLowerCase().replace(/\s+/g, ' ').replace(/\s*\(\s*/g, '(').replace(/\s*\)/g, ')').trim();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function normalizeSqlDefault(value: unknown): string {
|
|
304
|
+
if (value === null || value === undefined || String(value).toUpperCase() === 'NULL') return 'null';
|
|
305
|
+
const text = String(value);
|
|
306
|
+
if (text.startsWith("'") && text.endsWith("'")) return text.slice(1, -1).replace(/''/g, "'");
|
|
307
|
+
return text;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function loadOfficialMigrationFiles(): MigrationFile[] {
|
|
311
|
+
const directory = join(BAILINGHUB_CORE_ROOT, 'sql');
|
|
312
|
+
let filenames: string[];
|
|
313
|
+
try {
|
|
314
|
+
filenames = readdirSync(directory).filter((name) => name.endsWith('.sql')).sort();
|
|
315
|
+
assertOfficialMigrationFilenames(filenames);
|
|
316
|
+
} catch (error) {
|
|
317
|
+
throw new Error(`无法读取 Core 官方迁移目录:${directory}`, { cause: error });
|
|
318
|
+
}
|
|
319
|
+
if (filenames.length === 0) {
|
|
320
|
+
throw new Error(`Core 官方迁移目录中没有 sql 文件:${directory}`);
|
|
321
|
+
}
|
|
322
|
+
return filenames.map((filename) => {
|
|
323
|
+
const content = readFileSync(join(directory, filename), 'utf8');
|
|
324
|
+
return {
|
|
325
|
+
filename,
|
|
326
|
+
checksum: createHash('sha256').update(content, 'utf8').digest('hex'),
|
|
327
|
+
statements: splitStatements(content),
|
|
328
|
+
};
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// 官方迁移文件不包含存储过程,也不在字符串中使用分号;维持旧 init-db 的拆分语义。
|
|
333
|
+
function splitStatements(text: string): string[] {
|
|
334
|
+
const withoutFullLineComments = text
|
|
335
|
+
.split('\n')
|
|
336
|
+
.filter((line) => !line.trim().startsWith('--'))
|
|
337
|
+
.join('\n');
|
|
338
|
+
return withoutFullLineComments.split(';').map((statement) => statement.trim()).filter(Boolean);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async function ensureChecksumLedger(connection: CoreSchemaMigrationConnectionV1): Promise<void> {
|
|
342
|
+
await connection.query(CREATE_LEDGER_SQL);
|
|
343
|
+
try {
|
|
344
|
+
await connection.query(ADD_LEDGER_CHECKSUM_SQL);
|
|
345
|
+
} catch (error) {
|
|
346
|
+
if (!IDEMPOTENT_MYSQL_ERRNOS.has(mysqlErrno(error))) throw error;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
async function loadLedger(
|
|
351
|
+
connection: CoreSchemaMigrationConnectionV1,
|
|
352
|
+
): Promise<Map<string, string | null>> {
|
|
353
|
+
const [payload] = await connection.query(
|
|
354
|
+
'SELECT `filename`, `checksum_sha256` FROM `bz_schema_migrations`',
|
|
355
|
+
);
|
|
356
|
+
const ledger = new Map<string, string | null>();
|
|
357
|
+
for (const raw of rows(payload)) {
|
|
358
|
+
const row = raw as Partial<LedgerRow>;
|
|
359
|
+
const filename = String(row.filename ?? '').trim();
|
|
360
|
+
if (!filename) continue;
|
|
361
|
+
ledger.set(
|
|
362
|
+
filename,
|
|
363
|
+
row.checksum_sha256 === null || row.checksum_sha256 === undefined
|
|
364
|
+
? null
|
|
365
|
+
: String(row.checksum_sha256),
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
return ledger;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async function loadSingleLedgerChecksum(
|
|
372
|
+
connection: CoreSchemaMigrationConnectionV1,
|
|
373
|
+
filename: string,
|
|
374
|
+
): Promise<string | null | undefined> {
|
|
375
|
+
const [payload] = await connection.query(
|
|
376
|
+
'SELECT `checksum_sha256` FROM `bz_schema_migrations` WHERE `filename`=? LIMIT 1',
|
|
377
|
+
[filename],
|
|
378
|
+
);
|
|
379
|
+
const row = firstRow(payload) as Partial<LedgerRow> | undefined;
|
|
380
|
+
return row?.checksum_sha256;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function resolveLockName(input: MigrateBailingHubCoreSchemaInputV1): string {
|
|
384
|
+
const explicit = input.lockName?.trim();
|
|
385
|
+
if (explicit !== undefined) {
|
|
386
|
+
if (!explicit || Buffer.byteLength(explicit, 'utf8') > 64) {
|
|
387
|
+
throw new Error('Core Schema 迁移锁名称必须为 1~64 字节');
|
|
388
|
+
}
|
|
389
|
+
return explicit;
|
|
390
|
+
}
|
|
391
|
+
// GET_LOCK 的命名空间在 MySQL 服务器内;只按库名派生,确保同库经 IP/域名等不同主机别名连接仍竞争同一把锁。
|
|
392
|
+
const identity = input.mysql.database;
|
|
393
|
+
const digest = createHash('sha256').update(identity, 'utf8').digest('hex').slice(0, 40);
|
|
394
|
+
return `bailinghub:core-schema:${digest}`;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function resolveLockTimeout(value: number | undefined): number {
|
|
398
|
+
const timeout = value ?? DEFAULT_LOCK_TIMEOUT_SECONDS;
|
|
399
|
+
if (!Number.isInteger(timeout) || timeout < 0 || timeout > 300) {
|
|
400
|
+
throw new Error('lockTimeoutSeconds 必须是 0~300 的整数');
|
|
401
|
+
}
|
|
402
|
+
return timeout;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
async function defaultConnectionFactory(
|
|
406
|
+
mysqlConfig: MysqlConfig,
|
|
407
|
+
): Promise<CoreSchemaMigrationConnectionV1> {
|
|
408
|
+
const mysql = await import('mysql2/promise');
|
|
409
|
+
const raw = await mysql.createConnection({
|
|
410
|
+
host: mysqlConfig.host,
|
|
411
|
+
port: mysqlConfig.port,
|
|
412
|
+
user: mysqlConfig.user,
|
|
413
|
+
password: mysqlConfig.password,
|
|
414
|
+
database: mysqlConfig.database,
|
|
415
|
+
});
|
|
416
|
+
return {
|
|
417
|
+
async query(sql, params = []) {
|
|
418
|
+
const [payload, fields] = await raw.query(sql, params);
|
|
419
|
+
return [payload, fields];
|
|
420
|
+
},
|
|
421
|
+
async end() {
|
|
422
|
+
await raw.end();
|
|
423
|
+
},
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function rows(payload: unknown): Array<Record<string, unknown>> {
|
|
428
|
+
if (!Array.isArray(payload)) return [];
|
|
429
|
+
return payload.filter(
|
|
430
|
+
(row): row is Record<string, unknown> => Boolean(row) && typeof row === 'object',
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function firstRow(payload: unknown): Record<string, unknown> | undefined {
|
|
435
|
+
return rows(payload)[0];
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function numericField(row: Record<string, unknown> | undefined, field: string): number {
|
|
439
|
+
return Number(row?.[field]);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function normalizeChecksum(value: string | null | undefined): string | null | undefined {
|
|
443
|
+
return value === null || value === undefined ? value : value.trim().toLowerCase();
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function mysqlErrno(error: unknown): number {
|
|
447
|
+
if (!error || typeof error !== 'object' || !('errno' in error)) return Number.NaN;
|
|
448
|
+
return Number((error as { errno?: unknown }).errno);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function errorMessage(error: unknown): string {
|
|
452
|
+
return error instanceof Error ? error.message : String(error);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function migrationStatementError(filename: string, statement: string, cause: unknown): Error {
|
|
456
|
+
const preview = statement.replace(/\s+/g, ' ').slice(0, 120);
|
|
457
|
+
return new Error(
|
|
458
|
+
`${filename} 执行失败:${errorMessage(cause)};语句:${preview}${statement.length > 120 ? '…' : ''}`,
|
|
459
|
+
{ cause },
|
|
460
|
+
);
|
|
461
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import { assertOfficialMigrationFilenames } from './migration-files';
|
|
4
|
+
|
|
5
|
+
test('official migration filenames require one unique three-digit number', () => {
|
|
6
|
+
assert.doesNotThrow(() => assertOfficialMigrationFilenames([
|
|
7
|
+
'001_init.sql',
|
|
8
|
+
'003_add_jobs.sql',
|
|
9
|
+
]));
|
|
10
|
+
assert.throws(
|
|
11
|
+
() => assertOfficialMigrationFilenames(['001_init.sql', 'notes.sql']),
|
|
12
|
+
/非规范文件:notes\.sql/,
|
|
13
|
+
);
|
|
14
|
+
assert.throws(
|
|
15
|
+
() => assertOfficialMigrationFilenames(['003_jobs.sql', '003_routes.sql']),
|
|
16
|
+
/迁移编号重复:003=/,
|
|
17
|
+
);
|
|
18
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const OFFICIAL_MIGRATION_FILENAME = /^(\d{3})_[a-z0-9][a-z0-9_-]*\.sql$/;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runtime fail-closed validation for the migration artifact itself.
|
|
5
|
+
* Release checks are useful evidence, but a host must not execute an
|
|
6
|
+
* unclassified or duplicate-number SQL file merely because it ends in .sql.
|
|
7
|
+
*/
|
|
8
|
+
export function assertOfficialMigrationFilenames(filenames: readonly string[]): void {
|
|
9
|
+
const invalid = filenames.filter((filename) => !OFFICIAL_MIGRATION_FILENAME.test(filename));
|
|
10
|
+
if (invalid.length > 0) {
|
|
11
|
+
throw new Error(`Core 官方迁移包含非规范文件:${invalid.sort().join(', ')}`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const byNumber = new Map<string, string[]>();
|
|
15
|
+
for (const filename of filenames) {
|
|
16
|
+
const number = OFFICIAL_MIGRATION_FILENAME.exec(filename)![1]!;
|
|
17
|
+
const group = byNumber.get(number) ?? [];
|
|
18
|
+
group.push(filename);
|
|
19
|
+
byNumber.set(number, group);
|
|
20
|
+
}
|
|
21
|
+
const duplicates = [...byNumber]
|
|
22
|
+
.filter(([, files]) => files.length > 1)
|
|
23
|
+
.map(([number, files]) => `${number}=[${files.sort().join(', ')}]`);
|
|
24
|
+
if (duplicates.length > 0) {
|
|
25
|
+
throw new Error(`Core 官方迁移编号重复:${duplicates.join('; ')}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checksums for migrations that existed in pre-public deployments but are no
|
|
3
|
+
* longer part of the active, replayable Core migration sequence.
|
|
4
|
+
*
|
|
5
|
+
* These records are compatibility evidence only. Never restore the SQL files
|
|
6
|
+
* to sql/, and never execute them for a fresh schema.
|
|
7
|
+
*/
|
|
8
|
+
export interface BailingHubRetiredCoreMigrationV1 {
|
|
9
|
+
readonly filename: string;
|
|
10
|
+
readonly checksumSha256: string;
|
|
11
|
+
readonly byteLength: number;
|
|
12
|
+
readonly replay: 'never';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const BAILINGHUB_RETIRED_CORE_MIGRATIONS_V1: readonly BailingHubRetiredCoreMigrationV1[] =
|
|
16
|
+
Object.freeze([
|
|
17
|
+
Object.freeze({
|
|
18
|
+
filename: '020_provider_sign_version.sql',
|
|
19
|
+
checksumSha256: '9b4ed1ca632a0dcca92a768671584cbc29d09a4d6501697631d355616f15a21e',
|
|
20
|
+
byteLength: 564,
|
|
21
|
+
replay: 'never' as const,
|
|
22
|
+
}),
|
|
23
|
+
Object.freeze({
|
|
24
|
+
filename: '021_drop_provider_sign_version.sql',
|
|
25
|
+
checksumSha256: '513ba9dfe031e785da09d931405cbcf867f703b2b7a6d0ec7077719082206466',
|
|
26
|
+
byteLength: 407,
|
|
27
|
+
replay: 'never' as const,
|
|
28
|
+
}),
|
|
29
|
+
Object.freeze({
|
|
30
|
+
filename: '037_route_tools_shape.sql',
|
|
31
|
+
checksumSha256: 'da87330abb088868827445a425e30bc6928b8ee47d4aeb680784fcf8d0a11fb4',
|
|
32
|
+
byteLength: 1181,
|
|
33
|
+
replay: 'never' as const,
|
|
34
|
+
}),
|
|
35
|
+
]);
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { mkdtempSync, rmSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import test from 'node:test';
|
|
6
|
+
import type { AppConfig } from '../../core/config/config';
|
|
7
|
+
import type { Job } from '../../core/contracts/types';
|
|
8
|
+
import { ObservabilityLedger } from '../config/config-observability-ledger';
|
|
9
|
+
import { JsonlStore } from './state-jsonl';
|
|
10
|
+
import { MysqlStore } from './state-mysql';
|
|
11
|
+
|
|
12
|
+
const NOW = Date.parse('2026-07-24T08:00:00.000Z');
|
|
13
|
+
|
|
14
|
+
function job(input: Partial<Job> & Pick<Job, 'job_id' | 'status'>): Job {
|
|
15
|
+
return {
|
|
16
|
+
request_id: `req-${input.job_id}`,
|
|
17
|
+
profile: 'readonly',
|
|
18
|
+
project: 'demo',
|
|
19
|
+
source: 'test',
|
|
20
|
+
input_preview: 'test',
|
|
21
|
+
metadata: {},
|
|
22
|
+
created_at: '2026-07-24T07:59:30.000Z',
|
|
23
|
+
updated_at: '2026-07-24T07:59:30.000Z',
|
|
24
|
+
...input,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
test('JSONL operational snapshot: aggregates lifecycle, queue and lease health without exposing records', async () => {
|
|
29
|
+
const root = mkdtempSync(join(tmpdir(), 'bailing-metrics-jsonl-'));
|
|
30
|
+
try {
|
|
31
|
+
const store = new JsonlStore(join(root, 'jobs.jsonl'));
|
|
32
|
+
await store.init();
|
|
33
|
+
await store.createJob(job({
|
|
34
|
+
job_id: 'queued',
|
|
35
|
+
status: 'queued',
|
|
36
|
+
thread_id: 7,
|
|
37
|
+
run_after: '2026-07-24T08:01:00.000Z',
|
|
38
|
+
}));
|
|
39
|
+
await store.createJob(job({
|
|
40
|
+
job_id: 'monitor',
|
|
41
|
+
status: 'queued',
|
|
42
|
+
source: 'monitor',
|
|
43
|
+
created_at: '2026-07-23T00:00:00.000Z',
|
|
44
|
+
}));
|
|
45
|
+
await store.createJob(job({
|
|
46
|
+
job_id: 'running',
|
|
47
|
+
status: 'running',
|
|
48
|
+
thread_id: 7,
|
|
49
|
+
lease_until: '2026-07-24T07:59:59.000Z',
|
|
50
|
+
}));
|
|
51
|
+
await store.createJob(job({ job_id: 'done', status: 'done', updated_at: '2026-07-24T07:55:00.000Z' }));
|
|
52
|
+
await store.createJob(job({ job_id: 'error', status: 'error', updated_at: '2026-07-24T07:30:00.000Z' }));
|
|
53
|
+
await store.createJob(job({ job_id: 'rejected', status: 'rejected', updated_at: '2026-07-24T07:59:00.000Z' }));
|
|
54
|
+
|
|
55
|
+
const snapshot = await store.operationalMetricsSnapshot(NOW);
|
|
56
|
+
assert.deepEqual(snapshot.byStatus, { queued: 2, running: 1, dispatched: 0, done: 1, error: 1, rejected: 1 });
|
|
57
|
+
assert.deepEqual(snapshot.terminalLast15m, { done: 1, error: 0, rejected: 1 });
|
|
58
|
+
assert.equal(snapshot.oldestQueuedAgeSeconds, 30);
|
|
59
|
+
assert.equal(snapshot.delayedQueuedJobs, 1);
|
|
60
|
+
assert.equal(snapshot.expiredLeases, 1);
|
|
61
|
+
assert.equal(snapshot.blockedThreads, 1);
|
|
62
|
+
} finally {
|
|
63
|
+
rmSync(root, { recursive: true, force: true });
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('MySQL operational snapshot: uses one aggregate query and maps the result', async () => {
|
|
68
|
+
const calls: Array<{ sql: string; params: unknown[] }> = [];
|
|
69
|
+
const store = new MysqlStore({} as AppConfig['state']['mysql']);
|
|
70
|
+
Reflect.set(store, 'pool', {
|
|
71
|
+
query: async (sql: string, params: unknown[]) => {
|
|
72
|
+
calls.push({ sql, params });
|
|
73
|
+
return [[{
|
|
74
|
+
queued: 2, running: 1, dispatched: 3, done: 4, error: 5, rejected: 6,
|
|
75
|
+
done_15m: 2, error_15m: 1, rejected_15m: 0,
|
|
76
|
+
oldest_queued_at: '2026-07-24T07:59:30.000Z',
|
|
77
|
+
delayed_queued: 1, expired_leases: 2, blocked_threads: 3,
|
|
78
|
+
}], []];
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const snapshot = await store.operationalMetricsSnapshot(NOW);
|
|
83
|
+
assert.equal(calls.length, 1);
|
|
84
|
+
assert.match(calls[0]!.sql, /COUNT\(DISTINCT q\.thread_id\)/);
|
|
85
|
+
assert.equal(calls[0]!.params.length, 5);
|
|
86
|
+
assert.deepEqual(snapshot.byStatus, { queued: 2, running: 1, dispatched: 3, done: 4, error: 5, rejected: 6 });
|
|
87
|
+
assert.deepEqual(snapshot.terminalLast15m, { done: 2, error: 1, rejected: 0 });
|
|
88
|
+
assert.equal(snapshot.oldestQueuedAgeSeconds, 30);
|
|
89
|
+
assert.equal(snapshot.blockedThreads, 3);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('control-plane operational snapshot: counts approvals and executor heartbeat states in one query', async () => {
|
|
93
|
+
const calls: Array<{ sql: string; params: unknown[] }> = [];
|
|
94
|
+
const ledger = new ObservabilityLedger(() => ({
|
|
95
|
+
query: async (sql: string, params: unknown[]) => {
|
|
96
|
+
calls.push({ sql, params });
|
|
97
|
+
return [[{ pending_approvals: 4, executors_online: 2, executors_offline: 5 }], []];
|
|
98
|
+
},
|
|
99
|
+
}));
|
|
100
|
+
|
|
101
|
+
const snapshot = await ledger.operationalMetricsSnapshot(NOW);
|
|
102
|
+
assert.deepEqual(snapshot, { pendingApprovals: 4, executorsOnline: 2, executorsOffline: 5 });
|
|
103
|
+
assert.equal(calls.length, 1);
|
|
104
|
+
assert.match(calls[0]!.sql, /bz_tool_approvals/);
|
|
105
|
+
assert.match(calls[0]!.sql, /bz_executors/);
|
|
106
|
+
assert.equal(calls[0]!.params.length, 2);
|
|
107
|
+
});
|