principles-disciple 1.8.0 → 1.8.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 (460) hide show
  1. package/ADVANCED_CONFIG_ZH.md +97 -0
  2. package/AGENT_INSTALL.md +173 -0
  3. package/AGENT_INSTALL_EN.md +173 -0
  4. package/INSTALL.md +256 -0
  5. package/SKILL.md +63 -0
  6. package/docs/COMMAND_REFERENCE.md +76 -0
  7. package/docs/COMMAND_REFERENCE_EN.md +79 -0
  8. package/esbuild.config.js +75 -0
  9. package/openclaw.plugin.json +6 -1
  10. package/package.json +13 -15
  11. package/scripts/build-web.mjs +46 -0
  12. package/scripts/install-dependencies.cjs +47 -0
  13. package/scripts/sync-plugin.mjs +802 -0
  14. package/scripts/verify-build.mjs +109 -0
  15. package/src/agents/nocturnal-dreamer.md +152 -0
  16. package/src/agents/nocturnal-philosopher.md +138 -0
  17. package/src/agents/nocturnal-reflector.md +126 -0
  18. package/src/agents/nocturnal-scribe.md +164 -0
  19. package/src/commands/capabilities.ts +85 -0
  20. package/{dist/commands/context.js → src/commands/context.ts} +78 -38
  21. package/src/commands/evolution-status.ts +146 -0
  22. package/src/commands/export.ts +111 -0
  23. package/src/commands/focus.ts +533 -0
  24. package/src/commands/nocturnal-review.ts +311 -0
  25. package/src/commands/nocturnal-rollout.ts +763 -0
  26. package/src/commands/nocturnal-train.ts +1002 -0
  27. package/{dist/commands/pain.js → src/commands/pain.ts} +68 -49
  28. package/src/commands/principle-rollback.ts +27 -0
  29. package/{dist/commands/rollback.js → src/commands/rollback.ts} +44 -12
  30. package/src/commands/samples.ts +60 -0
  31. package/src/commands/strategy.ts +38 -0
  32. package/{dist/commands/thinking-os.js → src/commands/thinking-os.ts} +59 -36
  33. package/src/commands/workflow-debug.ts +128 -0
  34. package/{dist/config/defaults/runtime.js → src/config/defaults/runtime.ts} +12 -5
  35. package/src/config/errors.ts +163 -0
  36. package/{dist/config/index.d.ts → src/config/index.ts} +2 -1
  37. package/src/constants/diagnostician.ts +66 -0
  38. package/src/constants/tools.ts +62 -0
  39. package/src/core/adaptive-thresholds.ts +476 -0
  40. package/{dist/core/config-service.js → src/core/config-service.ts} +7 -4
  41. package/{dist/core/config.js → src/core/config.ts} +158 -46
  42. package/src/core/control-ui-db.ts +435 -0
  43. package/{dist/core/detection-funnel.js → src/core/detection-funnel.ts} +36 -21
  44. package/{dist/core/detection-service.js → src/core/detection-service.ts} +7 -4
  45. package/{dist/core/dictionary-service.js → src/core/dictionary-service.ts} +7 -4
  46. package/{dist/core/dictionary.js → src/core/dictionary.ts} +57 -34
  47. package/src/core/empathy-keyword-matcher.ts +327 -0
  48. package/src/core/empathy-types.ts +218 -0
  49. package/src/core/event-log.ts +544 -0
  50. package/src/core/evolution-engine.ts +612 -0
  51. package/src/core/evolution-logger.ts +353 -0
  52. package/src/core/evolution-migration.ts +77 -0
  53. package/src/core/evolution-reducer.ts +731 -0
  54. package/src/core/evolution-types.ts +456 -0
  55. package/src/core/external-training-contract.ts +527 -0
  56. package/src/core/focus-history.ts +1458 -0
  57. package/src/core/hygiene/tracker.ts +117 -0
  58. package/{dist/core/init.js → src/core/init.ts} +39 -26
  59. package/src/core/local-worker-routing.ts +617 -0
  60. package/{dist/core/migration.js → src/core/migration.ts} +18 -11
  61. package/src/core/model-deployment-registry.ts +722 -0
  62. package/src/core/model-training-registry.ts +813 -0
  63. package/src/core/nocturnal-arbiter.ts +706 -0
  64. package/src/core/nocturnal-candidate-scoring.ts +392 -0
  65. package/src/core/nocturnal-compliance.ts +1075 -0
  66. package/src/core/nocturnal-dataset.ts +668 -0
  67. package/src/core/nocturnal-executability.ts +428 -0
  68. package/src/core/nocturnal-export.ts +390 -0
  69. package/{dist/core/nocturnal-paths.js → src/core/nocturnal-paths.ts} +49 -23
  70. package/src/core/nocturnal-trajectory-extractor.ts +484 -0
  71. package/src/core/nocturnal-trinity.ts +1384 -0
  72. package/src/core/pain.ts +122 -0
  73. package/{dist/core/path-resolver.js → src/core/path-resolver.ts} +157 -36
  74. package/{dist/core/paths.js → src/core/paths.ts} +13 -4
  75. package/src/core/principle-training-state.ts +450 -0
  76. package/src/core/profile.ts +226 -0
  77. package/src/core/promotion-gate.ts +822 -0
  78. package/{dist/core/risk-calculator.js → src/core/risk-calculator.ts} +42 -16
  79. package/{dist/core/session-tracker.js → src/core/session-tracker.ts} +185 -63
  80. package/src/core/shadow-observation-registry.ts +534 -0
  81. package/{dist/core/system-logger.js → src/core/system-logger.ts} +9 -5
  82. package/src/core/thinking-models.ts +217 -0
  83. package/src/core/training-program.ts +630 -0
  84. package/src/core/trajectory-types.ts +243 -0
  85. package/src/core/trajectory.ts +1673 -0
  86. package/{dist/core/workspace-context.js → src/core/workspace-context.ts} +57 -32
  87. package/src/hooks/bash-risk.ts +171 -0
  88. package/src/hooks/edit-verification.ts +295 -0
  89. package/src/hooks/gate-block-helper.ts +160 -0
  90. package/src/hooks/gate.ts +210 -0
  91. package/src/hooks/gfi-gate.ts +177 -0
  92. package/src/hooks/lifecycle.ts +326 -0
  93. package/{dist/hooks/llm.js → src/hooks/llm.ts} +166 -139
  94. package/src/hooks/message-sanitize.ts +45 -0
  95. package/src/hooks/pain.ts +384 -0
  96. package/src/hooks/progressive-trust-gate.ts +174 -0
  97. package/src/hooks/prompt.ts +920 -0
  98. package/src/hooks/subagent.ts +207 -0
  99. package/src/hooks/thinking-checkpoint.ts +73 -0
  100. package/src/hooks/trajectory-collector.ts +290 -0
  101. package/src/http/principles-console-route.ts +716 -0
  102. package/src/i18n/commands.ts +117 -0
  103. package/src/index.ts +694 -0
  104. package/src/service/central-database.ts +831 -0
  105. package/src/service/control-ui-query-service.ts +888 -0
  106. package/src/service/evolution-query-service.ts +405 -0
  107. package/src/service/evolution-worker.ts +1646 -0
  108. package/src/service/health-query-service.ts +836 -0
  109. package/{dist/service/nocturnal-runtime.js → src/service/nocturnal-runtime.ts} +263 -36
  110. package/src/service/nocturnal-service.ts +1015 -0
  111. package/src/service/nocturnal-target-selector.ts +532 -0
  112. package/src/service/phase3-input-filter.ts +237 -0
  113. package/src/service/runtime-summary-service.ts +757 -0
  114. package/src/service/subagent-workflow/deep-reflect-workflow-manager.ts +513 -0
  115. package/src/service/subagent-workflow/empathy-observer-workflow-manager.ts +603 -0
  116. package/src/service/subagent-workflow/index.ts +51 -0
  117. package/src/service/subagent-workflow/nocturnal-workflow-manager.ts +856 -0
  118. package/src/service/subagent-workflow/runtime-direct-driver.ts +166 -0
  119. package/src/service/subagent-workflow/types.ts +378 -0
  120. package/src/service/subagent-workflow/workflow-store.ts +328 -0
  121. package/src/service/trajectory-service.ts +15 -0
  122. package/{dist/tools/critique-prompt.js → src/tools/critique-prompt.ts} +25 -8
  123. package/src/tools/deep-reflect.ts +349 -0
  124. package/{dist/tools/model-index.js → src/tools/model-index.ts} +33 -17
  125. package/src/types/event-types.ts +453 -0
  126. package/src/types/hygiene-types.ts +31 -0
  127. package/src/types/principle-tree-schema.ts +244 -0
  128. package/src/types/runtime-summary.ts +49 -0
  129. package/src/types.ts +74 -0
  130. package/src/utils/file-lock.ts +391 -0
  131. package/{dist/utils/glob-match.js → src/utils/glob-match.ts} +21 -20
  132. package/{dist/utils/hashing.js → src/utils/hashing.ts} +6 -4
  133. package/src/utils/io.ts +110 -0
  134. package/{dist/utils/nlp.js → src/utils/nlp.ts} +19 -12
  135. package/{dist/utils/plugin-logger.js → src/utils/plugin-logger.ts} +33 -8
  136. package/src/utils/subagent-probe.ts +94 -0
  137. package/templates/langs/zh/skills/pd-diagnostician/SKILL.md +70 -1
  138. package/templates/pain_settings.json +2 -1
  139. package/tests/README.md +120 -0
  140. package/tests/build-artifacts.test.ts +111 -0
  141. package/tests/commands/evolution-status.test.ts +222 -0
  142. package/tests/commands/evolver.test.ts +22 -0
  143. package/tests/commands/export.test.ts +78 -0
  144. package/tests/commands/nocturnal-review.test.ts +448 -0
  145. package/tests/commands/nocturnal-train.test.ts +97 -0
  146. package/tests/commands/pain.test.ts +108 -0
  147. package/tests/commands/samples.test.ts +65 -0
  148. package/tests/commands/strategy.test.ts +34 -0
  149. package/tests/commands/thinking-os.test.ts +88 -0
  150. package/tests/core/adaptive-thresholds.test.ts +261 -0
  151. package/tests/core/config-service.test.ts +89 -0
  152. package/tests/core/config.test.ts +90 -0
  153. package/tests/core/control-ui-db.test.ts +75 -0
  154. package/tests/core/core-template-guidance.test.ts +21 -0
  155. package/tests/core/detection-funnel.test.ts +63 -0
  156. package/tests/core/detection-service.test.ts +50 -0
  157. package/tests/core/dictionary-service.test.ts +116 -0
  158. package/tests/core/dictionary.test.ts +168 -0
  159. package/tests/core/empathy-keyword-matcher.test.ts +209 -0
  160. package/tests/core/event-log.test.ts +181 -0
  161. package/tests/core/evolution-e2e.test.ts +58 -0
  162. package/tests/core/evolution-engine-gate-integration.test.ts +543 -0
  163. package/tests/core/evolution-engine.test.ts +562 -0
  164. package/tests/core/evolution-logger.test.ts +148 -0
  165. package/tests/core/evolution-migration.test.ts +50 -0
  166. package/tests/core/evolution-paths.test.ts +21 -0
  167. package/tests/core/evolution-reducer.detector-metadata.test.ts +602 -0
  168. package/tests/core/evolution-reducer.test.ts +180 -0
  169. package/tests/core/evolution-types-loop.test.ts +48 -0
  170. package/tests/core/evolution-user-stories.e2e.test.ts +249 -0
  171. package/tests/core/external-training-contract.test.ts +463 -0
  172. package/tests/core/focus-history.test.ts +682 -0
  173. package/tests/core/init-flatten.test.ts +69 -0
  174. package/tests/core/init-refactor.test.ts +87 -0
  175. package/tests/core/init-v1.3.test.ts +46 -0
  176. package/tests/core/init.test.ts +190 -0
  177. package/tests/core/local-worker-routing.test.ts +757 -0
  178. package/tests/core/migration.test.ts +84 -0
  179. package/tests/core/model-deployment-registry.test.ts +845 -0
  180. package/tests/core/model-training-registry.test.ts +889 -0
  181. package/tests/core/nocturnal-arbiter.test.ts +494 -0
  182. package/tests/core/nocturnal-candidate-scoring.test.ts +400 -0
  183. package/tests/core/nocturnal-compliance.test.ts +646 -0
  184. package/tests/core/nocturnal-dataset.test.ts +892 -0
  185. package/tests/core/nocturnal-executability.test.ts +357 -0
  186. package/tests/core/nocturnal-export.test.ts +462 -0
  187. package/tests/core/nocturnal-reviewed-subset-comparison.test.ts +428 -0
  188. package/tests/core/nocturnal-trajectory-extractor.test.ts +634 -0
  189. package/tests/core/nocturnal-trinity.test.ts +953 -0
  190. package/tests/core/pain.test.ts +33 -0
  191. package/tests/core/path-resolver.test.ts +57 -0
  192. package/tests/core/paths-refactor.test.ts +42 -0
  193. package/tests/core/phase7-rollout-integration.test.ts +477 -0
  194. package/tests/core/principle-training-state.test.ts +712 -0
  195. package/tests/core/profile.test.ts +56 -0
  196. package/tests/core/promotion-gate.test.ts +556 -0
  197. package/tests/core/risk-calculator.test.ts +168 -0
  198. package/tests/core/session-tracker.test.ts +191 -0
  199. package/tests/core/training-program.test.ts +472 -0
  200. package/tests/core/trajectory.test.ts +265 -0
  201. package/tests/core/workspace-context-factory.test.ts +18 -0
  202. package/tests/core/workspace-context.test.ts +134 -0
  203. package/tests/fixtures/nocturnal-reviewed-subset.json +183 -0
  204. package/tests/fixtures/production-compatibility.test.ts +147 -0
  205. package/tests/fixtures/production-mock-generator.ts +282 -0
  206. package/tests/hooks/bash-risk-integration.test.ts +137 -0
  207. package/tests/hooks/bash-risk.test.ts +81 -0
  208. package/tests/hooks/edit-verification.test.ts +678 -0
  209. package/tests/hooks/gate-edit-verification-p1.test.ts +632 -0
  210. package/tests/hooks/gate-edit-verification.test.ts +435 -0
  211. package/tests/hooks/gate-pipeline-integration.test.ts +404 -0
  212. package/tests/hooks/gate.test.ts +271 -0
  213. package/tests/hooks/gfi-gate-unit.test.ts +422 -0
  214. package/tests/hooks/gfi-gate.test.ts +669 -0
  215. package/tests/hooks/lifecycle.test.ts +248 -0
  216. package/tests/hooks/llm.test.ts +308 -0
  217. package/tests/hooks/message-sanitize.test.ts +36 -0
  218. package/tests/hooks/pain.test.ts +141 -0
  219. package/tests/hooks/progressive-trust-gate.test.ts +277 -0
  220. package/tests/hooks/prompt.test.ts +1411 -0
  221. package/tests/hooks/subagent.test.ts +467 -0
  222. package/tests/hooks/thinking-gate.test.ts +313 -0
  223. package/tests/http/principles-console-route.test.ts +140 -0
  224. package/tests/hygiene-tracker.test.ts +77 -0
  225. package/tests/index.integration.test.ts +179 -0
  226. package/tests/index.shadow-routing.integration.test.ts +140 -0
  227. package/tests/index.test.ts +9 -0
  228. package/tests/integration/empathy-workflow-integration.test.ts +627 -0
  229. package/tests/service/control-ui-query-service.test.ts +121 -0
  230. package/tests/service/empathy-observer-workflow-manager.test.ts +176 -0
  231. package/tests/service/evolution-worker.test.ts +585 -0
  232. package/tests/service/nocturnal-runtime.test.ts +470 -0
  233. package/tests/service/nocturnal-service.test.ts +577 -0
  234. package/tests/service/nocturnal-target-selector.test.ts +615 -0
  235. package/tests/service/nocturnal-workflow-manager.test.ts +439 -0
  236. package/tests/service/phase3-input-filter.test.ts +289 -0
  237. package/tests/service/runtime-summary-service.test.ts +919 -0
  238. package/tests/task-compliance.test.ts +166 -0
  239. package/tests/test-utils.ts +48 -0
  240. package/tests/tools/critique-prompt.test.ts +260 -0
  241. package/tests/tools/deep-reflect.test.ts +232 -0
  242. package/tests/tools/model-index.test.ts +246 -0
  243. package/tests/ui/app.test.tsx +114 -0
  244. package/tests/utils/file-lock.test.ts +407 -0
  245. package/tests/utils/hashing.test.ts +32 -0
  246. package/tests/utils/io.test.ts +39 -0
  247. package/tests/utils/nlp.test.ts +53 -0
  248. package/tests/utils/plugin-logger.test.ts +156 -0
  249. package/tsconfig.json +16 -0
  250. package/tsconfig.tsbuildinfo +1 -0
  251. package/ui/src/App.tsx +45 -0
  252. package/ui/src/api.ts +216 -0
  253. package/ui/src/charts.tsx +586 -0
  254. package/ui/src/components/ErrorState.tsx +6 -0
  255. package/ui/src/components/Loading.tsx +13 -0
  256. package/ui/src/components/ProtectedRoute.tsx +12 -0
  257. package/ui/src/components/Shell.tsx +91 -0
  258. package/ui/src/components/WorkspaceConfig.tsx +146 -0
  259. package/ui/src/components/index.ts +5 -0
  260. package/ui/src/context/auth.tsx +80 -0
  261. package/ui/src/context/theme.tsx +66 -0
  262. package/ui/src/hooks/useAutoRefresh.ts +39 -0
  263. package/ui/src/i18n/ui.ts +363 -0
  264. package/ui/src/main.tsx +16 -0
  265. package/ui/src/pages/EvolutionPage.tsx +352 -0
  266. package/ui/src/pages/FeedbackPage.tsx +140 -0
  267. package/ui/src/pages/GateMonitorPage.tsx +136 -0
  268. package/ui/src/pages/LoginPage.tsx +88 -0
  269. package/ui/src/pages/OverviewPage.tsx +238 -0
  270. package/ui/src/pages/SamplesPage.tsx +174 -0
  271. package/ui/src/pages/ThinkingModelsPage.tsx +127 -0
  272. package/ui/src/styles.css +1661 -0
  273. package/ui/src/types.ts +368 -0
  274. package/ui/src/utils/format.ts +15 -0
  275. package/vitest.config.ts +23 -0
  276. package/dist/commands/capabilities.d.ts +0 -3
  277. package/dist/commands/capabilities.js +0 -73
  278. package/dist/commands/context.d.ts +0 -5
  279. package/dist/commands/evolution-status.d.ts +0 -4
  280. package/dist/commands/evolution-status.js +0 -117
  281. package/dist/commands/evolver.d.ts +0 -9
  282. package/dist/commands/evolver.js +0 -26
  283. package/dist/commands/export.d.ts +0 -2
  284. package/dist/commands/export.js +0 -98
  285. package/dist/commands/focus.d.ts +0 -14
  286. package/dist/commands/focus.js +0 -457
  287. package/dist/commands/nocturnal-review.d.ts +0 -24
  288. package/dist/commands/nocturnal-review.js +0 -265
  289. package/dist/commands/nocturnal-rollout.d.ts +0 -27
  290. package/dist/commands/nocturnal-rollout.js +0 -671
  291. package/dist/commands/nocturnal-train.d.ts +0 -25
  292. package/dist/commands/nocturnal-train.js +0 -919
  293. package/dist/commands/pain.d.ts +0 -5
  294. package/dist/commands/principle-rollback.d.ts +0 -4
  295. package/dist/commands/principle-rollback.js +0 -22
  296. package/dist/commands/rollback.d.ts +0 -19
  297. package/dist/commands/samples.d.ts +0 -2
  298. package/dist/commands/samples.js +0 -55
  299. package/dist/commands/strategy.d.ts +0 -3
  300. package/dist/commands/strategy.js +0 -29
  301. package/dist/commands/thinking-os.d.ts +0 -2
  302. package/dist/config/defaults/runtime.d.ts +0 -40
  303. package/dist/config/errors.d.ts +0 -84
  304. package/dist/config/errors.js +0 -94
  305. package/dist/config/index.js +0 -7
  306. package/dist/constants/diagnostician.d.ts +0 -12
  307. package/dist/constants/diagnostician.js +0 -56
  308. package/dist/constants/tools.d.ts +0 -17
  309. package/dist/constants/tools.js +0 -54
  310. package/dist/core/adaptive-thresholds.d.ts +0 -186
  311. package/dist/core/adaptive-thresholds.js +0 -300
  312. package/dist/core/config-service.d.ts +0 -15
  313. package/dist/core/config.d.ts +0 -127
  314. package/dist/core/control-ui-db.d.ts +0 -95
  315. package/dist/core/control-ui-db.js +0 -292
  316. package/dist/core/detection-funnel.d.ts +0 -33
  317. package/dist/core/detection-service.d.ts +0 -15
  318. package/dist/core/dictionary-service.d.ts +0 -15
  319. package/dist/core/dictionary.d.ts +0 -38
  320. package/dist/core/event-log.d.ts +0 -82
  321. package/dist/core/event-log.js +0 -463
  322. package/dist/core/evolution-engine.d.ts +0 -118
  323. package/dist/core/evolution-engine.js +0 -464
  324. package/dist/core/evolution-logger.d.ts +0 -137
  325. package/dist/core/evolution-logger.js +0 -256
  326. package/dist/core/evolution-migration.d.ts +0 -5
  327. package/dist/core/evolution-migration.js +0 -65
  328. package/dist/core/evolution-reducer.d.ts +0 -98
  329. package/dist/core/evolution-reducer.js +0 -465
  330. package/dist/core/evolution-types.d.ts +0 -287
  331. package/dist/core/evolution-types.js +0 -78
  332. package/dist/core/external-training-contract.d.ts +0 -276
  333. package/dist/core/external-training-contract.js +0 -269
  334. package/dist/core/focus-history.d.ts +0 -210
  335. package/dist/core/focus-history.js +0 -1185
  336. package/dist/core/hygiene/tracker.d.ts +0 -22
  337. package/dist/core/hygiene/tracker.js +0 -106
  338. package/dist/core/init.d.ts +0 -12
  339. package/dist/core/local-worker-routing.d.ts +0 -175
  340. package/dist/core/local-worker-routing.js +0 -525
  341. package/dist/core/migration.d.ts +0 -6
  342. package/dist/core/model-deployment-registry.d.ts +0 -218
  343. package/dist/core/model-deployment-registry.js +0 -503
  344. package/dist/core/model-training-registry.d.ts +0 -295
  345. package/dist/core/model-training-registry.js +0 -475
  346. package/dist/core/nocturnal-arbiter.d.ts +0 -159
  347. package/dist/core/nocturnal-arbiter.js +0 -534
  348. package/dist/core/nocturnal-candidate-scoring.d.ts +0 -137
  349. package/dist/core/nocturnal-candidate-scoring.js +0 -266
  350. package/dist/core/nocturnal-compliance.d.ts +0 -175
  351. package/dist/core/nocturnal-compliance.js +0 -824
  352. package/dist/core/nocturnal-dataset.d.ts +0 -224
  353. package/dist/core/nocturnal-dataset.js +0 -443
  354. package/dist/core/nocturnal-executability.d.ts +0 -85
  355. package/dist/core/nocturnal-executability.js +0 -331
  356. package/dist/core/nocturnal-export.d.ts +0 -124
  357. package/dist/core/nocturnal-export.js +0 -275
  358. package/dist/core/nocturnal-paths.d.ts +0 -124
  359. package/dist/core/nocturnal-trajectory-extractor.d.ts +0 -242
  360. package/dist/core/nocturnal-trajectory-extractor.js +0 -307
  361. package/dist/core/nocturnal-trinity.d.ts +0 -311
  362. package/dist/core/nocturnal-trinity.js +0 -880
  363. package/dist/core/pain.d.ts +0 -4
  364. package/dist/core/pain.js +0 -70
  365. package/dist/core/path-resolver.d.ts +0 -46
  366. package/dist/core/paths.d.ts +0 -65
  367. package/dist/core/principle-training-state.d.ts +0 -121
  368. package/dist/core/principle-training-state.js +0 -321
  369. package/dist/core/profile.d.ts +0 -62
  370. package/dist/core/profile.js +0 -210
  371. package/dist/core/promotion-gate.d.ts +0 -238
  372. package/dist/core/promotion-gate.js +0 -529
  373. package/dist/core/risk-calculator.d.ts +0 -22
  374. package/dist/core/session-tracker.d.ts +0 -99
  375. package/dist/core/shadow-observation-registry.d.ts +0 -217
  376. package/dist/core/shadow-observation-registry.js +0 -308
  377. package/dist/core/system-logger.d.ts +0 -8
  378. package/dist/core/thinking-models.d.ts +0 -38
  379. package/dist/core/thinking-models.js +0 -170
  380. package/dist/core/training-program.d.ts +0 -233
  381. package/dist/core/training-program.js +0 -433
  382. package/dist/core/trajectory.d.ts +0 -411
  383. package/dist/core/trajectory.js +0 -1307
  384. package/dist/core/workspace-context.d.ts +0 -71
  385. package/dist/hooks/bash-risk.d.ts +0 -57
  386. package/dist/hooks/bash-risk.js +0 -137
  387. package/dist/hooks/edit-verification.d.ts +0 -62
  388. package/dist/hooks/edit-verification.js +0 -256
  389. package/dist/hooks/gate-block-helper.d.ts +0 -44
  390. package/dist/hooks/gate-block-helper.js +0 -119
  391. package/dist/hooks/gate.d.ts +0 -24
  392. package/dist/hooks/gate.js +0 -173
  393. package/dist/hooks/gfi-gate.d.ts +0 -40
  394. package/dist/hooks/gfi-gate.js +0 -113
  395. package/dist/hooks/lifecycle.d.ts +0 -5
  396. package/dist/hooks/lifecycle.js +0 -284
  397. package/dist/hooks/llm.d.ts +0 -12
  398. package/dist/hooks/message-sanitize.d.ts +0 -3
  399. package/dist/hooks/message-sanitize.js +0 -37
  400. package/dist/hooks/pain.d.ts +0 -5
  401. package/dist/hooks/pain.js +0 -301
  402. package/dist/hooks/progressive-trust-gate.d.ts +0 -51
  403. package/dist/hooks/progressive-trust-gate.js +0 -89
  404. package/dist/hooks/prompt.d.ts +0 -47
  405. package/dist/hooks/prompt.js +0 -884
  406. package/dist/hooks/subagent.d.ts +0 -10
  407. package/dist/hooks/subagent.js +0 -387
  408. package/dist/hooks/thinking-checkpoint.d.ts +0 -37
  409. package/dist/hooks/thinking-checkpoint.js +0 -51
  410. package/dist/hooks/trajectory-collector.d.ts +0 -32
  411. package/dist/hooks/trajectory-collector.js +0 -256
  412. package/dist/http/principles-console-route.d.ts +0 -9
  413. package/dist/http/principles-console-route.js +0 -567
  414. package/dist/i18n/commands.d.ts +0 -26
  415. package/dist/i18n/commands.js +0 -116
  416. package/dist/index.d.ts +0 -7
  417. package/dist/index.js +0 -581
  418. package/dist/service/central-database.d.ts +0 -104
  419. package/dist/service/central-database.js +0 -649
  420. package/dist/service/control-ui-query-service.d.ts +0 -221
  421. package/dist/service/control-ui-query-service.js +0 -543
  422. package/dist/service/empathy-observer-manager.d.ts +0 -52
  423. package/dist/service/empathy-observer-manager.js +0 -229
  424. package/dist/service/evolution-query-service.d.ts +0 -155
  425. package/dist/service/evolution-query-service.js +0 -258
  426. package/dist/service/evolution-worker.d.ts +0 -101
  427. package/dist/service/evolution-worker.js +0 -974
  428. package/dist/service/nocturnal-runtime.d.ts +0 -183
  429. package/dist/service/nocturnal-service.d.ts +0 -163
  430. package/dist/service/nocturnal-service.js +0 -787
  431. package/dist/service/nocturnal-target-selector.d.ts +0 -145
  432. package/dist/service/nocturnal-target-selector.js +0 -315
  433. package/dist/service/phase3-input-filter.d.ts +0 -73
  434. package/dist/service/phase3-input-filter.js +0 -172
  435. package/dist/service/runtime-summary-service.d.ts +0 -122
  436. package/dist/service/runtime-summary-service.js +0 -485
  437. package/dist/service/trajectory-service.d.ts +0 -2
  438. package/dist/service/trajectory-service.js +0 -15
  439. package/dist/tools/critique-prompt.d.ts +0 -14
  440. package/dist/tools/deep-reflect.d.ts +0 -39
  441. package/dist/tools/deep-reflect.js +0 -350
  442. package/dist/tools/model-index.d.ts +0 -9
  443. package/dist/types/event-types.d.ts +0 -306
  444. package/dist/types/event-types.js +0 -106
  445. package/dist/types/hygiene-types.d.ts +0 -20
  446. package/dist/types/hygiene-types.js +0 -12
  447. package/dist/types/runtime-summary.d.ts +0 -47
  448. package/dist/types/runtime-summary.js +0 -1
  449. package/dist/types.d.ts +0 -50
  450. package/dist/types.js +0 -22
  451. package/dist/utils/file-lock.d.ts +0 -71
  452. package/dist/utils/file-lock.js +0 -309
  453. package/dist/utils/glob-match.d.ts +0 -28
  454. package/dist/utils/hashing.d.ts +0 -9
  455. package/dist/utils/io.d.ts +0 -6
  456. package/dist/utils/io.js +0 -106
  457. package/dist/utils/nlp.d.ts +0 -9
  458. package/dist/utils/plugin-logger.d.ts +0 -39
  459. package/dist/utils/subagent-probe.d.ts +0 -34
  460. package/dist/utils/subagent-probe.js +0 -81
