mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-10-31 18:22:22 +01:00
Compare commits
103
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef3467e8d4 | ||
|
|
72e8520fa3 | ||
|
|
9276db9c11 | ||
|
|
a797cee0b3 | ||
|
|
76061498c8 | ||
|
|
04f8546d9d | ||
|
|
9835c1a7f9 | ||
|
|
239f09cbc4 | ||
|
|
a1a7f8fc9a | ||
|
|
87193d23f5 | ||
|
|
516f046fef | ||
|
|
48c3a7fc36 | ||
|
|
70fc58225c | ||
|
|
9efedc8b07 | ||
|
|
1d251ef867 | ||
|
|
bf5bf57bc1 | ||
|
|
2019a97339 | ||
|
|
9a1fdb192e | ||
|
|
bebf53be3c | ||
|
|
a8e0ff1c8e | ||
|
|
31685b050a | ||
|
|
03d25943ee | ||
|
|
71e3649985 | ||
|
|
45a57c0c38 | ||
|
|
71f6dc4e5d | ||
|
|
0c8a06a937 | ||
|
|
eea39ce103 | ||
|
|
9a5a15e17f | ||
|
|
f141ad2be5 | ||
|
|
a6e9294394 | ||
|
|
cfbe4aeca4 | ||
|
|
6f0679bb25 | ||
|
|
93c93bf076 | ||
|
|
f0f7464dfa | ||
|
|
1e57f774d3 | ||
|
|
2b310010cf | ||
|
|
b88154ecf9 | ||
|
|
e7796f71d3 | ||
|
|
412789051e | ||
|
|
b8adc4eb60 | ||
|
|
2246215b79 | ||
|
|
06fee0a3d8 | ||
|
|
f3813c261c | ||
|
|
e7a376b161 | ||
|
|
fec2bb11d6 | ||
|
|
c811f76d44 | ||
|
|
02d28c06d0 | ||
|
|
00ecf1c74e | ||
|
|
63feb227bb | ||
|
|
12fcddefab | ||
|
|
b46aa02ce1 | ||
|
|
352ee706f3 | ||
|
|
98b359cd4c | ||
|
|
2a2256af0e | ||
|
|
4f131d7c21 | ||
|
|
945e747eb4 | ||
|
|
61051d28f0 | ||
|
|
d87ae2a7f5 | ||
|
|
4fe8fb28e5 | ||
|
|
84e232e7ee | ||
|
|
2cace27723 | ||
|
|
508c7b3d9d | ||
|
|
5b8a72931d | ||
|
|
2e7edf908d | ||
|
|
b80f9afb3e | ||
|
|
533d48ad8d | ||
|
|
d5bc7fb70e | ||
|
|
f510b1973a | ||
|
|
1339efa856 | ||
|
|
6b284d2bc5 | ||
|
|
8acaec20ee | ||
|
|
5f18bcf6f8 | ||
|
|
18b6140511 | ||
|
|
eeb1107f7b | ||
|
|
4795c8d462 | ||
|
|
5c738a5a8e | ||
|
|
0ff5d47a8c | ||
|
|
f780f1dce9 | ||
|
|
1c5853003e | ||
|
|
0f8b137713 | ||
|
|
2c5d5005ba | ||
|
|
901b3ff42d | ||
|
|
c207ed1ed8 | ||
|
|
8b3a866899 | ||
|
|
4d8d363681 | ||
|
|
a7e8a6ef2e | ||
|
|
12631da4d3 | ||
|
|
927985f847 | ||
|
|
2046ce893f | ||
|
|
35ef91b25c | ||
|
|
91922d854c | ||
|
|
641752387b | ||
|
|
b3f86a0724 | ||
|
|
94dc74e500 | ||
|
|
fd27e9a429 | ||
|
|
bf3c000d1c | ||
|
|
8cd96aad46 | ||
|
|
6123b70b5c | ||
|
|
bfa514f6e8 | ||
|
|
1ff2fd0054 | ||
|
|
15c4cef36a | ||
|
|
84cdd866c5 | ||
|
|
dbfc96b8f3 |
@@ -31,21 +31,43 @@ for spec in $SPECS; do
|
|||||||
name="${spec%%==*}"
|
name="${spec%%==*}"
|
||||||
ver="${spec##*==}"
|
ver="${spec##*==}"
|
||||||
# pip download builds metadata (and trips the same bug), so fetch the raw
|
# pip download builds metadata (and trips the same bug), so fetch the raw
|
||||||
# sdist URL from the PyPI JSON API instead.
|
# sdist URL from the PyPI JSON API instead. Use curl with retries to
|
||||||
url="$(python - "$name" "$ver" <<'PY'
|
# survive transient TLS / network failures that urllib cannot recover from.
|
||||||
import json, sys, urllib.request
|
metadata="${name}-${ver}.json"
|
||||||
name, ver = sys.argv[1], sys.argv[2]
|
curl --fail --silent --show-error --location \
|
||||||
data = json.load(urllib.request.urlopen(f"https://pypi.org/pypi/{name}/{ver}/json"))
|
--retry 8 \
|
||||||
for f in data["urls"]:
|
--retry-delay 3 \
|
||||||
if f["packagetype"] == "sdist":
|
--retry-max-time 120 \
|
||||||
print(f["url"]); break
|
--retry-all-errors \
|
||||||
|
"https://pypi.org/pypi/${name}/${ver}/json" \
|
||||||
|
-o "$metadata"
|
||||||
|
|
||||||
|
url="$(python - "$metadata" <<'PY'
|
||||||
|
import json, sys
|
||||||
|
|
||||||
|
with open(sys.argv[1], encoding="utf-8") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
for item in data["urls"]:
|
||||||
|
if item["packagetype"] == "sdist":
|
||||||
|
print(item["url"])
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
sys.exit(f"no sdist found for {name}=={ver}")
|
raise SystemExit("no sdist found")
|
||||||
PY
|
PY
|
||||||
)"
|
)"
|
||||||
|
rm -f "$metadata"
|
||||||
|
|
||||||
echo ">> fetching ${name} ${ver}: ${url}"
|
echo ">> fetching ${name} ${ver}: ${url}"
|
||||||
curl -fsSL "$url" -o "${name}.tar.gz"
|
curl --fail --silent --show-error --location \
|
||||||
|
--retry 8 \
|
||||||
|
--retry-delay 3 \
|
||||||
|
--retry-max-time 120 \
|
||||||
|
--retry-all-errors \
|
||||||
|
"$url" \
|
||||||
|
-o "${name}.tar.gz"
|
||||||
tar xzf "${name}.tar.gz"
|
tar xzf "${name}.tar.gz"
|
||||||
|
rm -f "${name}.tar.gz"
|
||||||
done
|
done
|
||||||
|
|
||||||
echo ">> patching get_version()"
|
echo ">> patching get_version()"
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ async def list_tools() -> list[Tool]:
|
|||||||
"prompt": {"type": "string", "description": "Image description prompt"},
|
"prompt": {"type": "string", "description": "Image description prompt"},
|
||||||
"model": {"type": "string", "description": "Model name (auto-detects if omitted)"},
|
"model": {"type": "string", "description": "Model name (auto-detects if omitted)"},
|
||||||
"size": {"type": "string", "description": "Image size (default 1024x1024)"},
|
"size": {"type": "string", "description": "Image size (default 1024x1024)"},
|
||||||
"quality": {"type": "string", "description": "Quality: low, medium, high, auto (default medium)"},
|
"quality": {"type": "string", "description": "Quality: low, medium, high, auto (default configured quality, otherwise medium)"},
|
||||||
},
|
},
|
||||||
"required": ["prompt"],
|
"required": ["prompt"],
|
||||||
},
|
},
|
||||||
@@ -66,7 +66,7 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
|
|||||||
|
|
||||||
if not model_spec:
|
if not model_spec:
|
||||||
model_spec = _settings.get("image_model", "")
|
model_spec = _settings.get("image_model", "")
|
||||||
if quality == "medium" and _settings.get("image_quality"):
|
if "quality" not in arguments and _settings.get("image_quality"):
|
||||||
quality = _settings["image_quality"]
|
quality = _settings["image_quality"]
|
||||||
|
|
||||||
# Auto-detect best available image model
|
# Auto-detect best available image model
|
||||||
|
|||||||
@@ -887,6 +887,8 @@ def setup_auth_routes(auth_manager: AuthManager) -> APIRouter:
|
|||||||
"""Create a new integration (admin only)."""
|
"""Create a new integration (admin only)."""
|
||||||
_require_admin_user(request)
|
_require_admin_user(request)
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
|
if not isinstance(body, dict):
|
||||||
|
raise HTTPException(400, "Request body must be a JSON object")
|
||||||
item = add_integration(body)
|
item = add_integration(body)
|
||||||
return {"ok": True, "integration": mask_integration_secret(item)}
|
return {"ok": True, "integration": mask_integration_secret(item)}
|
||||||
|
|
||||||
@@ -895,6 +897,8 @@ def setup_auth_routes(auth_manager: AuthManager) -> APIRouter:
|
|||||||
"""Update an existing integration (admin only)."""
|
"""Update an existing integration (admin only)."""
|
||||||
_require_admin_user(request)
|
_require_admin_user(request)
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
|
if not isinstance(body, dict):
|
||||||
|
raise HTTPException(400, "Request body must be a JSON object")
|
||||||
item = update_integration(integration_id, body)
|
item = update_integration(integration_id, body)
|
||||||
if not item:
|
if not item:
|
||||||
raise HTTPException(404, "Integration not found")
|
raise HTTPException(404, "Integration not found")
|
||||||
|
|||||||
+16
-16
@@ -80,6 +80,18 @@ def _require_user(request: Request) -> str:
|
|||||||
return FALLBACK_OWNER
|
return FALLBACK_OWNER
|
||||||
|
|
||||||
|
|
||||||
|
async def _json_object_body(request: Request) -> dict:
|
||||||
|
"""Parse a JSON object body. An unreadable body counts as empty; a body
|
||||||
|
that is valid JSON but not an object (a list, string or number) is a 400."""
|
||||||
|
try:
|
||||||
|
body = await request.json()
|
||||||
|
except Exception:
|
||||||
|
return {}
|
||||||
|
if not isinstance(body, dict):
|
||||||
|
raise HTTPException(400, "Request body must be a JSON object")
|
||||||
|
return body
|
||||||
|
|
||||||
|
|
||||||
def _get_or_404_calendar(db, cal_id: str, owner: str) -> CalendarCal:
|
def _get_or_404_calendar(db, cal_id: str, owner: str) -> CalendarCal:
|
||||||
cal = db.query(CalendarCal).filter(CalendarCal.id == cal_id).first()
|
cal = db.query(CalendarCal).filter(CalendarCal.id == cal_id).first()
|
||||||
if not cal:
|
if not cal:
|
||||||
@@ -994,10 +1006,7 @@ def setup_calendar_routes(upload_handler=None) -> APIRouter:
|
|||||||
async def save_config(request: Request):
|
async def save_config(request: Request):
|
||||||
"""Legacy single-account endpoint — upserts the first account."""
|
"""Legacy single-account endpoint — upserts the first account."""
|
||||||
owner = _require_user(request)
|
owner = _require_user(request)
|
||||||
try:
|
body = await _json_object_body(request)
|
||||||
body = await request.json()
|
|
||||||
except Exception:
|
|
||||||
body = {}
|
|
||||||
accounts = _get_caldav_accounts(owner)
|
accounts = _get_caldav_accounts(owner)
|
||||||
if not (body.get("url") or "").strip():
|
if not (body.get("url") or "").strip():
|
||||||
_save_caldav_accounts(owner, [])
|
_save_caldav_accounts(owner, [])
|
||||||
@@ -1052,10 +1061,7 @@ def setup_calendar_routes(upload_handler=None) -> APIRouter:
|
|||||||
"""Add a new CalDAV account."""
|
"""Add a new CalDAV account."""
|
||||||
import uuid as _uuid
|
import uuid as _uuid
|
||||||
owner = _require_user(request)
|
owner = _require_user(request)
|
||||||
try:
|
body = await _json_object_body(request)
|
||||||
body = await request.json()
|
|
||||||
except Exception:
|
|
||||||
body = {}
|
|
||||||
from src.caldav_sync import validate_caldav_url
|
from src.caldav_sync import validate_caldav_url
|
||||||
try:
|
try:
|
||||||
url = validate_caldav_url(body.get("url", ""))
|
url = validate_caldav_url(body.get("url", ""))
|
||||||
@@ -1080,10 +1086,7 @@ def setup_calendar_routes(upload_handler=None) -> APIRouter:
|
|||||||
async def update_caldav_account(account_id: str, request: Request):
|
async def update_caldav_account(account_id: str, request: Request):
|
||||||
"""Update an existing CalDAV account by id."""
|
"""Update an existing CalDAV account by id."""
|
||||||
owner = _require_user(request)
|
owner = _require_user(request)
|
||||||
try:
|
body = await _json_object_body(request)
|
||||||
body = await request.json()
|
|
||||||
except Exception:
|
|
||||||
body = {}
|
|
||||||
accounts = _get_caldav_accounts(owner)
|
accounts = _get_caldav_accounts(owner)
|
||||||
idx = next((i for i, a in enumerate(accounts) if a.get("id") == account_id), None)
|
idx = next((i for i, a in enumerate(accounts) if a.get("id") == account_id), None)
|
||||||
if idx is None:
|
if idx is None:
|
||||||
@@ -1124,10 +1127,7 @@ def setup_calendar_routes(upload_handler=None) -> APIRouter:
|
|||||||
test an already-saved account. Falls back to the first saved account
|
test an already-saved account. Falls back to the first saved account
|
||||||
when nothing is provided."""
|
when nothing is provided."""
|
||||||
owner = _require_user(request)
|
owner = _require_user(request)
|
||||||
try:
|
body = await _json_object_body(request)
|
||||||
body = await request.json()
|
|
||||||
except Exception:
|
|
||||||
body = {}
|
|
||||||
url = (body.get("url") or "").strip()
|
url = (body.get("url") or "").strip()
|
||||||
user = (body.get("username") or "").strip()
|
user = (body.get("username") or "").strip()
|
||||||
pw = body.get("password") or ""
|
pw = body.get("password") or ""
|
||||||
|
|||||||
@@ -1901,7 +1901,7 @@ def _ts_or_zero(value) -> float:
|
|||||||
|
|
||||||
|
|
||||||
def _recover_empty_session_model(sess, session_id: str, owner: str | None = None) -> bool:
|
def _recover_empty_session_model(sess, session_id: str, owner: str | None = None) -> bool:
|
||||||
"""Re-populate sess.model from the matching endpoint's cached models.
|
"""Recover sess.model from the endpoint's visible cached and pinned models.
|
||||||
|
|
||||||
Covers the window between endpoint setup and the first chat send: the
|
Covers the window between endpoint setup and the first chat send: the
|
||||||
picker showed a model in the dropdown but the session record never got
|
picker showed a model in the dropdown but the session record never got
|
||||||
@@ -1965,6 +1965,9 @@ def _recover_empty_session_model(sess, session_id: str, owner: str | None = None
|
|||||||
visible = _visible_models(cached, getattr(ep, "hidden_models", None))
|
visible = _visible_models(cached, getattr(ep, "hidden_models", None))
|
||||||
except Exception:
|
except Exception:
|
||||||
visible = cached
|
visible = cached
|
||||||
|
if not is_chatgpt_subscription:
|
||||||
|
from src.endpoint_resolver import _endpoint_enabled_models
|
||||||
|
visible = _endpoint_enabled_models(ep)
|
||||||
if current_model and current_model in {str(item).strip() for item in visible}:
|
if current_model and current_model in {str(item).strip() for item in visible}:
|
||||||
return False
|
return False
|
||||||
if is_chatgpt_subscription:
|
if is_chatgpt_subscription:
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ from fastapi import HTTPException
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from routes._validators import validate_remote_host, validate_ssh_port
|
from routes._validators import validate_remote_host, validate_ssh_port
|
||||||
|
from routes.cookbook_output import HF_PARTIAL_BLOB_HELPER
|
||||||
from core.platform_compat import _ssh_exec_argv
|
from core.platform_compat import _ssh_exec_argv
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -378,6 +379,7 @@ def _cached_model_scan_script(model_dirs: list[str] | None = None, add_hf_cache:
|
|||||||
"""
|
"""
|
||||||
lines = [
|
lines = [
|
||||||
"import json, os, re, shutil, subprocess, urllib.request",
|
"import json, os, re, shutil, subprocess, urllib.request",
|
||||||
|
*HF_PARTIAL_BLOB_HELPER.splitlines(),
|
||||||
"models = []",
|
"models = []",
|
||||||
"seen = set()",
|
"seen = set()",
|
||||||
"BLOCKED_ROOTS = ('/sys', '/proc', '/dev', '/run', '/var/run')",
|
"BLOCKED_ROOTS = ('/sys', '/proc', '/dev', '/run', '/var/run')",
|
||||||
@@ -436,8 +438,10 @@ def _cached_model_scan_script(model_dirs: list[str] | None = None, add_hf_cache:
|
|||||||
" sz, nf, ic = 0, 0, False",
|
" sz, nf, ic = 0, 0, False",
|
||||||
" if os.path.isdir(blobs):",
|
" if os.path.isdir(blobs):",
|
||||||
" for f in os.scandir(blobs):",
|
" for f in os.scandir(blobs):",
|
||||||
|
" if f.name.endswith('.incomplete'):",
|
||||||
|
" if not is_partial_blob(f.path): continue",
|
||||||
|
" ic = True",
|
||||||
" if f.is_file(): nf += 1; sz += f.stat().st_size",
|
" if f.is_file(): nf += 1; sz += f.stat().st_size",
|
||||||
" if f.name.endswith('.incomplete'): ic = True",
|
|
||||||
" snap = os.path.join(cache, d, 'snapshots')",
|
" snap = os.path.join(cache, d, 'snapshots')",
|
||||||
" def snapshot_size():",
|
" def snapshot_size():",
|
||||||
" total, count, incomplete = 0, 0, False",
|
" total, count, incomplete = 0, 0, False",
|
||||||
@@ -1487,6 +1491,20 @@ def _diagnose_serve_output(text: str) -> dict | None:
|
|||||||
"Port is already in use.",
|
"Port is already in use.",
|
||||||
[{"label": "retry on port 8001", "op": "replace", "flag": "--port", "value": "8001"}],
|
[{"label": "retry on port 8001", "op": "replace", "flag": "--port", "value": "8001"}],
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
# PyPI torch/vLLM wheels now target CUDA 13, which needs driver 580+.
|
||||||
|
# torch: "The NVIDIA driver on your system is too old (found version 12080)";
|
||||||
|
# vLLM kernels: "CUDA driver version is insufficient for CUDA runtime version".
|
||||||
|
r"NVIDIA driver on your system is too old|CUDA driver version is insufficient",
|
||||||
|
"The installed PyTorch/vLLM build needs a newer NVIDIA driver than this server has.",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"label": "install builds for the driver's CUDA version, e.g. vLLM's +cu129 wheel from its GitHub release with uv pip install … --torch-backend cu129",
|
||||||
|
"op": "manual",
|
||||||
|
},
|
||||||
|
{"label": "or update the NVIDIA driver (CUDA 13 builds need driver 580 or newer)", "op": "manual"},
|
||||||
|
],
|
||||||
|
),
|
||||||
(
|
(
|
||||||
r"No CUDA GPUs are available|no GPU.*found|CUDA_VISIBLE_DEVICES.*invalid",
|
r"No CUDA GPUs are available|no GPU.*found|CUDA_VISIBLE_DEVICES.*invalid",
|
||||||
"No GPUs are visible to the serve process.",
|
"No GPUs are visible to the serve process.",
|
||||||
|
|||||||
@@ -8,6 +8,20 @@ import re
|
|||||||
|
|
||||||
_FETCHING_ZERO_FILES_RE = re.compile(r"Fetching\s+0\s+files", re.IGNORECASE)
|
_FETCHING_ZERO_FILES_RE = re.compile(r"Fetching\s+0\s+files", re.IGNORECASE)
|
||||||
|
|
||||||
|
# Shared source embedded in local/remote inventory and status probes. A blank
|
||||||
|
# retry marker beside its completed, nonempty content-addressed blob is stale;
|
||||||
|
# a blank marker alone may be a download that has not received its first byte.
|
||||||
|
HF_PARTIAL_BLOB_HELPER = """def is_partial_blob(path):
|
||||||
|
if not path.endswith('.incomplete'):
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
completed = path[:-len('.incomplete')]
|
||||||
|
return (os.path.getsize(path) != 0 or not os.path.isfile(completed)
|
||||||
|
or os.path.getsize(completed) == 0)
|
||||||
|
except OSError:
|
||||||
|
return True
|
||||||
|
"""
|
||||||
|
|
||||||
# Probe scripts for the dead-session download check, run as
|
# Probe scripts for the dead-session download check, run as
|
||||||
# `python3 -c <PROBE> <repo_id> <cache_root>` (locally or over SSH).
|
# `python3 -c <PROBE> <repo_id> <cache_root>` (locally or over SSH).
|
||||||
# cache_root is the task's custom download dir, '' for the default HF cache.
|
# cache_root is the task's custom download dir, '' for the default HF cache.
|
||||||
@@ -15,7 +29,7 @@ _FETCHING_ZERO_FILES_RE = re.compile(r"Fetching\s+0\s+files", re.IGNORECASE)
|
|||||||
# HF_HOME=<local_dir>, so that task's cache lives under <local_dir>/hub, and
|
# HF_HOME=<local_dir>, so that task's cache lives under <local_dir>/hub, and
|
||||||
# the probe process's own environment knows nothing about it.
|
# the probe process's own environment knows nothing about it.
|
||||||
HF_CACHE_COMPLETE_PROBE = (
|
HF_CACHE_COMPLETE_PROBE = (
|
||||||
"import os,sys;"
|
"import os,sys\n" + HF_PARTIAL_BLOB_HELPER +
|
||||||
"repo=sys.argv[1];"
|
"repo=sys.argv[1];"
|
||||||
"root=os.path.expanduser(sys.argv[2]) if len(sys.argv)>2 and sys.argv[2] else '';"
|
"root=os.path.expanduser(sys.argv[2]) if len(sys.argv)>2 and sys.argv[2] else '';"
|
||||||
"base=os.path.join(root,'hub') if root else (os.environ.get('HUGGINGFACE_HUB_CACHE') or os.path.join(os.environ.get('HF_HOME', os.path.expanduser('~/.cache/huggingface')), 'hub'));"
|
"base=os.path.join(root,'hub') if root else (os.environ.get('HUGGINGFACE_HUB_CACHE') or os.path.join(os.environ.get('HF_HOME', os.path.expanduser('~/.cache/huggingface')), 'hub'));"
|
||||||
@@ -24,18 +38,18 @@ HF_CACHE_COMPLETE_PROBE = (
|
|||||||
"ok=os.path.isdir(snap) and any(os.path.isdir(os.path.join(snap,x)) and os.listdir(os.path.join(snap,x)) for x in os.listdir(snap));"
|
"ok=os.path.isdir(snap) and any(os.path.isdir(os.path.join(snap,x)) and os.listdir(os.path.join(snap,x)) for x in os.listdir(snap));"
|
||||||
"inc=False;"
|
"inc=False;"
|
||||||
"blobs=os.path.join(d,'blobs');"
|
"blobs=os.path.join(d,'blobs');"
|
||||||
"inc=os.path.isdir(blobs) and any(x.endswith('.incomplete') for x in os.listdir(blobs));"
|
"inc=os.path.isdir(blobs) and any(is_partial_blob(os.path.join(blobs,x)) for x in os.listdir(blobs));"
|
||||||
"sys.exit(0 if ok and not inc else 1)"
|
"sys.exit(0 if ok and not inc else 1)"
|
||||||
)
|
)
|
||||||
|
|
||||||
HF_CACHE_INCOMPLETE_PROBE = (
|
HF_CACHE_INCOMPLETE_PROBE = (
|
||||||
"import os,sys;"
|
"import os,sys\n" + HF_PARTIAL_BLOB_HELPER +
|
||||||
"repo=sys.argv[1];"
|
"repo=sys.argv[1];"
|
||||||
"root=os.path.expanduser(sys.argv[2]) if len(sys.argv)>2 and sys.argv[2] else '';"
|
"root=os.path.expanduser(sys.argv[2]) if len(sys.argv)>2 and sys.argv[2] else '';"
|
||||||
"base=os.path.join(root,'hub') if root else (os.environ.get('HUGGINGFACE_HUB_CACHE') or os.path.join(os.environ.get('HF_HOME', os.path.expanduser('~/.cache/huggingface')), 'hub'));"
|
"base=os.path.join(root,'hub') if root else (os.environ.get('HUGGINGFACE_HUB_CACHE') or os.path.join(os.environ.get('HF_HOME', os.path.expanduser('~/.cache/huggingface')), 'hub'));"
|
||||||
"d=os.path.join(base,'models--'+repo.replace('/','--'));"
|
"d=os.path.join(base,'models--'+repo.replace('/','--'));"
|
||||||
"blobs=os.path.join(d,'blobs');"
|
"blobs=os.path.join(d,'blobs');"
|
||||||
"inc=os.path.isdir(blobs) and any(x.endswith('.incomplete') for x in os.listdir(blobs));"
|
"inc=os.path.isdir(blobs) and any(is_partial_blob(os.path.join(blobs,x)) for x in os.listdir(blobs));"
|
||||||
"sys.exit(0 if inc else 1)"
|
"sys.exit(0 if inc else 1)"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+8
-174
@@ -468,175 +468,6 @@ def setup_cookbook_routes() -> APIRouter:
|
|||||||
task["payload"].pop("hf_token", None)
|
task["payload"].pop("hf_token", None)
|
||||||
return state
|
return state
|
||||||
|
|
||||||
def _diagnose_serve_output(text: str) -> dict | None:
|
|
||||||
"""Server-side mirror of the Cookbook UI's common serve diagnoses.
|
|
||||||
|
|
||||||
The browser uses cookbook-diagnosis.js for clickable fixes. This gives
|
|
||||||
the agent/tool path the same structured signal so it can retry with an
|
|
||||||
adjusted command instead of guessing from raw tmux output.
|
|
||||||
"""
|
|
||||||
if not text:
|
|
||||||
return None
|
|
||||||
tail = text[-6000:]
|
|
||||||
patterns = [
|
|
||||||
(
|
|
||||||
r"No available memory for the cache blocks|Available KV cache memory:.*-",
|
|
||||||
"No GPU memory left for KV cache after loading model.",
|
|
||||||
[
|
|
||||||
{"label": "retry with GPU memory utilization 0.95", "op": "replace", "flag": "--gpu-memory-utilization", "value": "0.95"},
|
|
||||||
{"label": "retry with context 2048", "op": "replace", "flag": "--max-model-len", "value": "2048"},
|
|
||||||
],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"CUDA out of memory|torch\.cuda\.OutOfMemoryError|CUDA error: out of memory|warming up sampler|max_num_seqs.*gpu_memory_utilization",
|
|
||||||
"GPU ran out of memory during startup or warmup.",
|
|
||||||
[
|
|
||||||
{"label": "retry with context 4096", "op": "replace", "flag": "--max-model-len", "value": "4096"},
|
|
||||||
{"label": "retry with GPU memory utilization 0.80", "op": "replace", "flag": "--gpu-memory-utilization", "value": "0.80"},
|
|
||||||
{"label": "retry with --enforce-eager", "op": "append", "arg": "--enforce-eager"},
|
|
||||||
],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"not divisib|must be divisible|attention heads.*divisible",
|
|
||||||
"Tensor parallel size is incompatible with the model.",
|
|
||||||
[
|
|
||||||
{"label": "retry with tensor parallel size 1", "op": "replace", "flag": "--tensor-parallel-size", "value": "1"},
|
|
||||||
{"label": "retry with tensor parallel size 2", "op": "replace", "flag": "--tensor-parallel-size", "value": "2"},
|
|
||||||
],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"KV cache.*too (small|large)|max_model_len.*exceeds|maximum.*context",
|
|
||||||
"Context length is too large for available GPU memory.",
|
|
||||||
[
|
|
||||||
{"label": "retry with context 8192", "op": "replace", "flag": "--max-model-len", "value": "8192"},
|
|
||||||
{"label": "retry with context 4096", "op": "replace", "flag": "--max-model-len", "value": "4096"},
|
|
||||||
],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"enable-auto-tool-choice requires --tool-call-parser",
|
|
||||||
"Auto tool choice requires an explicit tool call parser.",
|
|
||||||
[{"label": "retry with Hermes tool parser", "op": "append", "arg": "--tool-call-parser hermes"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"Please pass.*trust.remote.code=True|contains custom code which must be executed to correctly load|does not recognize this architecture|model type.*but Transformers does not",
|
|
||||||
"Model requires custom code or newer model support.",
|
|
||||||
[{"label": "retry with --trust-remote-code", "op": "append", "arg": "--trust-remote-code"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"Either a revision or a version must be specified|transformers\.integrations\.hub_kernels|kernels/layer",
|
|
||||||
"vLLM/Transformers kernel package mismatch.",
|
|
||||||
[{"label": "update vLLM, Transformers, and kernels on this server", "op": "dependency", "package": "vllm transformers kernels"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"Address already in use|bind.*address.*in use",
|
|
||||||
"Port is already in use.",
|
|
||||||
[{"label": "retry on port 8001", "op": "replace", "flag": "--port", "value": "8001"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"No CUDA GPUs are available|no GPU.*found|CUDA_VISIBLE_DEVICES.*invalid",
|
|
||||||
"No GPUs are visible to the serve process.",
|
|
||||||
[{"label": "clear Cookbook GPU selection or choose available GPUs", "op": "settings", "field": "gpus", "value": ""}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"Failed to infer device type|NVML Shared Library Not Found|No module named 'amdsmi'|platform is not available",
|
|
||||||
"vLLM could not find a supported GPU (CUDA or ROCm). "
|
|
||||||
"This machine may have integrated or unsupported graphics only.",
|
|
||||||
[
|
|
||||||
{"label": "switch to llama.cpp (CPU/Metal, works without a discrete GPU)", "op": "manual"},
|
|
||||||
{"label": "switch to Ollama (CPU/Metal, works without a discrete GPU)", "op": "manual"},
|
|
||||||
],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"vllm.*command not found|No module named vllm|ERROR: vLLM is not installed",
|
|
||||||
"vLLM is not installed or not in PATH on this server.",
|
|
||||||
[{"label": "install vLLM in Cookbook Dependencies", "op": "dependency", "package": "vllm"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"sgl_kernel[\s\S]*(Python\.h|libnuma\.so\.1|common_ops|libnvrtc\.so)|"
|
|
||||||
r"(Python\.h|libnuma\.so\.1|common_ops|libnvrtc\.so)[\s\S]*sgl_kernel|"
|
|
||||||
r"Could not load any common_ops library|"
|
|
||||||
r"Please ensure sgl_kernel is properly installed",
|
|
||||||
"SGLang native kernel/runtime is missing or mismatched on this server.",
|
|
||||||
[
|
|
||||||
{"label": "repair sglang-kernel in this Python environment", "op": "dependency", "package": "sglang-kernel"},
|
|
||||||
{"label": "if libnvrtc is still missing, install the matching CUDA/NVRTC runtime on this host", "op": "manual"},
|
|
||||||
],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"sglang.*command not found|No module named sglang|SGLang is not installed",
|
|
||||||
"SGLang is not installed or not in PATH on this server.",
|
|
||||||
[{"label": "install SGLang in Cookbook Dependencies", "op": "dependency", "package": "sglang[all]"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"No module named ['\"]?mlx_lm|mlx_lm.*command not found|MLX is not installed|MLX LM is not installed",
|
|
||||||
"MLX LM is not installed on this server.",
|
|
||||||
[{"label": "install mlx-lm in Cookbook Dependencies", "op": "dependency", "package": "mlx-lm"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"OmniGen2Pipeline|module diffusers has no attribute .*Pipeline|custom_pipeline=.*failed",
|
|
||||||
"This image model uses a custom Diffusers pipeline that the launch environment does not know yet.",
|
|
||||||
[{"label": "update Diffusers image dependencies", "op": "dependency", "package": "diffusers transformers accelerate"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"Unable to quantize model of type <class ['\"]mlx_lm\.models\.switch_layers\.QuantizedSwitchLinear['\"]>|QuantizedSwitchLinear",
|
|
||||||
"MLX-LM tried to quantize an already-quantized DeepSeek switch layer.",
|
|
||||||
[
|
|
||||||
{"label": "relaunch from the cached local Hugging Face snapshot path on this Mac", "op": "manual"},
|
|
||||||
{"label": "Odysseus now rewrites MLX repo-id launches to a cached snapshot when one exists", "op": "manual"},
|
|
||||||
],
|
|
||||||
),
|
|
||||||
# System build deps come BEFORE the generic llama.cpp catch-all
|
|
||||||
# so cmake / build-essential / git missing → a specific OS-package
|
|
||||||
# remediation instead of "install llama-cpp-python[server]" (which
|
|
||||||
# itself fails to compile when cmake is absent).
|
|
||||||
(
|
|
||||||
r"cmake: command not found|cmake.*not found.*[Cc]ould not",
|
|
||||||
"cmake is required to build llama.cpp from source but isn't installed on this server.",
|
|
||||||
[{"label": "install build deps for llama.cpp (apt: cmake build-essential git / pacman: cmake base-devel git / dnf: cmake gcc-c++ make git / brew: cmake git)", "op": "dependency", "package": "llama-cpp-python[server]"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"^(make|g\+\+|gcc): command not found|Could not find C\+\+ compiler",
|
|
||||||
"A C/C++ compiler (build-essential) is required to build llama.cpp from source.",
|
|
||||||
[{"label": "install build deps for llama.cpp on this server", "op": "dependency", "package": "llama-cpp-python[server]"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"^git: command not found",
|
|
||||||
"git is required to clone the llama.cpp source tree.",
|
|
||||||
[{"label": "install build deps for llama.cpp on this server", "op": "dependency", "package": "llama-cpp-python[server]"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"llama-server.*command not found|llama\.cpp.*not found|No module named.*llama_cpp|No module named 'starlette_context'",
|
|
||||||
"llama.cpp / llama-cpp-python dependencies are missing.",
|
|
||||||
[{"label": "install llama.cpp dependencies or llama-cpp-python[server]", "op": "dependency", "package": "llama-cpp-python[server]"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"No GGUF found on this host|no \.gguf file|No GGUF file found",
|
|
||||||
"No GGUF file found for this model on this host. The llama.cpp backend needs a .gguf file.",
|
|
||||||
[{"label": "download a GGUF build of this model (repo name usually ends in -GGUF, file like Q4_K_M.gguf)", "op": "manual"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"No module named 'torch'|No module named torch|No module named 'torchvision'|No module named torchvision|No module named 'diffusers'|No module named diffusers|No module named 'scipy'|No module named scipy|install scipy if you want to use beta sigmas|requires the Torchvision library",
|
|
||||||
"Diffusion serving requires PyTorch, Torchvision, Diffusers, Accelerate, and SciPy.",
|
|
||||||
[{"label": "install Diffusers image deps in Cookbook Dependencies", "op": "dependency", "package": "diffusers[torch] torchvision accelerate scipy python-multipart"}],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
r"403 Forbidden|401 Unauthorized|Access to model.*is restricted|gated repo|not in the authorized list|awaiting a review",
|
|
||||||
"Model access is gated or unauthorized.",
|
|
||||||
[{"label": "set HF token and request model access on HuggingFace", "op": "manual"}],
|
|
||||||
),
|
|
||||||
]
|
|
||||||
for pattern, message, suggestions in patterns:
|
|
||||||
if re.search(pattern, tail, re.I):
|
|
||||||
return {"message": message, "suggestions": suggestions}
|
|
||||||
if re.search(r"Traceback \(most recent call last\)", tail, re.I) and not re.search(
|
|
||||||
r"Application startup complete|GET /v1/|Uvicorn running on", tail, re.I
|
|
||||||
):
|
|
||||||
return {
|
|
||||||
"message": "Python traceback detected during serve startup.",
|
|
||||||
"suggestions": [{"label": "inspect traceback and retry with adjusted backend/settings", "op": "manual"}],
|
|
||||||
}
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _state_for_client(state):
|
def _state_for_client(state):
|
||||||
"""Return cookbook state without raw secrets for browser clients."""
|
"""Return cookbook state without raw secrets for browser clients."""
|
||||||
_strip_task_secrets(state)
|
_strip_task_secrets(state)
|
||||||
@@ -3930,7 +3761,7 @@ def setup_cookbook_routes() -> APIRouter:
|
|||||||
return {"ok": False, "files": [], "error": f"HF API HTTP {resp.status_code}"}
|
return {"ok": False, "files": [], "error": f"HF API HTTP {resp.status_code}"}
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("HF GGUF file scan failed for %s", repo)
|
logger.exception("HF GGUF file scan failed for %s", repo_id)
|
||||||
return {"ok": False, "files": [], "error": "HF API request failed"}
|
return {"ok": False, "files": [], "error": "HF API request failed"}
|
||||||
files = [
|
files = [
|
||||||
str(s.get("rfilename") or "")
|
str(s.get("rfilename") or "")
|
||||||
@@ -4302,7 +4133,7 @@ def setup_cookbook_routes() -> APIRouter:
|
|||||||
tmux output can stop at a stale progress line if the pane/session
|
tmux output can stop at a stale progress line if the pane/session
|
||||||
disappears before Cookbook captures the final DOWNLOAD_OK marker.
|
disappears before Cookbook captures the final DOWNLOAD_OK marker.
|
||||||
In that case, trust the cache shape: a snapshot directory with files
|
In that case, trust the cache shape: a snapshot directory with files
|
||||||
and no *.incomplete blobs means HuggingFace finished materializing the
|
and no unfinished blobs means HuggingFace finished materializing the
|
||||||
model. cache_root is the task's custom download dir — the runner
|
model. cache_root is the task's custom download dir — the runner
|
||||||
pointed HF_HOME there, so the cache lives under <cache_root>/hub,
|
pointed HF_HOME there, so the cache lives under <cache_root>/hub,
|
||||||
not wherever this probe's environment says.
|
not wherever this probe's environment says.
|
||||||
@@ -4327,7 +4158,8 @@ def setup_cookbook_routes() -> APIRouter:
|
|||||||
"""Best-effort check for resumable HF partial blobs.
|
"""Best-effort check for resumable HF partial blobs.
|
||||||
|
|
||||||
A lost SSH/tmux session can leave a real download still incomplete.
|
A lost SSH/tmux session can leave a real download still incomplete.
|
||||||
Treat any *.incomplete blob as stronger evidence than stale
|
Treat unfinished blobs (excluding empty markers paired with a
|
||||||
|
completed blob) as stronger evidence than stale
|
||||||
"100%" lines in the captured pane output.
|
"100%" lines in the captured pane output.
|
||||||
"""
|
"""
|
||||||
if not repo_id or "/" not in repo_id:
|
if not repo_id or "/" not in repo_id:
|
||||||
@@ -4445,7 +4277,9 @@ def setup_cookbook_routes() -> APIRouter:
|
|||||||
# fills the visible tail. Without this, output_tail ends up
|
# fills the visible tail. Without this, output_tail ends up
|
||||||
# as just "Locale: C / Ubuntu_Odysseus ❯" and the agent
|
# as just "Locale: C / Ubuntu_Odysseus ❯" and the agent
|
||||||
# can't diagnose the actual error.
|
# can't diagnose the actual error.
|
||||||
capture_cmd = ssh_base + [remote, _remote_tmux_command("capture-pane", "-t", session_id, "-p", "-S", "-500")]
|
# -J joins lines tmux wrapped at the pane width, so an error
|
||||||
|
# split mid-word still matches the serve diagnosis patterns.
|
||||||
|
capture_cmd = ssh_base + [remote, _remote_tmux_command("capture-pane", "-t", session_id, "-p", "-J", "-S", "-500")]
|
||||||
elif IS_WINDOWS:
|
elif IS_WINDOWS:
|
||||||
# LOCAL Windows task: launched as a detached process (no tmux).
|
# LOCAL Windows task: launched as a detached process (no tmux).
|
||||||
# Liveness comes from the <session>.pid file, output from the
|
# Liveness comes from the <session>.pid file, output from the
|
||||||
@@ -4454,7 +4288,7 @@ def setup_cookbook_routes() -> APIRouter:
|
|||||||
capture_cmd = None
|
capture_cmd = None
|
||||||
else:
|
else:
|
||||||
check_cmd = ["tmux", "has-session", "-t", session_id]
|
check_cmd = ["tmux", "has-session", "-t", session_id]
|
||||||
capture_cmd = ["tmux", "capture-pane", "-t", session_id, "-p", "-S", "-500"]
|
capture_cmd = ["tmux", "capture-pane", "-t", session_id, "-p", "-J", "-S", "-500"]
|
||||||
|
|
||||||
local_win_task = (not remote) and IS_WINDOWS
|
local_win_task = (not remote) and IS_WINDOWS
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ from core.database import SessionLocal, Document, DocumentVersion
|
|||||||
from core.database import Session as DbSession
|
from core.database import Session as DbSession
|
||||||
from src.auth_helpers import get_current_user, _auth_disabled
|
from src.auth_helpers import get_current_user, _auth_disabled
|
||||||
from src.constants import MAIL_ATTACHMENTS_DIR
|
from src.constants import MAIL_ATTACHMENTS_DIR
|
||||||
|
from src.session_search import _escape_like
|
||||||
from src.upload_handler import reserve_upload_references
|
from src.upload_handler import reserve_upload_references
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -571,9 +572,10 @@ def setup_document_routes(session_manager, upload_handler=None) -> APIRouter:
|
|||||||
# match docs containing both words regardless of position/order.
|
# match docs containing both words regardless of position/order.
|
||||||
if search:
|
if search:
|
||||||
for tok in search.split():
|
for tok in search.split():
|
||||||
term = f"%{tok}%"
|
term = f"%{_escape_like(tok)}%"
|
||||||
q = q.filter(
|
q = q.filter(
|
||||||
Document.title.ilike(term) | Document.current_content.ilike(term)
|
Document.title.ilike(term, escape="\\")
|
||||||
|
| Document.current_content.ilike(term, escape="\\")
|
||||||
)
|
)
|
||||||
|
|
||||||
# Language filter. "pdf" is a display language derived from the
|
# Language filter. "pdf" is a display language derived from the
|
||||||
@@ -781,9 +783,11 @@ def setup_document_routes(session_manager, upload_handler=None) -> APIRouter:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning("Failed to parse export request body, defaulting to empty", exc_info=e)
|
logger.warning("Failed to parse export request body, defaulting to empty", exc_info=e)
|
||||||
data = {}
|
data = {}
|
||||||
ids = data.get("ids") or []
|
ids = data.get("ids") if isinstance(data, dict) else None
|
||||||
if not ids:
|
if not ids:
|
||||||
raise HTTPException(400, "No documents specified")
|
raise HTTPException(400, "No documents specified")
|
||||||
|
if not isinstance(ids, list) or not all(isinstance(i, str) for i in ids):
|
||||||
|
raise HTTPException(400, "ids must be a list of document ids")
|
||||||
_ext = {
|
_ext = {
|
||||||
"javascript": ".js", "python": ".py", "html": ".html", "css": ".css",
|
"javascript": ".js", "python": ".py", "html": ".html", "css": ".css",
|
||||||
"markdown": ".md", "json": ".json", "yaml": ".yml", "bash": ".sh",
|
"markdown": ".md", "json": ".json", "yaml": ".yml", "bash": ".sh",
|
||||||
@@ -810,10 +814,13 @@ def setup_document_routes(session_manager, upload_handler=None) -> APIRouter:
|
|||||||
ext = _ext.get(doc.language or "text", ".txt")
|
ext = _ext.get(doc.language or "text", ".txt")
|
||||||
base = (doc.title or "document").strip() or "document"
|
base = (doc.title or "document").strip() or "document"
|
||||||
base = re.sub(r"[^\w\-. ]+", "", base)[:60].strip() or doc.id
|
base = re.sub(r"[^\w\-. ]+", "", base)[:60].strip() or doc.id
|
||||||
name = base if "." in base else base + ext
|
has_ext = base.lower().endswith(ext)
|
||||||
|
stem = base[:-len(ext)] if has_ext else base
|
||||||
|
suffix = base[-len(ext):] if has_ext else ext
|
||||||
|
name = stem + suffix
|
||||||
i = 1
|
i = 1
|
||||||
while name in used:
|
while name in used:
|
||||||
name = f"{base}-{i}" + ("" if "." in base else ext)
|
name = f"{stem}-{i}{suffix}"
|
||||||
i += 1
|
i += 1
|
||||||
used.add(name)
|
used.add(name)
|
||||||
content = doc.current_content or ""
|
content = doc.current_content or ""
|
||||||
|
|||||||
@@ -19,9 +19,10 @@ def _split_family_token(token):
|
|||||||
def _derive_family(filename):
|
def _derive_family(filename):
|
||||||
"""Derive a font-family name from a filename like 'JetBrainsMono-Regular.woff2' → 'JetBrains Mono'."""
|
"""Derive a font-family name from a filename like 'JetBrainsMono-Regular.woff2' → 'JetBrains Mono'."""
|
||||||
name = os.path.splitext(filename)[0]
|
name = os.path.splitext(filename)[0]
|
||||||
# Strip common weight/style suffixes
|
# Strip weight/style suffixes, including BoldItalic and Bold-Italic.
|
||||||
name = re.sub(
|
name = re.sub(
|
||||||
r'[-_ ]?(Thin|ExtraLight|UltraLight|Light|Regular|Medium|SemiBold|DemiBold|Bold|ExtraBold|UltraBold|Black|Heavy|Italic|Oblique|Variable|VF)$',
|
r'[-_ ]?(Thin|ExtraLight|UltraLight|Light|Regular|Medium|SemiBold|DemiBold|Bold|ExtraBold|UltraBold|Black|Heavy|Italic|Oblique|Variable|VF)'
|
||||||
|
r'(?:[-_ ]?(?:Italic|Oblique))?$',
|
||||||
'', name, flags=re.IGNORECASE
|
'', name, flags=re.IGNORECASE
|
||||||
)
|
)
|
||||||
# Replace dashes/underscores with spaces
|
# Replace dashes/underscores with spaces
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ from fastapi import APIRouter, HTTPException, Query, Request
|
|||||||
from core.database import SessionLocal, GalleryImage, GalleryAlbum, ModelEndpoint
|
from core.database import SessionLocal, GalleryImage, GalleryAlbum, ModelEndpoint
|
||||||
from core.database import Session as DbSession
|
from core.database import Session as DbSession
|
||||||
from src.auth_helpers import get_current_user, owner_filter, require_privilege
|
from src.auth_helpers import get_current_user, owner_filter, require_privilege
|
||||||
|
from src.session_search import escape_like_pattern
|
||||||
from src.upload_limits import (
|
from src.upload_limits import (
|
||||||
read_upload_limited,
|
read_upload_limited,
|
||||||
GALLERY_UPLOAD_MAX_BYTES,
|
GALLERY_UPLOAD_MAX_BYTES,
|
||||||
@@ -305,6 +306,23 @@ def _b64_bytes(value: str, *, label: str = "image") -> bytes:
|
|||||||
raise HTTPException(400, f"Invalid {label}") from exc
|
raise HTTPException(400, f"Invalid {label}") from exc
|
||||||
|
|
||||||
|
|
||||||
|
async def _json_object(request: Request) -> dict:
|
||||||
|
try:
|
||||||
|
data = await request.json()
|
||||||
|
except ValueError:
|
||||||
|
raise HTTPException(400, "Invalid JSON body")
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
raise HTTPException(400, "JSON object body required")
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
async def _image_ids(request: Request) -> list:
|
||||||
|
ids = (await _json_object(request)).get("image_ids", [])
|
||||||
|
if not isinstance(ids, list) or not all(isinstance(i, str) for i in ids):
|
||||||
|
raise HTTPException(400, "image_ids must be a list of image ids")
|
||||||
|
return ids
|
||||||
|
|
||||||
|
|
||||||
def _pil_image_to_b64(img, *, fmt: str = "PNG") -> str:
|
def _pil_image_to_b64(img, *, fmt: str = "PNG") -> str:
|
||||||
buf = io.BytesIO()
|
buf = io.BytesIO()
|
||||||
img.save(buf, format=fmt)
|
img.save(buf, format=fmt)
|
||||||
@@ -1184,15 +1202,18 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
)
|
)
|
||||||
q = _owner_filter(q, user)
|
q = _owner_filter(q, user)
|
||||||
|
|
||||||
# Search filter (prompt + tags + ai_tags)
|
# Per-term AND search: a single `%foo bar%` LIKE only matched the
|
||||||
|
# exact phrase, so "red car" missed "a car that is red".
|
||||||
|
# LIKE wildcards are escaped per token.
|
||||||
if search:
|
if search:
|
||||||
term = f"%{search}%"
|
|
||||||
from sqlalchemy import or_
|
from sqlalchemy import or_
|
||||||
q = q.filter(or_(
|
for tok in search.split():
|
||||||
GalleryImage.prompt.ilike(term),
|
term = f"%{escape_like_pattern(tok)}%"
|
||||||
GalleryImage.tags.ilike(term),
|
q = q.filter(or_(
|
||||||
GalleryImage.ai_tags.ilike(term),
|
GalleryImage.prompt.ilike(term, escape="\\"),
|
||||||
))
|
GalleryImage.tags.ilike(term, escape="\\"),
|
||||||
|
GalleryImage.ai_tags.ilike(term, escape="\\"),
|
||||||
|
))
|
||||||
|
|
||||||
# Tag filter. The UI stacks multiple tag pills by passing them
|
# Tag filter. The UI stacks multiple tag pills by passing them
|
||||||
# comma-separated — each tag adds a separate AND-filter so the
|
# comma-separated — each tag adds a separate AND-filter so the
|
||||||
@@ -1203,9 +1224,10 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
for one in (t.strip() for t in tag.split(",")):
|
for one in (t.strip() for t in tag.split(",")):
|
||||||
if not one:
|
if not one:
|
||||||
continue
|
continue
|
||||||
|
safe_tag = f"%{escape_like_pattern(one)}%"
|
||||||
q = q.filter(_or(
|
q = q.filter(_or(
|
||||||
GalleryImage.tags.ilike(f"%{one}%"),
|
GalleryImage.tags.ilike(safe_tag, escape="\\"),
|
||||||
GalleryImage.ai_tags.ilike(f"%{one}%"),
|
GalleryImage.ai_tags.ilike(safe_tag, escape="\\"),
|
||||||
))
|
))
|
||||||
|
|
||||||
# Model filter
|
# Model filter
|
||||||
@@ -2433,11 +2455,10 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
image_b64 = body.get("image")
|
image_b64 = body.get("image")
|
||||||
amount = body.get("amount", 50) / 100.0
|
amount = body.get("amount", 50) / 100.0
|
||||||
|
|
||||||
from PIL import Image, ImageFilter
|
from PIL import ImageFilter
|
||||||
import base64, io
|
import base64, io
|
||||||
|
|
||||||
img_bytes = _b64_bytes(image_b64)
|
img = _b64_to_pil_image(image_b64, mode="RGB")
|
||||||
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
||||||
|
|
||||||
# Unsharp mask: radius=2, percent=amount*200, threshold=3
|
# Unsharp mask: radius=2, percent=amount*200, threshold=3
|
||||||
sharpened = img.filter(ImageFilter.UnsharpMask(radius=2, percent=int(amount * 200), threshold=3))
|
sharpened = img.filter(ImageFilter.UnsharpMask(radius=2, percent=int(amount * 200), threshold=3))
|
||||||
@@ -2472,8 +2493,7 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
raise HTTPException(500, "Server missing a required dependency")
|
raise HTTPException(500, "Server missing a required dependency")
|
||||||
# Decode source image (RGB; Real-ESRGAN doesn't preserve alpha).
|
# Decode source image (RGB; Real-ESRGAN doesn't preserve alpha).
|
||||||
img_bytes = _b64_bytes(image_b64)
|
src = _b64_to_pil_image(image_b64, mode="RGB")
|
||||||
src = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
||||||
try:
|
try:
|
||||||
patch_realesrgan_torchvision_compat()
|
patch_realesrgan_torchvision_compat()
|
||||||
from realesrgan import RealESRGANer
|
from realesrgan import RealESRGANer
|
||||||
@@ -2525,8 +2545,7 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise HTTPException(500, "Server missing a required dependency")
|
raise HTTPException(500, "Server missing a required dependency")
|
||||||
img_bytes = _b64_bytes(image_b64)
|
src = _b64_to_pil_image(image_b64, mode="RGB")
|
||||||
src = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
||||||
try:
|
try:
|
||||||
patch_realesrgan_torchvision_compat()
|
patch_realesrgan_torchvision_compat()
|
||||||
from basicsr.archs.rrdbnet_arch import RRDBNet
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
||||||
@@ -2778,8 +2797,7 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
from PIL import Image
|
from PIL import Image
|
||||||
import base64, io
|
import base64, io
|
||||||
|
|
||||||
img_bytes = _b64_bytes(image_b64)
|
img = _b64_to_pil_image(image_b64)
|
||||||
img = Image.open(io.BytesIO(img_bytes)).convert("RGBA")
|
|
||||||
W, H = img.size
|
W, H = img.size
|
||||||
|
|
||||||
hint = None
|
hint = None
|
||||||
@@ -2868,8 +2886,7 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
from PIL import Image, ImageFilter, ImageEnhance
|
from PIL import Image, ImageFilter, ImageEnhance
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
img_bytes = _b64_bytes(image_b64)
|
img = _b64_to_pil_image(image_b64, mode="RGB")
|
||||||
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
||||||
|
|
||||||
# Try GFPGAN first (AI face restoration)
|
# Try GFPGAN first (AI face restoration)
|
||||||
try:
|
try:
|
||||||
@@ -2944,14 +2961,22 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
@router.put("/api/gallery/albums/{album_id}")
|
@router.put("/api/gallery/albums/{album_id}")
|
||||||
async def update_album(request: Request, album_id: str):
|
async def update_album(request: Request, album_id: str):
|
||||||
user = get_current_user(request)
|
user = get_current_user(request)
|
||||||
data = await request.json()
|
data = await _json_object(request)
|
||||||
|
name = data.get("name")
|
||||||
|
if name is not None:
|
||||||
|
if not isinstance(name, str) or not name.strip():
|
||||||
|
raise HTTPException(400, "Album name required")
|
||||||
|
name = name.strip()
|
||||||
|
description = data.get("description")
|
||||||
|
if description is not None and not isinstance(description, str):
|
||||||
|
raise HTTPException(400, "Album description must be a string")
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
album = _get_or_404_album(db, album_id, user)
|
album = _get_or_404_album(db, album_id, user)
|
||||||
if data.get("name") is not None:
|
if name is not None:
|
||||||
album.name = data["name"]
|
album.name = name
|
||||||
if data.get("description") is not None:
|
if description is not None:
|
||||||
album.description = data["description"]
|
album.description = description
|
||||||
if data.get("cover_id") is not None:
|
if data.get("cover_id") is not None:
|
||||||
cover_id = data["cover_id"] or None
|
cover_id = data["cover_id"] or None
|
||||||
if cover_id:
|
if cover_id:
|
||||||
@@ -2981,8 +3006,7 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
@router.post("/api/gallery/albums/{album_id}/add")
|
@router.post("/api/gallery/albums/{album_id}/add")
|
||||||
async def add_to_album(request: Request, album_id: str):
|
async def add_to_album(request: Request, album_id: str):
|
||||||
user = get_current_user(request)
|
user = get_current_user(request)
|
||||||
data = await request.json()
|
ids = await _image_ids(request)
|
||||||
ids = data.get("image_ids", [])
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
_get_or_404_album(db, album_id, user)
|
_get_or_404_album(db, album_id, user)
|
||||||
@@ -2999,8 +3023,7 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
@router.post("/api/gallery/albums/{album_id}/remove")
|
@router.post("/api/gallery/albums/{album_id}/remove")
|
||||||
async def remove_from_album(request: Request, album_id: str):
|
async def remove_from_album(request: Request, album_id: str):
|
||||||
user = get_current_user(request)
|
user = get_current_user(request)
|
||||||
data = await request.json()
|
ids = await _image_ids(request)
|
||||||
ids = data.get("image_ids", [])
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
_get_or_404_album(db, album_id, user)
|
_get_or_404_album(db, album_id, user)
|
||||||
|
|||||||
@@ -659,7 +659,7 @@ _NON_CHAT_CONTAINS = (
|
|||||||
"gliner", "riva", "-parse", "-embedqa", "-nemoretriever",
|
"gliner", "riva", "-parse", "-embedqa", "-nemoretriever",
|
||||||
"topic-control", "calibration",
|
"topic-control", "calibration",
|
||||||
"ai-synthetic-video", "cosmos-reason2",
|
"ai-synthetic-video", "cosmos-reason2",
|
||||||
"bge", "llama-guard",
|
"bge", "llama-guard", "-embed", "rerank",
|
||||||
)
|
)
|
||||||
_NON_CHAT_EXACT_PREFIXES = (
|
_NON_CHAT_EXACT_PREFIXES = (
|
||||||
"gpt-audio", # gpt-audio, gpt-audio-mini etc. (not gpt-4o-audio-preview which is chat)
|
"gpt-audio", # gpt-audio, gpt-audio-mini etc. (not gpt-4o-audio-preview which is chat)
|
||||||
@@ -2846,11 +2846,12 @@ def setup_model_routes(model_discovery):
|
|||||||
return {"endpoint_id": "", "endpoint_url": "", "model": ""}
|
return {"endpoint_id": "", "endpoint_url": "", "model": ""}
|
||||||
base = _normalize_base(ep.base_url)
|
base = _normalize_base(ep.base_url)
|
||||||
chat_url = build_chat_url(base)
|
chat_url = build_chat_url(base)
|
||||||
|
if model and not _is_chat_model(model):
|
||||||
|
model = ""
|
||||||
if not model and (getattr(ep, "cached_models", None) or getattr(ep, "pinned_models", None)):
|
if not model and (getattr(ep, "cached_models", None) or getattr(ep, "pinned_models", None)):
|
||||||
try:
|
try:
|
||||||
visible = _visible_models(ep.cached_models, getattr(ep, "hidden_models", None), getattr(ep, "pinned_models", None))
|
visible = _visible_models(ep.cached_models, getattr(ep, "hidden_models", None), getattr(ep, "pinned_models", None))
|
||||||
if visible:
|
model = next((mid for mid in visible if _is_chat_model(mid)), "")
|
||||||
model = visible[0]
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
return {"endpoint_id": ep.id, "endpoint_url": chat_url, "model": model}
|
return {"endpoint_id": ep.id, "endpoint_url": chat_url, "model": model}
|
||||||
|
|||||||
@@ -728,32 +728,35 @@ def setup_note_routes(task_scheduler=None, upload_handler=None):
|
|||||||
)
|
)
|
||||||
if body.title is not None:
|
if body.title is not None:
|
||||||
note.title = body.title
|
note.title = body.title
|
||||||
if body.content is not None:
|
# Omitted fields preserve their values; explicit null clears the
|
||||||
|
# nullable fields used by the editor's removal controls.
|
||||||
|
supplied = body.model_fields_set
|
||||||
|
if "content" in supplied:
|
||||||
note.content = body.content
|
note.content = body.content
|
||||||
if body.items is not None:
|
if "items" in supplied:
|
||||||
note.items = json.dumps(body.items)
|
note.items = json.dumps(body.items) if body.items is not None else None
|
||||||
flag_modified(note, "items")
|
flag_modified(note, "items")
|
||||||
if body.note_type is not None:
|
if body.note_type is not None:
|
||||||
note.note_type = body.note_type
|
note.note_type = body.note_type
|
||||||
if body.color is not None:
|
if "color" in supplied:
|
||||||
note.color = body.color
|
note.color = body.color
|
||||||
if body.label is not None:
|
if "label" in supplied:
|
||||||
note.label = body.label
|
note.label = body.label
|
||||||
if body.pinned is not None:
|
if body.pinned is not None:
|
||||||
note.pinned = body.pinned
|
note.pinned = body.pinned
|
||||||
if body.archived is not None:
|
if body.archived is not None:
|
||||||
note.archived = body.archived
|
note.archived = body.archived
|
||||||
if body.due_date is not None:
|
if "due_date" in supplied:
|
||||||
note.due_date = body.due_date
|
note.due_date = body.due_date
|
||||||
if body.image_url is not None:
|
if "image_url" in supplied:
|
||||||
note.image_url = body.image_url
|
note.image_url = body.image_url
|
||||||
if body.gallery_id is not None:
|
if "gallery_id" in supplied:
|
||||||
note.gallery_id = body.gallery_id
|
note.gallery_id = body.gallery_id
|
||||||
if body.repeat is not None:
|
if body.repeat is not None:
|
||||||
note.repeat = body.repeat
|
note.repeat = body.repeat
|
||||||
if body.sort_order is not None:
|
if body.sort_order is not None:
|
||||||
note.sort_order = body.sort_order
|
note.sort_order = body.sort_order
|
||||||
if body.agent_session_id is not None:
|
if "agent_session_id" in supplied:
|
||||||
note.agent_session_id = body.agent_session_id
|
note.agent_session_id = body.agent_session_id
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|||||||
@@ -183,6 +183,7 @@ def _resolve_endpoint_runtime(ep, owner=None, model: Optional[str] = None):
|
|||||||
OAuth tokens in ProviderAuthSession, so ep.api_key is intentionally empty.
|
OAuth tokens in ProviderAuthSession, so ep.api_key is intentionally empty.
|
||||||
"""
|
"""
|
||||||
from src.endpoint_resolver import (
|
from src.endpoint_resolver import (
|
||||||
|
_endpoint_enabled_models,
|
||||||
build_chat_url,
|
build_chat_url,
|
||||||
build_headers,
|
build_headers,
|
||||||
resolve_endpoint_runtime as resolve_model_endpoint_runtime,
|
resolve_endpoint_runtime as resolve_model_endpoint_runtime,
|
||||||
@@ -197,7 +198,7 @@ def _resolve_endpoint_runtime(ep, owner=None, model: Optional[str] = None):
|
|||||||
ep_model = (model or "").strip()
|
ep_model = (model or "").strip()
|
||||||
if not ep_model:
|
if not ep_model:
|
||||||
try:
|
try:
|
||||||
models = json.loads(ep.cached_models) if ep.cached_models else []
|
models = _endpoint_enabled_models(ep)
|
||||||
if models:
|
if models:
|
||||||
ep_model = _first_chat_model(models)
|
ep_model = _first_chat_model(models)
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -824,23 +825,13 @@ def setup_research_routes(research_handler, session_manager=None) -> APIRouter:
|
|||||||
if not ep_url or not ep_model:
|
if not ep_url or not ep_model:
|
||||||
# Last resort: this user's enabled endpoint, plus legacy shared rows.
|
# Last resort: this user's enabled endpoint, plus legacy shared rows.
|
||||||
from src.database import SessionLocal
|
from src.database import SessionLocal
|
||||||
from src.endpoint_resolver import normalize_base, build_chat_url, build_headers
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
ep = _owned_enabled_endpoint(db, user)
|
ep = _owned_enabled_endpoint(db, user)
|
||||||
if ep:
|
if ep:
|
||||||
base = normalize_base(ep.base_url)
|
resolved = _resolve_endpoint_runtime(ep, owner=user)
|
||||||
fallback_url = build_chat_url(base)
|
if resolved:
|
||||||
fallback_headers = build_headers(ep.api_key, base)
|
_merge(*resolved)
|
||||||
fallback_model = ""
|
|
||||||
if ep.cached_models:
|
|
||||||
try:
|
|
||||||
models = json.loads(ep.cached_models)
|
|
||||||
if models:
|
|
||||||
fallback_model = _first_chat_model(models)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
_merge(fallback_url, fallback_model, fallback_headers)
|
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|||||||
@@ -1315,7 +1315,7 @@ def setup_session_routes(
|
|||||||
def auto_sort_sessions(request: Request, skip_llm: bool = False):
|
def auto_sort_sessions(request: Request, skip_llm: bool = False):
|
||||||
"""Use AI to categorize all sessions into folders.
|
"""Use AI to categorize all sessions into folders.
|
||||||
|
|
||||||
Phase 1 deletes empty/throwaway sessions and Phase 2 asks the LLM
|
Phase 1 deletes empty/Incognito sessions and Phase 2 asks the LLM
|
||||||
to assign folders. When `skip_llm=true` the endpoint returns
|
to assign folders. When `skip_llm=true` the endpoint returns
|
||||||
after Phase 1 — used by the "Tidy (no AI)" UI affordance so
|
after Phase 1 — used by the "Tidy (no AI)" UI affordance so
|
||||||
users can clean junk without spending tokens.
|
users can clean junk without spending tokens.
|
||||||
@@ -1326,21 +1326,11 @@ def setup_session_routes(
|
|||||||
single_user_mode = not user and _auth_disabled()
|
single_user_mode = not user and _auth_disabled()
|
||||||
user_sessions = session_manager.get_sessions_for_user(user)
|
user_sessions = session_manager.get_sessions_for_user(user)
|
||||||
|
|
||||||
# Delete empty and throwaway sessions before sorting
|
# Saved conversations are never disposable based on their title or prompt.
|
||||||
from core.database import ChatMessage as DbMsg
|
from core.database import ChatMessage as DbMsg
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
deleted_empty = 0
|
deleted_empty = 0
|
||||||
deleted_throwaway = 0
|
deleted_throwaway = 0
|
||||||
# Names that indicate a throwaway/test session (case-insensitive exact or prefix match)
|
|
||||||
_THROWAWAY_NAMES = {
|
|
||||||
"test", "testing", "asdf", "asd", "hello", "hi", "hey",
|
|
||||||
"yo", "sup", "hola", "hii", "hiii", "heyo",
|
|
||||||
"foo", "bar", "baz", "tmp", "temp", "scratch", "untitled",
|
|
||||||
"new chat", "delete", "remove", "junk", "trash", "xxx",
|
|
||||||
"abc", "qwerty", "blah", "stuff", "whatever", "idk",
|
|
||||||
"ok", "lol", "bruh", "hmm", "hm", "meh",
|
|
||||||
}
|
|
||||||
_THROWAWAY_MAX_MESSAGES = 4 # only delete if <= this many messages
|
|
||||||
try:
|
try:
|
||||||
rows_q = db.query(DbSession).filter(DbSession.archived == False)
|
rows_q = db.query(DbSession).filter(DbSession.archived == False)
|
||||||
if user:
|
if user:
|
||||||
@@ -1349,15 +1339,11 @@ def setup_session_routes(
|
|||||||
rows_q = rows_q.filter(DbSession.owner == user)
|
rows_q = rows_q.filter(DbSession.owner == user)
|
||||||
rows = rows_q.limit(2000).all()
|
rows = rows_q.limit(2000).all()
|
||||||
folder_map = {r.id: r.folder for r in rows}
|
folder_map = {r.id: r.folder for r in rows}
|
||||||
# Precompute per-session message counts in TWO aggregate queries
|
# Precompute per-session message counts in one aggregate query
|
||||||
# instead of 1–3 queries PER session — with many chats the per-row
|
# instead of 1–3 queries PER session — with many chats the per-row
|
||||||
# loop was doing thousands of round-trips and blowing the timeout.
|
# loop was doing thousands of round-trips and blowing the timeout.
|
||||||
from sqlalchemy import func as _sa_func
|
from sqlalchemy import func as _sa_func
|
||||||
_counts = dict(db.query(DbMsg.session_id, _sa_func.count(DbMsg.id)).group_by(DbMsg.session_id).all())
|
_counts = dict(db.query(DbMsg.session_id, _sa_func.count(DbMsg.id)).group_by(DbMsg.session_id).all())
|
||||||
_asst_counts = dict(
|
|
||||||
db.query(DbMsg.session_id, _sa_func.count(DbMsg.id))
|
|
||||||
.filter(DbMsg.role == "assistant").group_by(DbMsg.session_id).all()
|
|
||||||
)
|
|
||||||
cleanup_now = utcnow_naive()
|
cleanup_now = utcnow_naive()
|
||||||
for row in rows:
|
for row in rows:
|
||||||
# Never delete important sessions
|
# Never delete important sessions
|
||||||
@@ -1367,9 +1353,10 @@ def setup_session_routes(
|
|||||||
if (row.name or "").strip() == "Incognito":
|
if (row.name or "").strip() == "Incognito":
|
||||||
should_delete = True
|
should_delete = True
|
||||||
deleted_throwaway += 1
|
deleted_throwaway += 1
|
||||||
db.delete(row)
|
|
||||||
if hasattr(session_manager, 'delete_session'):
|
if hasattr(session_manager, 'delete_session'):
|
||||||
session_manager.delete_session(row.id)
|
session_manager.delete_session(row.id)
|
||||||
|
else:
|
||||||
|
db.delete(row)
|
||||||
continue
|
continue
|
||||||
if is_session_recently_active(row, now=cleanup_now):
|
if is_session_recently_active(row, now=cleanup_now):
|
||||||
continue
|
continue
|
||||||
@@ -1378,31 +1365,11 @@ def setup_session_routes(
|
|||||||
if msg_count == 0:
|
if msg_count == 0:
|
||||||
should_delete = True
|
should_delete = True
|
||||||
deleted_empty += 1
|
deleted_empty += 1
|
||||||
elif msg_count <= _THROWAWAY_MAX_MESSAGES:
|
|
||||||
name = (row.name or "").strip().lower()
|
|
||||||
# Check first user message content (AI renames sessions, so
|
|
||||||
# "hi" becomes "Casual Greeting Exchange" — name alone won't match)
|
|
||||||
first_msg = db.query(DbMsg.content).filter(
|
|
||||||
DbMsg.session_id == row.id, DbMsg.role == "user"
|
|
||||||
).order_by(DbMsg.timestamp).first()
|
|
||||||
first_text = (first_msg[0] or "").strip().lower() if first_msg else ""
|
|
||||||
# Count assistant messages — if user sent something but AI never replied, it's dead
|
|
||||||
assistant_count = _asst_counts.get(row.id, 0)
|
|
||||||
if name in _THROWAWAY_NAMES or name.startswith("chat:") or first_text in _THROWAWAY_NAMES:
|
|
||||||
should_delete = True
|
|
||||||
deleted_throwaway += 1
|
|
||||||
# Single user message with no AI response = dead session
|
|
||||||
elif msg_count == 1 and assistant_count == 0:
|
|
||||||
should_delete = True
|
|
||||||
deleted_throwaway += 1
|
|
||||||
# Short phrase (1-3 words) with no real AI conversation (<=2 msgs)
|
|
||||||
elif msg_count <= 2 and first_text and len(first_text.split()) <= 3 and len(first_text) <= 40:
|
|
||||||
should_delete = True
|
|
||||||
deleted_throwaway += 1
|
|
||||||
if should_delete:
|
if should_delete:
|
||||||
db.delete(row)
|
|
||||||
if hasattr(session_manager, 'delete_session'):
|
if hasattr(session_manager, 'delete_session'):
|
||||||
session_manager.delete_session(row.id)
|
session_manager.delete_session(row.id)
|
||||||
|
else:
|
||||||
|
db.delete(row)
|
||||||
if deleted_empty or deleted_throwaway:
|
if deleted_empty or deleted_throwaway:
|
||||||
db.commit()
|
db.commit()
|
||||||
logger.info(f"Auto-sort: deleted {deleted_empty} empty + {deleted_throwaway} throwaway sessions")
|
logger.info(f"Auto-sort: deleted {deleted_empty} empty + {deleted_throwaway} throwaway sessions")
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import httpx
|
|||||||
from fastapi import APIRouter, HTTPException, Request
|
from fastapi import APIRouter, HTTPException, Request
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
from services.memory.skill_format import slugify
|
||||||
from services.memory.skills import SkillsManager
|
from services.memory.skills import SkillsManager
|
||||||
from src.auth_helpers import get_current_user
|
from src.auth_helpers import get_current_user
|
||||||
from services.skills import evaluation as _evaluation
|
from services.skills import evaluation as _evaluation
|
||||||
@@ -836,6 +837,10 @@ def setup_skills_routes(skills_manager: SkillsManager) -> APIRouter:
|
|||||||
updates = body.dict(exclude_none=True)
|
updates = body.dict(exclude_none=True)
|
||||||
if not updates:
|
if not updates:
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
|
if "name" in updates:
|
||||||
|
new_name = slugify(updates["name"] or match.get("name"))
|
||||||
|
if new_name != match.get("name") and skills_manager.has_skill(new_name, owner=user):
|
||||||
|
raise HTTPException(409, f"A skill named '{new_name}' already exists")
|
||||||
ok = skills_manager.update_skill(match.get("name"), updates, owner=user)
|
ok = skills_manager.update_skill(match.get("name"), updates, owner=user)
|
||||||
if not ok:
|
if not ok:
|
||||||
raise HTTPException(404, "Skill not found")
|
raise HTTPException(404, "Skill not found")
|
||||||
|
|||||||
+11
-17
@@ -20,7 +20,7 @@ from src.task_action_policy import (
|
|||||||
is_admin_only_task_action,
|
is_admin_only_task_action,
|
||||||
owner_has_admin_task_privileges,
|
owner_has_admin_task_privileges,
|
||||||
)
|
)
|
||||||
from src.task_scheduler import compute_next_run, HOUSEKEEPING_DEFAULTS
|
from src.task_scheduler import compute_next_run, is_valid_cron, HOUSEKEEPING_DEFAULTS
|
||||||
from routes.prefs_routes import _load_for_user, _save_for_user
|
from routes.prefs_routes import _load_for_user, _save_for_user
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -289,15 +289,15 @@ def _resolve_run_endpoint(db, task: ScheduledTask, run: TaskRun) -> str:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
from core.database import ModelEndpoint
|
from core.database import ModelEndpoint
|
||||||
eps = db.query(ModelEndpoint).filter(ModelEndpoint.is_enabled == True).all()
|
from src.auth_helpers import owner_filter
|
||||||
|
from src.endpoint_resolver import _endpoint_enabled_models
|
||||||
|
|
||||||
|
eps = owner_filter(
|
||||||
|
db.query(ModelEndpoint).filter(ModelEndpoint.is_enabled == True),
|
||||||
|
ModelEndpoint, getattr(task, "owner", None),
|
||||||
|
).all()
|
||||||
for ep in eps:
|
for ep in eps:
|
||||||
cached = []
|
if model in _endpoint_enabled_models(ep):
|
||||||
if ep.cached_models:
|
|
||||||
try:
|
|
||||||
cached = json.loads(ep.cached_models) or []
|
|
||||||
except Exception:
|
|
||||||
cached = []
|
|
||||||
if model in cached:
|
|
||||||
return ep.base_url or ""
|
return ep.base_url or ""
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -475,10 +475,7 @@ def setup_task_routes(task_scheduler) -> APIRouter:
|
|||||||
if req.trigger_type == "schedule" and req.schedule == "cron" and not req.cron_expression:
|
if req.trigger_type == "schedule" and req.schedule == "cron" and not req.cron_expression:
|
||||||
raise HTTPException(400, "Cron expression is required for cron schedule")
|
raise HTTPException(400, "Cron expression is required for cron schedule")
|
||||||
if req.trigger_type == "schedule" and req.schedule == "cron" and req.cron_expression:
|
if req.trigger_type == "schedule" and req.schedule == "cron" and req.cron_expression:
|
||||||
try:
|
if not is_valid_cron(req.cron_expression):
|
||||||
from croniter import croniter
|
|
||||||
croniter(req.cron_expression)
|
|
||||||
except Exception:
|
|
||||||
raise HTTPException(400, "Invalid cron expression")
|
raise HTTPException(400, "Invalid cron expression")
|
||||||
if req.trigger_type == "event" and not req.trigger_event:
|
if req.trigger_type == "event" and not req.trigger_event:
|
||||||
raise HTTPException(400, "Event name is required for event-triggered tasks")
|
raise HTTPException(400, "Event name is required for event-triggered tasks")
|
||||||
@@ -788,10 +785,7 @@ def setup_task_routes(task_scheduler) -> APIRouter:
|
|||||||
task.character_id = req.character_id or None
|
task.character_id = req.character_id or None
|
||||||
if req.cron_expression is not None:
|
if req.cron_expression is not None:
|
||||||
if req.cron_expression:
|
if req.cron_expression:
|
||||||
try:
|
if not is_valid_cron(req.cron_expression):
|
||||||
from croniter import croniter
|
|
||||||
croniter(req.cron_expression)
|
|
||||||
except Exception:
|
|
||||||
raise HTTPException(400, "Invalid cron expression")
|
raise HTTPException(400, "Invalid cron expression")
|
||||||
task.cron_expression = req.cron_expression or None
|
task.cron_expression = req.cron_expression or None
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ that are awkward at the shell). Filters images by tag, prompt, favorite,
|
|||||||
album, and surfaces EXIF metadata.
|
album, and surfaces EXIF metadata.
|
||||||
|
|
||||||
odysseus-gallery list --limit 20 --favorites
|
odysseus-gallery list --limit 20 --favorites
|
||||||
|
odysseus-gallery list --tag "sunset,beach" # all tags, manual or AI
|
||||||
odysseus-gallery show IMAGE_ID
|
odysseus-gallery show IMAGE_ID
|
||||||
odysseus-gallery albums | jq -r '.[] | .name'
|
odysseus-gallery albums | jq -r '.[] | .name'
|
||||||
odysseus-gallery search "sunset" # matches prompt + tags
|
odysseus-gallery search "sunset" # matches prompt + tags
|
||||||
@@ -83,7 +84,13 @@ def cmd_list(args):
|
|||||||
fail(f"no album named {args.album!r}")
|
fail(f"no album named {args.album!r}")
|
||||||
q = q.filter(GalleryImage.album_id == al.id)
|
q = q.filter(GalleryImage.album_id == al.id)
|
||||||
if args.tag:
|
if args.tag:
|
||||||
q = q.filter(GalleryImage.tags.ilike(f"%{args.tag}%"))
|
for tag in (value.strip() for value in args.tag.split(",")):
|
||||||
|
if not tag:
|
||||||
|
continue
|
||||||
|
q = q.filter(
|
||||||
|
GalleryImage.tags.ilike(f"%{tag}%") |
|
||||||
|
GalleryImage.ai_tags.ilike(f"%{tag}%")
|
||||||
|
)
|
||||||
q = q.order_by(GalleryImage.created_at.desc()).limit(args.limit)
|
q = q.order_by(GalleryImage.created_at.desc()).limit(args.limit)
|
||||||
emit([_serialize_image(i) for i in q.all()], args)
|
emit([_serialize_image(i) for i in q.all()], args)
|
||||||
finally:
|
finally:
|
||||||
@@ -159,7 +166,7 @@ def _build_parser():
|
|||||||
pl.add_argument("--limit", type=int, default=50)
|
pl.add_argument("--limit", type=int, default=50)
|
||||||
pl.add_argument("--favorites", action="store_true")
|
pl.add_argument("--favorites", action="store_true")
|
||||||
pl.add_argument("--album", help="filter by album name")
|
pl.add_argument("--album", help="filter by album name")
|
||||||
pl.add_argument("--tag", help="substring match against tags column")
|
pl.add_argument("--tag", help="comma-separated tags; each matches manual or AI tags by substring")
|
||||||
pl.set_defaults(func=cmd_list)
|
pl.set_defaults(func=cmd_list)
|
||||||
|
|
||||||
psh = sub.add_parser("show", parents=[common])
|
psh = sub.add_parser("show", parents=[common])
|
||||||
|
|||||||
+12
-6
@@ -43,7 +43,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from datetime import datetime
|
from datetime import datetime, timezone
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# Anchor the path so the script works when invoked from anywhere.
|
# Anchor the path so the script works when invoked from anywhere.
|
||||||
@@ -136,7 +136,7 @@ def cmd_list(args) -> None:
|
|||||||
emit([], args)
|
emit([], args)
|
||||||
return
|
return
|
||||||
all_uids = data[0].split()
|
all_uids = data[0].split()
|
||||||
# Newest first, limit
|
# Keep a bounded arrival batch; order its decoded rows by Date below.
|
||||||
uids = list(reversed(all_uids))[: args.limit]
|
uids = list(reversed(all_uids))[: args.limit]
|
||||||
out = []
|
out = []
|
||||||
for uid in uids:
|
for uid in uids:
|
||||||
@@ -165,10 +165,15 @@ def cmd_list(args) -> None:
|
|||||||
sender_name, sender_addr = parseaddr(sender)
|
sender_name, sender_addr = parseaddr(sender)
|
||||||
date_raw = msg.get("Date", "")
|
date_raw = msg.get("Date", "")
|
||||||
try:
|
try:
|
||||||
iso = parsedate_to_datetime(date_raw).isoformat() if date_raw else ""
|
parsed_date = parsedate_to_datetime(date_raw) if date_raw else None
|
||||||
|
iso = parsed_date.isoformat() if parsed_date else ""
|
||||||
|
if parsed_date is not None and parsed_date.tzinfo is None:
|
||||||
|
parsed_date = parsed_date.replace(tzinfo=timezone.utc)
|
||||||
|
date_epoch = parsed_date.timestamp() if parsed_date else float("-inf")
|
||||||
except Exception:
|
except Exception:
|
||||||
iso = ""
|
iso = ""
|
||||||
out.append({
|
date_epoch = float("-inf")
|
||||||
|
out.append((date_epoch, {
|
||||||
"uid": uid.decode(),
|
"uid": uid.decode(),
|
||||||
"date": iso,
|
"date": iso,
|
||||||
"from_addr": sender_addr,
|
"from_addr": sender_addr,
|
||||||
@@ -176,10 +181,11 @@ def cmd_list(args) -> None:
|
|||||||
"subject": subject,
|
"subject": subject,
|
||||||
"is_read": "\\Seen" in flags,
|
"is_read": "\\Seen" in flags,
|
||||||
"is_answered": "\\Answered" in flags,
|
"is_answered": "\\Answered" in flags,
|
||||||
})
|
}))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sys.stderr.write(f"warn: skipping uid {uid!r}: {e}\n")
|
sys.stderr.write(f"warn: skipping uid {uid!r}: {e}\n")
|
||||||
emit(out, args)
|
out.sort(key=lambda item: item[0], reverse=True)
|
||||||
|
emit([row for _, row in out], args)
|
||||||
|
|
||||||
|
|
||||||
# ─── read ────────────────────────────────────────────────────────────
|
# ─── read ────────────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -11,6 +11,38 @@ import time
|
|||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
if str(ROOT) not in sys.path:
|
||||||
|
sys.path.insert(0, str(ROOT))
|
||||||
|
|
||||||
|
|
||||||
|
def _stop_owned_server(server, identity):
|
||||||
|
"""Close only identity-bound descendants, including MCP's new sessions.
|
||||||
|
|
||||||
|
Capture before signalling the parent: an orphaned stderr holder can no
|
||||||
|
longer be attributed by ancestry. Never search by executable name or kill
|
||||||
|
the caller's process group.
|
||||||
|
"""
|
||||||
|
from src import process_lifecycle as lifecycle
|
||||||
|
|
||||||
|
descendants = (lifecycle.bind_descendants([identity.pid])
|
||||||
|
if identity.verdict() == lifecycle.OWNED else [])
|
||||||
|
if os.name == "nt":
|
||||||
|
if identity.verdict() == lifecycle.OWNED:
|
||||||
|
lifecycle.taskkill_tree(identity.pid)
|
||||||
|
else:
|
||||||
|
outcome = lifecycle.terminate_identities(
|
||||||
|
[*(entry.identity for entry in reversed(descendants)), identity],
|
||||||
|
steps=lifecycle.term_kill_steps(10),
|
||||||
|
)
|
||||||
|
if outcome.survivors or outcome.unverified:
|
||||||
|
raise RuntimeError(f"Disposable Notes cleanup could not verify exit: {outcome}")
|
||||||
|
try:
|
||||||
|
logs, _ = server.communicate(timeout=2)
|
||||||
|
except subprocess.TimeoutExpired as error:
|
||||||
|
# Do not turn an unidentified pipe holder into an unbounded wait.
|
||||||
|
server.stdout.close()
|
||||||
|
raise RuntimeError("Disposable Notes output pipe remained open after owned cleanup") from error
|
||||||
|
return logs
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -36,7 +68,10 @@ def main():
|
|||||||
server = subprocess.Popen(
|
server = subprocess.Popen(
|
||||||
[sys.executable, "-m", "uvicorn", "app:app", "--host", "127.0.0.1", "--port", str(port)],
|
[sys.executable, "-m", "uvicorn", "app:app", "--host", "127.0.0.1", "--port", str(port)],
|
||||||
cwd=target, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
|
cwd=target, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
|
||||||
|
start_new_session=(os.name != "nt"),
|
||||||
)
|
)
|
||||||
|
from src.process_lifecycle import ProcessIdentity
|
||||||
|
server_identity = ProcessIdentity.capture(server.pid)
|
||||||
result = None
|
result = None
|
||||||
try:
|
try:
|
||||||
origin = f"http://127.0.0.1:{port}"
|
origin = f"http://127.0.0.1:{port}"
|
||||||
@@ -62,12 +97,7 @@ def main():
|
|||||||
print(result.stderr, file=sys.stderr)
|
print(result.stderr, file=sys.stderr)
|
||||||
return result.returncode
|
return result.returncode
|
||||||
finally:
|
finally:
|
||||||
server.terminate()
|
logs = _stop_owned_server(server, server_identity)
|
||||||
try:
|
|
||||||
logs, _ = server.communicate(timeout=10)
|
|
||||||
except subprocess.TimeoutExpired:
|
|
||||||
server.kill()
|
|
||||||
logs, _ = server.communicate()
|
|
||||||
if result is None or result.returncode:
|
if result is None or result.returncode:
|
||||||
print(logs[-8000:], file=sys.stderr)
|
print(logs[-8000:], file=sys.stderr)
|
||||||
|
|
||||||
|
|||||||
@@ -202,17 +202,35 @@ def _infer_use_case(repo_id, pipeline_tag):
|
|||||||
return "general"
|
return "general"
|
||||||
|
|
||||||
|
|
||||||
|
# HF collections mix in models no Cookbook LLM backend can serve (vision
|
||||||
|
# encoders, time-series forecasters, image/video generators). Image models
|
||||||
|
# have their own registry, so keep these out of the LLM catalog.
|
||||||
|
_NON_LLM_PIPELINE_TAGS = frozenset({
|
||||||
|
"time-series-forecasting",
|
||||||
|
"zero-shot-image-classification",
|
||||||
|
"image-classification",
|
||||||
|
"image-feature-extraction",
|
||||||
|
"text-to-image",
|
||||||
|
"image-to-video",
|
||||||
|
"text-to-video",
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def _entry_from_collection_item(collection, item, source):
|
def _entry_from_collection_item(collection, item, source):
|
||||||
repo_id = item.get("id") or ""
|
repo_id = item.get("id") or ""
|
||||||
if item.get("type") != "model" or not repo_id:
|
if item.get("type") != "model" or not repo_id:
|
||||||
return None
|
return None
|
||||||
|
if (item.get("pipeline_tag") or "").strip().lower() in _NON_LLM_PIPELINE_TAGS:
|
||||||
|
return None
|
||||||
repo_prefix = source.get("repo_prefix")
|
repo_prefix = source.get("repo_prefix")
|
||||||
if repo_prefix and not repo_id.startswith(repo_prefix):
|
if repo_prefix and not repo_id.startswith(repo_prefix):
|
||||||
return None
|
return None
|
||||||
raw_params = item.get("numParameters") or 0
|
raw_params = item.get("numParameters") or 0
|
||||||
active = None
|
# Read the name even when HF gives a total: it is the only source of the
|
||||||
|
# MoE active size (e.g. "-35B-A3B"), which the speed estimate depends on.
|
||||||
|
name_params, active = _parse_params_from_name(repo_id)
|
||||||
if not raw_params:
|
if not raw_params:
|
||||||
raw_params, active = _parse_params_from_name(repo_id)
|
raw_params = name_params
|
||||||
param_label, raw_params = _format_params(raw_params)
|
param_label, raw_params = _format_params(raw_params)
|
||||||
if not raw_params:
|
if not raw_params:
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -84,10 +84,14 @@ _FM_KEY_RE = re.compile(r"^([a-z_][a-z0-9_]*):\s*(.*)$", re.IGNORECASE)
|
|||||||
_FM_BLOCK_LIST_RE = re.compile(r"^\s*-\s*(.*)$")
|
_FM_BLOCK_LIST_RE = re.compile(r"^\s*-\s*(.*)$")
|
||||||
|
|
||||||
|
|
||||||
def _parse_scalar(raw: str) -> Any:
|
def _parse_scalar(raw: str, *, string_only: bool = False) -> Any:
|
||||||
raw = raw.strip()
|
raw = raw.strip()
|
||||||
if raw == "":
|
if raw == "":
|
||||||
return ""
|
return ""
|
||||||
|
# String fields keep their spelling; quoted values still use the decoder
|
||||||
|
# below so existing escapes round-trip as before.
|
||||||
|
if string_only and not (raw[0] == raw[-1] and raw[0] in ("'", '"')):
|
||||||
|
return raw
|
||||||
if raw.startswith("[") and raw.endswith("]"):
|
if raw.startswith("[") and raw.endswith("]"):
|
||||||
inner = raw[1:-1].strip()
|
inner = raw[1:-1].strip()
|
||||||
if not inner:
|
if not inner:
|
||||||
@@ -126,10 +130,15 @@ def _parse_scalar(raw: str) -> Any:
|
|||||||
def _split_top_level(s: str, sep: str) -> List[str]:
|
def _split_top_level(s: str, sep: str) -> List[str]:
|
||||||
"""Split `s` on `sep` ignoring separators inside [] or quotes."""
|
"""Split `s` on `sep` ignoring separators inside [] or quotes."""
|
||||||
out, buf, depth, quote = [], [], 0, None
|
out, buf, depth, quote = [], [], 0, None
|
||||||
|
escaped = False
|
||||||
for ch in s:
|
for ch in s:
|
||||||
if quote:
|
if quote:
|
||||||
buf.append(ch)
|
buf.append(ch)
|
||||||
if ch == quote:
|
if escaped:
|
||||||
|
escaped = False
|
||||||
|
elif ch == "\\" and quote == '"':
|
||||||
|
escaped = True
|
||||||
|
elif ch == quote:
|
||||||
quote = None
|
quote = None
|
||||||
continue
|
continue
|
||||||
if ch in ("'", '"'):
|
if ch in ("'", '"'):
|
||||||
@@ -171,7 +180,7 @@ def parse_frontmatter(text: str) -> tuple[Dict[str, Any], str]:
|
|||||||
pending_key = key
|
pending_key = key
|
||||||
fm[key] = []
|
fm[key] = []
|
||||||
else:
|
else:
|
||||||
fm[key] = _parse_scalar(val)
|
fm[key] = _parse_scalar(val, string_only=(key == "version"))
|
||||||
pending_key = None
|
pending_key = None
|
||||||
continue
|
continue
|
||||||
m2 = _FM_BLOCK_LIST_RE.match(line)
|
m2 = _FM_BLOCK_LIST_RE.match(line)
|
||||||
|
|||||||
@@ -500,6 +500,19 @@ class SkillsManager:
|
|||||||
sk.path = self._skill_file(cat, nm)
|
sk.path = self._skill_file(cat, nm)
|
||||||
return sk.to_dict()
|
return sk.to_dict()
|
||||||
|
|
||||||
|
def has_skill(self, name: str, owner: Optional[str] = None) -> bool:
|
||||||
|
"""Whether `owner` already has a skill called `name`, in any category.
|
||||||
|
|
||||||
|
Update and delete find a skill by name within its owner, so two of
|
||||||
|
one owner's skills sharing a name would make them act on whichever
|
||||||
|
file is walked first.
|
||||||
|
"""
|
||||||
|
for path in self._iter_skill_files():
|
||||||
|
sk = self._read_skill(path)
|
||||||
|
if sk and sk.name == name and (sk.owner or "") == (owner or ""):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def update_skill(self, skill_id: str, updates: Dict, owner: Optional[str] = None) -> bool:
|
def update_skill(self, skill_id: str, updates: Dict, owner: Optional[str] = None) -> bool:
|
||||||
"""`skill_id` is the slug name. Allows updating any field plus
|
"""`skill_id` is the slug name. Allows updating any field plus
|
||||||
renames if `name` changes (file is moved on disk).
|
renames if `name` changes (file is moved on disk).
|
||||||
@@ -551,6 +564,9 @@ class SkillsManager:
|
|||||||
# Rename
|
# Rename
|
||||||
new_name = slugify(updates.get("name") or sk.name)
|
new_name = slugify(updates.get("name") or sk.name)
|
||||||
if new_name != sk.name:
|
if new_name != sk.name:
|
||||||
|
if self.has_skill(new_name, owner=owner):
|
||||||
|
logger.warning(f"Skill rename target name in use: {new_name}")
|
||||||
|
return False
|
||||||
sk.name = new_name
|
sk.name = new_name
|
||||||
|
|
||||||
# Write to potentially new path
|
# Write to potentially new path
|
||||||
|
|||||||
+23
-11
@@ -24193,11 +24193,14 @@ async def stream_agent_loop(
|
|||||||
budget_is_explicit,
|
budget_is_explicit,
|
||||||
hard_max=hard_max,
|
hard_max=hard_max,
|
||||||
)
|
)
|
||||||
if candidate_context <= 8192:
|
if 0 < candidate_context <= 8192:
|
||||||
# Small local servers often tokenize chat wrappers and tool
|
# Small local servers often tokenize chat wrappers and tool
|
||||||
# results much more generously than our rough estimator. Keep
|
# results much more generously than our rough estimator. Keep
|
||||||
# substantial headroom for those wrappers and generation so a
|
# substantial headroom for those wrappers and generation so a
|
||||||
# follow-up tool round cannot exceed the server's n_ctx.
|
# follow-up tool round cannot exceed the server's n_ctx.
|
||||||
|
# 0 means the window is unknown, which already gets the
|
||||||
|
# conservative default budget; clamping it as well cut every
|
||||||
|
# request to 1200 tokens and dropped open documents (#6678).
|
||||||
effective_budget = min(
|
effective_budget = min(
|
||||||
effective_budget,
|
effective_budget,
|
||||||
max(1200, int(candidate_context * 0.40)),
|
max(1200, int(candidate_context * 0.40)),
|
||||||
@@ -27833,16 +27836,25 @@ async def stream_agent_loop(
|
|||||||
if _streamed_tool_markup or _streamed_tool_markup_starts(_delta_text):
|
if _streamed_tool_markup or _streamed_tool_markup_starts(_delta_text):
|
||||||
_streamed_tool_markup += _delta_text
|
_streamed_tool_markup += _delta_text
|
||||||
if not _streamed_tool_markup_complete(_streamed_tool_markup):
|
if not _streamed_tool_markup_complete(_streamed_tool_markup):
|
||||||
continue
|
if _streamed_tool_markup_starts(_streamed_tool_markup):
|
||||||
_visible_markup_tail = strip_tool_blocks(
|
continue
|
||||||
_streamed_tool_markup,
|
# False alarm: a bare "<" (e.g. ``x < 2``) looked like
|
||||||
skip_fenced=True,
|
# a tag prefix, but the buffer can no longer become a
|
||||||
).strip()
|
# tool tag. Release it as prose instead of holding the
|
||||||
_streamed_tool_markup = ""
|
# rest of the answer forever.
|
||||||
if not _visible_markup_tail:
|
_delta_text = _streamed_tool_markup
|
||||||
continue
|
_streamed_tool_markup = ""
|
||||||
_delta_text = _visible_markup_tail
|
data["delta"] = _delta_text
|
||||||
data["delta"] = _delta_text
|
else:
|
||||||
|
_visible_markup_tail = strip_tool_blocks(
|
||||||
|
_streamed_tool_markup,
|
||||||
|
skip_fenced=True,
|
||||||
|
).strip()
|
||||||
|
_streamed_tool_markup = ""
|
||||||
|
if not _visible_markup_tail:
|
||||||
|
continue
|
||||||
|
_delta_text = _visible_markup_tail
|
||||||
|
data["delta"] = _delta_text
|
||||||
full_response += _delta_text
|
full_response += _delta_text
|
||||||
if not _suppress_unavailable_web_delta:
|
if not _suppress_unavailable_web_delta:
|
||||||
_qwen_buffered_route = bool(
|
_qwen_buffered_route = bool(
|
||||||
|
|||||||
@@ -1207,7 +1207,7 @@ async def do_generate_image(content: str, session_id: Optional[str] = None, owne
|
|||||||
Line 1: prompt describing the image
|
Line 1: prompt describing the image
|
||||||
Line 2: model name (optional, default auto-detects: prefers gpt-image-1.5 > gpt-image-1)
|
Line 2: model name (optional, default auto-detects: prefers gpt-image-1.5 > gpt-image-1)
|
||||||
Line 3: size (optional, defaults to 1024x1024)
|
Line 3: size (optional, defaults to 1024x1024)
|
||||||
Line 4: quality (optional, defaults to medium — options: low, medium, high, auto)
|
Line 4: quality (optional, defaults to configured quality or medium — low, medium, high, auto)
|
||||||
"""
|
"""
|
||||||
import base64
|
import base64
|
||||||
import httpx
|
import httpx
|
||||||
@@ -1225,13 +1225,13 @@ async def do_generate_image(content: str, session_id: Optional[str] = None, owne
|
|||||||
prompt = str(args.get('prompt') or '').strip()
|
prompt = str(args.get('prompt') or '').strip()
|
||||||
model_spec = str(args.get('model') or '').strip()
|
model_spec = str(args.get('model') or '').strip()
|
||||||
size = str(args.get('size') or '1024x1024')
|
size = str(args.get('size') or '1024x1024')
|
||||||
quality = str(args.get('quality') or 'medium')
|
quality = str(args.get('quality') or '')
|
||||||
else:
|
else:
|
||||||
lines = content.strip().split("\n")
|
lines = content.strip().split("\n")
|
||||||
prompt = lines[0].strip() if lines else ""
|
prompt = lines[0].strip() if lines else ""
|
||||||
model_spec = lines[1].strip() if len(lines) > 1 and lines[1].strip() else ""
|
model_spec = lines[1].strip() if len(lines) > 1 and lines[1].strip() else ""
|
||||||
size = lines[2].strip() if len(lines) > 2 and lines[2].strip() else "1024x1024"
|
size = lines[2].strip() if len(lines) > 2 and lines[2].strip() else "1024x1024"
|
||||||
quality = lines[3].strip() if len(lines) > 3 and lines[3].strip() else "medium"
|
quality = lines[3].strip() if len(lines) > 3 and lines[3].strip() else ""
|
||||||
|
|
||||||
if not prompt:
|
if not prompt:
|
||||||
return {"error": "Image prompt is required (line 1)"}
|
return {"error": "Image prompt is required (line 1)"}
|
||||||
@@ -1249,8 +1249,8 @@ async def do_generate_image(content: str, session_id: Optional[str] = None, owne
|
|||||||
# Use admin-configured model/quality if not specified by the tool call
|
# Use admin-configured model/quality if not specified by the tool call
|
||||||
if not model_spec:
|
if not model_spec:
|
||||||
model_spec = _settings.get("image_model", "")
|
model_spec = _settings.get("image_model", "")
|
||||||
if quality == "medium" and _settings.get("image_quality"):
|
if not quality:
|
||||||
quality = _settings["image_quality"]
|
quality = _settings.get("image_quality") or "medium"
|
||||||
|
|
||||||
# Auto-detect best available image model if still not set
|
# Auto-detect best available image model if still not set
|
||||||
if not model_spec:
|
if not model_spec:
|
||||||
@@ -1482,7 +1482,7 @@ async def do_edit_image(
|
|||||||
session_id: Optional[str] = None,
|
session_id: Optional[str] = None,
|
||||||
owner: Optional[str] = None,
|
owner: Optional[str] = None,
|
||||||
size: str = "auto",
|
size: str = "auto",
|
||||||
quality: str = "medium",
|
quality: Optional[str] = None,
|
||||||
progress_callback: Optional[Callable[[Dict[str, Any]], Awaitable[None]]] = None,
|
progress_callback: Optional[Callable[[Dict[str, Any]], Awaitable[None]]] = None,
|
||||||
incognito: bool = False,
|
incognito: bool = False,
|
||||||
) -> Dict:
|
) -> Dict:
|
||||||
@@ -1509,8 +1509,8 @@ async def do_edit_image(
|
|||||||
|
|
||||||
if not model_spec:
|
if not model_spec:
|
||||||
model_spec = _settings.get("image_model", "")
|
model_spec = _settings.get("image_model", "")
|
||||||
if quality == "medium" and _settings.get("image_quality"):
|
if quality is None:
|
||||||
quality = _settings["image_quality"]
|
quality = _settings.get("image_quality") or "medium"
|
||||||
if not model_spec:
|
if not model_spec:
|
||||||
return {"error": "No image model selected for image editing"}
|
return {"error": "No image model selected for image editing"}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ def compact_browser_observation(value, budget=8000):
|
|||||||
# Snapshot text already contains labels and refs in DOM order.
|
# Snapshot text already contains labels and refs in DOM order.
|
||||||
# The refs mapping repeats them and buries menus in raw JSON.
|
# The refs mapping repeats them and buries menus in raw JSON.
|
||||||
lines = [line for line in snapshot.splitlines()
|
lines = [line for line in snapshot.splitlines()
|
||||||
if not re.fullmatch(r'\s*-?\s*generic(?:\s+\[ref=e\d+\])?:?\s*', line)]
|
if not re.fullmatch(r'\s*(?:-\s*)?generic(?:\s+\[ref=e\d+\])?:?\s*', line)]
|
||||||
pages.append(('URL: ' + str(url) + '\n' if url else '') + '\n'.join(lines))
|
pages.append(('URL: ' + str(url) + '\n' if url else '') + '\n'.join(lines))
|
||||||
elif url:
|
elif url:
|
||||||
notices.append('URL: ' + str(url))
|
notices.append('URL: ' + str(url))
|
||||||
@@ -37,7 +37,8 @@ def compact_browser_observation(value, budget=8000):
|
|||||||
parsed = json.loads(item)
|
parsed = json.loads(item)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
# CLI status text precedes the JSON post-interaction state.
|
# CLI status text precedes the JSON post-interaction state.
|
||||||
parts = re.split(r'\n\n\[(?:post-[^\]]+|page state after failed [^\]]+)\]\n', item)
|
# Labels are one line; stopping at \n keeps the split linear.
|
||||||
|
parts = re.split(r'\n\n\[(?:post-[^\]\n]+|page state after failed [^\]\n]+)\]\n', item)
|
||||||
if len(parts) > 1:
|
if len(parts) > 1:
|
||||||
for part in parts:
|
for part in parts:
|
||||||
visit(part)
|
visit(part)
|
||||||
|
|||||||
+37
-5
@@ -10,7 +10,7 @@ import os
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime, timezone
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
from src.auth_helpers import owner_filter
|
from src.auth_helpers import owner_filter
|
||||||
@@ -1957,6 +1957,23 @@ async def action_learn_sender_signatures(owner: str, **kwargs) -> Tuple[str, boo
|
|||||||
return str(e), False
|
return str(e), False
|
||||||
|
|
||||||
|
|
||||||
|
def _daily_brief_utc_boundary(local_dt, tz=None):
|
||||||
|
"""Convert a local wall-clock boundary using the offset at that boundary."""
|
||||||
|
if local_dt.tzinfo is None and tz is not None:
|
||||||
|
local_dt = local_dt.replace(tzinfo=tz)
|
||||||
|
return local_dt.astimezone(timezone.utc).replace(tzinfo=None)
|
||||||
|
|
||||||
|
|
||||||
|
def _daily_brief_local_start(event, tz=None):
|
||||||
|
"""Keep date-only/floating rows local; convert only stored UTC instants."""
|
||||||
|
start = event.dtstart
|
||||||
|
if event.is_utc and not event.all_day:
|
||||||
|
if start.tzinfo is None:
|
||||||
|
start = start.replace(tzinfo=timezone.utc)
|
||||||
|
return start.astimezone(tz).replace(tzinfo=None)
|
||||||
|
return start
|
||||||
|
|
||||||
|
|
||||||
async def action_daily_brief(owner: str, **kwargs) -> Tuple[str, bool]:
|
async def action_daily_brief(owner: str, **kwargs) -> Tuple[str, bool]:
|
||||||
"""Build a short morning digest: today's calendar events, unread email count
|
"""Build a short morning digest: today's calendar events, unread email count
|
||||||
+ top-N senders/subjects, active todos."""
|
+ top-N senders/subjects, active todos."""
|
||||||
@@ -1966,10 +1983,13 @@ async def action_daily_brief(owner: str, **kwargs) -> Tuple[str, bool]:
|
|||||||
|
|
||||||
from core.database import SessionLocal, CalendarEvent, CalendarCal, Note
|
from core.database import SessionLocal, CalendarEvent, CalendarCal, Note
|
||||||
from routes.email_helpers import _imap_connect, _decode_header
|
from routes.email_helpers import _imap_connect, _decode_header
|
||||||
|
from sqlalchemy import and_, or_
|
||||||
|
|
||||||
# ----- Calendar: today's events -----
|
# ----- Calendar: today's events -----
|
||||||
today = _dt.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
today = _dt.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
tomorrow = today + _td(days=1)
|
tomorrow = today + _td(days=1)
|
||||||
|
utc_today = _daily_brief_utc_boundary(today)
|
||||||
|
utc_tomorrow = _daily_brief_utc_boundary(tomorrow)
|
||||||
# v2 review HIGH-12: gate the OR-null branch on single-user
|
# v2 review HIGH-12: gate the OR-null branch on single-user
|
||||||
# (unconfigured) deploys only. In a multi-user deploy, one
|
# (unconfigured) deploys only. In a multi-user deploy, one
|
||||||
# user's daily brief must not include another user's notes or
|
# user's daily brief must not include another user's notes or
|
||||||
@@ -1982,13 +2002,25 @@ async def action_daily_brief(owner: str, **kwargs) -> Tuple[str, bool]:
|
|||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
ev_q = db.query(CalendarEvent).join(CalendarCal).filter(
|
ev_q = db.query(CalendarEvent).join(CalendarCal).filter(
|
||||||
CalendarEvent.dtstart < tomorrow,
|
or_(
|
||||||
CalendarEvent.dtend > today,
|
and_(
|
||||||
|
CalendarEvent.is_utc.is_(True),
|
||||||
|
CalendarEvent.all_day.is_not(True),
|
||||||
|
CalendarEvent.dtstart < utc_tomorrow,
|
||||||
|
CalendarEvent.dtend > utc_today,
|
||||||
|
),
|
||||||
|
and_(
|
||||||
|
or_(CalendarEvent.is_utc.is_not(True), CalendarEvent.all_day.is_(True)),
|
||||||
|
CalendarEvent.dtstart < tomorrow,
|
||||||
|
CalendarEvent.dtend > today,
|
||||||
|
),
|
||||||
|
),
|
||||||
CalendarEvent.status != "cancelled",
|
CalendarEvent.status != "cancelled",
|
||||||
)
|
)
|
||||||
if owner:
|
if owner:
|
||||||
ev_q = owner_filter(ev_q, CalendarCal, owner, include_shared=_allow_null)
|
ev_q = owner_filter(ev_q, CalendarCal, owner, include_shared=_allow_null)
|
||||||
events = ev_q.order_by(CalendarEvent.dtstart).all()
|
events = ev_q.all()
|
||||||
|
events.sort(key=_daily_brief_local_start)
|
||||||
# ----- Notes: pinned + non-archived todos with at least one undone item -----
|
# ----- Notes: pinned + non-archived todos with at least one undone item -----
|
||||||
n_q = db.query(Note).filter(Note.archived == False) # noqa: E712
|
n_q = db.query(Note).filter(Note.archived == False) # noqa: E712
|
||||||
if owner:
|
if owner:
|
||||||
@@ -2058,7 +2090,7 @@ async def action_daily_brief(owner: str, **kwargs) -> Tuple[str, bool]:
|
|||||||
if events:
|
if events:
|
||||||
plain.append("Calendar:")
|
plain.append("Calendar:")
|
||||||
for e in events:
|
for e in events:
|
||||||
t = e.dtstart.strftime("%H:%M") if not e.all_day else "all day"
|
t = _daily_brief_local_start(e).strftime("%H:%M") if not e.all_day else "all day"
|
||||||
loc = f" @ {e.location}" if e.location else ""
|
loc = f" @ {e.location}" if e.location else ""
|
||||||
plain.append(f" {t} {e.summary}{loc}")
|
plain.append(f" {t} {e.summary}{loc}")
|
||||||
plain.append("")
|
plain.append("")
|
||||||
|
|||||||
+7
-1
@@ -282,7 +282,13 @@ class ChatHandler:
|
|||||||
_sync_upload_vision_to_gallery(file_info, owner, vl_desc)
|
_sync_upload_vision_to_gallery(file_info, owner, vl_desc)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
enhanced_message = f"{enhanced_message}\n\n[Image: {file_info['name']}]\n{vl_desc}"
|
enhanced_message = f"{enhanced_message}\n\n[Image: {file_info['name']}]\n"
|
||||||
|
if vl_desc:
|
||||||
|
# A separate vision call wrote this, but the chat model sees
|
||||||
|
# one message and answers the prose as if the user wrote it
|
||||||
|
# (#5573). Label it below the marker line — the client strips
|
||||||
|
# the block by matching `[Image: <name>]\n`.
|
||||||
|
enhanced_message += f"[Image description — context only, not part of the user's message]:\n{vl_desc}"
|
||||||
# Surface the description to the client live so it renders as a
|
# Surface the description to the client live so it renders as a
|
||||||
# collapsible "image description" on the user bubble (not just
|
# collapsible "image description" on the user bubble (not just
|
||||||
# after a refresh that re-parses the stored message).
|
# after a refresh that re-parses the stored message).
|
||||||
|
|||||||
@@ -1229,7 +1229,7 @@ def tasks_terminal_response(raw, *, user_text='', max_items=20):
|
|||||||
text = str(payload or '').strip()
|
text = str(payload or '').strip()
|
||||||
rows = []
|
rows = []
|
||||||
for line in text.splitlines():
|
for line in text.splitlines():
|
||||||
match = re.match(r'^\s*\d+\.\s+(.*?)\s+\(([^)]+)\)\s+—\s+(.+)$', line)
|
match = re.match(r'^\s*\d+\.\s(.*?)\s\(([^()]+)\)\s+—\s+(.+)$', line)
|
||||||
if match:
|
if match:
|
||||||
details = match[3].strip()
|
details = match[3].strip()
|
||||||
rows.append({
|
rows.append({
|
||||||
@@ -1346,7 +1346,7 @@ def skills_terminal_response(raw, *, user_text='', max_items=20):
|
|||||||
continue
|
continue
|
||||||
match = re.match(r'^\s*-\s+\*\*([^*]+)\*\*(?:\s+\(([^)]+)\))?', line)
|
match = re.match(r'^\s*-\s+\*\*([^*]+)\*\*(?:\s+\(([^)]+)\))?', line)
|
||||||
if not match:
|
if not match:
|
||||||
match = re.match(r'^\s*-\s+([^:]+?)(?:\s+\(([^)]+)\))?(?:\s*:|$)', line)
|
match = re.match(r'^\s*-\s+([^\s:][^:]*?)(?:\s\(([^()]+)\)(?:\s*:|$)|:|$)', line)
|
||||||
if match and status:
|
if match and status:
|
||||||
rows.append((status, match.group(1).strip(), (match.group(2) or '').strip()))
|
rows.append((status, match.group(1).strip(), (match.group(2) or '').strip()))
|
||||||
if not rows:
|
if not rows:
|
||||||
@@ -5065,7 +5065,7 @@ def email_search_result_empty(value):
|
|||||||
try:
|
try:
|
||||||
decoded = json.loads(value)
|
decoded = json.loads(value)
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
return bool(re.fullmatch(r'\s*No emails matched [^\n]+\.?\s*', value, re.I))
|
return bool(re.fullmatch(r'\s*No emails matched [^\n]+(?:\n\s*)?', value, re.I))
|
||||||
if decoded == value:
|
if decoded == value:
|
||||||
return False
|
return False
|
||||||
value = decoded
|
value = decoded
|
||||||
@@ -5339,6 +5339,16 @@ def private_browser_effective_url(result):
|
|||||||
return urls[-1] if urls else ''
|
return urls[-1] if urls else ''
|
||||||
|
|
||||||
|
|
||||||
|
def is_google_sorry_url(url):
|
||||||
|
"""Recognize Google's rate-limit interstitial."""
|
||||||
|
# Matching the host separately keeps this linear; one combined pattern
|
||||||
|
# rescans the host from every dot.
|
||||||
|
return any(
|
||||||
|
re.fullmatch(r'(?:[^/]+\.)?google\.[^/]+', host[1], re.I)
|
||||||
|
for host in re.finditer(r'https?://([^/]+)/sorry/', url, re.I)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def browser_observation_page_missing(raw):
|
def browser_observation_page_missing(raw):
|
||||||
"""Recognize a rendered error page, rather than an article mentioning 404."""
|
"""Recognize a rendered error page, rather than an article mentioning 404."""
|
||||||
text = str(raw or '')
|
text = str(raw or '')
|
||||||
@@ -8213,11 +8223,7 @@ async def stream_preview(*, endpoint_url, model, messages, headers, turn_contrac
|
|||||||
browser_search_url = requested_browser_url or effective_browser_url
|
browser_search_url = requested_browser_url or effective_browser_url
|
||||||
is_search_engine_navigation = contains_search_engine_navigation(
|
is_search_engine_navigation = contains_search_engine_navigation(
|
||||||
browser_search_url
|
browser_search_url
|
||||||
) or bool(re.search(
|
) or is_google_sorry_url(effective_browser_url)
|
||||||
r'https?://(?:[^/]+\.)?google\.[^/]+/sorry/',
|
|
||||||
effective_browser_url,
|
|
||||||
re.I,
|
|
||||||
))
|
|
||||||
has_native_search = any(
|
has_native_search = any(
|
||||||
canonical(schema['function']['name']) == 'web_search'
|
canonical(schema['function']['name']) == 'web_search'
|
||||||
for schema in offered
|
for schema in offered
|
||||||
|
|||||||
@@ -142,7 +142,10 @@ def _process_pdf(
|
|||||||
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
||||||
temp_img_path = tmp.name
|
temp_img_path = tmp.name
|
||||||
try:
|
try:
|
||||||
img.image.save(temp_img_path, "PNG") # pypdf -> PIL image
|
image = img.image
|
||||||
|
if image.mode == "CMYK":
|
||||||
|
image = image.convert("RGB")
|
||||||
|
image.save(temp_img_path, "PNG")
|
||||||
ocr_text = analyze_image_with_vl(temp_img_path, owner=owner)
|
ocr_text = analyze_image_with_vl(temp_img_path, owner=owner)
|
||||||
if ocr_text and "unavailable" not in ocr_text.lower():
|
if ocr_text and "unavailable" not in ocr_text.lower():
|
||||||
pdf_text += f"\n\n[Page {page_num + 1} image {img_index + 1} text]: {ocr_text}"
|
pdf_text += f"\n\n[Page {page_num + 1} image {img_index + 1} text]: {ocr_text}"
|
||||||
|
|||||||
+49
-11
@@ -422,7 +422,18 @@ def _stream_delta_event(text: str, *, thinking: bool = False) -> str:
|
|||||||
return f"data: {json.dumps(payload)}\n\n"
|
return f"data: {json.dumps(payload)}\n\n"
|
||||||
|
|
||||||
|
|
||||||
_DEGENERATE_WORD_RE = re.compile(r"[A-Za-z0-9_\u0370-\u03ff\u0400-\u04ff]+")
|
_WORD_CHARS = r"A-Za-z0-9_\u0370-\u03ff\u0400-\u04ff"
|
||||||
|
_DELIM_RE = re.compile(rf"[^{_WORD_CHARS}+\-.]")
|
||||||
|
_NUMERIC_PATTERN = rf"(?<![{_WORD_CHARS}])[+-]?(?:[0-9]+\.[0-9]+|\.[0-9]+|[0-9]+)(?:[eE][+-]?[0-9]+)?(?![{_WORD_CHARS}])"
|
||||||
|
_NUMERIC_FULLMATCH_RE = re.compile(rf"^[+-]?(?:[0-9]+\.[0-9]+|\.[0-9]+|[0-9]+)(?:[eE][+-]?[0-9]+)?$")
|
||||||
|
_DEGENERATE_WORD_RE = re.compile(rf"[{_WORD_CHARS}]+")
|
||||||
|
_TOKEN_RE = re.compile(rf"(?P<NUM>{_NUMERIC_PATTERN})|(?P<WORD>[{_WORD_CHARS}]+)")
|
||||||
|
|
||||||
|
|
||||||
|
def _is_numeric_token(token: str) -> bool:
|
||||||
|
if token.isdigit():
|
||||||
|
return True
|
||||||
|
return _NUMERIC_FULLMATCH_RE.fullmatch(token) is not None
|
||||||
|
|
||||||
|
|
||||||
class _DegenerateStreamGuard:
|
class _DegenerateStreamGuard:
|
||||||
@@ -432,32 +443,50 @@ class _DegenerateStreamGuard:
|
|||||||
"Summer Summer ..."). This is not a useful response and can burn context,
|
"Summer Summer ..."). This is not a useful response and can burn context,
|
||||||
browser memory, and GPU time. Keep the guard conservative: only fire on long
|
browser memory, and GPU time. Keep the guard conservative: only fire on long
|
||||||
same-token runs or a very dominant repeated token in the recent window.
|
same-token runs or a very dominant repeated token in the recent window.
|
||||||
|
Repetition made only of numbers is data (a zero or identity matrix, a
|
||||||
|
padded buffer), so it only stops at a long all-numeric backstop.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, model: str):
|
def __init__(self, model: str):
|
||||||
self.model = model or "model"
|
self.model = model or "model"
|
||||||
self.last_token = ""
|
self.last_token = ""
|
||||||
self.same_run = 0
|
self.same_run = 0
|
||||||
|
self.numeric_run = 0
|
||||||
self.recent_tokens: List[str] = []
|
self.recent_tokens: List[str] = []
|
||||||
self.periodic_tokens: List[str] = []
|
self.periodic_tokens: List[str] = []
|
||||||
self.total_chars = 0
|
self.total_chars = 0
|
||||||
|
self._buffer = ""
|
||||||
|
|
||||||
def check(self, text: str) -> Optional[str]:
|
def check(self, text: str) -> Optional[str]:
|
||||||
if not text:
|
if not text:
|
||||||
return None
|
return None
|
||||||
self.total_chars += len(text)
|
self.total_chars += len(text)
|
||||||
# Keep one-character tokens. Structured output often varies through
|
self._buffer += text
|
||||||
# numeric selectors or coordinates; dropping those tokens can make
|
|
||||||
# valid CSS/SVG rows look like an exact repeated phrase.
|
m_delims = list(_DELIM_RE.finditer(self._buffer))
|
||||||
tokens = [t.lower() for t in _DEGENERATE_WORD_RE.findall(text)]
|
if m_delims:
|
||||||
|
split_idx = m_delims[-1].end()
|
||||||
|
head = self._buffer[:split_idx]
|
||||||
|
self._buffer = self._buffer[split_idx:]
|
||||||
|
elif len(self._buffer) >= 256:
|
||||||
|
head = self._buffer
|
||||||
|
self._buffer = ""
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
tokens = []
|
||||||
|
for m in _TOKEN_RE.finditer(head):
|
||||||
|
tokens.append((m.group(0).lower(), m.lastgroup == "NUM"))
|
||||||
if not tokens:
|
if not tokens:
|
||||||
return None
|
return None
|
||||||
for token in tokens:
|
|
||||||
|
for token, is_num in tokens:
|
||||||
if token == self.last_token:
|
if token == self.last_token:
|
||||||
self.same_run += 1
|
self.same_run += 1
|
||||||
else:
|
else:
|
||||||
self.last_token = token
|
self.last_token = token
|
||||||
self.same_run = 1
|
self.same_run = 1
|
||||||
|
self.numeric_run = self.numeric_run + 1 if is_num else 0
|
||||||
self.recent_tokens.append(token)
|
self.recent_tokens.append(token)
|
||||||
self.periodic_tokens.append(token)
|
self.periodic_tokens.append(token)
|
||||||
if len(self.recent_tokens) > 96:
|
if len(self.recent_tokens) > 96:
|
||||||
@@ -466,14 +495,20 @@ class _DegenerateStreamGuard:
|
|||||||
self.periodic_tokens = self.periodic_tokens[-2048:]
|
self.periodic_tokens = self.periodic_tokens[-2048:]
|
||||||
|
|
||||||
reason = None
|
reason = None
|
||||||
if self.same_run >= 28 and self.total_chars >= 100:
|
if self.same_run >= 28 and self.total_chars >= 100 and not _is_numeric_token(self.last_token):
|
||||||
reason = f"repeated '{self.last_token}' {self.same_run} times"
|
reason = f"repeated '{self.last_token}' {self.same_run} times"
|
||||||
elif len(self.recent_tokens) >= 72:
|
elif len(self.recent_tokens) >= 72:
|
||||||
top = max(set(self.recent_tokens), key=self.recent_tokens.count)
|
top = max(set(self.recent_tokens), key=self.recent_tokens.count)
|
||||||
count = self.recent_tokens.count(top)
|
count = self.recent_tokens.count(top)
|
||||||
if count >= 60 and count / max(len(self.recent_tokens), 1) >= 0.78:
|
if count >= 60 and count / max(len(self.recent_tokens), 1) >= 0.78 and not _is_numeric_token(top):
|
||||||
reason = f"repeated '{top}' {count}/{len(self.recent_tokens)} recent tokens"
|
reason = f"repeated '{top}' {count}/{len(self.recent_tokens)} recent tokens"
|
||||||
if not reason and len(self.recent_tokens) >= 48:
|
if (
|
||||||
|
not reason
|
||||||
|
and len(self.recent_tokens) >= 48
|
||||||
|
# A window that is mostly numbers is matrix/table data; its
|
||||||
|
# periodicity is real. Mixed loops still hit the exact-block check.
|
||||||
|
and sum(_is_numeric_token(t) for t in self.recent_tokens) * 2 < len(self.recent_tokens)
|
||||||
|
):
|
||||||
# Detect a periodic suffix instead of counting repeated n-grams.
|
# Detect a periodic suffix instead of counting repeated n-grams.
|
||||||
# Reused property names are normal in CSS/SVG/JSON; a collapsed
|
# Reused property names are normal in CSS/SVG/JSON; a collapsed
|
||||||
# generation repeats nearly every token at a short fixed period.
|
# generation repeats nearly every token at a short fixed period.
|
||||||
@@ -508,12 +543,15 @@ class _DegenerateStreamGuard:
|
|||||||
if history[previous:previous + anchor_size] != anchor:
|
if history[previous:previous + anchor_size] != anchor:
|
||||||
continue
|
continue
|
||||||
period = current_anchor - previous
|
period = current_anchor - previous
|
||||||
if period < 24 or len(history) < period * 2:
|
if period < 24 or len(history) < period * 2 or self.numeric_run >= period:
|
||||||
continue
|
continue
|
||||||
if history[-2 * period:-period] == history[-period:]:
|
if history[-2 * period:-period] == history[-period:] and not all(_is_numeric_token(t) for t in history[-period:]):
|
||||||
reason = f"repeated an exact {period}-token block twice"
|
reason = f"repeated an exact {period}-token block twice"
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if not reason and self.numeric_run >= 2048:
|
||||||
|
reason = f"emitted {self.numeric_run} numbers in a row"
|
||||||
|
|
||||||
if not reason:
|
if not reason:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -193,7 +193,10 @@ class ModelDiscovery:
|
|||||||
data = r.json()
|
data = r.json()
|
||||||
# Some OpenAI-compatible servers return a bare list, not {"data": [...]}.
|
# Some OpenAI-compatible servers return a bare list, not {"data": [...]}.
|
||||||
items = data if isinstance(data, list) else ((data or {}).get("data") or [])
|
items = data if isinstance(data, list) else ((data or {}).get("data") or [])
|
||||||
ids = [m.get("id") for m in items if isinstance(m, dict) and m.get("id")]
|
ids = [
|
||||||
|
m["id"] for m in items
|
||||||
|
if isinstance(m, dict) and isinstance(m.get("id"), str) and m["id"]
|
||||||
|
]
|
||||||
if ids:
|
if ids:
|
||||||
return {
|
return {
|
||||||
"host": host,
|
"host": host,
|
||||||
|
|||||||
+4
-2
@@ -102,8 +102,10 @@ _PLAIN_FRONT_MATTER_RE = re.compile(
|
|||||||
# - **label:** value <!-- field=NAME-ENC type=text -->
|
# - **label:** value <!-- field=NAME-ENC type=text -->
|
||||||
# - **label** [opts]: value <!-- field=NAME-ENC type=choice -->
|
# - **label** [opts]: value <!-- field=NAME-ENC type=choice -->
|
||||||
# - [x] **label** <!-- field=NAME-ENC type=checkbox -->
|
# - [x] **label** <!-- field=NAME-ENC type=checkbox -->
|
||||||
|
# Accept Unicode word characters for older saved markers, whose encoder left
|
||||||
|
# non-ASCII letters/numbers literal. Newly rendered names are ASCII-safe.
|
||||||
_FIELD_BULLET_RE = re.compile(
|
_FIELD_BULLET_RE = re.compile(
|
||||||
r'^\s*-\s+(?P<body>.*?)\s*<!--\s*field=(?P<name>[A-Za-z0-9_.%-]+)\s+type=(?P<type>\w+)\s*-->\s*$'
|
r'^\s*-\s+(?P<body>.*?)\s*<!--\s*field=(?P<name>[\w.%-]+)\s+type=(?P<type>\w+)\s*-->\s*$'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -115,7 +117,7 @@ def _encode_name(name: str) -> str:
|
|||||||
"""
|
"""
|
||||||
out = []
|
out = []
|
||||||
for ch in name or "":
|
for ch in name or "":
|
||||||
if ch.isalnum() or ch in ("_", ".", "-"):
|
if (ch.isascii() and ch.isalnum()) or ch in ("_", ".", "-"):
|
||||||
out.append(ch)
|
out.append(ch)
|
||||||
else:
|
else:
|
||||||
for b in ch.encode("utf-8"):
|
for b in ch.encode("utf-8"):
|
||||||
|
|||||||
+63
-8
@@ -76,9 +76,23 @@ _ERROR_DETAIL = {
|
|||||||
"auth_or_protocol_error": "authentication or protocol error",
|
"auth_or_protocol_error": "authentication or protocol error",
|
||||||
"no_models": "endpoint returned no models",
|
"no_models": "endpoint returned no models",
|
||||||
"no_host": "no host configured",
|
"no_host": "no host configured",
|
||||||
|
"config_source_error": "configuration source could not be read",
|
||||||
"error": "probe failed",
|
"error": "probe failed",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Config/inventory source behind each network subsystem. `_gather_inputs()`
|
||||||
|
# fails soft, so a source that could not be read hands its probe an empty
|
||||||
|
# inventory — indistinguishable from "nothing configured", which every probe
|
||||||
|
# answers with `disabled`. The collector therefore reports the source failure
|
||||||
|
# instead of running a probe whose verdict would be a lie.
|
||||||
|
CONFIG_SOURCE_ERROR = "config_source_error"
|
||||||
|
_SUBSYSTEM_SOURCE = {
|
||||||
|
"searxng": "settings",
|
||||||
|
"ntfy": "integrations",
|
||||||
|
"email": "accounts",
|
||||||
|
"providers": "endpoints",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def _svc(name: str, status: str, detail: str, **meta: Any) -> Dict[str, Any]:
|
def _svc(name: str, status: str, detail: str, **meta: Any) -> Dict[str, Any]:
|
||||||
return {"name": name, "status": status, "detail": detail, "meta": dict(meta)}
|
return {"name": name, "status": status, "detail": detail, "meta": dict(meta)}
|
||||||
@@ -411,26 +425,33 @@ def _gather_inputs() -> Dict[str, Any]:
|
|||||||
"""Pull live config/account/endpoint lists from the app's data sources.
|
"""Pull live config/account/endpoint lists from the app's data sources.
|
||||||
|
|
||||||
Each lookup fails soft: a broken source yields an empty/neutral value so a
|
Each lookup fails soft: a broken source yields an empty/neutral value so a
|
||||||
single failure can't take down the whole health report.
|
single failure can't take down the whole health report. The failure is also
|
||||||
|
*recorded* under `failed` (source -> controlled category), because the empty
|
||||||
|
value alone cannot be told apart from a source that loaded and is genuinely
|
||||||
|
empty — and the probes read empty as `disabled`.
|
||||||
"""
|
"""
|
||||||
settings: Dict[str, Any] = {}
|
settings: Dict[str, Any] = {}
|
||||||
integrations: List[Dict[str, Any]] = []
|
integrations: List[Dict[str, Any]] = []
|
||||||
accounts: List[Dict[str, Any]] = []
|
accounts: List[Dict[str, Any]] = []
|
||||||
endpoints: List[Dict[str, Any]] = []
|
endpoints: List[Dict[str, Any]] = []
|
||||||
|
failed: Dict[str, str] = {}
|
||||||
try:
|
try:
|
||||||
from src.settings import load_settings
|
from src.settings import load_settings
|
||||||
settings = load_settings() or {}
|
settings = load_settings() or {}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
failed["settings"] = _classify_error(e)
|
||||||
logger.debug(f"service_health: settings load failed: {e}")
|
logger.debug(f"service_health: settings load failed: {e}")
|
||||||
try:
|
try:
|
||||||
from src.integrations import load_integrations
|
from src.integrations import load_integrations
|
||||||
integrations = load_integrations() or []
|
integrations = load_integrations() or []
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
failed["integrations"] = _classify_error(e)
|
||||||
logger.debug(f"service_health: integrations load failed: {e}")
|
logger.debug(f"service_health: integrations load failed: {e}")
|
||||||
try:
|
try:
|
||||||
from routes.email_helpers import _list_email_accounts
|
from routes.email_helpers import _list_email_accounts
|
||||||
accounts = _list_email_accounts() or []
|
accounts = _list_email_accounts() or []
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
failed["accounts"] = _classify_error(e)
|
||||||
logger.debug(f"service_health: email accounts load failed: {e}")
|
logger.debug(f"service_health: email accounts load failed: {e}")
|
||||||
try:
|
try:
|
||||||
from core.database import SessionLocal, ModelEndpoint
|
from core.database import SessionLocal, ModelEndpoint
|
||||||
@@ -443,9 +464,10 @@ def _gather_inputs() -> Dict[str, Any]:
|
|||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
failed["endpoints"] = _classify_error(e)
|
||||||
logger.debug(f"service_health: endpoint load failed: {e}")
|
logger.debug(f"service_health: endpoint load failed: {e}")
|
||||||
return {"settings": settings, "integrations": integrations,
|
return {"settings": settings, "integrations": integrations,
|
||||||
"accounts": accounts, "endpoints": endpoints}
|
"accounts": accounts, "endpoints": endpoints, "failed": failed}
|
||||||
|
|
||||||
|
|
||||||
async def _run_subsystem(name: str, fn: Callable, *args: Any) -> Dict[str, Any]:
|
async def _run_subsystem(name: str, fn: Callable, *args: Any) -> Dict[str, Any]:
|
||||||
@@ -464,6 +486,24 @@ async def _run_subsystem(name: str, fn: Callable, *args: Any) -> Dict[str, Any]:
|
|||||||
return _svc(name, DOWN, _detail_for(category), error=category)
|
return _svc(name, DOWN, _detail_for(category), error=category)
|
||||||
|
|
||||||
|
|
||||||
|
async def _source_unavailable(name: str, source: str,
|
||||||
|
category: str) -> Dict[str, Any]:
|
||||||
|
"""Controlled entry for a subsystem whose config source could not be read.
|
||||||
|
|
||||||
|
Deliberately not `disabled`: the configured state is *unknown*, not known to
|
||||||
|
be off. `degraded` rather than `down` because the failure is in reading the
|
||||||
|
inventory, not a probe that reached the subsystem and failed — but it still
|
||||||
|
carries non-zero severity, so a report containing one can never roll up to
|
||||||
|
`ok`. `meta` names the source and the controlled category of the underlying
|
||||||
|
exception; never its text.
|
||||||
|
"""
|
||||||
|
return _svc(name, DEGRADED,
|
||||||
|
f"Could not read configuration "
|
||||||
|
f"({_detail_for(CONFIG_SOURCE_ERROR)}).",
|
||||||
|
error=CONFIG_SOURCE_ERROR, source=source,
|
||||||
|
source_error=category)
|
||||||
|
|
||||||
|
|
||||||
async def collect_service_health(rag_manager: Any = None,
|
async def collect_service_health(rag_manager: Any = None,
|
||||||
memory_vector: Any = None) -> Dict[str, Any]:
|
memory_vector: Any = None) -> Dict[str, Any]:
|
||||||
"""Run every probe and return {overall, services, timestamp}.
|
"""Run every probe and return {overall, services, timestamp}.
|
||||||
@@ -472,6 +512,10 @@ async def collect_service_health(rag_manager: Any = None,
|
|||||||
four network subsystems run concurrently, each under `_SUBSYSTEM_DEADLINE`,
|
four network subsystems run concurrently, each under `_SUBSYSTEM_DEADLINE`,
|
||||||
with an overall `_AGGREGATE_DEADLINE` backstop. Per-item probes inside
|
with an overall `_AGGREGATE_DEADLINE` backstop. Per-item probes inside
|
||||||
providers/email are themselves bounded by `_FANOUT_BUDGET`.
|
providers/email are themselves bounded by `_FANOUT_BUDGET`.
|
||||||
|
|
||||||
|
A subsystem whose config/inventory source failed to load is reported via
|
||||||
|
`_source_unavailable()` instead of being probed with an empty inventory,
|
||||||
|
which every probe would otherwise report as `disabled`.
|
||||||
"""
|
"""
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
@@ -482,12 +526,23 @@ async def collect_service_health(rag_manager: Any = None,
|
|||||||
chroma = chromadb_health(rag_manager, memory_vector)
|
chroma = chromadb_health(rag_manager, memory_vector)
|
||||||
|
|
||||||
names = ["searxng", "ntfy", "email", "providers"]
|
names = ["searxng", "ntfy", "email", "providers"]
|
||||||
coros = [
|
probes = {
|
||||||
_run_subsystem("searxng", searxng_health, settings),
|
"searxng": (searxng_health, (settings,)),
|
||||||
_run_subsystem("ntfy", ntfy_health, inputs["integrations"], settings),
|
"ntfy": (ntfy_health, (inputs["integrations"], settings)),
|
||||||
_run_subsystem("email", email_health, inputs["accounts"]),
|
"email": (email_health, (inputs["accounts"],)),
|
||||||
_run_subsystem("providers", providers_health, inputs["endpoints"]),
|
"providers": (providers_health, (inputs["endpoints"],)),
|
||||||
]
|
}
|
||||||
|
# A subsystem whose inventory could not be read is reported as such. The
|
||||||
|
# others are still probed normally — one broken source degrades one entry.
|
||||||
|
failed = inputs.get("failed") or {}
|
||||||
|
coros = []
|
||||||
|
for name in names:
|
||||||
|
source = _SUBSYSTEM_SOURCE[name]
|
||||||
|
if source in failed:
|
||||||
|
coros.append(_source_unavailable(name, source, failed[source]))
|
||||||
|
else:
|
||||||
|
fn, args = probes[name]
|
||||||
|
coros.append(_run_subsystem(name, fn, *args))
|
||||||
try:
|
try:
|
||||||
results = await asyncio.wait_for(asyncio.gather(*coros),
|
results = await asyncio.wait_for(asyncio.gather(*coros),
|
||||||
timeout=_AGGREGATE_DEADLINE)
|
timeout=_AGGREGATE_DEADLINE)
|
||||||
|
|||||||
+8
-51
@@ -12,16 +12,6 @@ from datetime import datetime, timedelta, timezone
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Names that indicate a throwaway/test session
|
|
||||||
_THROWAWAY_NAMES = {
|
|
||||||
"test", "testing", "asdf", "asd", "hello", "hi", "hey",
|
|
||||||
"yo", "sup", "hola", "hii", "hiii", "heyo",
|
|
||||||
"foo", "bar", "baz", "tmp", "temp", "scratch", "untitled",
|
|
||||||
"new chat", "delete", "remove", "junk", "trash", "xxx",
|
|
||||||
"abc", "qwerty", "blah", "stuff", "whatever", "idk",
|
|
||||||
"ok", "lol", "bruh", "hmm", "hm", "meh",
|
|
||||||
}
|
|
||||||
_THROWAWAY_MAX_MESSAGES = 4
|
|
||||||
_FRESH_EMPTY_SESSION_GRACE = timedelta(minutes=10)
|
_FRESH_EMPTY_SESSION_GRACE = timedelta(minutes=10)
|
||||||
_FRESH_SESSION_GRACE = _FRESH_EMPTY_SESSION_GRACE
|
_FRESH_SESSION_GRACE = _FRESH_EMPTY_SESSION_GRACE
|
||||||
|
|
||||||
@@ -54,14 +44,15 @@ def is_session_recently_active(row, now=None, grace=_FRESH_SESSION_GRACE) -> boo
|
|||||||
|
|
||||||
|
|
||||||
async def run_auto_sort(owner: str, skip_llm: bool = False, delete_throwaway: bool = True) -> str:
|
async def run_auto_sort(owner: str, skip_llm: bool = False, delete_throwaway: bool = True) -> str:
|
||||||
"""Run session cleanup + (optional) AI folder sort for the given owner.
|
"""Remove empty/Incognito sessions and optionally organize saved chats.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
owner: user whose sessions to process
|
owner: user whose sessions to process
|
||||||
skip_llm: when True, do only Phase 1 (delete empty/throwaway sessions);
|
skip_llm: when True, do only Phase 1 (delete empty/Incognito sessions);
|
||||||
skip Phase 2 (AI folder assignment). Used by the built-in daily
|
skip Phase 2 (AI folder assignment). Used by the built-in daily
|
||||||
background sweep so it never burns LLM tokens.
|
background sweep so it never burns LLM tokens.
|
||||||
delete_throwaway: when False, only empty/incognito sessions are deleted.
|
delete_throwaway: retained for caller compatibility. Ordinary sessions
|
||||||
|
with saved messages are always preserved, regardless of this flag.
|
||||||
|
|
||||||
Returns a human-readable summary of what was done.
|
Returns a human-readable summary of what was done.
|
||||||
"""
|
"""
|
||||||
@@ -71,7 +62,7 @@ async def run_auto_sort(owner: str, skip_llm: bool = False, delete_throwaway: bo
|
|||||||
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
# ── Phase 1: Delete empty/throwaway sessions ──
|
# ── Phase 1: Delete only empty and explicitly Incognito sessions ──
|
||||||
deleted_empty = 0
|
deleted_empty = 0
|
||||||
deleted_throwaway = 0
|
deleted_throwaway = 0
|
||||||
|
|
||||||
@@ -93,47 +84,13 @@ async def run_auto_sort(owner: str, skip_llm: bool = False, delete_throwaway: bo
|
|||||||
if is_session_recently_active(row, now=cleanup_now):
|
if is_session_recently_active(row, now=cleanup_now):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
msg_count = db.query(DbMsg.id).filter(
|
has_messages = db.query(DbMsg.id).filter(
|
||||||
DbMsg.session_id == row.id
|
DbMsg.session_id == row.id
|
||||||
).limit(_THROWAWAY_MAX_MESSAGES + 1).count()
|
).first() is not None
|
||||||
should_delete = False
|
if not has_messages:
|
||||||
|
|
||||||
if msg_count == 0:
|
|
||||||
if is_fresh:
|
if is_fresh:
|
||||||
continue
|
continue
|
||||||
should_delete = True
|
|
||||||
deleted_empty += 1
|
deleted_empty += 1
|
||||||
elif delete_throwaway and msg_count <= _THROWAWAY_MAX_MESSAGES:
|
|
||||||
name = (row.name or "").strip().lower()
|
|
||||||
first_msg = db.query(DbMsg.content).filter(
|
|
||||||
DbMsg.session_id == row.id, DbMsg.role == "user"
|
|
||||||
).order_by(DbMsg.timestamp).first()
|
|
||||||
first_text = (first_msg[0] or "").strip().lower() if first_msg else ""
|
|
||||||
assistant_count = db.query(DbMsg.id).filter(
|
|
||||||
DbMsg.session_id == row.id, DbMsg.role == "assistant"
|
|
||||||
).limit(1).count()
|
|
||||||
|
|
||||||
if name in _THROWAWAY_NAMES or name.startswith("chat:") or first_text in _THROWAWAY_NAMES:
|
|
||||||
should_delete = True
|
|
||||||
deleted_throwaway += 1
|
|
||||||
elif msg_count == 1 and assistant_count == 0:
|
|
||||||
should_delete = True
|
|
||||||
deleted_throwaway += 1
|
|
||||||
elif msg_count <= 4 and first_text and len(first_text.split()) <= 8 and len(first_text) <= 80:
|
|
||||||
# Short trivial chats — e.g. "write hi to a friend" → "Hi!"
|
|
||||||
should_delete = True
|
|
||||||
deleted_throwaway += 1
|
|
||||||
else:
|
|
||||||
# Aggressive: total message text under 250 chars combined = trivial
|
|
||||||
msg_rows = db.query(DbMsg.content).filter(
|
|
||||||
DbMsg.session_id == row.id
|
|
||||||
).all()
|
|
||||||
total_chars = sum(len(m[0] or "") for m in msg_rows)
|
|
||||||
if total_chars <= 250:
|
|
||||||
should_delete = True
|
|
||||||
deleted_throwaway += 1
|
|
||||||
|
|
||||||
if should_delete:
|
|
||||||
db.delete(row)
|
db.delete(row)
|
||||||
|
|
||||||
if deleted_empty or deleted_throwaway:
|
if deleted_empty or deleted_throwaway:
|
||||||
|
|||||||
+33
-5
@@ -57,25 +57,53 @@ def _message_to_context(msg: DBChatMessage) -> dict[str, Any]:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _escape_like(value: str) -> str:
|
def escape_like_pattern(value: str) -> str:
|
||||||
|
"""Escape SQL LIKE wildcards (``%``, ``_``) and the escape char itself.
|
||||||
|
|
||||||
|
Public shared helper for building ``ilike(f"%{...}%", escape="\\\\")``
|
||||||
|
patterns from user input. Must escape backslash first.
|
||||||
|
"""
|
||||||
return value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
return value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
||||||
|
|
||||||
|
|
||||||
|
def _escape_like(value: str) -> str:
|
||||||
|
return escape_like_pattern(value)
|
||||||
|
|
||||||
|
|
||||||
def _snippet(content: str, query: str, radius: int = 60) -> str:
|
def _snippet(content: str, query: str, radius: int = 60) -> str:
|
||||||
content = content or ""
|
content = content or ""
|
||||||
query = query or ""
|
query = query or ""
|
||||||
if not query:
|
if not query:
|
||||||
return content[: radius * 2]
|
return content[: radius * 2]
|
||||||
|
|
||||||
idx = content.lower().find(query.lower())
|
match_start = -1
|
||||||
if idx == -1:
|
match_length = 0
|
||||||
|
lowered_content = content.lower()
|
||||||
|
for candidate in _query_terms(query):
|
||||||
|
idx = lowered_content.find(candidate.lower())
|
||||||
|
if idx != -1 and (match_start == -1 or idx < match_start):
|
||||||
|
match_start = idx
|
||||||
|
match_length = len(candidate)
|
||||||
|
|
||||||
|
if match_start == -1:
|
||||||
return content[: radius * 2]
|
return content[: radius * 2]
|
||||||
|
|
||||||
start = max(0, idx - radius)
|
start = max(0, match_start - radius)
|
||||||
end = min(len(content), idx + len(query) + radius)
|
end = min(len(content), match_start + match_length + radius)
|
||||||
return ("..." if start > 0 else "") + content[start:end] + ("..." if end < len(content) else "")
|
return ("..." if start > 0 else "") + content[start:end] + ("..." if end < len(content) else "")
|
||||||
|
|
||||||
|
|
||||||
|
def _query_terms(query: str) -> list[str]:
|
||||||
|
"""Return meaningful quoted phrases and tokens in user-entered order."""
|
||||||
|
terms: list[str] = []
|
||||||
|
for match in re.finditer(r'"([^"]+)"|[\w][\w._-]*', query, flags=re.UNICODE):
|
||||||
|
term = match.group(1) if match.group(1) is not None else match.group(0).strip("._-")
|
||||||
|
term = term.strip()
|
||||||
|
if term:
|
||||||
|
terms.append(term)
|
||||||
|
return terms
|
||||||
|
|
||||||
|
|
||||||
def _sanitize_fts_query(query: str) -> str | None:
|
def _sanitize_fts_query(query: str) -> str | None:
|
||||||
"""Convert free text into a conservative FTS5 MATCH query.
|
"""Convert free text into a conservative FTS5 MATCH query.
|
||||||
|
|
||||||
|
|||||||
+22
-2
@@ -132,6 +132,23 @@ async def _cached(key: Tuple, ttl: float, fetch: Callable[[], Awaitable[Any]]) -
|
|||||||
_shared_cache_pending.pop(key, None)
|
_shared_cache_pending.pop(key, None)
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_cron(cron_expression: str) -> bool:
|
||||||
|
"""True for a standard 5-field cron expression that croniter can parse.
|
||||||
|
|
||||||
|
croniter also accepts 6- and 7-field expressions with a seconds field, but
|
||||||
|
the scheduler works at one-minute resolution and would run such a task
|
||||||
|
every second.
|
||||||
|
"""
|
||||||
|
if len((cron_expression or "").split()) != 5:
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
from croniter import croniter
|
||||||
|
croniter(cron_expression)
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def compute_next_run(schedule: str, scheduled_time: str,
|
def compute_next_run(schedule: str, scheduled_time: str,
|
||||||
scheduled_day: int = None,
|
scheduled_day: int = None,
|
||||||
scheduled_date: datetime = None,
|
scheduled_date: datetime = None,
|
||||||
@@ -175,6 +192,9 @@ def compute_next_run(schedule: str, scheduled_time: str,
|
|||||||
return dt.astimezone(timezone.utc).replace(tzinfo=None)
|
return dt.astimezone(timezone.utc).replace(tzinfo=None)
|
||||||
|
|
||||||
if schedule == "cron" and cron_expression:
|
if schedule == "cron" and cron_expression:
|
||||||
|
if not is_valid_cron(cron_expression):
|
||||||
|
logger.warning(f"Invalid cron expression '{cron_expression}'")
|
||||||
|
return None
|
||||||
try:
|
try:
|
||||||
from croniter import croniter
|
from croniter import croniter
|
||||||
cron = croniter(cron_expression, now)
|
cron = croniter(cron_expression, now)
|
||||||
@@ -306,7 +326,7 @@ def _digest_windows(now):
|
|||||||
|
|
||||||
|
|
||||||
def _checkin_calendar_events(db, owner, start, end):
|
def _checkin_calendar_events(db, owner, start, end):
|
||||||
"""Calendar events in [start, end] for ONE owner, for the check-in digest.
|
"""Calendar events in [start, end) for ONE owner, for the check-in digest.
|
||||||
|
|
||||||
Ownership lives on CalendarCal.owner; events inherit it via calendar_id.
|
Ownership lives on CalendarCal.owner; events inherit it via calendar_id.
|
||||||
The digest query had no owner scope, so it pulled EVERY user's events into
|
The digest query had no owner scope, so it pulled EVERY user's events into
|
||||||
@@ -320,7 +340,7 @@ def _checkin_calendar_events(db, owner, start, end):
|
|||||||
.filter(
|
.filter(
|
||||||
_CC.owner == owner,
|
_CC.owner == owner,
|
||||||
_CE.dtstart >= start,
|
_CE.dtstart >= start,
|
||||||
_CE.dtstart <= end,
|
_CE.dtstart < end,
|
||||||
_CE.status != "cancelled",
|
_CE.status != "cancelled",
|
||||||
)
|
)
|
||||||
.order_by(_CE.dtstart)
|
.order_by(_CE.dtstart)
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ _REQUIRED_NATIVE_TOOL_ARGS = {
|
|||||||
"write_file": ("path",),
|
"write_file": ("path",),
|
||||||
"edit_file": ("path",),
|
"edit_file": ("path",),
|
||||||
"apply_patch": ("patch_text", "patchText", "patch"),
|
"apply_patch": ("patch_text", "patchText", "patch"),
|
||||||
|
"generate_image": ("prompt",),
|
||||||
"host_shell": ("command",),
|
"host_shell": ("command",),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4592,7 +4592,9 @@ body.bg-pattern-ascii-fireflies {
|
|||||||
width:100%; background:var(--bg); color:var(--fg);
|
width:100%; background:var(--bg); color:var(--fg);
|
||||||
border:1px solid var(--border); border-radius:6px;
|
border:1px solid var(--border); border-radius:6px;
|
||||||
padding:8px; font-family:inherit; font-size:0.95rem;
|
padding:8px; font-family:inherit; font-size:0.95rem;
|
||||||
resize:vertical; outline:none;
|
resize:both; outline:none;
|
||||||
|
field-sizing:content;
|
||||||
|
max-width:100%; box-sizing:border-box;
|
||||||
min-height:80px;
|
min-height:80px;
|
||||||
}
|
}
|
||||||
.edit-textarea:focus { border-color:var(--red); }
|
.edit-textarea:focus { border-color:var(--red); }
|
||||||
|
|||||||
@@ -771,7 +771,13 @@ function _getModal() {
|
|||||||
<button class="cal-side-nav cal-side-nav-next" id="cal-side-next" title="Next" aria-label="Next">›</button>
|
<button class="cal-side-nav cal-side-nav-next" id="cal-side-next" title="Next" aria-label="Next">›</button>
|
||||||
</div>`;
|
</div>`;
|
||||||
document.body.appendChild(_modal);
|
document.body.appendChild(_modal);
|
||||||
_modal.querySelector('#cal-close').addEventListener('click', closeCalendar);
|
_modal.querySelector('#cal-close').addEventListener('click', () => {
|
||||||
|
if (_modal.querySelector('.cal-form')) {
|
||||||
|
_render();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
closeCalendar();
|
||||||
|
});
|
||||||
_modal.querySelector('#cal-side-prev')?.addEventListener('click', (e) => {
|
_modal.querySelector('#cal-side-prev')?.addEventListener('click', (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
function streamError(payload) {
|
||||||
|
const detail = payload?.data ?? payload;
|
||||||
|
const candidates = [detail?.message, detail?.text, detail?.error?.message,
|
||||||
|
detail?.error, payload?.message, payload?.text, payload?.error?.message,
|
||||||
|
payload?.error, typeof payload === 'string' ? payload : null];
|
||||||
|
const message = candidates.find(value => typeof value === 'string' && value.trim());
|
||||||
|
return new Error(message || 'Model request failed.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Decode complete SSE frames, requiring the backend's terminal DONE event. */
|
||||||
|
export async function consumeCompareEvents(response, onEvent, onActivity) {
|
||||||
|
const reader = response.body.getReader();
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
let line = '', event = 'message', data = [], skipLF = false;
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (!done) onActivity?.();
|
||||||
|
let chunk = decoder.decode(value, { stream: !done });
|
||||||
|
if (skipLF && chunk.length) {
|
||||||
|
if (chunk.startsWith('\n')) chunk = chunk.slice(1);
|
||||||
|
skipLF = false;
|
||||||
|
}
|
||||||
|
const separators = /\r\n|\r|\n/g;
|
||||||
|
let start = 0, separator;
|
||||||
|
while ((separator = separators.exec(chunk))) {
|
||||||
|
line += chunk.slice(start, separator.index);
|
||||||
|
start = separator.index + separator[0].length;
|
||||||
|
skipLF = separator[0] === '\r' && start === chunk.length;
|
||||||
|
if (!line) {
|
||||||
|
if (data.length || event === 'error') {
|
||||||
|
const raw = data.join('\n');
|
||||||
|
if (event !== 'error' && raw === '[DONE]') return;
|
||||||
|
let payload;
|
||||||
|
try { payload = JSON.parse(raw); }
|
||||||
|
catch (_) {
|
||||||
|
if (event === 'error') throw streamError(raw);
|
||||||
|
throw new Error('Invalid model stream data.');
|
||||||
|
}
|
||||||
|
if (event === 'error' || payload?.type === 'error') throw streamError(payload);
|
||||||
|
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
|
||||||
|
throw new Error('Invalid model stream data.');
|
||||||
|
}
|
||||||
|
await onEvent(payload);
|
||||||
|
}
|
||||||
|
event = 'message';
|
||||||
|
data = [];
|
||||||
|
} else if (!line.startsWith(':')) {
|
||||||
|
const colon = line.indexOf(':');
|
||||||
|
const field = colon < 0 ? line : line.slice(0, colon);
|
||||||
|
let value = colon < 0 ? '' : line.slice(colon + 1);
|
||||||
|
if (value.startsWith(' ')) value = value.slice(1);
|
||||||
|
if (field === 'data') data.push(value);
|
||||||
|
else if (field === 'event') event = value || 'message';
|
||||||
|
}
|
||||||
|
line = '';
|
||||||
|
}
|
||||||
|
line += chunk.slice(start);
|
||||||
|
if (done) throw new Error('Stream ended before completion.');
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
try { await reader.cancel(); } catch (_) {}
|
||||||
|
reader.releaseLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-22
@@ -1,5 +1,6 @@
|
|||||||
// compare/stream.js — SSE streaming to panes
|
// compare/stream.js — SSE streaming to panes
|
||||||
import state from './state.js';
|
import state from './state.js';
|
||||||
|
import { consumeCompareEvents } from './sse.js';
|
||||||
import { addFinishBadge } from './vote.js?v=20260828resendcaldrag1';
|
import { addFinishBadge } from './vote.js?v=20260828resendcaldrag1';
|
||||||
import { getModelCost, renderAskUserCard, safeDisplayImageSrc } from '../chatRenderer.js?v=20260914metricssummary1';
|
import { getModelCost, renderAskUserCard, safeDisplayImageSrc } from '../chatRenderer.js?v=20260914metricssummary1';
|
||||||
import markdownModule from '../markdown.js';
|
import markdownModule from '../markdown.js';
|
||||||
@@ -472,25 +473,9 @@ async function streamToPane(paneIdx, sessionId, message, aiMsgEl, opts) {
|
|||||||
});
|
});
|
||||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||||
|
|
||||||
const reader = response.body.getReader();
|
await consumeCompareEvents(response,
|
||||||
const decoder = new TextDecoder();
|
(json) => {
|
||||||
let buffer = '';
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
const { done, value } = await reader.read();
|
|
||||||
if (done) break;
|
|
||||||
_resetIdleTimeout(); // any chunk = stream is alive
|
|
||||||
|
|
||||||
buffer += decoder.decode(value, { stream: true });
|
|
||||||
const lines = buffer.split('\n');
|
|
||||||
buffer = lines.pop() || '';
|
|
||||||
|
|
||||||
for (const line of lines) {
|
|
||||||
if (!line.startsWith('data: ')) continue;
|
|
||||||
const data = line.slice(6);
|
|
||||||
if (data === '[DONE]') break;
|
|
||||||
try {
|
try {
|
||||||
const json = JSON.parse(data);
|
|
||||||
if (json.type === 'metrics') {
|
if (json.type === 'metrics') {
|
||||||
metrics = json.data;
|
metrics = json.data;
|
||||||
|
|
||||||
@@ -672,7 +657,7 @@ async function streamToPane(paneIdx, sessionId, message, aiMsgEl, opts) {
|
|||||||
if (hist) hist.scrollTop = hist.scrollHeight;
|
if (hist) hist.scrollTop = hist.scrollHeight;
|
||||||
} else if (json.delta) {
|
} else if (json.delta) {
|
||||||
// Skip text deltas if we already rendered an image
|
// Skip text deltas if we already rendered an image
|
||||||
if (aiMsgEl._imageData) continue;
|
if (aiMsgEl._imageData) return;
|
||||||
// Capture TTFT on very first text delta
|
// Capture TTFT on very first text delta
|
||||||
if (!accumulated && !_ttft) _ttft = performance.now() - _timerStart;
|
if (!accumulated && !_ttft) _ttft = performance.now() - _timerStart;
|
||||||
// On first delta, destroy spinner and prepare text area
|
// On first delta, destroy spinner and prepare text area
|
||||||
@@ -701,8 +686,7 @@ async function streamToPane(paneIdx, sessionId, message, aiMsgEl, opts) {
|
|||||||
_scheduleLiveRender(target);
|
_scheduleLiveRender(target);
|
||||||
}
|
}
|
||||||
} catch (e) { console.warn('Compare stream render error:', e); }
|
} catch (e) { console.warn('Compare stream render error:', e); }
|
||||||
}
|
}, _resetIdleTimeout);
|
||||||
}
|
|
||||||
|
|
||||||
streamOk = true;
|
streamOk = true;
|
||||||
// Destroy any remaining spinner
|
// Destroy any remaining spinner
|
||||||
@@ -900,9 +884,15 @@ async function streamToPane(paneIdx, sessionId, message, aiMsgEl, opts) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.error('Compare stream error:', error);
|
console.error('Compare stream error:', error);
|
||||||
aiBody.innerHTML = '<span style="color:var(--color-error);">Error: ' + escapeHtml(error.message) + '</span>';
|
awaitingChoice = false;
|
||||||
|
const message = state._blindMode ? 'Model request failed. Try again.' : error.message;
|
||||||
|
aiBody.innerHTML = '<span style="color:var(--color-error);">Error: ' + escapeHtml(message) + '</span>';
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
if (aiMsgEl._spinner) {
|
||||||
|
aiMsgEl._spinner.destroy();
|
||||||
|
aiMsgEl._spinner = null;
|
||||||
|
}
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
_timerDone = true;
|
_timerDone = true;
|
||||||
cancelAnimationFrame(_rafId);
|
cancelAnimationFrame(_rafId);
|
||||||
|
|||||||
@@ -1195,11 +1195,12 @@ async function _fetchDependencies() {
|
|||||||
// For backends with a recipe catalog (vllm / sglang / llama_cpp),
|
// For backends with a recipe catalog (vllm / sglang / llama_cpp),
|
||||||
// append a caret button that toggles a per-row recipe panel below.
|
// append a caret button that toggles a per-row recipe panel below.
|
||||||
const hasRecipe = RECIPE_BACKENDS.has(pkg.name);
|
const hasRecipe = RECIPE_BACKENDS.has(pkg.name);
|
||||||
// Standalone recipe-caret button removed — the "Pick install
|
// The caret is the only way to open the recipe panel (the Installed ▾
|
||||||
// command" action lives inside the Installed ▾ dropdown menu
|
// menu has no recipe item), and the HW Fit / serve-card deep links
|
||||||
// (see _showDepMenu) so each row only has ONE caret to click.
|
// open it by clicking this caret (#6747).
|
||||||
// Kept the variable so downstream concat code stays the same.
|
const recipeCaret = hasRecipe
|
||||||
const recipeCaret = '';
|
? `<button class="cookbook-dep-tag cookbook-dep-recipe-caret" data-dep-recipe-toggle="${esc(pkg.name)}" title="Pick a model to see the exact install commands" aria-expanded="false" style="background:none;border:1px solid var(--border);padding:2px 6px;display:inline-flex;align-items:center;cursor:pointer;"><svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" style="transition:transform 0.15s"><polyline points="6 9 12 15 18 9"/></svg></button>`
|
||||||
|
: '';
|
||||||
const recipePanel = hasRecipe ? _recipePanelHtml(pkg.name) : '';
|
const recipePanel = hasRecipe ? _recipePanelHtml(pkg.name) : '';
|
||||||
// When llama_cpp (or any future engine) reports build_deps_missing
|
// When llama_cpp (or any future engine) reports build_deps_missing
|
||||||
// from its system_prereqs probe, surface a one-tap install button
|
// from its system_prereqs probe, surface a one-tap install button
|
||||||
|
|||||||
@@ -5232,7 +5232,7 @@ import { attachColorPicker } from './colorPicker.js?v=20260910eyedropper1';
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function _handleAttachUpload(e) {
|
async function _handleAttachUpload(e) {
|
||||||
const files = e.target.files;
|
const files = Array.from(e.target.files || []);
|
||||||
e.target.value = ''; // reset for next upload
|
e.target.value = ''; // reset for next upload
|
||||||
await _uploadComposeFiles(files);
|
await _uploadComposeFiles(files);
|
||||||
}
|
}
|
||||||
@@ -5963,7 +5963,7 @@ import { attachColorPicker } from './colorPicker.js?v=20260910eyedropper1';
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function _handleMarkdownImageUpload(e) {
|
async function _handleMarkdownImageUpload(e) {
|
||||||
const files = e.target.files;
|
const files = Array.from(e.target.files || []);
|
||||||
e.target.value = '';
|
e.target.value = '';
|
||||||
await _uploadMarkdownImages(files);
|
await _uploadMarkdownImages(files);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2650,7 +2650,14 @@ export function openEmailLibrary(opts = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wire events
|
// Wire events
|
||||||
document.getElementById('email-lib-close').addEventListener('click', closeEmailLibrary);
|
document.getElementById('email-lib-close').addEventListener('click', () => {
|
||||||
|
const modal = document.getElementById('email-lib-modal');
|
||||||
|
if (modal?.classList.contains('email-reading') || modal?.querySelector('.doclib-card-expanded')) {
|
||||||
|
_exitEmailReaderModeForList();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
closeEmailLibrary();
|
||||||
|
});
|
||||||
document.getElementById('email-settings-header-back')?.addEventListener('click', _hideEmailSettingsPage);
|
document.getElementById('email-settings-header-back')?.addEventListener('click', _hideEmailSettingsPage);
|
||||||
|
|
||||||
// Clicking the modal header (anywhere except buttons/inputs) collapses
|
// Clicking the modal header (anywhere except buttons/inputs) collapses
|
||||||
|
|||||||
@@ -73,16 +73,24 @@ function _isDangerousSrcset(value) {
|
|||||||
|
|
||||||
// Escape + linkify URLs and email addresses. Returns innerHTML-safe markup.
|
// Escape + linkify URLs and email addresses. Returns innerHTML-safe markup.
|
||||||
export function _escLinkify(text) {
|
export function _escLinkify(text) {
|
||||||
const escaped = _esc(text);
|
const raw = String(text || '');
|
||||||
// URLs: http(s)://... or www....
|
// Match raw text once so generated anchors are never linkified again.
|
||||||
const urlRe = /\b((?:https?:\/\/|www\.)[^\s<>"']+[^\s<>"'.,;:!?)\]])/g;
|
const tokenRe = /\b((?:https?:\/\/|www\.)[^\s<>"']+[^\s<>"'.,;:!?)\]])|\b([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,})\b/g;
|
||||||
const mailRe = /\b([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,})\b/g;
|
const parts = [];
|
||||||
return escaped
|
let offset = 0;
|
||||||
.replace(urlRe, (m) => {
|
for (const match of raw.matchAll(tokenRe)) {
|
||||||
const href = m.startsWith('www.') ? `https://${m}` : m;
|
parts.push(_esc(raw.slice(offset, match.index)));
|
||||||
return `<a href="${_attrEsc(href)}" target="_blank" rel="noopener noreferrer">${m}</a>`;
|
const token = match[0];
|
||||||
})
|
const isUrl = !!match[1];
|
||||||
.replace(mailRe, (m) => `<a href="${_attrEsc(`mailto:${m}`)}">${m}</a>`);
|
const href = isUrl
|
||||||
|
? (token.startsWith('www.') ? `https://${token}` : token)
|
||||||
|
: `mailto:${token}`;
|
||||||
|
const attrs = isUrl ? ' target="_blank" rel="noopener noreferrer"' : '';
|
||||||
|
parts.push(`<a href="${_attrEsc(_esc(href))}"${attrs}>${_esc(token)}</a>`);
|
||||||
|
offset = match.index + token.length;
|
||||||
|
}
|
||||||
|
parts.push(_esc(raw.slice(offset)));
|
||||||
|
return parts.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull display name out of "Name <email@x>"; fallback to local-part of
|
// Pull display name out of "Name <email@x>"; fallback to local-part of
|
||||||
|
|||||||
@@ -5759,7 +5759,20 @@ function _updateBrushCursor(e) {
|
|||||||
state.cursorEl.style.height = diameterY + 'px';
|
state.cursorEl.style.height = diameterY + 'px';
|
||||||
}
|
}
|
||||||
state.cursorEl.classList.toggle('ge-brush-cursor--pixel', pixelBrush);
|
state.cursorEl.classList.toggle('ge-brush-cursor--pixel', pixelBrush);
|
||||||
state.cursorEl.style.transform = `translate3d(${clientX - diameterX / 2}px, ${clientY - diameterY / 2}px, 0)`;
|
// Updating an inline style on every move invalidates document-wide :has()
|
||||||
|
// selectors. A paused animation keeps the same transform in the rendering
|
||||||
|
// pipeline without mutating the cursor's style attribute on every frame.
|
||||||
|
const cursorTransform = `translate3d(${clientX - diameterX / 2}px, ${clientY - diameterY / 2}px, 0)`;
|
||||||
|
if (!state.cursorEl._positionAnimation || state.cursorEl._positionAnimation.playState === 'idle') {
|
||||||
|
state.cursorEl._positionAnimation = state.cursorEl.animate(
|
||||||
|
[{ transform: cursorTransform }, { transform: cursorTransform }],
|
||||||
|
{ duration: 1, fill: 'both' },
|
||||||
|
);
|
||||||
|
state.cursorEl._positionAnimation.pause();
|
||||||
|
state.cursorEl._positionAnimation.currentTime = 0;
|
||||||
|
} else {
|
||||||
|
state.cursorEl._positionAnimation.effect.setKeyframes([{ transform: cursorTransform }, { transform: cursorTransform }]);
|
||||||
|
}
|
||||||
if (state.cursorEl.style.display !== 'block') {
|
if (state.cursorEl.style.display !== 'block') {
|
||||||
state.cursorEl.style.display = 'block';
|
state.cursorEl.style.display = 'block';
|
||||||
}
|
}
|
||||||
@@ -14061,7 +14074,20 @@ export function closeEditor(options = {}) {
|
|||||||
const h = state.editorDocClickHandlers.pop();
|
const h = state.editorDocClickHandlers.pop();
|
||||||
try { document.removeEventListener('click', h); } catch {}
|
try { document.removeEventListener('click', h); } catch {}
|
||||||
}
|
}
|
||||||
if (state.cursorEl) { state.cursorEl.remove(); state.cursorEl = null; }
|
if (_cursorRaf) { cancelAnimationFrame(_cursorRaf); _cursorRaf = null; }
|
||||||
|
_pendingCursorEvent = null;
|
||||||
|
_lastCursorRect = null;
|
||||||
|
state._lastCursorDiameterX = null;
|
||||||
|
state._lastCursorDiameterY = null;
|
||||||
|
state._lastCursorBorderColor = null;
|
||||||
|
if (state.cursorEl) {
|
||||||
|
if (state.cursorEl._positionAnimation) {
|
||||||
|
try { state.cursorEl._positionAnimation.cancel(); } catch {}
|
||||||
|
state.cursorEl._positionAnimation = null;
|
||||||
|
}
|
||||||
|
state.cursorEl.remove();
|
||||||
|
state.cursorEl = null;
|
||||||
|
}
|
||||||
try { document.getElementById('ge-cursor-layer')?.remove(); } catch {}
|
try { document.getElementById('ge-cursor-layer')?.remove(); } catch {}
|
||||||
// Tear down all floating popups + the dock so closing the editor
|
// Tear down all floating popups + the dock so closing the editor
|
||||||
// doesn't leave stale chips/panels behind on top of the gallery.
|
// doesn't leave stale chips/panels behind on top of the gallery.
|
||||||
|
|||||||
+4
-2
@@ -801,7 +801,8 @@ async function _sendRoundRobin(msg, box) {
|
|||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ messages: [{
|
body: JSON.stringify({ messages: [{
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: `[${m._groupName || m.display}]: ${response}`
|
content: `[${m._groupName || m.display}]: ${response}`,
|
||||||
|
metadata: { group_peer: true, group_model: m._groupName || m.display, model: m.mid }
|
||||||
}]}),
|
}]}),
|
||||||
});
|
});
|
||||||
} catch (e) { console.warn('[group] sync failed:', e); }
|
} catch (e) { console.warn('[group] sync failed:', e); }
|
||||||
@@ -828,7 +829,8 @@ async function _syncAllResponses(holders) {
|
|||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ messages: [{
|
body: JSON.stringify({ messages: [{
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: `[${model._groupName || model.display}]: ${response}`
|
content: `[${model._groupName || model.display}]: ${response}`,
|
||||||
|
metadata: { group_peer: true, group_model: model._groupName || model.display, model: model.mid }
|
||||||
}]}),
|
}]}),
|
||||||
});
|
});
|
||||||
} catch (e) { /* silent */ }
|
} catch (e) { /* silent */ }
|
||||||
|
|||||||
+5
-3
@@ -163,11 +163,13 @@ function buildCategoryChips() {
|
|||||||
const container = document.getElementById('memory-category-filters');
|
const container = document.getElementById('memory-category-filters');
|
||||||
if (!container) return;
|
if (!container) return;
|
||||||
|
|
||||||
|
const cats = new Set(memories.map(m => m.category || 'fact'));
|
||||||
|
if (activeCategory !== 'all' && !cats.has(activeCategory)) activeCategory = 'all';
|
||||||
|
|
||||||
// Hide the chip row entirely when there are no memories — no point showing
|
// Hide the chip row entirely when there are no memories — no point showing
|
||||||
// an "all" chip with nothing to filter.
|
// an "all" chip with nothing to filter.
|
||||||
if (!memories.length) { container.innerHTML = ''; return; }
|
if (!memories.length) { container.innerHTML = ''; return; }
|
||||||
|
|
||||||
const cats = new Set(memories.map(m => m.category || 'fact'));
|
|
||||||
const sorted = ['all', ...Array.from(cats).sort()];
|
const sorted = ['all', ...Array.from(cats).sort()];
|
||||||
const counts = memories.reduce((result, memory) => {
|
const counts = memories.reduce((result, memory) => {
|
||||||
const category = memory.category || 'fact';
|
const category = memory.category || 'fact';
|
||||||
@@ -277,6 +279,7 @@ export function openMemoryModal(tabName = 'browse') {
|
|||||||
else if (m.default?.loadSkills) m.default.loadSkills(true);
|
else if (m.default?.loadSkills) m.default.loadSkills(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (mode === 'memory') return loadMemories();
|
||||||
}
|
}
|
||||||
|
|
||||||
function _decodeMemoryAnchorId(memoryId) {
|
function _decodeMemoryAnchorId(memoryId) {
|
||||||
@@ -297,9 +300,8 @@ function _resetMemoryDeepLinkFilters() {
|
|||||||
// Memory search output may expose a shortened id, so prefix matching is
|
// Memory search output may expose a shortened id, so prefix matching is
|
||||||
// intentional here.
|
// intentional here.
|
||||||
export async function openMemory(memoryId, attempt = 0) {
|
export async function openMemory(memoryId, attempt = 0) {
|
||||||
openMemoryModal('browse');
|
|
||||||
_resetMemoryDeepLinkFilters();
|
_resetMemoryDeepLinkFilters();
|
||||||
await loadMemories();
|
await openMemoryModal('browse');
|
||||||
if (renderMemoryList) renderMemoryList();
|
if (renderMemoryList) renderMemoryList();
|
||||||
const wanted = _decodeMemoryAnchorId(memoryId);
|
const wanted = _decodeMemoryAnchorId(memoryId);
|
||||||
if (!wanted) return;
|
if (!wanted) return;
|
||||||
|
|||||||
+13
-2
@@ -299,7 +299,18 @@ function _renderHistoryMessage(msg, modelName) {
|
|||||||
} else {
|
} else {
|
||||||
displayContent = '';
|
displayContent = '';
|
||||||
}
|
}
|
||||||
if (msg.role === 'user') {
|
let displayRole = msg.role;
|
||||||
|
// Peer turns remain user-role context for the model, but are agent replies
|
||||||
|
// in history. Only explicit identity plus its exact prefix can opt in.
|
||||||
|
if (displayRole === 'user' && meta?.group_peer === true
|
||||||
|
&& typeof meta.group_model === 'string' && meta.group_model) {
|
||||||
|
const prefix = `[${meta.group_model}]: `;
|
||||||
|
if (displayContent.startsWith(prefix)) {
|
||||||
|
displayRole = 'assistant';
|
||||||
|
displayContent = displayContent.slice(prefix.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (displayRole === 'user') {
|
||||||
displayContent = _stripUserVisionBlocks(displayContent);
|
displayContent = _stripUserVisionBlocks(displayContent);
|
||||||
const trimmed = displayContent.trim();
|
const trimmed = displayContent.trim();
|
||||||
if (
|
if (
|
||||||
@@ -316,7 +327,7 @@ function _renderHistoryMessage(msg, modelName) {
|
|||||||
displayContent = `[Doc edit: ${docEditMatch[1]}] ${docEditMatch[3]}`;
|
displayContent = `[Doc edit: ${docEditMatch[1]}] ${docEditMatch[3]}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return _addHistoryMessageWithFullRenderer(msg.role, displayContent, modelName, meta);
|
return _addHistoryMessageWithFullRenderer(displayRole, displayContent, modelName, meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _clearHistoryPager() {
|
function _clearHistoryPager() {
|
||||||
|
|||||||
@@ -1260,6 +1260,9 @@ async function _expandSkillCard(card, name) {
|
|||||||
card._md = md || '';
|
card._md = md || '';
|
||||||
} else {
|
} else {
|
||||||
pre.textContent = 'Loading…';
|
pre.textContent = 'Loading…';
|
||||||
|
const editBtn = [...card.querySelectorAll('.skill-card-preview button')]
|
||||||
|
.find(button => button.textContent.trim() === 'Edit');
|
||||||
|
if (editBtn) editBtn.disabled = true;
|
||||||
try {
|
try {
|
||||||
const md = await _fetchSkillMarkdown(name);
|
const md = await _fetchSkillMarkdown(name);
|
||||||
pre.textContent = md || '(empty)';
|
pre.textContent = md || '(empty)';
|
||||||
@@ -1267,6 +1270,8 @@ async function _expandSkillCard(card, name) {
|
|||||||
card._md = md;
|
card._md = md;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
pre.textContent = `Failed to load SKILL.md: ${e.message || 'Unknown error'}`;
|
pre.textContent = `Failed to load SKILL.md: ${e.message || 'Unknown error'}`;
|
||||||
|
} finally {
|
||||||
|
if (editBtn) editBtn.disabled = !card._mdLoaded;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1275,6 +1280,9 @@ async function _expandSkillCard(card, name) {
|
|||||||
// Swap the read-only <pre> for an editable <textarea> (and back). The
|
// Swap the read-only <pre> for an editable <textarea> (and back). The
|
||||||
// Edit button toggles; a Save button commits via the markdown endpoint.
|
// Edit button toggles; a Save button commits via the markdown endpoint.
|
||||||
function _toggleSkillEdit(card, name) {
|
function _toggleSkillEdit(card, name) {
|
||||||
|
// A loading/error preview is not source text. Menu entry points must obey
|
||||||
|
// the same readiness gate as the disabled footer control.
|
||||||
|
if (!card._mdLoaded) return;
|
||||||
const preview = card.querySelector('.skill-card-preview');
|
const preview = card.querySelector('.skill-card-preview');
|
||||||
if (!preview) return;
|
if (!preview) return;
|
||||||
const existing = preview.querySelector('.skill-md-editor');
|
const existing = preview.querySelector('.skill-md-editor');
|
||||||
@@ -1288,6 +1296,9 @@ function _toggleSkillEdit(card, name) {
|
|||||||
ta.className = 'skill-md-editor';
|
ta.className = 'skill-md-editor';
|
||||||
ta.spellcheck = false;
|
ta.spellcheck = false;
|
||||||
ta.value = (card._md != null ? card._md : (pre ? pre.textContent : '')) || '';
|
ta.value = (card._md != null ? card._md : (pre ? pre.textContent : '')) || '';
|
||||||
|
// Let the source set the intrinsic height; the card's existing flex/max
|
||||||
|
// height keeps long files bounded and the Save footer reachable.
|
||||||
|
ta.rows = Math.max(ta.rows, ta.value.split(/\r\n|\r|\n/).length);
|
||||||
ta.addEventListener('click', (e) => e.stopPropagation());
|
ta.addEventListener('click', (e) => e.stopPropagation());
|
||||||
if (pre) pre.style.display = 'none';
|
if (pre) pre.style.display = 'none';
|
||||||
preview.insertBefore(ta, preview.querySelector('.doclib-card-expanded-actions'));
|
preview.insertBefore(ta, preview.querySelector('.doclib-card-expanded-actions'));
|
||||||
|
|||||||
+2
-1
@@ -7,7 +7,7 @@
|
|||||||
// - Other static assets (images/fonts/libs): cache-first with bg refresh.
|
// - Other static assets (images/fonts/libs): cache-first with bg refresh.
|
||||||
// - API / non-GET: never cached.
|
// - API / non-GET: never cached.
|
||||||
// Bump CACHE_NAME whenever the precache list or SW logic changes.
|
// Bump CACHE_NAME whenever the precache list or SW logic changes.
|
||||||
const CACHE_NAME = 'odysseus-v1114-pr40-editor-precache';
|
const CACHE_NAME = 'odysseus-v1115-pr40-editor-precache';
|
||||||
|
|
||||||
// KaTeX resolves these from its own stylesheet, so caching the CSS without them
|
// KaTeX resolves these from its own stylesheet, so caching the CSS without them
|
||||||
// gives offline math fallback glyphs instead of proper typesetting.
|
// gives offline math fallback glyphs instead of proper typesetting.
|
||||||
@@ -38,6 +38,7 @@ const KATEX_FONTS = [
|
|||||||
// Both are fetched at install time, in the background. Entries must match the
|
// Both are fetched at install time, in the background. Entries must match the
|
||||||
// exact URL the browser requests, query string included.
|
// exact URL the browser requests, query string included.
|
||||||
const PRECACHE = [
|
const PRECACHE = [
|
||||||
|
'/static/js/compare/sse.js',
|
||||||
'/',
|
'/',
|
||||||
'/static/css/00-tokens.css?v=20260929csssplit',
|
'/static/css/00-tokens.css?v=20260929csssplit',
|
||||||
'/static/css/01-agent-chat.css?v=20260929csssplit',
|
'/static/css/01-agent-chat.css?v=20260929csssplit',
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
"""Gallery CLI tag filters match both sources and stack like the web library."""
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
from core.database import Base, GalleryAlbum, GalleryImage
|
||||||
|
from tests.helpers.cli_loader import load_script
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def gallery_cli(monkeypatch, tmp_path):
|
||||||
|
engine = create_engine(f"sqlite:///{tmp_path / 'gallery.db'}")
|
||||||
|
Base.metadata.create_all(engine)
|
||||||
|
sessions = sessionmaker(bind=engine)
|
||||||
|
with sessions() as db:
|
||||||
|
db.add(GalleryAlbum(id='sea', name='Sea'))
|
||||||
|
for index, (uid, tags, ai_tags, favorite, album) in enumerate([
|
||||||
|
('manual', 'sunset,beach', '', True, 'sea'),
|
||||||
|
('ai', '', 'Beach,sunset', False, 'sea'),
|
||||||
|
('split', 'sunset', 'beach', True, None),
|
||||||
|
('one', 'sunset', '', False, None),
|
||||||
|
('other', 'forest', 'moss', False, None),
|
||||||
|
('inactive', 'sunset,beach', 'sunset,beach', True, 'sea'),
|
||||||
|
]):
|
||||||
|
db.add(GalleryImage(id=uid, filename=uid+'.png', tags=tags, ai_tags=ai_tags,
|
||||||
|
favorite=favorite, album_id=album, is_active=uid!='inactive',
|
||||||
|
created_at=datetime(2026, 1, 1) + timedelta(minutes=index)))
|
||||||
|
db.commit()
|
||||||
|
cli = load_script('odysseus-gallery')
|
||||||
|
monkeypatch.setattr(cli, 'SessionLocal', sessions)
|
||||||
|
output = []
|
||||||
|
monkeypatch.setattr(cli, 'emit', lambda rows, args: output.extend(rows))
|
||||||
|
|
||||||
|
def run(*argv):
|
||||||
|
output.clear()
|
||||||
|
args = cli._build_parser().parse_args(['list', *argv])
|
||||||
|
args.func(args)
|
||||||
|
return [row['id'] for row in output]
|
||||||
|
|
||||||
|
yield run
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('tag,expected', [
|
||||||
|
('sunset', ['one', 'split', 'ai', 'manual']),
|
||||||
|
('beach,sunset', ['split', 'ai', 'manual']),
|
||||||
|
('sunset,beach', ['split', 'ai', 'manual']),
|
||||||
|
(', sunset, , BEACH ,', ['split', 'ai', 'manual']),
|
||||||
|
('forest', ['other']),
|
||||||
|
('no-match', []),
|
||||||
|
(' , ', ['other', 'one', 'split', 'ai', 'manual']),
|
||||||
|
('sun', ['one', 'split', 'ai', 'manual']),
|
||||||
|
])
|
||||||
|
def test_tag_sources_and_stacking(gallery_cli, tag, expected):
|
||||||
|
assert gallery_cli('--tag', tag) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('options,expected', [
|
||||||
|
([], ['other', 'one', 'split', 'ai', 'manual']),
|
||||||
|
(['--tag', 'beach,sunset', '--favorites'], ['split', 'manual']),
|
||||||
|
(['--tag', 'beach,sunset', '--album', 'Sea'], ['ai', 'manual']),
|
||||||
|
(['--tag', 'beach', '--limit', '1'], ['split']),
|
||||||
|
])
|
||||||
|
def test_tag_filters_preserve_other_list_options(gallery_cli, options, expected):
|
||||||
|
assert gallery_cli(*options) == expected
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
"""The bounded arrival batch is displayed by Date, rather than arrival ID."""
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from tests.cli.test_mail_cli_read_empty_fetch import _load_mail_cli
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('dates,expected', [
|
||||||
|
(['Wed, 03 Jun 2026 12:00:00 +0000', 'Mon, 01 Jun 2026 09:00:00 +0000'], ['1', '2']),
|
||||||
|
(['Wed, 03 Jun 2026 12:00:00 +0000', 'Wed, 03 Jun 2026 13:00:00 +0200'], ['1', '2']),
|
||||||
|
(['Wed, 03 Jun 2026 12:00:00', 'Wed, 03 Jun 2026 11:00:00 +0000'], ['1', '2']),
|
||||||
|
(['Wed, 03 Jun 2026 12:00:00 +0000', None, 'not a date'], ['1', '3', '2']),
|
||||||
|
(['Wed, 03 Jun 2026 12:00:00 +0000', 'Wed, 03 Jun 2026 14:00:00 +0200'], ['2', '1']),
|
||||||
|
(['Wed, 03 Jun 2026 12:00:00 +0000', 'Thu, 04 Jun 2026 12:00:00 +0000'], ['2', '1']),
|
||||||
|
(['Wed, 31 Dec 1969 12:00:00 +0000', None], ['1', '2']),
|
||||||
|
])
|
||||||
|
def test_list_sorts_parsed_dates_without_changing_row_schema(monkeypatch, dates, expected):
|
||||||
|
cli = _load_mail_cli(monkeypatch)
|
||||||
|
fetched = []
|
||||||
|
|
||||||
|
class Connection:
|
||||||
|
def select(self, folder, readonly=True):
|
||||||
|
assert readonly is True
|
||||||
|
return 'OK', [str(len(dates)).encode()]
|
||||||
|
|
||||||
|
def search(self, *_):
|
||||||
|
return 'OK', [' '.join(str(i+1) for i in range(len(dates))).encode()]
|
||||||
|
|
||||||
|
def fetch(self, uid, _):
|
||||||
|
fetched.append(uid.decode())
|
||||||
|
date = dates[int(uid)-1]
|
||||||
|
raw = 'From: Sender <sender@example.com>\r\nSubject: Item '+uid.decode()+'\r\n'
|
||||||
|
if date is not None:
|
||||||
|
raw += 'Date: '+date+'\r\n'
|
||||||
|
return 'OK', [(b'1 (FLAGS (\\Seen) RFC822.HEADER)', (raw+'\r\n').encode())]
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def imap(_):
|
||||||
|
yield Connection()
|
||||||
|
|
||||||
|
rows = []
|
||||||
|
monkeypatch.setattr(cli, '_imap', imap)
|
||||||
|
monkeypatch.setattr(cli, 'emit', lambda value, args: rows.extend(value))
|
||||||
|
cli.cmd_list(SimpleNamespace(account='fixture', folder='INBOX', limit=20))
|
||||||
|
assert [row['uid'] for row in rows] == expected
|
||||||
|
assert fetched == [str(i) for i in range(len(dates), 0, -1)]
|
||||||
|
assert all(set(row) == {'uid', 'date', 'from_addr', 'from_name', 'subject', 'is_read', 'is_answered'} for row in rows)
|
||||||
|
assert all(row['is_read'] and not row['is_answered'] for row in rows)
|
||||||
|
|
||||||
|
|
||||||
|
def test_list_still_limits_the_arrival_batch(monkeypatch):
|
||||||
|
cli = _load_mail_cli(monkeypatch)
|
||||||
|
fetched = []
|
||||||
|
class Connection:
|
||||||
|
def select(self, *_args, **_kwargs):return 'OK', []
|
||||||
|
def search(self, *_):return 'OK', [b'1 2 3']
|
||||||
|
def fetch(self, uid, _):
|
||||||
|
fetched.append(uid)
|
||||||
|
return 'OK', [(b'3 (FLAGS () RFC822.HEADER)', b'From: x@example.com\r\nSubject: bounded\r\n\r\n')]
|
||||||
|
@contextmanager
|
||||||
|
def imap(_):yield Connection()
|
||||||
|
monkeypatch.setattr(cli, '_imap', imap)
|
||||||
|
monkeypatch.setattr(cli, 'emit', lambda *_:None)
|
||||||
|
cli.cmd_list(SimpleNamespace(account='fixture', folder='INBOX', limit=1))
|
||||||
|
assert fetched == [b'3']
|
||||||
@@ -46,3 +46,480 @@ test('brush cursor matches the rendered brush diameter and replaces the crosshai
|
|||||||
await expect.poll(async () => (await cursor.boundingBox()).width)
|
await expect.poll(async () => (await cursor.boundingBox()).width)
|
||||||
.toBeCloseTo(measurements.expectedWidth * 2, 1);
|
.toBeCloseTo(measurements.expectedWidth * 2, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('brush cursor follows the pointer before, during, and after a stroke and cancels animations on close', async ({ page }) => {
|
||||||
|
await openBlankEditor(page, { width: 400, height: 300 });
|
||||||
|
await selectTool(page, 'brush');
|
||||||
|
const canvas = await page.locator('.ge-main-canvas').boundingBox();
|
||||||
|
const cursor = page.locator('.ge-brush-cursor');
|
||||||
|
const checkCenter = async (x, y) => {
|
||||||
|
await expect.poll(async () => {
|
||||||
|
const box = await cursor.boundingBox();
|
||||||
|
return box ? Math.max(Math.abs(box.x + box.width / 2 - x), Math.abs(box.y + box.height / 2 - y)) : Infinity;
|
||||||
|
}).toBeLessThan(1);
|
||||||
|
};
|
||||||
|
const a = { x: canvas.x + canvas.width * 0.3, y: canvas.y + canvas.height * 0.4 };
|
||||||
|
const b = { x: canvas.x + canvas.width * 0.7, y: canvas.y + canvas.height * 0.6 };
|
||||||
|
await page.mouse.move(a.x, a.y);
|
||||||
|
await checkCenter(a.x, a.y);
|
||||||
|
await page.mouse.down();
|
||||||
|
await page.mouse.move(b.x, b.y, { steps: 8 });
|
||||||
|
await checkCenter(b.x, b.y);
|
||||||
|
await page.mouse.up();
|
||||||
|
await page.mouse.move(a.x, a.y);
|
||||||
|
await checkCenter(a.x, a.y);
|
||||||
|
|
||||||
|
// 1. Ensure at least one animation exists before checking cancellation
|
||||||
|
// 2. Verify the actual cursor-position animation is paused while active
|
||||||
|
const activeAnimation = await page.evaluate(() => {
|
||||||
|
const cursorEl = document.querySelector('.ge-brush-cursor');
|
||||||
|
const anims = cursorEl ? cursorEl.getAnimations() : [];
|
||||||
|
return {
|
||||||
|
count: anims.length,
|
||||||
|
hasPositionAnim: Boolean(cursorEl?._positionAnimation),
|
||||||
|
positionAnimPaused: cursorEl?._positionAnimation?.playState === 'paused',
|
||||||
|
positionAnimCurrentTime: cursorEl?._positionAnimation?.currentTime,
|
||||||
|
allPaused: anims.length > 0 && anims.every(a => a.playState === 'paused'),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(activeAnimation.count).toBeGreaterThanOrEqual(1);
|
||||||
|
expect(activeAnimation.hasPositionAnim).toBe(true);
|
||||||
|
expect(activeAnimation.positionAnimPaused).toBe(true);
|
||||||
|
expect(activeAnimation.positionAnimCurrentTime).toBe(0);
|
||||||
|
expect(activeAnimation.allPaused).toBe(true);
|
||||||
|
|
||||||
|
// 3. Verify it is canceled when the editor closes
|
||||||
|
// 4. Ensure temporary test-only references are cleaned up
|
||||||
|
const closeOutcome = await page.evaluate(async () => {
|
||||||
|
const cursorEl = document.querySelector('.ge-brush-cursor');
|
||||||
|
const anims = cursorEl ? cursorEl.getAnimations() : [];
|
||||||
|
const editor = await window.__photoEditorImport('/static/js/galleryEditor.js');
|
||||||
|
editor.closeEditor({ force: true });
|
||||||
|
return {
|
||||||
|
trackedCount: anims.length,
|
||||||
|
allIdle: anims.length > 0 && anims.every(a => a.playState === 'idle'),
|
||||||
|
states: anims.map(a => a.playState),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
await expect(cursor).toHaveCount(0);
|
||||||
|
expect(closeOutcome.trackedCount).toBeGreaterThanOrEqual(1);
|
||||||
|
expect(closeOutcome.allIdle).toBe(true);
|
||||||
|
expect(closeOutcome.states.every(s => s === 'idle')).toBe(true);
|
||||||
|
|
||||||
|
// Verify no temporary references linger on window
|
||||||
|
const windowClean = await page.evaluate(() => {
|
||||||
|
return !('__cursorAnimationsForTest' in window) && !('__styleTransformMutations' in window);
|
||||||
|
});
|
||||||
|
expect(windowClean).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('steady pointer movement does not perform repeated inline style-transform writes', async ({ page }) => {
|
||||||
|
await openBlankEditor(page, { width: 400, height: 300 });
|
||||||
|
await selectTool(page, 'brush');
|
||||||
|
|
||||||
|
const canvas = await page.locator('.ge-main-canvas').boundingBox();
|
||||||
|
const cursor = page.locator('.ge-brush-cursor');
|
||||||
|
|
||||||
|
// Warm up the cursor first: move pointer onto canvas
|
||||||
|
const startX = canvas.x + canvas.width * 0.25;
|
||||||
|
const startY = canvas.y + canvas.height * 0.25;
|
||||||
|
await page.mouse.move(startX, startY);
|
||||||
|
await expect(cursor).toBeVisible();
|
||||||
|
|
||||||
|
// Observe style attribute mutations during steady pointer movement.
|
||||||
|
// Brush size, color, and mode remain unchanged throughout.
|
||||||
|
await page.evaluate(() => {
|
||||||
|
const el = document.querySelector('.ge-brush-cursor');
|
||||||
|
window.__styleMutationRecords = [];
|
||||||
|
const observer = new MutationObserver((mutations) => {
|
||||||
|
for (const m of mutations) {
|
||||||
|
if (m.type === 'attributes' && m.attributeName === 'style') {
|
||||||
|
const oldStyle = m.oldValue || '';
|
||||||
|
const currentStyle = el.getAttribute('style') || '';
|
||||||
|
// Track mutations where style-transform was written inline
|
||||||
|
if (el.style.transform || currentStyle.includes('transform:') || oldStyle.includes('transform:')) {
|
||||||
|
window.__styleMutationRecords.push({
|
||||||
|
oldStyle,
|
||||||
|
currentStyle,
|
||||||
|
inlineTransform: el.style.transform,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
observer.observe(el, { attributes: true, attributeFilter: ['style'], attributeOldValue: true });
|
||||||
|
window.__styleMutationObserver = observer;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Preserve actual mouse interactions: steady movement across 10 steps
|
||||||
|
const endX = canvas.x + canvas.width * 0.75;
|
||||||
|
const endY = canvas.y + canvas.height * 0.75;
|
||||||
|
await page.mouse.move(endX, endY, { steps: 10 });
|
||||||
|
|
||||||
|
// Confirm the cursor accurately tracked the pointer
|
||||||
|
await expect.poll(async () => {
|
||||||
|
const box = await cursor.boundingBox();
|
||||||
|
return box ? Math.max(Math.abs(box.x + box.width / 2 - endX), Math.abs(box.y + box.height / 2 - endY)) : Infinity;
|
||||||
|
}).toBeLessThan(1);
|
||||||
|
|
||||||
|
// Collect observation results and clean up observer and globals
|
||||||
|
const { mutationCount, inlineTransform, animCount } = await page.evaluate(() => {
|
||||||
|
window.__styleMutationObserver?.disconnect();
|
||||||
|
delete window.__styleMutationObserver;
|
||||||
|
const records = window.__styleMutationRecords || [];
|
||||||
|
delete window.__styleMutationRecords;
|
||||||
|
const el = document.querySelector('.ge-brush-cursor');
|
||||||
|
return {
|
||||||
|
mutationCount: records.length,
|
||||||
|
inlineTransform: el?.style.transform || '',
|
||||||
|
animCount: el?.getAnimations?.().length || 0,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// The optimized implementation uses paused Web Animations keyframes, avoiding repeated
|
||||||
|
// inline style-transform writes that trigger expensive :has() document invalidations.
|
||||||
|
expect(mutationCount).toBe(0);
|
||||||
|
expect(inlineTransform).toBe('');
|
||||||
|
expect(animCount).toBeGreaterThanOrEqual(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('cross-tool cursor positioning, sizing, and styling across brush, eraser, pixel, lasso, inpaint, liquify, and clone', async ({ page }) => {
|
||||||
|
await openBlankEditor(page, { width: 400, height: 300 });
|
||||||
|
const canvas = await page.locator('.ge-main-canvas').boundingBox();
|
||||||
|
const cursor = page.locator('.ge-brush-cursor');
|
||||||
|
|
||||||
|
const checkCenter = async (x, y) => {
|
||||||
|
await expect.poll(async () => {
|
||||||
|
const box = await cursor.boundingBox();
|
||||||
|
return box ? Math.max(Math.abs(box.x + box.width / 2 - x), Math.abs(box.y + box.height / 2 - y)) : Infinity;
|
||||||
|
}).toBeLessThan(1);
|
||||||
|
};
|
||||||
|
|
||||||
|
const midX = canvas.x + canvas.width / 2;
|
||||||
|
const midY = canvas.y + canvas.height / 2;
|
||||||
|
|
||||||
|
// 1. Brush tool
|
||||||
|
await selectTool(page, 'brush');
|
||||||
|
await page.mouse.move(midX, midY);
|
||||||
|
await expect(cursor).toBeVisible();
|
||||||
|
await checkCenter(midX, midY);
|
||||||
|
let props = await page.evaluate(() => {
|
||||||
|
const el = document.querySelector('.ge-brush-cursor');
|
||||||
|
return {
|
||||||
|
isPixel: el.classList.contains('ge-brush-cursor--pixel'),
|
||||||
|
display: el.style.display,
|
||||||
|
borderStyle: el.style.borderStyle,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(props.isPixel).toBe(false);
|
||||||
|
expect(props.display).toBe('block');
|
||||||
|
|
||||||
|
// 2. Eraser tool
|
||||||
|
await selectTool(page, 'eraser');
|
||||||
|
await page.mouse.move(midX + 10, midY + 10);
|
||||||
|
await checkCenter(midX + 10, midY + 10);
|
||||||
|
props = await page.evaluate(() => {
|
||||||
|
const el = document.querySelector('.ge-brush-cursor');
|
||||||
|
return {
|
||||||
|
borderColor: el.style.borderColor,
|
||||||
|
isPixel: el.classList.contains('ge-brush-cursor--pixel'),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(props.borderColor).toBe('rgba(255, 255, 255, 0.6)');
|
||||||
|
expect(props.isPixel).toBe(false);
|
||||||
|
|
||||||
|
// 3. Pixel brush (via tool / preset)
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const { state } = await import('/static/js/editor/state.js');
|
||||||
|
state.tool = 'pixel';
|
||||||
|
state.brushPixel = true;
|
||||||
|
state.brushSize = 16;
|
||||||
|
});
|
||||||
|
await page.mouse.move(midX - 10, midY - 10);
|
||||||
|
await checkCenter(midX - 10, midY - 10);
|
||||||
|
props = await page.evaluate(() => {
|
||||||
|
const el = document.querySelector('.ge-brush-cursor');
|
||||||
|
return {
|
||||||
|
isPixel: el.classList.contains('ge-brush-cursor--pixel'),
|
||||||
|
width: parseFloat(el.style.width),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(props.isPixel).toBe(true);
|
||||||
|
|
||||||
|
// 4. Lasso tool
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const { state } = await import('/static/js/editor/state.js');
|
||||||
|
state.tool = 'lasso';
|
||||||
|
state.brushPixel = false;
|
||||||
|
});
|
||||||
|
await page.mouse.move(midX + 20, midY - 10);
|
||||||
|
await checkCenter(midX + 20, midY - 10);
|
||||||
|
props = await page.evaluate(() => {
|
||||||
|
const el = document.querySelector('.ge-brush-cursor');
|
||||||
|
return {
|
||||||
|
borderColor: el.style.borderColor,
|
||||||
|
background: el.style.background,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(props.borderColor).toBe('rgba(255, 255, 255, 0.85)');
|
||||||
|
expect(props.background).toBe('rgba(0, 0, 0, 0.15)');
|
||||||
|
|
||||||
|
// 5. Inpaint tool (paint mode and erase mode)
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const { state } = await import('/static/js/editor/state.js');
|
||||||
|
state.tool = 'inpaint';
|
||||||
|
state.inpaintEraseMode = false;
|
||||||
|
});
|
||||||
|
await page.mouse.move(midX, midY);
|
||||||
|
await checkCenter(midX, midY);
|
||||||
|
props = await page.evaluate(() => {
|
||||||
|
const el = document.querySelector('.ge-brush-cursor');
|
||||||
|
return {
|
||||||
|
borderColor: el.style.borderColor,
|
||||||
|
background: el.style.background,
|
||||||
|
borderStyle: el.style.borderStyle,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(props.borderColor).toBe('rgba(255, 80, 80, 0.8)');
|
||||||
|
expect(props.borderStyle).toBe('solid');
|
||||||
|
|
||||||
|
// Inpaint erase mode
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const { state } = await import('/static/js/editor/state.js');
|
||||||
|
state.inpaintEraseMode = true;
|
||||||
|
});
|
||||||
|
await page.mouse.move(midX + 5, midY + 5);
|
||||||
|
props = await page.evaluate(() => {
|
||||||
|
const el = document.querySelector('.ge-brush-cursor');
|
||||||
|
return {
|
||||||
|
borderColor: el.style.borderColor,
|
||||||
|
borderStyle: el.style.borderStyle,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(props.borderColor).toBe('rgba(255, 255, 255, 0.9)');
|
||||||
|
expect(props.borderStyle).toBe('dashed');
|
||||||
|
|
||||||
|
// 6. Liquify tool
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const { state } = await import('/static/js/editor/state.js');
|
||||||
|
state.tool = 'liquify';
|
||||||
|
state.liquifySize = 100;
|
||||||
|
});
|
||||||
|
await page.mouse.move(midX - 15, midY + 15);
|
||||||
|
await checkCenter(midX - 15, midY + 15);
|
||||||
|
props = await page.evaluate(() => {
|
||||||
|
const el = document.querySelector('.ge-brush-cursor');
|
||||||
|
return {
|
||||||
|
width: parseFloat(el.style.width),
|
||||||
|
height: parseFloat(el.style.height),
|
||||||
|
display: el.style.display,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(props.display).toBe('block');
|
||||||
|
expect(props.width).toBeGreaterThan(0);
|
||||||
|
|
||||||
|
// 7. Clone tool
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const { state } = await import('/static/js/editor/state.js');
|
||||||
|
state.tool = 'clone';
|
||||||
|
state.brushSize = 50;
|
||||||
|
});
|
||||||
|
await page.mouse.move(midX, midY);
|
||||||
|
await checkCenter(midX, midY);
|
||||||
|
await expect(cursor).toBeVisible();
|
||||||
|
|
||||||
|
// Clean close
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const editor = await window.__photoEditorImport('/static/js/galleryEditor.js');
|
||||||
|
editor.closeEditor({ force: true });
|
||||||
|
});
|
||||||
|
await expect(cursor).toHaveCount(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('canvas boundaries, pointer exit and re-entry, and viewport resize', async ({ page }) => {
|
||||||
|
await openBlankEditor(page, { width: 400, height: 300 });
|
||||||
|
await selectTool(page, 'brush');
|
||||||
|
|
||||||
|
const canvas = await page.locator('.ge-main-canvas').boundingBox();
|
||||||
|
const cursor = page.locator('.ge-brush-cursor');
|
||||||
|
|
||||||
|
const checkCenter = async (x, y) => {
|
||||||
|
await expect.poll(async () => {
|
||||||
|
const box = await cursor.boundingBox();
|
||||||
|
return box ? Math.max(Math.abs(box.x + box.width / 2 - x), Math.abs(box.y + box.height / 2 - y)) : Infinity;
|
||||||
|
}).toBeLessThan(1);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 1. Enter canvas
|
||||||
|
const insideX = canvas.x + canvas.width * 0.5;
|
||||||
|
const insideY = canvas.y + canvas.height * 0.5;
|
||||||
|
await page.mouse.move(insideX, insideY);
|
||||||
|
await expect(cursor).toBeVisible();
|
||||||
|
await checkCenter(insideX, insideY);
|
||||||
|
|
||||||
|
// 2. Exit canvas boundary: cursor should hide
|
||||||
|
const outsideX = canvas.x - 50;
|
||||||
|
const outsideY = canvas.y - 50;
|
||||||
|
await page.mouse.move(outsideX, outsideY);
|
||||||
|
await expect(cursor).toBeHidden();
|
||||||
|
|
||||||
|
// 3. Re-enter canvas: cursor should restore visibility and position
|
||||||
|
await page.mouse.move(insideX, insideY);
|
||||||
|
await expect(cursor).toBeVisible();
|
||||||
|
await checkCenter(insideX, insideY);
|
||||||
|
|
||||||
|
// 4. Viewport resize
|
||||||
|
await page.setViewportSize({ width: 1280, height: 800 });
|
||||||
|
await page.waitForTimeout(100);
|
||||||
|
const newCanvas = await page.locator('.ge-main-canvas').boundingBox();
|
||||||
|
const newInsideX = newCanvas.x + newCanvas.width * 0.5;
|
||||||
|
const newInsideY = newCanvas.y + newCanvas.height * 0.5;
|
||||||
|
await page.mouse.move(newInsideX, newInsideY);
|
||||||
|
await expect(cursor).toBeVisible();
|
||||||
|
await checkCenter(newInsideX, newInsideY);
|
||||||
|
|
||||||
|
// Clean close
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const editor = await window.__photoEditorImport('/static/js/galleryEditor.js');
|
||||||
|
editor.closeEditor({ force: true });
|
||||||
|
});
|
||||||
|
await expect(cursor).toHaveCount(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
for (const view of ['2x zoom', 'fit-to-view']) {
|
||||||
|
test(`zoom and fit-to-view cursor scaling and positioning: ${view}`, async ({ page }) => {
|
||||||
|
const size = view === '2x zoom' ? { width: 400, height: 300 } : { width: 1600, height: 1200 };
|
||||||
|
await openBlankEditor(page, size);
|
||||||
|
await selectTool(page, 'brush');
|
||||||
|
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const { state } = await import('/static/js/editor/state.js');
|
||||||
|
state.brushSize = 50;
|
||||||
|
// The zoom-in control applies 1.25x through the real zoom handler.
|
||||||
|
state.zoom = 1.6;
|
||||||
|
state.zoomViewMode = 'scale';
|
||||||
|
});
|
||||||
|
await page.locator('#ge-zoom-in').click();
|
||||||
|
await expect.poll(() => page.evaluate(async () => {
|
||||||
|
const { state } = await import('/static/js/editor/state.js');
|
||||||
|
return { zoom: state.zoom, mode: state.zoomViewMode };
|
||||||
|
})).toEqual({ zoom: 2, mode: 'scale' });
|
||||||
|
|
||||||
|
if (view === 'fit-to-view') {
|
||||||
|
await page.locator('#ge-zoom-100').click();
|
||||||
|
const fitted = await page.evaluate(async () => {
|
||||||
|
const { state } = await import('/static/js/editor/state.js');
|
||||||
|
const area = document.querySelector('.ge-canvas-area');
|
||||||
|
const pad = state._footerDocked ? 0 : 20;
|
||||||
|
const rulerPad = state.rulersVisible ? 18 : 0;
|
||||||
|
return {
|
||||||
|
zoom: state.zoom,
|
||||||
|
mode: state.zoomViewMode,
|
||||||
|
expectedZoom: Math.min(1,
|
||||||
|
(area.clientWidth - 2 * pad - rulerPad) / state.imgWidth,
|
||||||
|
(area.clientHeight - 2 * pad - rulerPad) / state.imgHeight),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(fitted.mode).toBe('fit');
|
||||||
|
expect(fitted.zoom).toBeGreaterThan(0);
|
||||||
|
expect(fitted.zoom).toBeLessThan(1);
|
||||||
|
expect(fitted.zoom).toBeCloseTo(fitted.expectedZoom, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
const canvas = page.locator('.ge-main-canvas');
|
||||||
|
const canvasBox = await canvas.boundingBox();
|
||||||
|
const cursor = page.locator('.ge-brush-cursor');
|
||||||
|
if (view === '2x zoom') {
|
||||||
|
expect(canvasBox.width).toBeCloseTo(size.width * 2, 1);
|
||||||
|
expect(canvasBox.height).toBeCloseTo(size.height * 2, 1);
|
||||||
|
}
|
||||||
|
const targetX = canvasBox.x + canvasBox.width * 0.4;
|
||||||
|
const targetY = canvasBox.y + canvasBox.height * 0.4;
|
||||||
|
await page.mouse.move(targetX, targetY);
|
||||||
|
await expect(cursor).toBeVisible();
|
||||||
|
|
||||||
|
// Pointer coordinates can be rounded to device pixels; keep the existing
|
||||||
|
// subpixel alignment tolerance separate from the stricter diameter check.
|
||||||
|
await expect.poll(async () => {
|
||||||
|
const box = await cursor.boundingBox();
|
||||||
|
return box ? Math.max(
|
||||||
|
Math.abs(box.x + box.width / 2 - targetX),
|
||||||
|
Math.abs(box.y + box.height / 2 - targetY),
|
||||||
|
) : Infinity;
|
||||||
|
}).toBeLessThan(1);
|
||||||
|
await expect.poll(() => page.evaluate(() => {
|
||||||
|
const canvasEl = document.querySelector('.ge-main-canvas');
|
||||||
|
const canvasRect = canvasEl.getBoundingClientRect();
|
||||||
|
const cursorRect = document.querySelector('.ge-brush-cursor').getBoundingClientRect();
|
||||||
|
return Math.max(
|
||||||
|
Math.abs(cursorRect.width - 50 * canvasRect.width / canvasEl.width),
|
||||||
|
Math.abs(cursorRect.height - 50 * canvasRect.height / canvasEl.height),
|
||||||
|
);
|
||||||
|
})).toBeLessThan(0.1);
|
||||||
|
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const editor = await window.__photoEditorImport('/static/js/galleryEditor.js');
|
||||||
|
editor.closeEditor({ force: true });
|
||||||
|
});
|
||||||
|
await expect(cursor).toHaveCount(0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
test('memory and lifecycle: repeated editor open/close cycles retain no detached cursors or active animations', async ({ page }) => {
|
||||||
|
for (let cycle = 0; cycle < 5; cycle++) {
|
||||||
|
await openBlankEditor(page, { width: 400, height: 300 });
|
||||||
|
await selectTool(page, 'brush');
|
||||||
|
|
||||||
|
const canvas = await page.locator('.ge-main-canvas').boundingBox();
|
||||||
|
await page.mouse.move(canvas.x + canvas.width * 0.5, canvas.y + canvas.height * 0.5);
|
||||||
|
const cursor = page.locator('.ge-brush-cursor');
|
||||||
|
await expect(cursor).toBeVisible();
|
||||||
|
|
||||||
|
// Verify cursor layer and animation are active
|
||||||
|
const cycleState = await page.evaluate(() => {
|
||||||
|
const el = document.querySelector('.ge-brush-cursor');
|
||||||
|
window.__lastCycleAnim = el?._positionAnimation;
|
||||||
|
return {
|
||||||
|
cursorCount: document.querySelectorAll('.ge-brush-cursor').length,
|
||||||
|
layerCount: document.querySelectorAll('#ge-cursor-layer').length,
|
||||||
|
hasAnim: Boolean(el?._positionAnimation),
|
||||||
|
animPaused: el?._positionAnimation?.playState === 'paused',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(cycleState.cursorCount).toBe(1);
|
||||||
|
expect(cycleState.layerCount).toBe(1);
|
||||||
|
expect(cycleState.animPaused).toBe(true);
|
||||||
|
|
||||||
|
// Close editor
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const editor = await window.__photoEditorImport('/static/js/galleryEditor.js');
|
||||||
|
editor.closeEditor({ force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify complete cleanup after close
|
||||||
|
await expect(cursor).toHaveCount(0);
|
||||||
|
const postClose = await page.evaluate(() => {
|
||||||
|
const anim = window.__lastCycleAnim;
|
||||||
|
delete window.__lastCycleAnim;
|
||||||
|
return {
|
||||||
|
cursorCount: document.querySelectorAll('.ge-brush-cursor').length,
|
||||||
|
layerCount: document.querySelectorAll('#ge-cursor-layer').length,
|
||||||
|
animIdle: anim ? anim.playState === 'idle' : true,
|
||||||
|
activeAnims: document.getAnimations().filter(a => {
|
||||||
|
const target = a.effect?.target;
|
||||||
|
return target && (target.classList?.contains('ge-brush-cursor') || target.id === 'ge-cursor-layer');
|
||||||
|
}).length,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(postClose.cursorCount).toBe(0);
|
||||||
|
expect(postClose.layerCount).toBe(0);
|
||||||
|
expect(postClose.animIdle).toBe(true);
|
||||||
|
expect(postClose.activeAnims).toBe(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
"""An unknown context window must not be budgeted like a tiny local server (#6678).
|
||||||
|
|
||||||
|
``budget_context_for_model`` returns 0 when the endpoint does not prove its
|
||||||
|
window, and ``compute_input_token_budget`` answers 0 with the conservative
|
||||||
|
default budget. The agent loop's small-server clamp (``<= 8192``) also caught
|
||||||
|
0 and cut the request to 1200 tokens, so an open document or earlier context
|
||||||
|
of a few thousand tokens was trimmed away before the model saw it.
|
||||||
|
"""
|
||||||
|
import inspect
|
||||||
|
import json
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
DONE = 'data: [DONE]\n\n'
|
||||||
|
MARKER = 'NIGHTJAR-7'
|
||||||
|
# ~3000 estimated tokens: over the old 1200 clamp, under the 6000 default.
|
||||||
|
CONTEXT = ''.join(
|
||||||
|
f'## Project-{n}\nOwner: team {n}. Status: active. Notes: routine maintenance '
|
||||||
|
'and quarterly review scheduled for every workstream in this registry.\n\n'
|
||||||
|
for n in range(60)
|
||||||
|
) + f'## Project-Alpha\nAlias: {MARKER}\n'
|
||||||
|
|
||||||
|
|
||||||
|
def _event(payload):
|
||||||
|
return 'data: ' + json.dumps(payload) + '\n\n'
|
||||||
|
|
||||||
|
|
||||||
|
async def _first_request(monkeypatch, tmp_path, context_window):
|
||||||
|
import src.agent_loop as al
|
||||||
|
from tests.test_agent_runtime_context import _patch_fake_skills
|
||||||
|
_patch_fake_skills(monkeypatch)
|
||||||
|
monkeypatch.setattr('src.tool_index.get_tool_index', lambda: None)
|
||||||
|
monkeypatch.setattr('src.model_context.budget_context_for_model',
|
||||||
|
lambda *args, **kwargs: context_window)
|
||||||
|
seen = []
|
||||||
|
|
||||||
|
async def provider(candidates, messages, **kwargs):
|
||||||
|
# Like llm_core: the per-candidate factory shapes and trims the request.
|
||||||
|
url, model, headers = candidates[0][:3]
|
||||||
|
request = kwargs['candidate_request_factory'](0, url, model, headers)
|
||||||
|
if inspect.isawaitable(request):
|
||||||
|
request = await request
|
||||||
|
seen.append((request or {}).get('messages', messages))
|
||||||
|
yield _event({'delta': 'ok'})
|
||||||
|
yield DONE
|
||||||
|
|
||||||
|
monkeypatch.setattr(al, 'stream_llm_with_fallback', provider)
|
||||||
|
history = [
|
||||||
|
{'role': 'user', 'content': 'Keep this registry for later:\n\n' + CONTEXT},
|
||||||
|
{'role': 'assistant', 'content': 'Saved.'},
|
||||||
|
{'role': 'user', 'content': 'What is the alias of Project-Alpha?'},
|
||||||
|
]
|
||||||
|
async for _ in al.stream_agent_loop('http://127.0.0.1:9/v1', 'model', history,
|
||||||
|
max_rounds=1, workspace=str(tmp_path)):
|
||||||
|
pass
|
||||||
|
assert seen, 'the provider was never called'
|
||||||
|
return json.dumps(seen[0])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_unknown_context_window_keeps_earlier_context(monkeypatch, tmp_path):
|
||||||
|
request = await _first_request(monkeypatch, tmp_path, 0)
|
||||||
|
assert MARKER in request
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_known_small_window_is_still_clamped(monkeypatch, tmp_path):
|
||||||
|
# The clamp exists for small local servers whose window is known (n_ctx
|
||||||
|
# 2048 here); it must keep protecting them.
|
||||||
|
request = await _first_request(monkeypatch, tmp_path, 2048)
|
||||||
|
assert MARKER not in request
|
||||||
@@ -13,6 +13,7 @@ import socketserver
|
|||||||
import threading
|
import threading
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
import requests
|
||||||
|
|
||||||
from src import caldav_sync, caldav_writeback
|
from src import caldav_sync, caldav_writeback
|
||||||
|
|
||||||
@@ -26,12 +27,13 @@ def test_build_dav_client_disables_redirects():
|
|||||||
|
|
||||||
def test_dav_client_does_not_follow_redirect_to_internal_host():
|
def test_dav_client_does_not_follow_redirect_to_internal_host():
|
||||||
"""End-to-end through the real DAVClient: a 302 toward an internal host
|
"""End-to-end through the real DAVClient: a 302 toward an internal host
|
||||||
must NOT be followed. Without the fix the sink is contacted (SSRF); with it
|
must NOT be followed. Exercise the session limit separately because some
|
||||||
the redirect is refused and the sink is never reached."""
|
caldav versions independently refuse cross-origin PROPFIND redirects."""
|
||||||
pytest.importorskip("caldav")
|
pytest.importorskip("caldav")
|
||||||
|
|
||||||
sink_hits: list[str] = []
|
sink_hits: list[str] = []
|
||||||
public_methods: list[str] = []
|
public_methods: list[str] = []
|
||||||
|
same_origin_hits: list[str] = []
|
||||||
|
|
||||||
class _Internal(http.server.BaseHTTPRequestHandler):
|
class _Internal(http.server.BaseHTTPRequestHandler):
|
||||||
# Stand-in for an internal service the attacker redirects toward.
|
# Stand-in for an internal service the attacker redirects toward.
|
||||||
@@ -48,9 +50,15 @@ def test_dav_client_does_not_follow_redirect_to_internal_host():
|
|||||||
class _Public(http.server.BaseHTTPRequestHandler):
|
class _Public(http.server.BaseHTTPRequestHandler):
|
||||||
# The "validated" public CalDAV server that redirects everything inward.
|
# The "validated" public CalDAV server that redirects everything inward.
|
||||||
def do_GET(self): # noqa: N802
|
def do_GET(self): # noqa: N802
|
||||||
|
if self.path == "/landing":
|
||||||
|
same_origin_hits.append(self.path)
|
||||||
|
self.send_response(200)
|
||||||
|
self.end_headers()
|
||||||
|
return
|
||||||
public_methods.append(self.command)
|
public_methods.append(self.command)
|
||||||
self.send_response(302)
|
self.send_response(302)
|
||||||
self.send_header("Location", f"http://127.0.0.1:{internal_port}/leak")
|
location = "/landing" if self.path == "/same" else f"http://127.0.0.1:{internal_port}/leak"
|
||||||
|
self.send_header("Location", location)
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
|
||||||
do_PROPFIND = do_GET
|
do_PROPFIND = do_GET
|
||||||
@@ -62,8 +70,10 @@ def test_dav_client_does_not_follow_redirect_to_internal_host():
|
|||||||
internal_port = internal.server_address[1]
|
internal_port = internal.server_address[1]
|
||||||
public = socketserver.TCPServer(("127.0.0.1", 0), _Public)
|
public = socketserver.TCPServer(("127.0.0.1", 0), _Public)
|
||||||
public_port = public.server_address[1]
|
public_port = public.server_address[1]
|
||||||
threading.Thread(target=internal.serve_forever, daemon=True).start()
|
internal_thread = threading.Thread(target=internal.serve_forever, daemon=True)
|
||||||
threading.Thread(target=public.serve_forever, daemon=True).start()
|
public_thread = threading.Thread(target=public.serve_forever, daemon=True)
|
||||||
|
internal_thread.start()
|
||||||
|
public_thread.start()
|
||||||
try:
|
try:
|
||||||
public_url = f"http://127.0.0.1:{public_port}/dav"
|
public_url = f"http://127.0.0.1:{public_port}/dav"
|
||||||
client = caldav_sync._build_dav_client(public_url, "u", "p")
|
client = caldav_sync._build_dav_client(public_url, "u", "p")
|
||||||
@@ -82,17 +92,35 @@ def test_dav_client_does_not_follow_redirect_to_internal_host():
|
|||||||
assert public_methods in (["PROPFIND"], ["PROPFIND", "GET"]), public_methods
|
assert public_methods in (["PROPFIND"], ["PROPFIND", "GET"]), public_methods
|
||||||
assert sink_hits == [], "redirect toward an internal host must not be followed"
|
assert sink_hits == [], "redirect toward an internal host must not be followed"
|
||||||
|
|
||||||
# Positive control: demonstrate that the sink would see the redirect
|
# GET redirects are handled by the HTTP session even in caldav 3.7.
|
||||||
# if the session's redirect protection were removed.
|
# This makes the cross-port SSRF assertion sensitive to our own limit.
|
||||||
|
with pytest.raises(Exception, match="Exceeded 0 redirects"):
|
||||||
|
client.request(public_url, "GET", "")
|
||||||
|
assert sink_hits == [], "GET redirect toward the internal sink must be blocked"
|
||||||
|
|
||||||
|
# requests can reach the cross-port sink when redirects are enabled,
|
||||||
|
# even if DAVClient independently rejects cross-origin PROPFIND redirects.
|
||||||
|
with requests.Session() as session:
|
||||||
|
response = session.get(public_url, timeout=5)
|
||||||
|
assert response.status_code == 207
|
||||||
|
assert sink_hits == ["/leak"], "the cross-port redirect must reach the sink"
|
||||||
|
|
||||||
|
# GET follows redirects at the HTTP session layer across these caldav
|
||||||
|
# versions. Check the application's limit on a same-origin redirect too.
|
||||||
|
same_url = f"http://127.0.0.1:{public_port}/same"
|
||||||
|
with pytest.raises(Exception, match="Exceeded 0 redirects"):
|
||||||
|
client.request(same_url, "GET", "")
|
||||||
|
assert same_origin_hits == []
|
||||||
client.session.max_redirects = 30
|
client.session.max_redirects = 30
|
||||||
try:
|
client.request(same_url, "GET", "")
|
||||||
client.request(public_url, "PROPFIND", "")
|
assert same_origin_hits == ["/landing"]
|
||||||
except Exception:
|
|
||||||
pass # The sink's response is intentionally not a valid DAV body.
|
|
||||||
assert sink_hits == ["/leak"], "the test must detect a followed redirect"
|
|
||||||
finally:
|
finally:
|
||||||
internal.shutdown()
|
internal.shutdown()
|
||||||
public.shutdown()
|
public.shutdown()
|
||||||
|
internal_thread.join()
|
||||||
|
public_thread.join()
|
||||||
|
internal.server_close()
|
||||||
|
public.server_close()
|
||||||
|
|
||||||
|
|
||||||
def test_sync_and_writeback_construct_clients_through_the_helper():
|
def test_sync_and_writeback_construct_clients_through_the_helper():
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
"""The CalDAV config routes answer 400 for a body that is JSON but not an object."""
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
from routes.calendar_routes import _json_object_body
|
||||||
|
|
||||||
|
|
||||||
|
def _request(raw: bytes):
|
||||||
|
async def _json():
|
||||||
|
return json.loads(raw)
|
||||||
|
return SimpleNamespace(json=_json)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse(raw: bytes):
|
||||||
|
return asyncio.run(_json_object_body(_request(raw)))
|
||||||
|
|
||||||
|
|
||||||
|
def test_object_body_is_returned():
|
||||||
|
assert _parse(b'{"url": "https://dav.example/"}') == {"url": "https://dav.example/"}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("raw", [b"", b"not json"])
|
||||||
|
def test_unreadable_body_counts_as_empty(raw):
|
||||||
|
assert _parse(raw) == {}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("raw", [b"[]", b'"x"', b"5", b"null"])
|
||||||
|
def test_non_object_body_is_a_400(raw):
|
||||||
|
with pytest.raises(HTTPException) as exc:
|
||||||
|
_parse(raw)
|
||||||
|
assert exc.value.status_code == 400
|
||||||
@@ -0,0 +1,148 @@
|
|||||||
|
"""Both chat tidy entry points retain saved conversations and still remove empties."""
|
||||||
|
import asyncio
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi import APIRouter, FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
import core.database as database
|
||||||
|
import core.session_manager as manager_module
|
||||||
|
import routes.session_routes as routes
|
||||||
|
import src.session_actions as actions
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def tidy_api(tmp_path, monkeypatch):
|
||||||
|
engine = create_engine(
|
||||||
|
f"sqlite:///{(tmp_path / 'tidy.db').as_posix()}",
|
||||||
|
connect_args={"check_same_thread": False},
|
||||||
|
)
|
||||||
|
database.Base.metadata.create_all(engine)
|
||||||
|
factory = sessionmaker(bind=engine, autoflush=False)
|
||||||
|
monkeypatch.setattr(database, "SessionLocal", factory)
|
||||||
|
monkeypatch.setattr(manager_module, "SessionLocal", factory)
|
||||||
|
monkeypatch.setattr(routes, "SessionLocal", factory)
|
||||||
|
monkeypatch.setattr(routes, "router", APIRouter(prefix="/api"))
|
||||||
|
monkeypatch.setattr(routes, "effective_user", lambda request: "alice")
|
||||||
|
manager = manager_module.SessionManager.__new__(manager_module.SessionManager)
|
||||||
|
manager.sessions = {}
|
||||||
|
manager.upload_handler = None
|
||||||
|
monkeypatch.setattr(manager, "get_sessions_for_user", lambda owner: {})
|
||||||
|
app = FastAPI()
|
||||||
|
app.include_router(routes.setup_session_routes(manager, {}))
|
||||||
|
with TestClient(app) as client:
|
||||||
|
yield client, factory, manager
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
def run_tidy(client, entrypoint):
|
||||||
|
if entrypoint == "action":
|
||||||
|
return asyncio.run(actions.run_auto_sort("alice", skip_llm=True))
|
||||||
|
response = client.post("/api/sessions/auto-sort?skip_llm=true")
|
||||||
|
assert response.status_code == 200, response.text
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("entrypoint", ["route", "action"])
|
||||||
|
@pytest.mark.parametrize("title,prompt", [
|
||||||
|
("New chat", "Please analyze the following contract and retain all details. " * 20),
|
||||||
|
("Contract analysis", "Review contract"),
|
||||||
|
("Chat: analysis", "Explain these results and retain the full answer"),
|
||||||
|
("Test", "Here is a large input that should be analyzed and retained"),
|
||||||
|
], ids=["generic-title", "short-prompt", "title-prefix", "test-title"])
|
||||||
|
def test_tidy_preserves_nontrivial_saved_answer(tidy_api, title, prompt, entrypoint):
|
||||||
|
client, factory, manager = tidy_api
|
||||||
|
old = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(hours=2)
|
||||||
|
answer = "Clause analysis and recommended changes.\n" * 2000
|
||||||
|
with factory() as db:
|
||||||
|
db.add(database.Session(
|
||||||
|
id="valuable-chat", name=title, owner="alice", endpoint_url="", model="",
|
||||||
|
archived=False, is_important=False, message_count=2,
|
||||||
|
created_at=old, updated_at=old, last_accessed=old, last_message_at=old,
|
||||||
|
))
|
||||||
|
db.commit()
|
||||||
|
db.add_all([
|
||||||
|
database.ChatMessage(id="prompt", session_id="valuable-chat", role="user",
|
||||||
|
content=prompt, timestamp=old),
|
||||||
|
database.ChatMessage(id="answer", session_id="valuable-chat", role="assistant",
|
||||||
|
content=answer, timestamp=old + timedelta(seconds=1)),
|
||||||
|
])
|
||||||
|
db.commit()
|
||||||
|
run_tidy(client, entrypoint)
|
||||||
|
with factory() as db:
|
||||||
|
remaining = db.query(database.ChatMessage).filter_by(session_id="valuable-chat").count()
|
||||||
|
survives = db.get(database.Session, "valuable-chat") is not None
|
||||||
|
assert survives and remaining == 2, "Title/prompt heuristics must not erase saved answers"
|
||||||
|
with factory() as db:
|
||||||
|
assert db.get(database.ChatMessage, "answer").content == answer
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("entrypoint", ["route", "action"])
|
||||||
|
@pytest.mark.parametrize("kind", ["unanswered", "greeting", "empty", "fresh-empty", "starred-empty", "archived", "other-owner", "incognito"])
|
||||||
|
def test_tidy_cleanup_boundaries(tidy_api, entrypoint, kind):
|
||||||
|
client, factory, manager = tidy_api
|
||||||
|
now = datetime.now(timezone.utc).replace(tzinfo=None)
|
||||||
|
timestamp = now if kind == "fresh-empty" else now - timedelta(hours=2)
|
||||||
|
message = kind in {"unanswered", "greeting", "archived", "other-owner", "incognito"}
|
||||||
|
with factory() as db:
|
||||||
|
db.add(database.Session(
|
||||||
|
id="boundary-chat", name="Incognito" if kind == "incognito" else "New chat",
|
||||||
|
owner="bob" if kind == "other-owner" else "alice", endpoint_url="", model="",
|
||||||
|
archived=kind == "archived", is_important=kind == "starred-empty",
|
||||||
|
# Deliberately inaccurate metadata: cleanup must use message rows.
|
||||||
|
message_count=0 if message else 40,
|
||||||
|
created_at=timestamp, updated_at=timestamp, last_accessed=timestamp, last_message_at=timestamp,
|
||||||
|
))
|
||||||
|
db.commit()
|
||||||
|
if message:
|
||||||
|
db.add(database.ChatMessage(id="boundary-message", session_id="boundary-chat", role="user",
|
||||||
|
content="hi" if kind == "greeting" else "Pending question " * 100,
|
||||||
|
timestamp=timestamp))
|
||||||
|
db.commit()
|
||||||
|
run_tidy(client, entrypoint)
|
||||||
|
deleted = kind in {"empty", "incognito"}
|
||||||
|
with factory() as db:
|
||||||
|
assert (db.get(database.Session, "boundary-chat") is None) == deleted
|
||||||
|
assert db.query(database.ChatMessage).filter_by(session_id="boundary-chat").count() == int(message and not deleted)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("entrypoint", ["route", "action"])
|
||||||
|
def test_tidy_keeps_folder_assignment_working(tidy_api, monkeypatch, entrypoint):
|
||||||
|
from types import SimpleNamespace
|
||||||
|
import src.llm_core as llm
|
||||||
|
import src.task_endpoint as endpoints
|
||||||
|
client, factory, manager = tidy_api
|
||||||
|
old = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(hours=2)
|
||||||
|
for sid in ["chat-one-long-id", "chat-two-long-id"]:
|
||||||
|
with factory() as db:
|
||||||
|
db.add(database.Session(id=sid, name="New chat", owner="alice", endpoint_url="", model="",
|
||||||
|
archived=False, created_at=old, updated_at=old,
|
||||||
|
last_accessed=old, last_message_at=old))
|
||||||
|
db.commit()
|
||||||
|
db.add(database.ChatMessage(id=f"{sid}-message", session_id=sid, role="user",
|
||||||
|
content="Saved question", timestamp=old))
|
||||||
|
db.commit()
|
||||||
|
manager.sessions[sid] = SimpleNamespace(id=sid, name="New chat", archived=False,
|
||||||
|
updated_at=old.isoformat(), created_at=old.isoformat())
|
||||||
|
monkeypatch.setattr(manager, "get_sessions_for_user", lambda owner: manager.sessions)
|
||||||
|
monkeypatch.setattr(endpoints, "resolve_task_endpoint", lambda *args, **kwargs: ("http://model.invalid", "test", {}))
|
||||||
|
response_text = '{"folders":{"Projects":["chat-one","chat-two"]}}'
|
||||||
|
monkeypatch.setattr(llm, "llm_call", lambda *args, **kwargs: response_text)
|
||||||
|
|
||||||
|
async def async_response(*args, **kwargs):
|
||||||
|
return response_text
|
||||||
|
|
||||||
|
monkeypatch.setattr(llm, "llm_call_async", async_response)
|
||||||
|
if entrypoint == "action":
|
||||||
|
result = asyncio.run(actions.run_auto_sort("alice"))
|
||||||
|
assert "Sorted 2 sessions" in result
|
||||||
|
else:
|
||||||
|
response = client.post("/api/sessions/auto-sort")
|
||||||
|
assert response.status_code == 200, response.text
|
||||||
|
assert response.json()["updated"] == 2
|
||||||
|
with factory() as db:
|
||||||
|
assert db.query(database.ChatMessage).count() == 2
|
||||||
|
assert {row.folder for row in db.query(database.Session).all()} == {"Projects"}
|
||||||
@@ -7,13 +7,13 @@ CalendarCal.owner; the query must join it, like routes/calendar_routes.
|
|||||||
"""
|
"""
|
||||||
import uuid
|
import uuid
|
||||||
import sys
|
import sys
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from tests.helpers.database import disposable_database
|
from tests.helpers.database import disposable_database
|
||||||
|
|
||||||
from core.database import CalendarEvent, CalendarCal
|
from core.database import CalendarEvent, CalendarCal
|
||||||
from src.task_scheduler import _checkin_calendar_events
|
from src.task_scheduler import _checkin_calendar_events, _digest_windows
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
@@ -68,3 +68,44 @@ def test_cancelled_excluded_and_window_respected():
|
|||||||
assert "cancelled" not in [ev.summary for ev in out]
|
assert "cancelled" not in [ev.summary for ev in out]
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("days, expected_label", [(2, "this_week"), (7, "next_30_days")])
|
||||||
|
def test_boundary_event_appears_in_only_one_digest_window(days, expected_label):
|
||||||
|
now = datetime(2026, 6, 1, 9)
|
||||||
|
boundary = now + timedelta(days=days)
|
||||||
|
with _TS() as db:
|
||||||
|
db.add(CalendarCal(id="boundary-cal", owner="alice", name="Boundary"))
|
||||||
|
db.add(CalendarEvent(
|
||||||
|
uid="boundary-event", calendar_id="boundary-cal", summary="Boundary meeting",
|
||||||
|
dtstart=boundary, dtend=boundary + timedelta(hours=1), status="confirmed",
|
||||||
|
))
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
buckets = [
|
||||||
|
label for label, start, end in _digest_windows(now)
|
||||||
|
if _checkin_calendar_events(db, "alice", start, end)
|
||||||
|
]
|
||||||
|
assert buckets == [expected_label]
|
||||||
|
|
||||||
|
|
||||||
|
def test_digest_includes_start_and_excludes_end():
|
||||||
|
start = datetime(2026, 6, 1, 9)
|
||||||
|
end = start + timedelta(days=2)
|
||||||
|
with _TS() as db:
|
||||||
|
db.add(CalendarCal(id="bounds-cal", owner="alice", name="Bounds"))
|
||||||
|
for uid, when in [
|
||||||
|
("before", start - timedelta(seconds=1)),
|
||||||
|
("start", start),
|
||||||
|
("inside", end - timedelta(seconds=1)),
|
||||||
|
("end", end),
|
||||||
|
]:
|
||||||
|
db.add(CalendarEvent(
|
||||||
|
uid=uid, calendar_id="bounds-cal", summary=uid, dtstart=when,
|
||||||
|
dtend=when + timedelta(hours=1), status="confirmed",
|
||||||
|
))
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
assert [ev.uid for ev in _checkin_calendar_events(db, "alice", start, end)] == [
|
||||||
|
"start", "inside",
|
||||||
|
]
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
"""Compare's actual stream consumer must preserve evidence and terminal state."""
|
||||||
|
import json
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
|
||||||
|
CASES = [
|
||||||
|
("lf", 'data: {"delta":"Hello"}\n\ndata: [DONE]\n\n', False, True, "Hello"),
|
||||||
|
("crlf", 'data:{"delta":"Héllö"}\r\n\r\ndata:[DONE]\r\n\r\n', False, True, "Héllö"),
|
||||||
|
("multiline", 'data: {"delta":\ndata: "Hello"}\n\ndata: [DONE]\n\n', False, True, "Hello"),
|
||||||
|
("eof", 'data: {"delta":"Partial"}\n\n', False, False, "ended before completion"),
|
||||||
|
("named-error", 'event: error\ndata: {"text":"Provider Z private-model failed"}\n\n', False, False, "Provider Z private-model failed"),
|
||||||
|
("typed-error", 'data: {"type":"error","data":{"message":"Provider Z <img src=x onerror=alert(1)>"}}\n\ndata: [DONE]\n\n', False, False, "Provider Z <img"),
|
||||||
|
("blind-error", 'event:error\ndata:{"message":"Provider Z private-model failed"}\n\n', True, False, "request failed"),
|
||||||
|
("empty-error", 'event:error\ndata:\n\n', False, False, "request failed"),
|
||||||
|
("done-open", 'data: {"delta":"Hello"}\n\ndata: [DONE]\n\n', False, True, "Hello"),
|
||||||
|
("heartbeat", 'data: {"delta":"Hello"}\n\ndata: [DONE]\n\n', False, True, "Hello"),
|
||||||
|
("cr", 'data: {"delta":"Hello"}\r\rdata:[DONE]\r\r', False, True, "Hello"),
|
||||||
|
("invalid", 'data: not-json\n\ndata: [DONE]\n\n', False, False, "Invalid model stream data"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("kind,wire,blind,success,text", CASES)
|
||||||
|
def test_compare_stream_contract(kind, wire, blind, success, text):
|
||||||
|
if not shutil.which("node"):
|
||||||
|
pytest.skip("node not installed")
|
||||||
|
script = r"""
|
||||||
|
const assert=require('node:assert/strict');
|
||||||
|
let chromium;try{({chromium}=require('playwright'));}catch(e){if(e.code==='MODULE_NOT_FOUND')process.exit(77);throw e;}
|
||||||
|
(async()=>{
|
||||||
|
const browser=await chromium.launch({headless:true});
|
||||||
|
try{
|
||||||
|
const page=await browser.newPage();
|
||||||
|
await page.goto(process.env.ODYSSEUS_TEST_STATIC_ORIGIN+'/static/js/storage.js');
|
||||||
|
await page.setContent('<div class="compare-pane" data-pane="0"><div id="cmp-badge-0"></div><div id="cmp-history-0"><div class="msg msg-ai"><div class="body"></div></div></div></div>');
|
||||||
|
const result=await page.evaluate(async(data)=>{
|
||||||
|
const {default:state}=await import('/static/js/compare/state.js');
|
||||||
|
const {streamToPane}=await import('/static/js/compare/stream.js');
|
||||||
|
Object.assign(state,{isActive:true,_selectedModels:[{model:'fixture',name:'Fixture'}],_paneSessionIds:['session'],_compareMode:'chat',_parallel:true,_blindMode:data.blind,_timeout:data.kind==='heartbeat'?1:5});
|
||||||
|
const nativeFetch=window.fetch,bytes=new TextEncoder().encode(data.wire);let index=0,cancelled=false,context=null;
|
||||||
|
window.fetch=async(url,options)=>{
|
||||||
|
if(!String(url).endsWith('/api/chat_stream'))return nativeFetch(url,options);
|
||||||
|
context=options.body.get('search_context');
|
||||||
|
const heartbeats=[...Array(6).fill(': ping\n\n'),data.wire];
|
||||||
|
return new Response(new ReadableStream({start(controller){options.signal.addEventListener('abort',()=>controller.error(new DOMException('Stopped','AbortError')),{once:true});},async pull(controller){if(data.kind==='heartbeat'){if(index<heartbeats.length){await new Promise(r=>setTimeout(r,200));controller.enqueue(new TextEncoder().encode(heartbeats[index++]));}else controller.close();}else if(index<bytes.length)controller.enqueue(bytes.slice(index,index+=1));else if(data.kind!=='done-open')controller.close();},cancel(){cancelled=true;}}),{headers:{'Content-Type':'text/event-stream'}});
|
||||||
|
};
|
||||||
|
const message=document.querySelector('.msg-ai'),spinner=document.createElement('span');let spinnerDestroyed=false;spinner.textContent='Loading';message.querySelector('.body').appendChild(spinner);message._spinner={element:spinner,destroy(){spinnerDestroyed=true;spinner.remove();}};
|
||||||
|
await Promise.race([streamToPane(0,'session','Prompt',message,{skipBadge:true}),new Promise((_,reject)=>setTimeout(()=>reject(Error('Consumer did not finish at DONE')),data.kind==='heartbeat'?6000:1500))]);
|
||||||
|
const pane=document.querySelector('.compare-pane');
|
||||||
|
return {done:pane.classList.contains('is-done'),failed:pane.classList.contains('is-failed'),text:pane.innerText,images:pane.querySelectorAll('img').length,cancelled,spinnerDestroyed,spinnerOwned:!!message._spinner};
|
||||||
|
},JSON.parse(process.argv[1]));
|
||||||
|
const expected=JSON.parse(process.argv[1]);
|
||||||
|
assert.equal(result.done,expected.success);assert.equal(result.failed,!expected.success);assert(result.text.includes(expected.text),JSON.stringify(result));assert.equal(result.images,0);
|
||||||
|
assert(result.spinnerDestroyed);assert(!result.spinnerOwned);
|
||||||
|
if(expected.blind)assert(!/Provider Z|private-model/.test(result.text),JSON.stringify(result));
|
||||||
|
if(expected.kind==='done-open')assert(result.cancelled);
|
||||||
|
console.log(JSON.stringify(result));
|
||||||
|
}finally{await browser.close();}
|
||||||
|
})().catch(e=>{console.error(e);process.exitCode=1;});
|
||||||
|
"""
|
||||||
|
data = dict(kind=kind, wire=wire, blind=blind, success=success, text=text)
|
||||||
|
result = subprocess.run(["node", "-e", script, json.dumps(data)], cwd=ROOT,
|
||||||
|
capture_output=True, text=True, timeout=30)
|
||||||
|
if result.returncode == 77:
|
||||||
|
pytest.skip("playwright not installed")
|
||||||
|
assert result.returncode == 0, result.stdout + result.stderr
|
||||||
@@ -70,3 +70,63 @@ def test_no_gguf_found_pattern():
|
|||||||
result = _diagnose_serve_output(out)
|
result = _diagnose_serve_output(out)
|
||||||
assert result is not None
|
assert result is not None
|
||||||
assert "GGUF" in result["message"]
|
assert "GGUF" in result["message"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_cookbook_routes_uses_shared_diagnosis_helper():
|
||||||
|
# /api/cookbook/tasks/status must use the maintained helper. A nested copy in
|
||||||
|
# cookbook_routes.py shadowed the import and silently drifted (missing the
|
||||||
|
# ModelOpt, mflux and MLX bridge diagnoses).
|
||||||
|
import ast
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
source = (Path(__file__).resolve().parents[1] / "routes" / "cookbook_routes.py").read_text(encoding="utf-8")
|
||||||
|
local_defs = [
|
||||||
|
node.lineno
|
||||||
|
for node in ast.walk(ast.parse(source))
|
||||||
|
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == "_diagnose_serve_output"
|
||||||
|
]
|
||||||
|
assert local_defs == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_modelopt_lm_head_checkpoint_returns_diagnosis():
|
||||||
|
out = "ValueError: There is no module or parameter named 'lm_head.input_scale' in Qwen3ForCausalLM"
|
||||||
|
result = _diagnose_serve_output(out)
|
||||||
|
assert result is not None
|
||||||
|
assert "ModelOpt" in result["message"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_nvidia_driver_too_old_for_torch_returns_diagnosis():
|
||||||
|
# vLLM 0.31 from PyPI ships torch cu130; on driver 570 (CUDA 12.8) the engine dies here.
|
||||||
|
out = (
|
||||||
|
"(EngineCore pid=1) ERROR [core.py:1433] self.driver_worker.init_device()\n"
|
||||||
|
"(EngineCore pid=1) ERROR [core.py:1433] RuntimeError: The NVIDIA driver on your system is too old "
|
||||||
|
"(found version 12080). Please update your GPU driver by downloading and installing a new version\n"
|
||||||
|
"Traceback (most recent call last):\n"
|
||||||
|
"RuntimeError: Engine core initialization failed. See root cause above. Failed core proc(s): {}\n"
|
||||||
|
)
|
||||||
|
result = _diagnose_serve_output(out)
|
||||||
|
assert result is not None
|
||||||
|
assert "driver" in result["message"].lower()
|
||||||
|
assert any("cu129" in s["label"] for s in result["suggestions"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_cuda_runtime_newer_than_driver_returns_diagnosis():
|
||||||
|
# torch switched to cu128 but the vLLM wheel's own kernels still need CUDA 13.
|
||||||
|
out = (
|
||||||
|
"RuntimeError: get_cuda_view_from_cpu_tensor, /workspace/csrc/libtorch_stable/cuda_view.cu:38, "
|
||||||
|
"cudaHostGetDevicePointer failed: CUDA driver version is insufficient for CUDA runtime version\n"
|
||||||
|
"Traceback (most recent call last):\n"
|
||||||
|
)
|
||||||
|
result = _diagnose_serve_output(out)
|
||||||
|
assert result is not None
|
||||||
|
assert "driver" in result["message"].lower()
|
||||||
|
|
||||||
|
|
||||||
|
def test_tasks_status_capture_joins_wrapped_tmux_lines():
|
||||||
|
# tmux wraps at the pane width (often mid-word), which hid errors like
|
||||||
|
# "insufficie|nt for CUDA runtime version" from the diagnosis patterns.
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
source = (Path(__file__).resolve().parents[1] / "routes" / "cookbook_routes.py").read_text(encoding="utf-8")
|
||||||
|
assert '_remote_tmux_command("capture-pane", "-t", session_id, "-p", "-J", "-S", "-500")' in source
|
||||||
|
assert '["tmux", "capture-pane", "-t", session_id, "-p", "-J", "-S", "-500"]' in source
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import pytest
|
||||||
|
import httpx
|
||||||
|
from unittest.mock import patch, AsyncMock
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from routes.cookbook_routes import setup_cookbook_routes
|
||||||
|
from src.auth_helpers import require_user
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
router = setup_cookbook_routes()
|
||||||
|
app.include_router(router)
|
||||||
|
app.dependency_overrides[require_user] = lambda: "test_user"
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_hf_gguf_files_success():
|
||||||
|
mock_resp = httpx.Response(
|
||||||
|
200,
|
||||||
|
json={
|
||||||
|
"siblings": [
|
||||||
|
{"rfilename": "model-q4_k_m.gguf"},
|
||||||
|
{"rfilename": "model-q8_0.gguf"},
|
||||||
|
{"rfilename": "README.md"},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
request=httpx.Request("GET", "https://huggingface.co/api/models/TheBloke/Llama-2-7B-GGUF"),
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("httpx.AsyncClient.get", new_callable=AsyncMock, return_value=mock_resp):
|
||||||
|
response = client.get("/api/cookbook/hf-gguf-files?repo_id=TheBloke/Llama-2-7B-GGUF")
|
||||||
|
assert response.status_code == 200
|
||||||
|
data = response.json()
|
||||||
|
assert data["ok"] is True
|
||||||
|
assert data["repo_id"] == "TheBloke/Llama-2-7B-GGUF"
|
||||||
|
assert data["files"] == ["model-q4_k_m.gguf", "model-q8_0.gguf"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_hf_gguf_files_http_error():
|
||||||
|
mock_resp = httpx.Response(
|
||||||
|
404,
|
||||||
|
json={"error": "Model not found"},
|
||||||
|
request=httpx.Request("GET", "https://huggingface.co/api/models/invalid/repo"),
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("httpx.AsyncClient.get", new_callable=AsyncMock, return_value=mock_resp):
|
||||||
|
response = client.get("/api/cookbook/hf-gguf-files?repo_id=invalid/repo")
|
||||||
|
assert response.status_code == 200
|
||||||
|
data = response.json()
|
||||||
|
assert data["ok"] is False
|
||||||
|
assert "HF API HTTP 404" in data["error"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_hf_gguf_files_exception_handling():
|
||||||
|
with patch("httpx.AsyncClient.get", new_callable=AsyncMock, side_effect=httpx.ConnectError("Network unreachable")):
|
||||||
|
response = client.get("/api/cookbook/hf-gguf-files?repo_id=TheBloke/Llama-2-7B-GGUF")
|
||||||
|
# Should gracefully return JSON error payload, not raise NameError / 500
|
||||||
|
assert response.status_code == 200
|
||||||
|
data = response.json()
|
||||||
|
assert data["ok"] is False
|
||||||
|
assert data["error"] == "HF API request failed"
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
"""Guard the only entry point to the Cookbook → Dependencies recipe panel.
|
||||||
|
|
||||||
|
The recipe panel (model picker, Pip/Docker toggle, commands and Run) is
|
||||||
|
rendered hidden, and only the `[data-dep-recipe-toggle]` caret shows it.
|
||||||
|
The HW Fit and serve-card deep links (`openCookbookDependencies(..., {
|
||||||
|
expandRecipe })`) open it by clicking that same caret. When the caret was
|
||||||
|
dropped from the row markup, nothing could open the panel (#6747).
|
||||||
|
"""
|
||||||
|
import re
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
JS_DIR = Path(__file__).resolve().parent.parent / "static" / "js"
|
||||||
|
COOKBOOK_JS = JS_DIR / "cookbook.js"
|
||||||
|
DIAGNOSIS_JS = JS_DIR / "cookbook-diagnosis.js"
|
||||||
|
|
||||||
|
|
||||||
|
def _row_renderer(source: str) -> str:
|
||||||
|
start = source.index("const hasRecipe = RECIPE_BACKENDS.has(pkg.name);")
|
||||||
|
end = source.index("+ recipePanel;", start)
|
||||||
|
return source[start:end]
|
||||||
|
|
||||||
|
|
||||||
|
def test_recipe_rows_render_the_panel_toggle():
|
||||||
|
row = _row_renderer(COOKBOOK_JS.read_text(encoding="utf-8"))
|
||||||
|
|
||||||
|
caret = re.search(r"const recipeCaret = hasRecipe\s*\?\s*`([^`]*)`\s*:\s*'';", row)
|
||||||
|
assert caret, "recipe rows must render a caret, or the recipe panel cannot be opened"
|
||||||
|
assert 'data-dep-recipe-toggle="${esc(pkg.name)}"' in caret.group(1)
|
||||||
|
assert 'aria-expanded="false"' in caret.group(1)
|
||||||
|
assert "+ recipeCaret" in row
|
||||||
|
|
||||||
|
|
||||||
|
def test_toggle_handler_and_deep_link_use_the_rendered_attribute():
|
||||||
|
cookbook = COOKBOOK_JS.read_text(encoding="utf-8")
|
||||||
|
diagnosis = DIAGNOSIS_JS.read_text(encoding="utf-8")
|
||||||
|
|
||||||
|
assert "list.querySelectorAll('[data-dep-recipe-toggle]')" in cookbook
|
||||||
|
assert "row.querySelector('[data-dep-recipe-toggle]')" in diagnosis
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
"""A completed blob's empty retry marker must not keep its model stalled."""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from routes.cookbook_helpers import _cached_model_scan_script
|
||||||
|
from routes.cookbook_output import HF_CACHE_COMPLETE_PROBE, HF_CACHE_INCOMPLETE_PROBE
|
||||||
|
|
||||||
|
_REPO = "fixture/stale-marker"
|
||||||
|
_STATES = ["complete", "stale", "empty-unpaired", "partial", "partial-paired", "empty-paired-empty"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(params=["custom", "hf-home", "hub-env"])
|
||||||
|
def cache(request, tmp_path):
|
||||||
|
root = tmp_path / "cache with spaces"
|
||||||
|
model = root / "hub" / ("models--" + _REPO.replace("/", "--"))
|
||||||
|
blobs = model / "blobs"
|
||||||
|
blobs.mkdir(parents=True)
|
||||||
|
snapshot = model / "snapshots" / "revision"
|
||||||
|
snapshot.mkdir(parents=True)
|
||||||
|
(snapshot / "config.json").write_text("{}")
|
||||||
|
env = {key: value for key, value in os.environ.items()
|
||||||
|
if key not in {"HF_HOME", "HUGGINGFACE_HUB_CACHE", "HF_HUB_CACHE"}}
|
||||||
|
if request.param == "custom":
|
||||||
|
argument = str(root)
|
||||||
|
scan = _cached_model_scan_script(add_hf_cache=str(root / "hub"))
|
||||||
|
else:
|
||||||
|
argument = ""
|
||||||
|
env["HF_HOME" if request.param == "hf-home" else "HUGGINGFACE_HUB_CACHE"] = str(
|
||||||
|
root if request.param == "hf-home" else root / "hub"
|
||||||
|
)
|
||||||
|
scan = _cached_model_scan_script()
|
||||||
|
return blobs, argument, env, scan
|
||||||
|
|
||||||
|
|
||||||
|
def _seed(blobs, state):
|
||||||
|
if state in {"complete", "stale", "partial-paired"}:
|
||||||
|
(blobs / "sha").write_bytes(b"completed model data")
|
||||||
|
elif state == "empty-paired-empty":
|
||||||
|
(blobs / "sha").touch()
|
||||||
|
if state != "complete":
|
||||||
|
(blobs / "sha.incomplete").write_bytes(b"partial" if state.startswith("partial") else b"")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("state", _STATES)
|
||||||
|
@pytest.mark.parametrize("probe", [HF_CACHE_COMPLETE_PROBE, HF_CACHE_INCOMPLETE_PROBE], ids=["complete", "incomplete"])
|
||||||
|
def test_cache_status_probes_distinguish_stale_and_partial(cache, state, probe):
|
||||||
|
blobs, argument, env, _ = cache
|
||||||
|
_seed(blobs, state)
|
||||||
|
before = {path.name: path.read_bytes() for path in blobs.iterdir()}
|
||||||
|
result = subprocess.run([sys.executable, "-c", probe, _REPO, argument],
|
||||||
|
env=env, capture_output=True, text=True, timeout=15)
|
||||||
|
complete = state in {"complete", "stale"}
|
||||||
|
expected = complete if probe == HF_CACHE_COMPLETE_PROBE else not complete
|
||||||
|
assert result.returncode == (0 if expected else 1), result.stderr
|
||||||
|
assert {path.name: path.read_bytes() for path in blobs.iterdir()} == before
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("state", _STATES)
|
||||||
|
def test_generated_inventory_agrees_without_mutating_cache(cache, state):
|
||||||
|
blobs, _, env, scan = cache
|
||||||
|
_seed(blobs, state)
|
||||||
|
before = {path.name: path.read_bytes() for path in blobs.iterdir()}
|
||||||
|
# Inventory's unrelated Ollama discovery stays off the network. Execute
|
||||||
|
# the actual generated scanner, including its cache-root resolution.
|
||||||
|
harness = (
|
||||||
|
"import urllib.request,shutil\n"
|
||||||
|
"def offline(*args,**kwargs): raise OSError('Ollama outside this fixture')\n"
|
||||||
|
"urllib.request.urlopen=offline\nshutil.which=lambda name: None\n"
|
||||||
|
+ scan
|
||||||
|
)
|
||||||
|
result = subprocess.run([sys.executable, "-c", harness], env=env,
|
||||||
|
capture_output=True, text=True, timeout=15)
|
||||||
|
assert result.returncode == 0, result.stderr
|
||||||
|
row = next(item for item in json.loads(result.stdout) if item["repo_id"] == _REPO)
|
||||||
|
assert row["has_incomplete"] == (state not in {"complete", "stale"})
|
||||||
|
if state in {"complete", "stale"}:
|
||||||
|
assert row["nb_files"] == 1
|
||||||
|
assert row["size_bytes"] == len(b"completed model data")
|
||||||
|
assert {path.name: path.read_bytes() for path in blobs.iterdir()} == before
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
"""Daily brief must query and display each calendar storage kind in local time."""
|
||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
from sqlalchemy.pool import StaticPool
|
||||||
|
|
||||||
|
from core import database
|
||||||
|
from src import builtin_actions
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def system_timezone(name):
|
||||||
|
previous = os.environ.get('TZ')
|
||||||
|
os.environ['TZ'] = name
|
||||||
|
time.tzset()
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
if previous is None:
|
||||||
|
os.environ.pop('TZ', None)
|
||||||
|
else:
|
||||||
|
os.environ['TZ'] = previous
|
||||||
|
time.tzset()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not hasattr(time, 'tzset'), reason='requires changing the process local timezone')
|
||||||
|
@pytest.mark.parametrize('zone', ['Europe/Berlin', 'America/Los_Angeles'])
|
||||||
|
def test_daily_brief_selects_local_day_and_orders_mixed_storage(monkeypatch, zone):
|
||||||
|
engine = create_engine('sqlite://', poolclass=StaticPool)
|
||||||
|
for model in [database.CalendarCal, database.CalendarEvent, database.Note]:
|
||||||
|
model.__table__.create(engine)
|
||||||
|
sessions = sessionmaker(bind=engine)
|
||||||
|
monkeypatch.setattr(database, 'SessionLocal', sessions)
|
||||||
|
from core.auth import AuthManager
|
||||||
|
monkeypatch.setattr(AuthManager, 'is_configured', property(lambda self: True))
|
||||||
|
from routes import email_helpers
|
||||||
|
def no_email(*args):
|
||||||
|
raise RuntimeError('No email account in this calendar-only test')
|
||||||
|
monkeypatch.setattr(email_helpers, '_imap_connect', no_email)
|
||||||
|
try:
|
||||||
|
with system_timezone(zone):
|
||||||
|
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
|
def utc(value):
|
||||||
|
return value.astimezone(timezone.utc).replace(tzinfo=None)
|
||||||
|
with sessions() as db:
|
||||||
|
for owner in ['alice', 'bob', None]:
|
||||||
|
db.add(database.CalendarCal(id=owner or 'shared', owner=owner, name=owner or 'shared'))
|
||||||
|
db.flush()
|
||||||
|
def add(uid, start, end, *, utc_row=False, all_day=False, owner='alice', status='confirmed'):
|
||||||
|
db.add(database.CalendarEvent(uid=uid, calendar_id=owner or 'shared', summary=uid,
|
||||||
|
dtstart=utc(start) if utc_row and not all_day else start,
|
||||||
|
dtend=utc(end) if utc_row and not all_day else end,
|
||||||
|
is_utc=utc_row, all_day=all_day, status=status))
|
||||||
|
add('early', today+timedelta(minutes=30), today+timedelta(hours=1), utc_row=True)
|
||||||
|
add('utc-ten', today+timedelta(hours=10), today+timedelta(hours=11), utc_row=True)
|
||||||
|
add('local-nine', today+timedelta(hours=9), today+timedelta(hours=10))
|
||||||
|
add('all-day', today, today+timedelta(days=1), utc_row=True, all_day=True)
|
||||||
|
add('next-day', today+timedelta(days=1,minutes=30), today+timedelta(days=1,hours=1), utc_row=True)
|
||||||
|
add('ended', today-timedelta(hours=1), today, utc_row=True)
|
||||||
|
add('cancelled', today+timedelta(hours=12), today+timedelta(hours=13), utc_row=True, status='cancelled')
|
||||||
|
add('foreign', today+timedelta(hours=12), today+timedelta(hours=13), utc_row=True, owner='bob')
|
||||||
|
add('unowned', today+timedelta(hours=12), today+timedelta(hours=13), owner=None)
|
||||||
|
db.commit()
|
||||||
|
text, success = asyncio.run(builtin_actions.action_daily_brief('alice'))
|
||||||
|
assert success, text
|
||||||
|
lines = ['all day all-day', '00:30 early', '09:00 local-nine', '10:00 utc-ten']
|
||||||
|
assert all(line in text for line in lines), text
|
||||||
|
assert [text.index(line) for line in lines] == sorted(text.index(line) for line in lines), text
|
||||||
|
for excluded in ['next-day', 'ended', 'cancelled', 'foreign', 'unowned']:
|
||||||
|
assert excluded not in text, text
|
||||||
|
finally:
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('day, hours', [('2026-03-29', 23), ('2026-10-25', 25)])
|
||||||
|
def test_utc_day_window_uses_offset_at_each_boundary(day, hours):
|
||||||
|
start = datetime.fromisoformat(day)
|
||||||
|
zone = ZoneInfo('Europe/Berlin')
|
||||||
|
begin = builtin_actions._daily_brief_utc_boundary(start, zone)
|
||||||
|
end = builtin_actions._daily_brief_utc_boundary(start+timedelta(days=1), zone)
|
||||||
|
assert end-begin == timedelta(hours=hours)
|
||||||
|
if hasattr(time, 'tzset'):
|
||||||
|
with system_timezone('Europe/Berlin'):
|
||||||
|
assert builtin_actions._daily_brief_utc_boundary(start) == begin
|
||||||
|
assert builtin_actions._daily_brief_utc_boundary(start+timedelta(days=1)) == end
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('utc_row, all_day, expected', [
|
||||||
|
(True, False, datetime(2026, 6, 1, 10)),
|
||||||
|
(False, False, datetime(2026, 6, 1, 8)),
|
||||||
|
(True, True, datetime(2026, 6, 1, 8)),
|
||||||
|
])
|
||||||
|
def test_local_start_preserves_floating_and_date_only_values(utc_row, all_day, expected):
|
||||||
|
event = SimpleNamespace(dtstart=datetime(2026,6,1,8), is_utc=utc_row, all_day=all_day)
|
||||||
|
assert builtin_actions._daily_brief_local_start(event, ZoneInfo('Europe/Berlin')) == expected
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
"""Issue #2137: never preselect an embedding or other non-chat model."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from core.database import ModelEndpoint
|
||||||
|
from routes import model_routes, prefs_routes
|
||||||
|
from tests.helpers.database import disposable_database
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def defaults_api(monkeypatch, tmp_path):
|
||||||
|
defaults = {}
|
||||||
|
with disposable_database(tmp_path) as factory:
|
||||||
|
monkeypatch.setattr(model_routes, "SessionLocal", factory)
|
||||||
|
monkeypatch.setattr(model_routes, "_load_settings", lambda: {})
|
||||||
|
monkeypatch.setattr(prefs_routes, "_load_for_user", lambda owner: defaults)
|
||||||
|
app = FastAPI()
|
||||||
|
app.state.auth_manager = SimpleNamespace(is_admin=lambda owner: False)
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def owner(request, call_next):
|
||||||
|
request.state.current_user = "alice"
|
||||||
|
return await call_next(request)
|
||||||
|
|
||||||
|
app.include_router(model_routes.setup_model_routes(model_discovery=None))
|
||||||
|
with TestClient(app) as client:
|
||||||
|
yield client, factory, defaults
|
||||||
|
|
||||||
|
|
||||||
|
def _endpoint(factory, *, cached=(), pinned=(), hidden=(), owner="alice", endpoint_id="ep"):
|
||||||
|
with factory() as db:
|
||||||
|
db.add(ModelEndpoint(
|
||||||
|
id=endpoint_id, name="Fixture", base_url="http://model.invalid/v1", owner=owner,
|
||||||
|
is_enabled=True, cached_models=json.dumps(cached),
|
||||||
|
pinned_models=json.dumps(pinned), hidden_models=json.dumps(hidden),
|
||||||
|
))
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("non_chat", [
|
||||||
|
"text-embedding-3-small", "nomic-embed-text:latest", "jina-reranker-v2-base",
|
||||||
|
"tts-1", "whisper-1", "gpt-image-1",
|
||||||
|
])
|
||||||
|
def test_automatic_default_skips_non_chat_models(defaults_api, non_chat):
|
||||||
|
client, factory, _ = defaults_api
|
||||||
|
_endpoint(factory, cached=[non_chat, "chat-model"])
|
||||||
|
assert client.get("/api/default-chat").json()["model"] == "chat-model"
|
||||||
|
|
||||||
|
|
||||||
|
def test_non_chat_saved_default_falls_back_to_visible_pinned_chat(defaults_api):
|
||||||
|
client, factory, defaults = defaults_api
|
||||||
|
_endpoint(factory, cached=["text-embedding-3-small", "hidden-chat"],
|
||||||
|
pinned=["jina-reranker-v2-base", "pinned-chat"], hidden=["hidden-chat"])
|
||||||
|
defaults.update(default_endpoint_id="ep", default_model="text-embedding-3-small")
|
||||||
|
assert client.get("/api/default-chat").json()["model"] == "pinned-chat"
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_chat_candidate_leaves_model_empty(defaults_api):
|
||||||
|
client, factory, defaults = defaults_api
|
||||||
|
_endpoint(factory, cached=["text-embedding-3-small", "hidden-chat"],
|
||||||
|
pinned=["jina-reranker-v2-base"], hidden=["hidden-chat"])
|
||||||
|
defaults.update(default_endpoint_id="ep", default_model="text-embedding-3-small")
|
||||||
|
result = client.get("/api/default-chat").json()
|
||||||
|
assert result == {"endpoint_id": "ep", "endpoint_url": "http://model.invalid/v1/chat/completions", "model": ""}
|
||||||
|
|
||||||
|
|
||||||
|
def test_valid_explicit_model_survives_stale_catalog(defaults_api):
|
||||||
|
client, factory, defaults = defaults_api
|
||||||
|
_endpoint(factory, cached=["text-embedding-3-small", "other-chat"])
|
||||||
|
defaults.update(default_endpoint_id="ep", default_model="my-custom-chat")
|
||||||
|
assert client.get("/api/default-chat").json()["model"] == "my-custom-chat"
|
||||||
|
|
||||||
|
|
||||||
|
def test_model_filter_preserves_owner_scope(defaults_api):
|
||||||
|
client, factory, defaults = defaults_api
|
||||||
|
_endpoint(factory, owner="bob", endpoint_id="bobs-endpoint", cached=["bobs-chat"])
|
||||||
|
_endpoint(factory, cached=["text-embedding-3-small", "alices-chat"])
|
||||||
|
defaults.update(default_endpoint_id="bobs-endpoint")
|
||||||
|
result = client.get("/api/default-chat").json()
|
||||||
|
assert result["endpoint_id"] == "ep"
|
||||||
|
assert result["model"] == "alices-chat"
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
"""ZIP exports keep language extensions and place collision suffixes before them."""
|
||||||
|
import io
|
||||||
|
import zipfile
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
from core.database import Base, Document
|
||||||
|
from routes.document import document_routes
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def exports(tmp_path, monkeypatch):
|
||||||
|
engine = create_engine(f"sqlite:///{tmp_path / 'documents.db'}", connect_args={"check_same_thread": False})
|
||||||
|
Base.metadata.create_all(engine)
|
||||||
|
factory = sessionmaker(bind=engine)
|
||||||
|
monkeypatch.setattr(document_routes, "SessionLocal", factory)
|
||||||
|
monkeypatch.setenv("AUTH_ENABLED", "true")
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def fixture_user(request: Request, call_next):
|
||||||
|
request.state.current_user = "alice"
|
||||||
|
return await call_next(request)
|
||||||
|
|
||||||
|
app.include_router(document_routes.setup_document_routes(None))
|
||||||
|
|
||||||
|
def add(doc_id, title, language, content="body\n", owner="alice"):
|
||||||
|
with factory() as db:
|
||||||
|
db.add(Document(id=doc_id, title=title, language=language,
|
||||||
|
current_content=content, owner=owner, is_active=True))
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
with TestClient(app) as client:
|
||||||
|
yield client, add
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("title,language,filename", [
|
||||||
|
("config.local", "python", "config.local.py"),
|
||||||
|
("report.v2", "markdown", "report.v2.md"),
|
||||||
|
("data.2024", "json", "data.2024.json"),
|
||||||
|
("module.ts", "typescript", "module.ts"),
|
||||||
|
("SCRIPT.PY", "python", "SCRIPT.PY"),
|
||||||
|
("résumé.v2", "text", "résumé.v2.txt"),
|
||||||
|
("unknown.v2", "unknown", "unknown.v2.txt"),
|
||||||
|
("draft", None, "draft.txt"),
|
||||||
|
("project/report.v2", "markdown", "projectreport.v2.md"),
|
||||||
|
])
|
||||||
|
def test_export_uses_the_language_extension_even_for_dotted_titles(exports, title, language, filename):
|
||||||
|
client, add = exports
|
||||||
|
body = "Original text: α\n"
|
||||||
|
add("doc", title, language, body)
|
||||||
|
response = client.post("/api/documents/export-zip", json={"ids": ["doc"]})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.headers["content-type"] == "application/zip"
|
||||||
|
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
|
||||||
|
assert archive.namelist() == [filename]
|
||||||
|
assert archive.read(filename).decode() == body
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("title", ["config.local", "config.local.py"])
|
||||||
|
def test_duplicate_names_keep_the_extension_at_the_end(exports, title):
|
||||||
|
client, add = exports
|
||||||
|
for i in range(3):
|
||||||
|
add(str(i), title, "python", f"body {i}")
|
||||||
|
response = client.post("/api/documents/export-zip", json={"ids": ["0", "1", "2"]})
|
||||||
|
assert response.status_code == 200
|
||||||
|
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
|
||||||
|
assert set(archive.namelist()) == {"config.local.py", "config.local-1.py", "config.local-2.py"}
|
||||||
|
assert {archive.read(name).decode() for name in archive.namelist()} == {"body 0", "body 1", "body 2"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_added_extension_collides_safely_with_an_existing_extension(exports):
|
||||||
|
client, add = exports
|
||||||
|
add("a", "report.v2", "markdown", "first")
|
||||||
|
add("b", "report.v2.md", "markdown", "second")
|
||||||
|
response = client.post("/api/documents/export-zip", json={"ids": ["a", "b"]})
|
||||||
|
assert response.status_code == 200
|
||||||
|
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
|
||||||
|
assert set(archive.namelist()) == {"report.v2.md", "report.v2-1.md"}
|
||||||
|
assert {archive.read(name) for name in archive.namelist()} == {b"first", b"second"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_foreign_documents_are_still_excluded_and_email_body_is_preserved(exports):
|
||||||
|
client, add = exports
|
||||||
|
add("owned", "reply.v2", "email", "Subject: Hello\n---\nBody")
|
||||||
|
add("foreign", "private", "python", "secret", owner="bob")
|
||||||
|
response = client.post("/api/documents/export-zip", json={"ids": ["owned", "foreign"]})
|
||||||
|
assert response.status_code == 200
|
||||||
|
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
|
||||||
|
assert archive.namelist() == ["reply.v2.eml"]
|
||||||
|
assert archive.read("reply.v2.eml") == b"Subject: Hello\r\n\r\nBody"
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
"""POST /api/documents/export-zip answers 400 for ids that are not a list of document ids."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
from tests.helpers.import_state import clear_fake_database_modules
|
||||||
|
|
||||||
|
clear_fake_database_modules()
|
||||||
|
|
||||||
|
import routes.document_routes as droutes
|
||||||
|
|
||||||
|
|
||||||
|
class _Req:
|
||||||
|
def __init__(self, body):
|
||||||
|
self.state = SimpleNamespace(current_user="tester")
|
||||||
|
self._body = body
|
||||||
|
|
||||||
|
async def json(self):
|
||||||
|
return self._body
|
||||||
|
|
||||||
|
|
||||||
|
def _export_zip():
|
||||||
|
router = droutes.setup_document_routes(MagicMock(), None)
|
||||||
|
for r in router.routes:
|
||||||
|
if getattr(r, "path", None) == "/api/documents/export-zip":
|
||||||
|
return r.endpoint
|
||||||
|
raise RuntimeError("export-zip route not found")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("body", [[], "x", 5, {}, {"ids": []}, {"ids": None}])
|
||||||
|
async def test_missing_ids_is_a_400(body):
|
||||||
|
with pytest.raises(HTTPException) as exc:
|
||||||
|
await _export_zip()(_Req(body))
|
||||||
|
assert exc.value.status_code == 400
|
||||||
|
assert exc.value.detail == "No documents specified"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("ids", [5, "abc", {"a": 1}, [1, 2], [["a"]], ["a", None]])
|
||||||
|
async def test_ids_that_are_not_a_list_of_strings_is_a_400(ids):
|
||||||
|
with pytest.raises(HTTPException) as exc:
|
||||||
|
await _export_zip()(_Req({"ids": ids}))
|
||||||
|
assert exc.value.status_code == 400
|
||||||
|
assert exc.value.detail == "ids must be a list of document ids"
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
"""Literal punctuation in library queries must not become SQL wildcards."""
|
||||||
|
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from core.database import Document
|
||||||
|
from routes.document import document_routes
|
||||||
|
from tests.helpers.database import disposable_database
|
||||||
|
|
||||||
|
|
||||||
|
CASES = [
|
||||||
|
("100%", "100% complete", "1000 complete"),
|
||||||
|
("a_b", "a_b", "axb"),
|
||||||
|
("%", "price 10%", "price 100"),
|
||||||
|
("_", "a_b", "axb"),
|
||||||
|
(r"path\notes", r"path\notes", "pathnotes"),
|
||||||
|
(r"path\_tag", r"path\_tag", r"path\Xtag"),
|
||||||
|
("50%_done", "50%_done", "500Xdone"),
|
||||||
|
("can't", "can't stop", "cannot stop"),
|
||||||
|
("mixed case", "Case ordered Mixed", "Mixed without"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("column", ["title", "current_content"])
|
||||||
|
@pytest.mark.parametrize("query,literal,lookalike", CASES)
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_library_search_matches_literal_tokens(tmp_path, monkeypatch, column, query, literal, lookalike):
|
||||||
|
with disposable_database(tmp_path) as factory:
|
||||||
|
monkeypatch.setattr(document_routes, "SessionLocal", factory)
|
||||||
|
with factory() as db:
|
||||||
|
for doc_id, text, owner, active, archived in [
|
||||||
|
("literal", literal, "alice", True, False),
|
||||||
|
("lookalike", lookalike, "alice", True, False),
|
||||||
|
("foreign", literal, "bob", True, False),
|
||||||
|
("inactive", literal, "alice", False, False),
|
||||||
|
("archived", literal, "alice", True, True),
|
||||||
|
]:
|
||||||
|
fields = {"title": "Document", "current_content": "Body", column: text}
|
||||||
|
db.add(Document(id=doc_id, owner=owner, is_active=active, archived=archived,
|
||||||
|
language="text", version_count=1, **fields))
|
||||||
|
db.commit()
|
||||||
|
router = document_routes.setup_document_routes(MagicMock())
|
||||||
|
endpoint = next(r.endpoint for r in router.routes if r.path == "/api/documents/library")
|
||||||
|
request = SimpleNamespace(state=SimpleNamespace(current_user="alice"))
|
||||||
|
result = await endpoint(request, search=query, language=None, sort="recent", offset=0, limit=20, archived=False)
|
||||||
|
assert result["total"] == 1
|
||||||
|
assert [row["id"] for row in result["documents"]] == ["literal"]
|
||||||
|
archived = await endpoint(request, search=query, language=None, sort="recent", offset=0, limit=20, archived=True)
|
||||||
|
assert [row["id"] for row in archived["documents"]] == ["archived"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_literal_search_keeps_cross_field_and_pagination_behavior(tmp_path, monkeypatch):
|
||||||
|
with disposable_database(tmp_path) as factory:
|
||||||
|
monkeypatch.setattr(document_routes, "SessionLocal", factory)
|
||||||
|
with factory() as db:
|
||||||
|
for doc_id, title, content in [
|
||||||
|
("first", "100% ready", "done"),
|
||||||
|
("second", "done", "100% ready"),
|
||||||
|
("lookalike", "1000 ready", "done"),
|
||||||
|
("missing-term", "100% ready", "waiting"),
|
||||||
|
]:
|
||||||
|
db.add(Document(id=doc_id, owner="alice", title=title, current_content=content,
|
||||||
|
is_active=True, archived=False, language="text", version_count=1))
|
||||||
|
db.commit()
|
||||||
|
router = document_routes.setup_document_routes(MagicMock())
|
||||||
|
endpoint = next(r.endpoint for r in router.routes if r.path == "/api/documents/library")
|
||||||
|
request = SimpleNamespace(state=SimpleNamespace(current_user="alice"))
|
||||||
|
results = [await endpoint(request, search="100% done", language="text", sort="alpha",
|
||||||
|
offset=offset, limit=1, archived=False) for offset in [0, 1]]
|
||||||
|
assert all(result["total"] == 2 for result in results)
|
||||||
|
assert {row["id"] for result in results for row in result["documents"]} == {"first", "second"}
|
||||||
@@ -93,7 +93,7 @@ def test_custom_filter_reuses_convolve_kernel():
|
|||||||
assert "convolve(readSource(snap), k, divisor, bias, 1)" in EFFECTS
|
assert "convolve(readSource(snap), k, divisor, bias, 1)" in EFFECTS
|
||||||
|
|
||||||
|
|
||||||
def test_new_filter_aliases_registered():
|
def test_blur_and_custom_filter_aliases_registered():
|
||||||
assert "'box-blur': 'blur-box'" in EDITOR
|
assert "'box-blur': 'blur-box'" in EDITOR
|
||||||
assert "'surface-blur': 'blur-surface'" in EDITOR
|
assert "'surface-blur': 'blur-surface'" in EDITOR
|
||||||
assert "custom: 'other-custom'" in EDITOR
|
assert "custom: 'other-custom'" in EDITOR
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"""DOM-XSS regressions for email plain-text linkification helpers."""
|
"""DOM-XSS regressions for email plain-text linkification helpers."""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import textwrap
|
import textwrap
|
||||||
@@ -100,3 +101,55 @@ def test_email_html_sanitizer_runs_to_fixpoint():
|
|||||||
assert "for (let i = 0; i < 4; i++)" in source
|
assert "for (let i = 0; i < 4; i++)" in source
|
||||||
assert "const next = _sanitizeHtmlOnce(out);" in source
|
assert "const next = _sanitizeHtmlOnce(out);" in source
|
||||||
assert "if (next === out) break;" in source
|
assert "if (next === out) break;" in source
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
|
||||||
|
def test_linkify_preserves_text_and_link_targets_in_a_real_dom():
|
||||||
|
cases = [
|
||||||
|
("See https://example.test/contact?email=jane@example.test&next=1 and bob@example.test.",
|
||||||
|
["https://example.test/contact?email=jane@example.test&next=1", "mailto:bob@example.test"]),
|
||||||
|
("https://alice@example.test/path", ["https://alice@example.test/path"]),
|
||||||
|
("www.example.test/?email=jane@example.test", ["https://www.example.test/?email=jane@example.test"]),
|
||||||
|
("<https://example.test/path> and \"jane@example.test\"",
|
||||||
|
["https://example.test/path", "mailto:jane@example.test"]),
|
||||||
|
("(https://example.test/path), jane@example.test!",
|
||||||
|
["https://example.test/path", "mailto:jane@example.test"]),
|
||||||
|
("https://example.test/?literal="&next=1", ["https://example.test/?literal="&next=1"]),
|
||||||
|
('<img src=x onerror="alert(1)"> jane@example.test', ["mailto:jane@example.test"]),
|
||||||
|
("No links\njust <plain> & text", []),
|
||||||
|
]
|
||||||
|
script = r'''
|
||||||
|
import { chromium } from 'playwright';
|
||||||
|
const browser = await chromium.launch({ headless: true });
|
||||||
|
try {
|
||||||
|
const page = await browser.newPage();
|
||||||
|
const origin = process.env.ODYSSEUS_TEST_STATIC_ORIGIN;
|
||||||
|
await page.goto(`${origin}/static/js/documentStats.js`);
|
||||||
|
const results = await page.evaluate(async (cases) => {
|
||||||
|
const { _escLinkify } = await import('/static/js/emailLibrary/utils.js');
|
||||||
|
return cases.map(([raw]) => {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
div.innerHTML = _escLinkify(raw);
|
||||||
|
return {
|
||||||
|
text: div.textContent,
|
||||||
|
hrefs: [...div.querySelectorAll('a')].map(a => a.getAttribute('href')),
|
||||||
|
otherElements: [...div.querySelectorAll('*')].map(e => e.tagName).filter(t => t !== 'A'),
|
||||||
|
unsafeBlankLink: [...div.querySelectorAll('a[target="_blank"]')]
|
||||||
|
.some(a => !a.relList.contains('noopener') || !a.relList.contains('noreferrer')),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}, JSON.parse(process.env.ODYSSEUS_LINKIFY_CASES));
|
||||||
|
console.log(JSON.stringify(results));
|
||||||
|
} finally {
|
||||||
|
await browser.close();
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
result = subprocess.run(
|
||||||
|
["node", "--input-type=module", "-e", script], cwd=_REPO,
|
||||||
|
capture_output=True, text=True, timeout=30,
|
||||||
|
env={**os.environ, "ODYSSEUS_LINKIFY_CASES": json.dumps(cases)},
|
||||||
|
)
|
||||||
|
assert result.returncode == 0, result.stderr
|
||||||
|
results = json.loads(result.stdout)
|
||||||
|
for (raw, hrefs), actual in zip(cases, results, strict=True):
|
||||||
|
assert actual == {"text": raw, "hrefs": hrefs, "otherElements": [], "unsafeBlankLink": False}, raw
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
"""The native FileList must survive resetting the compose file input."""
|
||||||
|
import json
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from tests.helpers.document_source import function_body
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("handler,uploader", [("_handleAttachUpload", "_uploadComposeFiles"), ("_handleMarkdownImageUpload", "_uploadMarkdownImages")])
|
||||||
|
@pytest.mark.parametrize("selections", [[["one.png"]], [["one.png", "two.png"]], [["one.png"], ["one.png"]], [[]]])
|
||||||
|
def test_compose_file_selection_survives_reset(tmp_path, selections, handler, uploader):
|
||||||
|
if not shutil.which("node"):
|
||||||
|
pytest.skip("node not installed")
|
||||||
|
repo = Path(__file__).resolve().parents[1]
|
||||||
|
for name, icon in [("one.png", "icon-192.png"), ("two.png", "icon-512.png")]:
|
||||||
|
shutil.copyfile(repo / "static/icons" / icon, tmp_path / name)
|
||||||
|
script = r"""
|
||||||
|
const fs=require('node:fs'),assert=require('node:assert/strict'),crypto=require('node:crypto');
|
||||||
|
let chromium;try{({chromium}=require('playwright'));}catch(e){if(e.code==='MODULE_NOT_FOUND')process.exit(77);throw e;}
|
||||||
|
(async()=>{
|
||||||
|
const browser=await chromium.launch({headless:true});
|
||||||
|
try{
|
||||||
|
const page=await browser.newPage();const errors=[];page.on('pageerror',e=>errors.push(e.message));
|
||||||
|
await page.goto(process.env.ODYSSEUS_TEST_STATIC_ORIGIN+'/static/js/storage.js');
|
||||||
|
await page.setContent('<input type="file" multiple aria-label="Compose attachments">');
|
||||||
|
await page.addScriptTag({content:`
|
||||||
|
window.uploads=[];window.finished=0;
|
||||||
|
async function __UPLOADER__(files){
|
||||||
|
for(const file of files){
|
||||||
|
const hash=Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256',await file.arrayBuffer()))).map(x=>x.toString(16).padStart(2,'0')).join('');
|
||||||
|
window.uploads.push({name:file.name,hash});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
__HANDLER__
|
||||||
|
document.querySelector('input').addEventListener('change',async event=>{
|
||||||
|
await __HANDLER_NAME__(event);window.finished++;
|
||||||
|
});
|
||||||
|
`});
|
||||||
|
const selections=JSON.parse(process.argv[1]),root=process.argv[2],expected=[];
|
||||||
|
for(const [index,names] of selections.entries()){
|
||||||
|
await page.getByLabel('Compose attachments').setInputFiles(names.map(name=>root+'/'+name));
|
||||||
|
await page.waitForFunction(n=>window.finished===n,index+1);
|
||||||
|
expected.push(...names.map(name=>({name,hash:crypto.createHash('sha256').update(fs.readFileSync(root+'/'+name)).digest('hex')})));
|
||||||
|
assert.deepEqual(await page.evaluate(()=>window.uploads),expected);
|
||||||
|
assert.equal(await page.getByLabel('Compose attachments').inputValue(),'');
|
||||||
|
assert.equal(await page.getByLabel('Compose attachments').evaluate(el=>el.files.length),0);
|
||||||
|
}
|
||||||
|
assert.deepEqual(errors,[]);console.log(JSON.stringify({files:expected.length}));
|
||||||
|
}finally{await browser.close();}
|
||||||
|
})().catch(e=>{console.error(e);process.exitCode=1;});
|
||||||
|
""".replace("__HANDLER__", function_body(handler)).replace("__HANDLER_NAME__", handler).replace("__UPLOADER__", uploader)
|
||||||
|
result = subprocess.run(["node", "-e", script, json.dumps(selections), str(tmp_path)],
|
||||||
|
cwd=Path(__file__).resolve().parents[1], capture_output=True,
|
||||||
|
text=True, timeout=30)
|
||||||
|
if result.returncode == 77:
|
||||||
|
pytest.skip("playwright not installed")
|
||||||
|
assert result.returncode == 0, result.stdout + result.stderr
|
||||||
|
assert json.loads(result.stdout)["files"] == sum(map(len, selections))
|
||||||
@@ -1,3 +1,8 @@
|
|||||||
|
import pytest
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
import routes.font_routes as font_routes
|
||||||
from routes.font_routes import _derive_family
|
from routes.font_routes import _derive_family
|
||||||
|
|
||||||
|
|
||||||
@@ -9,3 +14,37 @@ def test_derive_family_splits_common_family_suffixes():
|
|||||||
assert _derive_family("FiraCode-SemiBold.ttf") == "Fira Code"
|
assert _derive_family("FiraCode-SemiBold.ttf") == "Fira Code"
|
||||||
assert _derive_family("NotoSans-Bold.otf") == "Noto Sans"
|
assert _derive_family("NotoSans-Bold.otf") == "Noto Sans"
|
||||||
assert _derive_family("RobotoSlab-Bold.woff2") == "Roboto Slab"
|
assert _derive_family("RobotoSlab-Bold.woff2") == "Roboto Slab"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("filename,expected", [
|
||||||
|
("JetBrainsMono-SemiBoldItalic.woff2", "JetBrains Mono"),
|
||||||
|
("FiraCode-BoldItalic.ttf", "Fira Code"),
|
||||||
|
("NotoSans-ExtraLightItalic.otf", "Noto Sans"),
|
||||||
|
("RobotoSlab-UltraBoldOblique.WOFF2", "Roboto Slab"),
|
||||||
|
("SourceSansPro-Bold-Italic.woff", "Source Sans Pro"),
|
||||||
|
("FiraCode-SemiBold_Italic.ttf", "Fira Code"),
|
||||||
|
("FiraCode-Italic.woff2", "Fira Code"),
|
||||||
|
("FiraCode-VF.woff2", "Fira Code"),
|
||||||
|
("MyFont-Book.woff2", "My Font Book"),
|
||||||
|
])
|
||||||
|
def test_compound_weight_and_style_are_not_part_of_the_family(filename, expected):
|
||||||
|
assert _derive_family(filename) == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_custom_font_endpoint_groups_all_styles_under_one_family(tmp_path, monkeypatch):
|
||||||
|
files = ["JetBrainsMono-Regular.woff2", "JetBrainsMono-SemiBoldItalic.woff2",
|
||||||
|
"JetBrainsMono-Bold-Oblique.ttf"]
|
||||||
|
for name in [*files, "OtherFont-Regular.otf", "README.md"]:
|
||||||
|
(tmp_path / name).write_bytes(b"filename-only discovery fixture")
|
||||||
|
monkeypatch.setattr(font_routes, "CUSTOM_FONTS_DIR", str(tmp_path))
|
||||||
|
app = FastAPI()
|
||||||
|
app.include_router(font_routes.setup_font_routes())
|
||||||
|
with TestClient(app) as client:
|
||||||
|
response = client.get("/api/fonts/custom")
|
||||||
|
assert response.status_code == 200
|
||||||
|
fonts = response.json()["fonts"]
|
||||||
|
assert set(fonts) == {"JetBrains Mono", "Other Font"}
|
||||||
|
assert fonts["JetBrains Mono"] == [
|
||||||
|
{"file": name, "url": f"/static/fonts/custom/{name}", "format": name.rsplit(".", 1)[1]}
|
||||||
|
for name in sorted(files)
|
||||||
|
]
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ label with `[^*]+`, so they could not match a label like "Email *" / "State *"
|
|||||||
stayed empty and the exported PDF (and signed-reply attachment) came out blank
|
stayed empty and the exported PDF (and signed-reply attachment) came out blank
|
||||||
for that field, with no error.
|
for that field, with no error.
|
||||||
"""
|
"""
|
||||||
|
import pytest
|
||||||
|
|
||||||
from src.pdf_form_doc import render_form_as_markdown, parse_markdown_to_values
|
from src.pdf_form_doc import render_form_as_markdown, parse_markdown_to_values
|
||||||
|
|
||||||
|
|
||||||
@@ -38,3 +40,38 @@ def test_plain_labels_and_colon_values_unaffected():
|
|||||||
vals = parse_markdown_to_values(md)
|
vals = parse_markdown_to_values(md)
|
||||||
assert vals["name"] == "Alice"
|
assert vals["name"] == "Alice"
|
||||||
assert vals["time"] == "9:00 sharp"
|
assert vals["time"] == "9:00 sharp"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("name", ["Prénom", "Año", "İsim", "氏名", "اسم", "n²", "Cafe\u0301"])
|
||||||
|
@pytest.mark.parametrize("field_type, value", [
|
||||||
|
("text", "Alice"), ("checkbox", True), ("choice", "Yes"),
|
||||||
|
])
|
||||||
|
def test_unicode_field_names_survive_export_roundtrip(name, field_type, value):
|
||||||
|
fields = [{"name": name, "label": "Field *", "type": field_type,
|
||||||
|
"value": value, "options": ["Yes", "No"], "page": 1}]
|
||||||
|
markdown = render_form_as_markdown(fields, "upload", "Form")
|
||||||
|
marker_name = markdown.split("<!-- field=", 1)[1].split(" type=", 1)[0]
|
||||||
|
assert marker_name.isascii()
|
||||||
|
assert parse_markdown_to_values(markdown) == {name: value}
|
||||||
|
|
||||||
|
|
||||||
|
def test_previously_saved_unicode_markers_remain_exportable():
|
||||||
|
# Older imports emitted Unicode letters literally. Their persisted
|
||||||
|
# markdown must also work after upgrading, without re-importing the PDF.
|
||||||
|
markdown = (
|
||||||
|
"- **First name:** Alice <!-- field=Prénom type=text -->\n"
|
||||||
|
"- [x] **Consent** <!-- field=同意 type=checkbox -->\n"
|
||||||
|
"- **Year** [2025 / 2026]: 2026 <!-- field=Año type=choice -->\n"
|
||||||
|
)
|
||||||
|
assert parse_markdown_to_values(markdown) == {
|
||||||
|
"Prénom": "Alice", "同意": True, "Año": "2026",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_unicode_names_do_not_collide_with_literal_percent_or_ascii_names():
|
||||||
|
names = ["Prénom", "Pr%C3%A9nom", "Prenom", "Section.1_name-2"]
|
||||||
|
fields = [{"name": name, "type": "text", "value": str(i), "page": 1}
|
||||||
|
for i, name in enumerate(names)]
|
||||||
|
markdown = render_form_as_markdown(fields, "upload", "Form")
|
||||||
|
assert "field=Section.1_name-2 " in markdown
|
||||||
|
assert parse_markdown_to_values(markdown) == {name: str(i) for i, name in enumerate(names)}
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import asyncio
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
import routes.gallery_routes as gallery
|
||||||
|
|
||||||
|
|
||||||
|
def route(path, method):
|
||||||
|
return next(r for r in gallery.setup_gallery_routes().routes if r.path == path and method in r.methods)
|
||||||
|
|
||||||
|
|
||||||
|
def call(path, method, monkeypatch, body):
|
||||||
|
monkeypatch.setattr(gallery, 'get_current_user', lambda request: 'alice')
|
||||||
|
|
||||||
|
class Request:
|
||||||
|
async def json(self):
|
||||||
|
return body
|
||||||
|
|
||||||
|
return asyncio.run(route(path, method).endpoint(Request(), 'album-1'))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('body', [
|
||||||
|
{'name': ''},
|
||||||
|
{'name': ' '},
|
||||||
|
{'name': 123},
|
||||||
|
{'name': ['x']},
|
||||||
|
{'description': {'a': 1}},
|
||||||
|
[1],
|
||||||
|
])
|
||||||
|
def test_album_update_rejects_bad_input(body, monkeypatch):
|
||||||
|
with pytest.raises(HTTPException) as exc:
|
||||||
|
call('/api/gallery/albums/{album_id}', 'PUT', monkeypatch, body)
|
||||||
|
assert exc.value.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('op', ['add', 'remove'])
|
||||||
|
@pytest.mark.parametrize('body', [
|
||||||
|
{'image_ids': 'abc'},
|
||||||
|
{'image_ids': 5},
|
||||||
|
{'image_ids': None},
|
||||||
|
{'image_ids': [{'a': 1}]},
|
||||||
|
{'image_ids': ['ok', 1]},
|
||||||
|
[1],
|
||||||
|
])
|
||||||
|
def test_album_image_ids_must_be_a_list_of_strings(op, body, monkeypatch):
|
||||||
|
with pytest.raises(HTTPException) as exc:
|
||||||
|
call(f'/api/gallery/albums/{{album_id}}/{op}', 'POST', monkeypatch, body)
|
||||||
|
assert exc.value.status_code == 400
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import asyncio
|
||||||
|
import base64
|
||||||
|
import io
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi import HTTPException
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
import routes.gallery_routes as gallery
|
||||||
|
|
||||||
|
ROUTES = ['sharpen', 'remove-bg', 'denoise', 'upscale-local', 'enhance-face']
|
||||||
|
|
||||||
|
|
||||||
|
def route(name):
|
||||||
|
return next(r for r in gallery.setup_gallery_routes().routes if r.path == f'/api/image/{name}')
|
||||||
|
|
||||||
|
|
||||||
|
def call(name, monkeypatch, body):
|
||||||
|
monkeypatch.setattr(gallery, 'require_privilege', lambda *args: 'alice')
|
||||||
|
monkeypatch.setattr(gallery, '_local_image_models_enabled', lambda: True)
|
||||||
|
monkeypatch.setattr(gallery, '_remote_image_ops_base', lambda: '')
|
||||||
|
|
||||||
|
class Request:
|
||||||
|
async def json(self):
|
||||||
|
return body
|
||||||
|
|
||||||
|
return asyncio.run(route(name).endpoint(Request()))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('name', ROUTES)
|
||||||
|
@pytest.mark.parametrize('body', [
|
||||||
|
{},
|
||||||
|
{'image': '!!notb64'},
|
||||||
|
{'image': base64.b64encode(b'hello').decode()},
|
||||||
|
])
|
||||||
|
def test_bad_image_is_a_client_error(name, body, monkeypatch):
|
||||||
|
with pytest.raises(HTTPException) as exc:
|
||||||
|
call(name, monkeypatch, body)
|
||||||
|
assert exc.value.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
|
def test_sharpen_still_accepts_a_valid_image(monkeypatch):
|
||||||
|
output = io.BytesIO()
|
||||||
|
Image.new('RGB', (4, 4), 'red').save(output, format='PNG')
|
||||||
|
result = call('sharpen', monkeypatch, {'image': base64.b64encode(output.getvalue()).decode()})
|
||||||
|
assert Image.open(io.BytesIO(base64.b64decode(result['image']))).size == (4, 4)
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
"""Regression for #2123 (gallery part): library search must match multi-word
|
||||||
|
queries in any order and treat % / _ literally.
|
||||||
|
|
||||||
|
Old code built a single ``%{search}%`` LIKE over prompt/tags/ai_tags, so
|
||||||
|
"red car" missed "a car that is red", and a literal "%" matched every row.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
from sqlalchemy.pool import NullPool
|
||||||
|
|
||||||
|
import core.database as cdb
|
||||||
|
from core.database import GalleryImage
|
||||||
|
import routes.gallery_routes as gallery_routes
|
||||||
|
|
||||||
|
|
||||||
|
def _client_with_gallery(monkeypatch, tmp_path):
|
||||||
|
engine = create_engine(
|
||||||
|
f"sqlite:///{tmp_path / 'gallery_search.db'}",
|
||||||
|
connect_args={"check_same_thread": False},
|
||||||
|
poolclass=NullPool,
|
||||||
|
)
|
||||||
|
cdb.Base.metadata.create_all(engine)
|
||||||
|
session_factory = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||||
|
monkeypatch.setattr(gallery_routes, "SessionLocal", session_factory)
|
||||||
|
|
||||||
|
db = session_factory()
|
||||||
|
try:
|
||||||
|
db.add_all(
|
||||||
|
[
|
||||||
|
GalleryImage(
|
||||||
|
id="img-red-car",
|
||||||
|
filename=f"{uuid.uuid4().hex}.png",
|
||||||
|
prompt="a car that is red",
|
||||||
|
model="model-a",
|
||||||
|
tags="",
|
||||||
|
ai_tags="",
|
||||||
|
is_active=True,
|
||||||
|
file_size=10,
|
||||||
|
),
|
||||||
|
GalleryImage(
|
||||||
|
id="img-boat",
|
||||||
|
filename=f"{uuid.uuid4().hex}.png",
|
||||||
|
prompt="blue boat on the lake",
|
||||||
|
model="model-a",
|
||||||
|
tags="",
|
||||||
|
ai_tags="",
|
||||||
|
is_active=True,
|
||||||
|
file_size=10,
|
||||||
|
),
|
||||||
|
GalleryImage(
|
||||||
|
id="img-percent",
|
||||||
|
filename=f"{uuid.uuid4().hex}.png",
|
||||||
|
prompt="100% cotton shirt",
|
||||||
|
model="model-a",
|
||||||
|
tags="sale_2024",
|
||||||
|
ai_tags="",
|
||||||
|
is_active=True,
|
||||||
|
file_size=10,
|
||||||
|
),
|
||||||
|
GalleryImage(
|
||||||
|
id="img-backslash",
|
||||||
|
filename=f"{uuid.uuid4().hex}.png",
|
||||||
|
prompt=r"C:\photos\sunset",
|
||||||
|
model="model-a",
|
||||||
|
tags="",
|
||||||
|
ai_tags="",
|
||||||
|
is_active=True,
|
||||||
|
file_size=10,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
app.include_router(gallery_routes.setup_gallery_routes())
|
||||||
|
return TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_gallery_search_matches_words_in_any_order(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setenv("AUTH_ENABLED", "false")
|
||||||
|
client = _client_with_gallery(monkeypatch, tmp_path)
|
||||||
|
|
||||||
|
body = client.get("/api/gallery/library", params={"search": "red car"}).json()
|
||||||
|
ids = {item["id"] for item in body["items"]}
|
||||||
|
assert "img-red-car" in ids
|
||||||
|
assert "img-boat" not in ids
|
||||||
|
|
||||||
|
|
||||||
|
def test_gallery_search_escapes_percent_wildcard(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setenv("AUTH_ENABLED", "false")
|
||||||
|
client = _client_with_gallery(monkeypatch, tmp_path)
|
||||||
|
|
||||||
|
body = client.get("/api/gallery/library", params={"search": "%"}).json()
|
||||||
|
ids = {item["id"] for item in body["items"]}
|
||||||
|
assert ids == {"img-percent"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_gallery_search_escapes_underscore_wildcard(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setenv("AUTH_ENABLED", "false")
|
||||||
|
client = _client_with_gallery(monkeypatch, tmp_path)
|
||||||
|
|
||||||
|
# Only img-percent has a literal underscore (in tags "sale_2024").
|
||||||
|
# Unescaped, "_" matches any single char so every row matches.
|
||||||
|
body = client.get("/api/gallery/library", params={"search": "_"}).json()
|
||||||
|
ids = {item["id"] for item in body["items"]}
|
||||||
|
assert ids == {"img-percent"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_gallery_search_escapes_backslash(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setenv("AUTH_ENABLED", "false")
|
||||||
|
client = _client_with_gallery(monkeypatch, tmp_path)
|
||||||
|
|
||||||
|
# Only img-backslash has a literal backslash. The escape char itself
|
||||||
|
# must survive escaping instead of escaping the surrounding wildcards.
|
||||||
|
body = client.get("/api/gallery/library", params={"search": "\\"}).json()
|
||||||
|
ids = {item["id"] for item in body["items"]}
|
||||||
|
assert ids == {"img-backslash"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_gallery_tag_filter_escapes_wildcards(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setenv("AUTH_ENABLED", "false")
|
||||||
|
client = _client_with_gallery(monkeypatch, tmp_path)
|
||||||
|
|
||||||
|
# Unescaped, "%" / "_" match every row; escaped, only literal matches.
|
||||||
|
assert client.get("/api/gallery/library", params={"tag": "%"}).json()["items"] == []
|
||||||
|
|
||||||
|
body = client.get("/api/gallery/library", params={"tag": "_"}).json()
|
||||||
|
assert {item["id"] for item in body["items"]} == {"img-percent"}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
"""Native image calls must match the server schema and provide a prompt.
|
||||||
|
|
||||||
|
A missing prompt must be rejected before the fallback line parser mistakes the
|
||||||
|
whole arguments object for the requested image.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def _schemas():
|
||||||
|
import src.agent_tools # noqa: F401 (tool_schemas <-> agent_tools import cycle)
|
||||||
|
from src.tool_schemas import FUNCTION_TOOL_SCHEMAS
|
||||||
|
|
||||||
|
return {s["function"]["name"]: s["function"] for s in FUNCTION_TOOL_SCHEMAS}
|
||||||
|
|
||||||
|
|
||||||
|
def _convert(arguments):
|
||||||
|
import src.agent_tools # noqa: F401
|
||||||
|
from src.tool_schemas import function_call_to_tool_block
|
||||||
|
|
||||||
|
return function_call_to_tool_block("generate_image", json.dumps(arguments))
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_image_is_offered_to_native_function_calling_models():
|
||||||
|
schemas = _schemas()
|
||||||
|
assert "generate_image" in schemas, (
|
||||||
|
"generate_image has no native schema, so it is filtered out of the tools "
|
||||||
|
"sent to API models even when the selector picks it"
|
||||||
|
)
|
||||||
|
assert schemas["generate_image"]["parameters"]["required"] == ["prompt"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_native_call_reaches_the_executor_with_its_arguments_intact():
|
||||||
|
from src.tool_execution import _build_mcp_args
|
||||||
|
|
||||||
|
args = {"prompt": "a cat riding a bicycle", "model": "gpt-image-1",
|
||||||
|
"size": "1024x1024", "quality": "high"}
|
||||||
|
block = _convert(args)
|
||||||
|
assert block is not None and block.tool_type == "generate_image"
|
||||||
|
assert _build_mcp_args("generate_image", block.content) == args
|
||||||
|
|
||||||
|
|
||||||
|
def test_call_without_a_prompt_is_rejected_instead_of_drawing_its_own_arguments():
|
||||||
|
"""No prompt key means the line parser takes the whole JSON blob as the prompt,
|
||||||
|
so the model gets an image of its own arguments."""
|
||||||
|
assert _convert({"size": "512x512"}) is None
|
||||||
|
assert _convert({}) is None
|
||||||
|
assert _convert({"prompt": " "}) is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_advertised_parameters_are_the_ones_the_image_server_accepts():
|
||||||
|
"""Anything the schema advertises but the executor drops is a silent no-op."""
|
||||||
|
from mcp_servers.image_gen_server import list_tools
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
server_schema = asyncio.run(list_tools())[0].inputSchema
|
||||||
|
assert set(_schemas()["generate_image"]["parameters"]["properties"]) == set(
|
||||||
|
server_schema["properties"]
|
||||||
|
)
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
"""Peer injections retain their context role but hydrate as the named agent."""
|
||||||
|
import json
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from tests.helpers.js_modules import js_function_source
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
|
||||||
|
|
||||||
|
def _node(script, *args):
|
||||||
|
result = subprocess.run(['node', '--input-type=module', '-e', script, *map(json.dumps, args)],
|
||||||
|
cwd=ROOT, capture_output=True, text=True, timeout=15)
|
||||||
|
assert result.returncode == 0, result.stderr
|
||||||
|
return json.loads(result.stdout)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not shutil.which('node'), reason='node not installed')
|
||||||
|
@pytest.mark.parametrize('mode', ['parallel', 'round-robin'])
|
||||||
|
def test_peer_sync_persists_explicit_identity(mode):
|
||||||
|
source = (ROOT / 'static/js/group.js').read_text()
|
||||||
|
functions = '\n'.join(js_function_source(name, source) for name in ['_syncAllResponses', '_sendRoundRobin'])
|
||||||
|
data = _node(r'''
|
||||||
|
const source=JSON.parse(process.argv[1]),mode=JSON.parse(process.argv[2]);
|
||||||
|
const _models=[{mid:'model-a',display:'Model A',_groupName:'Persona A'}, {mid:'model-b',display:'Model B'}];
|
||||||
|
const _participantSessions=['a','b'],API_BASE='http://app',sent=[];
|
||||||
|
let _abortControllers=[];
|
||||||
|
const uiModule={scrollHistory(){}};
|
||||||
|
function _createGroupBubble(model){return {dataset:{raw:'Reply from '+model.mid}};}
|
||||||
|
async function _streamToHolder(){} function _saveState(){}
|
||||||
|
async function fetch(url,options){sent.push({url,...JSON.parse(options.body)});return {ok:true};}
|
||||||
|
eval(source+'\nglobalThis.runSync=async()=> mode==="parallel" ? _syncAllResponses(_models.map(_createGroupBubble)) : _sendRoundRobin("Hi",null);');
|
||||||
|
await globalThis.runSync();console.log(JSON.stringify(sent));
|
||||||
|
''', functions, mode)
|
||||||
|
assert len(data) == 2
|
||||||
|
for item in data:
|
||||||
|
message = item['messages'][0]
|
||||||
|
assert message['role'] == 'user' # Model context must retain peer attribution.
|
||||||
|
peer = 'model-b' if item['url'].endswith('/a/inject_messages') else 'model-a'
|
||||||
|
name = 'Model B' if peer == 'model-b' else 'Persona A'
|
||||||
|
assert message['content'] == f'[{name}]: Reply from {peer}'
|
||||||
|
assert message['metadata'] == {'group_peer': True, 'group_model': name, 'model': peer}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not shutil.which('node'), reason='node not installed')
|
||||||
|
@pytest.mark.parametrize('role,content,meta,expected_role,expected_content', [
|
||||||
|
('user', '[Persona A]: Reply', {'group_peer': True, 'group_model': 'Persona A'}, 'assistant', 'Reply'),
|
||||||
|
('user', '[Persona A]: Human text', {}, 'user', '[Persona A]: Human text'),
|
||||||
|
('user', 'Human text', {}, 'user', 'Human text'),
|
||||||
|
('assistant', 'Parent reply', {'group_model': 'Persona A'}, 'assistant', 'Parent reply'),
|
||||||
|
('user', 'No matching prefix', {'group_peer': True, 'group_model': 'Persona A'}, 'user', 'No matching prefix'),
|
||||||
|
('user', '[3]: Human text', {'group_peer': True, 'group_model': 3}, 'user', '[3]: Human text'),
|
||||||
|
('user', '[Persona A]: Human text', {'group_peer': False, 'group_model': 'Persona A'}, 'user', '[Persona A]: Human text'),
|
||||||
|
])
|
||||||
|
def test_history_uses_explicit_peer_identity_only(role, content, meta, expected_role, expected_content):
|
||||||
|
source = js_function_source('_renderHistoryMessage', (ROOT / 'static/js/sessions.js').read_text())
|
||||||
|
result = _node(r'''
|
||||||
|
const source=JSON.parse(process.argv[1]),message=JSON.parse(process.argv[2]);
|
||||||
|
function _displayHistoryContent(text){return text;}function _stripUserVisionBlocks(text){return text;}
|
||||||
|
function _addHistoryMessageWithFullRenderer(role,content,model,metadata){return {role,content,metadata};}
|
||||||
|
const run=eval('('+source+')');console.log(JSON.stringify(run(message,'session-model')));
|
||||||
|
''', source, {'role': role, 'content': content, 'metadata': meta})
|
||||||
|
assert result['role'] == expected_role
|
||||||
|
assert result['content'] == expected_content
|
||||||
|
assert result['metadata'] == {**meta, '_fromHistory': True}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
from services.hwfit.hf_discovery import _entry_from_collection_item
|
||||||
|
|
||||||
|
_SOURCE = {"owner": "Qwen", "provider": "Qwen"}
|
||||||
|
_COLLECTION = {"title": "Qwen3.6"}
|
||||||
|
|
||||||
|
|
||||||
|
def _item(repo_id, num_params):
|
||||||
|
item = {"id": repo_id, "type": "model", "pipeline_tag": "text-generation"}
|
||||||
|
if num_params is not None:
|
||||||
|
item["numParameters"] = num_params
|
||||||
|
return item
|
||||||
|
|
||||||
|
|
||||||
|
def test_moe_active_size_is_read_from_name_when_hf_reports_total():
|
||||||
|
# HF collection items carry numParameters for nearly every repo. The MoE
|
||||||
|
# active size only exists in the name, so it must still be parsed.
|
||||||
|
entry = _entry_from_collection_item(_COLLECTION, _item("Qwen/Qwen3.6-35B-A3B", 35_951_822_704), _SOURCE)
|
||||||
|
assert entry["parameters_raw"] == 35_951_822_704
|
||||||
|
assert entry["is_moe"] is True
|
||||||
|
assert entry["active_parameters"] == 3_000_000_000
|
||||||
|
|
||||||
|
|
||||||
|
def test_moe_entry_without_hf_count_falls_back_to_name():
|
||||||
|
entry = _entry_from_collection_item(_COLLECTION, _item("Qwen/Qwen3-Coder-30B-A3B-Instruct", None), _SOURCE)
|
||||||
|
assert entry["parameters_raw"] == 30_000_000_000
|
||||||
|
assert entry["is_moe"] is True
|
||||||
|
assert entry["active_parameters"] == 3_000_000_000
|
||||||
|
|
||||||
|
|
||||||
|
def test_dense_entry_keeps_hf_count_and_is_not_moe():
|
||||||
|
entry = _entry_from_collection_item(_COLLECTION, _item("Qwen/Qwen3.6-27B", 27_781_427_952), _SOURCE)
|
||||||
|
assert entry["parameters_raw"] == 27_781_427_952
|
||||||
|
assert "is_moe" not in entry
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from services.hwfit.hf_discovery import _entry_from_collection_item
|
||||||
|
|
||||||
|
_SOURCE = {"owner": "google", "provider": "Google"}
|
||||||
|
_COLLECTION = {"title": "collection"}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("repo_id,pipeline_tag", [
|
||||||
|
("google/timesfm-3.0-pytorch", "time-series-forecasting"),
|
||||||
|
("google/siglip2-base-patch16-224", "zero-shot-image-classification"),
|
||||||
|
("moonshotai/MoonViT-SO-400M", "image-feature-extraction"),
|
||||||
|
("zai-org/CogView4-6B", "text-to-image"),
|
||||||
|
("zai-org/CogVideoX-5b-I2V", "image-to-video"),
|
||||||
|
("zai-org/CogVideoX1.5-5B", "text-to-video"),
|
||||||
|
])
|
||||||
|
def test_non_llm_collection_models_are_skipped(repo_id, pipeline_tag):
|
||||||
|
item = {"id": repo_id, "type": "model", "pipeline_tag": pipeline_tag, "numParameters": 1_000_000_000}
|
||||||
|
assert _entry_from_collection_item(_COLLECTION, item, _SOURCE) is None
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("repo_id,pipeline_tag", [
|
||||||
|
("google/gemma-4-12B-it", "image-text-to-text"),
|
||||||
|
("Qwen/Qwen3.6-27B", "text-generation"),
|
||||||
|
("Qwen/Qwen3-Embedding-4B", "feature-extraction"),
|
||||||
|
("NousResearch/DeepHermes-Egregore-v2-RLAIF-8b-Atropos", "reinforcement-learning"),
|
||||||
|
("Qwen/Qwen3-Next-80B-A3B-Instruct", ""),
|
||||||
|
])
|
||||||
|
def test_servable_collection_models_are_kept(repo_id, pipeline_tag):
|
||||||
|
item = {"id": repo_id, "type": "model", "pipeline_tag": pipeline_tag, "numParameters": 4_000_000_000}
|
||||||
|
assert _entry_from_collection_item(_COLLECTION, item, _SOURCE) is not None
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
"""Explicit quality choices must take precedence over the configured default."""
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("caller", ["json", "lines", "mcp", "edit"])
|
||||||
|
@pytest.mark.parametrize("requested,configured,expected", [
|
||||||
|
("medium", "high", "medium"),
|
||||||
|
("medium", "low", "medium"),
|
||||||
|
("low", "high", "low"),
|
||||||
|
("high", "low", "high"),
|
||||||
|
("auto", "high", "auto"),
|
||||||
|
(None, "high", "high"),
|
||||||
|
(None, None, "medium"),
|
||||||
|
])
|
||||||
|
def test_image_quality_precedence(monkeypatch, tmp_path, caller, requested, configured, expected):
|
||||||
|
from src import ai_interaction, settings
|
||||||
|
from mcp_servers.image_gen_server import call_tool
|
||||||
|
|
||||||
|
config = {"image_gen_enabled": True, "image_model": "gpt-image-1"}
|
||||||
|
if configured is not None:
|
||||||
|
config["image_quality"] = configured
|
||||||
|
monkeypatch.setattr(settings, "load_settings", lambda: config)
|
||||||
|
monkeypatch.setattr(ai_interaction, "_resolve_model", lambda *args, **kwargs:
|
||||||
|
("http://fixture.invalid/v1/chat/completions", "gpt-image-1", {}))
|
||||||
|
captured = []
|
||||||
|
|
||||||
|
class Response:
|
||||||
|
status_code = 418
|
||||||
|
text = "Fixture captured request"
|
||||||
|
|
||||||
|
def json(self):
|
||||||
|
return {"error": {"message": self.text}}
|
||||||
|
|
||||||
|
class Client:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def __aenter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def post(self, url, **kwargs):
|
||||||
|
captured.append(kwargs.get("json") or kwargs.get("data"))
|
||||||
|
return Response()
|
||||||
|
|
||||||
|
monkeypatch.setattr("httpx.AsyncClient", Client)
|
||||||
|
args = {"prompt": "Fixture image", "model": "gpt-image-1"}
|
||||||
|
if requested is not None:
|
||||||
|
args["quality"] = requested
|
||||||
|
if caller == "json":
|
||||||
|
operation = ai_interaction.do_generate_image(json.dumps(args), incognito=True)
|
||||||
|
elif caller == "lines":
|
||||||
|
content = "Fixture image\ngpt-image-1\n1024x1024"
|
||||||
|
if requested is not None:
|
||||||
|
content += "\n" + requested
|
||||||
|
operation = ai_interaction.do_generate_image(content, incognito=True)
|
||||||
|
elif caller == "mcp":
|
||||||
|
operation = call_tool("generate_image", args)
|
||||||
|
else:
|
||||||
|
source = tmp_path / "fixture.png"
|
||||||
|
Image.new("RGB", (2, 2), "white").save(source)
|
||||||
|
kwargs = {} if requested is None else {"quality": requested}
|
||||||
|
operation = ai_interaction.do_edit_image("Fixture image", str(source), size="1024x1024",
|
||||||
|
incognito=True, **kwargs)
|
||||||
|
result = asyncio.run(operation)
|
||||||
|
assert "418" in str(result), result
|
||||||
|
assert len(captured) == 1
|
||||||
|
assert captured[0]["quality"] == expected
|
||||||
@@ -177,3 +177,28 @@ def test_update_integration_rejects_invalid_base_url_without_changing_existing(
|
|||||||
assert exc.value.status_code == 400
|
assert exc.value.status_code == 400
|
||||||
assert exc.value.detail == message
|
assert exc.value.detail == message
|
||||||
assert integrations.load_integrations()[0]["base_url"] == "https://example.test"
|
assert integrations.load_integrations()[0]["base_url"] == "https://example.test"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("body", [[], "x", 5, None])
|
||||||
|
def test_create_integration_rejects_non_object_body(integrations_routes, body):
|
||||||
|
endpoint, session_cookie, http_exception = integrations_routes
|
||||||
|
create_integration = endpoint("/api/auth/integrations", "POST")
|
||||||
|
|
||||||
|
with pytest.raises(http_exception) as exc:
|
||||||
|
asyncio.run(create_integration(_JsonRequest(body, session_cookie)))
|
||||||
|
|
||||||
|
assert exc.value.status_code == 400
|
||||||
|
assert integrations.load_integrations() == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("body", [[], "x", 5, None])
|
||||||
|
def test_update_integration_rejects_non_object_body(integrations_routes, body):
|
||||||
|
endpoint, session_cookie, http_exception = integrations_routes
|
||||||
|
update_integration = endpoint("/api/auth/integrations/{integration_id}", "PUT")
|
||||||
|
integrations.save_integrations([{"id": "existing", "name": "Original", "base_url": "https://example.test"}])
|
||||||
|
|
||||||
|
with pytest.raises(http_exception) as exc:
|
||||||
|
asyncio.run(update_integration("existing", _JsonRequest(body, session_cookie)))
|
||||||
|
|
||||||
|
assert exc.value.status_code == 400
|
||||||
|
assert integrations.load_integrations()[0]["name"] == "Original"
|
||||||
|
|||||||
@@ -1091,6 +1091,121 @@ def test_degenerate_stream_guard_allows_long_rows_with_changing_values():
|
|||||||
assert guard.check(rows) is None
|
assert guard.check(rows) is None
|
||||||
|
|
||||||
|
|
||||||
|
def _latex_matrix(cell):
|
||||||
|
rows = (" & ".join(cell(i, j) for j in range(8)) for i in range(8))
|
||||||
|
return "$$\\begin{pmatrix}\n" + " \\\\\n".join(rows) + "\n\\end{pmatrix}$$\n"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("text", [
|
||||||
|
_latex_matrix(lambda i, j: "0"),
|
||||||
|
_latex_matrix(lambda i, j: "1" if i == j else "0"),
|
||||||
|
_latex_matrix(lambda i, j: "2e10"),
|
||||||
|
_latex_matrix(lambda i, j: "1e-5"),
|
||||||
|
_latex_matrix(lambda i, j: "1.25e-3"),
|
||||||
|
_latex_matrix(lambda i, j: "-0.125"),
|
||||||
|
_latex_matrix(lambda i, j: "0.0"),
|
||||||
|
_latex_matrix(lambda i, j: "3.14"),
|
||||||
|
"buf = [" + ", ".join("0" for _ in range(64)) + "]\n",
|
||||||
|
"buf = [" + ", ".join("1.25e-3" for _ in range(64)) + "]\n",
|
||||||
|
"buf = [" + ", ".join("-0.125" for _ in range(64)) + "]\n",
|
||||||
|
"t,v1,v2,v3\n" + "\n".join(f"{i},1.25e-3,-0.125,2e10" for i in range(25)) + "\n",
|
||||||
|
"\n".join(f"{i}. Solve: $\\frac{{{i}}}{{{i+1}}}x + \\frac{{1}}{{{i+2}}} = \\frac{{{i+3}}}{{{i+4}}}$" for i in range(1, 20)) + "\n",
|
||||||
|
"<svg>" + "".join(f'<rect x="{i*10}" y="{j*10}" width="10" height="10" fill="#abc"/>' for i in range(4) for j in range(4)) + "</svg>\n",
|
||||||
|
"{" + ", ".join(f'"key_{i}": "translation_{i}"' for i in range(40)) + "}\n",
|
||||||
|
])
|
||||||
|
@pytest.mark.parametrize("chunk", [1, 3, 6, 8, 16, 32, 64, 128])
|
||||||
|
def test_degenerate_stream_guard_allows_numeric_data(text, chunk):
|
||||||
|
"""#5560: legitimate matrices, scientific numbers, tables, and structured data
|
||||||
|
repeat values or formatting by design; they are not token collapse."""
|
||||||
|
guard = llm_core._DegenerateStreamGuard("math-model")
|
||||||
|
|
||||||
|
assert all(guard.check(text[i:i + chunk]) is None for i in range(0, len(text), chunk))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("chunk", [1, 6, 64, 3000])
|
||||||
|
def test_degenerate_stream_guard_allows_numeric_data_full_and_char_stream(chunk):
|
||||||
|
text = _latex_matrix(lambda i, j: "1.25e-3")
|
||||||
|
guard = llm_core._DegenerateStreamGuard("math-model")
|
||||||
|
|
||||||
|
assert all(guard.check(text[i:i + chunk]) is None for i in range(0, len(text), chunk))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("loop_text", [
|
||||||
|
"0 " * 3000,
|
||||||
|
"1.25e-3 " * 3000,
|
||||||
|
"1 2 " * 1500,
|
||||||
|
])
|
||||||
|
@pytest.mark.parametrize("chunk", [1, 6, 32, 64, 3000])
|
||||||
|
def test_degenerate_stream_guard_still_stops_an_endless_number_run(loop_text, chunk):
|
||||||
|
guard = llm_core._DegenerateStreamGuard("looping-model")
|
||||||
|
|
||||||
|
stopped = False
|
||||||
|
for i in range(0, len(loop_text), chunk):
|
||||||
|
res = guard.check(loop_text[i:i + chunk])
|
||||||
|
if res is not None:
|
||||||
|
assert "numbers in a row" in res
|
||||||
|
stopped = True
|
||||||
|
break
|
||||||
|
assert stopped
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("loop_text", [
|
||||||
|
"row 0 0 0 0 " * 300,
|
||||||
|
"Step 1 2 3 " * 200,
|
||||||
|
])
|
||||||
|
@pytest.mark.parametrize("chunk", [1, 6, 32, 64, 1500])
|
||||||
|
def test_degenerate_stream_guard_still_stops_a_mixed_word_number_loop(loop_text, chunk):
|
||||||
|
guard = llm_core._DegenerateStreamGuard("looping-model")
|
||||||
|
|
||||||
|
stopped = False
|
||||||
|
for i in range(0, len(loop_text), chunk):
|
||||||
|
res = guard.check(loop_text[i:i + chunk])
|
||||||
|
if res is not None:
|
||||||
|
assert "repeated" in res
|
||||||
|
stopped = True
|
||||||
|
break
|
||||||
|
assert stopped
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("loop_text", [
|
||||||
|
"Summer " * 80,
|
||||||
|
"also be a software developer mode " * 30,
|
||||||
|
])
|
||||||
|
@pytest.mark.parametrize("chunk", [1, 6, 32, 64, 500])
|
||||||
|
def test_degenerate_stream_guard_still_stops_repeated_words_and_phrases(loop_text, chunk):
|
||||||
|
guard = llm_core._DegenerateStreamGuard("looping-model")
|
||||||
|
|
||||||
|
stopped = False
|
||||||
|
for i in range(0, len(loop_text), chunk):
|
||||||
|
res = guard.check(loop_text[i:i + chunk])
|
||||||
|
if res is not None:
|
||||||
|
assert "repeated" in res
|
||||||
|
stopped = True
|
||||||
|
break
|
||||||
|
assert stopped
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("bad_token", [
|
||||||
|
"1face ",
|
||||||
|
"1e ",
|
||||||
|
"25e ",
|
||||||
|
"0x12 ",
|
||||||
|
])
|
||||||
|
@pytest.mark.parametrize("chunk", [1, 6, 32, 64, 250])
|
||||||
|
def test_degenerate_stream_guard_stops_alphanumeric_repetition_resembling_numbers(bad_token, chunk):
|
||||||
|
guard = llm_core._DegenerateStreamGuard("looping-model")
|
||||||
|
text = bad_token * 50
|
||||||
|
|
||||||
|
stopped = False
|
||||||
|
for i in range(0, len(text), chunk):
|
||||||
|
res = guard.check(text[i:i + chunk])
|
||||||
|
if res is not None:
|
||||||
|
assert "repeated" in res
|
||||||
|
stopped = True
|
||||||
|
break
|
||||||
|
assert stopped
|
||||||
|
|
||||||
|
|
||||||
def test_terminal_stream_retries_degenerate_generation_before_emitting(monkeypatch):
|
def test_terminal_stream_retries_degenerate_generation_before_emitting(monkeypatch):
|
||||||
calls = 0
|
calls = 0
|
||||||
requests = []
|
requests = []
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ async def test_cron_create_edit_resume_and_invalid_edit_rollback(storage):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@pytest.mark.parametrize('expression', [None, '', 'bad', '99 99 * * *'])
|
@pytest.mark.parametrize('expression', [None, '', 'bad', '99 99 * * *', '* * * * * *', '* * * * * * *'])
|
||||||
async def test_invalid_cron_does_not_create_inert_task(storage, expression):
|
async def test_invalid_cron_does_not_create_inert_task(storage, expression):
|
||||||
result = await call(action='create', name='Invalid', prompt='Test', schedule='cron',
|
result = await call(action='create', name='Invalid', prompt='Test', schedule='cron',
|
||||||
cron_expression=expression)
|
cron_expression=expression)
|
||||||
@@ -171,3 +171,21 @@ async def test_invalid_cron_retime_rolls_back_all_edits(storage):
|
|||||||
row = db.get(database.ScheduledTask, result['task_id'])
|
row = db.get(database.ScheduledTask, result['task_id'])
|
||||||
assert row.name == 'Digest' and row.cron_expression == '15 9 * * 1,3,5'
|
assert row.name == 'Digest' and row.cron_expression == '15 9 * * 1,3,5'
|
||||||
assert row.next_run == datetime(2026, 10, 5, 9, 15)
|
assert row.next_run == datetime(2026, 10, 5, 9, 15)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('expression', ['* * * * *', '15 9 * * 1,3,5', '*/5 * * * *'])
|
||||||
|
def test_is_valid_cron_accepts_five_fields(expression):
|
||||||
|
from src.task_scheduler import is_valid_cron
|
||||||
|
assert is_valid_cron(expression)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('expression', [None, '', 'bad', '99 99 * * *', '* * * * * *', '* * * * * * *', '* * * *'])
|
||||||
|
def test_is_valid_cron_rejects_everything_else(expression):
|
||||||
|
from src.task_scheduler import is_valid_cron
|
||||||
|
assert not is_valid_cron(expression)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('expression', ['* * * * * *', '* * * * * * *'])
|
||||||
|
def test_compute_next_run_refuses_a_seconds_field(expression):
|
||||||
|
from src.task_scheduler import compute_next_run
|
||||||
|
assert compute_next_run('cron', '09:00', None, None, cron_expression=expression) is None
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
"""A malformed provider row must not hide its valid neighboring models."""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from src.model_discovery import ModelDiscovery
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('wrapped', [False, True], ids=['bare', 'openai'])
|
||||||
|
@pytest.mark.parametrize('invalid_id', [None, '', 0, False, [], {}, 42, True, ['bad'], {'bad': 'id'}])
|
||||||
|
def test_discovery_keeps_valid_models_beside_invalid_ids(monkeypatch, wrapped, invalid_id):
|
||||||
|
discovery = ModelDiscovery('fixture')
|
||||||
|
rows = [{'id': '/valid-model'}, {'id': invalid_id}, 'not a row', None, {'id': 'second-model'}]
|
||||||
|
payload = {'data': rows} if wrapped else rows
|
||||||
|
|
||||||
|
class Response:
|
||||||
|
is_success = True
|
||||||
|
def json(self):return payload
|
||||||
|
|
||||||
|
monkeypatch.setattr('src.model_discovery.httpx.get', lambda *_args, **_kwargs: Response())
|
||||||
|
monkeypatch.setattr(discovery, '_fingerprint_provider', lambda *_:'fixture')
|
||||||
|
result = discovery._check_port('fixture', 8080)
|
||||||
|
assert result is not None
|
||||||
|
assert result['models'] == ['/valid-model', 'second-model']
|
||||||
|
assert result['models_display'] == ['valid-model', 'second-model']
|
||||||
|
assert result['provider'] == 'fixture'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('payload', [{'data': [{'id': 42}]}, [{'id': ['bad']}], {'data': []}])
|
||||||
|
def test_discovery_with_no_valid_ids_stays_unavailable(monkeypatch, payload):
|
||||||
|
class Response:
|
||||||
|
is_success = True
|
||||||
|
def json(self):return payload
|
||||||
|
monkeypatch.setattr('src.model_discovery.httpx.get', lambda *_args, **_kwargs:Response())
|
||||||
|
assert ModelDiscovery('fixture')._check_port('fixture',8080) is None
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
"""Explicit null must clear nullable note fields; omission must preserve them."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from tests.helpers.database import disposable_database
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def client(tmp_path, monkeypatch):
|
||||||
|
import routes.note.note_routes as routes
|
||||||
|
|
||||||
|
with disposable_database(tmp_path) as factory:
|
||||||
|
monkeypatch.setattr(routes, "SessionLocal", factory)
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def identity(request, call_next):
|
||||||
|
request.state.current_user = request.headers.get("x-test-user", "alice")
|
||||||
|
return await call_next(request)
|
||||||
|
|
||||||
|
monkeypatch.setattr(routes, "_scheduler_ref", None)
|
||||||
|
app.include_router(routes.setup_note_routes())
|
||||||
|
with TestClient(app) as api:
|
||||||
|
yield api
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("field,value", [
|
||||||
|
("due_date", "2030-10-08T09:00:00"),
|
||||||
|
("label", "home"),
|
||||||
|
("image_url", "https://example.com/note.png"),
|
||||||
|
("content", "Saved body"),
|
||||||
|
("items", [{"text": "Saved item", "done": False}]),
|
||||||
|
("color", "blue"),
|
||||||
|
("gallery_id", "gallery-1"),
|
||||||
|
("agent_session_id", "chat-1"),
|
||||||
|
])
|
||||||
|
def test_explicit_null_clears_saved_field(client, field, value):
|
||||||
|
created = client.post("/api/notes", json={"title": "Test note"})
|
||||||
|
assert created.status_code == 200, created.text
|
||||||
|
path = f"/api/notes/{created.json()['id']}"
|
||||||
|
populated = client.put(path, json={field: value})
|
||||||
|
assert populated.status_code == 200, populated.text
|
||||||
|
assert client.get(path).json()[field] == value
|
||||||
|
|
||||||
|
updated = client.put(path, json={field: None})
|
||||||
|
assert updated.status_code == 200, updated.text
|
||||||
|
saved = client.get(path)
|
||||||
|
assert saved.status_code == 200, saved.text
|
||||||
|
assert saved.json()[field] is None
|
||||||
|
assert updated.json()[field] is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_omitted_fields_survive_title_edit(client):
|
||||||
|
fields = {
|
||||||
|
"due_date": "2030-10-08T09:00:00",
|
||||||
|
"label": "home",
|
||||||
|
"image_url": "https://example.com/note.png",
|
||||||
|
"content": "Saved body",
|
||||||
|
"items": [{"text": "Saved item", "done": False}],
|
||||||
|
"color": "blue",
|
||||||
|
"gallery_id": "gallery-1",
|
||||||
|
}
|
||||||
|
created = client.post("/api/notes", json={"title": "Before", **fields})
|
||||||
|
assert created.status_code == 200, created.text
|
||||||
|
path = f"/api/notes/{created.json()['id']}"
|
||||||
|
updated = client.put(path, json={"title": "After"})
|
||||||
|
assert updated.status_code == 200, updated.text
|
||||||
|
saved = client.get(path).json()
|
||||||
|
assert saved["title"] == "After"
|
||||||
|
for field, value in fields.items():
|
||||||
|
assert saved[field] == value
|
||||||
|
|
||||||
|
|
||||||
|
def test_null_update_cannot_clear_another_users_note(client):
|
||||||
|
created = client.post("/api/notes", json={"title": "Private", "label": "home"})
|
||||||
|
assert created.status_code == 200, created.text
|
||||||
|
path = f"/api/notes/{created.json()['id']}"
|
||||||
|
response = client.put(path, json={"label": None}, headers={"x-test-user": "bob"})
|
||||||
|
assert response.status_code == 404
|
||||||
|
assert client.get(path).json()["label"] == "home"
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
"""A detached Notes descendant must not hold output open or kill a bystander."""
|
||||||
|
import os
|
||||||
|
import runpy
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from src import process_lifecycle as lifecycle
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(os.name == "nt", reason="POSIX detached-session regression; Windows uses taskkill tree")
|
||||||
|
def test_notes_cleanup_reaps_detached_pipe_holder_and_preserves_bystander():
|
||||||
|
stop = runpy.run_path(str(ROOT / "scripts/verify_notes_browser_runtime.py"))["_stop_owned_server"]
|
||||||
|
child = "import time; time.sleep(60)"
|
||||||
|
program = ("import subprocess,sys,time; "
|
||||||
|
f"p=subprocess.Popen([sys.executable,'-c',{child!r}],start_new_session=True); "
|
||||||
|
"print(p.pid,flush=True); time.sleep(60)")
|
||||||
|
with subprocess.Popen([sys.executable, "-c", child]) as bystander:
|
||||||
|
server = subprocess.Popen([sys.executable, "-c", program], stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT, text=True, start_new_session=True)
|
||||||
|
root_identity = lifecycle.ProcessIdentity.capture(server.pid)
|
||||||
|
detached = None
|
||||||
|
try:
|
||||||
|
child_pid = int(server.stdout.readline())
|
||||||
|
detached = lifecycle.ProcessIdentity.capture(child_pid)
|
||||||
|
assert os.getpgid(child_pid) == child_pid
|
||||||
|
stop(server, root_identity)
|
||||||
|
assert server.poll() is not None
|
||||||
|
assert detached.exited()
|
||||||
|
assert bystander.poll() is None, "cleanup touched an unrelated process"
|
||||||
|
finally:
|
||||||
|
# Test-owned fallback cleanup also runs when the negative control
|
||||||
|
# intentionally substitutes the old parent-only teardown.
|
||||||
|
targets = [identity for identity in (detached, root_identity) if identity]
|
||||||
|
lifecycle.terminate_identities(targets, steps=lifecycle.term_kill_steps(1))
|
||||||
|
server.wait(timeout=3)
|
||||||
|
if server.stdout:
|
||||||
|
server.stdout.close()
|
||||||
|
bystander.terminate()
|
||||||
|
bystander.wait(timeout=3)
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
"""Actual embedded PDF images must reach vision as a decodable PNG."""
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
from pypdf import PdfReader, PdfWriter
|
||||||
|
from pypdf.generic import DecodedStreamObject, DictionaryObject, NameObject, NumberObject
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from src import document_processor as dp
|
||||||
|
|
||||||
|
|
||||||
|
def make_pdf(tmp_path, mode):
|
||||||
|
colors = {"CMYK": (0, 255, 255, 0), "RGB": (255, 0, 0), "L": 127}
|
||||||
|
path = tmp_path / (mode + ".pdf")
|
||||||
|
image = Image.new(mode, (128, 96), colors[mode])
|
||||||
|
if mode == "CMYK":
|
||||||
|
# A raw DeviceCMYK XObject avoids JPEG/Adobe inversion differences in
|
||||||
|
# Pillow-generated PDFs; the extracted color samples are unambiguous.
|
||||||
|
writer = PdfWriter()
|
||||||
|
page = writer.add_blank_page(width=128, height=96)
|
||||||
|
stream = DecodedStreamObject()
|
||||||
|
stream.set_data(image.tobytes())
|
||||||
|
stream.update({NameObject(key): value for key, value in {
|
||||||
|
"/Type": NameObject("/XObject"), "/Subtype": NameObject("/Image"),
|
||||||
|
"/ColorSpace": NameObject("/DeviceCMYK"), "/BitsPerComponent": NumberObject(8),
|
||||||
|
"/Width": NumberObject(128), "/Height": NumberObject(96),
|
||||||
|
}.items()})
|
||||||
|
page[NameObject("/Resources")] = DictionaryObject({NameObject("/XObject"):
|
||||||
|
DictionaryObject({NameObject("/Im0"): writer._add_object(stream)})})
|
||||||
|
content = DecodedStreamObject()
|
||||||
|
content.set_data(b"q 128 0 0 96 0 0 cm /Im0 Do Q")
|
||||||
|
page[NameObject("/Contents")] = writer._add_object(content)
|
||||||
|
writer.write(path)
|
||||||
|
else:
|
||||||
|
image.save(path, "PDF")
|
||||||
|
assert PdfReader(path).pages[0].images[0].image.mode == mode
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("mode,expected", [("CMYK", "RGB"), ("RGB", "RGB"), ("L", "L")])
|
||||||
|
def test_pdf_image_reaches_vision_with_owner_and_png_pixels(tmp_path, monkeypatch, mode, expected):
|
||||||
|
pdf = make_pdf(tmp_path, mode)
|
||||||
|
calls = []
|
||||||
|
def analyze(path, owner=None):
|
||||||
|
with Image.open(path) as image:
|
||||||
|
image.load()
|
||||||
|
assert image.format == "PNG" and image.mode == expected
|
||||||
|
assert image.size == (128, 96)
|
||||||
|
if mode == "CMYK":
|
||||||
|
red, green, blue = image.getpixel((64, 48))
|
||||||
|
assert red > 250 and green < 5 and blue < 5
|
||||||
|
calls.append((Path(path), owner))
|
||||||
|
return "Synthetic OCR result"
|
||||||
|
monkeypatch.setattr(dp, "analyze_image_with_vl", analyze)
|
||||||
|
output = dp._process_pdf(str(pdf), owner="alice")
|
||||||
|
assert "[Page 1 image 1 text]: Synthetic OCR result" in output
|
||||||
|
assert len(calls) == 1 and calls[0][1] == "alice"
|
||||||
|
assert not calls[0][0].exists(), "Temporary PNG must be removed after analysis"
|
||||||
|
assert PdfReader(pdf).pages[0].images[0].image.mode == mode, "Source PDF is unchanged"
|
||||||
|
|
||||||
|
|
||||||
|
def test_mixed_pdf_keeps_images_on_both_pages(tmp_path, monkeypatch):
|
||||||
|
writer = PdfWriter()
|
||||||
|
for mode in ("CMYK", "RGB"):
|
||||||
|
writer.append(str(make_pdf(tmp_path, mode)))
|
||||||
|
pdf = tmp_path / "mixed.pdf"
|
||||||
|
writer.write(pdf)
|
||||||
|
calls = []
|
||||||
|
def analyze(path, owner=None):
|
||||||
|
with Image.open(path) as image:
|
||||||
|
image.load()
|
||||||
|
calls.append(image.mode)
|
||||||
|
return "Image content"
|
||||||
|
monkeypatch.setattr(dp, "analyze_image_with_vl", analyze)
|
||||||
|
output = dp._process_pdf(str(pdf), owner="alice")
|
||||||
|
assert calls == ["RGB", "RGB"]
|
||||||
|
assert "Page 1 image 1 text" in output and "Page 2 image 1 text" in output
|
||||||
|
|
||||||
|
|
||||||
|
def test_text_only_extraction_does_not_enable_vision(tmp_path, monkeypatch):
|
||||||
|
pdf = make_pdf(tmp_path, "CMYK")
|
||||||
|
def unexpected(*args, **kwargs):
|
||||||
|
pytest.fail("Text-only extraction must not call vision")
|
||||||
|
monkeypatch.setattr(dp, "analyze_image_with_vl", unexpected)
|
||||||
|
assert "no readable content" in dp.extract_local_document(str(pdf), owner="alice")
|
||||||
|
|
||||||
|
|
||||||
|
def test_vision_failure_still_removes_converted_png(tmp_path, monkeypatch):
|
||||||
|
pdf = make_pdf(tmp_path, "CMYK")
|
||||||
|
paths = []
|
||||||
|
def unavailable(path, owner=None):
|
||||||
|
paths.append(Path(path))
|
||||||
|
with Image.open(path) as image:
|
||||||
|
assert image.mode == "RGB" and image.format == "PNG"
|
||||||
|
raise RuntimeError("Synthetic unavailable vision endpoint")
|
||||||
|
monkeypatch.setattr(dp, "analyze_image_with_vl", unavailable)
|
||||||
|
assert "no readable content" in dp._process_pdf(str(pdf), owner="alice")
|
||||||
|
assert len(paths) == 1 and not paths[0].exists()
|
||||||
@@ -144,10 +144,12 @@ def test_editor_agent_endpoint_reports_scoped_tools():
|
|||||||
|
|
||||||
async def _route_contract(monkeypatch, message, *, attachment_meta=(), form=None, messages_out=None):
|
async def _route_contract(monkeypatch, message, *, attachment_meta=(), form=None, messages_out=None):
|
||||||
from routes import chat_routes
|
from routes import chat_routes
|
||||||
from src import tool_security
|
from src import tool_security, tool_utils
|
||||||
from tests.test_foreground_model_routing import _RouteRequest, _chat_stream_endpoint
|
from tests.test_foreground_model_routing import _RouteRequest, _chat_stream_endpoint
|
||||||
|
|
||||||
with monkeypatch.context() as route_patch:
|
with monkeypatch.context() as route_patch:
|
||||||
|
# This built-in catalog fixture has no configured external MCP servers.
|
||||||
|
route_patch.setattr(tool_utils, "get_mcp_manager", lambda: None)
|
||||||
endpoint = _chat_stream_endpoint(
|
endpoint = _chat_stream_endpoint(
|
||||||
route_patch, "agent", {},
|
route_patch, "agent", {},
|
||||||
session_model="odysseus-qwen3.5-tools-pre-heretic",
|
session_model="odysseus-qwen3.5-tools-pre-heretic",
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
"""Route defaults must use manually pinned IDs without widening ownership."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
import core.database as cdb
|
||||||
|
import routes.chat_routes as chat
|
||||||
|
from routes.task.task_routes import _resolve_run_endpoint
|
||||||
|
from routes.research.research_routes import _resolve_endpoint_runtime
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def database(monkeypatch):
|
||||||
|
engine = create_engine("sqlite:///:memory:")
|
||||||
|
cdb.Base.metadata.create_all(engine)
|
||||||
|
factory = sessionmaker(bind=engine)
|
||||||
|
monkeypatch.setattr(chat, "SessionLocal", factory)
|
||||||
|
with factory() as db:
|
||||||
|
yield db
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
def endpoint(db, eid="owned", *, owner="alice", cached=(), pinned=("pinned-chat",), hidden=()):
|
||||||
|
row = cdb.ModelEndpoint(
|
||||||
|
id=eid, name=eid, owner=owner, is_enabled=True,
|
||||||
|
base_url=f"https://{eid}.example.test/v1", api_key=None,
|
||||||
|
cached_models=json.dumps(cached), pinned_models=json.dumps(pinned),
|
||||||
|
hidden_models=json.dumps(hidden),
|
||||||
|
)
|
||||||
|
db.add(row)
|
||||||
|
db.commit()
|
||||||
|
return row
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("cached", [(), ("hidden-chat",)])
|
||||||
|
def test_empty_session_recovers_a_visible_pinned_model(database, cached):
|
||||||
|
ep = endpoint(database, cached=cached, hidden=("hidden-chat",))
|
||||||
|
database.add(cdb.Session(id="chat", name="Chat", owner="alice", model="", endpoint_url=ep.base_url, endpoint_id=ep.id))
|
||||||
|
database.commit()
|
||||||
|
session = SimpleNamespace(model="", endpoint_url=ep.base_url, endpoint_id=ep.id)
|
||||||
|
|
||||||
|
assert chat._recover_empty_session_model(session, "chat", "alice") is True
|
||||||
|
assert session.model == "pinned-chat"
|
||||||
|
database.expire_all()
|
||||||
|
assert database.get(cdb.Session, "chat").model == "pinned-chat"
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_session_does_not_recover_a_hidden_pin(database):
|
||||||
|
ep = endpoint(database, hidden=("pinned-chat",))
|
||||||
|
session = SimpleNamespace(model="", endpoint_url=ep.base_url, endpoint_id=ep.id)
|
||||||
|
assert chat._recover_empty_session_model(session, "missing", "alice") is False
|
||||||
|
assert session.model == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_subscription_recovery_still_requires_its_live_catalog(database):
|
||||||
|
ep = endpoint(database)
|
||||||
|
ep.base_url = "https://chatgpt.com/backend-api/codex"
|
||||||
|
database.commit()
|
||||||
|
session = SimpleNamespace(model="", endpoint_url=ep.base_url, endpoint_id=ep.id)
|
||||||
|
assert chat._recover_empty_session_model(session, "missing", "alice") is False
|
||||||
|
assert session.model == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_task_run_reopens_against_its_owners_pinned_endpoint(database):
|
||||||
|
endpoint(database, "foreign", owner="bob", cached=("pinned-chat",))
|
||||||
|
own = endpoint(database)
|
||||||
|
task = SimpleNamespace(owner="alice", endpoint_url="", session_id=None, model="pinned-chat")
|
||||||
|
assert _resolve_run_endpoint(database, task, SimpleNamespace(model="pinned-chat")) == own.base_url
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("owner", ["alice", None])
|
||||||
|
def test_task_run_can_find_visible_pins_on_owned_or_shared_endpoints(database, owner):
|
||||||
|
ep = endpoint(database, owner=owner)
|
||||||
|
task = SimpleNamespace(owner="alice", endpoint_url="", session_id=None, model="pinned-chat")
|
||||||
|
assert _resolve_run_endpoint(database, task, SimpleNamespace(model=None)) == ep.base_url
|
||||||
|
|
||||||
|
|
||||||
|
def test_task_run_does_not_autoselect_a_hidden_pin(database):
|
||||||
|
endpoint(database, hidden=("pinned-chat",))
|
||||||
|
task = SimpleNamespace(owner="alice", endpoint_url="", session_id=None, model="pinned-chat")
|
||||||
|
assert _resolve_run_endpoint(database, task, SimpleNamespace(model="pinned-chat")) == ""
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("cached", [(), ("hidden-chat",)])
|
||||||
|
def test_research_default_uses_the_visible_pinned_id(database, cached):
|
||||||
|
ep = endpoint(database, cached=cached, hidden=("hidden-chat",))
|
||||||
|
assert _resolve_endpoint_runtime(ep, owner="alice") == (
|
||||||
|
ep.base_url + "/chat/completions", "pinned-chat", {},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_research_default_does_not_autoselect_a_hidden_pin(database):
|
||||||
|
ep = endpoint(database, hidden=("pinned-chat",))
|
||||||
|
assert _resolve_endpoint_runtime(ep, owner="alice") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_research_explicit_model_selection_is_preserved(database):
|
||||||
|
ep = endpoint(database, cached=("cached-chat",))
|
||||||
|
assert _resolve_endpoint_runtime(ep, owner="alice", model="explicit-chat")[1] == "explicit-chat"
|
||||||
|
|
||||||
|
|
||||||
|
def test_research_cached_default_keeps_priority_over_pins(database):
|
||||||
|
ep = endpoint(database, cached=("cached-chat",))
|
||||||
|
assert _resolve_endpoint_runtime(ep, owner="alice")[1] == "cached-chat"
|
||||||
+155
-34
@@ -12,6 +12,8 @@ exercising every procfs-touching code path on both a Linux and a non-Linux
|
|||||||
host. ``test_cookbook_stop_without_procfs.py`` covers the behaviour itself.
|
host. ``test_cookbook_stop_without_procfs.py`` covers the behaviour itself.
|
||||||
"""
|
"""
|
||||||
import ast
|
import ast
|
||||||
|
|
||||||
|
import pytest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||||
@@ -75,37 +77,112 @@ def _scan_sites(tree: ast.AST) -> list[ast.Call]:
|
|||||||
return sites
|
return sites
|
||||||
|
|
||||||
|
|
||||||
def _guard_lines(tree: ast.AST) -> list[int]:
|
def _condition_requires_procfs(node: ast.AST, truth: bool) -> bool:
|
||||||
"""Line numbers of calls that test whether procfs is present."""
|
"""Whether this Boolean outcome proves a direct procfs check succeeded.
|
||||||
lines = []
|
|
||||||
for node in ast.walk(tree):
|
An ignored call, another function's check, or a check on only one possible
|
||||||
if not isinstance(node, ast.Call):
|
OR branch provides no proof. Keep unknown expressions conservative.
|
||||||
continue
|
"""
|
||||||
func = node.func
|
if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not):
|
||||||
name = func.attr if isinstance(func, ast.Attribute) else getattr(func, "id", "")
|
return _condition_requires_procfs(node.operand, not truth)
|
||||||
if name not in _GUARD_FUNCS:
|
if isinstance(node, ast.BoolOp):
|
||||||
continue
|
facts = [_condition_requires_procfs(value, truth) for value in node.values]
|
||||||
subject_is_procfs = (
|
all_values_required = isinstance(node.op, ast.And) == truth
|
||||||
name in {"has_procfs", "is_wsl"}
|
return any(facts) if all_values_required else all(facts)
|
||||||
or (isinstance(func, ast.Attribute) and _is_procfs_root(func.value))
|
if not isinstance(node, ast.Call):
|
||||||
or any(_is_procfs_root(arg) for arg in node.args)
|
return False
|
||||||
)
|
func = node.func
|
||||||
if subject_is_procfs:
|
name = func.attr if isinstance(func, ast.Attribute) else getattr(func, "id", "")
|
||||||
lines.append(node.lineno)
|
subject_is_procfs = (
|
||||||
return lines
|
name == "has_procfs"
|
||||||
|
or (isinstance(func, ast.Attribute) and _is_procfs_root(func.value))
|
||||||
|
or any(_is_procfs_root(arg) for arg in node.args)
|
||||||
|
)
|
||||||
|
return truth and name in _GUARD_FUNCS and subject_is_procfs
|
||||||
|
|
||||||
|
|
||||||
def _enclosing_scope(tree: ast.AST, node: ast.AST) -> ast.AST:
|
def _guarded_scan_ids(tree: ast.AST) -> set[int]:
|
||||||
"""Smallest function/module scope containing ``node``."""
|
"""Follow conditional/early-exit paths instead of comparing line numbers.
|
||||||
best = tree
|
|
||||||
for candidate in ast.walk(tree):
|
Each function starts with no procfs assumption. Facts from a loop or try
|
||||||
if not isinstance(candidate, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
body cannot escape to code which may run without that body completing.
|
||||||
continue
|
This intentionally requires direct checks; arbitrary aliases/call graphs
|
||||||
end = getattr(candidate, "end_lineno", None) or candidate.lineno
|
are not a proof that procfs exists at the enumeration site.
|
||||||
if candidate.lineno <= node.lineno <= end:
|
"""
|
||||||
if best is tree or candidate.lineno > best.lineno:
|
scans = {id(site) for site in _scan_sites(tree)}
|
||||||
best = candidate
|
guarded = set()
|
||||||
return best
|
|
||||||
|
def expression(node, present):
|
||||||
|
if node is None:
|
||||||
|
return
|
||||||
|
if id(node) in scans and present:
|
||||||
|
guarded.add(id(node))
|
||||||
|
if isinstance(node, ast.BoolOp):
|
||||||
|
for value in node.values:
|
||||||
|
expression(value, present)
|
||||||
|
# Later AND operands run only after a true prefix; later OR
|
||||||
|
# operands run only after a false prefix.
|
||||||
|
present = present or _condition_requires_procfs(value, isinstance(node.op, ast.And))
|
||||||
|
elif isinstance(node, ast.IfExp):
|
||||||
|
expression(node.test, present)
|
||||||
|
expression(node.body, present or _condition_requires_procfs(node.test, True))
|
||||||
|
expression(node.orelse, present or _condition_requires_procfs(node.test, False))
|
||||||
|
elif isinstance(node, (ast.ListComp, ast.SetComp, ast.GeneratorExp, ast.DictComp)):
|
||||||
|
for generator in node.generators:
|
||||||
|
expression(generator.iter, present)
|
||||||
|
for condition in generator.ifs:
|
||||||
|
expression(condition, present)
|
||||||
|
present = present or _condition_requires_procfs(condition, True)
|
||||||
|
for attr in ("elt", "key", "value"):
|
||||||
|
expression(getattr(node, attr, None), present)
|
||||||
|
elif isinstance(node, ast.Lambda):
|
||||||
|
expression(node.body, False)
|
||||||
|
else:
|
||||||
|
for child in ast.iter_child_nodes(node):
|
||||||
|
expression(child, present)
|
||||||
|
|
||||||
|
def block(statements, present):
|
||||||
|
for statement in statements:
|
||||||
|
if isinstance(statement, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||||
|
block(statement.body, False)
|
||||||
|
for value in statement.decorator_list + statement.args.defaults:
|
||||||
|
expression(value, present)
|
||||||
|
elif isinstance(statement, ast.ClassDef):
|
||||||
|
block(statement.body, present)
|
||||||
|
elif isinstance(statement, ast.If):
|
||||||
|
expression(statement.test, present)
|
||||||
|
yes = block(statement.body, present or _condition_requires_procfs(statement.test, True))
|
||||||
|
no = block(statement.orelse, present or _condition_requires_procfs(statement.test, False))
|
||||||
|
continuing = [fact for fact in (yes, no) if fact is not None]
|
||||||
|
if not continuing:
|
||||||
|
return None
|
||||||
|
present = all(continuing)
|
||||||
|
elif isinstance(statement, (ast.Return, ast.Raise, ast.Break, ast.Continue)):
|
||||||
|
for child in ast.iter_child_nodes(statement):
|
||||||
|
expression(child, present)
|
||||||
|
return None
|
||||||
|
elif isinstance(statement, (ast.For, ast.AsyncFor, ast.While)):
|
||||||
|
condition = statement.test if isinstance(statement, ast.While) else statement.iter
|
||||||
|
expression(condition, present)
|
||||||
|
body_present = present or (isinstance(statement, ast.While) and _condition_requires_procfs(condition, True))
|
||||||
|
block(statement.body, body_present)
|
||||||
|
block(statement.orelse, present)
|
||||||
|
elif isinstance(statement, (ast.Try, getattr(ast, "TryStar", ast.Try))):
|
||||||
|
block(statement.body, present)
|
||||||
|
block(statement.orelse, present)
|
||||||
|
block(statement.finalbody, present)
|
||||||
|
for handler in statement.handlers:
|
||||||
|
block(handler.body, present)
|
||||||
|
elif isinstance(statement, (ast.With, ast.AsyncWith)):
|
||||||
|
for item in statement.items:
|
||||||
|
expression(item.context_expr, present)
|
||||||
|
block(statement.body, present)
|
||||||
|
else:
|
||||||
|
expression(statement, present)
|
||||||
|
return present
|
||||||
|
|
||||||
|
block(tree.body, False)
|
||||||
|
return guarded
|
||||||
|
|
||||||
|
|
||||||
def _app_python_files() -> list[Path]:
|
def _app_python_files() -> list[Path]:
|
||||||
@@ -139,12 +216,10 @@ def _collect_sites() -> tuple[list[str], list[str]]:
|
|||||||
sites = _scan_sites(tree)
|
sites = _scan_sites(tree)
|
||||||
if not sites:
|
if not sites:
|
||||||
continue
|
continue
|
||||||
guards = _guard_lines(tree)
|
guards = _guarded_scan_ids(tree)
|
||||||
for site in sites:
|
for site in sites:
|
||||||
scope = _enclosing_scope(tree, site)
|
|
||||||
start = getattr(scope, "lineno", 0)
|
|
||||||
label = f"{path.relative_to(REPO_ROOT)}:{site.lineno}"
|
label = f"{path.relative_to(REPO_ROOT)}:{site.lineno}"
|
||||||
if any(start <= g < site.lineno for g in guards):
|
if id(site) in guards:
|
||||||
guarded.append(label)
|
guarded.append(label)
|
||||||
else:
|
else:
|
||||||
unguarded.append(label)
|
unguarded.append(label)
|
||||||
@@ -154,7 +229,7 @@ def _collect_sites() -> tuple[list[str], list[str]]:
|
|||||||
def test_every_procfs_scan_is_guarded_by_an_existence_check():
|
def test_every_procfs_scan_is_guarded_by_an_existence_check():
|
||||||
_guarded, unguarded = _collect_sites()
|
_guarded, unguarded = _collect_sites()
|
||||||
assert not unguarded, (
|
assert not unguarded, (
|
||||||
"procfs pid scans with no existence check in the enclosing function — "
|
"procfs pid scans without an effective conditional existence guard — "
|
||||||
"these raise FileNotFoundError on macOS and Windows: "
|
"these raise FileNotFoundError on macOS and Windows: "
|
||||||
+ ", ".join(unguarded)
|
+ ", ".join(unguarded)
|
||||||
)
|
)
|
||||||
@@ -176,3 +251,49 @@ def test_the_guard_detector_still_sees_the_known_scans():
|
|||||||
f"root was renamed. Found: {sorted(found)}"
|
f"root was renamed. Found: {sorted(found)}"
|
||||||
)
|
)
|
||||||
assert len(found) >= 3, f"expected at least 3 procfs scans, found {sorted(found)}"
|
assert len(found) >= 3, f"expected at least 3 procfs scans, found {sorted(found)}"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("body", [
|
||||||
|
'Path("/proc").exists(); os.listdir("/proc")',
|
||||||
|
'if Path("/proc").exists():\n pass\nos.listdir("/proc")',
|
||||||
|
'if not Path("/proc").exists():\n os.listdir("/proc")',
|
||||||
|
'if ready or Path("/proc").exists():\n os.listdir("/proc")',
|
||||||
|
'if ready:\n Path("/proc").exists()\nos.listdir("/proc")',
|
||||||
|
'os.listdir("/proc"); Path("/proc").exists()',
|
||||||
|
'if Path("/tmp").exists():\n os.listdir("/proc")',
|
||||||
|
'def other():\n Path("/proc").exists()\nos.listdir("/proc")',
|
||||||
|
'if not has_procfs():\n pass\nos.listdir("/proc")',
|
||||||
|
'for x in []:\n if not has_procfs():\n return\nos.listdir("/proc")',
|
||||||
|
'if has_procfs():\n return\nelse:\n os.listdir("/proc")',
|
||||||
|
'[pid for pid in os.listdir("/proc") if has_procfs()]',
|
||||||
|
], ids=["ignored", "expired-branch", "inverted", "unsafe-or", "sibling-branch",
|
||||||
|
"too-late", "wrong-path", "other-function", "no-early-exit", "loop-may-not-run",
|
||||||
|
"wrong-else", "late-comprehension-filter"])
|
||||||
|
def test_guard_detector_rejects_ineffective_checks(body):
|
||||||
|
tree = ast.parse("def scan():\n" + "\n".join(" " + line for line in body.splitlines()))
|
||||||
|
sites = _scan_sites(tree)
|
||||||
|
assert sites, "negative control must contain a recognized enumeration"
|
||||||
|
assert any(id(site) not in _guarded_scan_ids(tree) for site in sites)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("body", [
|
||||||
|
'if Path("/proc").exists():\n os.listdir("/proc")',
|
||||||
|
'if not Path("/proc").exists():\n return\nos.listdir("/proc")',
|
||||||
|
'if not has_procfs():\n raise RuntimeError()\nos.listdir(PROC_ROOT)',
|
||||||
|
'if has_procfs():\n pass\nelse:\n return\nPROC_ROOT.iterdir()',
|
||||||
|
'if ready and has_procfs():\n os.scandir("/proc")',
|
||||||
|
'if not has_procfs() or disabled:\n return\nPROC_ROOT.glob("*")',
|
||||||
|
'if sys.platform == "linux" and has_procfs():\n PROC_ROOT.iterdir()',
|
||||||
|
'for item in items:\n if not has_procfs():\n continue\n os.listdir("/proc")',
|
||||||
|
'if os.path.isdir("/proc"):\n os.listdir("/proc")',
|
||||||
|
'if has_procfs() and list(PROC_ROOT.iterdir()):\n pass',
|
||||||
|
'if not has_procfs() or list(PROC_ROOT.iterdir()):\n pass',
|
||||||
|
'[pid for item in items if has_procfs() for pid in os.listdir("/proc")]',
|
||||||
|
], ids=["positive-branch", "early-return", "early-raise", "else-exits", "and",
|
||||||
|
"negative-or", "platform-and-procfs", "loop-continue", "isdir",
|
||||||
|
"short-circuit-and", "short-circuit-or", "guarded-comprehension"])
|
||||||
|
def test_guard_detector_preserves_effective_checks(body):
|
||||||
|
tree = ast.parse("def scan():\n" + "\n".join(" " + line for line in body.splitlines()))
|
||||||
|
sites = _scan_sites(tree)
|
||||||
|
assert sites, "positive control must contain a recognized enumeration"
|
||||||
|
assert all(id(site) in _guarded_scan_ids(tree) for site in sites)
|
||||||
|
|||||||
@@ -0,0 +1,141 @@
|
|||||||
|
"""Regression tests for py/polynomial-redos sinks in the agent preview parsers.
|
||||||
|
|
||||||
|
Each pattern had overlapping quantifiers (adjacent `\\s` runs, or a lazy group
|
||||||
|
that rescanned the rest of the line from every candidate) that backtrack
|
||||||
|
polynomially when the tail fails. The rewrites keep the same matches for the
|
||||||
|
text these tools emit. The old patterns took 5-20s on the flood inputs below;
|
||||||
|
the new ones finish in milliseconds against a 4s budget.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from src.browser_observation import compact_browser_observation
|
||||||
|
from src.clean_agent_preview import (
|
||||||
|
email_search_result_empty,
|
||||||
|
is_google_sorry_url,
|
||||||
|
skills_terminal_response,
|
||||||
|
tasks_terminal_response,
|
||||||
|
)
|
||||||
|
|
||||||
|
_BUDGET_S = 4.0
|
||||||
|
_DASH = chr(0x2014) # the separator src/tools/system.py writes into task rows
|
||||||
|
|
||||||
|
|
||||||
|
def _timed(fn, *args):
|
||||||
|
start = time.perf_counter()
|
||||||
|
result = fn(*args)
|
||||||
|
return result, time.perf_counter() - start
|
||||||
|
|
||||||
|
|
||||||
|
# Browser snapshot: bare `generic` container lines are dropped, others kept.
|
||||||
|
|
||||||
|
def test_generic_snapshot_lines_are_dropped():
|
||||||
|
snapshot = '- generic [ref=e1]:\n - generic\n-generic:\n- generic button\n- button "Go" [ref=e2]'
|
||||||
|
output = compact_browser_observation({'snapshot': snapshot})
|
||||||
|
assert output == '- generic button\n- button "Go" [ref=e2]'
|
||||||
|
|
||||||
|
|
||||||
|
def test_generic_filter_whitespace_flood_is_fast():
|
||||||
|
(output, dt) = _timed(compact_browser_observation, {'snapshot': '\t' * 120000 + '!'})
|
||||||
|
assert dt < _BUDGET_S, f'generic filter took {dt:.2f}s'
|
||||||
|
assert output.endswith('!')
|
||||||
|
|
||||||
|
|
||||||
|
# Browser CLI text: status text is split from the post-interaction state.
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('label', ['post-click page state', 'page state after failed click'])
|
||||||
|
def test_post_interaction_state_is_split(label):
|
||||||
|
raw = f'Clicked\n\n[{label}]\n{{"snapshot": "- link \\"A\\" [ref=e3]"}}'
|
||||||
|
assert compact_browser_observation(raw) == 'Clicked\n\n- link "A" [ref=e3]'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('opener', ['\n\n[post-', '\n\n[page state after failed '])
|
||||||
|
def test_post_interaction_split_flood_is_fast(opener):
|
||||||
|
(_, dt) = _timed(compact_browser_observation, 'x' + opener * 15000)
|
||||||
|
assert dt < _BUDGET_S, f'post-interaction split took {dt:.2f}s'
|
||||||
|
|
||||||
|
|
||||||
|
# Task rows: `N. name (id) <dash> detail`, where the name may contain parentheses.
|
||||||
|
|
||||||
|
def test_task_rows_parse():
|
||||||
|
raw = (f'1. Backup (nightly) (abc123) {_DASH} active, daily at 02:00\n'
|
||||||
|
f'2. (e2) {_DASH} paused\n'
|
||||||
|
f'not a task row')
|
||||||
|
assert tasks_terminal_response(raw) == (
|
||||||
|
f'Tasks (2):\n- [Backup (nightly)](#task-abc123) {_DASH} active\n'
|
||||||
|
f'- [](#task-e2) {_DASH} paused'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('evil', ['0.' + '\t' * 3000 + '!', '0.\t\t(' + '\t((' * 20000])
|
||||||
|
def test_task_row_flood_is_fast(evil):
|
||||||
|
(output, dt) = _timed(tasks_terminal_response, evil)
|
||||||
|
assert dt < _BUDGET_S, f'task row parse took {dt:.2f}s'
|
||||||
|
assert output == evil.strip()
|
||||||
|
|
||||||
|
|
||||||
|
# Skill inventory: `- name (category): summary` under a status heading.
|
||||||
|
|
||||||
|
def test_skill_rows_parse():
|
||||||
|
raw = '## Published\n- pdf-tools (documents): Read PDFs\n- web\n## Drafts\n- notes (writing)'
|
||||||
|
assert skills_terminal_response(raw) == (
|
||||||
|
'Skills (3):\n\n**Published**\n- [pdf-tools](#skill-pdf-tools) (documents)\n'
|
||||||
|
'- [web](#skill-web)\n\n**Drafts**\n- [notes](#skill-notes) (writing)'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_nameless_skill_bullet_is_not_a_row():
|
||||||
|
raw = '## Published\n- : x'
|
||||||
|
assert skills_terminal_response(raw) == raw
|
||||||
|
|
||||||
|
|
||||||
|
def test_skill_row_flood_is_fast():
|
||||||
|
(_, dt) = _timed(skills_terminal_response, '## Published\n- \t9' + '\t\t' * 30000 + '!')
|
||||||
|
assert dt < _BUDGET_S, f'skill row parse took {dt:.2f}s'
|
||||||
|
|
||||||
|
|
||||||
|
# Email search: the plain-text "no results" message.
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('value,expected', [
|
||||||
|
('No emails matched from:bob.', True),
|
||||||
|
(' no emails matched "invoice"\n\n', True),
|
||||||
|
('No emails matched ', False),
|
||||||
|
('No emails matched x\nmore', False),
|
||||||
|
('Found 2 emails', False),
|
||||||
|
])
|
||||||
|
def test_email_no_results_message(value, expected):
|
||||||
|
assert email_search_result_empty(value) is expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_email_no_results_flood_is_fast():
|
||||||
|
(result, dt) = _timed(email_search_result_empty, 'No emails matched x' + '\t' * 60000 + '\n!')
|
||||||
|
assert dt < _BUDGET_S, f'email no-results check took {dt:.2f}s'
|
||||||
|
assert result is False
|
||||||
|
|
||||||
|
|
||||||
|
# Google rate-limit interstitial.
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('url,expected', [
|
||||||
|
('https://www.google.com/sorry/index?continue=x', True),
|
||||||
|
('http://google.co.uk/sorry/', True),
|
||||||
|
('HTTPS://IPV4.GOOGLE.COM/SORRY/', True),
|
||||||
|
('https://example.com/sorry/?next=https://www.google.de/sorry/', True),
|
||||||
|
('https://example.com/sorry/', False),
|
||||||
|
('https://notgoogle.com/sorry/', False),
|
||||||
|
('https://www.google.com/search?q=sorry', False),
|
||||||
|
('', False),
|
||||||
|
])
|
||||||
|
def test_is_google_sorry_url(url, expected):
|
||||||
|
assert is_google_sorry_url(url) is expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('evil', [
|
||||||
|
'http://google.' + '..google.' * 50000,
|
||||||
|
'http://' + 'x.' * 200000 + 'google./sorry/', # reaches the host match
|
||||||
|
])
|
||||||
|
def test_google_sorry_flood_is_fast(evil):
|
||||||
|
(result, dt) = _timed(is_google_sorry_url, evil)
|
||||||
|
assert dt < _BUDGET_S, f'google sorry check took {dt:.2f}s'
|
||||||
|
assert result is False
|
||||||
@@ -137,3 +137,170 @@ def test_collect_aggregate_deadline_yields_controlled_result(monkeypatch):
|
|||||||
net = [s for s in out["services"] if s["name"] != "chromadb"]
|
net = [s for s in out["services"] if s["name"] != "chromadb"]
|
||||||
assert all(s["status"] == sh.DOWN and s["meta"].get("error") == "timeout"
|
assert all(s["status"] == sh.DOWN and s["meta"].get("error") == "timeout"
|
||||||
for s in net)
|
for s in net)
|
||||||
|
|
||||||
|
|
||||||
|
# ── Discovery failure is not "disabled" (#6154) ──
|
||||||
|
#
|
||||||
|
# `_gather_inputs()` fails soft to an empty inventory, and every probe reads an
|
||||||
|
# empty inventory as `disabled`. Without a record of *why* it is empty, a broken
|
||||||
|
# integrations file / email store / endpoint DB is reported as an intentionally
|
||||||
|
# unconfigured subsystem — and, with the rest disabled, `overall: "ok"`.
|
||||||
|
|
||||||
|
def _boom(*_a, **_k):
|
||||||
|
raise RuntimeError("secret-bearing detail: postgres://u:p@db/x")
|
||||||
|
|
||||||
|
|
||||||
|
def test_gather_inputs_records_each_failed_source(monkeypatch):
|
||||||
|
import core.database
|
||||||
|
import routes.email_helpers
|
||||||
|
import src.integrations
|
||||||
|
import src.settings
|
||||||
|
|
||||||
|
monkeypatch.setattr(src.settings, "load_settings", lambda: {"a": 1})
|
||||||
|
monkeypatch.setattr(src.integrations, "load_integrations", _boom)
|
||||||
|
monkeypatch.setattr(routes.email_helpers, "_list_email_accounts", _boom)
|
||||||
|
monkeypatch.setattr(core.database, "SessionLocal", _boom)
|
||||||
|
|
||||||
|
out = sh._gather_inputs()
|
||||||
|
assert out["settings"] == {"a": 1}
|
||||||
|
assert set(out["failed"]) == {"integrations", "accounts", "endpoints"}
|
||||||
|
# Controlled categories only — never the exception text.
|
||||||
|
assert set(out["failed"].values()) <= set(sh._ERROR_DETAIL)
|
||||||
|
assert "postgres" not in repr(out)
|
||||||
|
|
||||||
|
|
||||||
|
def test_gather_inputs_reports_no_failures_when_sources_load(monkeypatch):
|
||||||
|
import core.database
|
||||||
|
import routes.email_helpers
|
||||||
|
import src.integrations
|
||||||
|
import src.settings
|
||||||
|
|
||||||
|
class _DB:
|
||||||
|
def query(self, *_a):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def filter(self, *_a):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def all(self):
|
||||||
|
return []
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
monkeypatch.setattr(src.settings, "load_settings", lambda: {})
|
||||||
|
monkeypatch.setattr(src.integrations, "load_integrations", lambda: [])
|
||||||
|
monkeypatch.setattr(routes.email_helpers, "_list_email_accounts", lambda: [])
|
||||||
|
monkeypatch.setattr(core.database, "SessionLocal", lambda: _DB())
|
||||||
|
|
||||||
|
assert sh._gather_inputs()["failed"] == {}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("source,service", [
|
||||||
|
("settings", "searxng"),
|
||||||
|
("integrations", "ntfy"),
|
||||||
|
("accounts", "email"),
|
||||||
|
("endpoints", "providers"),
|
||||||
|
])
|
||||||
|
def test_failed_source_is_not_reported_as_disabled(monkeypatch, source, service):
|
||||||
|
import asyncio
|
||||||
|
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
|
||||||
|
"settings": {"search_provider": "disabled"},
|
||||||
|
"integrations": [], "accounts": [], "endpoints": [],
|
||||||
|
"failed": {source: "network_error"},
|
||||||
|
})
|
||||||
|
out = asyncio.run(sh.collect_service_health(None, None))
|
||||||
|
entry = next(s for s in out["services"] if s["name"] == service)
|
||||||
|
assert entry["status"] != sh.DISABLED
|
||||||
|
assert entry["meta"]["error"] == sh.CONFIG_SOURCE_ERROR
|
||||||
|
assert entry["meta"]["source"] == source
|
||||||
|
assert entry["meta"]["source_error"] == "network_error"
|
||||||
|
# A failed source must reach the aggregate verdict.
|
||||||
|
assert out["overall"] != sh.OK
|
||||||
|
assert set(out) == {"overall", "services", "timestamp"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_all_sources_failing_cannot_report_overall_ok(monkeypatch):
|
||||||
|
# The exact shape from the report: settings loads and says search is off,
|
||||||
|
# the other three sources raise. Before the fix this was overall "ok" with
|
||||||
|
# five "disabled" entries.
|
||||||
|
import asyncio
|
||||||
|
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
|
||||||
|
"settings": {"search_provider": "disabled"},
|
||||||
|
"integrations": [], "accounts": [], "endpoints": [],
|
||||||
|
"failed": {"integrations": "error", "accounts": "error",
|
||||||
|
"endpoints": "error"},
|
||||||
|
})
|
||||||
|
out = asyncio.run(sh.collect_service_health(None, None))
|
||||||
|
by_name = {s["name"]: s for s in out["services"]}
|
||||||
|
assert out["overall"] == sh.DEGRADED
|
||||||
|
# Loaded-and-genuinely-empty still reads as disabled.
|
||||||
|
assert by_name["searxng"]["status"] == sh.DISABLED
|
||||||
|
for name in ("ntfy", "email", "providers"):
|
||||||
|
assert by_name[name]["status"] == sh.DEGRADED
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_inventory_without_failure_stays_disabled(monkeypatch):
|
||||||
|
import asyncio
|
||||||
|
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
|
||||||
|
"settings": {"search_provider": "disabled"},
|
||||||
|
"integrations": [], "accounts": [], "endpoints": [], "failed": {},
|
||||||
|
})
|
||||||
|
out = asyncio.run(sh.collect_service_health(_Store(True), _Store(True)))
|
||||||
|
assert out["overall"] == sh.OK
|
||||||
|
assert all(s["status"] == sh.DISABLED
|
||||||
|
for s in out["services"] if s["name"] != "chromadb")
|
||||||
|
|
||||||
|
|
||||||
|
def test_one_failed_source_still_probes_the_others(monkeypatch):
|
||||||
|
# A broken endpoint DB must not stop ntfy/email from being probed.
|
||||||
|
import asyncio
|
||||||
|
probed = []
|
||||||
|
|
||||||
|
def _probe(name):
|
||||||
|
def _fn(*_a, **_k):
|
||||||
|
probed.append(name)
|
||||||
|
return sh._svc(name, sh.OK, "probed")
|
||||||
|
return _fn
|
||||||
|
|
||||||
|
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
|
||||||
|
"settings": {}, "integrations": [], "accounts": [], "endpoints": [],
|
||||||
|
"failed": {"endpoints": "timeout"},
|
||||||
|
})
|
||||||
|
monkeypatch.setattr(sh, "searxng_health", _probe("searxng"))
|
||||||
|
monkeypatch.setattr(sh, "ntfy_health", _probe("ntfy"))
|
||||||
|
monkeypatch.setattr(sh, "email_health", _probe("email"))
|
||||||
|
monkeypatch.setattr(sh, "providers_health", _probe("providers"))
|
||||||
|
|
||||||
|
out = asyncio.run(sh.collect_service_health(None, None))
|
||||||
|
assert sorted(probed) == ["email", "ntfy", "searxng"]
|
||||||
|
assert "providers" not in probed
|
||||||
|
by_name = {s["name"]: s for s in out["services"]}
|
||||||
|
assert by_name["providers"]["status"] == sh.DEGRADED
|
||||||
|
assert by_name["ntfy"]["status"] == sh.OK
|
||||||
|
|
||||||
|
|
||||||
|
def test_source_failure_entry_carries_no_raw_detail(monkeypatch):
|
||||||
|
import asyncio
|
||||||
|
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
|
||||||
|
"settings": {}, "integrations": [], "accounts": [], "endpoints": [],
|
||||||
|
"failed": {"accounts": "auth_or_protocol_error"},
|
||||||
|
})
|
||||||
|
out = asyncio.run(sh.collect_service_health(None, None))
|
||||||
|
entry = next(s for s in out["services"] if s["name"] == "email")
|
||||||
|
assert entry["detail"] == (
|
||||||
|
"Could not read configuration (configuration source could not be read).")
|
||||||
|
assert set(entry["meta"]) == {"error", "source", "source_error"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_collect_tolerates_inputs_without_a_failed_key(monkeypatch):
|
||||||
|
# `_gather_inputs()` is monkeypatched in a number of existing tests (and by
|
||||||
|
# anything calling the collector with a stub); a payload predating `failed`
|
||||||
|
# must still probe normally rather than raise.
|
||||||
|
import asyncio
|
||||||
|
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
|
||||||
|
"settings": {"search_provider": "disabled"},
|
||||||
|
"integrations": [], "accounts": [], "endpoints": [],
|
||||||
|
})
|
||||||
|
out = asyncio.run(sh.collect_service_health(_Store(True), _Store(True)))
|
||||||
|
assert out["overall"] == sh.OK
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ def test_auto_sort_keeps_fresh_session_while_first_response_is_pending(monkeypat
|
|||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
def test_auto_sort_still_deletes_old_throwaway_sessions(monkeypatch):
|
def test_auto_sort_keeps_old_nonempty_sessions_even_with_throwaway_title(monkeypatch):
|
||||||
session_factory = _make_session_factory()
|
session_factory = _make_session_factory()
|
||||||
_install_session_factory(monkeypatch, session_factory)
|
_install_session_factory(monkeypatch, session_factory)
|
||||||
|
|
||||||
@@ -160,7 +160,8 @@ def test_auto_sort_still_deletes_old_throwaway_sessions(monkeypatch):
|
|||||||
|
|
||||||
db = session_factory()
|
db = session_factory()
|
||||||
try:
|
try:
|
||||||
assert db.query(DbSession).filter(DbSession.id == sid).first() is None
|
assert db.query(DbSession).filter(DbSession.id == sid).first() is not None
|
||||||
assert "Cleaned 1 sessions" in result
|
assert db.query(DbMessage).filter(DbMessage.session_id == sid).count() == 1
|
||||||
|
assert "Cleaned 0 sessions" in result
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ from core.database import ChatMessage as DbMessage
|
|||||||
from core.database import Session as DbSession
|
from core.database import Session as DbSession
|
||||||
|
|
||||||
_TMPDB = tempfile.NamedTemporaryFile(suffix=".db", delete=False)
|
_TMPDB = tempfile.NamedTemporaryFile(suffix=".db", delete=False)
|
||||||
|
_TMPDB.close() # SQLite owns its connections; release the Windows file handle.
|
||||||
_ENGINE = create_engine(
|
_ENGINE = create_engine(
|
||||||
f"sqlite:///{_TMPDB.name}",
|
f"sqlite:///{_TMPDB.name}",
|
||||||
connect_args={"check_same_thread": False},
|
connect_args={"check_same_thread": False},
|
||||||
@@ -230,12 +231,14 @@ def test_patch_session_updates_persisted_cwd(monkeypatch):
|
|||||||
|
|
||||||
def test_auto_sort_skip_llm_cleans_owner_stamped_sessions_when_auth_disabled(monkeypatch):
|
def test_auto_sort_skip_llm_cleans_owner_stamped_sessions_when_auth_disabled(monkeypatch):
|
||||||
import routes.session_routes as sr
|
import routes.session_routes as sr
|
||||||
|
from fastapi import APIRouter
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
_stub_multipart_if_missing(monkeypatch)
|
_stub_multipart_if_missing(monkeypatch)
|
||||||
monkeypatch.setenv("AUTH_ENABLED", "false")
|
monkeypatch.setenv("AUTH_ENABLED", "false")
|
||||||
monkeypatch.setattr(sr, "SessionLocal", _TS)
|
monkeypatch.setattr(sr, "SessionLocal", _TS)
|
||||||
monkeypatch.setattr(sr, "effective_user", lambda request: None)
|
monkeypatch.setattr(sr, "effective_user", lambda request: None)
|
||||||
|
monkeypatch.setattr(sr, "router", APIRouter(prefix="/api"))
|
||||||
|
|
||||||
sid = str(uuid.uuid4())
|
sid = str(uuid.uuid4())
|
||||||
old_time = cdb.utcnow_naive() - timedelta(hours=2)
|
old_time = cdb.utcnow_naive() - timedelta(hours=2)
|
||||||
@@ -250,19 +253,12 @@ def test_auto_sort_skip_llm_cleans_owner_stamped_sessions_when_auth_disabled(mon
|
|||||||
endpoint_url="http://localhost",
|
endpoint_url="http://localhost",
|
||||||
model="gpt-4",
|
model="gpt-4",
|
||||||
archived=False,
|
archived=False,
|
||||||
message_count=1,
|
message_count=0,
|
||||||
created_at=old_time,
|
created_at=old_time,
|
||||||
updated_at=old_time,
|
updated_at=old_time,
|
||||||
last_message_at=old_time,
|
last_message_at=old_time,
|
||||||
last_accessed=old_time,
|
last_accessed=old_time,
|
||||||
))
|
))
|
||||||
db.add(DbMessage(
|
|
||||||
id="m-" + uuid.uuid4().hex,
|
|
||||||
session_id=sid,
|
|
||||||
role="user",
|
|
||||||
content="hi",
|
|
||||||
timestamp=old_time,
|
|
||||||
))
|
|
||||||
db.commit()
|
db.commit()
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
@@ -270,6 +266,13 @@ def test_auto_sort_skip_llm_cleans_owner_stamped_sessions_when_auth_disabled(mon
|
|||||||
session = MagicMock(id=sid, name="New chat", model="gpt-4", endpoint_url="http://localhost", rag=False, archived=False)
|
session = MagicMock(id=sid, name="New chat", model="gpt-4", endpoint_url="http://localhost", rag=False, archived=False)
|
||||||
sm = MagicMock()
|
sm = MagicMock()
|
||||||
sm.get_sessions_for_user.return_value = {sid: session}
|
sm.get_sessions_for_user.return_value = {sid: session}
|
||||||
|
|
||||||
|
def delete_session(session_id):
|
||||||
|
with _TS() as db:
|
||||||
|
db.query(DbSession).filter(DbSession.id == session_id).delete()
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
sm.delete_session.side_effect = delete_session
|
||||||
router = sr.setup_session_routes(sm, {})
|
router = sr.setup_session_routes(sm, {})
|
||||||
endpoint = next(r.endpoint for r in router.routes
|
endpoint = next(r.endpoint for r in router.routes
|
||||||
if getattr(r, "path", "") == "/api/sessions/auto-sort"
|
if getattr(r, "path", "") == "/api/sessions/auto-sort"
|
||||||
@@ -277,7 +280,8 @@ def test_auto_sort_skip_llm_cleans_owner_stamped_sessions_when_auth_disabled(mon
|
|||||||
|
|
||||||
result = endpoint(request=MagicMock(), skip_llm=True)
|
result = endpoint(request=MagicMock(), skip_llm=True)
|
||||||
|
|
||||||
assert result["deleted_throwaway"] == 1
|
assert result["deleted_empty"] == 1
|
||||||
|
assert result["deleted_throwaway"] == 0
|
||||||
db = _TS()
|
db = _TS()
|
||||||
try:
|
try:
|
||||||
assert db.query(DbSession).filter(DbSession.id == sid).first() is None
|
assert db.query(DbSession).filter(DbSession.id == sid).first() is None
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from sqlalchemy.orm import sessionmaker
|
|||||||
from core.database import Base
|
from core.database import Base
|
||||||
from core.database import ChatMessage as DbChatMessage
|
from core.database import ChatMessage as DbChatMessage
|
||||||
from core.database import Session as DbSession
|
from core.database import Session as DbSession
|
||||||
from src.session_search import SessionSearchResult, search_session_messages
|
from src.session_search import SessionSearchResult, _snippet, search_session_messages
|
||||||
|
|
||||||
|
|
||||||
def _db(with_fts=True):
|
def _db(with_fts=True):
|
||||||
@@ -61,6 +61,47 @@ def _has_fts(db):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_snippet_locates_quoted_phrase_instead_of_raw_query():
|
||||||
|
content = "A" * 100 + "The session search result is useful." + "Z" * 100
|
||||||
|
|
||||||
|
snippet = _snippet(content, '"session search"', radius=20)
|
||||||
|
|
||||||
|
assert "session search" in snippet
|
||||||
|
assert snippet.startswith("...")
|
||||||
|
assert snippet.endswith("...")
|
||||||
|
|
||||||
|
|
||||||
|
def test_snippet_locates_punctuation_bearing_term_case_insensitively():
|
||||||
|
content = "A" * 100 + "Use FOO_BAR.py for routing." + "Z" * 100
|
||||||
|
|
||||||
|
snippet = _snippet(content, "foo_bar.py!", radius=20)
|
||||||
|
|
||||||
|
assert "FOO_BAR.py" in snippet
|
||||||
|
|
||||||
|
|
||||||
|
def test_snippet_uses_first_meaningful_matching_term():
|
||||||
|
content = "A" * 100 + "second appears here; first appears later." + "Z" * 100
|
||||||
|
|
||||||
|
snippet = _snippet(content, "first second", radius=12)
|
||||||
|
|
||||||
|
assert "second appears" in snippet
|
||||||
|
assert "first appears" not in snippet
|
||||||
|
|
||||||
|
|
||||||
|
def test_snippet_falls_back_to_content_start_when_no_term_matches():
|
||||||
|
content = "Beginning of the transcript, followed by more detail."
|
||||||
|
|
||||||
|
assert _snippet(content, '"missing phrase"', radius=10) == content[:20]
|
||||||
|
|
||||||
|
|
||||||
|
def test_snippet_preserves_boundary_markers_and_truncation():
|
||||||
|
content = "A" * 50 + "target" + "Z" * 50
|
||||||
|
|
||||||
|
snippet = _snippet(content, "target", radius=5)
|
||||||
|
|
||||||
|
assert snippet == "...AAAAAtargetZZZZZ..."
|
||||||
|
|
||||||
|
|
||||||
def test_session_search_uses_fts_and_returns_context():
|
def test_session_search_uses_fts_and_returns_context():
|
||||||
db = _db(with_fts=True)
|
db = _db(with_fts=True)
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
"""Raw skill source needs a usable editor height without hiding its footer."""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("width", [1440, 390])
|
||||||
|
@pytest.mark.parametrize("lines", [8, 40, 400])
|
||||||
|
def test_skill_editor_source_height(tmp_path, width, lines):
|
||||||
|
if not shutil.which("node"):
|
||||||
|
pytest.skip("node not installed")
|
||||||
|
repo = Path(__file__).resolve().parents[1]
|
||||||
|
index = BeautifulSoup((repo / "static/index.html").read_text(), "html.parser")
|
||||||
|
modal = index.select_one("#memory-modal")
|
||||||
|
modal["class"] = [value for value in modal.get("class", []) if value != "hidden"]
|
||||||
|
for panel in modal.select("[data-memory-panel]"):
|
||||||
|
classes = set(panel.get("class", []))
|
||||||
|
if panel["data-memory-panel"] == "skills":
|
||||||
|
classes.discard("hidden")
|
||||||
|
else:
|
||||||
|
classes.add("hidden")
|
||||||
|
panel["class"] = sorted(classes)
|
||||||
|
styles = "".join(str(node) for node in index.select('link[rel="stylesheet"]'))
|
||||||
|
document = tmp_path / "skills.html"
|
||||||
|
document.write_text(styles + str(modal) + str(index.select_one("#toast")), encoding="utf-8")
|
||||||
|
script = r"""
|
||||||
|
const fs=require('node:fs'),assert=require('node:assert/strict');
|
||||||
|
let chromium;
|
||||||
|
try{({chromium}=require('playwright'));}catch(e){if(e.code==='MODULE_NOT_FOUND')process.exit(77);throw e;}
|
||||||
|
(async()=>{
|
||||||
|
const browser=await chromium.launch({headless:true});
|
||||||
|
try{
|
||||||
|
const origin=process.env.ODYSSEUS_TEST_STATIC_ORIGIN,lines=Number(process.argv[3]);
|
||||||
|
const page=await browser.newPage({viewport:{width:Number(process.argv[2]),height:900}});page.setDefaultTimeout(5000);
|
||||||
|
let source=Array.from({length:lines},(_,i)=>'Line '+(i+1)+' of synthetic skill source').join('\n'),posts=0;
|
||||||
|
const errors=[];page.on('pageerror',e=>errors.push(e.message));
|
||||||
|
await page.route(origin+'/api/**',async route=>{
|
||||||
|
const request=route.request(),path=new URL(request.url()).pathname;let data={};
|
||||||
|
if(path==='/api/skills')data={skills:[{name:'fixture-height',description:'Source height fixture',status:'draft',source:'user',confidence:0.9}]};
|
||||||
|
else if(path.endsWith('/markdown')){
|
||||||
|
if(request.method()==='POST'){posts++;source=request.postDataJSON().markdown;data={ok:true};}else data={markdown:source};
|
||||||
|
}else if(path.endsWith('/audit-all/status'))data={status:'none'};
|
||||||
|
await route.fulfill({contentType:'application/json',body:JSON.stringify(data)});
|
||||||
|
});
|
||||||
|
await page.goto(origin+'/static/js/storage.js');await page.setContent(fs.readFileSync(process.argv[1],'utf8'));
|
||||||
|
await page.evaluate(async()=>{const skills=await import('/static/js/skills.js');await skills.loadSkills();});
|
||||||
|
const card=page.locator('.skill-card[data-skill-name="fixture-height"]');await card.locator('.skill-card-name').click();
|
||||||
|
await card.getByRole('button',{name:'Edit',exact:true}).click();const editor=card.locator('.skill-md-editor');await editor.waitFor({state:'visible'});
|
||||||
|
const geometry=await editor.evaluate(el=>{
|
||||||
|
const box=el.getBoundingClientRect(),lineHeight=parseFloat(getComputedStyle(el).lineHeight);
|
||||||
|
const modal=el.closest('.modal-content').getBoundingClientRect();
|
||||||
|
return {height:box.height,visibleLines:el.clientHeight/lineHeight,modalBottom:modal.bottom,viewportHeight:innerHeight};
|
||||||
|
});
|
||||||
|
assert(geometry.visibleLines>=Math.min(lines,8),JSON.stringify(geometry));
|
||||||
|
const save=card.getByRole('button',{name:'Save',exact:true}),bounds=await save.boundingBox();
|
||||||
|
assert(bounds.y>=0&&bounds.y+bounds.height<=geometry.viewportHeight,JSON.stringify({geometry,bounds}));
|
||||||
|
assert(geometry.modalBottom<=geometry.viewportHeight+1,JSON.stringify(geometry));
|
||||||
|
assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false);
|
||||||
|
await editor.fill(source+'\nSaved final line');await save.click();await page.waitForFunction(()=>!document.querySelector('.skill-md-editor'));
|
||||||
|
assert.equal(posts,1);assert(source.endsWith('Saved final line'));assert.deepEqual(errors,[]);
|
||||||
|
console.log(JSON.stringify({width:process.argv[2],lines,geometry,posts}));
|
||||||
|
}finally{await browser.close();}
|
||||||
|
})().catch(e=>{console.error(e);process.exitCode=1;});
|
||||||
|
"""
|
||||||
|
result = subprocess.run(["node", "-e", script, str(document), str(width), str(lines)],
|
||||||
|
cwd=repo, env=os.environ.copy(), capture_output=True,
|
||||||
|
text=True, timeout=45)
|
||||||
|
if result.returncode == 77:
|
||||||
|
pytest.skip("playwright not installed")
|
||||||
|
assert result.returncode == 0, result.stdout + result.stderr
|
||||||
|
assert json.loads(result.stdout)["posts"] == 1
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
"""An unloaded or failed SKILL.md must never become editable placeholder text."""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("width", [1440, 390])
|
||||||
|
@pytest.mark.parametrize("mode", ["delayed", "error", "menu-error", "empty", "collapse"])
|
||||||
|
def test_skill_editor_waits_for_source(tmp_path, width, mode):
|
||||||
|
if not shutil.which("node"):
|
||||||
|
pytest.skip("node not installed")
|
||||||
|
repo = Path(__file__).resolve().parents[1]
|
||||||
|
index = BeautifulSoup((repo / "static/index.html").read_text(), "html.parser")
|
||||||
|
modal = index.select_one("#memory-modal")
|
||||||
|
modal["class"] = [value for value in modal.get("class", []) if value != "hidden"]
|
||||||
|
for panel in modal.select("[data-memory-panel]"):
|
||||||
|
classes = set(panel.get("class", []))
|
||||||
|
if panel["data-memory-panel"] == "skills":
|
||||||
|
classes.discard("hidden")
|
||||||
|
else:
|
||||||
|
classes.add("hidden")
|
||||||
|
panel["class"] = sorted(classes)
|
||||||
|
styles = "".join(str(node) for node in index.select('link[rel="stylesheet"]'))
|
||||||
|
document = tmp_path / "skills.html"
|
||||||
|
document.write_text(styles + str(modal) + str(index.select_one("#toast")), encoding="utf-8")
|
||||||
|
script = r"""
|
||||||
|
const fs=require('node:fs'),assert=require('node:assert/strict');
|
||||||
|
let chromium;try{({chromium}=require('playwright'));}catch(e){if(e.code==='MODULE_NOT_FOUND')process.exit(77);throw e;}
|
||||||
|
(async()=>{
|
||||||
|
const browser=await chromium.launch({headless:true});
|
||||||
|
try{
|
||||||
|
const origin=process.env.ODYSSEUS_TEST_STATIC_ORIGIN,mode=process.argv[3];
|
||||||
|
const page=await browser.newPage({viewport:{width:Number(process.argv[2]),height:900}});page.setDefaultTimeout(5000);
|
||||||
|
const original=mode==='empty'?'':'---\nname: fixture-loading\n---\n\n## Procedure\nOriginal source';
|
||||||
|
let source=original,posts=0,gets=0,release;const gate=new Promise(r=>release=r),errors=[];
|
||||||
|
page.on('pageerror',e=>errors.push(e.message));
|
||||||
|
await page.route(origin+'/api/**',async route=>{
|
||||||
|
const request=route.request(),path=new URL(request.url()).pathname;let data={};
|
||||||
|
if(path==='/api/skills')data={skills:[{name:'fixture-loading',description:'Lazy loading fixture',status:'draft',source:'user',confidence:0.9}]};
|
||||||
|
else if(path.endsWith('/markdown')){
|
||||||
|
if(request.method()==='POST'){posts++;source=request.postDataJSON().markdown;data={ok:true};}
|
||||||
|
else{
|
||||||
|
gets++;if(gets===1)await gate;
|
||||||
|
if((mode==='error'&&gets===1)||(mode==='menu-error'&&gets<=2)){
|
||||||
|
await route.fulfill({status:500,contentType:'application/json',body:JSON.stringify({detail:'Fixture read failed'})});return;
|
||||||
|
}
|
||||||
|
data={markdown:source};
|
||||||
|
}
|
||||||
|
}else if(path.endsWith('/audit-all/status'))data={status:'none'};
|
||||||
|
await route.fulfill({contentType:'application/json',body:JSON.stringify(data)});
|
||||||
|
});
|
||||||
|
await page.goto(origin+'/static/js/storage.js');await page.setContent(fs.readFileSync(process.argv[1],'utf8'));
|
||||||
|
await page.evaluate(async()=>{const skills=await import('/static/js/skills.js');await skills.loadSkills();});
|
||||||
|
const card=page.locator('.skill-card[data-skill-name="fixture-loading"]');await card.locator('.skill-card-name').click();
|
||||||
|
await card.getByText('Loading…',{exact:true}).waitFor();
|
||||||
|
const edit=card.getByRole('button',{name:'Edit',exact:true});assert(await edit.isDisabled(),'Edit must stay disabled while source is pending');
|
||||||
|
assert.equal(await card.locator('.skill-md-editor').count(),0);assert.equal(posts,0);
|
||||||
|
if(mode==='collapse')await card.locator('.skill-chevron-up').click();
|
||||||
|
release();
|
||||||
|
if(mode==='error'||mode==='menu-error'){
|
||||||
|
await card.getByText('Failed to load SKILL.md: Fixture read failed',{exact:true}).waitFor();
|
||||||
|
assert(await edit.isDisabled(),'Failed source must not be editable');
|
||||||
|
assert.equal(await card.locator('.skill-md-editor').count(),0);assert.equal(posts,0);
|
||||||
|
await card.locator('.skill-chevron-up').click();
|
||||||
|
if(mode==='menu-error'){
|
||||||
|
await card.locator('.skill-kebab-btn').click();
|
||||||
|
await page.locator('.skill-kebab-menu:visible').getByRole('button',{name:'Edit',exact:true}).click();
|
||||||
|
await card.getByText('Failed to load SKILL.md: Fixture read failed',{exact:true}).waitFor();
|
||||||
|
assert.equal(await card.locator('.skill-md-editor').count(),0);
|
||||||
|
assert(await edit.isDisabled());assert.equal(posts,0);
|
||||||
|
await card.locator('.skill-chevron-up').click();
|
||||||
|
}
|
||||||
|
await card.locator('.skill-card-name').click();
|
||||||
|
}else if(mode==='collapse'){
|
||||||
|
await page.waitForFunction(()=>document.querySelector('.skill-card')?._mdLoaded);
|
||||||
|
assert.equal(await card.locator('.skill-md-editor').count(),0);
|
||||||
|
assert(!(await card.getAttribute('class')).includes('doclib-card-expanded'));
|
||||||
|
await card.locator('.skill-card-name').click();
|
||||||
|
}
|
||||||
|
await page.waitForFunction(()=>document.querySelector('.skill-card')?._mdLoaded);
|
||||||
|
assert(await edit.isEnabled());await edit.click();const editor=card.locator('.skill-md-editor');await editor.waitFor({state:'visible'});assert.equal(await editor.inputValue(),original);
|
||||||
|
await editor.fill(original+'\nSaved real source');await card.getByRole('button',{name:'Save',exact:true}).click();await page.waitForFunction(()=>!document.querySelector('.skill-md-editor'));
|
||||||
|
assert.equal(posts,1);assert.equal(source,original+'\nSaved real source');assert.deepEqual(errors,[]);
|
||||||
|
console.log(JSON.stringify({mode,width:process.argv[2],gets,posts}));
|
||||||
|
}finally{await browser.close();}
|
||||||
|
})().catch(e=>{console.error(e);process.exitCode=1;});
|
||||||
|
"""
|
||||||
|
result = subprocess.run(["node", "-e", script, str(document), str(width), mode],
|
||||||
|
cwd=repo, env=os.environ.copy(), capture_output=True,
|
||||||
|
text=True, timeout=45)
|
||||||
|
if result.returncode == 77:
|
||||||
|
pytest.skip("playwright not installed")
|
||||||
|
assert result.returncode == 0, result.stdout + result.stderr
|
||||||
|
assert json.loads(result.stdout)["posts"] == 1
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user