doukui 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +85 -18
- package/README.md +8 -8
- package/bin/commands/help.js +2 -0
- package/bin/commands/plan-lint.js +105 -0
- package/bin/commands/verify.js +263 -0
- package/bin/doukui.js +2 -0
- package/package.json +1 -1
- package/plugins/core/hook-session-start/index.js +1 -1
- package/plugins/core/hook-skill-gate/index.js +64 -15
- package/plugins/core/hook-skill-gate/plugin.json +1 -1
- package/plugins/core/skill-agent-dispatcher/cleanup-reference.md +7 -28
- package/plugins/core/skill-agent-dispatcher/monitoring-reference.md +5 -177
- package/plugins/core/skill-agent-dispatcher/plugin.json +1 -1
- package/plugins/core/skill-agent-dispatcher/recovery-reference.md +99 -58
- package/plugins/core/skill-agent-dispatcher/roles-reference.md +9 -22
- package/plugins/core/skill-agent-dispatcher/skill.md +112 -524
- package/plugins/core/skill-agentic-loop/mirror-rules.md +15 -3
- package/plugins/core/skill-agentic-loop/plugin.json +1 -1
- package/plugins/core/skill-agentic-loop/skill.md +123 -324
- package/plugins/core/skill-batch-task-creator/plugin.json +1 -1
- package/plugins/core/skill-checklist/skill.md +15 -141
- package/plugins/core/skill-completion-report/plugin.json +1 -1
- package/plugins/core/skill-doukui-plan/plugin.json +1 -1
- package/plugins/core/skill-doukui-plan/skill.md +22 -22
- package/plugins/core/skill-experience-logger/plugin.json +1 -1
- package/plugins/core/skill-human-checkpoint/skill.md +1 -0
- package/plugins/core/skill-split-work-package/plugin.json +1 -1
- package/plugins/core/skill-task-creator/plugin.json +1 -1
- package/plugins/core/skill-task-creator/skill.md +1 -139
- package/plugins/core/skill-workflow-orchestrator/plugin.json +1 -1
- package/plugins/plugin-registry.json +0 -36
- package/plugins/runtime/harness-build.js +5 -5
- package/plugins/runtime/plan-reader.js +21 -3
- package/plugins/runtime/resolve-plugin-path.js +2 -2
- package/plugins/contracts/capabilities.js +0 -268
- package/plugins/core/provider-memory-store/index.js +0 -510
- package/plugins/core/provider-memory-store/plugin.json +0 -9
- package/plugins/core/provider-state-store/index.js +0 -244
- package/plugins/core/provider-state-store/plugin.json +0 -9
- package/plugins/core/provider-watchdog/assets/daemon-config.template.json +0 -32
- package/plugins/core/provider-watchdog/assets/lib/watchdog-multi-window.js +0 -676
- package/plugins/core/provider-watchdog/assets/watchdog.js +0 -754
- package/plugins/core/provider-watchdog/index.js +0 -151
- package/plugins/core/provider-watchdog/plugin.json +0 -13
- package/plugins/core/skill-agent-dispatcher/daemon-helpers-reference.md +0 -249
- package/plugins/core/skill-watchdog-manager/plugin.json +0 -10
- package/plugins/core/skill-watchdog-manager/skill.md +0 -143
- package/plugins/core/validator-doc-sync/index.js +0 -248
- package/plugins/core/validator-doc-sync/plugin.json +0 -12
- package/plugins/core/validator-work-package/index.js +0 -300
- package/plugins/core/validator-work-package/plugin.json +0 -12
- package/plugins/runtime/audit-logger.js +0 -311
- package/plugins/runtime/build-cli.js +0 -89
- package/plugins/runtime/event-bus.js +0 -207
- package/plugins/runtime/hook-dispatcher.js +0 -315
- package/plugins/runtime/plugin-loader.js +0 -798
- package/plugins/runtime/plugin-trust.js +0 -182
- package/plugins/runtime/sandbox-context.js +0 -235
- package/plugins/runtime/sandbox-manager.js +0 -609
- package/plugins/runtime/sandbox-worker.js +0 -215
- package/plugins/runtime/text-tokenize.js +0 -24
- package/plugins/runtime/validator-pipeline.js +0 -475
|
@@ -1,798 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PluginLoader - Plugin discovery, dependency resolution, and lifecycle management
|
|
3
|
-
*
|
|
4
|
-
* @module plugin-loader
|
|
5
|
-
*
|
|
6
|
-
* Features:
|
|
7
|
-
* - Load plugins from plugin-registry.json
|
|
8
|
-
* - Topological sort for dependency order
|
|
9
|
-
* - Circular dependency detection
|
|
10
|
-
* - Lifecycle management: load -> activate -> deactivate
|
|
11
|
-
* - Graceful handling of empty registries
|
|
12
|
-
* - Error isolation per plugin
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
// FILE-SIZE-MONITOR: 645 lines (as of 2026-05-31)
|
|
16
|
-
// SPLIT-THRESHOLD: 800 lines — if exceeded, consider splitting into:
|
|
17
|
-
// - plugin-loader-core.js (loading, activation, dependency injection)
|
|
18
|
-
// - plugin-loader-lifecycle.js (lifecycle management, event handling)
|
|
19
|
-
|
|
20
|
-
'use strict';
|
|
21
|
-
|
|
22
|
-
var fs = require('fs');
|
|
23
|
-
var path = require('path');
|
|
24
|
-
var pluginInterface = require('../contracts/plugin-interface');
|
|
25
|
-
var PluginState = pluginInterface.PluginState;
|
|
26
|
-
var PluginType = pluginInterface.PluginType;
|
|
27
|
-
var SkillPlugin = pluginInterface.SkillPlugin;
|
|
28
|
-
var HookPlugin = pluginInterface.HookPlugin;
|
|
29
|
-
var ValidatorPlugin = pluginInterface.ValidatorPlugin;
|
|
30
|
-
var ProviderPlugin = pluginInterface.ProviderPlugin;
|
|
31
|
-
var HookDispatcher = require('./hook-dispatcher').HookDispatcher;
|
|
32
|
-
var ValidatorPipeline = require('./validator-pipeline').ValidatorPipeline;
|
|
33
|
-
|
|
34
|
-
class PluginLoader {
|
|
35
|
-
/**
|
|
36
|
-
* @public
|
|
37
|
-
* @param {object} options
|
|
38
|
-
* @param {string} options.registryPath - path to plugin-registry.json
|
|
39
|
-
* @param {object} options.eventBus - EventBus instance
|
|
40
|
-
* @param {object} options.stateStore - StateStore instance
|
|
41
|
-
* @param {object} options.configManager - ConfigManager instance
|
|
42
|
-
* @param {object} options.logger - Logger instance
|
|
43
|
-
*/
|
|
44
|
-
constructor(options) {
|
|
45
|
-
options = options || {};
|
|
46
|
-
this._registryPath = options.registryPath || path.join(process.cwd(), 'plugins', 'plugin-registry.json');
|
|
47
|
-
this._projectRoot = options.projectRoot || (options.registryPath ? path.dirname(path.dirname(options.registryPath)) : process.cwd());
|
|
48
|
-
this._eventBus = options.eventBus || null;
|
|
49
|
-
this._stateStore = options.stateStore || null;
|
|
50
|
-
this._configManager = options.configManager || null;
|
|
51
|
-
this._logger = options.logger || null;
|
|
52
|
-
|
|
53
|
-
/** @type {Map<string, object>} loaded plugin instances */
|
|
54
|
-
this.loadedPlugins = new Map();
|
|
55
|
-
/** @type {Map<string, object>} plugin configs from registry */
|
|
56
|
-
this._pluginConfigs = new Map();
|
|
57
|
-
/** @type {object} registry data */
|
|
58
|
-
this._registry = null;
|
|
59
|
-
/** @type {Map<string, object>} provider registry - factory outputs */
|
|
60
|
-
this._providerRegistry = new Map();
|
|
61
|
-
/** @type {HookDispatcher} hook dispatcher for internal mode */
|
|
62
|
-
this._hookDispatcher = null;
|
|
63
|
-
/** @type {ValidatorPipeline} validator pipeline for automated validation */
|
|
64
|
-
this._validatorPipeline = null;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// --- public API ---
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Load and activate all plugins from the registry.
|
|
71
|
-
* Respects dependency order via topological sort.
|
|
72
|
-
* Handles empty registries gracefully.
|
|
73
|
-
*
|
|
74
|
-
* @public
|
|
75
|
-
* @returns {Promise<string[]>} names of successfully loaded plugins
|
|
76
|
-
*/
|
|
77
|
-
async loadAll() {
|
|
78
|
-
this._log('info', 'Starting plugin loading...');
|
|
79
|
-
|
|
80
|
-
// 1. Read registry
|
|
81
|
-
this._registry = this._readRegistry();
|
|
82
|
-
var pluginNames = this._getPluginNames();
|
|
83
|
-
|
|
84
|
-
if (pluginNames.length === 0) {
|
|
85
|
-
this._log('info', 'Registry is empty, no plugins to load');
|
|
86
|
-
return [];
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// 2. Build dependency graph from registry entries
|
|
90
|
-
var depGraph = this._buildDependencyGraph(pluginNames);
|
|
91
|
-
|
|
92
|
-
// 3. Topological sort (with cycle detection)
|
|
93
|
-
var loadOrder = this._topologicalSort(depGraph);
|
|
94
|
-
|
|
95
|
-
this._log('info', 'Load order resolved: ' + loadOrder.join(', '));
|
|
96
|
-
|
|
97
|
-
// 4. Load and activate in order
|
|
98
|
-
var loaded = [];
|
|
99
|
-
for (var i = 0; i < loadOrder.length; i++) {
|
|
100
|
-
var name = loadOrder[i];
|
|
101
|
-
var config = this._pluginConfigs.get(name);
|
|
102
|
-
|
|
103
|
-
if (config && config.enabled === false) {
|
|
104
|
-
this._log('info', 'Plugin "' + name + '" is disabled, skipping');
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
await this._loadPlugin(name, config);
|
|
110
|
-
loaded.push(name);
|
|
111
|
-
if (this._eventBus) {
|
|
112
|
-
this._eventBus.emit('plugin:loaded', { pluginName: name });
|
|
113
|
-
}
|
|
114
|
-
await this.activate(name);
|
|
115
|
-
this._log('info', 'Plugin "' + name + '" loaded and activated successfully');
|
|
116
|
-
} catch (err) {
|
|
117
|
-
this._log('error', 'Failed to load plugin "' + name + '": ' + err.message);
|
|
118
|
-
// Non-critical plugins don't block others
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
this._log('info', 'Plugin loading complete. ' + loaded.length + ' plugins loaded');
|
|
123
|
-
return loaded;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Activate a single loaded plugin.
|
|
128
|
-
* Creates PluginContext and injects EventBus, StateStore, ConfigManager, Logger.
|
|
129
|
-
* For Provider plugins, calls factory() and registers the output.
|
|
130
|
-
* @public
|
|
131
|
-
* @param {string} name - plugin name
|
|
132
|
-
*/
|
|
133
|
-
async activate(name) {
|
|
134
|
-
var plugin = this.loadedPlugins.get(name);
|
|
135
|
-
if (!plugin) {
|
|
136
|
-
throw new Error('Plugin "' + name + '" is not loaded');
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
var PluginContext = require('../contracts/plugin-interface').PluginContext;
|
|
140
|
-
|
|
141
|
-
// Create runtime object with all services
|
|
142
|
-
var runtime = {
|
|
143
|
-
eventBus: this._eventBus,
|
|
144
|
-
stateStore: this._stateStore,
|
|
145
|
-
configManager: this._configManager,
|
|
146
|
-
logger: this._logger ? this._logger.createChild(name) : null,
|
|
147
|
-
getProvider: this._getProvider.bind(this),
|
|
148
|
-
loadedPlugins: this.loadedPlugins,
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
var context = new PluginContext(name, runtime);
|
|
152
|
-
|
|
153
|
-
if (typeof plugin.onActivate === 'function') {
|
|
154
|
-
await plugin.onActivate(context);
|
|
155
|
-
}
|
|
156
|
-
plugin.state = PluginState.ACTIVATED;
|
|
157
|
-
|
|
158
|
-
// For Provider plugins, call factory() and register the output
|
|
159
|
-
if (plugin.type === PluginType.PROVIDER && typeof plugin.factory === 'function') {
|
|
160
|
-
var providerInstance = await plugin.factory(context);
|
|
161
|
-
// Register under provides names (e.g. "provider:state-store") and fallback to plugin name
|
|
162
|
-
var provides = plugin.provides || [];
|
|
163
|
-
var registeredAs = [];
|
|
164
|
-
for (var i = 0; i < provides.length; i++) {
|
|
165
|
-
var provName = provides[i].replace(/^provider:/, '');
|
|
166
|
-
this._providerRegistry.set(provName, providerInstance);
|
|
167
|
-
registeredAs.push(provName);
|
|
168
|
-
}
|
|
169
|
-
// Also register under full plugin name for backward compat
|
|
170
|
-
this._providerRegistry.set(name, providerInstance);
|
|
171
|
-
registeredAs.push(name);
|
|
172
|
-
this._log('info', 'Provider "' + name + '" factory() called and registered as: ' + registeredAs.join(', '));
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (this._eventBus) {
|
|
176
|
-
this._eventBus.emit('plugin:activated', { pluginName: name });
|
|
177
|
-
}
|
|
178
|
-
this._log('info', 'Plugin "' + name + '" activated');
|
|
179
|
-
|
|
180
|
-
// Initialize HookDispatcher after first hook is activated
|
|
181
|
-
if (plugin.type === PluginType.HOOK && !this._hookDispatcher) {
|
|
182
|
-
this._hookDispatcher = new HookDispatcher({
|
|
183
|
-
pluginLoader: this,
|
|
184
|
-
logger: this._logger,
|
|
185
|
-
});
|
|
186
|
-
this._log('info', 'HookDispatcher initialized');
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Initialize ValidatorPipeline after first validator is activated
|
|
190
|
-
if (plugin.type === PluginType.VALIDATOR && !this._validatorPipeline) {
|
|
191
|
-
this._validatorPipeline = new ValidatorPipeline({
|
|
192
|
-
pluginLoader: this,
|
|
193
|
-
eventBus: this._eventBus,
|
|
194
|
-
logger: this._logger,
|
|
195
|
-
});
|
|
196
|
-
this._log('info', 'ValidatorPipeline initialized');
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Deactivate a single plugin.
|
|
202
|
-
* @public
|
|
203
|
-
* @param {string} name
|
|
204
|
-
*/
|
|
205
|
-
async deactivate(name) {
|
|
206
|
-
var plugin = this.loadedPlugins.get(name);
|
|
207
|
-
if (!plugin) {
|
|
208
|
-
throw new Error('Plugin "' + name + '" is not loaded');
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (typeof plugin.onDeactivate === 'function') {
|
|
212
|
-
await plugin.onDeactivate();
|
|
213
|
-
}
|
|
214
|
-
plugin.state = PluginState.DEACTIVATED;
|
|
215
|
-
|
|
216
|
-
if (this._eventBus) {
|
|
217
|
-
this._eventBus.emit('plugin:deactivated', { pluginName: name });
|
|
218
|
-
}
|
|
219
|
-
this._log('info', 'Plugin "' + name + '" deactivated');
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Deactivate all loaded plugins in reverse order.
|
|
224
|
-
* @public
|
|
225
|
-
*/
|
|
226
|
-
async deactivateAll() {
|
|
227
|
-
var names = Array.from(this.loadedPlugins.keys()).reverse();
|
|
228
|
-
for (var i = 0; i < names.length; i++) {
|
|
229
|
-
try {
|
|
230
|
-
await this.deactivate(names[i]);
|
|
231
|
-
} catch (err) {
|
|
232
|
-
this._log('error', 'Error deactivating "' + names[i] + '": ' + err.message);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Get a loaded plugin by name.
|
|
239
|
-
* @public
|
|
240
|
-
* @param {string} name
|
|
241
|
-
* @returns {object|undefined}
|
|
242
|
-
*/
|
|
243
|
-
getPlugin(name) {
|
|
244
|
-
return this.loadedPlugins.get(name);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Check if a plugin is loaded.
|
|
249
|
-
* @public
|
|
250
|
-
* @param {string} name
|
|
251
|
-
* @returns {boolean}
|
|
252
|
-
*/
|
|
253
|
-
isLoaded(name) {
|
|
254
|
-
return this.loadedPlugins.has(name);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Get names of all loaded plugins.
|
|
259
|
-
* @public
|
|
260
|
-
* @returns {string[]}
|
|
261
|
-
*/
|
|
262
|
-
getLoadedNames() {
|
|
263
|
-
return Array.from(this.loadedPlugins.keys());
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Get a registered Provider instance by name.
|
|
268
|
-
* @public
|
|
269
|
-
* @param {string} name - provider name
|
|
270
|
-
* @returns {object|undefined} Provider instance from factory()
|
|
271
|
-
*/
|
|
272
|
-
getProvider(name) {
|
|
273
|
-
return this._providerRegistry.get(name);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* Get all registered Provider names.
|
|
278
|
-
* @public
|
|
279
|
-
* @returns {string[]}
|
|
280
|
-
*/
|
|
281
|
-
getRegisteredProviders() {
|
|
282
|
-
return Array.from(this._providerRegistry.keys());
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Get the HookDispatcher instance for internal hook execution.
|
|
287
|
-
* Returns null if no hooks have been activated yet.
|
|
288
|
-
* @public
|
|
289
|
-
* @returns {HookDispatcher|null}
|
|
290
|
-
*/
|
|
291
|
-
getHookDispatcher() {
|
|
292
|
-
return this._hookDispatcher;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Get the ValidatorPipeline instance for automated validation.
|
|
297
|
-
* Returns null if no validators have been activated yet.
|
|
298
|
-
* @public
|
|
299
|
-
* @returns {ValidatorPipeline|null}
|
|
300
|
-
*/
|
|
301
|
-
getValidatorPipeline() {
|
|
302
|
-
return this._validatorPipeline;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* Dispatch a hook event using internal mode.
|
|
307
|
-
* Shortcut for HookDispatcher.dispatch() with mode='internal'.
|
|
308
|
-
*
|
|
309
|
-
* @public
|
|
310
|
-
* @param {object} context - hook context { event, tool?, skill? }
|
|
311
|
-
* @returns {Promise<{ allowed: boolean, results?, reason? }>}
|
|
312
|
-
*/
|
|
313
|
-
async dispatchHook(context) {
|
|
314
|
-
if (!this._hookDispatcher) {
|
|
315
|
-
this._log('warn', 'HookDispatcher not initialized, cannot dispatch hook');
|
|
316
|
-
return { allowed: true };
|
|
317
|
-
}
|
|
318
|
-
context.mode = 'internal';
|
|
319
|
-
return await this._hookDispatcher.dispatch(context);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// --- internal ---
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Read and parse the registry file.
|
|
326
|
-
* @internal
|
|
327
|
-
* @returns {object}
|
|
328
|
-
*/
|
|
329
|
-
_readRegistry() {
|
|
330
|
-
try {
|
|
331
|
-
var content = fs.readFileSync(this._registryPath, 'utf-8');
|
|
332
|
-
return JSON.parse(content);
|
|
333
|
-
} catch (err) {
|
|
334
|
-
// If file doesn't exist or is invalid, return empty registry
|
|
335
|
-
this._log('warn', 'Could not read registry: ' + err.message + '. Using empty registry.');
|
|
336
|
-
return { version: '1.0.0', plugins: [] };
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* Extract plugin names and configs from registry.
|
|
342
|
-
* Supports both array format and object format.
|
|
343
|
-
* @internal
|
|
344
|
-
* @returns {string[]}
|
|
345
|
-
*/
|
|
346
|
-
_getPluginNames() {
|
|
347
|
-
var plugins = this._registry.plugins;
|
|
348
|
-
if (!plugins) return [];
|
|
349
|
-
|
|
350
|
-
// Array format: [{ name, source, enabled, config }]
|
|
351
|
-
if (Array.isArray(plugins)) {
|
|
352
|
-
var names = [];
|
|
353
|
-
for (var i = 0; i < plugins.length; i++) {
|
|
354
|
-
var entry = plugins[i];
|
|
355
|
-
var name = entry.name || entry;
|
|
356
|
-
this._pluginConfigs.set(name, entry);
|
|
357
|
-
names.push(name);
|
|
358
|
-
}
|
|
359
|
-
return names;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// Object format: { name: { source, enabled, config } }
|
|
363
|
-
if (typeof plugins === 'object') {
|
|
364
|
-
var keys = Object.keys(plugins);
|
|
365
|
-
for (var j = 0; j < keys.length; j++) {
|
|
366
|
-
this._pluginConfigs.set(keys[j], plugins[keys[j]]);
|
|
367
|
-
}
|
|
368
|
-
return keys;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
return [];
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Build a dependency graph from plugin configs.
|
|
376
|
-
*
|
|
377
|
-
* Supports two dependency declaration styles (B2 fix):
|
|
378
|
-
*
|
|
379
|
-
* 1. **Top-level `dependencies` string array** (canonical plugin.json schema,
|
|
380
|
-
* e.g. `"dependencies": ["provider:state-store", "base-plugin"]`).
|
|
381
|
-
* Each entry is classified:
|
|
382
|
-
* - `provider:xxx` or a name that resolves to a provider → provider edge
|
|
383
|
-
* - otherwise → plain plugin-name edge
|
|
384
|
-
* Short names without the `provider:` prefix are also resolved against
|
|
385
|
-
* the provider map (e.g. `["state-store"]`).
|
|
386
|
-
*
|
|
387
|
-
* 2. **Legacy `config.dependencies.{plugins,providers}` object form**
|
|
388
|
-
* (used by older test fixtures). Still accepted for backward compat.
|
|
389
|
-
*
|
|
390
|
-
* Provider dependencies are resolved against the provider-name → plugin-name
|
|
391
|
-
* map built by `_buildProviderMap` so that a plugin depending on a provider
|
|
392
|
-
* is loaded after the plugin that provides it.
|
|
393
|
-
*
|
|
394
|
-
* @internal
|
|
395
|
-
* @param {string[]} pluginNames
|
|
396
|
-
* @returns {Map<string, string[]>} name -> dependency names
|
|
397
|
-
*/
|
|
398
|
-
_buildDependencyGraph(pluginNames) {
|
|
399
|
-
var graph = new Map();
|
|
400
|
-
|
|
401
|
-
// Phase 1: Build a provider-name -> plugin-name mapping by scanning plugin.json.
|
|
402
|
-
// This also primes `_pluginJsonCache` so we can reuse the parsed plugin.json
|
|
403
|
-
// below without re-resolving paths.
|
|
404
|
-
var providerToPlugin = this._buildProviderMap(pluginNames);
|
|
405
|
-
|
|
406
|
-
// Index of plugin names for quick "is this a plain plugin dep?" lookup
|
|
407
|
-
var nameSet = new Set(pluginNames);
|
|
408
|
-
|
|
409
|
-
// Phase 2: Build dependency edges
|
|
410
|
-
for (var i = 0; i < pluginNames.length; i++) {
|
|
411
|
-
var name = pluginNames[i];
|
|
412
|
-
var entry = this._pluginConfigs.get(name);
|
|
413
|
-
var deps = [];
|
|
414
|
-
|
|
415
|
-
// Reuse the plugin.json already scanned in Phase 1 (fault-tolerant: {} on miss)
|
|
416
|
-
var pluginJson = this._pluginJsonCache && this._pluginJsonCache.get(name) || {};
|
|
417
|
-
|
|
418
|
-
// --- Canonical form: top-level `dependencies` string array on plugin.json
|
|
419
|
-
// (may also appear mirrored on entry.config.dependencies as array) ---
|
|
420
|
-
var topLevelDeps = [];
|
|
421
|
-
if (pluginJson && Array.isArray(pluginJson.dependencies)) {
|
|
422
|
-
topLevelDeps = topLevelDeps.concat(pluginJson.dependencies);
|
|
423
|
-
}
|
|
424
|
-
if (entry && entry.config && Array.isArray(entry.config.dependencies)) {
|
|
425
|
-
topLevelDeps = topLevelDeps.concat(entry.config.dependencies);
|
|
426
|
-
}
|
|
427
|
-
this._addClassifiedDeps(deps, topLevelDeps, providerToPlugin, nameSet, name);
|
|
428
|
-
|
|
429
|
-
// --- Legacy form: config.dependencies.{plugins,providers} object ---
|
|
430
|
-
if (entry && entry.config && entry.config.dependencies &&
|
|
431
|
-
!Array.isArray(entry.config.dependencies)) {
|
|
432
|
-
// Plugin dependencies: entry.config.dependencies.plugins
|
|
433
|
-
var pluginDeps = entry.config.dependencies.plugins || [];
|
|
434
|
-
if (Array.isArray(pluginDeps)) {
|
|
435
|
-
for (var j = 0; j < pluginDeps.length; j++) {
|
|
436
|
-
this._addPluginDep(deps, pluginDeps[j], nameSet, name);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
// Provider dependencies: entry.config.dependencies.providers
|
|
441
|
-
var providerDeps = entry.config.dependencies.providers || [];
|
|
442
|
-
if (Array.isArray(providerDeps)) {
|
|
443
|
-
for (var k = 0; k < providerDeps.length; k++) {
|
|
444
|
-
this._addProviderDep(deps, providerDeps[k], providerToPlugin, name);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
graph.set(name, deps);
|
|
450
|
-
}
|
|
451
|
-
return graph;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
* Classify and append a list of canonical (top-level) dependency tokens.
|
|
456
|
-
* A token is either a `provider:` reference (or a short provider name) or
|
|
457
|
-
* a plain plugin name.
|
|
458
|
-
* @internal
|
|
459
|
-
*/
|
|
460
|
-
_addClassifiedDeps(deps, tokens, providerToPlugin, nameSet, consumerName) {
|
|
461
|
-
for (var i = 0; i < tokens.length; i++) {
|
|
462
|
-
var tok = tokens[i];
|
|
463
|
-
if (typeof tok !== 'string' || tok === '') continue;
|
|
464
|
-
// Any token that maps to a provider (full or short name) → provider edge
|
|
465
|
-
if (providerToPlugin.has(tok) || providerToPlugin.has('provider:' + tok)) {
|
|
466
|
-
var resolved = providerToPlugin.get(tok) || providerToPlugin.get('provider:' + tok);
|
|
467
|
-
if (resolved && deps.indexOf(resolved) === -1) {
|
|
468
|
-
deps.push(resolved);
|
|
469
|
-
}
|
|
470
|
-
continue;
|
|
471
|
-
}
|
|
472
|
-
// Token explicitly written with a capability prefix we don't recognize
|
|
473
|
-
if (tok.indexOf(':') !== -1) {
|
|
474
|
-
this._log('warn', 'Plugin "' + consumerName + '" depends on provider "' + tok +
|
|
475
|
-
'" which is not provided by any plugin');
|
|
476
|
-
continue;
|
|
477
|
-
}
|
|
478
|
-
// Otherwise treat as plain plugin-name dependency
|
|
479
|
-
this._addPluginDep(deps, tok, nameSet, consumerName);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* Append a plain plugin-name dependency edge (dedup).
|
|
485
|
-
* @internal
|
|
486
|
-
*/
|
|
487
|
-
_addPluginDep(deps, depName, nameSet, consumerName) {
|
|
488
|
-
if (typeof depName !== 'string' || depName === '') return;
|
|
489
|
-
if (!nameSet.has(depName)) {
|
|
490
|
-
// Unknown dep will be flagged during topological sort; keep edge so the
|
|
491
|
-
// error surfaces rather than silently dropping it.
|
|
492
|
-
this._log('warn', 'Plugin "' + consumerName + '" depends on unknown plugin "' + depName + '"');
|
|
493
|
-
}
|
|
494
|
-
if (deps.indexOf(depName) === -1) {
|
|
495
|
-
deps.push(depName);
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
/**
|
|
500
|
-
* Append a provider dependency edge (dedup), resolving full or short name.
|
|
501
|
-
* @internal
|
|
502
|
-
*/
|
|
503
|
-
_addProviderDep(deps, providerName, providerToPlugin, consumerName) {
|
|
504
|
-
if (typeof providerName !== 'string' || providerName === '') return;
|
|
505
|
-
var resolved = providerToPlugin.get(providerName) ||
|
|
506
|
-
providerToPlugin.get('provider:' + providerName);
|
|
507
|
-
if (resolved) {
|
|
508
|
-
if (deps.indexOf(resolved) === -1) {
|
|
509
|
-
deps.push(resolved);
|
|
510
|
-
}
|
|
511
|
-
} else {
|
|
512
|
-
this._log('warn', 'Plugin "' + consumerName + '" depends on provider "' + providerName +
|
|
513
|
-
'" which is not provided by any plugin');
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* Build a mapping from provider names to plugin names by scanning plugin.json files.
|
|
519
|
-
* Reads each plugin's plugin.json to discover the "provides" field.
|
|
520
|
-
*
|
|
521
|
-
* The `provides` field may be either an array of capability strings
|
|
522
|
-
* (e.g. `["provider:state-store", "hook:session-start"]`) or a single
|
|
523
|
-
* capability string (e.g. `"provider:state-store"`). Both forms are
|
|
524
|
-
* normalized into the map under both the full (`provider:state-store`)
|
|
525
|
-
* and short (`state-store`) keys.
|
|
526
|
-
*
|
|
527
|
-
* @internal
|
|
528
|
-
* @param {string[]} pluginNames
|
|
529
|
-
* @returns {Map<string, string>} provider-name -> plugin-name
|
|
530
|
-
*/
|
|
531
|
-
_buildProviderMap(pluginNames) {
|
|
532
|
-
var providerToPlugin = new Map();
|
|
533
|
-
// Prime a name -> plugin.json cache so _buildDependencyGraph can reuse
|
|
534
|
-
// the same parsed plugin.json without re-resolving paths. Fault-tolerant:
|
|
535
|
-
// any plugin whose plugin.json can't be resolved/read is stored as {}.
|
|
536
|
-
if (!this._pluginJsonCache) {
|
|
537
|
-
this._pluginJsonCache = new Map();
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
for (var i = 0; i < pluginNames.length; i++) {
|
|
541
|
-
var name = pluginNames[i];
|
|
542
|
-
var entry = this._pluginConfigs.get(name);
|
|
543
|
-
|
|
544
|
-
var pluginJson = {};
|
|
545
|
-
try {
|
|
546
|
-
pluginJson = this._readPluginJsonForEntry(name, entry);
|
|
547
|
-
} catch (err) {
|
|
548
|
-
// If we can't resolve or read, skip — will be caught later during load
|
|
549
|
-
this._log('warn', 'Could not scan plugin.json for "' + name + '": ' + err.message);
|
|
550
|
-
}
|
|
551
|
-
this._pluginJsonCache.set(name, pluginJson);
|
|
552
|
-
|
|
553
|
-
var provides = pluginJson && pluginJson.provides;
|
|
554
|
-
if (!provides) continue;
|
|
555
|
-
|
|
556
|
-
// Normalize `provides` to an array of strings
|
|
557
|
-
var providesList = Array.isArray(provides) ? provides :
|
|
558
|
-
(typeof provides === 'string' ? [provides] : []);
|
|
559
|
-
|
|
560
|
-
for (var j = 0; j < providesList.length; j++) {
|
|
561
|
-
var cap = String(providesList[j]);
|
|
562
|
-
if (!cap) continue;
|
|
563
|
-
providerToPlugin.set(cap, name);
|
|
564
|
-
// Also store the short form (capability stripped of its prefix)
|
|
565
|
-
var shortName = cap.replace(/^[A-Za-z]+:/, '');
|
|
566
|
-
if (shortName && shortName !== cap) {
|
|
567
|
-
providerToPlugin.set(shortName, name);
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
return providerToPlugin;
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
/**
|
|
576
|
-
* Resolve the plugin directory for an entry and read its plugin.json.
|
|
577
|
-
* Shared by `_buildDependencyGraph` and `_buildProviderMap` so both phases
|
|
578
|
-
* see the same canonical plugin.json (rather than one reading plugin.json
|
|
579
|
-
* and the other reading only the registry entry's config).
|
|
580
|
-
* @internal
|
|
581
|
-
* @param {string} name
|
|
582
|
-
* @param {object} [entry]
|
|
583
|
-
* @returns {object} parsed plugin.json (empty object on failure)
|
|
584
|
-
*/
|
|
585
|
-
_readPluginJsonForEntry(name, entry) {
|
|
586
|
-
var source = entry && entry.source ? entry.source : name;
|
|
587
|
-
var sourceType = entry && entry.sourceType ? entry.sourceType : 'core';
|
|
588
|
-
var resolvePluginPath = require('./resolve-plugin-path').resolvePluginPath;
|
|
589
|
-
var registryDir = path.resolve(path.dirname(this._registryPath));
|
|
590
|
-
var defaultPluginsDir = path.join(registryDir, 'core');
|
|
591
|
-
var pluginDir = resolvePluginPath(
|
|
592
|
-
{ name: name, source: source, sourceType: sourceType },
|
|
593
|
-
defaultPluginsDir,
|
|
594
|
-
registryDir
|
|
595
|
-
);
|
|
596
|
-
var pluginJsonPath = path.join(pluginDir, 'plugin.json');
|
|
597
|
-
return this._readPluginJson(pluginJsonPath);
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
/**
|
|
601
|
-
* Topological sort with circular dependency detection.
|
|
602
|
-
* @internal
|
|
603
|
-
* @param {Map<string, string[]>} graph - name -> dependencies
|
|
604
|
-
* @returns {string[]} sorted plugin names
|
|
605
|
-
* @throws {Error} on circular dependency or missing dependency
|
|
606
|
-
*/
|
|
607
|
-
_topologicalSort(graph) {
|
|
608
|
-
var WHITE = 0, GRAY = 1, BLACK = 2;
|
|
609
|
-
var color = new Map();
|
|
610
|
-
var result = [];
|
|
611
|
-
|
|
612
|
-
// Initialize all as white
|
|
613
|
-
var keys = Array.from(graph.keys());
|
|
614
|
-
for (var i = 0; i < keys.length; i++) {
|
|
615
|
-
color.set(keys[i], WHITE);
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
function visit(node) {
|
|
619
|
-
var c = color.get(node);
|
|
620
|
-
if (c === GRAY) {
|
|
621
|
-
throw new Error('Circular dependency detected involving plugin: ' + node);
|
|
622
|
-
}
|
|
623
|
-
if (c === BLACK) return;
|
|
624
|
-
|
|
625
|
-
color.set(node, GRAY);
|
|
626
|
-
|
|
627
|
-
var deps = graph.get(node) || [];
|
|
628
|
-
for (var j = 0; j < deps.length; j++) {
|
|
629
|
-
var dep = deps[j];
|
|
630
|
-
if (!graph.has(dep)) {
|
|
631
|
-
throw new Error('Plugin "' + node + '" depends on unknown plugin: ' + dep);
|
|
632
|
-
}
|
|
633
|
-
visit(dep);
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
color.set(node, BLACK);
|
|
637
|
-
result.push(node);
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
for (var k = 0; k < keys.length; k++) {
|
|
641
|
-
if (color.get(keys[k]) === WHITE) {
|
|
642
|
-
visit(keys[k]);
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
return result;
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
/**
|
|
650
|
-
* Load a single plugin by type.
|
|
651
|
-
* - Skill: only metadata, no JS module
|
|
652
|
-
* - Hook/Validator/Provider: require() index.js and instantiate
|
|
653
|
-
*
|
|
654
|
-
* @internal
|
|
655
|
-
* @param {string} name
|
|
656
|
-
* @param {object} config
|
|
657
|
-
*/
|
|
658
|
-
async _loadPlugin(name, config) {
|
|
659
|
-
var source = config && config.source ? config.source : name;
|
|
660
|
-
var sourceType = config && config.sourceType ? config.sourceType : 'core';
|
|
661
|
-
// Resolve plugin directory using shared resolver
|
|
662
|
-
var resolvePluginPath = require('./resolve-plugin-path').resolvePluginPath;
|
|
663
|
-
var registryDir = path.resolve(path.dirname(this._registryPath));
|
|
664
|
-
var defaultPluginsDir = path.join(registryDir, 'core');
|
|
665
|
-
var pluginDir;
|
|
666
|
-
try {
|
|
667
|
-
pluginDir = resolvePluginPath(
|
|
668
|
-
{ name: name, source: source, sourceType: sourceType },
|
|
669
|
-
defaultPluginsDir,
|
|
670
|
-
registryDir
|
|
671
|
-
);
|
|
672
|
-
} catch (resolveErr) {
|
|
673
|
-
this._log('error', 'Path resolution failed for plugin "' + name + '": ' + resolveErr.message);
|
|
674
|
-
throw resolveErr;
|
|
675
|
-
}
|
|
676
|
-
var pluginJsonPath = path.join(pluginDir, 'plugin.json');
|
|
677
|
-
|
|
678
|
-
// Read plugin.json to determine type
|
|
679
|
-
var pluginJson = this._readPluginJson(pluginJsonPath);
|
|
680
|
-
var type = pluginJson.type || PluginType.SKILL;
|
|
681
|
-
|
|
682
|
-
var pluginInstance;
|
|
683
|
-
|
|
684
|
-
if (type === PluginType.SKILL) {
|
|
685
|
-
// Skill plugins have no JS module - store metadata only
|
|
686
|
-
pluginInstance = new SkillPlugin();
|
|
687
|
-
pluginInstance.name = name;
|
|
688
|
-
pluginInstance.version = pluginJson.version || '0.0.0';
|
|
689
|
-
pluginInstance.description = pluginJson.description || '';
|
|
690
|
-
pluginInstance.triggers = pluginJson.triggers || [];
|
|
691
|
-
pluginInstance.metadata = pluginJson.metadata || {};
|
|
692
|
-
} else {
|
|
693
|
-
// Hook/Validator/Provider: require and instantiate using absolute path.
|
|
694
|
-
//
|
|
695
|
-
// S3 SECURITY NOTE: this `require()` runs the plugin's constructor code in
|
|
696
|
-
// the MAIN process, with full access to fs/net/process/env. True isolation
|
|
697
|
-
// is NOT possible (worker_thread shares the same OS process/credentials).
|
|
698
|
-
// WP-D8 upgrades the previous purely-declarative capabilities to
|
|
699
|
-
// "checkable declarations" + informed-consent trust records (+ optional
|
|
700
|
-
// strict gating) — an awareness aid, not a security boundary. See
|
|
701
|
-
// docs/design/sandbox-threat-model.md for the full threat model.
|
|
702
|
-
var indexJsPath = path.resolve(pluginDir, 'index.js');
|
|
703
|
-
if (sourceType === 'npm' || sourceType === 'local') {
|
|
704
|
-
var pluginTrust = require('./plugin-trust');
|
|
705
|
-
// WP-D8:信任记录是机器级知情(同一操作环境看过该插件)——固定 ~/.doukui,
|
|
706
|
-
// 不随项目走(registry 父目录推导在单层 registryPath 时会落 os.tmpdir 导致跨项目污染)
|
|
707
|
-
var _stateDir = path.join(require('os').homedir(), '.doukui');
|
|
708
|
-
var _scan = pluginTrust.scanCapabilities(indexJsPath, pluginJson);
|
|
709
|
-
var _verdict = pluginTrust.evaluateLoad({
|
|
710
|
-
pluginName: name,
|
|
711
|
-
scan: _scan,
|
|
712
|
-
stateDir: _stateDir,
|
|
713
|
-
strict: process.env.DOUKUI_STRICT_PLUGINS === '1',
|
|
714
|
-
});
|
|
715
|
-
for (var _mi = 0; _mi < _verdict.messages.length; _mi++) {
|
|
716
|
-
var _msg = _verdict.messages[_mi];
|
|
717
|
-
// S3 精神保持:外部代码加载必须显眼——首次知情/violation/hash 变化都 warn;
|
|
718
|
-
// 仅「已信任且未变化」的重复加载走 info(不刷屏)
|
|
719
|
-
this._log('warn', _msg);
|
|
720
|
-
}
|
|
721
|
-
if (!_verdict.allowed) {
|
|
722
|
-
throw new Error('Plugin "' + name + '" rejected by DOUKUI_STRICT_PLUGINS=1 (not in trust record)');
|
|
723
|
-
}
|
|
724
|
-
pluginTrust.recordTrust(_stateDir, name, _scan.hash);
|
|
725
|
-
}
|
|
726
|
-
var PluginClass = require(indexJsPath);
|
|
727
|
-
|
|
728
|
-
// Verify the export is a constructor function
|
|
729
|
-
if (typeof PluginClass !== 'function') {
|
|
730
|
-
throw new Error('Plugin "' + name + '" does not export a constructor function');
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
if (type === PluginType.HOOK) {
|
|
734
|
-
pluginInstance = new PluginClass();
|
|
735
|
-
} else if (type === PluginType.VALIDATOR) {
|
|
736
|
-
pluginInstance = new PluginClass();
|
|
737
|
-
} else if (type === PluginType.PROVIDER) {
|
|
738
|
-
pluginInstance = new PluginClass();
|
|
739
|
-
} else {
|
|
740
|
-
throw new Error('Unknown plugin type: ' + type);
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
// Attach metadata from plugin.json to instance for all non-skill plugin types
|
|
744
|
-
if (pluginJson.metadata) {
|
|
745
|
-
pluginInstance.metadata = pluginJson.metadata;
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
// Set common properties
|
|
750
|
-
pluginInstance.config = config || {};
|
|
751
|
-
pluginInstance.state = PluginState.LOADED;
|
|
752
|
-
if (pluginJson.provides) {
|
|
753
|
-
pluginInstance.provides = pluginJson.provides;
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
this.loadedPlugins.set(name, pluginInstance);
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
/**
|
|
760
|
-
* Read and parse plugin.json.
|
|
761
|
-
* @internal
|
|
762
|
-
* @param {string} jsonPath
|
|
763
|
-
* @returns {object} parsed JSON or empty object
|
|
764
|
-
*/
|
|
765
|
-
_readPluginJson(jsonPath) {
|
|
766
|
-
try {
|
|
767
|
-
var content = fs.readFileSync(jsonPath, 'utf-8');
|
|
768
|
-
return JSON.parse(content);
|
|
769
|
-
} catch (err) {
|
|
770
|
-
this._log('warn', 'Could not read plugin.json at ' + jsonPath + ': ' + err.message);
|
|
771
|
-
return {};
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
/**
|
|
776
|
-
* Get a registered Provider instance by name.
|
|
777
|
-
* Returns the factory output from the provider registry.
|
|
778
|
-
* Used by PluginContext.getProvider().
|
|
779
|
-
* @internal
|
|
780
|
-
* @param {string} name - provider name
|
|
781
|
-
* @returns {object|undefined} Provider instance or undefined
|
|
782
|
-
*/
|
|
783
|
-
_getProvider(name) {
|
|
784
|
-
return this._providerRegistry.get(name);
|
|
785
|
-
}
|
|
786
|
-
|
|
787
|
-
/**
|
|
788
|
-
* Internal logging helper.
|
|
789
|
-
* @internal
|
|
790
|
-
*/
|
|
791
|
-
_log(level, message) {
|
|
792
|
-
if (this._logger && typeof this._logger[level] === 'function') {
|
|
793
|
-
this._logger[level]('plugin-loader', message);
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
module.exports = PluginLoader;
|