mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-05 16:57:32 +00:00
feat: 段页式记忆流转、coldcontext 重命名与安全治理
记忆层: - MemoryBlock 承载 FrameTS/PageSeq/PageKind/BlockRole/CallID - coldcontext 归档与回填按完整页传递,工具调用参数和配对键不丢失 - 召回先估算总 token,超预算时按全局页相似度标记 inactive - inactive 仅是 context 渲染期状态,不落盘、不进入 coldcontext,Prune 后重置 - Peek/确认/Remove 两阶段召回,失败保留冷上下文副本 - stablemem 记录 last_recalled_at,删块连带清理边 命名: - internal/memory/document → internal/memory/coldcontext - Doc/DocEvent/DocHit → ColdDoc/ColdEvent/ColdHit - ContextToColdContext/RecentColdContexts/FindColdContexts - 保留既有 JSON 标签、doc_ 文件前缀与 doc_count 状态键,兼容存量数据 安全治理: - 工具策略门禁、安全配置、事件总线发布者身份与入站脱敏 - 声明式 SecurityAPI 与 QQ owner/guest profile - 日志保留策略与相关 e2e/回归判据 验证:go build ./...、go vet ./...、go test ./...(62 包); 生产副本 e2e:7013 块迁移后 ID/向量/指纹 0 变化、0 缺失。
This commit is contained in:
@ -124,6 +124,11 @@ var methodCapability = map[string]Capability{
|
||||
MethodSecretsFill: CapCore,
|
||||
MethodSecretsEncrypt: CapCore,
|
||||
MethodSecretsDecrypt: CapCore,
|
||||
// 策略声明是 CapCore:任何插件都只能声明**自己名下**的档位
|
||||
//(Owner 由内核填),所以它不构成越权面;限制在"声明别人的档位"
|
||||
// 那一层由 internal/sdk/security_impl.go 拒绝。
|
||||
MethodSecurityDeclareProfile: CapCore,
|
||||
MethodSecurityDeclareProfiles: CapCore,
|
||||
// 生命周期自述(插件声明自己是否可自动重启)
|
||||
MethodLifecycleAutoRestart: CapCore,
|
||||
// 多模态内容块注入是工具返回值的一部分,不越权
|
||||
|
||||
@ -98,6 +98,11 @@ type CoreSDK interface {
|
||||
Settings() pubsdk.SettingsAPI
|
||||
// Secrets 是**全进程共享**的变量库(非 per-plugin)。
|
||||
Secrets() pubsdk.SecretsAPI
|
||||
// Security 是**策略声明面**(插件声明命名档位;判定由内核执行)。
|
||||
//
|
||||
// ★ 只声明、不判定:本接口刻意没有 CheckTool 之类的方法 ——
|
||||
// 那属于内核。插件自判会让策略分散、且插件崩溃即策略失效。
|
||||
Security() pubsdk.SecurityAPI
|
||||
Memory() pubsdk.MemoryAPI
|
||||
TextMemory() pubsdk.TextMemoryAPI
|
||||
DocMemory() pubsdk.DocMemoryAPI
|
||||
@ -187,6 +192,9 @@ func (h *coreHandler) Handle(method string, params json.RawMessage) (interface{}
|
||||
MethodSettingsDataDir:
|
||||
return h.handleSettings(method, params)
|
||||
|
||||
case MethodSecurityDeclareProfile, MethodSecurityDeclareProfiles:
|
||||
return h.handleSecurity(method, params)
|
||||
|
||||
case MethodLLMListSources, MethodLLMSetSource, MethodLLMCurrentSource:
|
||||
return h.handleLLM(method, params)
|
||||
|
||||
|
||||
69
internal/plugin/proc/corehandler_security.go
Normal file
69
internal/plugin/proc/corehandler_security.go
Normal file
@ -0,0 +1,69 @@
|
||||
package proc
|
||||
|
||||
// 安全策略**声明面**的宿主侧实现(插件子进程 → 内核)。
|
||||
//
|
||||
// ## 为什么在 proc 包而不是让插件直连 security
|
||||
//
|
||||
// 插件是**子进程**,它唯一的通道就是本桥(stdio JSON-RPC + 共享内存)。
|
||||
// 它拿不到 security.Runtime,也不该拿到 —— 那会让插件能直接改策略表。
|
||||
//
|
||||
// 而判定点必须在这里还有第二个原因:**Owner 要由内核填**。
|
||||
// 插件自报的 owner 可以伪造(任何插件都能声明 `qq.owner` 并给
|
||||
// AllowTools=["*"],等于把 QQ 的 owner 档位改成全放行),
|
||||
// 所以本包用 `h.sdk.PluginName()`(内核侧真实标识)作为 owner,
|
||||
// 而不是采信参数里的值。
|
||||
//
|
||||
// ## 与 internal/sdk/security_impl.go 的关系
|
||||
//
|
||||
// 那里的 `securityImpl` 才是真正的实现(含 owner 校验与 Runtime 转发)。
|
||||
// 本文件只做 JSON 解码 → 调它 → 把错误带回去,保持与其他
|
||||
// corehandler_* 分部函数同构。
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
pubsdk "github.com/JianFeeeee/hologramagentsdk/sdk"
|
||||
)
|
||||
|
||||
func (h *coreHandler) handleSecurity(method string, params json.RawMessage) (interface{}, error) {
|
||||
api := h.sdk.Security()
|
||||
if api == nil {
|
||||
// 未装配(独立工具/特殊宿主)时明确报错,而不是静默成功:
|
||||
// 静默成功会让插件以为档位已登记,直到注入时才暴露。
|
||||
return nil, fmt.Errorf("security: the declaration API is not wired in this host")
|
||||
}
|
||||
|
||||
switch method {
|
||||
case MethodSecurityDeclareProfile:
|
||||
var p struct {
|
||||
Profile pubsdk.SecurityProfile `json:"profile"`
|
||||
}
|
||||
if err := unmarshal(params, &p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := api.DeclareProfile(p.Profile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"ok": true, "name": p.Profile.Name}, nil
|
||||
|
||||
case MethodSecurityDeclareProfiles:
|
||||
var p struct {
|
||||
Profiles []pubsdk.SecurityProfile `json:"profiles"`
|
||||
}
|
||||
if err := unmarshal(params, &p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := api.DeclareProfiles(p.Profiles); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := make([]string, len(p.Profiles))
|
||||
for i, pr := range p.Profiles {
|
||||
names[i] = pr.Name
|
||||
}
|
||||
return map[string]interface{}{"ok": true, "names": names}, nil
|
||||
}
|
||||
|
||||
// 组内不应到达:Handle 的分派表与 case 标签同源。
|
||||
return nil, fmt.Errorf("未知 method: %s", method)
|
||||
}
|
||||
155
internal/plugin/proc/e2e_security_declare_test.go
Normal file
155
internal/plugin/proc/e2e_security_declare_test.go
Normal file
@ -0,0 +1,155 @@
|
||||
package proc
|
||||
|
||||
// 端到端:插件的**安全策略声明**能力(sdk `Security()`)。
|
||||
//
|
||||
// ## 这条链路为什么要真机验证
|
||||
//
|
||||
// 与 variable store(e2e_secrets)同型:新增一个 proc method 要改
|
||||
// **4 个地方**(协议常量 / 外层分派 / 内层分发 / 能力表),少一处就报
|
||||
// "未知 method"。单测只能证明各段自洽,证明不了接上了。
|
||||
//
|
||||
// 而且它跨了**四层**:
|
||||
//
|
||||
// ① 插件调 sdk.Security().DeclareProfile("qq.guest", …)
|
||||
// ② 模板 → RPC "security.declareProfile"
|
||||
// ③ 内核 coreHandler.handleSecurity → sdk.SecurityAPI(security_impl)
|
||||
// ④ → security.Runtime.DeclareProfile(进程级单例,真正被 CheckTool 读)
|
||||
//
|
||||
// ## 最关键的一点:Owner 由内核填
|
||||
//
|
||||
// 插件在参数里可以写任意 Owner(甚至写别的插件名),但宿主用
|
||||
// `PluginName()` 覆盖它 —— 本判据正面验证「插件声明自己的档位成功」,
|
||||
// 负面验证(冒名被拒)在 internal/sdk/security_impl_test.go。
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
internalConfig "github.com/JianFeeeee/HologramAgent/internal/config"
|
||||
internalSDK "github.com/JianFeeeee/HologramAgent/internal/sdk"
|
||||
internalSecurity "github.com/JianFeeeee/HologramAgent/internal/security"
|
||||
)
|
||||
|
||||
// securityE2EPlugin 用真实模板编译:只做一次档位声明。
|
||||
const securityE2EPlugin = `package main
|
||||
|
||||
import (
|
||||
sdk "github.com/JianFeeeee/hologramagentsdk/sdk"
|
||||
)
|
||||
|
||||
type secPlugin struct{ name string }
|
||||
|
||||
func NewPluginFactory(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||||
return &secPlugin{name: name}, nil
|
||||
}
|
||||
|
||||
func (p *secPlugin) Name() string { return p.name }
|
||||
func (p *secPlugin) Stop() error { return nil }
|
||||
|
||||
func (p *secPlugin) Start(s *sdk.PluginSDK) error {
|
||||
sec := s.Security()
|
||||
s.RegisterTool("e2e_declare", sdk.ToolDef{
|
||||
Description: "声明安全档位",
|
||||
Parameters: map[string]interface{}{"type": "object"},
|
||||
}, func(args map[string]interface{}) (interface{}, error) {
|
||||
if sec == nil {
|
||||
return "UNAVAILABLE", nil
|
||||
}
|
||||
err := sec.DeclareProfile(sdk.SecurityProfile{
|
||||
Name: "e2eproc.guest",
|
||||
DenyTools: []string{"cmd_*"},
|
||||
})
|
||||
if err != nil {
|
||||
return "DECLARE_ERR: " + err.Error(), nil
|
||||
}
|
||||
return "OK", nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
`
|
||||
|
||||
// ★★ 判据:真实模板插件的档位声明能穿透四层到达 Runtime。
|
||||
func TestE2E_RealTemplateSecurityDeclare(t *testing.T) {
|
||||
bin := buildPluginWithRealTemplate(t, securityE2EPlugin)
|
||||
|
||||
dir := t.TempDir()
|
||||
setTestCodec(t)
|
||||
|
||||
// 装配进程级 Runtime(生产同款单例;工具判定读的就是它)
|
||||
prev := internalSecurity.RuntimeGlobal()
|
||||
rt := internalSecurity.NewRuntime(nil)
|
||||
internalSecurity.SetRuntimeGlobal(rt)
|
||||
t.Cleanup(func() { internalSecurity.SetRuntimeGlobal(prev) })
|
||||
_ = internalConfig.NewConfigRegistry
|
||||
|
||||
host, err := NewHost()
|
||||
if err != nil {
|
||||
t.Fatalf("NewHost: %v", err)
|
||||
}
|
||||
defer host.Close()
|
||||
|
||||
core := newFakeCore()
|
||||
// ★ 用内核侧真实插件名注入 —— 这就是 Owner 不可伪造的来源。
|
||||
core.securityAPI = internalSDK.NewSecurityAPI("e2eproc")
|
||||
p := New("e2e", bin, dir, nil, host, nil)
|
||||
if err := p.Start(core); err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
}
|
||||
defer p.Close()
|
||||
|
||||
core.mu.Lock()
|
||||
handler, ok := core.tools["e2e_declare"]
|
||||
core.mu.Unlock()
|
||||
if !ok {
|
||||
t.Fatal("插件工具未注册")
|
||||
}
|
||||
|
||||
out, err := handler(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("工具调用失败: %v", err)
|
||||
}
|
||||
got, _ := out.(string)
|
||||
if got != "OK" {
|
||||
t.Fatalf("声明应成功,实际: %q", got)
|
||||
}
|
||||
|
||||
// ★ 核心断言:档位真的进了 Runtime(而不是只"没报错")
|
||||
profiles := rt.Profiles()
|
||||
found := false
|
||||
for _, n := range profiles {
|
||||
if n == "e2eproc.guest" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("★ 档位未登记到 Runtime,已有: %v —— RPC 链路某一段没接上", profiles)
|
||||
}
|
||||
|
||||
// 再验证它真的能约束工具(否则"登记了但没用"
|
||||
//
|
||||
// ★ 这里我曾把断言写反(`if d.Allowed { Fail("应放行") }`)——
|
||||
// 把“期望放行”写成了“若放行则失败”。现在明确:
|
||||
// 未激活档位 ⇒ cmd_run 必须**放行**(档位是按需激活的)
|
||||
if d := rt.CheckTool("cmd_run"); !d.Allowed {
|
||||
t.Fatalf("未激活档位时 cmd_run 应放行(档位按需激活),实际被拒: %s", d.Reason)
|
||||
}
|
||||
if err := rt.ActivateProfile("e2eproc.guest", 0); err != nil {
|
||||
t.Fatalf("激活档位失败: %v", err)
|
||||
}
|
||||
if d := rt.CheckTool("cmd_run"); d.Allowed {
|
||||
t.Fatal("激活 e2eproc.guest 后 cmd_run 必须被拒 —— 档位声明了却不生效")
|
||||
}
|
||||
}
|
||||
|
||||
// 未装配声明面时,插件应拿到明确错误而不是静默成功。
|
||||
func TestE2E_SecurityDeclareWithoutAPI(t *testing.T) {
|
||||
core := newFakeCore()
|
||||
core.securityAPI = nil // 模拟宿主未接声明面
|
||||
|
||||
h := &coreHandler{sdk: core}
|
||||
if _, err := h.Handle(MethodSecurityDeclareProfile, []byte(`{}`)); err == nil {
|
||||
t.Fatal("未装配声明面时必须报错(静默成功会让插件误以为已登记)")
|
||||
} else if !strings.Contains(err.Error(), "not wired") {
|
||||
t.Fatalf("错误信息应说明未装配,实际: %v", err)
|
||||
}
|
||||
}
|
||||
@ -45,6 +45,8 @@ type fakeCoreSDK struct {
|
||||
settingsAPI pubsdk.SettingsAPI
|
||||
// secretsAPI 同理:变量库后端(端到端判据用)。
|
||||
secretsAPI pubsdk.SecretsAPI
|
||||
// securityAPI 是策略声明面的后端(声明类端到端判据用)。
|
||||
securityAPI pubsdk.SecurityAPI
|
||||
}
|
||||
|
||||
// knowledgeAPITest 是公开 SDK 的知识库契约(不含内核扩展方法)。
|
||||
@ -69,6 +71,7 @@ func newFakeCore() *fakeCoreSDK {
|
||||
func (f *fakeCoreSDK) PluginName() string { return "fake" }
|
||||
func (f *fakeCoreSDK) Settings() pubsdk.SettingsAPI { return f.settingsAPI }
|
||||
func (f *fakeCoreSDK) Secrets() pubsdk.SecretsAPI { return f.secretsAPI }
|
||||
func (f *fakeCoreSDK) Security() pubsdk.SecurityAPI { return f.securityAPI }
|
||||
func (f *fakeCoreSDK) Memory() pubsdk.MemoryAPI { return nil }
|
||||
func (f *fakeCoreSDK) TextMemory() pubsdk.TextMemoryAPI { return nil }
|
||||
func (f *fakeCoreSDK) DocMemory() pubsdk.DocMemoryAPI { return f.docMem }
|
||||
|
||||
@ -204,6 +204,20 @@ const (
|
||||
MethodSecretsEncrypt = "secrets.encrypt" // 46
|
||||
MethodSecretsDecrypt = "secrets.decrypt" // 47
|
||||
|
||||
// 安全策略**声明面**(插件声明命名档位;判定由内核 security 执行)。
|
||||
//
|
||||
// ★ 为什么是方法而不是事件:声明必须**同步**生效。
|
||||
// 若走事件,插件可能在声明事件还在队列里时就用
|
||||
// InjectOptions.SecurityProfile="qq.guest" 注入 —— 内核那时还没登记
|
||||
// 该档位,会响亮报"未声明档位"(那是对的,但会让插件启动后头几个
|
||||
// 请求必然失败)。RPC 的响应返回即登记完成。
|
||||
//
|
||||
// ★ 为什么放在插件模块(proc)而不是让插件直连 security:
|
||||
// 插件是子进程,它唯一的通道就是本桥。而 Owner 必须由内核用
|
||||
// **真实插件名**填(插件自报可伪造),这个判定点只能在宿主侧。
|
||||
MethodSecurityDeclareProfile = "security.declareProfile" // 48
|
||||
MethodSecurityDeclareProfiles = "security.declareProfiles" // 49
|
||||
|
||||
// LLM 源(原 case 19/20/37)
|
||||
MethodLLMListSources = "llm.listSources" // 19
|
||||
MethodLLMSetSource = "llm.setSource" // 20
|
||||
|
||||
Reference in New Issue
Block a user