mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 17:38:10 +00:00
corehandler.go —— cabi/loader.go 51 个 case 体的整块平移(§3.2):
- 参数从「s1/s2/s3 + i1/i2 五个固定槽」改为结构化 JSON,语义不变
- CoreSDK 接口刻意只含外部插件应得能力:无 Selftest/Supervisor/Tracker/
Status/Adapter/Config/Tool/Indexer/OutputChan/Publish
→ 权限梯度从「C ABI 表达能力的意外产物」变成「显式声明并强制的策略」(§3.8)
- 事件订阅(case 23/24)与 SetToolBlocks 明确返回未实现,不再像 C ABI 那样静默成功
(静默成功后收不到事件比报错更难排查)
- ToolDef.Cleaner / ChannelDef.Cleaner 是函数,跨进程置 nil(§3.5 回调型资源)
host.go —— 共享段所有权中心:
- ❗ 全部子进程插件共享**同一块 memfd**。若每插件一段,
「内核 ctx → 段 → 插件改 → 回读 ctx」在多插件下退化成副本模型,
最后回读者覆盖前者,§8.4 的 35.8~36.8% lost update 原样复现
- stageMu 串行化整次 stage 对段的独占(内核可能并发触发 RunStage)
- 首个进入者写入段,最后离开者回读 + Compact(此时无插件持锁,满足 §3.3 前提)
stage.go —— RunStage 接线(风险 3.4 落点):
- 并发扇出保留(§0.2 第 1 条:并发扇出是原始设计,不是缺陷)
- 插件失败时 ForceReleaseLock,避免后续插件死锁(实验 9,无需 robust mutex)
plugin.go —— registry 可加载的插件实体:
- Start: spawn(fd 3 传共享段)→ 握手 → plugin.init → plugin.start
- Close: **真 kill + wait**,对比 cabi 的 Close 只做 dlclose 而后者是 no-op(§1.1)
- invokeOutput **同步等真实结果**,失败上报 error —— §9.4 根治
验证(34 项测试全绿,含 -race,全部用真实子进程):
- 单插件 stage 读改写经共享段回到内核 StageContext
- sanitizer(改写) + weather(只读) 并发:清洗结果不被覆盖(现网场景)
- **5 个独立进程并发 append 同一 FinalText:5 个标记全部保留,零丢失零撕裂**
(实验 8 在真实 RPC + 真实 RunStage 下的复刻)
- 工具注册可调用 / 输出通道真实失败上报 / start 期间反向调用
- 未知 method 与未实现能力被拒绝 / stage 外加锁被拒绝
接口冻结: git diff third_party/homeagent-sdk/sdk/ 为空
179 lines
3.9 KiB
Go
179 lines
3.9 KiB
Go
//go:build ignore
|
||
|
||
// readonlyplugin 是只读 stage 插件(模拟 weather 的 AfterToolcall):
|
||
// 读取共享段但不写回任何字段。
|
||
//
|
||
// **这是 lost update 修复的关键验证对象**:C ABI 副本模型下,
|
||
// 它会把自己收到的旧快照无条件回传,覆盖 sanitizer 的清洗结果
|
||
// (§8.6 实测现网 1.6~4.3% 被覆盖)。共享内存模型下它零写入。
|
||
package main
|
||
|
||
import (
|
||
"bufio"
|
||
"encoding/binary"
|
||
"encoding/json"
|
||
"fmt"
|
||
"os"
|
||
"sync"
|
||
"syscall"
|
||
)
|
||
|
||
type request struct {
|
||
ID uint64 `json:"id,omitempty"`
|
||
Method string `json:"method"`
|
||
Params json.RawMessage `json:"params,omitempty"`
|
||
}
|
||
|
||
type response struct {
|
||
ID uint64 `json:"id"`
|
||
Result interface{} `json:"result,omitempty"`
|
||
Error string `json:"error,omitempty"`
|
||
}
|
||
|
||
var (
|
||
out = bufio.NewWriter(os.Stdout)
|
||
writeMu sync.Mutex
|
||
|
||
nextID uint64
|
||
pendMu sync.Mutex
|
||
pending = map[uint64]chan json.RawMessage{}
|
||
|
||
shm []byte
|
||
)
|
||
|
||
func send(v interface{}) {
|
||
b, _ := json.Marshal(v)
|
||
writeMu.Lock()
|
||
out.Write(b)
|
||
out.WriteByte('\n')
|
||
out.Flush()
|
||
writeMu.Unlock()
|
||
}
|
||
|
||
func callKernel(method string, params interface{}) json.RawMessage {
|
||
pendMu.Lock()
|
||
nextID++
|
||
id := nextID
|
||
ch := make(chan json.RawMessage, 1)
|
||
pending[id] = ch
|
||
pendMu.Unlock()
|
||
|
||
var raw json.RawMessage
|
||
if params != nil {
|
||
b, _ := json.Marshal(params)
|
||
raw = b
|
||
}
|
||
send(request{ID: id, Method: method, Params: raw})
|
||
return <-ch
|
||
}
|
||
|
||
const (
|
||
offArenaBase = 8
|
||
offCtxBase = 20
|
||
sliceSize = 8
|
||
fToolResults = 10
|
||
)
|
||
|
||
func readToolResults() []byte {
|
||
base := binary.LittleEndian.Uint32(shm[offArenaBase:])
|
||
cb := binary.LittleEndian.Uint32(shm[offCtxBase:])
|
||
o := cb + uint32(fToolResults*sliceSize)
|
||
off := binary.LittleEndian.Uint32(shm[o:])
|
||
ln := binary.LittleEndian.Uint32(shm[o+4:])
|
||
if off == 0 && ln == 0 {
|
||
return nil
|
||
}
|
||
return shm[base+off : base+off+ln]
|
||
}
|
||
|
||
func main() {
|
||
in := bufio.NewScanner(bufio.NewReader(os.Stdin))
|
||
in.Buffer(make([]byte, 0, 64*1024), 1024*1024)
|
||
|
||
for in.Scan() {
|
||
line := make([]byte, len(in.Bytes()))
|
||
copy(line, in.Bytes())
|
||
|
||
var probe struct {
|
||
ID uint64 `json:"id"`
|
||
Method string `json:"method"`
|
||
}
|
||
if json.Unmarshal(line, &probe) != nil {
|
||
continue
|
||
}
|
||
if probe.Method == "" {
|
||
var resp struct {
|
||
ID uint64 `json:"id"`
|
||
Result json.RawMessage `json:"result"`
|
||
}
|
||
json.Unmarshal(line, &resp)
|
||
pendMu.Lock()
|
||
ch, ok := pending[resp.ID]
|
||
delete(pending, resp.ID)
|
||
pendMu.Unlock()
|
||
if ok {
|
||
ch <- resp.Result
|
||
}
|
||
continue
|
||
}
|
||
|
||
var req request
|
||
json.Unmarshal(line, &req)
|
||
|
||
switch req.Method {
|
||
case "handshake":
|
||
var hp struct {
|
||
ShmSize int `json:"shm_size"`
|
||
}
|
||
json.Unmarshal(req.Params, &hp)
|
||
if hp.ShmSize > 0 {
|
||
m, err := syscall.Mmap(3, 0, hp.ShmSize,
|
||
syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
|
||
if err != nil {
|
||
send(response{ID: req.ID, Error: fmt.Sprintf("mmap: %v", err)})
|
||
continue
|
||
}
|
||
shm = m
|
||
}
|
||
send(response{ID: req.ID, Result: map[string]interface{}{
|
||
"protocol": 1, "sdk_version": "test", "plugin_name": "readonly", "pid": os.Getpid(),
|
||
}})
|
||
|
||
case "plugin.init":
|
||
send(response{ID: req.ID})
|
||
|
||
case "plugin.start":
|
||
go func(id uint64) {
|
||
callKernel("stage.register", map[string]interface{}{
|
||
"stage": "after_toolcall",
|
||
"scope": "global",
|
||
})
|
||
send(response{ID: id})
|
||
}(req.ID)
|
||
|
||
case "plugin.stop":
|
||
send(response{ID: req.ID})
|
||
out.Flush()
|
||
os.Exit(0)
|
||
|
||
case "stage.invoke":
|
||
go func(id uint64) {
|
||
if shm == nil {
|
||
send(response{ID: id, Error: "共享段未挂载"})
|
||
return
|
||
}
|
||
callKernel("stage.lock", nil)
|
||
// 只读:读了但一个字节都不写回
|
||
_ = readToolResults()
|
||
callKernel("stage.unlock", nil)
|
||
send(response{ID: id, Result: map[string]interface{}{"dirty_fields": 0}})
|
||
}(req.ID)
|
||
|
||
default:
|
||
if req.ID != 0 {
|
||
send(response{ID: req.ID})
|
||
}
|
||
}
|
||
}
|
||
}
|