@octopi-agent/engine 0.98.4 → 0.102.10

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 (301) hide show
  1. package/dist/config-schema/agent.d.ts +35 -5
  2. package/dist/config-schema/agent.js +24 -4
  3. package/dist/config-schema/context.d.ts +6 -36
  4. package/dist/config-schema/context.js +4 -28
  5. package/dist/config-schema/engine.d.ts +57 -160
  6. package/dist/config-schema/engine.js +1 -5
  7. package/dist/config-schema/governance.d.ts +8 -1
  8. package/dist/config-schema/governance.js +8 -1
  9. package/dist/config-schema/models.d.ts +2 -8
  10. package/dist/config-schema/models.js +0 -2
  11. package/dist/config-schema/run.d.ts +2 -2
  12. package/dist/config-schema/run.js +2 -2
  13. package/dist/config-schema/shared.d.ts +0 -3
  14. package/dist/config-schema/shared.js +0 -2
  15. package/dist/config-schema/substrate.d.ts +14 -88
  16. package/dist/config-schema/substrate.js +22 -107
  17. package/dist/config.d.ts +45 -113
  18. package/dist/config.js +8 -2
  19. package/dist/harness/activation/dispatcher.js +7 -0
  20. package/dist/harness/activation/inbox.d.ts +3 -3
  21. package/dist/harness/activation/inbox.js +3 -3
  22. package/dist/harness/activation/runtime.js +13 -5
  23. package/dist/harness/activation/types.d.ts +2 -2
  24. package/dist/harness/agent/builder.d.ts +24 -3
  25. package/dist/harness/agent/builder.js +131 -27
  26. package/dist/harness/agent/config-bridge.js +9 -2
  27. package/dist/harness/agent/config-integrity.js +19 -2
  28. package/dist/harness/agent/wire-agent-builder.d.ts +11 -3
  29. package/dist/harness/agent/wire-agent-builder.js +26 -7
  30. package/dist/harness/capabilities/document/factory.d.ts +5 -0
  31. package/dist/harness/capabilities/document/factory.js +1 -0
  32. package/dist/harness/capabilities/document/port.js +6 -3
  33. package/dist/harness/capabilities/document/types.d.ts +5 -0
  34. package/dist/harness/collaboration/autonomous-subsystem/loader.js +1 -1
  35. package/dist/harness/collaboration/autonomous-subsystem/sense/engine.d.ts +1 -1
  36. package/dist/harness/collaboration/autonomous-subsystem/sense/engine.js +2 -2
  37. package/dist/harness/collaboration/autonomous-subsystem/sense/input-builder.js +4 -4
  38. package/dist/harness/collaboration/autonomous-subsystem/sense/metrics.d.ts +2 -2
  39. package/dist/harness/collaboration/autonomous-subsystem/sense/metrics.js +2 -2
  40. package/dist/harness/collaboration/autonomous-subsystem/signal/bus.d.ts +6 -0
  41. package/dist/harness/collaboration/autonomous-subsystem/signal/bus.js +25 -4
  42. package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +1 -1
  43. package/dist/harness/collaboration/autonomous-subsystem/types.d.ts +3 -3
  44. package/dist/harness/collaboration/delegation/child-run.d.ts +1 -1
  45. package/dist/harness/collaboration/delegation/child-run.js +4 -4
  46. package/dist/harness/collaboration/delegation/index.d.ts +1 -1
  47. package/dist/harness/collaboration/delegation/index.js +1 -1
  48. package/dist/harness/collaboration/delegation/notifier.d.ts +6 -1
  49. package/dist/harness/collaboration/delegation/notifier.js +8 -1
  50. package/dist/harness/collaboration/delegation/service.d.ts +2 -0
  51. package/dist/harness/collaboration/delegation/service.js +2 -1
  52. package/dist/harness/collaboration/delegation/store.d.ts +2 -0
  53. package/dist/harness/collaboration/delegation/store.js +2 -1
  54. package/dist/harness/collaboration/delegation/types.d.ts +23 -2
  55. package/dist/harness/collaboration/delegation/types.js +22 -0
  56. package/dist/harness/collaboration/delegation/wiring.js +11 -1
  57. package/dist/harness/context/budget-allocator.d.ts +26 -18
  58. package/dist/harness/context/budget-allocator.js +67 -32
  59. package/dist/harness/context/capabilities/compact/engine.d.ts +0 -6
  60. package/dist/harness/context/capabilities/compact/engine.js +169 -121
  61. package/dist/harness/context/capabilities/compact/handover.d.ts +5 -3
  62. package/dist/harness/context/capabilities/compact/handover.js +7 -5
  63. package/dist/harness/context/capabilities/compact/index.d.ts +5 -1
  64. package/dist/harness/context/capabilities/compact/index.js +4 -0
  65. package/dist/harness/context/capabilities/compact/mechanical.d.ts +130 -0
  66. package/dist/harness/context/capabilities/compact/mechanical.js +268 -0
  67. package/dist/harness/context/capabilities/compact/messages.d.ts +60 -0
  68. package/dist/harness/context/capabilities/compact/messages.js +91 -0
  69. package/dist/harness/context/capabilities/compact/types.d.ts +76 -11
  70. package/dist/harness/context/capabilities/compact/types.js +11 -1
  71. package/dist/harness/context/capabilities/compact/view-split.d.ts +36 -0
  72. package/dist/harness/context/capabilities/compact/view-split.js +72 -0
  73. package/dist/harness/context/capabilities/summary/port.js +0 -1
  74. package/dist/harness/context/capabilities/summary/resolver.d.ts +1 -1
  75. package/dist/harness/context/capabilities/summary/resolver.js +1 -6
  76. package/dist/harness/context/capabilities/summary/tool-binding.d.ts +1 -1
  77. package/dist/harness/context/capabilities/summary/types.d.ts +3 -6
  78. package/dist/harness/context/context-size-ledger.d.ts +143 -0
  79. package/dist/harness/context/context-size-ledger.js +236 -0
  80. package/dist/harness/context/default-context-engine.d.ts +195 -119
  81. package/dist/harness/context/default-context-engine.js +1241 -743
  82. package/dist/harness/context/index.d.ts +2 -2
  83. package/dist/harness/context/index.js +1 -1
  84. package/dist/harness/context/layer-types.d.ts +15 -0
  85. package/dist/harness/context/layer-types.js +14 -1
  86. package/dist/harness/context/layers.d.ts +14 -15
  87. package/dist/harness/context/layers.js +41 -11
  88. package/dist/harness/context/message-selector.d.ts +10 -8
  89. package/dist/harness/context/message-selector.js +49 -27
  90. package/dist/harness/context/summarize.d.ts +2 -2
  91. package/dist/harness/context/summarize.js +9 -2
  92. package/dist/harness/context/system-prompt-assembler.d.ts +2 -0
  93. package/dist/harness/context/system-prompt-assembler.js +2 -1
  94. package/dist/harness/context/token-constants.d.ts +8 -6
  95. package/dist/harness/context/token-constants.js +8 -7
  96. package/dist/harness/context/token-estimate-fns.d.ts +37 -27
  97. package/dist/harness/context/token-estimate-fns.js +85 -66
  98. package/dist/harness/context/token-estimator.d.ts +27 -12
  99. package/dist/harness/context/token-estimator.js +120 -66
  100. package/dist/harness/context/token-segments.d.ts +88 -0
  101. package/dist/harness/context/token-segments.js +103 -0
  102. package/dist/harness/context/tool-result-aging.d.ts +6 -1
  103. package/dist/harness/context/tool-result-aging.js +16 -5
  104. package/dist/harness/context/tool-result-body.js +2 -0
  105. package/dist/harness/context/transform/anchors.d.ts +66 -0
  106. package/dist/harness/context/transform/anchors.js +125 -0
  107. package/dist/harness/context/transform/fold.d.ts +56 -0
  108. package/dist/harness/context/transform/fold.js +135 -0
  109. package/dist/harness/context/transform/migrate.d.ts +41 -0
  110. package/dist/harness/context/transform/migrate.js +69 -0
  111. package/dist/harness/context/transform/types.d.ts +129 -0
  112. package/dist/harness/context/transform/types.js +19 -0
  113. package/dist/harness/context/turn-envelope.d.ts +6 -3
  114. package/dist/harness/context/types.d.ts +127 -117
  115. package/dist/harness/extension/execution-environment/ops-audit.d.ts +1 -1
  116. package/dist/harness/extension/execution-environment/resource-limits.d.ts +1 -1
  117. package/dist/harness/extension/execution-environment/resource-limits.js +1 -1
  118. package/dist/harness/extension/plugin-ecosystem/channel-types.d.ts +1 -1
  119. package/dist/harness/extension/plugin-ecosystem/commands/index.d.ts +3 -1
  120. package/dist/harness/extension/plugin-ecosystem/commands/index.js +1 -0
  121. package/dist/harness/extension/plugin-ecosystem/commands/plugin-bridge.d.ts +12 -1
  122. package/dist/harness/extension/plugin-ecosystem/commands/plugin-bridge.js +14 -4
  123. package/dist/harness/extension/plugin-ecosystem/commands/router.d.ts +5 -4
  124. package/dist/harness/extension/plugin-ecosystem/commands/router.js +23 -14
  125. package/dist/harness/extension/plugin-ecosystem/commands/types.d.ts +8 -1
  126. package/dist/harness/extension/plugin-ecosystem/commands/types.js +2 -1
  127. package/dist/harness/extension/plugin-ecosystem/hooks/adapter.d.ts +54 -0
  128. package/dist/harness/extension/plugin-ecosystem/hooks/adapter.js +397 -0
  129. package/dist/harness/extension/plugin-ecosystem/hooks/compaction-integration.d.ts +16 -0
  130. package/dist/harness/extension/plugin-ecosystem/hooks/compaction-integration.js +66 -0
  131. package/dist/harness/extension/plugin-ecosystem/hooks/index.d.ts +4 -0
  132. package/dist/harness/extension/plugin-ecosystem/hooks/index.js +2 -0
  133. package/dist/harness/extension/plugin-ecosystem/hooks/iteration-snapshot.d.ts +11 -0
  134. package/dist/harness/extension/plugin-ecosystem/hooks/iteration-snapshot.js +54 -0
  135. package/dist/harness/extension/plugin-ecosystem/hooks/journal.d.ts +9 -0
  136. package/dist/harness/extension/plugin-ecosystem/hooks/journal.js +22 -0
  137. package/dist/harness/extension/plugin-ecosystem/hooks/policy-error.d.ts +12 -0
  138. package/dist/harness/extension/plugin-ecosystem/hooks/policy-error.js +19 -0
  139. package/dist/harness/extension/plugin-ecosystem/hooks/run-integration.d.ts +36 -0
  140. package/dist/harness/extension/plugin-ecosystem/hooks/run-integration.js +338 -0
  141. package/dist/harness/extension/plugin-ecosystem/hooks/services.d.ts +24 -0
  142. package/dist/harness/extension/plugin-ecosystem/hooks/services.js +139 -0
  143. package/dist/harness/extension/plugin-ecosystem/hooks/types.d.ts +229 -0
  144. package/dist/harness/extension/plugin-ecosystem/hooks/types.js +1 -0
  145. package/dist/harness/extension/plugin-ecosystem/plugins/api.d.ts +83 -249
  146. package/dist/harness/extension/plugin-ecosystem/plugins/api.js +88 -214
  147. package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.d.ts +43 -0
  148. package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.js +61 -0
  149. package/dist/harness/extension/plugin-ecosystem/plugins/engine-compatibility.d.ts +4 -0
  150. package/dist/harness/extension/plugin-ecosystem/plugins/engine-compatibility.js +18 -0
  151. package/dist/harness/extension/plugin-ecosystem/plugins/entry.d.ts +7 -85
  152. package/dist/harness/extension/plugin-ecosystem/plugins/entry.js +8 -50
  153. package/dist/harness/extension/plugin-ecosystem/plugins/index.d.ts +6 -23
  154. package/dist/harness/extension/plugin-ecosystem/plugins/index.js +3 -23
  155. package/dist/harness/extension/plugin-ecosystem/plugins/loader.d.ts +50 -117
  156. package/dist/harness/extension/plugin-ecosystem/plugins/loader.js +316 -285
  157. package/dist/harness/extension/plugin-ecosystem/plugins/manager.d.ts +23 -490
  158. package/dist/harness/extension/plugin-ecosystem/plugins/manager.js +85 -353
  159. package/dist/harness/extension/plugin-ecosystem/plugins/manifest.d.ts +41 -223
  160. package/dist/harness/extension/plugin-ecosystem/plugins/manifest.js +138 -19
  161. package/dist/harness/extension/plugin-ecosystem/plugins/staging.d.ts +15 -0
  162. package/dist/harness/extension/plugin-ecosystem/plugins/staging.js +15 -0
  163. package/dist/harness/extension/plugin-ecosystem/skills/manager.d.ts +6 -1
  164. package/dist/harness/extension/plugin-ecosystem/skills/manager.js +17 -1
  165. package/dist/harness/extension/plugin-ecosystem/tools/ask-user.d.ts +3 -0
  166. package/dist/harness/extension/plugin-ecosystem/tools/ask-user.js +46 -21
  167. package/dist/harness/extension/plugin-ecosystem/tools/delegation.js +18 -8
  168. package/dist/harness/extension/plugin-ecosystem/tools/plugin-tool.d.ts +11 -0
  169. package/dist/harness/extension/plugin-ecosystem/tools/plugin-tool.js +39 -0
  170. package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.d.ts +1 -1
  171. package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.js +11 -2
  172. package/dist/harness/governance/accounting/usage-ledger.d.ts +13 -1
  173. package/dist/harness/governance/accounting/usage-ledger.js +23 -1
  174. package/dist/harness/governance/human-in-the-loop/decision-cache.d.ts +3 -3
  175. package/dist/harness/governance/human-in-the-loop/decision-cache.js +2 -2
  176. package/dist/harness/governance/security/capability-enforcer.js +1 -2
  177. package/dist/harness/index.d.ts +12 -12
  178. package/dist/harness/index.js +7 -7
  179. package/dist/harness/knowledge/boot-config.d.ts +55 -0
  180. package/dist/harness/knowledge/boot-config.js +50 -0
  181. package/dist/harness/knowledge/catalog-types.d.ts +2 -0
  182. package/dist/harness/knowledge/client.d.ts +24 -2
  183. package/dist/harness/knowledge/client.js +19 -5
  184. package/dist/harness/knowledge/db.js +5 -0
  185. package/dist/harness/knowledge/describe-sample.d.ts +23 -0
  186. package/dist/harness/knowledge/describe-sample.js +58 -0
  187. package/dist/harness/knowledge/describe.d.ts +8 -0
  188. package/dist/harness/knowledge/describe.js +18 -2
  189. package/dist/harness/knowledge/engine-thread.js +6 -0
  190. package/dist/harness/knowledge/grounding.js +3 -1
  191. package/dist/harness/knowledge/http-app.js +35 -4
  192. package/dist/harness/knowledge/http-params.d.ts +18 -0
  193. package/dist/harness/knowledge/http-params.js +24 -0
  194. package/dist/harness/knowledge/index.d.ts +2 -0
  195. package/dist/harness/knowledge/index.js +1 -0
  196. package/dist/harness/knowledge/knowledge-serve-child.js +3 -0
  197. package/dist/harness/knowledge/purge.js +5 -2
  198. package/dist/harness/knowledge/query-service.d.ts +13 -2
  199. package/dist/harness/knowledge/query-service.js +4 -2
  200. package/dist/harness/knowledge/query-worker-client.d.ts +9 -1
  201. package/dist/harness/knowledge/query-worker-client.js +4 -2
  202. package/dist/harness/knowledge/query-worker.js +4 -2
  203. package/dist/harness/knowledge/read-http.js +5 -1
  204. package/dist/harness/knowledge/serve.d.ts +8 -7
  205. package/dist/harness/knowledge/serve.js +9 -0
  206. package/dist/harness/knowledge/source-store.js +26 -3
  207. package/dist/harness/knowledge/start-service-process.d.ts +15 -7
  208. package/dist/harness/knowledge/start-service-process.js +21 -9
  209. package/dist/harness/knowledge/types.d.ts +7 -0
  210. package/dist/harness/knowledge/writer-service.d.ts +37 -9
  211. package/dist/harness/knowledge/writer-service.js +120 -14
  212. package/dist/harness/knowledge/writer-worker-client.d.ts +10 -9
  213. package/dist/harness/knowledge/writer-worker-client.js +20 -2
  214. package/dist/harness/knowledge/writer-worker.js +9 -1
  215. package/dist/harness/memory/sqlite/wisdom-store.js +35 -10
  216. package/dist/harness/memory/types.d.ts +6 -4
  217. package/dist/harness/memory/wisdom-formation.d.ts +14 -3
  218. package/dist/harness/memory/wisdom-formation.js +85 -22
  219. package/dist/harness/memory/wisdom-gates.d.ts +14 -0
  220. package/dist/harness/memory/wisdom-gates.js +59 -19
  221. package/dist/harness/memory/wisdom.js +9 -0
  222. package/dist/harness/observability/observer/hub.js +25 -15
  223. package/dist/harness/observability/observer/types.d.ts +10 -6
  224. package/dist/harness/run/agent/agent.d.ts +40 -21
  225. package/dist/harness/run/agent/agent.js +66 -13
  226. package/dist/harness/run/agent/index.d.ts +1 -1
  227. package/dist/harness/run/budget/budget.d.ts +28 -10
  228. package/dist/harness/run/budget/budget.js +38 -15
  229. package/dist/harness/run/model/index.d.ts +3 -1
  230. package/dist/harness/run/model/index.js +2 -1
  231. package/dist/harness/run/model/resolver.d.ts +4 -1
  232. package/dist/harness/run/model/resolver.js +36 -2
  233. package/dist/harness/run/model/run-scope.d.ts +2 -0
  234. package/dist/harness/run/model/run-scope.js +7 -0
  235. package/dist/harness/run/model/types.d.ts +22 -5
  236. package/dist/harness/run/model/types.js +3 -2
  237. package/dist/harness/run/model/window-truth.d.ts +37 -0
  238. package/dist/harness/run/model/window-truth.js +39 -0
  239. package/dist/harness/run/reliability/context-length-feedback.d.ts +77 -0
  240. package/dist/harness/run/reliability/context-length-feedback.js +128 -0
  241. package/dist/harness/run/reliability/harness-events.d.ts +3 -3
  242. package/dist/harness/run/reliability/model-error-policy.js +4 -0
  243. package/dist/harness/run/reliability/run-agent.d.ts +10 -4
  244. package/dist/harness/run/reliability/run-agent.js +137 -53
  245. package/dist/harness/run/reliability/run-metrics-collector.d.ts +19 -8
  246. package/dist/harness/run/reliability/run-metrics-collector.js +38 -23
  247. package/dist/harness/run/run-guard/default-run-guard.js +8 -8
  248. package/dist/harness/run/run-scope.d.ts +37 -4
  249. package/dist/harness/run/runner.d.ts +44 -29
  250. package/dist/harness/run/runner.js +320 -102
  251. package/dist/harness/session/attachments/index.d.ts +1 -0
  252. package/dist/harness/session/attachments/index.js +1 -0
  253. package/dist/harness/session/attachments/inject.d.ts +21 -10
  254. package/dist/harness/session/attachments/inject.js +76 -24
  255. package/dist/harness/session/attachments/recall-index.d.ts +70 -0
  256. package/dist/harness/session/attachments/recall-index.js +219 -0
  257. package/dist/harness/session/attachments/service.d.ts +21 -0
  258. package/dist/harness/session/attachments/service.js +258 -139
  259. package/dist/harness/session/attachments/types.d.ts +6 -0
  260. package/dist/harness/session/transform-store.d.ts +136 -0
  261. package/dist/harness/session/transform-store.js +302 -0
  262. package/dist/harness/session/types.d.ts +2 -2
  263. package/dist/harness/shared/events/agent-event-map.d.ts +46 -13
  264. package/dist/harness/shared/events/agent-event-map.js +3 -2
  265. package/dist/harness/shared/events/scenario-events.d.ts +6 -6
  266. package/dist/harness/shared/types/agent-definition.d.ts +1 -2
  267. package/dist/index.d.ts +14 -17
  268. package/dist/index.js +7 -9
  269. package/dist/integration/observability/metrics.d.ts +6 -6
  270. package/dist/integration/observability/metrics.js +13 -13
  271. package/dist/integration/observability/observer-bridge.js +2 -2
  272. package/dist/integration/observability/trace-collector.d.ts +5 -5
  273. package/dist/integration/observability/trace-collector.js +34 -35
  274. package/dist/integration/observability/trace-events.d.ts +3 -3
  275. package/dist/integration/observability/trace-events.js +3 -3
  276. package/dist/integration/providers/anthropic.js +14 -1
  277. package/dist/integration/providers/openai.js +7 -1
  278. package/dist/integration/providers/usage.d.ts +1 -1
  279. package/dist/integration/providers/wire-dump.d.ts +1 -1
  280. package/dist/integration/storage/archive-manager.d.ts +6 -0
  281. package/dist/integration/storage/archive-manager.js +10 -0
  282. package/dist/integration/storage/jsonl.d.ts +1 -1
  283. package/dist/integration/storage/jsonl.js +18 -16
  284. package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +8 -1
  285. package/dist/subsystems/wisdom-steward/formation/config.yaml +1 -0
  286. package/dist/subsystems/wisdom-steward/formation/handler.js +27 -5
  287. package/package.json +11 -14
  288. package/dist/harness/context/hybrid-compressor.d.ts +0 -41
  289. package/dist/harness/context/hybrid-compressor.js +0 -159
  290. package/dist/harness/context/llm-summarizer.d.ts +0 -46
  291. package/dist/harness/context/llm-summarizer.js +0 -249
  292. package/dist/harness/context/smart-router.d.ts +0 -75
  293. package/dist/harness/context/smart-router.js +0 -118
  294. package/dist/harness/context/truncate-compressor.d.ts +0 -28
  295. package/dist/harness/context/truncate-compressor.js +0 -107
  296. package/dist/harness/extension/plugin-ecosystem/plugins/capability.d.ts +0 -97
  297. package/dist/harness/extension/plugin-ecosystem/plugins/capability.js +0 -155
  298. package/dist/harness/extension/plugin-ecosystem/plugins/hooks.d.ts +0 -7
  299. package/dist/harness/extension/plugin-ecosystem/plugins/hooks.js +0 -7
  300. package/dist/harness/extension/plugin-ecosystem/plugins/lifecycle.d.ts +0 -86
  301. package/dist/harness/extension/plugin-ecosystem/plugins/lifecycle.js +0 -139
