Restrict priority demotion to same-model peers

This commit is contained in:
2026-05-13 08:37:39 +08:00
parent e778fad6ed
commit 2685450f3e
5 changed files with 82 additions and 14 deletions
+36 -8
View File
@@ -159,21 +159,49 @@ WHERE id = $1::uuid`, platformModelID, cooldownSeconds)
return err
}
func (s *Store) DemoteCandidatePlatformPriority(ctx context.Context, platformID string) (int, error) {
if strings.TrimSpace(platformID) == "" {
func (s *Store) DemoteCandidatePlatformPriority(ctx context.Context, platformID string, platformModelID string, requestedModel string, modelType string) (int, error) {
if strings.TrimSpace(platformID) == "" || strings.TrimSpace(platformModelID) == "" || strings.TrimSpace(requestedModel) == "" {
return 0, nil
}
var dynamicPriority int
err := s.pool.QueryRow(ctx, `
WITH current_model AS (
SELECT id, platform_id
FROM platform_models
WHERE id = $2::uuid
AND platform_id = $1::uuid
),
peer_priority AS (
SELECT MAX(COALESCE(peer.dynamic_priority, peer.priority)) AS max_priority
FROM current_model current
JOIN platform_models peer_model ON peer_model.platform_id <> current.platform_id
JOIN integration_platforms peer ON peer.id = peer_model.platform_id
LEFT JOIN base_model_catalog peer_base ON peer_base.id = peer_model.base_model_id
WHERE peer.status = 'enabled'
AND peer.deleted_at IS NULL
AND (peer.cooldown_until IS NULL OR peer.cooldown_until <= now())
AND peer_model.enabled = true
AND (peer_model.cooldown_until IS NULL OR peer_model.cooldown_until <= now())
AND (NULLIF($4::text, '') IS NULL OR peer_model.model_type @> jsonb_build_array($4::text))
AND (
(COALESCE(peer_model.model_alias, '') <> '' AND peer_model.model_alias = $3::text)
OR (
COALESCE(peer_model.model_alias, '') = ''
AND (
peer_model.model_name = $3::text
OR peer_base.canonical_model_key = $3::text
OR peer_base.provider_model_name = $3::text
)
)
)
)
UPDATE integration_platforms target
SET dynamic_priority = COALESCE((
SELECT MAX(COALESCE(peer.dynamic_priority, peer.priority))
FROM integration_platforms peer
WHERE peer.deleted_at IS NULL
), target.priority) + 1,
SET dynamic_priority = COALESCE((SELECT max_priority FROM peer_priority), target.priority) + 1,
updated_at = now()
WHERE target.id = $1::uuid
RETURNING dynamic_priority`, platformID).Scan(&dynamicPriority)
AND target.deleted_at IS NULL
AND EXISTS (SELECT 1 FROM current_model)
RETURNING dynamic_priority`, platformID, platformModelID, requestedModel, modelType).Scan(&dynamicPriority)
return dynamicPriority, err
}