foxclaw 0.1.0 → 0.1.2

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.
Files changed (495) hide show
  1. package/README.md +54 -29
  2. package/bin/foxclaw.cjs +51 -0
  3. package/index.js +11 -10
  4. package/package.json +46 -38
  5. package/python/.dockerignore +35 -0
  6. package/python/.env.example +165 -0
  7. package/python/.gitattributes +46 -0
  8. package/python/.github/workflows/ci.yml +122 -0
  9. package/python/.github/workflows/release.yml +183 -0
  10. package/python/Dockerfile +64 -0
  11. package/python/LICENSE +21 -0
  12. package/python/MANIFEST.in +14 -0
  13. package/python/README.md +192 -0
  14. package/python/README_CN.md +324 -0
  15. package/python/bench/bm25_audit.py +199 -0
  16. package/python/bench/bm25_regression.py +178 -0
  17. package/python/bench/bm25_stress.py +159 -0
  18. package/python/bench/cd_audit.py +238 -0
  19. package/python/bench/chaos.py +142 -0
  20. package/python/bench/concurrency.py +103 -0
  21. package/python/bench/e2e_real_models.py +95 -0
  22. package/python/bench/llm_retry_test.py +248 -0
  23. package/python/bench/load_test.py +196 -0
  24. package/python/bench/vector_ivf_upgrade.py +134 -0
  25. package/python/docs/CHANNELS.md +195 -0
  26. package/python/docs/GOVERNANCE.md +164 -0
  27. package/python/docs/RELEASE.md +109 -0
  28. package/python/docs/RUN.md +75 -0
  29. package/python/docs/RUN_CN.md +121 -0
  30. package/python/docs//351/205/215/347/275/256/346/214/207/345/215/227.md +152 -0
  31. package/python/evals/__init__.py +0 -0
  32. package/python/evals/governance/README.md +61 -0
  33. package/python/evals/governance/__init__.py +0 -0
  34. package/python/evals/governance/cases.py +152 -0
  35. package/python/evals/governance/compare_embeddings.py +94 -0
  36. package/python/evals/governance/convergence.svg +11 -0
  37. package/python/evals/governance/report.json +371 -0
  38. package/python/evals/governance/run_evals.py +400 -0
  39. package/python/examples/governance_middleware_demo.py +77 -0
  40. package/python/examples/langgraph_governance_demo.py +108 -0
  41. package/python/examples/sample_itinerary.csv +6 -0
  42. package/python/examples/sample_knowledge.csv +9 -0
  43. package/python/examples/sample_transactions.csv +14 -0
  44. package/python/foxclaw/__init__.py +526 -0
  45. package/python/foxclaw/agents/__init__.py +12 -0
  46. package/python/foxclaw/agents/agents.py +35 -0
  47. package/python/foxclaw/agents/base.py +865 -0
  48. package/python/foxclaw/agents/impl/__init__.py +1 -0
  49. package/python/foxclaw/agents/impl/_helpers.py +78 -0
  50. package/python/foxclaw/agents/impl/finance.py +248 -0
  51. package/python/foxclaw/agents/impl/office.py +367 -0
  52. package/python/foxclaw/agents/impl/supervisor.py +659 -0
  53. package/python/foxclaw/agents/mixins/_context.py +151 -0
  54. package/python/foxclaw/agents/mixins/augment.py +149 -0
  55. package/python/foxclaw/agents/mixins/llm.py +66 -0
  56. package/python/foxclaw/agents/mixins/memory.py +35 -0
  57. package/python/foxclaw/agents/mixins/preference.py +81 -0
  58. package/python/foxclaw/agents/mixins/prompt.py +77 -0
  59. package/python/foxclaw/agents/mixins/skillbox.py +58 -0
  60. package/python/foxclaw/agents/mixins/tool_exec.py +443 -0
  61. package/python/foxclaw/api/__init__.py +283 -0
  62. package/python/foxclaw/api/app.py +123 -0
  63. package/python/foxclaw/api/middleware.py +317 -0
  64. package/python/foxclaw/api/routes/ahs.py +417 -0
  65. package/python/foxclaw/api/routes/attachments.py +117 -0
  66. package/python/foxclaw/api/routes/browser.py +39 -0
  67. package/python/foxclaw/api/routes/domain.py +314 -0
  68. package/python/foxclaw/api/routes/knowledge.py +163 -0
  69. package/python/foxclaw/api/routes/llm_admin.py +461 -0
  70. package/python/foxclaw/api/routes/memory.py +89 -0
  71. package/python/foxclaw/api/routes/orchestration.py +41 -0
  72. package/python/foxclaw/api/routes/recovery.py +43 -0
  73. package/python/foxclaw/api/routes/review.py +275 -0
  74. package/python/foxclaw/api/routes/search.py +37 -0
  75. package/python/foxclaw/api/routes/system.py +327 -0
  76. package/python/foxclaw/api/routes/task.py +281 -0
  77. package/python/foxclaw/api/routes/ws.py +68 -0
  78. package/python/foxclaw/api/spine.py +55 -0
  79. package/python/foxclaw/browser/__init__.py +13 -0
  80. package/python/foxclaw/browser/action/__init__.py +1 -0
  81. package/python/foxclaw/browser/action/action_executor.py +54 -0
  82. package/python/foxclaw/browser/action/action_types.py +45 -0
  83. package/python/foxclaw/browser/action/action_verifier.py +68 -0
  84. package/python/foxclaw/browser/action/url_policy.py +147 -0
  85. package/python/foxclaw/browser/adapter/__init__.py +1 -0
  86. package/python/foxclaw/browser/adapter/base_adapter.py +23 -0
  87. package/python/foxclaw/browser/adapter/web_adapter.py +46 -0
  88. package/python/foxclaw/browser/driver/__init__.py +1 -0
  89. package/python/foxclaw/browser/driver/base_driver.py +61 -0
  90. package/python/foxclaw/browser/driver/playwright_driver.py +166 -0
  91. package/python/foxclaw/browser/perception/__init__.py +1 -0
  92. package/python/foxclaw/browser/perception/dom_extractor.py +43 -0
  93. package/python/foxclaw/browser/perception/element_indexer.py +57 -0
  94. package/python/foxclaw/browser/perception/semantic_reducer.py +33 -0
  95. package/python/foxclaw/browser/perception/snapshot.py +29 -0
  96. package/python/foxclaw/browser/service.py +148 -0
  97. package/python/foxclaw/browser/session/__init__.py +1 -0
  98. package/python/foxclaw/browser/session/session_manager.py +57 -0
  99. package/python/foxclaw/capability/__init__.py +12 -0
  100. package/python/foxclaw/capability/embedding_service.py +67 -0
  101. package/python/foxclaw/channels/__init__.py +140 -0
  102. package/python/foxclaw/channels/discord.py +184 -0
  103. package/python/foxclaw/channels/dispatcher.py +204 -0
  104. package/python/foxclaw/channels/feishu.py +225 -0
  105. package/python/foxclaw/channels/openclaw_bridge.py +156 -0
  106. package/python/foxclaw/channels/signing.py +187 -0
  107. package/python/foxclaw/channels/telegram.py +121 -0
  108. package/python/foxclaw/channels/weixin.py +225 -0
  109. package/python/foxclaw/cli.py +154 -0
  110. package/python/foxclaw/cognitive/__init__.py +4 -0
  111. package/python/foxclaw/cognitive/reasoner.py +82 -0
  112. package/python/foxclaw/cognitive/state.py +147 -0
  113. package/python/foxclaw/core/__init__.py +9 -0
  114. package/python/foxclaw/core/audit_chain.py +133 -0
  115. package/python/foxclaw/core/audit_sink.py +100 -0
  116. package/python/foxclaw/core/compaction.py +283 -0
  117. package/python/foxclaw/core/config.py +65 -0
  118. package/python/foxclaw/core/confirm_nonce.py +157 -0
  119. package/python/foxclaw/core/dispatch.py +27 -0
  120. package/python/foxclaw/core/envfile.py +48 -0
  121. package/python/foxclaw/core/failure_policy.py +151 -0
  122. package/python/foxclaw/core/gateway.py +85 -0
  123. package/python/foxclaw/core/governance_audit.py +175 -0
  124. package/python/foxclaw/core/idempotency.py +289 -0
  125. package/python/foxclaw/core/injection_guard.py +221 -0
  126. package/python/foxclaw/core/memory_adapter.py +516 -0
  127. package/python/foxclaw/core/metrics.py +89 -0
  128. package/python/foxclaw/core/model_router.py +1097 -0
  129. package/python/foxclaw/core/paths.py +103 -0
  130. package/python/foxclaw/core/recovery.py +362 -0
  131. package/python/foxclaw/core/scheduler.py +144 -0
  132. package/python/foxclaw/core/secrets.py +134 -0
  133. package/python/foxclaw/core/sessions.py +157 -0
  134. package/python/foxclaw/core/settings.py +430 -0
  135. package/python/foxclaw/core/shared_write.py +198 -0
  136. package/python/foxclaw/core/steering_queue.py +153 -0
  137. package/python/foxclaw/core/system_bus.py +84 -0
  138. package/python/foxclaw/core/thought_chain.py +318 -0
  139. package/python/foxclaw/core/token_meter.py +235 -0
  140. package/python/foxclaw/core/tools.py +172 -0
  141. package/python/foxclaw/core/tracing.py +390 -0
  142. package/python/foxclaw/core/truth_pipeline.py +114 -0
  143. package/python/foxclaw/core/vec_recall_adapter.py +503 -0
  144. package/python/foxclaw/core/verify.py +181 -0
  145. package/python/foxclaw/core/web_search.py +242 -0
  146. package/python/foxclaw/finance/__init__.py +15 -0
  147. package/python/foxclaw/finance/evidence.py +153 -0
  148. package/python/foxclaw/finance/fx.py +100 -0
  149. package/python/foxclaw/finance/graph.py +188 -0
  150. package/python/foxclaw/finance/models.py +150 -0
  151. package/python/foxclaw/finance/nlparse.py +575 -0
  152. package/python/foxclaw/finance/policy.py +329 -0
  153. package/python/foxclaw/finance/reasoner.py +129 -0
  154. package/python/foxclaw/finance/reasoning.py +340 -0
  155. package/python/foxclaw/finance/reconcile.py +180 -0
  156. package/python/foxclaw/finance/runtime.py +527 -0
  157. package/python/foxclaw/governance/__init__.py +24 -0
  158. package/python/foxclaw/governance/middleware.py +184 -0
  159. package/python/foxclaw/harness/__init__.py +17 -0
  160. package/python/foxclaw/harness/_base.py +72 -0
  161. package/python/foxclaw/harness/_policy.py +359 -0
  162. package/python/foxclaw/harness/drift.py +89 -0
  163. package/python/foxclaw/harness/event_bus.py +31 -0
  164. package/python/foxclaw/harness/harness.py +446 -0
  165. package/python/foxclaw/harness/loop_detection.py +152 -0
  166. package/python/foxclaw/itinerary/__init__.py +10 -0
  167. package/python/foxclaw/itinerary/amap_client.py +117 -0
  168. package/python/foxclaw/itinerary/models.py +80 -0
  169. package/python/foxclaw/itinerary/reasoner.py +101 -0
  170. package/python/foxclaw/itinerary/reasoning.py +174 -0
  171. package/python/foxclaw/itinerary/runtime.py +148 -0
  172. package/python/foxclaw/kernel/__init__.py +13 -0
  173. package/python/foxclaw/kernel/capabilities.py +73 -0
  174. package/python/foxclaw/kernel/policy_gate.py +114 -0
  175. package/python/foxclaw/kernel/process_manager.py +205 -0
  176. package/python/foxclaw/kernel/scheduler.py +131 -0
  177. package/python/foxclaw/kernel/sentinel.py +453 -0
  178. package/python/foxclaw/kernel/sentinel_client.py +246 -0
  179. package/python/foxclaw/kernel/spine.py +97 -0
  180. package/python/foxclaw/knowledge/__init__.py +376 -0
  181. package/python/foxclaw/knowledge/truth_crystallizer.py +92 -0
  182. package/python/foxclaw/learning/__init__.py +12 -0
  183. package/python/foxclaw/learning/knowledge_graph.py +149 -0
  184. package/python/foxclaw/learning/models.py +93 -0
  185. package/python/foxclaw/learning/reasoner.py +70 -0
  186. package/python/foxclaw/learning/runtime.py +363 -0
  187. package/python/foxclaw/learning/spaced_repetition.py +96 -0
  188. package/python/foxclaw/mcp_server.py +189 -0
  189. package/python/foxclaw/orchestration/__init__.py +184 -0
  190. package/python/foxclaw/pipeline/__init__.py +23 -0
  191. package/python/foxclaw/pipeline/cognitive_pipeline.py +481 -0
  192. package/python/foxclaw/pipeline/supervisor_stages.py +230 -0
  193. package/python/foxclaw/planning/__init__.py +17 -0
  194. package/python/foxclaw/planning/llm_planner.py +232 -0
  195. package/python/foxclaw/planning/planner.py +99 -0
  196. package/python/foxclaw/search/__init__.py +24 -0
  197. package/python/foxclaw/search/agent_port.py +194 -0
  198. package/python/foxclaw/search/evidence.py +113 -0
  199. package/python/foxclaw/search/external_search.py +50 -0
  200. package/python/foxclaw/search/hkc_read_port.py +60 -0
  201. package/python/foxclaw/search/internal_search.py +73 -0
  202. package/python/foxclaw/search/models.py +129 -0
  203. package/python/foxclaw/search/planner.py +44 -0
  204. package/python/foxclaw/search/retriever.py +39 -0
  205. package/python/foxclaw/search/runtime.py +134 -0
  206. package/python/foxclaw/search/strategy.py +44 -0
  207. package/python/foxclaw/skills/__init__.py +613 -0
  208. package/python/foxclaw/skills/hooks_spec.py +73 -0
  209. package/python/foxclaw/skills/lifecycle.py +278 -0
  210. package/python/foxclaw/skills/markdown_skill.py +174 -0
  211. package/python/foxclaw/skills/markdown_store.py +39 -0
  212. package/python/foxclaw/skills/mcp_client.py +190 -0
  213. package/python/foxclaw/skills/mcp_store.py +43 -0
  214. package/python/foxclaw/skills/permission.py +180 -0
  215. package/python/foxclaw/skills/pipeline.py +386 -0
  216. package/python/foxclaw/skills/policy.py +80 -0
  217. package/python/foxclaw/skills/review.py +72 -0
  218. package/python/foxclaw/skills/store.py +57 -0
  219. package/python/frontend/assets/core_bg.jpg +0 -0
  220. package/python/frontend/deck/auth.js +32 -0
  221. package/python/frontend/deck/chat_model_picker.js +537 -0
  222. package/python/frontend/deck/domain.js +296 -0
  223. package/python/frontend/deck/main.js +1950 -0
  224. package/python/frontend/deck/markdown.js +48 -0
  225. package/python/frontend/deck/settings.js +324 -0
  226. package/python/frontend/deck/state.js +6 -0
  227. package/python/frontend/deck/util.js +6 -0
  228. package/python/frontend/deck/visuals.js +212 -0
  229. package/python/frontend/deck.css +855 -0
  230. package/python/frontend/deck.html +686 -0
  231. package/python/frontend/deck_v2.html +341 -0
  232. package/python/frontend/planet/index.html +266 -0
  233. package/python/frontend/planet/planet.css +579 -0
  234. package/python/frontend/planet/planet.js +1637 -0
  235. package/python/frontend/planet/vendor/three.core.min.js +6 -0
  236. package/python/frontend/planet/vendor/three.module.min.js +6 -0
  237. package/python/frontend/v3/assets/index-BD7VPbMV.js +33 -0
  238. package/python/frontend/v3/assets/index-CAvnYWGH.css +1 -0
  239. package/python/frontend/v3/index.html +15 -0
  240. package/python/frontend/widget_runtime.js +295 -0
  241. package/python/frontend/widgets.js +696 -0
  242. package/python/frontend_src/index.html +14 -0
  243. package/python/frontend_src/package-lock.json +1376 -0
  244. package/python/frontend_src/package.json +19 -0
  245. package/python/frontend_src/src/App.vue +62 -0
  246. package/python/frontend_src/src/api.js +89 -0
  247. package/python/frontend_src/src/components/ChatDock.vue +377 -0
  248. package/python/frontend_src/src/components/ComposerMenu.vue +75 -0
  249. package/python/frontend_src/src/components/DocViewer.vue +171 -0
  250. package/python/frontend_src/src/components/Inspector.vue +241 -0
  251. package/python/frontend_src/src/components/LeftRail.vue +394 -0
  252. package/python/frontend_src/src/components/MenuPopover.vue +130 -0
  253. package/python/frontend_src/src/components/MessageStream.vue +401 -0
  254. package/python/frontend_src/src/components/ModelPicker.vue +355 -0
  255. package/python/frontend_src/src/components/QuickIngest.vue +118 -0
  256. package/python/frontend_src/src/components/Stage.vue +226 -0
  257. package/python/frontend_src/src/components/ToolMenu.vue +144 -0
  258. package/python/frontend_src/src/components/TopBar.vue +90 -0
  259. package/python/frontend_src/src/components/panels/ActivityPanel.vue +84 -0
  260. package/python/frontend_src/src/components/panels/AgentsPanel.vue +86 -0
  261. package/python/frontend_src/src/components/panels/KnowledgePanel.vue +441 -0
  262. package/python/frontend_src/src/components/panels/MemoryPanel.vue +118 -0
  263. package/python/frontend_src/src/components/panels/PanelBox.vue +51 -0
  264. package/python/frontend_src/src/components/panels/ReviewPanel.vue +308 -0
  265. package/python/frontend_src/src/components/panels/SettingsPanel.vue +254 -0
  266. package/python/frontend_src/src/components/panels/TasksPanel.vue +74 -0
  267. package/python/frontend_src/src/components/panels/ToolsPanel.vue +640 -0
  268. package/python/frontend_src/src/components/panels/UsagePanel.vue +105 -0
  269. package/python/frontend_src/src/lib/fmt.js +48 -0
  270. package/python/frontend_src/src/lib/md.js +204 -0
  271. package/python/frontend_src/src/main.js +7 -0
  272. package/python/frontend_src/src/stores/app.js +106 -0
  273. package/python/frontend_src/src/stores/chat.js +484 -0
  274. package/python/frontend_src/src/stores/model.js +104 -0
  275. package/python/frontend_src/src/stores/panels.js +330 -0
  276. package/python/frontend_src/src/styles/base.css +138 -0
  277. package/python/frontend_src/src/styles/panel.css +117 -0
  278. package/python/frontend_src/src/styles/tokens.css +78 -0
  279. package/python/frontend_src/vite.config.js +46 -0
  280. package/python/pyproject.toml +265 -0
  281. package/python/skills/README.md +123 -0
  282. package/python/skills/execution-contract/SKILL.md +30 -0
  283. package/python/skills/general-assistant/SKILL.md +34 -0
  284. package/python/skills/planner/SKILL.md +33 -0
  285. package/python/skills/schedule-itinerary/SKILL.md +35 -0
  286. package/python/skills/skill-composition/SKILL.md +31 -0
  287. package/python/skills/skill-guard/SKILL.md +67 -0
  288. package/python/skills/skill-memory/SKILL.md +50 -0
  289. package/python/skills/skill-router/SKILL.md +35 -0
  290. package/python/tests/conftest.py +76 -0
  291. package/python/tests/run_all.py +263 -0
  292. package/python/tests/test_agent_lifecycle.py +360 -0
  293. package/python/tests/test_ahs_p2.py +414 -0
  294. package/python/tests/test_anthropic_tools.py +206 -0
  295. package/python/tests/test_api.py +87 -0
  296. package/python/tests/test_api_auth.py +83 -0
  297. package/python/tests/test_api_host_guard.py +69 -0
  298. package/python/tests/test_attachments.py +167 -0
  299. package/python/tests/test_audit_chain.py +121 -0
  300. package/python/tests/test_audit_sink.py +136 -0
  301. package/python/tests/test_browser.py +153 -0
  302. package/python/tests/test_browser_request_guard.py +184 -0
  303. package/python/tests/test_capabilities.py +158 -0
  304. package/python/tests/test_channel_signing.py +192 -0
  305. package/python/tests/test_channel_trust.py +109 -0
  306. package/python/tests/test_channels.py +184 -0
  307. package/python/tests/test_cli.py +46 -0
  308. package/python/tests/test_compaction_toolpair.py +119 -0
  309. package/python/tests/test_config_layering.py +65 -0
  310. package/python/tests/test_confirm_nonce.py +197 -0
  311. package/python/tests/test_context_compaction_wiring.py +130 -0
  312. package/python/tests/test_dispatch.py +95 -0
  313. package/python/tests/test_domain_evals.py +403 -0
  314. package/python/tests/test_domain_import.py +78 -0
  315. package/python/tests/test_domain_reasoners.py +148 -0
  316. package/python/tests/test_e2e.py +178 -0
  317. package/python/tests/test_emb_cache.py +107 -0
  318. package/python/tests/test_embedding_singleton.py +89 -0
  319. package/python/tests/test_envfile.py +83 -0
  320. package/python/tests/test_evals_smoke.py +114 -0
  321. package/python/tests/test_event_spine.py +182 -0
  322. package/python/tests/test_evidence_semantic.py +101 -0
  323. package/python/tests/test_evidence_sse.py +97 -0
  324. package/python/tests/test_file_ingest.py +146 -0
  325. package/python/tests/test_finance.py +153 -0
  326. package/python/tests/test_finance_ledger.py +252 -0
  327. package/python/tests/test_finance_persist.py +178 -0
  328. package/python/tests/test_gateway.py +144 -0
  329. package/python/tests/test_governance.py +468 -0
  330. package/python/tests/test_governance_audit.py +359 -0
  331. package/python/tests/test_governance_middleware.py +143 -0
  332. package/python/tests/test_hkc.py +286 -0
  333. package/python/tests/test_hkc_index_build_manager.py +339 -0
  334. package/python/tests/test_hkc_manifest_reuse.py +317 -0
  335. package/python/tests/test_hkc_search_event_sync.py +533 -0
  336. package/python/tests/test_import_path_guard.py +161 -0
  337. package/python/tests/test_injection_guard.py +211 -0
  338. package/python/tests/test_integration.py +141 -0
  339. package/python/tests/test_itinerary.py +103 -0
  340. package/python/tests/test_kee_semantic.py +89 -0
  341. package/python/tests/test_kernel.py +222 -0
  342. package/python/tests/test_learning.py +110 -0
  343. package/python/tests/test_llm_admin.py +160 -0
  344. package/python/tests/test_llm_planner.py +366 -0
  345. package/python/tests/test_markdown_skills.py +73 -0
  346. package/python/tests/test_markdown_skills_openclaw.py +71 -0
  347. package/python/tests/test_mcp_connectors.py +109 -0
  348. package/python/tests/test_mcp_guardian_discovery.py +91 -0
  349. package/python/tests/test_mcp_no_memory.py +43 -0
  350. package/python/tests/test_mcp_policy_gate.py +77 -0
  351. package/python/tests/test_mcp_server.py +95 -0
  352. package/python/tests/test_memory_recall.py +95 -0
  353. package/python/tests/test_metrics.py +66 -0
  354. package/python/tests/test_multi_chain.py +102 -0
  355. package/python/tests/test_multimodal.py +135 -0
  356. package/python/tests/test_negotiation.py +146 -0
  357. package/python/tests/test_orchestration.py +119 -0
  358. package/python/tests/test_orchestration_decoupled.py +109 -0
  359. package/python/tests/test_p3_p5_hardening.py +338 -0
  360. package/python/tests/test_parallel_dispatch.py +215 -0
  361. package/python/tests/test_pipeline.py +533 -0
  362. package/python/tests/test_plan_visible.py +86 -0
  363. package/python/tests/test_planner.py +95 -0
  364. package/python/tests/test_policy_gate.py +109 -0
  365. package/python/tests/test_reasoner.py +185 -0
  366. package/python/tests/test_release_check.py +263 -0
  367. package/python/tests/test_resume_checkpoint.py +156 -0
  368. package/python/tests/test_routing.py +95 -0
  369. package/python/tests/test_scheduler.py +141 -0
  370. package/python/tests/test_search.py +204 -0
  371. package/python/tests/test_secrets.py +159 -0
  372. package/python/tests/test_semantic.py +78 -0
  373. package/python/tests/test_sessions.py +91 -0
  374. package/python/tests/test_settings.py +281 -0
  375. package/python/tests/test_shadow_learning.py +330 -0
  376. package/python/tests/test_shared_write.py +207 -0
  377. package/python/tests/test_skill_taint_gate.py +154 -0
  378. package/python/tests/test_skills.py +133 -0
  379. package/python/tests/test_stats_and_events.py +165 -0
  380. package/python/tests/test_streaming.py +60 -0
  381. package/python/tests/test_supervisor_pipeline.py +177 -0
  382. package/python/tests/test_supervisor_triage.py +108 -0
  383. package/python/tests/test_taint.py +117 -0
  384. package/python/tests/test_task_stream.py +85 -0
  385. package/python/tests/test_thinking_sse.py +67 -0
  386. package/python/tests/test_thinking_stream.py +76 -0
  387. package/python/tests/test_tool_center.py +129 -0
  388. package/python/tests/test_tool_parallel.py +113 -0
  389. package/python/tests/test_tool_params.py +103 -0
  390. package/python/tests/test_tool_use.py +242 -0
  391. package/python/tests/test_toplevel_concurrency.py +117 -0
  392. package/python/tests/test_tracing.py +290 -0
  393. package/python/tests/test_truth_crystallizer.py +127 -0
  394. package/python/tests/test_truth_flow.py +93 -0
  395. package/python/tests/test_url_policy.py +53 -0
  396. package/python/tests/test_verify_evidence.py +225 -0
  397. package/python/tests/test_version_sync.py +71 -0
  398. package/python/tests/test_web_trigger.py +99 -0
  399. package/python/tools/check_faiss_type.py +31 -0
  400. package/python/tools/check_log.ps1 +46 -0
  401. package/python/tools/ivf_status.py +72 -0
  402. package/python/tools/mypy_baseline.txt +5 -0
  403. package/python/tools/mypy_ratchet.py +97 -0
  404. package/python/tools/pack.py +178 -0
  405. package/python/tools/release_check.py +349 -0
  406. package/python/tools/restart_foxclaw.ps1 +74 -0
  407. package/python/tools/shared_write_audit.py +204 -0
  408. package/python/tools/size_baseline.txt +28 -0
  409. package/python/tools/size_check.py +102 -0
  410. package/python/tools/verify_env.bat +2 -0
  411. package/python/tools/verify_env.py +25 -0
  412. package/python/vendor/HMR_LICENSE +37 -0
  413. package/python/vendor/HMR_README.md +65 -0
  414. package/python/vendor/hkc/LICENSE +37 -0
  415. package/python/vendor/hkc/hkc_ace/__init__.py +0 -0
  416. package/python/vendor/hkc/hkc_ace/ace.py +457 -0
  417. package/python/vendor/hkc/hkc_ace/skill_taxonomy.json +465 -0
  418. package/python/vendor/hkc/hkc_api/__init__.py +0 -0
  419. package/python/vendor/hkc/hkc_api/container.py +416 -0
  420. package/python/vendor/hkc/hkc_api/ingest_jobs.py +103 -0
  421. package/python/vendor/hkc/hkc_api/main.py +157 -0
  422. package/python/vendor/hkc/hkc_api/routes/__init__.py +0 -0
  423. package/python/vendor/hkc/hkc_api/routes/ability.py +151 -0
  424. package/python/vendor/hkc/hkc_api/routes/config.py +51 -0
  425. package/python/vendor/hkc/hkc_api/routes/knowledge.py +630 -0
  426. package/python/vendor/hkc/hkc_api/routes/search.py +88 -0
  427. package/python/vendor/hkc/hkc_api/schemas.py +153 -0
  428. package/python/vendor/hkc/hkc_api/synthesis.py +179 -0
  429. package/python/vendor/hkc/hkc_core/__init__.py +0 -0
  430. package/python/vendor/hkc/hkc_core/graph/__init__.py +0 -0
  431. package/python/vendor/hkc/hkc_core/graph/base.py +188 -0
  432. package/python/vendor/hkc/hkc_core/graph/sqlite_store.py +789 -0
  433. package/python/vendor/hkc/hkc_core/models/__init__.py +0 -0
  434. package/python/vendor/hkc/hkc_core/models/enums.py +65 -0
  435. package/python/vendor/hkc/hkc_core/models/ku.py +307 -0
  436. package/python/vendor/hkc/hkc_core/utils/__init__.py +0 -0
  437. package/python/vendor/hkc/hkc_core/utils/id_gen.py +113 -0
  438. package/python/vendor/hkc/hkc_crystallizer/__init__.py +44 -0
  439. package/python/vendor/hkc/hkc_crystallizer/adapters.py +94 -0
  440. package/python/vendor/hkc/hkc_crystallizer/candidate.py +145 -0
  441. package/python/vendor/hkc/hkc_crystallizer/crystallizer.py +179 -0
  442. package/python/vendor/hkc/hkc_crystallizer/ingress.py +90 -0
  443. package/python/vendor/hkc/hkc_kde/__init__.py +0 -0
  444. package/python/vendor/hkc/hkc_kde/chunker.py +197 -0
  445. package/python/vendor/hkc/hkc_kde/extractor.py +623 -0
  446. package/python/vendor/hkc/hkc_kde/kde.py +298 -0
  447. package/python/vendor/hkc/hkc_kde/loaders/__init__.py +0 -0
  448. package/python/vendor/hkc/hkc_kde/loaders/ebook_loader.py +153 -0
  449. package/python/vendor/hkc/hkc_kde/loaders/office_loader.py +194 -0
  450. package/python/vendor/hkc/hkc_kde/loaders/pdf_loader.py +162 -0
  451. package/python/vendor/hkc/hkc_kde/loaders/text_loader.py +157 -0
  452. package/python/vendor/hkc/hkc_kde/loaders/web_loader.py +91 -0
  453. package/python/vendor/hkc/hkc_kde/models.py +74 -0
  454. package/python/vendor/hkc/hkc_kde/packager.py +329 -0
  455. package/python/vendor/hkc/hkc_kde/relation_disc.py +151 -0
  456. package/python/vendor/hkc/hkc_kee/__init__.py +0 -0
  457. package/python/vendor/hkc/hkc_kee/dedup.py +227 -0
  458. package/python/vendor/hkc/hkc_kee/fingerprint.py +106 -0
  459. package/python/vendor/hkc/hkc_kee/kee.py +541 -0
  460. package/python/vendor/hkc/hkc_kep/__init__.py +0 -0
  461. package/python/vendor/hkc/hkc_kep/event_bus.py +205 -0
  462. package/python/vendor/hkc/hkc_sdk/__init__.py +63 -0
  463. package/python/vendor/hkc/hkc_sdk/base.py +151 -0
  464. package/python/vendor/hkc/hkc_sdk/direct_client.py +153 -0
  465. package/python/vendor/hkc/hkc_sdk/exceptions.py +39 -0
  466. package/python/vendor/hkc/hkc_sdk/http_client.py +166 -0
  467. package/python/vendor/hkc/hkc_sdk/models.py +171 -0
  468. package/python/vendor/hkc/hkc_search/__init__.py +0 -0
  469. package/python/vendor/hkc/hkc_search/bm25.py +325 -0
  470. package/python/vendor/hkc/hkc_search/embedding_backends.py +374 -0
  471. package/python/vendor/hkc/hkc_search/graph_search.py +132 -0
  472. package/python/vendor/hkc/hkc_search/hybrid.py +331 -0
  473. package/python/vendor/hkc/hkc_search/index_build_manager.py +378 -0
  474. package/python/vendor/hkc/hkc_search/vector_search.py +484 -0
  475. package/python/vendor/hmr/__init__.py +0 -0
  476. package/python/vendor/hmr/core/__init__.py +0 -0
  477. package/python/vendor/hmr/core/hmr.py +684 -0
  478. package/python/vendor/hmr/core/models.py +310 -0
  479. package/python/vendor/hmr/engines/__init__.py +0 -0
  480. package/python/vendor/hmr/engines/evolution.py +710 -0
  481. package/python/vendor/hmr/engines/jit_compiler.py +472 -0
  482. package/python/vendor/hmr/engines/lifecycle.py +264 -0
  483. package/python/vendor/hmr/engines/policy.py +625 -0
  484. package/python/vendor/hmr/engines/recall.py +260 -0
  485. package/python/vendor/hmr/engines/runtime_state.py +158 -0
  486. package/python/vendor/hmr/engines/scheduler.py +323 -0
  487. package/python/vendor/hmr/engines/semantic.py +150 -0
  488. package/python/vendor/hmr/engines/temporal.py +261 -0
  489. package/python/vendor/hmr/engines/thought_chain.py +635 -0
  490. package/python/vendor/hmr/graph/__init__.py +0 -0
  491. package/python/vendor/hmr/graph/cwg.py +338 -0
  492. package/python/vendor/hmr/graph/memory_graph.py +530 -0
  493. package/python/vendor/hmr/storage/__init__.py +0 -0
  494. package/python/vendor/hmr/storage/memory_fs.py +300 -0
  495. package/python/vendor/hmr/storage/vector_store.py +520 -0
