mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-03 15:53:56 +00:00
fix(remotedevice): 设备通道名改用 - 分隔并派生合规名(v1.3.0 部署后 agent 完全不应答的根因)
## 事故
v1.3.0 部署到生产后,**整个 agent 不应答**:任何对话都返回
`all 3 providers failed, last error: api error 403: model "claude-opus-5" is not allowed for this key`。
回滚到 1.2.2 立即恢复(部署前 403=0/成功对话=10,部署后 403=5/成功对话=0)。
## 根因(网关日志给出的原文)
```
tier 3 gozen/deepseek-v4.1-flash: api error 400: [invalid_request_error]
Invalid 'tools[299].function.name': string does not match pattern '^[a-zA...
```
设备的每设备输出通道名叫 `device/<id>`,内核按 `output_send__<通道名>` 生成工具 ⇒
`output_send__device/<id>` 里的 `/` 违反上游函数名规范 `^[a-zA-Z0-9_-]{1,64}$`。
上游不是"拒掉这一个工具",而是**整条请求 400** ⇒ 网关 auto tier 全链条失败
(400/429/503 混在一起)⇒ 内核只能报"所有 provider 都失败"。
两台真实设备(waiter-fnnas / waiter-mainnas)一上线就登记了这种通道,于是必然触发。
## 修法(改插件,不改内核)
初版我在内核里加了"通道名净化 + 反向解析"层。用户否掉了这个方向,理由对:
**通道名是插件自己的声明,不合契约就该改插件**,不该让内核替插件擦屁股。
内核侧改动已全部回退(HEAD 干净)。
插件侧两处:
1. 分隔符 `device/<id>` → `device-<id>`(源码与来源标签统一,不留两套名字)。
2. 设备 id 是**外部输入**(设备自己声明),可能含空格/非 ASCII/超长 ⇒
`deviceChannelName()` 把它派生为**合规且唯一**的通道名:
保留 `[A-Za-z0-9_-]`、其它折成 `-`、主体截断到 32 字符(预算 64 = 13+7+32+7+…)、
发生截断或撞名时追加 id 的 6 位短哈希。同一 id 恒定同名;真名仍用于路由与日志。
核心契约写进了插件注释与 SDK 文档(见 SDK 仓同批提交):名字若来自外部输入,
**在插件侧派生合规名**,内核不会替你净化。
## 验证
- 新增 `TestDeviceChannelNameIsLLMFunctionNameSafe`:恶意 id(空格/符号/非 ASCII/超长/
会折成同名的两个 id)都必须派生出**合法且互不重复**的通道名与工具名。
反向验证:把分隔符改回 `/` 即 FAIL。
- 生产两台设备派生结果:`device-waiter-fnnas`、`device-waiter-mainnas`
⇒工具名 `output_send__device-waiter-fnnas`(37 字符,合规)。
- 全量 `go test ./...` = 37 包 ok / 0 FAIL;`-race`(remotedevice + core)无 DATA RACE。
This commit is contained in:
@ -9,6 +9,8 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@ -130,7 +132,7 @@ func TestDeviceChannelLifecycleAndPush(t *testing.T) {
|
||||
cli.sendText([]byte(`{"op":"hello","device":{"device_id":"spk-1","name":"音箱","kind":"speaker","caps":["speaker"]}}`))
|
||||
cli.readHelloAckAndBind(t, token)
|
||||
|
||||
ch := deviceChannelName("spk-1")
|
||||
ch := p.deviceChannelName("spk-1")
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
caps, ok := rec.caps(ch)
|
||||
for !ok && time.Now().Before(deadline) {
|
||||
@ -255,3 +257,31 @@ func TestDevicectlAggregateOutputAddressing(t *testing.T) {
|
||||
t.Fatal("不存在的设备应报错")
|
||||
}
|
||||
}
|
||||
|
||||
// 通道名合规性:设备通道名会被内核拼进 LLM **函数名**(output_send__<通道名>),
|
||||
// 而上游函数名规范是 ^[a-zA-Z0-9_-]{1,64}$ —— 违规会让**整条请求**被 400 拒绝
|
||||
// (实测把生产打挂:device/<id> 里的 `/` 触发 Invalid 'tools[299].function.name',
|
||||
// 网关 auto tier 全链条失败,整个 agent 不说话了)。
|
||||
//
|
||||
// 通道名是**插件自己的声明**,所以这条判据钉在插件侧。
|
||||
func TestDeviceChannelNameIsLLMFunctionNameSafe(t *testing.T) {
|
||||
re := regexp.MustCompile(`^[a-zA-Z0-9_-]{1,64}$`)
|
||||
// 含**恶意/异常** id:空格、符号、非 ASCII、超长、以及会折成同一个名字的两个 id
|
||||
ids := []string{"waiter-fnnas", "1", "a b!c", "中文设备", strings.Repeat("x", 120), "a b", "a-b"}
|
||||
p := &Plugin{}
|
||||
seen := map[string]string{}
|
||||
for _, id := range ids {
|
||||
ch := p.deviceChannelName(id)
|
||||
if prev, dup := seen[ch]; dup {
|
||||
t.Errorf("不同设备 id(%q 与 %q)派生出同一个通道名 %q", prev, id, ch)
|
||||
}
|
||||
seen[ch] = id
|
||||
if !re.MatchString(ch) {
|
||||
t.Errorf("设备通道名 %q 违反上游函数名规范 %s", ch, re)
|
||||
}
|
||||
toolName := "output_send__" + ch
|
||||
if !re.MatchString(toolName) {
|
||||
t.Errorf("派生出的工具名 %q 违反上游函数名规范 %s", toolName, re)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user