Put student-first detect learning ahead of the teacher call.
Deploy Development / deploy (push) Successful in 1m0s
Test Suite / pytest-backend (push) Failing after 2m58s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 23s

Identity-only senses no longer lock a new cue. Learning stages, journal-generate review, and additive docs follow the closed section 6.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-10-12 08:59:18 +02:00
1 parent a65ea98b0c
commit 011cc9dd81
26 files changed
+1893 -89

No files matched your search

+129 -9
View File
@@ -18,12 +18,27 @@ os.environ["KANSHO_FAKE_DETECT"] = "1"
from fastapi.testclient import TestClient
from detect_learning import (
cue_for_mention,
decision_for_mention,
excerpt_view,
get_cue_decision,
get_detect_operating_mode,
get_sense,
merge_gliner_candidates,
passage_provider,
record_cue,
record_sense,
set_detect_operating_mode,
try_local_passage_decision,
)
from detect_pipeline import (
MentionSpan,
compact_diff_counters,
compare_mentions,
map_entity_type,
prepare_detect_pipeline,
same_mention,
)
from providers import ChatResult, ProviderConfig
from dialogue_turn import visible_for_role
from identity_store import list_confirmed_identities
from main import app
@@ -75,8 +90,28 @@ def main() -> None:
providers = client.get("/api/admin/providers", headers=headers)
expect(providers.status_code == 200, "providers status")
expect(providers.json()["detect_operating_mode"] == "semantic", "default detect mode is semantic")
expect(get_detect_operating_mode() == "semantic", "store default is semantic")
expect(providers.json()["detect_operating_mode"] == "bypass", "fake-env default is bypass")
expect(get_detect_operating_mode() == "bypass", "store default in tests is bypass")
ner = providers.json().get("local_ner") or {}
expect(ner.get("enabled") is False, "GLiNER is off by default")
detect_before = next(item for item in providers.json()["roles"] if item["role"] == "detect")
remote_gliner = client.put(
"/api/admin/providers/local-ner",
headers=headers,
json={"url": "https://openrouter.ai/api/v1"},
)
expect(remote_gliner.status_code == 400, "GLiNER URL must stay local")
enabled = client.put(
"/api/admin/providers/local-ner",
headers=headers,
json={"enabled": True, "url": "http://192.168.2.144:8090"},
)
status_ok(enabled, "enable local GLiNER")
expect(enabled.json()["local_ner"]["enabled"] is True, "GLiNER enabled flag persisted")
expect(enabled.json()["local_ner"]["url"] == "http://192.168.2.144:8090", "GLiNER LAN URL persisted")
detect_after = next(item for item in enabled.json()["roles"] if item["role"] == "detect")
expect(detect_after.get("url") == detect_before.get("url"), "OpenRouter detect URL is unchanged")
client.put("/api/admin/providers/local-ner", headers=headers, json={"enabled": False})
bad = client.put("/api/admin/providers/detect-mode", headers=headers, json={"mode": "wordlist"})
expect(bad.status_code == 400, "invalid detect mode is rejected")
@@ -87,13 +122,13 @@ def main() -> None:
headers=headers,
json={"body": "Anna kam vorbei."},
)
status_ok(semantic_turn, "semantic turn")
expect(not semantic_turn.json().get("pending_mask_review"), "semantic mode does not pause for review")
expect(any(item.get("role") == "assistant" for item in semantic_turn.json().get("messages") or []), "semantic turn gets an assistant")
status_ok(semantic_turn, "bypass turn")
expect(not semantic_turn.json().get("pending_mask_review"), "bypass mode does not pause for review")
expect(any(item.get("role") == "assistant" for item in semantic_turn.json().get("messages") or []), "bypass turn gets an assistant")
switched = client.put("/api/admin/providers/detect-mode", headers=headers, json={"mode": "learning"})
status_ok(switched, "switch to learning")
expect(switched.json()["detect_operating_mode"] == "learning", "learning mode persisted")
status_ok(switched, "switch to learning alias")
expect(switched.json()["detect_operating_mode"] == "learning_full", "learning alias stores learning_full")
conv = open_conv(client, headers)
paused = client.post(
@@ -174,7 +209,11 @@ def main() -> None:
json={"body": "Hanna aß mit uns."},
)
status_ok(hanna_again, "second Hanna turn")
expect(not hanna_again.json().get("pending_mask_review"), "known non-identity skips the popup")
hanna_conflict = hanna_again.json().get("pending_mask_review") or {}
expect(
bool(hanna_conflict.get("id")),
"teacher PERSON versus stored not_identity cue stays a visible type conflict",
)
record_sense(profile_id, "Clarissa", identity=True)
record_sense(profile_id, "Clarissa", identity=False)
@@ -311,8 +350,89 @@ def main() -> None:
status_ok(sushi_again, "repeat homonym sentence")
expect(not sushi_again.json().get("pending_mask_review"), "same reviewed sentence does not re-ask")
dish = "Ich aß Sushi."
dish_at = dish.index("Sushi")
food_spans = [{"text": "Sushi", "start": dish_at, "end": dish_at + 5, "label": "food", "score": 0.8}]
food_cands, _maps = merge_gliner_candidates(profile_id, [], [], dish, dish, food_spans)
expect(
any(item.get("text") == "Sushi" and item.get("suggested") == "not_identity" for item in food_cands),
"GLiNER food span suggests not_identity",
)
person_line = "Ich war mit meiner Frau Sushi unterwegs."
person_at = person_line.index("Sushi")
gliner_miss = [{"text": "Sushi", "start": person_at, "end": person_at + 5, "label": "food", "score": 0.9}]
merged, _maps = merge_gliner_candidates(profile_id, [], [], person_line, person_line, gliner_miss)
expect(
not any(item.get("suggested") == "not_identity" for item in merged),
"GLiNER food does not override Frau attachment",
)
expect(
decision_for_mention(profile_id, "Sushi", person_line, person_at, person_at + 5, person_line) == "identity",
"learned Frau cue wins over GLiNER food",
)
dish_line = "Ich aß Sushi."
dish_pos = dish_line.index("Sushi")
expect(
decision_for_mention(profile_id, "Sushi", dish_line, dish_pos, dish_pos + 5, dish_line) == "ask",
"Tag-21: identity hits plus new cue stay visible",
)
person = MentionSpan("Sushi", 20, 25, "PERSON", "identity", "cue", True, "frau")
food = MentionSpan("Sushi", 20, 25, "FOOD", "not_identity", "teacher", False, "aß")
expect(same_mention(person, food), "same offsets are the same mention")
wrapped = MentionSpan("Frau Sushi", 15, 25, "PERSON", "identity", "teacher", False, "frau")
expect(same_mention(person, wrapped), "contained Sushi matches Frau Sushi")
expect(map_entity_type("person") == "PERSON", "GLiNER person maps to PERSON")
review, counters = compare_mentions(profile_id, dish_line, [person], [food], dish_line)
expect(counters["diff_type_conflict"] >= 1, "FOOD versus PERSON is a type conflict")
expect(any(item.get("reason") == "type_conflict" for item in review), "type conflict is offered")
compact = compact_diff_counters(counters)
dumped = str(compact)
expect("Sushi" not in dumped and "Frau" not in dumped, "compact diff counters carry no plaintext")
expect(set(compact) == {
"diff_agree",
"diff_only_student",
"diff_only_teacher",
"diff_type_conflict",
"diff_new_identity",
"diff_cue_reinforced",
}, "compact counters are the agreed keys")
set_detect_operating_mode("learning_reduced")
expect(get_detect_operating_mode() == "learning_reduced", "stage 2 mode persists")
set_detect_operating_mode("learning_manual")
expect(get_detect_operating_mode() == "learning_manual", "manual mode persists")
set_detect_operating_mode("student")
pipe = prepare_detect_pipeline(profile_id, "Anna kam vorbei.", "Anna kam vorbei.")
expect(pipe.mode == "student", "student path is selectable")
expect(not pipe.need_review, "zielpfad does not open a teacher popup")
record_sense(profile_id, "Sushi", identity=True)
record_cue(profile_id, "Sushi", "frau", "identity", "meine Frau Sushi")
expect(
decision_for_mention(profile_id, "Sushi", "Ich aß Sushi.", dish_pos, dish_pos + 5) == "ask",
"Tag-21 remains ask after more identity hits",
)
fake_passage = ProviderConfig(
role="passage",
name="ollama",
mode="http",
url="http://192.168.2.144:11434/v1/chat/completions",
model="phi3:mini",
key="",
local=True,
zdr=True,
no_train=True,
)
with patch("detect_learning.passage_provider", return_value=fake_passage):
with patch("providers.complete_chat", return_value=ChatResult(content='{"decision":"not_identity"}')) as chat:
decided = try_local_passage_decision("Rohan Sushi essen", "Sushi")
expect(decided == "not_identity", "passage profile can decide without local Detect")
expect(chat.called, "passage uses complete_chat")
expect(passage_provider() is None, "no passage profile means no silent detect fallback")
back = client.put("/api/admin/providers/detect-mode", headers=headers, json={"mode": "semantic"})
expect(back.json()["detect_operating_mode"] == "semantic", "mode can return to semantic")
expect(back.json()["detect_operating_mode"] == "bypass", "semantic alias stores bypass")
full = client.put("/api/admin/providers/detect-mode", headers=headers, json={"mode": "learning_full"})
expect(full.json()["detect_operating_mode"] == "learning_full", "learning_full is accepted")
if __name__ == "__main__":