@kenhkl/agent-moss 0.9.1 → 0.10.0

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # AgentMoss (TypeScript)
2
2
 
3
- AgentMoss TypeScript 实现 — 可被任意 AI Agent 调用的独立通用安全分析服务。三层防御引擎(启发式 → 逻辑规则 → LLM 语义分析),两级快速放行白名单,fail-closed 安全原则。
3
+ AgentMoss TypeScript 实现 — 可被任意 AI Agent 调用的独立通用安全分析服务。三层防御引擎(特征匹配 → 逻辑规则 → LLM 语义分析),两级快速放行白名单,fail-closed 安全原则。
4
4
 
5
5
  > 当前版本:**v0.2.1** | npm: `@kenhkl/agent-moss`
6
6
 
@@ -64,7 +64,7 @@ const result = await resp.json();
64
64
  ├── 完全安全 (15 bash 模式 + 6 action types) → 跳过 L2+L3
65
65
  └── 只读敏感 (15 bash 模式 + 8 action types) → 跳过 L3
66
66
 
67
- 层1: 启发式静态检测 (<1ms)
67
+ 层1: 特征匹配静态检测 (<1ms)
68
68
  ├── 危险命令正则: 48 个模式(rm 分级检测,sudo 只拦截危险命令,nmap/masscan/zmap 检测)
69
69
  ├── → high/critical → 直接 Deny
70
70
  ├── → 内联脚本 file_access 转层3(避免 'python -c "cat /etc/shadow"' 假阳性)
@@ -102,7 +102,7 @@ const result = await resp.json();
102
102
  | `AGENT_MOSS_LLM_TIMEOUT` | LLM 单次调用超时(秒),覆盖配置文件 `timeout.prompt1_timeout` | `40` |
103
103
  | `AGENT_MOSS_LLM_RETRIES` | LLM 调用重试次数 | `2`(共 3 次尝试) |
104
104
  | `AGENT_MOSS_DISABLE_LLM` | 设为 `1` 禁用层3 LLM | 未设置 |
105
- | `AGENT_MOSS_DISABLE_HEURISTIC` | 设为 `1` 禁用层1 启发式 | 未设置 |
105
+ | `AGENT_MOSS_DISABLE_HEURISTIC` | 设为 `1` 禁用层1 特征匹配 | 未设置 |
106
106
  | `AGENT_MOSS_DISABLE_LOGIC_RULES` | 设为 `1` 禁用层2 逻辑规则 | 未设置 |
107
107
  | `AGENT_MOSS_CUSTOM_RULES` | 自定义规则 JSON 数组 | `[]` |
108
108
 
@@ -51,29 +51,42 @@ def get_agent_extensions(agent_id: str) -> AgentExtensions | None:
51
51
  if agent_id in _cache:
52
52
  return _cache[agent_id]
53
53
 
54
- rules_file = _AGENTS_DIR / agent_id / "rules.json"
55
- if not rules_file.exists():
56
- logger.debug("agent %s 无 rules.json,走通用模式", agent_id)
57
- _cache[agent_id] = None
58
- return None
59
-
54
+ # 统一存储:优先读 runtime_config 的 agent_rules 分区(用户可在 console
55
+ # 查看/编辑/禁用/新增,享受与通用规则一致的合并升级)。回退 rules.json
56
+ # (无 runtime_config 时,如直接起服务未 seed、或旧部署)。
57
+ ext = None
60
58
  try:
61
- ext = AgentExtensions.from_json(rules_file)
62
- _cache[agent_id] = ext
63
- logger.info(
64
- "加载 agent %s 定制规则: %d cmd patterns, %d injection keywords, "
65
- "%d sensitive paths, %d builtin safe skills",
66
- agent_id,
67
- len(ext.extra_command_patterns),
68
- len(ext.extra_injection_keywords),
69
- len(ext.extra_sensitive_paths),
70
- len(ext.builtin_safe_skills),
71
- )
72
- return ext
73
- except Exception as e:
74
- logger.error("加载 agent %s rules.json 失败: %s", agent_id, e)
75
- _cache[agent_id] = None
76
- return None
59
+ from ..infra.runtime_config import has_runtime_config, load_runtime_config, get_enabled_agent_extensions
60
+ if has_runtime_config():
61
+ ext = get_enabled_agent_extensions(load_runtime_config(), agent_id)
62
+ except Exception:
63
+ pass # runtime_config 不可用时回退 rules.json
64
+
65
+ if ext is None:
66
+ rules_file = _AGENTS_DIR / agent_id / "rules.json"
67
+ if not rules_file.exists():
68
+ logger.debug("agent %s 无 rules.json,走通用模式", agent_id)
69
+ _cache[agent_id] = None
70
+ return None
71
+ try:
72
+ ext = AgentExtensions.from_json(rules_file)
73
+ except Exception as e:
74
+ logger.error("加载 agent %s rules.json 失败: %s", agent_id, e)
75
+ _cache[agent_id] = None
76
+ return None
77
+
78
+ _cache[agent_id] = ext
79
+ logger.info(
80
+ "加载 agent %s 定制规则: %d cmd patterns, %d injection keywords, "
81
+ "%d sensitive paths, %d builtin safe skills, %d builtin safe action types",
82
+ agent_id,
83
+ len(ext.extra_command_patterns),
84
+ len(ext.extra_injection_keywords),
85
+ len(ext.extra_sensitive_paths),
86
+ len(ext.builtin_safe_skills),
87
+ len(ext.builtin_safe_action_types),
88
+ )
89
+ return ext
77
90
 