@@ -1,336 +1,367 @@
1
- /**
2
- * Plugin Loader — Plugin 发现和加载
3
- *
4
- * 加载流程:
5
- * 1. Discovery — 从配置路径发现候选 plugins
6
- * 2. Validation — 验证 manifest、检查 enable/disable 状态
7
- * 3. Loading — 进程内加载 plugin 代码
8
- * 4. Registration — 调用 register() 注册能力
9
- */
10
- import * as path from 'node:path';
11
- import * as fs from 'node:fs/promises';
12
- import { existsSync } from 'node:fs';
13
- import { validateManifest } from './manifest.js';
14
- import { PluginApi as PluginApiClass } from './api.js';
15
- import { CapabilityRegistry, registerManifestCapabilities } from './capability.js';
16
- /**
17
- * Plugin Loader
18
- *
19
- * 负责从文件系统发现和加载 plugins。
20
- */
1
+ /** Manifest-first discovery: no plugin code runs before eligibility and config validation. */
2
+ import { existsSync, readdirSync, readFileSync, realpathSync } from 'node:fs';
3
+ import { join, relative, resolve, sep } from 'node:path';
4
+ import { pathToFileURL } from 'node:url';
5
+ import { PluginApi } from './api.js';
6
+ import { parseManifest, validatePluginConfig, } from './manifest.js';
7
+ import { PluginContributionRegistry, } from './contribution-registry.js';
8
+ import { getEngineVersion, satisfiesEngineRange } from './engine-compatibility.js';
9
+ import { readStaging } from './staging.js';
10
+ const PLUGIN_MANIFEST_FILENAME = 'octopi.plugin.json';
21
11
  export class PluginLoader {
22
- /** 已加载的 plugins */
23
- plugins = new Map();
24
- /** Capability Registry */
25
- capabilities = new CapabilityRegistry();
26
- /** 配置 */
12
+ contributions = new PluginContributionRegistry();
27
13
  config;
14
+ plugins = new Map();
15
+ diagnostics = [];
28
16
  constructor(config) {
29
17
  this.config = config;
30
18
  }
31
- /**
32
- * 发现并加载所有 plugins
33
- *
34
- * 扫描所有配置路径,查找包含 octopi.plugin.json 的目录。
35
- *
36
- * @returns 加载的 plugin 列表
37
- */
19
+ /** Resolve a loaded plugin's configured hook policy, including legacy aliases. */
20
+ getPluginConfig(id) {
21
+ const plugin = this.plugins.get(id);
22
+ if (!plugin)
23
+ return undefined;
24
+ const ids = [plugin.id, ...(plugin.manifest.legacyPluginIds ?? [])];
25
+ return ids.map((key) => this.config.entries?.[key]).find((entry) => entry !== undefined);
26
+ }
27
+ /** Discover, validate and atomically publish the successful plugin set. */
38
28
  async discover() {
39
- const discovered = [];
29
+ // Keep the previous effective snapshot until the next complete set validates.
30
+ this.diagnostics = [];
31
+ const candidates = new Map();
32
+ const aliases = new Map();
33
+ const rejectedIds = new Set();
40
34
  for (const loadPath of this.config.loadPaths) {
41
- try {
42
- const plugins = await this.scanDirectory(loadPath);
43
- discovered.push(...plugins);
44
- }
45
- catch (err) {
46
- console.warn(`[PluginLoader] Failed to scan "${loadPath}":`, err);
35
+ for (const rootDir of this.scanDirectories(loadPath)) {
36
+ const candidate = this.readCandidate(rootDir);
37
+ if (candidate)
38
+ this.addCandidate(candidate, candidates, aliases, rejectedIds);
47
39
  }
48
40
  }
49
- // 过滤:allowlist / blocklist / enabled
50
- const filtered = this.filterPlugins(discovered);
51
- // 依赖解析:按 requiresPlugins 拓扑排序
52
- const ordered = this.resolveDependencyOrder(filtered);
53
- // 检查缺失的依赖
54
- const ids = new Set(ordered.map(p => p.manifest.id));
55
- const disabled = new Set();
56
- for (const plugin of ordered) {
57
- const deps = plugin.manifest.requiresPlugins ?? [];
58
- for (const dep of deps) {
59
- if (!ids.has(dep)) {
60
- console.warn(`[PluginLoader] Plugin "${plugin.manifest.id}" requires "${dep}", but it was not found. Skipping.`);
61
- disabled.add(plugin.manifest.id);
62
- }
63
- }
41
+ for (const candidate of candidates.values()) {
42
+ candidate.config = this.config.entries?.[candidate.id];
64
43
  }
65
- // 执行 register()
66
- for (const plugin of ordered) {
67
- if (disabled.has(plugin.manifest.id))
68
- continue;
69
- await this.registerPlugin(plugin);
44
+ for (const id of rejectedIds) {
45
+ const candidate = candidates.get(id);
46
+ if (candidate)
47
+ candidate.rejected = true;
70
48
  }
71
- return ordered;
72
- }
73
- /**
74
- * 扫描单个目录,发现 plugins
75
- */
76
- async scanDirectory(dirPath) {
77
- const plugins = [];
78
- if (!existsSync(dirPath)) {
79
- return plugins;
80
- }
81
- const entries = await fs.readdir(dirPath, { withFileTypes: true });
82
- for (const entry of entries) {
83
- if (!entry.isDirectory())
84
- continue;
85
- const pluginDir = path.join(dirPath, entry.name);
86
- const manifestPath = path.join(pluginDir, 'octopi.plugin.json');
87
- if (!existsSync(manifestPath))
49
+ this.applyAliases(candidates);
50
+ for (const candidate of candidates.values()) {
51
+ candidate.eligible = this.isEligible(candidate);
52
+ }
53
+ for (const candidate of candidates.values()) {
54
+ if (candidate.eligible)
55
+ this.prepareCandidate(candidate);
56
+ }
57
+ // Configuration, entry, conflict and dependency failures must settle before any import.
58
+ let changed = true;
59
+ while (changed) {
60
+ const before = [...candidates.values()].filter((candidate) => candidate.eligible).length;
61
+ this.markDependencyFailures(candidates, aliases);
62
+ this.checkDeclaredConflicts(candidates);
63
+ changed = before !== [...candidates.values()].filter((candidate) => candidate.eligible).length;
64
+ }
65
+ const staged = [];
66
+ for (const candidate of candidates.values()) {
67
+ if (!candidate.eligible)
88
68
  continue;
89
- try {
90
- const plugin = await this.loadPluginFromDir(pluginDir, manifestPath);
91
- if (plugin) {
92
- plugins.push(plugin);
93
- }
69
+ const plugin = await this.importAndRegister(candidate);
70
+ if (plugin)
71
+ staged.push(plugin);
72
+ else
73
+ candidate.eligible = false;
74
+ }
75
+ this.markDependencyFailures(candidates, aliases);
76
+ const publishable = staged.filter((plugin) => candidates.get(plugin.manifest.id)?.eligible);
77
+ for (const plugin of staged) {
78
+ if (!candidates.get(plugin.manifest.id)?.eligible) {
79
+ this.fail(plugin.manifest.id, 'dependency', new Error('A required plugin failed to register'), plugin.path);
94
80
  }
95
- catch (err) {
96
- console.warn(`[PluginLoader] Failed to load plugin from "${pluginDir}":`, err);
81
+ }
82
+ const nextDiscoveryFailed = this.diagnostics.some((item) => item.status === 'failed');
83
+ if (publishable.length === 0 && nextDiscoveryFailed && this.plugins.size > 0) {
84
+ const reason = 'New discovery has no publishable plugins; last complete snapshot retained for diagnosis';
85
+ this.fail('discovery', 'publish', new Error(reason));
86
+ throw new Error(`Plugin discovery failed: ${reason}`);
87
+ }
88
+ try {
89
+ const nextPlugins = new Map();
90
+ for (const plugin of publishable) {
91
+ const loaded = {
92
+ ...plugin,
93
+ id: plugin.manifest.id,
94
+ name: plugin.manifest.name,
95
+ version: plugin.manifest.version,
96
+ ...(plugin.manifest.description === undefined ? {} : { description: plugin.manifest.description }),
97
+ source: 'local',
98
+ registered: true,
99
+ };
100
+ nextPlugins.set(loaded.id, loaded);
97
101
  }
102
+ this.contributions.publish(publishable);
103
+ this.plugins = nextPlugins;
104
+ for (const plugin of nextPlugins.values()) {
105
+ this.diagnostics.push({ id: plugin.id, status: 'registered', stage: 'publish', reason: 'registered', path: plugin.path });
106
+ }
107
+ }
108
+ catch (error) {
109
+ // publish constructs a replacement set before assignment; a conflict cannot leave a partial new set.
110
+ this.fail('unknown', 'publish', error);
98
111
  }
99
- return plugins;
112
+ return this.getRegisteredIds().map((id) => this.plugins.get(id));
100
113
  }
101
- /**
102
- * 从目录加载单个 plugin
103
- */
104
- async loadPluginFromDir(pluginDir, manifestPath) {
105
- // 1. 读取和验证 manifest
106
- const manifestJson = await fs.readFile(manifestPath, 'utf-8');
107
- let manifest;
114
+ scanDirectories(loadPath) {
115
+ const root = resolve(loadPath);
108
116
  try {
109
- manifest = validateManifest(JSON.parse(manifestJson));
110
- }
111
- catch (err) {
112
- console.warn(`[PluginLoader] Invalid manifest at "${manifestPath}":`, err);
113
- return null;
117
+ const stat = readdirSync(root, { withFileTypes: true });
118
+ if (existsSync(join(root, PLUGIN_MANIFEST_FILENAME)))
119
+ return [root];
120
+ return stat
121
+ .filter((entry) => entry.isDirectory() && existsSync(join(root, entry.name, PLUGIN_MANIFEST_FILENAME)))
122
+ .map((entry) => join(root, entry.name));
114
123
  }
115
- // 2. 查找入口文件
116
- const entryPoint = await this.resolveEntryPoint(pluginDir);
117
- if (!entryPoint) {
118
- console.warn(`[PluginLoader] No entry point found in "${pluginDir}"`);
119
- return null;
124
+ catch (error) {
125
+ this.diagnostics.push({ id: root, status: 'failed', stage: 'manifest', reason: errorMessage(error), path: root });
126
+ return [];
120
127
  }
121
- // 3. 加载 plugin 模块
122
- let definition;
128
+ }
129
+ readCandidate(rootDir) {
123
130
  try {
124
- const module = await import(entryPoint);
125
- definition = module.default ?? module;
126
- if (!definition?.id || !definition?.register) {
127
- console.warn(`[PluginLoader] Invalid plugin definition in "${entryPoint}": missing id or register`);
128
- return null;
129
- }
131
+ const manifest = parseManifest(readFileSync(join(rootDir, PLUGIN_MANIFEST_FILENAME), 'utf8'));
132
+ return { id: manifest.id, rootDir, manifest, eligible: false, rejected: false };
130
133
  }
131
- catch (err) {
132
- console.warn(`[PluginLoader] Failed to import "${entryPoint}":`, err);
134
+ catch (error) {
135
+ this.fail(rootDir, 'manifest', error, rootDir);
133
136
  return null;
134
137
  }
135
- // 4. 创建 PluginApi
136
- const entryConfig = this.config.pluginEntries?.[manifest.id];
137
- const api = new PluginApiClass({
138
- id: manifest.id,
139
- name: definition.name ?? manifest.name ?? manifest.id,
140
- version: definition.version ?? manifest.version,
141
- description: definition.description ?? manifest.description,
142
- source: entryPoint,
143
- rootDir: pluginDir,
144
- pluginConfig: entryConfig?.config,
145
- });
146
- // 5. 注册 manifest 中声明的能力
147
- try {
148
- registerManifestCapabilities(manifest, this.capabilities);
138
+ }
139
+ addCandidate(candidate, candidates, aliases, rejectedIds) {
140
+ const previous = candidates.get(candidate.id);
141
+ if (previous) {
142
+ this.fail(candidate.id, 'manifest', new Error(`Duplicate plugin id at ${candidate.rootDir} and ${previous.rootDir}`), candidate.rootDir);
143
+ rejectedIds.add(candidate.id);
144
+ return;
149
145
  }
150
- catch (err) {
151
- console.warn(`[PluginLoader] Capability registration failed for "${manifest.id}":`, err);
152
- return null;
146
+ const ids = [candidate.id, ...(candidate.manifest.legacyPluginIds ?? [])];
147
+ for (const id of ids) {
148
+ const owner = aliases.get(id);
149
+ if (owner && owner !== candidate.id) {
150
+ this.fail(candidate.id, 'manifest', new Error(`Plugin id or legacy alias "${id}" conflicts with "${owner}"`), candidate.rootDir);
151
+ this.fail(owner, 'manifest', new Error(`Plugin id or legacy alias "${id}" conflicts with "${candidate.id}"`));
152
+ rejectedIds.add(candidate.id).add(owner);
153
+ }
153
154
  }
154
- const plugin = {
155
- id: manifest.id,
156
- manifest,
157
- definition,
158
- api,
159
- source: entryPoint,
160
- rootDir: pluginDir,
161
- registered: false,
162
- };
163
- this.plugins.set(manifest.id, plugin);
164
- return plugin;
155
+ for (const id of ids)
156
+ aliases.set(id, candidate.id);
157
+ candidates.set(candidate.id, candidate);
165
158
  }
166
- /**
167
- * 解析 plugin 入口文件
168
- *
169
- * 查找顺序:
170
- * 1. package.json 中的 main / exports
171
- * 2. index.ts / index.js
172
- * 3. src/index.ts / src/index.js
173
- */
174
- async resolveEntryPoint(pluginDir) {
175
- // 检查 package.json
176
- const pkgPath = path.join(pluginDir, 'package.json');
177
- if (existsSync(pkgPath)) {
178
- try {
179
- const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
180
- // main 字段
181
- if (pkg.main) {
182
- const main = path.resolve(pluginDir, pkg.main);
183
- if (existsSync(main))
184
- return main;
185
- }
159
+ applyAliases(candidates) {
160
+ for (const candidate of candidates.values()) {
161
+ const spellings = [candidate.id, ...(candidate.manifest.legacyPluginIds ?? [])];
162
+ const configured = spellings.filter((id) => this.config.entries?.[id] !== undefined);
163
+ if (configured.length > 1) {
164
+ this.fail(candidate.id, 'policy', new Error(`Plugin is configured under multiple identities: ${configured.join(', ')}`), candidate.rootDir);
165
+ candidate.rejected = true;
186
166
  }
187
- catch {
188
- // ignore
167
+ else if (configured.length === 1) {
168
+ candidate.config = this.config.entries?.[configured[0]];
189
169
  }
190
170
  }
191
- // 尝试常见入口文件
192
- const candidates = [
193
- 'index.ts', 'index.js', 'index.mjs',
194
- 'src/index.ts', 'src/index.js', 'src/index.mjs',
195
- 'dist/index.js', 'dist/index.mjs',
196
- ];
197
- for (const candidate of candidates) {
198
- const fullPath = path.join(pluginDir, candidate);
199
- if (existsSync(fullPath))
200
- return fullPath;
201
- }
202
- return null;
203
171
  }
204
- /**
205
- * 过滤 plugins(allowlist / blocklist / enabled)
206
- */
207
- filterPlugins(plugins) {
208
- return plugins.filter((plugin) => {
209
- // Blocklist
210
- if (this.config.blockList?.includes(plugin.id)) {
211
- console.info(`[PluginLoader] Plugin "${plugin.id}" is blocklisted, skipping`);
212
- return false;
213
- }
214
- // Allowlist
215
- if (this.config.allowList && this.config.allowList.length > 0) {
216
- if (!this.config.allowList.includes(plugin.id)) {
217
- console.info(`[PluginLoader] Plugin "${plugin.id}" not in allowlist, skipping`);
218
- return false;
219
- }
172
+ isEligible(candidate) {
173
+ if (candidate.rejected)
174
+ return false;
175
+ const ids = [candidate.id, ...(candidate.manifest.legacyPluginIds ?? [])];
176
+ if (this.config.blockList?.some((id) => ids.includes(id))) {
177
+ this.disabled(candidate, 'blocklisted');
178
+ return false;
179
+ }
180
+ if (this.config.allowList && !ids.some((id) => this.config.allowList.includes(id))) {
181
+ this.disabled(candidate, 'not in allowList');
182
+ return false;
183
+ }
184
+ const enabled = candidate.config?.enabled ?? candidate.manifest.enabledByDefault ?? true;
185
+ if (!enabled) {
186
+ this.disabled(candidate, 'disabled by configuration or manifest default');
187
+ return false;
188
+ }
189
+ return true;
190
+ }
191
+ prepareCandidate(candidate) {
192
+ try {
193
+ candidate.entryPoint = this.resolveEntry(candidate.rootDir, candidate.manifest.entry);
194
+ candidate.pluginConfig = validatePluginConfig(candidate.manifest, candidate.config?.config);
195
+ const engineVersion = this.config.engineVersion ?? getEngineVersion();
196
+ if (!satisfiesEngineRange(engineVersion, candidate.manifest.engine.octopi)) {
197
+ throw new Error(`Requires Octopi ${candidate.manifest.engine.octopi}; host is ${engineVersion}`);
220
198
  }
221
- // Enabled check
222
- const entryConfig = this.config.pluginEntries?.[plugin.id];
223
- if (entryConfig?.enabled === false) {
224
- console.info(`[PluginLoader] Plugin "${plugin.id}" is disabled, skipping`);
199
+ }
200
+ catch (error) {
201
+ this.fail(candidate.id, 'config', error, candidate.rootDir);
202
+ candidate.eligible = false;
203
+ }
204
+ }
205
+ markDependencyFailures(candidates, aliases) {
206
+ const visiting = new Set();
207
+ const failed = new Set();
208
+ const visit = (id) => {
209
+ const candidate = candidates.get(id);
210
+ if (!candidate || !candidate.eligible)
211
+ return false;
212
+ if (visiting.has(id)) {
213
+ this.fail(id, 'dependency', new Error('Circular plugin dependency'), candidate.rootDir);
214
+ candidate.eligible = false;
215
+ failed.add(id);
225
216
  return false;
226
217
  }
227
- // Manifest enabledByDefault check
228
- if (plugin.manifest.enabledByDefault !== true && !entryConfig) {
229
- // 如果没有显式配置且 manifest 没有设置 enabledByDefault,则跳过
230
- // 除非有其他触发条件
231
- const hasActivation = plugin.manifest.activation &&
232
- (plugin.manifest.activation.onProviders?.length ||
233
- plugin.manifest.activation.onChannels?.length ||
234
- plugin.manifest.activation.onCommands?.length);
235
- if (!hasActivation && !plugin.manifest.enabledByDefault) {
236
- console.info(`[PluginLoader] Plugin "${plugin.id}" not enabled by default and no config, skipping`);
237
- return false;
218
+ if (failed.has(id))
219
+ return false;
220
+ visiting.add(id);
221
+ for (const dependencyName of candidate.manifest.requiresPlugins ?? []) {
222
+ const dependency = aliases.get(dependencyName) ?? dependencyName;
223
+ if (!visit(dependency)) {
224
+ this.fail(id, 'dependency', new Error(`Missing or ineligible dependency "${dependencyName}" (resolved to "${dependency}")`), candidate.rootDir);
225
+ candidate.eligible = false;
226
+ failed.add(id);
227
+ break;
238
228
  }
239
229
  }
240
- // 依赖检查
241
- if (plugin.manifest.requiresPlugins) {
242
- for (const dep of plugin.manifest.requiresPlugins) {
243
- if (!plugins.some((p) => p.id === dep)) {
244
- console.warn(`[PluginLoader] Plugin "${plugin.id}" requires "${dep}" which is not loaded`);
245
- // 依赖缺失不阻塞加载,只警告
230
+ visiting.delete(id);
231
+ return candidate.eligible;
232
+ };
233
+ for (const id of candidates.keys())
234
+ visit(id);
235
+ }
236
+ checkDeclaredConflicts(candidates) {
237
+ const claims = new Map();
238
+ for (const candidate of candidates.values()) {
239
+ if (!candidate.eligible)
240
+ continue;
241
+ const entries = [
242
+ ...(candidate.manifest.contributes?.tools ?? []).map((name) => `tool:${name}`),
243
+ ...(candidate.manifest.contributes?.commands ?? []).map((command) => `command:${command.name}`),
244
+ ...(candidate.manifest.contributes?.services ?? []).map((name) => `service:${name}`),
245
+ ];
246
+ for (const claim of entries) {
247
+ const owner = claims.get(claim);
248
+ if (owner) {
249
+ this.fail(candidate.id, 'register', new Error(`Declared ${claim} conflicts with "${owner}"`), candidate.rootDir);
250
+ candidate.eligible = false;
251
+ const first = candidates.get(owner);
252
+ if (first) {
253
+ first.eligible = false;
254
+ this.fail(owner, 'register', new Error(`Declared ${claim} conflicts with "${candidate.id}"`), first.rootDir);
246
255
  }
247
256
  }
257
+ else
258
+ claims.set(claim, candidate.id);
248
259
  }
249
- return true;
250
- });
260
+ }
251
261
  }
252
- /**
253
- * 拓扑排序:按依赖关系排列 plugin 顺序
254
- *
255
- * 使用 Kahn 算法。被依赖的 plugin 排在前面。
256
- * 存在循环依赖时,剩余的 plugin 按原始顺序追加并警告。
257
- */
258
- resolveDependencyOrder(plugins) {
259
- const idToPlugin = new Map();
260
- for (const p of plugins) {
261
- idToPlugin.set(p.id, p);
262
- }
263
- // 计算入度(有多少 plugin 依赖它)
264
- const inDegree = new Map();
265
- const dependents = new Map(); // dep → [plugins that depend on it]
266
- for (const p of plugins) {
267
- if (!inDegree.has(p.id))
268
- inDegree.set(p.id, 0);
269
- const deps = (p.manifest.requiresPlugins ?? []).filter(d => idToPlugin.has(d));
270
- for (const dep of deps) {
271
- inDegree.set(p.id, (inDegree.get(p.id) ?? 0) + 1);
272
- if (!dependents.has(dep))
273
- dependents.set(dep, []);
274
- dependents.get(dep).push(p.id);
275
- }
262
+ async importAndRegister(candidate) {
263
+ const { manifest, rootDir, entryPoint, pluginConfig } = candidate;
264
+ if (!entryPoint || !pluginConfig) {
265
+ this.fail(manifest.id, 'config', new Error('Candidate has not completed pre-import validation'), rootDir);
266
+ return null;
276
267
  }
277
- // Kahn 算法
278
- const queue = [];
279
- for (const [id, deg] of inDegree) {
280
- if (deg === 0)
281
- queue.push(id);
282
- }
283
- const result = [];
284
- while (queue.length > 0) {
285
- const id = queue.shift();
286
- result.push(idToPlugin.get(id));
287
- for (const dep of (dependents.get(id) ?? [])) {
288
- const newDeg = (inDegree.get(dep) ?? 1) - 1;
289
- inDegree.set(dep, newDeg);
290
- if (newDeg === 0)
291
- queue.push(dep);
268
+ try {
269
+ const module = await import(pathToFileURL(entryPoint).href);
270
+ const definition = module.default;
271
+ if (!definition || definition.id !== manifest.id || typeof definition.register !== 'function') {
272
+ throw new Error(`Default entry must be definePlugin({ id: "${manifest.id}", register })`);
292
273
  }
274
+ const api = new PluginApi({
275
+ id: manifest.id,
276
+ name: manifest.name,
277
+ version: manifest.version,
278
+ ...(manifest.description === undefined ? {} : { description: manifest.description }),
279
+ source: rootDir,
280
+ rootDir,
281
+ pluginConfig,
282
+ });
283
+ await definition.register(api);
284
+ this.assertActualContributions(manifest, api);
285
+ return { manifest, definition, api, path: rootDir };
293
286
  }
294
- // 追加未解析的(循环依赖)
295
- const resolved = new Set(result.map(p => p.id));
296
- const remaining = plugins.filter(p => !resolved.has(p.id));
297
- if (remaining.length > 0) {
298
- console.warn(`[PluginLoader] Circular dependency detected among: ${remaining.map(p => p.id).join(', ')}. Appending in original order.`);
299
- result.push(...remaining);
287
+ catch (error) {
288
+ this.fail(manifest.id, 'register', error, rootDir);
289
+ return null;
300
290
  }
301
- return result;
302
291
  }
303
- /**
304
- * 执行 plugin 的 register()
305
- */
306
- async registerPlugin(plugin) {
307
- if (plugin.registered)
308
- return;
309
- try {
310
- await plugin.definition.register(plugin.api);
311
- plugin.registered = true;
312
- console.info(`[PluginLoader] Plugin "${plugin.id}" registered successfully`);
292
+ resolveEntry(rootDir, entry) {
293
+ const root = realpathSync(rootDir);
294
+ const candidate = resolve(root, entry);
295
+ if (relative(root, candidate).startsWith(`..${sep}`) || relative(root, candidate) === '..') {
296
+ throw new Error(`Plugin entry escapes its root: ${entry}`);
313
297
  }
314
- catch (err) {
315
- console.error(`[PluginLoader] Plugin "${plugin.id}" register() failed:`, err);
298
+ const real = realpathSync(candidate);
299
+ const rel = relative(root, real);
300
+ if (rel === '..' || rel.startsWith(`..${sep}`)) {
301
+ throw new Error(`Plugin entry realpath escapes its root: ${entry}`);
316
302
  }
303
+ return real;
317
304
  }
318
- /**
319
- * 获取已加载的 plugin
320
- */
321
- getPlugin(id) {
322
- return this.plugins.get(id);
305
+ assertActualContributions(manifest, api) {
306
+ const staged = readStaging(api);
307
+ const declaredTools = new Set(manifest.contributes?.tools ?? []);
308
+ const actualTools = new Set(staged.tools.map((tool) => tool.definition.name));
309
+ const declaredCommands = new Map((manifest.contributes?.commands ?? []).map((command) => [command.name, command.sessionOps ?? []]));
310
+ for (const tool of actualTools) {
311
+ if (!declaredTools.has(tool))
312
+ throw new Error(`Tool "${tool}" is not declared in manifest`);
313
+ }
314
+ for (const tool of declaredTools) {
315
+ if (!actualTools.has(tool))
316
+ throw new Error(`Declared tool "${tool}" was not registered`);
317
+ }
318
+ const actualCommands = new Set(staged.commands.map((command) => command.name));
319
+ for (const command of actualCommands) {
320
+ if (!declaredCommands.has(command))
321
+ throw new Error(`Command "${command}" is not declared in manifest`);
322
+ }
323
+ for (const command of declaredCommands.keys()) {
324
+ if (!actualCommands.has(command))
325
+ throw new Error(`Declared command "${command}" was not registered`);
326
+ }
327
+ for (const command of staged.commands) {
328
+ const declared = declaredCommands.get(command.name) ?? [];
329
+ const actual = command.sessionOps ?? [];
330
+ if (JSON.stringify([...declared].sort()) !== JSON.stringify([...actual].sort())) {
331
+ throw new Error(`Command "${command.name}" sessionOps differ from manifest`);
332
+ }
333
+ }
334
+ const declaredHooks = new Set(manifest.contributes?.hooks ?? []);
335
+ for (const hook of staged.hooks.keys()) {
336
+ if (!declaredHooks.has(hook))
337
+ throw new Error(`Hook "${hook}" is not declared in manifest`);
338
+ }
339
+ for (const hook of declaredHooks) {
340
+ if (!staged.hooks.has(hook))
341
+ throw new Error(`Declared hook "${hook}" was not registered`);
342
+ }
343
+ const declaredServices = new Set(manifest.contributes?.services ?? []);
344
+ const actualServices = new Set(staged.services.map((service) => service.serviceId));
345
+ for (const service of actualServices) {
346
+ if (!declaredServices.has(service))
347
+ throw new Error(`Service "${service}" is not declared in manifest`);
348
+ }
349
+ for (const service of declaredServices) {
350
+ if (!actualServices.has(service))
351
+ throw new Error(`Declared service "${service}" was not registered`);
352
+ }
323
353
  }
324
- /**
325
- * 获取所有已加载的 plugins
326
- */
327
- getAllPlugins() {
328
- return [...this.plugins.values()];
354
+ disabled(candidate, reason) {
355
+ this.diagnostics.push({ id: candidate.id, status: 'disabled', stage: 'policy', reason, path: candidate.rootDir });
329
356
  }
330
- /**
331
- * 获取所有已注册的 plugin IDs
332
- */
333
- getRegisteredIds() {
334
- return [...this.plugins.values()].filter((p) => p.registered).map((p) => p.id);
357
+ fail(id, stage, error, path) {
358
+ this.diagnostics.push({ id, status: 'failed', stage, reason: errorMessage(error), ...(path === undefined ? {} : { path }) });
335
359
  }
360
+ getDiagnostics() { return this.diagnostics.map((item) => ({ ...item })); }
361
+ getRegisteredIds() { return [...this.plugins.keys()]; }
362
+ getPlugin(id) { return this.plugins.get(id); }
363
+ getAllPlugins() { return [...this.plugins.values()]; }
364
+ }
365
+ function errorMessage(error) {
366
+ return error instanceof Error ? error.message : String(error);
336
367
  }