Put student-first detect learning ahead of the teacher call.
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:
1 parent
a65ea98b0c
commit
011cc9dd81
26 files changed
+1893
-89
No files matched your search
@@ -36,6 +36,8 @@ UNIT_MODULES = frozenset(
|
||||
"test_journal_shape",
|
||||
"test_journal_body",
|
||||
"test_writing_profile",
|
||||
"test_gliner_client",
|
||||
"test_detect_pipeline",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -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__":
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Unit tests for student/teacher comparison. No database."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
os.environ.setdefault("KANSHO_PROVIDER_KEY", "")
|
||||
os.environ.setdefault("KANSHO_DETECT_PROVIDER_KEY", "")
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from detect_learning import canonicalize_detect_mode, decision_for_mention
|
||||
from detect_pipeline import (
|
||||
MentionSpan,
|
||||
compact_diff_counters,
|
||||
map_entity_type,
|
||||
protects,
|
||||
same_mention,
|
||||
types_equivalent,
|
||||
)
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
expect(canonicalize_detect_mode("learning") == "learning_full", "learning aliases to learning_full")
|
||||
expect(canonicalize_detect_mode("semantic") == "bypass", "semantic aliases to bypass")
|
||||
expect(canonicalize_detect_mode("wordlist") is None, "unknown mode is rejected")
|
||||
expect(map_entity_type("person") == "PERSON", "person maps to PERSON")
|
||||
expect(map_entity_type("place") == "PLACE", "place maps to PLACE")
|
||||
expect(protects("PERSON") and not protects("FOOD"), "PERSON protects, FOOD does not")
|
||||
expect(types_equivalent("PERSON", "PLACE"), "similar protectable types are equivalent")
|
||||
expect(not types_equivalent("FOOD", "PERSON"), "FOOD versus PERSON is never equivalent")
|
||||
inner = MentionSpan("Sushi", 20, 25, "PERSON", "identity", "cue", True, "frau")
|
||||
outer = MentionSpan("Frau Sushi", 15, 25, "PERSON", "identity", "teacher", False, "frau")
|
||||
expect(same_mention(inner, outer), "contained span is the same mention")
|
||||
food = MentionSpan("Sushi", 20, 25, "FOOD", "not_identity", "teacher", False, "aß")
|
||||
expect(same_mention(inner, food), "same offsets stay one mention even when types differ")
|
||||
compact = compact_diff_counters({"diff_agree": 2, "diff_type_conflict": 1, "label": "Sushi"})
|
||||
expect(compact["diff_agree"] == 2 and compact["diff_type_conflict"] == 1, "known counters are kept")
|
||||
expect("label" not in compact and "Sushi" not in str(compact), "compact counters drop plaintext")
|
||||
with patch("detect_learning.get_cue_decision", return_value=None), patch(
|
||||
"detect_learning.get_sense",
|
||||
return_value={"identity_hits": 3, "non_identity_hits": 0, "ambiguous": False},
|
||||
), patch("detect_learning.cue_for_mention", return_value="aß"):
|
||||
expect(
|
||||
decision_for_mention("p", "Sushi", "Ich aß Sushi.", 7, 12) == "ask",
|
||||
"Tag-21: identity-only sense does not lock a new cue",
|
||||
)
|
||||
with patch("detect_learning.get_cue_decision", return_value="identity"), patch(
|
||||
"detect_learning.cue_for_mention", return_value="frau"
|
||||
):
|
||||
expect(
|
||||
decision_for_mention("p", "Sushi", "meine Frau Sushi", 11, 16) == "identity",
|
||||
"stored cue remains binding",
|
||||
)
|
||||
print("detect pipeline unit ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -89,7 +89,7 @@ def main() -> None:
|
||||
expect(set(roles) == {"generate", "detect"}, "two provider roles")
|
||||
expect(providers.json()["remote_plaintext_allowed"] is True, "test runtime allows remote detect")
|
||||
expect(providers.json()["remote_plaintext_reason"] == "non_production", "tests are not production")
|
||||
expect(providers.json().get("detect_operating_mode") == "semantic", "detect mode defaults to semantic")
|
||||
expect(providers.json().get("detect_operating_mode") == "learning_full", "detect mode defaults to learning stage 1")
|
||||
expect(roles["generate"]["ready"] is False, "generate fail-closed without key")
|
||||
expect("sk-" not in providers.text, "provider status has no secret")
|
||||
expect("KANSHO_PROVIDER_KEY=" not in providers.text, "env assignment not leaked")
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
"""GLiNER client: local URL only, fail-soft, no request text in logs."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import httpx
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ.setdefault("KANSHO_PROVIDER_KEY", "")
|
||||
os.environ.setdefault("KANSHO_DETECT_PROVIDER_KEY", "")
|
||||
|
||||
from gliner_client import DEFAULT_URL, extract_entities, health, normalize_base_url
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
class _Resp:
|
||||
def __init__(self, status_code: int, payload):
|
||||
self.status_code = status_code
|
||||
self._payload = payload
|
||||
|
||||
def json(self):
|
||||
return self._payload
|
||||
|
||||
|
||||
def main() -> None:
|
||||
expect(normalize_base_url("http://192.168.2.144:8090/") == DEFAULT_URL, "default GLiNER URL is normalized")
|
||||
expect(extract_entities("Anna", url="https://openrouter.ai/api/v1") is None, "OpenRouter URL is rejected")
|
||||
expect(health("https://openrouter.ai").get("reason") == "not_local", "health rejects non-local URL")
|
||||
|
||||
with patch("gliner_client.httpx.post", return_value=_Resp(200, {"spans": [{"text": "Anna", "start": 0, "end": 4, "label": "person", "score": 0.9}]})):
|
||||
spans = extract_entities("Anna kam.", url="http://192.168.2.144:8090")
|
||||
expect(spans == [{"text": "Anna", "start": 0, "end": 4, "label": "person", "score": 0.9}], "extract returns spans")
|
||||
|
||||
with patch("gliner_client.httpx.post", side_effect=httpx.ConnectError("down")):
|
||||
expect(extract_entities("Anna kam.", url="http://192.168.2.144:8090") is None, "extract is fail-soft on errors")
|
||||
|
||||
with patch("gliner_client.httpx.get", return_value=_Resp(200, {"status": "ok", "model": "urchade/gliner_multi-v2.1", "offline": True})):
|
||||
ping = health("http://192.168.2.144:8090")
|
||||
expect(ping.get("ok") is True and ping.get("offline") is True, "health reports ready offline model")
|
||||
print("gliner client: OK")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -31,7 +31,7 @@ def expect(ok: bool, message: str) -> None:
|
||||
def main() -> None:
|
||||
expect(is_local_url("http://127.0.0.1:11434/v1/chat/completions"), "loopback is local")
|
||||
expect(is_local_url("http://localhost:11434/v1/chat/completions"), "localhost is local")
|
||||
expect(is_local_url("http://192.168.2.144:11434/v1/chat/completions"), "RFC1918 Ollama host is local")
|
||||
expect(is_local_url("http://192.168.2.144:8090"), "RFC1918 GLiNER host is local")
|
||||
expect(is_local_url("http://10.0.0.8:11434/v1/chat/completions"), "10/8 is local")
|
||||
expect(is_local_url("http://ollama:11434/v1/chat/completions"), "compose hostname ollama is local")
|
||||
expect(not is_local_url("https://openrouter.ai/api/v1/chat/completions"), "OpenRouter is not local")
|
||||
|
||||
Reference in new issue
Block a user