foxclaw 0.1.0 → 0.1.3
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/README.md +54 -29
- package/bin/foxclaw.cjs +51 -0
- package/index.js +4 -10
- package/package.json +46 -38
- package/python/.dockerignore +35 -0
- package/python/.env.example +173 -0
- package/python/.gitattributes +46 -0
- package/python/.github/workflows/ci.yml +167 -0
- package/python/.github/workflows/release.yml +183 -0
- package/python/CHANGELOG.md +57 -0
- package/python/CHANGELOG_CN.md +57 -0
- package/python/Dockerfile +64 -0
- package/python/LICENSE +21 -0
- package/python/MANIFEST.in +14 -0
- package/python/README.md +201 -0
- package/python/README_CN.md +336 -0
- package/python/bench/bm25_audit.py +186 -0
- package/python/bench/bm25_regression.py +177 -0
- package/python/bench/bm25_stress.py +155 -0
- package/python/bench/cd_audit.py +215 -0
- package/python/bench/chaos.py +140 -0
- package/python/bench/concurrency.py +103 -0
- package/python/bench/e2e_real_models.py +94 -0
- package/python/bench/llm_retry_test.py +241 -0
- package/python/bench/load_test.py +193 -0
- package/python/bench/vector_ivf_upgrade.py +128 -0
- package/python/docs/CHANNELS.md +195 -0
- package/python/docs/GOVERNANCE.md +164 -0
- package/python/docs/RELEASE.md +110 -0
- package/python/docs/RUN.md +75 -0
- package/python/docs/RUN_CN.md +121 -0
- package/python/docs//351/205/215/347/275/256/346/214/207/345/215/227.md +152 -0
- package/python/evals/__init__.py +0 -0
- package/python/evals/governance/README.md +61 -0
- package/python/evals/governance/__init__.py +0 -0
- package/python/evals/governance/cases.py +115 -0
- package/python/evals/governance/compare_embeddings.py +94 -0
- package/python/evals/governance/convergence.svg +11 -0
- package/python/evals/governance/report.json +371 -0
- package/python/evals/governance/run_evals.py +368 -0
- package/python/examples/governance_middleware_demo.py +72 -0
- package/python/examples/langgraph_governance_demo.py +104 -0
- package/python/examples/sample_itinerary.csv +6 -0
- package/python/examples/sample_knowledge.csv +9 -0
- package/python/examples/sample_transactions.csv +14 -0
- package/python/foxclaw/__init__.py +408 -0
- package/python/foxclaw/agents/__init__.py +12 -0
- package/python/foxclaw/agents/agents.py +35 -0
- package/python/foxclaw/agents/base.py +794 -0
- package/python/foxclaw/agents/impl/__init__.py +1 -0
- package/python/foxclaw/agents/impl/_helpers.py +70 -0
- package/python/foxclaw/agents/impl/finance.py +231 -0
- package/python/foxclaw/agents/impl/office.py +350 -0
- package/python/foxclaw/agents/impl/supervisor.py +592 -0
- package/python/foxclaw/agents/mixins/_context.py +128 -0
- package/python/foxclaw/agents/mixins/augment.py +143 -0
- package/python/foxclaw/agents/mixins/llm.py +67 -0
- package/python/foxclaw/agents/mixins/memory.py +41 -0
- package/python/foxclaw/agents/mixins/preference.py +131 -0
- package/python/foxclaw/agents/mixins/prompt.py +71 -0
- package/python/foxclaw/agents/mixins/skillbox.py +58 -0
- package/python/foxclaw/agents/mixins/tool_exec.py +417 -0
- package/python/foxclaw/api/__init__.py +224 -0
- package/python/foxclaw/api/_state.py +43 -0
- package/python/foxclaw/api/bootstrap.py +96 -0
- package/python/foxclaw/api/middleware.py +294 -0
- package/python/foxclaw/api/routes/ahs.py +404 -0
- package/python/foxclaw/api/routes/attachments.py +116 -0
- package/python/foxclaw/api/routes/browser.py +35 -0
- package/python/foxclaw/api/routes/domain.py +306 -0
- package/python/foxclaw/api/routes/knowledge.py +145 -0
- package/python/foxclaw/api/routes/llm_admin.py +449 -0
- package/python/foxclaw/api/routes/memory.py +105 -0
- package/python/foxclaw/api/routes/orchestration.py +39 -0
- package/python/foxclaw/api/routes/recovery.py +44 -0
- package/python/foxclaw/api/routes/review.py +266 -0
- package/python/foxclaw/api/routes/search.py +33 -0
- package/python/foxclaw/api/routes/system.py +311 -0
- package/python/foxclaw/api/routes/task.py +263 -0
- package/python/foxclaw/api/routes/ws.py +64 -0
- package/python/foxclaw/api/spine.py +52 -0
- package/python/foxclaw/browser/__init__.py +13 -0
- package/python/foxclaw/browser/action/__init__.py +1 -0
- package/python/foxclaw/browser/action/action_executor.py +50 -0
- package/python/foxclaw/browser/action/action_types.py +44 -0
- package/python/foxclaw/browser/action/action_verifier.py +68 -0
- package/python/foxclaw/browser/action/url_policy.py +143 -0
- package/python/foxclaw/browser/adapter/__init__.py +1 -0
- package/python/foxclaw/browser/adapter/base_adapter.py +23 -0
- package/python/foxclaw/browser/adapter/web_adapter.py +46 -0
- package/python/foxclaw/browser/driver/__init__.py +1 -0
- package/python/foxclaw/browser/driver/base_driver.py +61 -0
- package/python/foxclaw/browser/driver/playwright_driver.py +170 -0
- package/python/foxclaw/browser/perception/__init__.py +1 -0
- package/python/foxclaw/browser/perception/dom_extractor.py +42 -0
- package/python/foxclaw/browser/perception/element_indexer.py +56 -0
- package/python/foxclaw/browser/perception/semantic_reducer.py +33 -0
- package/python/foxclaw/browser/perception/snapshot.py +29 -0
- package/python/foxclaw/browser/service.py +144 -0
- package/python/foxclaw/browser/session/__init__.py +1 -0
- package/python/foxclaw/browser/session/session_manager.py +57 -0
- package/python/foxclaw/capability/__init__.py +12 -0
- package/python/foxclaw/capability/embedding_service.py +65 -0
- package/python/foxclaw/channels/__init__.py +138 -0
- package/python/foxclaw/channels/discord.py +175 -0
- package/python/foxclaw/channels/dispatcher.py +179 -0
- package/python/foxclaw/channels/feishu.py +215 -0
- package/python/foxclaw/channels/openclaw_bridge.py +151 -0
- package/python/foxclaw/channels/signing.py +187 -0
- package/python/foxclaw/channels/telegram.py +120 -0
- package/python/foxclaw/channels/weixin.py +215 -0
- package/python/foxclaw/cli.py +183 -0
- package/python/foxclaw/cognitive/__init__.py +4 -0
- package/python/foxclaw/cognitive/reasoner.py +81 -0
- package/python/foxclaw/cognitive/state.py +141 -0
- package/python/foxclaw/core/__init__.py +9 -0
- package/python/foxclaw/core/audit_chain.py +131 -0
- package/python/foxclaw/core/audit_sink.py +100 -0
- package/python/foxclaw/core/compaction.py +265 -0
- package/python/foxclaw/core/config.py +65 -0
- package/python/foxclaw/core/confirm_nonce.py +151 -0
- package/python/foxclaw/core/dispatch.py +27 -0
- package/python/foxclaw/core/envfile.py +48 -0
- package/python/foxclaw/core/failure_policy.py +142 -0
- package/python/foxclaw/core/gateway.py +81 -0
- package/python/foxclaw/core/governance_audit.py +171 -0
- package/python/foxclaw/core/idempotency.py +282 -0
- package/python/foxclaw/core/injection_guard.py +202 -0
- package/python/foxclaw/core/memory_adapter.py +489 -0
- package/python/foxclaw/core/metrics.py +88 -0
- package/python/foxclaw/core/model_router.py +985 -0
- package/python/foxclaw/core/paths.py +103 -0
- package/python/foxclaw/core/recovery.py +349 -0
- package/python/foxclaw/core/scheduler.py +141 -0
- package/python/foxclaw/core/secrets.py +131 -0
- package/python/foxclaw/core/sessions.py +155 -0
- package/python/foxclaw/core/settings.py +414 -0
- package/python/foxclaw/core/shared_write.py +186 -0
- package/python/foxclaw/core/steering_queue.py +154 -0
- package/python/foxclaw/core/system_bus.py +84 -0
- package/python/foxclaw/core/thought_chain.py +316 -0
- package/python/foxclaw/core/token_meter.py +231 -0
- package/python/foxclaw/core/tools.py +164 -0
- package/python/foxclaw/core/tracing.py +375 -0
- package/python/foxclaw/core/truth_pipeline.py +110 -0
- package/python/foxclaw/core/update_check.py +149 -0
- package/python/foxclaw/core/vec_recall_adapter.py +477 -0
- package/python/foxclaw/core/verify.py +173 -0
- package/python/foxclaw/core/web_search.py +218 -0
- package/python/foxclaw/finance/__init__.py +15 -0
- package/python/foxclaw/finance/evidence.py +149 -0
- package/python/foxclaw/finance/fx.py +98 -0
- package/python/foxclaw/finance/graph.py +179 -0
- package/python/foxclaw/finance/models.py +146 -0
- package/python/foxclaw/finance/nlparse.py +515 -0
- package/python/foxclaw/finance/policy.py +301 -0
- package/python/foxclaw/finance/reasoner.py +121 -0
- package/python/foxclaw/finance/reasoning.py +319 -0
- package/python/foxclaw/finance/reconcile.py +175 -0
- package/python/foxclaw/finance/runtime.py +500 -0
- package/python/foxclaw/governance/__init__.py +24 -0
- package/python/foxclaw/governance/middleware.py +176 -0
- package/python/foxclaw/harness/__init__.py +17 -0
- package/python/foxclaw/harness/_base.py +66 -0
- package/python/foxclaw/harness/_policy.py +340 -0
- package/python/foxclaw/harness/drift.py +77 -0
- package/python/foxclaw/harness/event_bus.py +28 -0
- package/python/foxclaw/harness/harness.py +358 -0
- package/python/foxclaw/harness/loop_detection.py +145 -0
- package/python/foxclaw/itinerary/__init__.py +10 -0
- package/python/foxclaw/itinerary/amap_client.py +114 -0
- package/python/foxclaw/itinerary/models.py +80 -0
- package/python/foxclaw/itinerary/reasoner.py +96 -0
- package/python/foxclaw/itinerary/reasoning.py +168 -0
- package/python/foxclaw/itinerary/runtime.py +144 -0
- package/python/foxclaw/kernel/__init__.py +13 -0
- package/python/foxclaw/kernel/capabilities.py +72 -0
- package/python/foxclaw/kernel/policy_gate.py +108 -0
- package/python/foxclaw/kernel/process_manager.py +186 -0
- package/python/foxclaw/kernel/scheduler.py +125 -0
- package/python/foxclaw/kernel/sentinel.py +443 -0
- package/python/foxclaw/kernel/sentinel_client.py +243 -0
- package/python/foxclaw/kernel/spine.py +97 -0
- package/python/foxclaw/knowledge/__init__.py +360 -0
- package/python/foxclaw/knowledge/truth_crystallizer.py +89 -0
- package/python/foxclaw/learning/__init__.py +12 -0
- package/python/foxclaw/learning/knowledge_graph.py +144 -0
- package/python/foxclaw/learning/models.py +93 -0
- package/python/foxclaw/learning/reasoner.py +67 -0
- package/python/foxclaw/learning/runtime.py +351 -0
- package/python/foxclaw/learning/spaced_repetition.py +91 -0
- package/python/foxclaw/mcp_server.py +188 -0
- package/python/foxclaw/orchestration/__init__.py +167 -0
- package/python/foxclaw/pipeline/__init__.py +23 -0
- package/python/foxclaw/pipeline/cognitive_pipeline.py +429 -0
- package/python/foxclaw/pipeline/stage_meta.py +21 -0
- package/python/foxclaw/pipeline/supervisor_stages.py +201 -0
- package/python/foxclaw/planning/__init__.py +17 -0
- package/python/foxclaw/planning/llm_planner.py +232 -0
- package/python/foxclaw/planning/planner.py +94 -0
- package/python/foxclaw/search/__init__.py +24 -0
- package/python/foxclaw/search/agent_port.py +190 -0
- package/python/foxclaw/search/evidence.py +109 -0
- package/python/foxclaw/search/external_search.py +50 -0
- package/python/foxclaw/search/hkc_read_port.py +60 -0
- package/python/foxclaw/search/internal_search.py +71 -0
- package/python/foxclaw/search/models.py +127 -0
- package/python/foxclaw/search/planner.py +44 -0
- package/python/foxclaw/search/retriever.py +39 -0
- package/python/foxclaw/search/runtime.py +127 -0
- package/python/foxclaw/search/strategy.py +44 -0
- package/python/foxclaw/skills/__init__.py +559 -0
- package/python/foxclaw/skills/archive.py +111 -0
- package/python/foxclaw/skills/fetch.py +226 -0
- package/python/foxclaw/skills/hooks_spec.py +69 -0
- package/python/foxclaw/skills/install_cmd.py +216 -0
- package/python/foxclaw/skills/lifecycle.py +252 -0
- package/python/foxclaw/skills/markdown_skill.py +175 -0
- package/python/foxclaw/skills/markdown_store.py +39 -0
- package/python/foxclaw/skills/mcp_client.py +184 -0
- package/python/foxclaw/skills/mcp_store.py +40 -0
- package/python/foxclaw/skills/permission.py +145 -0
- package/python/foxclaw/skills/pipeline.py +354 -0
- package/python/foxclaw/skills/policy.py +74 -0
- package/python/foxclaw/skills/review.py +72 -0
- package/python/foxclaw/skills/store.py +57 -0
- package/python/frontend/assets/core_bg.jpg +0 -0
- package/python/frontend/deck/auth.js +25 -0
- package/python/frontend/deck/chat_model_picker.js +483 -0
- package/python/frontend/deck/domain.js +276 -0
- package/python/frontend/deck/main.js +1806 -0
- package/python/frontend/deck/markdown.js +44 -0
- package/python/frontend/deck/settings.js +307 -0
- package/python/frontend/deck/state.js +5 -0
- package/python/frontend/deck/util.js +4 -0
- package/python/frontend/deck/visuals.js +190 -0
- package/python/frontend/deck.css +855 -0
- package/python/frontend/deck.html +686 -0
- package/python/frontend/deck_v2.html +341 -0
- package/python/frontend/planet/index.html +266 -0
- package/python/frontend/planet/planet.css +579 -0
- package/python/frontend/planet/planet.js +1436 -0
- package/python/frontend/planet/vendor/three.core.min.js +6 -0
- package/python/frontend/planet/vendor/three.module.min.js +6 -0
- package/python/frontend/v3/assets/index-CQSBdxk4.js +34 -0
- package/python/frontend/v3/assets/index-D5nIsjms.css +1 -0
- package/python/frontend/v3/index.html +15 -0
- package/python/frontend/widget_runtime.js +252 -0
- package/python/frontend/widgets.js +639 -0
- package/python/frontend_src/index.html +14 -0
- package/python/frontend_src/package-lock.json +1376 -0
- package/python/frontend_src/package.json +19 -0
- package/python/frontend_src/src/App.vue +58 -0
- package/python/frontend_src/src/api.js +82 -0
- package/python/frontend_src/src/components/ChatDock.vue +365 -0
- package/python/frontend_src/src/components/ComposerMenu.vue +75 -0
- package/python/frontend_src/src/components/DocViewer.vue +162 -0
- package/python/frontend_src/src/components/Inspector.vue +235 -0
- package/python/frontend_src/src/components/LeftRail.vue +415 -0
- package/python/frontend_src/src/components/MenuPopover.vue +128 -0
- package/python/frontend_src/src/components/MessageStream.vue +389 -0
- package/python/frontend_src/src/components/ModelPicker.vue +350 -0
- package/python/frontend_src/src/components/QuickIngest.vue +112 -0
- package/python/frontend_src/src/components/Stage.vue +272 -0
- package/python/frontend_src/src/components/ToolMenu.vue +141 -0
- package/python/frontend_src/src/components/TopBar.vue +118 -0
- package/python/frontend_src/src/components/panels/ActivityPanel.vue +82 -0
- package/python/frontend_src/src/components/panels/AgentsPanel.vue +83 -0
- package/python/frontend_src/src/components/panels/KnowledgePanel.vue +425 -0
- package/python/frontend_src/src/components/panels/MemoryPanel.vue +117 -0
- package/python/frontend_src/src/components/panels/PanelBox.vue +50 -0
- package/python/frontend_src/src/components/panels/ReviewPanel.vue +292 -0
- package/python/frontend_src/src/components/panels/SettingsPanel.vue +358 -0
- package/python/frontend_src/src/components/panels/TasksPanel.vue +73 -0
- package/python/frontend_src/src/components/panels/ToolsPanel.vue +746 -0
- package/python/frontend_src/src/components/panels/UsagePanel.vue +105 -0
- package/python/frontend_src/src/lib/fmt.js +44 -0
- package/python/frontend_src/src/lib/i18n.js +53 -0
- package/python/frontend_src/src/lib/locales/en.js +173 -0
- package/python/frontend_src/src/lib/locales/zh.js +174 -0
- package/python/frontend_src/src/lib/md.js +190 -0
- package/python/frontend_src/src/main.js +9 -0
- package/python/frontend_src/src/stores/app.js +91 -0
- package/python/frontend_src/src/stores/chat.js +450 -0
- package/python/frontend_src/src/stores/model.js +96 -0
- package/python/frontend_src/src/stores/panels.js +289 -0
- package/python/frontend_src/src/stores/profile.js +31 -0
- package/python/frontend_src/src/stores/system.js +25 -0
- package/python/frontend_src/src/styles/base.css +138 -0
- package/python/frontend_src/src/styles/panel.css +117 -0
- package/python/frontend_src/src/styles/tokens.css +78 -0
- package/python/frontend_src/vite.config.js +39 -0
- package/python/pyproject.toml +268 -0
- package/python/requirements.txt +311 -0
- package/python/skills/README.md +123 -0
- package/python/skills/execution-contract/SKILL.md +30 -0
- package/python/skills/general-assistant/SKILL.md +34 -0
- package/python/skills/planner/SKILL.md +33 -0
- package/python/skills/schedule-itinerary/SKILL.md +35 -0
- package/python/skills/skill-composition/SKILL.md +31 -0
- package/python/skills/skill-guard/SKILL.md +67 -0
- package/python/skills/skill-memory/SKILL.md +50 -0
- package/python/skills/skill-router/SKILL.md +35 -0
- package/python/tests/conftest.py +54 -0
- package/python/tests/run_all.py +200 -0
- package/python/tests/test_agent_lifecycle.py +326 -0
- package/python/tests/test_ahs_p2.py +393 -0
- package/python/tests/test_anthropic_tools.py +192 -0
- package/python/tests/test_api.py +97 -0
- package/python/tests/test_api_auth.py +79 -0
- package/python/tests/test_api_host_guard.py +69 -0
- package/python/tests/test_attachments.py +151 -0
- package/python/tests/test_audit_chain.py +116 -0
- package/python/tests/test_audit_sink.py +130 -0
- package/python/tests/test_browser.py +149 -0
- package/python/tests/test_browser_request_guard.py +180 -0
- package/python/tests/test_capabilities.py +152 -0
- package/python/tests/test_channel_signing.py +185 -0
- package/python/tests/test_channel_trust.py +109 -0
- package/python/tests/test_channels.py +174 -0
- package/python/tests/test_cli.py +46 -0
- package/python/tests/test_compaction_toolpair.py +119 -0
- package/python/tests/test_config_layering.py +61 -0
- package/python/tests/test_confirm_nonce.py +186 -0
- package/python/tests/test_context_compaction_wiring.py +124 -0
- package/python/tests/test_dispatch.py +89 -0
- package/python/tests/test_domain_evals.py +334 -0
- package/python/tests/test_domain_import.py +72 -0
- package/python/tests/test_domain_reasoners.py +134 -0
- package/python/tests/test_e2e.py +161 -0
- package/python/tests/test_emb_cache.py +97 -0
- package/python/tests/test_embedding_singleton.py +87 -0
- package/python/tests/test_envfile.py +83 -0
- package/python/tests/test_evals_smoke.py +90 -0
- package/python/tests/test_event_spine.py +174 -0
- package/python/tests/test_evidence_semantic.py +88 -0
- package/python/tests/test_evidence_sse.py +94 -0
- package/python/tests/test_file_ingest.py +135 -0
- package/python/tests/test_finance.py +144 -0
- package/python/tests/test_finance_ledger.py +245 -0
- package/python/tests/test_finance_persist.py +174 -0
- package/python/tests/test_gateway.py +125 -0
- package/python/tests/test_governance.py +421 -0
- package/python/tests/test_governance_audit.py +324 -0
- package/python/tests/test_governance_middleware.py +137 -0
- package/python/tests/test_hkc.py +268 -0
- package/python/tests/test_hkc_index_build_manager.py +306 -0
- package/python/tests/test_hkc_manifest_reuse.py +294 -0
- package/python/tests/test_hkc_search_event_sync.py +482 -0
- package/python/tests/test_import_path_guard.py +155 -0
- package/python/tests/test_injection_guard.py +193 -0
- package/python/tests/test_integration.py +117 -0
- package/python/tests/test_itinerary.py +100 -0
- package/python/tests/test_kee_semantic.py +84 -0
- package/python/tests/test_kernel.py +212 -0
- package/python/tests/test_learning.py +101 -0
- package/python/tests/test_llm_admin.py +145 -0
- package/python/tests/test_llm_planner.py +344 -0
- package/python/tests/test_markdown_skills.py +62 -0
- package/python/tests/test_markdown_skills_openclaw.py +66 -0
- package/python/tests/test_mcp_connectors.py +97 -0
- package/python/tests/test_mcp_guardian_discovery.py +87 -0
- package/python/tests/test_mcp_no_memory.py +43 -0
- package/python/tests/test_mcp_policy_gate.py +76 -0
- package/python/tests/test_mcp_server.py +92 -0
- package/python/tests/test_memory_recall.py +92 -0
- package/python/tests/test_metrics.py +58 -0
- package/python/tests/test_multi_chain.py +90 -0
- package/python/tests/test_multimodal.py +122 -0
- package/python/tests/test_negotiation.py +136 -0
- package/python/tests/test_orchestration.py +109 -0
- package/python/tests/test_orchestration_decoupled.py +103 -0
- package/python/tests/test_p3_p5_hardening.py +324 -0
- package/python/tests/test_parallel_dispatch.py +207 -0
- package/python/tests/test_pipeline.py +492 -0
- package/python/tests/test_plan_visible.py +86 -0
- package/python/tests/test_planner.py +88 -0
- package/python/tests/test_policy_gate.py +108 -0
- package/python/tests/test_profile.py +67 -0
- package/python/tests/test_readme_claims.py +163 -0
- package/python/tests/test_reasoner.py +170 -0
- package/python/tests/test_release_check.py +252 -0
- package/python/tests/test_resume_checkpoint.py +148 -0
- package/python/tests/test_routing.py +85 -0
- package/python/tests/test_scheduler.py +138 -0
- package/python/tests/test_search.py +194 -0
- package/python/tests/test_secrets.py +153 -0
- package/python/tests/test_semantic.py +69 -0
- package/python/tests/test_sessions.py +90 -0
- package/python/tests/test_settings.py +265 -0
- package/python/tests/test_shadow_learning.py +290 -0
- package/python/tests/test_shared_write.py +200 -0
- package/python/tests/test_skill_taint_gate.py +145 -0
- package/python/tests/test_skills.py +117 -0
- package/python/tests/test_skills_fetch.py +282 -0
- package/python/tests/test_stats_and_events.py +162 -0
- package/python/tests/test_streaming.py +55 -0
- package/python/tests/test_supervisor_pipeline.py +176 -0
- package/python/tests/test_supervisor_triage.py +102 -0
- package/python/tests/test_taint.py +112 -0
- package/python/tests/test_task_stream.py +74 -0
- package/python/tests/test_thinking_sse.py +66 -0
- package/python/tests/test_thinking_stream.py +76 -0
- package/python/tests/test_tool_center.py +113 -0
- package/python/tests/test_tool_parallel.py +113 -0
- package/python/tests/test_tool_params.py +99 -0
- package/python/tests/test_tool_use.py +229 -0
- package/python/tests/test_toplevel_concurrency.py +116 -0
- package/python/tests/test_tracing.py +256 -0
- package/python/tests/test_truth_crystallizer.py +125 -0
- package/python/tests/test_truth_flow.py +88 -0
- package/python/tests/test_update_check.py +95 -0
- package/python/tests/test_url_policy.py +53 -0
- package/python/tests/test_verify_evidence.py +212 -0
- package/python/tests/test_version_sync.py +100 -0
- package/python/tests/test_web_trigger.py +82 -0
- package/python/tools/check_faiss_type.py +30 -0
- package/python/tools/check_log.ps1 +38 -0
- package/python/tools/ivf_status.py +72 -0
- package/python/tools/mypy_baseline.txt +13 -0
- package/python/tools/mypy_ratchet.py +143 -0
- package/python/tools/pack.py +173 -0
- package/python/tools/release_check.py +349 -0
- package/python/tools/restart_foxclaw.ps1 +71 -0
- package/python/tools/shared_write_audit.py +190 -0
- package/python/tools/size_baseline.txt +28 -0
- package/python/tools/size_check.py +102 -0
- package/python/tools/strip_comments.py +228 -0
- package/python/tools/verify_env.bat +2 -0
- package/python/tools/verify_env.py +25 -0
- package/python/vendor/HMR_LICENSE +37 -0
- package/python/vendor/HMR_README.md +65 -0
- package/python/vendor/hkc/LICENSE +37 -0
- package/python/vendor/hkc/hkc_ace/__init__.py +0 -0
- package/python/vendor/hkc/hkc_ace/ace.py +457 -0
- package/python/vendor/hkc/hkc_ace/skill_taxonomy.json +465 -0
- package/python/vendor/hkc/hkc_api/__init__.py +0 -0
- package/python/vendor/hkc/hkc_api/container.py +416 -0
- package/python/vendor/hkc/hkc_api/ingest_jobs.py +103 -0
- package/python/vendor/hkc/hkc_api/main.py +157 -0
- package/python/vendor/hkc/hkc_api/routes/__init__.py +0 -0
- package/python/vendor/hkc/hkc_api/routes/ability.py +151 -0
- package/python/vendor/hkc/hkc_api/routes/config.py +51 -0
- package/python/vendor/hkc/hkc_api/routes/knowledge.py +630 -0
- package/python/vendor/hkc/hkc_api/routes/search.py +88 -0
- package/python/vendor/hkc/hkc_api/schemas.py +153 -0
- package/python/vendor/hkc/hkc_api/synthesis.py +179 -0
- package/python/vendor/hkc/hkc_core/__init__.py +0 -0
- package/python/vendor/hkc/hkc_core/graph/__init__.py +0 -0
- package/python/vendor/hkc/hkc_core/graph/base.py +188 -0
- package/python/vendor/hkc/hkc_core/graph/sqlite_store.py +789 -0
- package/python/vendor/hkc/hkc_core/models/__init__.py +0 -0
- package/python/vendor/hkc/hkc_core/models/enums.py +65 -0
- package/python/vendor/hkc/hkc_core/models/ku.py +307 -0
- package/python/vendor/hkc/hkc_core/utils/__init__.py +0 -0
- package/python/vendor/hkc/hkc_core/utils/id_gen.py +113 -0
- package/python/vendor/hkc/hkc_crystallizer/__init__.py +44 -0
- package/python/vendor/hkc/hkc_crystallizer/adapters.py +94 -0
- package/python/vendor/hkc/hkc_crystallizer/candidate.py +145 -0
- package/python/vendor/hkc/hkc_crystallizer/crystallizer.py +179 -0
- package/python/vendor/hkc/hkc_crystallizer/ingress.py +90 -0
- package/python/vendor/hkc/hkc_kde/__init__.py +0 -0
- package/python/vendor/hkc/hkc_kde/chunker.py +197 -0
- package/python/vendor/hkc/hkc_kde/extractor.py +623 -0
- package/python/vendor/hkc/hkc_kde/kde.py +298 -0
- package/python/vendor/hkc/hkc_kde/loaders/__init__.py +0 -0
- package/python/vendor/hkc/hkc_kde/loaders/ebook_loader.py +153 -0
- package/python/vendor/hkc/hkc_kde/loaders/office_loader.py +194 -0
- package/python/vendor/hkc/hkc_kde/loaders/pdf_loader.py +162 -0
- package/python/vendor/hkc/hkc_kde/loaders/text_loader.py +157 -0
- package/python/vendor/hkc/hkc_kde/loaders/web_loader.py +91 -0
- package/python/vendor/hkc/hkc_kde/models.py +74 -0
- package/python/vendor/hkc/hkc_kde/packager.py +329 -0
- package/python/vendor/hkc/hkc_kde/relation_disc.py +151 -0
- package/python/vendor/hkc/hkc_kee/__init__.py +0 -0
- package/python/vendor/hkc/hkc_kee/dedup.py +227 -0
- package/python/vendor/hkc/hkc_kee/fingerprint.py +106 -0
- package/python/vendor/hkc/hkc_kee/kee.py +541 -0
- package/python/vendor/hkc/hkc_kep/__init__.py +0 -0
- package/python/vendor/hkc/hkc_kep/event_bus.py +205 -0
- package/python/vendor/hkc/hkc_sdk/__init__.py +63 -0
- package/python/vendor/hkc/hkc_sdk/base.py +151 -0
- package/python/vendor/hkc/hkc_sdk/direct_client.py +153 -0
- package/python/vendor/hkc/hkc_sdk/exceptions.py +39 -0
- package/python/vendor/hkc/hkc_sdk/http_client.py +166 -0
- package/python/vendor/hkc/hkc_sdk/models.py +171 -0
- package/python/vendor/hkc/hkc_search/__init__.py +0 -0
- package/python/vendor/hkc/hkc_search/bm25.py +325 -0
- package/python/vendor/hkc/hkc_search/embedding_backends.py +374 -0
- package/python/vendor/hkc/hkc_search/graph_search.py +132 -0
- package/python/vendor/hkc/hkc_search/hybrid.py +331 -0
- package/python/vendor/hkc/hkc_search/index_build_manager.py +378 -0
- package/python/vendor/hkc/hkc_search/vector_search.py +484 -0
- package/python/vendor/hmr/__init__.py +0 -0
- package/python/vendor/hmr/core/__init__.py +0 -0
- package/python/vendor/hmr/core/hmr.py +684 -0
- package/python/vendor/hmr/core/models.py +310 -0
- package/python/vendor/hmr/engines/__init__.py +0 -0
- package/python/vendor/hmr/engines/evolution.py +710 -0
- package/python/vendor/hmr/engines/jit_compiler.py +472 -0
- package/python/vendor/hmr/engines/lifecycle.py +264 -0
- package/python/vendor/hmr/engines/policy.py +625 -0
- package/python/vendor/hmr/engines/recall.py +260 -0
- package/python/vendor/hmr/engines/runtime_state.py +158 -0
- package/python/vendor/hmr/engines/scheduler.py +323 -0
- package/python/vendor/hmr/engines/semantic.py +150 -0
- package/python/vendor/hmr/engines/temporal.py +261 -0
- package/python/vendor/hmr/engines/thought_chain.py +635 -0
- package/python/vendor/hmr/graph/__init__.py +0 -0
- package/python/vendor/hmr/graph/cwg.py +338 -0
- package/python/vendor/hmr/graph/memory_graph.py +530 -0
- package/python/vendor/hmr/storage/__init__.py +0 -0
- package/python/vendor/hmr/storage/memory_fs.py +300 -0
- package/python/vendor/hmr/storage/vector_store.py +520 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"""配置校验(core/settings.py)的行为测试。
|
|
2
|
+
|
|
3
|
+
立这些档的目的是让"配置错时的提示"本身成为被测试的对象:它比崩溃更容易腐坏 ——
|
|
4
|
+
提示错了没人会发现,只会让用户在错误的方向上改。
|
|
5
|
+
"""
|
|
6
|
+
import json
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
from foxclaw import cli
|
|
11
|
+
from foxclaw.core import settings
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@pytest.fixture(autouse=True)
|
|
15
|
+
def _reset_warned(monkeypatch):
|
|
16
|
+
monkeypatch.setattr(settings, "_warned", set())
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_get_int_reads_value(monkeypatch):
|
|
20
|
+
monkeypatch.setenv("FOXCLAW_PORT", "9001")
|
|
21
|
+
assert settings.get_int("FOXCLAW_PORT") == 9001
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_get_int_blank_means_default(monkeypatch):
|
|
25
|
+
monkeypatch.setenv("FOXCLAW_PORT", " ")
|
|
26
|
+
assert settings.get_int("FOXCLAW_PORT") == 8000
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_get_int_unset_means_default(monkeypatch):
|
|
30
|
+
monkeypatch.delenv("FOXCLAW_PORT", raising=False)
|
|
31
|
+
assert settings.get_int("FOXCLAW_PORT") == 8000
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_get_int_bad_value_names_variable_and_caller(monkeypatch):
|
|
35
|
+
monkeypatch.setenv("FOXCLAW_PORT", "80oo")
|
|
36
|
+
with pytest.raises(settings.ConfigError) as ei:
|
|
37
|
+
settings.get_int("FOXCLAW_PORT")
|
|
38
|
+
msg = str(ei.value)
|
|
39
|
+
assert "FOXCLAW_PORT" in msg and "80oo" in msg
|
|
40
|
+
assert "期望" in msg and "8000" in msg
|
|
41
|
+
assert "test_settings.py" in msg
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_get_int_below_min(monkeypatch):
|
|
45
|
+
monkeypatch.setenv("FOXCLAW_GATEWAY_RATE", "-5")
|
|
46
|
+
with pytest.raises(settings.ConfigError):
|
|
47
|
+
settings.get_int("FOXCLAW_GATEWAY_RATE")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_fractional_timeout_allowed(monkeypatch):
|
|
51
|
+
monkeypatch.setenv("FOXCLAW_TOOL_TIMEOUT", "0.3")
|
|
52
|
+
assert settings.get_float("FOXCLAW_TOOL_TIMEOUT") == pytest.approx(0.3)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_get_float_accepts_int_text(monkeypatch):
|
|
56
|
+
monkeypatch.setenv("FOXCLAW_LLM_COOLDOWN", "30")
|
|
57
|
+
assert settings.get_float("FOXCLAW_LLM_COOLDOWN") == 30.0
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_get_safe_degrades_and_warns_once(monkeypatch, caplog):
|
|
61
|
+
monkeypatch.setenv("FOXCLAW_TRACE_SLOW_MS", "慢")
|
|
62
|
+
with caplog.at_level("WARNING", logger="foxclaw.config"):
|
|
63
|
+
assert settings.get_safe("FOXCLAW_TRACE_SLOW_MS") == 1000.0
|
|
64
|
+
assert settings.get_safe("FOXCLAW_TRACE_SLOW_MS") == 1000.0
|
|
65
|
+
assert sum("FOXCLAW_TRACE_SLOW_MS" in r.message for r in caplog.records) == 1
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_get_safe_unknown_name_returns_default(monkeypatch):
|
|
69
|
+
monkeypatch.setenv("SOME_UNDECLARED_KNOB", "abc")
|
|
70
|
+
assert settings.get_safe("SOME_UNDECLARED_KNOB", "dflt") == "abc"
|
|
71
|
+
assert settings.get_safe("NOT_SET_AT_ALL_X", 7) == 7
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _clean(monkeypatch, *keys):
|
|
75
|
+
"""清掉一组变量,避免宿主机上真实存在的键干扰断言。"""
|
|
76
|
+
for k in keys:
|
|
77
|
+
monkeypatch.delenv(k, raising=False)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_check_flags_type_error_without_raising(monkeypatch):
|
|
81
|
+
monkeypatch.setenv("FOXCLAW_PORT", "80oo")
|
|
82
|
+
issues = settings.check({"FOXCLAW_PORT": "80oo", "DEEPSEEK_API_KEY": "x"})
|
|
83
|
+
errs = [i for i in issues if i.level == "error"]
|
|
84
|
+
assert len(errs) == 1 and errs[0].code == "type" and errs[0].name == "FOXCLAW_PORT"
|
|
85
|
+
assert errs[0].hint == ""
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_check_collects_multiple_errors(monkeypatch):
|
|
89
|
+
env = {"FOXCLAW_PORT": "x", "FOXCLAW_GATEWAY_RATE": "y",
|
|
90
|
+
"DEEPSEEK_API_KEY": "k"}
|
|
91
|
+
names = {i.name for i in settings.check(env) if i.level == "error"}
|
|
92
|
+
assert {"FOXCLAW_PORT", "FOXCLAW_GATEWAY_RATE"} <= names
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def test_check_choice_error(monkeypatch):
|
|
96
|
+
issues = settings.check({"LLM_PROVIDER": "deepsek", "DEEPSEEK_API_KEY": "k"})
|
|
97
|
+
codes = {(i.code, i.name) for i in issues if i.level == "error"}
|
|
98
|
+
assert ("choice", "LLM_PROVIDER") in codes
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_check_clean_env_has_no_error():
|
|
102
|
+
assert [i for i in settings.check({"DEEPSEEK_API_KEY": "k"}) if i.level == "error"] == []
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_check_secret_value_is_masked_in_message():
|
|
106
|
+
issues = settings.check({"LLM_PROVIDER": "openai", "OPENAI_API_KEY": " sk-live-1234567890 "})
|
|
107
|
+
msg = " ".join(i.message for i in issues)
|
|
108
|
+
assert "sk-live-1234567890" not in msg
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def test_provider_without_key_is_error():
|
|
112
|
+
issues = settings.check({"LLM_PROVIDER": "deepseek", "ANTHROPIC_API_KEY": ""})
|
|
113
|
+
hit = [i for i in issues if i.code == "provider_key"]
|
|
114
|
+
assert hit and hit[0].level == "error" and hit[0].name == "DEEPSEEK_API_KEY"
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def test_provider_with_key_is_fine():
|
|
118
|
+
issues = settings.check({"LLM_PROVIDER": "deepseek", "DEEPSEEK_API_KEY": "k"})
|
|
119
|
+
assert not [i for i in issues if i.code == "provider_key"]
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def test_legacy_provider_name_also_checked():
|
|
123
|
+
issues = settings.check({"FOXCLAW_PROVIDER": "openai"})
|
|
124
|
+
assert [i for i in issues if i.code == "provider_key"]
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def test_no_key_at_all_is_warn_not_error():
|
|
128
|
+
issues = settings.check({})
|
|
129
|
+
hit = [i for i in issues if i.code == "no_llm"]
|
|
130
|
+
assert hit and hit[0].level == "warn"
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def test_typo_key_is_flagged_with_suggestion():
|
|
134
|
+
issues = settings.check({"DEEPSEEK_API_KEY": "k", "FOXCLAW_TOOL_TIMEOUTS": "60"})
|
|
135
|
+
hit = [i for i in issues if i.name == "FOXCLAW_TOOL_TIMEOUTS"]
|
|
136
|
+
assert hit and hit[0].code == "unknown_key"
|
|
137
|
+
assert "FOXCLAW_TOOL_TIMEOUT" in hit[0].hint
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_known_key_is_not_flagged():
|
|
141
|
+
issues = settings.check({"FOXCLAW_TOOL_TIMEOUT": "60", "DEEPSEEK_API_KEY": "k"})
|
|
142
|
+
assert not [i for i in issues if i.name == "FOXCLAW_TOOL_TIMEOUT"]
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def test_system_env_vars_are_not_flagged():
|
|
146
|
+
issues = settings.check({"PATH": "/usr/bin", "DEEPSEEK_API_KEY": "k"})
|
|
147
|
+
assert not [i for i in issues if i.name == "PATH"]
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def test_known_keys_covers_real_usage():
|
|
151
|
+
known = settings.known_keys()
|
|
152
|
+
for name in ("FOXCLAW_PORT", "FOXCLAW_TOOL_TIMEOUT", "DEEPSEEK_API_KEY",
|
|
153
|
+
"FOXCLAW_CHANNEL_SIGNING_SECRET"):
|
|
154
|
+
assert name in known
|
|
155
|
+
assert "NOT_A_REAL_KEY_NAME" not in known
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def test_signing_key_id_without_secret_warns():
|
|
159
|
+
issues = settings.check({"DEEPSEEK_API_KEY": "k",
|
|
160
|
+
"FOXCLAW_CHANNEL_SIGNING_KEY_ID": "k2"})
|
|
161
|
+
assert [i for i in issues if i.code == "signing_pair"]
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def test_short_signing_secret_warns():
|
|
165
|
+
issues = settings.check({"DEEPSEEK_API_KEY": "k",
|
|
166
|
+
"FOXCLAW_CHANNEL_SIGNING_SECRET": "short"})
|
|
167
|
+
assert [i for i in issues if i.code == "weak_secret"]
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def test_open_cors_without_api_key_warns():
|
|
171
|
+
issues = settings.check({"DEEPSEEK_API_KEY": "k", "FOXCLAW_CORS_ORIGINS": "*"})
|
|
172
|
+
assert [i for i in issues if i.code == "open_cors"]
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def test_gateway_off_with_api_key_warns():
|
|
176
|
+
issues = settings.check({"DEEPSEEK_API_KEY": "k", "FOXCLAW_API_KEY": "s3cret",
|
|
177
|
+
"FOXCLAW_GATEWAY": "0"})
|
|
178
|
+
assert [i for i in issues if i.code == "gateway_off"]
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@pytest.mark.parametrize("value,expect", [("", "(空)"), ("abcd", "****"),
|
|
182
|
+
("sk-1234567890", "sk******90")])
|
|
183
|
+
def test_mask(value, expect):
|
|
184
|
+
assert settings.mask(value) == expect
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def test_is_secret_heuristics():
|
|
188
|
+
assert settings.is_secret("DEEPSEEK_API_KEY")
|
|
189
|
+
assert settings.is_secret("SOME_TOKEN_X")
|
|
190
|
+
assert settings.is_secret("AMAP_JS_SCODE")
|
|
191
|
+
assert not settings.is_secret("FOXCLAW_PORT")
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def test_format_issues_counts_and_limit():
|
|
195
|
+
issues = [settings.Issue("error", "type", f"K{i}", "m") for i in range(5)]
|
|
196
|
+
out = settings.format_issues(issues, limit=2)
|
|
197
|
+
assert "5 个错误" in out and "另有 3 条" in out
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def test_format_issues_clean():
|
|
201
|
+
assert "通过" in settings.format_issues([])
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def test_fatal_issues_only_type_and_choice():
|
|
205
|
+
issues = [settings.Issue("error", "type", "A", "m"),
|
|
206
|
+
settings.Issue("error", "provider_key", "B", "m"),
|
|
207
|
+
settings.Issue("warn", "type", "C", "m")]
|
|
208
|
+
assert [i.name for i in settings.fatal_issues(issues)] == ["A"]
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def test_cli_config_check_exit_code_on_error(monkeypatch, capsys):
|
|
212
|
+
monkeypatch.setenv("FOXCLAW_PORT", "80oo")
|
|
213
|
+
assert cli.main(["config", "check"]) == 1
|
|
214
|
+
out = capsys.readouterr().out
|
|
215
|
+
assert "FOXCLAW_PORT" in out and "80oo" in out
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def test_cli_config_check_clean_exit_zero(monkeypatch, capsys):
|
|
219
|
+
monkeypatch.setenv("DEEPSEEK_API_KEY", "k")
|
|
220
|
+
monkeypatch.delenv("FOXCLAW_PORT", raising=False)
|
|
221
|
+
assert cli.main(["config", "check"]) == 0
|
|
222
|
+
assert "配置检查" in capsys.readouterr().out
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def test_cli_config_check_json(monkeypatch, capsys):
|
|
226
|
+
monkeypatch.setenv("FOXCLAW_PORT", "80oo")
|
|
227
|
+
cli.main(["config", "check", "--json"])
|
|
228
|
+
data = json.loads(capsys.readouterr().out)
|
|
229
|
+
assert isinstance(data, list) and data[0]["name"] == "FOXCLAW_PORT"
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def test_cli_config_without_action_is_usage_error(capsys):
|
|
233
|
+
assert cli.main(["config"]) == 2
|
|
234
|
+
assert "config check" in capsys.readouterr().out
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def test_cli_config_show_masks_secrets(monkeypatch, capsys):
|
|
238
|
+
monkeypatch.setenv("DEEPSEEK_API_KEY", "sk-abcdef123456")
|
|
239
|
+
cli.main(["config", "show"])
|
|
240
|
+
out = capsys.readouterr().out
|
|
241
|
+
assert "sk-abcdef123456" not in out and "DEEPSEEK_API_KEY" in out
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def test_cli_config_show_reveal(monkeypatch, capsys):
|
|
245
|
+
monkeypatch.setenv("DEEPSEEK_API_KEY", "sk-abcdef123456")
|
|
246
|
+
cli.main(["config", "show", "--reveal"])
|
|
247
|
+
assert "sk-abcdef123456" in capsys.readouterr().out
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def test_serve_aborts_before_starting_on_bad_type(monkeypatch):
|
|
251
|
+
monkeypatch.setenv("FOXCLAW_PORT", "80oo")
|
|
252
|
+
args = cli.build_parser().parse_args(["serve"])
|
|
253
|
+
assert cli._cmd_serve(args) == 2
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def test_serve_semantic_warning_does_not_block(monkeypatch):
|
|
257
|
+
monkeypatch.setenv("FOXCLAW_CORS_ORIGINS", "*")
|
|
258
|
+
monkeypatch.setenv("DEEPSEEK_API_KEY", "k")
|
|
259
|
+
args = cli.build_parser().parse_args(["serve"])
|
|
260
|
+
import sys
|
|
261
|
+
import types
|
|
262
|
+
fake = types.ModuleType("uvicorn")
|
|
263
|
+
fake.run = lambda *a, **k: None
|
|
264
|
+
monkeypatch.setitem(sys.modules, "uvicorn", fake)
|
|
265
|
+
assert cli._cmd_serve(args) == 0
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"""批次 4 · P0-1 Harness 自学习影子验证 + 回滚 + 边界。
|
|
2
|
+
|
|
3
|
+
验收(执行计划/文稿附录 C):
|
|
4
|
+
BLOCK 进 shadow_rules → 观察 N 次 → promote/discard;安全维度禁学。
|
|
5
|
+
覆盖(对应文稿「验证」四条 + 回滚/边界/持久化/事件):
|
|
6
|
+
1. BLOCK(tool) → 规则进 shadow_rules,**不生效**(不进 rules、不重建索引)
|
|
7
|
+
2. 影子观察:适用场景计数 observations;双轨 would_block/would_pass
|
|
8
|
+
3. 观察达标 → promote(移入 rules、生效、history、事件)
|
|
9
|
+
4. 误伤规则(would_block 率 > 0.3)→ discard;无意义规则(双零)→ discard
|
|
10
|
+
5. 安全边界:semantic BLOCK 不生成规则(仅权重);未知维度 fail-closed 只记录
|
|
11
|
+
6. rollback(rule_id):撤出生效、标记 rolled_back、不再生效
|
|
12
|
+
7. 持久化:shadow_rules/history 落盘,重载后影子状态保留
|
|
13
|
+
8. 事件:proposed/promoted/discarded/rolled_back 全发出
|
|
14
|
+
"""
|
|
15
|
+
import os
|
|
16
|
+
import sys
|
|
17
|
+
import tempfile
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
21
|
+
os.environ.setdefault("FOXCLAW_SKIP_DOTENV", "1")
|
|
22
|
+
os.environ.setdefault("HKC_EMBEDDING", "stub")
|
|
23
|
+
|
|
24
|
+
CHECKS = []
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def check(cond, msg):
|
|
28
|
+
CHECKS.append((bool(cond), msg))
|
|
29
|
+
print(f" {'✓' if cond else '✗'} {msg}")
|
|
30
|
+
if not cond:
|
|
31
|
+
raise AssertionError(f"断言失败: {msg}")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
from foxclaw.core.thought_chain import ThoughtChain, ThoughtNode
|
|
35
|
+
from foxclaw.harness.harness import Harness
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _tc():
|
|
39
|
+
return ThoughtChain(db_path=tempfile.mktemp(suffix=".db"))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def fresh(tc, promote_min_obs=3, baseline_entities=frozenset({"预算"})):
|
|
43
|
+
"""实判态 harness:跳过阈值校准观测期,固定阈值,小 promote 门槛便于测试。"""
|
|
44
|
+
h = Harness(agent_id="t", thought_chain=tc,
|
|
45
|
+
policy_path=tempfile.mktemp(suffix=".yaml"),
|
|
46
|
+
shadow_rounds=0, promote_min_obs=promote_min_obs)
|
|
47
|
+
h.register_goal("分析财务预算", allowed_tool_categories={"read", "finance"},
|
|
48
|
+
baseline_entities=baseline_entities)
|
|
49
|
+
h._shadow_mode = False
|
|
50
|
+
h._block_threshold, h._warn_threshold = 0.25, 0.18
|
|
51
|
+
return h
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def node(tc, tool=None, entities=None, content="继续分析预算"):
|
|
55
|
+
n = ThoughtNode.create(session_id="s", agent_id="t", type="tool_intent",
|
|
56
|
+
content=content, conf=0.9,
|
|
57
|
+
tool_name=tool, entities=entities or [])
|
|
58
|
+
tc.add_node(n)
|
|
59
|
+
return n
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def events_of(h):
|
|
63
|
+
"""收集 harness bus 本地发出的事件名序列。"""
|
|
64
|
+
return getattr(h, "_test_events", [])
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _tap_events(h):
|
|
68
|
+
h._test_events = []
|
|
69
|
+
for ev in ("shadow_rule_proposed", "shadow_rule_promoted",
|
|
70
|
+
"shadow_rule_discarded", "shadow_rule_rolled_back"):
|
|
71
|
+
h.bus.on(ev, lambda data, _ev=ev: h._test_events.append(_ev))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_block_goes_to_shadow():
|
|
75
|
+
print("\n【1】BLOCK → 影子候选(不生效)+ 双轨观察计数")
|
|
76
|
+
tc = _tc()
|
|
77
|
+
h = fresh(tc, promote_min_obs=3)
|
|
78
|
+
_tap_events(h)
|
|
79
|
+
|
|
80
|
+
r = h.check(node(tc, tool="delete_everything"))
|
|
81
|
+
check(r.is_blocked, "越权工具被 BLOCK(行为不变)")
|
|
82
|
+
check(not h._is_penalized_tool("delete_everything"),
|
|
83
|
+
"影子期不生效:工具未被 penalize(未重建索引)")
|
|
84
|
+
sh_rules = h._policy["shadow_rules"]
|
|
85
|
+
check(len(sh_rules) == 1, "候选进入 shadow_rules(非 rules)")
|
|
86
|
+
check(h._policy.get("rules", []) == [], "rules 仍为空(未生效)")
|
|
87
|
+
sh = sh_rules[0]["shadow"]
|
|
88
|
+
check(sh["status"] == "observing" and sh["observations"] == 0,
|
|
89
|
+
"影子账本初始化 observing/0")
|
|
90
|
+
check("shadow_rule_proposed" in events_of(h), "shadow_rule_proposed 事件已发")
|
|
91
|
+
check(any(e["event"] == "propose" for e in h._policy["history"]),
|
|
92
|
+
"history 记 propose")
|
|
93
|
+
|
|
94
|
+
for _ in range(2):
|
|
95
|
+
h.check(node(tc, tool="delete_everything"))
|
|
96
|
+
sh = h._policy["shadow_rules"][0]["shadow"]
|
|
97
|
+
check(sh["observations"] == 2, f"适用场景 observations 累积(实为 {sh['observations']})")
|
|
98
|
+
h.check(node(tc, tool="delete_everything"))
|
|
99
|
+
sh = h._policy["shadow_rules"][0]["shadow"]
|
|
100
|
+
check(sh["status"] == "discarded",
|
|
101
|
+
"双零证据(影子与 real 判定完全一致)→ 无意义 → discard")
|
|
102
|
+
check("shadow_rule_discarded" in events_of(h), "shadow_rule_discarded 事件已发")
|
|
103
|
+
check(h._policy.get("rules", []) == [], "discard 后 rules 仍为空")
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_promote_after_evidence():
|
|
107
|
+
print("\n【2】影子证据达标 → promote(生效 + history + 事件)")
|
|
108
|
+
tc = _tc()
|
|
109
|
+
h = fresh(tc, promote_min_obs=5)
|
|
110
|
+
_tap_events(h)
|
|
111
|
+
cand = {
|
|
112
|
+
"id": "rule_manual_test",
|
|
113
|
+
"trigger": {"harness_status": "BLOCK", "dominant_dim": "tool",
|
|
114
|
+
"delta_threshold": 0.3},
|
|
115
|
+
"action": {"type": "penalize_tool", "tool_pattern": "suspicious_api",
|
|
116
|
+
"weight_delta": -0.3},
|
|
117
|
+
"created_at": "2026-10-01T00:00:00+00:00",
|
|
118
|
+
"source": "harness_learned", "agent_id": "t",
|
|
119
|
+
"shadow": {"created_at": "2026-10-01T00:00:00+00:00",
|
|
120
|
+
"observations": 0, "would_block": 0, "would_pass": 0,
|
|
121
|
+
"status": "observing"},
|
|
122
|
+
}
|
|
123
|
+
h._policy["shadow_rules"].append(cand)
|
|
124
|
+
|
|
125
|
+
orig = h.tool_drift.score
|
|
126
|
+
h.tool_drift.score = lambda tool, allowed: 0.5 if tool == "suspicious_api" else orig(tool, allowed)
|
|
127
|
+
try:
|
|
128
|
+
r = h.check(node(tc, tool="suspicious_api"))
|
|
129
|
+
check(not r.is_blocked, "real 判定 PASS(tool=0.5 未跨阈,影子不拦 real)")
|
|
130
|
+
for i in range(4):
|
|
131
|
+
h.check(node(tc, tool=f"other_tool_{i}"))
|
|
132
|
+
rules = h._policy.get("rules", [])
|
|
133
|
+
check(any(x["id"] == "rule_manual_test" for x in rules),
|
|
134
|
+
"证据达标(wb/obs=1/5≤0.3)→ promote 到 rules")
|
|
135
|
+
check(h._policy["shadow_rules"] == [], "shadow_rules 已清空(promote 移出)")
|
|
136
|
+
check(h._is_penalized_tool("suspicious_api"),
|
|
137
|
+
"promote 后规则生效(索引重建)")
|
|
138
|
+
check(any(e["event"] == "promote" for e in h._policy["history"]),
|
|
139
|
+
"history 记 promote")
|
|
140
|
+
check("shadow_rule_promoted" in events_of(h), "shadow_rule_promoted 事件已发")
|
|
141
|
+
r2 = h.check(node(tc, tool="suspicious_api"))
|
|
142
|
+
check(r2.is_blocked, "promote 后同一工具被真实 BLOCK(规则级生效闭环)")
|
|
143
|
+
finally:
|
|
144
|
+
h.tool_drift.score = orig
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_discard_harmful_rule():
|
|
148
|
+
print("\n【3】误伤规则(would_block 率>0.3)→ discard")
|
|
149
|
+
tc = _tc()
|
|
150
|
+
h = fresh(tc, promote_min_obs=3)
|
|
151
|
+
_tap_events(h)
|
|
152
|
+
cand = {
|
|
153
|
+
"id": "rule_harmful",
|
|
154
|
+
"trigger": {"harness_status": "BLOCK", "dominant_dim": "tool",
|
|
155
|
+
"delta_threshold": 0.3},
|
|
156
|
+
"action": {"type": "penalize_tool", "tool_pattern": "get_balance"},
|
|
157
|
+
"created_at": "2026-10-01T00:00:00+00:00",
|
|
158
|
+
"source": "harness_learned", "agent_id": "t",
|
|
159
|
+
"shadow": {"created_at": "2026-10-01T00:00:00+00:00",
|
|
160
|
+
"observations": 0, "would_block": 0, "would_pass": 0,
|
|
161
|
+
"status": "observing"},
|
|
162
|
+
}
|
|
163
|
+
h._policy["shadow_rules"].append(cand)
|
|
164
|
+
orig = h.tool_drift.score
|
|
165
|
+
h.tool_drift.score = lambda tool, allowed: 0.5 if tool == "get_balance" else orig(tool, allowed)
|
|
166
|
+
try:
|
|
167
|
+
for _ in range(3):
|
|
168
|
+
h.check(node(tc, tool="get_balance"))
|
|
169
|
+
sh = h._policy["shadow_rules"][0]["shadow"]
|
|
170
|
+
check(sh["status"] == "discarded",
|
|
171
|
+
f"would_block 率 {sh['would_block']}/{sh['observations']} > 0.3 → discard")
|
|
172
|
+
check(sh["would_block"] >= 2, "would_block 如实计数(影子会多拦)")
|
|
173
|
+
check(not h._is_penalized_tool("get_balance"), "误伤规则未生效")
|
|
174
|
+
check("shadow_rule_discarded" in events_of(h), "discard 事件已发")
|
|
175
|
+
finally:
|
|
176
|
+
h.tool_drift.score = orig
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def test_safety_boundary():
|
|
180
|
+
print("\n【4】安全边界:semantic 仅权重级 / 未知维度 fail-closed")
|
|
181
|
+
tc = _tc()
|
|
182
|
+
h = fresh(tc)
|
|
183
|
+
_tap_events(h)
|
|
184
|
+
|
|
185
|
+
w0 = dict(h.weights)
|
|
186
|
+
r = h.check(node(tc, content="量子力学与黑洞信息悖论的数学形式化推导",
|
|
187
|
+
entities=["预算"]))
|
|
188
|
+
if r.is_blocked and r.dominant_dim == "semantic":
|
|
189
|
+
check(h._policy.get("shadow_rules", []) == [],
|
|
190
|
+
"semantic BLOCK 不生成影子规则(仅权重级)")
|
|
191
|
+
check(h.weights["semantic"] > w0["semantic"],
|
|
192
|
+
"semantic 权重被上调(权重级学习保留)")
|
|
193
|
+
else:
|
|
194
|
+
from foxclaw.harness.harness import HarnessResult
|
|
195
|
+
res = HarnessResult(status="BLOCK", delta=0.9, dominant_dim="semantic",
|
|
196
|
+
scores={"semantic": 0.9, "tool": 0.0, "scope": 0.0},
|
|
197
|
+
reason="t")
|
|
198
|
+
h._trigger_policy_learning(node(tc), res)
|
|
199
|
+
check(h._policy.get("shadow_rules", []) == [],
|
|
200
|
+
"semantic BLOCK(直驱)不生成影子规则(仅权重级)")
|
|
201
|
+
check(h.weights["semantic"] > w0["semantic"], "semantic 权重上调")
|
|
202
|
+
|
|
203
|
+
from foxclaw.harness.harness import HarnessResult
|
|
204
|
+
w1 = dict(h.weights)
|
|
205
|
+
res = HarnessResult(status="BLOCK", delta=0.9, dominant_dim="financial",
|
|
206
|
+
scores={}, reason="t")
|
|
207
|
+
h._trigger_policy_learning(node(tc), res)
|
|
208
|
+
check(h.weights == w1, "未知维度 fail-closed:权重不动")
|
|
209
|
+
check(any(e["event"] == "skip" for e in h._policy["history"]),
|
|
210
|
+
"未知维度只记录(history skip)")
|
|
211
|
+
check(h._policy.get("shadow_rules", []) == [], "未知维度不生成规则")
|
|
212
|
+
check("shadow_rule_proposed" not in events_of(h), "未知维度不发 propose 事件")
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def test_rollback():
|
|
216
|
+
print("\n【5】rollback:撤出生效 + 标记 rolled_back + 不再生效")
|
|
217
|
+
tc = _tc()
|
|
218
|
+
h = fresh(tc)
|
|
219
|
+
_tap_events(h)
|
|
220
|
+
h._policy["rules"].append({
|
|
221
|
+
"id": "rule_rb", "trigger": {"dominant_dim": "tool"},
|
|
222
|
+
"action": {"type": "penalize_tool", "tool_pattern": "bad_tool"},
|
|
223
|
+
"source": "harness_learned", "agent_id": "t"})
|
|
224
|
+
h._reindex_learned_actions()
|
|
225
|
+
check(h._is_penalized_tool("bad_tool"), "前提:规则生效中")
|
|
226
|
+
|
|
227
|
+
ok = h.rollback("rule_rb")
|
|
228
|
+
check(ok, "rollback 返回 True")
|
|
229
|
+
check(not h._is_penalized_tool("bad_tool"), "回滚后不再生效(索引已重建)")
|
|
230
|
+
check(h._policy["rules"] == [], "rules 已移除该规则")
|
|
231
|
+
rb = h._policy["shadow_rules"][0]
|
|
232
|
+
check(rb["shadow"]["status"] == "rolled_back", "规则标记 rolled_back(保留可审计)")
|
|
233
|
+
check(any(e["event"] == "rollback" for e in h._policy["history"]),
|
|
234
|
+
"history 记 rollback")
|
|
235
|
+
check("shadow_rule_rolled_back" in events_of(h), "rolled_back 事件已发")
|
|
236
|
+
check(h.rollback("nonexistent") is False, "找不到规则返回 False")
|
|
237
|
+
|
|
238
|
+
h.check(node(tc, tool="bad_tool"))
|
|
239
|
+
check(h._policy["shadow_rules"][0]["shadow"]["observations"] == 0,
|
|
240
|
+
"rolled_back 不再参与影子观察")
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def test_persistence():
|
|
244
|
+
print("\n【6】持久化:shadow/history 落盘,重载保留")
|
|
245
|
+
tc = _tc()
|
|
246
|
+
p = tempfile.mktemp(suffix=".yaml")
|
|
247
|
+
h = Harness(agent_id="t", thought_chain=tc, policy_path=p,
|
|
248
|
+
shadow_rounds=0, promote_min_obs=3)
|
|
249
|
+
h.register_goal("分析财务预算", allowed_tool_categories={"read", "finance"},
|
|
250
|
+
baseline_entities=frozenset({"预算"}))
|
|
251
|
+
h._shadow_mode = False
|
|
252
|
+
h.check(node(tc, tool="delete_everything"))
|
|
253
|
+
|
|
254
|
+
import yaml
|
|
255
|
+
doc = yaml.safe_load(open(p, encoding="utf-8"))
|
|
256
|
+
check("shadow_rules" in doc and len(doc["shadow_rules"]) == 1,
|
|
257
|
+
"shadow_rules 已落盘")
|
|
258
|
+
check("history" in doc and any(e["event"] == "propose" for e in doc["history"]),
|
|
259
|
+
"history 已落盘")
|
|
260
|
+
|
|
261
|
+
h2 = Harness(agent_id="t", thought_chain=tc, policy_path=p,
|
|
262
|
+
shadow_rounds=0, promote_min_obs=3)
|
|
263
|
+
h2.register_goal("分析财务预算", allowed_tool_categories={"read", "finance"},
|
|
264
|
+
baseline_entities=frozenset({"预算"}))
|
|
265
|
+
h2._shadow_mode = False
|
|
266
|
+
check(len(h2._policy["shadow_rules"]) == 1
|
|
267
|
+
and h2._policy["shadow_rules"][0]["shadow"]["status"] == "observing",
|
|
268
|
+
"重载后影子规则与观察状态保留")
|
|
269
|
+
check(not h2._is_penalized_tool("delete_everything"),
|
|
270
|
+
"重载后影子仍未生效")
|
|
271
|
+
os.unlink(p)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def main():
|
|
275
|
+
print("=" * 60)
|
|
276
|
+
print("批次 4 · P0-1 影子学习(shadow→promote/discard+回滚+边界)")
|
|
277
|
+
print("=" * 60)
|
|
278
|
+
test_block_goes_to_shadow()
|
|
279
|
+
test_promote_after_evidence()
|
|
280
|
+
test_discard_harmful_rule()
|
|
281
|
+
test_safety_boundary()
|
|
282
|
+
test_rollback()
|
|
283
|
+
test_persistence()
|
|
284
|
+
print("\n" + "=" * 60)
|
|
285
|
+
print(f"✓ 全部 {len(CHECKS)} 项断言通过")
|
|
286
|
+
print("=" * 60)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
if __name__ == "__main__":
|
|
290
|
+
main()
|