bugx-cli 0.0.0-stage → 0.1.1
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/LICENSE +55 -0
- package/README.md +284 -2
- package/core/CATALOG.json +141 -0
- package/core/README.md +254 -0
- package/core/adapters/README.md +35 -0
- package/core/adapters/android-interaction.json +81 -0
- package/core/adapters/android.json +24 -0
- package/core/adapters/desktop-macos.json +20 -0
- package/core/adapters/ios-interaction.json +34 -0
- package/core/adapters/ios.json +23 -0
- package/core/adapters/memory-macos.json +24 -0
- package/core/agent/README.md +51 -0
- package/core/agent/__init__.py +146 -0
- package/core/agent/agent_artifacts.py +766 -0
- package/core/agent/agent_eval.py +1155 -0
- package/core/agent/agent_execution.py +718 -0
- package/core/agent/agent_orchestrator.py +1351 -0
- package/core/agent/agent_targets.py +877 -0
- package/core/agent/agent_workspace.py +770 -0
- package/core/agent/feature_baseline.py +150 -0
- package/core/assets/README.md +24 -0
- package/core/assets/axe-core/LICENSE-3RD-PARTY.txt +66 -0
- package/core/assets/axe-core/LICENSE.txt +362 -0
- package/core/assets/axe-core/UPSTREAM.md +10 -0
- package/core/assets/axe-core/axe.min.js +12 -0
- package/core/assets/bugx-avatar-1024.png +0 -0
- package/core/assets/bugx-avatar-512.png +0 -0
- package/core/assets/bugx-avatar-tight.svg +23 -0
- package/core/assets/bugx-avatar.svg +28 -0
- package/core/assets/bugx-hero-cn.png +0 -0
- package/core/assets/bugx-hero-en.png +0 -0
- package/core/assets/bugx-hero-en.svg +74 -0
- package/core/assets/bugx-hero-zh.svg +74 -0
- package/core/assets/codex.png +0 -0
- package/core/assets/dsh.png +0 -0
- package/core/assets/project-layout.md +42 -0
- package/core/assets/webui-chatgpt.jpg +0 -0
- package/core/assets/webui-session.jpg +0 -0
- package/core/assets/webui-skills.jpg +0 -0
- package/core/assets/webui-studio.jpg +0 -0
- package/core/assets/webui-zcode.jpg +0 -0
- package/core/assets/zcode.png +0 -0
- package/core/audit_packs/README.md +37 -0
- package/core/audit_packs/data-audit/manifest.json +94 -0
- package/core/audit_packs/data-audit/stages/00-scope.md +14 -0
- package/core/audit_packs/data-audit/stages/10-schema-inventory.md +14 -0
- package/core/audit_packs/data-audit/stages/20-query-and-integrity.md +14 -0
- package/core/audit_packs/data-audit/stages/80-evidence-regression.md +14 -0
- package/core/audit_packs/desktop-audit/manifest.json +123 -0
- package/core/audit_packs/desktop-audit/stages/00-scope.md +15 -0
- package/core/audit_packs/desktop-audit/stages/10-process-model.md +14 -0
- package/core/audit_packs/desktop-audit/stages/20-package-integrity.md +15 -0
- package/core/audit_packs/desktop-audit/stages/30-ui-behavior.md +14 -0
- package/core/audit_packs/desktop-audit/stages/80-evidence-regression.md +15 -0
- package/core/audit_packs/memory-testing.md +227 -0
- package/core/audit_packs/mobile-audit/manifest.json +155 -0
- package/core/audit_packs/mobile-audit/modules/android.md +19 -0
- package/core/audit_packs/mobile-audit/modules/h5.md +18 -0
- package/core/audit_packs/mobile-audit/modules/ios.md +25 -0
- package/core/audit_packs/mobile-audit/modules/miniprogram.md +26 -0
- package/core/audit_packs/mobile-audit/modules/webview.md +18 -0
- package/core/audit_packs/mobile-audit/stages/00-scope.md +14 -0
- package/core/audit_packs/mobile-audit/stages/10-platform-inventory.md +14 -0
- package/core/audit_packs/mobile-audit/stages/20-masvs-static.md +20 -0
- package/core/audit_packs/mobile-audit/stages/30-component-exposure.md +14 -0
- package/core/audit_packs/mobile-audit/stages/40-device-behavior.md +14 -0
- package/core/audit_packs/mobile-audit/stages/80-evidence-regression.md +13 -0
- package/core/audit_packs/native-audit/manifest.json +127 -0
- package/core/audit_packs/native-audit/stages/00-scope.md +14 -0
- package/core/audit_packs/native-audit/stages/10-static-white-box.md +14 -0
- package/core/audit_packs/native-audit/stages/20-memory-concurrency.md +15 -0
- package/core/audit_packs/native-audit/stages/30-behavior-black-box.md +39 -0
- package/core/audit_packs/native-audit/stages/80-evidence-regression.md +14 -0
- package/core/audit_packs/platform-testing.md +174 -0
- package/core/audit_packs/web-audit/manifest.json +191 -0
- package/core/audit_packs/web-audit/modules/graphql.md +5 -0
- package/core/audit_packs/web-audit/modules/h5.md +18 -0
- package/core/audit_packs/web-audit/modules/multi-tenant.md +4 -0
- package/core/audit_packs/web-audit/modules/oauth.md +4 -0
- package/core/audit_packs/web-audit/modules/upload.md +4 -0
- package/core/audit_packs/web-audit/modules/websocket.md +4 -0
- package/core/audit_packs/web-audit/stages/00-scope.md +13 -0
- package/core/audit_packs/web-audit/stages/10-discovery.md +13 -0
- package/core/audit_packs/web-audit/stages/20-auth-session.md +13 -0
- package/core/audit_packs/web-audit/stages/30-authz-business.md +13 -0
- package/core/audit_packs/web-audit/stages/40-input-output.md +13 -0
- package/core/audit_packs/web-audit/stages/50-state-async.md +13 -0
- package/core/audit_packs/web-audit/stages/60-file-ssrf-deser.md +12 -0
- package/core/audit_packs/web-audit/stages/70-config-dependency.md +12 -0
- package/core/audit_packs/web-audit/stages/80-evidence-regression.md +12 -0
- package/core/autonomous_cve/INTERFACE.md +61 -0
- package/core/autonomous_cve/README.md +58 -0
- package/core/autonomous_cve/SPEC.md +25 -0
- package/core/autonomous_cve/__init__.py +109 -0
- package/core/autonomous_cve/attack_graph.py +1096 -0
- package/core/autonomous_cve/attack_graph_store.py +1868 -0
- package/core/autonomous_cve/audit_campaign.py +1232 -0
- package/core/autonomous_cve/behavior_graph.py +465 -0
- package/core/autonomous_cve/ghsa_advisories.py +174 -0
- package/core/autonomous_cve/models.py +93 -0
- package/core/autonomous_cve/mutator.py +87 -0
- package/core/autonomous_cve/patcher.py +126 -0
- package/core/autonomous_cve/scout.py +278 -0
- package/core/autonomous_cve/synthesizer.py +207 -0
- package/core/autonomous_cve/verifier.py +103 -0
- package/core/bugx.md +1299 -0
- package/core/bugx_paths.py +1881 -0
- package/core/cli-help.txt +66 -0
- package/core/cloud_sync/INTERFACE.md +32 -0
- package/core/cloud_sync/README.md +62 -0
- package/core/cloud_sync/SPEC.md +25 -0
- package/core/cloud_sync/__init__.py +23 -0
- package/core/cloud_sync/auto_sync.py +398 -0
- package/core/cloud_sync/cloud_client.py +1218 -0
- package/core/cloud_sync/cloud_onboard.py +743 -0
- package/core/cloud_sync/cloud_schema.py +648 -0
- package/core/cloud_sync/consent.py +81 -0
- package/core/cloud_sync/device_identity.py +307 -0
- package/core/cloud_sync/endpoint_router.py +415 -0
- package/core/cloud_sync/flywheel_client.py +769 -0
- package/core/cloud_sync/network_transport.py +296 -0
- package/core/cloud_sync/onboard_wizard.py +493 -0
- package/core/cloud_sync/sensitive_filter.py +285 -0
- package/core/cloud_sync/sync_pull.py +695 -0
- package/core/cloud_sync/sync_push.py +723 -0
- package/core/contracts/AGENT_GUIDE.md +359 -0
- package/core/contracts/README.md +34 -0
- package/core/contracts/bugx-blackbox.md +541 -0
- package/core/contracts/bugx-opencode.md +469 -0
- package/core/contracts/bugx-quick.md +44 -0
- package/core/contracts/bugx-runtime-contract.md +353 -0
- package/core/contracts/bugx-worker.md +470 -0
- package/core/contracts/engine_gap_contracts.py +473 -0
- package/core/coverage/README.md +70 -0
- package/core/coverage/__init__.py +106 -0
- package/core/coverage/coverage_ai_review.py +185 -0
- package/core/coverage/coverage_backfill.py +54 -0
- package/core/coverage/coverage_bootstrap.py +713 -0
- package/core/coverage/coverage_business.py +36 -0
- package/core/coverage/coverage_cli.py +715 -0
- package/core/coverage/coverage_discovery.py +1429 -0
- package/core/coverage/coverage_evidence.py +1024 -0
- package/core/coverage/coverage_forensics.py +124 -0
- package/core/coverage/coverage_gate.py +277 -0
- package/core/coverage/coverage_intelligence.py +734 -0
- package/core/coverage/coverage_manifest.py +396 -0
- package/core/coverage/coverage_platform.py +77 -0
- package/core/coverage/coverage_validate.py +241 -0
- package/core/coverage/module_coverage.py +157 -0
- package/core/dev_harness/INTERFACE.md +20 -0
- package/core/dev_harness/README.md +21 -0
- package/core/dev_harness/SPEC.md +25 -0
- package/core/dev_harness/__init__.py +5 -0
- package/core/dev_harness/interaction_preset.py +408 -0
- package/core/engine-integrity.json +2797 -0
- package/core/fuzzing/README.md +49 -0
- package/core/fuzzing/__init__.py +82 -0
- package/core/fuzzing/concurrency_fuzzer.py +435 -0
- package/core/fuzzing/differential_oracle.py +1164 -0
- package/core/fuzzing/fuzz_input.py +1291 -0
- package/core/fuzzing/stateful_fuzzer.py +404 -0
- package/core/governance/README.md +61 -0
- package/core/governance/__init__.py +130 -0
- package/core/governance/capacity_planner.py +124 -0
- package/core/governance/dependency_lock.py +317 -0
- package/core/governance/goal_governor.py +754 -0
- package/core/governance/guide_budget.py +458 -0
- package/core/governance/lockdown.sh +50 -0
- package/core/governance/readonly_enforcer.py +154 -0
- package/core/governance/runtime_tool_gate.py +373 -0
- package/core/governance/secret_redactor.py +784 -0
- package/core/governance/token_budget.py +38 -0
- package/core/host_acp/INTERFACE.md +36 -0
- package/core/host_acp/README.md +51 -0
- package/core/host_acp/SPEC.md +25 -0
- package/core/host_acp/__init__.py +121 -0
- package/core/host_acp/base.py +111 -0
- package/core/host_acp/claude.py +99 -0
- package/core/host_acp/cline.py +298 -0
- package/core/host_acp/codex.py +300 -0
- package/core/host_acp/dsh.py +106 -0
- package/core/host_acp/opencode.py +209 -0
- package/core/host_acp/pi.py +94 -0
- package/core/host_acp/zcode.py +99 -0
- package/core/host_runtime/INTERFACE.md +24 -0
- package/core/host_runtime/README.md +62 -0
- package/core/host_runtime/SPEC.md +25 -0
- package/core/host_runtime/__init__.py +21 -0
- package/core/host_runtime/adapters.py +412 -0
- package/core/host_runtime/autonomous_runner.py +1152 -0
- package/core/host_runtime/cline.py +448 -0
- package/core/host_runtime/common.py +103 -0
- package/core/host_runtime/free_models.py +786 -0
- package/core/host_runtime/host_bootstrap.py +389 -0
- package/core/host_runtime/host_evaluators.py +761 -0
- package/core/host_runtime/host_probe.py +41 -0
- package/core/host_runtime/host_provider_arbitrator.py +831 -0
- package/core/host_runtime/host_registry.py +662 -0
- package/core/host_runtime/host_usage.py +127 -0
- package/core/host_runtime/mcp_process_tracker.py +509 -0
- package/core/host_runtime/mcp_registry_gate.py +379 -0
- package/core/host_runtime/pi.py +102 -0
- package/core/host_runtime/provider_facts.py +66 -0
- package/core/hosts/INTERFACE.md +51 -0
- package/core/hosts/README.md +67 -0
- package/core/hosts/SPEC.md +25 -0
- package/core/hosts/__init__.py +140 -0
- package/core/hosts/_base_usage.py +152 -0
- package/core/hosts/_codex_usage.py +297 -0
- package/core/hosts/_dsh_usage.py +184 -0
- package/core/hosts/_mcp_toml.py +365 -0
- package/core/hosts/_zcode_usage.py +269 -0
- package/core/hosts/base.py +479 -0
- package/core/hosts/claude.py +212 -0
- package/core/hosts/cline.py +229 -0
- package/core/hosts/codex.py +1006 -0
- package/core/hosts/dsh.py +526 -0
- package/core/hosts/opencode.py +358 -0
- package/core/hosts/pi.py +308 -0
- package/core/hosts/zcode.py +219 -0
- package/core/i18n.py +252 -0
- package/core/infra/README.md +41 -0
- package/core/infra/__init__.py +17 -0
- package/core/infra/build_info.py +105 -0
- package/core/infra/client_metadata.py +185 -0
- package/core/infra/json_utils.py +22 -0
- package/core/infra/python_runtime.py +540 -0
- package/core/infra/safe_xml.py +221 -0
- package/core/launch_bugx.py +118 -0
- package/core/launch_phases/INTERFACE.md +31 -0
- package/core/launch_phases/README.md +106 -0
- package/core/launch_phases/SPEC.md +25 -0
- package/core/launch_phases/__init__.py +29 -0
- package/core/launch_phases/candidate_revalidation.py +484 -0
- package/core/launch_phases/common.py +1060 -0
- package/core/launch_phases/finding_confirm_phase.py +391 -0
- package/core/launch_phases/finding_settlement.py +1432 -0
- package/core/launch_phases/findings_adjudication.py +666 -0
- package/core/launch_phases/findings_snapshot.py +357 -0
- package/core/launch_phases/judge_phase.py +848 -0
- package/core/launch_phases/lesson_context.py +536 -0
- package/core/launch_phases/lifecycle_reporter.py +627 -0
- package/core/launch_phases/orchestrator_controls.py +686 -0
- package/core/launch_phases/post_commit.py +567 -0
- package/core/launch_phases/post_coverage_sync.py +598 -0
- package/core/launch_phases/post_dry_run.py +817 -0
- package/core/launch_phases/post_observability.py +367 -0
- package/core/launch_phases/post_phase.py +1223 -0
- package/core/launch_phases/post_receipt.py +373 -0
- package/core/launch_phases/post_round_lifecycle.py +786 -0
- package/core/launch_phases/pre_phase.py +1029 -0
- package/core/launch_phases/pre_recon_scout.py +879 -0
- package/core/launch_phases/pre_state_builder.py +1133 -0
- package/core/launch_phases/report_sharing.py +414 -0
- package/core/launch_phases/round_orchestrator.py +1116 -0
- package/core/launch_phases/session_runtime.py +1670 -0
- package/core/launch_phases/startup_gates.py +1050 -0
- package/core/launch_phases/startup_interaction.py +1003 -0
- package/core/launch_phases/startup_version_gate.py +879 -0
- package/core/launch_phases/supervisor_bridge.py +1253 -0
- package/core/launch_phases/supervisor_coverage.py +423 -0
- package/core/launch_phases/supervisor_degraded.py +1211 -0
- package/core/launch_phases/worker_barriers.py +424 -0
- package/core/launch_phases/worker_orchestrator.py +1246 -0
- package/core/launch_phases/worker_prior.py +749 -0
- package/core/ledger/INTERFACE.md +35 -0
- package/core/ledger/README.md +160 -0
- package/core/ledger/SPEC.md +25 -0
- package/core/ledger/__init__.py +67 -0
- package/core/ledger/_facade.py +227 -0
- package/core/ledger/archive_ops.py +630 -0
- package/core/ledger/auto_materialization.py +459 -0
- package/core/ledger/bug-log.md +22 -0
- package/core/ledger/cli.py +275 -0
- package/core/ledger/export_ops.py +554 -0
- package/core/ledger/finding_confirmation.py +1316 -0
- package/core/ledger/finding_io.py +299 -0
- package/core/ledger/finding_lifecycle.py +730 -0
- package/core/ledger/finding_verification.py +405 -0
- package/core/ledger/incidents.py +1005 -0
- package/core/ledger/lesson-replay-cases.json +20 -0
- package/core/ledger/lesson_ledger.py +241 -0
- package/core/ledger/lesson_replay.py +272 -0
- package/core/ledger/life_ledger.py +668 -0
- package/core/ledger/mistake-book.md +130 -0
- package/core/ledger/patterns.py +684 -0
- package/core/ledger/receipt_contract.py +269 -0
- package/core/ledger/schema.py +1192 -0
- package/core/ledger/sync_ops.py +940 -0
- package/core/ledger/tool_experience.py +683 -0
- package/core/ledger/verify_life.py +1597 -0
- package/core/lesson-ledger.json +275 -0
- package/core/locales/README.md +19 -0
- package/core/locales/en-US.json +462 -0
- package/core/locales/zh-CN.json +462 -0
- package/core/mcp_handlers/INTERFACE.md +75 -0
- package/core/mcp_handlers/README.md +62 -0
- package/core/mcp_handlers/SPEC.md +25 -0
- package/core/mcp_handlers/__init__.py +128 -0
- package/core/mcp_handlers/base.py +717 -0
- package/core/mcp_handlers/channel_lifecycle.py +751 -0
- package/core/mcp_handlers/graph_handlers.py +222 -0
- package/core/mcp_handlers/knowledge_handlers.py +536 -0
- package/core/mcp_handlers/mining_handlers.py +586 -0
- package/core/mcp_handlers/resource_prompt_table.py +116 -0
- package/core/mcp_handlers/retrieval_handlers.py +249 -0
- package/core/mcp_handlers/rpc_server.py +470 -0
- package/core/mcp_handlers/supervisor_handlers.py +1461 -0
- package/core/mcp_handlers/telemetry_handlers.py +227 -0
- package/core/mcp_handlers/tool_table.py +1030 -0
- package/core/mcp_runtime_support/README.md +21 -0
- package/core/mcp_runtime_support/__init__.py +1 -0
- package/core/mcp_runtime_support/common.py +363 -0
- package/core/mcp_runtime_support/environment.py +567 -0
- package/core/mcp_runtime_support/verifier.py +758 -0
- package/core/metadata.txt +4 -0
- package/core/planning/README.md +157 -0
- package/core/planning/__init__.py +242 -0
- package/core/planning/capability_plan.py +187 -0
- package/core/planning/capability_planner.py +195 -0
- package/core/planning/domain_archetype.py +542 -0
- package/core/planning/domain_router.py +269 -0
- package/core/planning/dynamic_flow_analyzer.py +533 -0
- package/core/planning/impact_pruner.py +435 -0
- package/core/planning/intent_adjudicator.py +175 -0
- package/core/planning/judge_pipeline.py +1246 -0
- package/core/planning/logic_analysis.py +419 -0
- package/core/planning/prompt_harness.py +1704 -0
- package/core/planning/source_inspector.py +267 -0
- package/core/planning/spec_harvester.py +449 -0
- package/core/planning/target_sandbox_manager.py +573 -0
- package/core/planning/test_capability_verifier.py +1364 -0
- package/core/planning/tools-kb.md +38 -0
- package/core/planning/unit_kind.py +122 -0
- package/core/probes/INTERFACE.md +83 -0
- package/core/probes/README.md +51 -0
- package/core/probes/SPEC.md +36 -0
- package/core/probes/__init__.py +76 -0
- package/core/probes/android_probe.py +552 -0
- package/core/probes/base.py +147 -0
- package/core/probes/codebase_memory_probe.py +647 -0
- package/core/probes/desktop_probe.py +342 -0
- package/core/probes/differential_probe.py +434 -0
- package/core/probes/efficiency_probe.py +232 -0
- package/core/probes/ios_binary_probe.py +472 -0
- package/core/probes/memory_probe.py +392 -0
- package/core/probes/miniprogram_probe.py +385 -0
- package/core/probes/mobile_probe.py +466 -0
- package/core/probes/native_probe.py +302 -0
- package/core/probes/openwiki_probe.py +263 -0
- package/core/probes/pattern_probe.py +453 -0
- package/core/probes/platform_probe.py +424 -0
- package/core/probes/probe_registry.py +1770 -0
- package/core/probes/sast_fixture_corpus.py +340 -0
- package/core/probes/sast_probe.py +1510 -0
- package/core/probes/sast_rules.py +1481 -0
- package/core/probes/sca_scanner.py +779 -0
- package/core/probes/ui_accessibility.py +149 -0
- package/core/probes/ui_audit.py +513 -0
- package/core/probes/ui_contract.py +273 -0
- package/core/probes/ui_evidence.py +212 -0
- package/core/probes/ui_measurements.py +314 -0
- package/core/probes/ui_repair.py +250 -0
- package/core/probes/ui_review.py +133 -0
- package/core/probes/ui_storybook.py +107 -0
- package/core/probes/ui_tokens.py +185 -0
- package/core/probes/ui_visual_diff.py +131 -0
- package/core/probes/web_probe.py +714 -0
- package/core/project_profiler/INTERFACE.md +62 -0
- package/core/project_profiler/README.md +53 -0
- package/core/project_profiler/SPEC.md +28 -0
- package/core/project_profiler/__init__.py +201 -0
- package/core/project_profiler/arbitrator.py +316 -0
- package/core/project_profiler/constants.py +247 -0
- package/core/project_profiler/detector.py +399 -0
- package/core/project_profiler/extractors.py +836 -0
- package/core/project_profiler/fingerprint.py +116 -0
- package/core/project_profiler/storage.py +258 -0
- package/core/project_profiler/strategy.py +301 -0
- package/core/prompts/README.md +35 -0
- package/core/prompts/codex_dialog_dispatch.md +9 -0
- package/core/prompts/command_routing_en.md +42 -0
- package/core/prompts/command_routing_zh.md +42 -0
- package/core/prompts/l0_router.md +47 -0
- package/core/prompts/rules/anti_patterns.md +16 -0
- package/core/prompts/rules/concurrency_rules.md +14 -0
- package/core/prompts/rules/memory_leak_rules.md +13 -0
- package/core/prompts/rules/prompt_injection_defense.md +13 -0
- package/core/prompts/stages/stage_explore.md +31 -0
- package/core/prompts/stages/stage_gate.md +43 -0
- package/core/prompts/stages/stage_mutate.md +42 -0
- package/core/prompts/stages/stage_repair.md +85 -0
- package/core/repair/README.md +74 -0
- package/core/repair/__init__.py +169 -0
- package/core/repair/fix_watchlist.py +391 -0
- package/core/repair/l7_dynamic.py +418 -0
- package/core/repair/l7_repair_loop.py +462 -0
- package/core/repair/l7_selfcheck.py +862 -0
- package/core/repair/minimize_repro.py +466 -0
- package/core/repair/patch_synthesizer.py +617 -0
- package/core/repair/regression_pack.py +379 -0
- package/core/repair/rsi_fixer.py +910 -0
- package/core/repair/rsi_loop.py +311 -0
- package/core/repair/rsi_repair.py +348 -0
- package/core/repair/rsi_rounds.py +212 -0
- package/core/repair/rsi_scope.py +281 -0
- package/core/repair/self_heal.py +604 -0
- package/core/repair/test_generator.py +1108 -0
- package/core/repair/test_quality_auditor.py +410 -0
- package/core/repair/test_strategy.py +511 -0
- package/core/report_engine/INTERFACE.md +59 -0
- package/core/report_engine/README.md +69 -0
- package/core/report_engine/SPEC.md +28 -0
- package/core/report_engine/__init__.py +47 -0
- package/core/report_engine/attestation.py +228 -0
- package/core/report_engine/authority.py +255 -0
- package/core/report_engine/finding_report.py +418 -0
- package/core/report_engine/formal_report_builder.py +924 -0
- package/core/report_engine/html_renderers.py +696 -0
- package/core/report_engine/models.py +759 -0
- package/core/report_engine/pdf_exporter.py +231 -0
- package/core/report_engine/projects_assets.py +730 -0
- package/core/report_engine/report_client.py +743 -0
- package/core/report_engine/report_data_builder.py +388 -0
- package/core/report_engine/report_share.py +459 -0
- package/core/report_engine/sast_bridge.py +116 -0
- package/core/report_engine/server.py +832 -0
- package/core/report_engine/visual_report_builder.py +796 -0
- package/core/report_engine/web-conformance.md +168 -0
- package/core/report_engine/web_conformance.py +428 -0
- package/core/report_engine/workbench_assets.py +484 -0
- package/core/retrieval/INTERFACE.md +20 -0
- package/core/retrieval/README.md +161 -0
- package/core/retrieval/SPEC.md +25 -0
- package/core/retrieval/__init__.py +3 -0
- package/core/retrieval/code_analyzer.py +1720 -0
- package/core/retrieval/codebase_memory_bridge.py +337 -0
- package/core/retrieval/context_assembler.py +240 -0
- package/core/retrieval/corpus_fetch.py +369 -0
- package/core/retrieval/graph_ego.py +597 -0
- package/core/retrieval/knowledge_auto.py +662 -0
- package/core/retrieval/knowledge_sources.py +168 -0
- package/core/retrieval/knowledge_sync.py +1003 -0
- package/core/retrieval/memory_store.py +480 -0
- package/core/retrieval/query_planner.py +1020 -0
- package/core/retrieval/rrf_engine.py +137 -0
- package/core/retrieval/slice_extractor.py +1209 -0
- package/core/retrieval/slice_normalizer.py +373 -0
- package/core/retrieval/slice_spec.py +357 -0
- package/core/retrieval/symbol_index.py +1151 -0
- package/core/retrieval/universal_code_matcher.py +622 -0
- package/core/retrieval/xerj_bridge.py +476 -0
- package/core/retrieval/xerj_supervisor.py +362 -0
- package/core/rules/README.md +33 -0
- package/core/rules/ios/PROVENANCE.json +30 -0
- package/core/rules/ios/crypto.yaml +36 -0
- package/core/rules/ios/network.yaml +20 -0
- package/core/rules/ios/resilience.yaml +47 -0
- package/core/rules/ios/storage.yaml +48 -0
- package/core/rules/mastg/LICENSE +173 -0
- package/core/rules/mastg/PROVENANCE.json +270 -0
- package/core/rules/mastg/mastg-android-asymmetric-key-pair-used-for-multiple-purposes.yml +13 -0
- package/core/rules/mastg/mastg-android-backup-manifest.yml +24 -0
- package/core/rules/mastg/mastg-android-biometric-event-bound.yml +49 -0
- package/core/rules/mastg/mastg-android-biometric-invalidated-enrollment.yml +11 -0
- package/core/rules/mastg/mastg-android-biometric-no-confirmation-required.yml +10 -0
- package/core/rules/mastg/mastg-android-biometric-validity-duration.yml +24 -0
- package/core/rules/mastg/mastg-android-broken-encryption-algorithms.yaml +9 -0
- package/core/rules/mastg/mastg-android-broken-encryption-modes.yaml +13 -0
- package/core/rules/mastg/mastg-android-content-provider-exported.yml +35 -0
- package/core/rules/mastg/mastg-android-custom-deeplink-scheme.yml +29 -0
- package/core/rules/mastg/mastg-android-dangerous-app-permissions.yaml +65 -0
- package/core/rules/mastg/mastg-android-data-unencrypted-shared-storage-no-user-interaction-apis.yml +36 -0
- package/core/rules/mastg/mastg-android-data-unencrypted-shared-storage-no-user-interaction-manifest.yml +15 -0
- package/core/rules/mastg/mastg-android-debuggable-flag.yml +10 -0
- package/core/rules/mastg/mastg-android-debugger-checks.yml +12 -0
- package/core/rules/mastg/mastg-android-deeplink-autoverify-missing.yml +33 -0
- package/core/rules/mastg/mastg-android-deeplink-unvalidated-parameter.yml +11 -0
- package/core/rules/mastg/mastg-android-device-passcode-present.yml +17 -0
- package/core/rules/mastg/mastg-android-fileprovider-broad-scope.yml +46 -0
- package/core/rules/mastg/mastg-android-hardcoded-crypto-keys-usage.yml +14 -0
- package/core/rules/mastg/mastg-android-hardcoded-security-provider.yaml +18 -0
- package/core/rules/mastg/mastg-android-implicit-intent-internal-communication.yml +30 -0
- package/core/rules/mastg/mastg-android-implicit-intent-leaking-extras.yml +29 -0
- package/core/rules/mastg/mastg-android-input-field-usage.yml +11 -0
- package/core/rules/mastg/mastg-android-key-generation-with-insufficient-key-length.yml +21 -0
- package/core/rules/mastg/mastg-android-keyboard-cache-input-types.yml +10 -0
- package/core/rules/mastg/mastg-android-local-storage-input-validation.yml +48 -0
- package/core/rules/mastg/mastg-android-native-debugger-checks.yml +12 -0
- package/core/rules/mastg/mastg-android-network-checkservertrusted.yml +12 -0
- package/core/rules/mastg/mastg-android-network-hostname-verification.yml +11 -0
- package/core/rules/mastg/mastg-android-network-insecure-trust-anchors.yml +11 -0
- package/core/rules/mastg/mastg-android-network-onreceivedsslerror.yml +12 -0
- package/core/rules/mastg/mastg-android-non-random-use.yml +16 -0
- package/core/rules/mastg/mastg-android-object-deserialization.yml +14 -0
- package/core/rules/mastg/mastg-android-overlay-protection.yml +68 -0
- package/core/rules/mastg/mastg-android-pendingintent-mutable.yml +39 -0
- package/core/rules/mastg/mastg-android-random-apis-insufficient-entropy.yml +17 -0
- package/core/rules/mastg/mastg-android-root-detection.yaml +59 -0
- package/core/rules/mastg/mastg-android-sdk-version.yml +10 -0
- package/core/rules/mastg/mastg-android-sensitive-data-in-notifications-manifest.yml +17 -0
- package/core/rules/mastg/mastg-android-sensitive-data-in-notifications.yml +11 -0
- package/core/rules/mastg/mastg-android-sensitive-data-in-screenshot.yml +18 -0
- package/core/rules/mastg/mastg-android-sql-injection-contentprovider.yml +17 -0
- package/core/rules/mastg/mastg-android-ssl-socket-hostnameverifier.yml +15 -0
- package/core/rules/mastg/mastg-android-strictmode.yml +10 -0
- package/core/rules/mastg/mastg-android-system-alert-window.yml +9 -0
- package/core/rules/mastg/mastg-android-webview-allow-local-access.yml +15 -0
- package/core/rules/mastg/mastg-android-webview-bridges.yml +28 -0
- package/core/rules/mastg/mastg-android-webview-safebrowsing.yml +20 -0
- package/core/rules/mastg/mastg-android-webview-url-handlers.yml +41 -0
- package/core/rules/miniprogram/PROVENANCE.json +26 -0
- package/core/rules/miniprogram/network.yaml +37 -0
- package/core/rules/miniprogram/platform.yaml +36 -0
- package/core/rules/miniprogram/storage.yaml +48 -0
- package/core/rules/mobsfscan-swift/LICENSE +165 -0
- package/core/rules/mobsfscan-swift/PROVENANCE.json +58 -0
- package/core/rules/mobsfscan-swift/auth.yaml +59 -0
- package/core/rules/mobsfscan-swift/crypto.yaml +66 -0
- package/core/rules/mobsfscan-swift/jailbreak.yaml +14 -0
- package/core/rules/mobsfscan-swift/keyboard.yaml +34 -0
- package/core/rules/mobsfscan-swift/logging.yaml +23 -0
- package/core/rules/mobsfscan-swift/network.yaml +55 -0
- package/core/rules/mobsfscan-swift/resilience.yaml +29 -0
- package/core/rules/mobsfscan-swift/secrets.yaml +33 -0
- package/core/rules/mobsfscan-swift/storage.yaml +29 -0
- package/core/rules/mobsfscan-swift/webview.yaml +32 -0
- package/core/runtime/README.md +198 -0
- package/core/runtime/__init__.py +3 -0
- package/core/runtime/bugx_logging.py +1464 -0
- package/core/runtime/daemon_runner.py +623 -0
- package/core/runtime/fixture_solidify.py +444 -0
- package/core/runtime/global_telemetry.py +549 -0
- package/core/runtime/incident_eval.py +148 -0
- package/core/runtime/interaction.py +605 -0
- package/core/runtime/interaction_store.py +435 -0
- package/core/runtime/interactive_menu.py +95 -0
- package/core/runtime/log_analytics.py +922 -0
- package/core/runtime/mining_quality_framework.py +1387 -0
- package/core/runtime/model_runner.py +732 -0
- package/core/runtime/platform_adapter.py +579 -0
- package/core/runtime/prep_validate.py +478 -0
- package/core/runtime/project_graph.py +827 -0
- package/core/runtime/project_init.py +481 -0
- package/core/runtime/project_profile.py +224 -0
- package/core/runtime/project_settings.py +964 -0
- package/core/runtime/provider_window.py +97 -0
- package/core/runtime/resume_ticket.py +115 -0
- package/core/runtime/scenario_eval.py +856 -0
- package/core/runtime/seed_drill.py +549 -0
- package/core/runtime/self_audit.py +550 -0
- package/core/runtime/session_profile.py +205 -0
- package/core/runtime/setup_ui_env.py +229 -0
- package/core/runtime/startup_environment.py +859 -0
- package/core/runtime/sync_store.py +1421 -0
- package/core/runtime/tool_install_targets.py +18 -0
- package/core/schemas/README.md +35 -0
- package/core/schemas/agent-graph.schema.json +368 -0
- package/core/schemas/attack-graph.schema.json +88 -0
- package/core/schemas/evidence-bundle.schema.json +33 -0
- package/core/schemas/skill-pack.schema.json +230 -0
- package/core/schemas/worker-checkpoint.schema.json +119 -0
- package/core/secure_atomic.py +289 -0
- package/core/seed_corpus/README.md +55 -0
- package/core/seed_corpus/c.txt +29 -0
- package/core/seed_corpus/cpp.txt +32 -0
- package/core/seed_corpus/css.txt +70 -0
- package/core/seed_corpus/go.txt +46 -0
- package/core/seed_corpus/java.txt +30 -0
- package/core/seed_corpus/javascript.txt +55 -0
- package/core/seed_corpus/kotlin.txt +28 -0
- package/core/seed_corpus/python.txt +71 -0
- package/core/seed_corpus/rust.txt +53 -0
- package/core/seed_corpus/swift.txt +29 -0
- package/core/seed_corpus/typescript.txt +29 -0
- package/core/skills/README.md +53 -0
- package/core/skills/__init__.py +189 -0
- package/core/skills/bugx_docs_mcp_server.py +418 -0
- package/core/skills/bugx_mcp_server.py +1161 -0
- package/core/skills/mcp_runtime.py +968 -0
- package/core/skills/mcp_session_proof.py +605 -0
- package/core/skills/mobile_ui_inspector.py +405 -0
- package/core/skills/skill_pack.py +1214 -0
- package/core/skills/skill_synthesizer.py +1153 -0
- package/core/skills/tools_kb.py +313 -0
- package/core/supervisor/INTERFACE.md +121 -0
- package/core/supervisor/README.md +209 -0
- package/core/supervisor/SPEC.md +25 -0
- package/core/supervisor/__init__.py +175 -0
- package/core/supervisor/audit_supervisor.py +1015 -0
- package/core/supervisor/budget_governor.py +1254 -0
- package/core/supervisor/checkpoint_handler.py +368 -0
- package/core/supervisor/checkpoint_pipeline.py +1564 -0
- package/core/supervisor/checkpoint_sync.py +653 -0
- package/core/supervisor/claim_events.py +74 -0
- package/core/supervisor/claim_sync.py +590 -0
- package/core/supervisor/cli.py +812 -0
- package/core/supervisor/context_sync.py +55 -0
- package/core/supervisor/coverage_sync.py +115 -0
- package/core/supervisor/finding_commit.py +470 -0
- package/core/supervisor/finding_sync.py +688 -0
- package/core/supervisor/finding_workflow.py +676 -0
- package/core/supervisor/finish_policy.py +224 -0
- package/core/supervisor/finish_sync.py +185 -0
- package/core/supervisor/graph_sync.py +669 -0
- package/core/supervisor/lease_manager.py +788 -0
- package/core/supervisor/lifecycle_blocks.py +129 -0
- package/core/supervisor/next_round_sync.py +126 -0
- package/core/supervisor/observability.py +140 -0
- package/core/supervisor/recovery_sync.py +165 -0
- package/core/supervisor/rejection_remedies.py +355 -0
- package/core/supervisor/round_governor.py +306 -0
- package/core/supervisor/serial_authorization.py +544 -0
- package/core/supervisor/start_sync.py +87 -0
- package/core/supervisor/state_store.py +249 -0
- package/core/supervisor/supervisor_host.py +511 -0
- package/core/supervisor/supervisor_models.py +1143 -0
- package/core/supervisor/systemic_failure.py +104 -0
- package/core/supervisor/task_scheduler.py +1640 -0
- package/core/supervisor/worker_checkpoint.py +230 -0
- package/core/supervisor/worker_execution.py +265 -0
- package/core/supervisor/worker_handoff.py +192 -0
- package/core/supervisor/worker_lifecycle.py +396 -0
- package/core/supervisor/worker_runtime.py +1005 -0
- package/core/supervisor/worker_sync.py +89 -0
- package/core/supervisor_types/README.md +36 -0
- package/core/supervisor_types/__init__.py +0 -0
- package/core/supervisor_types/exceptions.py +52 -0
- package/core/supervisor_types/graph_models.py +357 -0
- package/core/supervisor_types/path_containment.py +151 -0
- package/core/supervisor_types/target_fingerprint.py +310 -0
- package/core/supervisor_types/worker_claim.py +411 -0
- package/core/webui/INTERFACE.md +50 -0
- package/core/webui/README.md +57 -0
- package/core/webui/SPEC.md +26 -0
- package/core/webui/__init__.py +11 -0
- package/core/webui/adapters/__init__.py +27 -0
- package/core/webui/adapters/base.py +87 -0
- package/core/webui/adapters/bugx.py +102 -0
- package/core/webui/adapters/claude.py +222 -0
- package/core/webui/adapters/cline.py +407 -0
- package/core/webui/adapters/codex.py +260 -0
- package/core/webui/adapters/dsh.py +177 -0
- package/core/webui/adapters/opencode.py +218 -0
- package/core/webui/adapters/pi.py +272 -0
- package/core/webui/adapters/registry.py +100 -0
- package/core/webui/adapters/zcode.py +312 -0
- package/core/webui/api_routes_post.py +668 -0
- package/core/webui/dispatcher.py +1376 -0
- package/core/webui/docs_session_service.py +473 -0
- package/core/webui/handler.py +674 -0
- package/core/webui/ide_cloud_service.py +526 -0
- package/core/webui/mcp_inspector.py +1747 -0
- package/core/webui/static/00-base.css +1060 -0
- package/core/webui/static/01-monitoring.css +802 -0
- package/core/webui/static/02-header-actions.css +321 -0
- package/core/webui/static/03-trigger-popover.css +697 -0
- package/core/webui/static/04-composer.css +628 -0
- package/core/webui/static/05-skills-center.css +878 -0
- package/core/webui/static/06-mcp-center.css +623 -0
- package/core/webui/static/07-skill-detail.css +265 -0
- package/core/webui/static/08-files-management.css +167 -0
- package/core/webui/static/09-files-center.css +386 -0
- package/core/webui/static/10-icons.css +105 -0
- package/core/webui/static/11-project-options.css +185 -0
- package/core/webui/static/12-profile-tab.css +383 -0
- package/core/webui/static/assets/index-CXAsl8mr.js +333 -0
- package/core/webui/static/assets/index-DHQUtWtx.css +1 -0
- package/core/webui/static/assets/source-serif-4-cyrillic-400-normal-B8Z6Jvzv.woff +0 -0
- package/core/webui/static/assets/source-serif-4-cyrillic-400-normal-C44pakCn.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-cyrillic-500-normal-Bb4u56_6.woff +0 -0
- package/core/webui/static/assets/source-serif-4-cyrillic-500-normal-BdVUvljg.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-cyrillic-ext-400-normal-CWV284fw.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-cyrillic-ext-400-normal-DLkyKLvH.woff +0 -0
- package/core/webui/static/assets/source-serif-4-cyrillic-ext-500-normal-8-8HP2PF.woff +0 -0
- package/core/webui/static/assets/source-serif-4-cyrillic-ext-500-normal-DaDZE_Na.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-greek-400-normal-BccYJ-vZ.woff +0 -0
- package/core/webui/static/assets/source-serif-4-greek-400-normal-Cw5-AHNN.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-greek-500-normal-By8GFJzz.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-greek-500-normal-z-Q6FFuG.woff +0 -0
- package/core/webui/static/assets/source-serif-4-latin-400-normal-DJ5YJwmz.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-latin-400-normal-Dn3IlU-Z.woff +0 -0
- package/core/webui/static/assets/source-serif-4-latin-500-normal-CUHe9xxf.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-latin-500-normal-DVVamBqs.woff +0 -0
- package/core/webui/static/assets/source-serif-4-latin-ext-400-normal-CEpydyUl.woff +0 -0
- package/core/webui/static/assets/source-serif-4-latin-ext-400-normal-Cp7z-ARB.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-latin-ext-500-normal-BYjRCDVy.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-latin-ext-500-normal-D-qasQTj.woff +0 -0
- package/core/webui/static/assets/source-serif-4-vietnamese-400-normal-BbG3LG1A.woff2 +0 -0
- package/core/webui/static/assets/source-serif-4-vietnamese-400-normal-DR1UuFon.woff +0 -0
- package/core/webui/static/assets/source-serif-4-vietnamese-500-normal-8N8MYVKB.woff +0 -0
- package/core/webui/static/assets/source-serif-4-vietnamese-500-normal-Bi3OSTTW.woff2 +0 -0
- package/core/webui/static/design-tokens.css +113 -0
- package/core/webui/static/favicon.png +0 -0
- package/core/webui/static/icon-192.png +0 -0
- package/core/webui/static/index.html +55 -0
- package/core/webui/static/manifest.webmanifest +19 -0
- package/core/webui/static/project-settings.js +9602 -0
- package/core/webui/static/sw.js +8 -0
- package/core/webui/workspace_service.py +218 -0
- package/docs/07_distribution/01_README_NPM.md +285 -0
- package/ecosystem/README.md +129 -0
- package/ecosystem/integrations/README.md +38 -0
- package/ecosystem/integrations/cline/skills/bugx/SKILL.md +69 -0
- package/ecosystem/integrations/dsh/agents/bugx.md +27 -0
- package/ecosystem/integrations/dsh/dsh-patch.yml +18 -0
- package/ecosystem/integrations/dsh/mcp/cordis.patch.yml +4 -0
- package/ecosystem/integrations/dsh/mcp/index.js +219 -0
- package/ecosystem/integrations/dsh/mcp/package.json +28 -0
- package/ecosystem/integrations/dsh/skills/bugx/SKILL.md +70 -0
- package/ecosystem/integrations/dsh/skills/bugx-dsh/SKILL.md +229 -0
- package/ecosystem/integrations/dsh/ui/client.js +87 -0
- package/ecosystem/integrations/dsh/ui/cordis.patch.yml +4 -0
- package/ecosystem/integrations/dsh/ui/index.js +7 -0
- package/ecosystem/integrations/dsh/ui/package.json +27 -0
- package/ecosystem/integrations/opencode/bugx-mcp-manifest.json +53 -0
- package/ecosystem/integrations/opencode/mcp-lock.json +3075 -0
- package/ecosystem/integrations/opencode/opencode.json +129 -0
- package/ecosystem/integrations/pi/extensions/bugx/README.md +12 -0
- package/ecosystem/integrations/pi/extensions/bugx/index.ts +299 -0
- package/ecosystem/integrations/pi/extensions/bugx/manifest.json +11 -0
- package/ecosystem/integrations/pi/skills/bugx/SKILL.md +69 -0
- package/ecosystem/integrations/qoder/CONNECTORS.md +54 -0
- package/ecosystem/integrations/qoder/connector-meta.json +21 -0
- package/ecosystem/integrations/workbuddy/connector-meta.json +19 -0
- package/ecosystem/integrations/workbuddy/icon.png +0 -0
- package/ecosystem/integrations/workbuddy/skills/bugx/SKILL.md +70 -0
- package/ecosystem/plugins/README.md +37 -0
- package/ecosystem/plugins/bugx/.claude-plugin/marketplace.json +17 -0
- package/ecosystem/plugins/bugx/.claude-plugin/plugin.json +16 -0
- package/ecosystem/plugins/bugx/.codex-plugin/plugin.json +41 -0
- package/ecosystem/plugins/bugx/.zcode-plugin/plugin.json +19 -0
- package/ecosystem/plugins/bugx/agents/bugx-judge.md +71 -0
- package/ecosystem/plugins/bugx/agents/bugx-worker.md +31 -0
- package/ecosystem/plugins/bugx/agents/bugx.md +74 -0
- package/ecosystem/plugins/bugx/assets/bugx-avatar-512.png +0 -0
- package/ecosystem/plugins/bugx/skills/bugx/SKILL.md +118 -0
- package/ecosystem/plugins/bugx/skills/bugx-claude/SKILL.md +326 -0
- package/ecosystem/plugins/bugx/skills/bugx-dsh/SKILL.md +229 -0
- package/ecosystem/plugins/bugx/skills/bugx-opencode/SKILL.md +129 -0
- package/ecosystem/plugins/bugx/skills/bugx-zcode/SKILL.md +459 -0
- package/interfaces/bin/bugx.js +117 -0
- package/interfaces/cli/bugx_cli/__init__.py +6 -0
- package/interfaces/cli/bugx_cli/cli.py +1685 -0
- package/interfaces/cli/bugx_cli/commands/__init__.py +1 -0
- package/interfaces/cli/bugx_cli/commands/advisory.py +94 -0
- package/interfaces/cli/bugx_cli/commands/agent.py +4210 -0
- package/interfaces/cli/bugx_cli/commands/audit.py +632 -0
- package/interfaces/cli/bugx_cli/commands/autonomous.py +260 -0
- package/interfaces/cli/bugx_cli/commands/cloud.py +1371 -0
- package/interfaces/cli/bugx_cli/commands/daemon.py +50 -0
- package/interfaces/cli/bugx_cli/commands/doctor.py +1590 -0
- package/interfaces/cli/bugx_cli/commands/goal.py +107 -0
- package/interfaces/cli/bugx_cli/commands/interaction.py +248 -0
- package/interfaces/cli/bugx_cli/commands/knowledge.py +619 -0
- package/interfaces/cli/bugx_cli/commands/lifecycle.py +2763 -0
- package/interfaces/cli/bugx_cli/commands/patch.py +1014 -0
- package/interfaces/cli/bugx_cli/commands/plugin.py +2749 -0
- package/interfaces/cli/bugx_cli/commands/plugin_claude.py +1446 -0
- package/interfaces/cli/bugx_cli/commands/plugin_cline.py +269 -0
- package/interfaces/cli/bugx_cli/commands/plugin_codex.py +74 -0
- package/interfaces/cli/bugx_cli/commands/plugin_funide.py +729 -0
- package/interfaces/cli/bugx_cli/commands/plugin_qoder.py +216 -0
- package/interfaces/cli/bugx_cli/commands/plugin_zcode.py +710 -0
- package/interfaces/cli/bugx_cli/commands/project.py +670 -0
- package/interfaces/cli/bugx_cli/commands/report.py +256 -0
- package/interfaces/cli/bugx_cli/commands/skill.py +251 -0
- package/interfaces/cli/bugx_cli/commands/stats.py +133 -0
- package/interfaces/cli/bugx_cli/commands/system.py +2567 -0
- package/interfaces/cli/bugx_cli/common.py +906 -0
- package/interfaces/cli/bugx_cli/host_adapters.py +1892 -0
- package/interfaces/cli/bugx_cli/hosts/__init__.py +80 -0
- package/interfaces/cli/bugx_cli/hosts/base.py +99 -0
- package/interfaces/cli/bugx_cli/hosts/registry.py +573 -0
- package/interfaces/cli/bugx_cli/hosts/template_engine.py +188 -0
- package/interfaces/cli/bugx_cli/infra/__init__.py +209 -0
- package/interfaces/cli/bugx_cli/infra/ai_advisor.py +851 -0
- package/interfaces/cli/bugx_cli/infra/engine_installer.py +1945 -0
- package/interfaces/cli/bugx_cli/infra/host_sync.py +548 -0
- package/interfaces/cli/bugx_cli/infra/mcp_advisor.py +826 -0
- package/interfaces/cli/bugx_cli/infra/security.py +339 -0
- package/package.json +99 -4
- package/pyproject.toml +258 -0
- package/scripts/ensure-python.mjs +97 -0
- package/scripts/isolate-npm-readme.mjs +40 -0
- package/scripts/runtime.lock.json +31 -0
- package/scripts/sync-version.mjs +26 -0
- package/scripts/test-npm-wrapper.mjs +265 -0
|
@@ -0,0 +1,1024 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""可验证的覆盖证据存储。
|
|
3
|
+
|
|
4
|
+
这个模块只负责保存和验证证据元数据,不会执行 ``command`` 字符串。
|
|
5
|
+
worker 可以并发调用 :meth:`EvidenceStore.store`/``submit``;每次写入都在
|
|
6
|
+
进程内线程锁和文件锁保护下进行,并通过临时文件 + ``os.replace`` 原子提交。
|
|
7
|
+
``merge`` 是 supervisor 使用的串行合并入口:同一 ``unit_id`` 的不同版本、
|
|
8
|
+
target、artifact 或命令都会返回 ``conflict``,绝不静默覆盖已有记录。
|
|
9
|
+
|
|
10
|
+
唯一例外是显式 ``allow_supersede=True``(Supervisor 复验路径):同一 ``unit``
|
|
11
|
+
的旧观察可以被更新一轮的观察取代,但不是静默覆盖 —— 被取代记录逐字留档在
|
|
12
|
+
``superseded`` 审计数组中,且已通过的结论永不退化为未通过。
|
|
13
|
+
|
|
14
|
+
证据记录的必需字段为:
|
|
15
|
+
``unit_id``、``command``、``exit_code``、``artifact_path``、
|
|
16
|
+
``artifact_sha256``、``target_fingerprint``、``observed_at``、``worker_id``、
|
|
17
|
+
``status``。目标漂移收卷还可带 ``target_drifted=true``,用于保留
|
|
18
|
+
Supervisor 已接纳但不再代表当前目标的证据语义。
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import errno
|
|
24
|
+
import json
|
|
25
|
+
import os
|
|
26
|
+
import tempfile
|
|
27
|
+
import threading
|
|
28
|
+
from contextlib import contextmanager
|
|
29
|
+
from dataclasses import dataclass, replace
|
|
30
|
+
from datetime import datetime, timedelta, timezone
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
from typing import Any, Iterable, Iterator, Mapping
|
|
33
|
+
|
|
34
|
+
import core.bugx_paths as bugx_paths
|
|
35
|
+
from core.retrieval import symbol_index
|
|
36
|
+
|
|
37
|
+
try:
|
|
38
|
+
import fcntl
|
|
39
|
+
except ImportError: # pragma: no cover - Windows fallback uses msvcrt below.
|
|
40
|
+
fcntl = None
|
|
41
|
+
|
|
42
|
+
try: # pragma: no cover - Windows-only module.
|
|
43
|
+
import msvcrt
|
|
44
|
+
except ImportError: # pragma: no cover
|
|
45
|
+
msvcrt = None # type: ignore[assignment]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
SCHEMA_VERSION = 1
|
|
49
|
+
VALID_STATUSES = frozenset({"passed", "failed", "error", "skipped", "blocked"})
|
|
50
|
+
SHA256_RE = __import__("re").compile(r"[0-9a-fA-F]{64}")
|
|
51
|
+
REQUIRED_FIELDS = (
|
|
52
|
+
"unit_id",
|
|
53
|
+
"command",
|
|
54
|
+
"exit_code",
|
|
55
|
+
"artifact_path",
|
|
56
|
+
"artifact_sha256",
|
|
57
|
+
"target_fingerprint",
|
|
58
|
+
"observed_at",
|
|
59
|
+
"worker_id",
|
|
60
|
+
"status",
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class EvidenceError(ValueError):
|
|
65
|
+
"""证据输入或持久化状态无效。"""
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class EvidenceValidationError(EvidenceError):
|
|
69
|
+
"""尝试存储无效证据时抛出的错误。"""
|
|
70
|
+
|
|
71
|
+
def __init__(self, errors: Iterable[str]):
|
|
72
|
+
self.errors = tuple(str(error) for error in errors)
|
|
73
|
+
super().__init__("证据无效: " + "; ".join(self.errors))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(frozen=True, slots=True)
|
|
77
|
+
class EvidenceRecord:
|
|
78
|
+
"""一条不可变的 worker 覆盖证据记录。"""
|
|
79
|
+
|
|
80
|
+
unit_id: str
|
|
81
|
+
command: str
|
|
82
|
+
exit_code: int
|
|
83
|
+
artifact_path: str
|
|
84
|
+
artifact_sha256: str
|
|
85
|
+
target_fingerprint: str
|
|
86
|
+
observed_at: str
|
|
87
|
+
worker_id: str
|
|
88
|
+
status: str
|
|
89
|
+
# 可选版本用于阻止同一 unit 的跨版本静默覆盖;旧调用无需提供它。
|
|
90
|
+
version: str | int | None = None
|
|
91
|
+
# Checkpoint receipt identity is retained in the durable manifest. These
|
|
92
|
+
# fields bind the stored record back to the Supervisor claim; they are not
|
|
93
|
+
# an assertion that an external command was observed executing.
|
|
94
|
+
execution_id: str | None = None
|
|
95
|
+
lease_id: str | None = None
|
|
96
|
+
execution_receipt_sha256: str | None = None
|
|
97
|
+
# Supervisor admits the artifact but marks it non-current when the target
|
|
98
|
+
# changed while the claimed worker was producing evidence.
|
|
99
|
+
target_drifted: bool = False
|
|
100
|
+
# A failed execution can still be valid coverage when it is a Finding-backed
|
|
101
|
+
# reproduction PoC; retain the Supervisor-owned classification through the
|
|
102
|
+
# durable manifest so post can make the same decision after reload.
|
|
103
|
+
reproduced_crash: bool = False
|
|
104
|
+
evidence_type: str | None = None
|
|
105
|
+
|
|
106
|
+
@classmethod
|
|
107
|
+
def from_dict(cls, payload: Mapping[str, Any]) -> "EvidenceRecord":
|
|
108
|
+
if not isinstance(payload, Mapping):
|
|
109
|
+
raise EvidenceError("证据记录必须是 JSON 对象")
|
|
110
|
+
missing = [field for field in REQUIRED_FIELDS if field not in payload]
|
|
111
|
+
if missing:
|
|
112
|
+
raise EvidenceError("证据缺少字段: " + ", ".join(missing))
|
|
113
|
+
values = {field: payload[field] for field in REQUIRED_FIELDS}
|
|
114
|
+
values["version"] = payload.get("version")
|
|
115
|
+
values["execution_id"] = payload.get("execution_id")
|
|
116
|
+
values["lease_id"] = payload.get("lease_id")
|
|
117
|
+
values["execution_receipt_sha256"] = payload.get(
|
|
118
|
+
"execution_receipt_sha256"
|
|
119
|
+
)
|
|
120
|
+
values["target_drifted"] = payload.get("target_drifted", False)
|
|
121
|
+
values["reproduced_crash"] = payload.get("reproduced_crash", False)
|
|
122
|
+
values["evidence_type"] = payload.get("evidence_type")
|
|
123
|
+
return cls(**values)
|
|
124
|
+
|
|
125
|
+
def to_dict(self) -> dict[str, Any]:
|
|
126
|
+
payload: dict[str, Any] = {field: getattr(self, field) for field in REQUIRED_FIELDS}
|
|
127
|
+
if self.version is not None:
|
|
128
|
+
payload["version"] = self.version
|
|
129
|
+
for field in ("execution_id", "lease_id", "execution_receipt_sha256"):
|
|
130
|
+
value = getattr(self, field)
|
|
131
|
+
if value is not None:
|
|
132
|
+
payload[field] = value
|
|
133
|
+
if self.target_drifted:
|
|
134
|
+
payload["target_drifted"] = True
|
|
135
|
+
if self.reproduced_crash:
|
|
136
|
+
payload["reproduced_crash"] = True
|
|
137
|
+
if self.evidence_type is not None:
|
|
138
|
+
payload["evidence_type"] = self.evidence_type
|
|
139
|
+
return payload
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@dataclass(frozen=True, slots=True)
|
|
143
|
+
class VerificationResult:
|
|
144
|
+
valid: bool
|
|
145
|
+
errors: tuple[str, ...] = ()
|
|
146
|
+
record: EvidenceRecord | None = None
|
|
147
|
+
|
|
148
|
+
def __bool__(self) -> bool:
|
|
149
|
+
return self.valid
|
|
150
|
+
|
|
151
|
+
def __getitem__(self, key: str) -> Any:
|
|
152
|
+
return self.as_dict()[key]
|
|
153
|
+
|
|
154
|
+
def as_dict(self) -> dict[str, Any]:
|
|
155
|
+
return {
|
|
156
|
+
"valid": self.valid,
|
|
157
|
+
"errors": list(self.errors),
|
|
158
|
+
"record": self.record.to_dict() if self.record else None,
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
@dataclass(frozen=True, slots=True)
|
|
163
|
+
class StoreResult:
|
|
164
|
+
"""worker 提交结果:``stored``、``duplicate``、``superseded`` 或 ``conflict``。"""
|
|
165
|
+
|
|
166
|
+
status: str
|
|
167
|
+
unit_id: str
|
|
168
|
+
record: EvidenceRecord | None = None
|
|
169
|
+
existing: EvidenceRecord | None = None
|
|
170
|
+
reason: str = ""
|
|
171
|
+
|
|
172
|
+
def __getitem__(self, key: str) -> Any:
|
|
173
|
+
return self.as_dict()[key]
|
|
174
|
+
|
|
175
|
+
def as_dict(self) -> dict[str, Any]:
|
|
176
|
+
return {
|
|
177
|
+
"status": self.status,
|
|
178
|
+
"unit_id": self.unit_id,
|
|
179
|
+
"record": self.record.to_dict() if self.record else None,
|
|
180
|
+
"existing": self.existing.to_dict() if self.existing else None,
|
|
181
|
+
"reason": self.reason,
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
@dataclass(frozen=True, slots=True)
|
|
186
|
+
class ManifestReport:
|
|
187
|
+
"""清单校验或合并报告。
|
|
188
|
+
|
|
189
|
+
``invalid``、``stale``、``duplicate``、``conflicts`` 均为包含 unit 信息的
|
|
190
|
+
字典列表;``missing`` 是缺失的 unit id 列表。``valid`` 只有在所有门禁均
|
|
191
|
+
为空时才为真。
|
|
192
|
+
"""
|
|
193
|
+
|
|
194
|
+
missing: tuple[str, ...] = ()
|
|
195
|
+
stale: tuple[dict[str, Any], ...] = ()
|
|
196
|
+
invalid: tuple[dict[str, Any], ...] = ()
|
|
197
|
+
duplicate: tuple[dict[str, Any], ...] = ()
|
|
198
|
+
records: tuple[EvidenceRecord, ...] = ()
|
|
199
|
+
conflicts: tuple[dict[str, Any], ...] = ()
|
|
200
|
+
|
|
201
|
+
@property
|
|
202
|
+
def duplicates(self) -> tuple[dict[str, Any], ...]:
|
|
203
|
+
"""复数别名,方便调用方按自然语言命名访问。"""
|
|
204
|
+
return self.duplicate
|
|
205
|
+
|
|
206
|
+
@property
|
|
207
|
+
def valid(self) -> bool:
|
|
208
|
+
return not any((self.missing, self.stale, self.invalid, self.duplicate, self.conflicts))
|
|
209
|
+
|
|
210
|
+
def __bool__(self) -> bool:
|
|
211
|
+
return self.valid
|
|
212
|
+
|
|
213
|
+
def __getitem__(self, key: str) -> Any:
|
|
214
|
+
return self.as_dict()[key]
|
|
215
|
+
|
|
216
|
+
def as_dict(self) -> dict[str, Any]:
|
|
217
|
+
return {
|
|
218
|
+
"valid": self.valid,
|
|
219
|
+
"missing": list(self.missing),
|
|
220
|
+
"stale": [dict(item) for item in self.stale],
|
|
221
|
+
"invalid": [dict(item) for item in self.invalid],
|
|
222
|
+
"duplicate": [dict(item) for item in self.duplicate],
|
|
223
|
+
"duplicates": [dict(item) for item in self.duplicate],
|
|
224
|
+
"conflicts": [dict(item) for item in self.conflicts],
|
|
225
|
+
"records": [record.to_dict() for record in self.records],
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
_THREAD_LOCKS: dict[str, threading.RLock] = {}
|
|
230
|
+
_THREAD_LOCKS_GUARD = threading.Lock()
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _thread_lock(path: Path) -> threading.RLock:
|
|
234
|
+
key = str(path)
|
|
235
|
+
with _THREAD_LOCKS_GUARD:
|
|
236
|
+
lock = _THREAD_LOCKS.get(key)
|
|
237
|
+
if lock is None:
|
|
238
|
+
lock = threading.RLock()
|
|
239
|
+
_THREAD_LOCKS[key] = lock
|
|
240
|
+
return lock
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
@contextmanager
|
|
244
|
+
def _exclusive_lock(path: Path) -> Iterator[None]:
|
|
245
|
+
"""同时覆盖同进程线程和跨进程 worker。
|
|
246
|
+
|
|
247
|
+
LOG-04:Windows 上没有 ``fcntl``,而 Worker 是独立进程,仅有进程内
|
|
248
|
+
``threading.RLock`` 无法阻止跨进程的 read-modify-write 丢更新。这里补
|
|
249
|
+
``msvcrt.locking`` 分支(与 ``bugx_mcp_server._ControllerLease``、
|
|
250
|
+
``audit_supervisor._file_lock`` 同型);POSIX 路径保持 ``fcntl.flock`` 不变。
|
|
251
|
+
锁文件本身也不能被符号链接替换:先做词法预检,再用 ``O_NOFOLLOW`` 原子打开,
|
|
252
|
+
避免 ``is_symlink`` 与 ``open`` 之间的 TOCTOU 窗口。
|
|
253
|
+
"""
|
|
254
|
+
lock = _thread_lock(path)
|
|
255
|
+
with lock:
|
|
256
|
+
bugx_paths.assert_no_symlink_components(path, error_cls=EvidenceError)
|
|
257
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
258
|
+
open_flags = os.O_RDWR | os.O_CREAT | getattr(os, "O_NOFOLLOW", 0)
|
|
259
|
+
try:
|
|
260
|
+
descriptor = os.open(path, open_flags, 0o600)
|
|
261
|
+
except OSError as exc:
|
|
262
|
+
if exc.errno == errno.ELOOP:
|
|
263
|
+
raise EvidenceError(f"拒绝使用符号链接证据锁: {path}") from exc
|
|
264
|
+
raise EvidenceError(f"无法安全打开证据锁: {path}") from exc
|
|
265
|
+
with os.fdopen(descriptor, "a+", encoding="utf-8") as handle:
|
|
266
|
+
if fcntl is not None:
|
|
267
|
+
fcntl.flock(handle.fileno(), fcntl.LOCK_EX)
|
|
268
|
+
elif msvcrt is not None: # pragma: no cover - Windows 由模拟测试覆盖
|
|
269
|
+
handle.seek(0)
|
|
270
|
+
msvcrt.locking(handle.fileno(), msvcrt.LK_LOCK, 1)
|
|
271
|
+
else: # pragma: no cover - unsupported platform
|
|
272
|
+
raise EvidenceError(
|
|
273
|
+
"证据锁在当前平台不可用(fcntl 和 msvcrt 均缺失)"
|
|
274
|
+
)
|
|
275
|
+
try:
|
|
276
|
+
yield
|
|
277
|
+
finally:
|
|
278
|
+
if fcntl is not None:
|
|
279
|
+
fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
|
|
280
|
+
elif msvcrt is not None: # pragma: no cover - Windows 由模拟测试覆盖
|
|
281
|
+
handle.seek(0)
|
|
282
|
+
msvcrt.locking(handle.fileno(), msvcrt.LK_UNLCK, 1)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def _atomic_json_write(path: Path, payload: Mapping[str, Any]) -> None:
|
|
286
|
+
"""在目标文件同一目录中完成 fsync + replace,避免半写 JSON。"""
|
|
287
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
288
|
+
fd, temporary_name = tempfile.mkstemp(
|
|
289
|
+
prefix=f".{path.name}.", suffix=".tmp", dir=str(path.parent)
|
|
290
|
+
)
|
|
291
|
+
temporary = Path(temporary_name)
|
|
292
|
+
try:
|
|
293
|
+
with os.fdopen(fd, "w", encoding="utf-8") as handle:
|
|
294
|
+
json.dump(payload, handle, ensure_ascii=False, indent=2, sort_keys=True)
|
|
295
|
+
handle.write("\n")
|
|
296
|
+
handle.flush()
|
|
297
|
+
os.fsync(handle.fileno())
|
|
298
|
+
os.replace(temporary, path)
|
|
299
|
+
try:
|
|
300
|
+
directory_fd = os.open(path.parent, os.O_RDONLY)
|
|
301
|
+
try:
|
|
302
|
+
os.fsync(directory_fd)
|
|
303
|
+
finally:
|
|
304
|
+
os.close(directory_fd)
|
|
305
|
+
except OSError:
|
|
306
|
+
# 某些文件系统不允许 fsync 目录;replace 本身仍是原子的。
|
|
307
|
+
pass
|
|
308
|
+
finally:
|
|
309
|
+
if temporary.exists():
|
|
310
|
+
temporary.unlink()
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def _read_json(path: Path) -> Any:
|
|
314
|
+
try:
|
|
315
|
+
with path.open("r", encoding="utf-8") as handle:
|
|
316
|
+
return json.load(handle)
|
|
317
|
+
except (OSError, json.JSONDecodeError) as exc:
|
|
318
|
+
raise EvidenceError(f"无法读取证据 JSON: {path}: {exc}") from exc
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def _within(path: Path, root: Path) -> bool:
|
|
322
|
+
try:
|
|
323
|
+
path.relative_to(root)
|
|
324
|
+
except ValueError:
|
|
325
|
+
return False
|
|
326
|
+
return True
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _resolve_artifact(raw_path: str, workspace_root: Path) -> Path:
|
|
330
|
+
if not isinstance(raw_path, str) or not raw_path.strip():
|
|
331
|
+
raise EvidenceError("artifact_path 必须是非空字符串")
|
|
332
|
+
root = workspace_root.resolve()
|
|
333
|
+
raw = Path(raw_path)
|
|
334
|
+
candidate = raw if raw.is_absolute() else root / raw
|
|
335
|
+
lexical = Path(os.path.abspath(str(candidate)))
|
|
336
|
+
if not _within(lexical, root):
|
|
337
|
+
raise EvidenceError(f"artifact_path 逃逸 workspace_root: {raw_path}")
|
|
338
|
+
relative = lexical.relative_to(root)
|
|
339
|
+
current = root
|
|
340
|
+
for part in relative.parts:
|
|
341
|
+
current /= part
|
|
342
|
+
if current.is_symlink():
|
|
343
|
+
raise EvidenceError(f"artifact_path 不能包含符号链接: {raw_path}")
|
|
344
|
+
try:
|
|
345
|
+
resolved = lexical.resolve(strict=False)
|
|
346
|
+
except OSError as exc:
|
|
347
|
+
raise EvidenceError(f"artifact_path 无法解析: {raw_path}: {exc}") from exc
|
|
348
|
+
if not _within(resolved, root):
|
|
349
|
+
raise EvidenceError(f"artifact_path 逃逸 workspace_root: {raw_path}")
|
|
350
|
+
if not resolved.is_file():
|
|
351
|
+
raise EvidenceError(f"artifact 不存在或不是普通文件: {raw_path}")
|
|
352
|
+
return resolved
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def sha256_file(path: Path) -> str:
|
|
356
|
+
"""计算文件内容的 SHA-256。
|
|
357
|
+
|
|
358
|
+
实现只保留一份,位于 ``symbol_index.file_sha256``(QUA-03);本模块只做
|
|
359
|
+
转发,避免三份分块读取互相漂移。
|
|
360
|
+
"""
|
|
361
|
+
return symbol_index.file_sha256(path)
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
# 兼容模块内既有的私有调用点;实现只此一份。
|
|
365
|
+
_sha256 = sha256_file
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def record_authority_check(
|
|
369
|
+
record: EvidenceRecord, workspace_root: Path, *,
|
|
370
|
+
require_current_fingerprint: bool = False,
|
|
371
|
+
) -> tuple[bool, str]:
|
|
372
|
+
"""读取侧证据权威性校验(OQ-5 用户裁决 2026-11-18:补 sha/stale)。
|
|
373
|
+
|
|
374
|
+
形状校验(:meth:`EvidenceRecord.from_dict`)只证明记录完整,不证明
|
|
375
|
+
记录可信。读取判定路径(DIM3 R-1 P1 归属、沿链逐跳)在把一条记录
|
|
376
|
+
当作「本地权威」之前,须补齐与 store/validate 同口径的两层校验:
|
|
377
|
+
|
|
378
|
+
1. **sha 完整性**:``artifact_sha256`` 与实际 artifact 文件一致
|
|
379
|
+
(``_resolve_artifact`` + ``sha256_file``,含越界/符号链接/缺失防
|
|
380
|
+
御)——防已落盘证据被离线篡改后按形状放行为伪权威;
|
|
381
|
+
2. **stale 标志**:Supervisor 已标记 ``target_drifted`` 的记录是非
|
|
382
|
+
current 收容产物,不构成任何方向的权威(既不证明覆盖、也不构成
|
|
383
|
+
推翻——证据产生于漂移执行窗口,对新旧 target 都无裁决力);
|
|
384
|
+
3. (可选)**当前指纹一致性**:``target_fingerprint`` 与当前 workspace
|
|
385
|
+
指纹一致(supervisor ``invalidate_coverage`` L1-ADV-D4 同语义:目标
|
|
386
|
+
字节一变旧证据即失效)。仅在判定**本地**记录权威时启用;沿链跳的
|
|
387
|
+
祖先记录必然来自不同会话的指纹,绝不可启用(否则跨 Session 继承
|
|
388
|
+
机制整体被杀)。
|
|
389
|
+
|
|
390
|
+
返回 ``(ok, reason)``:``ok=False`` 时 ``reason`` 说明拒绝原因(调用方
|
|
391
|
+
必须留痕,不得静默丢弃)。记录本身不做任何修改。
|
|
392
|
+
"""
|
|
393
|
+
if record.target_drifted:
|
|
394
|
+
return False, "supervisor 已标记 target_drifted(非 current 收容产物)"
|
|
395
|
+
if not SHA256_RE.fullmatch(str(record.artifact_sha256 or "")):
|
|
396
|
+
return False, "artifact_sha256 不是合法 sha256 形态"
|
|
397
|
+
try:
|
|
398
|
+
artifact = _resolve_artifact(record.artifact_path, workspace_root)
|
|
399
|
+
except EvidenceError as exc:
|
|
400
|
+
return False, f"artifact 不可达: {exc}"
|
|
401
|
+
actual = _sha256(artifact)
|
|
402
|
+
if actual.lower() != str(record.artifact_sha256).lower():
|
|
403
|
+
return False, (
|
|
404
|
+
f"artifact_sha256 不匹配(疑似离线篡改): expected={record.artifact_sha256} actual={actual}"
|
|
405
|
+
)
|
|
406
|
+
if require_current_fingerprint:
|
|
407
|
+
from core.supervisor_types.target_fingerprint import workspace_fingerprint
|
|
408
|
+
if str(record.target_fingerprint) != workspace_fingerprint(workspace_root):
|
|
409
|
+
return False, (
|
|
410
|
+
f"target_fingerprint 已漂移(target 漂移后未 rebind): record={record.target_fingerprint}"
|
|
411
|
+
)
|
|
412
|
+
return True, ""
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
def _parse_timestamp(value: Any) -> datetime:
|
|
416
|
+
if not isinstance(value, str) or not value.strip():
|
|
417
|
+
raise EvidenceError("observed_at 必须是非空 ISO-8601 字符串")
|
|
418
|
+
normalized = value.strip()
|
|
419
|
+
if normalized.endswith("Z"):
|
|
420
|
+
normalized = normalized[:-1] + "+00:00"
|
|
421
|
+
try:
|
|
422
|
+
return datetime.fromisoformat(normalized)
|
|
423
|
+
except ValueError as exc:
|
|
424
|
+
raise EvidenceError(f"observed_at 无法解析: {value}") from exc
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def _record_identity(record: EvidenceRecord) -> tuple[Any, ...]:
|
|
428
|
+
"""忽略时间和 worker,识别同一份实际证据。"""
|
|
429
|
+
return (
|
|
430
|
+
record.unit_id,
|
|
431
|
+
record.version,
|
|
432
|
+
record.target_fingerprint,
|
|
433
|
+
record.command,
|
|
434
|
+
record.exit_code,
|
|
435
|
+
record.artifact_path,
|
|
436
|
+
record.artifact_sha256.lower(),
|
|
437
|
+
record.status,
|
|
438
|
+
record.reproduced_crash,
|
|
439
|
+
record.evidence_type,
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
def _supersede_allowed(
|
|
444
|
+
existing: EvidenceRecord,
|
|
445
|
+
incoming: EvidenceRecord,
|
|
446
|
+
*,
|
|
447
|
+
allow_target_rebind: bool = False,
|
|
448
|
+
) -> bool:
|
|
449
|
+
"""Permit re-verification replacements.
|
|
450
|
+
|
|
451
|
+
BX-ENGINE-21:默认仍要求 ``target_fingerprint`` 一致(既有契约)。但在 target
|
|
452
|
+
重绑(workspace 变更后的显式 rebind)之后,旧记录的目标已不是当前目标,若继续
|
|
453
|
+
要求一致,旧证据就成为**不可超越的墓碑**——同一 unit 的任何新证据(含
|
|
454
|
+
``blocked``)都被判 conflict,该单元永久无法 covered,也无法如实报告受阻。
|
|
455
|
+
``allow_target_rebind=True`` 由**复验路径**(Supervisor checkpoint pipeline,
|
|
456
|
+
已通过 ``_assert_target_fingerprint`` 校验当前目标)显式开启;
|
|
457
|
+
「已通过证据不得被未通过记录退化」的护栏在任何模式下都不变。
|
|
458
|
+
"""
|
|
459
|
+
if existing.version != incoming.version:
|
|
460
|
+
return False
|
|
461
|
+
if (
|
|
462
|
+
existing.target_fingerprint != incoming.target_fingerprint
|
|
463
|
+
and not allow_target_rebind
|
|
464
|
+
):
|
|
465
|
+
return False
|
|
466
|
+
return not (existing.status == "passed" and incoming.status != "passed")
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
def _validate_record(record: EvidenceRecord, workspace_root: Path) -> VerificationResult:
|
|
470
|
+
errors: list[str] = []
|
|
471
|
+
string_fields = (
|
|
472
|
+
"unit_id",
|
|
473
|
+
"command",
|
|
474
|
+
"artifact_path",
|
|
475
|
+
"target_fingerprint",
|
|
476
|
+
"worker_id",
|
|
477
|
+
)
|
|
478
|
+
for field in string_fields:
|
|
479
|
+
value = getattr(record, field)
|
|
480
|
+
if not isinstance(value, str) or not value.strip():
|
|
481
|
+
errors.append(f"{field} 必须是非空字符串")
|
|
482
|
+
if isinstance(record.exit_code, bool) or not isinstance(record.exit_code, int):
|
|
483
|
+
errors.append("exit_code 必须是整数")
|
|
484
|
+
if not isinstance(record.status, str) or record.status not in VALID_STATUSES:
|
|
485
|
+
errors.append(f"status 必须属于: {sorted(VALID_STATUSES)}")
|
|
486
|
+
if not isinstance(record.target_drifted, bool):
|
|
487
|
+
errors.append("target_drifted 必须是布尔值")
|
|
488
|
+
if not isinstance(record.reproduced_crash, bool):
|
|
489
|
+
errors.append("reproduced_crash 必须是布尔值")
|
|
490
|
+
if record.evidence_type is not None and (
|
|
491
|
+
not isinstance(record.evidence_type, str) or not record.evidence_type.strip()
|
|
492
|
+
):
|
|
493
|
+
errors.append("evidence_type 必须是非空字符串或省略")
|
|
494
|
+
if isinstance(record.exit_code, int) and not isinstance(record.exit_code, bool):
|
|
495
|
+
if record.status == "passed" and record.exit_code != 0:
|
|
496
|
+
errors.append("status=passed 时 exit_code 必须为 0")
|
|
497
|
+
if record.status in {"failed", "error"} and record.exit_code == 0:
|
|
498
|
+
errors.append(f"status={record.status} 时 exit_code 不能为 0")
|
|
499
|
+
if not isinstance(record.artifact_sha256, str) or not SHA256_RE.fullmatch(
|
|
500
|
+
record.artifact_sha256
|
|
501
|
+
):
|
|
502
|
+
errors.append("artifact_sha256 必须是 64 位十六进制 SHA-256")
|
|
503
|
+
try:
|
|
504
|
+
_parse_timestamp(record.observed_at)
|
|
505
|
+
except EvidenceError as exc:
|
|
506
|
+
errors.append(str(exc))
|
|
507
|
+
artifact: Path | None = None
|
|
508
|
+
if isinstance(record.artifact_path, str) and record.artifact_path.strip():
|
|
509
|
+
try:
|
|
510
|
+
artifact = _resolve_artifact(record.artifact_path, workspace_root)
|
|
511
|
+
except EvidenceError as exc:
|
|
512
|
+
errors.append(str(exc))
|
|
513
|
+
if artifact is not None and SHA256_RE.fullmatch(str(record.artifact_sha256 or "")):
|
|
514
|
+
actual = _sha256(artifact)
|
|
515
|
+
if actual.lower() != record.artifact_sha256.lower():
|
|
516
|
+
errors.append(
|
|
517
|
+
f"artifact_sha256 不匹配: expected={record.artifact_sha256.lower()} actual={actual}"
|
|
518
|
+
)
|
|
519
|
+
if record.version is not None and (
|
|
520
|
+
isinstance(record.version, bool) or not isinstance(record.version, (str, int))
|
|
521
|
+
):
|
|
522
|
+
errors.append("version 必须是字符串、整数或省略")
|
|
523
|
+
for field in ("execution_id", "lease_id"):
|
|
524
|
+
value = getattr(record, field)
|
|
525
|
+
if value is not None and (not isinstance(value, str) or not value.strip()):
|
|
526
|
+
errors.append(f"{field} 必须是非空字符串或省略")
|
|
527
|
+
if record.execution_receipt_sha256 is not None and not SHA256_RE.fullmatch(
|
|
528
|
+
str(record.execution_receipt_sha256)
|
|
529
|
+
):
|
|
530
|
+
errors.append("execution_receipt_sha256 必须是 64 位十六进制 SHA-256 或省略")
|
|
531
|
+
return VerificationResult(not errors, tuple(errors), record if not errors else None)
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
def _coerce_record(value: EvidenceRecord | Mapping[str, Any]) -> EvidenceRecord:
|
|
535
|
+
if isinstance(value, EvidenceRecord):
|
|
536
|
+
return value
|
|
537
|
+
if isinstance(value, Mapping):
|
|
538
|
+
return EvidenceRecord.from_dict(value)
|
|
539
|
+
raise EvidenceError("manifest 条目必须是 EvidenceRecord 或 JSON 对象")
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
def _manifest_values(manifest: Any) -> list[Any]:
|
|
543
|
+
if isinstance(manifest, (str, Path)):
|
|
544
|
+
payload = _read_json(Path(manifest))
|
|
545
|
+
else:
|
|
546
|
+
payload = manifest
|
|
547
|
+
if isinstance(payload, Mapping):
|
|
548
|
+
if "records" in payload:
|
|
549
|
+
payload = payload["records"]
|
|
550
|
+
elif all(field in payload for field in REQUIRED_FIELDS):
|
|
551
|
+
payload = [payload]
|
|
552
|
+
else:
|
|
553
|
+
raise EvidenceError("manifest 必须包含 records 数组")
|
|
554
|
+
if isinstance(payload, (str, bytes)) or not isinstance(payload, Iterable):
|
|
555
|
+
raise EvidenceError("manifest 必须是 records 数组或 JSON 对象")
|
|
556
|
+
return list(payload)
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
def _expected_ids(expected_units: Iterable[str] | Mapping[str, Any] | None) -> set[str]:
|
|
560
|
+
if expected_units is None:
|
|
561
|
+
return set()
|
|
562
|
+
if isinstance(expected_units, Mapping):
|
|
563
|
+
return {str(unit_id) for unit_id in expected_units}
|
|
564
|
+
return {str(unit_id) for unit_id in expected_units}
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
def _validate_manifest_values(
|
|
568
|
+
values: list[Any],
|
|
569
|
+
workspace_root: Path,
|
|
570
|
+
*,
|
|
571
|
+
expected_units: Iterable[str] | Mapping[str, Any] | None = None,
|
|
572
|
+
expected_targets: Mapping[str, str] | None = None,
|
|
573
|
+
stale_after: timedelta | float | int | None = None,
|
|
574
|
+
now: datetime | None = None,
|
|
575
|
+
) -> ManifestReport:
|
|
576
|
+
invalid: list[dict[str, Any]] = []
|
|
577
|
+
duplicate: list[dict[str, Any]] = []
|
|
578
|
+
stale: list[dict[str, Any]] = []
|
|
579
|
+
records: list[EvidenceRecord] = []
|
|
580
|
+
indexes_by_unit: dict[str, list[int]] = {}
|
|
581
|
+
|
|
582
|
+
for index, value in enumerate(values):
|
|
583
|
+
try:
|
|
584
|
+
record = _coerce_record(value)
|
|
585
|
+
except EvidenceError as exc:
|
|
586
|
+
invalid.append({"index": index, "errors": [str(exc)]})
|
|
587
|
+
continue
|
|
588
|
+
indexes_by_unit.setdefault(record.unit_id, []).append(index)
|
|
589
|
+
result = _validate_record(record, workspace_root)
|
|
590
|
+
if not result.valid:
|
|
591
|
+
invalid.append({"index": index, "unit_id": record.unit_id, "errors": list(result.errors)})
|
|
592
|
+
continue
|
|
593
|
+
records.append(record)
|
|
594
|
+
if expected_targets and record.unit_id in expected_targets:
|
|
595
|
+
if record.target_fingerprint != expected_targets[record.unit_id]:
|
|
596
|
+
stale.append({
|
|
597
|
+
"index": index,
|
|
598
|
+
"unit_id": record.unit_id,
|
|
599
|
+
"reason": "target_fingerprint_changed",
|
|
600
|
+
})
|
|
601
|
+
if stale_after is not None:
|
|
602
|
+
if isinstance(stale_after, timedelta):
|
|
603
|
+
age_limit = stale_after
|
|
604
|
+
else:
|
|
605
|
+
age_limit = timedelta(seconds=float(stale_after))
|
|
606
|
+
observed = _parse_timestamp(record.observed_at)
|
|
607
|
+
current = now or datetime.now(timezone.utc)
|
|
608
|
+
if observed.tzinfo is None and current.tzinfo is not None:
|
|
609
|
+
current = current.replace(tzinfo=None)
|
|
610
|
+
if observed < current - age_limit:
|
|
611
|
+
stale.append({
|
|
612
|
+
"index": index,
|
|
613
|
+
"unit_id": record.unit_id,
|
|
614
|
+
"reason": "observed_at_expired",
|
|
615
|
+
"observed_at": record.observed_at,
|
|
616
|
+
})
|
|
617
|
+
|
|
618
|
+
for unit_id, indexes in indexes_by_unit.items():
|
|
619
|
+
if len(indexes) > 1:
|
|
620
|
+
duplicate.append({"unit_id": unit_id, "indexes": indexes})
|
|
621
|
+
|
|
622
|
+
expected = _expected_ids(expected_units)
|
|
623
|
+
present = set(indexes_by_unit)
|
|
624
|
+
missing = tuple(sorted(expected - present))
|
|
625
|
+
return ManifestReport(
|
|
626
|
+
missing=missing,
|
|
627
|
+
stale=tuple(stale),
|
|
628
|
+
invalid=tuple(invalid),
|
|
629
|
+
duplicate=tuple(duplicate),
|
|
630
|
+
records=tuple(records),
|
|
631
|
+
)
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
class EvidenceStore:
|
|
635
|
+
"""工作区内的原子证据存储与 supervisor 合并器。"""
|
|
636
|
+
|
|
637
|
+
def __init__(self, path: str | Path, *, workspace_root: str | Path | None = None):
|
|
638
|
+
raw_path = Path(path).expanduser()
|
|
639
|
+
root = Path(workspace_root).expanduser() if workspace_root is not None else raw_path.parent
|
|
640
|
+
self.workspace_root = root.resolve(strict=True)
|
|
641
|
+
candidate = raw_path if raw_path.is_absolute() else Path.cwd() / raw_path
|
|
642
|
+
self.path = candidate.resolve(strict=False)
|
|
643
|
+
if not _within(self.path, self.workspace_root):
|
|
644
|
+
raise EvidenceError("证据 manifest 路径逃逸 workspace_root")
|
|
645
|
+
self.lock_path = self.path.with_name(self.path.name + ".lock")
|
|
646
|
+
|
|
647
|
+
def _load_records(self) -> list[EvidenceRecord]:
|
|
648
|
+
if not self.path.exists():
|
|
649
|
+
return []
|
|
650
|
+
payload = _read_json(self.path)
|
|
651
|
+
values = payload.get("records", []) if isinstance(payload, Mapping) else payload
|
|
652
|
+
if not isinstance(values, list):
|
|
653
|
+
raise EvidenceError("证据 manifest 的 records 必须是数组")
|
|
654
|
+
records: list[EvidenceRecord] = []
|
|
655
|
+
for value in values:
|
|
656
|
+
records.append(_coerce_record(value))
|
|
657
|
+
return records
|
|
658
|
+
|
|
659
|
+
def records(self) -> list[EvidenceRecord]:
|
|
660
|
+
"""读取当前记录快照。"""
|
|
661
|
+
with _exclusive_lock(self.lock_path):
|
|
662
|
+
return self._load_records()
|
|
663
|
+
|
|
664
|
+
def retarget(self, unit_ids: Iterable[str], target_fingerprint: str) -> list[str]:
|
|
665
|
+
"""把已存在记录的 ``target_fingerprint`` 改写到新目标(审计留档)。
|
|
666
|
+
|
|
667
|
+
只用于 ``rebind_target`` 中「本轮重扫确认该单元字节未变」的单元:它们的
|
|
668
|
+
证据仍指向同一段源码,只是目标指纹前滚了。不改这条字段,新的 state 指纹
|
|
669
|
+
与旧证据就会长期不一致,下一次 post 的 checkpoint 结算必然判 stale。
|
|
670
|
+
字节已变的单元一律不要调用——那会把旧结论套到新源码上。
|
|
671
|
+
"""
|
|
672
|
+
wanted = {str(unit) for unit in unit_ids}
|
|
673
|
+
if not wanted or not target_fingerprint.strip():
|
|
674
|
+
return []
|
|
675
|
+
with _exclusive_lock(self.lock_path):
|
|
676
|
+
records = self._load_records()
|
|
677
|
+
changed: list[str] = []
|
|
678
|
+
rewritten: list[EvidenceRecord] = []
|
|
679
|
+
for record in records:
|
|
680
|
+
if record.unit_id in wanted and record.target_fingerprint != target_fingerprint:
|
|
681
|
+
rewritten.append(replace(record, target_fingerprint=target_fingerprint))
|
|
682
|
+
changed.append(record.unit_id)
|
|
683
|
+
else:
|
|
684
|
+
rewritten.append(record)
|
|
685
|
+
if changed:
|
|
686
|
+
_atomic_json_write(self.path, self._state(rewritten, self._load_superseded()))
|
|
687
|
+
return sorted(changed)
|
|
688
|
+
|
|
689
|
+
def verify(
|
|
690
|
+
self,
|
|
691
|
+
record: EvidenceRecord | Mapping[str, Any] | str,
|
|
692
|
+
) -> VerificationResult:
|
|
693
|
+
"""验证传入记录;传入 unit_id 时验证 store 中的当前记录。"""
|
|
694
|
+
if isinstance(record, str):
|
|
695
|
+
current = next((item for item in self.records() if item.unit_id == record), None)
|
|
696
|
+
if current is None:
|
|
697
|
+
return VerificationResult(False, (f"找不到 unit_id: {record}",))
|
|
698
|
+
record = current
|
|
699
|
+
try:
|
|
700
|
+
coerced = _coerce_record(record)
|
|
701
|
+
except EvidenceError as exc:
|
|
702
|
+
return VerificationResult(False, (str(exc),))
|
|
703
|
+
return _validate_record(coerced, self.workspace_root)
|
|
704
|
+
|
|
705
|
+
def _normalise(self, record: EvidenceRecord | Mapping[str, Any]) -> EvidenceRecord:
|
|
706
|
+
coerced = _coerce_record(record)
|
|
707
|
+
result = _validate_record(coerced, self.workspace_root)
|
|
708
|
+
if not result.valid:
|
|
709
|
+
raise EvidenceValidationError(result.errors)
|
|
710
|
+
artifact = _resolve_artifact(coerced.artifact_path, self.workspace_root)
|
|
711
|
+
relative = artifact.relative_to(self.workspace_root).as_posix()
|
|
712
|
+
return replace(coerced, artifact_path=relative, artifact_sha256=coerced.artifact_sha256.lower())
|
|
713
|
+
|
|
714
|
+
@staticmethod
|
|
715
|
+
def _state(
|
|
716
|
+
records: Iterable[EvidenceRecord],
|
|
717
|
+
superseded: Iterable[Mapping[str, Any]] | None = None,
|
|
718
|
+
) -> dict[str, Any]:
|
|
719
|
+
payload: dict[str, Any] = {
|
|
720
|
+
"schema_version": SCHEMA_VERSION,
|
|
721
|
+
"records": [record.to_dict() for record in records],
|
|
722
|
+
}
|
|
723
|
+
entries = [dict(entry) for entry in superseded or ()]
|
|
724
|
+
if entries:
|
|
725
|
+
payload["superseded"] = entries
|
|
726
|
+
return payload
|
|
727
|
+
|
|
728
|
+
def _load_superseded(self) -> list[dict[str, Any]]:
|
|
729
|
+
"""读取被受控替换的历史记录(仅审计留档,不参与门禁判定)。"""
|
|
730
|
+
if not self.path.exists():
|
|
731
|
+
return []
|
|
732
|
+
payload = _read_json(self.path)
|
|
733
|
+
if not isinstance(payload, Mapping):
|
|
734
|
+
return []
|
|
735
|
+
values = payload.get("superseded") or []
|
|
736
|
+
if not isinstance(values, list):
|
|
737
|
+
raise EvidenceError("证据 manifest 的 superseded 必须是数组")
|
|
738
|
+
return [dict(value) for value in values if isinstance(value, Mapping)]
|
|
739
|
+
|
|
740
|
+
def superseded(self) -> list[dict[str, Any]]:
|
|
741
|
+
"""公开的审计视图:返回所有被取代的旧记录(含 unit_id 与 replaced)。"""
|
|
742
|
+
with _exclusive_lock(self.lock_path):
|
|
743
|
+
return self._load_superseded()
|
|
744
|
+
|
|
745
|
+
def store(
|
|
746
|
+
self,
|
|
747
|
+
record: EvidenceRecord | Mapping[str, Any],
|
|
748
|
+
*,
|
|
749
|
+
allow_supersede: bool = False,
|
|
750
|
+
allow_target_rebind: bool = False,
|
|
751
|
+
) -> StoreResult:
|
|
752
|
+
"""并发安全提交一条证据,不覆盖同 unit 的现有版本(除非显式指定 allow_supersede)。
|
|
753
|
+
|
|
754
|
+
``allow_target_rebind``(BX-ENGINE-21)仅在 ``allow_supersede`` 之上放宽
|
|
755
|
+
「target 必须一致」这一条,用于 target 重绑后的复验,避免旧证据成为不可超越
|
|
756
|
+
的墓碑;「已通过不得退化为未通过」的护栏不受影响。
|
|
757
|
+
"""
|
|
758
|
+
incoming = self._normalise(record)
|
|
759
|
+
with _exclusive_lock(self.lock_path):
|
|
760
|
+
records = self._load_records()
|
|
761
|
+
existing_index = next(
|
|
762
|
+
(i for i, item in enumerate(records) if item.unit_id == incoming.unit_id),
|
|
763
|
+
None,
|
|
764
|
+
)
|
|
765
|
+
if existing_index is not None:
|
|
766
|
+
existing = records[existing_index]
|
|
767
|
+
if _record_identity(existing) == _record_identity(incoming):
|
|
768
|
+
return StoreResult("duplicate", incoming.unit_id, incoming, existing)
|
|
769
|
+
if not allow_supersede:
|
|
770
|
+
return StoreResult(
|
|
771
|
+
"conflict",
|
|
772
|
+
incoming.unit_id,
|
|
773
|
+
incoming,
|
|
774
|
+
existing,
|
|
775
|
+
"同一 unit 的版本、target 或证据内容发生变化",
|
|
776
|
+
)
|
|
777
|
+
if not _supersede_allowed(
|
|
778
|
+
existing, incoming, allow_target_rebind=allow_target_rebind,
|
|
779
|
+
):
|
|
780
|
+
reason = (
|
|
781
|
+
"已通过的 coverage 证据不能被未通过记录退化"
|
|
782
|
+
if existing.status == "passed" and incoming.status != "passed"
|
|
783
|
+
else "同一 unit 的版本、target 或证据内容发生变化"
|
|
784
|
+
)
|
|
785
|
+
return StoreResult(
|
|
786
|
+
"conflict",
|
|
787
|
+
incoming.unit_id,
|
|
788
|
+
incoming,
|
|
789
|
+
existing,
|
|
790
|
+
reason,
|
|
791
|
+
)
|
|
792
|
+
superseded = self._load_superseded()
|
|
793
|
+
superseded.append({
|
|
794
|
+
"unit_id": incoming.unit_id,
|
|
795
|
+
"reason": "reverification supersedes a prior observation",
|
|
796
|
+
"at": incoming.observed_at,
|
|
797
|
+
"replaced": existing.to_dict(),
|
|
798
|
+
})
|
|
799
|
+
records[existing_index] = incoming
|
|
800
|
+
_atomic_json_write(self.path, self._state(records, superseded))
|
|
801
|
+
return StoreResult("superseded", incoming.unit_id, incoming, existing)
|
|
802
|
+
|
|
803
|
+
records.append(incoming)
|
|
804
|
+
_atomic_json_write(self.path, self._state(records))
|
|
805
|
+
return StoreResult("stored", incoming.unit_id, incoming)
|
|
806
|
+
|
|
807
|
+
def submit(
|
|
808
|
+
self,
|
|
809
|
+
record: EvidenceRecord | Mapping[str, Any],
|
|
810
|
+
*,
|
|
811
|
+
allow_supersede: bool = False,
|
|
812
|
+
allow_target_rebind: bool = False,
|
|
813
|
+
) -> StoreResult:
|
|
814
|
+
"""worker 侧语义别名;实际写入仍由原子 store 完成。"""
|
|
815
|
+
return self.store(
|
|
816
|
+
record,
|
|
817
|
+
allow_supersede=allow_supersede,
|
|
818
|
+
allow_target_rebind=allow_target_rebind,
|
|
819
|
+
)
|
|
820
|
+
|
|
821
|
+
def validate_manifest(
|
|
822
|
+
self,
|
|
823
|
+
manifest: Any,
|
|
824
|
+
*,
|
|
825
|
+
expected_units: Iterable[str] | Mapping[str, Any] | None = None,
|
|
826
|
+
expected_targets: Mapping[str, str] | None = None,
|
|
827
|
+
stale_after: timedelta | float | int | None = None,
|
|
828
|
+
now: datetime | None = None,
|
|
829
|
+
) -> ManifestReport:
|
|
830
|
+
values = _manifest_values(manifest)
|
|
831
|
+
return _validate_manifest_values(
|
|
832
|
+
values,
|
|
833
|
+
self.workspace_root,
|
|
834
|
+
expected_units=expected_units,
|
|
835
|
+
expected_targets=expected_targets,
|
|
836
|
+
stale_after=stale_after,
|
|
837
|
+
now=now,
|
|
838
|
+
)
|
|
839
|
+
|
|
840
|
+
def merge(
|
|
841
|
+
self,
|
|
842
|
+
manifest: Any,
|
|
843
|
+
*,
|
|
844
|
+
expected_units: Iterable[str] | Mapping[str, Any] | None = None,
|
|
845
|
+
expected_targets: Mapping[str, str] | None = None,
|
|
846
|
+
stale_after: timedelta | float | int | None = None,
|
|
847
|
+
now: datetime | None = None,
|
|
848
|
+
allow_supersede: bool = False,
|
|
849
|
+
) -> ManifestReport:
|
|
850
|
+
"""supervisor 串行合并 worker manifest,显式报告 duplicate/conflict。"""
|
|
851
|
+
values = _manifest_values(manifest)
|
|
852
|
+
report = _validate_manifest_values(
|
|
853
|
+
values,
|
|
854
|
+
self.workspace_root,
|
|
855
|
+
expected_units=expected_units,
|
|
856
|
+
expected_targets=expected_targets,
|
|
857
|
+
stale_after=stale_after,
|
|
858
|
+
now=now,
|
|
859
|
+
)
|
|
860
|
+
invalid_indexes = {item.get("index") for item in report.invalid}
|
|
861
|
+
valid_candidates: list[EvidenceRecord] = []
|
|
862
|
+
for index, value in enumerate(values):
|
|
863
|
+
if index in invalid_indexes:
|
|
864
|
+
continue
|
|
865
|
+
try:
|
|
866
|
+
valid_candidates.append(self._normalise(_coerce_record(value)))
|
|
867
|
+
except EvidenceError:
|
|
868
|
+
continue
|
|
869
|
+
|
|
870
|
+
duplicates = [dict(item) for item in report.duplicate]
|
|
871
|
+
conflicts: list[dict[str, Any]] = []
|
|
872
|
+
seen: dict[str, EvidenceRecord] = {}
|
|
873
|
+
for candidate in valid_candidates:
|
|
874
|
+
previous = seen.get(candidate.unit_id)
|
|
875
|
+
if previous is not None:
|
|
876
|
+
if _record_identity(previous) != _record_identity(candidate):
|
|
877
|
+
conflicts.append({
|
|
878
|
+
"unit_id": candidate.unit_id,
|
|
879
|
+
"reason": "同一 manifest 中同 unit 的版本或 target 变更",
|
|
880
|
+
})
|
|
881
|
+
continue
|
|
882
|
+
seen[candidate.unit_id] = candidate
|
|
883
|
+
|
|
884
|
+
with _exclusive_lock(self.lock_path):
|
|
885
|
+
current_records = self._load_records()
|
|
886
|
+
current_by_unit = {record.unit_id: record for record in current_records}
|
|
887
|
+
superseded = self._load_superseded()
|
|
888
|
+
changed = False
|
|
889
|
+
for candidate in seen.values():
|
|
890
|
+
existing = current_by_unit.get(candidate.unit_id)
|
|
891
|
+
if existing is None:
|
|
892
|
+
current_records.append(candidate)
|
|
893
|
+
current_by_unit[candidate.unit_id] = candidate
|
|
894
|
+
changed = True
|
|
895
|
+
elif _record_identity(existing) == _record_identity(candidate):
|
|
896
|
+
duplicates.append({"unit_id": candidate.unit_id, "reason": "store 已存在相同证据"})
|
|
897
|
+
elif allow_supersede and _supersede_allowed(existing, candidate):
|
|
898
|
+
idx = next(i for i, r in enumerate(current_records) if r.unit_id == candidate.unit_id)
|
|
899
|
+
superseded.append({
|
|
900
|
+
"unit_id": candidate.unit_id,
|
|
901
|
+
"reason": "reverification supersedes a prior observation",
|
|
902
|
+
"at": candidate.observed_at,
|
|
903
|
+
"replaced": existing.to_dict(),
|
|
904
|
+
})
|
|
905
|
+
current_records[idx] = candidate
|
|
906
|
+
current_by_unit[candidate.unit_id] = candidate
|
|
907
|
+
changed = True
|
|
908
|
+
elif allow_supersede and existing.status == "passed" and candidate.status != "passed":
|
|
909
|
+
conflicts.append({
|
|
910
|
+
"unit_id": candidate.unit_id,
|
|
911
|
+
"reason": "已通过的 coverage 证据不能被未通过记录退化",
|
|
912
|
+
})
|
|
913
|
+
else:
|
|
914
|
+
conflicts.append({
|
|
915
|
+
"unit_id": candidate.unit_id,
|
|
916
|
+
"reason": "store 中同 unit 的版本、target 或证据内容发生变化",
|
|
917
|
+
})
|
|
918
|
+
if changed:
|
|
919
|
+
_atomic_json_write(self.path, self._state(current_records, superseded))
|
|
920
|
+
|
|
921
|
+
return ManifestReport(
|
|
922
|
+
missing=report.missing,
|
|
923
|
+
stale=report.stale,
|
|
924
|
+
invalid=report.invalid,
|
|
925
|
+
duplicate=tuple(duplicates),
|
|
926
|
+
records=report.records,
|
|
927
|
+
conflicts=tuple(conflicts),
|
|
928
|
+
)
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
def validate_manifest(
|
|
932
|
+
manifest: Any,
|
|
933
|
+
*,
|
|
934
|
+
workspace_root: str | Path | None = None,
|
|
935
|
+
expected_units: Iterable[str] | Mapping[str, Any] | None = None,
|
|
936
|
+
expected_targets: Mapping[str, str] | None = None,
|
|
937
|
+
stale_after: timedelta | float | int | None = None,
|
|
938
|
+
now: datetime | None = None,
|
|
939
|
+
) -> ManifestReport:
|
|
940
|
+
"""无状态清单校验 API。"""
|
|
941
|
+
values = _manifest_values(manifest)
|
|
942
|
+
root = Path(workspace_root or Path.cwd()).expanduser().resolve(strict=True)
|
|
943
|
+
return _validate_manifest_values(
|
|
944
|
+
values,
|
|
945
|
+
root,
|
|
946
|
+
expected_units=expected_units,
|
|
947
|
+
expected_targets=expected_targets,
|
|
948
|
+
stale_after=stale_after,
|
|
949
|
+
now=now,
|
|
950
|
+
)
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
def merge(
|
|
954
|
+
manifest: Any,
|
|
955
|
+
store_path: str | Path,
|
|
956
|
+
*,
|
|
957
|
+
workspace_root: str | Path | None = None,
|
|
958
|
+
expected_units: Iterable[str] | Mapping[str, Any] | None = None,
|
|
959
|
+
expected_targets: Mapping[str, str] | None = None,
|
|
960
|
+
stale_after: timedelta | float | int | None = None,
|
|
961
|
+
now: datetime | None = None,
|
|
962
|
+
) -> ManifestReport:
|
|
963
|
+
"""无状态风格的串行 merge 便捷 API。"""
|
|
964
|
+
store = EvidenceStore(store_path, workspace_root=workspace_root)
|
|
965
|
+
return store.merge(
|
|
966
|
+
manifest,
|
|
967
|
+
expected_units=expected_units,
|
|
968
|
+
expected_targets=expected_targets,
|
|
969
|
+
stale_after=stale_after,
|
|
970
|
+
now=now,
|
|
971
|
+
)
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
def attach_attack_graph_snapshot(
|
|
975
|
+
evidence_dir: str | Path,
|
|
976
|
+
graph: Any,
|
|
977
|
+
) -> Path:
|
|
978
|
+
"""DOC-125: 在证据目录或存储目录下原子持久化动态攻击面图谱快照。"""
|
|
979
|
+
target_dir = Path(evidence_dir)
|
|
980
|
+
if target_dir.is_file():
|
|
981
|
+
target_dir = target_dir.parent
|
|
982
|
+
target_dir.mkdir(parents=True, exist_ok=True)
|
|
983
|
+
snapshot_path = target_dir / "attack-graph-snapshot.json"
|
|
984
|
+
|
|
985
|
+
if hasattr(graph, "to_dict"):
|
|
986
|
+
payload = graph.to_dict()
|
|
987
|
+
elif isinstance(graph, Mapping):
|
|
988
|
+
payload = dict(graph)
|
|
989
|
+
else:
|
|
990
|
+
raise EvidenceError("攻击面图谱对象必须提供 to_dict() 或为 Mapping")
|
|
991
|
+
|
|
992
|
+
_atomic_json_write(snapshot_path, payload)
|
|
993
|
+
return snapshot_path
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
def load_attack_graph_snapshot(evidence_dir: str | Path) -> dict[str, Any] | None:
|
|
997
|
+
"""DOC-125: 从证据目录中读取持久化的攻击面图谱快照。"""
|
|
998
|
+
target_dir = Path(evidence_dir)
|
|
999
|
+
if target_dir.is_file():
|
|
1000
|
+
target_dir = target_dir.parent
|
|
1001
|
+
snapshot_path = target_dir / "attack-graph-snapshot.json"
|
|
1002
|
+
if not snapshot_path.is_file():
|
|
1003
|
+
return None
|
|
1004
|
+
try:
|
|
1005
|
+
with open(snapshot_path, "r", encoding="utf-8") as f:
|
|
1006
|
+
return json.load(f)
|
|
1007
|
+
except (OSError, json.JSONDecodeError):
|
|
1008
|
+
return None
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
__all__ = [
|
|
1012
|
+
"EvidenceError",
|
|
1013
|
+
"EvidenceRecord",
|
|
1014
|
+
"EvidenceStore",
|
|
1015
|
+
"EvidenceValidationError",
|
|
1016
|
+
"ManifestReport",
|
|
1017
|
+
"StoreResult",
|
|
1018
|
+
"VALID_STATUSES",
|
|
1019
|
+
"VerificationResult",
|
|
1020
|
+
"attach_attack_graph_snapshot",
|
|
1021
|
+
"load_attack_graph_snapshot",
|
|
1022
|
+
"merge",
|
|
1023
|
+
"validate_manifest",
|
|
1024
|
+
]
|