doukui 0.0.0-stage → 0.3.1
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/LICENSE +21 -0
- package/README.en.md +42 -0
- package/README.md +147 -2
- package/bin/commands/build.js +178 -0
- package/bin/commands/config.js +88 -0
- package/bin/commands/help.js +83 -0
- package/bin/commands/init.js +194 -0
- package/bin/commands/install.js +202 -0
- package/bin/commands/interactive.js +352 -0
- package/bin/commands/list.js +108 -0
- package/bin/commands/loop-server.js +357 -0
- package/bin/commands/loop.js +1443 -0
- package/bin/commands/migrate.js +120 -0
- package/bin/commands/setup-global.js +81 -0
- package/bin/commands/status.js +140 -0
- package/bin/commands/team-cleanup.js +190 -0
- package/bin/commands/validate-config.js +35 -0
- package/bin/commands/validate.js +21 -0
- package/bin/commands/version.js +22 -0
- package/bin/context.js +143 -0
- package/bin/doukui.js +216 -0
- package/package.json +47 -4
- package/plugins/contracts/capabilities.js +268 -0
- package/plugins/contracts/checkresult-schema.json +75 -0
- package/plugins/contracts/config-schema.json +179 -0
- package/plugins/contracts/plugin-interface.js +262 -0
- package/plugins/contracts/plugin-schema.json +104 -0
- package/plugins/core/hook-session-start/index.js +281 -0
- package/plugins/core/hook-session-start/plugin.json +7 -0
- package/plugins/core/hook-skill-gate/index.js +661 -0
- package/plugins/core/hook-skill-gate/plugin.json +12 -0
- package/plugins/core/provider-loop-engine/index.js +1411 -0
- package/plugins/core/provider-loop-engine/plugin.json +15 -0
- package/plugins/core/provider-memory-store/index.js +510 -0
- package/plugins/core/provider-memory-store/plugin.json +9 -0
- package/plugins/core/provider-role-registry/index.js +376 -0
- package/plugins/core/provider-role-registry/plugin.json +9 -0
- package/plugins/core/provider-state-store/index.js +244 -0
- package/plugins/core/provider-state-store/plugin.json +9 -0
- package/plugins/core/provider-watchdog/assets/daemon-config.template.json +32 -0
- package/plugins/core/provider-watchdog/assets/lib/watchdog-multi-window.js +676 -0
- package/plugins/core/provider-watchdog/assets/watchdog.js +754 -0
- package/plugins/core/provider-watchdog/index.js +151 -0
- package/plugins/core/provider-watchdog/plugin.json +13 -0
- package/plugins/core/skill-agent-dispatcher/cleanup-reference.md +214 -0
- package/plugins/core/skill-agent-dispatcher/daemon-helpers-reference.md +249 -0
- package/plugins/core/skill-agent-dispatcher/plugin.json +13 -0
- package/plugins/core/skill-agent-dispatcher/roles-reference.md +253 -0
- package/plugins/core/skill-agent-dispatcher/skill.md +1291 -0
- package/plugins/core/skill-agentic-loop/plugin.json +20 -0
- package/plugins/core/skill-agentic-loop/skill.md +588 -0
- package/plugins/core/skill-batch-task-creator/plugin.json +15 -0
- package/plugins/core/skill-batch-task-creator/skill.md +620 -0
- package/plugins/core/skill-checklist/plugin.json +10 -0
- package/plugins/core/skill-checklist/skill.md +171 -0
- package/plugins/core/skill-completion-report/plugin.json +10 -0
- package/plugins/core/skill-completion-report/skill.md +331 -0
- package/plugins/core/skill-doukui-plan/plugin.json +15 -0
- package/plugins/core/skill-doukui-plan/skill.md +233 -0
- package/plugins/core/skill-doukui-sync/plugin.json +10 -0
- package/plugins/core/skill-doukui-sync/skill.md +76 -0
- package/plugins/core/skill-experience-logger/plugin.json +10 -0
- package/plugins/core/skill-experience-logger/skill.md +235 -0
- package/plugins/core/skill-human-checkpoint/plugin.json +10 -0
- package/plugins/core/skill-human-checkpoint/skill.md +194 -0
- package/plugins/core/skill-progress-tracker/plugin.json +10 -0
- package/plugins/core/skill-progress-tracker/skill.md +228 -0
- package/plugins/core/skill-role-manager/plugin.json +10 -0
- package/plugins/core/skill-role-manager/skill.md +252 -0
- package/plugins/core/skill-split-work-package/plugin.json +15 -0
- package/plugins/core/skill-split-work-package/skill.md +441 -0
- package/plugins/core/skill-task-archive/plugin.json +10 -0
- package/plugins/core/skill-task-archive/skill.md +116 -0
- package/plugins/core/skill-task-creator/plugin.json +15 -0
- package/plugins/core/skill-task-creator/skill.md +886 -0
- package/plugins/core/skill-team-cleanup/plugin.json +10 -0
- package/plugins/core/skill-team-cleanup/skill.md +266 -0
- package/plugins/core/skill-watchdog-manager/plugin.json +10 -0
- package/plugins/core/skill-watchdog-manager/skill.md +143 -0
- package/plugins/core/skill-workflow-orchestrator/plugin.json +13 -0
- package/plugins/core/skill-workflow-orchestrator/skill.md +273 -0
- package/plugins/core/validator-doc-sync/index.js +248 -0
- package/plugins/core/validator-doc-sync/plugin.json +12 -0
- package/plugins/core/validator-work-package/index.js +300 -0
- package/plugins/core/validator-work-package/plugin.json +12 -0
- package/plugins/plugin-registry.json +186 -0
- package/plugins/runtime/agent-def-generator.js +150 -0
- package/plugins/runtime/audit-logger.js +311 -0
- package/plugins/runtime/build-cli.js +89 -0
- package/plugins/runtime/checkresult-validator.js +131 -0
- package/plugins/runtime/claude-md-injector.js +159 -0
- package/plugins/runtime/cleanup-utils.js +293 -0
- package/plugins/runtime/config-manager.js +317 -0
- package/plugins/runtime/config-validator.js +320 -0
- package/plugins/runtime/context-window.js +81 -0
- package/plugins/runtime/event-bus.js +207 -0
- package/plugins/runtime/executor-claude.js +828 -0
- package/plugins/runtime/executor-default.js +448 -0
- package/plugins/runtime/executor-local.js +178 -0
- package/plugins/runtime/executor-sdk.js +415 -0
- package/plugins/runtime/harness-build.js +1037 -0
- package/plugins/runtime/hook-dispatcher.js +315 -0
- package/plugins/runtime/logger.js +192 -0
- package/plugins/runtime/loop-actuator.js +292 -0
- package/plugins/runtime/loop-coordinator.js +347 -0
- package/plugins/runtime/loop-defaults.js +29 -0
- package/plugins/runtime/loop-dispatch-batch.js +247 -0
- package/plugins/runtime/loop-executor.js +120 -0
- package/plugins/runtime/loop-report.js +327 -0
- package/plugins/runtime/loop-server-core.js +750 -0
- package/plugins/runtime/loop-snapshot.js +581 -0
- package/plugins/runtime/loop-trace.js +242 -0
- package/plugins/runtime/loop-worktree.js +404 -0
- package/plugins/runtime/manifest-resolver.js +457 -0
- package/plugins/runtime/multi-window-coordinator.js +560 -0
- package/plugins/runtime/objective-verifier.js +136 -0
- package/plugins/runtime/plan-lint.js +100 -0
- package/plugins/runtime/plan-reader.js +763 -0
- package/plugins/runtime/plugin-loader.js +798 -0
- package/plugins/runtime/plugin-trust.js +182 -0
- package/plugins/runtime/plugin-validator.js +491 -0
- package/plugins/runtime/provider-failover.js +114 -0
- package/plugins/runtime/provider-resolver.js +342 -0
- package/plugins/runtime/reflection-evaluator.js +545 -0
- package/plugins/runtime/resolve-plugin-path.js +297 -0
- package/plugins/runtime/safe-path.js +119 -0
- package/plugins/runtime/sandbox-context.js +235 -0
- package/plugins/runtime/sandbox-manager.js +609 -0
- package/plugins/runtime/sandbox-worker.js +215 -0
- package/plugins/runtime/self-rescue-ladder.js +38 -0
- package/plugins/runtime/settings-merger.js +172 -0
- package/plugins/runtime/state-store.js +527 -0
- package/plugins/runtime/team-cleanup.js +497 -0
- package/plugins/runtime/text-tokenize.js +25 -0
- package/plugins/runtime/validator-pipeline.js +475 -0
- package/plugins/runtime/yaml-parser.js +692 -0
- package/templates/harness-config.yaml +394 -0
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* team-cleanup - Deterministic agent-team directory removal
|
|
3
|
+
*
|
|
4
|
+
* WP-179 方案 A 的基石:用确定性的「逻辑销毁 + 批末目录清理」替代被 harness
|
|
5
|
+
* 拦截的 shutdown 协议帧通道。本模块提供跨平台、带安全校验、可单元测试的
|
|
6
|
+
* team 目录清理能力,让 skill.md 的「批末清理」只需调一行 CLI。
|
|
7
|
+
*
|
|
8
|
+
* 设计约束:
|
|
9
|
+
* - markTeameeDestroyed 是纯内存原语(无 SendMessage、无文件系统),由调用者
|
|
10
|
+
* 持有的 teameeMap 注入,避免任何 harness 工具协议帧拦截。
|
|
11
|
+
* - cleanupTeam 的删除逻辑全部经 opts.homeDir 注入,绝不硬编码 os.homedir()
|
|
12
|
+
* 写入,保证测试与生产隔离(生产 CLI 才默认 os.homedir())。
|
|
13
|
+
* - 不重用 cleanup-utils.removeEmptyDir:team 目录非空(含 config.json/members)。
|
|
14
|
+
*
|
|
15
|
+
* @module team-cleanup
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
'use strict';
|
|
19
|
+
|
|
20
|
+
var path = require('path');
|
|
21
|
+
var fs = require('fs');
|
|
22
|
+
var os = require('os');
|
|
23
|
+
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// Error type
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* TeamCleanupError - raised for programmer errors (invalid input / unsafe path).
|
|
30
|
+
*
|
|
31
|
+
* Runtime file-system failures are collected into result.errors[] instead,
|
|
32
|
+
* so a single unreadable config.json or locked file never aborts a whole
|
|
33
|
+
* team cleanup.
|
|
34
|
+
*
|
|
35
|
+
* @public
|
|
36
|
+
* @param {string} message - Human-readable error detail
|
|
37
|
+
* @param {string} code - 'invalid_name' | 'unsafe_path' | 'config_read_failed'
|
|
38
|
+
*/
|
|
39
|
+
function TeamCleanupError(message, code) {
|
|
40
|
+
Error.call(this, message);
|
|
41
|
+
this.name = 'TeamCleanupError';
|
|
42
|
+
this.message = message;
|
|
43
|
+
this.code = code;
|
|
44
|
+
if (Error.captureStackTrace) {
|
|
45
|
+
Error.captureStackTrace(this, TeamCleanupError);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Node-compatible Error inheritance (works on older + modern engines).
|
|
50
|
+
TeamCleanupError.prototype = Object.create(Error.prototype);
|
|
51
|
+
TeamCleanupError.prototype.constructor = TeamCleanupError;
|
|
52
|
+
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
// Name validation
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Validate a team name against a safe charset + length policy.
|
|
59
|
+
*
|
|
60
|
+
* Accepts: leading alnum then alnum/underscore/dash, 1-64 chars total.
|
|
61
|
+
* Explicitly rejects '.', '..', '', and any path separator (/ \ :).
|
|
62
|
+
*
|
|
63
|
+
* @public
|
|
64
|
+
* @param {string} name - Candidate team name
|
|
65
|
+
* @returns {{ok:true} | {ok:false, reason:'empty'|'invalid_chars'|'too_long'|'leading_separator'}}
|
|
66
|
+
*/
|
|
67
|
+
function validateTeamName(name) {
|
|
68
|
+
if (name === undefined || name === null || name === '') {
|
|
69
|
+
return { ok: false, reason: 'empty' };
|
|
70
|
+
}
|
|
71
|
+
if (typeof name !== 'string') {
|
|
72
|
+
return { ok: false, reason: 'invalid_chars' };
|
|
73
|
+
}
|
|
74
|
+
if (name === '.' || name === '..') {
|
|
75
|
+
return { ok: false, reason: 'invalid_chars' };
|
|
76
|
+
}
|
|
77
|
+
if (name.length > 64) {
|
|
78
|
+
return { ok: false, reason: 'too_long' };
|
|
79
|
+
}
|
|
80
|
+
// Reject anything containing a path separator or drive colon.
|
|
81
|
+
if (/[\/\\:]/.test(name)) {
|
|
82
|
+
// A leading separator on its own is a distinct, actionable user mistake.
|
|
83
|
+
if (/^[\/\\]/.test(name)) {
|
|
84
|
+
return { ok: false, reason: 'leading_separator' };
|
|
85
|
+
}
|
|
86
|
+
return { ok: false, reason: 'invalid_chars' };
|
|
87
|
+
}
|
|
88
|
+
// Anchored charset check: leading alnum, body alnum/_/-.
|
|
89
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(name)) {
|
|
90
|
+
return { ok: false, reason: 'invalid_chars' };
|
|
91
|
+
}
|
|
92
|
+
return { ok: true };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// Path safety
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Check that a resolved path is safely contained under a parent directory
|
|
101
|
+
* and that its basename matches the expected team name.
|
|
102
|
+
*
|
|
103
|
+
* Defends against path traversal ('..') and absolute-path drift by requiring
|
|
104
|
+
* the parent prefix match (including a trailing separator, so '/foo' does not
|
|
105
|
+
* count as safe under '/foobar') AND a basename equality.
|
|
106
|
+
*
|
|
107
|
+
* @public
|
|
108
|
+
* @param {string} resolvedPath - Fully resolved candidate path
|
|
109
|
+
* @param {string} parentDir - Required parent directory (no trailing sep)
|
|
110
|
+
* @param {string} teamName - Required basename
|
|
111
|
+
* @returns {boolean}
|
|
112
|
+
*/
|
|
113
|
+
function isPathSafe(resolvedPath, parentDir, teamName) {
|
|
114
|
+
if (typeof resolvedPath !== 'string' || typeof parentDir !== 'string' || typeof teamName !== 'string') {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
var prefix = parentDir + path.sep;
|
|
118
|
+
return resolvedPath.indexOf(prefix) === 0 && path.basename(resolvedPath) === teamName;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ---------------------------------------------------------------------------
|
|
122
|
+
// Path resolution
|
|
123
|
+
// ---------------------------------------------------------------------------
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Resolve the on-disk locations of a team's teams/ and tasks/ directories.
|
|
127
|
+
*
|
|
128
|
+
* Pure function: performs no file-system access. homeDir/claudeDir default
|
|
129
|
+
* to os.homedir() / '.claude' but callers (tests, CLI with --root) inject
|
|
130
|
+
* explicit values to avoid touching the real ~/.claude.
|
|
131
|
+
*
|
|
132
|
+
* @public
|
|
133
|
+
* @param {string} teamName - Already-validated team name
|
|
134
|
+
* @param {object} [opts]
|
|
135
|
+
* @param {string} [opts.homeDir] - Defaults to os.homedir()
|
|
136
|
+
* @param {string} [opts.claudeDir] - Defaults to '.claude'
|
|
137
|
+
* @returns {{teamsDir,tasksDir,teamPath,tasksPath,baseOk}}
|
|
138
|
+
*/
|
|
139
|
+
function resolveTeamPaths(teamName, opts) {
|
|
140
|
+
opts = opts || {};
|
|
141
|
+
var homeDir = opts.homeDir || os.homedir();
|
|
142
|
+
var claudeDir = opts.claudeDir || '.claude';
|
|
143
|
+
|
|
144
|
+
var teamsDir = path.join(homeDir, claudeDir, 'teams');
|
|
145
|
+
var tasksDir = path.join(homeDir, claudeDir, 'tasks');
|
|
146
|
+
var teamPath = path.join(teamsDir, teamName);
|
|
147
|
+
var tasksPath = path.join(tasksDir, teamName);
|
|
148
|
+
|
|
149
|
+
var baseOk = isPathSafe(teamPath, teamsDir, teamName) &&
|
|
150
|
+
isPathSafe(tasksPath, tasksDir, teamName) &&
|
|
151
|
+
path.basename(teamPath) === teamName &&
|
|
152
|
+
path.basename(tasksPath) === teamName;
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
teamsDir: teamsDir,
|
|
156
|
+
tasksDir: tasksDir,
|
|
157
|
+
teamPath: teamPath,
|
|
158
|
+
tasksPath: tasksPath,
|
|
159
|
+
baseOk: baseOk,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
// Logical destruction (core primitive)
|
|
165
|
+
// ---------------------------------------------------------------------------
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Mark a teamee as destroyed by removing its taskId from the caller's map.
|
|
169
|
+
*
|
|
170
|
+
* This is the WP-179 方案 A core primitive: a pure in-memory operation that
|
|
171
|
+
* replaces the harness-blocked shutdown_request protocol frame. No SendMessage,
|
|
172
|
+
* no Agent call, no file-system access. The in-process member process ends
|
|
173
|
+
* naturally with the session; the caller's map is the only source of truth.
|
|
174
|
+
*
|
|
175
|
+
* Mutates teameeMap in place (delete is the whole operation).
|
|
176
|
+
*
|
|
177
|
+
* @public
|
|
178
|
+
* @param {object} teameeMap - Caller-owned { taskId: teameeName } map
|
|
179
|
+
* @param {string} taskId - Task id whose mapping should be dropped
|
|
180
|
+
* @returns {{removed:true, teameeName:string} | {removed:false}}
|
|
181
|
+
*/
|
|
182
|
+
function markTeameeDestroyed(teameeMap, taskId) {
|
|
183
|
+
if (!teameeMap || !Object.prototype.hasOwnProperty.call(teameeMap, taskId)) {
|
|
184
|
+
return { removed: false };
|
|
185
|
+
}
|
|
186
|
+
var teameeName = teameeMap[taskId];
|
|
187
|
+
delete teameeMap[taskId];
|
|
188
|
+
return { removed: true, teameeName: teameeName };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
// Read-only artifact inspection
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* List the artifacts (directories + members) that exist for a team.
|
|
197
|
+
*
|
|
198
|
+
* Read-only: never deletes anything. Tries to read config.json for member
|
|
199
|
+
* names; a missing/unreadable config is recorded in errors[] but does not
|
|
200
|
+
* throw (the team dir may exist with no config, or config may be locked).
|
|
201
|
+
*
|
|
202
|
+
* @public
|
|
203
|
+
* @param {string} teamName - Team name (validated here)
|
|
204
|
+
* @param {object} [opts] - Forwarded to resolveTeamPaths ({homeDir,claudeDir})
|
|
205
|
+
* @returns {{teamsExists,tasksExists,members,errors:[]}}
|
|
206
|
+
* @throws {TeamCleanupError} when teamName is invalid or path is unsafe
|
|
207
|
+
*/
|
|
208
|
+
function listTeamArtifacts(teamName, opts) {
|
|
209
|
+
var validation = validateTeamName(teamName);
|
|
210
|
+
if (!validation.ok) {
|
|
211
|
+
throw new TeamCleanupError('Invalid team name: ' + teamName, 'invalid_name');
|
|
212
|
+
}
|
|
213
|
+
var paths = resolveTeamPaths(teamName, opts);
|
|
214
|
+
if (!paths.baseOk) {
|
|
215
|
+
throw new TeamCleanupError('Unsafe resolved path for team: ' + teamName, 'unsafe_path');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
var members = null;
|
|
219
|
+
var errors = [];
|
|
220
|
+
|
|
221
|
+
var teamsExists = fs.existsSync(paths.teamPath);
|
|
222
|
+
var tasksExists = fs.existsSync(paths.tasksPath);
|
|
223
|
+
|
|
224
|
+
if (teamsExists) {
|
|
225
|
+
var configPath = path.join(paths.teamPath, 'config.json');
|
|
226
|
+
if (fs.existsSync(configPath)) {
|
|
227
|
+
try {
|
|
228
|
+
var raw = fs.readFileSync(configPath, 'utf-8');
|
|
229
|
+
var config = JSON.parse(raw);
|
|
230
|
+
if (config && Array.isArray(config.members)) {
|
|
231
|
+
members = config.members;
|
|
232
|
+
} else {
|
|
233
|
+
members = [];
|
|
234
|
+
}
|
|
235
|
+
} catch (e) {
|
|
236
|
+
// Unreadable/invalid config is non-fatal: still report existence.
|
|
237
|
+
members = null;
|
|
238
|
+
errors.push({ kind: 'config_read_failed', path: configPath, message: e.message });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return {
|
|
244
|
+
teamsExists: teamsExists,
|
|
245
|
+
tasksExists: tasksExists,
|
|
246
|
+
members: members,
|
|
247
|
+
errors: errors,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ---------------------------------------------------------------------------
|
|
252
|
+
// Cleanup (file-system writes)
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Remove a team's teams/ and tasks/ directories with safety checks.
|
|
257
|
+
*
|
|
258
|
+
* Algorithm (see WP-179-1-impl-a.md):
|
|
259
|
+
* 1. validateTeamName -> TeamCleanupError('invalid_name')
|
|
260
|
+
* 2. resolveTeamPaths + baseOk -> TeamCleanupError('unsafe_path')
|
|
261
|
+
* 3. Neither dir exists -> { skipped: true }
|
|
262
|
+
* 4. dryRun -> { wouldDelete: [existing paths] } (no fs writes)
|
|
263
|
+
* 5. force:false + config shows active team created <5min ago -> refuse
|
|
264
|
+
* 6. Optional teamDeleteFn (test injection; CLI omits)
|
|
265
|
+
* 7. fs.rmSync(target, {recursive,force,maxRetries:3,retryDelay:100})
|
|
266
|
+
* per directory; single-dir failures go to errors[]
|
|
267
|
+
* 8. Verify each path no longer exists
|
|
268
|
+
*
|
|
269
|
+
* @public
|
|
270
|
+
* @param {string} teamName
|
|
271
|
+
* @param {object} [opts]
|
|
272
|
+
* @param {boolean} [opts.dryRun=false] - Report only, delete nothing
|
|
273
|
+
* @param {boolean} [opts.force=false] - Skip the fresh-team guard
|
|
274
|
+
* @param {string} [opts.homeDir] - Override ~/.claude root
|
|
275
|
+
* @param {function} [opts.log] - Progress logger (no-op default)
|
|
276
|
+
* @param {function} [opts.teamDeleteFn] - Test injection hook
|
|
277
|
+
* @returns {{teamsDeleted,tasksDeleted,skipped,wouldDelete,errors,teamDeleteAttempted,teamDeleteSucceeded}}
|
|
278
|
+
* @throws {TeamCleanupError} for invalid name / unsafe path only
|
|
279
|
+
*/
|
|
280
|
+
function cleanupTeam(teamName, opts) {
|
|
281
|
+
opts = opts || {};
|
|
282
|
+
var dryRun = opts.dryRun === true;
|
|
283
|
+
var force = opts.force === true;
|
|
284
|
+
var log = typeof opts.log === 'function' ? opts.log : function () {};
|
|
285
|
+
|
|
286
|
+
var validation = validateTeamName(teamName);
|
|
287
|
+
if (!validation.ok) {
|
|
288
|
+
throw new TeamCleanupError('Invalid team name: ' + teamName + ' (' + validation.reason + ')', 'invalid_name');
|
|
289
|
+
}
|
|
290
|
+
var paths = resolveTeamPaths(teamName, opts);
|
|
291
|
+
if (!paths.baseOk) {
|
|
292
|
+
throw new TeamCleanupError('Unsafe resolved path for team: ' + teamName, 'unsafe_path');
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
var teamsExists = fs.existsSync(paths.teamPath);
|
|
296
|
+
var tasksExists = fs.existsSync(paths.tasksPath);
|
|
297
|
+
|
|
298
|
+
// 3. Nothing to clean.
|
|
299
|
+
if (!teamsExists && !tasksExists) {
|
|
300
|
+
log('No team artifacts found for "' + teamName + '"');
|
|
301
|
+
return {
|
|
302
|
+
teamsDeleted: 0,
|
|
303
|
+
tasksDeleted: 0,
|
|
304
|
+
skipped: true,
|
|
305
|
+
wouldDelete: [],
|
|
306
|
+
errors: [],
|
|
307
|
+
teamDeleteAttempted: false,
|
|
308
|
+
teamDeleteSucceeded: false,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// 4. Dry run reports candidates only.
|
|
313
|
+
if (dryRun) {
|
|
314
|
+
var wouldDelete = [];
|
|
315
|
+
if (teamsExists) wouldDelete.push(paths.teamPath);
|
|
316
|
+
if (tasksExists) wouldDelete.push(paths.tasksPath);
|
|
317
|
+
log('Dry run: would delete ' + wouldDelete.length + ' path(s)');
|
|
318
|
+
return {
|
|
319
|
+
teamsDeleted: 0,
|
|
320
|
+
tasksDeleted: 0,
|
|
321
|
+
skipped: false,
|
|
322
|
+
wouldDelete: wouldDelete,
|
|
323
|
+
errors: [],
|
|
324
|
+
teamDeleteAttempted: false,
|
|
325
|
+
teamDeleteSucceeded: false,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// 5. Fresh-team guard: refuse to nuke a team whose config.json reports an
|
|
330
|
+
// active status and was created within the last 5 minutes unless --force.
|
|
331
|
+
// Prevents a stale-dispatch race from wiping a freshly-booted team.
|
|
332
|
+
if (!force) {
|
|
333
|
+
var guard = checkFreshActiveTeam(paths.teamPath);
|
|
334
|
+
if (guard.blocked) {
|
|
335
|
+
return {
|
|
336
|
+
teamsDeleted: 0,
|
|
337
|
+
tasksDeleted: 0,
|
|
338
|
+
skipped: false,
|
|
339
|
+
wouldDelete: [],
|
|
340
|
+
errors: [{
|
|
341
|
+
kind: 'fresh_active_team',
|
|
342
|
+
path: paths.teamPath,
|
|
343
|
+
message: guard.reason,
|
|
344
|
+
}],
|
|
345
|
+
teamDeleteAttempted: false,
|
|
346
|
+
teamDeleteSucceeded: false,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// 6. Optional injected team-delete hook (tests only; CLI never passes it).
|
|
352
|
+
var teamDeleteAttempted = false;
|
|
353
|
+
var teamDeleteSucceeded = false;
|
|
354
|
+
if (typeof opts.teamDeleteFn === 'function') {
|
|
355
|
+
teamDeleteAttempted = true;
|
|
356
|
+
try {
|
|
357
|
+
var fnResult = opts.teamDeleteFn(teamName, paths);
|
|
358
|
+
teamDeleteSucceeded = fnResult !== false;
|
|
359
|
+
} catch (e) {
|
|
360
|
+
// Hook failure is non-fatal; fall through to file-system deletion.
|
|
361
|
+
log('teamDeleteFn threw: ' + e.message);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// 7. File-system removal, per target, collecting failures into errors[].
|
|
366
|
+
var errors = [];
|
|
367
|
+
var teamsDeleted = 0;
|
|
368
|
+
var tasksDeleted = 0;
|
|
369
|
+
|
|
370
|
+
if (teamsExists) {
|
|
371
|
+
var tErr = removeOne(paths.teamPath, log);
|
|
372
|
+
if (tErr) {
|
|
373
|
+
errors.push(tErr);
|
|
374
|
+
} else if (!fs.existsSync(paths.teamPath)) {
|
|
375
|
+
teamsDeleted = 1;
|
|
376
|
+
} else {
|
|
377
|
+
errors.push({
|
|
378
|
+
kind: 'still_exists',
|
|
379
|
+
path: paths.teamPath,
|
|
380
|
+
message: 'rmSync completed but path still exists',
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (tasksExists) {
|
|
386
|
+
var sErr = removeOne(paths.tasksPath, log);
|
|
387
|
+
if (sErr) {
|
|
388
|
+
errors.push(sErr);
|
|
389
|
+
} else if (!fs.existsSync(paths.tasksPath)) {
|
|
390
|
+
tasksDeleted = 1;
|
|
391
|
+
} else {
|
|
392
|
+
errors.push({
|
|
393
|
+
kind: 'still_exists',
|
|
394
|
+
path: paths.tasksPath,
|
|
395
|
+
message: 'rmSync completed but path still exists',
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return {
|
|
401
|
+
teamsDeleted: teamsDeleted,
|
|
402
|
+
tasksDeleted: tasksDeleted,
|
|
403
|
+
skipped: false,
|
|
404
|
+
wouldDelete: [],
|
|
405
|
+
errors: errors,
|
|
406
|
+
teamDeleteAttempted: teamDeleteAttempted,
|
|
407
|
+
teamDeleteSucceeded: teamDeleteSucceeded,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Remove a single target directory, returning null on success or an error
|
|
413
|
+
* descriptor on failure. Internal helper, not exported.
|
|
414
|
+
*
|
|
415
|
+
* @private
|
|
416
|
+
* @param {string} target - Directory path to remove
|
|
417
|
+
* @param {function} log - Progress logger
|
|
418
|
+
* @returns {null | {kind,path,message}}
|
|
419
|
+
*/
|
|
420
|
+
function removeOne(target, log) {
|
|
421
|
+
try {
|
|
422
|
+
log('Removing ' + target);
|
|
423
|
+
fs.rmSync(target, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
|
|
424
|
+
return null;
|
|
425
|
+
} catch (e) {
|
|
426
|
+
return { kind: 'rm_failed', path: target, message: e.message };
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Fresh-active-team guard. Internal helper.
|
|
432
|
+
*
|
|
433
|
+
* Blocks deletion when config.json is readable, reports status 'active'
|
|
434
|
+
* (or omits status), and the team directory's ctime is within 5 minutes.
|
|
435
|
+
* Unreadable/missing config or older teams never block.
|
|
436
|
+
*
|
|
437
|
+
* @private
|
|
438
|
+
* @param {string} teamPath
|
|
439
|
+
* @returns {{blocked:boolean, reason?:string}}
|
|
440
|
+
*/
|
|
441
|
+
function checkFreshActiveTeam(teamPath) {
|
|
442
|
+
var configPath = path.join(teamPath, 'config.json');
|
|
443
|
+
if (!fs.existsSync(configPath)) {
|
|
444
|
+
return { blocked: false };
|
|
445
|
+
}
|
|
446
|
+
var config;
|
|
447
|
+
try {
|
|
448
|
+
config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
449
|
+
} catch (e) {
|
|
450
|
+
return { blocked: false };
|
|
451
|
+
}
|
|
452
|
+
if (!config) {
|
|
453
|
+
return { blocked: false };
|
|
454
|
+
}
|
|
455
|
+
// Only guard teams that look active. status absent is treated conservatively
|
|
456
|
+
// as active (we can't prove it's done).
|
|
457
|
+
var status = config.status;
|
|
458
|
+
if (status && status !== 'active') {
|
|
459
|
+
return { blocked: false };
|
|
460
|
+
}
|
|
461
|
+
var dirStat;
|
|
462
|
+
try {
|
|
463
|
+
dirStat = fs.statSync(teamPath);
|
|
464
|
+
} catch (e) {
|
|
465
|
+
return { blocked: false };
|
|
466
|
+
}
|
|
467
|
+
// Prefer config.createdAt if present; fall back to directory ctime.
|
|
468
|
+
var createdMs = dirStat.ctimeMs;
|
|
469
|
+
if (config.createdAt && typeof config.createdAt === 'string') {
|
|
470
|
+
var parsed = Date.parse(config.createdAt);
|
|
471
|
+
if (!isNaN(parsed)) {
|
|
472
|
+
createdMs = parsed;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
var ageMs = Date.now() - createdMs;
|
|
476
|
+
if (ageMs < 5 * 60 * 1000) {
|
|
477
|
+
return {
|
|
478
|
+
blocked: true,
|
|
479
|
+
reason: 'Team "' + path.basename(teamPath) + '" appears active and was created less than 5 minutes ago; pass --force to override.',
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
return { blocked: false };
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// ---------------------------------------------------------------------------
|
|
486
|
+
// Module exports
|
|
487
|
+
// ---------------------------------------------------------------------------
|
|
488
|
+
|
|
489
|
+
module.exports = {
|
|
490
|
+
TeamCleanupError: TeamCleanupError,
|
|
491
|
+
validateTeamName: validateTeamName,
|
|
492
|
+
resolveTeamPaths: resolveTeamPaths,
|
|
493
|
+
isPathSafe: isPathSafe,
|
|
494
|
+
markTeameeDestroyed: markTeameeDestroyed,
|
|
495
|
+
listTeamArtifacts: listTeamArtifacts,
|
|
496
|
+
cleanupTeam: cleanupTeam,
|
|
497
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// 共享分词(WP-S4 从 provider-memory-store._tokenize 提取)
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// 中文按字符 bigram(无词典依赖),拉丁/数字按词。两个消费方:
|
|
7
|
+
// - provider-memory-store.recall(记忆召回打分)
|
|
8
|
+
// - executor-claude buildPrompt 的精读 retry(失败项 ↔ 文档段落关联打分)
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 分词:拉丁词(小写)+ 中文字符 bigram。
|
|
13
|
+
* @param {string} text
|
|
14
|
+
* @returns {string[]}
|
|
15
|
+
*/
|
|
16
|
+
function tokenize(text) {
|
|
17
|
+
var tokens = [];
|
|
18
|
+
var latin = String(text || '').toLowerCase().match(/[a-z0-9_]+/g) || [];
|
|
19
|
+
for (var i = 0; i < latin.length; i++) tokens.push(latin[i]);
|
|
20
|
+
var cjk = String(text || '').match(/[\u4e00-\u9fff]/g) || [];
|
|
21
|
+
for (var j = 0; j < cjk.length - 1; j++) tokens.push(cjk[j] + cjk[j + 1]);
|
|
22
|
+
return tokens;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = { tokenize: tokenize };
|