carlos-flow 2.12.8 → 2.14.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/AGENTS.md +15 -1
- package/CHANGELOG.md +23 -0
- package/README.en.md +27 -2
- package/README.md +26 -3
- package/README.zh.md +26 -3
- package/TRIGGER_GUIDE.md +3 -3
- package/bridge/mcp-bridge.js +106 -6
- package/client/carlos.js +157 -3
- package/ghost.json +1 -1
- package/package.json +8 -4
- package/scripts/check-stubs.mjs +1 -1
- package/scripts/full-audit.mjs +3 -1
- package/scripts/lang-audit.mjs +7 -1
- package/scripts/mutation-check.mjs +79 -12
- package/src/dispatch.js +204 -17
- package/src/index.js +181 -47
- package/src/protocol.js +182 -1
- package/src/state.js +36 -4
package/src/state.js
CHANGED
|
@@ -307,6 +307,7 @@ export function withStateLock(sid, fn, opts = {}) {
|
|
|
307
307
|
const staleMs = Number(opts.staleMs) || 30000;
|
|
308
308
|
const deadline = Date.now() + timeoutMs;
|
|
309
309
|
let acquired = false;
|
|
310
|
+
let attempt = 0;
|
|
310
311
|
|
|
311
312
|
for (;;) {
|
|
312
313
|
try {
|
|
@@ -314,14 +315,34 @@ export function withStateLock(sid, fn, opts = {}) {
|
|
|
314
315
|
// wx = O_CREAT|O_EXCL,文件已存在则抛 EEXIST —— 原子排他
|
|
315
316
|
const fd = fs.openSync(lockFile, "wx");
|
|
316
317
|
fs.closeSync(fd);
|
|
318
|
+
// 把持有者 pid 写进锁文件:**排他性仍完全由 O_CREAT|O_EXCL 保证**,
|
|
319
|
+
// 写内容只发生在拿到锁之后,用途有两个:
|
|
320
|
+
// ① 诊断 —— 锁超时时能说清「是哪个进程占着」;
|
|
321
|
+
// ② 即时回收 —— 持有者崩了(ESRCH)可以立刻回收,不必干等满 staleMs。
|
|
322
|
+
try { fs.writeFileSync(lockFile, String(process.pid), "utf8"); } catch { /* 内容非必需 */ }
|
|
317
323
|
acquired = true;
|
|
318
324
|
break;
|
|
319
325
|
} catch (e) {
|
|
320
326
|
if (e.code !== "EEXIST") throw e;
|
|
321
|
-
// 陈旧锁(持有者已崩溃)→
|
|
327
|
+
// 陈旧锁(持有者已崩溃)→ 回收。两道判定:
|
|
328
|
+
// ① 锁文件里记录的 pid 已不是活进程(ESRCH)→ 立即回收;
|
|
329
|
+
// ② 老版本锁文件没有 pid,或 pid 不可解析 → 只能按 mtime 超过 staleMs 判陈旧。
|
|
330
|
+
// EPERM 表示「进程存在但无权发信号」→ 按活着处理,绝不误删别人的锁。
|
|
322
331
|
try {
|
|
323
332
|
const st = fs.statSync(lockFile);
|
|
324
|
-
|
|
333
|
+
let holderDead = false;
|
|
334
|
+
let holderPid = 0;
|
|
335
|
+
try {
|
|
336
|
+
holderPid = parseInt(fs.readFileSync(lockFile, "utf8").trim(), 10);
|
|
337
|
+
if (Number.isInteger(holderPid) && holderPid > 0 && holderPid !== process.pid) {
|
|
338
|
+
try {
|
|
339
|
+
process.kill(holderPid, 0);
|
|
340
|
+
} catch (err) {
|
|
341
|
+
if (err && err.code === "ESRCH") holderDead = true;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
} catch { /* 内容读不到:按时间判定 */ }
|
|
345
|
+
if (holderDead || Date.now() - st.mtimeMs > staleMs) {
|
|
325
346
|
fs.unlinkSync(lockFile);
|
|
326
347
|
continue;
|
|
327
348
|
}
|
|
@@ -330,9 +351,20 @@ export function withStateLock(sid, fn, opts = {}) {
|
|
|
330
351
|
}
|
|
331
352
|
if (Date.now() > deadline) {
|
|
332
353
|
// 拿不到锁:fail-closed —— 宁可报错也不要在无锁下写状态
|
|
333
|
-
|
|
354
|
+
let holder = "";
|
|
355
|
+
try {
|
|
356
|
+
const pid = parseInt(fs.readFileSync(lockFile, "utf8").trim(), 10);
|
|
357
|
+
if (Number.isInteger(pid) && pid > 0) holder = `(持有者 pid=${pid})`;
|
|
358
|
+
} catch { /* 读不到内容就不提 */ }
|
|
359
|
+
throw new Error(`获取状态锁超时(session=${sanitizeId(sid)}${holder}),请稍后重试`);
|
|
334
360
|
}
|
|
335
|
-
|
|
361
|
+
// 指数退避 + 抖动。
|
|
362
|
+
// 固定 15ms 会让多个进程「同步醒来」反复撞在一起(并发套件里表现为偶发超时);
|
|
363
|
+
// 抖动把重试错开,退避上限 120ms 保证总吞吐不被拖垮。
|
|
364
|
+
attempt++;
|
|
365
|
+
const base = Math.min(120, 5 * Math.pow(1.6, attempt));
|
|
366
|
+
const jitter = Math.floor(Math.random() * base * 0.3);
|
|
367
|
+
sleepMs(Math.floor(base + jitter));
|
|
336
368
|
}
|
|
337
369
|
}
|
|
338
370
|
|