feat(shm): 文档/知识正文入共享内存 + 协议版本 bump 到 2(§13.13)

## 数据面补齐

- doc.insert / doc.insertWithMedia:新增 doc_ref / attachments_ref,
  模板序列化后 putValueInArena
- knowledge.add:新增 content_ref(内容是 JSON 字符串,读出后再解一层)
- 抽出通用 resolveJSONRef(resolveBlocks 也改用它),三处共用一套
  “共享优先、内联回退”逻辑

## 协议版本 bump:让错配显式失败,而不是静默坏

这是本轮更重要的部分。§13.6/§13.13 改了内核→插件 payload 的承载方式,
两种错配都不会报错、只会静默失效:

- v1 插件只读内联 args(tool/cleaner/output)→ 遇到 v2 内核拿到空参数
- v2 插件发 blocks_ref → v1 内核反序列化时静默忽略(旧内核
  io.setToolBlocks 还是桩实现)

现场表现为“输出变空 / 图注入没反应”,极难定位。所以把 ProtocolVersion
与模板 procProtocolVersion 一起 bump 到 2:双方都是等值校验,v1 插件遇上
v2 内核会在建链时明确报“协议版本不匹配…请用配套 plugindev 重编”。

测试里把“错误必须带出重编指令”也断言上了——生产上碰到它的现场就是
“只更新了内核没重编插件”,光报“不匹配”定位不到行动。

testdata 8 个插件的 protocol 同步更新(badprotoplugin 仍用 999 验证拒绝)。
工具链已重建并安装(协议 2,内嵌 blocks_ref/doc_ref/content_ref),
回滚副本 plugindev.bak-20260910-232544。

验证:-race 全绿。新增 KnowledgeAddViaArena(12000B 正文)、
KnowledgeAddInline、DocInsertViaArena、SetToolBlocks 三例 +
真实模板 e2e + 协议不匹配断言强化。

§13.13 第 5 条(反向大结果)范围更大——需把内核→插件的应答路径整体改成
“大结果写段 + 返回 ref”,涉及 callCore 的返回处理与 doc.query/llm.chat 等
所有读大结果的 method。已在 plan.md 标注未做,不冒充完成。
This commit is contained in:
JianFeeeee
2026-09-10 23:26:16 +08:00
parent 15d912ef34
commit 50ba4a64cb
13 changed files with 252 additions and 37 deletions

View File

@ -187,7 +187,7 @@ func main() {
shm = m[ctxOff : ctxOff+ctxSize]
}
send(response{ID: req.ID, Result: map[string]interface{}{
"protocol": 1, "sdk_version": "test",
"protocol": 2, "sdk_version": "test",
"plugin_name": "append-" + tag, "pid": os.Getpid(),
}})

View File

@ -95,7 +95,7 @@ func main() {
switch req.Method {
case "handshake":
send(response{ID: req.ID, Result: map[string]interface{}{
"protocol": 1,
"protocol": 2,
"sdk_version": "test",
"plugin_name": "cb",
"pid": os.Getpid(),

View File

@ -40,7 +40,7 @@ func main() {
switch req.Method {
case "handshake":
send(response{ID: req.ID, Result: map[string]interface{}{
"protocol": 1, "sdk_version": "test", "plugin_name": "crash", "pid": os.Getpid(),
"protocol": 2, "sdk_version": "test", "plugin_name": "crash", "pid": os.Getpid(),
}})
case "tool.invoke":
// 模拟插件 bug直接 panic进程带非零码退出

View File

@ -43,7 +43,7 @@ func main() {
switch req.Method {
case "handshake":
send(response{ID: req.ID, Result: map[string]interface{}{
"protocol": 1,
"protocol": 2,
"sdk_version": "test",
"plugin_name": "echo",
"pid": os.Getpid(),

View File

@ -57,7 +57,7 @@ func main() {
switch req.Method {
case "handshake":
send(response{ID: req.ID, Result: map[string]interface{}{
"protocol": 1, "sdk_version": "test", "plugin_name": "fork", "pid": os.Getpid(),
"protocol": 2, "sdk_version": "test", "plugin_name": "fork", "pid": os.Getpid(),
}})
case "tool.invoke":
// 不回应答,直接退出:模拟插件突然死亡(崩溃/被 kill

View File

@ -42,7 +42,7 @@ func main() {
switch req.Method {
case "handshake":
send(response{ID: req.ID, Result: map[string]interface{}{
"protocol": 1, "sdk_version": "test", "plugin_name": "hang", "pid": os.Getpid(),
"protocol": 2, "sdk_version": "test", "plugin_name": "hang", "pid": os.Getpid(),
}})
case "tool.invoke":
// 永久卡住,永不回应答

View File

@ -139,7 +139,7 @@ func main() {
shm = m[ctxOff : ctxOff+ctxSize]
}
send(response{ID: req.ID, Result: map[string]interface{}{
"protocol": 1, "sdk_version": "test", "plugin_name": "readonly", "pid": os.Getpid(),
"protocol": 2, "sdk_version": "test", "plugin_name": "readonly", "pid": os.Getpid(),
}})
case "plugin.init":

View File

@ -354,7 +354,7 @@ func main() {
shm = m[ctxOff : ctxOff+ctxSize]
}
send(response{ID: req.ID, Result: map[string]interface{}{
"protocol": 1, "sdk_version": "test", "plugin_name": "stage", "pid": os.Getpid(),
"protocol": 2, "sdk_version": "test", "plugin_name": "stage", "pid": os.Getpid(),
}})
case "plugin.init":
@ -540,12 +540,12 @@ func main() {
args := p.Args
if !p.Frame.IsZero() {
if blob := frameInput(p.Frame, p.ArgsLen); len(blob) > 0 {
var decoded map[string]interface{}
if err := json.Unmarshal(blob, &decoded); err != nil {
send(response{ID: id, Error: "解析输出参数: " + err.Error()})
return
}
args = decoded
var decoded map[string]interface{}
if err := json.Unmarshal(blob, &decoded); err != nil {
send(response{ID: id, Error: "解析输出参数: " + err.Error()})
return
}
args = decoded
}
}
payload, _ := args["payload"].(string)