78
91
 
79
92
  def clear_agent_extensions_cache() -> None:
@@ -17,6 +17,7 @@ JSON schema(agent_moss/agents/<id>/rules.json):
17
17
  "extra_injection_keywords": [InjectionKeyword...],
18
18
  "extra_sensitive_paths": [SensitivePath...],
19
19
  "builtin_safe_skills": ["xiaoo-guardian"],
20
+ "builtin_safe_action_types": ["memory-", "todomgr-", "mailbox-getEmailList"],
20
21
  "extra_skills": []
21
22
  }
22
23
  """
@@ -52,6 +53,9 @@ class AgentExtensions:
52
53
  extra_injection_keywords: list[InjectionKeyword] = field(default_factory=list)
53
54
  extra_sensitive_paths: list[SensitivePath] = field(default_factory=list)
54
55
  builtin_safe_skills: list[str] = field(default_factory=list)
56
+ # agent 专属"完全安全操作"白名单(精确名或前缀,前缀以 '-' 结尾)→ 跳过 L2+L3。
57
+ # 对齐 TS ts/src/agents/types.ts。前缀匹配(如 "memory-")走 startswith,精确名走 ==。
58
+ builtin_safe_action_types: list[str] = field(default_factory=list)
55
59
  extra_skills: list[dict] = field(default_factory=list)
56
60
 
57
61
  @classmethod
@@ -112,5 +116,6 @@ class AgentExtensions:
112
116
  extra_injection_keywords=extra_inj_keywords,
113
117
  extra_sensitive_paths=extra_sensitive_paths,
114
118
  builtin_safe_skills=data.get("builtin_safe_skills", []),
119
+ builtin_safe_action_types=data.get("builtin_safe_action_types", []),
115
120
  extra_skills=data.get("extra_skills", []),
116
121
  )
@@ -0,0 +1,29 @@
1
+ {
2
+ "agent_id": "opendesk",
3
+ "description": "OpenDesk 宿主 agent 专属安全规则。OpenDesk 工具按命名空间前缀组织(memory-/todomgr-/planning-/mailbox-/skill-manager-),其中纯数据/只读类操作走 L3 LLM 分析无意义且耗时 1~137s(实测日志),在此白名单豁免。发信类(sendEmail/replyEmail)外传风险保留 L3;skill-*/skill-manager-* 内部工具执行不经网关,本次不动。",
4
+ "extra_command_patterns": [],
5
+ "extra_injection_keywords": [],
6
+ "extra_sensitive_paths": [],
7
+ "builtin_safe_skills": [],
8
+ "builtin_safe_action_types": [
9
+ "memory-",
10
+ "todomgr-",
11
+ "planning-",
12
+ "mailbox-addMailAccount",
13
+ "mailbox-listMailAccounts",
14
+ "mailbox-removeMailAccount",
15
+ "mailbox-switchMailAccount",
16
+ "mailbox-getEmailList",
17
+ "mailbox-getEmailContent",
18
+ "mailbox-searchEmails",
19
+ "mailbox-markEmailReadStatus",
20
+ "mailbox-starEmail",
21
+ "mailbox-deleteEmail",
22
+ "mailbox-getMailSyncStatus",
23
+ "mailbox-getCalendarItems",
24
+ "mailbox-addCalendarItem",
25
+ "mailbox-deleteCalendarItem",
26
+ "mailbox-getCurrentTime"
27
+ ],
28
+ "extra_skills": []
29
+ }