diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 229a4d599..5d70ee765 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -256,3 +256,10 @@ jobs: env: PYTEST_SHARD: ${{ matrix.shard }} run: python -m pytest -q -rs --durations=25 --shard "$PYTEST_SHARD" + + - name: Playwright mobile layout checks (shard 1/4 only) + if: steps.docs-check.outputs.docs_only != 'true' && matrix.shard == '1/4' + run: | + ODYSSEUS_TEST_PYTHON=python ODYSSEUS_LAYOUT_BROWSER=chromium npx --no-install playwright test \ + --config tests/e2e/ios-layout.config.cjs \ + --reporter=line diff --git a/package.json b/package.json index aa454e017..79cf83c3d 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,11 @@ "test:photo-editor": "playwright test --config tests/e2e/playwright.config.js", "test:photo-editor:install": "playwright install chromium firefox webkit", "test:photo-editor:firefox": "PHOTO_EDITOR_E2E_BROWSER=firefox playwright test --config tests/e2e/playwright.config.js", - "test:photo-editor:webkit": "PHOTO_EDITOR_E2E_BROWSER=webkit playwright test --config tests/e2e/playwright.config.js" + "test:photo-editor:webkit": "PHOTO_EDITOR_E2E_BROWSER=webkit playwright test --config tests/e2e/playwright.config.js", + "test:ios-layout": "playwright test --config tests/e2e/ios-layout.config.cjs", + "test:ios-layout:chromium": "ODYSSEUS_LAYOUT_BROWSER=chromium playwright test --config tests/e2e/ios-layout.config.cjs", + "test:ios-layout:webkit": "ODYSSEUS_LAYOUT_BROWSER=webkit playwright test --config tests/e2e/ios-layout.config.cjs", + "test:ios-layout:install": "playwright install chromium webkit" }, "devDependencies": { "@antithesishq/bombadil": "^0.7.0", diff --git a/routes/cookbook_helpers.py b/routes/cookbook_helpers.py index 856c8bdb5..24419c29c 100644 --- a/routes/cookbook_helpers.py +++ b/routes/cookbook_helpers.py @@ -1030,6 +1030,166 @@ def _append_llama_cpp_linux_accel_build_lines(runner_lines: list[str]) -> None: runner_lines.append(' fi # end _odysseus_have_prebuilt guard') +ODYSSEUS_LLAMA_SHIM_MARKER = "# Auto-generated by Odysseus Cookbook: a `llama-server` lookalike" + + +def _append_llama_server_resolution_definition_lines(runner_lines: list[str]) -> None: + """Define the helper shell function that discovers, reconciles and prioritizes llama-server.""" + marker = f"'{_bash_squote(ODYSSEUS_LLAMA_SHIM_MARKER)}'" + runner_lines.append('_odysseus_resolve_llama_server() {') + runner_lines.append(' _odysseus_llama_bin=""') + runner_lines.append(f' local _marker={marker}') + runner_lines.append(' local _saved_ifs="$IFS"') + runner_lines.append(" IFS=':'") + runner_lines.append(' local -a _parts') + runner_lines.append(' read -r -a _parts <<< "$PATH"') + runner_lines.append(' IFS="$_saved_ifs"') + runner_lines.append(' local _dir _cand') + runner_lines.append(' for _dir in "${_parts[@]}"; do') + runner_lines.append(' [ -n "$_dir" ] || continue') + runner_lines.append(' _cand="$_dir/llama-server"') + runner_lines.append(' [ -x "$_cand" ] || continue') + runner_lines.append(' if [ -f "$_cand" ] && [ ! -L "$_cand" ] && grep -Fxq "$_marker" "$_cand" 2>/dev/null; then') + runner_lines.append(' continue') + runner_lines.append(' fi') + runner_lines.append(' if "$_cand" --version >/dev/null 2>&1; then') + runner_lines.append(' _odysseus_llama_bin="$_cand"') + runner_lines.append(' break') + runner_lines.append(' fi') + runner_lines.append(' done') + runner_lines.append(' if [ -f "$HOME/bin/llama-server" ] && [ ! -L "$HOME/bin/llama-server" ] && grep -Fxq "$_marker" "$HOME/bin/llama-server" 2>/dev/null; then') + runner_lines.append(' if [ -n "$_odysseus_llama_bin" ]; then') + runner_lines.append(' echo "[odysseus] Native llama-server detected — retiring Odysseus compatibility shim in ~/bin"') + runner_lines.append(' rm -f "$HOME/bin/llama-server"') + runner_lines.append(' hash -r') + runner_lines.append(' elif python3 -c "import llama_cpp.server" 2>/dev/null; then') + runner_lines.append(' _odysseus_llama_bin="$HOME/bin/llama-server"') + runner_lines.append(' else') + runner_lines.append(' echo "[odysseus] Removing stale Odysseus llama-server shim (python3 -m llama_cpp.server unavailable)"') + runner_lines.append(' rm -f "$HOME/bin/llama-server"') + runner_lines.append(' hash -r') + runner_lines.append(' fi') + runner_lines.append(' fi') + runner_lines.append(' if [ -n "$_odysseus_llama_bin" ]; then') + runner_lines.append(' local _cand_dir') + runner_lines.append(' _cand_dir="$(dirname "$_odysseus_llama_bin")"') + runner_lines.append(' if [ "${PATH%%:*}" != "$_cand_dir" ]; then') + runner_lines.append(' export PATH="$_cand_dir:$PATH"') + runner_lines.append(' fi') + runner_lines.append(' hash -r') + runner_lines.append(' return 0') + runner_lines.append(' fi') + runner_lines.append(' return 1') + runner_lines.append('}') + + +def _append_llama_server_shim_definition_lines(runner_lines: list[str]) -> None: + """Define the helper shell function that generates the Odysseus compatibility shim.""" + _append_llama_server_resolution_definition_lines(runner_lines) + marker = f"'{_bash_squote(ODYSSEUS_LLAMA_SHIM_MARKER)}'" + runner_lines.append('_odysseus_create_llama_shim() {') + runner_lines.append(' local _target="$HOME/bin/llama-server"') + runner_lines.append(f' local _marker={marker}') + runner_lines.append(' if [ -e "$_target" ] || [ -L "$_target" ]; then') + runner_lines.append(' if [ -f "$_target" ] && [ ! -L "$_target" ] && grep -Fxq "$_marker" "$_target" 2>/dev/null; then') + runner_lines.append(' : # existing Odysseus shim — safe to replace') + runner_lines.append(' else') + runner_lines.append(' echo "[odysseus] WARNING: $_target already exists and is not an Odysseus-managed shim; leaving untouched." >&2') + runner_lines.append(' return 1') + runner_lines.append(' fi') + runner_lines.append(' fi') + runner_lines.append(' mkdir -p "$HOME/bin" || return 1') + runner_lines.append(' local _tmp') + runner_lines.append(' _tmp="$(mktemp "$HOME/bin/.llama-server.odysseus.tmp.XXXXXX")" || {') + runner_lines.append(' echo "[odysseus] ERROR: unable to create secure temporary llama-server shim in $HOME/bin" >&2') + runner_lines.append(' return 1') + runner_lines.append(' }') + runner_lines.append(" if ! cat > \"$_tmp\" <<'_ODY_LLAMA_SHIM_EOF'") + runner_lines.append('#!/usr/bin/env bash') + runner_lines.append(ODYSSEUS_LLAMA_SHIM_MARKER) + runner_lines.append('# that translates the native CLI to `python -m llama_cpp.server`.') + runner_lines.append('# Lets cookbook-generated launch commands run unchanged on hosts') + runner_lines.append('# where only the pip llama-cpp-python package is installed.') + runner_lines.append('ARGS=()') + runner_lines.append('while [ $# -gt 0 ]; do') + runner_lines.append(' case "$1" in') + runner_lines.append(' -ngl|--gpu-layers|--n-gpu-layers) ARGS+=(--n_gpu_layers "$2"); shift 2 ;;') + runner_lines.append(' -c|--ctx-size) ARGS+=(--n_ctx "$2"); shift 2 ;;') + runner_lines.append(' -b|--batch-size) ARGS+=(--n_batch "$2"); shift 2 ;;') + runner_lines.append(' -ub|--ubatch-size) shift 2 ;; # llama-cpp-python has no separate ubatch') + runner_lines.append(' --flash-attn) ARGS+=(--flash_attn true); shift 2 ;;') + runner_lines.append(' --cache-type-k) ARGS+=(--type_k "$2"); shift 2 ;;') + runner_lines.append(' --cache-type-v) ARGS+=(--type_v "$2"); shift 2 ;;') + runner_lines.append(' --n-cpu-moe) ARGS+=(--n_cpu_moe "$2"); shift 2 ;;') + runner_lines.append(' --mmproj) ARGS+=(--clip_model_path "$2"); shift 2 ;;') + runner_lines.append(' --image-max-tokens) shift 2 ;; # native-only') + runner_lines.append(' --no-mmap) ARGS+=(--no_mmap true); shift ;;') + runner_lines.append(' --no-warmup) shift ;; # native-only') + runner_lines.append(' --chat-template) ARGS+=(--chat_format "$2"); shift 2 ;;') + runner_lines.append(' --fit|--split-mode|--tensor-split|--main-gpu|--parallel) shift 2 ;; # native-only') + runner_lines.append(' --mlock) ARGS+=(--use_mlock true); shift ;;') + runner_lines.append(' *) ARGS+=("$1"); shift ;;') + runner_lines.append(' esac') + runner_lines.append('done') + runner_lines.append('exec python3 -m llama_cpp.server "${ARGS[@]}"') + runner_lines.append('_ODY_LLAMA_SHIM_EOF') + runner_lines.append(' then') + runner_lines.append(' rm -f -- "$_tmp"') + runner_lines.append(' return 1') + runner_lines.append(' fi') + runner_lines.append(' chmod +x "$_tmp" || { rm -f -- "$_tmp"; return 1; }') + runner_lines.append(' mv -f -- "$_tmp" "$_target" || { rm -f -- "$_tmp"; return 1; }') + runner_lines.append(' hash -r') + runner_lines.append(' echo "[odysseus] Created llama-server shim → python -m llama_cpp.server (no native binary needed)"') + runner_lines.append('}') + + +def _append_llama_server_shim_reconciliation_lines(runner_lines: list[str]) -> None: + """Reconcile an existing ~/bin/llama-server if it is owned by Odysseus Cookbook. + + Invariants: + - Never touch an arbitrary user-owned executable or symlink. + - If ~/bin/llama-server is an Odysseus generated shim: + - If a native llama-server is present and healthy elsewhere on PATH, remove the shim + so the native binary is used. + - If python3 -m llama_cpp.server is unavailable, remove the stale shim + and hash -r so the fallback/build path is triggered. + - If no healthy native server exists and llama_cpp.server is working, preserve it. + - If a healthy native candidate exists on PATH (even later in PATH), prioritize it and + ensure the launched executable is the validated candidate. + """ + runner_lines.append('[ -r ~/.config/odysseus-llama-cpp-env ] && . ~/.config/odysseus-llama-cpp-env') + runner_lines.append('if ! declare -F _odysseus_resolve_llama_server >/dev/null 2>&1; then') + _append_llama_server_resolution_definition_lines(runner_lines) + runner_lines.append('fi') + runner_lines.append('_odysseus_resolve_llama_server || true') + + +def _append_llama_server_finalization_lines(runner_lines: list[str], platform_hint: str = "") -> None: + """Reconcile post-install launcher availability and enforce fail-closed invariant. + + Guarantees that if python3 -m llama_cpp.server is usable but the `llama-server` + command is unresolvable, the compatibility launcher is created. If no usable + launcher exists after all build/install attempts, sets ODYSSEUS_PREFLIGHT_EXIT=127. + """ + err_msg = ( + f"ERROR: llama.cpp serving is not available on {platform_hint} after install attempts." + if platform_hint + else "ERROR: llama.cpp serving is not available after install/build attempts." + ) + runner_lines.append('if ! declare -F _odysseus_resolve_llama_server >/dev/null 2>&1; then') + _append_llama_server_resolution_definition_lines(runner_lines) + runner_lines.append('fi') + runner_lines.append('if [ -z "$_odysseus_llama_bin" ] && python3 -c "import llama_cpp.server" 2>/dev/null; then') + runner_lines.append(' _odysseus_create_llama_shim') + runner_lines.append(' _odysseus_resolve_llama_server || true') + runner_lines.append('fi') + runner_lines.append('if [ -z "$_odysseus_llama_bin" ]; then') + runner_lines.append(f' echo "{err_msg}"') + runner_lines.append(' ODYSSEUS_PREFLIGHT_EXIT=127') + runner_lines.append('fi') + + def _llama_cpp_rebuild_cmd(update_source: bool = False) -> str: """Shell command that clears the Cookbook-managed llama.cpp build. diff --git a/routes/cookbook_routes.py b/routes/cookbook_routes.py index 02b419894..ae2667f65 100644 --- a/routes/cookbook_routes.py +++ b/routes/cookbook_routes.py @@ -57,9 +57,11 @@ from routes.cookbook_helpers import ( _pip_install_no_cache, _user_shell_path_bootstrap, _venv_safe_local_pip_install_cmd, _diagnose_serve_output, run_ssh_command_async, _ollama_bind_from_cmd, _pip_install_fallback_chain, _pip_install_no_cache, - _user_shell_path_bootstrap, _venv_safe_local_pip_install_cmd, _append_pip_install_runner_lines, _pip_install_command_without_break_system_packages, _normalize_llama_cpp_python_cache_types, + _append_llama_server_shim_definition_lines, + _append_llama_server_shim_reconciliation_lines, + _append_llama_server_finalization_lines, ModelDownloadRequest, ServeRequest, ) @@ -2161,7 +2163,7 @@ def setup_cookbook_routes() -> APIRouter: ps_lines.append('}') elif "llama_cpp" in req.cmd or "llama-server" in req.cmd: ps_lines.append('# Auto-install llama-cpp-python if missing') - ps_lines.append('try { python -c "import llama_cpp" 2>$null } catch {}') + ps_lines.append('try { python -c "import llama_cpp.server" 2>$null } catch {}') ps_lines.append('if ($LASTEXITCODE -ne 0) {') ps_lines.append(' Write-Host "Installing llama-cpp-python..."') ps_lines.append(' python -m pip install llama-cpp-python[server]') @@ -2247,14 +2249,17 @@ def setup_cookbook_routes() -> APIRouter: # ollama is found (otherwise macOS falls back to a slow source build). # /opt/homebrew = Apple Silicon, /usr/local = Intel; harmless on Linux. runner_lines.append('export PATH="$HOME/.local/bin:$HOME/bin:$HOME/llama.cpp/build/bin:/opt/homebrew/bin:/usr/local/bin:$PATH"') + _append_llama_server_shim_definition_lines(runner_lines) + _append_llama_server_shim_reconciliation_lines(runner_lines) runner_lines.append('if [ -d /data/data/com.termux ]; then') runner_lines.append(' # Termux: no native build — use the Python bindings (CPU).') - runner_lines.append(' if ! python3 -c "import llama_cpp" 2>/dev/null; then') + runner_lines.append(' if ! python3 -c "import llama_cpp.server" 2>/dev/null; then') runner_lines.append(' pkg install -y cmake 2>/dev/null') runner_lines.append(' pip install numpy diskcache jinja2 2>/dev/null') runner_lines.append(' CMAKE_ARGS="-DGGML_BLAS=OFF -DGGML_LLAMAFILE=OFF" pip install \'llama-cpp-python[server]\' --no-build-isolation --no-cache-dir 2>&1 || true') runner_lines.append(' fi') - runner_lines.append('elif ! command -v llama-server &>/dev/null; then') + _append_llama_server_finalization_lines(runner_lines, platform_hint="Termux") + runner_lines.append('elif [ -z "$_odysseus_llama_bin" ]; then') runner_lines.append(' echo "Native llama-server not found — building from source (one-time, may take a few minutes)..."') runner_lines.append(' mkdir -p ~/bin') runner_lines.append(' cd ~ && [ -d llama.cpp ] || git clone --depth 1 https://github.com/ggml-org/llama.cpp') @@ -2271,8 +2276,7 @@ def setup_cookbook_routes() -> APIRouter: # would reuse the bad settings and fail again. CMAKE_BUILD_TYPE is # explicit so the binary is optimized (Metal auto-enables on macOS). runner_lines.append(' cd ~/llama.cpp && rm -rf build && cmake -B build -DCMAKE_BUILD_TYPE=Release \\') - runner_lines.append(' && cmake --build build -j"$NPROC" --target llama-server \\') - runner_lines.append(' && ln -sf ~/llama.cpp/build/bin/llama-server ~/bin/llama-server') + runner_lines.append(' && cmake --build build -j"$NPROC" --target llama-server') runner_lines.append(' else') _append_llama_cpp_linux_accel_build_lines(runner_lines) runner_lines.append(' fi') @@ -2296,58 +2300,13 @@ def setup_cookbook_routes() -> APIRouter: runner_lines.append(' fi') runner_lines.append(' fi') runner_lines.append(' fi') - # SHORT-CIRCUIT before the build/pip fallback: if the - # native binary is missing but llama_cpp Python is already - # installed, drop a wrapper at ~/bin/llama-server that - # translates llama-server CLI args to llama_cpp.server's - # underscore-style flags. The user's serve command stays - # `llama-server ...` and "just works" — no build, no cmake, - # no second install. This is the path that unblocks every - # remote where pip-installed llama-cpp-python is already - # working but Cookbook used to insist on a native binary. - runner_lines.append(' if ! command -v llama-server >/dev/null 2>&1 && python3 -c "import llama_cpp" 2>/dev/null; then') - runner_lines.append(' mkdir -p ~/bin') - runner_lines.append(' cat > ~/bin/llama-server <<\'_ODY_LLAMA_SHIM_EOF\'') - runner_lines.append('#!/usr/bin/env bash') - runner_lines.append('# Auto-generated by Odysseus Cookbook: a `llama-server` lookalike') - runner_lines.append('# that translates the native CLI to `python -m llama_cpp.server`.') - runner_lines.append('# Lets cookbook-generated launch commands run unchanged on hosts') - runner_lines.append('# where only the pip llama-cpp-python package is installed.') - runner_lines.append('ARGS=()') - runner_lines.append('while [ $# -gt 0 ]; do') - runner_lines.append(' case "$1" in') - runner_lines.append(' -ngl|--gpu-layers|--n-gpu-layers) ARGS+=(--n_gpu_layers "$2"); shift 2 ;;') - runner_lines.append(' -c|--ctx-size) ARGS+=(--n_ctx "$2"); shift 2 ;;') - runner_lines.append(' -b|--batch-size) ARGS+=(--n_batch "$2"); shift 2 ;;') - runner_lines.append(' -ub|--ubatch-size) shift 2 ;; # llama-cpp-python has no separate ubatch') - runner_lines.append(' --flash-attn) ARGS+=(--flash_attn true); shift 2 ;;') - runner_lines.append(' --cache-type-k) ARGS+=(--type_k "$2"); shift 2 ;;') - runner_lines.append(' --cache-type-v) ARGS+=(--type_v "$2"); shift 2 ;;') - runner_lines.append(' --n-cpu-moe) ARGS+=(--n_cpu_moe "$2"); shift 2 ;;') - runner_lines.append(' --mmproj) ARGS+=(--clip_model_path "$2"); shift 2 ;;') - runner_lines.append(' --image-max-tokens) shift 2 ;; # native-only') - runner_lines.append(' --no-mmap) ARGS+=(--no_mmap true); shift ;;') - runner_lines.append(' --no-warmup) shift ;; # native-only') - runner_lines.append(' --chat-template) ARGS+=(--chat_format "$2"); shift 2 ;;') - runner_lines.append(' --fit|--split-mode|--tensor-split|--main-gpu|--parallel) shift 2 ;; # native-only') - runner_lines.append(' --mlock) ARGS+=(--use_mlock true); shift ;;') - runner_lines.append(' *) ARGS+=("$1"); shift ;;') - runner_lines.append(' esac') - runner_lines.append('done') - runner_lines.append('exec python3 -m llama_cpp.server "${ARGS[@]}"') - runner_lines.append('_ODY_LLAMA_SHIM_EOF') - runner_lines.append(' chmod +x ~/bin/llama-server') - runner_lines.append(' echo "[odysseus] Created llama-server shim → python -m llama_cpp.server (no native binary needed)"') - runner_lines.append(' fi') - runner_lines.append(' # If the native build failed, fall back to the Python bindings.') - runner_lines.append(' if ! command -v llama-server &>/dev/null && ! python3 -c "import llama_cpp" 2>/dev/null; then') + runner_lines.append(' _odysseus_resolve_llama_server || true') + # If the native build failed, fall back to the Python bindings. + runner_lines.append(' if [ -z "$_odysseus_llama_bin" ] && ! python3 -c "import llama_cpp.server" 2>/dev/null; then') runner_lines.append(' echo "llama-server build failed — installing Python bindings as fallback..."') runner_lines.append(f" {_pip_install_fallback_chain('llama-cpp-python[server]', python_cmd='pip')} || true") runner_lines.append(' fi') - runner_lines.append(' if ! command -v llama-server &>/dev/null && ! python3 -c "import llama_cpp" 2>/dev/null; then') - runner_lines.append(' echo "ERROR: llama.cpp serving is not available after install/build attempts."') - runner_lines.append(' ODYSSEUS_PREFLIGHT_EXIT=127') - runner_lines.append(' fi') + _append_llama_server_finalization_lines(runner_lines) runner_lines.append('fi') elif re.search(r"\bollama\s+serve\b", req.cmd): handled_ollama_serve = True diff --git a/routes/session_routes.py b/routes/session_routes.py index 1c903f194..388de5b20 100644 --- a/routes/session_routes.py +++ b/routes/session_routes.py @@ -1315,7 +1315,7 @@ def setup_session_routes( def auto_sort_sessions(request: Request, skip_llm: bool = False): """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 after Phase 1 — used by the "Tidy (no AI)" UI affordance so users can clean junk without spending tokens. @@ -1326,21 +1326,11 @@ def setup_session_routes( single_user_mode = not user and _auth_disabled() 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 db = SessionLocal() deleted_empty = 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: rows_q = db.query(DbSession).filter(DbSession.archived == False) if user: @@ -1349,15 +1339,11 @@ def setup_session_routes( rows_q = rows_q.filter(DbSession.owner == user) rows = rows_q.limit(2000).all() 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 # loop was doing thousands of round-trips and blowing the timeout. 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()) - _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() for row in rows: # Never delete important sessions @@ -1367,9 +1353,10 @@ def setup_session_routes( if (row.name or "").strip() == "Incognito": should_delete = True deleted_throwaway += 1 - db.delete(row) if hasattr(session_manager, 'delete_session'): session_manager.delete_session(row.id) + else: + db.delete(row) continue if is_session_recently_active(row, now=cleanup_now): continue @@ -1378,31 +1365,11 @@ def setup_session_routes( if msg_count == 0: should_delete = True 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: - db.delete(row) if hasattr(session_manager, 'delete_session'): session_manager.delete_session(row.id) + else: + db.delete(row) if deleted_empty or deleted_throwaway: db.commit() logger.info(f"Auto-sort: deleted {deleted_empty} empty + {deleted_throwaway} throwaway sessions") diff --git a/src/mcp_manager.py b/src/mcp_manager.py index 53112009d..b47d0e1d7 100644 --- a/src/mcp_manager.py +++ b/src/mcp_manager.py @@ -10,6 +10,7 @@ import logging import os import re import asyncio +import contextlib from typing import Any, Dict, List, Optional, Set, Tuple from src.database import McpServer, SessionLocal @@ -383,12 +384,21 @@ class McpManager: provider = build_provider(server_id, url, on_redirect=_on_redirect) stack = AsyncExitStack() - transport = await stack.enter_async_context(streamablehttp_client(url, auth=provider)) - read_stream, write_stream, _get_session_id = transport - session = await stack.enter_async_context(ClientSession(read_stream, write_stream)) - await session.initialize() + try: + transport = await stack.enter_async_context(streamablehttp_client(url, auth=provider)) + read_stream, write_stream, _get_session_id = transport + session = await stack.enter_async_context(ClientSession(read_stream, write_stream)) + await session.initialize() - tools_result = await session.list_tools() + tools_result = await session.list_tools() + except BaseException: + # Close the half-open transport here, in the task that entered + # it. Left for the garbage collector, its AnyIO cancel scope is + # exited from the wrong task and the event loop keeps spinning + # (#5518). Its own teardown error would only mask the real one. + with contextlib.suppress(Exception): + await stack.aclose() + raise tools = [] for tool in tools_result.tools: tools.append({ diff --git a/src/session_actions.py b/src/session_actions.py index 1efa484e3..9859ef486 100644 --- a/src/session_actions.py +++ b/src/session_actions.py @@ -12,16 +12,6 @@ from datetime import datetime, timedelta, timezone 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_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: - """Run session cleanup + (optional) AI folder sort for the given owner. + """Remove empty/Incognito sessions and optionally organize saved chats. Args: 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 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. """ @@ -71,7 +62,7 @@ async def run_auto_sort(owner: str, skip_llm: bool = False, delete_throwaway: bo db = SessionLocal() try: - # ── Phase 1: Delete empty/throwaway sessions ── + # ── Phase 1: Delete only empty and explicitly Incognito sessions ── deleted_empty = 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): continue - msg_count = db.query(DbMsg.id).filter( + has_messages = db.query(DbMsg.id).filter( DbMsg.session_id == row.id - ).limit(_THROWAWAY_MAX_MESSAGES + 1).count() - should_delete = False - - if msg_count == 0: + ).first() is not None + if not has_messages: if is_fresh: continue - should_delete = True 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) if deleted_empty or deleted_throwaway: diff --git a/static/css/17-research.css b/static/css/17-research.css index c4de5cb75..2c43aa2c8 100644 --- a/static/css/17-research.css +++ b/static/css/17-research.css @@ -2690,11 +2690,14 @@ body.theme-frosted .modal { max-height: 95dvh !important; } + /* Notes and Documents must respect the same viewport and safe-area + top as other mobile full-height sheets. Keep these authoritative rules + consistent with 05-documents.css and 14-email.css. */ body.doc-view .doc-editor-pane, body.notes-view .notes-pane { - inset: auto 0 0 !important; - height: 95dvh !important; - max-height: 95dvh !important; + inset: var(--sheet-top) 0 0 !important; + height: calc(var(--app-height) - var(--sheet-top)) !important; + max-height: calc(var(--app-height) - var(--sheet-top)) !important; } } diff --git a/static/index.html b/static/index.html index aafacc2e1..df3045af3 100644 --- a/static/index.html +++ b/static/index.html @@ -16,13 +16,6 @@