LoopForge icon indicating copy to clipboard operation
LoopForge copied to clipboard

[Bug]: 纯中文角色 ID 派生 subagent 名退化为 role-,多个角色会撞名

Open kixuan opened this issue 1 week ago • 1 comments

Component / host

Platform — web / API

Edition

Platform

LoopForge version

main @ d4e85440 (2026-11-26);同一段代码在 v0.4.2 也存在。

What happened?

角色 ID 允许中文:internal/domain/resource_id.go 的 ValidateResourceID 只拒绝空格、控制字符和 / \ ? # %。但角色作为子 Agent 派发时,subagent 名由 roleSubagentName 从 ID 派生:

// internal/httpapi/assistant_subagents.go
var roleSubagentUnsafe = regexp.MustCompile(`[^a-z0-9]+`)

func roleSubagentName(id string) string {
    name := strings.Trim(roleSubagentUnsafe.ReplaceAllString(strings.ToLower(id), "-"), "-")
    return "role-" + name
}

纯中文 ID 会被全部替换成 -,再 trim 成空,最终得到 role-。于是:

  • 两个中文角色(例如 产品经理、后端工程师)都变成 subagent_type=role-,Main Bot 无法区分;
  • renderRoleAgentFile 写出的 name: 都是 role-;
  • writeRoleAgentFiles 写到同一个 agents/role-.md,后一个覆盖前一个;
  • roleAgentIDs 以 subagent 名为 key,resume 映射也会串到另一个角色。

Reproduction

  1. 打开项目配置 → 角色定义,新建两个角色,ID 分别填 产品经理、后端工程师(两者都是合法 ID),名称和提示词随便填。
  2. 在对话里 @ 这两个角色,或让 Main Bot 用 subagent_type=role-产品经理 派发。
  3. 查看 workbench prompt、agents/ 目录或诊断信息:两个角色都是 role-,agents/role-.md 只剩下最后写入的那个。

也可以直接验证:

roleSubagentName("产品经理") == "role-"
roleSubagentName("后端工程师") == "role-"

Expected

每个角色得到稳定、唯一、ASCII 的 subagent 名,中文 ID 不应退化为 role-。例如:

  • role-<slug>-<short-hash>;或
  • sanitize 后为空时,用 role-<sha256(id) 前 8 位> 兜底。

Suggested fix

在 roleSubagentName 里,当 sanitize 结果为空时追加原 ID 的稳定短 hash(不必引入拼音库):

name := strings.Trim(roleSubagentUnsafe.ReplaceAllString(strings.ToLower(id), "-"), "-")
if name == "" {
    sum := sha256.Sum256([]byte(id))
    name = hex.EncodeToString(sum[:4])
}
return "role-" + name

并补测试:

  • 纯中文:产品经理、后端工程师 得到不同名字;
  • 中英混合、纯 ASCII 保持现有行为;
  • 两个不同 ID 不碰撞;
  • renderRoleAgentFile 的 name: 与文件名一致。

Impact

使用中文角色 ID 的项目里,多角色场景下子 Agent 会派发到错误角色、resume 串线、角色文件互相覆盖。不是安全问题,但会让多角色协作不可用。

Safety checks

  • [x] 已搜索现有 issue,未发现重复
  • [x] 已移除凭据、私有地址和内部数据

kixuan avatar Nov 26 '26 12:11 kixuan

感谢反馈。我的理解是:角色 ID 可以用纯中文,但派成子 Agent 时名字会收成同一个 role-,多个中文角色可能分不清,角色文件也会写到同一处。

已经看到这条 Issue,会按现有排期查看。版本、复现步骤和期望结果都写清楚了。

cocofhu00 avatar Nov 26 '26 13:11 cocofhu00