Add support for handling non-identity entity types in detection logic
Deploy Development / deploy (push) Successful in 54s
Test Suite / pytest-backend (push) Successful in 2m37s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 15s
Deploy Production / deploy (push) Successful in 43s

- Introduced a new set of non-identity entity types (FOOD, DISH, MEAL, CUISINE, FOODSTUFF, INGREDIENT) that are omitted from detection results without causing contract violations.
- Updated the `DetectionStats` class to track omitted non-identity types.
- Modified validation logic to ensure that food-related mentions are not treated as identity types, preserving the context of mentions as either PERSON or omitted.
- Enhanced tests to verify the correct handling of food homonyms and ensure that invalid entity types are recorded appropriately without causing errors.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-09-08 07:35:44 +02:00
1 parent 79e4fd0d4b
commit dd7858347c
7 files changed
+170 -22

No files matched your search

+69 -2
View File
@@ -113,6 +113,73 @@ def main() -> None:
expect("[[PERSON:" in (person_sushi.trace.get("egress") or ""), "same word can be a person in another sentence")
expect("Sushi" not in (person_sushi.trace.get("egress") or "").replace("[[PERSON:", ""), "person Sushi is masked")
mixed_homonym = "Ich aß Sushi. Meine Frau Sushi kam später."
food_at = mixed_homonym.find("Sushi")
person_at = mixed_homonym.find("Sushi", food_at + 1)
dish = validate_detected_entity(
{"start": food_at, "end": food_at + 5, "text": "Sushi", "entity_type": "FOOD"},
mixed_homonym,
0,
)
expect(dish is None, "FOOD on a dish mention is omitted, not a contract error")
person_from_food = validate_detected_entity(
{"start": person_at, "end": person_at + 5, "text": "Sushi", "entity_type": "FOOD"},
mixed_homonym,
0,
)
expect(
person_from_food is not None and person_from_food.entity_type == "PERSON",
"FOOD on a kinship mention stays PERSON",
)
arrived = "Sushi kam später."
arrived_span = validate_detected_entity(
{"start": 0, "end": 5, "text": "Sushi", "entity_type": "DISH"},
arrived,
0,
)
expect(
arrived_span is not None and arrived_span.entity_type == "PERSON",
"DISH on a person subject stays PERSON",
)
noted = validate_detected_entity(
{
"start": food_at,
"end": food_at + 5,
"text": "Sushi",
"entity_type": "FOOD",
"note": "Gericht",
},
mixed_homonym,
0,
)
expect(noted is None, "extra fields on a FOOD dish do not fail the pass")
install_test_spans(
[
{"start": food_at, "end": food_at + 5, "text": "Sushi", "entity_type": "FOOD"},
{"start": person_at, "end": person_at + 5, "text": "Sushi", "entity_type": "FOOD"},
]
)
try:
mixed_run = _run(profile_id, mixed_homonym)
mixed_egress = mixed_run.trace.get("egress") or ""
expect(mixed_run.allowed, "FOOD homonym does not block generate")
expect("aß Sushi" in mixed_egress, "reconstructed dish stays unmasked")
expect("[[PERSON:" in mixed_egress, "person-shaped FOOD span is masked")
expect(
not mixed_run.diagnostics.get("contract_violation"),
"food type is not a contract violation",
)
expect(
not mixed_run.diagnostics.get("invalid_entity_type"),
"food type is not recorded as invalid",
)
expect(
"FOOD" in (mixed_run.diagnostics.get("omitted_non_identity_types") or []),
"omitted food type is recorded without span text",
)
finally:
reset_detect_test_hooks()
mixed = _run(
profile_id,
"Ich traf Anna. Ich arbeitete am privaten Projekt Aurora. "
@@ -470,7 +537,7 @@ def main() -> None:
"SELECT seed_revision, template, default_template FROM ai_prompts WHERE slug = ?",
("mvp.entity_detect",),
).fetchone()
expect(row["seed_revision"] == "2026-08-27-detect-ground-v1", "detect prompt revision is stored")
expect(row["seed_revision"] == "2026-09-08-detect-homonym-v1", "detect prompt revision is stored")
expect("{{known_labels}}" not in (row["template"] or ""), "new detect prompt has no known_labels skip list")
expect("Zwiebeln" not in (row["template"] or ""), "detect prompt has no food word list")
@@ -488,7 +555,7 @@ def main() -> None:
("mvp.entity_detect",),
).fetchone()
expect(custom["template"] == "CUSTOM DETECT {{source_text}}", "user-edited detect prompt is not overwritten")
expect(custom["seed_revision"] == "2026-08-27-detect-ground-v1", "revision still updates the default")
expect(custom["seed_revision"] == "2026-09-08-detect-homonym-v1", "revision still updates the default")
remote = ProviderConfig(
role="detect",