mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-01 06:47:29 +00:00
## 为什么必须做
SDK 仓(独立仓库)已把 module 从 `github.com/JianFeeeee/homeagentsdk`
改名为 `hologramagentsdk`。主仓通过 `replace` 直接引用它的**物理目录**:
replace github.com/JianFeeeee/homeagentsdk => ./third_party/homeagent-sdk
一旦 SDK 侧改成新 module 名,主仓立刻炸:
go: ./third_party/homeagent-sdk@ used for two different module paths
(github.com/JianFeeeee/hologramagentsdk and github.com/JianFeeeee/homeagentsdk)
Go **不允许**同一目录被两个 module 路径 replace。所以不能"新旧并存",
只能把主仓 59 个文件的 import 全部改到新名,并删掉旧 replace。
## 一个被证伪的前提
我最初按"third_party/homeagent-sdk 是独立仓库,不能在此仓修改其源码"
来处理,并因此判断两个仓的改动可以隔离。实际上主仓是靠 `replace` 指向
该目录的物理副本 —— **两个仓的改动不可能真正隔离**:SDK 侧改 module 名,
主仓必然跟着改 import。
这个前提应该在做任何改动之前用 `go list -m` 验证,而不是假设。
## 连带修掉两处
| 缺陷 | 性质 |
|---|---|
| `TestLuaSDKMockSingleSource` 失败 | SDK 的 `sdk/lua/sdk.lua` 变了,主仓内嵌副本未同步(跑 `scripts/sync-lua-sdk.sh` 修复) |
| `sdkModuleNames` 分散两处 | 已在 SDK 仓合并为单一数据源(见 SDK 仓 725732b) |
第一条是**副本一致性判据抓到的**,不是我自己发现的 —— 这类判据的价值在此:
两个仓必然有副本,靠人记住"改了 A 要同步 B"是做不到的。
## 验证
- `go build ./...` / `go vet ./...` 干净
- `go test ./... -count=1`:**58 包 0 失败**
- `internal/plugin`(Lua SDK 副本一致性)+ `internal/sdk` 全绿
- 主仓 `third_party/homeagent-sdk/` 与 SDK 仓 `main` 无差异
193 lines
6.1 KiB
Go
193 lines
6.1 KiB
Go
package proc
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
|
||
pubsdk "github.com/JianFeeeee/hologramagentsdk/sdk"
|
||
)
|
||
|
||
// handlePluginMgr 处理插件管理:reloadOne / listLoaded / isDisabled。
|
||
//
|
||
// 本函数体是 corehandler.go 里 Handle 那一个大 switch 的**整块平移**:
|
||
// case 标签与 case 体逐字保留,只换了宿主函数(§3.2 的平移原则)。
|
||
func (h *coreHandler) handlePluginMgr(method string, params json.RawMessage) (interface{}, error) {
|
||
switch method {
|
||
// ---- 插件管理(原 case 48/49/50)----
|
||
case MethodPluginReloadOne:
|
||
pm := h.sdk.PluginMgr()
|
||
if pm == nil {
|
||
return nil, errUnavailable("plugin manager")
|
||
}
|
||
var p struct {
|
||
Name string `json:"name"`
|
||
}
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, pm.ReloadOne(p.Name)
|
||
case MethodPluginListLoaded:
|
||
pm := h.sdk.PluginMgr()
|
||
if pm == nil {
|
||
return nil, errUnavailable("plugin manager")
|
||
}
|
||
list := pm.ListLoadedPlugins()
|
||
if list == nil {
|
||
list = []string{}
|
||
}
|
||
return map[string]interface{}{"plugins": list}, nil
|
||
case MethodPluginIsDisabled:
|
||
pm := h.sdk.PluginMgr()
|
||
if pm == nil {
|
||
return nil, errUnavailable("plugin manager")
|
||
}
|
||
var p struct {
|
||
Name string `json:"name"`
|
||
}
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
return map[string]interface{}{"disabled": pm.IsPluginDisabled(p.Name)}, nil
|
||
case MethodPluginMgrStop:
|
||
pm := h.sdk.PluginMgr()
|
||
if pm == nil {
|
||
return nil, errUnavailable("plugin manager")
|
||
}
|
||
var p struct {
|
||
Name string `json:"name"`
|
||
}
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, pm.StopPlugin(p.Name)
|
||
case MethodPluginMgrStart:
|
||
pm := h.sdk.PluginMgr()
|
||
if pm == nil {
|
||
return nil, errUnavailable("plugin manager")
|
||
}
|
||
var p struct {
|
||
Name string `json:"name"`
|
||
}
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, pm.StartPlugin(p.Name)
|
||
case MethodPluginMgrIsRuntimeStopped:
|
||
pm := h.sdk.PluginMgr()
|
||
if pm == nil {
|
||
return nil, errUnavailable("plugin manager")
|
||
}
|
||
var p struct {
|
||
Name string `json:"name"`
|
||
}
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
return map[string]interface{}{"runtime_stopped": pm.IsPluginRuntimeStopped(p.Name)}, nil
|
||
|
||
}
|
||
|
||
// 组内不应到达:Handle 的分派表与本函数的 case 标签同源,
|
||
// 出现即说明两处不同步。
|
||
return nil, fmt.Errorf("未知 method: %s", method)
|
||
}
|
||
|
||
// handleStageLocks 处理共享段锁仲裁(§3.7)。
|
||
//
|
||
// 本函数体是 corehandler.go 里 Handle 那一个大 switch 的**整块平移**:
|
||
// case 标签与 case 体逐字保留,只换了宿主函数(§3.2 的平移原则)。
|
||
func (h *coreHandler) handleStageLocks(method string, params json.RawMessage) (interface{}, error) {
|
||
switch method {
|
||
// ---- 共享段锁仲裁(新增,§3.7)----
|
||
case MethodStageLock:
|
||
if h.locks == nil {
|
||
return nil, fmt.Errorf("stage.lock: 锁仲裁未就绪")
|
||
}
|
||
return nil, h.locks.acquire(h.name)
|
||
case MethodStageUnlock:
|
||
if h.locks == nil {
|
||
return nil, fmt.Errorf("stage.unlock: 锁仲裁未就绪")
|
||
}
|
||
return nil, h.locks.release(h.name)
|
||
|
||
}
|
||
|
||
// 组内不应到达:Handle 的分派表与本函数的 case 标签同源,
|
||
// 出现即说明两处不同步。
|
||
return nil, fmt.Errorf("未知 method: %s", method)
|
||
}
|
||
|
||
// handleEvents 处理事件订阅(§3.6,子进程下首次真正可用)。
|
||
//
|
||
// 本函数体是 corehandler.go 里 Handle 那一个大 switch 的**整块平移**:
|
||
// case 标签与 case 体逐字保留,只换了宿主函数(§3.2 的平移原则)。
|
||
func (h *coreHandler) handleEvents(method string, params json.RawMessage) (interface{}, error) {
|
||
switch method {
|
||
// ---- 事件订阅(原 case 23/24,子进程下首次真正可用,§3.6)----
|
||
case MethodEventsSubscribe:
|
||
var p struct {
|
||
Types []pubsdk.EventType `json:"types"`
|
||
}
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
if h.evtRing == nil {
|
||
return nil, fmt.Errorf("%s: 事件环未就绪", method)
|
||
}
|
||
// 订阅请求来自子进程——handler 直接注册到 Bus,
|
||
// 事件经 EventRing 写入环后由子进程消费。
|
||
//
|
||
// ★ 用 tracked 版本:订阅会被登记,Host.Close 在内核关停时统一退订。
|
||
// 必须如此——这些 handler 写共享内存,而 Host.Close 会 munmap 整块区域;
|
||
// 未退订的 handler 在关停后会写已解除映射的内存 ⇒ SIGSEGV。
|
||
h.evtRing.EvtRingSubscribeTracked(p.Types)
|
||
return nil, nil
|
||
|
||
case MethodEventsUnsubscribe:
|
||
// 事件环的订阅没有**按插件**持久化句柄(取消函数由 Subscribe 返回,
|
||
// 但子进程不保存,故无法精确撤销单个插件的订阅)。
|
||
// 当前设计:子进程 Stop 时由内核统一清理——具体落点是
|
||
// Host.Close → evtCloser.Close 退订全部 tracked 订阅。
|
||
// 因此这里仍是 no-op;但「统一清理」现在是真的有实现,
|
||
// 不再是只写在注释里的承诺。
|
||
return nil, nil
|
||
|
||
}
|
||
|
||
// 组内不应到达:Handle 的分派表与本函数的 case 标签同源,
|
||
// 出现即说明两处不同步。
|
||
return nil, fmt.Errorf("未知 method: %s", method)
|
||
}
|
||
|
||
// handleArena 处理共享槽池分配/释放(内部传输层,见 protocol.go)。
|
||
//
|
||
// 本函数体是 corehandler.go 里 Handle 那一个大 switch 的**整块平移**:
|
||
// case 标签与 case 体逐字保留,只换了宿主函数(§3.2 的平移原则)。
|
||
func (h *coreHandler) handleArena(method string, params json.RawMessage) (interface{}, error) {
|
||
switch method {
|
||
// ---- 共享槽池(内部传输层,见 protocol.go 注释)----
|
||
case MethodArenaAlloc:
|
||
var p ArenaAllocParams
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
ref, err := h.arenaAlloc(p.Size)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return ArenaAllocResult{Ref: ref}, nil
|
||
|
||
case MethodArenaFree:
|
||
var p ArenaFreeParams
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, h.arenaFree(p.Ref)
|
||
|
||
}
|
||
|
||
// 组内不应到达:Handle 的分派表与本函数的 case 标签同源,
|
||
// 出现即说明两处不同步。
|
||
return nil, fmt.Errorf("未知 method: %s", method)
|
||
}
|