@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/server.js
CHANGED
|
@@ -3229,8 +3229,8 @@ function loadRuntimeConfig() {
|
|
|
3229
3229
|
}
|
|
3230
3230
|
try {
|
|
3231
3231
|
const st = statSync3(p);
|
|
3232
|
-
const
|
|
3233
|
-
if (runtimeConfigCache && runtimeConfigCache.key ===
|
|
3232
|
+
const cacheKey2 = `${p}|${st.mtimeMs}|${st.size}`;
|
|
3233
|
+
if (runtimeConfigCache && runtimeConfigCache.key === cacheKey2) {
|
|
3234
3234
|
return runtimeConfigCache.value;
|
|
3235
3235
|
}
|
|
3236
3236
|
const data = JSON.parse(readFileSync6(p, "utf-8"));
|
|
@@ -3255,7 +3255,7 @@ function loadRuntimeConfig() {
|
|
|
3255
3255
|
agent_rules: merged.agent_rules || {},
|
|
3256
3256
|
scenario_rules: merged.scenario_rules || {}
|
|
3257
3257
|
};
|
|
3258
|
-
runtimeConfigCache = { key:
|
|
3258
|
+
runtimeConfigCache = { key: cacheKey2, value: result };
|
|
3259
3259
|
return result;
|
|
3260
3260
|
} catch {
|
|
3261
3261
|
return emptyRuntimeConfig();
|
|
@@ -4895,15 +4895,66 @@ var init_config = __esm({
|
|
|
4895
4895
|
}
|
|
4896
4896
|
});
|
|
4897
4897
|
|
|
4898
|
-
// src/
|
|
4899
|
-
import { readFileSync as readFileSync7,
|
|
4900
|
-
import {
|
|
4898
|
+
// src/infra/edition-scope.ts
|
|
4899
|
+
import { existsSync as existsSync8, readFileSync as readFileSync7, statSync as statSync4 } from "fs";
|
|
4900
|
+
import { dirname as dirname6, join as join8, resolve as resolve5 } from "path";
|
|
4901
4901
|
import { fileURLToPath as fileURLToPath6 } from "url";
|
|
4902
|
+
function scopePath(moduleDir) {
|
|
4903
|
+
const root = getRulesRoot();
|
|
4904
|
+
if (root) return join8(root, "edition_scope.json");
|
|
4905
|
+
const prod = resolve5(moduleDir, "..", "edition_scope.json");
|
|
4906
|
+
if (existsSync8(prod)) return prod;
|
|
4907
|
+
const dev = resolve5(moduleDir, "../../..", "agent_moss", "edition_scope.json");
|
|
4908
|
+
if (existsSync8(dev)) return dev;
|
|
4909
|
+
return prod;
|
|
4910
|
+
}
|
|
4911
|
+
function loadScope(moduleDir) {
|
|
4912
|
+
const file = scopePath(moduleDir);
|
|
4913
|
+
try {
|
|
4914
|
+
const st = statSync4(file);
|
|
4915
|
+
const key = `${file}:${st.mtimeMs}:${st.size}`;
|
|
4916
|
+
if (key === cacheKey) return cacheData;
|
|
4917
|
+
const parsed = JSON.parse(readFileSync7(file, "utf-8"));
|
|
4918
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
4919
|
+
throw new Error("\u6839\u8282\u70B9\u4E0D\u662F\u5BF9\u8C61");
|
|
4920
|
+
}
|
|
4921
|
+
cacheKey = key;
|
|
4922
|
+
cacheData = parsed;
|
|
4923
|
+
return cacheData;
|
|
4924
|
+
} catch {
|
|
4925
|
+
cacheKey = "";
|
|
4926
|
+
cacheData = null;
|
|
4927
|
+
return null;
|
|
4928
|
+
}
|
|
4929
|
+
}
|
|
4930
|
+
function isAllowed(kind, id, moduleDir = MODULE_DIR) {
|
|
4931
|
+
const scope = loadScope(moduleDir);
|
|
4932
|
+
if (!scope) return true;
|
|
4933
|
+
const allowed = scope[kind];
|
|
4934
|
+
if (!Array.isArray(allowed)) return true;
|
|
4935
|
+
return allowed.includes(id);
|
|
4936
|
+
}
|
|
4937
|
+
var MODULE_DIR, cacheKey, cacheData;
|
|
4938
|
+
var init_edition_scope = __esm({
|
|
4939
|
+
"src/infra/edition-scope.ts"() {
|
|
4940
|
+
"use strict";
|
|
4941
|
+
init_esm_shims();
|
|
4942
|
+
init_paths();
|
|
4943
|
+
MODULE_DIR = dirname6(fileURLToPath6(import.meta.url));
|
|
4944
|
+
cacheKey = "";
|
|
4945
|
+
cacheData = null;
|
|
4946
|
+
}
|
|
4947
|
+
});
|
|
4948
|
+
|
|
4949
|
+
// src/agents/index.ts
|
|
4950
|
+
import { readFileSync as readFileSync8, existsSync as existsSync9 } from "fs";
|
|
4951
|
+
import { resolve as resolve6, dirname as dirname7, join as join9 } from "path";
|
|
4952
|
+
import { fileURLToPath as fileURLToPath7 } from "url";
|
|
4902
4953
|
function resolveAgentsDir() {
|
|
4903
4954
|
const root = getRulesRoot();
|
|
4904
|
-
if (root) return
|
|
4905
|
-
if (
|
|
4906
|
-
if (
|
|
4955
|
+
if (root) return join9(root, "agents");
|
|
4956
|
+
if (existsSync9(AGENTS_DIR_PROD)) return AGENTS_DIR_PROD;
|
|
4957
|
+
if (existsSync9(AGENTS_DIR_DEV_FALLBACK)) return AGENTS_DIR_DEV_FALLBACK;
|
|
4907
4958
|
return AGENTS_DIR_PROD;
|
|
4908
4959
|
}
|
|
4909
4960
|
function getAgentExtensions(agentId) {
|
|
@@ -4912,6 +4963,7 @@ function getAgentExtensions(agentId) {
|
|
|
4912
4963
|
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)}`);
|
|
4913
4964
|
return null;
|
|
4914
4965
|
}
|
|
4966
|
+
if (!isAllowed("agents", agentId)) return null;
|
|
4915
4967
|
if (cache.size >= CACHE_MAX) cache.clear();
|
|
4916
4968
|
const fp = getRuleSourceFingerprint();
|
|
4917
4969
|
const cached2 = cache.get(agentId);
|
|
@@ -4925,13 +4977,13 @@ function getAgentExtensions(agentId) {
|
|
|
4925
4977
|
}
|
|
4926
4978
|
if (ext === null) {
|
|
4927
4979
|
const agentsDir = resolveAgentsDir();
|
|
4928
|
-
const rulesFile =
|
|
4929
|
-
if (!
|
|
4980
|
+
const rulesFile = resolve6(agentsDir, agentId, "rules.json");
|
|
4981
|
+
if (!existsSync9(rulesFile)) {
|
|
4930
4982
|
cache.set(agentId, { fp, ext: null });
|
|
4931
4983
|
return null;
|
|
4932
4984
|
}
|
|
4933
4985
|
try {
|
|
4934
|
-
const raw =
|
|
4986
|
+
const raw = readFileSync8(rulesFile, "utf-8");
|
|
4935
4987
|
ext = JSON.parse(raw);
|
|
4936
4988
|
if (!ext.agent_id) {
|
|
4937
4989
|
console.warn(`[AgentMoss agents] rules.json \u7F3A\u5C11 agent_id \u5B57\u6BB5: ${rulesFile}`);
|
|
@@ -4972,25 +5024,26 @@ var init_agents = __esm({
|
|
|
4972
5024
|
"use strict";
|
|
4973
5025
|
init_esm_shims();
|
|
4974
5026
|
init_paths();
|
|
5027
|
+
init_edition_scope();
|
|
4975
5028
|
init_runtime_config();
|
|
4976
|
-
__filename4 =
|
|
4977
|
-
__dirname5 =
|
|
4978
|
-
AGENTS_DIR_PROD =
|
|
4979
|
-
AGENTS_DIR_DEV_FALLBACK =
|
|
5029
|
+
__filename4 = fileURLToPath7(import.meta.url);
|
|
5030
|
+
__dirname5 = dirname7(__filename4);
|
|
5031
|
+
AGENTS_DIR_PROD = resolve6(__dirname5, "agents");
|
|
5032
|
+
AGENTS_DIR_DEV_FALLBACK = resolve6(__dirname5, "../../../agent_moss/agents");
|
|
4980
5033
|
cache = /* @__PURE__ */ new Map();
|
|
4981
5034
|
CACHE_MAX = 512;
|
|
4982
5035
|
}
|
|
4983
5036
|
});
|
|
4984
5037
|
|
|
4985
5038
|
// src/scenarios/index.ts
|
|
4986
|
-
import { readFileSync as
|
|
4987
|
-
import { resolve as
|
|
4988
|
-
import { fileURLToPath as
|
|
5039
|
+
import { readFileSync as readFileSync9, existsSync as existsSync10 } from "fs";
|
|
5040
|
+
import { resolve as resolve7, dirname as dirname8, join as join10 } from "path";
|
|
5041
|
+
import { fileURLToPath as fileURLToPath8 } from "url";
|
|
4989
5042
|
function resolveScenariosDir() {
|
|
4990
5043
|
const root = getRulesRoot();
|
|
4991
|
-
if (root) return
|
|
4992
|
-
if (
|
|
4993
|
-
if (
|
|
5044
|
+
if (root) return join10(root, "scenarios");
|
|
5045
|
+
if (existsSync10(SCENARIOS_DIR_PROD)) return SCENARIOS_DIR_PROD;
|
|
5046
|
+
if (existsSync10(SCENARIOS_DIR_DEV_FALLBACK)) return SCENARIOS_DIR_DEV_FALLBACK;
|
|
4994
5047
|
return SCENARIOS_DIR_PROD;
|
|
4995
5048
|
}
|
|
4996
5049
|
function getScenarioExtensions(scenarioId) {
|
|
@@ -4999,6 +5052,7 @@ function getScenarioExtensions(scenarioId) {
|
|
|
4999
5052
|
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)}`);
|
|
5000
5053
|
return null;
|
|
5001
5054
|
}
|
|
5055
|
+
if (!isAllowed("scenarios", scenarioId)) return null;
|
|
5002
5056
|
if (cache2.size >= CACHE_MAX2) cache2.clear();
|
|
5003
5057
|
const fp = getRuleSourceFingerprint();
|
|
5004
5058
|
const cached2 = cache2.get(scenarioId);
|
|
@@ -5018,19 +5072,19 @@ function getScenarioExtensions(scenarioId) {
|
|
|
5018
5072
|
}
|
|
5019
5073
|
}
|
|
5020
5074
|
if (ext === null) {
|
|
5021
|
-
const builtinFile =
|
|
5075
|
+
const builtinFile = resolve7(resolveScenariosDir(), scenarioId, "rules.json");
|
|
5022
5076
|
const rulesFile = overrideHit ?? builtinFile;
|
|
5023
5077
|
if (overrideHit) {
|
|
5024
5078
|
console.warn(
|
|
5025
5079
|
`[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`
|
|
5026
5080
|
);
|
|
5027
5081
|
}
|
|
5028
|
-
if (!
|
|
5082
|
+
if (!existsSync10(rulesFile)) {
|
|
5029
5083
|
cache2.set(scenarioId, { fp, ext: null });
|
|
5030
5084
|
return null;
|
|
5031
5085
|
}
|
|
5032
5086
|
try {
|
|
5033
|
-
const raw =
|
|
5087
|
+
const raw = readFileSync9(rulesFile, "utf-8");
|
|
5034
5088
|
const data = JSON.parse(raw);
|
|
5035
5089
|
const id = data.scenario_id || data.agent_id || "";
|
|
5036
5090
|
if (!id) {
|
|
@@ -5076,13 +5130,14 @@ var init_scenarios = __esm({
|
|
|
5076
5130
|
"use strict";
|
|
5077
5131
|
init_esm_shims();
|
|
5078
5132
|
init_paths();
|
|
5133
|
+
init_edition_scope();
|
|
5079
5134
|
init_agents();
|
|
5080
5135
|
init_runtime_config();
|
|
5081
5136
|
init_rules_loader();
|
|
5082
|
-
__filename5 =
|
|
5083
|
-
__dirname6 =
|
|
5084
|
-
SCENARIOS_DIR_PROD =
|
|
5085
|
-
SCENARIOS_DIR_DEV_FALLBACK =
|
|
5137
|
+
__filename5 = fileURLToPath8(import.meta.url);
|
|
5138
|
+
__dirname6 = dirname8(__filename5);
|
|
5139
|
+
SCENARIOS_DIR_PROD = resolve7(__dirname6, "scenarios");
|
|
5140
|
+
SCENARIOS_DIR_DEV_FALLBACK = resolve7(__dirname6, "../../../agent_moss/scenarios");
|
|
5086
5141
|
cache2 = /* @__PURE__ */ new Map();
|
|
5087
5142
|
CACHE_MAX2 = 512;
|
|
5088
5143
|
}
|
|
@@ -6533,9 +6588,9 @@ var init_heuristic = __esm({
|
|
|
6533
6588
|
});
|
|
6534
6589
|
|
|
6535
6590
|
// src/engine/template-loader.ts
|
|
6536
|
-
import { readFileSync as
|
|
6537
|
-
import { resolve as
|
|
6538
|
-
import { fileURLToPath as
|
|
6591
|
+
import { readFileSync as readFileSync10, existsSync as existsSync11 } from "fs";
|
|
6592
|
+
import { resolve as resolve8, dirname as dirname9 } from "path";
|
|
6593
|
+
import { fileURLToPath as fileURLToPath9 } from "url";
|
|
6539
6594
|
function loadTemplates() {
|
|
6540
6595
|
if (cachedTemplates) return cachedTemplates;
|
|
6541
6596
|
cachedTemplates = {};
|
|
@@ -6546,10 +6601,10 @@ function loadTemplates() {
|
|
|
6546
6601
|
return cachedTemplates;
|
|
6547
6602
|
}
|
|
6548
6603
|
for (const file of TEMPLATE_FILES) {
|
|
6549
|
-
const filePath =
|
|
6550
|
-
if (
|
|
6604
|
+
const filePath = resolve8(dir, file);
|
|
6605
|
+
if (existsSync11(filePath)) {
|
|
6551
6606
|
const key = file.replace(".txt", "").replace("_template", "");
|
|
6552
|
-
cachedTemplates[key] =
|
|
6607
|
+
cachedTemplates[key] = readFileSync10(filePath, "utf-8");
|
|
6553
6608
|
}
|
|
6554
6609
|
}
|
|
6555
6610
|
console.log(`[AgentMoss] Loaded ${Object.keys(cachedTemplates).length} templates from ${dir}`);
|
|
@@ -6561,16 +6616,16 @@ var init_template_loader = __esm({
|
|
|
6561
6616
|
"use strict";
|
|
6562
6617
|
init_esm_shims();
|
|
6563
6618
|
init_asset_dir();
|
|
6564
|
-
__filename6 =
|
|
6565
|
-
__dirname7 =
|
|
6619
|
+
__filename6 = fileURLToPath9(import.meta.url);
|
|
6620
|
+
__dirname7 = dirname9(__filename6);
|
|
6566
6621
|
TEMPLATE_FILES = ["prompt1_template.txt", "prompt2_template.txt", "security_judge_template.txt"];
|
|
6567
6622
|
cachedTemplates = null;
|
|
6568
6623
|
}
|
|
6569
6624
|
});
|
|
6570
6625
|
|
|
6571
6626
|
// src/engine/script-analyzer.ts
|
|
6572
|
-
import { readFileSync as
|
|
6573
|
-
import { resolve as
|
|
6627
|
+
import { readFileSync as readFileSync11 } from "fs";
|
|
6628
|
+
import { resolve as resolve9, join as join11 } from "path";
|
|
6574
6629
|
function emptyResult() {
|
|
6575
6630
|
return { scriptPath: "", scriptContent: "", matchedKeywords: [], lineCount: 0, isSuspicious: false, error: "", riskLevel: "", riskReason: "" };
|
|
6576
6631
|
}
|
|
@@ -6623,8 +6678,8 @@ function assessKeywordCombinationRisk(matchedKeywords, scriptContent) {
|
|
|
6623
6678
|
}
|
|
6624
6679
|
function readScriptContent(scriptPath, maxLines = 500) {
|
|
6625
6680
|
try {
|
|
6626
|
-
const expandedPath = scriptPath === "~" || scriptPath.startsWith("~/") ?
|
|
6627
|
-
const raw =
|
|
6681
|
+
const expandedPath = scriptPath === "~" || scriptPath.startsWith("~/") ? join11(process.env.HOME || "/home/user", scriptPath.slice(1)) : resolve9(scriptPath);
|
|
6682
|
+
const raw = readFileSync11(expandedPath, "utf-8");
|
|
6628
6683
|
const lines = raw.split("\n").slice(0, maxLines);
|
|
6629
6684
|
return { content: lines.join("\n"), lineCount: lines.length, error: "" };
|
|
6630
6685
|
} catch (e) {
|
|
@@ -7902,7 +7957,7 @@ function evalIfCondition(cond, args) {
|
|
|
7902
7957
|
const op = m[2];
|
|
7903
7958
|
let left = m[1].trim();
|
|
7904
7959
|
let right = m[3].trim();
|
|
7905
|
-
const
|
|
7960
|
+
const resolve13 = (tok) => {
|
|
7906
7961
|
const rm = tok.match(/^\$(\d+)$/);
|
|
7907
7962
|
if (rm) {
|
|
7908
7963
|
const idx = parseInt(rm[1], 10);
|
|
@@ -7910,8 +7965,8 @@ function evalIfCondition(cond, args) {
|
|
|
7910
7965
|
}
|
|
7911
7966
|
return tok.startsWith("$") ? null : tok;
|
|
7912
7967
|
};
|
|
7913
|
-
const lv =
|
|
7914
|
-
const rv =
|
|
7968
|
+
const lv = resolve13(left);
|
|
7969
|
+
const rv = resolve13(right);
|
|
7915
7970
|
if (lv === null || rv === null) return null;
|
|
7916
7971
|
if (op === "=~") {
|
|
7917
7972
|
let re = rv;
|
|
@@ -8253,8 +8308,8 @@ var init_tracker = __esm({
|
|
|
8253
8308
|
});
|
|
8254
8309
|
|
|
8255
8310
|
// src/engine/script-content-analyzer.ts
|
|
8256
|
-
import { statSync as
|
|
8257
|
-
import { resolve as
|
|
8311
|
+
import { statSync as statSync6, existsSync as existsSync13 } from "fs";
|
|
8312
|
+
import { resolve as resolve11, join as join13, isAbsolute as isAbsolute4 } from "path";
|
|
8258
8313
|
import { homedir as homedir5 } from "os";
|
|
8259
8314
|
function emptyChain() {
|
|
8260
8315
|
return {
|
|
@@ -8366,8 +8421,8 @@ function extractInlineScriptPath(actionDetail) {
|
|
|
8366
8421
|
if (looksLikeScriptPath(tok)) {
|
|
8367
8422
|
try {
|
|
8368
8423
|
const resolved = resolvePath2(tok.replace(/^['"]|['"]$/g, ""));
|
|
8369
|
-
if (
|
|
8370
|
-
const stat =
|
|
8424
|
+
if (existsSync13(resolved)) {
|
|
8425
|
+
const stat = statSync6(resolved);
|
|
8371
8426
|
if (stat.isFile()) return resolved;
|
|
8372
8427
|
}
|
|
8373
8428
|
} catch {
|
|
@@ -8380,8 +8435,8 @@ function extractInlineScriptPath(actionDetail) {
|
|
|
8380
8435
|
const p = m[1];
|
|
8381
8436
|
try {
|
|
8382
8437
|
const resolved = resolvePath2(p);
|
|
8383
|
-
if (
|
|
8384
|
-
const stat =
|
|
8438
|
+
if (existsSync13(resolved)) {
|
|
8439
|
+
const stat = statSync6(resolved);
|
|
8385
8440
|
if (stat.isFile()) return resolved;
|
|
8386
8441
|
}
|
|
8387
8442
|
} catch {
|
|
@@ -8396,8 +8451,8 @@ function extractInlineScriptPath(actionDetail) {
|
|
|
8396
8451
|
let resolved;
|
|
8397
8452
|
try {
|
|
8398
8453
|
resolved = resolvePath2(p);
|
|
8399
|
-
if (
|
|
8400
|
-
const stat =
|
|
8454
|
+
if (existsSync13(resolved)) {
|
|
8455
|
+
const stat = statSync6(resolved);
|
|
8401
8456
|
if (stat.isFile()) return resolved;
|
|
8402
8457
|
}
|
|
8403
8458
|
} catch {
|
|
@@ -8437,9 +8492,9 @@ function shlexSplit(s) {
|
|
|
8437
8492
|
function resolvePath2(p) {
|
|
8438
8493
|
let expanded = p;
|
|
8439
8494
|
if (expanded.startsWith("~/") || expanded === "~") {
|
|
8440
|
-
expanded =
|
|
8495
|
+
expanded = join13(homedir5(), expanded.slice(1));
|
|
8441
8496
|
}
|
|
8442
|
-
return
|
|
8497
|
+
return resolve11(expanded);
|
|
8443
8498
|
}
|
|
8444
8499
|
function scanExtensionPatterns(ext, content) {
|
|
8445
8500
|
const pats = ext?.extra_command_patterns;
|
|
@@ -8526,7 +8581,7 @@ function analyzeScriptContent2(input, ext, stopOnCritical = true) {
|
|
|
8526
8581
|
isInline = true;
|
|
8527
8582
|
}
|
|
8528
8583
|
if (resolvedPath && !isAbsolute4(resolvedPath) && !resolvedPath.startsWith("~/")) {
|
|
8529
|
-
resolvedPath =
|
|
8584
|
+
resolvedPath = join13(input.cwd || process.cwd(), resolvedPath);
|
|
8530
8585
|
}
|
|
8531
8586
|
const l1Adapter = (content) => {
|
|
8532
8587
|
const extHit = scanExtensionPatterns(ext, content);
|
|
@@ -9127,17 +9182,17 @@ __export(token_stats_exports, {
|
|
|
9127
9182
|
resetTokenStats: () => resetTokenStats,
|
|
9128
9183
|
resetTokenStatsCacheForTests: () => resetTokenStatsCacheForTests
|
|
9129
9184
|
});
|
|
9130
|
-
import { readFileSync as
|
|
9185
|
+
import { readFileSync as readFileSync14, writeFileSync as writeFileSync2, existsSync as existsSync14, mkdirSync as mkdirSync3, renameSync, unlinkSync as unlinkSync2, statSync as statSync7 } from "fs";
|
|
9131
9186
|
function getStatsPath() {
|
|
9132
9187
|
const envPath = process.env[ENV_STATS_PATH];
|
|
9133
9188
|
if (envPath) return envPath;
|
|
9134
9189
|
return getTokenStatsPath();
|
|
9135
9190
|
}
|
|
9136
9191
|
function loadAllRecords(statsPath) {
|
|
9137
|
-
if (!
|
|
9192
|
+
if (!existsSync14(statsPath)) return [];
|
|
9138
9193
|
let sig = "";
|
|
9139
9194
|
try {
|
|
9140
|
-
const st =
|
|
9195
|
+
const st = statSync7(statsPath);
|
|
9141
9196
|
sig = `${st.mtimeMs}:${st.size}`;
|
|
9142
9197
|
} catch {
|
|
9143
9198
|
return [];
|
|
@@ -9146,7 +9201,7 @@ function loadAllRecords(statsPath) {
|
|
|
9146
9201
|
return _recordsCache.records.slice();
|
|
9147
9202
|
}
|
|
9148
9203
|
try {
|
|
9149
|
-
const data = JSON.parse(
|
|
9204
|
+
const data = JSON.parse(readFileSync14(statsPath, "utf-8"));
|
|
9150
9205
|
let records;
|
|
9151
9206
|
if (Array.isArray(data)) records = data;
|
|
9152
9207
|
else if (data && Array.isArray(data.records)) records = data.records;
|
|
@@ -9160,7 +9215,7 @@ function loadAllRecords(statsPath) {
|
|
|
9160
9215
|
function saveRecords(statsPath, records) {
|
|
9161
9216
|
try {
|
|
9162
9217
|
const dir = statsPath.substring(0, statsPath.lastIndexOf("/"));
|
|
9163
|
-
if (dir && !
|
|
9218
|
+
if (dir && !existsSync14(dir)) mkdirSync3(dir, { recursive: true });
|
|
9164
9219
|
const tmp = `${statsPath}.${process.pid}.${Date.now()}.tmp`;
|
|
9165
9220
|
try {
|
|
9166
9221
|
writeFileSync2(tmp, JSON.stringify(records, null, 2), "utf-8");
|
|
@@ -9172,7 +9227,7 @@ function saveRecords(statsPath, records) {
|
|
|
9172
9227
|
}
|
|
9173
9228
|
}
|
|
9174
9229
|
try {
|
|
9175
|
-
const st =
|
|
9230
|
+
const st = statSync7(statsPath);
|
|
9176
9231
|
_recordsCache = {
|
|
9177
9232
|
path: statsPath,
|
|
9178
9233
|
sig: `${st.mtimeMs}:${st.size}`,
|
|
@@ -9309,7 +9364,7 @@ function getTokenTrend(days = 0, startDate, endDate) {
|
|
|
9309
9364
|
}
|
|
9310
9365
|
function resetTokenStats() {
|
|
9311
9366
|
const statsPath = getStatsPath();
|
|
9312
|
-
if (
|
|
9367
|
+
if (existsSync14(statsPath)) {
|
|
9313
9368
|
try {
|
|
9314
9369
|
unlinkSync2(statsPath);
|
|
9315
9370
|
} catch {
|
|
@@ -9409,8 +9464,8 @@ async function runLLMAnalysis(input, previous) {
|
|
|
9409
9464
|
}
|
|
9410
9465
|
const config = perRequest?.apiKey ? { ...globalConfig, ...perRequest } : globalConfig;
|
|
9411
9466
|
const rawDetail = input.aNext.actionDetail.replace(/^terminal-\w+:\s*command=([^,]+).*/, "$1").trim();
|
|
9412
|
-
const
|
|
9413
|
-
const cached2 = llmCache.get(
|
|
9467
|
+
const cacheKey2 = `${input.aNext.actionType}:${createHash3("sha256").update(rawDetail || input.aNext.actionDetail).digest("hex")}`;
|
|
9468
|
+
const cached2 = llmCache.get(cacheKey2);
|
|
9414
9469
|
if (cached2) {
|
|
9415
9470
|
console.log("[AgentMoss] LLM cache hit");
|
|
9416
9471
|
return cached2;
|
|
@@ -9499,7 +9554,7 @@ async function runLLMAnalysis(input, previous) {
|
|
|
9499
9554
|
const firstKey = llmCache.keys().next().value;
|
|
9500
9555
|
if (firstKey) llmCache.delete(firstKey);
|
|
9501
9556
|
}
|
|
9502
|
-
llmCache.set(
|
|
9557
|
+
llmCache.set(cacheKey2, result);
|
|
9503
9558
|
return result;
|
|
9504
9559
|
}
|
|
9505
9560
|
function logPrompt(prompt, aNext, logPath) {
|
|
@@ -9847,7 +9902,7 @@ async function analyzeWithLLM(prompt, config) {
|
|
|
9847
9902
|
)}`;
|
|
9848
9903
|
if (attempt < maxRetries) {
|
|
9849
9904
|
console.error(`[AgentMoss] LLM call failed (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${retryCfg.retryInterval}s: ${detail}`);
|
|
9850
|
-
await new Promise((
|
|
9905
|
+
await new Promise((resolve13) => setTimeout(resolve13, retryCfg.retryInterval * 1e3));
|
|
9851
9906
|
} else {
|
|
9852
9907
|
clearTimeout(totalTimeoutTimer);
|
|
9853
9908
|
console.error(`[AgentMoss] LLM call failed after all retries: ${detail}`);
|
|
@@ -9893,8 +9948,8 @@ var init_llm_analyzer = __esm({
|
|
|
9893
9948
|
});
|
|
9894
9949
|
|
|
9895
9950
|
// src/brain/rule-store.ts
|
|
9896
|
-
import { existsSync as
|
|
9897
|
-
import { join as
|
|
9951
|
+
import { existsSync as existsSync15, readFileSync as readFileSync15, writeFileSync as writeFileSync3, mkdirSync as mkdirSync4 } from "fs";
|
|
9952
|
+
import { join as join14 } from "path";
|
|
9898
9953
|
import { homedir as homedir6 } from "os";
|
|
9899
9954
|
function getRulesPath() {
|
|
9900
9955
|
const envPath = process.env[ENV_RULES_PATH];
|
|
@@ -9913,7 +9968,7 @@ var init_rule_store = __esm({
|
|
|
9913
9968
|
"src/brain/rule-store.ts"() {
|
|
9914
9969
|
"use strict";
|
|
9915
9970
|
init_esm_shims();
|
|
9916
|
-
DEFAULT_RULES_PATH =
|
|
9971
|
+
DEFAULT_RULES_PATH = join14(homedir6(), ".agentmoss", "brain", "rules.json");
|
|
9917
9972
|
ENV_RULES_PATH = "AGENT_MOSS_BRAIN_RULES_PATH";
|
|
9918
9973
|
STATUS_DRAFT = "draft";
|
|
9919
9974
|
STATUS_APPROVED = "approved";
|
|
@@ -9930,11 +9985,11 @@ var init_rule_store = __esm({
|
|
|
9930
9985
|
this.data = this.load();
|
|
9931
9986
|
}
|
|
9932
9987
|
load() {
|
|
9933
|
-
if (!
|
|
9988
|
+
if (!existsSync15(this.path)) {
|
|
9934
9989
|
return { version: 1, rules: [], pattern_candidates: [] };
|
|
9935
9990
|
}
|
|
9936
9991
|
try {
|
|
9937
|
-
const raw = JSON.parse(
|
|
9992
|
+
const raw = JSON.parse(readFileSync15(this.path, "utf-8"));
|
|
9938
9993
|
return {
|
|
9939
9994
|
version: raw.version || 1,
|
|
9940
9995
|
rules: raw.rules || [],
|
|
@@ -10528,9 +10583,9 @@ var init_reviewer = __esm({
|
|
|
10528
10583
|
});
|
|
10529
10584
|
|
|
10530
10585
|
// src/brain/skill-manager.ts
|
|
10531
|
-
import { existsSync as
|
|
10586
|
+
import { existsSync as existsSync16, mkdirSync as mkdirSync5, readFileSync as readFileSync16, renameSync as renameSync2, writeFileSync as writeFileSync4 } from "fs";
|
|
10532
10587
|
import { homedir as homedir7 } from "os";
|
|
10533
|
-
import { join as
|
|
10588
|
+
import { join as join15 } from "path";
|
|
10534
10589
|
import { randomUUID } from "crypto";
|
|
10535
10590
|
function getMetaPath(override) {
|
|
10536
10591
|
return override || process.env[ENV_SKILL_META_PATH] || DEFAULT_SKILL_META_PATH;
|
|
@@ -10540,14 +10595,14 @@ var init_skill_manager = __esm({
|
|
|
10540
10595
|
"src/brain/skill-manager.ts"() {
|
|
10541
10596
|
"use strict";
|
|
10542
10597
|
init_esm_shims();
|
|
10543
|
-
DEFAULT_SKILL_META_PATH =
|
|
10598
|
+
DEFAULT_SKILL_META_PATH = join15(homedir7(), ".agentmoss", "brain", "skill_meta.json");
|
|
10544
10599
|
ENV_SKILL_META_PATH = "AGENT_MOSS_BRAIN_SKILL_META_PATH";
|
|
10545
10600
|
BrainSkillManager = class {
|
|
10546
10601
|
constructor(skillsDir, metaPath) {
|
|
10547
10602
|
this.skillsDir = skillsDir;
|
|
10548
10603
|
this.metaPath = getMetaPath(metaPath);
|
|
10549
10604
|
try {
|
|
10550
|
-
mkdirSync5(
|
|
10605
|
+
mkdirSync5(join15(this.metaPath, ".."), { recursive: true });
|
|
10551
10606
|
} catch {
|
|
10552
10607
|
}
|
|
10553
10608
|
this.load();
|
|
@@ -10557,9 +10612,9 @@ var init_skill_manager = __esm({
|
|
|
10557
10612
|
skills = [];
|
|
10558
10613
|
metaPath;
|
|
10559
10614
|
load() {
|
|
10560
|
-
if (!
|
|
10615
|
+
if (!existsSync16(this.metaPath)) return;
|
|
10561
10616
|
try {
|
|
10562
|
-
const data = JSON.parse(
|
|
10617
|
+
const data = JSON.parse(readFileSync16(this.metaPath, "utf-8"));
|
|
10563
10618
|
this.skills = data.skills || [];
|
|
10564
10619
|
} catch {
|
|
10565
10620
|
this.skills = [];
|
|
@@ -10582,7 +10637,7 @@ var init_skill_manager = __esm({
|
|
|
10582
10637
|
let changed = false;
|
|
10583
10638
|
for (const sk of this.skills) {
|
|
10584
10639
|
if (sk.status !== "active") continue;
|
|
10585
|
-
if (!
|
|
10640
|
+
if (!existsSync16(join15(this.skillsDir, `${sk.name}.md`))) {
|
|
10586
10641
|
sk.status = "stale";
|
|
10587
10642
|
changed = true;
|
|
10588
10643
|
}
|
|
@@ -10593,9 +10648,9 @@ var init_skill_manager = __esm({
|
|
|
10593
10648
|
addSkill(skill) {
|
|
10594
10649
|
const id = skill.id || randomUUID();
|
|
10595
10650
|
const name = skill.name || `brain_${id.slice(0, 8)}`;
|
|
10596
|
-
if (!
|
|
10597
|
-
const mdPath =
|
|
10598
|
-
if (
|
|
10651
|
+
if (!existsSync16(this.skillsDir)) return "";
|
|
10652
|
+
const mdPath = join15(this.skillsDir, `${name}.md`);
|
|
10653
|
+
if (existsSync16(mdPath)) return "";
|
|
10599
10654
|
try {
|
|
10600
10655
|
writeFileSync4(mdPath, skill.content, "utf-8");
|
|
10601
10656
|
} catch {
|
|
@@ -10644,12 +10699,12 @@ var init_skill_manager = __esm({
|
|
|
10644
10699
|
const out = [];
|
|
10645
10700
|
for (const sk of this.skills) {
|
|
10646
10701
|
if (sk.origin_node !== "local" || sk.status !== "active") continue;
|
|
10647
|
-
const mdPath =
|
|
10648
|
-
if (!
|
|
10702
|
+
const mdPath = join15(this.skillsDir, `${sk.name}.md`);
|
|
10703
|
+
if (!existsSync16(mdPath)) continue;
|
|
10649
10704
|
out.push({
|
|
10650
10705
|
name: sk.name,
|
|
10651
10706
|
category: sk.category || "",
|
|
10652
|
-
content:
|
|
10707
|
+
content: readFileSync16(mdPath, "utf-8"),
|
|
10653
10708
|
keywords: sk.keywords || []
|
|
10654
10709
|
});
|
|
10655
10710
|
}
|
|
@@ -10736,8 +10791,8 @@ var init_curator = __esm({
|
|
|
10736
10791
|
});
|
|
10737
10792
|
|
|
10738
10793
|
// src/brain/contributor.ts
|
|
10739
|
-
import { dirname as
|
|
10740
|
-
import { fileURLToPath as
|
|
10794
|
+
import { dirname as dirname12 } from "path";
|
|
10795
|
+
import { fileURLToPath as fileURLToPath10 } from "url";
|
|
10741
10796
|
var __dirname8;
|
|
10742
10797
|
var init_contributor = __esm({
|
|
10743
10798
|
"src/brain/contributor.ts"() {
|
|
@@ -10746,7 +10801,7 @@ var init_contributor = __esm({
|
|
|
10746
10801
|
init_rule_store();
|
|
10747
10802
|
init_skill_manager();
|
|
10748
10803
|
init_asset_dir();
|
|
10749
|
-
__dirname8 =
|
|
10804
|
+
__dirname8 = dirname12(fileURLToPath10(import.meta.url));
|
|
10750
10805
|
}
|
|
10751
10806
|
});
|
|
10752
10807
|
|
|
@@ -13900,8 +13955,8 @@ init_paths();
|
|
|
13900
13955
|
import { cors } from "hono/cors";
|
|
13901
13956
|
import { logger } from "hono/logger";
|
|
13902
13957
|
import net from "net";
|
|
13903
|
-
import { existsSync as
|
|
13904
|
-
import { dirname as
|
|
13958
|
+
import { existsSync as existsSync22, writeFileSync as writeFileSync7, renameSync as renameSync3, mkdirSync as mkdirSync10 } from "fs";
|
|
13959
|
+
import { dirname as dirname16 } from "path";
|
|
13905
13960
|
|
|
13906
13961
|
// src/package-json.ts
|
|
13907
13962
|
init_esm_shims();
|
|
@@ -14078,9 +14133,9 @@ var ReviewResponseSchema = z.object({
|
|
|
14078
14133
|
init_esm_shims();
|
|
14079
14134
|
init_security_policy();
|
|
14080
14135
|
init_coordinator();
|
|
14081
|
-
import { existsSync as
|
|
14136
|
+
import { existsSync as existsSync19, readFileSync as readFileSync19 } from "fs";
|
|
14082
14137
|
import { tmpdir as tmpdir3 } from "os";
|
|
14083
|
-
import { join as
|
|
14138
|
+
import { join as join19, resolve as resolve12 } from "path";
|
|
14084
14139
|
|
|
14085
14140
|
// src/engine/types.ts
|
|
14086
14141
|
init_esm_shims();
|
|
@@ -14101,10 +14156,10 @@ init_security_policy();
|
|
|
14101
14156
|
init_paths();
|
|
14102
14157
|
init_config();
|
|
14103
14158
|
import { createHash as createHash6 } from "crypto";
|
|
14104
|
-
import { existsSync as
|
|
14159
|
+
import { existsSync as existsSync18, lstatSync as lstatSync2, readdirSync as readdirSync6, readFileSync as readFileSync18, rmdirSync, unlinkSync as unlinkSync5 } from "fs";
|
|
14105
14160
|
import { tmpdir as tmpdir2 } from "os";
|
|
14106
|
-
import { join as
|
|
14107
|
-
var CACHE_DIR =
|
|
14161
|
+
import { join as join18 } from "path";
|
|
14162
|
+
var CACHE_DIR = join18(tmpdir2(), "agent_moss_policy_cache");
|
|
14108
14163
|
var cacheDirOverride = null;
|
|
14109
14164
|
function cacheDir() {
|
|
14110
14165
|
return cacheDirOverride ?? CACHE_DIR;
|
|
@@ -14113,7 +14168,7 @@ function safeCacheDir() {
|
|
|
14113
14168
|
const dir = cacheDir();
|
|
14114
14169
|
if (ensurePrivateDir(dir)) return dir;
|
|
14115
14170
|
try {
|
|
14116
|
-
const fallback =
|
|
14171
|
+
const fallback = join18(getDataRoot(), "policy_cache");
|
|
14117
14172
|
if (ensurePrivateDir(fallback)) {
|
|
14118
14173
|
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}`);
|
|
14119
14174
|
return fallback;
|
|
@@ -14135,7 +14190,7 @@ var PolicyCache = class {
|
|
|
14135
14190
|
cacheFilePath(sessionId, promptSession) {
|
|
14136
14191
|
const promptHash = createHash6("md5").update(promptSession).digest("hex").slice(0, 12);
|
|
14137
14192
|
const safeSessionId = sessionId.replace(/\//g, "_").replace(/\\/g, "_").replace(/\.\./g, "__");
|
|
14138
|
-
return
|
|
14193
|
+
return join18(safeCacheDir(), safeSessionId, `${promptHash}.toml`);
|
|
14139
14194
|
}
|
|
14140
14195
|
/** 取缓存:内存 → 文件;未命中返回 null。 */
|
|
14141
14196
|
get(sessionId, promptSession) {
|
|
@@ -14148,9 +14203,9 @@ var PolicyCache = class {
|
|
|
14148
14203
|
return hit;
|
|
14149
14204
|
}
|
|
14150
14205
|
const file = this.cacheFilePath(sessionId, promptSession);
|
|
14151
|
-
if (
|
|
14206
|
+
if (existsSync18(file) && !lstatSync2(file).isSymbolicLink()) {
|
|
14152
14207
|
try {
|
|
14153
|
-
const policy =
|
|
14208
|
+
const policy = readFileSync18(file, "utf-8");
|
|
14154
14209
|
this.remember(key, policy);
|
|
14155
14210
|
return policy;
|
|
14156
14211
|
} catch {
|
|
@@ -14166,7 +14221,7 @@ var PolicyCache = class {
|
|
|
14166
14221
|
try {
|
|
14167
14222
|
const dir = safeCacheDir();
|
|
14168
14223
|
const sub = sessionId.replace(/\//g, "_").replace(/\\/g, "_").replace(/\.\./g, "__");
|
|
14169
|
-
if (!ensurePrivateDir(
|
|
14224
|
+
if (!ensurePrivateDir(join18(dir, sub))) return;
|
|
14170
14225
|
writePrivateText(file, policy);
|
|
14171
14226
|
} catch {
|
|
14172
14227
|
}
|
|
@@ -14192,12 +14247,12 @@ var PolicyCache = class {
|
|
|
14192
14247
|
if (k.startsWith(`${sessionId}::`)) this.memory.delete(k);
|
|
14193
14248
|
}
|
|
14194
14249
|
const safeSessionId = sessionId.replace(/\//g, "_").replace(/\\/g, "_").replace(/\.\./g, "__");
|
|
14195
|
-
const dir =
|
|
14250
|
+
const dir = join18(cacheDir(), safeSessionId);
|
|
14196
14251
|
try {
|
|
14197
|
-
if (
|
|
14252
|
+
if (existsSync18(dir)) {
|
|
14198
14253
|
for (const f of readdirSync6(dir)) {
|
|
14199
14254
|
try {
|
|
14200
|
-
unlinkSync5(
|
|
14255
|
+
unlinkSync5(join18(dir, f));
|
|
14201
14256
|
} catch {
|
|
14202
14257
|
}
|
|
14203
14258
|
}
|
|
@@ -14209,11 +14264,11 @@ var PolicyCache = class {
|
|
|
14209
14264
|
clear() {
|
|
14210
14265
|
this.memory.clear();
|
|
14211
14266
|
try {
|
|
14212
|
-
if (
|
|
14267
|
+
if (existsSync18(cacheDir())) {
|
|
14213
14268
|
for (const sessionDir of readdirSync6(cacheDir())) {
|
|
14214
|
-
const dir =
|
|
14269
|
+
const dir = join18(cacheDir(), sessionDir);
|
|
14215
14270
|
try {
|
|
14216
|
-
for (const f of readdirSync6(dir)) unlinkSync5(
|
|
14271
|
+
for (const f of readdirSync6(dir)) unlinkSync5(join18(dir, f));
|
|
14217
14272
|
rmdirSync(dir);
|
|
14218
14273
|
} catch {
|
|
14219
14274
|
}
|
|
@@ -14300,7 +14355,7 @@ ${safe(policy, 2e3)}`);
|
|
|
14300
14355
|
|
|
14301
14356
|
// src/engine/analyzer.ts
|
|
14302
14357
|
import { parse as parseToml } from "smol-toml";
|
|
14303
|
-
var POLICY_OUTPUT_DIR =
|
|
14358
|
+
var POLICY_OUTPUT_DIR = join19(tmpdir3(), "agent_moss");
|
|
14304
14359
|
var policyOutputDirOverride = null;
|
|
14305
14360
|
function policyOutputDir() {
|
|
14306
14361
|
return policyOutputDirOverride ?? POLICY_OUTPUT_DIR;
|
|
@@ -14339,7 +14394,7 @@ function savePolicyToFile(sessionId, policyToml) {
|
|
|
14339
14394
|
return null;
|
|
14340
14395
|
}
|
|
14341
14396
|
const safeName = sessionId.replace(/\//g, "_").replace(/\\/g, "_").replace(/\.\./g, "__");
|
|
14342
|
-
const filepath =
|
|
14397
|
+
const filepath = join19(outDir, `${safeName}.toml`);
|
|
14343
14398
|
writePrivateText(filepath, policyToml);
|
|
14344
14399
|
return filepath;
|
|
14345
14400
|
} catch (err) {
|
|
@@ -14377,12 +14432,12 @@ function validateToml(tomlStr) {
|
|
|
14377
14432
|
function loadPolicyMappingDoc() {
|
|
14378
14433
|
const dir = resolveAssetDir(__dirname, "templates");
|
|
14379
14434
|
if (dir) {
|
|
14380
|
-
const p =
|
|
14381
|
-
if (
|
|
14435
|
+
const p = resolve12(dir, "policy_mapping.md");
|
|
14436
|
+
if (existsSync19(p)) return readFileSync19(p, "utf-8");
|
|
14382
14437
|
}
|
|
14383
14438
|
for (const cand of assetDirCandidates(__dirname, "templates")) {
|
|
14384
|
-
const p =
|
|
14385
|
-
if (
|
|
14439
|
+
const p = resolve12(cand, "policy_mapping.md");
|
|
14440
|
+
if (existsSync19(p)) return readFileSync19(p, "utf-8");
|
|
14386
14441
|
}
|
|
14387
14442
|
return "";
|
|
14388
14443
|
}
|
|
@@ -14978,9 +15033,9 @@ function poolsSnapshot() {
|
|
|
14978
15033
|
|
|
14979
15034
|
// src/routes.ts
|
|
14980
15035
|
init_metrics();
|
|
14981
|
-
import { join as
|
|
14982
|
-
import { fileURLToPath as
|
|
14983
|
-
var __dirname9 =
|
|
15036
|
+
import { join as join21, dirname as dirname14 } from "path";
|
|
15037
|
+
import { fileURLToPath as fileURLToPath11 } from "url";
|
|
15038
|
+
var __dirname9 = dirname14(fileURLToPath11(import.meta.url));
|
|
14984
15039
|
async function healthRoute(c) {
|
|
14985
15040
|
const metrics = {};
|
|
14986
15041
|
try {
|
|
@@ -15538,7 +15593,7 @@ function getBrainRuleStore() {
|
|
|
15538
15593
|
return new BrainRuleStore();
|
|
15539
15594
|
}
|
|
15540
15595
|
function getBrainSkillManager() {
|
|
15541
|
-
const dir = resolveAssetDir(__dirname9, "skills") ??
|
|
15596
|
+
const dir = resolveAssetDir(__dirname9, "skills") ?? join21(__dirname9, "..", "..", "agent_moss", "skills");
|
|
15542
15597
|
return new BrainSkillManager(dir);
|
|
15543
15598
|
}
|
|
15544
15599
|
async function brainRulesRoute(c) {
|
|
@@ -15640,13 +15695,14 @@ init_token_stats();
|
|
|
15640
15695
|
init_rules_loader();
|
|
15641
15696
|
init_asset_dir();
|
|
15642
15697
|
init_rules_mode();
|
|
15698
|
+
init_edition_scope();
|
|
15643
15699
|
import { Hono } from "hono";
|
|
15644
15700
|
import { timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
15645
|
-
import { readFileSync as
|
|
15646
|
-
import { join as
|
|
15647
|
-
import { fileURLToPath as
|
|
15648
|
-
var __dirname10 =
|
|
15649
|
-
var STATIC_DIR =
|
|
15701
|
+
import { readFileSync as readFileSync21, existsSync as existsSync21, unlinkSync as unlinkSync6 } from "fs";
|
|
15702
|
+
import { join as join22, dirname as dirname15 } from "path";
|
|
15703
|
+
import { fileURLToPath as fileURLToPath12 } from "url";
|
|
15704
|
+
var __dirname10 = dirname15(fileURLToPath12(import.meta.url));
|
|
15705
|
+
var STATIC_DIR = join22(__dirname10, "static");
|
|
15650
15706
|
var CONSOLE_TOKEN_ENV = "AGENT_MOSS_CONSOLE_TOKEN";
|
|
15651
15707
|
function getConsoleToken() {
|
|
15652
15708
|
return process.env[CONSOLE_TOKEN_ENV] || "";
|
|
@@ -15673,6 +15729,16 @@ function authMiddleware() {
|
|
|
15673
15729
|
function displayRuntimeConfig() {
|
|
15674
15730
|
return isRuntimeConfigDisabled() ? generateSourceDefaults() : loadRuntimeConfig();
|
|
15675
15731
|
}
|
|
15732
|
+
function scopedAgentRules(rc) {
|
|
15733
|
+
return Object.fromEntries(
|
|
15734
|
+
Object.entries(getAgentRules(rc)).filter(([id]) => isAllowed("agents", id))
|
|
15735
|
+
);
|
|
15736
|
+
}
|
|
15737
|
+
function scopedScenarioRules(rc) {
|
|
15738
|
+
return Object.fromEntries(
|
|
15739
|
+
Object.entries(getScenarioRules(rc)).filter(([id]) => isAllowed("scenarios", id))
|
|
15740
|
+
);
|
|
15741
|
+
}
|
|
15676
15742
|
function builtinOnlyReadOnlyMiddleware() {
|
|
15677
15743
|
return async (c, next) => {
|
|
15678
15744
|
if (isRuntimeConfigDisabled() && !["GET", "HEAD", "OPTIONS"].includes(c.req.method)) {
|
|
@@ -15688,9 +15754,9 @@ function createConsoleRouter() {
|
|
|
15688
15754
|
console2.use("/api/*", authMiddleware());
|
|
15689
15755
|
console2.use("/api/*", builtinOnlyReadOnlyMiddleware());
|
|
15690
15756
|
const serveIndex = (c) => {
|
|
15691
|
-
const indexFile =
|
|
15692
|
-
if (
|
|
15693
|
-
return c.html(
|
|
15757
|
+
const indexFile = join22(STATIC_DIR, "index.html");
|
|
15758
|
+
if (existsSync21(indexFile)) {
|
|
15759
|
+
return c.html(readFileSync21(indexFile, "utf-8"));
|
|
15694
15760
|
}
|
|
15695
15761
|
return c.html("<h1>AgentMoss Policy Console</h1><p>Frontend not built. Use /console/api/* endpoints.</p>");
|
|
15696
15762
|
};
|
|
@@ -15885,22 +15951,22 @@ function createConsoleRouter() {
|
|
|
15885
15951
|
if (!/^[A-Za-z0-9_-]+$/.test(skillId)) {
|
|
15886
15952
|
return c.json({ detail: "\u975E\u6CD5 skill id" }, 400);
|
|
15887
15953
|
}
|
|
15888
|
-
const userPath =
|
|
15954
|
+
const userPath = join22(dirname15(getRuntimeConfigPath()), "agent_moss_skills", `${skillId}.md`);
|
|
15889
15955
|
const builtinDir = resolveAssetDir(__dirname10, "skills");
|
|
15890
15956
|
const candidates = [
|
|
15891
15957
|
...isRuntimeConfigDisabled() ? [] : [userPath],
|
|
15892
|
-
builtinDir ?
|
|
15958
|
+
builtinDir ? join22(builtinDir, `${skillId}.md`) : ""
|
|
15893
15959
|
].filter(Boolean);
|
|
15894
15960
|
for (const p of candidates) {
|
|
15895
|
-
if (
|
|
15896
|
-
return c.json({ skill_id: skillId, path: p, content:
|
|
15961
|
+
if (existsSync21(p)) {
|
|
15962
|
+
return c.json({ skill_id: skillId, path: p, content: readFileSync21(p, "utf-8") });
|
|
15897
15963
|
}
|
|
15898
15964
|
}
|
|
15899
15965
|
return c.json({ detail: `skill ${skillId} \u4E0D\u5B58\u5728` }, 404);
|
|
15900
15966
|
});
|
|
15901
15967
|
console2.get("/api/agents", (c) => {
|
|
15902
15968
|
const rc = displayRuntimeConfig();
|
|
15903
|
-
return c.json({ agents:
|
|
15969
|
+
return c.json({ agents: scopedAgentRules(rc) });
|
|
15904
15970
|
});
|
|
15905
15971
|
console2.put("/api/agents/rules/enabled", async (c) => {
|
|
15906
15972
|
const b = await c.req.json();
|
|
@@ -15909,8 +15975,9 @@ function createConsoleRouter() {
|
|
|
15909
15975
|
if (b.pattern) fp.pattern = b.pattern;
|
|
15910
15976
|
if (b.keyword) fp.keyword = b.keyword;
|
|
15911
15977
|
if (b.path) fp.path = b.path;
|
|
15978
|
+
if (!isAllowed("agents", b.agent_id)) return c.json({ detail: `agent ${b.agent_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
15912
15979
|
const rc = updateAgentRuleEnabled(b.agent_id, b.category, fp, b.enabled);
|
|
15913
|
-
return c.json({ agents:
|
|
15980
|
+
return c.json({ agents: scopedAgentRules(rc) });
|
|
15914
15981
|
});
|
|
15915
15982
|
console2.post("/api/agents/rules", async (c) => {
|
|
15916
15983
|
const b = await c.req.json();
|
|
@@ -15918,8 +15985,9 @@ function createConsoleRouter() {
|
|
|
15918
15985
|
for (const [k, v] of Object.entries(b)) {
|
|
15919
15986
|
if (!["agent_id", "category"].includes(k) && v !== void 0 && v !== null) rule[k] = v;
|
|
15920
15987
|
}
|
|
15988
|
+
if (!isAllowed("agents", b.agent_id)) return c.json({ detail: `agent ${b.agent_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
15921
15989
|
const rc = addAgentRule(b.agent_id, b.category, rule);
|
|
15922
|
-
return c.json({ agents:
|
|
15990
|
+
return c.json({ agents: scopedAgentRules(rc) });
|
|
15923
15991
|
});
|
|
15924
15992
|
console2.delete("/api/agents/rules", async (c) => {
|
|
15925
15993
|
const b = await c.req.json();
|
|
@@ -15928,18 +15996,20 @@ function createConsoleRouter() {
|
|
|
15928
15996
|
if (b.pattern) fp.pattern = b.pattern;
|
|
15929
15997
|
if (b.keyword) fp.keyword = b.keyword;
|
|
15930
15998
|
if (b.path) fp.path = b.path;
|
|
15999
|
+
if (!isAllowed("agents", b.agent_id)) return c.json({ detail: `agent ${b.agent_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
15931
16000
|
const rc = deleteAgentRule(b.agent_id, b.category, fp);
|
|
15932
|
-
return c.json({ agents:
|
|
16001
|
+
return c.json({ agents: scopedAgentRules(rc) });
|
|
15933
16002
|
});
|
|
15934
16003
|
console2.put("/api/agents/categories/enabled", async (c) => {
|
|
15935
16004
|
const b = await c.req.json();
|
|
16005
|
+
if (!isAllowed("agents", b.agent_id)) return c.json({ detail: `agent ${b.agent_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
15936
16006
|
const rc = updateAgentCategoryEnabled(b.agent_id, b.category, b.enabled);
|
|
15937
|
-
return c.json({ agents:
|
|
16007
|
+
return c.json({ agents: scopedAgentRules(rc) });
|
|
15938
16008
|
});
|
|
15939
16009
|
const enabledScenarioIds = () => (process.env.AGENT_MOSS_SCENARIO || "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
15940
16010
|
console2.get("/api/scenarios", (c) => {
|
|
15941
|
-
const all =
|
|
15942
|
-
const enabled = enabledScenarioIds();
|
|
16011
|
+
const all = scopedScenarioRules(displayRuntimeConfig());
|
|
16012
|
+
const enabled = enabledScenarioIds().filter((sid) => isAllowed("scenarios", sid));
|
|
15943
16013
|
if (!enabled.length) return c.json({ scenarios: {}, enabled: [] });
|
|
15944
16014
|
const shown = {};
|
|
15945
16015
|
for (const sid of enabled) if (all[sid]) shown[sid] = all[sid];
|
|
@@ -15952,8 +16022,9 @@ function createConsoleRouter() {
|
|
|
15952
16022
|
if (b.pattern) fp.pattern = b.pattern;
|
|
15953
16023
|
if (b.keyword) fp.keyword = b.keyword;
|
|
15954
16024
|
if (b.path) fp.path = b.path;
|
|
16025
|
+
if (!isAllowed("scenarios", b.scenario_id)) return c.json({ detail: `scenario ${b.scenario_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
15955
16026
|
const rc = updateScenarioRuleEnabled(b.scenario_id, b.category, fp, b.enabled);
|
|
15956
|
-
return c.json({ scenarios:
|
|
16027
|
+
return c.json({ scenarios: scopedScenarioRules(rc), enabled: enabledScenarioIds().filter((sid) => isAllowed("scenarios", sid)) });
|
|
15957
16028
|
});
|
|
15958
16029
|
console2.post("/api/scenarios/rules", async (c) => {
|
|
15959
16030
|
const b = await c.req.json();
|
|
@@ -15961,8 +16032,9 @@ function createConsoleRouter() {
|
|
|
15961
16032
|
for (const [k, v] of Object.entries(b)) {
|
|
15962
16033
|
if (!["scenario_id", "category"].includes(k) && v !== void 0 && v !== null) rule[k] = v;
|
|
15963
16034
|
}
|
|
16035
|
+
if (!isAllowed("scenarios", b.scenario_id)) return c.json({ detail: `scenario ${b.scenario_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
15964
16036
|
const rc = addScenarioRule(b.scenario_id, b.category, rule);
|
|
15965
|
-
return c.json({ scenarios:
|
|
16037
|
+
return c.json({ scenarios: scopedScenarioRules(rc), enabled: enabledScenarioIds().filter((sid) => isAllowed("scenarios", sid)) });
|
|
15966
16038
|
});
|
|
15967
16039
|
console2.delete("/api/scenarios/rules", async (c) => {
|
|
15968
16040
|
const b = await c.req.json();
|
|
@@ -15971,13 +16043,15 @@ function createConsoleRouter() {
|
|
|
15971
16043
|
if (b.pattern) fp.pattern = b.pattern;
|
|
15972
16044
|
if (b.keyword) fp.keyword = b.keyword;
|
|
15973
16045
|
if (b.path) fp.path = b.path;
|
|
16046
|
+
if (!isAllowed("scenarios", b.scenario_id)) return c.json({ detail: `scenario ${b.scenario_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
15974
16047
|
const rc = deleteScenarioRule(b.scenario_id, b.category, fp);
|
|
15975
|
-
return c.json({ scenarios:
|
|
16048
|
+
return c.json({ scenarios: scopedScenarioRules(rc), enabled: enabledScenarioIds().filter((sid) => isAllowed("scenarios", sid)) });
|
|
15976
16049
|
});
|
|
15977
16050
|
console2.put("/api/scenarios/categories/enabled", async (c) => {
|
|
15978
16051
|
const b = await c.req.json();
|
|
16052
|
+
if (!isAllowed("scenarios", b.scenario_id)) return c.json({ detail: `scenario ${b.scenario_id} \u4E0D\u5728\u672C\u4EA4\u4ED8\u8303\u56F4\u5185` }, 404);
|
|
15979
16053
|
const rc = updateScenarioCategoryEnabled(b.scenario_id, b.category, b.enabled);
|
|
15980
|
-
return c.json({ scenarios:
|
|
16054
|
+
return c.json({ scenarios: scopedScenarioRules(rc), enabled: enabledScenarioIds().filter((sid) => isAllowed("scenarios", sid)) });
|
|
15981
16055
|
});
|
|
15982
16056
|
console2.get("/api/config", (c) => {
|
|
15983
16057
|
const rc = displayRuntimeConfig();
|
|
@@ -15990,7 +16064,7 @@ function createConsoleRouter() {
|
|
|
15990
16064
|
const defaults = generateSourceDefaults();
|
|
15991
16065
|
try {
|
|
15992
16066
|
const p = getRuntimeConfigPath();
|
|
15993
|
-
if (
|
|
16067
|
+
if (existsSync21(p)) unlinkSync6(p);
|
|
15994
16068
|
} catch {
|
|
15995
16069
|
}
|
|
15996
16070
|
return c.json(withL22View(defaults));
|
|
@@ -16049,7 +16123,7 @@ function logDeploymentSelfCheck() {
|
|
|
16049
16123
|
console.log("\u5C42\u7EA7\u5F00\u5173: " + layerKeys.map((k) => `${k}=${isLayerEnabled(k) ? "on" : "OFF"}`).join(" "));
|
|
16050
16124
|
if (!builtinOnly) {
|
|
16051
16125
|
try {
|
|
16052
|
-
const dirs = rulesDirs().filter((d) =>
|
|
16126
|
+
const dirs = rulesDirs().filter((d) => existsSync22(d));
|
|
16053
16127
|
if (dirs.length > 0) {
|
|
16054
16128
|
console.log(`\u89C4\u5219\u76EE\u5F55: ${dirs.join(", ")}\uFF08\u53EA\u8BFB\u5176\u4E2D ${OVERRIDE_FILENAME} / ${EXTRA_FILENAME}\uFF09`);
|
|
16055
16129
|
}
|
|
@@ -16292,12 +16366,12 @@ function createApp() {
|
|
|
16292
16366
|
}
|
|
16293
16367
|
async function findFreePort(startPort, host = "127.0.0.1", scan = 100) {
|
|
16294
16368
|
for (let port = startPort; port < startPort + scan; port++) {
|
|
16295
|
-
const free = await new Promise((
|
|
16369
|
+
const free = await new Promise((resolve13) => {
|
|
16296
16370
|
const server = net.createServer();
|
|
16297
16371
|
server.listen(port, host, () => {
|
|
16298
|
-
server.close(() =>
|
|
16372
|
+
server.close(() => resolve13(true));
|
|
16299
16373
|
});
|
|
16300
|
-
server.on("error", () =>
|
|
16374
|
+
server.on("error", () => resolve13(false));
|
|
16301
16375
|
});
|
|
16302
16376
|
if (free) {
|
|
16303
16377
|
if (port !== startPort) {
|
|
@@ -16342,7 +16416,7 @@ async function warmUpEngine() {
|
|
|
16342
16416
|
function writePortFile(port) {
|
|
16343
16417
|
try {
|
|
16344
16418
|
const path9 = getPortFilePath();
|
|
16345
|
-
mkdirSync10(
|
|
16419
|
+
mkdirSync10(dirname16(path9), { recursive: true });
|
|
16346
16420
|
const payload = JSON.stringify({
|
|
16347
16421
|
port,
|
|
16348
16422
|
pid: process.pid,
|
|
@@ -16388,29 +16462,29 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
|
|
|
16388
16462
|
socketPath,
|
|
16389
16463
|
close: async () => {
|
|
16390
16464
|
if (!server) return;
|
|
16391
|
-
await new Promise((
|
|
16392
|
-
server.close(() =>
|
|
16465
|
+
await new Promise((resolve13) => {
|
|
16466
|
+
server.close(() => resolve13());
|
|
16393
16467
|
});
|
|
16394
16468
|
server = null;
|
|
16395
16469
|
},
|
|
16396
16470
|
serve: async () => {
|
|
16397
16471
|
failFastOnAuthError();
|
|
16398
|
-
const { mkdirSync: mkdirSync11, existsSync:
|
|
16399
|
-
const { dirname:
|
|
16400
|
-
const dir =
|
|
16472
|
+
const { mkdirSync: mkdirSync11, existsSync: existsSync23, unlinkSync: unlinkSync7 } = await import("fs");
|
|
16473
|
+
const { dirname: dirname17 } = await import("path");
|
|
16474
|
+
const dir = dirname17(socketPath);
|
|
16401
16475
|
if (dir) {
|
|
16402
16476
|
try {
|
|
16403
16477
|
mkdirSync11(dir, { recursive: true });
|
|
16404
16478
|
} catch {
|
|
16405
16479
|
}
|
|
16406
16480
|
}
|
|
16407
|
-
if (
|
|
16481
|
+
if (existsSync23(socketPath)) {
|
|
16408
16482
|
const { connect } = await import("net");
|
|
16409
|
-
const inUse = await new Promise((
|
|
16483
|
+
const inUse = await new Promise((resolve13) => {
|
|
16410
16484
|
const probe = connect(socketPath);
|
|
16411
16485
|
const done = (v) => {
|
|
16412
16486
|
probe.destroy();
|
|
16413
|
-
|
|
16487
|
+
resolve13(v);
|
|
16414
16488
|
};
|
|
16415
16489
|
probe.once("connect", () => done(true));
|
|
16416
16490
|
probe.once("error", () => done(false));
|
|
@@ -16438,9 +16512,9 @@ async function startUnixSocketServer(socketPath = "/var/run/agent_moss/agent_mos
|
|
|
16438
16512
|
server.keepAliveTimeout = keepAliveSec() * 1e3;
|
|
16439
16513
|
} catch {
|
|
16440
16514
|
}
|
|
16441
|
-
await new Promise((
|
|
16515
|
+
await new Promise((resolve13, reject) => {
|
|
16442
16516
|
server.once("error", reject);
|
|
16443
|
-
server.listen(socketPath, () =>
|
|
16517
|
+
server.listen(socketPath, () => resolve13());
|
|
16444
16518
|
});
|
|
16445
16519
|
try {
|
|
16446
16520
|
const { chmodSync: chmodSync2 } = await import("fs");
|