feat(tags): protective alias creation per observed tagger category with kind fallback

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-16 13:39:16 -04:00
parent 33efba8bf5
commit 8fbeabe9ba
2 changed files with 144 additions and 1 deletions
+44 -1
View File
@@ -412,4 +412,47 @@ class TagService:
async def _create_protective_aliases(
self, src_name: str, src_kind: TagKind, tgt: int
) -> bool:
return False
"""One alias per category the tagger has actually emitted for
src_name (so future predictions resolve to target); fall back to
the tag's kind when the tagger never predicted this exact name.
Idempotent — never clobbers a pre-existing operator alias."""
from ..models.tag_alias import TagAlias
rows = (
await self.session.execute(
text(
"SELECT DISTINCT "
" (tagger_predictions::jsonb -> :n ->> 'category') "
" AS cat "
"FROM image_record "
"WHERE tagger_predictions IS NOT NULL "
" AND (tagger_predictions::jsonb) ? :n"
),
{"n": src_name},
)
).all()
categories = {r.cat for r in rows if r.cat}
if not categories:
kind_val = (
src_kind.value
if hasattr(src_kind, "value")
else str(src_kind)
)
categories = {kind_val}
created = False
for cat in categories:
res = await self.session.execute(
pg_insert(TagAlias)
.values(
alias_string=src_name,
alias_category=cat,
canonical_tag_id=tgt,
)
.on_conflict_do_nothing(
index_elements=["alias_string", "alias_category"]
)
)
if res.rowcount:
created = True
return created