@@ -0,0 +1,684 @@
1
+ """
2
+ HMR v2.0 - 核心运行时引擎
3
+
4
+ v1.0 基础记忆存储
5
+ v1.1 持久化修复(VectorStore / SM-2 / 文件锁)
6
+ v1.5 记忆操作系统(Scheduler / JIT / Lifecycle / MemoryGraph)
7
+ v2.0 认知智能层:
8
+ + ThoughtChain Engine — 显式推理链 + 反思循环
9
+ + Memory Policy Engine — 策略学习(存/取/忘)
10
+ + Self-Evolution Engine — 模式识别 + 知识抽象 + 矛盾解决
11
+ """
12
+
13
+ from typing import List, Dict, Any, Optional
14
+ from datetime import datetime
15
+
16
+ from .models import (
17
+ MemoryObject, RuntimeState, AgentWorkspace, CognitiveNode, RecallResult
18
+ )
19
+ from ..engines.semantic import SemanticMemoryEngine
20
+ from ..engines.runtime_state import RuntimeStateEngine
21
+ from ..engines.recall import ActiveRecallEngine
22
+ from ..engines.temporal import TemporalEngine
23
+ from ..engines.jit_compiler import JITMemoryCompiler, CompileResult
24
+ from ..engines.lifecycle import MemoryLifecycleEngine, LifecycleConfig
25
+ from ..engines.scheduler import MemoryScheduler, RecallStrategy
26
+ from ..engines.thought_chain import ThoughtChainEngine, ThoughtType, ThoughtChain
27
+ from ..engines.policy import MemoryPolicyEngine
28
+ from ..engines.evolution import SelfEvolutionEngine
29
+ from ..storage.memory_fs import MemoryFS
30
+ from ..storage.vector_store import VectorStore
31
+ from ..graph.cwg import CognitiveWorkspaceGraph
32
+ from ..graph.memory_graph import MemoryGraph
33
+
34
+
35
+ class HMR:
36
+ """
37
+ Foxclaw Memory Runtime v2.0
38
+
39
+ v2.0 新增:
40
+ self.thought_chain → 推理链引擎
41
+ self.policy → 策略学习引擎
42
+ self.evolution → 自我演化引擎
43
+ """
44
+
45
+ VERSION = "2.0.0"
46
+
47
+ def __init__(
48
+ self,
49
+ storage_path: str = "./hmr_data",
50
+ embedding_model: str = None, # 【P1】默认 None → VectorStore 内部按 env 链解析
51
+ llm_api_key: Optional[str] = None,
52
+ lifecycle_config: Optional[LifecycleConfig] = None,
53
+ ):
54
+ self.storage_path = storage_path
55
+ self._llm_api_key = llm_api_key
56
+
57
+ # ── 存储层 ────────────────────────────────────────────────────────────
58
+ self.memory_fs = MemoryFS(storage_path)
59
+ self.vector_store = VectorStore(
60
+ embedding_model=embedding_model,
61
+ storage_path=f"{storage_path}/vector_store"
62
+ )
63
+
64
+ # ── v1.x 核心引擎 ─────────────────────────────────────────────────────
65
+ self.semantic_engine = SemanticMemoryEngine(
66
+ vector_store=self.vector_store, memory_fs=self.memory_fs
67
+ )
68
+ self.temporal_engine = TemporalEngine(memory_fs=self.memory_fs)
69
+ self.runtime_engine = RuntimeStateEngine(memory_fs=self.memory_fs)
70
+ self.recall_engine = ActiveRecallEngine(
71
+ semantic_engine=self.semantic_engine,
72
+ runtime_engine=self.runtime_engine,
73
+ temporal_engine=self.temporal_engine
74
+ )
75
+ self.jit_compiler = JITMemoryCompiler(
76
+ semantic_engine=self.semantic_engine,
77
+ temporal_engine=self.temporal_engine
78
+ )
79
+ self.scheduler = MemoryScheduler(
80
+ temporal_engine=self.temporal_engine,
81
+ memory_fs=self.memory_fs
82
+ )
83
+ self.lifecycle = MemoryLifecycleEngine(
84
+ memory_fs=self.memory_fs,
85
+ temporal_engine=self.temporal_engine,
86
+ config=lifecycle_config or LifecycleConfig()
87
+ )
88
+ self.lifecycle.register_compress_fn(self.compress_memories)
89
+
90
+ # ── 图层 ──────────────────────────────────────────────────────────────
91
+ self.cwg = CognitiveWorkspaceGraph(memory_fs=self.memory_fs)
92
+ self.memory_graph = MemoryGraph(storage_path=f"{storage_path}/memory_graph")
93
+
94
+ # ── v2.0 新增认知引擎 ─────────────────────────────────────────────────
95
+ self.thought_chain = ThoughtChainEngine(
96
+ storage_path=f"{storage_path}/thought_chains"
97
+ )
98
+ self.thought_chain.register_ingest_fn(self.ingest)
99
+
100
+ self.policy = MemoryPolicyEngine(
101
+ storage_path=f"{storage_path}/policy"
102
+ )
103
+
104
+ self.evolution = SelfEvolutionEngine(
105
+ storage_path=f"{storage_path}/evolution"
106
+ )
107
+ self.evolution.register_ingest_fn(self.ingest)
108
+ self.evolution.register_llm_fn(self._summarize_with_llm)
109
+
110
+ # ── 代理工作区 ────────────────────────────────────────────────────────
111
+ self.workspaces: Dict[str, AgentWorkspace] = {}
112
+ self._load_workspaces()
113
+
114
+ # ── 当前运行时 ────────────────────────────────────────────────────────
115
+ self.current_runtime: Optional[RuntimeState] = None
116
+
117
+ # ── 启动同步 ──────────────────────────────────────────────────────────
118
+ self._sync_vector_store()
119
+
120
+ # =========================================================================
121
+ # 启动同步
122
+ # =========================================================================
123
+
124
+ def _sync_vector_store(self):
125
+ all_memories = self.memory_fs.list_memories()
126
+ vs_count = len(self.vector_store.vectors)
127
+
128
+ # 换了 embedding 模型:旧向量维度与新模型不符,检索会退化成"全 0 分"。
129
+ # 这里主动发现并全量重编码,换模型就只剩「改 env + 重启」一步,
130
+ # 不用手工删索引,也不用记得清。
131
+ if vs_count and self.vector_store.purge_incompatible(
132
+ self.vector_store.embedder.dimension):
133
+ print(f"[HMR] embedding 模型已变化,重建向量索引({len(all_memories)} 条)...")
134
+ self.vector_store.rebuild_from_memories(all_memories)
135
+ return
136
+
137
+ if all_memories and vs_count == 0:
138
+ print(f"[HMR] 重建向量索引({len(all_memories)} 条)...")
139
+ self.vector_store.rebuild_from_memories(all_memories)
140
+ for mem in all_memories:
141
+ self.temporal_engine.create_temporal_state(mem)
142
+ elif all_memories:
143
+ missing = [m for m in all_memories if m.id not in self.vector_store.vectors]
144
+ if missing:
145
+ self.vector_store.rebuild_from_memories(missing)
146
+
147
+ def _load_workspaces(self):
148
+ for ws in self.memory_fs.list_workspaces():
149
+ self.workspaces[ws.agent_id] = ws
150
+
151
+ # =========================================================================
152
+ # ingest(v2.0:Policy 驱动)
153
+ # =========================================================================
154
+
155
+ def ingest(
156
+ self,
157
+ content: str,
158
+ memory_type: str = "concept",
159
+ title: Optional[str] = None,
160
+ metadata: Optional[Dict[str, Any]] = None,
161
+ use_policy: bool = False, # 是否用 Policy 自动决定类型和置信度
162
+ ) -> MemoryObject:
163
+ """
164
+ 摄入记忆。
165
+
166
+ v2.0 新增:
167
+ - use_policy=True 时,Policy 自动推断 memory_type 和 confidence
168
+ """
169
+ # Policy 驱动模式
170
+ if use_policy:
171
+ ctx = {
172
+ "title": title,
173
+ "tags": (metadata or {}).get("tags", []),
174
+ "runtime_dependencies": (metadata or {}).get("runtime_dependencies", []),
175
+ "existing_memory_count": len(self.vector_store.vectors),
176
+ }
177
+ decision = self.policy.decide_ingest(content, title, ctx.get("tags"), ctx.get("runtime_dependencies"), ctx["existing_memory_count"])
178
+ if not decision["should_store"]:
179
+ # Policy 认为不值得存,创建一个临时对象返回(不实际存储)
180
+ return MemoryObject(
181
+ type="concept", title=title or "跳过", content=content,
182
+ semantic_summary=f"[Policy跳过] {decision['reasoning']}"
183
+ )
184
+ memory_type = memory_type if memory_type != "concept" else decision["memory_type"]
185
+ if metadata is None:
186
+ metadata = {}
187
+ if "confidence" not in metadata:
188
+ metadata["confidence"] = decision["confidence"]
189
+
190
+ summary = self._generate_summary(content, title)
191
+ memory = MemoryObject(
192
+ type=memory_type,
193
+ title=title or f"{memory_type.title()} {datetime.utcnow().strftime('%H:%M')}",
194
+ content=content,
195
+ semantic_summary=summary,
196
+ )
197
+
198
+ if metadata:
199
+ for key in ["tags", "runtime_dependencies", "linked_memories", "confidence"]:
200
+ if key in metadata:
201
+ setattr(memory, key, metadata[key])
202
+
203
+ self.semantic_engine.store(memory)
204
+ self.temporal_engine.create_temporal_state(memory)
205
+
206
+ if self.current_runtime:
207
+ self._update_cwg_for_memory(memory)
208
+
209
+ try:
210
+ self.memory_graph.add_from_memory(memory)
211
+ except Exception:
212
+ pass
213
+
214
+ # v2.0:通知 Policy 记录这次摄入
215
+ self.lifecycle.on_ingest(memory)
216
+
217
+ return memory
218
+
219
+ # =========================================================================
220
+ # recall(v2.0:Policy 融合)
221
+ # =========================================================================
222
+
223
+ def recall(
224
+ self,
225
+ query: Optional[str] = None,
226
+ context: Optional[Dict[str, Any]] = None,
227
+ top_k: int = 5,
228
+ strategy: Optional[str] = None,
229
+ use_policy: bool = True, # 是否用 Policy 辅助调度决策
230
+ ) -> RecallResult:
231
+ """
232
+ 召回记忆。
233
+
234
+ v2.0 新增:
235
+ - use_policy=True 时,Policy 辅助 Scheduler 做策略决策
236
+ - 召回结果自动通知 Policy 记录(用于策略学习)
237
+ """
238
+ context = context or self._get_current_context()
239
+
240
+ # Policy 辅助决策
241
+ if use_policy and not strategy:
242
+ policy_ctx = {
243
+ **context,
244
+ "total_memories": len(self.vector_store.vectors),
245
+ }
246
+ pd = self.policy.decide_recall(query or "", policy_ctx, base_top_k=top_k)
247
+ if not strategy:
248
+ strategy = pd["strategy"]
249
+ top_k = pd["top_k"]
250
+
251
+ plan = self.scheduler.schedule(query, context, hint=strategy)
252
+
253
+ cache_key = f"{query}|{context.get('active_goal', '')}|{plan.top_k}"
254
+ cached = self.scheduler.get_from_cache(cache_key)
255
+ if cached:
256
+ return RecallResult(
257
+ memory_objects=cached,
258
+ recall_reasoning=f"[Cache] {plan.reasoning}",
259
+ predicted_need=[query or ""],
260
+ relevance_scores={m.id: 1.0 for m in cached}
261
+ )
262
+
263
+ if plan.use_jit:
264
+ cr: CompileResult = self.jit_compiler.compile(
265
+ query=query or context.get("active_goal", ""),
266
+ context=context, top_k=plan.top_k, max_steps=plan.jit_max_steps
267
+ )
268
+ memories, scores = cr.memories, cr.relevance_scores
269
+ reasoning = f"[{plan.strategy.value.upper()}] {cr.reasoning}"
270
+ else:
271
+ r = self.recall_engine.recall(query=query, context=context, top_k=plan.top_k)
272
+ memories, scores = r.memory_objects, r.relevance_scores
273
+ reasoning = f"[{plan.strategy.value.upper()}] {r.recall_reasoning}"
274
+
275
+ # Graph 增强
276
+ if plan.strategy in (RecallStrategy.GRAPH, RecallStrategy.HYBRID):
277
+ graph_ids = self.memory_graph.get_memory_ids_for_query(
278
+ query or context.get("active_goal", "")
279
+ )
280
+ seen = {m.id for m in memories}
281
+ for mid in graph_ids:
282
+ if mid not in seen and len(memories) < plan.top_k * 2:
283
+ mem = self.memory_fs.read_memory(mid)
284
+ if mem:
285
+ memories.append(mem)
286
+ scores[mid] = 0.5
287
+ seen.add(mid)
288
+ memories.sort(key=lambda m: scores.get(m.id, 0), reverse=True)
289
+ memories = memories[:plan.top_k]
290
+
291
+ if plan.cache_result:
292
+ self.scheduler.put_to_cache(cache_key, memories)
293
+
294
+ return RecallResult(
295
+ memory_objects=memories,
296
+ recall_reasoning=reasoning,
297
+ predicted_need=[query or ""] + list(context.get("focus_areas", [])),
298
+ relevance_scores=scores
299
+ )
300
+
301
+ # =========================================================================
302
+ # v2.0 新增:ThoughtChain API
303
+ # =========================================================================
304
+
305
+ def start_thinking(
306
+ self,
307
+ goal: str,
308
+ agent_id: Optional[str] = None,
309
+ expected_outcome: Optional[str] = None,
310
+ ) -> ThoughtChain:
311
+ """
312
+ 开始一条新的思维链。
313
+
314
+ 示例:
315
+ chain = hmr.start_thinking("为什么调度器超时")
316
+ hmr.think(chain.chain_id, "IPC 队列深度 > 1000", ThoughtType.OBSERVATION)
317
+ hmr.think(chain.chain_id, "可能是消费者速度不够", ThoughtType.HYPOTHESIS)
318
+ hmr.think(chain.chain_id, "增加消费者线程", ThoughtType.DECISION)
319
+ hmr.think(chain.chain_id, "已部署 3 个消费者", ThoughtType.ACTION)
320
+ result = hmr.reflect_on(chain.chain_id, "延迟降低 80%", rating=0.9)
321
+ """
322
+ runtime_id = self.current_runtime.runtime_id if self.current_runtime else None
323
+ return self.thought_chain.create_chain(
324
+ goal=goal, agent_id=agent_id,
325
+ runtime_id=runtime_id, expected_outcome=expected_outcome
326
+ )
327
+
328
+ def think(
329
+ self,
330
+ chain_id: str,
331
+ content: str,
332
+ thought_type: ThoughtType,
333
+ confidence: float = 0.7,
334
+ memory_ids: Optional[List[str]] = None,
335
+ ):
336
+ """向思维链追加一个思维节点"""
337
+ return self.thought_chain.add_thought(
338
+ chain_id=chain_id, content=content,
339
+ thought_type=thought_type, confidence=confidence,
340
+ memory_ids=memory_ids or []
341
+ )
342
+
343
+ def reflect_on(
344
+ self,
345
+ chain_id: str,
346
+ actual_outcome: str,
347
+ rating: Optional[float] = None,
348
+ ):
349
+ """
350
+ 对思维链执行反思,自动评估推理质量并存储洞察。
351
+ rating: 0-1,1=完全成功,0=完全失败(不传则自动推断)
352
+ """
353
+ return self.thought_chain.reflect(chain_id, actual_outcome, rating)
354
+
355
+ def best_decision_for(self, goal: str) -> Optional[str]:
356
+ """从历史成功链中查找同类问题的最优决策"""
357
+ return self.thought_chain.get_best_decision_for(goal)
358
+
359
+ # =========================================================================
360
+ # v2.0 新增:Policy Feedback API
361
+ # =========================================================================
362
+
363
+ def feedback(
364
+ self,
365
+ event_type: str,
366
+ memory_ids: List[str],
367
+ signal: float,
368
+ context: Optional[Dict[str, Any]] = None,
369
+ query: Optional[str] = None,
370
+ strategy: Optional[str] = None,
371
+ ):
372
+ """
373
+ 向策略引擎提供反馈,让系统从结果中学习。
374
+
375
+ event_type:
376
+ "recall_hit" — 召回结果有用(signal > 0)
377
+ "recall_miss" — 召回结果无用(signal < 0)
378
+ "task_success" — 任务成功(signal 0.5~1.0)
379
+ "task_failure" — 任务失败(signal -0.5~-1.0)
380
+ "compress_gain" — 压缩提升了召回质量(signal > 0)
381
+
382
+ 示例:
383
+ result = hmr.recall(query="调度器超时原因")
384
+ # ... 使用召回结果 ...
385
+ hmr.feedback(
386
+ "recall_hit",
387
+ memory_ids=[m.id for m in result.memory_objects[:2]],
388
+ signal=0.8,
389
+ query="调度器超时原因",
390
+ strategy="jit"
391
+ )
392
+ """
393
+ self.policy.record_feedback(
394
+ event_type=event_type,
395
+ memory_ids=memory_ids,
396
+ signal=signal,
397
+ context=context or {},
398
+ query=query,
399
+ strategy=strategy,
400
+ )
401
+
402
+ # =========================================================================
403
+ # v2.0 新增:Self-Evolution API
404
+ # =========================================================================
405
+
406
+ def evolve(self, dry_run: bool = False) -> Dict[str, Any]:
407
+ """
408
+ 运行一次自我演化周期。
409
+
410
+ dry_run=True:只分析,不修改(先预览再决定)
411
+
412
+ 示例:
413
+ # 先预览
414
+ report = hmr.evolve(dry_run=True)
415
+ print(report["summary"])
416
+ for action in report["actions"]:
417
+ print(f" {action}")
418
+
419
+ # 确认后执行
420
+ hmr.evolve(dry_run=False)
421
+ """
422
+ system_stats = self.get_system_status()
423
+ return self.evolution.evolve(
424
+ memories=self.memory_fs.list_memories(),
425
+ vector_store=self.vector_store,
426
+ memory_fs=self.memory_fs,
427
+ system_stats=system_stats,
428
+ dry_run=dry_run,
429
+ )
430
+
431
+ # =========================================================================
432
+ # Runtime State
433
+ # =========================================================================
434
+
435
+ def save_runtime_state(
436
+ self,
437
+ goal: Optional[str] = None,
438
+ plan: Optional[List[str]] = None,
439
+ context: Optional[Dict[str, Any]] = None
440
+ ) -> RuntimeState:
441
+ state = RuntimeState(
442
+ active_goal=goal, current_plan=plan or [],
443
+ current_context=context or {},
444
+ active_agents=list(self.workspaces.keys()),
445
+ )
446
+ sm2_data = self.temporal_engine.export_sm2_states()
447
+ state.current_context["__sm2_states__"] = sm2_data
448
+ self.runtime_engine.save_state(state)
449
+ self.current_runtime = state
450
+ return state
451
+
452
+ def restore_runtime_state(self, runtime_id: Optional[str] = None) -> Optional[RuntimeState]:
453
+ state = self.runtime_engine.restore_state(runtime_id)
454
+ if state:
455
+ self.current_runtime = state
456
+ sm2_data = state.current_context.pop("__sm2_states__", {})
457
+ if sm2_data:
458
+ self.temporal_engine.import_sm2_states(sm2_data)
459
+ self._preload_runtime_memories(state)
460
+ return state
461
+
462
+ # =========================================================================
463
+ # Workspace
464
+ # =========================================================================
465
+
466
+ def get_workspace(self, agent_id: str, create: bool = True) -> Optional[AgentWorkspace]:
467
+ if agent_id not in self.workspaces:
468
+ if not create:
469
+ return None
470
+ ws = AgentWorkspace(agent_id=agent_id)
471
+ self.workspaces[agent_id] = ws
472
+ self.memory_fs.write_workspace(ws)
473
+ return self.workspaces.get(agent_id)
474
+
475
+ def save_workspace(self, agent_id: str):
476
+ ws = self.workspaces.get(agent_id)
477
+ if ws:
478
+ self.memory_fs.write_workspace(ws)
479
+
480
+ # =========================================================================
481
+ # Snapshot
482
+ # =========================================================================
483
+
484
+ def snapshot(self) -> Dict[str, Any]:
485
+ return {
486
+ "version": self.VERSION,
487
+ "current_runtime": self.current_runtime.snapshot() if self.current_runtime else None,
488
+ "workspaces": {aid: ws.model_dump() for aid, ws in self.workspaces.items()},
489
+ "sm2_states": self.temporal_engine.export_sm2_states(),
490
+ "timestamp": datetime.utcnow().isoformat()
491
+ }
492
+
493
+ def restore_snapshot(self, snapshot: Dict[str, Any]):
494
+ if snapshot.get("current_runtime"):
495
+ self.current_runtime = RuntimeState.restore(snapshot["current_runtime"])
496
+ for agent_id, ws_data in snapshot.get("workspaces", {}).items():
497
+ ws = AgentWorkspace(**ws_data)
498
+ self.workspaces[agent_id] = ws
499
+ self.memory_fs.write_workspace(ws)
500
+ if snapshot.get("sm2_states"):
501
+ self.temporal_engine.import_sm2_states(snapshot["sm2_states"])
502
+
503
+ # =========================================================================
504
+ # compress_memories
505
+ # =========================================================================
506
+
507
+ def compress_memories(
508
+ self,
509
+ memory_ids: Optional[List[str]] = None,
510
+ memory_type: Optional[str] = None,
511
+ max_memories: int = 20
512
+ ) -> Optional[MemoryObject]:
513
+ if memory_ids:
514
+ memories = [self.memory_fs.read_memory(mid) for mid in memory_ids]
515
+ memories = [m for m in memories if m]
516
+ elif memory_type:
517
+ memories = self.memory_fs.list_memories(memory_type)
518
+ else:
519
+ memories = self.memory_fs.list_memories()
520
+
521
+ memories = [m for m in memories if m.temporal_weight < 0.5 or m.access_count > 5][:max_memories]
522
+ if len(memories) < 2:
523
+ return None
524
+
525
+ combined = "\n\n---\n\n".join([
526
+ f"[{m.type}] {m.title}:\n{m.content[:300]}" for m in memories
527
+ ])
528
+ abstract = self._compress_with_llm(combined, memories) or self._compress_with_tfidf(memories)
529
+ all_tags = list(set(t for m in memories for t in m.tags))
530
+ all_deps = list(set(d for m in memories for d in m.runtime_dependencies))
531
+
532
+ compressed = self.ingest(
533
+ content=abstract, memory_type="concept",
534
+ title=f"[压缩] {self._extract_topic(memories)}",
535
+ metadata={"tags": all_tags[:10] + ["compressed"], "runtime_dependencies": all_deps[:5], "confidence": 0.85}
536
+ )
537
+
538
+ for m in memories:
539
+ m.confidence = max(0.1, m.confidence * 0.5)
540
+ if "compressed_source" not in m.tags:
541
+ m.tags.append("compressed_source")
542
+ self.memory_fs.write_memory(m)
543
+
544
+ # 通知 Policy
545
+ self.policy.record_feedback("compress_gain", [compressed.id], 0.6)
546
+ return compressed
547
+
548
+ # =========================================================================
549
+ # 系统状态(v2.0 扩展)
550
+ # =========================================================================
551
+
552
+ def get_system_status(self) -> Dict[str, Any]:
553
+ fs_stats = self.memory_fs.get_statistics()
554
+ vs_stats = self.vector_store.get_stats()
555
+ lc_stats = self.lifecycle.get_lifecycle_stats()
556
+ graph_stats = self.memory_graph.get_stats()
557
+ scheduler_stats = self.scheduler.get_stats()
558
+ overdue = self.temporal_engine.get_forgetting_schedule()
559
+
560
+ return {
561
+ "version": self.VERSION,
562
+ "memory_fs": fs_stats,
563
+ "vector_store": vs_stats,
564
+ "synced": fs_stats["total_memories"] <= vs_stats["total_vectors"],
565
+ "lifecycle": lc_stats,
566
+ "memory_graph": graph_stats,
567
+ "scheduler": scheduler_stats,
568
+ # v2.0 新增
569
+ "thought_chain": self.thought_chain.get_stats(),
570
+ "policy": self.policy.get_stats(),
571
+ "evolution": self.evolution.get_stats(),
572
+ # 通用
573
+ "active_runtime": self.current_runtime.runtime_id if self.current_runtime else None,
574
+ "active_workspaces":len(self.workspaces),
575
+ "overdue_reviews": len([x for x in overdue if x["overdue_days"] > 0]),
576
+ "embedding_provider": vs_stats.get("embedding_provider", "unknown"),
577
+ }
578
+
579
+ # =========================================================================
580
+ # 辅助方法
581
+ # =========================================================================
582
+
583
+ def _get_current_context(self) -> Dict[str, Any]:
584
+ return {
585
+ "runtime_id": self.current_runtime.runtime_id if self.current_runtime else None,
586
+ "active_goal": self.current_runtime.active_goal if self.current_runtime else None,
587
+ "focus_areas": self.current_runtime.focus_areas if self.current_runtime else [],
588
+ "pending_tasks": self.current_runtime.pending_tasks if self.current_runtime else [],
589
+ "active_agents": list(self.workspaces.keys()),
590
+ }
591
+
592
+ def _update_cwg_for_memory(self, memory: MemoryObject):
593
+ node = CognitiveNode(
594
+ type="memory", content=memory.title,
595
+ metadata={"memory_id": memory.id, "memory_type": memory.type}
596
+ )
597
+ self.cwg.add_node(node)
598
+ if self.current_runtime:
599
+ self.cwg.link_to_runtime(node.node_id, self.current_runtime.runtime_id)
600
+
601
+ def _preload_runtime_memories(self, state: RuntimeState):
602
+ if state.active_goal:
603
+ result = self.recall(query=state.active_goal, context={
604
+ "active_goal": state.active_goal,
605
+ "focus_areas": state.focus_areas,
606
+ "pending_tasks": state.pending_tasks
607
+ })
608
+ state.active_memory_ids = [m.id for m in result.memory_objects]
609
+
610
+ def _generate_summary(self, content: str, title: Optional[str] = None) -> str:
611
+ if len(content) > 200:
612
+ s = self._summarize_with_llm(content)
613
+ if s:
614
+ return s
615
+ return self._summarize_with_keywords(content, title)
616
+
617
+ def _summarize_with_llm(self, content: str) -> Optional[str]:
618
+ try:
619
+ import openai, os
620
+ key = self._llm_api_key or os.environ.get("OPENAI_API_KEY", "")
621
+ if not key:
622
+ return None
623
+ client = openai.OpenAI(api_key=key)
624
+ resp = client.chat.completions.create(
625
+ model="gpt-4o-mini",
626
+ messages=[{"role": "user", "content": f"用一句话(30字以内)概括核心要点:\n\n{content[:1000]}"}],
627
+ max_tokens=60
628
+ )
629
+ return resp.choices[0].message.content.strip()
630
+ except Exception:
631
+ return None
632
+
633
+ def _summarize_with_keywords(self, content: str, title: Optional[str] = None) -> str:
634
+ import re
635
+ from collections import Counter
636
+ stop = {"的","了","是","在","我","有","和","就","不","人","都",
637
+ "a","an","the","is","are","to","of","and","in","for","that","this"}
638
+ words = re.findall(r'\b\w{2,}\b', content.lower())
639
+ kws = [w for w in words if w not in stop]
640
+ top = [w for w, _ in Counter(kws).most_common(8)]
641
+ prefix = f"[{title}] " if title else ""
642
+ candidate = f"{prefix}核心:{', '.join(top)}" if top else content[:80]
643
+ return candidate if len(candidate) < len(content) else content[:80] + "..."
644
+
645
+ def _compress_with_llm(self, combined: str, memories) -> Optional[str]:
646
+ try:
647
+ import openai, os
648
+ key = self._llm_api_key or os.environ.get("OPENAI_API_KEY", "")
649
+ if not key:
650
+ return None
651
+ client = openai.OpenAI(api_key=key)
652
+ resp = client.chat.completions.create(
653
+ model="gpt-4o-mini",
654
+ messages=[{"role": "user", "content":
655
+ f"从以下{len(memories)}条记忆中提取核心规律(200字以内):\n{combined[:3000]}"}],
656
+ max_tokens=300
657
+ )
658
+ return resp.choices[0].message.content.strip()
659
+ except Exception:
660
+ return None
661
+
662
+ def _compress_with_tfidf(self, memories) -> str:
663
+ import re
664
+ from collections import Counter
665
+ stop = {"的","了","是","在","a","an","the","is","to","of","and","in","for"}
666
+ all_text = " ".join(m.content for m in memories)
667
+ words = re.findall(r'\b\w{2,}\b', all_text.lower())
668
+ kws = [w for w in words if w not in stop]
669
+ top = [w for w, _ in Counter(kws).most_common(15)]
670
+ types = list(set(m.type for m in memories))
671
+ return (
672
+ f"涵盖 {len(memories)} 条记忆(类型:{', '.join(types)})。\n"
673
+ f"核心关键词:{', '.join(top)}。"
674
+ )
675
+
676
+ def _extract_topic(self, memories) -> str:
677
+ import re
678
+ from collections import Counter
679
+ words = [w for m in memories for w in re.findall(r'\b\w{3,}\b', m.title)]
680
+ return " + ".join(w for w, _ in Counter(words).most_common(2)) if words else "混合记忆"
681
+
682
+
683
+ def create_hmr(storage_path: str = "./hmr_data", llm_api_key: Optional[str] = None) -> HMR:
684
+ return HMR(storage_path=storage_path, llm_api_key=llm_api_key)