Files
kunail0804-ComfyUI-Scene-Co…/compiler/common/message_codes.py
T
54b76ca75a fix(compiler): recover lost scene detail via 16.4k Danbooru KB + harden analyzer (#79)
* fix: recover concept detail lost in resolver and harden analyzer parsing

Three shippability fixes for the v1 pipeline:

P2 (main): the Illustrious Resolver did exact-key KB lookup only, so any
compound concept the analyzer emits ("white summer dress", "black business
suit") missed and was dropped silently as SC0001. Add a deterministic
head-noun fallback (§17.2): on a miss, drop leading modifiers and look up the
longest remaining suffix ("white summer dress" -> "dress"). No tag/concept is
invented. Emits SC0019 (Concept Reduced) so the reduction is visible.

P1a: the analyzer required the entire model response to be a single JSON
document, so any surrounding prose ("Sure! Here is the scene:") failed parsing
and, with the prompt reused unchanged, every retry failed identically ->
empty output. Extract the first balanced top-level {...} object from
surrounding text (string-aware brace scan); the object bytes are unchanged and
must still validate, so this is unwrapping, not JSON repair.

P1b: the node's "raw" output was to_raw_json(result.data) -- a re-serialization
of the parsed Scene, so on failure it was empty and the model's actual text was
invisible. Capture the last raw model text in analyzer metadata and surface it
as the node's raw output.

Regenerated goldens: unknown_concepts "hologram jacket" now resolves to
"jacket" via head-noun fallback (intended).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat: add entry rating + resolver.include_nsfw gating

Foundations for the Danbooru-vocab KB: a per-entry `rating` (general|explicit,
default general) and a `resolver.include_nsfw` config flag (default false). The
resolver omits explicit-rated entries from lookup and expansion unless enabled,
so a gated concept is reported as unknown (SC0001). Schema, model, config, and
example config updated.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat: generate KB from embedded Danbooru vocab (188 -> 16.4k tags)

Replace the coverage ceiling of the 188-entry hand KB by importing a large,
validated Danbooru tag vocabulary. This is the fix for the core "detail loss"
symptom: compound concepts the analyzer emits ("red dress", "sunflower field",
"train station platform") are now real tags that resolve instead of being
dropped.

- scripts/build_vocab.py: one-time dev step that filters the Raffle node's
  categorized tag dump (drops artist/character/copyright/attribution categories
  and subjective adjectives, keeps only id-pattern-valid tags) into a committed,
  self-contained snapshot at data/danbooru_vocab.txt (16.4k tags).
- scripts/generate_kb_from_vocab.py: additive, non-destructive generator. Writes
  knowledge_base/gen_<category>.json, mapping the Raffle categories to the 19
  canonical categories and tagging NSFW categories rating=explicit. Skips any tag
  already defined as a hand-curated id/alias, so the curated canonicalization
  (female + aliases) and expansions (school_uniform -> blazer, pleated_skirt) are
  preserved and always win.

Result: 16.4k additive entries + 188 curated. Plain natural-language concepts
now resolve broadly via curated aliases + head-noun fallback with no analyzer
prompt change; NSFW gated behind resolver.include_nsfw; golden and dataset tests
stay green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* style: ruff format build_vocab.py

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(security): drop untrusted CLI path in build_vocab (SonarCloud S8707)

The --source CLI argument fed an untrusted path to the file system (taint).
The script is a one-time dev step; hard-code the source path constant instead.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: kunail0804 <kunail0804@gmail.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-10 19:45:32 +02:00

75 lines
2.7 KiB
Python

"""Central registry of compiler message codes (MASTER_SPEC §21.2, Appendix B).
Every diagnostic :class:`Message` uses a stable code from here, giving one source
of truth for a code's canonical severity and title. Codes SC0001-SC0014 come from
Appendix B; SC0015+ are extensions added as the compiler is implemented and are
intended to be folded back into Appendix B and the wiki.
Use :func:`message` to build a message so the severity and title always match the
code.
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
from compiler.common.result import Message, Severity
@dataclass(frozen=True)
class CodeSpec:
"""The canonical severity and title for a message code."""
severity: Severity
title: str
# Appendix B (SC0001-SC0014).
_APPENDIX_B: dict[str, CodeSpec] = {
"SC0001": CodeSpec(Severity.WARNING, "Unknown Concept"),
"SC0002": CodeSpec(Severity.ERROR, "Invalid Scene JSON"),
"SC0003": CodeSpec(Severity.ERROR, "Circular Expansion"),
"SC0004": CodeSpec(Severity.FATAL, "Knowledge Base Load Failure"),
"SC0005": CodeSpec(Severity.ERROR, "Duplicate Canonical ID"),
"SC0006": CodeSpec(Severity.WARNING, "Deprecated Concept"),
"SC0007": CodeSpec(Severity.WARNING, "Duplicate Tag Removed"),
"SC0008": CodeSpec(Severity.ERROR, "Invalid Category"),
"SC0009": CodeSpec(Severity.ERROR, "Missing Required Field"),
"SC0010": CodeSpec(Severity.ERROR, "Schema Version Mismatch"),
"SC0011": CodeSpec(Severity.ERROR, "Analyzer Schema Validation Failure"),
"SC0012": CodeSpec(Severity.FATAL, "Analyzer Unavailable"),
"SC0013": CodeSpec(Severity.ERROR, "Analyzer Timeout"),
"SC0014": CodeSpec(Severity.FATAL, "Invalid Configuration"),
}
# Extensions beyond Appendix B (to be documented in the spec/wiki).
_EXTENSIONS: dict[str, CodeSpec] = {
"SC0015": CodeSpec(Severity.WARNING, "Unexpected Field Removed"),
"SC0016": CodeSpec(Severity.WARNING, "Empty Concept Removed"),
"SC0017": CodeSpec(Severity.WARNING, "Interaction Dropped"),
"SC0018": CodeSpec(Severity.ERROR, "Analyzer Unexpected Response"),
"SC0019": CodeSpec(Severity.WARNING, "Concept Reduced"),
}
CODES: dict[str, CodeSpec] = {**_APPENDIX_B, **_EXTENSIONS}
def message(code: str, description: str, **context: Any) -> Message:
"""Build a :class:`Message` for a registered code.
The severity and title are taken from the registry so they always match the
code; only the description and optional context vary per call.
Raises:
KeyError: If ``code`` is not registered.
"""
spec = CODES[code]
return Message(
code=code,
severity=spec.severity,
title=spec.title,
description=description,
context=context,
)