@kenhkl/agent-moss 0.17.36 → 0.17.38

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 CHANGED
@@ -1130,7 +1130,7 @@ function logRoots() {
1130
1130
  }
1131
1131
  function reviewRoots() {
1132
1132
  const roots = [join4(homeDir(), ".local", "share", "agent_gate")];
1133
- for (const name of ["AGENT_GATE_DB", "AGENT_GATE_JSON_PATH"]) {
1133
+ for (const name of ["AGENT_GATE_DB", "AGENT_GATE_JSON_PATH", "AGENT_GATE_DATA_DIR"]) {
1134
1134
  const v = env(name);
1135
1135
  if (v) roots.push(existsSync3(v) && lstatSync(v).isDirectory() ? v : dirname2(v));
1136
1136
  }
@@ -3310,7 +3310,7 @@ function _dedup(rules, category) {
3310
3310
  const seen = /* @__PURE__ */ new Set();
3311
3311
  const out = [];
3312
3312
  for (const r of rules) {
3313
- const k = keys.map((x) => String(r?.[x] ?? "")).join("\0");
3313
+ const k = keys.map((x) => x === "os" ? String(r?.os || "linux") : String(r?.[x] ?? "")).join("\0");
3314
3314
  if (seen.has(k)) continue;
3315
3315
  seen.add(k);
3316
3316
  out.push(r);
@@ -4530,7 +4530,11 @@ var init_runtime_config = __esm({
4530
4530
  dangerous_commands: ["pattern"],
4531
4531
  password_modify_consent: ["pattern"],
4532
4532
  user_deletion_consent: ["pattern"],
4533
- sensitive_path_access: ["path"],
4533
+ // ⚠️ 必须含 os:跨平台同名 path(.ssh / .aws / .agentmoss)各有独立条目。
4534
+ // 去重发生在 _filterRulesByOs 之前,若键只有 path,linux 条目(种子中先出现)
4535
+ // 会把 windows 同名条目顶掉 → Windows 真实运行时的目录级删除拦截静默失效
4536
+ // (2026-11 修;与 Python `_DEDUP_KEYS` 同步)。
4537
+ sensitive_path_access: ["path", "os"],
4534
4538
  injection_detection: ["keyword", "lang"],
4535
4539
  intent_consistency: ["intent_keywords", "reason"]
4536
4540
  };
@@ -11998,6 +12002,74 @@ function lifecycleFor(detail, stage, isError) {
11998
12002
  if (detail.includes(">>") || detail.includes(">")) return "alive";
11999
12003
  return "";
12000
12004
  }
12005
+ function resolveMaxNodes() {
12006
+ const n = Number.parseInt(process.env.AGENT_MOSS_CHAIN_MAX_NODES || "100000", 10);
12007
+ return Number.isFinite(n) && n > 0 ? n : 1e5;
12008
+ }
12009
+ function resolveRetainNodes() {
12010
+ const raw = process.env.AGENT_MOSS_CHAIN_RETAIN_NODES || "";
12011
+ const def = Math.max(1, Math.floor(MAX_NODES / 2));
12012
+ const n = raw ? Number.parseInt(raw, 10) : def;
12013
+ const safe2 = Number.isFinite(n) && n > 0 ? n : def;
12014
+ return Math.max(1, Math.min(safe2, MAX_NODES - 1));
12015
+ }
12016
+ function envInt3(name, def) {
12017
+ const n = Number.parseInt(process.env[name] || "", 10);
12018
+ return Number.isFinite(n) && n >= 0 ? n : def;
12019
+ }
12020
+ function cleanupChainArchives(dir, maxBytes = envInt3("AGENT_MOSS_CHAIN_ARCHIVE_MAX_MB", 512) * 1024 * 1024, maxAgeDays = envInt3("AGENT_MOSS_CHAIN_ARCHIVE_MAX_AGE_DAYS", 0)) {
12021
+ let files = [];
12022
+ try {
12023
+ files = fs5.readdirSync(dir).filter((f) => f.startsWith("chain-nodes-") && f.endsWith(".jsonl.gz")).map((f) => path6.join(dir, f)).sort((a, b) => fs5.statSync(a).mtimeMs - fs5.statSync(b).mtimeMs);
12024
+ } catch {
12025
+ return { deleted: 0, files: 0, bytes: 0 };
12026
+ }
12027
+ let removed = 0;
12028
+ if (maxAgeDays > 0) {
12029
+ const cutoff = Date.now() - maxAgeDays * 864e5;
12030
+ files = files.filter((p) => {
12031
+ try {
12032
+ if (fs5.statSync(p).mtimeMs < cutoff) {
12033
+ fs5.unlinkSync(p);
12034
+ removed += 1;
12035
+ return false;
12036
+ }
12037
+ } catch {
12038
+ }
12039
+ return true;
12040
+ });
12041
+ }
12042
+ let total = files.reduce((n, p) => {
12043
+ try {
12044
+ return n + fs5.statSync(p).size;
12045
+ } catch {
12046
+ return n;
12047
+ }
12048
+ }, 0);
12049
+ if (maxBytes > 0) {
12050
+ for (const p of files) {
12051
+ if (total <= maxBytes) break;
12052
+ try {
12053
+ const size = fs5.statSync(p).size;
12054
+ fs5.unlinkSync(p);
12055
+ total -= size;
12056
+ removed += 1;
12057
+ } catch {
12058
+ }
12059
+ }
12060
+ }
12061
+ let remainFiles = 0;
12062
+ let remainBytes = 0;
12063
+ try {
12064
+ for (const f of fs5.readdirSync(dir)) {
12065
+ if (!f.startsWith("chain-nodes-") || !f.endsWith(".jsonl.gz")) continue;
12066
+ remainFiles += 1;
12067
+ remainBytes += fs5.statSync(path6.join(dir, f)).size;
12068
+ }
12069
+ } catch {
12070
+ }
12071
+ return { deleted: removed, files: remainFiles, bytes: remainBytes };
12072
+ }
12001
12073
  var DESTROY_RE, NEVER_ACTIVATED_RE, CRED_FILE_RE2, EXFIL_RE2, PORT_RE, BARE_PORT_RE, SOCK_RE, UNIT_RE, PORT_STAGE_RE, STAGES_PATTERNS, CHAINS, COMMON_DIR_RESOURCES2, MAX_NODES, StatefulChainTracker;
12002
12074
  var init_stateful = __esm({
12003
12075
  "src/engine/formal-engine/stateful.ts"() {
@@ -12029,7 +12101,7 @@ var init_stateful = __esm({
12029
12101
  "/data",
12030
12102
  "/storage"
12031
12103
  ]);
12032
- MAX_NODES = 1e5;
12104
+ MAX_NODES = resolveMaxNodes();
12033
12105
  StatefulChainTracker = class _StatefulChainTracker {
12034
12106
  /** 本地接收时刻(实时路径无 called_at 时的兜底时间戳,ISO)。 */
12035
12107
  static now() {
@@ -12136,7 +12208,7 @@ var init_stateful = __esm({
12136
12208
  * 派生缓存不是原始历史存储;归档失败抛异常,调用方不得丢状态。
12137
12209
  */
12138
12210
  rotate(archiveDir, retain) {
12139
- const keep = Math.max(1, Math.min(retain ?? Math.floor(MAX_NODES / 2), MAX_NODES - 1));
12211
+ const keep = Math.max(1, Math.min(retain ?? resolveRetainNodes(), MAX_NODES - 1));
12140
12212
  if (this.nodes_.length <= keep) return { archived: 0, file: "" };
12141
12213
  const old = this.nodes_.slice(0, this.nodes_.length - keep);
12142
12214
  const kept = this.nodes_.slice(-keep);
@@ -12146,6 +12218,16 @@ var init_stateful = __esm({
12146
12218
  const file = path6.join(dir, `chain-nodes-${safeAgent}-${(/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-")}.jsonl.gz`);
12147
12219
  const payload = old.map((n) => JSON.stringify(n)).join("\n") + "\n";
12148
12220
  fs5.writeFileSync(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
12221
+ try {
12222
+ const c = cleanupChainArchives(dir);
12223
+ if (c.deleted > 0) {
12224
+ console.error(
12225
+ `[AgentMoss] chain-archive \u6E05\u7406: \u5220\u9664 ${c.deleted} \u4E2A\u65E7\u5F52\u6863\uFF08\u5269\u4F59 ${c.files} \u4E2A / ${c.bytes} \u5B57\u8282\uFF09`
12226
+ );
12227
+ }
12228
+ } catch (e) {
12229
+ console.error(`[AgentMoss] chain-archive \u6E05\u7406\u5931\u8D25\uFF08\u7EE7\u7EED\uFF09: ${e?.message ?? e}`);
12230
+ }
12149
12231
  this.nodes_ = [];
12150
12232
  this.ptr = {};
12151
12233
  this.tainted.clear();
@@ -13391,7 +13473,7 @@ var init_types = __esm({
13391
13473
 
13392
13474
  // src/infra/policy-cache.ts
13393
13475
  import { createHash as createHash5 } from "crypto";
13394
- import { existsSync as existsSync17, lstatSync as lstatSync2, readdirSync as readdirSync5, readFileSync as readFileSync17, rmdirSync, unlinkSync as unlinkSync4 } from "fs";
13476
+ import { existsSync as existsSync17, lstatSync as lstatSync2, readdirSync as readdirSync6, readFileSync as readFileSync17, rmdirSync, unlinkSync as unlinkSync5 } from "fs";
13395
13477
  import { tmpdir as tmpdir3 } from "os";
13396
13478
  import { join as join18 } from "path";
13397
13479
  function cacheDir() {
@@ -13485,7 +13567,7 @@ var init_policy_cache = __esm({
13485
13567
  invalidate(sessionId, promptSession) {
13486
13568
  this.memory.delete(this.cacheKey(sessionId, promptSession));
13487
13569
  try {
13488
- unlinkSync4(this.cacheFilePath(sessionId, promptSession));
13570
+ unlinkSync5(this.cacheFilePath(sessionId, promptSession));
13489
13571
  } catch {
13490
13572
  }
13491
13573
  }
@@ -13497,9 +13579,9 @@ var init_policy_cache = __esm({
13497
13579
  const dir = join18(cacheDir(), safeSessionId);
13498
13580
  try {
13499
13581
  if (existsSync17(dir)) {
13500
- for (const f of readdirSync5(dir)) {
13582
+ for (const f of readdirSync6(dir)) {
13501
13583
  try {
13502
- unlinkSync4(join18(dir, f));
13584
+ unlinkSync5(join18(dir, f));
13503
13585
  } catch {
13504
13586
  }
13505
13587
  }
@@ -13512,10 +13594,10 @@ var init_policy_cache = __esm({
13512
13594
  this.memory.clear();
13513
13595
  try {
13514
13596
  if (existsSync17(cacheDir())) {
13515
- for (const sessionDir of readdirSync5(cacheDir())) {
13597
+ for (const sessionDir of readdirSync6(cacheDir())) {
13516
13598
  const dir = join18(cacheDir(), sessionDir);
13517
13599
  try {
13518
- for (const f of readdirSync5(dir)) unlinkSync4(join18(dir, f));
13600
+ for (const f of readdirSync6(dir)) unlinkSync5(join18(dir, f));
13519
13601
  rmdirSync(dir);
13520
13602
  } catch {
13521
13603
  }
@@ -14878,7 +14960,7 @@ var init_routes = __esm({
14878
14960
  // src/console/router.ts
14879
14961
  import { Hono } from "hono";
14880
14962
  import { timingSafeEqual as timingSafeEqual2 } from "crypto";
14881
- import { readFileSync as readFileSync20, existsSync as existsSync20, unlinkSync as unlinkSync5 } from "fs";
14963
+ import { readFileSync as readFileSync20, existsSync as existsSync20, unlinkSync as unlinkSync6 } from "fs";
14882
14964
  import { join as join22, dirname as dirname14 } from "path";
14883
14965
  import { fileURLToPath as fileURLToPath11 } from "url";
14884
14966
  function getConsoleToken() {
@@ -15223,7 +15305,7 @@ function createConsoleRouter() {
15223
15305
  const defaults = generateSourceDefaults();
15224
15306
  try {
15225
15307
  const p = getRuntimeConfigPath();
15226
- if (existsSync20(p)) unlinkSync5(p);
15308
+ if (existsSync20(p)) unlinkSync6(p);
15227
15309
  } catch {
15228
15310
  }
15229
15311
  return c.json(withL22View(defaults));
@@ -15586,7 +15668,7 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
15586
15668
  },
15587
15669
  serve: async () => {
15588
15670
  failFastOnAuthError();
15589
- const { mkdirSync: mkdirSync13, existsSync: existsSync23, unlinkSync: unlinkSync6 } = await import("fs");
15671
+ const { mkdirSync: mkdirSync13, existsSync: existsSync23, unlinkSync: unlinkSync7 } = await import("fs");
15590
15672
  const { dirname: dirname17 } = await import("path");
15591
15673
  const dir = dirname17(socketPath);
15592
15674
  if (dir) {
@@ -15613,7 +15695,7 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
15613
15695
  );
15614
15696
  } else {
15615
15697
  try {
15616
- unlinkSync6(socketPath);
15698
+ unlinkSync7(socketPath);
15617
15699
  console.log(`[AgentMoss] \u6E05\u7406\u6B8B\u7559 socket \u6587\u4EF6: ${socketPath}`);
15618
15700
  } catch {
15619
15701
  }
@@ -15725,7 +15807,7 @@ __export(rules_tools_exports, {
15725
15807
  rulesStatusText: () => rulesStatusText,
15726
15808
  templateSourceDir: () => templateSourceDir
15727
15809
  });
15728
- import { existsSync as existsSync22, mkdirSync as mkdirSync12, readdirSync as readdirSync7, readFileSync as readFileSync21, writeFileSync as writeFileSync9 } from "fs";
15810
+ import { existsSync as existsSync22, mkdirSync as mkdirSync12, readdirSync as readdirSync8, readFileSync as readFileSync21, writeFileSync as writeFileSync9 } from "fs";
15729
15811
  import { dirname as dirname16, join as join24 } from "path";
15730
15812
  import { fileURLToPath as fileURLToPath12 } from "url";
15731
15813
  function initRulesDir(targetDir, opts = {}) {
@@ -15772,7 +15854,7 @@ function templateFiles(dirs) {
15772
15854
  const out = [];
15773
15855
  for (const d of dirs) {
15774
15856
  try {
15775
- for (const name of readdirSync7(d)) {
15857
+ for (const name of readdirSync8(d)) {
15776
15858
  if (name.endsWith(".json.\u793A\u4F8B")) out.push(join24(d, name));
15777
15859
  }
15778
15860
  } catch {
@@ -2431,7 +2431,7 @@ function _dedup(rules, category) {
2431
2431
  const seen = /* @__PURE__ */ new Set();
2432
2432
  const out = [];
2433
2433
  for (const r of rules) {
2434
- const k = keys.map((x) => String(r?.[x] ?? "")).join("\0");
2434
+ const k = keys.map((x) => x === "os" ? String(r?.os || "linux") : String(r?.[x] ?? "")).join("\0");
2435
2435
  if (seen.has(k)) continue;
2436
2436
  seen.add(k);
2437
2437
  out.push(r);
@@ -3651,7 +3651,11 @@ var init_runtime_config = __esm({
3651
3651
  dangerous_commands: ["pattern"],
3652
3652
  password_modify_consent: ["pattern"],
3653
3653
  user_deletion_consent: ["pattern"],
3654
- sensitive_path_access: ["path"],
3654
+ // ⚠️ 必须含 os:跨平台同名 path(.ssh / .aws / .agentmoss)各有独立条目。
3655
+ // 去重发生在 _filterRulesByOs 之前,若键只有 path,linux 条目(种子中先出现)
3656
+ // 会把 windows 同名条目顶掉 → Windows 真实运行时的目录级删除拦截静默失效
3657
+ // (2026-11 修;与 Python `_DEDUP_KEYS` 同步)。
3658
+ sensitive_path_access: ["path", "os"],
3655
3659
  injection_detection: ["keyword", "lang"],
3656
3660
  intent_consistency: ["intent_keywords", "reason"]
3657
3661
  };
@@ -10708,6 +10712,11 @@ import * as fs5 from "fs";
10708
10712
  import * as os3 from "os";
10709
10713
  import * as path6 from "path";
10710
10714
  import { gzipSync } from "zlib";
10715
+ function resolveMaxNodes() {
10716
+ const n = Number.parseInt(process.env.AGENT_MOSS_CHAIN_MAX_NODES || "100000", 10);
10717
+ return Number.isFinite(n) && n > 0 ? n : 1e5;
10718
+ }
10719
+ var MAX_NODES = resolveMaxNodes();
10711
10720
 
10712
10721
  // src/engine/formal-engine/chain_state.ts
10713
10722
  init_esm_shims();
@@ -2109,6 +2109,13 @@
2109
2109
  "deny_mode": "deny_both",
2110
2110
  "scope": "home"
2111
2111
  },
2112
+ {
2113
+ "path": ".config/agentmoss/agent_moss_runtime.json",
2114
+ "risk_level": "critical",
2115
+ "desc": "AgentMoss 控制台运行时配置(规则启停/层级开关),写=agent 自行关闭安全检测",
2116
+ "deny_mode": "deny_write",
2117
+ "scope": "home"
2118
+ },
2112
2119
  {
2113
2120
  "path": ".config/agentmoss",
2114
2121
  "risk_level": "high",
@@ -2116,6 +2123,34 @@
2116
2123
  "deny_mode": "deny_write",
2117
2124
  "scope": "home"
2118
2125
  },
2126
+ {
2127
+ "path": ".config/agentgate/auth_token",
2128
+ "risk_level": "critical",
2129
+ "desc": "AgentGate 访问 token(拿到即可冒充 agent 调用网关、篡改审计/判定)",
2130
+ "deny_mode": "deny_both",
2131
+ "scope": "home"
2132
+ },
2133
+ {
2134
+ "path": ".config/agentgate/tool_name_map.json",
2135
+ "risk_level": "critical",
2136
+ "desc": "AgentGate 工具名映射文件(写=把危险工具伪装成只读/执行,绕过 L2 规则)",
2137
+ "deny_mode": "deny_write",
2138
+ "scope": "home"
2139
+ },
2140
+ {
2141
+ "path": ".config/agentgate",
2142
+ "risk_level": "high",
2143
+ "desc": "AgentGate 自身配置目录(token/工具名映射等),agent 可读但不可写(防篡改安全策略)",
2144
+ "deny_mode": "deny_write",
2145
+ "scope": "home"
2146
+ },
2147
+ {
2148
+ "path": ".local/share/agent_gate",
2149
+ "risk_level": "high",
2150
+ "desc": "AgentGate 数据库与归档目录(判定记录/审计),agent 可读但不可写(防篡改记录与归档)",
2151
+ "deny_mode": "deny_write",
2152
+ "scope": "home"
2153
+ },
2119
2154
  {
2120
2155
  "path": ".agentmoss/brain",
2121
2156
  "risk_level": "high",
@@ -2123,6 +2158,13 @@
2123
2158
  "deny_mode": "deny_write",
2124
2159
  "scope": "home"
2125
2160
  },
2161
+ {
2162
+ "path": ".agentmoss",
2163
+ "risk_level": "high",
2164
+ "desc": "AgentMoss 状态根目录(链状态库/instance 子目录/备份),agent 可读但不可写(防篡改判定链状态)",
2165
+ "deny_mode": "deny_write",
2166
+ "scope": "home"
2167
+ },
2126
2168
  {
2127
2169
  "path": ".pgpass",
2128
2170
  "risk_level": "critical",
@@ -2361,6 +2403,78 @@
2361
2403
  "scope": "home",
2362
2404
  "os": "windows"
2363
2405
  },
2406
+ {
2407
+ "path": ".config\\agentmoss\\auth_token",
2408
+ "risk_level": "critical",
2409
+ "desc": "AgentMoss 访问 token(Windows),拿到即可冒充调用分析接口/篡改判定",
2410
+ "deny_mode": "deny_both",
2411
+ "scope": "home",
2412
+ "os": "windows"
2413
+ },
2414
+ {
2415
+ "path": ".config\\agentmoss\\config.json",
2416
+ "risk_level": "critical",
2417
+ "desc": "AgentMoss 主配置(Windows),写=关闭安全检测",
2418
+ "deny_mode": "deny_both",
2419
+ "scope": "home",
2420
+ "os": "windows"
2421
+ },
2422
+ {
2423
+ "path": ".config\\agentmoss\\agent_moss_runtime.json",
2424
+ "risk_level": "critical",
2425
+ "desc": "AgentMoss 控制台运行时配置(Windows),写=agent 自行关闭安全检测",
2426
+ "deny_mode": "deny_write",
2427
+ "scope": "home",
2428
+ "os": "windows"
2429
+ },
2430
+ {
2431
+ "path": ".config\\agentmoss",
2432
+ "risk_level": "high",
2433
+ "desc": "AgentMoss 自身配置目录(Windows),agent 可读但不可写(防篡改关闭安全检测)",
2434
+ "deny_mode": "deny_write",
2435
+ "scope": "home",
2436
+ "os": "windows"
2437
+ },
2438
+ {
2439
+ "path": ".config\\agentgate\\auth_token",
2440
+ "risk_level": "critical",
2441
+ "desc": "AgentGate 访问 token(Windows),拿到即可冒充 agent 调用网关、篡改审计",
2442
+ "deny_mode": "deny_both",
2443
+ "scope": "home",
2444
+ "os": "windows"
2445
+ },
2446
+ {
2447
+ "path": ".config\\agentgate\\tool_name_map.json",
2448
+ "risk_level": "critical",
2449
+ "desc": "AgentGate 工具名映射文件(Windows),写=把危险工具伪装成只读,绕过 L2 规则",
2450
+ "deny_mode": "deny_write",
2451
+ "scope": "home",
2452
+ "os": "windows"
2453
+ },
2454
+ {
2455
+ "path": ".config\\agentgate",
2456
+ "risk_level": "high",
2457
+ "desc": "AgentGate 自身配置目录(Windows),agent 可读但不可写(防篡改安全策略)",
2458
+ "deny_mode": "deny_write",
2459
+ "scope": "home",
2460
+ "os": "windows"
2461
+ },
2462
+ {
2463
+ "path": ".local\\share\\agent_gate",
2464
+ "risk_level": "high",
2465
+ "desc": "AgentGate 数据库与归档目录(Windows),agent 可读但不可写(防篡改判定记录)",
2466
+ "deny_mode": "deny_write",
2467
+ "scope": "home",
2468
+ "os": "windows"
2469
+ },
2470
+ {
2471
+ "path": ".agentmoss",
2472
+ "risk_level": "high",
2473
+ "desc": "AgentMoss 状态根目录(Windows),agent 可读但不可写(防篡改判定链状态)",
2474
+ "deny_mode": "deny_write",
2475
+ "scope": "home",
2476
+ "os": "windows"
2477
+ },
2364
2478
  {
2365
2479
  "path": "C:\\ProgramData",
2366
2480
  "risk_level": "medium",
package/dist/server.js CHANGED
@@ -976,7 +976,7 @@ function logRoots() {
976
976
  }
977
977
  function reviewRoots() {
978
978
  const roots = [join4(homeDir(), ".local", "share", "agent_gate")];
979
- for (const name of ["AGENT_GATE_DB", "AGENT_GATE_JSON_PATH"]) {
979
+ for (const name of ["AGENT_GATE_DB", "AGENT_GATE_JSON_PATH", "AGENT_GATE_DATA_DIR"]) {
980
980
  const v = env(name);
981
981
  if (v) roots.push(existsSync3(v) && lstatSync(v).isDirectory() ? v : dirname2(v));
982
982
  }
@@ -2986,7 +2986,7 @@ function _dedup(rules, category) {
2986
2986
  const seen = /* @__PURE__ */ new Set();
2987
2987
  const out = [];
2988
2988
  for (const r of rules) {
2989
- const k = keys.map((x) => String(r?.[x] ?? "")).join("\0");
2989
+ const k = keys.map((x) => x === "os" ? String(r?.os || "linux") : String(r?.[x] ?? "")).join("\0");
2990
2990
  if (seen.has(k)) continue;
2991
2991
  seen.add(k);
2992
2992
  out.push(r);
@@ -4206,7 +4206,11 @@ var init_runtime_config = __esm({
4206
4206
  dangerous_commands: ["pattern"],
4207
4207
  password_modify_consent: ["pattern"],
4208
4208
  user_deletion_consent: ["pattern"],
4209
- sensitive_path_access: ["path"],
4209
+ // ⚠️ 必须含 os:跨平台同名 path(.ssh / .aws / .agentmoss)各有独立条目。
4210
+ // 去重发生在 _filterRulesByOs 之前,若键只有 path,linux 条目(种子中先出现)
4211
+ // 会把 windows 同名条目顶掉 → Windows 真实运行时的目录级删除拦截静默失效
4212
+ // (2026-11 修;与 Python `_DEDUP_KEYS` 同步)。
4213
+ sensitive_path_access: ["path", "os"],
4210
4214
  injection_detection: ["keyword", "lang"],
4211
4215
  intent_consistency: ["intent_keywords", "reason"]
4212
4216
  };
@@ -11409,6 +11413,74 @@ function lifecycleFor(detail, stage, isError) {
11409
11413
  if (detail.includes(">>") || detail.includes(">")) return "alive";
11410
11414
  return "";
11411
11415
  }
11416
+ function resolveMaxNodes() {
11417
+ const n = Number.parseInt(process.env.AGENT_MOSS_CHAIN_MAX_NODES || "100000", 10);
11418
+ return Number.isFinite(n) && n > 0 ? n : 1e5;
11419
+ }
11420
+ function resolveRetainNodes() {
11421
+ const raw = process.env.AGENT_MOSS_CHAIN_RETAIN_NODES || "";
11422
+ const def = Math.max(1, Math.floor(MAX_NODES / 2));
11423
+ const n = raw ? Number.parseInt(raw, 10) : def;
11424
+ const safe2 = Number.isFinite(n) && n > 0 ? n : def;
11425
+ return Math.max(1, Math.min(safe2, MAX_NODES - 1));
11426
+ }
11427
+ function envInt3(name, def) {
11428
+ const n = Number.parseInt(process.env[name] || "", 10);
11429
+ return Number.isFinite(n) && n >= 0 ? n : def;
11430
+ }
11431
+ function cleanupChainArchives(dir, maxBytes = envInt3("AGENT_MOSS_CHAIN_ARCHIVE_MAX_MB", 512) * 1024 * 1024, maxAgeDays = envInt3("AGENT_MOSS_CHAIN_ARCHIVE_MAX_AGE_DAYS", 0)) {
11432
+ let files = [];
11433
+ try {
11434
+ files = fs5.readdirSync(dir).filter((f) => f.startsWith("chain-nodes-") && f.endsWith(".jsonl.gz")).map((f) => path6.join(dir, f)).sort((a, b) => fs5.statSync(a).mtimeMs - fs5.statSync(b).mtimeMs);
11435
+ } catch {
11436
+ return { deleted: 0, files: 0, bytes: 0 };
11437
+ }
11438
+ let removed = 0;
11439
+ if (maxAgeDays > 0) {
11440
+ const cutoff = Date.now() - maxAgeDays * 864e5;
11441
+ files = files.filter((p) => {
11442
+ try {
11443
+ if (fs5.statSync(p).mtimeMs < cutoff) {
11444
+ fs5.unlinkSync(p);
11445
+ removed += 1;
11446
+ return false;
11447
+ }
11448
+ } catch {
11449
+ }
11450
+ return true;
11451
+ });
11452
+ }
11453
+ let total = files.reduce((n, p) => {
11454
+ try {
11455
+ return n + fs5.statSync(p).size;
11456
+ } catch {
11457
+ return n;
11458
+ }
11459
+ }, 0);
11460
+ if (maxBytes > 0) {
11461
+ for (const p of files) {
11462
+ if (total <= maxBytes) break;
11463
+ try {
11464
+ const size = fs5.statSync(p).size;
11465
+ fs5.unlinkSync(p);
11466
+ total -= size;
11467
+ removed += 1;
11468
+ } catch {
11469
+ }
11470
+ }
11471
+ }
11472
+ let remainFiles = 0;
11473
+ let remainBytes = 0;
11474
+ try {
11475
+ for (const f of fs5.readdirSync(dir)) {
11476
+ if (!f.startsWith("chain-nodes-") || !f.endsWith(".jsonl.gz")) continue;
11477
+ remainFiles += 1;
11478
+ remainBytes += fs5.statSync(path6.join(dir, f)).size;
11479
+ }
11480
+ } catch {
11481
+ }
11482
+ return { deleted: removed, files: remainFiles, bytes: remainBytes };
11483
+ }
11412
11484
  var DESTROY_RE, NEVER_ACTIVATED_RE, CRED_FILE_RE2, EXFIL_RE2, PORT_RE, BARE_PORT_RE, SOCK_RE, UNIT_RE, PORT_STAGE_RE, STAGES_PATTERNS, CHAINS, COMMON_DIR_RESOURCES2, MAX_NODES, StatefulChainTracker;
11413
11485
  var init_stateful = __esm({
11414
11486
  "src/engine/formal-engine/stateful.ts"() {
@@ -11440,7 +11512,7 @@ var init_stateful = __esm({
11440
11512
  "/data",
11441
11513
  "/storage"
11442
11514
  ]);
11443
- MAX_NODES = 1e5;
11515
+ MAX_NODES = resolveMaxNodes();
11444
11516
  StatefulChainTracker = class _StatefulChainTracker {
11445
11517
  /** 本地接收时刻(实时路径无 called_at 时的兜底时间戳,ISO)。 */
11446
11518
  static now() {
@@ -11547,7 +11619,7 @@ var init_stateful = __esm({
11547
11619
  * 派生缓存不是原始历史存储;归档失败抛异常,调用方不得丢状态。
11548
11620
  */
11549
11621
  rotate(archiveDir, retain) {
11550
- const keep = Math.max(1, Math.min(retain ?? Math.floor(MAX_NODES / 2), MAX_NODES - 1));
11622
+ const keep = Math.max(1, Math.min(retain ?? resolveRetainNodes(), MAX_NODES - 1));
11551
11623
  if (this.nodes_.length <= keep) return { archived: 0, file: "" };
11552
11624
  const old = this.nodes_.slice(0, this.nodes_.length - keep);
11553
11625
  const kept = this.nodes_.slice(-keep);
@@ -11557,6 +11629,16 @@ var init_stateful = __esm({
11557
11629
  const file = path6.join(dir, `chain-nodes-${safeAgent}-${(/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-")}.jsonl.gz`);
11558
11630
  const payload = old.map((n) => JSON.stringify(n)).join("\n") + "\n";
11559
11631
  fs5.writeFileSync(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
11632
+ try {
11633
+ const c = cleanupChainArchives(dir);
11634
+ if (c.deleted > 0) {
11635
+ console.error(
11636
+ `[AgentMoss] chain-archive \u6E05\u7406: \u5220\u9664 ${c.deleted} \u4E2A\u65E7\u5F52\u6863\uFF08\u5269\u4F59 ${c.files} \u4E2A / ${c.bytes} \u5B57\u8282\uFF09`
11637
+ );
11638
+ }
11639
+ } catch (e) {
11640
+ console.error(`[AgentMoss] chain-archive \u6E05\u7406\u5931\u8D25\uFF08\u7EE7\u7EED\uFF09: ${e?.message ?? e}`);
11641
+ }
11560
11642
  this.nodes_ = [];
11561
11643
  this.ptr = {};
11562
11644
  this.tainted.clear();
@@ -13122,7 +13204,7 @@ init_security_policy();
13122
13204
  init_paths();
13123
13205
  init_config();
13124
13206
  import { createHash as createHash5 } from "crypto";
13125
- import { existsSync as existsSync17, lstatSync as lstatSync2, readdirSync as readdirSync5, readFileSync as readFileSync17, rmdirSync, unlinkSync as unlinkSync4 } from "fs";
13207
+ import { existsSync as existsSync17, lstatSync as lstatSync2, readdirSync as readdirSync6, readFileSync as readFileSync17, rmdirSync, unlinkSync as unlinkSync5 } from "fs";
13126
13208
  import { tmpdir as tmpdir2 } from "os";
13127
13209
  import { join as join17 } from "path";
13128
13210
  var CACHE_DIR = join17(tmpdir2(), "agent_moss_policy_cache");
@@ -13204,7 +13286,7 @@ var PolicyCache = class {
13204
13286
  invalidate(sessionId, promptSession) {
13205
13287
  this.memory.delete(this.cacheKey(sessionId, promptSession));
13206
13288
  try {
13207
- unlinkSync4(this.cacheFilePath(sessionId, promptSession));
13289
+ unlinkSync5(this.cacheFilePath(sessionId, promptSession));
13208
13290
  } catch {
13209
13291
  }
13210
13292
  }
@@ -13216,9 +13298,9 @@ var PolicyCache = class {
13216
13298
  const dir = join17(cacheDir(), safeSessionId);
13217
13299
  try {
13218
13300
  if (existsSync17(dir)) {
13219
- for (const f of readdirSync5(dir)) {
13301
+ for (const f of readdirSync6(dir)) {
13220
13302
  try {
13221
- unlinkSync4(join17(dir, f));
13303
+ unlinkSync5(join17(dir, f));
13222
13304
  } catch {
13223
13305
  }
13224
13306
  }
@@ -13231,10 +13313,10 @@ var PolicyCache = class {
13231
13313
  this.memory.clear();
13232
13314
  try {
13233
13315
  if (existsSync17(cacheDir())) {
13234
- for (const sessionDir of readdirSync5(cacheDir())) {
13316
+ for (const sessionDir of readdirSync6(cacheDir())) {
13235
13317
  const dir = join17(cacheDir(), sessionDir);
13236
13318
  try {
13237
- for (const f of readdirSync5(dir)) unlinkSync4(join17(dir, f));
13319
+ for (const f of readdirSync6(dir)) unlinkSync5(join17(dir, f));
13238
13320
  rmdirSync(dir);
13239
13321
  } catch {
13240
13322
  }
@@ -14521,7 +14603,7 @@ init_asset_dir();
14521
14603
  init_rules_mode();
14522
14604
  import { Hono } from "hono";
14523
14605
  import { timingSafeEqual as timingSafeEqual2 } from "crypto";
14524
- import { readFileSync as readFileSync20, existsSync as existsSync20, unlinkSync as unlinkSync5 } from "fs";
14606
+ import { readFileSync as readFileSync20, existsSync as existsSync20, unlinkSync as unlinkSync6 } from "fs";
14525
14607
  import { join as join21, dirname as dirname14 } from "path";
14526
14608
  import { fileURLToPath as fileURLToPath11 } from "url";
14527
14609
  var __dirname10 = dirname14(fileURLToPath11(import.meta.url));
@@ -14869,7 +14951,7 @@ function createConsoleRouter() {
14869
14951
  const defaults = generateSourceDefaults();
14870
14952
  try {
14871
14953
  const p = getRuntimeConfigPath();
14872
- if (existsSync20(p)) unlinkSync5(p);
14954
+ if (existsSync20(p)) unlinkSync6(p);
14873
14955
  } catch {
14874
14956
  }
14875
14957
  return c.json(withL22View(defaults));
@@ -15249,7 +15331,7 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
15249
15331
  },
15250
15332
  serve: async () => {
15251
15333
  failFastOnAuthError();
15252
- const { mkdirSync: mkdirSync11, existsSync: existsSync22, unlinkSync: unlinkSync6 } = await import("fs");
15334
+ const { mkdirSync: mkdirSync11, existsSync: existsSync22, unlinkSync: unlinkSync7 } = await import("fs");
15253
15335
  const { dirname: dirname16 } = await import("path");
15254
15336
  const dir = dirname16(socketPath);
15255
15337
  if (dir) {
@@ -15276,7 +15358,7 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
15276
15358
  );
15277
15359
  } else {
15278
15360
  try {
15279
- unlinkSync6(socketPath);
15361
+ unlinkSync7(socketPath);
15280
15362
  console.log(`[AgentMoss] \u6E05\u7406\u6B8B\u7559 socket \u6587\u4EF6: ${socketPath}`);
15281
15363
  } catch {
15282
15364
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kenhkl/agent-moss",
3
- "version": "0.17.36",
3
+ "version": "0.17.38",
4
4
  "description": "Multi-layer security analysis engine for AI agents (TypeScript)",
5
5
  "type": "module",
6
6
  "main": "dist/server.js",