@kenhkl/agent-moss 0.17.44 → 0.17.45
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 +13 -212
- package/dist/engine/coordinator.js +10 -195
- package/dist/server.js +10 -209
- package/dist/static/index.html +1 -1
- package/package.json +1 -1
- package/dist/static/vendor/ECHARTS_LICENSE.txt +0 -222
- package/dist/static/vendor/README.md +0 -10
- package/dist/static/vendor/echarts.min.js +0 -45
package/dist/cli.js
CHANGED
|
@@ -5063,7 +5063,7 @@ function isLLMAvailable() {
|
|
|
5063
5063
|
if (!layerEnabledOrDefault("L3_llm_analysis")) return false;
|
|
5064
5064
|
return llmConfig.apiKey.length > 0;
|
|
5065
5065
|
}
|
|
5066
|
-
async function probeLLMConnection(
|
|
5066
|
+
async function probeLLMConnection(timeoutMs = 3e4) {
|
|
5067
5067
|
if (!isLLMAvailable()) return false;
|
|
5068
5068
|
const OpenAI3 = (await import("openai")).default;
|
|
5069
5069
|
const config = getLLMConfig();
|
|
@@ -5072,7 +5072,7 @@ async function probeLLMConnection(timeoutMs2 = 3e4) {
|
|
|
5072
5072
|
const client = new OpenAI3({
|
|
5073
5073
|
baseURL: config.baseUrl,
|
|
5074
5074
|
apiKey: config.apiKey,
|
|
5075
|
-
timeout:
|
|
5075
|
+
timeout: timeoutMs,
|
|
5076
5076
|
maxRetries: 0,
|
|
5077
5077
|
defaultHeaders: Object.keys(extraHeaders).length > 0 ? extraHeaders : void 0
|
|
5078
5078
|
});
|
|
@@ -12100,7 +12100,7 @@ function maskedBaseUrl(raw) {
|
|
|
12100
12100
|
return "(unparseable)";
|
|
12101
12101
|
}
|
|
12102
12102
|
}
|
|
12103
|
-
function llmErrorDetail(err, config, attempt, maxRetries, elapsedMs, promptChars,
|
|
12103
|
+
function llmErrorDetail(err, config, attempt, maxRetries, elapsedMs, promptChars, timeoutMs) {
|
|
12104
12104
|
const chain = [];
|
|
12105
12105
|
let cur = err;
|
|
12106
12106
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -12112,7 +12112,7 @@ function llmErrorDetail(err, config, attempt, maxRetries, elapsedMs, promptChars
|
|
|
12112
12112
|
const proxy = ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"].filter((n) => process.env[n]).join(",") || "none";
|
|
12113
12113
|
const cause = err?.cause ?? err?.context;
|
|
12114
12114
|
const errno = cause?.errno ?? cause?.code ?? err?.errno ?? err?.code;
|
|
12115
|
-
return `attempt=${attempt}/${maxRetries + 1} elapsed=${(elapsedMs / 1e3).toFixed(1)}s timeout=${(
|
|
12115
|
+
return `attempt=${attempt}/${maxRetries + 1} elapsed=${(elapsedMs / 1e3).toFixed(1)}s timeout=${(timeoutMs / 1e3).toFixed(1)}s model=${config.model} provider=${config.provider || "auto"} base_url=${maskedBaseUrl(config.baseUrl)} proxy=${proxy} prompt_chars=${promptChars} errno=${errno ?? "-"} chain=${chain.join(" <- ")}`;
|
|
12116
12116
|
}
|
|
12117
12117
|
async function analyzeWithLLM(prompt, config) {
|
|
12118
12118
|
console.log(`[AgentMoss] LLM analyzing with model: ${config.model}, url: ${config.baseUrl}`);
|
|
@@ -12884,7 +12884,7 @@ ${reason.slice(0, 500)}
|
|
|
12884
12884
|
|
|
12885
12885
|
// src/brain/reviewer.ts
|
|
12886
12886
|
import OpenAI2 from "openai";
|
|
12887
|
-
function createBrainReviewer(
|
|
12887
|
+
function createBrainReviewer(timeoutMs = 1e4) {
|
|
12888
12888
|
return async (opts) => {
|
|
12889
12889
|
if (process.env.AGENT_MOSS_BRAIN_REVIEW === "0") return null;
|
|
12890
12890
|
if (!isLLMAvailable()) return null;
|
|
@@ -12894,7 +12894,7 @@ function createBrainReviewer(timeoutMs2 = 1e4) {
|
|
|
12894
12894
|
const client = new OpenAI2({
|
|
12895
12895
|
baseURL: config.baseUrl,
|
|
12896
12896
|
apiKey: config.apiKey,
|
|
12897
|
-
timeout:
|
|
12897
|
+
timeout: timeoutMs,
|
|
12898
12898
|
maxRetries: 1,
|
|
12899
12899
|
defaultHeaders: Object.keys(extraHeaders).length > 0 ? extraHeaders : void 0
|
|
12900
12900
|
});
|
|
@@ -14324,10 +14324,10 @@ function checkNeverExfilAfterPrivesc(graph) {
|
|
|
14324
14324
|
const candidates = privescInWindow(nodes, privescIdx, firstEv, windowSec);
|
|
14325
14325
|
if (candidates.length > 0) {
|
|
14326
14326
|
const pre = nodes[candidates[candidates.length - 1]];
|
|
14327
|
-
const
|
|
14327
|
+
const post = nodes[firstEv];
|
|
14328
14328
|
const path10 = [
|
|
14329
14329
|
{ sessionId: pre.sessionId, seq: pre.seq, detail: pre.actionDetail.slice(0, 60) },
|
|
14330
|
-
{ sessionId:
|
|
14330
|
+
{ sessionId: post.sessionId, seq: post.seq, detail: post.actionDetail.slice(0, 60) }
|
|
14331
14331
|
];
|
|
14332
14332
|
return [{
|
|
14333
14333
|
propertyId: "no-exfil-after-privesc",
|
|
@@ -14391,10 +14391,10 @@ function checkNeverExfilAfterCred(graph) {
|
|
|
14391
14391
|
if (isCredNode(nodes[i])) pre = nodes[i];
|
|
14392
14392
|
}
|
|
14393
14393
|
if (pre) {
|
|
14394
|
-
const
|
|
14394
|
+
const post = nodes[firstEv];
|
|
14395
14395
|
const path10 = [
|
|
14396
14396
|
{ sessionId: pre.sessionId, seq: pre.seq, detail: pre.actionDetail.slice(0, 60) },
|
|
14397
|
-
{ sessionId:
|
|
14397
|
+
{ sessionId: post.sessionId, seq: post.seq, detail: post.actionDetail.slice(0, 60) }
|
|
14398
14398
|
];
|
|
14399
14399
|
return [{
|
|
14400
14400
|
propertyId: "no-exfil-after-credential",
|
|
@@ -15893,175 +15893,6 @@ var init_formal_engine = __esm({
|
|
|
15893
15893
|
}
|
|
15894
15894
|
});
|
|
15895
15895
|
|
|
15896
|
-
// src/engine/intent-guard.ts
|
|
15897
|
-
import http from "http";
|
|
15898
|
-
function intentGuardServiceUrl() {
|
|
15899
|
-
return (getAgentMossSetting("AGENT_MOSS_INTENT_GUARD_URL", "") || "").trim();
|
|
15900
|
-
}
|
|
15901
|
-
function isIntentGuardEnabled() {
|
|
15902
|
-
return intentGuardServiceUrl() !== "";
|
|
15903
|
-
}
|
|
15904
|
-
function timeoutMs() {
|
|
15905
|
-
const raw = parseInt(getAgentMossSetting("AGENT_MOSS_INTENT_GUARD_TIMEOUT_MS", "1500") || "1500", 10);
|
|
15906
|
-
return Number.isFinite(raw) && raw > 0 ? raw : 1500;
|
|
15907
|
-
}
|
|
15908
|
-
function uncertainPolicy() {
|
|
15909
|
-
return (getAgentMossSetting("AGENT_MOSS_INTENT_GUARD_UNCERTAIN", "deny") || "deny").trim().toLowerCase();
|
|
15910
|
-
}
|
|
15911
|
-
function isIntentGuardObserveMode() {
|
|
15912
|
-
return getAgentMossSetting("AGENT_MOSS_INTENT_GUARD_OBSERVE", "") === "1";
|
|
15913
|
-
}
|
|
15914
|
-
function stripEnvironmentNoise(detail, cwd) {
|
|
15915
|
-
let s = detail.replace(
|
|
15916
|
-
/(^|\s)(?:export\s+)?PATH=(?:"[^"]*"|'[^']*'|\S+)\s*(?:&&\s*|;\s*)?/g,
|
|
15917
|
-
"$1"
|
|
15918
|
-
);
|
|
15919
|
-
const dir = (cwd ?? "").trim().replace(/\/+$/, "");
|
|
15920
|
-
if (dir && dir !== "/") {
|
|
15921
|
-
const q = dir.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
15922
|
-
s = s.replace(new RegExp(`cd\\s+["']?${q}["']?\\s*(?:&&|;)\\s*`, "g"), "");
|
|
15923
|
-
s = s.replace(new RegExp(`\\s+in\\s+${q}\\s*$`), "");
|
|
15924
|
-
s = s.split(`${dir}/`).join("");
|
|
15925
|
-
s = s.replace(new RegExp(`(?<![\\w./-])${q}(?![\\w./-])`, "g"), "");
|
|
15926
|
-
}
|
|
15927
|
-
return s.trim();
|
|
15928
|
-
}
|
|
15929
|
-
function normalizeActionDetail(actionDetail, cwd) {
|
|
15930
|
-
const s = (actionDetail ?? "").trim();
|
|
15931
|
-
if (!s.startsWith("{")) return stripEnvironmentNoise(s, cwd);
|
|
15932
|
-
let o;
|
|
15933
|
-
try {
|
|
15934
|
-
const parsed = JSON.parse(s);
|
|
15935
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return stripEnvironmentNoise(s, cwd);
|
|
15936
|
-
o = parsed;
|
|
15937
|
-
} catch {
|
|
15938
|
-
return stripEnvironmentNoise(s, cwd);
|
|
15939
|
-
}
|
|
15940
|
-
const str = (k) => typeof o[k] === "string" ? o[k] : "";
|
|
15941
|
-
const command = str("command") || str("cmd") || str("script");
|
|
15942
|
-
if (command.trim()) return stripEnvironmentNoise(command, cwd);
|
|
15943
|
-
const filePath = str("file_path");
|
|
15944
|
-
if (filePath.trim()) return stripEnvironmentNoise(`\u8BFB\u53D6\u6587\u4EF6: ${filePath}`, cwd);
|
|
15945
|
-
const path10 = str("path");
|
|
15946
|
-
const content = str("content");
|
|
15947
|
-
if (path10.trim() && content) return stripEnvironmentNoise(`\u5199\u5165\u6587\u4EF6: ${path10} \u2014 ${content.slice(0, 200)}`, cwd);
|
|
15948
|
-
const pattern = str("pattern");
|
|
15949
|
-
if (pattern.trim()) return stripEnvironmentNoise(`\u641C\u7D22: ${pattern}${path10 ? ` in ${path10}` : ""}`, cwd);
|
|
15950
|
-
if (path10.trim()) return stripEnvironmentNoise(`\u6587\u4EF6: ${path10}`, cwd);
|
|
15951
|
-
return stripEnvironmentNoise(s, cwd);
|
|
15952
|
-
}
|
|
15953
|
-
function post(url, payload, ms) {
|
|
15954
|
-
return new Promise((resolve13, reject) => {
|
|
15955
|
-
let u;
|
|
15956
|
-
try {
|
|
15957
|
-
u = new URL(url);
|
|
15958
|
-
} catch (e) {
|
|
15959
|
-
reject(e);
|
|
15960
|
-
return;
|
|
15961
|
-
}
|
|
15962
|
-
const body = Buffer.from(JSON.stringify(payload), "utf-8");
|
|
15963
|
-
const req = http.request(
|
|
15964
|
-
{
|
|
15965
|
-
hostname: u.hostname,
|
|
15966
|
-
port: u.port || 80,
|
|
15967
|
-
path: u.pathname.replace(/\/$/, "") + "/judge",
|
|
15968
|
-
method: "POST",
|
|
15969
|
-
headers: { "Content-Type": "application/json", "Content-Length": body.length },
|
|
15970
|
-
timeout: ms
|
|
15971
|
-
},
|
|
15972
|
-
(res) => {
|
|
15973
|
-
const chunks = [];
|
|
15974
|
-
res.on("data", (c) => chunks.push(c));
|
|
15975
|
-
res.on("end", () => {
|
|
15976
|
-
try {
|
|
15977
|
-
resolve13(JSON.parse(Buffer.concat(chunks).toString("utf-8")));
|
|
15978
|
-
} catch (e) {
|
|
15979
|
-
reject(e);
|
|
15980
|
-
}
|
|
15981
|
-
});
|
|
15982
|
-
}
|
|
15983
|
-
);
|
|
15984
|
-
req.on("timeout", () => {
|
|
15985
|
-
req.destroy(new Error(`timeout after ${ms}ms`));
|
|
15986
|
-
});
|
|
15987
|
-
req.on("error", reject);
|
|
15988
|
-
req.write(body);
|
|
15989
|
-
req.end();
|
|
15990
|
-
});
|
|
15991
|
-
}
|
|
15992
|
-
async function judgeIntentGuard(input) {
|
|
15993
|
-
const url = intentGuardServiceUrl();
|
|
15994
|
-
if (!url) return null;
|
|
15995
|
-
const histTexts = (input.promptHistory ?? []).map((h) => typeof h === "string" ? h : h?.text ?? "");
|
|
15996
|
-
if (!(input.promptSession || "").trim() && !histTexts.some((t) => t.trim())) return null;
|
|
15997
|
-
const cwd = input.cwd;
|
|
15998
|
-
const nextType = input.aNext?.actionType ?? "";
|
|
15999
|
-
const nextDetail = normalizeActionDetail(input.aNext?.actionDetail ?? "", cwd);
|
|
16000
|
-
const history = (input.actionHistory ?? []).map((a) => ({
|
|
16001
|
-
action_type: a.actionType ?? "",
|
|
16002
|
-
action_detail: normalizeActionDetail(a.actionDetail ?? "", cwd)
|
|
16003
|
-
}));
|
|
16004
|
-
while (history.length && history[history.length - 1].action_type === nextType && history[history.length - 1].action_detail === nextDetail) {
|
|
16005
|
-
history.pop();
|
|
16006
|
-
}
|
|
16007
|
-
const payload = {
|
|
16008
|
-
intent: input.promptSession || "",
|
|
16009
|
-
prompt_history: histTexts.map((text) => ({ text })),
|
|
16010
|
-
action_history: history,
|
|
16011
|
-
action_type: nextType,
|
|
16012
|
-
action_detail: nextDetail,
|
|
16013
|
-
// reason 也是模型输入的一部分,同样要剥掉 cwd/PATH 噪声:实测同一误拦里
|
|
16014
|
-
// action_detail 已归一成 `python3 fib.py`,但 reason 仍是
|
|
16015
|
-
// `cd /tmp/ws/runNNN && python3 fib.py` → 模型仍判 DENY。
|
|
16016
|
-
reason: stripEnvironmentNoise(input.reason || "", cwd)
|
|
16017
|
-
};
|
|
16018
|
-
let body;
|
|
16019
|
-
try {
|
|
16020
|
-
body = await post(url, payload, timeoutMs());
|
|
16021
|
-
} catch (e) {
|
|
16022
|
-
console.warn(`[AgentMoss] \u610F\u56FE\u4E00\u81F4\u6027\u670D\u52A1\u4E0D\u53EF\u7528\uFF0C\u8DF3\u8FC7 ${INTENT_GUARD_LAYER}: ${e.message}`);
|
|
16023
|
-
return null;
|
|
16024
|
-
}
|
|
16025
|
-
const rec = body;
|
|
16026
|
-
if (!rec || !["ALLOW", "DENY", "UNCERTAIN"].includes(rec.decision)) {
|
|
16027
|
-
console.warn(`[AgentMoss] \u610F\u56FE\u4E00\u81F4\u6027\u670D\u52A1\u8FD4\u56DE\u975E\u6CD5\u54CD\u5E94\uFF0C\u8DF3\u8FC7 ${INTENT_GUARD_LAYER}`);
|
|
16028
|
-
return null;
|
|
16029
|
-
}
|
|
16030
|
-
return rec;
|
|
16031
|
-
}
|
|
16032
|
-
function toIntentGuardJudgment(rec) {
|
|
16033
|
-
if (rec.decision === "ALLOW") return null;
|
|
16034
|
-
const detail = rec.detail || "";
|
|
16035
|
-
const rule = rec.rule || "";
|
|
16036
|
-
const probs = `p_allow=${(rec.p_allow ?? 0).toFixed(3)} p_deny=${(rec.p_deny ?? 0).toFixed(3)} p_uncertain=${(rec.p_uncertain ?? 0).toFixed(3)}`;
|
|
16037
|
-
if (isIntentGuardObserveMode()) {
|
|
16038
|
-
console.warn(
|
|
16039
|
-
`[AgentMoss] \u610F\u56FE\u4E00\u81F4\u6027 observe\uFF08\u53EA\u8BB0\u5F55\u4E0D\u62E6\u622A\uFF09: decision=${rec.decision} rule=${rule} ${probs} | ${detail}`
|
|
16040
|
-
);
|
|
16041
|
-
return null;
|
|
16042
|
-
}
|
|
16043
|
-
if (rec.decision === "UNCERTAIN" && uncertainPolicy() !== "deny") {
|
|
16044
|
-
return null;
|
|
16045
|
-
}
|
|
16046
|
-
const uncertain = rec.decision === "UNCERTAIN";
|
|
16047
|
-
return {
|
|
16048
|
-
riskLevel: uncertain ? "medium" : "high",
|
|
16049
|
-
riskType: uncertain ? "intent_uncertain" : "intent_deviation",
|
|
16050
|
-
reason: `\u52A8\u4F5C\u4E0E\u7528\u6237\u539F\u59CB\u610F\u56FE\u4E0D\u4E00\u81F4\uFF08${uncertain ? "\u65E0\u6CD5\u5224\u5B9A\uFF0C\u9700\u4EBA\u5DE5\u786E\u8BA4" : "\u8D8A\u6743"}\uFF09\uFF1A${detail}\uFF1B${probs}`,
|
|
16051
|
-
confidence: Math.round(100 * ((uncertain ? rec.p_uncertain : rec.p_deny) ?? 0)),
|
|
16052
|
-
rule
|
|
16053
|
-
};
|
|
16054
|
-
}
|
|
16055
|
-
var INTENT_GUARD_LAYER;
|
|
16056
|
-
var init_intent_guard = __esm({
|
|
16057
|
-
"src/engine/intent-guard.ts"() {
|
|
16058
|
-
"use strict";
|
|
16059
|
-
init_esm_shims();
|
|
16060
|
-
init_config();
|
|
16061
|
-
INTENT_GUARD_LAYER = "L2.4";
|
|
16062
|
-
}
|
|
16063
|
-
});
|
|
16064
|
-
|
|
16065
15896
|
// src/engine/coordinator.ts
|
|
16066
15897
|
var coordinator_exports = {};
|
|
16067
15898
|
__export(coordinator_exports, {
|
|
@@ -16463,28 +16294,6 @@ async function runAnalysis(input) {
|
|
|
16463
16294
|
}
|
|
16464
16295
|
}
|
|
16465
16296
|
}
|
|
16466
|
-
if (isLogicRulesEnabled() && isIntentGuardEnabled()) {
|
|
16467
|
-
const rec = await judgeIntentGuard({
|
|
16468
|
-
promptSession: input.promptSession,
|
|
16469
|
-
actionHistory: input.actionHistory.map((a) => ({ actionType: a.actionType, actionDetail: a.actionDetail })),
|
|
16470
|
-
aNext: { actionType: input.aNext.actionType, actionDetail: input.aNext.actionDetail },
|
|
16471
|
-
reason: input.reason,
|
|
16472
|
-
promptHistory: input.promptHistory,
|
|
16473
|
-
cwd: input.cwd
|
|
16474
|
-
});
|
|
16475
|
-
const hit = rec ? toIntentGuardJudgment(rec) : null;
|
|
16476
|
-
if (hit) {
|
|
16477
|
-
return {
|
|
16478
|
-
allowed: false,
|
|
16479
|
-
reason: hit.reason,
|
|
16480
|
-
riskLevel: hit.riskLevel,
|
|
16481
|
-
riskType: hit.riskType,
|
|
16482
|
-
confidence: hit.confidence,
|
|
16483
|
-
source: "intent_guard",
|
|
16484
|
-
violatedLayers: [...observedFormalLayers, INTENT_GUARD_LAYER]
|
|
16485
|
-
};
|
|
16486
|
-
}
|
|
16487
|
-
}
|
|
16488
16297
|
if (brainStore) {
|
|
16489
16298
|
const brainL2 = checkBrainRules(brainStore, {
|
|
16490
16299
|
layer: 2,
|
|
@@ -16638,7 +16447,6 @@ var init_coordinator = __esm({
|
|
|
16638
16447
|
init_asset_dir();
|
|
16639
16448
|
init_runtime_config_bridge();
|
|
16640
16449
|
init_formal_engine();
|
|
16641
|
-
init_intent_guard();
|
|
16642
16450
|
TRUSTED_SKILLS_BASE = /* @__PURE__ */ new Set([]);
|
|
16643
16451
|
_brainStore = null;
|
|
16644
16452
|
_brainExtractor = null;
|
|
@@ -18361,13 +18169,6 @@ function createConsoleRouter() {
|
|
|
18361
18169
|
};
|
|
18362
18170
|
console2.get("/", serveIndex);
|
|
18363
18171
|
console2.get("", serveIndex);
|
|
18364
|
-
console2.get("/static/vendor/echarts.min.js", (c) => {
|
|
18365
|
-
const asset = join23(STATIC_DIR, "vendor", "echarts.min.js");
|
|
18366
|
-
if (!existsSync21(asset)) return c.notFound();
|
|
18367
|
-
return c.body(readFileSync21(asset, "utf-8"), 200, {
|
|
18368
|
-
"Content-Type": "application/javascript; charset=utf-8"
|
|
18369
|
-
});
|
|
18370
|
-
});
|
|
18371
18172
|
console2.get("/api/layers", (c) => {
|
|
18372
18173
|
const rc = displayRuntimeConfig();
|
|
18373
18174
|
return c.json({ layers: rc.layers, env_overrides: getEnvOverrides(), builtin_only: isBuiltinOnly(), rules_mode: getRulesMode() });
|
|
@@ -19623,7 +19424,7 @@ async function main() {
|
|
|
19623
19424
|
break;
|
|
19624
19425
|
}
|
|
19625
19426
|
case "status": {
|
|
19626
|
-
const
|
|
19427
|
+
const http = await import("http");
|
|
19627
19428
|
const { execSync } = await import("child_process");
|
|
19628
19429
|
let systemdInfo = "";
|
|
19629
19430
|
try {
|
|
@@ -19651,7 +19452,7 @@ async function main() {
|
|
|
19651
19452
|
for (let port = 9090; port <= 9095; port++) {
|
|
19652
19453
|
try {
|
|
19653
19454
|
const data = await new Promise((resolve13, reject) => {
|
|
19654
|
-
const req =
|
|
19455
|
+
const req = http.get(`http://127.0.0.1:${port}/api/v1/health`, { timeout: 2e3 }, (res) => {
|
|
19655
19456
|
let body = "";
|
|
19656
19457
|
res.on("data", (chunk) => body += chunk);
|
|
19657
19458
|
res.on("end", () => {
|
|
@@ -19697,7 +19498,7 @@ async function main() {
|
|
|
19697
19498
|
}
|
|
19698
19499
|
try {
|
|
19699
19500
|
const data = await new Promise((resolve13, reject) => {
|
|
19700
|
-
const req =
|
|
19501
|
+
const req = http.get(`http://127.0.0.1:${foundPort}/console/api/config`, { timeout: 3e3 }, (res) => {
|
|
19701
19502
|
let body = "";
|
|
19702
19503
|
res.on("data", (chunk) => body += chunk);
|
|
19703
19504
|
res.on("end", () => {
|
|
@@ -4117,7 +4117,7 @@ function isLLMAvailable() {
|
|
|
4117
4117
|
if (!layerEnabledOrDefault("L3_llm_analysis")) return false;
|
|
4118
4118
|
return llmConfig.apiKey.length > 0;
|
|
4119
4119
|
}
|
|
4120
|
-
async function probeLLMConnection(
|
|
4120
|
+
async function probeLLMConnection(timeoutMs = 3e4) {
|
|
4121
4121
|
if (!isLLMAvailable()) return false;
|
|
4122
4122
|
const OpenAI3 = (await import("openai")).default;
|
|
4123
4123
|
const config = getLLMConfig();
|
|
@@ -4126,7 +4126,7 @@ async function probeLLMConnection(timeoutMs2 = 3e4) {
|
|
|
4126
4126
|
const client = new OpenAI3({
|
|
4127
4127
|
baseURL: config.baseUrl,
|
|
4128
4128
|
apiKey: config.apiKey,
|
|
4129
|
-
timeout:
|
|
4129
|
+
timeout: timeoutMs,
|
|
4130
4130
|
maxRetries: 0,
|
|
4131
4131
|
defaultHeaders: Object.keys(extraHeaders).length > 0 ? extraHeaders : void 0
|
|
4132
4132
|
});
|
|
@@ -11170,7 +11170,7 @@ function maskedBaseUrl(raw) {
|
|
|
11170
11170
|
return "(unparseable)";
|
|
11171
11171
|
}
|
|
11172
11172
|
}
|
|
11173
|
-
function llmErrorDetail(err, config, attempt, maxRetries, elapsedMs, promptChars,
|
|
11173
|
+
function llmErrorDetail(err, config, attempt, maxRetries, elapsedMs, promptChars, timeoutMs) {
|
|
11174
11174
|
const chain = [];
|
|
11175
11175
|
let cur = err;
|
|
11176
11176
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -11182,7 +11182,7 @@ function llmErrorDetail(err, config, attempt, maxRetries, elapsedMs, promptChars
|
|
|
11182
11182
|
const proxy = ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"].filter((n) => process.env[n]).join(",") || "none";
|
|
11183
11183
|
const cause = err?.cause ?? err?.context;
|
|
11184
11184
|
const errno = cause?.errno ?? cause?.code ?? err?.errno ?? err?.code;
|
|
11185
|
-
return `attempt=${attempt}/${maxRetries + 1} elapsed=${(elapsedMs / 1e3).toFixed(1)}s timeout=${(
|
|
11185
|
+
return `attempt=${attempt}/${maxRetries + 1} elapsed=${(elapsedMs / 1e3).toFixed(1)}s timeout=${(timeoutMs / 1e3).toFixed(1)}s model=${config.model} provider=${config.provider || "auto"} base_url=${maskedBaseUrl(config.baseUrl)} proxy=${proxy} prompt_chars=${promptChars} errno=${errno ?? "-"} chain=${chain.join(" <- ")}`;
|
|
11186
11186
|
}
|
|
11187
11187
|
async function analyzeWithLLM(prompt, config) {
|
|
11188
11188
|
console.log(`[AgentMoss] LLM analyzing with model: ${config.model}, url: ${config.baseUrl}`);
|
|
@@ -11940,7 +11940,7 @@ var REVIEW_PROMPT = `\u4F60\u662F AgentMoss \u5B89\u5168\u89C4\u5219\u5BA1\u6838
|
|
|
11940
11940
|
|
|
11941
11941
|
## \u8F93\u51FA JSON\uFF08\u53EA\u8F93\u51FA JSON\uFF0C\u65E0\u5176\u4ED6\u6587\u5B57\uFF09
|
|
11942
11942
|
{"should_keep": true/false, "confidence": 0-100, "reason": "\u7B80\u77ED\u7406\u7531\uFF08\u4E2D\u6587\uFF09", "suggested_rule": "\u82E5\u4FDD\u7559\u5219\u8F93\u51FA\u5EFA\u8BAE\u6B63\u5219\uFF0C\u5426\u5219\u7A7A\u4E32", "duplicate_of": "\u82E5\u91CD\u590D\u5219\u8BF4\u660E\u662F\u54EA\u4E2A\uFF0C\u5426\u5219\u7A7A\u4E32"}`;
|
|
11943
|
-
function createBrainReviewer(
|
|
11943
|
+
function createBrainReviewer(timeoutMs = 1e4) {
|
|
11944
11944
|
return async (opts) => {
|
|
11945
11945
|
if (process.env.AGENT_MOSS_BRAIN_REVIEW === "0") return null;
|
|
11946
11946
|
if (!isLLMAvailable()) return null;
|
|
@@ -11950,7 +11950,7 @@ function createBrainReviewer(timeoutMs2 = 1e4) {
|
|
|
11950
11950
|
const client = new OpenAI2({
|
|
11951
11951
|
baseURL: config.baseUrl,
|
|
11952
11952
|
apiKey: config.apiKey,
|
|
11953
|
-
timeout:
|
|
11953
|
+
timeout: timeoutMs,
|
|
11954
11954
|
maxRetries: 1,
|
|
11955
11955
|
defaultHeaders: Object.keys(extraHeaders).length > 0 ? extraHeaders : void 0
|
|
11956
11956
|
});
|
|
@@ -13054,10 +13054,10 @@ function checkNeverExfilAfterPrivesc(graph) {
|
|
|
13054
13054
|
const candidates = privescInWindow(nodes, privescIdx, firstEv, windowSec);
|
|
13055
13055
|
if (candidates.length > 0) {
|
|
13056
13056
|
const pre = nodes[candidates[candidates.length - 1]];
|
|
13057
|
-
const
|
|
13057
|
+
const post = nodes[firstEv];
|
|
13058
13058
|
const path9 = [
|
|
13059
13059
|
{ sessionId: pre.sessionId, seq: pre.seq, detail: pre.actionDetail.slice(0, 60) },
|
|
13060
|
-
{ sessionId:
|
|
13060
|
+
{ sessionId: post.sessionId, seq: post.seq, detail: post.actionDetail.slice(0, 60) }
|
|
13061
13061
|
];
|
|
13062
13062
|
return [{
|
|
13063
13063
|
propertyId: "no-exfil-after-privesc",
|
|
@@ -13121,10 +13121,10 @@ function checkNeverExfilAfterCred(graph) {
|
|
|
13121
13121
|
if (isCredNode(nodes[i])) pre = nodes[i];
|
|
13122
13122
|
}
|
|
13123
13123
|
if (pre) {
|
|
13124
|
-
const
|
|
13124
|
+
const post = nodes[firstEv];
|
|
13125
13125
|
const path9 = [
|
|
13126
13126
|
{ sessionId: pre.sessionId, seq: pre.seq, detail: pre.actionDetail.slice(0, 60) },
|
|
13127
|
-
{ sessionId:
|
|
13127
|
+
{ sessionId: post.sessionId, seq: post.seq, detail: post.actionDetail.slice(0, 60) }
|
|
13128
13128
|
];
|
|
13129
13129
|
return [{
|
|
13130
13130
|
propertyId: "no-exfil-after-credential",
|
|
@@ -13447,169 +13447,6 @@ import * as os4 from "os";
|
|
|
13447
13447
|
import * as path8 from "path";
|
|
13448
13448
|
import { createHash as createHash4 } from "crypto";
|
|
13449
13449
|
|
|
13450
|
-
// src/engine/intent-guard.ts
|
|
13451
|
-
init_esm_shims();
|
|
13452
|
-
init_config();
|
|
13453
|
-
import http from "http";
|
|
13454
|
-
var INTENT_GUARD_LAYER = "L2.4";
|
|
13455
|
-
function intentGuardServiceUrl() {
|
|
13456
|
-
return (getAgentMossSetting("AGENT_MOSS_INTENT_GUARD_URL", "") || "").trim();
|
|
13457
|
-
}
|
|
13458
|
-
function isIntentGuardEnabled() {
|
|
13459
|
-
return intentGuardServiceUrl() !== "";
|
|
13460
|
-
}
|
|
13461
|
-
function timeoutMs() {
|
|
13462
|
-
const raw = parseInt(getAgentMossSetting("AGENT_MOSS_INTENT_GUARD_TIMEOUT_MS", "1500") || "1500", 10);
|
|
13463
|
-
return Number.isFinite(raw) && raw > 0 ? raw : 1500;
|
|
13464
|
-
}
|
|
13465
|
-
function uncertainPolicy() {
|
|
13466
|
-
return (getAgentMossSetting("AGENT_MOSS_INTENT_GUARD_UNCERTAIN", "deny") || "deny").trim().toLowerCase();
|
|
13467
|
-
}
|
|
13468
|
-
function isIntentGuardObserveMode() {
|
|
13469
|
-
return getAgentMossSetting("AGENT_MOSS_INTENT_GUARD_OBSERVE", "") === "1";
|
|
13470
|
-
}
|
|
13471
|
-
function stripEnvironmentNoise(detail, cwd) {
|
|
13472
|
-
let s = detail.replace(
|
|
13473
|
-
/(^|\s)(?:export\s+)?PATH=(?:"[^"]*"|'[^']*'|\S+)\s*(?:&&\s*|;\s*)?/g,
|
|
13474
|
-
"$1"
|
|
13475
|
-
);
|
|
13476
|
-
const dir = (cwd ?? "").trim().replace(/\/+$/, "");
|
|
13477
|
-
if (dir && dir !== "/") {
|
|
13478
|
-
const q = dir.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
13479
|
-
s = s.replace(new RegExp(`cd\\s+["']?${q}["']?\\s*(?:&&|;)\\s*`, "g"), "");
|
|
13480
|
-
s = s.replace(new RegExp(`\\s+in\\s+${q}\\s*$`), "");
|
|
13481
|
-
s = s.split(`${dir}/`).join("");
|
|
13482
|
-
s = s.replace(new RegExp(`(?<![\\w./-])${q}(?![\\w./-])`, "g"), "");
|
|
13483
|
-
}
|
|
13484
|
-
return s.trim();
|
|
13485
|
-
}
|
|
13486
|
-
function normalizeActionDetail(actionDetail, cwd) {
|
|
13487
|
-
const s = (actionDetail ?? "").trim();
|
|
13488
|
-
if (!s.startsWith("{")) return stripEnvironmentNoise(s, cwd);
|
|
13489
|
-
let o;
|
|
13490
|
-
try {
|
|
13491
|
-
const parsed = JSON.parse(s);
|
|
13492
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return stripEnvironmentNoise(s, cwd);
|
|
13493
|
-
o = parsed;
|
|
13494
|
-
} catch {
|
|
13495
|
-
return stripEnvironmentNoise(s, cwd);
|
|
13496
|
-
}
|
|
13497
|
-
const str = (k) => typeof o[k] === "string" ? o[k] : "";
|
|
13498
|
-
const command = str("command") || str("cmd") || str("script");
|
|
13499
|
-
if (command.trim()) return stripEnvironmentNoise(command, cwd);
|
|
13500
|
-
const filePath = str("file_path");
|
|
13501
|
-
if (filePath.trim()) return stripEnvironmentNoise(`\u8BFB\u53D6\u6587\u4EF6: ${filePath}`, cwd);
|
|
13502
|
-
const path9 = str("path");
|
|
13503
|
-
const content = str("content");
|
|
13504
|
-
if (path9.trim() && content) return stripEnvironmentNoise(`\u5199\u5165\u6587\u4EF6: ${path9} \u2014 ${content.slice(0, 200)}`, cwd);
|
|
13505
|
-
const pattern = str("pattern");
|
|
13506
|
-
if (pattern.trim()) return stripEnvironmentNoise(`\u641C\u7D22: ${pattern}${path9 ? ` in ${path9}` : ""}`, cwd);
|
|
13507
|
-
if (path9.trim()) return stripEnvironmentNoise(`\u6587\u4EF6: ${path9}`, cwd);
|
|
13508
|
-
return stripEnvironmentNoise(s, cwd);
|
|
13509
|
-
}
|
|
13510
|
-
function post(url, payload, ms) {
|
|
13511
|
-
return new Promise((resolve12, reject) => {
|
|
13512
|
-
let u;
|
|
13513
|
-
try {
|
|
13514
|
-
u = new URL(url);
|
|
13515
|
-
} catch (e) {
|
|
13516
|
-
reject(e);
|
|
13517
|
-
return;
|
|
13518
|
-
}
|
|
13519
|
-
const body = Buffer.from(JSON.stringify(payload), "utf-8");
|
|
13520
|
-
const req = http.request(
|
|
13521
|
-
{
|
|
13522
|
-
hostname: u.hostname,
|
|
13523
|
-
port: u.port || 80,
|
|
13524
|
-
path: u.pathname.replace(/\/$/, "") + "/judge",
|
|
13525
|
-
method: "POST",
|
|
13526
|
-
headers: { "Content-Type": "application/json", "Content-Length": body.length },
|
|
13527
|
-
timeout: ms
|
|
13528
|
-
},
|
|
13529
|
-
(res) => {
|
|
13530
|
-
const chunks = [];
|
|
13531
|
-
res.on("data", (c) => chunks.push(c));
|
|
13532
|
-
res.on("end", () => {
|
|
13533
|
-
try {
|
|
13534
|
-
resolve12(JSON.parse(Buffer.concat(chunks).toString("utf-8")));
|
|
13535
|
-
} catch (e) {
|
|
13536
|
-
reject(e);
|
|
13537
|
-
}
|
|
13538
|
-
});
|
|
13539
|
-
}
|
|
13540
|
-
);
|
|
13541
|
-
req.on("timeout", () => {
|
|
13542
|
-
req.destroy(new Error(`timeout after ${ms}ms`));
|
|
13543
|
-
});
|
|
13544
|
-
req.on("error", reject);
|
|
13545
|
-
req.write(body);
|
|
13546
|
-
req.end();
|
|
13547
|
-
});
|
|
13548
|
-
}
|
|
13549
|
-
async function judgeIntentGuard(input) {
|
|
13550
|
-
const url = intentGuardServiceUrl();
|
|
13551
|
-
if (!url) return null;
|
|
13552
|
-
const histTexts = (input.promptHistory ?? []).map((h) => typeof h === "string" ? h : h?.text ?? "");
|
|
13553
|
-
if (!(input.promptSession || "").trim() && !histTexts.some((t) => t.trim())) return null;
|
|
13554
|
-
const cwd = input.cwd;
|
|
13555
|
-
const nextType = input.aNext?.actionType ?? "";
|
|
13556
|
-
const nextDetail = normalizeActionDetail(input.aNext?.actionDetail ?? "", cwd);
|
|
13557
|
-
const history = (input.actionHistory ?? []).map((a) => ({
|
|
13558
|
-
action_type: a.actionType ?? "",
|
|
13559
|
-
action_detail: normalizeActionDetail(a.actionDetail ?? "", cwd)
|
|
13560
|
-
}));
|
|
13561
|
-
while (history.length && history[history.length - 1].action_type === nextType && history[history.length - 1].action_detail === nextDetail) {
|
|
13562
|
-
history.pop();
|
|
13563
|
-
}
|
|
13564
|
-
const payload = {
|
|
13565
|
-
intent: input.promptSession || "",
|
|
13566
|
-
prompt_history: histTexts.map((text) => ({ text })),
|
|
13567
|
-
action_history: history,
|
|
13568
|
-
action_type: nextType,
|
|
13569
|
-
action_detail: nextDetail,
|
|
13570
|
-
// reason 也是模型输入的一部分,同样要剥掉 cwd/PATH 噪声:实测同一误拦里
|
|
13571
|
-
// action_detail 已归一成 `python3 fib.py`,但 reason 仍是
|
|
13572
|
-
// `cd /tmp/ws/runNNN && python3 fib.py` → 模型仍判 DENY。
|
|
13573
|
-
reason: stripEnvironmentNoise(input.reason || "", cwd)
|
|
13574
|
-
};
|
|
13575
|
-
let body;
|
|
13576
|
-
try {
|
|
13577
|
-
body = await post(url, payload, timeoutMs());
|
|
13578
|
-
} catch (e) {
|
|
13579
|
-
console.warn(`[AgentMoss] \u610F\u56FE\u4E00\u81F4\u6027\u670D\u52A1\u4E0D\u53EF\u7528\uFF0C\u8DF3\u8FC7 ${INTENT_GUARD_LAYER}: ${e.message}`);
|
|
13580
|
-
return null;
|
|
13581
|
-
}
|
|
13582
|
-
const rec = body;
|
|
13583
|
-
if (!rec || !["ALLOW", "DENY", "UNCERTAIN"].includes(rec.decision)) {
|
|
13584
|
-
console.warn(`[AgentMoss] \u610F\u56FE\u4E00\u81F4\u6027\u670D\u52A1\u8FD4\u56DE\u975E\u6CD5\u54CD\u5E94\uFF0C\u8DF3\u8FC7 ${INTENT_GUARD_LAYER}`);
|
|
13585
|
-
return null;
|
|
13586
|
-
}
|
|
13587
|
-
return rec;
|
|
13588
|
-
}
|
|
13589
|
-
function toIntentGuardJudgment(rec) {
|
|
13590
|
-
if (rec.decision === "ALLOW") return null;
|
|
13591
|
-
const detail = rec.detail || "";
|
|
13592
|
-
const rule = rec.rule || "";
|
|
13593
|
-
const probs = `p_allow=${(rec.p_allow ?? 0).toFixed(3)} p_deny=${(rec.p_deny ?? 0).toFixed(3)} p_uncertain=${(rec.p_uncertain ?? 0).toFixed(3)}`;
|
|
13594
|
-
if (isIntentGuardObserveMode()) {
|
|
13595
|
-
console.warn(
|
|
13596
|
-
`[AgentMoss] \u610F\u56FE\u4E00\u81F4\u6027 observe\uFF08\u53EA\u8BB0\u5F55\u4E0D\u62E6\u622A\uFF09: decision=${rec.decision} rule=${rule} ${probs} | ${detail}`
|
|
13597
|
-
);
|
|
13598
|
-
return null;
|
|
13599
|
-
}
|
|
13600
|
-
if (rec.decision === "UNCERTAIN" && uncertainPolicy() !== "deny") {
|
|
13601
|
-
return null;
|
|
13602
|
-
}
|
|
13603
|
-
const uncertain = rec.decision === "UNCERTAIN";
|
|
13604
|
-
return {
|
|
13605
|
-
riskLevel: uncertain ? "medium" : "high",
|
|
13606
|
-
riskType: uncertain ? "intent_uncertain" : "intent_deviation",
|
|
13607
|
-
reason: `\u52A8\u4F5C\u4E0E\u7528\u6237\u539F\u59CB\u610F\u56FE\u4E0D\u4E00\u81F4\uFF08${uncertain ? "\u65E0\u6CD5\u5224\u5B9A\uFF0C\u9700\u4EBA\u5DE5\u786E\u8BA4" : "\u8D8A\u6743"}\uFF09\uFF1A${detail}\uFF1B${probs}`,
|
|
13608
|
-
confidence: Math.round(100 * ((uncertain ? rec.p_uncertain : rec.p_deny) ?? 0)),
|
|
13609
|
-
rule
|
|
13610
|
-
};
|
|
13611
|
-
}
|
|
13612
|
-
|
|
13613
13450
|
// src/engine/coordinator.ts
|
|
13614
13451
|
var TRUSTED_SKILLS_BASE = /* @__PURE__ */ new Set([]);
|
|
13615
13452
|
function whitelistTier(command, fullySafe, readonlySensitive) {
|
|
@@ -14009,28 +13846,6 @@ async function runAnalysis(input) {
|
|
|
14009
13846
|
}
|
|
14010
13847
|
}
|
|
14011
13848
|
}
|
|
14012
|
-
if (isLogicRulesEnabled() && isIntentGuardEnabled()) {
|
|
14013
|
-
const rec = await judgeIntentGuard({
|
|
14014
|
-
promptSession: input.promptSession,
|
|
14015
|
-
actionHistory: input.actionHistory.map((a) => ({ actionType: a.actionType, actionDetail: a.actionDetail })),
|
|
14016
|
-
aNext: { actionType: input.aNext.actionType, actionDetail: input.aNext.actionDetail },
|
|
14017
|
-
reason: input.reason,
|
|
14018
|
-
promptHistory: input.promptHistory,
|
|
14019
|
-
cwd: input.cwd
|
|
14020
|
-
});
|
|
14021
|
-
const hit = rec ? toIntentGuardJudgment(rec) : null;
|
|
14022
|
-
if (hit) {
|
|
14023
|
-
return {
|
|
14024
|
-
allowed: false,
|
|
14025
|
-
reason: hit.reason,
|
|
14026
|
-
riskLevel: hit.riskLevel,
|
|
14027
|
-
riskType: hit.riskType,
|
|
14028
|
-
confidence: hit.confidence,
|
|
14029
|
-
source: "intent_guard",
|
|
14030
|
-
violatedLayers: [...observedFormalLayers, INTENT_GUARD_LAYER]
|
|
14031
|
-
};
|
|
14032
|
-
}
|
|
14033
|
-
}
|
|
14034
13849
|
if (brainStore) {
|
|
14035
13850
|
const brainL2 = checkBrainRules(brainStore, {
|
|
14036
13851
|
layer: 2,
|