fix(mcp): messages d erreur exploitables et rejeu unique sur HTTP 502/503/504 et timeout (infra-2026-10-042b)
This commit is contained in:
1 parent
cc20ccef2f
commit
67f33fc81e
2 files changed
+283
-53
No files matched your search
@@ -5,6 +5,7 @@ Garantit côté serveur le cloisonnement strict des sphères (tt, nyora, perso,
|
||||
Double contrôle d'autorisation avant et après appel à Open Notebook.
|
||||
Gestion des jetons par empreinte sha256, limitation de débit et journalisation sans secrets."""
|
||||
|
||||
import asyncio
|
||||
import contextvars
|
||||
from datetime import datetime, timezone
|
||||
import hashlib
|
||||
@@ -39,6 +40,7 @@ ACCESS_LOG_PATH = os.path.join(LOGS_DIR, "access.log")
|
||||
RATE_LIMIT_TOTAL_PER_MIN = int(os.environ.get("RATE_LIMIT_TOTAL_PER_MIN", "60"))
|
||||
RATE_LIMIT_ASK_PER_MIN = int(os.environ.get("RATE_LIMIT_ASK_PER_MIN", "10"))
|
||||
TIMEOUT_SECONDS = float(os.environ.get("TIMEOUT_SECONDS", "60.0"))
|
||||
RETRY_DELAY_SECONDS = float(os.environ.get("RETRY_DELAY_SECONDS", "2.0"))
|
||||
|
||||
# ---------------------------------------------------------------- Journalisation des accès
|
||||
os.makedirs(LOGS_DIR, exist_ok=True)
|
||||
@@ -281,6 +283,57 @@ def check_notebook_auth(notebook_id: str, identity: str) -> dict:
|
||||
return row
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- Gestion des erreurs et pannes Open Notebook
|
||||
def is_timeout_exception(e: Exception) -> bool:
|
||||
if isinstance(e, (httpx.TimeoutException, asyncio.TimeoutError)):
|
||||
return True
|
||||
name = type(e).__name__.lower()
|
||||
if "timeout" in name:
|
||||
return True
|
||||
msg = str(e).lower()
|
||||
if "délai dépassé" in msg or "timed out" in msg or "timeout" in msg:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def format_open_notebook_error(err_or_status: Exception | int | None, retried: bool = False) -> str:
|
||||
suffix = " (après rejeu)" if retried else ""
|
||||
if isinstance(err_or_status, (int, str)) and str(err_or_status).isdigit():
|
||||
code = int(err_or_status)
|
||||
if code in (502, 503, 504):
|
||||
return (
|
||||
f"Open Notebook a répondu HTTP {code}{suffix}. Un nouvel essai ultérieur peut réussir."
|
||||
if retried
|
||||
else f"Open Notebook a répondu HTTP {code}. Un nouvel essai peut réussir."
|
||||
)
|
||||
elif code == 500:
|
||||
return f"Open Notebook a répondu HTTP 500{suffix}. Un nouvel essai ultérieur peut réussir."
|
||||
elif 400 <= code < 500:
|
||||
return f"Open Notebook a répondu HTTP {code}. Inutile de réessayer avec les mêmes paramètres."
|
||||
else:
|
||||
return f"Open Notebook a répondu HTTP {code}{suffix}. Un nouvel essai peut échouer."
|
||||
|
||||
if err_or_status is None:
|
||||
return "Open Notebook injoignable. Un nouvel essai ultérieur peut réussir une fois le service rétabli."
|
||||
|
||||
exc = err_or_status
|
||||
if is_timeout_exception(exc):
|
||||
if retried:
|
||||
return "Délai dépassé lors de la communication avec Open Notebook (après rejeu). Un nouvel essai ultérieur peut réussir."
|
||||
return "Délai dépassé lors de la communication avec Open Notebook. Un nouvel essai peut réussir."
|
||||
|
||||
return "Open Notebook injoignable. Un nouvel essai ultérieur peut réussir une fois le service rétabli."
|
||||
|
||||
|
||||
def wrap_open_notebook_exception(e: Exception, retried: bool = False) -> Exception:
|
||||
if isinstance(e, (ValueError, NameError, TypeError, AttributeError)):
|
||||
return e
|
||||
if isinstance(e, RuntimeError) and ("délai dépassé" in str(e).lower() or "open notebook" in str(e).lower()):
|
||||
return e
|
||||
msg = format_open_notebook_error(e, retried=retried)
|
||||
return RuntimeError(msg)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- Client Open Notebook
|
||||
def get_on_password() -> str:
|
||||
try:
|
||||
@@ -375,9 +428,10 @@ async def notebook_list() -> dict:
|
||||
log_access(identity, "notebook_list", None, "ok", int((time.monotonic() - t0) * 1000))
|
||||
return {"notebooks": results}
|
||||
except Exception as e:
|
||||
status = "rate_limit_exceeded" if "débit" in str(e).lower() else "error"
|
||||
clean_e = wrap_open_notebook_exception(e)
|
||||
status = "rate_limit_exceeded" if "débit" in str(clean_e).lower() else "error"
|
||||
log_access(identity, "notebook_list", None, status, int((time.monotonic() - t0) * 1000))
|
||||
raise
|
||||
raise clean_e from e
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
@@ -401,13 +455,17 @@ async def notebook_create(sphere: str, name: str, description: str = "") -> dict
|
||||
if sphere_clean not in allowed:
|
||||
raise ValueError(f"Sphère non autorisée pour cette identité : {sphere}")
|
||||
|
||||
st, rep = await on_request(
|
||||
"POST",
|
||||
"/notebooks",
|
||||
{"name": str(name).strip(), "description": str(description).strip()}
|
||||
)
|
||||
try:
|
||||
st, rep = await on_request(
|
||||
"POST",
|
||||
"/notebooks",
|
||||
{"name": str(name).strip(), "description": str(description).strip()}
|
||||
)
|
||||
except Exception as e:
|
||||
raise wrap_open_notebook_exception(e, retried=False) from e
|
||||
|
||||
if st not in (200, 201) or not isinstance(rep, dict) or "id" not in rep:
|
||||
raise RuntimeError(f"Erreur création carnet dans Open Notebook (HTTP {st})")
|
||||
raise RuntimeError(format_open_notebook_error(st, retried=False))
|
||||
|
||||
nb_id = str(rep["id"])
|
||||
create_notebook_entry(nb_id, sphere_clean, str(name).strip(), str(description).strip(), identity)
|
||||
@@ -420,9 +478,10 @@ async def notebook_create(sphere: str, name: str, description: str = "") -> dict
|
||||
"description": str(description).strip(),
|
||||
}
|
||||
except Exception as e:
|
||||
status = "rate_limit_exceeded" if "débit" in str(e).lower() else "error"
|
||||
clean_e = wrap_open_notebook_exception(e)
|
||||
status = "rate_limit_exceeded" if "débit" in str(clean_e).lower() else "error"
|
||||
log_access(identity, "notebook_create", None, status, int((time.monotonic() - t0) * 1000))
|
||||
raise
|
||||
raise clean_e from e
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
@@ -445,19 +504,23 @@ async def source_add_text(notebook_id: str, title: str, content: str) -> dict:
|
||||
if len(content) > 200000:
|
||||
raise ValueError("Le contenu dépasse la taille maximale autorisée de 200 000 caractères")
|
||||
|
||||
st, rep = await on_request(
|
||||
"POST",
|
||||
"/sources/json",
|
||||
{
|
||||
"title": str(title).strip(),
|
||||
"type": "text",
|
||||
"content": content,
|
||||
"notebook_id": str(notebook_id).strip(),
|
||||
"embed": True,
|
||||
}
|
||||
)
|
||||
try:
|
||||
st, rep = await on_request(
|
||||
"POST",
|
||||
"/sources/json",
|
||||
{
|
||||
"title": str(title).strip(),
|
||||
"type": "text",
|
||||
"content": content,
|
||||
"notebook_id": str(notebook_id).strip(),
|
||||
"embed": True,
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
raise wrap_open_notebook_exception(e, retried=False) from e
|
||||
|
||||
if st not in (200, 201) or not isinstance(rep, dict) or "id" not in rep:
|
||||
raise RuntimeError(f"Erreur ajout source dans Open Notebook (HTTP {st})")
|
||||
raise RuntimeError(format_open_notebook_error(st, retried=False))
|
||||
|
||||
src_id = str(rep["id"])
|
||||
create_source_entry(src_id, str(notebook_id).strip(), str(title).strip(), content[:300])
|
||||
@@ -470,9 +533,10 @@ async def source_add_text(notebook_id: str, title: str, content: str) -> dict:
|
||||
"embedded": bool(rep.get("embedded", False)),
|
||||
}
|
||||
except Exception as e:
|
||||
status = "rate_limit_exceeded" if "débit" in str(e).lower() else "error"
|
||||
clean_e = wrap_open_notebook_exception(e)
|
||||
status = "rate_limit_exceeded" if "débit" in str(clean_e).lower() else "error"
|
||||
log_access(identity, "source_add_text", str(notebook_id).strip(), status, int((time.monotonic() - t0) * 1000))
|
||||
raise
|
||||
raise clean_e from e
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
@@ -489,12 +553,16 @@ async def source_list(notebook_id: str) -> dict:
|
||||
try:
|
||||
nb = check_notebook_auth(notebook_id, identity)
|
||||
nb_id_clean = str(notebook_id).strip()
|
||||
st, rep = await on_request(
|
||||
"GET",
|
||||
f"/sources?notebook_id={urllib.parse.quote(nb_id_clean)}"
|
||||
)
|
||||
try:
|
||||
st, rep = await on_request(
|
||||
"GET",
|
||||
f"/sources?notebook_id={urllib.parse.quote(nb_id_clean)}"
|
||||
)
|
||||
except Exception as e:
|
||||
raise wrap_open_notebook_exception(e, retried=False) from e
|
||||
|
||||
if st != 200 or not isinstance(rep, list):
|
||||
raise RuntimeError(f"Erreur récupération sources dans Open Notebook (HTTP {st})")
|
||||
raise RuntimeError(format_open_notebook_error(st, retried=False))
|
||||
|
||||
sources = []
|
||||
for s in rep:
|
||||
@@ -506,9 +574,10 @@ async def source_list(notebook_id: str) -> dict:
|
||||
log_access(identity, "source_list", nb_id_clean, "ok", int((time.monotonic() - t0) * 1000))
|
||||
return {"sources": sources}
|
||||
except Exception as e:
|
||||
status = "rate_limit_exceeded" if "débit" in str(e).lower() else "error"
|
||||
clean_e = wrap_open_notebook_exception(e)
|
||||
status = "rate_limit_exceeded" if "débit" in str(clean_e).lower() else "error"
|
||||
log_access(identity, "source_list", str(notebook_id).strip(), status, int((time.monotonic() - t0) * 1000))
|
||||
raise
|
||||
raise clean_e from e
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
@@ -529,19 +598,46 @@ async def notebook_ask(notebook_id: str, question: str) -> dict:
|
||||
raise ValueError("La question ne peut pas être vide")
|
||||
|
||||
chat_model = await get_default_chat_model()
|
||||
st, rep = await on_request(
|
||||
"POST",
|
||||
"/search/ask/simple",
|
||||
{
|
||||
"question": str(question).strip(),
|
||||
"notebook_id": nb_id_clean,
|
||||
"strategy_model": chat_model,
|
||||
"answer_model": chat_model,
|
||||
"final_answer_model": chat_model,
|
||||
}
|
||||
)
|
||||
if st != 200 or not isinstance(rep, dict):
|
||||
raise RuntimeError(f"Erreur interrogation carnet Open Notebook (HTTP {st})")
|
||||
payload = {
|
||||
"question": str(question).strip(),
|
||||
"notebook_id": nb_id_clean,
|
||||
"strategy_model": chat_model,
|
||||
"answer_model": chat_model,
|
||||
"final_answer_model": chat_model,
|
||||
}
|
||||
|
||||
st = None
|
||||
rep = None
|
||||
timeout_err = None
|
||||
try:
|
||||
st, rep = await on_request("POST", "/search/ask/simple", payload)
|
||||
except Exception as e:
|
||||
if is_timeout_exception(e):
|
||||
timeout_err = e
|
||||
else:
|
||||
raise wrap_open_notebook_exception(e, retried=False) from e
|
||||
|
||||
# Rejeu unique si 502, 503, 504 ou délai dépassé
|
||||
if (timeout_err is not None) or (isinstance(st, int) and st in (502, 503, 504)):
|
||||
await asyncio.sleep(RETRY_DELAY_SECONDS)
|
||||
timeout_err = None
|
||||
try:
|
||||
st, rep = await on_request("POST", "/search/ask/simple", payload)
|
||||
except Exception as e:
|
||||
if is_timeout_exception(e):
|
||||
timeout_err = e
|
||||
else:
|
||||
raise wrap_open_notebook_exception(e, retried=True) from e
|
||||
|
||||
if timeout_err is not None:
|
||||
raise RuntimeError(format_open_notebook_error(timeout_err, retried=True))
|
||||
if st != 200 or not isinstance(rep, dict):
|
||||
raise RuntimeError(format_open_notebook_error(st, retried=True))
|
||||
else:
|
||||
if timeout_err is not None:
|
||||
raise RuntimeError(format_open_notebook_error(timeout_err, retried=False))
|
||||
if st != 200 or not isinstance(rep, dict):
|
||||
raise RuntimeError(format_open_notebook_error(st, retried=False))
|
||||
|
||||
raw_answer = str(rep.get("answer", ""))
|
||||
raw_sources = rep.get("sources", [])
|
||||
@@ -554,11 +650,14 @@ async def notebook_ask(notebook_id: str, question: str) -> dict:
|
||||
src_row = get_source(s_id)
|
||||
src_nb_id = src_row["notebook_id"] if src_row else None
|
||||
if not src_nb_id:
|
||||
st_s, s_info = await on_request("GET", f"/sources/{s_id}")
|
||||
if st_s == 200 and isinstance(s_info, dict):
|
||||
nbs_list = s_info.get("notebooks") or []
|
||||
if nb_id_clean in nbs_list:
|
||||
src_nb_id = nb_id_clean
|
||||
try:
|
||||
st_s, s_info = await on_request("GET", f"/sources/{s_id}")
|
||||
if st_s == 200 and isinstance(s_info, dict):
|
||||
nbs_list = s_info.get("notebooks") or []
|
||||
if nb_id_clean in nbs_list:
|
||||
src_nb_id = nb_id_clean
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if src_nb_id:
|
||||
nb_entry = get_notebook(src_nb_id)
|
||||
@@ -577,9 +676,10 @@ async def notebook_ask(notebook_id: str, question: str) -> dict:
|
||||
log_access(identity, "notebook_ask", nb_id_clean, "ok", int((time.monotonic() - t0) * 1000))
|
||||
return {"answer": answer_text, "sources": filtered_sources}
|
||||
except Exception as e:
|
||||
status = "rate_limit_exceeded" if "débit" in str(e).lower() else "error"
|
||||
clean_e = wrap_open_notebook_exception(e)
|
||||
status = "rate_limit_exceeded" if "débit" in str(clean_e).lower() else "error"
|
||||
log_access(identity, "notebook_ask", str(notebook_id).strip(), status, int((time.monotonic() - t0) * 1000))
|
||||
raise
|
||||
raise clean_e from e
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
@@ -684,9 +784,10 @@ async def notebook_search(query: str, notebook_id: str = "", max_results: int =
|
||||
log_access(identity, "notebook_search", notebook_id, "ok", int((time.monotonic() - t0) * 1000))
|
||||
return {"results": results}
|
||||
except Exception as e:
|
||||
status = "rate_limit_exceeded" if "débit" in str(e).lower() else "error"
|
||||
clean_e = wrap_open_notebook_exception(e)
|
||||
status = "rate_limit_exceeded" if "débit" in str(clean_e).lower() else "error"
|
||||
log_access(identity, "notebook_search", str(notebook_id).strip(), status, int((time.monotonic() - t0) * 1000))
|
||||
raise
|
||||
raise clean_e from e
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- ASGI & Starlette Setup
|
||||
|
||||
Reference in new issue
Block a user