mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-05 16:57:32 +00:00
## 起因(生产故障)
A2A 分诊助手(驻留子)把 terminal_create 当一次性命令执行器:
terminal_create(command='cd /usr/lib/node_modules/@deepseek-ai && ls -d */ | head -60 && …')
→ read /dev/ptmx: input/output error
→ 再开 term_7、term_8、term_9 …
链路拆开四层:
① cmd 插件 auto_start=false ⇒ 工具表里没有 cmd_run
② 需要 cmd-tools skill 说明"先 plugin_start cmd"
③ 但 skill 索引没进那个 agent 的提示词
④ 因为 skillIndex 只是根 agent 的字段、由 bootstrap 手工接线
④ 才是总根。而它之所以必须手工接线:agent/core 读不到内核的共享数据。
## 环的精确形状(实测,非推测)
kernelboot 因为持有 KernelState 而依赖 19 个业务包:
kernelboot ──▶ agent/core ──▶ …
──▶ plugin
──▶ security
──▶ memory / knowledge / text / coldcontext / …
而 kernelboot.go / scheduler.go / funcmodule.go / syncbus.go / lifecycle.go
**一行业务代码都不需要** —— 依赖全在 kernelstate.go 这一个数据文件里。
后果是四个底层包各自造通道:
core → Agent 字段(每种 agent 构造点都要记得传)
plugin → 12 个 SetXxx
sdk → SDKConfig 传参
security → 包级单例 Global() / RuntimeGlobal() / ConfigCodec()
四条通道是同一个根因的四个表现。
## 本次做的事
### 1. 抽出 internal/kstate(断环)
kernelstate.go / syncbus.go / lifecycle.go 搬出 kernelboot:
internal/kstate 纯数据 + 唯一挂载点(叶子,谁都能 import)
internal/kernelboot 装配引擎,零业务包
挂载点 API:Mount / Get(nil 安全)/ MountForTest(返回 restore,并行测试隔离)。
验证:core 现在能 import kstate(此前 import cycle not allowed)。
### 2. StageHost / Agent 字段存 any
它们是 core 自己的类型(core 是环的一极,这是真环)。按用户裁定的方向:
> 是其他包在kstate挂共享内容啊
存 any,消费方断言回具体类型 —— 与本仓既有的 PluginCatalog /
PluginScheduler / UpdateCapabilities 同型。
★ 其余字段保持**具体类型**,没有一律改 any。判据是"它会不会反向依赖
agent/core":实测 plugin / security / sdk 都不依赖 core,所以它们作为
具体类型不构成环,而类型化字段让"拼错/取错类型"是编译错误(本结构体的
设计原则)。
### 3. 插件声明式系统提示词段
SDK 新增 SystemPromptAPI(见 submodule 提交)。内核侧:
kstate.State.PromptSections *promptsec.Registry ← 唯一事实来源
Agent.renderSystemPrompt() 每轮重建时读它
skillmgr 从"实现 SkillIndexProvider 让内核来问"改为"声明自己的段":
DeclarePromptSection{Section:"skillmgr.skills", Order:100, Volatile:true}
并挂到全部变更点(启动全扫后、移交事件后、load/unload/create/install/
enable/disable 后、Stop 时撤销)。
### 4. 内核自身的提示词段也走同一管理器
按用户裁定「把现在内核所有 systemprompt 的相关注入都接入到这个统一的
systemprompt 管理器上」,把 buildSystemPrompt 里硬拼的十几块
(人格卡/任务提示词/记忆说明/输出规则/工具目录…)改为声明式:
kernelSections() 当轮计算 → 与插件段按 Order 合并渲染。
顺序由 Order 常量决定,改结构变成调常量而不是挪代码块。
## 判据(均经变异验证)
TestCoreCanImportKstate 真编译探针;环重现则红(实测)
TestKstateDoesNotDependOnCore 静态版,指向具体字段
TestKstateIsLeaf kstate 不得反依赖上层
TestRTChildSeesPromptSections 子 agent 能看到插件段
TestResidentSeesPromptSections 生产实际中招的那一类;变异后红(实测)
TestRootAgentSeesPromptSections 确认没改坏根路径
TestNoPromptSectionsMount 未装配时不 panic、不凭空出现段
TestDeclareTwiceRendersOnce ★ 回应"会不会多份":声明 5 次渲染 1 份
TestSectionRemovalTakesEffect Stop 后段消失
TestRuntimeUpdateVisibleToExistingAgent 同一实例下一轮看到新内容
TestSectionOrderIsRespected Order 生效且稳定
## 验证
go build ./... PASS
go vet ./... PASS
gofmt -l internal/ cmd/ pkg/ 干净
go test ./... -count=1 63 包全过,0 FAIL
干净检出(git worktree) build + 4 包测试全绿
## 未做(见 kernelplan.md)
T3 security 三单例收敛(含 Store 复用 Codec 的惰性迁移)
T5 plugin Setter 收敛
T6 防腐判据(部分已随本次落地)
72 lines
2.4 KiB
Go
72 lines
2.4 KiB
Go
package memory
|
||
|
||
// "nil 加读取前检查" 的判据(用户指定的做法)。
|
||
//
|
||
// ## 为什么需要判据守它
|
||
//
|
||
// `KernelState` 的字段是**类型化的**(用户设计),好处是拼错/取错类型是
|
||
// 编译错误。但**跨模块的"未就绪"仍表现为 nil** —— 而 nil 解引用会
|
||
// **panic**,栈信息指向无关处(如 `strings.Split`),排查成本高。
|
||
//
|
||
// 用户明确要求:"结构体成员初始置 nil 加**读取前检查**"。
|
||
// 本判据确保每个模块对**不由依赖图保证**的字段(Group 0 产物)做检查。
|
||
//
|
||
// ★ 为什么要判据而不是靠自觉:`ks.CfgReg` 是 Group 0 的产物,
|
||
// **无法通过 Requires 声明**(Group 0 不是调度模块)⇒ 依赖图保证不了它。
|
||
// 这正是最容易忘、忘了就 panic 的地方。
|
||
|
||
import (
|
||
"path/filepath"
|
||
"strings"
|
||
"testing"
|
||
|
||
internalConfig "github.com/JianFeeeee/HologramAgent/internal/config"
|
||
"github.com/JianFeeeee/HologramAgent/internal/kstate"
|
||
)
|
||
|
||
// ★★★ 判据:**CfgReg 为 nil 时给可操作错误,而不是 panic**。
|
||
func TestMemoryModuleRejectsNilCfgReg(t *testing.T) {
|
||
dir := t.TempDir()
|
||
ks := &kstate.State{DataDir: dir} // CfgReg 故意为 nil
|
||
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
t.Fatalf("★ CfgReg 为 nil 时 panic 了(应返回可操作错误): %v\n"+
|
||
" 用户要求的是「nil 加读取前检查」,panic 的栈信息指向无关处", r)
|
||
}
|
||
}()
|
||
|
||
err := (Module{}).Init(ks)
|
||
if err == nil {
|
||
t.Fatal("★ CfgReg 为 nil 却成功返回 —— 后面会在读配置时 panic")
|
||
}
|
||
// 错误必须点出**是哪个字段**以及**该做什么**
|
||
msg := err.Error()
|
||
if !strings.Contains(msg, "CfgReg") {
|
||
t.Errorf("错误应点名字段 CfgReg(便于定位): %v", err)
|
||
}
|
||
if !strings.Contains(msg, "Group 0") {
|
||
t.Errorf("错误应指出根因(Group 0 必须先跑): %v", err)
|
||
}
|
||
}
|
||
|
||
// ★★★ 判据:**DataDir 为空时也拒绝**(同类检查)。
|
||
func TestMemoryModuleRejectsEmptyDataDir(t *testing.T) {
|
||
dir := t.TempDir()
|
||
reg := newTestReg(t, dir)
|
||
ks := &kstate.State{CfgReg: reg} // DataDir 空
|
||
|
||
err := (Module{}).Init(ks)
|
||
if err == nil {
|
||
t.Fatal("★ DataDir 为空却成功返回")
|
||
}
|
||
if !strings.Contains(err.Error(), "DataDir") {
|
||
t.Errorf("错误应点名字段 DataDir: %v", err)
|
||
}
|
||
}
|
||
|
||
func newTestReg(t *testing.T, dir string) *internalConfig.ConfigRegistry {
|
||
t.Helper()
|
||
return internalConfig.NewConfigRegistry(filepath.Join(dir, "config.db"))
|
||
}
|