bugx-cli 0.0.0-stage → 0.1.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/LICENSE +55 -0
- package/README.md +280 -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 +50 -0
- package/core/agent/__init__.py +146 -0
- package/core/agent/agent_artifacts.py +764 -0
- package/core/agent/agent_eval.py +1073 -0
- package/core/agent/agent_execution.py +552 -0
- package/core/agent/agent_orchestrator.py +1345 -0
- package/core/agent/agent_targets.py +871 -0
- package/core/agent/agent_workspace.py +770 -0
- package/core/assets/README.md +24 -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/project-layout.md +42 -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 +1822 -0
- package/core/autonomous_cve/audit_campaign.py +1232 -0
- package/core/autonomous_cve/behavior_graph.py +459 -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 +100 -0
- package/core/bugx.md +1299 -0
- package/core/bugx_paths.py +1846 -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 +1199 -0
- package/core/cloud_sync/cloud_onboard.py +702 -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 +760 -0
- package/core/cloud_sync/network_transport.py +275 -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 +721 -0
- package/core/contracts/AGENT_GUIDE.md +362 -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 +390 -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 +859 -0
- package/core/coverage/coverage_evidence.py +977 -0
- package/core/coverage/coverage_forensics.py +115 -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 +236 -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 +2625 -0
- package/core/fuzzing/README.md +49 -0
- package/core/fuzzing/__init__.py +82 -0
- package/core/fuzzing/concurrency_fuzzer.py +382 -0
- package/core/fuzzing/differential_oracle.py +629 -0
- package/core/fuzzing/fuzz_input.py +1188 -0
- package/core/fuzzing/stateful_fuzzer.py +332 -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 +772 -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 +41 -0
- package/core/host_acp/cline.py +235 -0
- package/core/host_acp/codex.py +245 -0
- package/core/host_acp/dsh.py +48 -0
- package/core/host_acp/opencode.py +154 -0
- package/core/host_acp/pi.py +36 -0
- package/core/host_acp/zcode.py +41 -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 +435 -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 +721 -0
- package/core/host_runtime/host_probe.py +41 -0
- package/core/host_runtime/host_provider_arbitrator.py +777 -0
- package/core/host_runtime/host_registry.py +655 -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 +368 -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 +136 -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 +451 -0
- package/core/hosts/claude.py +157 -0
- package/core/hosts/cline.py +225 -0
- package/core/hosts/codex.py +948 -0
- package/core/hosts/dsh.py +518 -0
- package/core/hosts/opencode.py +358 -0
- package/core/hosts/pi.py +151 -0
- package/core/hosts/zcode.py +219 -0
- package/core/i18n.py +252 -0
- package/core/infra/README.md +39 -0
- package/core/infra/__init__.py +15 -0
- package/core/infra/build_info.py +86 -0
- package/core/infra/client_metadata.py +185 -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 +1041 -0
- package/core/launch_phases/finding_confirm_phase.py +387 -0
- package/core/launch_phases/finding_settlement.py +1245 -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 +791 -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 +682 -0
- package/core/launch_phases/post_commit.py +567 -0
- package/core/launch_phases/post_coverage_sync.py +591 -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 +1087 -0
- package/core/launch_phases/post_receipt.py +362 -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 +862 -0
- package/core/launch_phases/pre_state_builder.py +1286 -0
- package/core/launch_phases/report_sharing.py +414 -0
- package/core/launch_phases/round_orchestrator.py +1133 -0
- package/core/launch_phases/session_runtime.py +1623 -0
- package/core/launch_phases/startup_gates.py +1022 -0
- package/core/launch_phases/startup_interaction.py +947 -0
- package/core/launch_phases/startup_version_gate.py +873 -0
- package/core/launch_phases/supervisor_bridge.py +1224 -0
- package/core/launch_phases/supervisor_coverage.py +417 -0
- package/core/launch_phases/supervisor_degraded.py +1219 -0
- package/core/launch_phases/worker_barriers.py +414 -0
- package/core/launch_phases/worker_orchestrator.py +1166 -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 +226 -0
- package/core/ledger/archive_ops.py +572 -0
- package/core/ledger/auto_materialization.py +459 -0
- package/core/ledger/bug-log.md +22 -0
- package/core/ledger/cli.py +270 -0
- package/core/ledger/export_ops.py +549 -0
- package/core/ledger/finding_confirmation.py +1274 -0
- package/core/ledger/finding_io.py +299 -0
- package/core/ledger/finding_lifecycle.py +307 -0
- package/core/ledger/finding_verification.py +369 -0
- package/core/ledger/incidents.py +999 -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 +681 -0
- package/core/ledger/receipt_contract.py +253 -0
- package/core/ledger/schema.py +1192 -0
- package/core/ledger/sync_ops.py +935 -0
- package/core/ledger/tool_experience.py +682 -0
- package/core/ledger/verify_life.py +1526 -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 +697 -0
- package/core/mcp_handlers/channel_lifecycle.py +696 -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 +239 -0
- package/core/mcp_handlers/rpc_server.py +465 -0
- package/core/mcp_handlers/supervisor_handlers.py +1435 -0
- package/core/mcp_handlers/telemetry_handlers.py +227 -0
- package/core/mcp_handlers/tool_table.py +1015 -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 +605 -0
- package/core/mcp_runtime_support/verifier.py +810 -0
- package/core/metadata.txt +5 -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 +267 -0
- package/core/planning/dynamic_flow_analyzer.py +328 -0
- package/core/planning/impact_pruner.py +435 -0
- package/core/planning/intent_adjudicator.py +175 -0
- package/core/planning/judge_pipeline.py +1163 -0
- package/core/planning/logic_analysis.py +419 -0
- package/core/planning/prompt_harness.py +1702 -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 +73 -0
- package/core/probes/README.md +51 -0
- package/core/probes/SPEC.md +26 -0
- package/core/probes/__init__.py +70 -0
- package/core/probes/android_probe.py +384 -0
- package/core/probes/base.py +147 -0
- package/core/probes/codebase_memory_probe.py +590 -0
- package/core/probes/desktop_probe.py +238 -0
- package/core/probes/differential_probe.py +356 -0
- package/core/probes/efficiency_probe.py +168 -0
- package/core/probes/ios_binary_probe.py +368 -0
- package/core/probes/memory_probe.py +326 -0
- package/core/probes/miniprogram_probe.py +323 -0
- package/core/probes/mobile_probe.py +281 -0
- package/core/probes/native_probe.py +214 -0
- package/core/probes/openwiki_probe.py +193 -0
- package/core/probes/pattern_probe.py +347 -0
- package/core/probes/platform_probe.py +343 -0
- package/core/probes/probe_registry.py +1669 -0
- package/core/probes/sast_probe.py +1009 -0
- package/core/probes/sast_rules.py +841 -0
- package/core/probes/sca_scanner.py +731 -0
- package/core/probes/web_probe.py +359 -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 +311 -0
- package/core/project_profiler/constants.py +247 -0
- package/core/project_profiler/detector.py +392 -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 +265 -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 +465 -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 +313 -0
- package/core/repair/rsi_repair.py +348 -0
- package/core/repair/rsi_rounds.py +212 -0
- package/core/repair/rsi_scope.py +262 -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 +224 -0
- package/core/report_engine/authority.py +255 -0
- package/core/report_engine/finding_report.py +417 -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 +761 -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 +822 -0
- package/core/report_engine/visual_report_builder.py +796 -0
- package/core/report_engine/web-conformance.md +63 -0
- package/core/report_engine/web_conformance.py +459 -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 +1684 -0
- package/core/retrieval/codebase_memory_bridge.py +337 -0
- package/core/retrieval/context_assembler.py +234 -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 +909 -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 +1132 -0
- package/core/retrieval/universal_code_matcher.py +437 -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 +1393 -0
- package/core/runtime/daemon_runner.py +623 -0
- package/core/runtime/fixture_solidify.py +449 -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 +769 -0
- package/core/runtime/platform_adapter.py +579 -0
- package/core/runtime/prep_validate.py +476 -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 +963 -0
- package/core/runtime/provider_window.py +97 -0
- package/core/runtime/resume_ticket.py +115 -0
- package/core/runtime/scenario_eval.py +814 -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 +416 -0
- package/core/skills/bugx_mcp_server.py +1134 -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 +760 -0
- package/core/supervisor/checkpoint_handler.py +368 -0
- package/core/supervisor/checkpoint_pipeline.py +1322 -0
- package/core/supervisor/checkpoint_sync.py +640 -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 +659 -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 +124 -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 +308 -0
- package/core/supervisor/round_governor.py +306 -0
- package/core/supervisor/serial_authorization.py +500 -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 +223 -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 +999 -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 +402 -0
- package/core/webui/INTERFACE.md +50 -0
- package/core/webui/README.md +57 -0
- package/core/webui/SPEC.md +25 -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 +218 -0
- package/core/webui/adapters/cline.py +399 -0
- package/core/webui/adapters/codex.py +256 -0
- package/core/webui/adapters/dsh.py +173 -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 +1743 -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/favicon.png +0 -0
- package/core/webui/static/icon-192.png +0 -0
- package/core/webui/static/index.html +42 -0
- package/core/webui/static/manifest.webmanifest +19 -0
- package/core/webui/static/project-settings.css +6458 -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 +281 -0
- package/docs/07_distribution/README.md +16 -0
- package/docs/README.md +121 -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 +3067 -0
- package/ecosystem/integrations/opencode/opencode.json +118 -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/README.md +143 -0
- package/interfaces/bin/README.md +35 -0
- package/interfaces/bin/bugx.js +80 -0
- package/interfaces/cli/README.md +90 -0
- package/interfaces/cli/bugx_cli/__init__.py +4 -0
- package/interfaces/cli/bugx_cli/cli.py +1678 -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 +3944 -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 +1370 -0
- package/interfaces/cli/bugx_cli/commands/daemon.py +50 -0
- package/interfaces/cli/bugx_cli/commands/doctor.py +1345 -0
- package/interfaces/cli/bugx_cli/commands/goal.py +107 -0
- package/interfaces/cli/bugx_cli/commands/interaction.py +244 -0
- package/interfaces/cli/bugx_cli/commands/knowledge.py +617 -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 +2714 -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 +728 -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 +260 -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 +898 -0
- package/interfaces/cli/bugx_cli/host_adapters.py +1917 -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 +571 -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 +721 -0
- package/interfaces/cli/bugx_cli/infra/engine_installer.py +1937 -0
- package/interfaces/cli/bugx_cli/infra/host_sync.py +545 -0
- package/interfaces/cli/bugx_cli/infra/mcp_advisor.py +755 -0
- package/interfaces/cli/bugx_cli/infra/security.py +332 -0
- package/package.json +98 -4
- package/pyproject.toml +246 -0
- package/scripts/README.md +118 -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 +37 -0
- package/scripts/test-npm-wrapper.mjs +265 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Supervisor Life context 绑定与事件编排边界。
|
|
3
|
+
|
|
4
|
+
输入入口、锁和持久化仍由 ``audit_supervisor.Supervisor`` 负责;本模块只
|
|
5
|
+
组合已校验的上下文字段并复用 Supervisor 的状态机窄边界。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from .supervisor_models import InvalidCheckpoint, InvalidTransition, RunState, SHA256_RE
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def apply_bind_context_operation(
|
|
14
|
+
supervisor,
|
|
15
|
+
state: RunState,
|
|
16
|
+
now: float,
|
|
17
|
+
*,
|
|
18
|
+
project_id: str | None = None,
|
|
19
|
+
session_id: str | None = None,
|
|
20
|
+
round_id: str | None = None,
|
|
21
|
+
life_id: str | None = None,
|
|
22
|
+
plan_hash: str | None = None,
|
|
23
|
+
) -> RunState:
|
|
24
|
+
"""``bind_context`` 的状态变更体(DES-03)。"""
|
|
25
|
+
if state.active_claims:
|
|
26
|
+
raise InvalidTransition("仍有 active worker,不能切换 Life 上下文")
|
|
27
|
+
supervisor._ensure_live_for_work(state)
|
|
28
|
+
supervisor._assert_target_fingerprint(state, now)
|
|
29
|
+
if plan_hash is not None and (
|
|
30
|
+
not isinstance(plan_hash, str) or not SHA256_RE.fullmatch(plan_hash)
|
|
31
|
+
):
|
|
32
|
+
raise InvalidCheckpoint("plan_hash 必须是 64 位十六进制 SHA-256")
|
|
33
|
+
for name, value in {
|
|
34
|
+
"project_id": project_id,
|
|
35
|
+
"session_id": session_id,
|
|
36
|
+
"round_id": round_id,
|
|
37
|
+
"life_id": life_id,
|
|
38
|
+
"plan_hash": plan_hash,
|
|
39
|
+
}.items():
|
|
40
|
+
if value is not None:
|
|
41
|
+
setattr(state, name, value)
|
|
42
|
+
supervisor._append_event(
|
|
43
|
+
state,
|
|
44
|
+
"life_context_bound",
|
|
45
|
+
now,
|
|
46
|
+
project_id=state.project_id,
|
|
47
|
+
session_id=state.session_id,
|
|
48
|
+
round_id=state.round_id,
|
|
49
|
+
life_id=state.life_id,
|
|
50
|
+
plan_hash=state.plan_hash,
|
|
51
|
+
)
|
|
52
|
+
return state
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
__all__ = ["apply_bind_context_operation"]
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""覆盖失效事务体(DES-03:把大闭包提升为具名操作)。
|
|
2
|
+
|
|
3
|
+
``L1-ADV-D4``:内容绑定覆盖一旦判定"字节变了旧证据失效",必须有一条引擎侧
|
|
4
|
+
唯一退回路径把 ``covered`` 退回 ``pending``,否则 ``TERMINAL_COVERED`` 会让
|
|
5
|
+
claim 永久拒绝该单元,单轮内无法重新覆盖。
|
|
6
|
+
|
|
7
|
+
本模块只放 ``AuditSupervisor.invalidate_coverage`` 的**事务体**;方法本身保持薄壳
|
|
8
|
+
(文件锁、revision 栅栏、事件与持久化都由 ``Supervisor._mutate`` 提供)。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from typing import Iterable
|
|
14
|
+
|
|
15
|
+
from .supervisor_models import (
|
|
16
|
+
COVERAGE_COVERED,
|
|
17
|
+
COVERAGE_PENDING,
|
|
18
|
+
ClaimRejected,
|
|
19
|
+
InvalidCheckpoint,
|
|
20
|
+
RunState,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
FENCEABLE_CLAIM_STATES = frozenset({"submitted", "released", "expired", "superseded"})
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def normalize_units(units: Iterable[str]) -> list[str]:
|
|
28
|
+
"""校验并去重宿主提交的覆盖单元(不接受臆造 id / 空值 / 字符串)。"""
|
|
29
|
+
if isinstance(units, (str, bytes)) or not hasattr(units, "__iter__"):
|
|
30
|
+
raise InvalidCheckpoint("units 必须是字符串序列")
|
|
31
|
+
wanted: list[str] = []
|
|
32
|
+
for unit in units:
|
|
33
|
+
if not isinstance(unit, str) or not unit.strip():
|
|
34
|
+
raise InvalidCheckpoint("coverage unit 必须是非空字符串")
|
|
35
|
+
name = unit.strip()
|
|
36
|
+
if name not in wanted:
|
|
37
|
+
wanted.append(name)
|
|
38
|
+
if not wanted:
|
|
39
|
+
raise InvalidCheckpoint("invalidate_coverage 必须提供至少一个 coverage unit")
|
|
40
|
+
return wanted
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def apply_invalidation(
|
|
44
|
+
supervisor: object,
|
|
45
|
+
state: RunState,
|
|
46
|
+
now: float,
|
|
47
|
+
*,
|
|
48
|
+
units: list[str],
|
|
49
|
+
reason: str,
|
|
50
|
+
expected_revision: int | None = None,
|
|
51
|
+
) -> dict[str, object]:
|
|
52
|
+
"""``covered -> pending`` 的唯一路径,含历史代钉扎(fenced claims)。"""
|
|
53
|
+
supervisor._check_revision(state, expected_revision) # type: ignore[attr-defined]
|
|
54
|
+
unknown = [unit for unit in units if unit not in state.coverage]
|
|
55
|
+
if unknown:
|
|
56
|
+
raise InvalidCheckpoint("未知 coverage unit: " + ", ".join(sorted(unknown)))
|
|
57
|
+
active = {
|
|
58
|
+
str(unit)
|
|
59
|
+
for claim in state.claims.values() if getattr(claim, "active", False)
|
|
60
|
+
for unit in getattr(claim, "unit_ids", ())
|
|
61
|
+
}
|
|
62
|
+
busy = sorted(active & set(units))
|
|
63
|
+
if busy:
|
|
64
|
+
raise ClaimRejected(
|
|
65
|
+
"覆盖单元仍有活跃租约,拒绝在飞行中失效: " + ", ".join(busy)
|
|
66
|
+
)
|
|
67
|
+
invalidated = [
|
|
68
|
+
unit for unit in units if state.coverage.get(unit) == COVERAGE_COVERED
|
|
69
|
+
]
|
|
70
|
+
skipped = [unit for unit in units if unit not in invalidated]
|
|
71
|
+
for unit in invalidated:
|
|
72
|
+
state.coverage[unit] = COVERAGE_PENDING
|
|
73
|
+
fenced = fence_claims_for(state, invalidated)
|
|
74
|
+
detail = str(reason).strip()
|
|
75
|
+
if invalidated:
|
|
76
|
+
supervisor._append_event( # type: ignore[attr-defined]
|
|
77
|
+
state, "coverage_invalidated", now,
|
|
78
|
+
units=list(invalidated), reason=detail,
|
|
79
|
+
channel="coverage_invalidate",
|
|
80
|
+
fenced_claims=sorted(fenced),
|
|
81
|
+
previous_revision=state.revision,
|
|
82
|
+
next_action="REDISPATCH_UNITS",
|
|
83
|
+
)
|
|
84
|
+
return {
|
|
85
|
+
"invalidated": list(invalidated),
|
|
86
|
+
"skipped": list(skipped),
|
|
87
|
+
"fenced_claims": sorted(fenced),
|
|
88
|
+
"reason": detail,
|
|
89
|
+
"coverage_complete": state.coverage_complete,
|
|
90
|
+
"revision": state.revision,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def fence_claims_for(state: RunState, units: list[str]) -> list[str]:
|
|
95
|
+
"""把失效单元的历史 claim 钉成历史代。
|
|
96
|
+
|
|
97
|
+
同一 Round 的去重规则(``lease_manager.claimed_units_this_round``)按"当前覆盖
|
|
98
|
+
代际"生效:内容失效使旧 claim 的产物作废,不钉扎就会继续拒绝重新派发。
|
|
99
|
+
``covered`` 单元本身不可申领(``TERMINAL_COVERED``),因此钉扎不会制造同一
|
|
100
|
+
单元的并发重复劳动;活跃租约在调用方已被 fail-closed 拒绝。
|
|
101
|
+
"""
|
|
102
|
+
targets = set(units)
|
|
103
|
+
if not targets:
|
|
104
|
+
return []
|
|
105
|
+
fenced: list[str] = []
|
|
106
|
+
for worker_key, claim in state.claims.items():
|
|
107
|
+
if not (set(getattr(claim, "unit_ids", ())) & targets):
|
|
108
|
+
continue
|
|
109
|
+
if getattr(claim, "state", "") not in FENCEABLE_CLAIM_STATES:
|
|
110
|
+
continue
|
|
111
|
+
if getattr(claim, "fenced_from_attempts", False):
|
|
112
|
+
continue
|
|
113
|
+
claim.fenced_from_attempts = True
|
|
114
|
+
fenced.append(worker_key)
|
|
115
|
+
return sorted(fenced)
|
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Crash-safe state/report/seal commit log for Finding publication.
|
|
3
|
+
|
|
4
|
+
The Supervisor owns the business lock; this module owns the durable PREPARE /
|
|
5
|
+
APPLY / COMMIT record underneath it. A pending transaction is always replayed
|
|
6
|
+
forward from the recorded after bytes. It is never repaired by guessing which
|
|
7
|
+
directory is newest or by copying an unrelated backup over a newer state.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import base64
|
|
13
|
+
import hashlib
|
|
14
|
+
import json
|
|
15
|
+
import os
|
|
16
|
+
import tempfile
|
|
17
|
+
import time
|
|
18
|
+
import uuid
|
|
19
|
+
from contextlib import contextmanager
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Callable, Mapping
|
|
22
|
+
|
|
23
|
+
try:
|
|
24
|
+
import fcntl
|
|
25
|
+
except ImportError: # pragma: no cover
|
|
26
|
+
fcntl = None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
MAX_PARTICIPANTS = 4
|
|
30
|
+
MAX_PAYLOAD_BYTES = 16 * 1024 * 1024
|
|
31
|
+
MAX_MANIFEST_BYTES = 64 * 1024
|
|
32
|
+
COMMIT_SCHEMA_VERSION = 1
|
|
33
|
+
PHASE_PRECHECK = "PRECHECK"
|
|
34
|
+
PHASE_PREPARE = "PREPARE"
|
|
35
|
+
PHASE_APPLY = "APPLY"
|
|
36
|
+
PHASE_COMMIT = "COMMIT"
|
|
37
|
+
PHASE_CLEANUP = "CLEANUP"
|
|
38
|
+
RECOVERY_REQUIRED = "RECOVERY_REQUIRED"
|
|
39
|
+
# Short names mirror the protocol text and keep test/host callers from
|
|
40
|
+
# hard-coding the wire values.
|
|
41
|
+
PRECHECK = PHASE_PRECHECK
|
|
42
|
+
PREPARE = PHASE_PREPARE
|
|
43
|
+
APPLY = PHASE_APPLY
|
|
44
|
+
COMMIT = PHASE_COMMIT
|
|
45
|
+
CLEANUP = PHASE_CLEANUP
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class FindingCommitError(RuntimeError):
|
|
49
|
+
"""Commit validation or I/O failure."""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class RecoveryRequired(FindingCommitError):
|
|
53
|
+
"""The on-disk transaction cannot be safely replayed."""
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _sha256(data: bytes) -> str:
|
|
57
|
+
return hashlib.sha256(data).hexdigest()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _canonical(value: object) -> bytes:
|
|
61
|
+
return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False).encode("utf-8")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _fsync_dir(path: Path) -> None:
|
|
65
|
+
try:
|
|
66
|
+
descriptor = os.open(path, os.O_RDONLY)
|
|
67
|
+
try:
|
|
68
|
+
os.fsync(descriptor)
|
|
69
|
+
finally:
|
|
70
|
+
os.close(descriptor)
|
|
71
|
+
except OSError:
|
|
72
|
+
pass
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _atomic_bytes(path: Path, data: bytes, *, mode: int = 0o600) -> None:
|
|
76
|
+
path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
77
|
+
try:
|
|
78
|
+
os.chmod(path.parent, 0o700)
|
|
79
|
+
except OSError:
|
|
80
|
+
pass
|
|
81
|
+
descriptor, temporary = tempfile.mkstemp(prefix=f".{path.name}.", dir=str(path.parent))
|
|
82
|
+
try:
|
|
83
|
+
os.fchmod(descriptor, mode)
|
|
84
|
+
with os.fdopen(descriptor, "wb") as handle:
|
|
85
|
+
handle.write(data)
|
|
86
|
+
handle.flush()
|
|
87
|
+
os.fsync(handle.fileno())
|
|
88
|
+
os.replace(temporary, path)
|
|
89
|
+
_fsync_dir(path.parent)
|
|
90
|
+
except BaseException:
|
|
91
|
+
try:
|
|
92
|
+
os.close(descriptor)
|
|
93
|
+
except OSError:
|
|
94
|
+
pass
|
|
95
|
+
try:
|
|
96
|
+
os.unlink(temporary)
|
|
97
|
+
except OSError:
|
|
98
|
+
pass
|
|
99
|
+
raise
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _safe_participant(root: Path, path: Path) -> Path:
|
|
103
|
+
root = Path(root).resolve()
|
|
104
|
+
path = Path(path)
|
|
105
|
+
if not path.is_absolute():
|
|
106
|
+
path = root / path
|
|
107
|
+
# macOS exposes /var and /tmp as lexical aliases of /private/var and
|
|
108
|
+
# /private/tmp. Resolve only for the containment comparison; retain the
|
|
109
|
+
# original path for the no-follow final-component check.
|
|
110
|
+
lexical = path.absolute()
|
|
111
|
+
path = Path(os.path.realpath(str(lexical)))
|
|
112
|
+
try:
|
|
113
|
+
path.relative_to(root)
|
|
114
|
+
except ValueError as exc:
|
|
115
|
+
raise FindingCommitError(f"participant 路径越界: {path}") from exc
|
|
116
|
+
lexical_for_check = lexical
|
|
117
|
+
# macOS exposes /var and /tmp as lexical aliases of /private/*; normalize
|
|
118
|
+
# only that platform alias before checking user-controlled components.
|
|
119
|
+
for alias in ("var", "tmp"):
|
|
120
|
+
alias_path = Path("/") / alias
|
|
121
|
+
canonical_alias = Path("/private") / alias
|
|
122
|
+
if lexical.parts[:2] == alias_path.parts and root.parts[:3] == canonical_alias.parts:
|
|
123
|
+
try:
|
|
124
|
+
lexical_for_check = canonical_alias / lexical.relative_to(alias_path)
|
|
125
|
+
except ValueError:
|
|
126
|
+
pass
|
|
127
|
+
break
|
|
128
|
+
cursor = lexical_for_check
|
|
129
|
+
lexical_symlink = False
|
|
130
|
+
try:
|
|
131
|
+
relative = lexical_for_check.relative_to(root)
|
|
132
|
+
cursor = root
|
|
133
|
+
for part in relative.parts:
|
|
134
|
+
cursor = cursor / part
|
|
135
|
+
if cursor.is_symlink():
|
|
136
|
+
lexical_symlink = True
|
|
137
|
+
break
|
|
138
|
+
except ValueError:
|
|
139
|
+
lexical_symlink = True
|
|
140
|
+
if lexical_symlink or lexical.is_symlink() or path.is_symlink():
|
|
141
|
+
raise FindingCommitError(f"participant 不能是符号链接: {path}")
|
|
142
|
+
if path.exists():
|
|
143
|
+
try:
|
|
144
|
+
if path.stat().st_nlink != 1:
|
|
145
|
+
raise FindingCommitError(f"participant 不能是硬链接别名: {path}")
|
|
146
|
+
except OSError as exc:
|
|
147
|
+
raise FindingCommitError(f"participant 无法 stat: {path}") from exc
|
|
148
|
+
if not path.is_file():
|
|
149
|
+
raise FindingCommitError(f"participant 不是普通文件: {path}")
|
|
150
|
+
return path
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _read_existing(path: Path) -> tuple[bool, bytes]:
|
|
154
|
+
if path.is_symlink():
|
|
155
|
+
raise FindingCommitError(f"participant 不能是符号链接: {path}")
|
|
156
|
+
if not path.exists():
|
|
157
|
+
return False, b""
|
|
158
|
+
if not path.is_file():
|
|
159
|
+
raise FindingCommitError(f"participant 不是普通文件: {path}")
|
|
160
|
+
return True, path.read_bytes()
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _write_participant(path: Path, data: bytes, *, exists: bool) -> None:
|
|
164
|
+
if not exists:
|
|
165
|
+
try:
|
|
166
|
+
path.unlink()
|
|
167
|
+
except FileNotFoundError:
|
|
168
|
+
pass
|
|
169
|
+
return
|
|
170
|
+
_atomic_bytes(path, data)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _call_failpoint(callback: Callable[[str], object] | None, stage: str) -> None:
|
|
174
|
+
if callback is not None:
|
|
175
|
+
callback(stage)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class FindingCommitLog:
|
|
179
|
+
"""One bounded transaction log rooted at ``state_root/finding-commit``."""
|
|
180
|
+
|
|
181
|
+
def __init__(self, state_root: Path, *, failpoint: Callable[[str], object] | None = None) -> None:
|
|
182
|
+
self.state_root = Path(state_root).absolute()
|
|
183
|
+
if self.state_root.is_symlink() or not self.state_root.is_dir():
|
|
184
|
+
raise FindingCommitError(f"state_root 不是安全目录: {self.state_root}")
|
|
185
|
+
self.root = self.state_root / "finding-commit"
|
|
186
|
+
self.pending_path = self.root / "pending.json"
|
|
187
|
+
self.lock_path = self.root / "commit.lock"
|
|
188
|
+
self.failpoint = failpoint
|
|
189
|
+
|
|
190
|
+
@contextmanager
|
|
191
|
+
def _lock(self):
|
|
192
|
+
self.root.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
193
|
+
descriptor = os.open(self.lock_path, os.O_RDWR | os.O_CREAT, 0o600)
|
|
194
|
+
handle = os.fdopen(descriptor, "a+b")
|
|
195
|
+
try:
|
|
196
|
+
if fcntl is not None:
|
|
197
|
+
fcntl.flock(handle.fileno(), fcntl.LOCK_EX)
|
|
198
|
+
yield
|
|
199
|
+
finally:
|
|
200
|
+
try:
|
|
201
|
+
if fcntl is not None:
|
|
202
|
+
fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
|
|
203
|
+
finally:
|
|
204
|
+
handle.close()
|
|
205
|
+
|
|
206
|
+
def _transaction_dir(self, txid: str) -> Path:
|
|
207
|
+
if not isinstance(txid, str) or not txid or "/" in txid or "\\" in txid or len(txid) > 96:
|
|
208
|
+
raise FindingCommitError("txid 无效")
|
|
209
|
+
return self.root / txid
|
|
210
|
+
|
|
211
|
+
def _load_manifest(self, txid: str) -> dict[str, object]:
|
|
212
|
+
directory = self._transaction_dir(txid)
|
|
213
|
+
path = directory / "manifest.json"
|
|
214
|
+
try:
|
|
215
|
+
if path.is_symlink() or not path.is_file() or path.stat().st_size > MAX_MANIFEST_BYTES:
|
|
216
|
+
raise RecoveryRequired("manifest 不存在或超过大小限制")
|
|
217
|
+
payload = json.loads(path.read_text(encoding="utf-8"))
|
|
218
|
+
except RecoveryRequired:
|
|
219
|
+
raise
|
|
220
|
+
except (OSError, UnicodeError, ValueError, json.JSONDecodeError) as exc:
|
|
221
|
+
raise RecoveryRequired(f"manifest 无法读取: {exc}") from exc
|
|
222
|
+
if not isinstance(payload, dict) or payload.get("schema_version") != COMMIT_SCHEMA_VERSION:
|
|
223
|
+
raise RecoveryRequired("manifest schema 不支持")
|
|
224
|
+
if payload.get("txid") != txid:
|
|
225
|
+
raise RecoveryRequired("manifest txid 不匹配")
|
|
226
|
+
participants = payload.get("participants")
|
|
227
|
+
if not isinstance(participants, dict) or not participants or len(participants) > MAX_PARTICIPANTS:
|
|
228
|
+
raise RecoveryRequired("manifest participants 无效")
|
|
229
|
+
return payload
|
|
230
|
+
|
|
231
|
+
def _validate_manifest_paths(self, manifest: Mapping[str, object]) -> list[tuple[Path, dict[str, object]]]:
|
|
232
|
+
txid = str(manifest.get("txid"))
|
|
233
|
+
directory = self._transaction_dir(txid)
|
|
234
|
+
participants = manifest.get("participants")
|
|
235
|
+
if not isinstance(participants, Mapping):
|
|
236
|
+
raise RecoveryRequired("manifest participants 无效")
|
|
237
|
+
result: list[tuple[Path, dict[str, object]]] = []
|
|
238
|
+
for name, raw in participants.items():
|
|
239
|
+
if not isinstance(name, str) or not isinstance(raw, Mapping):
|
|
240
|
+
raise RecoveryRequired("manifest participant 条目无效")
|
|
241
|
+
relative = raw.get("path")
|
|
242
|
+
if not isinstance(relative, str) or relative.startswith("/") or "/" in relative or "\\" in relative:
|
|
243
|
+
raise RecoveryRequired("manifest participant path 非固定路径")
|
|
244
|
+
if relative not in {"state", "backup", "report", "seal"}:
|
|
245
|
+
raise RecoveryRequired("manifest 含未知 participant")
|
|
246
|
+
target = _safe_participant(self.state_root, str(raw.get("target")))
|
|
247
|
+
if target.is_symlink():
|
|
248
|
+
raise RecoveryRequired("participant 目标是符号链接")
|
|
249
|
+
for field in ("before_sha256", "after_sha256"):
|
|
250
|
+
value = raw.get(field)
|
|
251
|
+
if not isinstance(value, str) or len(value) != 64:
|
|
252
|
+
raise RecoveryRequired(f"manifest {field} 无效")
|
|
253
|
+
before_exists = raw.get("before_exists")
|
|
254
|
+
after_exists = raw.get("after_exists")
|
|
255
|
+
if not isinstance(before_exists, bool) or not isinstance(after_exists, bool):
|
|
256
|
+
raise RecoveryRequired("manifest 存在性字段无效")
|
|
257
|
+
result.append((target, dict(raw)))
|
|
258
|
+
return result
|
|
259
|
+
|
|
260
|
+
def _payload(self, directory: Path, relative: str, *, after: bool) -> bytes:
|
|
261
|
+
path = directory / f"{relative}.{'after' if after else 'before'}"
|
|
262
|
+
if path.is_symlink() or not path.is_file():
|
|
263
|
+
raise RecoveryRequired(f"缺少 transaction payload: {path}")
|
|
264
|
+
data = path.read_bytes()
|
|
265
|
+
if len(data) > MAX_PAYLOAD_BYTES:
|
|
266
|
+
raise RecoveryRequired("transaction payload 超过大小限制")
|
|
267
|
+
return data
|
|
268
|
+
|
|
269
|
+
def _verify_bytes(self, data: bytes, expected: str, label: str) -> None:
|
|
270
|
+
if _sha256(data) != expected:
|
|
271
|
+
raise RecoveryRequired(f"{label} hash 不匹配")
|
|
272
|
+
|
|
273
|
+
def _recover_locked(self) -> dict[str, object] | None:
|
|
274
|
+
"""Replay a pending transaction; caller must hold ``_lock``."""
|
|
275
|
+
if not self.pending_path.exists():
|
|
276
|
+
return None
|
|
277
|
+
if self.pending_path.is_symlink():
|
|
278
|
+
raise RecoveryRequired("pending pointer 是符号链接")
|
|
279
|
+
try:
|
|
280
|
+
pointer = json.loads(self.pending_path.read_text(encoding="utf-8"))
|
|
281
|
+
except (OSError, UnicodeError, ValueError, json.JSONDecodeError) as exc:
|
|
282
|
+
raise RecoveryRequired(f"pending pointer 无法解析: {exc}") from exc
|
|
283
|
+
if not isinstance(pointer, Mapping) or not isinstance(pointer.get("txid"), str):
|
|
284
|
+
raise RecoveryRequired("pending pointer 无效")
|
|
285
|
+
txid = str(pointer["txid"])
|
|
286
|
+
manifest = self._load_manifest(txid)
|
|
287
|
+
participants = self._validate_manifest_paths(manifest)
|
|
288
|
+
directory = self._transaction_dir(txid)
|
|
289
|
+
for target, item in participants:
|
|
290
|
+
relative = str(item["path"])
|
|
291
|
+
after = self._payload(directory, relative, after=True)
|
|
292
|
+
self._verify_bytes(after, str(item["after_sha256"]), f"{relative}.after")
|
|
293
|
+
before_exists = bool(item["before_exists"])
|
|
294
|
+
before = self._payload(directory, relative, after=False) if before_exists else b""
|
|
295
|
+
if before_exists:
|
|
296
|
+
self._verify_bytes(before, str(item["before_sha256"]), f"{relative}.before")
|
|
297
|
+
current_exists, current = _read_existing(target)
|
|
298
|
+
current_sha = _sha256(current) if current_exists else _sha256(b"")
|
|
299
|
+
if current_exists != before_exists and current_sha != str(item["after_sha256"]):
|
|
300
|
+
raise RecoveryRequired(f"{relative} 当前存在性与 before/after 均不匹配")
|
|
301
|
+
if current_exists and current_sha not in {str(item["before_sha256"]), str(item["after_sha256"])}:
|
|
302
|
+
raise RecoveryRequired(f"{relative} 当前字节不是记录的 before/after")
|
|
303
|
+
if not current_exists and current_sha not in {str(item["before_sha256"]), str(item["after_sha256"])}:
|
|
304
|
+
raise RecoveryRequired(f"{relative} 当前缺失且不是记录的 before")
|
|
305
|
+
if current_sha != str(item["after_sha256"]) or current_exists != bool(item["after_exists"]):
|
|
306
|
+
_write_participant(target, after, exists=bool(item["after_exists"]))
|
|
307
|
+
receipt = self._commit_marker(manifest, recovered=True)
|
|
308
|
+
try:
|
|
309
|
+
self.pending_path.unlink()
|
|
310
|
+
except FileNotFoundError:
|
|
311
|
+
pass
|
|
312
|
+
_fsync_dir(self.root)
|
|
313
|
+
return receipt
|
|
314
|
+
|
|
315
|
+
def recover(self) -> dict[str, object] | None:
|
|
316
|
+
"""Replay the one pending transaction, if any, to COMMITTED."""
|
|
317
|
+
with self._lock():
|
|
318
|
+
return self._recover_locked()
|
|
319
|
+
|
|
320
|
+
def _commit_marker(self, manifest: Mapping[str, object], *, recovered: bool = False) -> dict[str, object]:
|
|
321
|
+
txid = str(manifest["txid"])
|
|
322
|
+
receipt = {
|
|
323
|
+
"schema_version": COMMIT_SCHEMA_VERSION,
|
|
324
|
+
"txid": txid,
|
|
325
|
+
"phase": PHASE_COMMIT,
|
|
326
|
+
"committed_at": time.time(),
|
|
327
|
+
"recovered": bool(recovered),
|
|
328
|
+
"participants": list((manifest.get("participants") or {}).keys()),
|
|
329
|
+
}
|
|
330
|
+
_atomic_bytes(self._transaction_dir(txid) / "COMMITTED.json", _canonical(receipt))
|
|
331
|
+
return receipt
|
|
332
|
+
|
|
333
|
+
def commit(self, participants: Mapping[str, Path | str | None], after: Mapping[str, bytes], *,
|
|
334
|
+
txid: str | None = None, request_id: str | None = None,
|
|
335
|
+
expected_revision: int | None = None) -> dict[str, object]:
|
|
336
|
+
"""Commit fixed participant paths with PRECHECK -> PREPARE -> APPLY -> COMMIT."""
|
|
337
|
+
names = set(participants)
|
|
338
|
+
if not names or len(names) > MAX_PARTICIPANTS or not names.issubset({"state", "backup", "report", "seal"}):
|
|
339
|
+
raise FindingCommitError("participants 必须是固定且不超过四个路径")
|
|
340
|
+
if set(after) != names:
|
|
341
|
+
raise FindingCommitError("after 必须覆盖全部 participants")
|
|
342
|
+
if expected_revision is not None:
|
|
343
|
+
if isinstance(expected_revision, bool) or not isinstance(expected_revision, int) or expected_revision < 0:
|
|
344
|
+
raise FindingCommitError("expected_revision 无效")
|
|
345
|
+
try:
|
|
346
|
+
state_payload = json.loads(after["state"].decode("utf-8"))
|
|
347
|
+
except (AttributeError, UnicodeError, ValueError, json.JSONDecodeError) as exc:
|
|
348
|
+
raise FindingCommitError("state after 无法校验 revision") from exc
|
|
349
|
+
if not isinstance(state_payload, Mapping) or state_payload.get("revision") != expected_revision:
|
|
350
|
+
raise FindingCommitError("state after revision 与 expected_revision 不一致")
|
|
351
|
+
txid = txid or str(uuid.uuid4())
|
|
352
|
+
if request_id is not None and (not isinstance(request_id, str) or not request_id.strip()):
|
|
353
|
+
raise FindingCommitError("request_id 无效")
|
|
354
|
+
with self._lock():
|
|
355
|
+
recovered = self._recover_locked()
|
|
356
|
+
if recovered is not None and self.pending_path.exists():
|
|
357
|
+
raise RecoveryRequired("无法在已有 pending transaction 后继续提交")
|
|
358
|
+
targets: dict[str, Path] = {}
|
|
359
|
+
entries: dict[str, dict[str, object]] = {}
|
|
360
|
+
for name, raw_path in participants.items():
|
|
361
|
+
if raw_path is None:
|
|
362
|
+
raise FindingCommitError(f"participant {name} 路径缺失")
|
|
363
|
+
target = _safe_participant(self.state_root, Path(raw_path))
|
|
364
|
+
existing, before = _read_existing(target)
|
|
365
|
+
payload = after[name]
|
|
366
|
+
if not isinstance(payload, bytes):
|
|
367
|
+
raise FindingCommitError(f"participant {name} after 必须是 bytes")
|
|
368
|
+
if len(payload) > MAX_PAYLOAD_BYTES:
|
|
369
|
+
raise FindingCommitError(f"participant {name} 超过大小限制")
|
|
370
|
+
targets[name] = target
|
|
371
|
+
entries[name] = {
|
|
372
|
+
"path": name,
|
|
373
|
+
"target": str(target),
|
|
374
|
+
"before_exists": existing,
|
|
375
|
+
"after_exists": True,
|
|
376
|
+
"before_sha256": _sha256(before),
|
|
377
|
+
"after_sha256": _sha256(payload),
|
|
378
|
+
}
|
|
379
|
+
directory = self._transaction_dir(txid)
|
|
380
|
+
if directory.exists():
|
|
381
|
+
raise FindingCommitError(f"txid 已存在: {txid}")
|
|
382
|
+
directory.mkdir(parents=True, mode=0o700)
|
|
383
|
+
for name, target in targets.items():
|
|
384
|
+
existing, before = _read_existing(target)
|
|
385
|
+
_atomic_bytes(directory / f"{name}.before", before)
|
|
386
|
+
_atomic_bytes(directory / f"{name}.after", after[name])
|
|
387
|
+
manifest = {
|
|
388
|
+
"schema_version": COMMIT_SCHEMA_VERSION,
|
|
389
|
+
"txid": txid,
|
|
390
|
+
"request_id": request_id,
|
|
391
|
+
"expected_revision": expected_revision,
|
|
392
|
+
"phase": PHASE_PREPARE,
|
|
393
|
+
"created_at": time.time(),
|
|
394
|
+
"participants": entries,
|
|
395
|
+
}
|
|
396
|
+
_atomic_bytes(directory / "manifest.json", _canonical(manifest))
|
|
397
|
+
_call_failpoint(self.failpoint, "after-prepare")
|
|
398
|
+
_atomic_bytes(self.pending_path, _canonical({"schema_version": COMMIT_SCHEMA_VERSION, "txid": txid}))
|
|
399
|
+
_call_failpoint(self.failpoint, "after-prepared")
|
|
400
|
+
manifest["phase"] = PHASE_APPLY
|
|
401
|
+
_atomic_bytes(directory / "manifest.json", _canonical(manifest))
|
|
402
|
+
for name, target in targets.items():
|
|
403
|
+
_write_participant(target, after[name], exists=True)
|
|
404
|
+
_call_failpoint(self.failpoint, f"after-apply-{name}")
|
|
405
|
+
_call_failpoint(self.failpoint, "after-apply")
|
|
406
|
+
manifest["phase"] = PHASE_COMMIT
|
|
407
|
+
_atomic_bytes(directory / "manifest.json", _canonical(manifest))
|
|
408
|
+
receipt = self._commit_marker(manifest)
|
|
409
|
+
try:
|
|
410
|
+
self.pending_path.unlink()
|
|
411
|
+
except FileNotFoundError:
|
|
412
|
+
pass
|
|
413
|
+
_fsync_dir(self.root)
|
|
414
|
+
_call_failpoint(self.failpoint, "after-commit")
|
|
415
|
+
return receipt
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def commit_finding_transaction(state_root: Path, *, state_path: Path, backup_path: Path | None = None,
|
|
419
|
+
report_path: Path | None = None, seal_path: Path | None = None,
|
|
420
|
+
state_bytes: bytes, report_bytes: bytes | None = None,
|
|
421
|
+
seal_bytes: bytes | None = None, failpoint: Callable[[str], object] | None = None,
|
|
422
|
+
txid: str | None = None, request_id: str | None = None,
|
|
423
|
+
expected_revision: int | None = None) -> dict[str, object]:
|
|
424
|
+
"""Convenience entry point for the Supervisor state/report/seal trio."""
|
|
425
|
+
participants: dict[str, Path] = {"state": Path(state_path)}
|
|
426
|
+
after: dict[str, bytes] = {"state": state_bytes}
|
|
427
|
+
if backup_path is not None:
|
|
428
|
+
participants["backup"] = Path(backup_path)
|
|
429
|
+
after["backup"] = state_bytes
|
|
430
|
+
if report_path is not None and report_bytes is not None:
|
|
431
|
+
participants["report"] = Path(report_path)
|
|
432
|
+
after["report"] = report_bytes
|
|
433
|
+
if seal_path is not None and seal_bytes is not None:
|
|
434
|
+
participants["seal"] = Path(seal_path)
|
|
435
|
+
after["seal"] = seal_bytes
|
|
436
|
+
if report_bytes is not None and seal_bytes is not None:
|
|
437
|
+
try:
|
|
438
|
+
seal = json.loads(seal_bytes.decode("utf-8"))
|
|
439
|
+
except (UnicodeError, ValueError, json.JSONDecodeError) as exc:
|
|
440
|
+
raise FindingCommitError("seal after 无法解析") from exc
|
|
441
|
+
if not isinstance(seal, Mapping) or seal.get("report_sha256") != _sha256(report_bytes):
|
|
442
|
+
raise FindingCommitError("seal.report_sha256 与 report after 不一致")
|
|
443
|
+
try:
|
|
444
|
+
state_payload = json.loads(state_bytes.decode("utf-8"))
|
|
445
|
+
except (UnicodeError, ValueError, json.JSONDecodeError) as exc:
|
|
446
|
+
raise FindingCommitError("state after 无法解析") from exc
|
|
447
|
+
if isinstance(state_payload, Mapping) and state_payload.get("report_seal") not in (None, dict(seal)):
|
|
448
|
+
raise FindingCommitError("state.report_seal 与 seal after 不一致")
|
|
449
|
+
return FindingCommitLog(state_root, failpoint=failpoint).commit(
|
|
450
|
+
participants, after, txid=txid, request_id=request_id,
|
|
451
|
+
expected_revision=expected_revision,
|
|
452
|
+
)
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
def recover_finding_commit(state_root: Path) -> dict[str, object] | None:
|
|
456
|
+
return FindingCommitLog(state_root).recover()
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
# The log object is the protocol entry point; retain a descriptive alias for
|
|
460
|
+
# callers that name it after the transaction rather than its storage medium.
|
|
461
|
+
FindingCommit = FindingCommitLog
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
__all__ = [
|
|
465
|
+
"MAX_PARTICIPANTS", "MAX_PAYLOAD_BYTES", "MAX_MANIFEST_BYTES", "COMMIT_SCHEMA_VERSION",
|
|
466
|
+
"PHASE_PRECHECK", "PHASE_PREPARE", "PHASE_APPLY", "PHASE_COMMIT", "PHASE_CLEANUP",
|
|
467
|
+
"PRECHECK", "PREPARE", "APPLY", "COMMIT", "CLEANUP",
|
|
468
|
+
"RECOVERY_REQUIRED", "FindingCommitError", "RecoveryRequired", "FindingCommitLog", "FindingCommit",
|
|
469
|
+
"commit_finding_transaction", "recover_finding_commit",
|
|
470
|
+
]
|