carlos-flow 2.13.0 → 2.14.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/AGENTS.md +27 -2
- package/CHANGELOG.md +26 -0
- package/README.en.md +40 -2
- package/README.md +39 -3
- package/README.zh.md +39 -3
- package/TRIGGER_GUIDE.md +3 -3
- package/bridge/mcp-bridge.js +106 -6
- package/client/carlos.js +187 -4
- package/ghost.json +19 -559
- package/main.js +267 -0
- package/package.json +12 -7
- package/scripts/check-stubs.mjs +1 -1
- package/scripts/check-templates.mjs +42 -3
- package/scripts/full-audit.mjs +66 -6
- package/scripts/lang-audit.mjs +7 -1
- package/scripts/mutation-check.mjs +74 -7
- package/src/dispatch.js +204 -17
- package/src/index.js +208 -74
- package/src/mcp-stdio.mjs +139 -0
- package/src/protocol.js +182 -1
- package/src/state.js +36 -4
package/client/carlos.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { createConnection, createServer } from "node:net";
|
|
8
8
|
import { spawn } from "node:child_process";
|
|
9
|
-
import { existsSync, readFileSync, writeFileSync, unlinkSync, mkdirSync } from "node:fs";
|
|
9
|
+
import { existsSync, readFileSync, writeFileSync, unlinkSync, mkdirSync, readdirSync, statSync, rmSync } from "node:fs";
|
|
10
10
|
import { randomBytes } from "node:crypto";
|
|
11
11
|
import { resolve, join, dirname } from "node:path";
|
|
12
12
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
@@ -85,6 +85,95 @@ try {
|
|
|
85
85
|
const toolName = process.argv[2];
|
|
86
86
|
const argsJson = process.argv[3] || "{}";
|
|
87
87
|
|
|
88
|
+
// 测试会自动生成大量会话。这些前缀是「机器造的」,与真实使用可可靠区分,
|
|
89
|
+
// clean 命令默认只动它们。
|
|
90
|
+
const TEST_SESSION_PREFIXES = ["e2e-", "p0-", "probe-", "guide-", "test-", "smoke-", "unknown"];
|
|
91
|
+
|
|
92
|
+
function dirSize(p) {
|
|
93
|
+
let total = 0;
|
|
94
|
+
const walk = (d) => {
|
|
95
|
+
let ents = [];
|
|
96
|
+
try { ents = readdirSync(d, { withFileTypes: true }); } catch { return; }
|
|
97
|
+
for (const e of ents) {
|
|
98
|
+
const full = join(d, e.name);
|
|
99
|
+
if (e.isDirectory()) walk(full);
|
|
100
|
+
else { try { total += statSync(full).size; } catch {} }
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
walk(p);
|
|
104
|
+
return total;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 清理测试残留会话(gate-state/runs/<sid>)。
|
|
109
|
+
*
|
|
110
|
+
* 绝不删除 gate-audit:审计是逐日哈希链,删一天就断链,
|
|
111
|
+
* 「谁在什么时候批了什么」就永久查不回来了 —— 这不是磁盘占用问题,是可信度问题。
|
|
112
|
+
*/
|
|
113
|
+
function runClean(opts) {
|
|
114
|
+
const runsDir = join(process.env.CARLOS_FLOW_HOME || DEFAULT_FLOW_HOME, "gate-state", "runs");
|
|
115
|
+
if (!existsSync(runsDir)) {
|
|
116
|
+
console.log("没有需要清理的会话目录(" + runsDir + " 不存在)");
|
|
117
|
+
return { ok: true };
|
|
118
|
+
}
|
|
119
|
+
const now = Date.now();
|
|
120
|
+
const cutoff = opts.olderThanDays > 0 ? now - opts.olderThanDays * 86400_000 : 0;
|
|
121
|
+
const isTest = (name) => (opts.prefixes ? opts.prefixes.some((p) => name === p || name.startsWith(p)) : true);
|
|
122
|
+
|
|
123
|
+
let names = [];
|
|
124
|
+
try {
|
|
125
|
+
names = readdirSync(runsDir, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name);
|
|
126
|
+
} catch (e) {
|
|
127
|
+
console.error("读取会话目录失败:" + e.message);
|
|
128
|
+
return { ok: false };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const targets = [];
|
|
132
|
+
const kept = [];
|
|
133
|
+
for (const n of names) {
|
|
134
|
+
const full = join(runsDir, n);
|
|
135
|
+
let mtimeMs = 0;
|
|
136
|
+
try { mtimeMs = statSync(full).mtimeMs; } catch {}
|
|
137
|
+
const oldEnough = !cutoff || mtimeMs < cutoff;
|
|
138
|
+
if (isTest(n) && oldEnough) targets.push({ name: n, full, size: dirSize(full) });
|
|
139
|
+
else kept.push(n);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const totalBytes = targets.reduce((s, t) => s + t.size, 0);
|
|
143
|
+
const mb = (n) => (n / 1048576).toFixed(1) + " MB";
|
|
144
|
+
|
|
145
|
+
console.log("════ 会话清理" + (opts.apply ? "(执行模式)" : "(预览模式,不会删任何东西)") + " ════");
|
|
146
|
+
console.log(" 会话目录: " + runsDir);
|
|
147
|
+
console.log(" 总会话数: " + names.length);
|
|
148
|
+
console.log(" 待清理 : " + targets.length + " 个测试残留,共 " + mb(totalBytes));
|
|
149
|
+
console.log(" 保留 : " + kept.length + " 个(真实会话 + 不匹配前缀的)");
|
|
150
|
+
if (targets.length > 0) {
|
|
151
|
+
const sample = targets.slice(0, 5).map((t) => t.name).join(", ");
|
|
152
|
+
console.log(" 样例 : " + sample + (targets.length > 5 ? " …" : ""));
|
|
153
|
+
}
|
|
154
|
+
console.log(" 注意 : 审计流水(gate-audit)永远不会被删 —— 它是哈希链,断一天就查不回去了");
|
|
155
|
+
|
|
156
|
+
if (!opts.apply) {
|
|
157
|
+
console.log("\n确认无误后加 --apply 真正执行:");
|
|
158
|
+
console.log(" node carlos.js clean --apply");
|
|
159
|
+
console.log("只清更早的: node carlos.js clean --apply --older-than 7");
|
|
160
|
+
return { ok: true };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let removed = 0, reclaimed = 0, failed = 0;
|
|
164
|
+
for (const t of targets) {
|
|
165
|
+
try {
|
|
166
|
+
rmSync(t.full, { recursive: true, force: true, maxRetries: 3, retryDelay: 50 });
|
|
167
|
+
removed++; reclaimed += t.size;
|
|
168
|
+
} catch {
|
|
169
|
+
failed++;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
console.log("\n✅ 已删除 " + removed + " 个会话,释放 " + mb(reclaimed) + (failed ? ";失败 " + failed + " 个(可能被占用)" : ""));
|
|
173
|
+
if (failed) console.log(" 补充:关闭占用这些目录的进程后重跑即可。");
|
|
174
|
+
return { ok: failed === 0 };
|
|
175
|
+
}
|
|
176
|
+
|
|
88
177
|
// ===== init 命令:一键生成宿主配置 =====
|
|
89
178
|
if (toolName === "init") {
|
|
90
179
|
const target = process.argv.includes("--target")
|
|
@@ -100,10 +189,38 @@ if (toolName === "init") {
|
|
|
100
189
|
process.exit(0);
|
|
101
190
|
}
|
|
102
191
|
|
|
192
|
+
if (toolName === "clean") {
|
|
193
|
+
// 清理命令(不需要 bridge / MCP):轻量化的运维入口。
|
|
194
|
+
//
|
|
195
|
+
// 背景(2026-11-17 实测):840 个会话目录里 587 个是测试残留,18MB。
|
|
196
|
+
// 以前没有任何清理手段,只能人工进目录删。
|
|
197
|
+
//
|
|
198
|
+
// 安全口径:
|
|
199
|
+
// · 默认**只列不删**(dry-run),必须显式 --apply 才真的删
|
|
200
|
+
// · 默认只动**测试前缀**的会话(e2e-/p0-/probe-/guide-/test-/smoke-),
|
|
201
|
+
// 真实会话(root-/maint-/audit-/用户自起名)一律不碰
|
|
202
|
+
// · **绝不删除 gate-audit**:审计是哈希链,删一天就断链,追溯能力就没了
|
|
203
|
+
const opts = { apply: false, prefixes: TEST_SESSION_PREFIXES, olderThanDays: 0 };
|
|
204
|
+
for (let i = 3; i < process.argv.length; i++) {
|
|
205
|
+
const a = process.argv[i];
|
|
206
|
+
if (a === "--apply") opts.apply = true;
|
|
207
|
+
else if (a === "--all") opts.prefixes = null; // 危险:连真实会话一起列(仍默认不删)
|
|
208
|
+
else if (a === "--older-than") opts.olderThanDays = Number(process.argv[++i]) || 0;
|
|
209
|
+
else if (a === "--prefix") opts.prefixes = [String(process.argv[++i] || "").trim()].filter(Boolean);
|
|
210
|
+
}
|
|
211
|
+
const r = runClean(opts);
|
|
212
|
+
process.exit(r.ok ? 0 : 1);
|
|
213
|
+
}
|
|
214
|
+
|
|
103
215
|
if (!toolName) {
|
|
104
|
-
console.error("用法: node carlos.js
|
|
216
|
+
console.error("用法: node carlos.js <工具名> [json参数]");
|
|
217
|
+
console.error(" node carlos.js clean [--apply] [--older-than 7] 清理测试残留会话(默认只列不删)");
|
|
105
218
|
console.error(" node carlos.js init --target cindy|opencode|cursor|cline|vscode|claude");
|
|
106
|
-
console.error("
|
|
219
|
+
console.error("");
|
|
220
|
+
console.error("最常用的三个工具(其余见 flow_protocol):");
|
|
221
|
+
console.error(" flow_status 看当前进度:有没有任务卡、走到哪一步、下一步该做什么");
|
|
222
|
+
console.error(" flow_trigger_check 判断这句话该不该启动工作流(智能激活)");
|
|
223
|
+
console.error(" flow_bypass 临时跳过审查(需 confirmed:true)");
|
|
107
224
|
process.exit(1);
|
|
108
225
|
}
|
|
109
226
|
|
|
@@ -428,6 +545,35 @@ async function runInit(target) {
|
|
|
428
545
|
// 两种目标格式要分别处理:
|
|
429
546
|
// - JSON(ghost.json / mcp.json / opencode.json):必须 JSON 转义(反斜杠 → \\)
|
|
430
547
|
// - YAML(.mcprc):Windows 路径一律用正斜杠(反斜杠在 YAML 双引号标量里同样非法)
|
|
548
|
+
// 把包内相对入口(entry / node.entry / node.entries[] / settingsHtml / icon)渲染成绝对路径。
|
|
549
|
+
//
|
|
550
|
+
// 为什么需要:ghost.json 现在是**包清单**(chip 形态,包里全是相对路径),而
|
|
551
|
+
// `init --target cindy` 只复制清单文件、不复制目录结构 —— 相对路径在
|
|
552
|
+
// ~/.cindy/ghosts/ 下没有包根可参照,插件会起不来。这里渲染成绝对路径,
|
|
553
|
+
// 保证「开发直连仓库」这条路继续可用。
|
|
554
|
+
function renderRelativeEntries(text, rootAbs) {
|
|
555
|
+
let obj;
|
|
556
|
+
try {
|
|
557
|
+
obj = JSON.parse(text);
|
|
558
|
+
} catch {
|
|
559
|
+
return text; // 已经是非法 JSON 就别再动它,交给门禁去报
|
|
560
|
+
}
|
|
561
|
+
const toAbs = (rel) => {
|
|
562
|
+
if (typeof rel !== "string" || rel === "") return rel;
|
|
563
|
+
const normalized = rel.replace(/\\/g, "/");
|
|
564
|
+
if (/^([A-Za-z]:[\\/]|[\\/]|https?:)/.test(normalized)) return normalized;
|
|
565
|
+
return join(rootAbs, normalized);
|
|
566
|
+
};
|
|
567
|
+
obj.entry = toAbs(obj.entry);
|
|
568
|
+
if (obj.settingsHtml) obj.settingsHtml = toAbs(obj.settingsHtml);
|
|
569
|
+
if (obj.icon) obj.icon = toAbs(obj.icon);
|
|
570
|
+
if (obj.node && typeof obj.node === "object") {
|
|
571
|
+
obj.node.entry = toAbs(obj.node.entry);
|
|
572
|
+
if (Array.isArray(obj.node.entries)) obj.node.entries = obj.node.entries.map(toAbs);
|
|
573
|
+
}
|
|
574
|
+
return JSON.stringify(obj, null, 2) + "\n";
|
|
575
|
+
}
|
|
576
|
+
|
|
431
577
|
const rootJson = JSON.stringify(ROOT_ABS).slice(1, -1);
|
|
432
578
|
const rootYaml = ROOT_ABS.replace(/\\/g, "/");
|
|
433
579
|
|
|
@@ -438,7 +584,7 @@ async function runInit(target) {
|
|
|
438
584
|
const ghostPath = join(ghostsDir, "carlos-flow.json");
|
|
439
585
|
|
|
440
586
|
const ghostTemplate = readFileSync(resolve(ROOT_ABS, "ghost.json"), "utf8");
|
|
441
|
-
const ghostContent = ghostTemplate.replace(/__ROOT__/g, rootJson);
|
|
587
|
+
const ghostContent = renderRelativeEntries(ghostTemplate.replace(/__ROOT__/g, rootJson), ROOT_ABS);
|
|
442
588
|
writeFileSync(ghostPath, ghostContent, "utf8");
|
|
443
589
|
console.log(`✅ Cindy ghost 已写入: ${ghostPath}`);
|
|
444
590
|
console.log(` 重启 Cindy 后即可在侧栏看到 "Carlos 工作流" 插件`);
|
|
@@ -522,7 +668,44 @@ async function runInit(target) {
|
|
|
522
668
|
console.log(` 提示: 设置环境变量 CARLOS_FLOW_HOME 可自定义状态目录(默认 ~/.carlos-flow)`);
|
|
523
669
|
}
|
|
524
670
|
|
|
671
|
+
// 清理「指向已死进程」的 bridge-*.pid。
|
|
672
|
+
//
|
|
673
|
+
// 背景:bridge 以前从不退出,pid 文件也没人回收,实测仓库根里积了 40 个,
|
|
674
|
+
// 36 个指向早已不存在的进程。那些文件除了干扰人工排查,还会让新启动的
|
|
675
|
+
// 客户端误以为「某个端口上有 bridge」。
|
|
676
|
+
// 只删**确认已死**的,活着的一个不动。返回删掉的个数。
|
|
677
|
+
function sweepStalePidFiles() {
|
|
678
|
+
try {
|
|
679
|
+
const names = readdirSync(ROOT_ABS).filter((f) => /^bridge-\d+\.pid$/.test(f));
|
|
680
|
+
let removed = 0;
|
|
681
|
+
for (const n of names) {
|
|
682
|
+
const full = join(ROOT_ABS, n);
|
|
683
|
+
let pid = 0;
|
|
684
|
+
try { pid = parseInt(readFileSync(full, "utf8").trim(), 10); } catch { continue; }
|
|
685
|
+
if (!pid) {
|
|
686
|
+
// 内容坏了:直接删,它已经提供不了任何信息
|
|
687
|
+
try { unlinkSync(full); removed++; } catch {}
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
if (pid === process.pid) continue;
|
|
691
|
+
let alive = true;
|
|
692
|
+
try { process.kill(pid, 0); } catch { alive = false; }
|
|
693
|
+
if (!alive) {
|
|
694
|
+
try { unlinkSync(full); removed++; } catch {}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
if (removed > 0) process.stderr.write(`[carlos] 已清理 ${removed} 个失效的 bridge pid 文件\n`);
|
|
698
|
+
return removed;
|
|
699
|
+
} catch {
|
|
700
|
+
return 0;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
525
704
|
async function main() {
|
|
705
|
+
// 启动前先扫一遍死工牌:bridge 现在会空闲自退,历史遗留的 pid 文件
|
|
706
|
+
// 必须自动回收,不能越积越多。
|
|
707
|
+
sweepStalePidFiles();
|
|
708
|
+
|
|
526
709
|
// 确保 bridge 运行(带重试)
|
|
527
710
|
let lastErr = null;
|
|
528
711
|
for (let attempt = 0; attempt < 3; attempt++) {
|