"""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("marc")
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")
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é
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
def check_stations_dans_la_seance():
from outils import AVEC_SORTIE
assert "stations" in AVEC_SORTIE
def check_sortie_mail():
# la sortie mail : prompt sans la présentation, outils sans compiler, versions mail-vN.json, pièces du dossier seules
import json
from seance import Seance, systeme
m, p = systeme("support", "mail"), systeme("support", "pdf")
assert "ecrire_mail" in m and "modele.typ\">" not in m and "ecrire_presentation" not in m.split("")[-1]
assert "modele.typ\">" in p and "ecrire_presentation" in p.split("")[-1]
d = seance_temporaire()
(d / "bosse.png").write_bytes(b"x")
b = boite(d)
assert "pièces refusées" in leve(b.executer_support, "ecrire_mail", {"texte": "t", "pieces_jointes": ["../x.png"]})
leve(b.executer_support, "ecrire_mail", {"texte": "t", "pieces_jointes": ["absente.png"]})
assert b.executer_support("ecrire_mail", {"texte": "Bonjour,", "pieces_jointes": ["bosse.png"]}).startswith("mail-v1.json")
b.executer_support("ecrire_mail", {"texte": "Bonjour, v2", "pieces_jointes": []})
assert json.loads((d / "mail-v2.json").read_text()) == {"texte": "Bonjour, v2", "pieces_jointes": []}
import seance
seance.CASES, avant = d, seance.CASES
try:
s = Seance("mail", [], "", DONNEES, lambda e: None, client=FauxClient(), sortie="mail")
assert [o["name"] for o in s.support.outils][-1] == "ecrire_mail" and s.derniere_version() is None
for i in (1, 2, 10):
(s.dossier / f"mail-v{i}.json").write_text("{}")
assert s.derniere_version().name == "mail-v10.json"
finally:
seance.CASES = avant
CHECKS = [check_sortie_mail, 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,
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__)