Add multi-turn Assistent session diagnostics to the Debug API.
POST /assistent/session + /chat with rich per-turn traces (patch, Exact merge, compact_context); chat-eval wraps one session turn. Docs playbook and unit/HTTP tests included. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+24
-249
@@ -2,7 +2,8 @@
|
||||
|
||||
Probes extension compile/load markers, overlay personas, ollama-roles,
|
||||
sqlite, live SwarmUI Assistent* APIs, optional 1-token Ollama chat smoke,
|
||||
and opt-in AssistentChat evaluation (/assistent/chat-eval).
|
||||
opt-in AssistentChat evaluation (/assistent/chat-eval), and multi-turn
|
||||
debug sessions (/assistent/session*).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -437,107 +438,23 @@ def run_assistent_chat_eval(
|
||||
pack: str | None = None,
|
||||
model: str | None = None,
|
||||
timeout: float | int | str | None = None,
|
||||
context: dict | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Opt-in AssistentChat round-trip via local Swarm tunnel, else SSH :7801.
|
||||
"""Opt-in AssistentChat round-trip (session create → one chat → delete).
|
||||
|
||||
May load the chat model into VRAM and write chat history if Sqlite works.
|
||||
Not part of /snapshot.
|
||||
Not part of /snapshot. Prefer POST /assistent/session for multi-turn.
|
||||
"""
|
||||
text = (message or DEFAULT_CHAT_EVAL_MESSAGE).strip() or DEFAULT_CHAT_EVAL_MESSAGE
|
||||
persona_id = (persona or "").strip() or "neutral"
|
||||
pack_name = (pack or "").strip() or "ordinary"
|
||||
t_chat = _clamp_chat_eval_timeout(timeout)
|
||||
hints: list[str] = [
|
||||
"opt-in AssistentChat eval — may load VRAM; may SaveChat if Sqlite works",
|
||||
"not included in /snapshot",
|
||||
]
|
||||
errors: list[str] = []
|
||||
t0 = time.perf_counter()
|
||||
from gpu_rent.debug_assistent_session import run_assistent_chat_eval_via_session
|
||||
|
||||
base, via = _swarm_base(cfg)
|
||||
if not base:
|
||||
return _assistent_chat_eval_via_ssh(
|
||||
cfg,
|
||||
message=text,
|
||||
persona=persona_id,
|
||||
pack=pack_name,
|
||||
model=(model or "").strip() or None,
|
||||
timeout=t_chat,
|
||||
hints=hints,
|
||||
t0=t0,
|
||||
)
|
||||
|
||||
sid, err, ms_sess = _session_id(base)
|
||||
if not sid:
|
||||
return {
|
||||
"ok": False,
|
||||
"via": via,
|
||||
"ms": round((time.perf_counter() - t0) * 1000, 1),
|
||||
"session_ms": round(ms_sess, 1),
|
||||
"error": f"GetNewSession failed: {err}",
|
||||
"errors": [f"GetNewSession: {err}"],
|
||||
"hints": hints,
|
||||
"message": text,
|
||||
"persona": persona_id,
|
||||
"pack": pack_name,
|
||||
}
|
||||
|
||||
chosen, preferred = _resolve_chat_model(cfg, base, sid, model=model)
|
||||
if not chosen:
|
||||
return {
|
||||
"ok": False,
|
||||
"via": via,
|
||||
"ms": round((time.perf_counter() - t0) * 1000, 1),
|
||||
"session_ms": round(ms_sess, 1),
|
||||
"error": "no model (AssistentListModels.preferred / default_chat)",
|
||||
"errors": ["model required"],
|
||||
"hints": hints + ["GET /assistent/roles + /ollama — нет preferred chat model"],
|
||||
"message": text,
|
||||
"persona": persona_id,
|
||||
"pack": pack_name,
|
||||
"preferred": preferred,
|
||||
}
|
||||
|
||||
payload = {
|
||||
"session_id": sid,
|
||||
"baseUrl": "http://127.0.0.1:11434",
|
||||
"model": chosen,
|
||||
"pack": pack_name,
|
||||
"persona": persona_id,
|
||||
"includeBase": True,
|
||||
"messages": [{"role": "user", "content": text}],
|
||||
"context_json": json.dumps(
|
||||
{
|
||||
"persona": persona_id,
|
||||
"debug_eval": True,
|
||||
"has_vision_image": False,
|
||||
"images_in_request": False,
|
||||
},
|
||||
ensure_ascii=False,
|
||||
),
|
||||
"skills": [],
|
||||
}
|
||||
ok, data, ms_call = _http_json(
|
||||
f"{base}/API/AssistentChat",
|
||||
method="POST",
|
||||
body=payload,
|
||||
timeout=t_chat,
|
||||
)
|
||||
return _finish_chat_eval(
|
||||
ok=ok,
|
||||
data=data,
|
||||
ms_call=ms_call,
|
||||
ms_total=(time.perf_counter() - t0) * 1000,
|
||||
via=via,
|
||||
session_ms=ms_sess,
|
||||
message=text,
|
||||
persona=persona_id,
|
||||
pack=pack_name,
|
||||
model=chosen,
|
||||
preferred=preferred,
|
||||
hints=hints,
|
||||
errors=errors,
|
||||
timeout=t_chat,
|
||||
return run_assistent_chat_eval_via_session(
|
||||
cfg,
|
||||
message=message,
|
||||
persona=persona,
|
||||
pack=pack,
|
||||
model=model,
|
||||
timeout=timeout,
|
||||
context=context,
|
||||
)
|
||||
|
||||
|
||||
@@ -623,157 +540,6 @@ def _finish_chat_eval(
|
||||
return out
|
||||
|
||||
|
||||
def _assistent_chat_eval_via_ssh(
|
||||
cfg: Config,
|
||||
*,
|
||||
message: str,
|
||||
persona: str,
|
||||
pack: str,
|
||||
model: str | None,
|
||||
timeout: float,
|
||||
hints: list[str],
|
||||
t0: float,
|
||||
) -> dict[str, Any]:
|
||||
state = load_state()
|
||||
if not debug_checks.ssh_ready(cfg, state):
|
||||
return {
|
||||
**debug_checks._ssh_fail(state.phase),
|
||||
"ok": False,
|
||||
"ms": round((time.perf_counter() - t0) * 1000, 1),
|
||||
"message": message,
|
||||
"persona": persona,
|
||||
"pack": pack,
|
||||
"model": model,
|
||||
"errors": [debug_checks.SSH_UNAVAILABLE],
|
||||
"hints": hints + ["туннель SwarmUI закрыт и SSH нет — gpu-rent tunnel / up"],
|
||||
}
|
||||
host = debug_checks.ssh_host(state)
|
||||
assert host is not None
|
||||
# Resolve model on laptop if possible (roles via SSH fs), else let remote ListModels.
|
||||
chosen = model
|
||||
preferred = None
|
||||
if not chosen:
|
||||
roles = collect_assistent_roles(cfg)
|
||||
preferred = (roles.get("roles") or {}).get("default_chat")
|
||||
if isinstance(preferred, str) and preferred.strip():
|
||||
chosen = preferred.strip()
|
||||
preferred = chosen
|
||||
|
||||
payload_model = chosen or ""
|
||||
# Remote script: GetNewSession → optional ListModels → AssistentChat
|
||||
script = f'''
|
||||
import json, urllib.request, time
|
||||
MSG = {json.dumps(message, ensure_ascii=False)}
|
||||
PERSONA = {json.dumps(persona, ensure_ascii=False)}
|
||||
PACK = {json.dumps(pack, ensure_ascii=False)}
|
||||
MODEL = {json.dumps(payload_model, ensure_ascii=False)}
|
||||
TIMEOUT = {float(timeout)}
|
||||
|
||||
def post(path, payload, timeout=TIMEOUT):
|
||||
t0 = time.time()
|
||||
req = urllib.request.Request(
|
||||
"http://127.0.0.1:7801" + path,
|
||||
data=json.dumps(payload).encode(),
|
||||
headers={{"Content-Type": "application/json"}},
|
||||
method="POST",
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
||||
raw = resp.read().decode("utf-8", "replace")
|
||||
ms = (time.time() - t0) * 1000
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except Exception:
|
||||
data = raw[:500]
|
||||
return True, data, ms
|
||||
except Exception as e:
|
||||
return False, str(e), (time.time() - t0) * 1000
|
||||
|
||||
ok, sess, ms = post("/API/GetNewSession", {{}}, timeout=20)
|
||||
if not ok or not isinstance(sess, dict) or not sess.get("session_id"):
|
||||
print(json.dumps({{"ok": False, "error": sess, "session_ms": ms, "via": "ssh"}}))
|
||||
raise SystemExit(0)
|
||||
sid = sess["session_id"]
|
||||
preferred = None
|
||||
model = MODEL.strip()
|
||||
if not model:
|
||||
cok, mdata, _ = post("/API/AssistentListModels", {{
|
||||
"session_id": sid, "baseUrl": "http://127.0.0.1:11434"
|
||||
}}, timeout=25)
|
||||
if cok and isinstance(mdata, dict):
|
||||
preferred = mdata.get("preferred")
|
||||
model = (preferred or "").strip()
|
||||
if not model:
|
||||
print(json.dumps({{
|
||||
"ok": False, "error": "no model", "session_ms": ms, "via": "ssh",
|
||||
"preferred": preferred
|
||||
}}))
|
||||
raise SystemExit(0)
|
||||
payload = {{
|
||||
"session_id": sid,
|
||||
"baseUrl": "http://127.0.0.1:11434",
|
||||
"model": model,
|
||||
"pack": PACK,
|
||||
"persona": PERSONA,
|
||||
"includeBase": True,
|
||||
"messages": [{{"role": "user", "content": MSG}}],
|
||||
"context_json": json.dumps({{
|
||||
"persona": PERSONA, "debug_eval": True,
|
||||
"has_vision_image": False, "images_in_request": False
|
||||
}}, ensure_ascii=False),
|
||||
"skills": [],
|
||||
}}
|
||||
cok, data, cms = post("/API/AssistentChat", payload, timeout=TIMEOUT)
|
||||
print(json.dumps({{
|
||||
"ok": cok, "data": data, "chat_ms": cms, "session_ms": ms,
|
||||
"via": "ssh", "model": model, "preferred": preferred or model
|
||||
}}, ensure_ascii=False))
|
||||
'''
|
||||
try:
|
||||
from gpu_rent.ssh_ops import run_ssh
|
||||
|
||||
out = run_ssh(
|
||||
cfg,
|
||||
host,
|
||||
"python3 - <<'PY'\n" + script + "\nPY",
|
||||
check=False,
|
||||
timeout=int(timeout) + 40,
|
||||
).strip()
|
||||
remote = json.loads(out.splitlines()[-1])
|
||||
except Exception as exc:
|
||||
return {
|
||||
"ok": False,
|
||||
"via": "ssh",
|
||||
"ms": round((time.perf_counter() - t0) * 1000, 1),
|
||||
"message": message,
|
||||
"persona": persona,
|
||||
"pack": pack,
|
||||
"model": model,
|
||||
"error": str(exc)[:240],
|
||||
"errors": [str(exc)[:240]],
|
||||
"hints": hints,
|
||||
}
|
||||
|
||||
data = remote.get("data")
|
||||
ok = bool(remote.get("ok"))
|
||||
return _finish_chat_eval(
|
||||
ok=ok,
|
||||
data=data if ok or isinstance(data, dict) else remote.get("error") or data,
|
||||
ms_call=float(remote.get("chat_ms") or 0),
|
||||
ms_total=(time.perf_counter() - t0) * 1000,
|
||||
via="ssh",
|
||||
session_ms=float(remote.get("session_ms") or 0),
|
||||
message=message,
|
||||
persona=persona,
|
||||
pack=pack,
|
||||
model=remote.get("model") or chosen,
|
||||
preferred=remote.get("preferred") or preferred,
|
||||
hints=hints,
|
||||
errors=[],
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
|
||||
_FS_CACHE: tuple[float, str, dict[str, Any]] | None = None
|
||||
_FS_TTL = 8.0
|
||||
|
||||
@@ -1366,6 +1132,15 @@ def collect_assistent_deep(
|
||||
"memory_broken": "GET /assistent/memory + /assistent/api (ListMemory)",
|
||||
"personas_missing": "GET /assistent/overlay + ListPersonas in /assistent/api",
|
||||
"chat_smoke": "GET /assistent/api?chat_smoke=1",
|
||||
"chat_eval": "POST /assistent/chat-eval (opt-in AssistentChat; not in /snapshot)",
|
||||
"chat_eval": "POST /assistent/chat-eval (one-shot; not in /snapshot)",
|
||||
"session": (
|
||||
"POST /assistent/session → POST .../chat (multi-turn + trace) "
|
||||
"→ DELETE /assistent/session/{id}"
|
||||
),
|
||||
"diagnose": "GET /assistent/diagnose or /assistent?logs=1",
|
||||
"compact_context_gap": (
|
||||
"AssistentChat does not return compactContext; "
|
||||
"trace fills from GetConfig Exact + synthetic context_json (see trace.gaps)"
|
||||
),
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user