2026-11-05 12:58:39 +01:00
|
|
|
"""Vérifications du support par l'API, sans appel à l'API (faux client) : API/.venv/bin/python API/support/check.py"""
|
|
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from types import SimpleNamespace as NS
|
|
|
|
|
|
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
|
|
|
from agent import Agent # noqa: E402
|
|
|
|
|
|
|
|
|
|
API = Path(__file__).resolve().parents[1]
|
|
|
|
|
DONNEES = API.parent / "data"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Bloc(NS):
|
|
|
|
|
def model_dump(self, **_):
|
|
|
|
|
return {k: v for k, v in vars(self).items() if v is not None}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reponse(contenu, stop, entree=10, sortie=10):
|
|
|
|
|
return NS(content=contenu, stop_reason=stop, usage=NS(input_tokens=entree, output_tokens=sortie,
|
|
|
|
|
cache_read_input_tokens=0, cache_creation_input_tokens=0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FauxClient:
|
|
|
|
|
"""beta.messages.stream(**kw) rend, dans l'ordre, les réponses scriptées ; une exception scriptée est levée."""
|
|
|
|
|
def __init__(self, *reponses):
|
|
|
|
|
self.reponses, self.appels = list(reponses), []
|
|
|
|
|
self.beta = NS(messages=NS(stream=self._stream))
|
|
|
|
|
|
|
|
|
|
def _stream(self, **kw):
|
|
|
|
|
self.appels.append(kw)
|
|
|
|
|
r = self.reponses.pop(0)
|
|
|
|
|
client = self
|
|
|
|
|
|
|
|
|
|
class Flux:
|
|
|
|
|
def __enter__(self):
|
|
|
|
|
if isinstance(r, Exception):
|
|
|
|
|
raise r
|
|
|
|
|
return NS(get_final_message=lambda: r)
|
|
|
|
|
|
|
|
|
|
def __exit__(self, *_):
|
|
|
|
|
return False
|
|
|
|
|
client.dernier = kw
|
|
|
|
|
return Flux()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def agent(client, executer=lambda n, e: "ok"):
|
|
|
|
|
journal = []
|
|
|
|
|
a = Agent("analyste", "système", [], executer, client, journal.append, effort="high")
|
|
|
|
|
return a, journal
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_boucle():
|
|
|
|
|
appels = []
|
|
|
|
|
c = FauxClient(reponse([Bloc(type="tool_use", id="t1", name="detecteurs", input={"station": "X"})], "tool_use"),
|
|
|
|
|
reponse([Bloc(type="text", text="fini")], "end_turn"))
|
|
|
|
|
a, _ = agent(c, lambda n, e: appels.append((n, e)) or "3 événements")
|
|
|
|
|
assert a.tour("question", "support") == "fini"
|
|
|
|
|
roles = [m["role"] for m in a.messages]
|
|
|
|
|
assert roles == ["user", "assistant", "user", "assistant"], roles
|
|
|
|
|
res = a.messages[2]["content"][0]
|
|
|
|
|
assert res["type"] == "tool_result" and res["tool_use_id"] == "t1" and res["content"] == "3 événements"
|
|
|
|
|
assert appels == [("detecteurs", {"station": "X"})]
|
|
|
|
|
assert not a.occupe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_erreur():
|
|
|
|
|
c = FauxClient(reponse([Bloc(type="tool_use", id="t1", name="detecteurs", input={})], "tool_use"),
|
|
|
|
|
RuntimeError("surcharge"))
|
|
|
|
|
a, journal = agent(c)
|
|
|
|
|
a.tour("question", "marc")
|
|
|
|
|
assert any(e["type"] == "erreur" for e in journal)
|
|
|
|
|
dernier = a.messages[-1]
|
|
|
|
|
assert not (dernier["role"] == "assistant" and any(b.get("type") == "tool_use" for b in dernier["content"]))
|
|
|
|
|
assert not a.occupe
|
|
|
|
|
# le tour suivant repart proprement : user après user est permis, pas de tool_use orphelin
|
|
|
|
|
c.reponses.append(reponse([Bloc(type="text", text="repris")], "end_turn"))
|
|
|
|
|
assert a.tour("encore", "marc") == "repris"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_cout():
|
|
|
|
|
r = reponse([Bloc(type="text", text="x")], "end_turn", entree=1_000_000, sortie=100_000)
|
|
|
|
|
a, journal = agent(FauxClient(r))
|
|
|
|
|
a.tour("q", "marc")
|
|
|
|
|
assert abs(a.cout - 6.0) < 1e-9, a.cout
|
|
|
|
|
assert any(e["type"] == "cout" for e in journal)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_source():
|
|
|
|
|
a, journal = agent(FauxClient(reponse([Bloc(type="text", text="x")], "end_turn")))
|
|
|
|
|
a.tour("bonjour", "marc")
|
|
|
|
|
premier = a.messages[0]["content"][0]
|
|
|
|
|
assert premier["type"] == "text" and premier["text"].startswith("<source>marc</source>")
|
|
|
|
|
assert journal[0]["type"] == "entree" and journal[0]["source"] == "marc"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_outil_qui_leve():
|
|
|
|
|
c = FauxClient(reponse([Bloc(type="tool_use", id="t1", name="x", input={})], "tool_use"),
|
|
|
|
|
reponse([Bloc(type="text", text="vu")], "end_turn"))
|
|
|
|
|
a, _ = agent(c, lambda n, e: 1 / 0)
|
|
|
|
|
a.tour("q", "marc")
|
|
|
|
|
res = a.messages[2]["content"][0]
|
|
|
|
|
assert res["is_error"] is True and "division" in res["content"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def boite(dossier, station="NEBRASKA"):
|
|
|
|
|
from outils import Boite
|
|
|
|
|
return Boite(lambda: dossier, lambda: station, DONNEES, lambda m: "réponse", lambda st, su: dossier)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def leve(f, *a):
|
|
|
|
|
from outils import ErreurOutil
|
|
|
|
|
try:
|
|
|
|
|
f(*a)
|
|
|
|
|
except ErreurOutil as e:
|
|
|
|
|
return str(e)
|
|
|
|
|
raise AssertionError(f"{f.__name__}{a} n'a pas levé ErreurOutil")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def seance_temporaire():
|
|
|
|
|
import tempfile
|
|
|
|
|
return Path(tempfile.mkdtemp(prefix="seance-"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_lecture_refusee():
|
|
|
|
|
b = boite(seance_temporaire())
|
|
|
|
|
assert "rm" in leve(b.executer_analyste, "outil_de_lecture", {"nom": "rm", "arguments": []})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_chemin():
|
|
|
|
|
b = boite(seance_temporaire())
|
|
|
|
|
for c in ("../../.env", "/etc/passwd"):
|
|
|
|
|
leve(b.executer_analyste, "voir_image", {"chemin": c})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_station_absente():
|
|
|
|
|
b = boite(seance_temporaire())
|
|
|
|
|
m = leve(b.executer_analyste, "detecteurs", {"station": "NULLEPART", "du": "2026-10-01", "au": "2026-10-02"})
|
|
|
|
|
assert "NULLEPART" in m
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_detecteurs():
|
|
|
|
|
b = boite(seance_temporaire())
|
|
|
|
|
t = b.executer_analyste("detecteurs", {"station": "NEBRASKA", "du": "2026-10-20", "au": "2026-11-01"})
|
|
|
|
|
assert "événement" in t and "capteurs :" in t, t
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_plafond():
|
|
|
|
|
from outils import plafonner
|
|
|
|
|
t = plafonner("x" * 50_000)
|
|
|
|
|
assert len(t) <= 40_100 and t.endswith("tronqué : 50000 caractères]"), t[-60:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_image():
|
|
|
|
|
import base64
|
|
|
|
|
import io
|
|
|
|
|
from PIL import Image
|
|
|
|
|
from outils import bloc_image
|
|
|
|
|
jpg = sorted((DONNEES / "NEBRASKA" / "images").glob("*.jpg"))[0]
|
|
|
|
|
b = bloc_image(jpg)
|
|
|
|
|
assert b["type"] == "image" and b["source"]["media_type"] == "image/jpeg"
|
|
|
|
|
im = Image.open(io.BytesIO(base64.b64decode(b["source"]["data"])))
|
|
|
|
|
assert max(im.size) <= 1568, im.size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_compiler():
|
|
|
|
|
import re
|
|
|
|
|
import shutil
|
|
|
|
|
d = seance_temporaire()
|
|
|
|
|
modele = (API / "knowledge" / "support" / "modele.typ").read_text()
|
|
|
|
|
(d / "nebraska.typ").write_text(modele)
|
|
|
|
|
for f in set(re.findall(r'image\("([^"]+)"\)', modele)):
|
|
|
|
|
shutil.copy(API.parent / "support" / "2026-11-02_BREITHORN_rapport-situation" / f, d)
|
|
|
|
|
b = boite(d)
|
|
|
|
|
assert b.executer_support("compiler", {}).startswith("v1.pdf : ") and (d / "v1.pdf").exists()
|
|
|
|
|
assert b.executer_support("compiler", {}).startswith("v2.pdf : ")
|
|
|
|
|
(d / "nebraska.typ").write_text("#oups(")
|
|
|
|
|
leve(b.executer_support, "compiler", {})
|
|
|
|
|
assert not (d / "v3.pdf").exists()
|
|
|
|
|
page = b.executer_support("voir_page", {"n": 1})
|
|
|
|
|
assert page[0]["type"] == "image"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_max_tokens():
|
|
|
|
|
# un tour coupé (max_tokens, refus) avec un tool_use : rien n'entre dans l'historique, la séance continue
|
|
|
|
|
c = FauxClient(reponse([Bloc(type="tool_use", id="t1", name="ecrire_presentation", input={})], "max_tokens"),
|
|
|
|
|
reponse([Bloc(type="text", text="repris")], "end_turn"))
|
|
|
|
|
a, journal = agent(c)
|
|
|
|
|
a.tour("q", "marc")
|
|
|
|
|
assert any(e["type"] == "erreur" for e in journal)
|
|
|
|
|
assert all(m["role"] == "user" for m in a.messages), [m["role"] for m in a.messages]
|
|
|
|
|
assert a.tour("encore", "marc") == "repris"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_sans_repli():
|
|
|
|
|
# le repli de modèle casse l'historique renvoyé (clé from_) : il n'est pas demandé
|
|
|
|
|
c = FauxClient(reponse([Bloc(type="text", text="x")], "end_turn"))
|
|
|
|
|
a, _ = agent(c)
|
|
|
|
|
a.tour("q", "marc")
|
|
|
|
|
assert "fallbacks" not in c.dernier and "server-side-fallback-2026-07-01" not in c.dernier.get("betas", [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_capture_reduite():
|
|
|
|
|
import io
|
|
|
|
|
import shutil
|
|
|
|
|
from PIL import Image
|
|
|
|
|
from seance import Seance
|
|
|
|
|
tampon = io.BytesIO()
|
|
|
|
|
Image.new("RGB", (4000, 3000), "white").save(tampon, "PNG")
|
|
|
|
|
s = Seance("mail", [("image/png", tampon.getvalue())], "", DONNEES, lambda e: None,
|
|
|
|
|
client=FauxClient(reponse([Bloc(type="text", text="x")], "end_turn")))
|
|
|
|
|
try:
|
|
|
|
|
s.demarrer()
|
|
|
|
|
bloc = next(b for b in s.support.messages[0]["content"] if b.get("type") == "image")
|
|
|
|
|
import base64
|
|
|
|
|
im = Image.open(io.BytesIO(base64.b64decode(bloc["source"]["data"])))
|
|
|
|
|
assert max(im.size) <= 1568, im.size
|
|
|
|
|
finally:
|
|
|
|
|
shutil.rmtree(s.dossier)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_image_proche():
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
b = boite(seance_temporaire())
|
|
|
|
|
jpg = sorted((DONNEES / "NEBRASKA" / "images").glob("*.jpg"))[0]
|
|
|
|
|
t = datetime.strptime(jpg.name[:14], "%Y%m%dT%H%MZ") + timedelta(minutes=2)
|
|
|
|
|
bloc = b.executer_analyste("voir_image", {"chemin": "images/" + t.strftime("%Y%m%dT%H%MZ")})
|
|
|
|
|
assert bloc[0]["type"] == "image"
|
|
|
|
|
assert b._image_proche(t.strftime("%Y%m%dT%H%MZ")) == jpg
|
|
|
|
|
leve(b._image_proche, "20200101T0000Z")
|
|
|
|
|
|
|
|
|
|
|
2026-11-05 13:04:08 +01:00
|
|
|
def check_stabilite():
|
|
|
|
|
b = boite(seance_temporaire(), "LENZSPITZE")
|
|
|
|
|
t = b.executer_analyste("stabilite", {"station": "LENZSPITZE", "du": "2026-10-16", "au": "2026-10-20",
|
|
|
|
|
"hors": ["2026-10-17T00:00Z/2026-10-18T00:00Z"]})
|
|
|
|
|
assert "1017" in t and "étendue" in t, t[:400]
|
|
|
|
|
ligne = next(l for l in t.splitlines() if l.startswith("1017 "))
|
|
|
|
|
assert ligne.split()[1] == "3", ligne # 4 jours, dont le 17 écarté
|
|
|
|
|
|
|
|
|
|
|
2026-11-05 13:08:07 +01:00
|
|
|
def check_lire_document():
|
|
|
|
|
b = boite(seance_temporaire())
|
|
|
|
|
t = b.executer_analyste("lire_document", {"nom": "carte-des-etiquettes.md",
|
|
|
|
|
"section": "La station s'éteint faute d'énergie"})
|
|
|
|
|
assert t.startswith("#### La station s'éteint faute d'énergie") and "#### Coupure du secteur" not in t, t[:200]
|
|
|
|
|
assert "Les frontières" in b.executer_analyste("lire_document", {"nom": "familles.md", "section": ""})
|
|
|
|
|
assert "sections" in leve(b.executer_analyste, "lire_document", {"nom": "familles.md", "section": "n'existe pas"})
|
|
|
|
|
leve(b.executer_analyste, "lire_document", {"nom": "../../.env", "section": ""})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_cartes():
|
|
|
|
|
from seance import systeme
|
|
|
|
|
a = systeme("analyste")
|
|
|
|
|
assert len(a) < 60_000, len(a) # les cartes, pas tout le savoir (214 000 caractères avant)
|
|
|
|
|
assert "## Sommaire" in a and "#### Un trou commun" not in a and "## Les frontières" in a
|
|
|
|
|
assert "lire-les-donnees.md" in a # dans l'index
|
|
|
|
|
assert "#### Un trou commun" not in a and "## Les outils" not in a
|
|
|
|
|
|
|
|
|
|
|
2026-11-05 12:58:39 +01:00
|
|
|
def check_stations_dans_la_seance():
|
|
|
|
|
from outils import AVEC_SORTIE
|
|
|
|
|
assert "stations" in AVEC_SORTIE
|
|
|
|
|
|
|
|
|
|
|
2026-11-05 14:03:52 +01:00
|
|
|
def check_serveur():
|
|
|
|
|
# le vrai serveur sur un port libre, une séance au faux client : la garde, l'analyse, le flux, le PDF absent
|
|
|
|
|
import json
|
|
|
|
|
import shutil
|
|
|
|
|
import socket
|
|
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
import urllib.request
|
|
|
|
|
from urllib.error import HTTPError
|
|
|
|
|
import uvicorn
|
|
|
|
|
import seance
|
|
|
|
|
import serveur
|
|
|
|
|
with socket.socket() as so:
|
|
|
|
|
so.bind(("127.0.0.1", 0))
|
|
|
|
|
port = so.getsockname()[1]
|
|
|
|
|
cases = seance.CASES
|
|
|
|
|
seance.CASES, serveur.CLIENT = seance_temporaire(), FauxClient(reponse([Bloc(type="text", text="Bonjour Marc")], "end_turn"))
|
|
|
|
|
srv = uvicorn.Server(uvicorn.Config(serveur.app, host="127.0.0.1", port=port, log_level="error"))
|
|
|
|
|
threading.Thread(target=srv.run, daemon=True).start()
|
|
|
|
|
while not srv.started:
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
base = f"http://127.0.0.1:{port}"
|
|
|
|
|
|
|
|
|
|
def appel(chemin, corps=None, entetes=None):
|
|
|
|
|
r = urllib.request.Request(base + chemin, json.dumps(corps).encode() if corps is not None else None,
|
|
|
|
|
{"Content-Type": "application/json", **(entetes or {})})
|
|
|
|
|
try:
|
|
|
|
|
with urllib.request.urlopen(r) as rep:
|
|
|
|
|
return rep.status
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
return e.code
|
|
|
|
|
try:
|
|
|
|
|
assert appel("/api/pdf", entetes={"Host": "evil.example:80"}) == 403
|
|
|
|
|
assert appel("/api/analyser", {"mail": "x"}, {"Content-Type": "text/plain"}) == 403
|
|
|
|
|
assert appel("/api/analyser", {"mail": " "}) == 400
|
|
|
|
|
assert appel("/api/analyser", {"mail": "Bonjour", "contexte": "", "images": []}) == 200
|
|
|
|
|
with urllib.request.urlopen(base + "/api/flux", timeout=10) as f:
|
|
|
|
|
premier, textes = None, []
|
|
|
|
|
for ligne in f:
|
|
|
|
|
if ligne.startswith(b"data: "):
|
|
|
|
|
m = json.loads(ligne[6:])
|
|
|
|
|
premier = premier or m
|
|
|
|
|
textes += [e["texte"] for e in m["evenements"]]
|
|
|
|
|
if "Bonjour Marc" in textes and not m["occupe"]["support"]:
|
|
|
|
|
break
|
|
|
|
|
assert premier["depuis"] == 0 and premier["id"], premier
|
|
|
|
|
assert appel("/api/pdf") == 404
|
|
|
|
|
(serveur.etat["seance"].dossier / "v1.pdf").write_bytes(b"%PDF-1.4")
|
|
|
|
|
with urllib.request.urlopen(base + "/api/pdf") as rep:
|
|
|
|
|
assert rep.read() == b"%PDF-1.4" and rep.headers["Content-Disposition"].startswith("inline"), rep.headers
|
|
|
|
|
finally:
|
|
|
|
|
srv.should_exit = True
|
|
|
|
|
shutil.rmtree(seance.CASES)
|
|
|
|
|
seance.CASES = cases
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CHECKS = [check_serveur, check_lire_document, check_cartes, check_stabilite, check_image_proche, check_max_tokens, check_sans_repli, check_capture_reduite, check_stations_dans_la_seance, check_boucle, check_erreur, check_cout, check_source, check_outil_qui_leve, check_lecture_refusee,
|
2026-11-05 12:58:39 +01:00
|
|
|
check_chemin, check_station_absente, check_detecteurs, check_plafond, check_image, check_compiler]
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
nom = sys.argv[1] if len(sys.argv) > 1 else None
|
|
|
|
|
for f in CHECKS:
|
|
|
|
|
if nom in (None, f.__name__):
|
|
|
|
|
f()
|
|
|
|
|
print("ok", f.__name__)
|