fix(onboarding): 租户序列避让保留 ID 并为存量空间补齐开箱默认库
deploy-hxtesting / build-and-deploy (push) Successful in 1m15s

根因(26040006 等首用户空间零知识库):
- 000000 初始迁移把 tenants_id_seq 起点设为 10000,恰好等于内置模型宿主
  保留租户 id(DefaultBuiltinModelTenantID=10000):全新环境的第一个用户
  空间必然撞上 SeedTenantDefaults 的「永不 seed」豁免,一个默认库都没有;
  后续空间(10001+)正常。内置模型行(tenant_id=10000)被真实空间占住,
  还存在归属歧义隐患
- 注册引导是创建时一次性动作,存量空间因引导当时失败(如模型未就绪)或
  上述碰撞而缺库时,此后永不补建

修复:
- migration 000160:租户序列推到 10001 之后——全新库从 10001 起号(10000
  永久保留给内置模型宿主),存量库按 max(id) 连续续号不受影响
- SeedTenantDefaults 拆出 seedStarterKB 供创建时引导与自愈共用;保留租户
  豁免注释同步更新
- 新增 EnsureTenantDefaultKnowledgeBases 惰性自愈:挂在知识库列表读路径
  (幂等/fail-open/进程内互斥防并发重复建档)——企业培训课件/题库两库恒补
  (Source 去重),「新手入门」仅零库空间补建(不复活用户主动删除的入门库)

效果:26040006(租户 10000)等存量用户打开「知识库」页即自动补齐 3 个默认
库;全新环境的首个用户空间不再撞保留 id
This commit is contained in:
runny committed 2026-11-24 11:10:14 +08:00
1 parent 42042ca8bd
commit 196478114c
5 files changed
+177 -24

No files matched your search

+13
View File
@@ -48,6 +48,9 @@ type KnowledgeBaseHandler struct {
// default handle mode is available.
fileService interfaces.FileService
storageResolver interfaces.StorageBackendResolver
// bootstrap 幂等自愈空间开箱默认库(新手入门/企业培训课件/题库),挂在
// 知识库列表读路径上补齐存量空间缺失的默认库;可为 nil(测试桩)。
bootstrap interfaces.TenantBootstrapService
}
// NewKnowledgeBaseHandler creates a new knowledge base handler instance
@@ -63,6 +66,7 @@ func NewKnowledgeBaseHandler(
fileService interfaces.FileService,
storageResolver interfaces.StorageBackendResolver,
profileService interfaces.KnowledgeBaseProfileService,
bootstrap interfaces.TenantBootstrapService,
) *KnowledgeBaseHandler {
return &KnowledgeBaseHandler{
cfg: cfg,
@@ -76,6 +80,7 @@ func NewKnowledgeBaseHandler(
userService: userService,
fileService: fileService,
storageResolver: storageResolver,
bootstrap: bootstrap,
}
}
@@ -639,6 +644,14 @@ func (h *KnowledgeBaseHandler) ListKnowledgeBases(c *gin.Context) {
return
}
// 开箱默认库惰性自愈(幂等/fail-open):注册引导是一次性动作,存量空间因
// 引导当时失败或保留租户 id 碰撞(migration 000160 前首空间占住 10000)
// 而缺失默认库时,访问列表即补齐——培训课件/题库两库恒补,新手入门仅
// 零库空间补建。补建后本次查询即可见。
if h.bootstrap != nil {
h.bootstrap.EnsureTenantDefaultKnowledgeBases(ctx)
}
// Get all knowledge bases for this tenant
kbs, err := h.service.ListKnowledgeBases(ctx)
if err != nil {