fix(register): require explicit topic when ambiguous

Assistant: codex
Assistant-Model: gpt-5.6-sol
Assistant-Session: 01a06d83-1cbc-71f2-b0dc-e0f48cedae43
This commit is contained in:
tegwick 2026-09-04 21:16:07 +02:00
parent 803bb95e1d
commit e663f209f7
4 changed files with 209 additions and 4 deletions

View file

@ -250,7 +250,14 @@ def run_register(args: argparse.Namespace) -> None:
if domain not in domain_slugs:
domain = _ask_for_domain(domain, domain_slugs)
topic = _find_or_create_topic(domain, snapshot.project_name, repo_slug, inference, args.api_base)
topic = _find_or_create_topic(
domain,
snapshot.project_name,
repo_slug,
inference,
args.api_base,
requested_topic_slug=getattr(args, "topic", None),
)
topic_id = topic["id"]
topic_slug = topic.get("slug") or domain
@ -831,11 +838,54 @@ def _find_or_create_topic(
repo_slug: str,
inference: RegisterInference,
api_base: str,
*,
requested_topic_slug: str | None = None,
) -> dict[str, Any]:
topics = _api_get("/topics/?status=active", api_base)
existing = next((t for t in topics if t.get("domain_slug") == domain), None)
if existing:
return existing
domain_topics = [topic for topic in topics if topic.get("domain_slug") == domain]
explicit_slug = _slugify(requested_topic_slug or "") or None
inferred_slug = inference.topic_slug
preferred_slug = explicit_slug or inferred_slug
if preferred_slug:
existing = next(
(topic for topic in domain_topics if topic.get("slug") == preferred_slug),
None,
)
if existing:
return existing
if explicit_slug:
cross_domain = [
str(topic.get("domain_slug"))
for topic in topics
if topic.get("slug") == explicit_slug and topic.get("domain_slug") != domain
]
if cross_domain:
domains = ", ".join(sorted(set(cross_domain)))
raise SystemExit(
f"ERROR: Topic '{explicit_slug}' belongs to domain(s) {domains}, "
f"not '{domain}'."
)
available = ", ".join(
sorted(str(topic.get("slug")) for topic in domain_topics if topic.get("slug"))
) or "(none)"
raise SystemExit(
f"ERROR: Active topic '{explicit_slug}' was not found in domain '{domain}'. "
f"Available: {available}"
)
if len(domain_topics) == 1:
return domain_topics[0]
if len(domain_topics) > 1:
available = ", ".join(
sorted(str(topic.get("slug")) for topic in domain_topics if topic.get("slug"))
)
raise SystemExit(
f"ERROR: Domain '{domain}' has multiple active topics: {available}. "
"Pass --topic <slug>."
)
slug = inference.topic_slug or repo_slug
title = inference.topic_title or project_name
print(f"==> Creating active topic '{slug}' for domain '{domain}'")