feat: heal dead sync channels and block deleting system default (Z12h)

Ensure default channel and rebind Agent/Binding on list, ticket, agents/me, and ensure-binding so SyncPage does not stay on deleted-channel errors.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
whm
2026-08-06 08:57:30 +08:00
parent 2a7da53769
commit 362dcee242
8 changed files with 309 additions and 85 deletions

View File

@@ -3,6 +3,7 @@ package applogic
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
@@ -14,7 +15,7 @@ import (
"aijianzhan/platform/internal/userstore"
)
// AgentMe Z12b智能体自查绑定(无需「管理智能体」)
// AgentMe Z12b/Z12h:智能体自查绑定;通道缺失时自愈
func (l *AuthLogic) AgentMe() (*agentstore.Account, error) {
if !authx.IsAgent(authx.Role(l.ctx)) {
return nil, fmt.Errorf("仅智能体可访问")
@@ -28,6 +29,13 @@ func (l *AuthLogic) AgentMe() (*agentstore.Account, error) {
if err != nil {
return nil, err
}
if l.svcCtx.DBSync != nil {
if _, hErr := HealTenantSyncBind(l.ctx, l.svcCtx, tid); hErr != nil {
log.Printf("agent me heal: %v", hErr)
} else if refreshed, gErr := l.svcCtx.Agents.Get(l.ctx, tid, aid); gErr == nil && refreshed != nil {
acc = refreshed
}
}
return acc, nil
}
@@ -52,30 +60,13 @@ func (l *AuthLogic) CreateBindCode(req BindCodeCreateReq) (*bindcodestore.BindCo
online := strings.TrimSpace(req.OnlineDBID)
dbName := strings.TrimSpace(req.DatabaseName)
if channelID == "" && l.svcCtx.DBSync != nil {
if ch, err := l.svcCtx.DBSync.Store().FindSystemDefaultChannel(tid); err == nil && ch != nil {
channelID = ch.ID
// Z12h生成绑定码前确保默认通道存在含误删自愈
if healed, err := HealTenantSyncBind(l.ctx, l.svcCtx, tid); err != nil {
return nil, fmt.Errorf("无默认同步通道:%w", err)
} else if healed != nil && strings.TrimSpace(healed.ChannelID) != "" {
channelID = healed.ChannelID
if online == "" {
online = dbsync.ResolveOnlineDBID("", ch.ID)
}
_ = l.svcCtx.DBSync.StartChannel(ch.ID) // 已有默认同步通道也保持运行中
} else {
// 尝试创建默认同步通道
cfg := l.svcCtx.Config.DBSync
driver := dbsync.Driver(strings.TrimSpace(cfg.DefaultRemoteDriver))
if driver == "" {
driver = dbsync.DriverPostgres
}
saved, err := l.svcCtx.DBSync.EnsureAndStartSystemDefaultChannel(dbsync.DefaultChannelOpts{
TenantID: tid,
RemoteDriver: driver,
RemoteDSN: strings.TrimSpace(cfg.DefaultRemoteDSN),
})
if err != nil {
return nil, fmt.Errorf("无默认同步通道:%w", err)
}
channelID = saved.ID
if online == "" {
online = dbsync.ResolveOnlineDBID("", saved.ID)
online = dbsync.ResolveOnlineDBID("", channelID)
}
}
}