@kenhkl/agent-moss 0.17.42 → 0.17.43
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 +265 -191
- package/dist/engine/coordinator.js +147 -92
- package/dist/server.js +228 -154
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -3562,8 +3562,8 @@ function loadRuntimeConfig() {
|
|
|
3562
3562
|
}
|
|
3563
3563
|
try {
|
|
3564
3564
|
const st = statSync3(p);
|
|
3565
|
-
const
|
|
3566
|
-
if (runtimeConfigCache && runtimeConfigCache.key ===
|
|
3565
|
+
const cacheKey2 = `${p}|${st.mtimeMs}|${st.size}`;
|
|
3566
|
+
if (runtimeConfigCache && runtimeConfigCache.key === cacheKey2) {
|
|
3567
3567
|
return runtimeConfigCache.value;
|
|
3568
3568
|
}
|
|
3569
3569
|
const data = JSON.parse(readFileSync6(p, "utf-8"));
|
|
@@ -3588,7 +3588,7 @@ function loadRuntimeConfig() {
|
|
|
3588
3588
|
agent_rules: merged.agent_rules || {},
|
|
3589
3589
|
scenario_rules: merged.scenario_rules || {}
|
|
3590
3590
|
};
|
|
3591
|
-
runtimeConfigCache = { key:
|
|
3591
|
+
runtimeConfigCache = { key: cacheKey2, value: result };
|
|
3592
3592
|
return result;
|
|
3593
3593
|
} catch {
|
|
3594
3594
|
return emptyRuntimeConfig();
|
|
@@ -5228,15 +5228,66 @@ var init_config = __esm({
|
|
|
5228
5228
|
}
|
|
5229
5229
|
});
|
|
5230
5230
|
|
|
5231
|
-
// src/
|
|
5232
|
-
import { readFileSync as readFileSync7,
|
|
5233
|
-
import {
|
|
5231
|
+
// src/infra/edition-scope.ts
|
|
5232
|
+
import { existsSync as existsSync8, readFileSync as readFileSync7, statSync as statSync4 } from "fs";
|
|
5233
|
+
import { dirname as dirname6, join as join8, resolve as resolve5 } from "path";
|
|
5234
5234
|
import { fileURLToPath as fileURLToPath6 } from "url";
|
|
5235
|
+
function scopePath(moduleDir) {
|
|
5236
|
+
const root = getRulesRoot();
|
|
5237
|
+
if (root) return join8(root, "edition_scope.json");
|
|
5238
|
+
const prod = resolve5(moduleDir, "..", "edition_scope.json");
|
|
5239
|
+
if (existsSync8(prod)) return prod;
|
|
5240
|
+
const dev = resolve5(moduleDir, "../../..", "agent_moss", "edition_scope.json");
|
|
5241
|
+
if (existsSync8(dev)) return dev;
|
|
5242
|
+
return prod;
|
|
5243
|
+
}
|
|
5244
|
+
function loadScope(moduleDir) {
|
|
5245
|
+
const file = scopePath(moduleDir);
|
|
5246
|
+
try {
|
|
5247
|
+
const st = statSync4(file);
|
|
5248
|
+
const key = `${file}:${st.mtimeMs}:${st.size}`;
|
|
5249
|
+
if (key === cacheKey) return cacheData;
|
|
5250
|
+
const parsed = JSON.parse(readFileSync7(file, "utf-8"));
|
|
5251
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
5252
|
+
throw new Error("\u6839\u8282\u70B9\u4E0D\u662F\u5BF9\u8C61");
|
|
5253
|
+
}
|
|
5254
|
+
cacheKey = key;
|
|
5255
|
+
cacheData = parsed;
|
|
5256
|
+
return cacheData;
|
|
5257
|
+
} catch {
|
|
5258
|
+
cacheKey = "";
|
|
5259
|
+
cacheData = null;
|
|
5260
|
+
return null;
|
|
5261
|
+
}
|
|
5262
|
+
}
|
|
5263
|
+
function isAllowed(kind, id, moduleDir = MODULE_DIR) {
|
|
5264
|
+
const scope = loadScope(moduleDir);
|
|
5265
|
+
if (!scope) return true;
|
|
5266
|
+
const allowed = scope[kind];
|
|
5267
|
+
if (!Array.isArray(allowed)) return true;
|
|
5268
|
+
return allowed.includes(id);
|
|
5269
|
+
}
|
|
5270
|
+
var MODULE_DIR, cacheKey, cacheData;
|
|
5271
|
+
var init_edition_scope = __esm({
|
|
5272
|
+
"src/infra/edition-scope.ts"() {
|
|
5273
|
+
"use strict";
|
|
5274
|
+
init_esm_shims();
|
|
5275
|
+
init_paths();
|
|
5276
|
+
MODULE_DIR = dirname6(fileURLToPath6(import.meta.url));
|
|
5277
|
+
cacheKey = "";
|
|
5278
|
+
cacheData = null;
|
|
5279
|
+
}
|
|
5280
|
+
});
|
|
5281
|
+
|
|
5282
|
+
// src/agents/index.ts
|
|
5283
|
+
import { readFileSync as readFileSync8, existsSync as existsSync9 } from "fs";
|
|
5284
|
+
import { resolve as resolve6, dirname as dirname7, join as join9 } from "path";
|
|
5285
|
+
import { fileURLToPath as fileURLToPath7 } from "url";
|
|
5235
5286
|
function resolveAgentsDir() {
|
|
5236
5287
|
const root = getRulesRoot();
|
|
5237
|
-
if (root) return
|
|
5238
|
-
if (
|
|
5239
|
-
if (
|
|
5288
|
+
if (root) return join9(root, "agents");
|
|
5289
|
+
if (existsSync9(AGENTS_DIR_PROD)) return AGENTS_DIR_PROD;
|
|
5290
|
+
if (existsSync9(AGENTS_DIR_DEV_FALLBACK)) return AGENTS_DIR_DEV_FALLBACK;
|
|
5240
5291
|
return AGENTS_DIR_PROD;
|
|
5241
5292
|
}
|
|
5242
5293
|
function getAgentExtensions(agentId) {
|
|
@@ -5245,6 +5296,7 @@ function getAgentExtensions(agentId) {
|
|
|
5245
5296
|
console.warn(`[AgentMoss agents] \u975E\u6CD5 agent_id \u5DF2\u5FFD\u7565\uFF08\u542B\u8DEF\u5F84\u5B57\u7B26\u6216\u8D85\u957F\uFF09: ${agentId.slice(0, 120)}`);
|
|
5246
5297
|
return null;
|
|
5247
5298
|
}
|
|
5299
|
+
if (!isAllowed("agents", agentId)) return null;
|
|
5248
5300
|
if (cache.size >= CACHE_MAX) cache.clear();
|
|
5249
5301
|
const fp = getRuleSourceFingerprint();
|
|
5250
5302
|
const cached2 = cache.get(agentId);
|
|
@@ -5258,13 +5310,13 @@ function getAgentExtensions(agentId) {
|
|
|
5258
5310
|
}
|
|
5259
5311
|
if (ext === null) {
|
|
5260
5312
|
const agentsDir = resolveAgentsDir();
|
|
5261
|
-
const rulesFile =
|
|
5262
|
-
if (!
|
|
5313
|
+
const rulesFile = resolve6(agentsDir, agentId, "rules.json");
|
|
5314
|
+
if (!existsSync9(rulesFile)) {
|
|
5263
5315
|
cache.set(agentId, { fp, ext: null });
|
|
5264
5316
|
return null;
|
|
5265
5317
|
}
|
|
5266
5318
|
try {
|
|
5267
|
-
const raw =
|
|
5319
|
+
const raw = readFileSync8(rulesFile, "utf-8");
|
|
5268
5320
|
ext = JSON.parse(raw);
|
|
5269
5321
|
if (!ext.agent_id) {
|
|
5270
5322
|
console.warn(`[AgentMoss agents] rules.json \u7F3A\u5C11 agent_id \u5B57\u6BB5: ${rulesFile}`);
|
|
@@ -5305,25 +5357,26 @@ var init_agents = __esm({
|
|
|
5305
5357
|
"use strict";
|
|
5306
5358
|
init_esm_shims();
|
|
5307
5359
|
init_paths();
|
|
5360
|
+
init_edition_scope();
|
|
5308
5361
|
init_runtime_config();
|
|
5309
|
-
__filename4 =
|
|
5310
|
-
__dirname5 =
|
|
5311
|
-
AGENTS_DIR_PROD =
|
|
5312
|
-
AGENTS_DIR_DEV_FALLBACK =
|
|
5362
|
+
__filename4 = fileURLToPath7(import.meta.url);
|
|
5363
|
+
__dirname5 = dirname7(__filename4);
|
|
5364
|
+
AGENTS_DIR_PROD = resolve6(__dirname5, "agents");
|
|
5365
|
+
AGENTS_DIR_DEV_FALLBACK = resolve6(__dirname5, "../../../agent_moss/agents");
|
|
5313
5366
|
cache = /* @__PURE__ */ new Map();
|
|
5314
5367
|
CACHE_MAX = 512;
|
|
5315
5368
|
}
|
|
5316
5369
|
});
|
|
5317
5370
|
|
|
5318
5371
|
// src/scenarios/index.ts
|
|
5319
|
-
import { readFileSync as
|
|
5320
|
-
import { resolve as
|
|
5321
|
-
import { fileURLToPath as
|
|
5372
|
+
import { readFileSync as readFileSync9, existsSync as existsSync10 } from "fs";
|
|
5373
|
+
import { resolve as resolve7, dirname as dirname8, join as join10 } from "path";
|
|
5374
|
+
import { fileURLToPath as fileURLToPath8 } from "url";
|
|
5322
5375
|
function resolveScenariosDir() {
|
|
5323
5376
|
const root = getRulesRoot();
|
|
5324
|
-
if (root) return
|
|
5325
|
-
if (
|
|
5326
|
-
if (
|
|
5377
|
+
if (root) return join10(root, "scenarios");
|
|
5378
|
+
if (existsSync10(SCENARIOS_DIR_PROD)) return SCENARIOS_DIR_PROD;
|
|
5379
|
+
if (existsSync10(SCENARIOS_DIR_DEV_FALLBACK)) return SCENARIOS_DIR_DEV_FALLBACK;
|
|
5327
5380
|
return SCENARIOS_DIR_PROD;
|
|
5328
5381
|
}
|
|
5329
5382
|
function getScenarioExtensions(scenarioId) {
|
|
@@ -5332,6 +5385,7 @@ function getScenarioExtensions(scenarioId) {
|
|
|
5332
5385
|
console.warn(`[AgentMoss scenarios] \u975E\u6CD5 scenario_id \u5DF2\u5FFD\u7565\uFF08\u542B\u8DEF\u5F84\u5B57\u7B26\u6216\u8D85\u957F\uFF09: ${scenarioId.slice(0, 120)}`);
|
|
5333
5386
|
return null;
|
|
5334
5387
|
}
|
|
5388
|
+
if (!isAllowed("scenarios", scenarioId)) return null;
|
|
5335
5389
|
if (cache2.size >= CACHE_MAX2) cache2.clear();
|
|
5336
5390
|
const fp = getRuleSourceFingerprint();
|
|
5337
5391
|
const cached2 = cache2.get(scenarioId);
|
|
@@ -5351,19 +5405,19 @@ function getScenarioExtensions(scenarioId) {
|
|
|
5351
5405
|
}
|
|
5352
5406
|
}
|
|
5353
5407
|
if (ext === null) {
|
|
5354
|
-
const builtinFile =
|
|
5408
|
+
const builtinFile = resolve7(resolveScenariosDir(), scenarioId, "rules.json");
|
|
5355
5409
|
const rulesFile = overrideHit ?? builtinFile;
|
|
5356
5410
|
if (overrideHit) {
|
|
5357
5411
|
console.warn(
|
|
5358
5412
|
`[AgentMoss] \u26A0\uFE0F \u573A\u666F ${scenarioId} \u89C4\u5219\u88AB\u8986\u76D6\u76EE\u5F55\u8986\u76D6: ${overrideHit}\uFF08\u5185\u7F6E\u9ED8\u8BA4 ${builtinFile} \u88AB\u5FFD\u7565\uFF09\u3002\u8BE5\u6587\u4EF6\u7531\u90E8\u7F72\u4FA7\u81EA\u884C\u7EF4\u62A4\uFF0C\u5185\u7F6E\u573A\u666F\u89C4\u5219\u66F4\u65B0\u4E0D\u4F1A\u53CD\u6620\u5230\u5B83\u4E0A\u9762\u3002`
|
|
5359
5413
|
);
|
|
5360
5414
|
}
|
|
5361
|
-
if (!
|
|
5415
|
+
if (!existsSync10(rulesFile)) {
|
|
5362
5416
|
cache2.set(scenarioId, { fp, ext: null });
|
|
5363
5417
|
return null;
|
|
5364
5418
|
}
|
|
5365
5419
|
try {
|
|
5366
|
-
const raw =
|
|
5420
|
+
const raw = readFileSync9(rulesFile, "utf-8");
|
|
5367
5421
|
const data = JSON.parse(raw);
|
|
5368
5422
|
const id = data.scenario_id || data.agent_id || "";
|
|
5369
5423
|
if (!id) {
|
|
@@ -5409,13 +5463,14 @@ var init_scenarios = __esm({
|
|
|
5409
5463
|
"use strict";
|
|
5410
5464
|
init_esm_shims();
|
|
5411
5465
|
init_paths();
|
|
5466
|
+
init_edition_scope();
|
|
5412
5467
|
init_agents();
|
|
5413
5468
|
init_runtime_config();
|
|
5414
5469
|
init_rules_loader();
|
|
5415
|
-
__filename5 =
|
|
5416
|
-
__dirname6 =
|
|
5417
|
-
SCENARIOS_DIR_PROD =
|
|
5418
|
-
SCENARIOS_DIR_DEV_FALLBACK =
|
|
5470
|
+
__filename5 = fileURLToPath8(import.meta.url);
|
|
5471
|
+
__dirname6 = dirname8(__filename5);
|
|
5472
|
+
SCENARIOS_DIR_PROD = resolve7(__dirname6, "scenarios");
|
|
5473
|
+
SCENARIOS_DIR_DEV_FALLBACK = resolve7(__dirname6, "../../../agent_moss/scenarios");
|
|
5419
5474
|
cache2 = /* @__PURE__ */ new Map();
|
|
5420
5475
|
CACHE_MAX2 = 512;
|
|
5421
5476
|
}
|
|
@@ -6866,9 +6921,9 @@ var init_heuristic = __esm({
|
|
|
6866
6921
|
});
|
|
6867
6922
|
|
|
6868
6923
|
// src/engine/template-loader.ts
|
|
6869
|
-
import { readFileSync as
|
|
6870
|
-
import { resolve as
|
|
6871
|
-
import { fileURLToPath as
|
|
6924
|
+
import { readFileSync as readFileSync10, existsSync as existsSync11 } from "fs";
|
|
6925
|
+
import { resolve as resolve8, dirname as dirname9 } from "path";
|
|
6926
|
+
import { fileURLToPath as fileURLToPath9 } from "url";
|
|
6872
6927
|
function loadTemplates() {
|
|
6873
6928
|
if (cachedTemplates) return cachedTemplates;
|
|
6874
6929
|
cachedTemplates = {};
|
|
@@ -6879,10 +6934,10 @@ function loadTemplates() {
|
|
|
6879
6934
|
return cachedTemplates;
|
|
6880
6935
|
}
|
|
6881
6936
|
for (const file of TEMPLATE_FILES) {
|
|
6882
|
-
const filePath =
|
|
6883
|
-
if (
|
|
6937
|
+
const filePath = resolve8(dir, file);
|
|
6938
|
+
if (existsSync11(filePath)) {
|
|
6884
6939
|
const key = file.replace(".txt", "").replace("_template", "");
|
|
6885
|
-
cachedTemplates[key] =
|
|
6940
|
+
cachedTemplates[key] = readFileSync10(filePath, "utf-8");
|
|
6886
6941
|
}
|
|
6887
6942
|
}
|
|
6888
6943
|
console.log(`[AgentMoss] Loaded ${Object.keys(cachedTemplates).length} templates from ${dir}`);
|
|
@@ -6894,16 +6949,16 @@ var init_template_loader = __esm({
|
|
|
6894
6949
|
"use strict";
|
|
6895
6950
|
init_esm_shims();
|
|
6896
6951
|
init_asset_dir();
|
|
6897
|
-
__filename6 =
|
|
6898
|
-
__dirname7 =
|
|
6952
|
+
__filename6 = fileURLToPath9(import.meta.url);
|
|
6953
|
+
__dirname7 = dirname9(__filename6);
|
|
6899
6954
|
TEMPLATE_FILES = ["prompt1_template.txt", "prompt2_template.txt", "security_judge_template.txt"];
|
|
6900
6955
|
cachedTemplates = null;
|
|
6901
6956
|
}
|
|
6902
6957
|
});
|
|
6903
6958
|
|
|
6904
6959
|
// src/engine/script-analyzer.ts
|
|
6905
|
-
import { readFileSync as
|
|
6906
|
-
import { resolve as
|
|
6960
|
+
import { readFileSync as readFileSync11 } from "fs";
|
|
6961
|
+
import { resolve as resolve9, join as join11 } from "path";
|
|
6907
6962
|
function emptyResult() {
|
|
6908
6963
|
return { scriptPath: "", scriptContent: "", matchedKeywords: [], lineCount: 0, isSuspicious: false, error: "", riskLevel: "", riskReason: "" };
|
|
6909
6964
|
}
|
|
@@ -6956,8 +7011,8 @@ function assessKeywordCombinationRisk(matchedKeywords, scriptContent) {
|
|
|
6956
7011
|
}
|
|
6957
7012
|
function readScriptContent(scriptPath, maxLines = 500) {
|
|
6958
7013
|
try {
|
|
6959
|
-
const expandedPath = scriptPath === "~" || scriptPath.startsWith("~/") ?
|
|
6960
|
-
const raw =
|
|
7014
|
+
const expandedPath = scriptPath === "~" || scriptPath.startsWith("~/") ? join11(process.env.HOME || "/home/user", scriptPath.slice(1)) : resolve9(scriptPath);
|
|
7015
|
+
const raw = readFileSync11(expandedPath, "utf-8");
|
|
6961
7016
|
const lines = raw.split("\n").slice(0, maxLines);
|
|
6962
7017
|
return { content: lines.join("\n"), lineCount: lines.length, error: "" };
|
|
6963
7018
|
} catch (e) {
|
|
@@ -8235,7 +8290,7 @@ function evalIfCondition(cond, args) {
|
|
|
8235
8290
|
const op = m[2];
|
|
8236
8291
|
let left = m[1].trim();
|
|
8237
8292
|
let right = m[3].trim();
|
|
8238
|
-
const
|
|
8293
|
+
const resolve13 = (tok) => {
|
|
8239
8294
|
const rm = tok.match(/^\$(\d+)$/);
|
|
8240
8295
|
if (rm) {
|
|
8241
8296
|
const idx = parseInt(rm[1], 10);
|
|
@@ -8243,8 +8298,8 @@ function evalIfCondition(cond, args) {
|
|
|
8243
8298
|
}
|
|
8244
8299
|
return tok.startsWith("$") ? null : tok;
|
|
8245
8300
|
};
|
|
8246
|
-
const lv =
|
|
8247
|
-
const rv =
|
|
8301
|
+
const lv = resolve13(left);
|
|
8302
|
+
const rv = resolve13(right);
|
|
8248
8303
|
if (lv === null || rv === null) return null;
|
|
8249
8304
|
if (op === "=~") {
|
|
8250
8305
|
let re = rv;
|
|
@@ -8586,8 +8641,8 @@ var init_tracker = __esm({
|
|
|
8586
8641
|
});
|
|
8587
8642
|
|
|
8588
8643
|
// src/engine/script-content-analyzer.ts
|
|
8589
|
-
import { statSync as
|
|
8590
|
-
import { resolve as
|
|
8644
|
+
import { statSync as statSync6, existsSync as existsSync13 } from "fs";
|
|
8645
|
+
import { resolve as resolve11, join as join13, isAbsolute as isAbsolute4 } from "path";
|
|
8591
8646
|
import { homedir as homedir5 } from "os";
|
|
8592
8647
|
function emptyChain() {
|
|
8593
8648
|
return {
|
|
@@ -8699,8 +8754,8 @@ function extractInlineScriptPath(actionDetail) {
|
|
|
8699
8754
|
if (looksLikeScriptPath(tok)) {
|
|
8700
8755
|
try {
|
|
8701
8756
|
const resolved = resolvePath2(tok.replace(/^['"]|['"]$/g, ""));
|
|
8702
|
-
if (
|
|
8703
|
-
const stat =
|
|
8757
|
+
if (existsSync13(resolved)) {
|
|
8758
|
+
const stat = statSync6(resolved);
|
|
8704
8759
|
if (stat.isFile()) return resolved;
|
|
8705
8760
|
}
|
|
8706
8761
|
} catch {
|
|
@@ -8713,8 +8768,8 @@ function extractInlineScriptPath(actionDetail) {
|
|
|
8713
8768
|
const p = m[1];
|
|
8714
8769
|
try {
|
|
8715
8770
|
const resolved = resolvePath2(p);
|
|
8716
|
-
if (
|
|
8717
|
-
const stat =
|
|
8771
|
+
if (existsSync13(resolved)) {
|
|
8772
|
+
const stat = statSync6(resolved);
|
|
8718
8773
|
if (stat.isFile()) return resolved;
|
|
8719
8774
|
}
|
|
8720
8775
|
} catch {
|
|
@@ -8729,8 +8784,8 @@ function extractInlineScriptPath(actionDetail) {
|
|
|
8729
8784
|
let resolved;
|
|
8730
8785
|
try {
|
|
8731
8786
|
resolved = resolvePath2(p);
|
|
8732
|
-
if (
|
|
8733
|
-
const stat =
|
|
8787
|
+
if (existsSync13(resolved)) {
|
|
8788
|
+
const stat = statSync6(resolved);
|
|
8734
8789
|
if (stat.isFile()) return resolved;
|
|
8735
8790
|
}
|
|
8736
8791
|
} catch {
|
|
@@ -8770,9 +8825,9 @@ function shlexSplit(s) {
|
|
|
8770
8825
|
function resolvePath2(p) {
|
|
8771
8826
|
let expanded = p;
|
|
8772
8827
|
if (expanded.startsWith("~/") || expanded === "~") {
|
|
8773
|
-
expanded =
|
|
8828
|
+
expanded = join13(homedir5(), expanded.slice(1));
|
|
8774
8829
|
}
|
|
8775
|
-
return
|
|
8830
|
+
return resolve11(expanded);
|
|
8776
8831
|
}
|
|
8777
8832
|
function scanExtensionPatterns(ext, content) {
|
|
8778
8833
|
const pats = ext?.extra_command_patterns;
|
|
@@ -8859,7 +8914,7 @@ function analyzeScriptContent2(input, ext, stopOnCritical = true) {
|
|
|
8859
8914
|
isInline = true;
|
|
8860
8915
|
}
|
|
8861
8916
|
if (resolvedPath && !isAbsolute4(resolvedPath) && !resolvedPath.startsWith("~/")) {
|
|
8862
|
-
resolvedPath =
|
|
8917
|
+
resolvedPath = join13(input.cwd || process.cwd(), resolvedPath);
|
|
8863
8918
|
}
|
|
8864
8919
|
const l1Adapter = (content) => {
|
|
8865
8920
|
const extHit = scanExtensionPatterns(ext, content);
|
|
@@ -9460,17 +9515,17 @@ __export(token_stats_exports, {
|
|
|
9460
9515
|
resetTokenStats: () => resetTokenStats,
|
|
9461
9516
|
resetTokenStatsCacheForTests: () => resetTokenStatsCacheForTests
|
|
9462
9517
|
});
|
|
9463
|
-
import { readFileSync as
|
|
9518
|
+
import { readFileSync as readFileSync14, writeFileSync as writeFileSync2, existsSync as existsSync14, mkdirSync as mkdirSync3, renameSync, unlinkSync as unlinkSync2, statSync as statSync7 } from "fs";
|
|
9464
9519
|
function getStatsPath() {
|
|
9465
9520
|
const envPath = process.env[ENV_STATS_PATH];
|
|
9466
9521
|
if (envPath) return envPath;
|
|
9467
9522
|
return getTokenStatsPath();
|
|
9468
9523
|
}
|
|
9469
9524
|
function loadAllRecords(statsPath) {
|
|
9470
|
-
if (!
|
|
9525
|
+
if (!existsSync14(statsPath)) return [];
|
|
9471
9526
|
let sig = "";
|
|
9472
9527
|
try {
|
|
9473
|
-
const st =
|
|
9528
|
+
const st = statSync7(statsPath);
|
|
9474
9529
|
sig = `${st.mtimeMs}:${st.size}`;
|
|
9475
9530
|
} catch {
|
|
9476
9531
|
return [];
|
|
@@ -9479,7 +9534,7 @@ function loadAllRecords(statsPath) {
|
|
|
9479
9534
|
return _recordsCache.records.slice();
|
|
9480
9535
|
}
|
|
9481
9536
|
try {
|
|
9482
|
-
const data = JSON.parse(
|
|
9537
|
+
const data = JSON.parse(readFileSync14(statsPath, "utf-8"));
|
|
9483
9538
|
let records;
|
|
9484
9539
|
if (Array.isArray(data)) records = data;
|
|
9485
9540
|
else if (data && Array.isArray(data.records)) records = data.records;
|
|
@@ -9493,7 +9548,7 @@ function loadAllRecords(statsPath) {
|
|
|
9493
9548
|
function saveRecords(statsPath, records) {
|
|
9494
9549
|
try {
|
|
9495
9550
|
const dir = statsPath.substring(0, statsPath.lastIndexOf("/"));
|
|
9496
|
-
if (dir && !
|
|
9551
|
+
if (dir && !existsSync14(dir)) mkdirSync3(dir, { recursive: true });
|
|
9497
9552
|
const tmp = `${statsPath}.${process.pid}.${Date.now()}.tmp`;
|
|
9498
9553
|
try {
|
|
9499
9554
|
writeFileSync2(tmp, JSON.stringify(records, null, 2), "utf-8");
|
|
@@ -9505,7 +9560,7 @@ function saveRecords(statsPath, records) {
|
|
|
9505
9560
|
}
|
|
9506
9561
|
}
|
|
9507
9562
|
try {
|
|
9508
|
-
const st =
|
|
9563
|
+
const st = statSync7(statsPath);
|
|
9509
9564
|
_recordsCache = {
|
|
9510
9565
|
path: statsPath,
|
|
9511
9566
|
sig: `${st.mtimeMs}:${st.size}`,
|
|
@@ -9642,7 +9697,7 @@ function getTokenTrend(days = 0, startDate, endDate) {
|
|
|
9642
9697
|
}
|
|
9643
9698
|
function resetTokenStats() {
|
|
9644
9699
|
const statsPath = getStatsPath();
|
|
9645
|
-
if (
|
|
9700
|
+
if (existsSync14(statsPath)) {
|
|
9646
9701
|
try {
|
|
9647
9702
|
unlinkSync2(statsPath);
|
|
9648
9703
|
} catch {
|
|
@@ -9742,8 +9797,8 @@ async function runLLMAnalysis(input, previous) {
|
|
|
9742
9797
|
}
|
|
9743
9798
|
const config = perRequest?.apiKey ? { ...globalConfig, ...perRequest } : globalConfig;
|
|
9744
9799
|
const rawDetail = input.aNext.actionDetail.replace(/^terminal-\w+:\s*command=([^,]+).*/, "$1").trim();
|
|
9745
|
-
const
|
|
9746
|
-
const cached2 = llmCache.get(
|
|
9800
|
+
const cacheKey2 = `${input.aNext.actionType}:${createHash3("sha256").update(rawDetail || input.aNext.actionDetail).digest("hex")}`;
|
|
9801
|
+
const cached2 = llmCache.get(cacheKey2);
|
|
9747
9802
|
if (cached2) {
|
|
9748
9803
|
console.log("[AgentMoss] LLM cache hit");
|
|
9749
9804
|
return cached2;
|
|
@@ -9832,7 +9887,7 @@ async function runLLMAnalysis(input, previous) {
|
|
|
9832
9887
|
const firstKey = llmCache.keys().next().value;
|
|
9833
9888
|
if (firstKey) llmCache.delete(firstKey);
|
|
9834
9889
|
}
|
|
9835
|
-
llmCache.set(
|
|
9890
|
+
llmCache.set(cacheKey2, result);
|
|
9836
9891
|
return result;
|
|
9837
9892
|
}
|
|
9838
9893
|
function logPrompt(prompt, aNext, logPath) {
|
|
@@ -10180,7 +10235,7 @@ async function analyzeWithLLM(prompt, config) {
|
|
|
10180
10235
|
)}`;
|
|
10181
10236
|
if (attempt < maxRetries) {
|
|
10182
10237
|
console.error(`[AgentMoss] LLM call failed (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${retryCfg.retryInterval}s: ${detail}`);
|
|
10183
|
-
await new Promise((
|
|
10238
|
+
await new Promise((resolve13) => setTimeout(resolve13, retryCfg.retryInterval * 1e3));
|
|
10184
10239
|
} else {
|
|
10185
10240
|
clearTimeout(totalTimeoutTimer);
|
|
10186
10241
|
console.error(`[AgentMoss] LLM call failed after all retries: ${detail}`);
|
|
@@ -10226,8 +10281,8 @@ var init_llm_analyzer = __esm({
|
|
|
10226
10281
|
});
|
|
10227
10282
|
|
|
10228
10283
|
// src/brain/rule-store.ts
|
|
10229
|
-
import { existsSync as
|
|
10230
|
-
import { join as
|
|
10284
|
+
import { existsSync as existsSync15, readFileSync as readFileSync15, writeFileSync as writeFileSync3, mkdirSync as mkdirSync4 } from "fs";
|
|
10285
|
+
import { join as join14 } from "path";
|
|
10231
10286
|
import { homedir as homedir6 } from "os";
|
|
10232
10287
|
function getRulesPath() {
|
|
10233
10288
|
const envPath = process.env[ENV_RULES_PATH];
|
|
@@ -10246,7 +10301,7 @@ var init_rule_store = __esm({
|
|
|
10246
10301
|
"src/brain/rule-store.ts"() {
|
|
10247
10302
|
"use strict";
|
|
10248
10303
|
init_esm_shims();
|
|
10249
|
-
DEFAULT_RULES_PATH =
|
|
10304
|
+
DEFAULT_RULES_PATH = join14(homedir6(), ".agentmoss", "brain", "rules.json");
|
|
10250
10305
|
ENV_RULES_PATH = "AGENT_MOSS_BRAIN_RULES_PATH";
|
|
10251
10306
|
STATUS_DRAFT = "draft";
|
|
10252
10307
|
STATUS_APPROVED = "approved";
|
|
@@ -10263,11 +10318,11 @@ var init_rule_store = __esm({
|
|
|
10263
10318
|
this.data = this.load();
|
|
10264
10319
|
}
|
|
10265
10320
|
load() {
|
|
10266
|
-
if (!
|
|
10321
|
+
if (!existsSync15(this.path)) {
|
|
10267
10322
|
return { version: 1, rules: [], pattern_candidates: [] };
|
|
10268
10323
|
}
|
|
10269
10324
|
try {
|
|
10270
|
-
const raw = JSON.parse(
|
|
10325
|
+
const raw = JSON.parse(readFileSync15(this.path, "utf-8"));
|
|
10271
10326
|
return {
|
|
10272
10327
|
version: raw.version || 1,
|
|
10273
10328
|
rules: raw.rules || [],
|
|
@@ -10861,9 +10916,9 @@ var init_reviewer = __esm({
|
|
|
10861
10916
|
});
|
|
10862
10917
|
|
|
10863
10918
|
// src/brain/skill-manager.ts
|
|
10864
|
-
import { existsSync as
|
|
10919
|
+
import { existsSync as existsSync16, mkdirSync as mkdirSync5, readFileSync as readFileSync16, renameSync as renameSync2, writeFileSync as writeFileSync4 } from "fs";
|
|
10865
10920
|
import { homedir as homedir7 } from "os";
|
|
10866
|
-
import { join as
|
|
10921
|
+
import { join as join15 } from "path";
|
|
10867
10922
|
import { randomUUID } from "crypto";
|
|
10868
10923
|
function getMetaPath(override) {
|
|
10869
10924
|
return override || process.env[ENV_SKILL_META_PATH] || DEFAULT_SKILL_META_PATH;
|
|
@@ -10873,14 +10928,14 @@ var init_skill_manager = __esm({
|
|
|
10873
10928
|
"src/brain/skill-manager.ts"() {
|
|
10874
10929
|
"use strict";
|
|
10875
10930
|
init_esm_shims();
|
|
10876
|
-
DEFAULT_SKILL_META_PATH =
|
|
10931
|
+
DEFAULT_SKILL_META_PATH = join15(homedir7(), ".agentmoss", "brain", "skill_meta.json");
|
|
10877
10932
|
ENV_SKILL_META_PATH = "AGENT_MOSS_BRAIN_SKILL_META_PATH";
|
|
10878
10933
|
BrainSkillManager = class {
|
|
10879
10934
|
constructor(skillsDir, metaPath) {
|
|
10880
10935
|
this.skillsDir = skillsDir;
|
|
10881
10936
|
this.metaPath = getMetaPath(metaPath);
|
|
10882
10937
|
try {
|
|
10883
|
-
mkdirSync5(
|
|
10938
|
+
mkdirSync5(join15(this.metaPath, ".."), { recursive: true });
|
|
10884
10939
|
} catch {
|
|
10885
10940
|
}
|
|
10886
10941
|
this.load();
|
|
@@ -10890,9 +10945,9 @@ var init_skill_manager = __esm({
|
|
|
10890
10945
|
skills = [];
|
|
10891
10946
|
metaPath;
|
|
10892
10947
|
load() {
|
|
10893
|
-
if (!
|
|
10948
|
+
if (!existsSync16(this.metaPath)) return;
|
|
10894
10949
|
try {
|
|
10895
|
-
const data = JSON.parse(
|
|
10950
|
+
const data = JSON.parse(readFileSync16(this.metaPath, "utf-8"));
|
|
10896
10951
|
this.skills = data.skills || [];
|
|
10897
10952
|
} catch {
|
|
10898
10953
|
this.skills = [];
|
|
@@ -10915,7 +10970,7 @@ var init_skill_manager = __esm({
|
|
|
10915
10970
|
let changed = false;
|
|
10916
10971
|
for (const sk of this.skills) {
|
|
10917
10972
|
if (sk.status !== "active") continue;
|
|
10918
|
-
if (!
|
|
10973
|
+
if (!existsSync16(join15(this.skillsDir, `${sk.name}.md`))) {
|
|
10919
10974
|
sk.status = "stale";
|
|
10920
10975
|
changed = true;
|
|
10921
10976
|
}
|
|
@@ -10926,9 +10981,9 @@ var init_skill_manager = __esm({
|
|
|
10926
10981
|
addSkill(skill) {
|
|
10927
10982
|
const id = skill.id || randomUUID();
|
|
10928
10983
|
const name = skill.name || `brain_${id.slice(0, 8)}`;
|
|
10929
|
-
if (!
|
|
10930
|
-
const mdPath =
|
|
10931
|
-
if (
|
|
10984
|
+
if (!existsSync16(this.skillsDir)) return "";
|
|
10985
|
+
const mdPath = join15(this.skillsDir, `${name}.md`);
|
|
10986
|
+
if (existsSync16(mdPath)) return "";
|
|
10932
10987
|
try {
|
|
10933
10988
|
writeFileSync4(mdPath, skill.content, "utf-8");
|
|
10934
10989
|
} catch {
|
|
@@ -10977,12 +11032,12 @@ var init_skill_manager = __esm({
|
|
|
10977
11032
|
const out = [];
|
|
10978
11033
|
for (const sk of this.skills) {
|
|
10979
11034
|
if (sk.origin_node !== "local" || sk.status !== "active") continue;
|
|
10980
|
-
const mdPath =
|
|
10981
|
-
if (!
|
|
11035
|
+
const mdPath = join15(this.skillsDir, `${sk.name}.md`);
|
|
11036
|
+
if (!existsSync16(mdPath)) continue;
|
|
10982
11037
|
out.push({
|
|
10983
11038
|
name: sk.name,
|
|
10984
11039
|
category: sk.category || "",
|
|
10985
|
-
content:
|
|
11040
|
+
content: readFileSync16(mdPath, "utf-8"),
|
|
10986
11041
|
keywords: sk.keywords || []
|
|
10987
11042
|
});
|
|
10988
11043
|
}
|
|
@@ -11094,10 +11149,10 @@ __export(contributor_exports, {
|
|
|
11094
11149
|
});
|
|
11095
11150
|
import { mkdirSync as mkdirSync6, mkdtempSync, rmSync, writeFileSync as writeFileSync5 } from "fs";
|
|
11096
11151
|
import { tmpdir as tmpdir2 } from "os";
|
|
11097
|
-
import { join as
|
|
11152
|
+
import { join as join16 } from "path";
|
|
11098
11153
|
import { spawnSync } from "child_process";
|
|
11099
|
-
import { dirname as
|
|
11100
|
-
import { fileURLToPath as
|
|
11154
|
+
import { dirname as dirname12 } from "path";
|
|
11155
|
+
import { fileURLToPath as fileURLToPath10 } from "url";
|
|
11101
11156
|
function collectRules(store) {
|
|
11102
11157
|
return store.getActiveRules().filter((r) => EXPORT_RULE_SOURCES.includes(r.source)).map((r) => ({
|
|
11103
11158
|
layer: r.layer,
|
|
@@ -11115,7 +11170,7 @@ function collectSkills(skillMgr) {
|
|
|
11115
11170
|
}
|
|
11116
11171
|
function writeExportFiles(outDir, rules, skills) {
|
|
11117
11172
|
const written = [];
|
|
11118
|
-
const rulesDir2 =
|
|
11173
|
+
const rulesDir2 = join16(outDir, "rules");
|
|
11119
11174
|
mkdirSync6(rulesDir2, { recursive: true });
|
|
11120
11175
|
const byLayer = /* @__PURE__ */ new Map();
|
|
11121
11176
|
for (const r of rules) {
|
|
@@ -11123,24 +11178,24 @@ function writeExportFiles(outDir, rules, skills) {
|
|
|
11123
11178
|
byLayer.get(r.layer).push(r);
|
|
11124
11179
|
}
|
|
11125
11180
|
for (const layer of [...byLayer.keys()].sort((a, b) => a - b)) {
|
|
11126
|
-
const path9 =
|
|
11181
|
+
const path9 = join16(rulesDir2, `l${layer}_brain_rules.json`);
|
|
11127
11182
|
writeFileSync5(path9, JSON.stringify(byLayer.get(layer), null, 2), "utf-8");
|
|
11128
11183
|
written.push(path9);
|
|
11129
11184
|
}
|
|
11130
11185
|
if (rules.length === 0) {
|
|
11131
|
-
const path9 =
|
|
11186
|
+
const path9 = join16(rulesDir2, "empty.txt");
|
|
11132
11187
|
writeFileSync5(path9, "(no brain rules to contribute)", "utf-8");
|
|
11133
11188
|
written.push(path9);
|
|
11134
11189
|
}
|
|
11135
|
-
const skillsDir =
|
|
11190
|
+
const skillsDir = join16(outDir, "skills");
|
|
11136
11191
|
mkdirSync6(skillsDir, { recursive: true });
|
|
11137
11192
|
for (const sk of skills) {
|
|
11138
|
-
const path9 =
|
|
11193
|
+
const path9 = join16(skillsDir, `${sk.name}.md`);
|
|
11139
11194
|
writeFileSync5(path9, sk.content || "", "utf-8");
|
|
11140
11195
|
written.push(path9);
|
|
11141
11196
|
}
|
|
11142
11197
|
if (skills.length === 0) {
|
|
11143
|
-
const path9 =
|
|
11198
|
+
const path9 = join16(skillsDir, "empty.txt");
|
|
11144
11199
|
writeFileSync5(path9, "(no brain skills to contribute)", "utf-8");
|
|
11145
11200
|
written.push(path9);
|
|
11146
11201
|
}
|
|
@@ -11270,7 +11325,7 @@ async function contribute(opts = {}) {
|
|
|
11270
11325
|
console.log(`[Brain] push \u76EE\u6807: ${pushTarget}`);
|
|
11271
11326
|
console.log(`[Brain] MR \u6307\u5411: ${DEFAULT_TARGET_REPO}`);
|
|
11272
11327
|
const store = new BrainRuleStore();
|
|
11273
|
-
const skillsDir = resolveAssetDir(__dirname8, "skills") ??
|
|
11328
|
+
const skillsDir = resolveAssetDir(__dirname8, "skills") ?? join16(__dirname8, "..", "..", "agent_moss", "skills");
|
|
11274
11329
|
const skillMgr = new BrainSkillManager(skillsDir);
|
|
11275
11330
|
let rules;
|
|
11276
11331
|
let skills;
|
|
@@ -11285,9 +11340,9 @@ async function contribute(opts = {}) {
|
|
|
11285
11340
|
return 0;
|
|
11286
11341
|
}
|
|
11287
11342
|
console.log(`[Brain] \u5BFC\u51FA: ${rules.length} \u6761\u89C4\u5219, ${skills.length} \u4E2A Skill`);
|
|
11288
|
-
const workdir = mkdtempSync(
|
|
11343
|
+
const workdir = mkdtempSync(join16(tmpdir2(), "agentmoss-contrib-"));
|
|
11289
11344
|
try {
|
|
11290
|
-
writeExportFiles(
|
|
11345
|
+
writeExportFiles(join16(workdir, "agent_moss", "brain_contrib"), rules, skills);
|
|
11291
11346
|
console.log(`[Brain] \u63A2\u6D4B ${pushTarget} \u8BBF\u95EE\u6743\u9650 ...`);
|
|
11292
11347
|
if (!probeAccess(pushTarget)) {
|
|
11293
11348
|
const username = getCurrentUsername();
|
|
@@ -11337,7 +11392,7 @@ var init_contributor = __esm({
|
|
|
11337
11392
|
init_rule_store();
|
|
11338
11393
|
init_skill_manager();
|
|
11339
11394
|
init_asset_dir();
|
|
11340
|
-
__dirname8 =
|
|
11395
|
+
__dirname8 = dirname12(fileURLToPath10(import.meta.url));
|
|
11341
11396
|
DEFAULT_TARGET_REPO = "ssh://git@atomgit.com/kenhkl/AgentMoss.git";
|
|
11342
11397
|
CONTACT_EMAIL = "kenhkl11@hotmail.com";
|
|
11343
11398
|
TOKEN_ENV = "AGENT_MOSS_CONTRIBUTE_TOKEN";
|
|
@@ -14370,9 +14425,9 @@ var init_types = __esm({
|
|
|
14370
14425
|
|
|
14371
14426
|
// src/infra/policy-cache.ts
|
|
14372
14427
|
import { createHash as createHash6 } from "crypto";
|
|
14373
|
-
import { existsSync as
|
|
14428
|
+
import { existsSync as existsSync18, lstatSync as lstatSync2, readdirSync as readdirSync6, readFileSync as readFileSync18, rmdirSync, unlinkSync as unlinkSync5 } from "fs";
|
|
14374
14429
|
import { tmpdir as tmpdir3 } from "os";
|
|
14375
|
-
import { join as
|
|
14430
|
+
import { join as join19 } from "path";
|
|
14376
14431
|
function cacheDir() {
|
|
14377
14432
|
return cacheDirOverride ?? CACHE_DIR;
|
|
14378
14433
|
}
|
|
@@ -14380,7 +14435,7 @@ function safeCacheDir() {
|
|
|
14380
14435
|
const dir = cacheDir();
|
|
14381
14436
|
if (ensurePrivateDir(dir)) return dir;
|
|
14382
14437
|
try {
|
|
14383
|
-
const fallback =
|
|
14438
|
+
const fallback = join19(getDataRoot(), "policy_cache");
|
|
14384
14439
|
if (ensurePrivateDir(fallback)) {
|
|
14385
14440
|
console.warn(`[AgentMoss] \u7F13\u5B58\u76EE\u5F55 ${dir} \u4E0D\u5B89\u5168\uFF08\u88AB\u5176\u4ED6\u7528\u6237\u62E5\u6709\u6216\u4E3A\u7B26\u53F7\u94FE\u63A5\uFF09\uFF0C\u5DF2\u56DE\u9000\u5230 ${fallback}`);
|
|
14386
14441
|
return fallback;
|
|
@@ -14401,7 +14456,7 @@ var init_policy_cache = __esm({
|
|
|
14401
14456
|
init_security_policy();
|
|
14402
14457
|
init_paths();
|
|
14403
14458
|
init_config();
|
|
14404
|
-
CACHE_DIR =
|
|
14459
|
+
CACHE_DIR = join19(tmpdir3(), "agent_moss_policy_cache");
|
|
14405
14460
|
cacheDirOverride = null;
|
|
14406
14461
|
PolicyCache = class {
|
|
14407
14462
|
// Map 的插入序即 LRU 序(get 时重新 set 到末尾)
|
|
@@ -14416,7 +14471,7 @@ var init_policy_cache = __esm({
|
|
|
14416
14471
|
cacheFilePath(sessionId, promptSession) {
|
|
14417
14472
|
const promptHash = createHash6("md5").update(promptSession).digest("hex").slice(0, 12);
|
|
14418
14473
|
const safeSessionId = sessionId.replace(/\//g, "_").replace(/\\/g, "_").replace(/\.\./g, "__");
|
|
14419
|
-
return
|
|
14474
|
+
return join19(safeCacheDir(), safeSessionId, `${promptHash}.toml`);
|
|
14420
14475
|
}
|
|
14421
14476
|
/** 取缓存:内存 → 文件;未命中返回 null。 */
|
|
14422
14477
|
get(sessionId, promptSession) {
|
|
@@ -14429,9 +14484,9 @@ var init_policy_cache = __esm({
|
|
|
14429
14484
|
return hit;
|
|
14430
14485
|
}
|
|
14431
14486
|
const file = this.cacheFilePath(sessionId, promptSession);
|
|
14432
|
-
if (
|
|
14487
|
+
if (existsSync18(file) && !lstatSync2(file).isSymbolicLink()) {
|
|
14433
14488
|
try {
|
|
14434
|
-
const policy =
|
|
14489
|
+
const policy = readFileSync18(file, "utf-8");
|
|
14435
14490
|
this.remember(key, policy);
|
|
14436
14491
|
return policy;
|
|
14437
14492
|
} catch {
|
|
@@ -14447,7 +14502,7 @@ var init_policy_cache = __esm({
|
|
|
14447
14502
|
try {
|
|
14448
14503
|
const dir = safeCacheDir();
|
|
14449
14504
|
const sub = sessionId.replace(/\//g, "_").replace(/\\/g, "_").replace(/\.\./g, "__");
|
|
14450
|
-
if (!ensurePrivateDir(
|
|
14505
|
+
if (!ensurePrivateDir(join19(dir, sub))) return;
|
|
14451
14506
|
writePrivateText(file, policy);
|
|
14452
14507
|
} catch {
|
|
14453
14508
|
}
|
|
@@ -14473,12 +14528,12 @@ var init_policy_cache = __esm({
|
|
|
14473
14528
|
if (k.startsWith(`${sessionId}::`)) this.memory.delete(k);
|
|
14474
14529
|
}
|
|
14475
14530
|
const safeSessionId = sessionId.replace(/\//g, "_").replace(/\\/g, "_").replace(/\.\./g, "__");
|
|
14476
|
-
const dir =
|
|
14531
|
+
const dir = join19(cacheDir(), safeSessionId);
|
|
14477
14532
|
try {
|
|
14478
|
-
if (
|
|
14533
|
+
if (existsSync18(dir)) {
|
|
14479
14534
|
for (const f of readdirSync6(dir)) {
|
|
14480
14535
|
try {
|
|
14481
|
-
unlinkSync5(
|
|
14536
|
+
unlinkSync5(join19(dir, f));
|
|
14482
14537
|
} catch {
|
|
14483
14538
|
}
|
|
14484
14539
|
}
|
|
@@ -14490,11 +14545,11 @@ var init_policy_cache = __esm({
|
|
|
14490
14545
|
clear() {
|
|
14491
14546
|
this.memory.clear();
|
|
14492
14547
|
try {
|
|
14493
|
-
if (
|
|
14548
|
+
if (existsSync18(cacheDir())) {
|
|
14494
14549
|
for (const sessionDir of readdirSync6(cacheDir())) {
|
|
14495
|
-
const dir =
|
|
14550
|
+
const dir = join19(cacheDir(), sessionDir);
|
|
14496
14551
|
try {
|
|
14497
|
-
for (const f of readdirSync6(dir)) unlinkSync5(
|
|
14552
|
+
for (const f of readdirSync6(dir)) unlinkSync5(join19(dir, f));
|
|
14498
14553
|
rmdirSync(dir);
|
|
14499
14554
|
} catch {
|
|
14500
14555
|
}
|
|
@@ -14590,9 +14645,9 @@ __export(analyzer_exports, {
|
|
|
14590
14645
|
savePolicyToFile: () => savePolicyToFile,
|
|
14591
14646
|
validateToml: () => validateToml
|
|
14592
14647
|
});
|
|
14593
|
-
import { existsSync as
|
|
14648
|
+
import { existsSync as existsSync19, readFileSync as readFileSync19 } from "fs";
|
|
14594
14649
|
import { tmpdir as tmpdir4 } from "os";
|
|
14595
|
-
import { join as
|
|
14650
|
+
import { join as join20, resolve as resolve12 } from "path";
|
|
14596
14651
|
import { parse as parseToml } from "smol-toml";
|
|
14597
14652
|
function _setPolicyOutputDirForTest(dir) {
|
|
14598
14653
|
policyOutputDirOverride = dir;
|
|
@@ -14609,7 +14664,7 @@ function savePolicyToFile(sessionId, policyToml) {
|
|
|
14609
14664
|
return null;
|
|
14610
14665
|
}
|
|
14611
14666
|
const safeName = sessionId.replace(/\//g, "_").replace(/\\/g, "_").replace(/\.\./g, "__");
|
|
14612
|
-
const filepath =
|
|
14667
|
+
const filepath = join20(outDir, `${safeName}.toml`);
|
|
14613
14668
|
writePrivateText(filepath, policyToml);
|
|
14614
14669
|
return filepath;
|
|
14615
14670
|
} catch (err) {
|
|
@@ -14647,12 +14702,12 @@ function validateToml(tomlStr) {
|
|
|
14647
14702
|
function loadPolicyMappingDoc() {
|
|
14648
14703
|
const dir = resolveAssetDir(__dirname, "templates");
|
|
14649
14704
|
if (dir) {
|
|
14650
|
-
const p =
|
|
14651
|
-
if (
|
|
14705
|
+
const p = resolve12(dir, "policy_mapping.md");
|
|
14706
|
+
if (existsSync19(p)) return readFileSync19(p, "utf-8");
|
|
14652
14707
|
}
|
|
14653
14708
|
for (const cand of assetDirCandidates(__dirname, "templates")) {
|
|
14654
|
-
const p =
|
|
14655
|
-
if (
|
|
14709
|
+
const p = resolve12(cand, "policy_mapping.md");
|
|
14710
|
+
if (existsSync19(p)) return readFileSync19(p, "utf-8");
|
|
14656
14711
|
}
|
|
14657
14712
|
return "";
|
|
14658
14713
|
}
|
|
@@ -14832,7 +14887,7 @@ var init_analyzer = __esm({
|
|
|
14832
14887
|
init_asset_dir();
|
|
14833
14888
|
init_token_stats();
|
|
14834
14889
|
init_logging();
|
|
14835
|
-
POLICY_OUTPUT_DIR =
|
|
14890
|
+
POLICY_OUTPUT_DIR = join20(tmpdir4(), "agent_moss");
|
|
14836
14891
|
policyOutputDirOverride = null;
|
|
14837
14892
|
DEFAULT_READONLY_POLICY = `landlock_optional = false
|
|
14838
14893
|
mount_isolation_fallback = false
|
|
@@ -15320,8 +15375,8 @@ var init_runtime_stats = __esm({
|
|
|
15320
15375
|
|
|
15321
15376
|
// src/routes.ts
|
|
15322
15377
|
import { randomBytes as randomBytes2 } from "crypto";
|
|
15323
|
-
import { join as
|
|
15324
|
-
import { fileURLToPath as
|
|
15378
|
+
import { join as join22, dirname as dirname14 } from "path";
|
|
15379
|
+
import { fileURLToPath as fileURLToPath11 } from "url";
|
|
15325
15380
|
async function healthRoute(c) {
|
|
15326
15381
|
const metrics = {};
|
|
15327
15382
|
try {
|
|
@@ -15878,7 +15933,7 @@ function getBrainRuleStore() {
|
|
|
15878
15933
|
return new BrainRuleStore();
|
|
15879
15934
|
}
|
|
15880
15935
|
function getBrainSkillManager() {
|
|
15881
|
-
const dir = resolveAssetDir(__dirname9, "skills") ??
|
|
15936
|
+
const dir = resolveAssetDir(__dirname9, "skills") ?? join22(__dirname9, "..", "..", "agent_moss", "skills");
|
|
15882
15937
|
return new BrainSkillManager(dir);
|
|
15883
15938
|
}
|
|
15884
15939
|
async function brainRulesRoute(c) {
|
|
@@ -15991,7 +16046,7 @@ var init_routes = __esm({
|
|
|
15991
16046
|
init_loop_monitor();
|
|
15992
16047
|
init_runtime_stats();
|
|
15993
16048
|
init_metrics();
|
|
15994
|
-
__dirname9 =
|
|
16049
|
+
__dirname9 = dirname14(fileURLToPath11(import.meta.url));
|
|
15995
16050
|
RISK_PRIORITY = { low: 0, medium: 1, high: 2, critical: 3 };
|
|
15996
16051
|
}
|
|
15997
16052
|
});
|
|
@@ -15999,9 +16054,9 @@ var init_routes = __esm({
|
|
|
15999
16054
|
// src/console/router.ts
|
|
16000
16055
|
import { Hono } from "hono";
|
|
16001
16056
|
import { timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
16002
|
-
import { readFileSync as
|
|
16003
|
-
import { join as
|
|
16004
|
-
import { fileURLToPath as
|
|
16057
|
+
import { readFileSync as readFileSync21, existsSync as existsSync21, unlinkSync as unlinkSync6 } from "fs";
|
|
16058
|
+
import { join as join23, dirname as dirname15 } from "path";
|
|
16059
|
+
import { fileURLToPath as fileURLToPath12 } from "url";
|
|
16005
16060
|
function getConsoleToken() {
|
|
16006
16061
|
return process.env[CONSOLE_TOKEN_ENV] || "";
|
|
16007
16062
|
}
|
|
@@ -16027,6 +16082,16 @@ function authMiddleware() {
|
|
|
16027
16082
|
function displayRuntimeConfig() {
|
|
16028
16083
|
return isRuntimeConfigDisabled() ? generateSourceDefaults() : loadRuntimeConfig();
|
|
16029
16084
|
}
|
|
16085
|
+
function scopedAgentRules(rc) {
|
|
16086
|
+
return Object.fromEntries(
|
|
16087
|
+
Object.entries(getAgentRules(rc)).filter(([id]) => isAllowed("agents", id))
|
|
16088
|
+
);
|
|
16089
|
+
}
|
|
16090
|
+
function scopedScenarioRules(rc) {
|
|
16091
|
+
return Object.fromEntries(
|
|
16092
|
+
Object.entries(getScenarioRules(rc)).filter(([id]) => isAllowed("scenarios", id))
|
|
16093
|
+
);
|
|
16094
|
+
}
|
|
16030
16095
|
function builtinOnlyReadOnlyMiddleware() {
|
|
16031
16096
|
return async (c, next) => {
|
|
16032
16097
|
if (isRuntimeConfigDisabled() && !["GET", "HEAD", "OPTIONS"].includes(c.req.method)) {
|
|
@@ -16042,9 +16107,9 @@ function createConsoleRouter() {
|
|
|
16042
16107
|
console2.use("/api/*", authMiddleware());
|
|
16043
16108
|
console2.use("/api/*", builtinOnlyReadOnlyMiddleware());
|
|
16044
16109
|
const serveIndex = (c) => {
|
|
16045
|
-
const indexFile =
|
|
16046
|
-
if (
|
|
16047
|
-
return c.html(
|
|
16110
|
+
const indexFile = join23(STATIC_DIR, "index.html");
|
|
16111
|
+
if (existsSync21(indexFile)) {
|
|
16112
|
+
return c.html(readFileSync21(indexFile, "utf-8"));
|
|
16048
16113
|
}
|
|
16049
16114
|
return c.html("<h1>AgentMoss Policy Console</h1><p>Frontend not built. Use /console/api/* endpoints.</p>");
|
|
16050
16115
|
};
|
|
@@ -16239,22 +16304,22 @@ function createConsoleRouter() {
|
|
|
16239
16304
|
if (!/^[A-Za-z0-9_-]+$/.test(skillId)) {
|
|
16240
16305
|
return c.json({ detail: "\u975E\u6CD5 skill id" }, 400);
|
|
16241
16306
|
}
|
|
16242
|
-
const userPath =
|
|
16307
|
+
const userPath = join23(dirname15(getRuntimeConfigPath()), "agent_moss_skills", `${skillId}.md`);
|
|
16243
16308
|
const builtinDir = resolveAssetDir(__dirname10, "skills");
|
|
16244
16309
|
const candidates = [
|
|
16245
16310
|
...isRuntimeConfigDisabled() ? [] : [userPath],
|
|
16246
|
-
builtinDir ?
|
|
16311
|
+
builtinDir ? join23(builtinDir, `${skillId}.md`) : ""
|
|
16247
16312
|
].filter(Boolean);
|
|
16248
16313
|
for (const p of candidates) {
|
|
16249
|
-
if (
|
|
16250
|
-
return c.json({ skill_id: skillId, path: p, content:
|
|
16314
|
+
if (existsSync21(p)) {
|
|
16315
|
+
return c.json({ skill_id: skillId, path: p, content: readFileSync21(p, "utf-8") });
|
|
16251
16316
|
}
|
|
16252
16317
|
}
|
|
16253
16318
|
return c.json({ detail: `skill ${skillId} \u4E0D\u5B58\u5728` }, 404);
|
|
16254
16319
|
});
|
|
16255
16320
|
console2.get("/api/agents", (c) => {
|
|
16256
16321
|
const rc = displayRuntimeConfig();
|
|
16257
|
-
return c.json({ agents:
|
|
16322
|
+
return c.json({ agents: scopedAgentRules(rc) });
|
|
16258
16323
|
});
|
|
16259
16324
|
console2.put("/api/agents/rules/enabled", async (c) => {
|
|
16260
16325
|
const b = await c.req.json();
|
|
@@ -16263,8 +16328,9 @@ function createConsoleRouter() {
|
|
|
16263
16328
|
if (b.pattern) fp.pattern = b.pattern;
|
|
16264
16329
|
if (b.keyword) fp.keyword = b.keyword;
|
|
16265
16330
|
if (b.path) fp.path = b.path;
|
|
16331
|
+
if (!isAllowed("agents", b.agent_id)) return c.json({ detail: `agent ${b.agent_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
16266
16332
|
const rc = updateAgentRuleEnabled(b.agent_id, b.category, fp, b.enabled);
|
|
16267
|
-
return c.json({ agents:
|
|
16333
|
+
return c.json({ agents: scopedAgentRules(rc) });
|
|
16268
16334
|
});
|
|
16269
16335
|
console2.post("/api/agents/rules", async (c) => {
|
|
16270
16336
|
const b = await c.req.json();
|
|
@@ -16272,8 +16338,9 @@ function createConsoleRouter() {
|
|
|
16272
16338
|
for (const [k, v] of Object.entries(b)) {
|
|
16273
16339
|
if (!["agent_id", "category"].includes(k) && v !== void 0 && v !== null) rule[k] = v;
|
|
16274
16340
|
}
|
|
16341
|
+
if (!isAllowed("agents", b.agent_id)) return c.json({ detail: `agent ${b.agent_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
16275
16342
|
const rc = addAgentRule(b.agent_id, b.category, rule);
|
|
16276
|
-
return c.json({ agents:
|
|
16343
|
+
return c.json({ agents: scopedAgentRules(rc) });
|
|
16277
16344
|
});
|
|
16278
16345
|
console2.delete("/api/agents/rules", async (c) => {
|
|
16279
16346
|
const b = await c.req.json();
|
|
@@ -16282,18 +16349,20 @@ function createConsoleRouter() {
|
|
|
16282
16349
|
if (b.pattern) fp.pattern = b.pattern;
|
|
16283
16350
|
if (b.keyword) fp.keyword = b.keyword;
|
|
16284
16351
|
if (b.path) fp.path = b.path;
|
|
16352
|
+
if (!isAllowed("agents", b.agent_id)) return c.json({ detail: `agent ${b.agent_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
16285
16353
|
const rc = deleteAgentRule(b.agent_id, b.category, fp);
|
|
16286
|
-
return c.json({ agents:
|
|
16354
|
+
return c.json({ agents: scopedAgentRules(rc) });
|
|
16287
16355
|
});
|
|
16288
16356
|
console2.put("/api/agents/categories/enabled", async (c) => {
|
|
16289
16357
|
const b = await c.req.json();
|
|
16358
|
+
if (!isAllowed("agents", b.agent_id)) return c.json({ detail: `agent ${b.agent_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
16290
16359
|
const rc = updateAgentCategoryEnabled(b.agent_id, b.category, b.enabled);
|
|
16291
|
-
return c.json({ agents:
|
|
16360
|
+
return c.json({ agents: scopedAgentRules(rc) });
|
|
16292
16361
|
});
|
|
16293
16362
|
const enabledScenarioIds = () => (process.env.AGENT_MOSS_SCENARIO || "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
16294
16363
|
console2.get("/api/scenarios", (c) => {
|
|
16295
|
-
const all =
|
|
16296
|
-
const enabled = enabledScenarioIds();
|
|
16364
|
+
const all = scopedScenarioRules(displayRuntimeConfig());
|
|
16365
|
+
const enabled = enabledScenarioIds().filter((sid) => isAllowed("scenarios", sid));
|
|
16297
16366
|
if (!enabled.length) return c.json({ scenarios: {}, enabled: [] });
|
|
16298
16367
|
const shown = {};
|
|
16299
16368
|
for (const sid of enabled) if (all[sid]) shown[sid] = all[sid];
|
|
@@ -16306,8 +16375,9 @@ function createConsoleRouter() {
|
|
|
16306
16375
|
if (b.pattern) fp.pattern = b.pattern;
|
|
16307
16376
|
if (b.keyword) fp.keyword = b.keyword;
|
|
16308
16377
|
if (b.path) fp.path = b.path;
|
|
16378
|
+
if (!isAllowed("scenarios", b.scenario_id)) return c.json({ detail: `scenario ${b.scenario_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
16309
16379
|
const rc = updateScenarioRuleEnabled(b.scenario_id, b.category, fp, b.enabled);
|
|
16310
|
-
return c.json({ scenarios:
|
|
16380
|
+
return c.json({ scenarios: scopedScenarioRules(rc), enabled: enabledScenarioIds().filter((sid) => isAllowed("scenarios", sid)) });
|
|
16311
16381
|
});
|
|
16312
16382
|
console2.post("/api/scenarios/rules", async (c) => {
|
|
16313
16383
|
const b = await c.req.json();
|
|
@@ -16315,8 +16385,9 @@ function createConsoleRouter() {
|
|
|
16315
16385
|
for (const [k, v] of Object.entries(b)) {
|
|
16316
16386
|
if (!["scenario_id", "category"].includes(k) && v !== void 0 && v !== null) rule[k] = v;
|
|
16317
16387
|
}
|
|
16388
|
+
if (!isAllowed("scenarios", b.scenario_id)) return c.json({ detail: `scenario ${b.scenario_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
16318
16389
|
const rc = addScenarioRule(b.scenario_id, b.category, rule);
|
|
16319
|
-
return c.json({ scenarios:
|
|
16390
|
+
return c.json({ scenarios: scopedScenarioRules(rc), enabled: enabledScenarioIds().filter((sid) => isAllowed("scenarios", sid)) });
|
|
16320
16391
|
});
|
|
16321
16392
|
console2.delete("/api/scenarios/rules", async (c) => {
|
|
16322
16393
|
const b = await c.req.json();
|
|
@@ -16325,13 +16396,15 @@ function createConsoleRouter() {
|
|
|
16325
16396
|
if (b.pattern) fp.pattern = b.pattern;
|
|
16326
16397
|
if (b.keyword) fp.keyword = b.keyword;
|
|
16327
16398
|
if (b.path) fp.path = b.path;
|
|
16399
|
+
if (!isAllowed("scenarios", b.scenario_id)) return c.json({ detail: `scenario ${b.scenario_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
16328
16400
|
const rc = deleteScenarioRule(b.scenario_id, b.category, fp);
|
|
16329
|
-
return c.json({ scenarios:
|
|
16401
|
+
return c.json({ scenarios: scopedScenarioRules(rc), enabled: enabledScenarioIds().filter((sid) => isAllowed("scenarios", sid)) });
|
|
16330
16402
|
});
|
|
16331
16403
|
console2.put("/api/scenarios/categories/enabled", async (c) => {
|
|
16332
16404
|
const b = await c.req.json();
|
|
16405
|
+
if (!isAllowed("scenarios", b.scenario_id)) return c.json({ detail: `scenario ${b.scenario_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
16333
16406
|
const rc = updateScenarioCategoryEnabled(b.scenario_id, b.category, b.enabled);
|
|
16334
|
-
return c.json({ scenarios:
|
|
16407
|
+
return c.json({ scenarios: scopedScenarioRules(rc), enabled: enabledScenarioIds().filter((sid) => isAllowed("scenarios", sid)) });
|
|
16335
16408
|
});
|
|
16336
16409
|
console2.get("/api/config", (c) => {
|
|
16337
16410
|
const rc = displayRuntimeConfig();
|
|
@@ -16344,7 +16417,7 @@ function createConsoleRouter() {
|
|
|
16344
16417
|
const defaults = generateSourceDefaults();
|
|
16345
16418
|
try {
|
|
16346
16419
|
const p = getRuntimeConfigPath();
|
|
16347
|
-
if (
|
|
16420
|
+
if (existsSync21(p)) unlinkSync6(p);
|
|
16348
16421
|
} catch {
|
|
16349
16422
|
}
|
|
16350
16423
|
return c.json(withL22View(defaults));
|
|
@@ -16380,8 +16453,9 @@ var init_router = __esm({
|
|
|
16380
16453
|
init_rules_loader();
|
|
16381
16454
|
init_asset_dir();
|
|
16382
16455
|
init_rules_mode();
|
|
16383
|
-
|
|
16384
|
-
|
|
16456
|
+
init_edition_scope();
|
|
16457
|
+
__dirname10 = dirname15(fileURLToPath12(import.meta.url));
|
|
16458
|
+
STATIC_DIR = join23(__dirname10, "static");
|
|
16385
16459
|
CONSOLE_TOKEN_ENV = "AGENT_MOSS_CONSOLE_TOKEN";
|
|
16386
16460
|
}
|
|
16387
16461
|
});
|
|
@@ -16409,8 +16483,8 @@ import { Hono as Hono2 } from "hono";
|
|
|
16409
16483
|
import { cors } from "hono/cors";
|
|
16410
16484
|
import { logger } from "hono/logger";
|
|
16411
16485
|
import net from "net";
|
|
16412
|
-
import { existsSync as
|
|
16413
|
-
import { dirname as
|
|
16486
|
+
import { existsSync as existsSync22, writeFileSync as writeFileSync8, renameSync as renameSync3, mkdirSync as mkdirSync11 } from "fs";
|
|
16487
|
+
import { dirname as dirname16 } from "path";
|
|
16414
16488
|
function logDeploymentSelfCheck() {
|
|
16415
16489
|
try {
|
|
16416
16490
|
const mode = getRulesMode();
|
|
@@ -16437,7 +16511,7 @@ function logDeploymentSelfCheck() {
|
|
|
16437
16511
|
console.log("\u5C42\u7EA7\u5F00\u5173: " + layerKeys.map((k) => `${k}=${isLayerEnabled(k) ? "on" : "OFF"}`).join(" "));
|
|
16438
16512
|
if (!builtinOnly) {
|
|
16439
16513
|
try {
|
|
16440
|
-
const dirs = rulesDirs().filter((d) =>
|
|
16514
|
+
const dirs = rulesDirs().filter((d) => existsSync22(d));
|
|
16441
16515
|
if (dirs.length > 0) {
|
|
16442
16516
|
console.log(`\u89C4\u5219\u76EE\u5F55: ${dirs.join(", ")}\uFF08\u53EA\u8BFB\u5176\u4E2D ${OVERRIDE_FILENAME} / ${EXTRA_FILENAME}\uFF09`);
|
|
16443
16517
|
}
|
|
@@ -16635,12 +16709,12 @@ function createApp() {
|
|
|
16635
16709
|
}
|
|
16636
16710
|
async function findFreePort(startPort, host = "127.0.0.1", scan = 100) {
|
|
16637
16711
|
for (let port = startPort; port < startPort + scan; port++) {
|
|
16638
|
-
const free = await new Promise((
|
|
16712
|
+
const free = await new Promise((resolve13) => {
|
|
16639
16713
|
const server = net.createServer();
|
|
16640
16714
|
server.listen(port, host, () => {
|
|
16641
|
-
server.close(() =>
|
|
16715
|
+
server.close(() => resolve13(true));
|
|
16642
16716
|
});
|
|
16643
|
-
server.on("error", () =>
|
|
16717
|
+
server.on("error", () => resolve13(false));
|
|
16644
16718
|
});
|
|
16645
16719
|
if (free) {
|
|
16646
16720
|
if (port !== startPort) {
|
|
@@ -16679,7 +16753,7 @@ async function warmUpEngine() {
|
|
|
16679
16753
|
function writePortFile(port) {
|
|
16680
16754
|
try {
|
|
16681
16755
|
const path9 = getPortFilePath();
|
|
16682
|
-
mkdirSync11(
|
|
16756
|
+
mkdirSync11(dirname16(path9), { recursive: true });
|
|
16683
16757
|
const payload = JSON.stringify({
|
|
16684
16758
|
port,
|
|
16685
16759
|
pid: process.pid,
|
|
@@ -16725,29 +16799,29 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
|
|
|
16725
16799
|
socketPath,
|
|
16726
16800
|
close: async () => {
|
|
16727
16801
|
if (!server) return;
|
|
16728
|
-
await new Promise((
|
|
16729
|
-
server.close(() =>
|
|
16802
|
+
await new Promise((resolve13) => {
|
|
16803
|
+
server.close(() => resolve13());
|
|
16730
16804
|
});
|
|
16731
16805
|
server = null;
|
|
16732
16806
|
},
|
|
16733
16807
|
serve: async () => {
|
|
16734
16808
|
failFastOnAuthError();
|
|
16735
|
-
const { mkdirSync: mkdirSync13, existsSync:
|
|
16736
|
-
const { dirname:
|
|
16737
|
-
const dir =
|
|
16809
|
+
const { mkdirSync: mkdirSync13, existsSync: existsSync24, unlinkSync: unlinkSync7 } = await import("fs");
|
|
16810
|
+
const { dirname: dirname18 } = await import("path");
|
|
16811
|
+
const dir = dirname18(socketPath);
|
|
16738
16812
|
if (dir) {
|
|
16739
16813
|
try {
|
|
16740
16814
|
mkdirSync13(dir, { recursive: true });
|
|
16741
16815
|
} catch {
|
|
16742
16816
|
}
|
|
16743
16817
|
}
|
|
16744
|
-
if (
|
|
16818
|
+
if (existsSync24(socketPath)) {
|
|
16745
16819
|
const { connect } = await import("net");
|
|
16746
|
-
const inUse = await new Promise((
|
|
16820
|
+
const inUse = await new Promise((resolve13) => {
|
|
16747
16821
|
const probe = connect(socketPath);
|
|
16748
16822
|
const done = (v) => {
|
|
16749
16823
|
probe.destroy();
|
|
16750
|
-
|
|
16824
|
+
resolve13(v);
|
|
16751
16825
|
};
|
|
16752
16826
|
probe.once("connect", () => done(true));
|
|
16753
16827
|
probe.once("error", () => done(false));
|
|
@@ -16775,9 +16849,9 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
|
|
|
16775
16849
|
server.keepAliveTimeout = keepAliveSec() * 1e3;
|
|
16776
16850
|
} catch {
|
|
16777
16851
|
}
|
|
16778
|
-
await new Promise((
|
|
16852
|
+
await new Promise((resolve13, reject) => {
|
|
16779
16853
|
server.once("error", reject);
|
|
16780
|
-
server.listen(socketPath, () =>
|
|
16854
|
+
server.listen(socketPath, () => resolve13());
|
|
16781
16855
|
});
|
|
16782
16856
|
try {
|
|
16783
16857
|
const { chmodSync: chmodSync2 } = await import("fs");
|
|
@@ -16871,26 +16945,26 @@ __export(rules_tools_exports, {
|
|
|
16871
16945
|
rulesStatusText: () => rulesStatusText,
|
|
16872
16946
|
templateSourceDir: () => templateSourceDir
|
|
16873
16947
|
});
|
|
16874
|
-
import { existsSync as
|
|
16875
|
-
import { dirname as
|
|
16876
|
-
import { fileURLToPath as
|
|
16948
|
+
import { existsSync as existsSync23, mkdirSync as mkdirSync12, readdirSync as readdirSync8, readFileSync as readFileSync22, writeFileSync as writeFileSync9 } from "fs";
|
|
16949
|
+
import { dirname as dirname17, join as join25 } from "path";
|
|
16950
|
+
import { fileURLToPath as fileURLToPath13 } from "url";
|
|
16877
16951
|
function initRulesDir(targetDir, opts = {}) {
|
|
16878
16952
|
mkdirSync12(targetDir, { recursive: true });
|
|
16879
16953
|
const written = [];
|
|
16880
|
-
const extraPath =
|
|
16881
|
-
if (opts.force || !
|
|
16954
|
+
const extraPath = join25(targetDir, EXTRA_FILENAME);
|
|
16955
|
+
if (opts.force || !existsSync23(extraPath)) {
|
|
16882
16956
|
writeFileSync9(extraPath, JSON.stringify(EXTRA_TEMPLATE, null, 2), "utf-8");
|
|
16883
16957
|
written.push(extraPath);
|
|
16884
16958
|
}
|
|
16885
|
-
const samplePath =
|
|
16886
|
-
if (opts.force || !
|
|
16959
|
+
const samplePath = join25(targetDir, OVERRIDE_FILENAME);
|
|
16960
|
+
if (opts.force || !existsSync23(samplePath)) {
|
|
16887
16961
|
let payload = OVERRIDE_TEMPLATE;
|
|
16888
16962
|
if (opts.withBuiltin) {
|
|
16889
16963
|
let data = {};
|
|
16890
16964
|
const builtin = resolveRulesFile("l1_rules.json");
|
|
16891
|
-
if (builtin &&
|
|
16965
|
+
if (builtin && existsSync23(builtin)) {
|
|
16892
16966
|
try {
|
|
16893
|
-
data = JSON.parse(
|
|
16967
|
+
data = JSON.parse(readFileSync22(builtin, "utf-8")) || {};
|
|
16894
16968
|
} catch {
|
|
16895
16969
|
data = {};
|
|
16896
16970
|
}
|
|
@@ -16901,17 +16975,17 @@ function initRulesDir(targetDir, opts = {}) {
|
|
|
16901
16975
|
writeFileSync9(samplePath, JSON.stringify(payload, null, 2), "utf-8");
|
|
16902
16976
|
written.push(samplePath);
|
|
16903
16977
|
}
|
|
16904
|
-
const readmeSrc =
|
|
16905
|
-
const readmeDst =
|
|
16906
|
-
if (
|
|
16907
|
-
writeFileSync9(readmeDst,
|
|
16978
|
+
const readmeSrc = join25(templateSourceDir(), "README.md");
|
|
16979
|
+
const readmeDst = join25(targetDir, "README.md");
|
|
16980
|
+
if (existsSync23(readmeSrc) && (opts.force || !existsSync23(readmeDst))) {
|
|
16981
|
+
writeFileSync9(readmeDst, readFileSync22(readmeSrc, "utf-8"), "utf-8");
|
|
16908
16982
|
written.push(readmeDst);
|
|
16909
16983
|
}
|
|
16910
16984
|
return written;
|
|
16911
16985
|
}
|
|
16912
16986
|
function templateSourceDir() {
|
|
16913
|
-
const cands = [
|
|
16914
|
-
for (const c of cands) if (
|
|
16987
|
+
const cands = [join25(__dirname11, "data", "rules_template"), join25(__dirname11, "..", "..", "agent_moss", "data", "rules_template")];
|
|
16988
|
+
for (const c of cands) if (existsSync23(c)) return c;
|
|
16915
16989
|
return cands[0];
|
|
16916
16990
|
}
|
|
16917
16991
|
function templateFiles(dirs) {
|
|
@@ -16919,7 +16993,7 @@ function templateFiles(dirs) {
|
|
|
16919
16993
|
for (const d of dirs) {
|
|
16920
16994
|
try {
|
|
16921
16995
|
for (const name of readdirSync8(d)) {
|
|
16922
|
-
if (name.endsWith(".json.\u793A\u4F8B")) out.push(
|
|
16996
|
+
if (name.endsWith(".json.\u793A\u4F8B")) out.push(join25(d, name));
|
|
16923
16997
|
}
|
|
16924
16998
|
} catch {
|
|
16925
16999
|
}
|
|
@@ -16928,7 +17002,7 @@ function templateFiles(dirs) {
|
|
|
16928
17002
|
}
|
|
16929
17003
|
function rulesStatusText() {
|
|
16930
17004
|
const lines = [];
|
|
16931
|
-
const existing = rulesDirs().filter((d) =>
|
|
17005
|
+
const existing = rulesDirs().filter((d) => existsSync23(d));
|
|
16932
17006
|
lines.push("\u89C4\u5219\u76EE\u5F55: " + (existing.length > 0 ? existing.join(", ") : "\uFF08\u672A\u8BBE\u7F6E / \u4E0D\u5B58\u5728\uFF09"));
|
|
16933
17007
|
if (existing.length > 0) {
|
|
16934
17008
|
lines.push(` \uFF08\u53EA\u8BFB\u5176\u4E2D\u7684 ${OVERRIDE_FILENAME} \u4E0E ${EXTRA_FILENAME}\uFF1B\u5176\u4ED6\u6587\u4EF6\u540D\u4E00\u5F8B\u4E0D\u8BFB\uFF09`);
|
|
@@ -16975,7 +17049,7 @@ var init_rules_tools = __esm({
|
|
|
16975
17049
|
"use strict";
|
|
16976
17050
|
init_esm_shims();
|
|
16977
17051
|
init_rules_loader();
|
|
16978
|
-
__dirname11 =
|
|
17052
|
+
__dirname11 = dirname17(fileURLToPath13(import.meta.url));
|
|
16979
17053
|
EXTRA_TEMPLATE = {
|
|
16980
17054
|
_\u8BF4\u660E: [
|
|
16981
17055
|
"\u8FFD\u52A0\u89C4\u5219\uFF1A\u5185\u7F6E\u89C4\u5219\u5168\u90E8\u4FDD\u7559\uFF0C\u4F60\u5199\u5728\u8FD9\u91CC\u7684\u89C4\u5219\u52A0\u5728\u5185\u7F6E\u4E4B\u540E\u4E00\u8D77\u5224\u5B9A\u3002",
|
|
@@ -17018,7 +17092,7 @@ var init_rules_tools = __esm({
|
|
|
17018
17092
|
init_esm_shims();
|
|
17019
17093
|
init_server();
|
|
17020
17094
|
init_package_json();
|
|
17021
|
-
import { fileURLToPath as
|
|
17095
|
+
import { fileURLToPath as fileURLToPath14 } from "url";
|
|
17022
17096
|
import { realpathSync as realpathSync2 } from "fs";
|
|
17023
17097
|
function printHelp() {
|
|
17024
17098
|
console.log(`AgentMoss security analysis engine (TypeScript v${version})
|
|
@@ -17251,7 +17325,7 @@ async function main() {
|
|
|
17251
17325
|
const { execSync } = await import("child_process");
|
|
17252
17326
|
const fs9 = await import("fs");
|
|
17253
17327
|
const path9 = await import("path");
|
|
17254
|
-
const { fileURLToPath:
|
|
17328
|
+
const { fileURLToPath: fileURLToPath15 } = await import("url");
|
|
17255
17329
|
if (process.getuid?.() !== 0) {
|
|
17256
17330
|
try {
|
|
17257
17331
|
execSync("sudo -n true", { stdio: "ignore", timeout: 5e3 });
|
|
@@ -17266,7 +17340,7 @@ async function main() {
|
|
|
17266
17340
|
execSync(sudoArgs.join(" "), { stdio: "inherit" });
|
|
17267
17341
|
process.exit(0);
|
|
17268
17342
|
}
|
|
17269
|
-
const __filename7 =
|
|
17343
|
+
const __filename7 = fileURLToPath15(import.meta.url);
|
|
17270
17344
|
const __dirname12 = path9.dirname(__filename7);
|
|
17271
17345
|
let srcPath = path9.join(__dirname12, "data", "agent_moss.service");
|
|
17272
17346
|
if (!fs9.existsSync(srcPath)) {
|
|
@@ -17323,13 +17397,13 @@ async function main() {
|
|
|
17323
17397
|
const seen = [];
|
|
17324
17398
|
for (let port = 9090; port <= 9095; port++) {
|
|
17325
17399
|
try {
|
|
17326
|
-
const data = await new Promise((
|
|
17400
|
+
const data = await new Promise((resolve13, reject) => {
|
|
17327
17401
|
const req = http.get(`http://127.0.0.1:${port}/api/v1/health`, { timeout: 2e3 }, (res) => {
|
|
17328
17402
|
let body = "";
|
|
17329
17403
|
res.on("data", (chunk) => body += chunk);
|
|
17330
17404
|
res.on("end", () => {
|
|
17331
17405
|
try {
|
|
17332
|
-
|
|
17406
|
+
resolve13(JSON.parse(body));
|
|
17333
17407
|
} catch {
|
|
17334
17408
|
reject();
|
|
17335
17409
|
}
|
|
@@ -17369,13 +17443,13 @@ async function main() {
|
|
|
17369
17443
|
console.log(` Systemd: \u672A\u6CE8\u518C\uFF08\u8FD0\u884C agent-moss install --enable \u5B89\u88C5\uFF09`);
|
|
17370
17444
|
}
|
|
17371
17445
|
try {
|
|
17372
|
-
const data = await new Promise((
|
|
17446
|
+
const data = await new Promise((resolve13, reject) => {
|
|
17373
17447
|
const req = http.get(`http://127.0.0.1:${foundPort}/console/api/config`, { timeout: 3e3 }, (res) => {
|
|
17374
17448
|
let body = "";
|
|
17375
17449
|
res.on("data", (chunk) => body += chunk);
|
|
17376
17450
|
res.on("end", () => {
|
|
17377
17451
|
try {
|
|
17378
|
-
|
|
17452
|
+
resolve13(JSON.parse(body));
|
|
17379
17453
|
} catch {
|
|
17380
17454
|
reject();
|
|
17381
17455
|
}
|
|
@@ -17440,9 +17514,9 @@ async function main() {
|
|
|
17440
17514
|
var isMain = (() => {
|
|
17441
17515
|
try {
|
|
17442
17516
|
if (!process.argv[1]) return false;
|
|
17443
|
-
const thisFile = realpathSync2(
|
|
17517
|
+
const thisFile = realpathSync2(fileURLToPath14(import.meta.url));
|
|
17444
17518
|
const argv1 = realpathSync2(
|
|
17445
|
-
process.argv[1].startsWith("file://") ?
|
|
17519
|
+
process.argv[1].startsWith("file://") ? fileURLToPath14(process.argv[1]) : process.argv[1]
|
|
17446
17520
|
);
|
|
17447
17521
|
return thisFile === argv1;
|
|
17448
17522
|
} catch {
|