mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 17:38:10 +00:00
现场(用户在线上跑驻留子联调,日志实录):
父 agent 侧「通道装载完整」,子 `demo-resident` 侧 `childIO` 是**空壳**:
子的 `output_list_channels` 为空、`output_send__<通道>` 一律被判
「通道 [X] 不存在或不可用」,连 `output_send__*` 工具都不生成 ⇒ 子不会发消息。
根因:**输出通道在 io 层就是 Device**,而它们由插件登记在**父**的 `IOManager` 上。
`SpawnResident` 给子建的是全新 `IOManager`(它确实该有自己的输入入口与 outputCh),
却只共享了 inputch 登记表,**没有继承设备/输出通道视图**:
- `executeOutputSendTool` → `a.io.GetChannelCapabilities(ch)` 查的是 `devices[ch]` ⇒ 0
- 投递路径 `a.io.GetDevice(ch).Execute("output", …)` ⇒ nil
- 工具面 `tooldefs.go` 从 `a.io.ListChannels()` 生成 `output_send__*` ⇒ 空
改法:给 `IOManager` 增加**上级回退**(`SetParentIO`)——驻留子创建时把自己的 io 挂到
父的 io 上,`GetDevice` / `GetChannelCapabilities` / `ListChannels` / `ExecuteTool`
在自己没有时回退到上级。
为什么是**实时回退**而不是创建时复制快照:设备随资源生灭(远程设备上线/掉线以分钟计,
现场日志 60 秒一个来回),复制出来的表转瞬即过期;而回退永远与父一致。
**授权不受影响**:回退只解决"看得见",能不能用仍由各自的 `AllowedOutputs` 白名单把关
(`executeOutputSendTool` 的授权闸 + 工具生成时的过滤都在白名单之后);
自己的登记优先,子可以覆盖/屏蔽同名通道。
判据(新增 5 条):
- io 层:无上级时行为与以前完全一致 / 挂上级后看得见 / **实时**(父新登记立刻可见、
注销立刻不可见)/ 同名自己的优先且不重复列出 / `ExecuteTool` 同样回退
- 内核层:子看得见父通道 + 真能发出(父通道收到 1 次 output)/ 白名单外被拒且未送达 /
子工具面只生成授权通道(含 `_help`)/ 父后登记的通道立刻可见 / 默认即完整授权
103 lines
3.7 KiB
Go
103 lines
3.7 KiB
Go
package io
|
||
|
||
import "testing"
|
||
|
||
// 上级回退:驻留子的轻量内核有自己的 IOManager,但输出通道(io 里的 Device)
|
||
// 是插件登记在**父**的 io 上的。子若看不见它们,`output_send__<通道>` 会被判
|
||
// "通道不存在或不可用"、`output_list_channels` 为空 —— 现场联调实录
|
||
// 「父侧通道装载完整、子侧 childIO 空壳」。
|
||
func TestIOManagerParentFallback(t *testing.T) {
|
||
parent := NewIOManager()
|
||
if err := parent.RegisterDevice(&mockDevice{name: "qq", devType: DeviceOutput, caps: CapText}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
child := NewIOManager()
|
||
// 未挂上级时行为与以前完全一致(不能悄悄多出通道)
|
||
if got := child.GetChannelCapabilities("qq"); got != 0 {
|
||
t.Fatalf("无上级时不应看见父的通道,得到 %v", got)
|
||
}
|
||
if n := len(child.ListChannels()); n != 0 {
|
||
t.Fatalf("无上级时通道数应为 0,得到 %d", n)
|
||
}
|
||
|
||
child.SetParentIO(parent)
|
||
if got := child.GetChannelCapabilities("qq"); got != CapText {
|
||
t.Fatalf("挂上级后应看见父通道能力 CapText,得到 %v", got)
|
||
}
|
||
if dev := child.GetDevice("qq"); dev == nil || dev.Name() != "qq" {
|
||
t.Fatalf("GetDevice 未回退到父: %v", dev)
|
||
}
|
||
if n := len(child.ListChannels()); n != 1 {
|
||
t.Fatalf("ListChannels 未回退到父,得到 %d 条", n)
|
||
}
|
||
|
||
// **实时**回退而非快照:父后来登记的通道,子立刻可见。
|
||
// (设备随资源生灭 —— 远程设备上线/掉线以分钟计,快照一分钟就过期)
|
||
if err := parent.RegisterDevice(&mockDevice{name: "newdev", devType: DeviceOutput, caps: CapImage}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if got := child.GetChannelCapabilities("newdev"); got != CapImage {
|
||
t.Fatalf("子应实时看见父新登记的通道,得到 %v", got)
|
||
}
|
||
|
||
// 父掉线注销后,子也立刻看不见(不是复制出来的旧表)
|
||
parent.UnregisterDevice("newdev")
|
||
if got := child.GetChannelCapabilities("newdev"); got != 0 {
|
||
t.Fatalf("父注销后子不应再看见,得到 %v", got)
|
||
}
|
||
}
|
||
|
||
// 自己的登记优先:子可以覆盖/屏蔽同名通道,父的登记不会重复列出。
|
||
func TestIOManagerOwnDeviceWins(t *testing.T) {
|
||
parent := NewIOManager()
|
||
if err := parent.RegisterDevice(&mockDevice{name: "ch", devType: DeviceOutput, caps: CapText}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
child := NewIOManager()
|
||
child.SetParentIO(parent)
|
||
if err := child.RegisterDevice(&mockDevice{name: "ch", devType: DeviceOutput, caps: CapImage}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
if got := child.GetChannelCapabilities("ch"); got != CapImage {
|
||
t.Fatalf("同名时自己的登记应优先,得到 %v", got)
|
||
}
|
||
list := child.ListChannels()
|
||
if len(list) != 1 {
|
||
t.Fatalf("同名通道不应重复列出,得到 %d 条", len(list))
|
||
}
|
||
if list[0].OutputCaps != CapImage {
|
||
t.Fatalf("列出的应是子自己的那条,得到 %v", list[0].OutputCaps)
|
||
}
|
||
}
|
||
|
||
// 设备工具(io.ExecuteTool)同样回退:子的设备工具都在父的 io 上。
|
||
func TestIOManagerExecuteToolFallsBackToParent(t *testing.T) {
|
||
parent := NewIOManager()
|
||
called := 0
|
||
if err := parent.RegisterDevice(&mockDevice{
|
||
name: "dev", devType: DeviceIO,
|
||
tools: []ToolDef{{Name: "dev_do", Description: "干点什么"}},
|
||
executeFn: func(tool string, args map[string]interface{}) (interface{}, error) {
|
||
called++
|
||
return "parent-done", nil
|
||
},
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
child := NewIOManager()
|
||
if _, err := child.ExecuteTool("dev_do", nil); err == nil {
|
||
t.Fatal("无上级时不该能执行父的设备工具")
|
||
}
|
||
child.SetParentIO(parent)
|
||
got, err := child.ExecuteTool("dev_do", map[string]interface{}{"x": 1})
|
||
if err != nil {
|
||
t.Fatalf("应回退到父执行: %v", err)
|
||
}
|
||
if got != "parent-done" || called != 1 {
|
||
t.Fatalf("执行结果=%v called=%d", got, called)
|
||
}
|
||
}
|