* 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>
135 lines
4.0 KiB
Python
135 lines
4.0 KiB
Python
"""Tests for the configuration system (issue #5, MASTER_SPEC §23)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import dataclasses
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from compiler.common.config import Config, ConfigError, load_config
|
|
from compiler.common.result import Severity
|
|
|
|
# The canonical example from MASTER_SPEC §23.2.
|
|
EXAMPLE_CONFIG = {
|
|
"schema": "1.0",
|
|
"analyzer": {
|
|
"backend": "ollama",
|
|
"model": "llama3",
|
|
"temperature": 0.0,
|
|
"max_retries": 3,
|
|
"timeout": 60,
|
|
},
|
|
"resolver": {
|
|
"knowledge_base": "knowledge_base/",
|
|
"strict_mode": True,
|
|
"allow_aliases": True,
|
|
"expansion_enabled": True,
|
|
"max_expansion_depth": 8,
|
|
"include_nsfw": False,
|
|
},
|
|
"validator": {"allow_unknown_fields": False},
|
|
"prompt_builder": {
|
|
"target": "easy_illustrious",
|
|
"separator": ",",
|
|
"trim_empty_outputs": True,
|
|
"remove_duplicate_tags": True,
|
|
},
|
|
"debug": {"enabled": False, "level": "basic"},
|
|
}
|
|
|
|
|
|
# --- defaults --------------------------------------------------------------
|
|
|
|
|
|
def test_defaults_match_section_23_2() -> None:
|
|
c = Config()
|
|
assert c.schema == "1.0"
|
|
assert c.analyzer.backend == "ollama"
|
|
assert c.analyzer.temperature == pytest.approx(0.0)
|
|
assert c.analyzer.max_retries == 3
|
|
assert c.analyzer.timeout == 60
|
|
assert c.resolver.knowledge_base == "knowledge_base/"
|
|
assert c.resolver.max_expansion_depth == 8
|
|
assert c.resolver.strict_mode is True
|
|
assert c.validator.allow_unknown_fields is False
|
|
assert c.prompt_builder.separator == ","
|
|
assert c.prompt_builder.target == "easy_illustrious"
|
|
assert c.debug.enabled is False
|
|
assert c.debug.level == "basic"
|
|
|
|
|
|
def test_defaults_equal_full_example() -> None:
|
|
assert Config() == Config.from_json(EXAMPLE_CONFIG)
|
|
|
|
|
|
# --- load ------------------------------------------------------------------
|
|
|
|
|
|
def test_full_example_loads_and_roundtrips() -> None:
|
|
c = Config.from_json(EXAMPLE_CONFIG)
|
|
assert c.to_json() == EXAMPLE_CONFIG
|
|
|
|
|
|
def test_empty_config_uses_all_defaults() -> None:
|
|
assert Config.from_json({}) == Config()
|
|
|
|
|
|
def test_partial_config_fills_defaults() -> None:
|
|
c = Config.from_json({"analyzer": {"model": "mistral"}})
|
|
assert c.analyzer.model == "mistral"
|
|
assert c.analyzer.temperature == pytest.approx(0.0) # default preserved
|
|
assert c.resolver.max_expansion_depth == 8
|
|
|
|
|
|
def test_load_config_reads_file(tmp_path) -> None:
|
|
path = tmp_path / "config.json"
|
|
path.write_text(json.dumps(EXAMPLE_CONFIG), encoding="utf-8")
|
|
assert load_config(path) == Config.from_json(EXAMPLE_CONFIG)
|
|
|
|
|
|
# --- invalid config -> SC0014 ----------------------------------------------
|
|
|
|
|
|
def test_wrong_type_rejected_with_sc0014() -> None:
|
|
with pytest.raises(ConfigError) as exc:
|
|
Config.from_json({"analyzer": {"temperature": "hot"}})
|
|
assert exc.value.message.code == "SC0014"
|
|
assert exc.value.message.severity is Severity.FATAL
|
|
|
|
|
|
def test_unknown_field_rejected() -> None:
|
|
with pytest.raises(ConfigError):
|
|
Config.from_json({"analyzer": {"unknown_option": 1}})
|
|
|
|
|
|
def test_random_seed_option_is_forbidden() -> None:
|
|
with pytest.raises(ConfigError) as exc:
|
|
Config.from_json({"analyzer": {"seed": 42}})
|
|
assert exc.value.message.code == "SC0014"
|
|
|
|
|
|
def test_negative_temperature_rejected() -> None:
|
|
with pytest.raises(ConfigError):
|
|
Config.from_json({"analyzer": {"temperature": -1.0}})
|
|
|
|
|
|
# --- immutability ----------------------------------------------------------
|
|
|
|
|
|
def test_config_is_immutable() -> None:
|
|
with pytest.raises(dataclasses.FrozenInstanceError):
|
|
Config().analyzer.temperature = 1.0 # type: ignore[misc]
|
|
|
|
|
|
# --- shipped default file --------------------------------------------------
|
|
|
|
|
|
def test_shipped_default_config_file_matches_defaults() -> None:
|
|
from pathlib import Path
|
|
|
|
repo_root = Path(__file__).resolve().parent.parent
|
|
default_file = repo_root / "config" / "default_config.json"
|
|
data = json.loads(default_file.read_text(encoding="utf-8"))
|
|
assert Config.from_json(data) == Config()
|