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:
@@ -47,45 +47,59 @@ func fillUserSyncOnToken(svcCtx *svc.ServiceContext, resp *types.TokenResp, u *u
|
||||
resp.SyncBound = resp.ChannelID != "" && resp.OnlineDBID != ""
|
||||
}
|
||||
|
||||
// ensureAgentSyncBind Z12c:启用/创建时若无通道则自动创建公司默认同步通道并写回智能体。
|
||||
// ensureAgentSyncBind Z12c/Z12h:无通道或通道已删除时,创建/复用默认同步通道并写回智能体。
|
||||
func (l *AgentAdminLogic) ensureAgentSyncBind(acc *agentstore.Account) (*agentstore.Account, error) {
|
||||
if acc == nil {
|
||||
return nil, fmt.Errorf("agent nil")
|
||||
}
|
||||
if strings.TrimSpace(acc.ChannelID) != "" && strings.TrimSpace(acc.OnlineDBID) != "" {
|
||||
return acc, nil
|
||||
}
|
||||
if l.svcCtx.DBSync == nil {
|
||||
return acc, nil
|
||||
}
|
||||
cfg := l.svcCtx.Config.DBSync
|
||||
driver := dbsync.Driver(strings.TrimSpace(cfg.DefaultRemoteDriver))
|
||||
if driver == "" {
|
||||
driver = dbsync.DriverPostgres
|
||||
alive := channelAlive(l.svcCtx.DBSync.Store(), acc.TenantID, acc.ChannelID)
|
||||
if alive && strings.TrimSpace(acc.OnlineDBID) != "" {
|
||||
return acc, nil
|
||||
}
|
||||
ch, err := l.svcCtx.DBSync.EnsureAndStartSystemDefaultChannel(dbsync.DefaultChannelOpts{
|
||||
TenantID: acc.TenantID,
|
||||
AgentID: acc.AgentID,
|
||||
Name: fmt.Sprintf("默认同步 · %s", acc.Name),
|
||||
RemoteDriver: driver,
|
||||
RemoteDSN: strings.TrimSpace(cfg.DefaultRemoteDSN),
|
||||
DatabaseName: acc.DatabaseName,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ensure default channel: %w", err)
|
||||
}
|
||||
online := dbsync.ResolveOnlineDBID(acc.OnlineDBID, ch.ID)
|
||||
dbName := strings.TrimSpace(acc.DatabaseName)
|
||||
if dbName == "" {
|
||||
dbName = fmt.Sprintf("agent_%d", acc.AgentID)
|
||||
if _, err := HealTenantSyncBind(l.ctx, l.svcCtx, acc.TenantID); err != nil {
|
||||
return nil, fmt.Errorf("heal sync bind: %w", err)
|
||||
}
|
||||
st, err := l.store()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updated, err := st.AttachSyncBind(l.ctx, acc.AgentID, acc.TenantID, ch.ID, online, dbName, acc.Status == agentstore.StatusActive || acc.Status == "")
|
||||
updated, err := st.Get(l.ctx, acc.TenantID, acc.AgentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 若 heal 未覆盖该智能体(例如无 Binding 仅 Agent),再显式挂一次
|
||||
if !channelAlive(l.svcCtx.DBSync.Store(), updated.TenantID, updated.ChannelID) || strings.TrimSpace(updated.OnlineDBID) == "" {
|
||||
cfg := l.svcCtx.Config.DBSync
|
||||
driver := dbsync.Driver(strings.TrimSpace(cfg.DefaultRemoteDriver))
|
||||
if driver == "" {
|
||||
driver = dbsync.DriverPostgres
|
||||
}
|
||||
ch, err := l.svcCtx.DBSync.EnsureAndStartSystemDefaultChannel(dbsync.DefaultChannelOpts{
|
||||
TenantID: acc.TenantID,
|
||||
AgentID: acc.AgentID,
|
||||
Name: fmt.Sprintf("默认同步 · %s", acc.Name),
|
||||
RemoteDriver: driver,
|
||||
RemoteDSN: strings.TrimSpace(cfg.DefaultRemoteDSN),
|
||||
DatabaseName: acc.DatabaseName,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ensure default channel: %w", err)
|
||||
}
|
||||
online := strings.TrimSpace(updated.OnlineDBID)
|
||||
if online == "" {
|
||||
online = dbsync.ResolveOnlineDBID("", ch.ID)
|
||||
}
|
||||
dbName := strings.TrimSpace(updated.DatabaseName)
|
||||
if dbName == "" {
|
||||
dbName = fmt.Sprintf("agent_%d", updated.AgentID)
|
||||
}
|
||||
updated, err = st.AttachSyncBind(l.ctx, updated.AgentID, updated.TenantID, ch.ID, online, dbName, updated.Status == agentstore.StatusActive || updated.Status == "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user