fix(aliases): store modal alias under raw model key + make aliases visible/manageable
The headline bug: aliases created from the modal NEVER resolved. Create
sent the normalized display name ('Sword', 'Uchiha Sasuke') while
resolution keys on the raw booru model key ('sword', 'uchiha_sasuke',
case-sensitive) — so the mapping was stored under a key nothing looks up,
and the prediction kept reappearing unaliased. The raw key wasn't even in
the /suggestions response, so the modal couldn't send it.
- Suggestion now carries raw_name (the model key an alias must use) and
via_alias (surfaced via an operator alias); both serialized by the API.
- Modal alias-create sends raw_name, not display_name (the fix). Aliased
suggestions show an 'alias' badge and a 'Remove alias' action; 'Treat as
alias for…' is hidden for centroid hits (no model key) and already-aliased
rows.
- Tag-side management: TagCard ⋮ → 'Aliases…' opens a dialog listing the
model keys that fold into a tag, with remove (GET /api/tags/<id>/aliases +
AliasService.list_for_tag). Creation stays in the modal suggestion flow.
Tests: full API round-trip locking the raw-key contract (raw_name exposed →
alias authored with it → resolves + via_alias on a later image);
list_for_tag (service + API); via_alias/raw_name on the existing service
suggestion tests. No migration.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -37,6 +37,11 @@ async def get_suggestions(image_id: int):
|
||||
"score": round(s.score, 4),
|
||||
"source": s.source,
|
||||
"creates_new_tag": s.creates_new_tag,
|
||||
# raw model key (alias is stored under this) + whether an
|
||||
# operator alias produced this suggestion — drive the
|
||||
# modal's "Treat as alias"/"Remove alias" affordances.
|
||||
"raw_name": s.raw_name,
|
||||
"via_alias": s.via_alias,
|
||||
}
|
||||
for s in items
|
||||
]
|
||||
|
||||
@@ -8,6 +8,7 @@ from ..extensions import get_session
|
||||
from ..models import Tag, TagKind
|
||||
from ..models.tag_allowlist import TagAllowlist
|
||||
from ..services.bulk_tag_service import BulkTagService
|
||||
from ..services.ml.aliases import AliasService
|
||||
from ..services.series_match_service import SeriesMatchService
|
||||
from ..services.series_service import SeriesError, SeriesService
|
||||
from ..services.tag_directory_service import TagDirectoryService
|
||||
@@ -200,6 +201,25 @@ async def get_tag(tag_id: int):
|
||||
)
|
||||
|
||||
|
||||
@tags_bp.route("/tags/<int:tag_id>/aliases", methods=["GET"])
|
||||
async def list_tag_aliases(tag_id: int):
|
||||
"""Model keys that fold into this tag (tag-side alias view). Remove via the
|
||||
shared DELETE /api/aliases/<string>/<category>."""
|
||||
async with get_session() as session:
|
||||
if await session.get(Tag, tag_id) is None:
|
||||
return jsonify({"error": "tag not found"}), 404
|
||||
rows = await AliasService(session).list_for_tag(tag_id)
|
||||
return jsonify(
|
||||
[
|
||||
{
|
||||
"alias_string": r.alias_string,
|
||||
"alias_category": r.alias_category,
|
||||
}
|
||||
for r in rows
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@tags_bp.route("/tags/<int:tag_id>", methods=["PATCH"])
|
||||
async def update_tag(tag_id: int):
|
||||
"""Rename and/or re-fandom a tag. Body may carry `name` and/or
|
||||
|
||||
@@ -81,6 +81,31 @@ class AliasService:
|
||||
.where(TagAlias.alias_category == alias_category)
|
||||
)
|
||||
|
||||
async def list_for_tag(self, canonical_tag_id: int) -> Sequence[AliasRow]:
|
||||
"""Aliases that resolve TO this tag — drives the tag-side 'Aliases'
|
||||
view (see/remove the model keys that fold into a tag)."""
|
||||
stmt = (
|
||||
select(
|
||||
TagAlias.alias_string,
|
||||
TagAlias.alias_category,
|
||||
TagAlias.canonical_tag_id,
|
||||
Tag.name,
|
||||
)
|
||||
.join(Tag, Tag.id == TagAlias.canonical_tag_id)
|
||||
.where(TagAlias.canonical_tag_id == canonical_tag_id)
|
||||
.order_by(TagAlias.alias_string.asc())
|
||||
)
|
||||
rows = (await self.session.execute(stmt)).all()
|
||||
return [
|
||||
AliasRow(
|
||||
alias_string=r[0],
|
||||
alias_category=r[1],
|
||||
canonical_tag_id=r[2],
|
||||
canonical_tag_name=r[3],
|
||||
)
|
||||
for r in rows
|
||||
]
|
||||
|
||||
async def list_all(self) -> Sequence[AliasRow]:
|
||||
stmt = (
|
||||
select(
|
||||
|
||||
@@ -31,6 +31,14 @@ class Suggestion:
|
||||
score: float
|
||||
source: str # 'tagger' | 'centroid' | 'both'
|
||||
creates_new_tag: bool
|
||||
# raw_name = the booru model vocab key behind this suggestion. It's the key
|
||||
# an alias MUST be stored under (resolution looks up the raw key), so the
|
||||
# modal needs it to author an alias correctly. None for centroid-only hits
|
||||
# (no underlying prediction → nothing to alias).
|
||||
raw_name: str | None = None
|
||||
# via_alias = this suggestion was surfaced because an operator alias remapped
|
||||
# the raw prediction to this canonical tag. Lets the UI mark it + offer undo.
|
||||
via_alias: bool = False
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -161,6 +169,11 @@ class SuggestionService:
|
||||
if existing.source != sug.source
|
||||
else existing.source,
|
||||
creates_new_tag=existing.creates_new_tag,
|
||||
# Keep the alias identity from `existing`: the tagger pass
|
||||
# (which carries raw_name / via_alias) runs before centroid
|
||||
# augmentation, so it's always the first writer for a key.
|
||||
raw_name=existing.raw_name,
|
||||
via_alias=existing.via_alias,
|
||||
)
|
||||
|
||||
for raw, display, category, conf in candidates:
|
||||
@@ -177,6 +190,8 @@ class SuggestionService:
|
||||
score=conf,
|
||||
source="tagger",
|
||||
creates_new_tag=False,
|
||||
raw_name=raw,
|
||||
via_alias=True,
|
||||
),
|
||||
)
|
||||
else:
|
||||
@@ -208,6 +223,8 @@ class SuggestionService:
|
||||
score=conf,
|
||||
source="tagger",
|
||||
creates_new_tag=False,
|
||||
raw_name=raw,
|
||||
via_alias=False,
|
||||
),
|
||||
)
|
||||
else:
|
||||
@@ -220,6 +237,8 @@ class SuggestionService:
|
||||
score=conf,
|
||||
source="tagger",
|
||||
creates_new_tag=True,
|
||||
raw_name=raw,
|
||||
via_alias=False,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user