mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-06 01:07:35 +00:00
refactor(security): 删除三份包级单例,共享数据统一挂 kstate
## 用户裁定
> secutiry 的实现也是错误的,所有共享数据统一挂载一个 kernel 提供的
> 挂载点上暴露。
> store也应该调用security的加密能力!
## 缺陷
security 包原有**三份**独立包级单例,各自被 InitXxx/SetXxx 装入、
各自有访问器,三份都绕开了内核统一挂载点 kstate.State:
global *Store 变量库(凭据别名) Global()
runtime *Runtime 安全防火墙 RuntimeGlobal()
globalCodec *Codec 配置落盘加密 ConfigCodec()
## 为什么单例是错的(不是"多一个全局变量")
本 agent 的 a.security 与 security.Global() 是**两个独立变量**。
正常装配下它们恰好指向同一份数据,但"恰好"不是保证:
· 测试替身:测试装单例、被测代码用 agent 字段 ⇒ 测的与跑的不是同一份
· 多 Runtime:两个内核实例/热重载时单例只有一份,另一方被覆盖
· 装配顺序:单例装晚了,先注册的 provider 就没被装饰
第三条**曾真实存在**:ProviderManager.Register 在注册时读单例套装饰器,
而 llm 模块原本不声明 FacSecurity 依赖 —— 顺序正确纯属巧合。
改动过程中判据如实报红 640 次泄漏。
## 本次做的(T3.1 + T3.3)
### 删 global *Store → 注入
变量库的唯一来源是 security 模块产出的 Runtime,经注入面分发:
AgentConfig.Security → a.security.Store() → a.secretStore()
ProviderManager.SetSecretStore
plugin.Registry.SetSecretStore → sdk.NewSecretsAPI(store)
core 的 4 处 security.Global()(output/toolcall/tooldefs/secret_tools)
改为 a.secretStore();context 与 stageHost 改为注入字段;
provider 装饰器改为读注入的 Store。
### 删 runtime *Runtime → 注入
plugin.Registry.SetSecurityRuntime → sdk.NewSecurityAPI(name, rt)
→ sdk.newEventsAPI(name, bus, rt)
Prepare 的幂等缓存改为 kernelmod/security 私有变量 ——
装配器私有,不再是任何包都能取的公开访问器。
### 顺带删死代码
security.FillToolArgs / FillPayload 生产调用者为 0(StageHost 已自己
用注入的 Store 做同一件事),连同只测它们的判据一起删除。
### llm 模块补 FacSecurity 依赖
这是**真实存在**的依赖:注册 provider 时要套出向装饰器,而装饰器需要
变量库。此前靠"单例先装了"掩盖了顺序错误。
## 判据(均经变异验证)
TestNoPackageLevelSingletons 源码扫描;加回 var global ⇒ 红(实测)
TestMustStoreDoesNotRegisterGlobally 两次 MustStore 必须得到不同实例
TestStoreEncryptsWithOwnKey 落盘无明文 + 重载可解
## 验证
go build ./... PASS
go vet ./... PASS
gofmt -l 干净
go test ./... -count=1 63 包全过,0 FAIL
干净检出(git worktree) build + 3 包测试全绿
## 未做(见 kernelplan.md T3.2)
globalCodec 收敛 + Store 复用 Codec
还牵涉 config 的 6 处调用点与**存量密文惰性迁移**,单独一步做。
TestNoPackageLevelSingletons 的禁表暂不含 globalCodec ——
半成品状态长期红的判据会被人习惯性忽略,那时它就等于不存在。
This commit is contained in:
@ -156,7 +156,12 @@ func TestRunRejectsLayerOrderViolation(t *testing.T) {
|
||||
|
||||
// ② 指名道姓:llm 缺 lua、text-memory 缺 memory/onnx、knowledge-store 缺迁移标记。
|
||||
want := map[string][]string{
|
||||
"llm": {kernelmod.FacLua},
|
||||
// ★ 2026-11-04:llm 新增了 FacSecurity 依赖。
|
||||
// 理由:ProviderManager.Register 在注册每个 provider 时套出向装饰器
|
||||
// (明文→别名),而装饰器需要变量库。单例删除后变量库必须**在注册前**
|
||||
// 由 SetSecretStore 注入 ⇒ 依赖图必须保证 security 先完成,
|
||||
// 不能靠"碰巧先装了单例"。
|
||||
"llm": {kernelmod.FacLua, kernelmod.FacSecurity},
|
||||
"text-memory": {kernelmod.FacMemory, kernelmod.FacONNX},
|
||||
"knowledge-store": {kernelmod.FacMigrated},
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"github.com/JianFeeeee/HologramAgent/internal/security"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
@ -17,8 +18,18 @@ import (
|
||||
// Module 提供多源 provider 管理。
|
||||
type Module struct{}
|
||||
|
||||
func (Module) Name() string { return "llm" }
|
||||
func (Module) Requires() []string { return []string{kernelmod.FacConfig, kernelmod.FacLua} }
|
||||
func (Module) Name() string { return "llm" }
|
||||
func (Module) Requires() []string {
|
||||
// ★ FacSecurity(2026-11-04 补):这是**真实存在**的依赖。
|
||||
//
|
||||
// ProviderManager.Register 在注册每个 provider 时套出向装饰器(明文→别名),
|
||||
// 而装饰器需要变量库。此前那份变量库来自进程级单例 security.Global() ——
|
||||
// 装配晚了也"恰好"能读到(因为单例先装了),于是顺序错误被掩盖。
|
||||
//
|
||||
// 单例删除后,变量库必须**在注册前**由 SetSecretStore 注入。声明依赖
|
||||
// FacSecurity 让依赖图保证 security 先于本模块完成,而不是靠"碰巧"。
|
||||
return []string{kernelmod.FacConfig, kernelmod.FacLua, kernelmod.FacSecurity}
|
||||
}
|
||||
func (Module) Provides() []string { return []string{kernelmod.FacLLM} }
|
||||
|
||||
func (Module) Init(ks *kstate.State) error {
|
||||
@ -30,15 +41,24 @@ func (Module) Init(ks *kstate.State) error {
|
||||
//
|
||||
// 实测:该装配**就在 initLLMProviders 内部**(不是 main 的独立语句)
|
||||
// ⇒ 声明式下它天然"紧邻",不再依赖 main 里那两行的相对位置。
|
||||
// ★ 注入必须**早于任何 provider 注册**:Register 时才会套装饰器,
|
||||
// 注入晚了先注册的那批就没被装饰(静默漏掉一条出网路径)。
|
||||
//
|
||||
// 依赖图(Requires 里的 FacSecurity)保证 ks.Security 此时非 nil。
|
||||
ks.BaseAPIKey = resolveBaseAPIKey(ks.Cfg)
|
||||
ks.LLMManager = buildProviderManager(ks.Cfg, ks.LuaVM, ks.BaseAPIKey)
|
||||
ks.LLMManager = buildProviderManager(ks.Cfg, ks.LuaVM, ks.BaseAPIKey, ks.Security)
|
||||
ks.LLMProvider = ks.LLMManager.Default()
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildProviderManager 按配置注册全部 LLM 源(原 cmd/homed 的整块平移)。
|
||||
func buildProviderManager(cfg *types.Config, luaVM *luapkg.VM, baseAPIKey string) *api.ProviderManager {
|
||||
func buildProviderManager(cfg *types.Config, luaVM *luapkg.VM, baseAPIKey string,
|
||||
secRT *security.Runtime) *api.ProviderManager {
|
||||
providerMgr := api.NewProviderManager()
|
||||
// 先注入变量库,再注册 —— 顺序反了会静默漏装饰(见 Init 的注释)。
|
||||
if secRT != nil {
|
||||
providerMgr.SetSecretStore(secRT.Store())
|
||||
}
|
||||
adapterConcurrency := map[string]int{}
|
||||
for _, src := range cfg.LLM.Sources {
|
||||
if !api.IsValidSourceConfig(src.Name, src.BaseURL, src.Model, src.Adapter) {
|
||||
|
||||
@ -307,6 +307,14 @@ func injectBaseDeps(ks *kstate.State) {
|
||||
}
|
||||
reg.SetTracker(ks.Tracker)
|
||||
reg.SetConfig(ks.Cfg)
|
||||
// 变量库注入:sdk.SecretsAPI 桥接到它(不再读包级单例)。
|
||||
// ks.Security 可能为 nil(未启用安全模块),那正是"不可用"语义。
|
||||
if ks.Security != nil {
|
||||
reg.SetSecretStore(ks.Security.Store())
|
||||
// 同一个 Runtime 也供插件的事件闸门与档位声明面使用
|
||||
// (原先是 sdk 自己读 RuntimeGlobal 包级单例)。
|
||||
reg.SetSecurityRuntime(ks.Security)
|
||||
}
|
||||
// StageHost 在挂载点里存 any(kstate 不依赖 agent/core,否则成环)。
|
||||
// 断言成 sdk.ToolSource(plugin 真正需要的只读工具视图)。
|
||||
//
|
||||
|
||||
@ -36,6 +36,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/JianFeeeee/HologramAgent/internal/config"
|
||||
"github.com/JianFeeeee/HologramAgent/internal/events"
|
||||
@ -44,6 +45,30 @@ import (
|
||||
sec "github.com/JianFeeeee/HologramAgent/internal/security"
|
||||
)
|
||||
|
||||
// 出参槽:Prepare 的幂等缓存(**装配器私有**,不对外暴露)。
|
||||
//
|
||||
// ★ 与已删除的 security.RuntimeGlobal() 的区别:
|
||||
//
|
||||
// · 那个是**公开访问器**,任何包都能取 ⇒ 成了绕过 kstate 的第二条通道
|
||||
// · 这个是**本模块私有**,只用于"Prepare 只跑一次",产物同时挂到
|
||||
// ks.Security —— 外部一律从 ks 取,没有第二条通道
|
||||
var (
|
||||
preparedMu sync.RWMutex
|
||||
prepared *sec.Runtime
|
||||
)
|
||||
|
||||
func preparedRuntime() *sec.Runtime {
|
||||
preparedMu.RLock()
|
||||
defer preparedMu.RUnlock()
|
||||
return prepared
|
||||
}
|
||||
|
||||
func setPreparedRuntime(rt *sec.Runtime) {
|
||||
preparedMu.Lock()
|
||||
prepared = rt
|
||||
preparedMu.Unlock()
|
||||
}
|
||||
|
||||
// Module 提供安全防火墙设施。
|
||||
type Module struct{}
|
||||
|
||||
@ -97,7 +122,16 @@ func init() { kernelmod.Register(Module{}) }
|
||||
//
|
||||
// 返回的 Runtime 里带着 Store 与 Gate(边界闸门)。
|
||||
func Prepare(dataDir string, cfgReg *config.ConfigRegistry) (*sec.Runtime, error) {
|
||||
if rt := sec.RuntimeGlobal(); rt != nil {
|
||||
// 幂等:Group 0 与模块 Init 都会调它,但只能装一次。
|
||||
//
|
||||
// ★ 为什么幂等状态放在**本模块**而不是 security 包(2026-11-04):
|
||||
// 原先靠 `security.RuntimeGlobal()` 判断"装过没有",那让 security 包
|
||||
// 持有一份进程级单例 —— 而单例绕开了内核统一挂载点
|
||||
// (kstate.State.Security),导致"哪一份才是权威"取决于装配顺序。
|
||||
//
|
||||
// 现在幂等状态是**装配器的私有状态**:它只保证 Prepare 只跑一次,
|
||||
// 产物交给内核挂到 ks.Security,其它模块一律从 ks 取。
|
||||
if rt := preparedRuntime(); rt != nil {
|
||||
return rt, nil
|
||||
}
|
||||
if cfgReg == nil {
|
||||
@ -107,7 +141,7 @@ func Prepare(dataDir string, cfgReg *config.ConfigRegistry) (*sec.Runtime, error
|
||||
// 显式关闭:装一个空 Runtime(所有闸门零成本透传),
|
||||
// 而不是留 nil 让调用点各自判断。
|
||||
rt := sec.NewDisabledRuntime()
|
||||
sec.SetRuntimeGlobal(rt)
|
||||
setPreparedRuntime(rt)
|
||||
log.Printf("[security] module disabled by config (core.security.enabled=false)")
|
||||
return rt, nil
|
||||
}
|
||||
@ -141,7 +175,12 @@ func Prepare(dataDir string, cfgReg *config.ConfigRegistry) (*sec.Runtime, error
|
||||
log.Printf("[security] loaded %d historical/extra key(s) (smooth rotation + embedded migration)", len(oldKeys))
|
||||
}
|
||||
|
||||
store, err := sec.InitGlobal(sec.Options{
|
||||
// ★ 2026-11-04:改用 sec.New + Load,不再走 InitGlobal(包级单例已删)。
|
||||
//
|
||||
// 变量库的唯一事实来源是**模块产出的 Runtime**(挂在 ks.Security 上),
|
||||
// 各消费方从自己的注入面取(AgentConfig.Security / ProviderManager /
|
||||
// plugin.Registry)。这条路径取代了原先"单例 + 各处 Global()"的双轨。
|
||||
store, err := sec.MustStore(sec.Options{
|
||||
Path: filepath.Join(dataDir, "security", "variables.json"),
|
||||
KeyFn: func() ([]byte, error) { return key, nil },
|
||||
OldKeys: oldKeys,
|
||||
@ -169,7 +208,7 @@ func Prepare(dataDir string, cfgReg *config.ConfigRegistry) (*sec.Runtime, error
|
||||
}
|
||||
|
||||
rt := sec.NewRuntime(store)
|
||||
sec.SetRuntimeGlobal(rt)
|
||||
setPreparedRuntime(rt)
|
||||
|
||||
// ★ 全局策略基线(部署方兵底,用户 2026-10-29 裁定选项 B)。
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user