@kenhkl/agent-moss 0.17.23 → 0.17.24
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/dist/cli.js +88 -21
- package/dist/engine/coordinator.js +4 -4
- package/dist/server.js +88 -21
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
5
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
6
|
-
}) : x)(function(x) {
|
|
7
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
8
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
9
|
-
});
|
|
10
4
|
var __esm = (fn, res) => function __init() {
|
|
11
5
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
12
6
|
};
|
|
@@ -879,7 +873,7 @@ var init_rules_contract = __esm({
|
|
|
879
873
|
|
|
880
874
|
// src/infra/security-policy.ts
|
|
881
875
|
import { createHash as createHash2, randomBytes, timingSafeEqual } from "crypto";
|
|
882
|
-
import { chmodSync, closeSync, existsSync as existsSync3, lstatSync, mkdirSync, openSync, readFileSync as readFileSync4, readSync, realpathSync, writeSync } from "fs";
|
|
876
|
+
import { chmodSync, closeSync, constants as fsConstants, existsSync as existsSync3, lstatSync, mkdirSync, openSync, readFileSync as readFileSync4, readSync, realpathSync, writeSync } from "fs";
|
|
883
877
|
import { homedir as homedir2, tmpdir } from "os";
|
|
884
878
|
import { basename, isAbsolute, join as join4, relative, resolve as resolve2, dirname as dirname2 } from "path";
|
|
885
879
|
function env(name, dflt = "") {
|
|
@@ -1163,9 +1157,8 @@ function ensurePrivateDir(dir) {
|
|
|
1163
1157
|
}
|
|
1164
1158
|
function writePrivateText(file, text) {
|
|
1165
1159
|
try {
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
if (flags.O_NOFOLLOW) openFlags |= flags.O_NOFOLLOW;
|
|
1160
|
+
let openFlags = fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_TRUNC;
|
|
1161
|
+
if (fsConstants.O_NOFOLLOW) openFlags |= fsConstants.O_NOFOLLOW;
|
|
1169
1162
|
const fd = openSync(file, openFlags, 384);
|
|
1170
1163
|
try {
|
|
1171
1164
|
writeSync(fd, text);
|
|
@@ -8249,7 +8242,7 @@ var init_token_stats = __esm({
|
|
|
8249
8242
|
|
|
8250
8243
|
// src/engine/llm-analyzer.ts
|
|
8251
8244
|
import { createHash as createHash3 } from "crypto";
|
|
8252
|
-
import { openSync as openSync2, writeSync as writeSync2, closeSync as closeSync2, constants as
|
|
8245
|
+
import { openSync as openSync2, writeSync as writeSync2, closeSync as closeSync2, constants as fsConstants2 } from "fs";
|
|
8253
8246
|
import OpenAI from "openai";
|
|
8254
8247
|
function responseFieldLengths(message) {
|
|
8255
8248
|
if (!message || typeof message !== "object") return {};
|
|
@@ -8386,8 +8379,8 @@ function logPrompt(prompt, aNext, logPath) {
|
|
|
8386
8379
|
${prompt}
|
|
8387
8380
|
--- END PROMPT ---
|
|
8388
8381
|
`;
|
|
8389
|
-
let flags =
|
|
8390
|
-
if (
|
|
8382
|
+
let flags = fsConstants2.O_WRONLY | fsConstants2.O_CREAT | fsConstants2.O_APPEND;
|
|
8383
|
+
if (fsConstants2.O_NOFOLLOW) flags |= fsConstants2.O_NOFOLLOW;
|
|
8391
8384
|
const fd = openSync2(logPath, flags, 384);
|
|
8392
8385
|
try {
|
|
8393
8386
|
writeSync2(fd, line);
|
|
@@ -11705,6 +11698,13 @@ var init_chain_state = __esm({
|
|
|
11705
11698
|
_trackers = /* @__PURE__ */ new Map();
|
|
11706
11699
|
/** 自上次落盘以来变过的 agentId */
|
|
11707
11700
|
_dirty = /* @__PURE__ */ new Set();
|
|
11701
|
+
/**
|
|
11702
|
+
* 多进程模式(2026-11,对齐 Python `enable_multi_process`):
|
|
11703
|
+
* AGENT_MOSS_MULTI_PROCESS=1 时启用。Node cluster / PM2 多进程部署下,
|
|
11704
|
+
* 各进程的 `data` 是独立快照,防抖写会互相覆盖(JSON 文件没有 SQLite 那样的锁)。
|
|
11705
|
+
* 启用后:每次 applyAction 用**文件锁**串行化,锁内重新读文件 → 判定 → 立即写。
|
|
11706
|
+
*/
|
|
11707
|
+
_multiProcess = false;
|
|
11708
11708
|
constructor(filePath = "") {
|
|
11709
11709
|
this.filePath = filePath || defaultStateDbPath();
|
|
11710
11710
|
this.data = this._read();
|
|
@@ -11775,6 +11775,55 @@ var init_chain_state = __esm({
|
|
|
11775
11775
|
delete this.data[agentId];
|
|
11776
11776
|
this._scheduleWrite();
|
|
11777
11777
|
}
|
|
11778
|
+
/** 启用多进程模式(对齐 Python)。 */
|
|
11779
|
+
enableMultiProcess() {
|
|
11780
|
+
this._multiProcess = true;
|
|
11781
|
+
}
|
|
11782
|
+
/**
|
|
11783
|
+
* 同步文件锁(原子创建 `<file>.lock`)。
|
|
11784
|
+
*
|
|
11785
|
+
* Node 是单线程,锁竞争时用 Atomics.wait 做**同步睡眠**而不是忙等 ——
|
|
11786
|
+
* 忙等会把事件循环完全占死,其它请求也没法推进。
|
|
11787
|
+
*/
|
|
11788
|
+
_withFileLock(fn) {
|
|
11789
|
+
const lockPath = `${this.filePath}.lock`;
|
|
11790
|
+
const sleepBuf = new Int32Array(new SharedArrayBuffer(4));
|
|
11791
|
+
for (let attempt = 0; attempt < 100; attempt++) {
|
|
11792
|
+
try {
|
|
11793
|
+
const fd = fs5.openSync(lockPath, "wx");
|
|
11794
|
+
try {
|
|
11795
|
+
return fn();
|
|
11796
|
+
} finally {
|
|
11797
|
+
try {
|
|
11798
|
+
fs5.closeSync(fd);
|
|
11799
|
+
} catch {
|
|
11800
|
+
}
|
|
11801
|
+
try {
|
|
11802
|
+
fs5.unlinkSync(lockPath);
|
|
11803
|
+
} catch {
|
|
11804
|
+
}
|
|
11805
|
+
}
|
|
11806
|
+
} catch (e) {
|
|
11807
|
+
if (e?.code !== "EEXIST") throw e;
|
|
11808
|
+
try {
|
|
11809
|
+
Atomics.wait(sleepBuf, 0, 0, 20);
|
|
11810
|
+
} catch {
|
|
11811
|
+
}
|
|
11812
|
+
}
|
|
11813
|
+
}
|
|
11814
|
+
throw new Error(`chain state lock timeout: ${lockPath}`);
|
|
11815
|
+
}
|
|
11816
|
+
/** 立即把内存写进文件(多进程模式用,不走防抖)。 */
|
|
11817
|
+
_flushNow() {
|
|
11818
|
+
this._syncDirtyToData();
|
|
11819
|
+
try {
|
|
11820
|
+
const _dirOk = ensurePrivateDir(path5.dirname(this.filePath));
|
|
11821
|
+
const _wOk = writePrivateText(this.filePath, JSON.stringify(this.data, null, 2));
|
|
11822
|
+
if (!_wOk) console.error("[AgentMoss] chain state flush failed:", this.filePath, "dirOk=", _dirOk);
|
|
11823
|
+
} catch (e) {
|
|
11824
|
+
console.error("[AgentMoss] chain state flush failed:", e?.message || e);
|
|
11825
|
+
}
|
|
11826
|
+
}
|
|
11778
11827
|
/** 当前内存中的 tracker 数 / 节点总数(健康检查用)。 */
|
|
11779
11828
|
stats() {
|
|
11780
11829
|
let nodes = 0;
|
|
@@ -11782,6 +11831,17 @@ var init_chain_state = __esm({
|
|
|
11782
11831
|
return { agents: this._trackers.size, nodes };
|
|
11783
11832
|
}
|
|
11784
11833
|
applyAction(agentId, aNext, sessionId, isError = false, sourceAgent = "", instanceId = "") {
|
|
11834
|
+
if (this._multiProcess) {
|
|
11835
|
+
return this._withFileLock(() => {
|
|
11836
|
+
this.data = this._read();
|
|
11837
|
+
this._trackers.clear();
|
|
11838
|
+
const t2 = this.load(agentId) || new StatefulChainTracker(agentId);
|
|
11839
|
+
const r2 = t2.advance({ aNext, sessionId, isError, agentId: sourceAgent, instanceId });
|
|
11840
|
+
this.save(t2);
|
|
11841
|
+
this._flushNow();
|
|
11842
|
+
return r2;
|
|
11843
|
+
});
|
|
11844
|
+
}
|
|
11785
11845
|
const t = this.load(agentId) || new StatefulChainTracker(agentId);
|
|
11786
11846
|
const r = t.advance({ aNext, sessionId, isError, agentId: sourceAgent, instanceId });
|
|
11787
11847
|
this.save(t);
|
|
@@ -12317,7 +12377,7 @@ var init_coordinator = __esm({
|
|
|
12317
12377
|
|
|
12318
12378
|
// src/infra/policy-cache.ts
|
|
12319
12379
|
import { createHash as createHash5 } from "crypto";
|
|
12320
|
-
import { existsSync as existsSync17, lstatSync as lstatSync2, readdirSync as readdirSync5, readFileSync as readFileSync17, rmdirSync, unlinkSync as
|
|
12380
|
+
import { existsSync as existsSync17, lstatSync as lstatSync2, readdirSync as readdirSync5, readFileSync as readFileSync17, rmdirSync, unlinkSync as unlinkSync4 } from "fs";
|
|
12321
12381
|
import { tmpdir as tmpdir3 } from "os";
|
|
12322
12382
|
import { join as join16 } from "path";
|
|
12323
12383
|
function cacheDir() {
|
|
@@ -12411,7 +12471,7 @@ var init_policy_cache = __esm({
|
|
|
12411
12471
|
invalidate(sessionId, promptSession) {
|
|
12412
12472
|
this.memory.delete(this.cacheKey(sessionId, promptSession));
|
|
12413
12473
|
try {
|
|
12414
|
-
|
|
12474
|
+
unlinkSync4(this.cacheFilePath(sessionId, promptSession));
|
|
12415
12475
|
} catch {
|
|
12416
12476
|
}
|
|
12417
12477
|
}
|
|
@@ -12425,7 +12485,7 @@ var init_policy_cache = __esm({
|
|
|
12425
12485
|
if (existsSync17(dir)) {
|
|
12426
12486
|
for (const f of readdirSync5(dir)) {
|
|
12427
12487
|
try {
|
|
12428
|
-
|
|
12488
|
+
unlinkSync4(join16(dir, f));
|
|
12429
12489
|
} catch {
|
|
12430
12490
|
}
|
|
12431
12491
|
}
|
|
@@ -12441,7 +12501,7 @@ var init_policy_cache = __esm({
|
|
|
12441
12501
|
for (const sessionDir of readdirSync5(cacheDir())) {
|
|
12442
12502
|
const dir = join16(cacheDir(), sessionDir);
|
|
12443
12503
|
try {
|
|
12444
|
-
for (const f of readdirSync5(dir))
|
|
12504
|
+
for (const f of readdirSync5(dir)) unlinkSync4(join16(dir, f));
|
|
12445
12505
|
rmdirSync(dir);
|
|
12446
12506
|
} catch {
|
|
12447
12507
|
}
|
|
@@ -13760,7 +13820,7 @@ var init_routes = __esm({
|
|
|
13760
13820
|
// src/console/router.ts
|
|
13761
13821
|
import { Hono } from "hono";
|
|
13762
13822
|
import { timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
13763
|
-
import { readFileSync as readFileSync20, existsSync as existsSync20, unlinkSync as
|
|
13823
|
+
import { readFileSync as readFileSync20, existsSync as existsSync20, unlinkSync as unlinkSync5 } from "fs";
|
|
13764
13824
|
import { join as join20, dirname as dirname13 } from "path";
|
|
13765
13825
|
import { fileURLToPath as fileURLToPath11 } from "url";
|
|
13766
13826
|
function getConsoleToken() {
|
|
@@ -14105,7 +14165,7 @@ function createConsoleRouter() {
|
|
|
14105
14165
|
const defaults = generateSourceDefaults();
|
|
14106
14166
|
try {
|
|
14107
14167
|
const p = getRuntimeConfigPath();
|
|
14108
|
-
if (existsSync20(p))
|
|
14168
|
+
if (existsSync20(p)) unlinkSync5(p);
|
|
14109
14169
|
} catch {
|
|
14110
14170
|
}
|
|
14111
14171
|
return c.json(withL22View(defaults));
|
|
@@ -14236,6 +14296,13 @@ function logDeploymentSelfCheck() {
|
|
|
14236
14296
|
}
|
|
14237
14297
|
}
|
|
14238
14298
|
function createApp() {
|
|
14299
|
+
try {
|
|
14300
|
+
if (String(process.env.AGENT_MOSS_MULTI_PROCESS || "").toLowerCase() in { "1": 1, "true": 1, "yes": 1, "on": 1 }) {
|
|
14301
|
+
getChainStateStore().enableMultiProcess();
|
|
14302
|
+
console.log("[AgentMoss] \u94FE\u72B6\u6001\u591A\u8FDB\u7A0B\u6A21\u5F0F\uFF1A\u5DF2\u542F\u7528\uFF08\u6587\u4EF6\u9501 + \u7ACB\u5373\u843D\u76D8\uFF09");
|
|
14303
|
+
}
|
|
14304
|
+
} catch {
|
|
14305
|
+
}
|
|
14239
14306
|
enforceRulesContract();
|
|
14240
14307
|
hardenExistingPaths();
|
|
14241
14308
|
const app = new Hono2();
|
|
@@ -14438,7 +14505,7 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
|
|
|
14438
14505
|
},
|
|
14439
14506
|
serve: async () => {
|
|
14440
14507
|
failFastOnAuthError();
|
|
14441
|
-
const { mkdirSync: mkdirSync10, existsSync: existsSync23, unlinkSync:
|
|
14508
|
+
const { mkdirSync: mkdirSync10, existsSync: existsSync23, unlinkSync: unlinkSync6 } = await import("fs");
|
|
14442
14509
|
const { dirname: dirname15 } = await import("path");
|
|
14443
14510
|
const dir = dirname15(socketPath);
|
|
14444
14511
|
if (dir) {
|
|
@@ -14465,7 +14532,7 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
|
|
|
14465
14532
|
);
|
|
14466
14533
|
} else {
|
|
14467
14534
|
try {
|
|
14468
|
-
|
|
14535
|
+
unlinkSync6(socketPath);
|
|
14469
14536
|
console.log(`[AgentMoss] \u6E05\u7406\u6B8B\u7559 socket \u6587\u4EF6: ${socketPath}`);
|
|
14470
14537
|
} catch {
|
|
14471
14538
|
}
|
|
@@ -4836,7 +4836,7 @@ import { createHash } from "crypto";
|
|
|
4836
4836
|
// src/infra/security-policy.ts
|
|
4837
4837
|
init_esm_shims();
|
|
4838
4838
|
init_paths();
|
|
4839
|
-
import { chmodSync, closeSync, existsSync as existsSync8, lstatSync, mkdirSync as mkdirSync2, openSync, readFileSync as readFileSync7, readSync, realpathSync, writeSync } from "fs";
|
|
4839
|
+
import { chmodSync, closeSync, constants as fsConstants, existsSync as existsSync8, lstatSync, mkdirSync as mkdirSync2, openSync, readFileSync as readFileSync7, readSync, realpathSync, writeSync } from "fs";
|
|
4840
4840
|
import { homedir as homedir2, tmpdir } from "os";
|
|
4841
4841
|
import { basename, isAbsolute, join as join7, relative, resolve as resolve6, dirname as dirname6 } from "path";
|
|
4842
4842
|
var TRUTHY2 = ["1", "true", "yes", "on"];
|
|
@@ -7021,7 +7021,7 @@ ${contentPreview}
|
|
|
7021
7021
|
}
|
|
7022
7022
|
|
|
7023
7023
|
// src/engine/llm-analyzer.ts
|
|
7024
|
-
import { openSync as openSync2, writeSync as writeSync2, closeSync as closeSync2, constants as
|
|
7024
|
+
import { openSync as openSync2, writeSync as writeSync2, closeSync as closeSync2, constants as fsConstants2 } from "fs";
|
|
7025
7025
|
import OpenAI from "openai";
|
|
7026
7026
|
|
|
7027
7027
|
// src/infra/trace-log.ts
|
|
@@ -7558,8 +7558,8 @@ function logPrompt(prompt, aNext, logPath) {
|
|
|
7558
7558
|
${prompt}
|
|
7559
7559
|
--- END PROMPT ---
|
|
7560
7560
|
`;
|
|
7561
|
-
let flags =
|
|
7562
|
-
if (
|
|
7561
|
+
let flags = fsConstants2.O_WRONLY | fsConstants2.O_CREAT | fsConstants2.O_APPEND;
|
|
7562
|
+
if (fsConstants2.O_NOFOLLOW) flags |= fsConstants2.O_NOFOLLOW;
|
|
7563
7563
|
const fd = openSync2(logPath, flags, 384);
|
|
7564
7564
|
try {
|
|
7565
7565
|
writeSync2(fd, line);
|
package/dist/server.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
4
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
5
|
-
}) : x)(function(x) {
|
|
6
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
7
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
|
-
});
|
|
9
3
|
var __esm = (fn, res) => function __init() {
|
|
10
4
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
11
5
|
};
|
|
@@ -725,7 +719,7 @@ var init_rules_loader = __esm({
|
|
|
725
719
|
|
|
726
720
|
// src/infra/security-policy.ts
|
|
727
721
|
import { createHash as createHash2, randomBytes, timingSafeEqual } from "crypto";
|
|
728
|
-
import { chmodSync, closeSync, existsSync as existsSync3, lstatSync, mkdirSync, openSync, readFileSync as readFileSync4, readSync, realpathSync, writeSync } from "fs";
|
|
722
|
+
import { chmodSync, closeSync, constants as fsConstants, existsSync as existsSync3, lstatSync, mkdirSync, openSync, readFileSync as readFileSync4, readSync, realpathSync, writeSync } from "fs";
|
|
729
723
|
import { homedir as homedir2, tmpdir } from "os";
|
|
730
724
|
import { basename, isAbsolute, join as join4, relative, resolve as resolve2, dirname as dirname2 } from "path";
|
|
731
725
|
function env(name, dflt = "") {
|
|
@@ -1009,9 +1003,8 @@ function ensurePrivateDir(dir) {
|
|
|
1009
1003
|
}
|
|
1010
1004
|
function writePrivateText(file, text) {
|
|
1011
1005
|
try {
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
if (flags.O_NOFOLLOW) openFlags |= flags.O_NOFOLLOW;
|
|
1006
|
+
let openFlags = fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_TRUNC;
|
|
1007
|
+
if (fsConstants.O_NOFOLLOW) openFlags |= fsConstants.O_NOFOLLOW;
|
|
1015
1008
|
const fd = openSync(file, openFlags, 384);
|
|
1016
1009
|
try {
|
|
1017
1010
|
writeSync(fd, text);
|
|
@@ -7949,7 +7942,7 @@ var init_token_stats = __esm({
|
|
|
7949
7942
|
|
|
7950
7943
|
// src/engine/llm-analyzer.ts
|
|
7951
7944
|
import { createHash as createHash3 } from "crypto";
|
|
7952
|
-
import { openSync as openSync2, writeSync as writeSync2, closeSync as closeSync2, constants as
|
|
7945
|
+
import { openSync as openSync2, writeSync as writeSync2, closeSync as closeSync2, constants as fsConstants2 } from "fs";
|
|
7953
7946
|
import OpenAI from "openai";
|
|
7954
7947
|
function responseFieldLengths(message) {
|
|
7955
7948
|
if (!message || typeof message !== "object") return {};
|
|
@@ -8086,8 +8079,8 @@ function logPrompt(prompt, aNext, logPath) {
|
|
|
8086
8079
|
${prompt}
|
|
8087
8080
|
--- END PROMPT ---
|
|
8088
8081
|
`;
|
|
8089
|
-
let flags =
|
|
8090
|
-
if (
|
|
8082
|
+
let flags = fsConstants2.O_WRONLY | fsConstants2.O_CREAT | fsConstants2.O_APPEND;
|
|
8083
|
+
if (fsConstants2.O_NOFOLLOW) flags |= fsConstants2.O_NOFOLLOW;
|
|
8091
8084
|
const fd = openSync2(logPath, flags, 384);
|
|
8092
8085
|
try {
|
|
8093
8086
|
writeSync2(fd, line);
|
|
@@ -11140,6 +11133,13 @@ var init_chain_state = __esm({
|
|
|
11140
11133
|
_trackers = /* @__PURE__ */ new Map();
|
|
11141
11134
|
/** 自上次落盘以来变过的 agentId */
|
|
11142
11135
|
_dirty = /* @__PURE__ */ new Set();
|
|
11136
|
+
/**
|
|
11137
|
+
* 多进程模式(2026-11,对齐 Python `enable_multi_process`):
|
|
11138
|
+
* AGENT_MOSS_MULTI_PROCESS=1 时启用。Node cluster / PM2 多进程部署下,
|
|
11139
|
+
* 各进程的 `data` 是独立快照,防抖写会互相覆盖(JSON 文件没有 SQLite 那样的锁)。
|
|
11140
|
+
* 启用后:每次 applyAction 用**文件锁**串行化,锁内重新读文件 → 判定 → 立即写。
|
|
11141
|
+
*/
|
|
11142
|
+
_multiProcess = false;
|
|
11143
11143
|
constructor(filePath = "") {
|
|
11144
11144
|
this.filePath = filePath || defaultStateDbPath();
|
|
11145
11145
|
this.data = this._read();
|
|
@@ -11210,6 +11210,55 @@ var init_chain_state = __esm({
|
|
|
11210
11210
|
delete this.data[agentId];
|
|
11211
11211
|
this._scheduleWrite();
|
|
11212
11212
|
}
|
|
11213
|
+
/** 启用多进程模式(对齐 Python)。 */
|
|
11214
|
+
enableMultiProcess() {
|
|
11215
|
+
this._multiProcess = true;
|
|
11216
|
+
}
|
|
11217
|
+
/**
|
|
11218
|
+
* 同步文件锁(原子创建 `<file>.lock`)。
|
|
11219
|
+
*
|
|
11220
|
+
* Node 是单线程,锁竞争时用 Atomics.wait 做**同步睡眠**而不是忙等 ——
|
|
11221
|
+
* 忙等会把事件循环完全占死,其它请求也没法推进。
|
|
11222
|
+
*/
|
|
11223
|
+
_withFileLock(fn) {
|
|
11224
|
+
const lockPath = `${this.filePath}.lock`;
|
|
11225
|
+
const sleepBuf = new Int32Array(new SharedArrayBuffer(4));
|
|
11226
|
+
for (let attempt = 0; attempt < 100; attempt++) {
|
|
11227
|
+
try {
|
|
11228
|
+
const fd = fs5.openSync(lockPath, "wx");
|
|
11229
|
+
try {
|
|
11230
|
+
return fn();
|
|
11231
|
+
} finally {
|
|
11232
|
+
try {
|
|
11233
|
+
fs5.closeSync(fd);
|
|
11234
|
+
} catch {
|
|
11235
|
+
}
|
|
11236
|
+
try {
|
|
11237
|
+
fs5.unlinkSync(lockPath);
|
|
11238
|
+
} catch {
|
|
11239
|
+
}
|
|
11240
|
+
}
|
|
11241
|
+
} catch (e) {
|
|
11242
|
+
if (e?.code !== "EEXIST") throw e;
|
|
11243
|
+
try {
|
|
11244
|
+
Atomics.wait(sleepBuf, 0, 0, 20);
|
|
11245
|
+
} catch {
|
|
11246
|
+
}
|
|
11247
|
+
}
|
|
11248
|
+
}
|
|
11249
|
+
throw new Error(`chain state lock timeout: ${lockPath}`);
|
|
11250
|
+
}
|
|
11251
|
+
/** 立即把内存写进文件(多进程模式用,不走防抖)。 */
|
|
11252
|
+
_flushNow() {
|
|
11253
|
+
this._syncDirtyToData();
|
|
11254
|
+
try {
|
|
11255
|
+
const _dirOk = ensurePrivateDir(path5.dirname(this.filePath));
|
|
11256
|
+
const _wOk = writePrivateText(this.filePath, JSON.stringify(this.data, null, 2));
|
|
11257
|
+
if (!_wOk) console.error("[AgentMoss] chain state flush failed:", this.filePath, "dirOk=", _dirOk);
|
|
11258
|
+
} catch (e) {
|
|
11259
|
+
console.error("[AgentMoss] chain state flush failed:", e?.message || e);
|
|
11260
|
+
}
|
|
11261
|
+
}
|
|
11213
11262
|
/** 当前内存中的 tracker 数 / 节点总数(健康检查用)。 */
|
|
11214
11263
|
stats() {
|
|
11215
11264
|
let nodes = 0;
|
|
@@ -11217,6 +11266,17 @@ var init_chain_state = __esm({
|
|
|
11217
11266
|
return { agents: this._trackers.size, nodes };
|
|
11218
11267
|
}
|
|
11219
11268
|
applyAction(agentId, aNext, sessionId, isError = false, sourceAgent = "", instanceId = "") {
|
|
11269
|
+
if (this._multiProcess) {
|
|
11270
|
+
return this._withFileLock(() => {
|
|
11271
|
+
this.data = this._read();
|
|
11272
|
+
this._trackers.clear();
|
|
11273
|
+
const t2 = this.load(agentId) || new StatefulChainTracker(agentId);
|
|
11274
|
+
const r2 = t2.advance({ aNext, sessionId, isError, agentId: sourceAgent, instanceId });
|
|
11275
|
+
this.save(t2);
|
|
11276
|
+
this._flushNow();
|
|
11277
|
+
return r2;
|
|
11278
|
+
});
|
|
11279
|
+
}
|
|
11220
11280
|
const t = this.load(agentId) || new StatefulChainTracker(agentId);
|
|
11221
11281
|
const r = t.advance({ aNext, sessionId, isError, agentId: sourceAgent, instanceId });
|
|
11222
11282
|
this.save(t);
|
|
@@ -12053,7 +12113,7 @@ init_security_policy();
|
|
|
12053
12113
|
init_paths();
|
|
12054
12114
|
init_config();
|
|
12055
12115
|
import { createHash as createHash5 } from "crypto";
|
|
12056
|
-
import { existsSync as existsSync17, lstatSync as lstatSync2, readdirSync as readdirSync5, readFileSync as readFileSync17, rmdirSync, unlinkSync as
|
|
12116
|
+
import { existsSync as existsSync17, lstatSync as lstatSync2, readdirSync as readdirSync5, readFileSync as readFileSync17, rmdirSync, unlinkSync as unlinkSync4 } from "fs";
|
|
12057
12117
|
import { tmpdir as tmpdir2 } from "os";
|
|
12058
12118
|
import { join as join15 } from "path";
|
|
12059
12119
|
var CACHE_DIR = join15(tmpdir2(), "agent_moss_policy_cache");
|
|
@@ -12135,7 +12195,7 @@ var PolicyCache = class {
|
|
|
12135
12195
|
invalidate(sessionId, promptSession) {
|
|
12136
12196
|
this.memory.delete(this.cacheKey(sessionId, promptSession));
|
|
12137
12197
|
try {
|
|
12138
|
-
|
|
12198
|
+
unlinkSync4(this.cacheFilePath(sessionId, promptSession));
|
|
12139
12199
|
} catch {
|
|
12140
12200
|
}
|
|
12141
12201
|
}
|
|
@@ -12149,7 +12209,7 @@ var PolicyCache = class {
|
|
|
12149
12209
|
if (existsSync17(dir)) {
|
|
12150
12210
|
for (const f of readdirSync5(dir)) {
|
|
12151
12211
|
try {
|
|
12152
|
-
|
|
12212
|
+
unlinkSync4(join15(dir, f));
|
|
12153
12213
|
} catch {
|
|
12154
12214
|
}
|
|
12155
12215
|
}
|
|
@@ -12165,7 +12225,7 @@ var PolicyCache = class {
|
|
|
12165
12225
|
for (const sessionDir of readdirSync5(cacheDir())) {
|
|
12166
12226
|
const dir = join15(cacheDir(), sessionDir);
|
|
12167
12227
|
try {
|
|
12168
|
-
for (const f of readdirSync5(dir))
|
|
12228
|
+
for (const f of readdirSync5(dir)) unlinkSync4(join15(dir, f));
|
|
12169
12229
|
rmdirSync(dir);
|
|
12170
12230
|
} catch {
|
|
12171
12231
|
}
|
|
@@ -13409,7 +13469,7 @@ init_asset_dir();
|
|
|
13409
13469
|
init_rules_mode();
|
|
13410
13470
|
import { Hono } from "hono";
|
|
13411
13471
|
import { timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
13412
|
-
import { readFileSync as readFileSync20, existsSync as existsSync20, unlinkSync as
|
|
13472
|
+
import { readFileSync as readFileSync20, existsSync as existsSync20, unlinkSync as unlinkSync5 } from "fs";
|
|
13413
13473
|
import { join as join19, dirname as dirname13 } from "path";
|
|
13414
13474
|
import { fileURLToPath as fileURLToPath11 } from "url";
|
|
13415
13475
|
var __dirname10 = dirname13(fileURLToPath11(import.meta.url));
|
|
@@ -13757,7 +13817,7 @@ function createConsoleRouter() {
|
|
|
13757
13817
|
const defaults = generateSourceDefaults();
|
|
13758
13818
|
try {
|
|
13759
13819
|
const p = getRuntimeConfigPath();
|
|
13760
|
-
if (existsSync20(p))
|
|
13820
|
+
if (existsSync20(p)) unlinkSync5(p);
|
|
13761
13821
|
} catch {
|
|
13762
13822
|
}
|
|
13763
13823
|
return c.json(withL22View(defaults));
|
|
@@ -13900,6 +13960,13 @@ var RateLimiter = class {
|
|
|
13900
13960
|
}
|
|
13901
13961
|
};
|
|
13902
13962
|
function createApp() {
|
|
13963
|
+
try {
|
|
13964
|
+
if (String(process.env.AGENT_MOSS_MULTI_PROCESS || "").toLowerCase() in { "1": 1, "true": 1, "yes": 1, "on": 1 }) {
|
|
13965
|
+
getChainStateStore().enableMultiProcess();
|
|
13966
|
+
console.log("[AgentMoss] \u94FE\u72B6\u6001\u591A\u8FDB\u7A0B\u6A21\u5F0F\uFF1A\u5DF2\u542F\u7528\uFF08\u6587\u4EF6\u9501 + \u7ACB\u5373\u843D\u76D8\uFF09");
|
|
13967
|
+
}
|
|
13968
|
+
} catch {
|
|
13969
|
+
}
|
|
13903
13970
|
enforceRulesContract();
|
|
13904
13971
|
hardenExistingPaths();
|
|
13905
13972
|
const app = new Hono2();
|
|
@@ -14108,7 +14175,7 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
|
|
|
14108
14175
|
},
|
|
14109
14176
|
serve: async () => {
|
|
14110
14177
|
failFastOnAuthError();
|
|
14111
|
-
const { mkdirSync: mkdirSync8, existsSync: existsSync22, unlinkSync:
|
|
14178
|
+
const { mkdirSync: mkdirSync8, existsSync: existsSync22, unlinkSync: unlinkSync6 } = await import("fs");
|
|
14112
14179
|
const { dirname: dirname14 } = await import("path");
|
|
14113
14180
|
const dir = dirname14(socketPath);
|
|
14114
14181
|
if (dir) {
|
|
@@ -14135,7 +14202,7 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
|
|
|
14135
14202
|
);
|
|
14136
14203
|
} else {
|
|
14137
14204
|
try {
|
|
14138
|
-
|
|
14205
|
+
unlinkSync6(socketPath);
|
|
14139
14206
|
console.log(`[AgentMoss] \u6E05\u7406\u6B8B\u7559 socket \u6587\u4EF6: ${socketPath}`);
|
|
14140
14207
|
} catch {
|
|
14141
14208
|
}
|