Files
HomeAgent/internal/plugins/all.go
JianFeeeee 81a0f5f598 feat(security): 插件按工具声明内容策略 + 目录名统一
## 一、插件声明层(用户裁定)

> 「敏感词策略也应该是插件声明啊,每个插件声明自己轮次以及自己工具的
>   敏感词策略」

SecurityProfile 新增两个字段:

  ToolSensitiveWords map[string][]string  // key=工具名模式(尾 *),value=词表
  DetectInjection    bool                 // 开启内核内置注入迹象识别

语义是**追加不覆盖**:整轮 SensitiveWords 仍然生效。
取宽不取窄的理由:当前处置是 B 档(标记+警示,不阻断),
多标一条只是多一句警示,漏标则是安全缺口。

## 二、修掉我自己造的「莲藕式分层」

前一版把 *security.Round 从 runInputTask 一路搬到工具链
(prepareInputTask(evt, round) → TaskFrame.SecRound →
executeToolCallOutcomeWithRound),并在 rtchild 里另补一条。

那是把**分层策略实现成跨层传参**:每多一条工具执行路径就要求调用方
多传一次句柄。profileActivator 的设计注释早就写明了正路:

> 工具调用发生在 LLM 决定之后,此时插件已不在调用栈上。
> 唯一可靠的传递方式是**内核在输入进来时记住**,在该轮的工具判定里使用。

改为 security 自取:

  Runtime.ScanContentForAgent(agentID, isChild, ins, text)
    → ForAgent(agentID, isChild)   // per-agent 视图,天然不串档
    → EffectiveProfile()           // session > longtime
    → 合并 全局词表 + 档位词表 + 工具词表 + DetectInjection

core 只递交**事实**(我是谁 / 哪个工具 / 什么文本),不认识 Round。
与 CheckToolForAgent 同一套身份推断,不引入第二个入口语义。
TaskFrame.SecRound / executeToolCallOutcomeWithRound 全部撤除。

## 三、目录名统一

third_party/homeagent-sdk → third_party/hologramagentsdk

module 与远端仓早已是 hologramagentsdk,只剩物理目录是遗留名。
同步改了 go.mod replace、构建/打包/部署脚本、5 处测试里的分段路径。
注意:这里改的是**主仓 vendored 的 SDK 源码路径**,example/ 由 SDK
独立仓自己跟踪,主仓不涉及。

## 判据

TestToolSensitiveWordsAppendOnlyForThatTool   词表只对目标工具生效 + 追加语义
TestToolSensitiveWordsUseAgentScope           security 按 agent scope 自取生效
(变异验证:删掉 ForAgent 查询 → 红)

## 验证

	go build ./...                 → 0
	go vet ./...                   → 0
	go test ./... -count=1 -race   → 62 ok / 0 FAIL / 0 DATA RACE
	gofmt -l internal/             → 干净
2026-11-03 09:51:11 +08:00