@@ -0,0 +1,122 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { serializeKvLines, parseKvLines } from '../utils/io.js';
4
+ import { resolvePdPath } from './paths.js';
5
+ import { ConfigService } from './config-service.js';
6
+
7
+ export function computePainScore(rc: number, isSpiral: boolean, missingTestCommand: boolean, softScore: number, projectDir?: string): number {
8
+ let score = Math.max(0, softScore || 0);
9
+
10
+ const stateDir = projectDir ? resolvePdPath(projectDir, 'STATE_DIR') : undefined;
11
+ const config = stateDir ? ConfigService.get(stateDir) : null;
12
+ const scoreSettings = config ? config.get('scores') : {
13
+ exit_code_penalty: 70,
14
+ spiral_penalty: 40,
15
+ missing_test_command_penalty: 30
16
+ };
17
+
18
+ if (rc !== 0) {
19
+ score += scoreSettings.exit_code_penalty;
20
+ }
21
+
22
+ if (isSpiral) {
23
+ score += scoreSettings.spiral_penalty;
24
+ }
25
+
26
+ if (missingTestCommand) {
27
+ score += scoreSettings.missing_test_command_penalty;
28
+ }
29
+
30
+ return Math.min(100, score);
31
+ }
32
+
33
+ export function painSeverityLabel(painScore: number, isSpiral: boolean = false, projectDir?: string): string {
34
+ if (isSpiral) {
35
+ return "critical";
36
+ }
37
+
38
+ const stateDir = projectDir ? resolvePdPath(projectDir, 'STATE_DIR') : undefined;
39
+ const config = stateDir ? ConfigService.get(stateDir) : null;
40
+ const thresholds = config ? config.get('severity_thresholds') : {
41
+ high: 70,
42
+ medium: 40,
43
+ low: 20
44
+ };
45
+
46
+ if (painScore >= thresholds.high) {
47
+ return "high";
48
+ } else if (painScore >= thresholds.medium) {
49
+ return "medium";
50
+ } else if (painScore >= thresholds.low) {
51
+ return "low";
52
+ } else {
53
+ return "info";
54
+ }
55
+ }
56
+
57
+ export function writePainFlag(projectDir: string, painData: Record<string, string>): void {
58
+ const painFlagPath = resolvePdPath(projectDir, 'PAIN_FLAG');
59
+ const dir = path.dirname(painFlagPath);
60
+ if (!fs.existsSync(dir)) {
61
+ fs.mkdirSync(dir, { recursive: true });
62
+ }
63
+ fs.writeFileSync(painFlagPath, serializeKvLines(painData), "utf-8");
64
+ }
65
+
66
+ export function readPainFlagData(projectDir: string): Record<string, string> {
67
+ const painFlagPath = resolvePdPath(projectDir, 'PAIN_FLAG');
68
+ try {
69
+ if (!fs.existsSync(painFlagPath)) {
70
+ return {};
71
+ }
72
+ const content = fs.readFileSync(painFlagPath, "utf-8");
73
+ return parseKvLines(content);
74
+ } catch (e) {
75
+ return {};
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Track principle value metrics when a pain signal is written.
81
+ * This is observation-only — it does NOT affect the pain flag write flow.
82
+ * If any principle matches the pain signal, its painPreventedCount is incremented.
83
+ * Errors are silently ignored to avoid disrupting the pain pipeline.
84
+ */
85
+ export function trackPrincipleValue(
86
+ workspaceDir: string,
87
+ painData: { reason?: string; source?: string; score?: string },
88
+ getActivePrinciples: () => Array<{
89
+ id: string;
90
+ trigger: string;
91
+ valueMetrics?: { painPreventedCount: number; lastPainPreventedAt?: string; calculatedAt: string };
92
+ }>,
93
+ updatePrincipleMetrics: (id: string, metrics: { painPreventedCount: number; lastPainPreventedAt: string; calculatedAt: string }) => void,
94
+ ): void {
95
+ try {
96
+ const activePrinciples = getActivePrinciples();
97
+ if (!activePrinciples.length) return;
98
+
99
+ const painText = `${painData.reason || ''} ${painData.source || ''}`.toLowerCase();
100
+ const now = new Date().toISOString();
101
+
102
+ for (const principle of activePrinciples) {
103
+ const triggerPattern = principle.trigger?.toLowerCase();
104
+ if (!triggerPattern) continue;
105
+
106
+ // Simple keyword match — if any word from the trigger appears in the pain text
107
+ const triggerWords = triggerPattern.split(/[\s|\\.+*?()[\]{}^$-]+/).filter((w) => w.length > 2);
108
+ const matchCount = triggerWords.filter((word) => painText.includes(word)).length;
109
+
110
+ if (matchCount >= 2 || (triggerWords.length <= 3 && matchCount >= 1)) {
111
+ const currentMetrics = principle.valueMetrics ?? { painPreventedCount: 0, calculatedAt: now };
112
+ updatePrincipleMetrics(principle.id, {
113
+ painPreventedCount: currentMetrics.painPreventedCount + 1,
114
+ lastPainPreventedAt: now,
115
+ calculatedAt: now,
116
+ });
117
+ }
118
+ }
119
+ } catch {
120
+ // Observation only — never disrupt the pain pipeline
121
+ }
122
+ }
@@ -1,14 +1,29 @@
1
1
  import * as path from 'path';
2
2
  import * as os from 'os';
3
3
  import * as fs from 'fs';
4
+ import type { OpenClawPluginApi } from '../openclaw-sdk.js';
4
5
  import { PathResolutionError } from '../config/index.js';
6
+
7
+ export interface PathResolverOptions {
8
+ workspaceDir?: string;
9
+ normalizeWorkspace?: boolean;
10
+ logger?: {
11
+ debug?: (msg: string) => void;
12
+ info?: (msg: string) => void;
13
+ warn?: (msg: string) => void;
14
+ error?: (msg: string) => void;
15
+ };
16
+ }
17
+
5
18
  const DEFAULT_WORKSPACE_SUBPATH = '.openclaw/workspace';
19
+
6
20
  export const PD_ENV_VARS = {
7
21
  WORKSPACE_DIR: 'PD_WORKSPACE_DIR',
8
22
  STATE_DIR: 'PD_STATE_DIR',
9
23
  DEBUG: 'DEBUG',
10
- };
11
- export const PD_ENV_DESCRIPTIONS = {
24
+ } as const;
25
+
26
+ export const PD_ENV_DESCRIPTIONS: Record<keyof typeof PD_ENV_VARS, { desc: string; example: string }> = {
12
27
  WORKSPACE_DIR: {
13
28
  desc: 'Override the default workspace directory',
14
29
  example: 'PD_WORKSPACE_DIR=/home/user/my-workspace',
@@ -22,34 +37,47 @@ export const PD_ENV_DESCRIPTIONS = {
22
37
  example: 'DEBUG=true',
23
38
  },
24
39
  };
25
- export function printEnvVarHelp() {
40
+
41
+ export function printEnvVarHelp(): void {
26
42
  console.log('\n📁 Principles Disciple - Environment Variables\n');
27
43
  console.log('You can customize directory paths using environment variables:\n');
44
+
28
45
  for (const [key, varName] of Object.entries(PD_ENV_VARS)) {
29
- const info = PD_ENV_DESCRIPTIONS[key];
46
+ const info = PD_ENV_DESCRIPTIONS[key as keyof typeof PD_ENV_VARS];
30
47
  console.log(` ${varName}`);
31
48
  console.log(` Description: ${info.desc}`);
32
49
  console.log(` Example: ${info.example}`);
33
50
  console.log();
34
51
  }
52
+
35
53
  console.log('💡 Tips:');
36
54
  console.log(' - These can be set in your shell profile (~/.bashrc, ~/.zshrc)');
37
55
  console.log(' - Or passed when starting OpenClaw gateway');
38
56
  console.log(' - Example: PD_WORKSPACE_DIR=/custom/path openclaw-gateway start\n');
39
57
  }
58
+
59
+ export interface PDConfig {
60
+ workspace?: string;
61
+ state?: string;
62
+ debug?: boolean;
63
+ }
64
+
40
65
  const PD_CONFIG_FILE = 'principles-disciple.json';
41
66
  const PD_CONFIG_LOCATIONS = [
42
67
  path.join(process.cwd(), PD_CONFIG_FILE),
43
68
  path.join(os.homedir(), '.openclaw', PD_CONFIG_FILE),
44
69
  path.join(os.homedir(), '.principles', PD_CONFIG_FILE),
45
70
  ];
46
- function isWindowsPath(inputPath) {
71
+
72
+ function isWindowsPath(inputPath: string): boolean {
47
73
  return /^[A-Za-z]:[\\/]/.test(inputPath) || inputPath.startsWith('\\\\');
48
74
  }
49
- function isPosixAbsolutePath(inputPath) {
75
+
76
+ function isPosixAbsolutePath(inputPath: string): boolean {
50
77
  return inputPath.startsWith('/');
51
78
  }
52
- function getPathApi(inputPath) {
79
+
80
+ function getPathApi(inputPath: string): typeof path.posix | typeof path.win32 | typeof path {
53
81
  if (isWindowsPath(inputPath)) {
54
82
  return path.win32;
55
83
  }
@@ -58,7 +86,8 @@ function getPathApi(inputPath) {
58
86
  }
59
87
  return path;
60
88
  }
61
- function findConfigFile() {
89
+
90
+ function findConfigFile(): string | null {
62
91
  for (const loc of PD_CONFIG_LOCATIONS) {
63
92
  if (fs.existsSync(loc)) {
64
93
  return loc;
@@ -66,30 +95,36 @@ function findConfigFile() {
66
95
  }
67
96
  return null;
68
97
  }
69
- function loadConfigFromFile() {
98
+
99
+ /* eslint-disable no-console */
100
+ function loadConfigFromFile(): PDConfig | null {
70
101
  const configPath = findConfigFile();
71
102
  if (!configPath) {
72
103
  return null;
73
104
  }
105
+
74
106
  try {
75
107
  const content = fs.readFileSync(configPath, 'utf8');
76
108
  const config = JSON.parse(content);
77
109
  console.log(`[PD:PathResolver] Loaded config from: ${configPath}`);
78
110
  return config;
79
- }
80
- catch (e) {
111
+ } catch (e) {
81
112
  console.warn(`[PD:PathResolver] Failed to load config from ${configPath}: ${String(e)}`);
82
113
  return null;
83
114
  }
84
115
  }
116
+ /* eslint-enable no-console */
117
+
85
118
  export class PathResolver {
86
- static extensionRoot = null;
87
- workspaceDir = null;
88
- stateDir = null;
89
- logger;
90
- normalizeWorkspace;
91
- initialized = false;
92
- static setExtensionRoot(extensionRootPath) {
119
+ private static extensionRoot: string | null = null;
120
+ private workspaceDir: string | null = null;
121
+ private stateDir: string | null = null;
122
+ private readonly logger?: PathResolverOptions['logger'];
123
+ private readonly normalizeWorkspace: boolean;
124
+ private initialized = false;
125
+
126
+
127
+ static setExtensionRoot(extensionRootPath: string): void {
93
128
  if (!extensionRootPath || !extensionRootPath.trim()) {
94
129
  return;
95
130
  }
@@ -97,12 +132,15 @@ export class PathResolver {
97
132
  const pathApi = getPathApi(trimmed);
98
133
  PathResolver.extensionRoot = pathApi.normalize(trimmed);
99
134
  }
100
- static getExtensionRoot() {
135
+
136
+ static getExtensionRoot(): string | null {
101
137
  return PathResolver.extensionRoot;
102
138
  }
103
- constructor(options = {}) {
139
+
140
+ constructor(options: PathResolverOptions = {}) {
104
141
  this.logger = options.logger;
105
142
  this.normalizeWorkspace = options.normalizeWorkspace ?? true;
143
+
106
144
  if (options.workspaceDir) {
107
145
  const original = options.workspaceDir;
108
146
  const normalized = this.normalizeWorkspace ? this.normalizePath(original) : original;
@@ -113,53 +151,64 @@ export class PathResolver {
113
151
  this.initialized = true;
114
152
  }
115
153
  }
116
- log(level, msg) {
154
+
155
+ private log(level: 'debug' | 'info' | 'warn' | 'error', msg: string): void {
117
156
  const prefix = '[PD:PathResolver]';
118
157
  const fullMsg = `${prefix} ${msg}`;
158
+
119
159
  if (process.env.DEBUG === 'true') {
120
- console.debug(fullMsg);
160
+ this.logger?.debug?.(fullMsg);
121
161
  }
162
+
122
163
  switch (level) {
123
164
  case 'debug':
124
165
  this.logger?.debug?.(fullMsg);
125
166
  break;
126
167
  case 'info':
127
- this.logger?.info?.(fullMsg) || console.log(fullMsg);
168
+ this.logger?.info?.(fullMsg);
128
169
  break;
129
170
  case 'warn':
130
- this.logger?.warn?.(fullMsg) || console.warn(fullMsg);
171
+ this.logger?.warn?.(fullMsg);
131
172
  break;
132
173
  case 'error':
133
- this.logger?.error?.(fullMsg) || console.error(fullMsg);
174
+ this.logger?.error?.(fullMsg);
134
175
  break;
135
176
  }
136
177
  }
137
- detectWorkspaceDir() {
178
+
179
+ private detectWorkspaceDir(): string {
138
180
  const envWorkspace = process.env.PD_WORKSPACE_DIR;
139
181
  if (envWorkspace && envWorkspace.trim()) {
140
182
  this.log('info', `Using workspace from PD_WORKSPACE_DIR: ${envWorkspace}`);
141
183
  return envWorkspace.trim();
142
184
  }
185
+
143
186
  const envOpenclaw = process.env.OPENCLAW_WORKSPACE;
144
187
  if (envOpenclaw && envOpenclaw.trim()) {
145
188
  this.log('info', `Using workspace from OPENCLAW_WORKSPACE: ${envOpenclaw}`);
146
189
  return envOpenclaw.trim();
147
190
  }
191
+
148
192
  const fileConfig = loadConfigFromFile();
149
193
  if (fileConfig?.workspace) {
150
194
  this.log('info', `Using workspace from config file: ${fileConfig.workspace}`);
151
195
  return fileConfig.workspace;
152
196
  }
197
+
153
198
  const homeDir = os.homedir();
154
199
  const defaultWorkspace = path.join(homeDir, DEFAULT_WORKSPACE_SUBPATH);
155
200
  this.log('info', `Using default workspace: ${defaultWorkspace}`);
201
+
156
202
  return defaultWorkspace;
157
203
  }
158
- normalizePath(inputPath) {
204
+
205
+ private normalizePath(inputPath: string): string {
159
206
  const pathApi = getPathApi(inputPath);
160
207
  let normalized = pathApi === path ? path.resolve(inputPath) : pathApi.normalize(inputPath);
208
+
161
209
  if (this.normalizeWorkspace) {
162
210
  const problematicSuffixes = ['/memory', '/docs', '\\memory', '\\docs'];
211
+
163
212
  for (const suffix of problematicSuffixes) {
164
213
  if (normalized.endsWith(suffix)) {
165
214
  const parent = pathApi.dirname(normalized);
@@ -169,38 +218,49 @@ export class PathResolver {
169
218
  }
170
219
  }
171
220
  }
221
+
172
222
  return normalized;
173
223
  }
174
- normalizeWorkspacePath(inputPath) {
224
+
225
+ normalizeWorkspacePath(inputPath: string): string {
175
226
  return this.normalizePath(inputPath);
176
227
  }
177
- getWorkspaceDir() {
228
+
229
+ getWorkspaceDir(): string {
178
230
  if (this.initialized && this.workspaceDir) {
179
231
  return this.workspaceDir;
180
232
  }
233
+
181
234
  let dir = this.detectWorkspaceDir();
235
+
182
236
  if (this.normalizeWorkspace) {
183
237
  const originalDir = dir;
184
238
  dir = this.normalizePath(dir);
239
+
185
240
  if (originalDir !== dir) {
186
241
  this.log('info', `Workspace path normalized: ${originalDir} -> ${dir}`);
187
242
  }
188
243
  }
244
+
189
245
  this.workspaceDir = dir;
190
246
  this.initialized = true;
191
247
  this.log('debug', `Final workspace directory: ${this.workspaceDir}`);
248
+
192
249
  return this.workspaceDir;
193
250
  }
194
- getStateDir() {
251
+
252
+ getStateDir(): string {
195
253
  if (this.stateDir) {
196
254
  return this.stateDir;
197
255
  }
256
+
198
257
  const envStateDir = process.env.PD_STATE_DIR;
199
258
  if (envStateDir && envStateDir.trim()) {
200
259
  this.stateDir = envStateDir.trim();
201
260
  this.log('info', `Using state directory from PD_STATE_DIR: ${this.stateDir}`);
202
261
  return this.stateDir;
203
262
  }
263
+
204
264
  // If workspaceDir was explicitly provided via constructor, use workspace-based state dir
205
265
  // This ensures tests and programmatic usage don't get polluted by global config
206
266
  if (this.initialized && this.workspaceDir) {
@@ -209,19 +269,23 @@ export class PathResolver {
209
269
  this.log('debug', `Using workspace-based state directory: ${this.stateDir}`);
210
270
  return this.stateDir;
211
271
  }
272
+
212
273
  const fileConfig = loadConfigFromFile();
213
274
  if (fileConfig?.state) {
214
275
  this.stateDir = fileConfig.state;
215
276
  this.log('info', `Using state directory from config file: ${this.stateDir}`);
216
277
  return this.stateDir;
217
278
  }
279
+
218
280
  const workspaceDir = this.getWorkspaceDir();
219
281
  const pathApi = getPathApi(workspaceDir);
220
282
  this.stateDir = pathApi.join(workspaceDir, '.state');
221
283
  this.log('debug', `Computed state directory: ${this.stateDir}`);
284
+
222
285
  return this.stateDir;
223
286
  }
224
- resolve(key) {
287
+
288
+ resolve(key: string): string {
225
289
  const workspace = this.getWorkspaceDir();
226
290
  const state = this.getStateDir();
227
291
  const workspacePath = getPathApi(workspace);
@@ -233,7 +297,8 @@ export class PathResolver {
233
297
  const evolutionWorker = fs.existsSync(extensionSrc)
234
298
  ? extensionPath.join(extensionSrc, 'service', 'evolution-worker.ts')
235
299
  : extensionPath.join(extensionDist, 'service', 'evolution-worker.js');
236
- const pathMap = {
300
+
301
+ const pathMap: Record<string, string> = {
237
302
  'PROFILE': workspacePath.join(workspace, '.principles', 'PROFILE.json'),
238
303
  'PRINCIPLES': workspacePath.join(workspace, '.principles', 'PRINCIPLES.md'),
239
304
  'THINKING_OS': workspacePath.join(workspace, '.principles', 'THINKING_OS.md'),
@@ -269,51 +334,107 @@ export class PathResolver {
269
334
  'PRINCIPLE_BLACKLIST': workspacePath.join(state, 'principle_blacklist.json'),
270
335
  'MEMORY': memory,
271
336
  };
337
+
272
338
  const resolved = pathMap[key];
273
339
  if (!resolved) {
274
340
  this.log('warn', `Unknown path key: ${key}`);
275
341
  throw new PathResolutionError(key);
276
342
  }
343
+
277
344
  this.log('debug', `Resolved path for '${key}': ${resolved}`);
345
+
278
346
  return resolved;
279
347
  }
280
- ensureStateDir() {
348
+
349
+ ensureStateDir(): void {
281
350
  const stateDir = this.getStateDir();
351
+
282
352
  if (!fs.existsSync(stateDir)) {
283
353
  this.log('info', `Creating state directory: ${stateDir}`);
284
354
  fs.mkdirSync(stateDir, { recursive: true });
285
355
  }
286
356
  }
287
- static createFromHookContext(ctx) {
357
+
358
+ static createFromHookContext(ctx: any): PathResolver {
288
359
  const resolver = new PathResolver();
360
+
289
361
  if (ctx?.workspaceDir) {
290
362
  let workspaceDir = ctx.workspaceDir;
363
+
291
364
  if (resolver.normalizeWorkspace) {
292
365
  workspaceDir = resolver.normalizePath(workspaceDir);
293
366
  }
367
+
294
368
  resolver.workspaceDir = workspaceDir;
295
369
  resolver.initialized = true;
296
370
  resolver.log('info', `Created from hook context with workspace: ${workspaceDir}`);
297
371
  }
372
+
298
373
  if (ctx?.stateDir) {
299
374
  resolver.stateDir = ctx.stateDir;
300
375
  }
376
+
301
377
  return resolver;
302
378
  }
303
379
  }
304
- export function createDefaultConfig(targetPath) {
305
- const defaultConfig = {
380
+
381
+ export function createDefaultConfig(targetPath?: string): string {
382
+ const defaultConfig: PDConfig = {
306
383
  workspace: path.join(os.homedir(), '.openclaw', 'workspace'),
307
384
  state: path.join(os.homedir(), '.openclaw', 'workspace', '.state'),
308
385
  debug: false,
309
386
  };
387
+
310
388
  const target = targetPath || path.join(os.homedir(), '.openclaw', PD_CONFIG_FILE);
311
389
  const dir = path.dirname(target);
390
+
312
391
  if (!fs.existsSync(dir)) {
313
392
  fs.mkdirSync(dir, { recursive: true });
314
393
  }
394
+
315
395
  fs.writeFileSync(target, JSON.stringify(defaultConfig, null, 2), 'utf8');
396
+
316
397
  console.log(`✅ Created default config at: ${target}`);
317
398
  console.log(` You can edit this file to customize paths.`);
399
+
318
400
  return target;
319
401
  }
402
+
403
+ // ── OpenClaw API Workspace Resolution ────────────────────────────
404
+
405
+ /**
406
+ * Resolve workspace directory via OpenClaw's official API.
407
+ *
408
+ * Replaces the removed `api.workspaceDir` field.
409
+ *
410
+ * Priority: api.runtime.agent.resolveAgentWorkspaceDir(agentId)
411
+ * → PathResolver.getWorkspaceDir() (PD env vars, config file, default)
412
+ *
413
+ * @param api - Plugin API instance
414
+ * @param agentId - Agent ID (defaults to 'main' if not provided)
415
+ * @returns Resolved workspace directory, or `undefined` if all resolution paths fail
416
+ */
417
+ export function resolveWorkspaceDirFromApi(
418
+ api: OpenClawPluginApi | undefined,
419
+ agentId?: string,
420
+ ): string | undefined {
421
+ if (!api) return undefined;
422
+
423
+ // 1. Official API: api.runtime.agent.resolveAgentWorkspaceDir
424
+ const officialAgent = (api.runtime as { agent?: { resolveAgentWorkspaceDir?: (cfg: unknown, id: string) => string } }).agent;
425
+ if (officialAgent?.resolveAgentWorkspaceDir) {
426
+ try {
427
+ return officialAgent.resolveAgentWorkspaceDir(api.config, agentId ?? 'main');
428
+ } catch {
429
+ // Fall through to PathResolver
430
+ }
431
+ }
432
+
433
+ // 2. Fallback: PathResolver (PD_WORKSPACE_DIR env, config file, default)
434
+ try {
435
+ const pr = new PathResolver();
436
+ return pr.getWorkspaceDir();
437
+ } catch {
438
+ return undefined;
439
+ }
440
+ }
@@ -1,9 +1,12 @@
1
1
  import * as path from 'path';
2
- const posixJoin = (...parts) => path.posix.join(...parts);
3
- function isWindowsPath(input) {
2
+
3
+ const posixJoin = (...parts: string[]): string => path.posix.join(...parts);
4
+
5
+ function isWindowsPath(input: string): boolean {
4
6
  return /^[A-Za-z]:[\\/]/.test(input) || input.startsWith('\\\\');
5
7
  }
6
- function joinWorkspacePath(workspaceDir, relativePath) {
8
+
9
+ function joinWorkspacePath(workspaceDir: string, relativePath: string): string {
7
10
  if (isWindowsPath(workspaceDir)) {
8
11
  return path.win32.join(workspaceDir, relativePath);
9
12
  }
@@ -12,6 +15,7 @@ function joinWorkspacePath(workspaceDir, relativePath) {
12
15
  }
13
16
  return path.join(workspaceDir, relativePath);
14
17
  }
18
+
15
19
  /**
16
20
  * Principles Disciple Directory Constants
17
21
  * Establishing a logical separation between Identity, State, and Memory.
@@ -30,6 +34,7 @@ export const PD_DIRS = {
30
34
  NOCTURNAL_MEMORY: posixJoin('.state', 'nocturnal', 'memory'),
31
35
  NOCTURNAL_EXPORTS: posixJoin('.state', 'exports', 'orpo'),
32
36
  };
37
+
33
38
  /**
34
39
  * Standard File Path Mappings
35
40
  */
@@ -40,6 +45,7 @@ export const PD_FILES = {
40
45
  KERNEL: posixJoin(PD_DIRS.IDENTITY, '00-kernel.md'),
41
46
  DECISION_POLICY: posixJoin(PD_DIRS.IDENTITY, 'DECISION_POLICY.json'),
42
47
  MODELS_DIR: PD_DIRS.MODELS,
48
+
43
49
  STATE_DIR: PD_DIRS.STATE,
44
50
  EVOLUTION_QUEUE: posixJoin(PD_DIRS.STATE, 'evolution_queue.json'),
45
51
  EVOLUTION_DIRECTIVE: posixJoin(PD_DIRS.STATE, 'evolution_directive.json'),
@@ -59,9 +65,11 @@ export const PD_FILES = {
59
65
  NOCTURNAL_SAMPLES_DIR: PD_DIRS.NOCTURNAL_SAMPLES,
60
66
  NOCTURNAL_MEMORY_DIR: PD_DIRS.NOCTURNAL_MEMORY,
61
67
  NOCTURNAL_EXPORTS_DIR: PD_DIRS.NOCTURNAL_EXPORTS,
68
+
62
69
  PLAN: 'PLAN.md',
63
70
  MEMORY_MD: 'MEMORY.md',
64
71
  HEARTBEAT: 'HEARTBEAT.md',
72
+
65
73
  SYSTEM_LOG: posixJoin(PD_DIRS.LOGS, 'SYSTEM.log'),
66
74
  REFLECTION_LOG: posixJoin(PD_DIRS.MEMORY, 'reflection-log.md'),
67
75
  USER_CONTEXT: posixJoin(PD_DIRS.MEMORY, 'USER_CONTEXT.md'),
@@ -73,9 +81,10 @@ export const PD_FILES = {
73
81
  EVOLUTION_STREAM: posixJoin(PD_DIRS.MEMORY, 'evolution.jsonl'),
74
82
  EVOLUTION_LOCK: posixJoin(PD_DIRS.LOCKS, 'evolution'),
75
83
  };
84
+
76
85
  /**
77
86
  * Resolves a PD file path within a given workspace.
78
87
  */
79
- export function resolvePdPath(workspaceDir, fileKey) {
88
+ export function resolvePdPath(workspaceDir: string, fileKey: keyof typeof PD_FILES): string {
80
89
  return joinWorkspacePath(workspaceDir, PD_FILES[fileKey]);
81
90
  }