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,770 @@
|
|
|
1
|
+
"""Private, content-addressed working copies for autonomous agents.
|
|
2
|
+
|
|
3
|
+
The caller supplies the engine's private directory, a run UUID, and the local
|
|
4
|
+
project to inspect. The origin is only opened for reading; all execution
|
|
5
|
+
copies live below ``runs/<run_uuid>/workspace``.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import fnmatch
|
|
11
|
+
import hashlib
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
import shutil
|
|
15
|
+
import stat
|
|
16
|
+
import uuid
|
|
17
|
+
from pathlib import Path, PurePosixPath
|
|
18
|
+
from types import MappingProxyType
|
|
19
|
+
from typing import Iterator, Mapping
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class AgentWorkspaceError(RuntimeError):
|
|
23
|
+
"""Base error for fail-closed AgentWorkspace operations."""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class TargetDriftError(AgentWorkspaceError):
|
|
27
|
+
"""Raised when the snapshotted project changes during a run."""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class OriginWriteViolationError(AgentWorkspaceError):
|
|
31
|
+
"""Raised when any entry in the origin project differs from its baseline."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class WorkspaceOwnershipError(AgentWorkspaceError):
|
|
35
|
+
"""Raised when cleanup cannot prove that a path belongs to this instance."""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class AgentWorkspace:
|
|
39
|
+
"""Create and manage isolated project copies for one agent run.
|
|
40
|
+
|
|
41
|
+
``engine_private_dir`` must already exist and be private to the engine.
|
|
42
|
+
The instance creates ``runs/<run_uuid>/workspace`` with mode ``0700`` and
|
|
43
|
+
stores the selected project snapshot, a baseline execution copy, and patch
|
|
44
|
+
attempts below it. Patch attempts always derive from the snapshot, never
|
|
45
|
+
from another mutable execution copy.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
EXCLUDED_DIRECTORIES = frozenset(
|
|
49
|
+
{
|
|
50
|
+
".git",
|
|
51
|
+
".bugx",
|
|
52
|
+
"__pycache__",
|
|
53
|
+
"node_modules",
|
|
54
|
+
".pytest_cache",
|
|
55
|
+
".mypy_cache",
|
|
56
|
+
".ruff_cache",
|
|
57
|
+
".tox",
|
|
58
|
+
".venv",
|
|
59
|
+
"venv",
|
|
60
|
+
".cache",
|
|
61
|
+
"build",
|
|
62
|
+
"dist",
|
|
63
|
+
"target",
|
|
64
|
+
"coverage",
|
|
65
|
+
"htmlcov",
|
|
66
|
+
"artifacts",
|
|
67
|
+
"bugx-artifacts",
|
|
68
|
+
".history",
|
|
69
|
+
"history",
|
|
70
|
+
"test-results",
|
|
71
|
+
"benchmark-results",
|
|
72
|
+
}
|
|
73
|
+
)
|
|
74
|
+
EXCLUDED_FILE_NAMES = frozenset(
|
|
75
|
+
{
|
|
76
|
+
".DS_Store",
|
|
77
|
+
"Thumbs.db",
|
|
78
|
+
".coverage",
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
EXCLUDED_FILE_PATTERNS = (
|
|
82
|
+
"*.pyc",
|
|
83
|
+
"*.pyo",
|
|
84
|
+
"*.log",
|
|
85
|
+
"*.log.*",
|
|
86
|
+
"*.swp",
|
|
87
|
+
"*.swo",
|
|
88
|
+
)
|
|
89
|
+
OWNER_MARKER = ".agent-workspace-owner.json"
|
|
90
|
+
CHUNK_SIZE = 1024 * 1024
|
|
91
|
+
|
|
92
|
+
def __init__(
|
|
93
|
+
self,
|
|
94
|
+
origin_root: Path | str,
|
|
95
|
+
engine_private_dir: Path | str,
|
|
96
|
+
run_uuid: str,
|
|
97
|
+
) -> None:
|
|
98
|
+
if not isinstance(run_uuid, str):
|
|
99
|
+
raise TypeError("run_uuid must be a canonical UUID string")
|
|
100
|
+
try:
|
|
101
|
+
normalized_run_uuid = str(uuid.UUID(run_uuid))
|
|
102
|
+
except (ValueError, AttributeError) as exc:
|
|
103
|
+
raise ValueError("run_uuid must be a canonical UUID string") from exc
|
|
104
|
+
if run_uuid.lower() != normalized_run_uuid:
|
|
105
|
+
raise ValueError("run_uuid must be a canonical UUID string")
|
|
106
|
+
|
|
107
|
+
origin_input = Path(origin_root).expanduser()
|
|
108
|
+
if origin_input.is_symlink() or not origin_input.is_dir():
|
|
109
|
+
raise ValueError("origin_root must be an existing, non-symlink directory")
|
|
110
|
+
engine_input = Path(engine_private_dir).expanduser()
|
|
111
|
+
if engine_input.is_symlink() or not engine_input.is_dir():
|
|
112
|
+
raise ValueError("engine_private_dir must be an existing, non-symlink directory")
|
|
113
|
+
|
|
114
|
+
self.origin_root = origin_input.resolve(strict=True)
|
|
115
|
+
self.engine_private_dir = engine_input.resolve(strict=True)
|
|
116
|
+
self._engine_private_identity = self._identity(self.engine_private_dir)
|
|
117
|
+
self.run_uuid = normalized_run_uuid
|
|
118
|
+
self.runs_root = self.engine_private_dir / "runs"
|
|
119
|
+
self.run_root = self.runs_root / self.run_uuid
|
|
120
|
+
self.workspace_root = self.run_root / "workspace"
|
|
121
|
+
self.snapshot_root = self.workspace_root / "snapshot"
|
|
122
|
+
self.baseline_root = self.workspace_root / "baseline"
|
|
123
|
+
self.patched_root = self.workspace_root / "patched"
|
|
124
|
+
|
|
125
|
+
if self._paths_overlap(self.origin_root, self.workspace_root):
|
|
126
|
+
raise ValueError("workspace_root and origin_root must be separate trees")
|
|
127
|
+
if self.runs_root.is_symlink() or (self.runs_root.exists() and not self.runs_root.is_dir()):
|
|
128
|
+
raise WorkspaceOwnershipError("engine runs path must be a real directory")
|
|
129
|
+
|
|
130
|
+
self._owner_token = uuid.uuid4().hex
|
|
131
|
+
self._workspace_identity: tuple[int, int] | None = None
|
|
132
|
+
self._run_root_identity: tuple[int, int] | None = None
|
|
133
|
+
self._runs_root_identity: tuple[int, int] | None = None
|
|
134
|
+
self._baseline_identity: tuple[int, int] | None = None
|
|
135
|
+
self._patch_identities: dict[str, tuple[int, int]] = {}
|
|
136
|
+
self._closed = False
|
|
137
|
+
self._aborted = False
|
|
138
|
+
self._origin_violation_detail: str | None = None
|
|
139
|
+
self._run_root_created = False
|
|
140
|
+
self.snapshot_manifest: Mapping[str, str] = MappingProxyType({})
|
|
141
|
+
self._snapshot_integrity_manifest: dict[str, str] = {}
|
|
142
|
+
self._origin_integrity_manifest: dict[str, str] = {}
|
|
143
|
+
|
|
144
|
+
if not self.runs_root.exists():
|
|
145
|
+
self.runs_root.mkdir(mode=0o700)
|
|
146
|
+
self._runs_root_identity = self._identity(self.runs_root)
|
|
147
|
+
try:
|
|
148
|
+
self.run_root.mkdir(mode=0o700)
|
|
149
|
+
self._run_root_created = True
|
|
150
|
+
self._run_root_identity = self._identity(self.run_root)
|
|
151
|
+
self.workspace_root.mkdir(mode=0o700)
|
|
152
|
+
os.chmod(self.workspace_root, 0o700)
|
|
153
|
+
self._workspace_identity = self._identity(self.workspace_root)
|
|
154
|
+
self._write_owner_marker()
|
|
155
|
+
|
|
156
|
+
initial_origin = self._scan_origin_integrity()
|
|
157
|
+
copied_manifest = self._copy_origin_to_snapshot()
|
|
158
|
+
current_source_tree = self._scan_project_integrity(self.origin_root, project_only=True)
|
|
159
|
+
snapshot_tree = self._scan_project_integrity(self.snapshot_root, project_only=False)
|
|
160
|
+
current_source_files = {
|
|
161
|
+
key.removeprefix("file:"): digest
|
|
162
|
+
for key, digest in current_source_tree.items()
|
|
163
|
+
if key.startswith("file:")
|
|
164
|
+
}
|
|
165
|
+
if copied_manifest != current_source_files or current_source_tree != snapshot_tree:
|
|
166
|
+
raise TargetDriftError(
|
|
167
|
+
self._format_manifest_diff(
|
|
168
|
+
current_source_tree, snapshot_tree, "project changed while snapshotting"
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
current_origin = self._scan_origin_integrity()
|
|
172
|
+
if initial_origin != current_origin:
|
|
173
|
+
raise OriginWriteViolationError(
|
|
174
|
+
self._format_manifest_diff(
|
|
175
|
+
initial_origin, current_origin, "origin changed while snapshotting"
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
self._origin_integrity_manifest = initial_origin
|
|
179
|
+
self._snapshot_integrity_manifest = snapshot_tree
|
|
180
|
+
self.snapshot_manifest = MappingProxyType(dict(copied_manifest))
|
|
181
|
+
self._protect_snapshot()
|
|
182
|
+
self.verify_before_start()
|
|
183
|
+
self.create_baseline()
|
|
184
|
+
except BaseException:
|
|
185
|
+
self._cleanup_after_initialization_failure()
|
|
186
|
+
raise
|
|
187
|
+
|
|
188
|
+
@staticmethod
|
|
189
|
+
def _paths_overlap(first: Path, second: Path) -> bool:
|
|
190
|
+
return first == second or first.is_relative_to(second) or second.is_relative_to(first)
|
|
191
|
+
|
|
192
|
+
@staticmethod
|
|
193
|
+
def _identity(path: Path) -> tuple[int, int]:
|
|
194
|
+
info = path.lstat()
|
|
195
|
+
if not stat.S_ISDIR(info.st_mode):
|
|
196
|
+
raise WorkspaceOwnershipError(f"expected a real directory: {path}")
|
|
197
|
+
return info.st_dev, info.st_ino
|
|
198
|
+
|
|
199
|
+
@staticmethod
|
|
200
|
+
def _file_signature(info: os.stat_result) -> tuple[int, int, int, int, int]:
|
|
201
|
+
return (
|
|
202
|
+
info.st_dev,
|
|
203
|
+
info.st_ino,
|
|
204
|
+
info.st_size,
|
|
205
|
+
info.st_mtime_ns,
|
|
206
|
+
info.st_ctime_ns,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
@staticmethod
|
|
210
|
+
def _is_within(path: Path, parent: Path) -> bool:
|
|
211
|
+
try:
|
|
212
|
+
path.relative_to(parent)
|
|
213
|
+
return True
|
|
214
|
+
except ValueError:
|
|
215
|
+
return False
|
|
216
|
+
|
|
217
|
+
def _write_owner_marker(self) -> None:
|
|
218
|
+
marker = self.workspace_root / self.OWNER_MARKER
|
|
219
|
+
payload = json.dumps(
|
|
220
|
+
{"run_uuid": self.run_uuid, "owner_token": self._owner_token},
|
|
221
|
+
sort_keys=True,
|
|
222
|
+
).encode("utf-8")
|
|
223
|
+
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
|
|
224
|
+
descriptor = os.open(marker, flags, 0o600)
|
|
225
|
+
try:
|
|
226
|
+
with os.fdopen(descriptor, "wb") as marker_file:
|
|
227
|
+
marker_file.write(payload)
|
|
228
|
+
marker_file.flush()
|
|
229
|
+
os.fsync(marker_file.fileno())
|
|
230
|
+
except BaseException:
|
|
231
|
+
try:
|
|
232
|
+
os.close(descriptor)
|
|
233
|
+
except OSError:
|
|
234
|
+
pass
|
|
235
|
+
raise
|
|
236
|
+
|
|
237
|
+
@classmethod
|
|
238
|
+
def _is_excluded(cls, relative_path: str, is_directory: bool) -> bool:
|
|
239
|
+
parts = PurePosixPath(relative_path).parts
|
|
240
|
+
if any(part in cls.EXCLUDED_DIRECTORIES for part in parts):
|
|
241
|
+
return True
|
|
242
|
+
name = parts[-1]
|
|
243
|
+
if is_directory:
|
|
244
|
+
return False
|
|
245
|
+
if name in cls.EXCLUDED_FILE_NAMES:
|
|
246
|
+
return True
|
|
247
|
+
return any(fnmatch.fnmatchcase(name, pattern) for pattern in cls.EXCLUDED_FILE_PATTERNS)
|
|
248
|
+
|
|
249
|
+
@classmethod
|
|
250
|
+
def _walk_tree(
|
|
251
|
+
cls, root: Path, *, project_only: bool
|
|
252
|
+
) -> Iterator[tuple[str, str, Path, os.stat_result]]:
|
|
253
|
+
root_info = root.lstat()
|
|
254
|
+
if not stat.S_ISDIR(root_info.st_mode):
|
|
255
|
+
raise ValueError(f"project root is missing or is a symbolic link: {root}")
|
|
256
|
+
pending = [root]
|
|
257
|
+
while pending:
|
|
258
|
+
current = pending.pop()
|
|
259
|
+
try:
|
|
260
|
+
with os.scandir(current) as iterator:
|
|
261
|
+
entries = sorted(iterator, key=lambda entry: entry.name)
|
|
262
|
+
except OSError as exc:
|
|
263
|
+
raise OSError(f"cannot inspect project directory {current}: {exc}") from exc
|
|
264
|
+
|
|
265
|
+
directories: list[Path] = []
|
|
266
|
+
for entry in entries:
|
|
267
|
+
path = Path(entry.path)
|
|
268
|
+
relative = path.relative_to(root).as_posix()
|
|
269
|
+
info = entry.stat(follow_symlinks=False)
|
|
270
|
+
kind = (
|
|
271
|
+
"symlink"
|
|
272
|
+
if stat.S_ISLNK(info.st_mode)
|
|
273
|
+
else "directory"
|
|
274
|
+
if stat.S_ISDIR(info.st_mode)
|
|
275
|
+
else "file"
|
|
276
|
+
if stat.S_ISREG(info.st_mode)
|
|
277
|
+
else "special"
|
|
278
|
+
)
|
|
279
|
+
if project_only and cls._is_excluded(relative, kind == "directory"):
|
|
280
|
+
continue
|
|
281
|
+
yield relative, kind, path, info
|
|
282
|
+
if kind == "directory":
|
|
283
|
+
directories.append(path)
|
|
284
|
+
elif kind == "special":
|
|
285
|
+
raise ValueError(f"unsupported special project entry: {relative}")
|
|
286
|
+
pending.extend(reversed(directories))
|
|
287
|
+
|
|
288
|
+
@classmethod
|
|
289
|
+
def _sha256_file(cls, path: Path) -> str:
|
|
290
|
+
digest = hashlib.sha256()
|
|
291
|
+
descriptor = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0))
|
|
292
|
+
try:
|
|
293
|
+
before = os.fstat(descriptor)
|
|
294
|
+
if not stat.S_ISREG(before.st_mode):
|
|
295
|
+
raise ValueError(f"expected a regular file: {path}")
|
|
296
|
+
with os.fdopen(descriptor, "rb") as source:
|
|
297
|
+
descriptor = -1
|
|
298
|
+
while True:
|
|
299
|
+
block = source.read(cls.CHUNK_SIZE)
|
|
300
|
+
if not block:
|
|
301
|
+
break
|
|
302
|
+
digest.update(block)
|
|
303
|
+
after = os.fstat(source.fileno())
|
|
304
|
+
if cls._file_signature(before) != cls._file_signature(after):
|
|
305
|
+
raise OSError(f"file changed while hashing: {path}")
|
|
306
|
+
except BaseException:
|
|
307
|
+
if descriptor >= 0:
|
|
308
|
+
os.close(descriptor)
|
|
309
|
+
raise
|
|
310
|
+
return digest.hexdigest()
|
|
311
|
+
|
|
312
|
+
@classmethod
|
|
313
|
+
def _scan_project_integrity(cls, root: Path, *, project_only: bool) -> dict[str, str]:
|
|
314
|
+
manifest: dict[str, str] = {}
|
|
315
|
+
for relative, kind, path, _info in cls._walk_tree(root, project_only=project_only):
|
|
316
|
+
if kind == "symlink":
|
|
317
|
+
raise ValueError(f"source snapshot does not accept symbolic links: {relative}")
|
|
318
|
+
if kind == "directory":
|
|
319
|
+
manifest[f"directory:{relative}"] = "directory"
|
|
320
|
+
elif kind == "file":
|
|
321
|
+
manifest[f"file:{relative}"] = cls._sha256_file(path)
|
|
322
|
+
return manifest
|
|
323
|
+
|
|
324
|
+
def _copy_origin_to_snapshot(self) -> dict[str, str]:
|
|
325
|
+
self.snapshot_root.mkdir(mode=0o700)
|
|
326
|
+
manifest: dict[str, str] = {}
|
|
327
|
+
for relative, kind, source_path, source_info in self._walk_tree(
|
|
328
|
+
self.origin_root, project_only=True
|
|
329
|
+
):
|
|
330
|
+
destination = self.snapshot_root.joinpath(*PurePosixPath(relative).parts)
|
|
331
|
+
if kind == "symlink":
|
|
332
|
+
raise ValueError(f"source snapshot does not accept symbolic links: {relative}")
|
|
333
|
+
if kind == "directory":
|
|
334
|
+
destination.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
335
|
+
continue
|
|
336
|
+
destination.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
337
|
+
|
|
338
|
+
open_flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
|
|
339
|
+
source_fd = os.open(source_path, open_flags)
|
|
340
|
+
destination_fd: int | None = None
|
|
341
|
+
digest = hashlib.sha256()
|
|
342
|
+
try:
|
|
343
|
+
opened_info = os.fstat(source_fd)
|
|
344
|
+
if not stat.S_ISREG(opened_info.st_mode):
|
|
345
|
+
raise ValueError(f"source changed to a non-file during snapshot: {relative}")
|
|
346
|
+
destination_fd = os.open(
|
|
347
|
+
destination,
|
|
348
|
+
os.O_WRONLY | os.O_CREAT | os.O_EXCL,
|
|
349
|
+
0o600,
|
|
350
|
+
)
|
|
351
|
+
with (
|
|
352
|
+
os.fdopen(source_fd, "rb") as source_file,
|
|
353
|
+
os.fdopen(destination_fd, "wb") as destination_file,
|
|
354
|
+
):
|
|
355
|
+
source_fd = -1
|
|
356
|
+
destination_fd = None
|
|
357
|
+
while True:
|
|
358
|
+
block = source_file.read(self.CHUNK_SIZE)
|
|
359
|
+
if not block:
|
|
360
|
+
break
|
|
361
|
+
digest.update(block)
|
|
362
|
+
destination_file.write(block)
|
|
363
|
+
destination_file.flush()
|
|
364
|
+
os.fsync(destination_file.fileno())
|
|
365
|
+
after_info = os.fstat(source_file.fileno())
|
|
366
|
+
if self._file_signature(opened_info) != self._file_signature(after_info):
|
|
367
|
+
raise TargetDriftError(
|
|
368
|
+
f"source changed while copying snapshot file: {relative}"
|
|
369
|
+
)
|
|
370
|
+
snapshot_mode = (stat.S_IMODE(source_info.st_mode) | 0o400) & 0o777
|
|
371
|
+
os.chmod(destination, snapshot_mode)
|
|
372
|
+
manifest[relative] = digest.hexdigest()
|
|
373
|
+
finally:
|
|
374
|
+
if source_fd >= 0:
|
|
375
|
+
os.close(source_fd)
|
|
376
|
+
if destination_fd is not None:
|
|
377
|
+
os.close(destination_fd)
|
|
378
|
+
return manifest
|
|
379
|
+
|
|
380
|
+
def _scan_origin_integrity(self) -> dict[str, str]:
|
|
381
|
+
manifest: dict[str, str] = {}
|
|
382
|
+
for relative, kind, path, _info in self._walk_tree(self.origin_root, project_only=False):
|
|
383
|
+
if kind == "directory":
|
|
384
|
+
manifest[f"directory:{relative}"] = "directory"
|
|
385
|
+
elif kind == "file":
|
|
386
|
+
manifest[f"file:{relative}"] = self._sha256_file(path)
|
|
387
|
+
elif kind == "symlink":
|
|
388
|
+
target = os.readlink(path)
|
|
389
|
+
manifest[f"symlink:{relative}"] = hashlib.sha256(os.fsencode(target)).hexdigest()
|
|
390
|
+
return manifest
|
|
391
|
+
|
|
392
|
+
def _protect_snapshot(self) -> None:
|
|
393
|
+
"""Remove write bits from the canonical tree used to seed attempts."""
|
|
394
|
+
for current, directory_names, file_names in os.walk(
|
|
395
|
+
self.snapshot_root, topdown=True, followlinks=False
|
|
396
|
+
):
|
|
397
|
+
current_path = Path(current)
|
|
398
|
+
for directory_name in directory_names:
|
|
399
|
+
child = current_path / directory_name
|
|
400
|
+
if child.is_symlink():
|
|
401
|
+
raise WorkspaceOwnershipError(f"snapshot contains a symbolic link: {child}")
|
|
402
|
+
os.chmod(child, 0o500)
|
|
403
|
+
for file_name in file_names:
|
|
404
|
+
child = current_path / file_name
|
|
405
|
+
if child.is_symlink() or not child.is_file():
|
|
406
|
+
raise WorkspaceOwnershipError(f"snapshot contains an unsafe file: {child}")
|
|
407
|
+
os.chmod(child, stat.S_IMODE(child.stat().st_mode) & ~0o222)
|
|
408
|
+
os.chmod(self.snapshot_root, 0o500)
|
|
409
|
+
|
|
410
|
+
def _assert_snapshot_unchanged(self) -> None:
|
|
411
|
+
try:
|
|
412
|
+
current = self._scan_project_integrity(self.snapshot_root, project_only=False)
|
|
413
|
+
except (OSError, ValueError) as exc:
|
|
414
|
+
self._aborted = True
|
|
415
|
+
raise WorkspaceOwnershipError(f"cannot verify canonical snapshot: {exc}") from exc
|
|
416
|
+
if current != self._snapshot_integrity_manifest:
|
|
417
|
+
self._aborted = True
|
|
418
|
+
raise WorkspaceOwnershipError(
|
|
419
|
+
self._format_manifest_diff(
|
|
420
|
+
self._snapshot_integrity_manifest,
|
|
421
|
+
current,
|
|
422
|
+
"canonical snapshot was modified",
|
|
423
|
+
)
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
@staticmethod
|
|
427
|
+
def _format_manifest_diff(
|
|
428
|
+
expected: Mapping[str, str],
|
|
429
|
+
actual: Mapping[str, str],
|
|
430
|
+
heading: str,
|
|
431
|
+
) -> str:
|
|
432
|
+
expected_paths = set(expected)
|
|
433
|
+
actual_paths = set(actual)
|
|
434
|
+
added = sorted(actual_paths - expected_paths)
|
|
435
|
+
removed = sorted(expected_paths - actual_paths)
|
|
436
|
+
modified = sorted(
|
|
437
|
+
key for key in expected_paths & actual_paths if expected[key] != actual[key]
|
|
438
|
+
)
|
|
439
|
+
details: list[str] = []
|
|
440
|
+
for label, paths in (("added", added), ("removed", removed), ("modified", modified)):
|
|
441
|
+
if paths:
|
|
442
|
+
sample = ", ".join(paths[:8])
|
|
443
|
+
suffix = " ..." if len(paths) > 8 else ""
|
|
444
|
+
details.append(f"{label}={sample}{suffix}")
|
|
445
|
+
return heading if not details else f"{heading}: " + "; ".join(details)
|
|
446
|
+
|
|
447
|
+
def _ensure_active(self) -> None:
|
|
448
|
+
if self._closed:
|
|
449
|
+
raise AgentWorkspaceError("workspace is already closed")
|
|
450
|
+
if self._aborted:
|
|
451
|
+
raise TargetDriftError("workspace is aborted after an integrity violation")
|
|
452
|
+
|
|
453
|
+
def verify_target_unchanged(self, stage: str = "") -> None:
|
|
454
|
+
"""Raise ``TargetDriftError`` unless source-file contents match the snapshot."""
|
|
455
|
+
self._ensure_active()
|
|
456
|
+
try:
|
|
457
|
+
current = self._scan_project_integrity(self.origin_root, project_only=True)
|
|
458
|
+
except (OSError, ValueError) as exc:
|
|
459
|
+
self._aborted = True
|
|
460
|
+
raise TargetDriftError(
|
|
461
|
+
f"cannot verify target at {stage or 'checkpoint'}: {exc}"
|
|
462
|
+
) from exc
|
|
463
|
+
if current != self._snapshot_integrity_manifest:
|
|
464
|
+
self._aborted = True
|
|
465
|
+
heading = f"target drift at {stage}" if stage else "target drift"
|
|
466
|
+
raise TargetDriftError(
|
|
467
|
+
self._format_manifest_diff(self._snapshot_integrity_manifest, current, heading)
|
|
468
|
+
)
|
|
469
|
+
|
|
470
|
+
def verify_before_start(self) -> None:
|
|
471
|
+
"""Check source identity immediately before starting a run."""
|
|
472
|
+
self.verify_target_unchanged("before_start")
|
|
473
|
+
|
|
474
|
+
def verify_after_sampling(self) -> None:
|
|
475
|
+
"""Check that sampling did not race with an origin-project change."""
|
|
476
|
+
self.verify_target_unchanged("after_sampling")
|
|
477
|
+
|
|
478
|
+
def verify_before_stage(self, stage: str) -> None:
|
|
479
|
+
"""Check target drift before executing a named stage."""
|
|
480
|
+
if not isinstance(stage, str) or not stage.strip():
|
|
481
|
+
raise ValueError("stage must be a non-empty string")
|
|
482
|
+
self.verify_target_unchanged(stage.strip())
|
|
483
|
+
|
|
484
|
+
def verify_origin_untouched(self) -> None:
|
|
485
|
+
"""Raise ``OriginWriteViolationError`` if any origin entry changed."""
|
|
486
|
+
if self._closed:
|
|
487
|
+
raise AgentWorkspaceError("workspace is already closed")
|
|
488
|
+
if self._origin_violation_detail is not None:
|
|
489
|
+
raise OriginWriteViolationError(self._origin_violation_detail)
|
|
490
|
+
try:
|
|
491
|
+
current = self._scan_origin_integrity()
|
|
492
|
+
except (OSError, ValueError) as exc:
|
|
493
|
+
self._aborted = True
|
|
494
|
+
self._origin_violation_detail = f"cannot prove origin project untouched: {exc}"
|
|
495
|
+
raise OriginWriteViolationError(self._origin_violation_detail) from exc
|
|
496
|
+
if current != self._origin_integrity_manifest:
|
|
497
|
+
self._aborted = True
|
|
498
|
+
self._origin_violation_detail = self._format_manifest_diff(
|
|
499
|
+
self._origin_integrity_manifest,
|
|
500
|
+
current,
|
|
501
|
+
"origin project write violation",
|
|
502
|
+
)
|
|
503
|
+
raise OriginWriteViolationError(self._origin_violation_detail)
|
|
504
|
+
|
|
505
|
+
def create_baseline(self) -> Path:
|
|
506
|
+
"""Return the independently writable baseline execution copy."""
|
|
507
|
+
self._ensure_active()
|
|
508
|
+
self.verify_before_stage("baseline-copy")
|
|
509
|
+
if self._baseline_identity is not None:
|
|
510
|
+
self._assert_owned_child(self.baseline_root, self._baseline_identity)
|
|
511
|
+
return self.baseline_root
|
|
512
|
+
self.baseline_root.mkdir(mode=0o700)
|
|
513
|
+
self._baseline_identity = self._identity(self.baseline_root)
|
|
514
|
+
try:
|
|
515
|
+
self._copy_snapshot_to_execution_tree(self.baseline_root)
|
|
516
|
+
self.verify_target_unchanged("baseline-copy-complete")
|
|
517
|
+
return self.baseline_root
|
|
518
|
+
except BaseException:
|
|
519
|
+
self._remove_owned_child(self.baseline_root, self._baseline_identity)
|
|
520
|
+
self._baseline_identity = None
|
|
521
|
+
raise
|
|
522
|
+
|
|
523
|
+
def create_patched_copy(self, patch_digest: str) -> Path:
|
|
524
|
+
"""Create a fresh writable ``patched/<sha256>`` copy for one patch.
|
|
525
|
+
|
|
526
|
+
Reusing a digest in this instance removes only that instance's prior
|
|
527
|
+
attempt and recreates it from the immutable-at-source snapshot. A
|
|
528
|
+
pre-existing path that this instance did not create is never removed.
|
|
529
|
+
"""
|
|
530
|
+
self._ensure_active()
|
|
531
|
+
if not isinstance(patch_digest, str) or len(patch_digest) != 64:
|
|
532
|
+
raise ValueError("patch_digest must be a 64-character SHA-256 hex digest")
|
|
533
|
+
if any(character not in "0123456789abcdef" for character in patch_digest.lower()):
|
|
534
|
+
raise ValueError("patch_digest must be a 64-character SHA-256 hex digest")
|
|
535
|
+
digest = patch_digest.lower()
|
|
536
|
+
self.verify_before_stage(f"patch:{digest}")
|
|
537
|
+
if not self.patched_root.exists():
|
|
538
|
+
self.patched_root.mkdir(mode=0o700)
|
|
539
|
+
elif self.patched_root.is_symlink() or not self.patched_root.is_dir():
|
|
540
|
+
raise WorkspaceOwnershipError("patched path must be a real directory")
|
|
541
|
+
destination = self.patched_root / digest
|
|
542
|
+
|
|
543
|
+
previous_identity = self._patch_identities.get(digest)
|
|
544
|
+
if destination.exists() or destination.is_symlink():
|
|
545
|
+
if previous_identity is None:
|
|
546
|
+
raise WorkspaceOwnershipError(
|
|
547
|
+
f"refusing to replace a patch directory not owned by this instance: {digest}"
|
|
548
|
+
)
|
|
549
|
+
self._assert_owned_child(destination, previous_identity)
|
|
550
|
+
self._remove_owned_child(destination, previous_identity)
|
|
551
|
+
self._patch_identities.pop(digest, None)
|
|
552
|
+
|
|
553
|
+
destination.mkdir(mode=0o700)
|
|
554
|
+
identity = self._identity(destination)
|
|
555
|
+
self._patch_identities[digest] = identity
|
|
556
|
+
try:
|
|
557
|
+
self._copy_snapshot_to_execution_tree(destination)
|
|
558
|
+
self.verify_target_unchanged(f"patch-copy-complete:{digest}")
|
|
559
|
+
return destination
|
|
560
|
+
except BaseException:
|
|
561
|
+
self._remove_owned_child(destination, identity)
|
|
562
|
+
self._patch_identities.pop(digest, None)
|
|
563
|
+
raise
|
|
564
|
+
|
|
565
|
+
def _copy_snapshot_to_execution_tree(self, destination: Path) -> None:
|
|
566
|
+
self._assert_workspace_owned()
|
|
567
|
+
self._assert_snapshot_unchanged()
|
|
568
|
+
shutil.copytree(
|
|
569
|
+
self.snapshot_root, destination, dirs_exist_ok=True, copy_function=shutil.copy2
|
|
570
|
+
)
|
|
571
|
+
copied_integrity = self._scan_project_integrity(destination, project_only=False)
|
|
572
|
+
if copied_integrity != self._snapshot_integrity_manifest:
|
|
573
|
+
raise WorkspaceOwnershipError("execution copy does not match the canonical snapshot")
|
|
574
|
+
for current, directory_names, file_names in os.walk(
|
|
575
|
+
destination, topdown=True, followlinks=False
|
|
576
|
+
):
|
|
577
|
+
current_path = Path(current)
|
|
578
|
+
for directory_name in directory_names:
|
|
579
|
+
child = current_path / directory_name
|
|
580
|
+
if child.is_symlink() or not child.is_dir():
|
|
581
|
+
raise WorkspaceOwnershipError(
|
|
582
|
+
f"snapshot copy contains an unsafe directory: {child}"
|
|
583
|
+
)
|
|
584
|
+
os.chmod(child, (stat.S_IMODE(child.stat().st_mode) | 0o700) & 0o777)
|
|
585
|
+
for file_name in file_names:
|
|
586
|
+
child = current_path / file_name
|
|
587
|
+
if child.is_symlink() or not child.is_file():
|
|
588
|
+
raise WorkspaceOwnershipError(f"snapshot copy contains an unsafe file: {child}")
|
|
589
|
+
os.chmod(child, (stat.S_IMODE(child.stat().st_mode) | 0o200) & 0o777)
|
|
590
|
+
os.chmod(destination, 0o700)
|
|
591
|
+
self._assert_snapshot_unchanged()
|
|
592
|
+
|
|
593
|
+
def _assert_workspace_owned(self) -> None:
|
|
594
|
+
if (
|
|
595
|
+
self.engine_private_dir.is_symlink()
|
|
596
|
+
or self._identity(self.engine_private_dir) != self._engine_private_identity
|
|
597
|
+
):
|
|
598
|
+
raise WorkspaceOwnershipError("engine private directory identity changed")
|
|
599
|
+
if (
|
|
600
|
+
self.runs_root.is_symlink()
|
|
601
|
+
or not self.runs_root.is_dir()
|
|
602
|
+
or self._runs_root_identity is None
|
|
603
|
+
or self._identity(self.runs_root) != self._runs_root_identity
|
|
604
|
+
):
|
|
605
|
+
raise WorkspaceOwnershipError("engine runs directory is missing or replaced")
|
|
606
|
+
if (
|
|
607
|
+
self._run_root_identity is None
|
|
608
|
+
or self._identity(self.run_root) != self._run_root_identity
|
|
609
|
+
):
|
|
610
|
+
raise WorkspaceOwnershipError("run directory identity changed")
|
|
611
|
+
if self.workspace_root.is_symlink() or not self.workspace_root.is_dir():
|
|
612
|
+
raise WorkspaceOwnershipError("workspace directory is missing or replaced")
|
|
613
|
+
workspace_info = self.workspace_root.lstat()
|
|
614
|
+
if (
|
|
615
|
+
self._workspace_identity is None
|
|
616
|
+
or (workspace_info.st_dev, workspace_info.st_ino) != self._workspace_identity
|
|
617
|
+
):
|
|
618
|
+
raise WorkspaceOwnershipError("workspace directory identity changed")
|
|
619
|
+
if stat.S_IMODE(workspace_info.st_mode) != 0o700:
|
|
620
|
+
os.chmod(self.workspace_root, 0o700)
|
|
621
|
+
marker_path = self.workspace_root / self.OWNER_MARKER
|
|
622
|
+
try:
|
|
623
|
+
marker_info = marker_path.lstat()
|
|
624
|
+
if not stat.S_ISREG(marker_info.st_mode):
|
|
625
|
+
raise WorkspaceOwnershipError("workspace ownership marker is not a regular file")
|
|
626
|
+
descriptor = os.open(
|
|
627
|
+
marker_path,
|
|
628
|
+
os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0),
|
|
629
|
+
)
|
|
630
|
+
with os.fdopen(descriptor, "rb") as marker_file:
|
|
631
|
+
marker = json.loads(marker_file.read(4096).decode("utf-8"))
|
|
632
|
+
except (OSError, ValueError, TypeError) as exc:
|
|
633
|
+
raise WorkspaceOwnershipError(
|
|
634
|
+
"workspace ownership marker is missing or invalid"
|
|
635
|
+
) from exc
|
|
636
|
+
if marker != {"owner_token": self._owner_token, "run_uuid": self.run_uuid}:
|
|
637
|
+
raise WorkspaceOwnershipError("workspace ownership marker does not match this instance")
|
|
638
|
+
|
|
639
|
+
def _assert_owned_child(self, path: Path, identity: tuple[int, int]) -> None:
|
|
640
|
+
if not self._is_within(path, self.workspace_root) or path == self.workspace_root:
|
|
641
|
+
raise WorkspaceOwnershipError(f"refusing to operate outside workspace: {path}")
|
|
642
|
+
if path.is_symlink() or not path.is_dir() or self._identity(path) != identity:
|
|
643
|
+
raise WorkspaceOwnershipError(f"owned workspace directory was replaced: {path}")
|
|
644
|
+
self._assert_workspace_owned()
|
|
645
|
+
|
|
646
|
+
def _remove_owned_child(self, path: Path, identity: tuple[int, int]) -> None:
|
|
647
|
+
self._assert_owned_child(path, identity)
|
|
648
|
+
self._assert_safe_removal_tree(path)
|
|
649
|
+
self._make_tree_removable(path)
|
|
650
|
+
shutil.rmtree(path)
|
|
651
|
+
|
|
652
|
+
@staticmethod
|
|
653
|
+
def _assert_safe_removal_tree(root: Path) -> None:
|
|
654
|
+
"""Refuse recursive cleanup across mounts or externally linked files."""
|
|
655
|
+
root_info = root.lstat()
|
|
656
|
+
root_device = root_info.st_dev
|
|
657
|
+
for current, directory_names, file_names in os.walk(root, topdown=True, followlinks=False):
|
|
658
|
+
current_path = Path(current)
|
|
659
|
+
for directory_name in directory_names:
|
|
660
|
+
child = current_path / directory_name
|
|
661
|
+
info = child.lstat()
|
|
662
|
+
if stat.S_ISLNK(info.st_mode):
|
|
663
|
+
continue
|
|
664
|
+
if (
|
|
665
|
+
not stat.S_ISDIR(info.st_mode)
|
|
666
|
+
or info.st_dev != root_device
|
|
667
|
+
or os.path.ismount(child)
|
|
668
|
+
):
|
|
669
|
+
raise WorkspaceOwnershipError(
|
|
670
|
+
f"refusing cleanup across a mounted or replaced directory: {child}"
|
|
671
|
+
)
|
|
672
|
+
for file_name in file_names:
|
|
673
|
+
child = current_path / file_name
|
|
674
|
+
info = child.lstat()
|
|
675
|
+
if stat.S_ISLNK(info.st_mode):
|
|
676
|
+
continue
|
|
677
|
+
if info.st_dev != root_device or info.st_nlink > 1:
|
|
678
|
+
raise WorkspaceOwnershipError(
|
|
679
|
+
f"refusing cleanup of an externally linked file: {child}"
|
|
680
|
+
)
|
|
681
|
+
|
|
682
|
+
@staticmethod
|
|
683
|
+
def _make_tree_removable(root: Path) -> None:
|
|
684
|
+
"""Restore owner traversal/write bits without following symlinks."""
|
|
685
|
+
for current, directory_names, _file_names in os.walk(root, topdown=True, followlinks=False):
|
|
686
|
+
current_path = Path(current)
|
|
687
|
+
for directory_name in directory_names:
|
|
688
|
+
child = current_path / directory_name
|
|
689
|
+
if child.is_symlink():
|
|
690
|
+
continue
|
|
691
|
+
os.chmod(child, (stat.S_IMODE(child.stat().st_mode) | 0o700) & 0o777)
|
|
692
|
+
if root.is_dir() and not root.is_symlink():
|
|
693
|
+
os.chmod(root, (stat.S_IMODE(root.stat().st_mode) | 0o700) & 0o777)
|
|
694
|
+
|
|
695
|
+
def _cleanup_after_initialization_failure(self) -> None:
|
|
696
|
+
if self._workspace_identity is not None:
|
|
697
|
+
try:
|
|
698
|
+
anchors_are_owned = (
|
|
699
|
+
self._identity(self.engine_private_dir) == self._engine_private_identity
|
|
700
|
+
and self._runs_root_identity is not None
|
|
701
|
+
and self._identity(self.runs_root) == self._runs_root_identity
|
|
702
|
+
and self._run_root_identity is not None
|
|
703
|
+
and self._identity(self.run_root) == self._run_root_identity
|
|
704
|
+
and self._identity(self.workspace_root) == self._workspace_identity
|
|
705
|
+
)
|
|
706
|
+
if anchors_are_owned:
|
|
707
|
+
self._assert_safe_removal_tree(self.workspace_root)
|
|
708
|
+
self._make_tree_removable(self.workspace_root)
|
|
709
|
+
shutil.rmtree(self.workspace_root)
|
|
710
|
+
except (OSError, WorkspaceOwnershipError):
|
|
711
|
+
pass
|
|
712
|
+
if self._run_root_created and self._run_root_identity is not None:
|
|
713
|
+
try:
|
|
714
|
+
if self._identity(self.run_root) == self._run_root_identity:
|
|
715
|
+
self.run_root.rmdir()
|
|
716
|
+
except (OSError, WorkspaceOwnershipError):
|
|
717
|
+
pass
|
|
718
|
+
|
|
719
|
+
def cleanup(self) -> None:
|
|
720
|
+
"""Remove only this instance's workspace and an empty owned run directory.
|
|
721
|
+
|
|
722
|
+
No process is signalled or terminated. If the origin changed, the
|
|
723
|
+
owned workspace is still safely removed and the violation is raised
|
|
724
|
+
after cleanup so callers cannot mistake the run for a clean one.
|
|
725
|
+
"""
|
|
726
|
+
if self._closed:
|
|
727
|
+
return
|
|
728
|
+
origin_violation: OriginWriteViolationError | None = None
|
|
729
|
+
try:
|
|
730
|
+
self.verify_origin_untouched()
|
|
731
|
+
except OriginWriteViolationError as exc:
|
|
732
|
+
origin_violation = exc
|
|
733
|
+
|
|
734
|
+
self._assert_workspace_owned()
|
|
735
|
+
self._assert_safe_removal_tree(self.workspace_root)
|
|
736
|
+
self._make_tree_removable(self.workspace_root)
|
|
737
|
+
shutil.rmtree(self.workspace_root)
|
|
738
|
+
self._closed = True
|
|
739
|
+
if self._run_root_created and self._run_root_identity is not None:
|
|
740
|
+
try:
|
|
741
|
+
if self._identity(self.run_root) == self._run_root_identity:
|
|
742
|
+
self.run_root.rmdir()
|
|
743
|
+
except OSError:
|
|
744
|
+
# A non-owned sibling file prevents removal; leave it in place.
|
|
745
|
+
pass
|
|
746
|
+
if origin_violation is not None:
|
|
747
|
+
raise origin_violation
|
|
748
|
+
|
|
749
|
+
def __enter__(self) -> AgentWorkspace:
|
|
750
|
+
self._ensure_active()
|
|
751
|
+
return self
|
|
752
|
+
|
|
753
|
+
def __exit__(self, exc_type: object, exc: object, traceback: object) -> bool:
|
|
754
|
+
try:
|
|
755
|
+
self.cleanup()
|
|
756
|
+
except OriginWriteViolationError as violation:
|
|
757
|
+
if isinstance(exc, BaseException):
|
|
758
|
+
exc.add_note(str(violation))
|
|
759
|
+
else:
|
|
760
|
+
raise
|
|
761
|
+
return False
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
__all__ = [
|
|
765
|
+
"AgentWorkspace",
|
|
766
|
+
"AgentWorkspaceError",
|
|
767
|
+
"OriginWriteViolationError",
|
|
768
|
+
"TargetDriftError",
|
|
769
|
+
"WorkspaceOwnershipError",
|
|
770
|
+
]
|