109 lines
6.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package plugins
// 编译期内置插件清单。
//
// ★ 顺序无关(各插件在 init() 里自注册),但**排序保持一致**:
// 这样 diff 里能一眼看出增删,而不是被 import 重排掩盖。
//
// ## timer 已移出本清单(2026-10-19)
//
// timer 原先是这里的编译器内置插件(internal/plugins/timer + RegisterFactory),
// 后果是改一行定时器逻辑要重编整个 homed(87MB)并重启内核 ——
// 而它是**最该独立演进**的部件之一:有自己的持久化状态、自己的触发语义、
// 自己的并发模型,却被绑死在主进程的发版节奏上。
//
// 现在它作为**外部插件**(plugins/timer,.hmap 安装)运行:
// - 换实现只需重装插件 + 重启单个插件进程,不动主进程;
// - 插件崩了自己被拉起,不影响 agent 主循环;
// - 工具名 / 配置表(config_timer)/ 注入通道(timer)保持不变,
// 模型侧与已设的定时器都无需迁移。
//
// ⚠️ 不要再把 timer 加回这里:父子两份同时存在会导致同名工具注册冲突,
// 而「谁赢」取决于加载顺序 —— 那是最难排查的一类故障。
//
// ## 判定标准:只有「必须用内部 SDK 能力」的才留在本清单(2026-10-19)
//
// 内部 SDK(internal/sdk)比公开 SDK(third_party/hologramagentsdk/sdk)
// 多出一批**不对外授予**的能力面。外部插件用的是 proc 桥的收窄能力集,
// 这些接口在类型层就不存在(见 internal/plugin/proc/capability.go 的
// withheldCapabilities)。所以:
//
// 插件需要 withheld 里的能力 ⇒ **必须内置**(外置会静默缺功能)
// 插件只用公开 SDK 能力 ⇒ **应当外置**(.hmap 安装,独立演进)
//
// 正例(留内置,各有非它不可的理由):
// cli / webui TrackerAPI/StatusAPI/SupervisorAPI/AdapterAPI
// healthcheck SelftestAPI(能构造虚拟内核实例)
// pluginmgr failback 恢复链必需(webui/pluginmgr/recoverydiag)
// seq ToolAPI(工具表直接操作)
// remotedevice RegisterChannel(内核 IO 通道注册面)
// clawhubadapter InjectInputSync(OC 主动注入)
// agentcli ★ EventPublish —— 见下
// cfgmgr / skillmgr / seemedia (保留)
//
// 反例(已外置,因为它们只要公开能力):
// cmd 仅 internal/plugin 的 RegisterFactory → 换成 NewPluginFactory 即可
// localuse 仅 RegisterTool + Settings
// mcp 仅 RegisterTool + Settings + RegisterStage
//
// ★ 终审翻案(2026-10-19,逐方法对照公开 SDK 后加回两个):
// kbtree KnowledgeAPI.Subtree/CategoryCounts/SearchIn/Categories ——
// 公开 KnowledgeAPI 只有 Search/Add/List,且公开 SDK 没有
// KnowledgeTreeView/KnowledgeCategoryCount 类型 ⇒ 必须内置
// multimodal LLMAPI.Chat + LLMMessage/LLMContentBlock(多模态消息)——
// 公开 LLMAPI 只有 ListSources/SetSource/CurrentSource,
// 没有 Chat,也没有消息类型 ⇒ 必须内置
// (agentcli 的 EventPublish 同理,见上)
//
// 教训:判「只用公开 SDK」不能只 grep 方法名前缀,要**逐方法对照公开接口**。
// s.Knowledge() / s.LLM() 两边同名,但接口面不同;粗看就会误判成可外置。
//
// ## ★ agentcli 为什么不能外置(2026-10-19 核实)
//
// 它靠 s.Publish(&sdk.Event{Type: EventTerminalOutput, ...}) 上报
// 终端输出与生命周期(创建/关闭/退出)。而 **EventPublish 在
// withheldCapabilities 里被明确扣留**,公开 SDK 连 Publish 方法都没有 ——
// 外置后终端输出会静默消失:WebUI 的终端面板、CLI 的 /terminal,
// 全都失去数据源,而且**不报错**。
//
// 想让 agentcli 外置,前提是先把「终端输出」从事件改为公开的输出通道
// (RegisterOutputChannel 是公开能力)。在那之前必须内置。
// ## ai_image / files 已移出本清单(2026-10-19)
//
// 与 timer 同理:它们是**纯工具型**插件(agi_image_generate / files_read 等),
// 只在该被调用时做事,没有任何「外部事件驱动主动注入」的行为,
// 因此没有必须常驻的理由,也没有随内核发版的必要。
//
// 二者都由 SDK 仓 `example/` 下的同版本外部插件提供(ai_image 1.3.0、
// files 1.0.0),工具名与配置表不变(config_ai_image / config_files),
// 存量配置无需迁移。
//
// ⚠️ 同一个坑:不要为「怕插件没装」而把内置版加回来。两份并存时
// loadOne 先查 factories ⇒ 内置版永远赢,外部版**装了也不生效**,
// 而且 plugin_status 会显示 builtin=true(见 PLUGIN_DEV.md 的同款说明)。
//
// ## kbtree 已归位到本清单(2026-10-19)
//
// 它本就是一个注册了 RegisterFactory 的编译期内置插件,却一直被
// `cmd/homed/main.go` 单独空白导入 —— 绕过了「内置插件清单」这个唯一入口。
// 后果:任何按 all.go 盘点内置插件的工具/文档都会漏掉它(本次文档校正
// 就踩了这个坑:先把数量写成 17,实际运行时是 18)。
// 归位后 main.go 不再单独导入它。
import (
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/agentcli"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/cfgmgr"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/clawhubadapter"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/cli"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/healthcheck"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/kbtree"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/multimodal"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/pluginmgr"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/remotedevice"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/seemedia"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/seq"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/skillmgr"
_ "github.com/JianFeeeee/HologramAgent/internal/plugins/webui"
)