Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: load sidebar chats, projects and training runs 20 at a time from the server (#13420)
Daniel Han6db2bf05e388
diff --git a/studio/backend/loggers/handlers.py b/studio/backend/loggers/handlers.pyindex 8c7574bf1c6..dbd5a409455 100644--- a/studio/backend/loggers/handlers.py+++ b/studio/backend/loggers/handlers.py@@ -182,11 +182,18 @@ _SELF_READ_PATHS = {}+_CHAT_LIST_POST_READ_PATHS = frozenset({"/api/chat/sidebar/rows"})++def _is_quiet_success(method: str, path: str, status_code: int, pre_auth: bool) -> bool:- """GET-only. Suppress a 2xx poll line that carries no signal, plus a chat list poll's transient pre-auth 401- (only in the bootstrap window before the first successful token refresh). Mutations, real (post-refresh)- auth failures, and all other errors always log. --verbose disables the whole suppressor, except for the log- viewer's own reads."""+ """GET-only, plus the listed POST reads. Suppress a 2xx poll line that carries no signal, plus a chat list+ poll's transient pre-auth 401 (only in the bootstrap window before the first successful token refresh).+ Mutations, real (post-refresh) auth failures, and all other errors always log. --verbose disables the whole+ suppressor, except for the log viewer's own reads."""+ if method == "POST" and path in _CHAT_LIST_POST_READ_PATHS:+ if _VERBOSE_ACCESS_LOG:+ return False+ return 200 <= status_code < 300 or (pre_auth and status_code == 401)if method != "GET":return Falseif 200 <= status_code < 300 and path in _SELF_READ_PATHS:diff --git a/studio/backend/routes/chat_history.py b/studio/backend/routes/chat_history.pyindex 296fce8780a..c0e7949efee 100644--- a/studio/backend/routes/chat_history.py+++ b/studio/backend/routes/chat_history.py@@ -69,6 +69,10 @@ from storage.studio_db import (get_chat_message,list_chat_attachments_page,list_chat_projects,+ list_chat_sidebar_rows,+ lookup_chat_sidebar_row_ids,+ SidebarCursorError,+ SIDEBAR_ROWS_MAX_LIMIT,list_chat_legacy_imports,list_chat_settings,list_chat_messages,@@ -332,6 +336,7 @@ class ChatProject(BaseModel):archived: bool = FalsecreatedAt: intupdatedAt: int+ lastActivityAt: Optional[int] = Noneclass ChatProjectWorkingFolder(BaseModel):@@ -365,6 +370,47 @@ class ChatProjectListResponse(BaseModel):projects: list[ChatProject]+class ChatSidebarRowsRequest(BaseModel):+ model_config = ConfigDict(extra = "forbid")++ archived: bool = False+ projectId: Optional[str] = None+ unfiledOnly: bool = False+ includeIds: Optional[list[str]] = None+ excludeIds: list[str] = Field(default_factory = list)+ sort: Literal["updated", "manual"] = "updated"+ manualOrder: Optional[list[str]] = None+ limit: int = Field(default = 20, ge = 1, le = SIDEBAR_ROWS_MAX_LIMIT)+ cursor: Optional[str] = Field(default = None, max_length = 4096)+++class ChatSidebarRow(BaseModel):+ """One sidebar row, shaped as the frontend's SidebarItem."""++ id: str+ type: Literal["single", "compare"]+ threadIds: list[str]+ title: str+ createdAt: int+ updatedAt: int+ modifiedAt: Optional[int] = None+ modelIds: Optional[list[str]] = None+ isFork: Optional[bool] = None+ threadTitles: Optional[list[str]] = None+ projectId: Optional[str] = None+++class ChatSidebarRowsResponse(BaseModel):+ rows: list[ChatSidebarRow]+ nextCursor: Optional[str] = None+ hasMore: bool+ total: int+++class ChatSidebarThreadRowsResponse(BaseModel):+ rowIdByThreadId: dict[str, str]++class ChatMessageListResponse(BaseModel):messages: list[ChatMessage]@@ -673,6 +719,48 @@ def list_threads(return ChatThreadListResponse(threads = [thread_from_row(t) for t in threads])+ "/sidebar/rows",+ response_model = ChatSidebarRowsResponse,+ response_model_exclude_unset = True,+)+def list_sidebar_rows(+ payload: ChatSidebarRowsRequest, current_subject: str = Depends(get_current_subject)+):+ """One page of sidebar rows, compare pairs grouped before the limit."""+ try:+ page = list_chat_sidebar_rows(+ archived = payload.archived,+ project_id = payload.projectId,+ unfiled_only = payload.unfiledOnly,+ include_ids = payload.includeIds,+ exclude_ids = payload.excludeIds,+ sort = payload.sort,+ manual_order = payload.manualOrder,+ limit = payload.limit,+ cursor = payload.cursor,+ )+ except SidebarCursorError as exc:+ raise HTTPException(status_code = 400, detail = str(exc)) from exc+ return ChatSidebarRowsResponse(**page)+++_SIDEBAR_THREAD_ROWS_MAX = 500++[email protected]("/sidebar/thread-rows", response_model = ChatSidebarThreadRowsResponse)+def sidebar_thread_rows(+ thread_ids: list[str] = Query(default_factory = list),+ current_subject: str = Depends(get_current_subject),+):+ if len(thread_ids) > _SIDEBAR_THREAD_ROWS_MAX:+ raise HTTPException(+ status_code = 400,+ detail = f"At most {_SIDEBAR_THREAD_ROWS_MAX} thread ids per request",+ )+ return ChatSidebarThreadRowsResponse(rowIdByThreadId = lookup_chat_sidebar_row_ids(thread_ids))++def _missing_project_error(project_id: Optional[str]) -> HTTPException:"""The row references a project that is gone, whether the check or the write noticed it."""return HTTPException(status_code = 404, detail = f"Project {project_id} not found")@@ -1182,7 +1270,12 @@ def list_projects():return ChatProjectListResponse(projects = [- ChatProject(**(ensure_chat_project_workspace(project["id"]) or project))+ ChatProject(+ **{+ **(ensure_chat_project_workspace(project["id"]) or project),+ "lastActivityAt": project["lastActivityAt"],+ }+ )for project in list_chat_projects(include_archived = include_archived)])diff --git a/studio/backend/storage/studio_db.py b/studio/backend/storage/studio_db.pyindex c854f91048d..a5ca2bab702 100644--- a/studio/backend/storage/studio_db.py+++ b/studio/backend/storage/studio_db.py@@ -2504,6 +2504,237 @@ def list_chat_threads(conn.close()+SIDEBAR_ROWS_CURSOR_VERSION = 2+SIDEBAR_ROWS_MAX_LIMIT = 200++_SIDEBAR_ROW_ID_SQL = "COALESCE(NULLIF(pair_id, ''), id)"+_SIDEBAR_ACTIVITY_SQL = "COALESCE(updated_at, created_at)"+++class SidebarCursorError(ValueError):+ """A cursor this listing did not issue, or one issued for another sort."""+++def encode_sidebar_cursor(payload: dict) -> str:+ raw = json.dumps({"v": SIDEBAR_ROWS_CURSOR_VERSION, **payload}, separators = (",", ":")).encode()+ return base64.urlsafe_b64encode(raw).decode().rstrip("=")+++def decode_sidebar_cursor(cursor: str, sort: str) -> dict:+ try:+ padded = cursor + "=" * (-len(cursor) % 4)+ payload = json.loads(base64.urlsafe_b64decode(padded.encode()).decode())+ except (binascii.Error, UnicodeError, ValueError) as exc:+ raise SidebarCursorError("Malformed cursor") from exc+ if not isinstance(payload, dict) or payload.get("v") != SIDEBAR_ROWS_CURSOR_VERSION:+ raise SidebarCursorError("Unsupported cursor version")+ if payload.get("s") != sort:+ raise SidebarCursorError("Cursor was issued for another sort")+ keys = ("o",) if sort == "manual" else ("u", "c", "r")+ # SQLite binds 64-bit integers only; the exclusive lower bound keeps `-r` in range too+ if not all(type(v) is int and -(2**63) < v < 2**63 for v in map(payload.get, keys)):+ raise SidebarCursorError("Malformed cursor")+ if sort == "manual" and payload["o"] < 0:+ raise SidebarCursorError("Malformed cursor")+ return payload+++def list_chat_sidebar_rows(+ *,+ archived: bool = False,+ project_id: str | None = None,+ unfiled_only: bool = False,+ include_ids: list[str] | None = None,+ exclude_ids: list[str] | None = None,+ sort: str = "updated",+ manual_order: list[str] | None = None,+ limit: int = 20,+ cursor: str | None = None,+) -> dict:+ """One page of sidebar rows, grouped (before the limit) and ordered as the frontend's groupThreads+ does. `updated` pages by keyset; `manual` follows applyManualOrder and pages by offset."""+ position = decode_sidebar_cursor(cursor, sort) if cursor else None++ filters: list[str] = []+ filter_values: list[object] = []+ if include_ids is not None:+ # a row id, or any member thread id: a running compare pane names its pair by its own id. Only a+ # pane on this side of the archive filter counts, as the full list filters panes before matching+ filters.append(+ "(rows.row_id IN (SELECT value FROM json_each(?)) OR rows.row_id IN ("+ f"SELECT {_SIDEBAR_ROW_ID_SQL} FROM chat_threads WHERE id IN (SELECT value FROM json_each(?))"+ " AND (COALESCE(archived, 0) <> 0) = ?))"+ )+ filter_values += [json.dumps(include_ids), json.dumps(include_ids), 1 if archived else 0]+ if exclude_ids:+ filters.append("rows.row_id NOT IN (SELECT value FROM json_each(?))")+ filter_values.append(json.dumps(exclude_ids))+ if project_id is not None:+ filters.append("rows.project_id = ?")+ filter_values.append(project_id)+ if unfiled_only:+ filters.append("rows.project_id IS NULL")++ # Last occurrence wins, as `new Map(order.map((id, i) => [id, i]))` does.+ ranks_json = json.dumps(manual_order or []) if sort == "manual" else "[]"+ base = f"""+ WITH members AS (+ SELECT {_SIDEBAR_ROW_ID_SQL} AS row_id,+ CASE WHEN COALESCE(pair_id, '') <> '' THEN 1 ELSE 0 END AS is_pair,+ title,+ NULLIF(project_id, '') AS project_id,+ created_at,+ {_SIDEBAR_ACTIVITY_SQL} AS activity_at,+ NULLIF(modified_at, 0) AS modified_at,+ CASE WHEN COALESCE(forked_from_thread_id, '') <> '' THEN 1 ELSE 0 END AS is_fork,+ rowid AS member_rowid,+ ROW_NUMBER() OVER (+ PARTITION BY {_SIDEBAR_ROW_ID_SQL}+ ORDER BY {_SIDEBAR_ACTIVITY_SQL} DESC, created_at DESC, rowid ASC+ ) AS member_rank+ FROM chat_threads+ WHERE (COALESCE(archived, 0) <> 0) = ?+ ),+ rows AS (+ SELECT row_id,+ MAX(is_pair) AS is_pair,+ MAX(created_at) AS created_at,+ MAX(activity_at) AS updated_at,+ MAX(modified_at) AS modified_at,+ MAX(CASE WHEN member_rank = 1 THEN title END) AS title,+ MAX(CASE WHEN member_rank = 1 THEN project_id END) AS project_id,+ MAX(CASE WHEN member_rank = 1 THEN is_fork END) AS is_fork,+ -- groupThreads sorts stably, so rows tied on updatedAt keep list order: their newest+ -- member's created_at DESC, then storage order.+ MAX(CASE WHEN member_rank = 1 THEN created_at END) AS lead_created_at,+ MAX(CASE WHEN member_rank = 1 THEN member_rowid END) AS lead_rowid+ FROM members+ GROUP BY row_id+ ),+ ranks AS (+ SELECT value AS row_id, MAX(CAST(key AS INTEGER)) AS manual_rank+ FROM json_each(?)+ GROUP BY value+ ),+ eligible AS (+ SELECT rows.*, ranks.manual_rank+ FROM rows LEFT JOIN ranks ON ranks.row_id = rows.row_id+ {"WHERE " + " AND ".join(filters) if filters else ""}+ )+ """+ base_values: list[object] = [1 if archived else 0, ranks_json, *filter_values]++ page_values = list(base_values)+ if sort == "updated":+ where = ""+ if position is not None:+ where = "WHERE (updated_at, lead_created_at, -lead_rowid) < (?, ?, ?)"+ page_values += [position["u"], position["c"], -position["r"]]+ page_sql = (+ f"{base} SELECT * FROM (SELECT *, COUNT(*) OVER () AS total FROM eligible) {where} "+ "ORDER BY updated_at DESC, lead_created_at DESC, lead_rowid ASC LIMIT ?"+ )+ page_values.append(limit + 1)+ else:+ offset = position["o"] if position is not None else 0+ page_sql = (+ f"{base} SELECT *, COUNT(*) OVER () AS total FROM eligible "+ "ORDER BY manual_rank IS NOT NULL, manual_rank ASC, updated_at DESC, lead_created_at DESC, lead_rowid ASC "+ "LIMIT ? OFFSET ?"+ )+ page_values += [limit + 1, offset]++ conn = get_connection()+ try:+ conn.execute("BEGIN")+ page = conn.execute(page_sql, page_values).fetchall()+ if page:+ total = page[0]["total"]+ else:+ total = conn.execute(f"{base} SELECT COUNT(*) FROM eligible", base_values).fetchone()[0]+ has_more = len(page) > limit+ page = page[:limit]+ members_by_row: dict[str, list[sqlite3.Row]] = {row["row_id"]: [] for row in page}+ if page:+ row_ids = json.dumps(list(members_by_row))+ for member in conn.execute(+ f"""+ SELECT id, {_SIDEBAR_ROW_ID_SQL} AS row_id, NULLIF(model_id, '') AS model_id, title+ FROM chat_threads+ WHERE (COALESCE(archived, 0) <> 0) = ?+ AND (+ pair_id IN (SELECT value FROM json_each(?))+ OR (COALESCE(pair_id, '') = '' AND id IN (SELECT value FROM json_each(?)))+ )+ ORDER BY {_SIDEBAR_ACTIVITY_SQL} DESC, created_at DESC, rowid ASC+ """,+ (1 if archived else 0, row_ids, row_ids),+ ):+ members_by_row[member["row_id"]].append(member)+ conn.commit()+ finally:+ conn.close()++ rows: list[dict] = []+ for row in page:+ members = members_by_row[row["row_id"]]+ model_ids: list[str] = []+ for member in members:+ if member["model_id"] and member["model_id"] not in model_ids:+ model_ids.append(member["model_id"])+ item: dict[str, Any] = {+ "id": row["row_id"],+ "type": "compare" if row["is_pair"] else "single",+ "threadIds": [member["id"] for member in members],+ "title": row["title"],+ "createdAt": row["created_at"],+ "updatedAt": row["updated_at"],+ "projectId": row["project_id"],+ }+ if row["modified_at"]:+ item["modifiedAt"] = row["modified_at"]+ if model_ids:+ item["modelIds"] = model_ids+ if not row["is_pair"]:+ item["isFork"] = bool(row["is_fork"])+ else:+ item["threadTitles"] = [member["title"] or "" for member in members]+ rows.append(item)++ next_cursor = None+ if has_more and page:+ if sort == "updated":+ last = page[-1]+ next_cursor = encode_sidebar_cursor(+ {+ "s": "updated",+ "u": last["updated_at"],+ "c": last["lead_created_at"],+ "r": last["lead_rowid"],+ }+ )+ else:+ offset = (position["o"] if position is not None else 0) + len(page)+ next_cursor = encode_sidebar_cursor({"s": "manual", "o": offset})+ return {"rows": rows, "nextCursor": next_cursor, "hasMore": has_more, "total": total}+++def lookup_chat_sidebar_row_ids(thread_ids: list[str]) -> dict[str, str]:+ """The sidebar row each existing thread belongs to; unknown ids are left out."""+ if not thread_ids:+ return {}+ conn = get_connection()+ try:+ rows = conn.execute(+ f"SELECT id, {_SIDEBAR_ROW_ID_SQL} AS row_id FROM chat_threads "+ "WHERE id IN (SELECT value FROM json_each(?))",+ (json.dumps(thread_ids),),+ ).fetchall()+ return {row["id"]: row["row_id"] for row in rows}+ finally:+ conn.close()++def build_chat_history_export() -> tuple[list[dict], list[dict], list[dict]]:conn = get_connection()try:@@ -3051,13 +3282,43 @@ def get_chat_project(id: str) -> Optional[dict]:def list_chat_projects(include_archived: bool = False) -> list[dict]:+ """Projects with `lastActivityAt`: their own last edit or newest unarchived chat, whichever is later."""conn = get_connection()try:- where = "" if include_archived else "WHERE archived = 0"+ where = "" if include_archived else "WHERE p.archived = 0"rows = conn.execute(- f"SELECT * FROM chat_projects {where} ORDER BY updated_at DESC"+ f"""+ SELECT p.*,+ MAX(+ COALESCE(p.updated_at, p.created_at),+ COALESCE(chats.last_chat_at, 0)+ ) AS last_activity_at+ FROM chat_projects p+ LEFT JOIN (+ -- one row per sidebar row, as the sidebar groups it: a compare pair's activity is its+ -- newest pane's and counts for that pane's project only+ SELECT project_id, MAX(row_activity) AS last_chat_at+ FROM (+ SELECT NULLIF(project_id, '') AS project_id,+ MAX({_SIDEBAR_ACTIVITY_SQL}) OVER (PARTITION BY {_SIDEBAR_ROW_ID_SQL}) AS row_activity,+ ROW_NUMBER() OVER (+ PARTITION BY {_SIDEBAR_ROW_ID_SQL}+ ORDER BY {_SIDEBAR_ACTIVITY_SQL} DESC, created_at DESC, rowid ASC+ ) AS member_rank+ FROM chat_threads+ WHERE COALESCE(archived, 0) = 0+ )+ WHERE member_rank = 1 AND project_id IS NOT NULL+ GROUP BY project_id+ ) chats ON chats.project_id = p.id+ {where}+ ORDER BY p.updated_at DESC+ """).fetchall()- return [_chat_project_from_row(row) for row in rows]+ return [+ {**_chat_project_from_row(row), "lastActivityAt": row["last_activity_at"]}+ for row in rows+ ]finally:conn.close()diff --git a/studio/backend/tests/test_chat_sidebar_rows.py b/studio/backend/tests/test_chat_sidebar_rows.pynew file mode 100644index 00000000000..67236651151--- /dev/null+++ b/studio/backend/tests/test_chat_sidebar_rows.py@@ -0,0 +1,459 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Paged sidebar rows: parity with the frontend's groupThreads + applyManualOrder."""++import inspect+import os+import sys++import pytest+from fastapi import FastAPI, HTTPException+from fastapi.testclient import TestClient++_backend = os.path.join(os.path.dirname(__file__), "..")+sys.path.insert(0, _backend)++from auth.authentication import get_current_subject+from routes import chat_history+from storage import studio_db+++def db(tmp_path, monkeypatch):+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))+ monkeypatch.setenv("UNSLOTH_STUDIO_PROJECTS_HOME", str(tmp_path / "Projects"))+ monkeypatch.setattr(studio_db, "_schema_ready", set())+ yield tmp_path+++def client(db):+ app = FastAPI()+ app.include_router(chat_history.router, prefix = "/api/chat")+ app.dependency_overrides[get_current_subject] = lambda: "test-user"+ return TestClient(app)+++def _insert_threads(threads: list[dict]) -> None:+ conn = studio_db.get_connection()+ try:+ conn.executemany(+ """+ INSERT INTO chat_threads (id, title, model_type, model_id, pair_id, project_id, archived,+ created_at, updated_at, forked_from_thread_id, modified_at)+ VALUES (:id, :title, :model_type, :model_id, :pair_id, :project_id, :archived,+ :created_at, :updated_at, :forked_from_thread_id, :modified_at)+ """,+ [+ {+ "title": t["id"],+ "model_type": "base",+ "model_id": None,+ "pair_id": None,+ "project_id": None,+ "archived": 0,+ "updated_at": None,+ "forked_from_thread_id": None,+ "modified_at": None,+ **t,+ }+ for t in threads+ ],+ )+ conn.commit()+ finally:+ conn.close()+++def _insert_project(+ project_id: str,+ *,+ created_at: int,+ updated_at: int,+ name = None,+) -> None:+ conn = studio_db.get_connection()+ try:+ conn.execute(+ "INSERT INTO chat_projects (id, name, instructions, archived, created_at, updated_at) "+ "VALUES (?, ?, '', 0, ?, ?)",+ (project_id, name or project_id, created_at, updated_at),+ )+ conn.commit()+ finally:+ conn.close()+++def _singles(n: int, *, start: int = 1_000) -> list[dict]:+ return [{"id": f"t{i:04d}", "created_at": start + i, "updated_at": start + i} for i in range(n)]+++def _group_threads(threads: list[dict], archived: bool = False) -> list[dict]:+ """Python port of use-chat-sidebar-items.ts groupThreads, fed what GET /threads returns."""+ items: list[dict] = []+ pairs: dict[str, dict] = {}+ for t in threads:+ if bool(t.get("archived")) != archived:+ continue+ activity = t.get("updatedAt") if t.get("updatedAt") is not None else t["createdAt"]+ if t.get("pairId"):+ existing = pairs.get(t["pairId"])+ if existing:+ existing["createdAt"] = max(existing["createdAt"], t["createdAt"])+ existing["updatedAt"] = max(existing["updatedAt"], activity)+ if t.get("modifiedAt"):+ existing["modifiedAt"] = max(existing.get("modifiedAt") or 0, t["modifiedAt"])+ if t.get("modelId") and t["modelId"] not in existing.get("modelIds", []):+ existing["modelIds"] = [*existing.get("modelIds", []), t["modelId"]]+ existing["threadIds"].append(t["id"])+ continue+ item = {+ "type": "compare",+ "id": t["pairId"],+ "threadIds": [t["id"]],+ "title": t["title"],+ "createdAt": t["createdAt"],+ "updatedAt": activity,+ **({"modifiedAt": t["modifiedAt"]} if t.get("modifiedAt") else {}),+ **({"modelIds": [t["modelId"]]} if t.get("modelId") else {}),+ "projectId": t.get("projectId"),+ }+ pairs[t["pairId"]] = item+ items.append(item)+ else:+ items.append(+ {+ "type": "single",+ "id": t["id"],+ "threadIds": [t["id"]],+ "title": t["title"],+ "createdAt": t["createdAt"],+ "updatedAt": activity,+ **({"modifiedAt": t["modifiedAt"]} if t.get("modifiedAt") else {}),+ **({"modelIds": [t["modelId"]]} if t.get("modelId") else {}),+ "isFork": bool(t.get("forkedFromThreadId")),+ "projectId": t.get("projectId"),+ }+ )+ return sorted(items, key = lambda item: -item["updatedAt"])+++def _apply_manual_order(items: list[dict], order: list[str]) -> list[dict]:+ if not order:+ return items+ rank = {row_id: index for index, row_id in enumerate(order)}+ return sorted(items, key = lambda item: rank.get(item["id"], -1))+++def _page_all(client, body: dict) -> list[dict]:+ rows, cursor, pages = [], None, 0+ while True:+ response = client.post("/api/chat/sidebar/rows", json = {**body, "cursor": cursor})+ assert response.status_code == 200, response.text+ data = response.json()+ assert len(data["rows"]) <= body.get("limit", 20)+ rows += data["rows"]+ pages += 1+ assert data["hasMore"] == (data["nextCursor"] is not None)+ if not data["hasMore"]:+ return rows+ cursor = data["nextCursor"]+ assert pages < 1000++[email protected]("n", [0, 1, 19, 20, 21, 40, 41])+def test_page_edges(client, n):+ _insert_threads(_singles(n))+ first = client.post("/api/chat/sidebar/rows", json = {}).json()+ assert len(first["rows"]) == min(n, 20)+ assert first["total"] == n+ assert first["hasMore"] == (n > 20)+ rows = _page_all(client, {})+ assert [row["id"] for row in rows] == [f"t{i:04d}" for i in reversed(range(n))]+++def test_equal_timestamps_page_exactly_once(client):+ _insert_threads([{"id": f"t{i:03d}", "created_at": 5, "updated_at": 5} for i in range(45)])+ rows = _page_all(client, {"limit": 7})+ ids = [row["id"] for row in rows]+ assert len(ids) == len(set(ids)) == 45+ assert ids == [f"t{i:03d}" for i in range(45)]+++def test_full_traversal_matches_get_threads_and_group_threads(client):+ threads = []+ for i in range(70):+ base = {"created_at": 1_000 + i * 10, "updated_at": 1_000 + i * 10 + 3}+ if i % 7 == 0:+ threads.append(+ {+ **base,+ "id": f"p{i}a",+ "pair_id": f"pair{i}",+ "title": f"old {i}",+ "model_id": "m1",+ }+ )+ threads.append(+ {+ **base,+ "id": f"p{i}b",+ "pair_id": f"pair{i}",+ "title": f"new {i}",+ "model_id": "m2",+ "updated_at": base["updated_at"] + 5,+ "modified_at": 77 if i % 2 else None,+ "project_id": None,+ }+ )+ else:+ threads.append(+ {+ **base,+ "id": f"s{i}",+ "model_id": "m1" if i % 3 else "",+ "updated_at": None if i % 5 == 0 else base["updated_at"],+ "forked_from_thread_id": "s1" if i % 4 == 0 else None,+ "modified_at": 9 if i % 6 == 0 else None,+ "archived": 1 if i % 11 == 0 else 0,+ }+ )+ _insert_threads(threads)+ listed = client.get("/api/chat/threads?include_archived=true").json()["threads"]+ for archived in (False, True):+ expected = _group_threads(listed, archived = archived)+ rows = _page_all(client, {"archived": archived, "limit": 6})+ title_of = {t["id"]: t["title"] for t in listed}+ for row in rows:+ titles = row.pop("threadTitles", None)+ assert titles == (+ [title_of[t] for t in row["threadIds"]] if row["type"] == "compare" else None+ )+ assert rows == expected+++def test_pair_is_never_split_and_takes_newest_member_title_and_project(client, db):+ _insert_project("proj", created_at = 1, updated_at = 1)+ threads = _singles(19)+ threads += [+ {"id": "pa", "pair_id": "pair", "title": "A", "created_at": 50, "updated_at": 60},+ {+ "id": "pb",+ "pair_id": "pair",+ "title": "B",+ "project_id": "proj",+ "created_at": 40,+ "updated_at": 9_000,+ },+ ]+ _insert_threads(threads)+ first = client.post("/api/chat/sidebar/rows", json = {"limit": 1}).json()+ assert first["rows"][0]["id"] == "pair"+ assert first["rows"][0]["threadIds"] == ["pb", "pa"]+ assert first["rows"][0]["title"] == "B"+ assert first["rows"][0]["threadTitles"] == ["B", "A"]+ assert first["rows"][0]["projectId"] == "proj"+ assert first["rows"][0]["createdAt"] == 50+ assert first["rows"][0]["type"] == "compare"+ assert "isFork" not in first["rows"][0]+ assert first["total"] == 20+++def test_archived_null_reads_as_unarchived(client, db):+ _insert_threads(_singles(3))+ conn = studio_db.get_connection()+ try:+ # The column is NOT NULL today; an older table may not be, so rebuild it without the check.+ conn.execute("ALTER TABLE chat_threads RENAME TO chat_threads_old")+ conn.execute("CREATE TABLE chat_threads AS SELECT * FROM chat_threads_old WHERE 0")+ conn.execute("INSERT INTO chat_threads SELECT * FROM chat_threads_old")+ conn.execute("DROP TABLE chat_threads_old")+ conn.execute("UPDATE chat_threads SET archived = NULL WHERE id = 't0001'")+ conn.commit()+ finally:+ conn.close()+ ids = [row["id"] for row in _page_all(client, {})]+ assert ids == ["t0002", "t0001", "t0000"]+ assert _page_all(client, {"archived": True}) == []+++def test_include_exclude_project_and_unfiled(client, db):+ _insert_project("proj", created_at = 1, updated_at = 1)+ threads = _singles(30)+ for t in threads[:10]:+ t["project_id"] = "proj"+ _insert_threads(threads)+ pinned = ["t0003", "t0025", "missing"]+ included = _page_all(client, {"includeIds": pinned})+ assert [row["id"] for row in included] == ["t0025", "t0003"]+ assert _page_all(client, {"includeIds": []}) == []++ recents = _page_all(client, {"excludeIds": ["t0025", "t0029"], "unfiledOnly": True})+ assert [row["id"] for row in recents] == [+ f"t{i:04d}" for i in reversed(range(10, 30)) if i not in (25, 29)+ ]+ folder = client.post(+ "/api/chat/sidebar/rows",+ json = {"projectId": "proj", "excludeIds": ["t0003"], "limit": 4},+ ).json()+ assert [row["id"] for row in folder["rows"]] == ["t0009", "t0008", "t0007", "t0006"]+ assert folder["total"] == 9+ assert all(row["projectId"] == "proj" for row in folder["rows"])+++def test_include_ids_match_a_compare_pair_through_a_pane_thread(client):+ _insert_threads(+ [+ {"id": "pane-a", "pair_id": "pair", "created_at": 1},+ {"id": "pane-b", "pair_id": "pair", "created_at": 2},+ {"id": "solo", "created_at": 3},+ ]+ )+ rows = _page_all(client, {"includeIds": ["pane-b", "solo", "pair"]})+ assert [(row["id"], sorted(row["threadIds"])) for row in rows] == [+ ("solo", ["solo"]),+ ("pair", ["pane-a", "pane-b"]),+ ]+++def test_manual_sort_matches_apply_manual_order(client):+ _insert_threads(_singles(45))+ listed = client.get("/api/chat/threads").json()["threads"]+ order = ["t0002", "t0040", "missing", "t0010", "t0002", "t0033"]+ expected = _apply_manual_order(_group_threads(listed), order)+ rows = _page_all(client, {"sort": "manual", "manualOrder": order, "limit": 20})+ assert rows == expected+ assert [row["id"] for row in rows[-4:]] == ["t0040", "t0010", "t0002", "t0033"]+ assert _page_all(client, {"sort": "manual"}) == _group_threads(listed)+++ "cursor",+ [+ "!!!",+ "bm90IGpzb24", # "not json"+ studio_db.encode_sidebar_cursor({"s": "manual", "o": 3}), # another sort's cursor+ studio_db.encode_sidebar_cursor({"s": "updated", "u": "x", "i": "t"}),+ studio_db.encode_sidebar_cursor({"s": "updated", "u": 2**63, "c": 0, "r": 0}),+ studio_db.encode_sidebar_cursor({"s": "updated", "u": 0, "c": 0, "r": -(2**63)}),+ ],+)+def test_malformed_cursor_is_400(client, cursor):+ _insert_threads(_singles(3))+ response = client.post("/api/chat/sidebar/rows", json = {"cursor": cursor})+ assert response.status_code == 400+ with pytest.raises(HTTPException) as raised:+ chat_history.list_sidebar_rows(+ chat_history.ChatSidebarRowsRequest(cursor = cursor), current_subject = "t"+ )+ assert raised.value.status_code == 400+++def test_id_lists_past_ten_thousand_are_accepted(client):+ _insert_threads(_singles(30))+ order = [f"t{i:04d}" for i in reversed(range(30))] + [f"gone{i}" for i in range(20_000)]+ exclude = ["t0000"] + [f"gone{i}" for i in range(20_000)]+ response = client.post(+ "/api/chat/sidebar/rows",+ json = {"sort": "manual", "manualOrder": order, "excludeIds": exclude, "limit": 5},+ )+ assert response.status_code == 200+ page = response.json()+ assert [row["id"] for row in page["rows"]] == ["t0029", "t0028", "t0027", "t0026", "t0025"]+ assert page["total"] == 29++[email protected]("limit", [0, 201])+def test_limit_is_bounded(client, limit):+ assert client.post("/api/chat/sidebar/rows", json = {"limit": limit}).status_code == 422+++def test_projects_last_activity(client, db):+ _insert_project("a", created_at = 10, updated_at = 10, name = "beta")+ _insert_project("b", created_at = 20, updated_at = 500, name = "Alpha")+ _insert_project("c", created_at = 30, updated_at = 30, name = "gamma")+ _insert_threads(+ [+ {"id": "x", "project_id": "a", "created_at": 900, "updated_at": 1_000},+ {"id": "y", "project_id": "c", "created_at": 9_999, "archived": 1},+ {"id": "z", "project_id": "c", "created_at": 40, "updated_at": None},+ ]+ )+ data = client.get("/api/chat/projects").json()+ by_id = {p["id"]: p for p in data["projects"]}+ assert {k: v["lastActivityAt"] for k, v in by_id.items()} == {"a": 1_000, "b": 500, "c": 40}+ assert [p["id"] for p in data["projects"]] == ["b", "c", "a"]+++def test_projects_last_activity_follows_the_pair_grouping(client, db):+ _insert_project("old", created_at = 1, updated_at = 1)+ _insert_project("new", created_at = 1, updated_at = 1)+ _insert_threads(+ [+ {+ "id": "pa",+ "pair_id": "pair",+ "project_id": "old",+ "created_at": 5,+ "updated_at": 900,+ },+ {"id": "pb", "pair_id": "pair", "project_id": "new", "created_at": 5, "updated_at": 50},+ ]+ )+ by_id = {+ p["id"]: p["lastActivityAt"] for p in client.get("/api/chat/projects").json()["projects"]+ }+ rows = client.post("/api/chat/sidebar/rows", json = {"limit": 5}).json()["rows"]+ assert rows[0]["projectId"] == "old"+ assert by_id == {"old": 900, "new": 1}+++def test_thread_rows_lookup(client):+ _insert_threads(+ [+ {"id": "pa", "pair_id": "pair", "created_at": 1},+ {"id": "pb", "pair_id": "pair", "created_at": 2},+ {"id": "s", "created_at": 3, "archived": 1},+ ]+ )+ response = client.get(+ "/api/chat/sidebar/thread-rows?thread_ids=pa&thread_ids=pb&thread_ids=s&thread_ids=gone"+ )+ assert response.json() == {"rowIdByThreadId": {"pa": "pair", "pb": "pair", "s": "s"}}+ too_many = "&".join(f"thread_ids=t{i}" for i in range(501))+ assert client.get(f"/api/chat/sidebar/thread-rows?{too_many}").status_code == 400+++def test_sidebar_handlers_stay_sync():+ """Sync handlers run in the threadpool; the async-handler inventory test pins the rest."""+ assert not inspect.iscoroutinefunction(chat_history.list_sidebar_rows)+ assert not inspect.iscoroutinefunction(chat_history.sidebar_thread_rows)+++def test_include_ids_pane_follows_the_archive_filter(client):+ # one pane archived, one not: each side finds the pair only through its own pane+ _insert_threads(+ [+ {"id": "live", "pair_id": "mixed", "title": "L", "created_at": 1, "updated_at": 2},+ {+ "id": "gone",+ "pair_id": "mixed",+ "title": "G",+ "created_at": 1,+ "updated_at": 3,+ "archived": 1,+ },+ ]+ )++ def ids(archived, include):+ body = {"limit": 20, "archived": archived, "includeIds": include}+ return [+ row["id"] for row in client.post("/api/chat/sidebar/rows", json = body).json()["rows"]+ ]++ assert ids(False, ["gone"]) == []+ assert ids(True, ["live"]) == []+ assert ids(False, ["live"]) == ["mixed"]+ assert ids(True, ["gone"]) == ["mixed"]diff --git a/studio/backend/tests/test_logging_middleware.py b/studio/backend/tests/test_logging_middleware.pyindex 64f2370fe72..99879ee3d2c 100644--- a/studio/backend/tests/test_logging_middleware.py+++ b/studio/backend/tests/test_logging_middleware.py@@ -789,3 +789,16 @@ def test_verbose_restores_every_thread_poll_line(logs, monkeypatch):for _ in range(3):_run(mw(_http_scope("/api/chat/threads/abc"), _noop_receive, _drop))assert len(_paths_logged(logs)) == 3+++def test_sidebar_rows_post_read_is_quiet_but_errors_and_other_posts_log(logs):+ rows = "/api/chat/sidebar/rows"+ _run(LoggingMiddleware(_status_app(200))(_http_scope(rows, "POST"), _noop_receive, _drop))+ assert logs.events == []+ _run(LoggingMiddleware(_status_app(500))(_http_scope(rows, "POST"), _noop_receive, _drop))+ _run(+ LoggingMiddleware(_status_app(200))(+ _http_scope("/api/chat/threads", "POST"), _noop_receive, _drop+ )+ )+ assert _paths_logged(logs) == [rows, "/api/chat/threads"]diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.tsindex 3c74bab749e..996da55a5c8 100644--- a/studio/frontend/scripts/check-bundle-budget.ts+++ b/studio/frontend/scripts/check-bundle-budget.ts@@ -65,8 +65,10 @@ export const BUDGET = {// no single import doing it. The largest step is per-chat file history and /rewind (#13522, +4.3 KB// raw / +1.3 KB transfer in the eager chat chunk); the rest are 0.1 to 0.6 KB each. Raised by that// growth so main's margin stays where it was.- transferBytes: 2_015_400,- rawBytes: 6_710_300,+ // Sidebar server paging adds 17.4 KB raw / 5.6 KB transfer to the first screen (6,544.5 -> 6,561.9+ // KB raw, 1,967.1 -> 1,972.7 KB transfer against b73110c6f, one machine and build), raised by that.+ transferBytes: 2_021_100,+ rawBytes: 6_728_100,};// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering thediff --git a/studio/frontend/src/components/app-sidebar.tsx b/studio/frontend/src/components/app-sidebar.tsxindex f9b26bc5e74..8c92e1d143c 100644--- a/studio/frontend/src/components/app-sidebar.tsx+++ b/studio/frontend/src/components/app-sidebar.tsx@@ -48,6 +48,8 @@ import {} from "@/components/ui/dropdown-menu";import { NonModalDropdownMenu } from "@/components/ui/non-modal-dropdown-menu";import { ProgressiveRows } from "@/components/progressive-rows";+import { keepOpenedChats, touchOpenedChat } from "@/features/chat/utils/opened-chats";+import { isAssistantLocalThreadId } from "@/features/chat/utils/thread-ids";import {HELP_GROUPS,HELP_ITEMS,@@ -174,7 +176,18 @@ import {useChatRuntimeStore,useChatProjects,useChatSearchStore,- useChatSidebarItems,+ useSidebarRowPages,+ fetchSidebarRowPage,+ CHAT_HISTORY_UPDATED_EVENT,+ fetchSidebarThreadRows,+ readAllSidebarRowIds,+ readAllSidebarRows,+ type SidebarRowSlot,+ sidebarRowQuery,+ holdRowsPastLoadedChats,+ mergeManualOrderTail,+ settledManualOrder,+ SIDEBAR_PAGE_SIZE,usePinnedChatsStore,usePinnedProjectsStore,rangeBetween,@@ -359,7 +372,7 @@ const MoreVerticalCenteredIcon = MoreVerticalIcon.map(([tag, attrs]) => [const PROJECT_CHAT_LIMIT = 4;const SIDEBAR_PROJECT_LIMIT = 5;-const RECENTS_PAGE_SIZE = 50;+const RECENTS_PAGE_SIZE = SIDEBAR_PAGE_SIZE;// shared radio items tick on the right; these read as settings, so tick first.// sidebar and account menu padding precedes UI scaling; rows keep 2px margins and submenu gaps.@@ -405,6 +418,34 @@ const MOVE_TO_LIST =const MOVE_TO_MAX = 12;// Folder rows match their hover pill.const DROP_INTO_ROW_CUE = `${DROP_CUE_CLASS} before:pointer-events-none before:absolute before:inset-x-0 before:top-0 before:bottom-px before:rounded-full before:bg-primary/8 before:border-[1.5px] before:border-primary before:content-['']`;+/** Reports while it is mounted, so a folder row ProgressiveRows has not drawn reads nothing. */+function MountedFolder({+ id,+ onChange,+}: {+ id: string;+ onChange: (id: string, mounted: boolean) => void;+}) {+ useEffect(() => {+ onChange(id, true);+ return () => onChange(id, false);+ }, [id, onChange]);+ return null;+}++/** Chats per project, newest first. */+function groupChatsByProject(items: readonly SidebarItem[]): Map<string, SidebarItem[]> {+ const map = new Map<string, SidebarItem[]>();+ for (const item of items) {+ if (!item.projectId) continue;+ const list = map.get(item.projectId);+ if (list) list.push(item);+ else map.set(item.projectId, [item]);+ }+ for (const list of map.values()) list.sort((a, b) => b.updatedAt - a.updatedAt);+ return map;+}+// The menu keeps a 1px gap between rows. A pointer resting on that gap would hit the section// instead, which answers with its last slot, so each row's box reaches over the gap below it.const DROP_ROW_HIT = "pb-px -mb-px";@@ -1349,7 +1390,11 @@ export function AppSidebar() {// Training runs surface as sidebar "Recents" on Train/Recipes/Export, else chat recents.// Read up here because the chat lists below only exist when this is off.const trainingRecentsRoute = isStudioRoute || isRecipesRoute || isExportRoute;- const { items: runItems } = useTrainingHistorySidebarItems(+ const {+ items: runItems,+ hasMore: runsHaveMore,+ loadMore: loadMoreRuns,+ } = useTrainingHistorySidebarItems(!chatOnly && trainingRecentsRoute,);const showTrainingRecents =@@ -1360,14 +1405,6 @@ export function AppSidebar() {const activeProjectId = isChatRoute? ((search.project as string | undefined) ?? null): null;- const {- items: allChatItems,- archivedItems: archivedChatItems,- loaded: chatItemsLoaded,- } = useChatSidebarItems({- enabled: !isStudioRoute,- requireMessages: false,- });const pinnedIds = usePinnedChatsStore((s) => s.pinnedIds);const togglePinnedChat = usePinnedChatsStore((s) => s.togglePin);const setPinnedChats = usePinnedChatsStore((s) => s.setPinned);@@ -1379,16 +1416,6 @@ export function AppSidebar() {(s) => s.alwaysDeleteChatFiles,);const pinnedIdSet = useMemo(() => new Set(pinnedIds), [pinnedIds]);- // Which row every thread belongs to, listed or not. The published lists carry- // only what is on screen, so this is what keeps an unread Compare row behind- // a collapsed section counting as one chat.- const rowIdByThreadId = useMemo(() => {- const map: Record<string, string> = {};- for (const item of allChatItems) {- for (const threadId of getSidebarItemThreadIds(item)) map[threadId] = item.id;- }- return map;- }, [allChatItems]);const organizeBy = useSidebarOrganizationStore((s) => s.organizeBy);// User-made sections, what is filed in them, and which sections "Show" turned off.const customSections = useSidebarOrganizationStore((s) => s.customSections);@@ -1447,6 +1474,14 @@ export function AppSidebar() {const chatSort = useSidebarOrganizationStore((s) => s.chatSort);const pinnedSort = useSidebarOrganizationStore((s) => s.pinnedSort);const manualOrder = useSidebarOrganizationStore((s) => s.manualOrder);+ const [pendingManualOrder, setPendingManualOrder] = useState<Record<string, string[]>>({});+ const displayManualOrder = useMemo(+ () =>+ Object.keys(pendingManualOrder).length > 0+ ? { ...manualOrder, ...pendingManualOrder }+ : manualOrder,+ [manualOrder, pendingManualOrder],+ );const setOrganizeBy = useSidebarOrganizationStore((s) => s.setOrganizeBy);const setChatSort = useSidebarOrganizationStore((s) => s.setChatSort);const setPinnedSort = useSidebarOrganizationStore((s) => s.setPinnedSort);@@ -1466,18 +1501,198 @@ export function AppSidebar() {const setProjectsSection = useSidebarOrganizationStore((s) => s.setProjectsSection);const setPagesSection = useSidebarOrganizationStore((s) => s.setPagesSection);const setSectionHidden = useSidebarOrganizationStore((s) => s.setSectionHidden);+ // `allChatItems` is only what the paged lists have loaded, not the whole history.+ const sectionFiledChatIds = useMemo(() => Object.keys(sectionByChatId), [sectionByChatId]);+ const [openFolderIds, setOpenFolderIds] = useState<readonly string[]>([]);+ // the last 20 opened chats and every running one load their rows, so row lookups never miss them+ const slotStoreThreadId = useChatRuntimeStore((s) => s.activeThreadId);+ const runningThreadKey = useChatRuntimeStore((s) =>+ Object.keys(s.runningByThreadId)+ .filter((id) => s.runningByThreadId[id])+ .sort()+ .join("\n"),+ );+ const slotActiveId = isChatRoute+ ? ((search.thread as string | undefined) ??+ (search.compare as string | undefined) ??+ slotStoreThreadId ??+ undefined)+ : undefined;+ const [openedChatIds, setOpenedChatIds] = useState<readonly string[]>([]);+ const chatRowSlots = useMemo(() => {+ const excludeIds = [...pinnedIds, ...sectionFiledChatIds];+ const lookupIds = [+ ...new Set(+ [slotActiveId, ...openedChatIds, ...runningThreadKey.split("\n")].filter(+ (id): id is string => Boolean(id) && !isAssistantLocalThreadId(id),+ ),+ ),+ ].sort();+ const slots: Record<string, SidebarRowSlot | null> = {+ recents: {+ query: sidebarRowQuery({+ unfiledOnly: !showsInRecents("project", organizeBy),+ excludeIds,+ sort: chatSort,+ manualOrder: manualOrder[RECENTS_ORDER_SCOPE],+ }),+ },+ pinned: pinnedIds.length+ ? {+ query: sidebarRowQuery({+ includeIds: pinnedIds,+ sort: pinnedSort,+ manualOrder:+ pinnedSort === "manual"+ ? applyManualOrder(pinnedIds, manualOrder[PINNED_ORDER_SCOPE], (id) => id)+ : null,+ }),+ }+ : null,+ active: lookupIds.length+ ? {+ query: sidebarRowQuery({ includeIds: lookupIds }),+ initialLimit: Number.MAX_SAFE_INTEGER,+ lookupOnly: true,+ }+ : null,+ };+ for (const section of customSections) {+ const members = sectionFiledChatIds.filter((id) => sectionByChatId[id] === section.id);+ const ids = members.filter((id) => !pinnedIdSet.has(id));+ // the newest member alone, pinned ones included as main counted them, so a section keeps its+ // place in Move to when that chat is not among its drawn rows (hidden, hand-ordered, pinned)+ if (+ members.length > 0 &&+ (hiddenSectionSet.has(section.id) ||+ section.sort === "manual" ||+ ids.length < members.length)+ ) {+ slots[`latest:${section.id}`] = {+ initialLimit: 1,+ query: sidebarRowQuery({ includeIds: members }),+ lookupOnly: true,+ };+ }+ if (ids.length === 0 || hiddenSectionSet.has(section.id)) continue;+ slots[`section:${section.id}`] = {+ query: sidebarRowQuery({+ includeIds: ids,+ sort: section.sort,+ manualOrder: manualOrder[customSectionScope(section.id)],+ }),+ };+ }+ for (const projectId of openFolderIds) {+ slots[`folder:${projectId}`] = {+ initialLimit: PROJECT_CHAT_LIMIT + 1,+ query: sidebarRowQuery({+ projectId,+ excludeIds,+ sort: chatSort,+ manualOrder: manualOrder[projectOrderScope(projectId)],+ }),+ };+ }+ return slots;+ }, [+ pinnedIds,+ pinnedIdSet,+ sectionFiledChatIds,+ sectionByChatId,+ organizeBy,+ chatSort,+ pinnedSort,+ manualOrder,+ customSections,+ hiddenSectionSet,+ openFolderIds,+ slotActiveId,+ openedChatIds,+ runningThreadKey,+ ]);+ const {+ lists: chatRowLists,+ items: allChatItems,+ listedItems: listedChatItems,+ } = useSidebarRowPages(+ chatRowSlots,+ !isStudioRoute,+ );+ const runningRowKey = useMemo(() => {+ if (!runningThreadKey) return "";+ const running = new Set(runningThreadKey.split("\n"));+ const rows = new Set(running);+ for (const item of allChatItems) {+ if (item.threadIds?.some((id) => running.has(id))) rows.add(item.id);+ }+ return [...rows].sort().join("\n");+ }, [runningThreadKey, allChatItems]);+ useEffect(() => {+ if (!slotActiveId || isAssistantLocalThreadId(slotActiveId)) return;+ const running = new Set(runningRowKey.split("\n"));+ setOpenedChatIds((opened) => keepOpenedChats(touchOpenedChat(opened, slotActiveId), running));+ }, [slotActiveId, runningRowKey]);+ const recentsPages = chatRowLists.recents;+ // a failed first read is not an empty history: "No chats yet" waits for a read that worked+ const chatItemsLoaded = Boolean(recentsPages?.loaded && !recentsPages.error);+ const folderPages = useCallback(+ (projectId: string) => chatRowLists[`folder:${projectId}`],+ [chatRowLists],+ );+ // "No chats yet" also needs no archived chat and none in an unloaded folder+ // null until a probe answers: main decided from one full read, so it never showed it early+ const [hasUnlistedChats, setHasUnlistedChats] = useState<boolean | null>(null);+ const sidebarLooksEmpty = chatItemsLoaded && allChatItems.length === 0;+ useEffect(() => {+ if (!sidebarLooksEmpty) {+ setHasUnlistedChats(null);+ return;+ }+ let cancelled = false;+ let latest = 0;+ const probe = () => {+ const mine = ++latest;+ void Promise.all(+ [sidebarRowQuery({}), sidebarRowQuery({ archived: true })].map((query) =>+ fetchSidebarRowPage(query, 1, null),+ ),+ )+ .then((pages) => {+ if (!cancelled && mine === latest) {+ setHasUnlistedChats(pages.some((page) => page.rows.length > 0));+ }+ })+ .catch(() => {+ if (!cancelled && mine === latest) setHasUnlistedChats(null);+ });+ };+ probe();+ window.addEventListener(CHAT_HISTORY_UPDATED_EVENT, probe);+ return () => {+ cancelled = true;+ window.removeEventListener(CHAT_HISTORY_UPDATED_EVENT, probe);+ };+ }, [sidebarLooksEmpty]);+ const rowIdByThreadId = useMemo(() => {+ const map: Record<string, string> = {};+ for (const item of allChatItems) {+ for (const threadId of getSidebarItemThreadIds(item)) map[threadId] = item.id;+ }+ return map;+ }, [allChatItems]);// With the Projects section on, a project chat lives in its folder and repeating it here would be// noise. With it off there are no folders, so Recents is where those chats go. Pinned chats are// held back either way: the Pinned section renders those, as a custom section does its own.const recentChatItems = useMemo(() =>- allChatItems.filter(+ listedChatItems.filter((item) =>!pinnedIdSet.has(item.id) &&!sectionByChatId[item.id] &&showsInRecents(item.projectId, organizeBy),),- [allChatItems, pinnedIdSet, sectionByChatId, organizeBy],+ [listedChatItems, pinnedIdSet, sectionByChatId, organizeBy],);const [pinnedOpen, setPinnedOpen] = useState(true);const pinnedPages = usePinnedPages();@@ -1492,31 +1707,27 @@ export function AppSidebar() {);// Pinned chats, in pin order. A pinned project chat shows here only.const pinnedChatItems = useMemo(() => {- const byId = new Map(allChatItems.map((item) => [item.id, item]));+ const byId = new Map(listedChatItems.map((item) => [item.id, item]));return pinnedIds.map((id) => byId.get(id)).filter((item): item is SidebarItem => Boolean(item));- }, [allChatItems, pinnedIds]);+ }, [listedChatItems, pinnedIds]);// Chats per project, newest first. Pinned ones included, as they still count as activity.- const chatsByProjectId = useMemo(() => {- const map = new Map<string, SidebarItem[]>();- for (const item of allChatItems) {- if (!item.projectId) continue;- const list = map.get(item.projectId);- if (list) list.push(item);- else map.set(item.projectId, [item]);- }- for (const list of map.values())- list.sort((a, b) => b.updatedAt - a.updatedAt);- return map;- }, [allChatItems]);+ const chatsByProjectId = useMemo(() => groupChatsByProject(allChatItems), [allChatItems]);+ const listedChatsByProjectId = useMemo(+ () => groupChatsByProject(listedChatItems),+ [listedChatItems],+ );// A project's last activity: its own edits or its newest chat. Its updatedAt only moves when it// is edited.const projectActivityAt = useMemo(() => {const map = new Map<string, number>();for (const project of projects) {const newest = chatsByProjectId.get(project.id)?.[0]?.updatedAt ?? 0;- map.set(project.id, Math.max(project.updatedAt ?? project.createdAt, newest));+ map.set(+ project.id,+ Math.max(project.lastActivityAt ?? 0, project.updatedAt ?? project.createdAt, newest),+ );}return map;}, [projects, chatsByProjectId]);@@ -1589,13 +1800,20 @@ export function AppSidebar() {// Memoised for its identity, not for the slice. It feeds the rendered-row set the selection guard// depends on, and that effect sets state: React re-renders once to find the bail-out, which would// rebuild this array and schedule the effect again, without end.+ const [projectPageLimit, setProjectPageLimit] = useState(SIDEBAR_PAGE_SIZE);const visibleProjectRecords = useMemo(() =>- showAllProjects- ? sidebarProjectRecords- : sidebarProjectRecords.slice(0, SIDEBAR_PROJECT_LIMIT),- [showAllProjects, sidebarProjectRecords],+ sidebarProjectRecords.slice(+ 0,+ showAllProjects ? projectPageLimit : SIDEBAR_PROJECT_LIMIT,+ ),+ [showAllProjects, projectPageLimit, sidebarProjectRecords],);+ const moreProjectsUnlisted =+ showAllProjects && sidebarProjectRecords.length > visibleProjectRecords.length;+ const listMoreProjects = useCallback(() => {+ setProjectPageLimit((count) => count + SIDEBAR_PAGE_SIZE);+ }, []);// Default expanded; the row toggles this. Show-more reveals chats past the limit.const [collapsedProjectIds, setCollapsedProjectIds] = useState<Set<string>>(() => new Set(),@@ -1717,6 +1935,7 @@ export function AppSidebar() {const queueByThreadId = usePromptQueueUI((s) => s.byThreadId);// In the navigation store, not local state: the unread chords register outside this tree.const unreadThreadIds = useChatNavigationStore((s) => s.unreadThreadIds);+ const unreadRowIds = useChatNavigationStore((s) => s.unreadRowIds);const markThreadsUnread = useChatNavigationStore((s) => s.markThreadsUnread);const clearThreadsUnread = useChatNavigationStore((s) => s.clearThreadsUnread,@@ -1753,11 +1972,11 @@ export function AppSidebar() {if (mode === "manual") {// Incoming order is the list's own rule, so undragged rows keep it:// newest first in Recents, pin order in Pinned.- return applyManualOrder(items, manualOrder[scope], (item) => item.id);+ return applyManualOrder(items, displayManualOrder[scope], (item) => item.id);}return [...items].sort((a, b) => b.updatedAt - a.updatedAt);},- [manualOrder],+ [displayManualOrder],);const sortedRecentChatItems = useMemo(() => sortChatItems(recentChatItems, RECENTS_ORDER_SCOPE, chatSort),@@ -1794,7 +2013,7 @@ export function AppSidebar() {const sortedChatsByProjectId = useMemo(() => {const map = new Map<string, SidebarItem[]>();- for (const [projectId, items] of chatsByProjectId) {+ for (const [projectId, items] of listedChatsByProjectId) {map.set(projectId,sortChatItems(@@ -1808,7 +2027,7 @@ export function AppSidebar() {);}return map;- }, [chatsByProjectId, sortChatItems, chatSort, pinnedIdSet, sectionByChatId]);+ }, [listedChatsByProjectId, sortChatItems, chatSort, pinnedIdSet, sectionByChatId]);// One id array per list, shared by every row in it. Built per row, these// would be N arrays of length N on each render.const recentRowIds = useMemo(@@ -1824,6 +2043,15 @@ export function AppSidebar() {// until a drop says otherwise; a chat sort other than Manual keeps the chats after them.// Pinning is its own grouping: a pinned folder stays here whichever way the sidebar organizes,// and "In one list" only takes the Projects section away.+ const pinnedHasMore = chatRowLists.pinned?.hasMore;+ const sectionHasMoreKey = customSections+ .filter((section) => chatRowLists[`section:${section.id}`]?.hasMore)+ .map((section) => section.id)+ .join("\n");+ const sectionHasMore = useMemo(+ () => new Set(sectionHasMoreKey ? sectionHasMoreKey.split("\n") : []),+ [sectionHasMoreKey],+ );const pinnedRows = useMemo(() => {const rows: PinnedRow[] = [];for (const project of pinnedProjectBase) {@@ -1836,10 +2064,22 @@ export function AppSidebar() {for (const page of pinnedPages) {if (!sectionByPageId[page.id]) rows.push({ kind: "page", id: page.id, page });}- return pinnedSort === "manual"- ? applyManualOrder(rows, manualOrder[PINNED_ORDER_SCOPE], (row) => row.id)- : rows;- }, [pinnedProjectBase, sortedPinnedChatItems, pinnedPages, sectionByPageId, pinnedSort, manualOrder]);+ if (pinnedSort !== "manual") return rows;+ const order = displayManualOrder[PINNED_ORDER_SCOPE];+ return holdRowsPastLoadedChats(+ applyManualOrder(rows, order, (row) => row.id),+ order,+ Boolean(pinnedHasMore),+ );+ }, [+ pinnedProjectBase,+ sortedPinnedChatItems,+ pinnedPages,+ sectionByPageId,+ pinnedSort,+ displayManualOrder,+ pinnedHasMore,+ ]);const pinnedRowIds = useMemo(() => pinnedRows.map((row) => row.id), [pinnedRows]);// Each custom section is one list of folders and chats, as Pinned is: folders lead until a drop// says otherwise. A pinned row stays in Pinned and keeps its section for when it is unpinned.@@ -1857,7 +2097,7 @@ export function AppSidebar() {if (!sectionId || pinnedProjectIdSet.has(project.id)) continue;folders.get(sectionId)?.push({ kind: "project", id: project.id, project });}- for (const item of allChatItems) {+ for (const item of listedChatItems) {const sectionId = sectionByChatId[item.id];if (!sectionId || pinnedIdSet.has(item.id)) continue;chats.get(sectionId)?.push(item);@@ -1876,18 +2116,24 @@ export function AppSidebar() {),...(pages.get(section.id) ?? []),];+ const order = displayManualOrder[scope];rows.set(section.id,section.sort === "manual"- ? applyManualOrder(list, manualOrder[scope], (row) => row.id)+ ? holdRowsPastLoadedChats(+ applyManualOrder(list, order, (row) => row.id),+ order,+ sectionHasMore.has(section.id),+ ): list,);}return rows;}, [+ sectionHasMore,customSections,projects,- allChatItems,+ listedChatItems,sectionByProjectId,sectionByChatId,pinnedPages,@@ -1895,7 +2141,7 @@ export function AppSidebar() {pinnedProjectIdSet,pinnedIdSet,sortChatItems,- manualOrder,+ displayManualOrder,]);const visibleCustomSections = useMemo(() => customSections.filter((section) => !hiddenSectionSet.has(section.id)),@@ -1958,6 +2204,51 @@ export function AppSidebar() {// unread would take it over the open chat. Navigation is exempt: it moves the chat the user IS// looking at, and the sheet is closed for most of its life on a narrow window.const chatRowsOnScreen = chatListsOnScreen && (!isMobile || openMobile);+ const [mountedFolderIds, setMountedFolderIds] = useState<ReadonlySet<string>>(() => new Set());+ const folderMountCountsRef = useRef(new Map<string, number>());+ const setFolderMounted = useCallback((id: string, mounted: boolean) => {+ const counts = folderMountCountsRef.current;+ const count = (counts.get(id) ?? 0) + (mounted ? 1 : -1);+ if (count > 0) counts.set(id, count);+ else counts.delete(id);+ setMountedFolderIds((current) => {+ if (current.has(id) === count > 0) return current;+ const next = new Set(current);+ if (count > 0) next.add(id);+ else next.delete(id);+ return next;+ });+ }, []);+ const wantedFolderIds = useMemo(() => {+ if (!chatListsOnScreen || organizeBy !== "project") return "";+ const ids = new Set<string>();+ const add = (open: boolean, records: ProjectRecord[]) => {+ if (!open) return;+ for (const project of records) {+ if (!collapsedProjectIds.has(project.id) && mountedFolderIds.has(project.id)) {+ ids.add(project.id);+ }+ }+ };+ add(projectsSectionRendered && projectsOpen, visibleProjectRecords);+ add(pinnedOpen, pinnedProjectRecords);+ add(true, customSectionProjectRecords);+ return [...ids].sort().join("\n");+ }, [+ chatListsOnScreen,+ organizeBy,+ collapsedProjectIds,+ projectsSectionRendered,+ projectsOpen,+ visibleProjectRecords,+ pinnedOpen,+ pinnedProjectRecords,+ customSectionProjectRecords,+ mountedFolderIds,+ ]);+ useEffect(() => {+ setOpenFolderIds(wantedFolderIds ? wantedFolderIds.split("\n") : []);+ }, [wantedFolderIds]);// The chats a section's folders show, in row order. A pinned folder leaves with Pinned's// disclosure, not the Projects one, so each section collects its own.const folderChatItems = useCallback(@@ -2122,6 +2413,20 @@ export function AppSidebar() {attentionItemIds,activeThreadId,]);+ const chordTargetRef = useRef<string | null>(null);+ const lastRecentId = visibleRecentItems[visibleRecentItems.length - 1]?.id;+ const loadMoreRecents = recentsPages?.hasMore ? recentsPages.loadMore : undefined;+ useEffect(() => {+ if (!activeThreadId || chordTargetRef.current !== activeThreadId) return;+ if (activeThreadId === lastRecentId) void loadMoreRecents?.();+ }, [activeThreadId, lastRecentId, loadMoreRecents]);+ useEffect(() => {+ if (!activeThreadId || chordTargetRef.current !== activeThreadId) return;+ for (const [name, list] of Object.entries(chatRowLists)) {+ if (name === "recents" || name.startsWith("latest:") || !list.hasMore) continue;+ if (list.rows[list.rows.length - 1]?.id === activeThreadId) void list.loadMore();+ }+ }, [activeThreadId, chatRowLists]);const projectRowIds = useMemo(() => sidebarProjectRecords.map((project) => project.id),@@ -2222,8 +2527,35 @@ export function AppSidebar() {});}, [chatRowsOnScreen, clearSelection, renderedChatIds, renderedProjectIds]);- /** Select every chat row on screen, pinned block included. */+ const [selectAllPending, setSelectAllPending] = useState(false);const selectAllChats = useCallback(() => {+ if (!chatRowsOnScreen) return;+ const partial = Object.entries(chatRowLists).filter(([name, list]) => {+ if (!list.hasMore) return false;+ if (name === "recents") return chatOpen;+ if (name === "pinned") return pinnedOpen;+ const sectionId = customSectionIdOf(name);+ if (sectionId) return !collapsedSectionIds.has(sectionId);+ return name.startsWith("folder:") && expandedChatProjectIds.has(name.slice(7));+ });+ if (partial.length > 0) {+ // a failed page selects nothing: a bulk action must never take a silent subset for "all"+ void Promise.all(partial.map(([, list]) => list.loadAll()))+ .then(() => setSelectAllPending(true))+ .catch(() => undefined);+ return;+ }+ setSelectAllPending(true);+ }, [+ chatRowsOnScreen,+ chatRowLists,+ chatOpen,+ pinnedOpen,+ collapsedSectionIds,+ expandedChatProjectIds,+ ]);+ /** Select every chat row on screen, pinned block included. */+ const selectRenderedChats = useCallback(() => {if (!chatRowsOnScreen) return;const ids = renderedChatItems.map((item) => item.id);if (ids.length === 0) return;@@ -2232,6 +2564,67 @@ export function AppSidebar() {selectionAnchorRef.current = null;setSelectedChatIds(new Set(ids));}, [chatRowsOnScreen, renderedChatItems, dropProjectSelection]);+ useEffect(() => {+ if (!selectAllPending) return;+ setSelectAllPending(false);+ selectRenderedChats();+ }, [selectAllPending, selectRenderedChats]);++ const manualOrderWritesRef = useRef(new Map<string, number>());+ const writeManualOrder = useCallback(+ (scope: string, ids: string[]) => {+ const writes = manualOrderWritesRef.current;+ const generation = (writes.get(scope) ?? 0) + 1;+ writes.set(scope, generation);+ const slot =+ scope === RECENTS_ORDER_SCOPE+ ? "recents"+ : scope === PINNED_ORDER_SCOPE+ ? "pinned"+ : customSectionIdOf(scope)+ ? scope+ : scope.startsWith("project:")+ ? `folder:${scope.slice("project:".length)}`+ : null;+ const query = slot ? chatRowSlots[slot]?.query : undefined;+ if (!slot || !query || !chatRowLists[slot]?.hasMore) {+ setManualOrder(scope, ids);+ setPendingManualOrder(({ [scope]: _superseded, ...rest }) => rest);+ return;+ }+ const saved = useSidebarOrganizationStore.getState().manualOrder[scope] ?? [];+ const shown = mergeManualOrderTail(ids, saved);+ setPendingManualOrder((pending) => ({ ...pending, [scope]: shown }));+ const settle = (order: string[]) => {+ if (writes.get(scope) !== generation) return;+ setManualOrder(scope, order);+ setPendingManualOrder(({ [scope]: _settled, ...rest }) => rest);+ };+ // a partial order would rank every unloaded row ahead of the ones just arranged, so it is never+ // saved: a read that keeps failing leaves the saved order as it was+ const readAll = async () => {+ for (let attempt = 0; ; attempt++) {+ try {+ return await readAllSidebarRowIds(query);+ } catch (error) {+ if (attempt >= 2) throw error;+ await new Promise((resolve) => setTimeout(resolve, 1000 * (attempt + 1)));+ }+ }+ };+ void readAll()+ .then((all) => {+ const current = useSidebarOrganizationStore.getState().manualOrder[scope] ?? [];+ settle(settledManualOrder(ids, all, current));+ })+ .catch(() => {+ if (writes.get(scope) !== generation) return;+ setPendingManualOrder(({ [scope]: _dropped, ...rest }) => rest);+ toast.error("Couldn't save the new order");+ });+ },+ [setManualOrder, chatRowSlots, chatRowLists],+ );// Escape leaves a selection, as it does the menus. A passive listener rather than one that consumes// the key: dictation's Escape reads defaultPrevented first, and a stale selection must not outrank@@ -2545,7 +2938,7 @@ export function AppSidebar() {superseded?: boolean,) => {for (const order of effects.orders) {- setManualOrder(+ writeManualOrder(order.scope,before ? landedOrder(order, before[order.scope]) : order.ids,);@@ -2648,7 +3041,7 @@ export function AppSidebar() {const next = moveIdBy(orderedIds, item.id, delta);if (next === orderedIds) return;const resorts = sort !== undefined && sort.value !== "manual";- setManualOrder(item.scope, next);+ writeManualOrder(item.scope, next);if (resorts) {sort.set("manual");toast.info(t("shell.organize.switchedToManual"));@@ -2702,6 +3095,18 @@ export function AppSidebar() {markThreadsUnread(completedThreadIds, rowIdByThreadId);clearThreadsUnread([...activeVisibleThreadIdSet]);});+ const unlisted = completedThreadIds.filter((id) => !rowIdByThreadId[id]);+ if (unlisted.length > 0) {+ void fetchSidebarThreadRows(unlisted)+ .then((rows) => {+ if (!rows || Object.keys(rows).length === 0) return;+ // a chat opened while the lookup was out is read; do not mark it again+ const { unreadThreadIds: unread } = useChatNavigationStore.getState();+ const still = unlisted.filter((id) => unread.has(id));+ if (still.length > 0) markThreadsUnread(still, rows);+ })+ .catch(() => undefined);+ }}previousRunningByThreadIdRef.current = runningByThreadId;}, [@@ -3503,8 +3908,11 @@ export function AppSidebar() {}fn(activeChatItem);};- const goToChat = (pick: (state: ChatNavigationState) => SidebarItem | null) =>- openChatItemById(pick(useChatNavigationStore.getState()));+ const goToChat = (pick: (state: ChatNavigationState) => SidebarItem | null) => {+ const item = pick(useChatNavigationStore.getState());+ chordTargetRef.current = item?.id ?? null;+ openChatItemById(item);+ };// With rows selected these act on the selection, matching the context menu; otherwise on the open// chat. Acting on a selection clears it, so without this latch a second press would land on the@@ -3907,7 +4315,19 @@ export function AppSidebar() {const chats = (customSectionRows.get(section.id) ?? []).flatMap((row) =>row.kind === "chat" ? [row.item] : [],);- const threadIds = chats.flatMap(getSidebarItemThreadIds);+ // the whole section, not its loaded pages: unread by membership, archive after a full read+ const inSection = (rowId: string) =>+ sectionByChatId[rowId] === section.id && !pinnedIdSet.has(rowId);+ const unreadInSection = [...unreadThreadIds].filter((threadId) =>+ inSection(unreadRowIds[threadId] ?? threadId),+ );+ const sectionPages = chatRowLists[`section:${section.id}`];+ const sectionQuery = chatRowSlots[`section:${section.id}`]?.query;+ const archiveSection = async () => {+ const all =+ sectionPages?.hasMore && sectionQuery ? await readAllSidebarRows(sectionQuery) : chats;+ await archiveChatItems(all);+ };body = (<><DropdownMenuItem onSelect={() => setSectionDialog({ mode: "rename", section })}>@@ -3916,13 +4336,16 @@ export function AppSidebar() {</DropdownMenuItem><DropdownMenuSeparator /><DropdownMenuItem- disabled={!threadIds.some((id) => unreadThreadIds.has(id))}- onSelect={() => clearThreadsUnread(threadIds)}+ disabled={unreadInSection.length === 0}+ onSelect={() => clearThreadsUnread(unreadInSection)}><HugeiconsIcon icon={Tick02Icon} strokeWidth={1.75} className="size-icon" /><span>{t("shell.sections.markAllRead")}</span></DropdownMenuItem>- <DropdownMenuItem disabled={chats.length === 0} onSelect={() => void archiveChatItems(chats)}>+ <DropdownMenuItem+ disabled={chats.length === 0}+ onSelect={() => void archiveSection().catch(() => undefined)}+ ><HugeiconsIcon icon={Archive03Icon} strokeWidth={1.75} className="size-icon" /><span>{t("shell.selection.archiveChats")}</span></DropdownMenuItem>@@ -4039,7 +4462,13 @@ export function AppSidebar() {{...dnd.dropZoneProps({ section: "pinned" })}><SidebarMenu>- {pinnedRows.map((row) =>+ <ProgressiveRows+ items={pinnedRows}+ pageSize={SIDEBAR_PAGE_SIZE}+ mountAll+ hasMore={chatRowLists.pinned?.hasMore}+ onReachEnd={chatRowLists.pinned?.loadMore}+ renderItem={(row) =>row.kind === "project"? renderProjectFolderRow(row.project, {scope: PINNED_ORDER_SCOPE,@@ -4061,8 +4490,9 @@ export function AppSidebar() {orderIds: pinnedRowIds,section: "pinned",sort: { value: pinnedSort, set: setPinnedSort },- }),- )}+ })+ }+ />{pinnedRows.length === 0 ? (// Every pinned page is filed elsewhere: somewhere to drag them back to.<SidebarMenuItem>@@ -4179,7 +4609,13 @@ export function AppSidebar() {{...dnd.dropZoneProps({ section: scope })}><SidebarMenu>- {rows.map((row) =>+ <ProgressiveRows+ items={rows}+ pageSize={SIDEBAR_PAGE_SIZE}+ mountAll+ hasMore={chatRowLists[`section:${section.id}`]?.hasMore}+ onReachEnd={chatRowLists[`section:${section.id}`]?.loadMore}+ renderItem={(row) =>row.kind === "project"? renderProjectFolderRow(row.project, {scope,@@ -4196,8 +4632,9 @@ export function AppSidebar() {orderIds: ids.rows,section: scope,sort,- }),- )}+ })+ }+ />{rows.length === 0 ? (// Gives an empty section a body to hit, as the empty Projects line does.<SidebarMenuItem>@@ -4295,14 +4732,20 @@ export function AppSidebar() {{...dnd.dropZoneProps({ section: "projects" })}><SidebarMenu>- {visibleProjectRecords.map((project) =>- renderProjectFolderRow(project, {- scope: PROJECT_ORDER_SCOPE,- orderedIds: projectRowIds,- section: "projects",- sort: { value: projectSort, set: setProjectSort },- }),- )}+ <ProgressiveRows+ items={visibleProjectRecords}+ pageSize={SIDEBAR_PAGE_SIZE}+ hasMore={moreProjectsUnlisted}+ onReachEnd={listMoreProjects}+ renderItem={(project) =>+ renderProjectFolderRow(project, {+ scope: PROJECT_ORDER_SCOPE,+ orderedIds: projectRowIds,+ section: "projects",+ sort: { value: projectSort, set: setProjectSort },+ })+ }+ />{/* Every project is pinned, so the section is drawn with nothing in it. Theline gives the body a height: without one it collapses to zero and thehit test walks straight past it, so a pinned folder dragged back has@@ -4324,7 +4767,10 @@ export function AppSidebar() {{sidebarProjectRecords.length > SIDEBAR_PROJECT_LIMIT && (<SidebarMenuItem><SidebarMenuButton- onClick={() => setShowAllProjects((prev) => !prev)}+ onClick={() => {+ setShowAllProjects((prev) => !prev);+ setProjectPageLimit(SIDEBAR_PAGE_SIZE);+ }}className="sidebar-nav-btn h-[calc(30px*var(--ui-space-scale,1))] rounded-full pl-3 pr-4 font-medium text-nav-fg-muted!"><span className="text-ui-13 leading-ui-18 tracking-nav">@@ -5154,17 +5600,25 @@ export function AppSidebar() {</ContextMenuTrigger>{renderProjectContextMenu(project)}</ContextMenu>- {expanded &&- visibleChats.map((chat) =>- renderChatSidebarItem(chat, "project", {- scope: projectOrderScope(project.id),- ids: projectChatIds,- section: order.section,- folderId: project.id,- blockEnd,- sort: { value: chatSort, set: setChatSort },- }),- )}+ <MountedFolder id={project.id} onChange={setFolderMounted} />+ {expanded && (+ <ProgressiveRows+ items={visibleChats}+ pageSize={SIDEBAR_PAGE_SIZE}+ hasMore={showAll && folderPages(project.id)?.hasMore}+ onReachEnd={folderPages(project.id)?.loadMore}+ renderItem={(chat) =>+ renderChatSidebarItem(chat, "project", {+ scope: projectOrderScope(project.id),+ ids: projectChatIds,+ section: order.section,+ folderId: project.id,+ blockEnd,+ sort: { value: chatSort, set: setChatSort },+ })+ }+ />+ )}{/* An open folder with nothing in it says so. Muted and static apart from the drop: an emptyfolder is exactly where a chat is dragged, and it has no row of its own to land on. */}{expanded && projectChats.length === 0 && (@@ -5183,7 +5637,10 @@ export function AppSidebar() {{...dnd.dropZoneProps({ section: order.section, folderId: project.id, blockEnd })}><SidebarMenuButton- onClick={() => toggleProjectShowAll(project.id)}+ onClick={() => {+ if (!showAll) void folderPages(project.id)?.loadMore();+ toggleProjectShowAll(project.id);+ }}// Force the muted token: .sidebar-nav-btn's own color rule outweighs a plain text utility,// so Show more would otherwise match the chat rows.className="sidebar-nav-btn h-[calc(30px*var(--ui-space-scale,1))] rounded-full pl-9 pr-4 font-medium text-nav-fg-muted!"@@ -5746,6 +6203,8 @@ export function AppSidebar() {<ProgressiveRowsitems={sortedRecentChatItems}pageSize={RECENTS_PAGE_SIZE}+ hasMore={recentsPages?.hasMore}+ onReachEnd={recentsPages?.loadMore}renderItem={(item) =>renderChatSidebarItem(item, "recent", {scope: RECENTS_ORDER_SCOPE,@@ -5773,9 +6232,8 @@ export function AppSidebar() {/></SidebarMenu>{/* existing project or archived threads can leave Recents empty. */}- {chatItemsLoaded &&- allChatItems.length === 0 &&- archivedChatItems.length === 0 && (+ {sidebarLooksEmpty &&+ hasUnlistedChats === false && (<p className="px-3 py-2 text-xs text-muted-foreground">{t("shell.navigation.noChatsYet")}</p>@@ -5798,7 +6256,12 @@ export function AppSidebar() {<CollapsibleContent><SidebarGroupContent className={unrailedRowPadding}><SidebarMenu>- {runItems.map((run) => {+ <ProgressiveRows+ items={runItems}+ pageSize={SIDEBAR_PAGE_SIZE}+ hasMore={runsHaveMore}+ onReachEnd={loadMoreRuns}+ renderItem={(run) => {// Explicit selection wins. Otherwise highlight the active job only while the "Current Run"// tab is the view, keeping Configure unhighlighted even though activeJobId stays pinned.const isActiveRun =@@ -5885,7 +6348,8 @@ export function AppSidebar() {</NonModalDropdownMenu></SidebarMenuItem>);- })}+ }}+ /></SidebarMenu></SidebarGroupContent></CollapsibleContent>diff --git a/studio/frontend/src/components/progressive-rows.tsx b/studio/frontend/src/components/progressive-rows.tsxindex 324190f2db6..17c8a52bad3 100644--- a/studio/frontend/src/components/progressive-rows.tsx+++ b/studio/frontend/src/components/progressive-rows.tsx@@ -1,10 +1,24 @@// SPDX-License-Identifier: AGPL-3.0-only// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0-import { memo, type ReactNode, useEffect, useRef, useState } from "react";+import {+ type ReactNode,+ memo,+ useCallback,+ useEffect,+ useRef,+ useState,+} from "react";/** mounts the next page this far below the sidebar's visible edge. */const PRELOAD_PX = 800;+/** frees a fetch that drew no rows (an error or the end), past the sidebar's render batching. */+const FETCH_SETTLE_MS = 250;+/** a scroll this soon after a page is drawn is the same gesture's own scroll, not a new one. */+const GESTURE_TAIL_MS = 200;++// one server page per user scroll, so a tall window or the preload margin never drains pages+const ARMING_EVENTS = ["scroll", "wheel", "touchstart", "touchmove"] as const;/** preserves mounted rows when the list grows because `render` remains stable. */const Row = memo(function Row<T>({@@ -14,47 +28,127 @@ const Row = memo(function Row<T>({return render(item);}) as <T>(props: { item: T; render: (item: T) => ReactNode }) => ReactNode;-/** pages mounted rows to bound Radix keydown listeners while callers retain the full list state. */+/** pages mounted rows to bound Radix keydown listeners; with `hasMore`, the end asks `onReachEnd` for more. */export function ProgressiveRows<T extends { id: string }>({items,pageSize,renderItem,end,+ hasMore = false,+ onReachEnd,+ mountAll = false,}: {items: readonly T[];pageSize: number;renderItem: (item: T) => ReactNode;end?: ReactNode;+ hasMore?: boolean;+ onReachEnd?: () => Promise<void> | void;+ /** Mount every row it is given and only page the server: for lists whose rows already arrive a+ * page at a time, so the rows navigation and selection see are the rows on screen. */+ mountAll?: boolean;}) {- const [limit, setLimit] = useState(pageSize);+ const [limit, setLimit] = useState(mountAll ? Number.POSITIVE_INFINITY : pageSize);const sentinelRef = useRef<HTMLLIElement>(null);- const hasMore = items.length > limit;+ const intersectingRef = useRef(false);+ const fetchingRef = useRef(false);+ const armedRef = useRef(false);+ const fetchFromRef = useRef(0);+ const itemCountRef = useRef(items.length);+ const drawnAtRef = useRef(0);+ const mountedMore = items.length > limit;+ const fetchMore = hasMore && Boolean(onReachEnd);+ const showSentinel = mountedMore || fetchMore;++ const advance = useCallback(+ (root: Element | null) => {+ if (items.length > limit) {+ setLimit((count) => count + pageSize);+ return;+ }+ if (!fetchMore || fetchingRef.current || !root || !armedRef.current) return;+ armedRef.current = false;+ fetchingRef.current = true;+ fetchFromRef.current = items.length;+ setLimit((count) => Math.max(count, items.length) + pageSize);+ void Promise.resolve(onReachEnd?.()).finally(() => {+ setTimeout(() => {+ if (itemCountRef.current <= fetchFromRef.current) fetchingRef.current = false;+ }, FETCH_SETTLE_MS);+ });+ },+ [items.length, limit, pageSize, fetchMore, onReachEnd],+ );++ // a scroll between the answer and the rows being drawn sees the old sentinel and would fetch again+ useEffect(() => {+ itemCountRef.current = items.length;+ if (fetchingRef.current && items.length > fetchFromRef.current) {+ fetchingRef.current = false;+ drawnAtRef.current = performance.now();+ }+ }, [items.length]);+// re-observe because IntersectionObserver does not repeat while the sentinel stays intersecting- // biome-ignore lint/correctness/useExhaustiveDependencies: `limit` is the re-observe triggeruseEffect(() => {const sentinel = sentinelRef.current;- if (!hasMore || !sentinel) return;+ if (!showSentinel || !sentinel) return;+ // use the sidebar scroller because the viewport sees its clipped sentinel only when visible+ const root = sentinel.closest<HTMLElement>("[data-sidebar='content']");const observer = new IntersectionObserver((entries) => {- if (entries.some((entry) => entry.isIntersecting)) {- setLimit((count) => count + pageSize);- }- },- {- // use the sidebar scroller because the viewport sees its clipped sentinel only when visible- root: sentinel.closest<HTMLElement>("[data-sidebar='content']"),- rootMargin: `0px 0px ${PRELOAD_PX}px 0px`,+ intersectingRef.current = entries.some((entry) => entry.isIntersecting);+ if (intersectingRef.current) advance(root);},+ { root, rootMargin: `0px 0px ${PRELOAD_PX}px 0px` },);observer.observe(sentinel);- return () => observer.disconnect();- }, [hasMore, limit, pageSize]);+ let lastTop = root?.scrollTop ?? 0;+ let touchY: number | null = null;+ const onArm = (event: Event) => {+ if (event.type === "touchstart") {+ touchY = (event as TouchEvent).touches[0]?.clientY ?? null;+ return;+ }+ // only a scroll down asks for more: a click's focus nudge or a scroll up does not+ if (event.type === "scroll") {+ const top = root?.scrollTop ?? 0;+ const down = top > lastTop;+ lastTop = top;+ if (!down) return;+ } else if (event.type === "wheel" && (event as WheelEvent).deltaY <= 0) {+ return;+ } else if (event.type === "touchmove") {+ // a finger moving up scrolls the list down; a swipe up the list or sideways does not+ const y = (event as TouchEvent).touches[0]?.clientY;+ const down = touchY !== null && y !== undefined && y < touchY;+ if (y !== undefined) touchY = y;+ if (!down) return;+ }+ if (fetchingRef.current || performance.now() - drawnAtRef.current < GESTURE_TAIL_MS) return;+ armedRef.current = true;+ if (intersectingRef.current) advance(root);+ };+ const armTarget = fetchMore ? root : null;+ for (const name of ARMING_EVENTS) {+ armTarget?.addEventListener(name, onArm, { passive: true });+ }+ return () => {+ observer.disconnect();+ for (const name of ARMING_EVENTS)+ armTarget?.removeEventListener(name, onArm);+ };+ }, [showSentinel, fetchMore, advance]);return (<>{items.slice(0, limit).map((item) => (<Row key={item.id} item={item} render={renderItem} />))}- {hasMore ? <li ref={sentinelRef} aria-hidden className="h-px" /> : end}+ {showSentinel ? (+ <li ref={sentinelRef} aria-hidden className="h-px" />+ ) : (+ end+ )}</>);}diff --git a/studio/frontend/src/features/chat/api/chat-api.ts b/studio/frontend/src/features/chat/api/chat-api.tsindex f218ad8ef34..dc9444ce8fa 100644--- a/studio/frontend/src/features/chat/api/chat-api.ts+++ b/studio/frontend/src/features/chat/api/chat-api.ts@@ -942,6 +942,104 @@ export async function listChatThreads(return Array.isArray(data.threads) ? data.threads : [];}+export interface SidebarRowRecord {+ id: string;+ type: "single" | "compare";+ threadIds: string[];+ title: string;+ createdAt: number;+ updatedAt: number;+ modifiedAt?: number | null;+ modelIds?: string[] | null;+ isFork?: boolean | null;+ /** Compare rows: each pane's own title, by `threadIds` position. */+ threadTitles?: string[] | null;+ projectId?: string | null;+}++export interface SidebarRowsRequest {+ archived: boolean;+ projectId: string | null;+ unfiledOnly: boolean;+ includeIds: string[] | null;+ excludeIds: string[];+ sort: "updated" | "manual";+ manualOrder: string[] | null;+ limit: number;+ cursor: string | null;+}++export interface SidebarRowsPage {+ rows: SidebarRowRecord[];+ nextCursor: string | null;+ hasMore: boolean;+ total?: number | null;+}++export class SidebarCursorRejectedError extends Error {+ readonly cursorRejected = true;+ constructor() {+ super("Sidebar page cursor rejected");+ this.name = "SidebarCursorRejectedError";+ }+}++/** Null on a server without the route: the caller falls back to the full list. */+export async function fetchSidebarRows(+ request: SidebarRowsRequest,+): Promise<SidebarRowsPage | null> {+ if (request.includeIds && request.includeIds.length === 0) {+ return { rows: [], nextCursor: null, hasMore: false, total: 0 };+ }+ const body: SidebarRowsRequest = {+ archived: request.archived,+ projectId: request.projectId,+ unfiledOnly: request.unfiledOnly,+ includeIds: request.includeIds,+ excludeIds: request.excludeIds,+ sort: request.sort,+ manualOrder: request.manualOrder,+ limit: request.limit,+ cursor: request.cursor,+ };+ const response = await authFetch("/api/chat/sidebar/rows", {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify(body),+ });+ if (response.status === 404 || response.status === 405) return null;+ if (response.status === 400 && request.cursor) throw new SidebarCursorRejectedError();+ const data = await parseJsonOrThrow<SidebarRowsPage>(response);+ return {+ rows: Array.isArray(data.rows) ? data.rows : [],+ nextCursor: data.nextCursor ?? null,+ hasMore: Boolean(data.hasMore),+ total: typeof data.total === "number" ? data.total : null,+ };+}++// under the route's 500 cap: 500 ids make a ~24 KB request line, past h11's 16 KiB head limit+const SIDEBAR_THREAD_ROWS_MAX = 200;++export async function fetchSidebarThreadRows(+ threadIds: string[],+): Promise<Record<string, string> | null> {+ const out: Record<string, string> = {};+ for (let start = 0; start < threadIds.length; start += SIDEBAR_THREAD_ROWS_MAX) {+ const params = new URLSearchParams();+ for (const id of threadIds.slice(start, start + SIDEBAR_THREAD_ROWS_MAX)) {+ params.append("thread_ids", id);+ }+ const response = await authFetch(`/api/chat/sidebar/thread-rows?${params}`);+ if (response.status === 404 || response.status === 405) return null;+ const data = await parseJsonOrThrow<{ rowIdByThreadId?: Record<string, string> }>(response);+ for (const [threadId, rowId] of Object.entries(data.rowIdByThreadId ?? {})) {+ if (typeof rowId === "string" && rowId) out[threadId] = rowId;+ }+ }+ return out;+}+/** One chat message attachment, as listed for the settings uploaded-files view. */export interface ChatAttachmentRecord {id: string;diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsxindex c3235b918d5..8ece87a0913 100644--- a/studio/frontend/src/features/chat/chat-page.tsx+++ b/studio/frontend/src/features/chat/chat-page.tsx@@ -2870,7 +2870,8 @@ export function ChatPage({: null;const { items: currentProjectItems, loaded: currentProjectItemsLoaded } =useChatSidebarItems({- projectId: currentProjectId ?? "__no_project_selected__",+ projectId: currentProjectId ?? "__no_project_selected__",+ enabled: Boolean(currentProjectId),});const currentChatTitle = activeThreadId? currentProjectItems.find((item) => item.id === activeThreadId)?.titlediff --git a/studio/frontend/src/features/chat/hooks/use-chat-projects.ts b/studio/frontend/src/features/chat/hooks/use-chat-projects.tsindex cfac0276248..b009bf000ab 100644--- a/studio/frontend/src/features/chat/hooks/use-chat-projects.ts+++ b/studio/frontend/src/features/chat/hooks/use-chat-projects.ts@@ -2,7 +2,10 @@// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0import { useEffect, useState, useSyncExternalStore } from "react";-import { CHAT_PROJECTS_UPDATED_EVENT } from "../api/chat-api";+import {+ CHAT_HISTORY_UPDATED_EVENT,+ CHAT_PROJECTS_UPDATED_EVENT,+} from "../api/chat-api";import type { ProjectRecord } from "../types";import {createStoredChatProject,@@ -14,6 +17,7 @@ import {} from "../utils/chat-history-storage";import { offerToDeleteKeptSandboxes } from "../utils/offer-kept-sandbox-files";import type { SidebarItem } from "./use-chat-sidebar-items";+import { readFullListProjectActivity } from "./use-sidebar-row-pages";let cachedProjects: ProjectRecord[] = [];let projectsLoaded = false;@@ -21,6 +25,16 @@ let projectsRequest: Promise<ProjectRecord[]> | null = null;let projectsRefreshPending = false;let lastProjectsUpdateEvent: Event | null = null;const projectSubscribers = new Set<() => void>();+const ACTIVITY_REFRESH_DEBOUNCE_MS = 2000;+let activityRefreshTimer: ReturnType<typeof setTimeout> | null = null;++function onChatActivity(): void {+ if (activityRefreshTimer !== null) clearTimeout(activityRefreshTimer);+ activityRefreshTimer = setTimeout(() => {+ activityRefreshTimer = null;+ void loadProjects(true, true).catch(() => undefined);+ }, ACTIVITY_REFRESH_DEBOUNCE_MS);+}function subscribeToProjects(onStoreChange: () => void): () => void {projectSubscribers.add(onStoreChange);@@ -32,11 +46,23 @@ function getProjectsSnapshot(): ProjectRecord[] {}function publishProjects(projects: ProjectRecord[]): void {+ if (projectsLoaded && JSON.stringify(projects) === JSON.stringify(cachedProjects)) return;cachedProjects = projects;projectsLoaded = true;for (const onStoreChange of projectSubscribers) onStoreChange();}+async function withChatActivity(projects: ProjectRecord[]): Promise<ProjectRecord[]> {+ if (projects.every((project) => "lastActivityAt" in project)) return projects;+ const activity = await readFullListProjectActivity().catch(() => null);+ if (!activity) return projects;+ return projects.map((project) =>+ "lastActivityAt" in project+ ? project+ : { ...project, lastActivityAt: activity.get(project.id) ?? null },+ );+}+function loadProjects(force = false,followUpIfPending = false,@@ -55,7 +81,7 @@ function loadProjects(projectsRefreshPending = false;try {const next = await listStoredChatProjects({ includeArchived: false });- nextProjects = Array.isArray(next) ? next : [];+ nextProjects = Array.isArray(next) ? await withChatActivity(next) : [];} catch (error) {if (!isExpectedBackgroundChatStorageError(error)) throw error;nextProjects = null;@@ -108,11 +134,15 @@ export function useChatProjects(): {};// Cached rows render immediately, then one shared request reconciles changes made by another// browser tab or API client.+ // one function per consumer: a shared reference is one DOM listener the first unmount would remove+ const onActivity = () => onChatActivity();void refresh(projectsLoaded);window.addEventListener(CHAT_PROJECTS_UPDATED_EVENT, onProjectsUpdated);+ window.addEventListener(CHAT_HISTORY_UPDATED_EVENT, onActivity);return () => {cancelled = true;window.removeEventListener(CHAT_PROJECTS_UPDATED_EVENT, onProjectsUpdated);+ window.removeEventListener(CHAT_HISTORY_UPDATED_EVENT, onActivity);};}, []);diff --git a/studio/frontend/src/features/chat/hooks/use-sidebar-row-pages.ts b/studio/frontend/src/features/chat/hooks/use-sidebar-row-pages.tsnew file mode 100644index 00000000000..d8af5002ed9--- /dev/null+++ b/studio/frontend/src/features/chat/hooks/use-sidebar-row-pages.ts@@ -0,0 +1,380 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import { getAuthSessionEpoch } from "@/features/auth";+import { useEffect, useMemo, useReducer, useRef } from "react";+import {+ CHAT_HISTORY_UPDATED_EVENT,+ type SidebarRowRecord,+ fetchSidebarRows,+} from "../api/chat-api";+import {+ listStoredChatThreads,+ settleLegacyChatImport,+} from "../utils/chat-history-storage";+import { isChatThreadDeleted } from "../utils/chat-thread-tombstones";+import { repairLegacyChatTitles } from "../utils/repair-legacy-chat-titles";+import { type RowPageResult, RowPager } from "../utils/sidebar-row-pager";+import {+ SIDEBAR_PAGE_SIZE,+ type SidebarRowQuery,+ applySidebarRowQuery,+ sidebarRowQueryKey,+} from "../utils/sidebar-row-query";+import type { ThreadRecord } from "../types";+import { type SidebarItem, groupThreads } from "./use-chat-sidebar-items";++const SIDEBAR_REFRESH_DEBOUNCE_MS = 300;+const FALLBACK_CURSOR = "full:";++function toSidebarItem(row: SidebarRowRecord): SidebarItem | null {+ // a delete tombstones its threads before the server forgets them+ const threadIds = (row.threadIds?.length ? row.threadIds : [row.id]).filter(+ (id) => !isChatThreadDeleted(id),+ );+ if (threadIds.length === 0) return null;+ return {+ type: row.type,+ id: row.id,+ threadIds,+ title: row.title,+ createdAt: row.createdAt,+ updatedAt: row.updatedAt,+ ...(row.modifiedAt ? { modifiedAt: row.modifiedAt } : {}),+ ...(row.modelIds?.length ? { modelIds: row.modelIds } : {}),+ ...(row.type === "single" ? { isFork: Boolean(row.isFork) } : {}),+ projectId: row.projectId ?? null,+ };+}++let serverPages: boolean | null = null;++let fullList: {+ epoch: number;+ session: number;+ promise: Promise<Awaited<ReturnType<typeof listStoredChatThreads>>>;+} | null = null;+let fullListEpoch = 0;++function readFullList() {+ const session = getAuthSessionEpoch();+ if (+ !fullList ||+ fullList.epoch !== fullListEpoch ||+ fullList.session !== session+ ) {+ const promise = listStoredChatThreads({ includeArchived: true });+ fullList = { epoch: fullListEpoch, session, promise };+ void promise.then(repairLegacyChatTitles).catch(() => undefined);+ promise.catch(() => {+ if (fullList?.promise === promise) fullList = null;+ });+ }+ return fullList.promise;+}++/** Each project's newest chat activity from the full listing, for servers whose projects come+ * without `lastActivityAt`: the same listing their paged lists read, so folders order as before. */+export async function readFullListProjectActivity(): Promise<Map<string, number>> {+ const activity = new Map<string, number>();+ for (const item of groupThreads(await readFullList())) {+ if (item.projectId && item.updatedAt > (activity.get(item.projectId) ?? 0)) {+ activity.set(item.projectId, item.updatedAt);+ }+ }+ return activity;+}++async function fullListPage(+ query: SidebarRowQuery,+ limit: number,+ cursor: string | null,+): Promise<RowPageResult<SidebarItem>> {+ const rows = applySidebarRowQuery(+ groupThreads(await readFullList(), query.archived),+ query,+ );+ const offset = cursor?.startsWith(FALLBACK_CURSOR)+ ? Number(cursor.slice(FALLBACK_CURSOR.length)) || 0+ : 0;+ const end = offset + limit;+ const hasMore = end < rows.length;+ return {+ rows: rows.slice(offset, end),+ nextCursor: hasMore ? `${FALLBACK_CURSOR}${end}` : null,+ hasMore,+ total: rows.length,+ };+}++export async function fetchSidebarRowPage(+ query: SidebarRowQuery,+ limit: number,+ cursor: string | null,+): Promise<RowPageResult<SidebarItem>> {+ const continuesFullList = cursor?.startsWith(FALLBACK_CURSOR) ?? false;+ if (+ !continuesFullList &&+ serverPages !== false &&+ (await settleLegacyChatImport())+ ) {+ const page = await fetchSidebarRows({ ...query, limit, cursor });+ if (page) {+ serverPages = true;+ const shown = page.rows.flatMap((row) =>+ (row.threadIds?.length ? row.threadIds : [row.id]).map((id, index) => ({+ id,+ title: row.threadTitles?.[index] ?? row.title,+ })),+ );+ void repairLegacyChatTitles(shown as ThreadRecord[]).catch(() => undefined);+ return {+ rows: page.rows+ .map(toSidebarItem)+ .filter((row): row is SidebarItem => row !== null),+ nextCursor: page.nextCursor,+ hasMore: page.hasMore,+ total: page.total ?? null,+ };+ }+ serverPages = false;+ }+ return fullListPage(query, limit, continuesFullList ? cursor : null);+}++/** Every row id in order, so a manual order written from a partly loaded list keeps unloaded rows. */+export async function readAllSidebarRowIds(+ query: SidebarRowQuery,+): Promise<string[]> {+ return (await readAllSidebarRows(query)).map((row) => row.id);+}++/** Every row of a query, so a whole-list action never takes the loaded pages for the list. */+export async function readAllSidebarRows(+ query: SidebarRowQuery,+): Promise<SidebarItem[]> {+ const rows: SidebarItem[] = [];+ let cursor: string | null = null;+ for (let guard = 0; guard < 10_000; guard++) {+ const page: RowPageResult<SidebarItem> = await fetchSidebarRowPage(+ query,+ 200,+ cursor,+ );+ rows.push(...page.rows);+ // a page emptied by chats deleted here still moves the cursor on; only one that did not ends it+ if (!page.hasMore || !page.nextCursor) break;+ if (page.rows.length === 0 && page.nextCursor === cursor) break;+ cursor = page.nextCursor;+ }+ return rows;+}++type SidebarPager = RowPager<SidebarRowQuery, SidebarItem>;++const livePagers = new Set<SidebarPager>();+let refreshTimer: ReturnType<typeof setTimeout> | null = null;++function onHistoryUpdated() {+ if (refreshTimer !== null) clearTimeout(refreshTimer);+ refreshTimer = setTimeout(() => {+ refreshTimer = null;+ fullListEpoch += 1;+ for (const pager of livePagers) {+ const { loaded, loading } = pager.getState();+ if (loaded || loading) void pager.refresh();+ }+ }, SIDEBAR_REFRESH_DEBOUNCE_MS);+}++function registerPager(pager: SidebarPager) {+ if (livePagers.size === 0 && typeof window !== "undefined") {+ window.addEventListener(CHAT_HISTORY_UPDATED_EVENT, onHistoryUpdated);+ }+ livePagers.add(pager);+}++function unregisterPager(pager: SidebarPager) {+ livePagers.delete(pager);+ if (livePagers.size === 0 && typeof window !== "undefined") {+ window.removeEventListener(CHAT_HISTORY_UPDATED_EVENT, onHistoryUpdated);+ if (refreshTimer !== null) clearTimeout(refreshTimer);+ refreshTimer = null;+ }+}++export interface SidebarRowSlot {+ query: SidebarRowQuery;+ initialLimit?: number;+ /** loaded for row lookups only: its rows are in `items` but never in `listedItems` */+ lookupOnly?: boolean;+}++export interface SidebarRowPages {+ rows: SidebarItem[];+ hasMore: boolean;+ total: number | null;+ loaded: boolean;+ loading: boolean;+ error: unknown;+ loadMore: () => Promise<void>;+ loadAll: () => Promise<void>;+ retry: () => Promise<void>;+}++const EMPTY_ROWS: SidebarItem[] = [];+const IDLE_LIST: SidebarRowPages = {+ rows: EMPTY_ROWS,+ hasMore: false,+ total: null,+ loaded: false,+ loading: false,+ error: null,+ loadMore: () => Promise.resolve(),+ loadAll: () => Promise.resolve(),+ retry: () => Promise.resolve(),+};++type RowUnion = { parts: SidebarItem[][]; items: SidebarItem[] };++/** Every row of `parts` once, in order; the previous array while no part changed. */+function unionRows(previous: RowUnion, parts: SidebarItem[][]): RowUnion {+ if (+ previous.parts.length === parts.length &&+ previous.parts.every((part, index) => part === parts[index])+ ) {+ return previous;+ }+ const seen = new Set<string>();+ const items: SidebarItem[] = [];+ for (const part of parts) {+ for (const item of part) {+ if (seen.has(item.id)) continue;+ seen.add(item.id);+ items.push(item);+ }+ }+ return { parts, items: items.length ? items : EMPTY_ROWS };+}++/** `items` is every loaded row once, in slot order, for lookups; `listedItems` leaves out+ * lookup-only slots, so a list never draws a row its own pages have not reached. */+export function useSidebarRowPages(+ slots: Record<string, SidebarRowSlot | null>,+ enabled: boolean,+): {+ lists: Record<string, SidebarRowPages>;+ items: SidebarItem[];+ listedItems: SidebarItem[];+} {+ const pagersRef = useRef(new Map<string, SidebarPager>());+ const [version, bump] = useReducer((n: number) => n + 1, 0);++ const active = Object.entries(slots).filter(+ (entry): entry is [string, SidebarRowSlot] => entry[1] !== null,+ );+ const slotKey = enabled+ ? JSON.stringify(+ active.map(([name, slot]) => [+ name,+ sidebarRowQueryKey(slot.query),+ slot.initialLimit ?? SIDEBAR_PAGE_SIZE,+ ]),+ )+ : "";+ const slotsRef = useRef(active);+ slotsRef.current = active;++ // biome-ignore lint/correctness/useExhaustiveDependencies: `slotKey` names every slot's query+ useEffect(() => {+ const pagers = pagersRef.current;+ const wanted = enabled ? slotsRef.current : [];+ const names = new Set(wanted.map(([name]) => name));+ for (const [name, pager] of pagers) {+ if (names.has(name)) continue;+ unregisterPager(pager);+ pagers.delete(name);+ }+ for (const [name, slot] of wanted) {+ const key = sidebarRowQueryKey(slot.query);+ const existing = pagers.get(name);+ if (existing) {+ void existing.setQuery(slot.query, key);+ continue;+ }+ const pager: SidebarPager = new RowPager(+ fetchSidebarRowPage,+ slot.query,+ key,+ {+ initialLimit: slot.initialLimit ?? SIDEBAR_PAGE_SIZE,+ pageSize: SIDEBAR_PAGE_SIZE,+ },+ );+ pagers.set(name, pager);+ registerPager(pager);+ void pager.start();+ }+ // one sidebar render per 16 ms however many lists answer; a timer, as a page drawing no frames never runs rAF+ let timer: ReturnType<typeof setTimeout> | null = null;+ const schedule = () => {+ if (timer !== null) return;+ timer = setTimeout(() => {+ timer = null;+ bump();+ }, 16);+ };+ const unsubscribes = [...pagers.values()].map((pager) =>+ pager.subscribe(schedule),+ );+ bump();+ return () => {+ for (const unsubscribe of unsubscribes) unsubscribe();+ if (timer !== null) clearTimeout(timer);+ };+ }, [slotKey, enabled]);++ useEffect(() => {+ const pagers = pagersRef.current;+ return () => {+ for (const pager of pagers.values()) unregisterPager(pager);+ pagers.clear();+ };+ }, []);++ const unionRef = useRef<RowUnion>({ parts: [], items: EMPTY_ROWS });+ const listedRef = useRef<RowUnion>({ parts: [], items: EMPTY_ROWS });++ // biome-ignore lint/correctness/useExhaustiveDependencies: `version` moves on every pager change+ return useMemo(() => {+ const lists: Record<string, SidebarRowPages> = {};+ const parts: SidebarItem[][] = [];+ const listedParts: SidebarItem[][] = [];+ for (const [name, slot] of slotsRef.current) {+ const pager = pagersRef.current.get(name);+ if (!pager) {+ lists[name] = IDLE_LIST;+ continue;+ }+ const state = pager.getState();+ parts.push(state.rows);+ if (!slot.lookupOnly) listedParts.push(state.rows);+ lists[name] = {+ ...state,+ loadMore: () => (state.error ? pager.retry() : pager.loadMore()),+ // rejects when a page failed, so a caller never takes the partial list for the whole+ loadAll: async () => {+ await pager.loadAll();+ // render now, not on the batched timer: a caller acting on the rows (Select All) renders next+ bump();+ const { error } = pager.getState();+ if (error) throw error;+ },+ retry: () => pager.retry(),+ };+ }+ unionRef.current = unionRows(unionRef.current, parts);+ listedRef.current = unionRows(listedRef.current, listedParts);+ return { lists, items: unionRef.current.items, listedItems: listedRef.current.items };+ }, [version, slotKey]);+}diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.tsindex fc80cc219c3..bbb057bf352 100644--- a/studio/frontend/src/features/chat/index.ts+++ b/studio/frontend/src/features/chat/index.ts@@ -39,6 +39,8 @@ export {type KvCacheEstimate,type LocalModelInfo,type ScanFolderInfo,+ CHAT_HISTORY_UPDATED_EVENT,+ fetchSidebarThreadRows,} from "./api/chat-api";export type {ApiMonitorEntry,@@ -533,6 +535,22 @@ export {useChatSidebarItems,type SidebarItem,} from "./hooks/use-chat-sidebar-items";+export {+ fetchSidebarRowPage,+ readAllSidebarRowIds,+ readAllSidebarRows,+ useSidebarRowPages,+ type SidebarRowPages,+ type SidebarRowSlot,+} from "./hooks/use-sidebar-row-pages";+export {+ SIDEBAR_PAGE_SIZE,+ holdRowsPastLoadedChats,+ mergeManualOrderTail,+ settledManualOrder,+ sidebarRowQuery,+ type SidebarRowQuery,+} from "./utils/sidebar-row-query";export {createChatProject,deleteChatProject,diff --git a/studio/frontend/src/features/chat/types.ts b/studio/frontend/src/features/chat/types.tsindex dbfe7ee79a4..c33ad7d1b8c 100644--- a/studio/frontend/src/features/chat/types.ts+++ b/studio/frontend/src/features/chat/types.ts@@ -26,6 +26,7 @@ export interface ProjectRecord {archived: boolean;createdAt: number;updatedAt: number;+ lastActivityAt?: number | null;}export interface ThreadRecord {diff --git a/studio/frontend/src/features/chat/utils/chat-history-storage.ts b/studio/frontend/src/features/chat/utils/chat-history-storage.tsindex 62c2292a6e8..117d8ecffa3 100644--- a/studio/frontend/src/features/chat/utils/chat-history-storage.ts+++ b/studio/frontend/src/features/chat/utils/chat-history-storage.ts@@ -511,6 +511,13 @@ async function dexieIsEmpty(): Promise<boolean> {}}+/** Runs this session's legacy import; true once the server alone holds every chat. */+export async function settleLegacyChatImport(): Promise<boolean> {+ await importLegacyChatsIfNeeded().catch(() => undefined);+ await Promise.allSettled([...pendingLegacyThreadImports]);+ return isLegacyChatImportDone();+}+async function importLegacyChatsIfNeeded(): Promise<void> {// Session-level cache: repeated sidebar mounts share one import. localStorage is NOT// consulted, so a studio.db wipe re-triggers the import even with a stale hint.diff --git a/studio/frontend/src/features/chat/utils/opened-chats.ts b/studio/frontend/src/features/chat/utils/opened-chats.tsnew file mode 100644index 00000000000..5b001fb9c23--- /dev/null+++ b/studio/frontend/src/features/chat/utils/opened-chats.ts@@ -0,0 +1,31 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++/** How many recently opened chats keep their sidebar rows loaded. */+export const OPENED_CHATS_LIMIT = 20;++/** Most recently opened first. */+export function touchOpenedChat(opened: readonly string[], id: string): string[] {+ return [id, ...opened.filter((other) => other !== id)];+}++/** Evicts the least recently opened chats past `limit`, skipping running ones and the open one: a+ * running chat is never evicted, so the list passes `limit` when more than `limit` are running. */+export function keepOpenedChats(+ opened: readonly string[],+ running: ReadonlySet<string>,+ limit = OPENED_CHATS_LIMIT,+): string[] {+ let over = opened.length - limit;+ if (over <= 0) return [...opened];+ const kept: string[] = [];+ for (let index = opened.length - 1; index >= 0; index--) {+ const id = opened[index];+ if (over > 0 && index > 0 && !running.has(id)) {+ over -= 1;+ continue;+ }+ kept.push(id);+ }+ return kept.reverse();+}diff --git a/studio/frontend/src/features/chat/utils/sidebar-row-pager.ts b/studio/frontend/src/features/chat/utils/sidebar-row-pager.tsnew file mode 100644index 00000000000..88b0f907b3d--- /dev/null+++ b/studio/frontend/src/features/chat/utils/sidebar-row-pager.ts@@ -0,0 +1,246 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++export interface RowPageResult<T> {+ rows: T[];+ nextCursor: string | null;+ hasMore: boolean;+ total?: number | null;+}++export type FetchRowPage<Q, T> = (+ query: Q,+ limit: number,+ cursor: string | null,+) => Promise<RowPageResult<T>>;++export interface RowPagerState<T> {+ rows: T[];+ hasMore: boolean;+ total: number | null;+ loaded: boolean;+ loading: boolean;+ error: unknown;+}++export interface RowPagerOptions {+ initialLimit: number;+ pageSize: number;+ maxRequestLimit?: number;+}++const DEFAULT_MAX_REQUEST_LIMIT = 200;++function dedupeAppend<T extends { id: string }>(head: T[], tail: T[]): T[] {+ const seen = new Set(head.map((row) => row.id));+ const out = [...head];+ for (const row of tail) {+ if (seen.has(row.id)) continue;+ seen.add(row.id);+ out.push(row);+ }+ return out;+}++/** One list read a page at a time; a refresh re-reads the loaded window; answers from an older generation are dropped. */+export class RowPager<Q, T extends { id: string }> {+ private state: RowPagerState<T> = {+ rows: [],+ hasMore: false,+ total: null,+ loaded: false,+ loading: false,+ error: null,+ };+ private cursor: string | null = null;+ private generation = 0;+ private pending: Promise<void> | null = null;+ private refreshing = false;+ // the rows on screen are not this query's top window (a refresh failed), so retry re-reads+ private stale = false;+ private wantedDepth = 0;+ private readonly listeners = new Set<() => void>();+ private readonly maxRequestLimit: number;+ private readonly fetchPage: FetchRowPage<Q, T>;+ private query: Q;+ private queryKey: string;+ private readonly options: RowPagerOptions;++ constructor(+ fetchPage: FetchRowPage<Q, T>,+ query: Q,+ queryKey: string,+ options: RowPagerOptions,+ ) {+ this.fetchPage = fetchPage;+ this.query = query;+ this.queryKey = queryKey;+ this.options = options;+ this.maxRequestLimit = options.maxRequestLimit ?? DEFAULT_MAX_REQUEST_LIMIT;+ }++ getState(): RowPagerState<T> {+ return this.state;+ }++ subscribe(listener: () => void): () => void {+ this.listeners.add(listener);+ return () => this.listeners.delete(listener);+ }++ /** `quiet` stores without telling listeners: nothing on screen changed, so no render is owed. */+ private patch(next: Partial<RowPagerState<T>>, quiet = false): void {+ this.state = { ...this.state, ...next };+ if (quiet) return;+ for (const listener of this.listeners) listener();+ }++ private async read(+ cursor: string | null,+ count: number,+ generation: number,+ ): Promise<{+ rows: T[];+ cursor: string | null;+ hasMore: boolean;+ total: number | null;+ }> {+ let rows: T[] = [];+ let next = cursor;+ let hasMore = true;+ let total: number | null = null;+ while (rows.length < count && hasMore && generation === this.generation) {+ const limit = Math.min(this.maxRequestLimit, count - rows.length);+ const from = next;+ const page = await this.fetchPage(this.query, limit, next);+ rows = dedupeAppend(rows, page.rows);+ next = page.nextCursor;+ hasMore = page.hasMore && page.nextCursor !== null;+ total = page.total ?? total;+ // a page emptied by chats deleted here still moves the cursor on; only one that did not ends it+ if (page.rows.length === 0 && next === from) break;+ }+ return { rows, cursor: next, hasMore, total };+ }++ start(): Promise<void> {+ if (this.state.loaded || this.state.loading)+ return this.pending ?? Promise.resolve();+ return this.refresh();+ }++ /** Keeps the loaded depth, so a re-sort or a pin does not reset the scroll to page one. */+ setQuery(query: Q, queryKey: string): Promise<void> {+ if (queryKey === this.queryKey) return Promise.resolve();+ this.query = query;+ this.queryKey = queryKey;+ // a page asked of the old query is not a page of this one+ this.wantedDepth = 0;+ if (!this.state.loaded && !this.state.loading) return Promise.resolve();+ return this.refresh();+ }++ refresh(): Promise<void> {+ const generation = ++this.generation;+ // a page asked for and not yet drawn is read too, or the refresh would drop the scroll's page+ const window = Math.max(this.options.initialLimit, this.state.rows.length, this.wantedDepth);+ this.patch({ loading: true }, this.state.loaded);+ this.refreshing = true;+ const run = (async () => {+ try {+ const result = await this.read(null, window, generation);+ if (generation !== this.generation) return;+ this.refreshing = false;+ this.wantedDepth = 0;+ this.cursor = result.cursor;+ this.stale = false;+ const prev = this.state;+ const same =+ prev.loaded &&+ prev.error === null &&+ prev.hasMore === result.hasMore &&+ prev.total === result.total &&+ JSON.stringify(prev.rows) === JSON.stringify(result.rows);+ this.patch(+ {+ rows: same ? prev.rows : result.rows,+ hasMore: result.hasMore,+ total: result.total,+ loaded: true,+ loading: false,+ error: null,+ },+ same,+ );+ } catch (error) {+ if (generation !== this.generation) return;+ this.refreshing = false;+ this.stale = true;+ this.patch({ loaded: true, loading: false, error });+ }+ })();+ this.pending = run;+ return run;+ }++ private extend(count: number): Promise<void> {+ if (this.state.loading) {+ if (this.refreshing && this.pending) return this.pending.then(() => this.extend(count));+ return this.pending ?? Promise.resolve();+ }+ if (!this.state.loaded || !this.state.hasMore || this.state.error) {+ return Promise.resolve();+ }+ const generation = this.generation;+ if (count === this.options.pageSize) this.wantedDepth = this.state.rows.length + count;+ this.patch({ loading: true });+ const run = (async () => {+ try {+ const result = await this.read(this.cursor, count, generation);+ if (generation !== this.generation) return;+ this.wantedDepth = 0;+ this.cursor = result.cursor;+ this.patch({+ rows: dedupeAppend(this.state.rows, result.rows),+ hasMore: result.hasMore,+ total: result.total ?? this.state.total,+ loading: false,+ });+ } catch (error) {+ if (generation !== this.generation) return;+ if ((error as { cursorRejected?: boolean } | null)?.cursorRejected) {+ this.patch({ loading: false });+ await this.refresh();+ return;+ }+ this.wantedDepth = 0;+ this.patch({ loading: false, error });+ }+ })();+ this.pending = run;+ return run;+ }++ loadMore(): Promise<void> {+ return this.extend(this.options.pageSize);+ }++ async loadAll(): Promise<void> {+ await this.start();+ for (let guard = 0; guard < 10_000; guard++) {+ if (this.state.loading) {+ await this.pending;+ continue;+ }+ if (!this.state.hasMore || this.state.error) return;+ await this.extend(Number.MAX_SAFE_INTEGER);+ }+ }++ retry(): Promise<void> {+ if (!this.state.error) return Promise.resolve();+ if (this.stale || this.state.rows.length === 0) return this.refresh();+ this.patch({ error: null });+ return this.loadMore();+ }+}diff --git a/studio/frontend/src/features/chat/utils/sidebar-row-query.ts b/studio/frontend/src/features/chat/utils/sidebar-row-query.tsnew file mode 100644index 00000000000..d94b410e359--- /dev/null+++ b/studio/frontend/src/features/chat/utils/sidebar-row-query.ts@@ -0,0 +1,108 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import type { SidebarRowsRequest } from "../api/chat-api";++export const SIDEBAR_PAGE_SIZE = 20;++export type SidebarRowQuery = Omit<SidebarRowsRequest, "limit" | "cursor">;++export function sidebarRowQuery(+ query: Partial<SidebarRowQuery>,+): SidebarRowQuery {+ return {+ archived: query.archived ?? false,+ projectId: query.projectId ?? null,+ unfiledOnly: query.unfiledOnly ?? false,+ includeIds: query.includeIds ?? null,+ excludeIds: [...new Set(query.excludeIds ?? [])].sort(),+ sort: query.sort ?? "updated",+ manualOrder: query.sort === "manual" ? (query.manualOrder ?? []) : null,+ };+}++export function sidebarRowQueryKey(query: SidebarRowQuery): string {+ return JSON.stringify(query);+}++interface QueryRow {+ id: string;+ projectId?: string | null;+ threadIds?: string[];+}++/** The server's filter and order on the client, for a server without the paged route. */+export function applySidebarRowQuery<T extends QueryRow>(+ rows: readonly T[],+ query: SidebarRowQuery,+): T[] {+ const include = query.includeIds ? new Set(query.includeIds) : null;+ const exclude = new Set(query.excludeIds);+ const out = rows.filter((row) => {+ if (include && !include.has(row.id) && !row.threadIds?.some((id) => include.has(id))) {+ return false;+ }+ if (exclude.has(row.id)) return false;+ if (+ query.projectId !== null &&+ (row.projectId ?? null) !== query.projectId+ ) {+ return false;+ }+ if (query.unfiledOnly && row.projectId) return false;+ return true;+ });+ if (query.sort !== "manual" || !query.manualOrder?.length) return out;+ const rank = new Map(query.manualOrder.map((id, index) => [id, index]));+ return out.sort((a, b) => (rank.get(a.id) ?? -1) - (rank.get(b.id) ?? -1));+}++/** `next` orders the loaded rows; rows of `full` it omits keep their places after them. */+export function mergeManualOrderTail(+ next: readonly string[],+ full: readonly string[],+): string[] {+ const seen = new Set(next);+ const out = [...next];+ for (const id of full) {+ if (!seen.has(id)) {+ seen.add(id);+ out.push(id);+ }+ }+ return out;+}++/** A manual list whose chats are still paging draws only what comes before its last loaded chat: a+ * folder or page ranked later waits for the page that reaches it instead of sitting above chats+ * that have not loaded. Unranked folders lead, as `applyManualOrder` sorts them. */+export function holdRowsPastLoadedChats<T extends { id: string; kind: string }>(+ rows: readonly T[],+ order: readonly string[] | undefined,+ hasMore: boolean,+): T[] {+ if (!hasMore) return [...rows];+ const rank = new Map((order ?? []).map((id, index) => [id, index]));+ const key = (row: T) => rank.get(row.id) ?? -1;+ const chats = rows.filter((row) => row.kind === "chat");+ const edge = chats.length ? key(chats[chats.length - 1]) : -1;+ return rows.filter(+ (row) =>+ row.kind === "chat" || (key(row) === -1 && row.kind === "project") || key(row) < edge,+ );+}++/** The order a drag saves once a paged list's ids are all known: the dragged rows, then the+ * unloaded chats with no rank (they sort first), then everything else where it was, so a row+ * held back past the loaded chats keeps its place among them. */+export function settledManualOrder(+ dragged: readonly string[],+ all: readonly string[],+ current: readonly string[],+): string[] {+ const ranked = new Set(current);+ return mergeManualOrderTail(+ mergeManualOrderTail(dragged, all.filter((id) => !ranked.has(id))),+ current,+ );+}diff --git a/studio/frontend/src/features/training/hooks/use-training-history-sidebar.ts b/studio/frontend/src/features/training/hooks/use-training-history-sidebar.tsindex 6569214c40a..5756c398d1b 100644--- a/studio/frontend/src/features/training/hooks/use-training-history-sidebar.ts+++ b/studio/frontend/src/features/training/hooks/use-training-history-sidebar.ts@@ -12,6 +12,7 @@ import {import type { TrainingRunSummary } from "../types/history";const SIDEBAR_LIMIT = 20;+const MAX_REQUEST_LIMIT = 200;const RUNNING_POLL_MS = 5000;const INITIAL_RETRY_DELAYS_MS = [500, 1500, 3500];const LOAD_FAILURE_TOAST_ID = "training-history-load-failure";@@ -23,9 +24,62 @@ function isAbortError(err: unknown): boolean {export function useTrainingHistorySidebarItems(enabled: boolean) {const [items, setItems] = useState<TrainingRunSummary[]>([]);const [loaded, setLoaded] = useState(false);+ const [total, setTotal] = useState(0);const controllerRef = useRef<AbortController | null>(null);+ const loadedCountRef = useRef(0);+ useEffect(() => {+ loadedCountRef.current = items.length;+ }, [items.length]);const fetchRuns = useCallback(async (): Promise<void> => {+ if (controllerRef.current && !controllerRef.current.signal.aborted) {+ return;+ }+ const controller = new AbortController();+ controllerRef.current = controller;+ try {+ const wanted = Math.max(SIDEBAR_LIMIT, loadedCountRef.current);+ let runs: TrainingRunSummary[] = [];+ let runTotal = 0;+ for (let attempt = 0; attempt < 3; attempt++) {+ runs = [];+ let offset = 0;+ let firstTotal: number | null = null;+ let shifted = false;+ while (offset < wanted) {+ const result = await listTrainingRuns(+ Math.min(MAX_REQUEST_LIMIT, wanted - offset),+ offset,+ controller.signal,+ );+ offset += result.runs.length;+ firstTotal ??= result.total;+ const seen = new Set(runs.map((run) => run.id));+ const fresh = result.runs.filter((run) => !seen.has(run.id));+ shifted ||= result.total !== firstTotal || fresh.length < result.runs.length;+ runs.push(...fresh);+ runTotal = result.total;+ if (result.runs.length === 0 || offset >= result.total) break;+ }+ if (!shifted) break;+ }+ if (controller.signal.aborted) {+ return;+ }+ setItems(runs);+ setTotal(runTotal);+ setLoaded(true);+ toast.dismiss(LOAD_FAILURE_TOAST_ID);+ } finally {+ if (controllerRef.current === controller) {+ controllerRef.current = null;+ }+ }+ }, []);++ const hasMore = loaded && items.length < total;+ // shares the in-flight guard so a poll and a page never race to write the list+ const loadMore = useCallback(async (): Promise<void> => {if (controllerRef.current && !controllerRef.current.signal.aborted) {return;}@@ -34,15 +88,18 @@ export function useTrainingHistorySidebarItems(enabled: boolean) {try {const result = await listTrainingRuns(SIDEBAR_LIMIT,- 0,+ loadedCountRef.current,controller.signal,);if (controller.signal.aborted) {return;}- setItems(result.runs);- setLoaded(true);- toast.dismiss(LOAD_FAILURE_TOAST_ID);+ setItems((prev) => {+ const seen = new Set(prev.map((run) => run.id));+ return [...prev, ...result.runs.filter((run) => !seen.has(run.id))];+ });+ setTotal(result.total);+ } catch {} finally {if (controllerRef.current === controller) {controllerRef.current = null;@@ -178,6 +235,7 @@ export function useTrainingHistorySidebarItems(enabled: boolean) {const offDeleted = onTrainingRunDeleted((runId) => {controllerRef.current?.abort();setItems((prev) => prev.filter((run) => run.id !== runId));+ setTotal((count) => Math.max(0, count - 1));});const offChanged = onTrainingRunsChanged(() => {controllerRef.current?.abort();@@ -190,5 +248,5 @@ export function useTrainingHistorySidebarItems(enabled: boolean) {};}, [refresh]);- return { items, loaded, refresh };+ return { items, loaded, refresh, hasMore, loadMore };}diff --git a/studio/frontend/tests/keyboard-shortcuts.test.ts b/studio/frontend/tests/keyboard-shortcuts.test.tsindex d19c388f211..33eb01cf3f1 100644--- a/studio/frontend/tests/keyboard-shortcuts.test.ts+++ b/studio/frontend/tests/keyboard-shortcuts.test.ts@@ -1606,9 +1606,12 @@ test("the published chat lists stop where the sidebar stops", async () => {]) {assert.match(APP_SIDEBAR, group);}- // Select All reads the same rows the walk does, so it cannot reach further.- const selectAll = APP_SIDEBAR.indexOf("const selectAllChats = useCallback(");- assert.ok(selectAll !== -1, "selectAllChats moved");+ assert.match(+ APP_SIDEBAR,+ /const selectAllChats = useCallback\(\(\) => \{\n\s*if \(!chatRowsOnScreen\) return;/,+ );+ const selectAll = APP_SIDEBAR.indexOf("const selectRenderedChats = useCallback(");+ assert.ok(selectAll !== -1, "selectRenderedChats moved");assert.match(APP_SIDEBAR.slice(selectAll, APP_SIDEBAR.indexOf("\n }, [", selectAll)),/const ids = renderedChatItems\.map\(\(item\) => item\.id\);/,diff --git a/studio/frontend/tests/sidebar-custom-sections.test.ts b/studio/frontend/tests/sidebar-custom-sections.test.tsindex 54b5531244a..ae13717826d 100644--- a/studio/frontend/tests/sidebar-custom-sections.test.ts+++ b/studio/frontend/tests/sidebar-custom-sections.test.ts@@ -741,9 +741,11 @@ test("a custom section's menu edits it, acts on its chats, and removes it", () =assert.equal((menu.match(/<DropdownMenuSeparator \/>/g) ?? []).length, 2);// Only the section's own chats, and each action is off when it has nothing to do.assert.match(menu, /row\.kind === "chat" \? \[row\.item\] : \[\]/);- assert.match(menu, /disabled=\{!threadIds\.some\(\(id\) => unreadThreadIds\.has\(id\)\)\}/);- assert.match(menu, /onSelect=\{\(\) => clearThreadsUnread\(threadIds\)\}/);- assert.match(menu, /disabled=\{chats\.length === 0\} onSelect=\{\(\) => void archiveChatItems\(chats\)\}/);+ // the whole section's unread chats, not only its loaded pages+ assert.match(menu, /disabled=\{unreadInSection\.length === 0\}/);+ assert.match(menu, /onSelect=\{\(\) => clearThreadsUnread\(unreadInSection\)\}/);+ assert.match(menu, /disabled=\{chats\.length === 0\}\n\s*onSelect=\{\(\) => void archiveSection\(\)\.catch\(\(\) => undefined\)\}/);+ assert.match(menu, /await archiveChatItems\(all\);/);assert.match(menu, /icon=\{Settings02Icon\}[\s\S]*icon=\{Tick02Icon\}[\s\S]*icon=\{Archive03Icon\}[\s\S]*icon=\{Cancel01Icon\}/);assert.doesNotMatch(menu, /variant="destructive"|renderSortSubmenu/);});diff --git a/studio/frontend/tests/sidebar-opened-chats.test.ts b/studio/frontend/tests/sidebar-opened-chats.test.tsnew file mode 100644index 00000000000..e9b32968057--- /dev/null+++ b/studio/frontend/tests/sidebar-opened-chats.test.ts@@ -0,0 +1,155 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import test from "node:test";+import type * as RowPagesModule from "../src/features/chat/hooks/use-sidebar-row-pages.ts";+import {+ keepOpenedChats,+ OPENED_CHATS_LIMIT,+ touchOpenedChat,+} from "../src/features/chat/utils/opened-chats.ts";+import { RowPager } from "../src/features/chat/utils/sidebar-row-pager.ts";+import * as rowQuery from "../src/features/chat/utils/sidebar-row-query.ts";+import { loadWithStubs } from "./helpers/module-stubs.ts";++const none = new Set<string>();++function open(ids: string[], running: ReadonlySet<string> = none, start: string[] = []) {+ let opened = start;+ for (const id of ids) opened = keepOpenedChats(touchOpenedChat(opened, id), running);+ return opened;+}++const ids = (n: number, prefix = "c") => Array.from({ length: n }, (_, i) => `${prefix}${i}`);++test("the cap is 20 and the most recently opened chat comes first", () => {+ assert.equal(OPENED_CHATS_LIMIT, 20);+ assert.deepEqual(open(["a", "b", "c"]), ["c", "b", "a"]);+});++test("opening a 21st chat evicts the least recently opened one", () => {+ const opened = open(ids(21));+ assert.equal(opened.length, 20);+ assert.equal(opened[0], "c20");+ assert.ok(!opened.includes("c0"), "the oldest opened chat is evicted");+ assert.ok(opened.includes("c1"));+});++test("reopening a chat moves it to the front instead of evicting it", () => {+ const opened = open(["c0"], none, open(ids(20)));+ assert.equal(opened.length, 20);+ assert.equal(opened[0], "c0");+ // the next new chat evicts c1, now the least recently opened, not c0+ const next = open(["new"], none, opened);+ assert.ok(next.includes("c0"));+ assert.ok(!next.includes("c1"));+});++test("a running chat is never evicted; the next oldest idle one goes instead", () => {+ const running = new Set(["c0"]);+ const opened = open(ids(25), running);+ assert.equal(opened.length, 20);+ assert.ok(opened.includes("c0"), "the running chat stays");+ for (const gone of ["c1", "c2", "c3", "c4", "c5"]) assert.ok(!opened.includes(gone), gone);+});++test("with more than 20 running chats the list passes the cap rather than evict one", () => {+ const all = ids(23);+ const running = new Set(all);+ const opened = open(all, running);+ assert.equal(opened.length, 23);+ const after = open(["x"], new Set(all.slice(1)), opened);+ assert.ok(!after.includes("c0"), "the stopped chat is the first evicted");+ assert.equal(after[0], "x", "the chat just opened is never the one evicted");+ assert.equal(after.length, 23);+});++test("eviction is deterministic: same opens, same running set, same list", () => {+ const sequence = [...ids(30), "c3", "c29", "c10", ...ids(5, "d")];+ const running = new Set(["c2", "c7"]);+ assert.deepEqual(open(sequence, running), open(sequence, running));+});++/** The hook under a minimal React: state cells by call order, effects re-run on changed deps. */+function renderHook(slots: Record<string, unknown>) {+ (globalThis as { window?: unknown }).window = new EventTarget();+ const cells: { v: unknown }[] = [];+ let at = 0;+ const cell = <T>(init: () => T): { v: T } => {+ const i = at++;+ if (!(i in cells)) cells[i] = { v: init() };+ return cells[i] as { v: T };+ };+ const react = {+ useRef: (value: unknown) => cell(() => ({ current: value })).v,+ useReducer: () => [0, () => {}],+ useMemo: (fn: () => unknown) => fn(),+ useEffect: (fn: () => unknown, deps?: unknown[]) => {+ const c = cell<{ deps?: unknown[] } | null>(() => null);+ const same = c.v?.deps && deps && deps.every((dep, k) => Object.is(dep, c.v?.deps?.[k]));+ if (!same) {+ c.v = { deps };+ fn();+ }+ },+ };+ const rows = (prefix: string, n: number) =>+ Array.from({ length: n }, (_, i) => ({+ id: `${prefix}${i}`,+ type: "single",+ threadIds: [`${prefix}${i}`],+ title: "",+ createdAt: 0,+ updatedAt: n - i,+ }));+ const mod = loadWithStubs<typeof RowPagesModule>(+ new URL("../src/features/chat/hooks/use-sidebar-row-pages.ts", import.meta.url),+ {+ react,+ "@/features/auth": { getAuthSessionEpoch: () => 0 },+ "../api/chat-api": {+ CHAT_HISTORY_UPDATED_EVENT: "history",+ fetchSidebarRows: async (request: { includeIds: string[] | null; limit: number }) => {+ const list = request.includeIds+ ? rows("old", 1).map((row) => ({ ...row, id: request.includeIds?.[0] ?? row.id }))+ : rows("r", request.limit);+ return { rows: list, nextCursor: null, hasMore: false, total: list.length };+ },+ },+ "../utils/chat-history-storage": {+ listStoredChatThreads: async () => [],+ settleLegacyChatImport: async () => true,+ },+ "../utils/repair-legacy-chat-titles": { repairLegacyChatTitles: async () => 0 },+ "../utils/chat-thread-tombstones": { isChatThreadDeleted: () => false },+ "../utils/sidebar-row-pager": { RowPager },+ "../utils/sidebar-row-query": rowQuery,+ "./use-chat-sidebar-items": { groupThreads: () => [] },+ },+ );+ return () => {+ at = 0;+ return mod.useSidebarRowPages(slots as never, true);+ };+}++test("an opened chat past Recents' loaded page is looked up but never listed", async () => {+ const render = renderHook({+ recents: { query: rowQuery.sidebarRowQuery({}) },+ active: {+ query: rowQuery.sidebarRowQuery({ includeIds: ["deep-chat"] }),+ initialLimit: Number.MAX_SAFE_INTEGER,+ lookupOnly: true,+ },+ });+ render();+ await new Promise((resolve) => setTimeout(resolve, 20));+ const { items, listedItems } = render();+ assert.ok(items.some((row) => row.id === "deep-chat"), "lookups see the opened chat");+ assert.ok(!listedItems.some((row) => row.id === "deep-chat"), "no list draws it out of order");+ assert.deepEqual(+ listedItems.map((row) => row.id),+ Array.from({ length: 20 }, (_, i) => `r${i}`),+ );+});diff --git a/studio/frontend/tests/sidebar-row-drag.test.ts b/studio/frontend/tests/sidebar-row-drag.test.tsindex 3ecc5400b0c..2c168001b29 100644--- a/studio/frontend/tests/sidebar-row-drag.test.ts+++ b/studio/frontend/tests/sidebar-row-drag.test.ts@@ -1088,7 +1088,7 @@ test("a drop that moves writes its slot and takes the pin off after the move", (APP_SIDEBAR.indexOf("function commitDrop("),APP_SIDEBAR.indexOf("function dropCueClass("),);- assert.equal((commit.match(/setManualOrder\(/g) ?? []).length, 1);+ assert.equal((commit.match(/writeManualOrder\(/g) ?? []).length, 1);// A slow move can be overtaken by a reorder of the list it lands in. The slot is re-aimed at// the list as it stands then, not written from the snapshot taken at the drop.assert.match(diff --git a/studio/frontend/tests/sidebar-row-menu-and-pinned-folders.test.ts b/studio/frontend/tests/sidebar-row-menu-and-pinned-folders.test.tsindex eafa7acc874..f203a917e10 100644--- a/studio/frontend/tests/sidebar-row-menu-and-pinned-folders.test.ts+++ b/studio/frontend/tests/sidebar-row-menu-and-pinned-folders.test.ts@@ -188,7 +188,7 @@ test("a pinned folder is a row of Pinned, not of Projects", () => {// Projects renders what is left, in its own.assert.match(APP_SIDEBAR,- /visibleProjectRecords\.map\(\(project\) =>\n\s*renderProjectFolderRow\(project, \{\n\s*scope: PROJECT_ORDER_SCOPE,\n\s*orderedIds: projectRowIds,/,+ /items=\{visibleProjectRecords\}[\s\S]{0,200}renderItem=\{\(project\) =>\n\s*renderProjectFolderRow\(project, \{\n\s*scope: PROJECT_ORDER_SCOPE,\n\s*orderedIds: projectRowIds,/,);// And the Projects list no longer carries them, so a folder is never in both sections.const records = APP_SIDEBAR.slice(@@ -389,7 +389,7 @@ test("a pinned folder in one list is a way in, not a second copy of its chats",);// Everything a folder draws under itself hangs off that one flag.for (const gated of [- /\{expanded &&\n\s*visibleChats\.map\(/,+ /\{expanded && \(\n\s*<ProgressiveRows\n\s*items=\{visibleChats\}/,/\{expanded && projectChats\.length === 0 && \(/,/\{expanded &&\n\s*projectChats\.length > PROJECT_CHAT_LIMIT && \(/,]) {diff --git a/studio/frontend/tests/sidebar-row-pages-races.test.ts b/studio/frontend/tests/sidebar-row-pages-races.test.tsnew file mode 100644index 00000000000..88cd3fa3115--- /dev/null+++ b/studio/frontend/tests/sidebar-row-pages-races.test.ts@@ -0,0 +1,201 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import test from "node:test";+import type * as RowPagesModule from "../src/features/chat/hooks/use-sidebar-row-pages.ts";+import { RowPager } from "../src/features/chat/utils/sidebar-row-pager.ts";+import * as rowQuery from "../src/features/chat/utils/sidebar-row-query.ts";+import { loadWithStubs } from "./helpers/module-stubs.ts";++type Row = { id: string; projectId?: string | null; updatedAt: number };+const tick = () => new Promise((resolve) => setTimeout(resolve, 0));++function offsetPage<T>(list: T[], limit: number, cursor: string | null) {+ const offset = cursor ? Number(cursor) : 0;+ const end = offset + limit;+ return {+ rows: list.slice(offset, end),+ nextCursor: end < list.length ? String(end) : null,+ hasMore: end < list.length,+ total: list.length,+ };+}++test("a retry after a failed query change re-reads the new query from the top", async () => {+ const lists: Record<string, Row[]> = {+ a: Array.from({ length: 100 }, (_, i) => ({ id: `a${i}`, updatedAt: 0 })),+ b: Array.from({ length: 100 }, (_, i) => ({ id: `b${i}`, updatedAt: 0 })),+ };+ let failNext = false;+ const pager = new RowPager<string, Row>(+ async (query, limit, cursor) => {+ if (failNext) {+ failNext = false;+ throw new Error("offline");+ }+ return offsetPage(lists[query], limit, cursor);+ },+ "a",+ "a",+ { initialLimit: 20, pageSize: 20 },+ );+ await pager.start();+ failNext = true;+ await pager.setQuery("b", "b");+ assert.ok(pager.getState().error);+ await pager.retry();+ const ids = pager.getState().rows.map((row) => row.id);+ assert.equal(ids[0], "b0");+ assert.ok(ids.every((id) => id.startsWith("b")), "rows mix the old and new query");+});++test("a read replaced by a refresh stops asking the server", async () => {+ const all = Array.from({ length: 2000 }, (_, i) => ({ id: `c${i}`, updatedAt: 0 }));+ const calls: (string | null)[] = [];+ const pager = new RowPager<unknown, Row>(+ async (_query, limit, cursor) => {+ calls.push(cursor);+ await tick();+ return offsetPage(all, limit, cursor);+ },+ {},+ "q",+ { initialLimit: 20, pageSize: 20 },+ );+ await pager.start();+ calls.length = 0;+ const selectAll = pager.loadAll();+ await tick();+ void pager.refresh();+ await selectAll;+ assert.ok(calls.length <= 12, `the list was walked twice: ${calls.length} requests`);+});++/** The hook under a minimal React: state cells by call order, effects re-run on changed deps. */+function hookHarness(fetchSidebarRows: (request: { limit: number; cursor: string | null }) => unknown) {+ (globalThis as { window?: unknown }).window = new EventTarget();+ const cells: { v: unknown }[] = [];+ let at = 0;+ const cell = <T>(init: () => T): { v: T } => {+ const i = at++;+ if (!(i in cells)) cells[i] = { v: init() };+ return cells[i] as { v: T };+ };+ const react = {+ useRef: (value: unknown) => cell(() => ({ current: value })).v,+ useReducer: () => [0, () => {}],+ useMemo: (fn: () => unknown) => fn(),+ useEffect: (fn: () => unknown, deps?: unknown[]) => {+ const c = cell<{ deps?: unknown[] } | null>(() => null);+ const same =+ c.v?.deps && deps && deps.every((dep, k) => Object.is(dep, c.v?.deps?.[k]));+ if (!same) {+ c.v = { deps };+ fn();+ }+ },+ };+ const mod = loadWithStubs<typeof RowPagesModule>(+ new URL("../src/features/chat/hooks/use-sidebar-row-pages.ts", import.meta.url),+ {+ react,+ "@/features/auth": { getAuthSessionEpoch: () => 0 },+ "../api/chat-api": { CHAT_HISTORY_UPDATED_EVENT: "history", fetchSidebarRows },+ "../utils/chat-history-storage": {+ listStoredChatThreads: async () => [],+ settleLegacyChatImport: async () => true,+ },+ "../utils/repair-legacy-chat-titles": { repairLegacyChatTitles: async () => 0 },+ "../utils/chat-thread-tombstones": { isChatThreadDeleted: () => false },+ "../utils/sidebar-row-pager": { RowPager },+ "../utils/sidebar-row-query": rowQuery,+ "./use-chat-sidebar-items": { groupThreads: () => [] },+ },+ );+ const slots = { recents: { query: rowQuery.sidebarRowQuery({}) } };+ const render = () => {+ at = 0;+ return mod.useSidebarRowPages(slots as never, true).lists.recents;+ };+ return { render };+}++function sidebarRows(ids: string[]) {+ return ids.map((id) => ({+ id,+ type: "single",+ threadIds: [id],+ title: "",+ createdAt: 0,+ updatedAt: 0,+ }));+}++test("a history change during a list's first load is not lost", async () => {+ let server = ["old"];+ let release = () => {};+ let gate: Promise<void> | null = new Promise((resolve) => {+ release = resolve;+ });+ const { render } = hookHarness(async () => {+ const snapshot = sidebarRows(server);+ if (gate) await gate;+ return { rows: snapshot, nextCursor: null, hasMore: false, total: snapshot.length };+ });+ render();+ await tick();+ server = ["new", "old"];+ window.dispatchEvent(new Event("history"));+ await new Promise((resolve) => setTimeout(resolve, 350));+ gate = null;+ release();+ await new Promise((resolve) => setTimeout(resolve, 50));+ assert.deepEqual(+ render().rows.map((row) => row.id),+ ["new", "old"],+ );+});++test("a scroll after one failed page asks for that page again", async () => {+ let failNext = false;+ const { render } = hookHarness(async ({ limit, cursor }) => {+ if (failNext) {+ failNext = false;+ throw new Error("offline");+ }+ const offset = cursor ? Number(cursor) : 0;+ const end = offset + limit;+ return {+ rows: sidebarRows(Array.from({ length: limit }, (_, i) => `c${offset + i}`)),+ nextCursor: String(end),+ hasMore: end < 100,+ total: 100,+ };+ });+ render();+ await new Promise((resolve) => setTimeout(resolve, 10));+ failNext = true;+ await render().loadMore();+ await render().loadMore();+ assert.equal(render().rows.length, 40);+});++test("Select All's full read rejects when a page fails, so no partial list is taken for all", async () => {+ let calls = 0;+ const { render } = hookHarness(async ({ limit, cursor }) => {+ calls += 1;+ if (calls === 2) throw new Error("offline");+ const offset = cursor ? Number(cursor) : 0;+ const end = offset + limit;+ return {+ rows: sidebarRows(Array.from({ length: limit }, (_, i) => `c${offset + i}`)),+ nextCursor: String(end),+ hasMore: end < 100,+ total: 100,+ };+ });+ render();+ await new Promise((resolve) => setTimeout(resolve, 10));+ await assert.rejects(render().loadAll());+});diff --git a/studio/frontend/tests/sidebar-server-paging.test.ts b/studio/frontend/tests/sidebar-server-paging.test.tsnew file mode 100644index 00000000000..ba68b78ce45--- /dev/null+++ b/studio/frontend/tests/sidebar-server-paging.test.ts@@ -0,0 +1,872 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+++import assert from "node:assert/strict";+import { readFileSync } from "node:fs";+import test from "node:test";+import * as React from "react";+import { renderToStaticMarkup } from "react-dom/server";+import * as jsxRuntime from "react/jsx-runtime";+import type * as ProgressiveRowsModule from "../src/components/progressive-rows.tsx";+import type * as RowPagesModule from "../src/features/chat/hooks/use-sidebar-row-pages.ts";+import {+ type RowPageResult,+ RowPager,+} from "../src/features/chat/utils/sidebar-row-pager.ts";+import * as rowQuery from "../src/features/chat/utils/sidebar-row-query.ts";+import { readSrc } from "./helpers/kit.ts";+import { loadWithStubs } from "./helpers/module-stubs.ts";++const {+ applySidebarRowQuery,+ mergeManualOrderTail,+ sidebarRowQuery,+ sidebarRowQueryKey,+} = rowQuery;++type Row = { id: string; projectId?: string | null; updatedAt: number };++function rows(count: number, prefix = "c"): Row[] {+ return Array.from({ length: count }, (_, i) => ({+ id: `${prefix}${i}`,+ updatedAt: count - i,+ }));+}++function fakeServer(all: () => Row[]) {+ const calls: { limit: number; cursor: string | null }[] = [];+ const fetchPage = async (+ _query: unknown,+ limit: number,+ cursor: string | null,+ ): Promise<RowPageResult<Row>> => {+ calls.push({ limit, cursor });+ const list = all();+ const offset = cursor ? Number(cursor) : 0;+ const end = offset + limit;+ return {+ rows: list.slice(offset, end),+ nextCursor: end < list.length ? String(end) : null,+ hasMore: end < list.length,+ total: list.length,+ };+ };+ return { calls, fetchPage };+}++function pager(+ fetchPage: ReturnType<typeof fakeServer>["fetchPage"],+ initialLimit = 20,+) {+ return new RowPager<unknown, Row>(fetchPage, {}, "q", {+ initialLimit,+ pageSize: 20,+ });+}++test("the first window asks for one page, and each further page for twenty", async () => {+ const server = fakeServer(() => rows(1000));+ const list = pager(server.fetchPage);+ await list.start();+ assert.equal(list.getState().rows.length, 20);+ assert.equal(list.getState().hasMore, true);+ assert.equal(list.getState().total, 1000);+ await list.loadMore();+ await list.loadMore();+ assert.deepEqual(server.calls, [+ { limit: 20, cursor: null },+ { limit: 20, cursor: "20" },+ { limit: 20, cursor: "40" },+ ]);+ assert.equal(list.getState().rows.length, 60);+ assert.equal(new Set(list.getState().rows.map((r) => r.id)).size, 60);+});++test("a list that fits in one page is exhausted and never asks again", async () => {+ for (const count of [0, 1, 19, 20]) {+ const server = fakeServer(() => rows(count));+ const list = pager(server.fetchPage);+ await list.start();+ assert.equal(list.getState().rows.length, count);+ assert.equal(list.getState().hasMore, false);+ await list.loadMore();+ assert.equal(server.calls.length, 1, `${count} rows`);+ }+});++test("twenty-one rows take exactly two pages; forty-one take three", async () => {+ for (const [count, pages] of [+ [21, 2],+ [40, 2],+ [41, 3],+ ] as const) {+ const server = fakeServer(() => rows(count));+ const list = pager(server.fetchPage);+ await list.start();+ while (list.getState().hasMore) await list.loadMore();+ assert.equal(list.getState().rows.length, count);+ assert.equal(server.calls.length, pages, `${count} rows`);+ }+});++test("a refresh re-reads the loaded window from the top in one request", async () => {+ let data = rows(500);+ const server = fakeServer(() => data);+ const list = pager(server.fetchPage);+ await list.start();+ await list.loadMore();+ await list.loadMore();+ server.calls.length = 0;+ data = [+ { id: "c300", updatedAt: 10_000 },+ ...data.filter((r) => r.id !== "c300"),+ ];+ await list.refresh();+ assert.deepEqual(server.calls, [{ limit: 60, cursor: null }]);+ assert.equal(list.getState().rows.length, 60);+ assert.equal(list.getState().rows[0].id, "c300");+});++test("a refresh that reads the same rows renders nothing; one that reads a change renders once", async () => {+ let data = rows(100);+ const server = fakeServer(() => data.map((row) => ({ ...row })));+ const list = pager(server.fetchPage);+ await list.start();+ const before = list.getState().rows;+ let renders = 0;+ list.subscribe(() => {+ renders += 1;+ });+ await list.refresh();+ assert.equal(renders, 0);+ assert.equal(list.getState().rows, before, "an unchanged window keeps its identity");+ data = [{ id: "new", updatedAt: 1_000 }, ...data];+ await list.refresh();+ assert.equal(renders, 1);+ assert.equal(list.getState().rows[0].id, "new");+});++test("a page asked for while a history refresh is in flight loads after it, not never", async () => {+ const server = fakeServer(() => rows(200));+ let gate: Promise<void> | null = null;+ let release = () => {};+ const list = pager(async (query, limit, cursor) => {+ if (gate) await gate;+ return server.fetchPage(query, limit, cursor);+ });+ await list.start();+ gate = new Promise<void>((resolve) => {+ release = resolve;+ });+ const refresh = list.refresh();+ const more = list.loadMore();+ gate = null;+ release();+ await Promise.all([refresh, more]);+ assert.equal(list.getState().rows.length, 40);+ // two scrolls during one page still ask for one page each, never more+ await Promise.all([list.loadMore(), list.loadMore()]);+ assert.equal(list.getState().rows.length, 60);+});++test("a window past one request's cap is read in steps", async () => {+ const server = fakeServer(() => rows(1000));+ const list = new RowPager<unknown, Row>(server.fetchPage, {}, "q", {+ initialLimit: 20,+ pageSize: 20,+ maxRequestLimit: 200,+ });+ await list.loadAll();+ assert.equal(list.getState().rows.length, 1000);+ assert.equal(list.getState().hasMore, false);+ assert.ok(server.calls.every((call) => call.limit <= 200));+ server.calls.length = 0;+ await list.refresh();+ assert.deepEqual(+ server.calls.map((call) => call.limit),+ [200, 200, 200, 200, 200],+ );+});++test("a page landing after the query changed is dropped", async () => {+ let release: (() => void) | undefined;+ let slow = false;+ const answer = (prefix: string, limit: number): RowPageResult<Row> => ({+ rows: rows(limit, prefix),+ nextCursor: "next",+ hasMore: true,+ });+ const fetchPage = async (query: unknown, limit: number) => {+ const prefix = (query as { name: string }).name;+ if (slow) {+ await new Promise<void>((resolve) => {+ release = resolve;+ });+ }+ return answer(prefix, limit);+ };+ const list = new RowPager<unknown, Row>(fetchPage, { name: "a" }, "a", {+ initialLimit: 20,+ pageSize: 20,+ });+ await list.start();+ slow = true;+ const late = list.loadMore();+ slow = false;+ await list.setQuery({ name: "b" }, "b");+ release?.();+ await late;+ assert.ok(list.getState().rows.every((row) => row.id.startsWith("b")));+ assert.equal(list.getState().rows.length, 20);+});++test("rows a page repeats are kept once", async () => {+ const fetchPage = async (+ _q: unknown,+ limit: number,+ cursor: string | null,+ ) => ({+ rows: cursor+ ? [{ id: "c19", updatedAt: 0 }, ...rows(limit, "d")]+ : rows(limit),+ nextCursor: "x",+ hasMore: true,+ });+ const list = new RowPager<unknown, Row>(fetchPage, {}, "q", {+ initialLimit: 20,+ pageSize: 20,+ });+ await list.start();+ await list.loadMore();+ const ids = list.getState().rows.map((row) => row.id);+ assert.equal(ids.length, new Set(ids).size);+ assert.equal(ids.length, 40);+});++test("a cursor the server rejects re-reads from the top through the page it asked for", async () => {+ const server = fakeServer(() => rows(100));+ let reject = false;+ const fetchPage: typeof server.fetchPage = async (q, limit, cursor) => {+ if (reject && cursor) {+ reject = false;+ throw Object.assign(new Error("stale"), { cursorRejected: true });+ }+ return server.fetchPage(q, limit, cursor);+ };+ const list = pager(fetchPage);+ await list.start();+ await list.loadMore();+ reject = true;+ server.calls.length = 0;+ await list.loadMore();+ assert.equal(list.getState().error, null);+ assert.equal(list.getState().rows.length, 60);+ assert.deepEqual(server.calls, [{ limit: 60, cursor: null }]);+});++test("a failed page stops paging until retried, and keeps the rows it had", async () => {+ let fail = false;+ const server = fakeServer(() => rows(100));+ const fetchPage: typeof server.fetchPage = async (q, limit, cursor) => {+ if (fail) throw new Error("offline");+ return server.fetchPage(q, limit, cursor);+ };+ const list = pager(fetchPage);+ await list.start();+ fail = true;+ await list.loadMore();+ assert.equal(list.getState().rows.length, 20);+ assert.ok(list.getState().error);+ fail = false;+ const before = server.calls.length;+ await list.loadMore();+ assert.equal(server.calls.length, before, "an errored list kept asking");+ await list.retry();+ assert.equal(list.getState().rows.length, 40);+ assert.equal(list.getState().error, null);+});++test("the full-list fallback filters and orders like the server", () => {+ const all: Row[] = [+ { id: "a", updatedAt: 9, projectId: null },+ { id: "b", updatedAt: 8, projectId: "p" },+ { id: "c", updatedAt: 7, projectId: null },+ { id: "d", updatedAt: 6, projectId: "p" },+ { id: "e", updatedAt: 5, projectId: null },+ ];+ const ids = (query: Partial<rowQuery.SidebarRowQuery>) =>+ applySidebarRowQuery(all, sidebarRowQuery(query)).map((row) => row.id);+ assert.deepEqual(ids({}), ["a", "b", "c", "d", "e"]);+ assert.deepEqual(ids({ unfiledOnly: true, excludeIds: ["c"] }), ["a", "e"]);+ assert.deepEqual(ids({ projectId: "p" }), ["b", "d"]);+ assert.deepEqual(ids({ includeIds: ["e", "a"] }), ["a", "e"]);+ assert.deepEqual(ids({ sort: "manual", manualOrder: ["d", "a"] }), [+ "b",+ "c",+ "e",+ "d",+ "a",+ ]);+ assert.deepEqual(ids({ sort: "updated", manualOrder: ["e"] }), [+ "a",+ "b",+ "c",+ "d",+ "e",+ ]);+});++test("the same lists are the same query however the stores ordered them", () => {+ assert.equal(+ sidebarRowQueryKey(sidebarRowQuery({ excludeIds: ["b", "a", "b"] })),+ sidebarRowQueryKey(sidebarRowQuery({ excludeIds: ["a", "b"] })),+ );+});++test("a manual order written from loaded rows keeps every unloaded row after them", () => {+ assert.deepEqual(+ mergeManualOrderTail(["c", "a", "b"], ["a", "b", "c", "d", "e"]),+ ["c", "a", "b", "d", "e"],+ );+ assert.deepEqual(mergeManualOrderTail(["x"], []), ["x"]);+});++function loadRowPages(options: {+ server: "paged" | "old";+ legacyDone?: boolean;+ /** Answers the paged route instead of the default endless single-row pages. */+ pages?: (cursor: string | null) => unknown;+}) {+ const requests: unknown[] = [];+ const repaired: { id: string; title: string }[][] = [];+ let fullReads = 0;+ const threads = Array.from({ length: 45 }, (_, i) => ({+ id: `t${i}`,+ title: `chat ${i}`,+ modelType: "base",+ archived: i % 9 === 8,+ createdAt: 1,+ updatedAt: 1000 - i,+ projectId: i % 5 === 4 ? "p" : null,+ }));+ const groupThreads = (list: typeof threads, archived = false) =>+ list+ .filter((t) => Boolean(t.archived) === archived)+ .map((t) => ({+ type: "single" as const,+ id: t.id,+ threadIds: [t.id],+ title: t.title,+ createdAt: t.createdAt,+ updatedAt: t.updatedAt,+ projectId: t.projectId,+ }));+ const mod = loadWithStubs<typeof RowPagesModule>(+ new URL(+ "../src/features/chat/hooks/use-sidebar-row-pages.ts",+ import.meta.url,+ ),+ {+ react: React,+ "@/features/auth": { getAuthSessionEpoch: () => 0 },+ "../api/chat-api": {+ CHAT_HISTORY_UPDATED_EVENT: "x",+ fetchSidebarRows: async (request: {+ limit: number;+ cursor: string | null;+ }) => {+ requests.push(request);+ if (options.server === "old") return null;+ if (options.pages) return options.pages(request.cursor);+ const offset = request.cursor ? Number(request.cursor) : 0;+ const end = offset + request.limit;+ return {+ rows: [+ {+ id: `s${offset}`,+ type: "single",+ threadIds: ["gone"],+ title: "",+ createdAt: 1,+ updatedAt: 1,+ },+ ],+ nextCursor: String(end),+ hasMore: true,+ total: 99,+ };+ },+ },+ "../utils/chat-history-storage": {+ listStoredChatThreads: async () => {+ fullReads += 1;+ return threads;+ },+ settleLegacyChatImport: async () => options.legacyDone ?? true,+ },+ "../utils/repair-legacy-chat-titles": {+ repairLegacyChatTitles: async (list: { id: string; title: string }[]) => {+ repaired.push(list);+ return 0;+ },+ },+ "../utils/chat-thread-tombstones": {+ isChatThreadDeleted: (id: string) => id === "gone",+ },+ "../utils/sidebar-row-pager": { RowPager },+ "../utils/sidebar-row-query": rowQuery,+ "./use-chat-sidebar-items": { groupThreads },+ },+ );+ return { mod, requests, repaired, fullReads: () => fullReads };+}++test("an old server is answered from the full list, sliced in pages, read once", async () => {+ const { mod, requests, fullReads } = loadRowPages({ server: "old" });+ const query = sidebarRowQuery({ unfiledOnly: true });+ const first = await mod.fetchSidebarRowPage(query, 20, null);+ assert.equal(requests.length, 1, "the paged route was not tried first");+ assert.equal(first.rows.length, 20);+ assert.ok(first.rows.every((row) => !row.projectId));+ assert.equal(first.hasMore, true);+ const second = await mod.fetchSidebarRowPage(query, 20, first.nextCursor);+ assert.equal(requests.length, 1);+ assert.equal(+ new Set([...first.rows, ...second.rows].map((r) => r.id)).size,+ first.rows.length + second.rows.length,+ );+ assert.equal(first.total, 32);+ assert.equal(second.hasMore, false);+ assert.equal(fullReads(), 1, "each page re-read the full list");+});++test("an old server's folders take their activity from the full list the lists already read", async () => {+ const { mod, fullReads } = loadRowPages({ server: "old" });+ await mod.fetchSidebarRowPage(sidebarRowQuery({}), 20, null);+ // t4 is the newest unarchived chat in "p"; t44 is archived and does not count+ assert.deepEqual([...(await mod.readFullListProjectActivity())], [["p", 996]]);+ assert.equal(fullReads(), 1, "the folder activity read the history again");+ const projects = readSrc("features/chat/hooks/use-chat-projects.ts");+ assert.match(projects, /if \(projects\.every\(\(project\) => "lastActivityAt" in project\)\) return projects;/);+ assert.match(projects, /nextProjects = Array\.isArray\(next\) \? await withChatActivity\(next\) : \[\];/);+});++test("until the browser-history import is done, the sidebar lists through the full list", async () => {+ const { mod, requests } = loadRowPages({+ server: "paged",+ legacyDone: false,+ });+ const page = await mod.fetchSidebarRowPage(sidebarRowQuery({}), 20, null);+ assert.equal(requests.length, 0);+ assert.equal(page.rows.length, 20);+});++test("a paged server's rows pass through, minus chats already deleted here", async () => {+ const { mod, requests } = loadRowPages({ server: "paged" });+ const page = await mod.fetchSidebarRowPage(sidebarRowQuery({}), 20, null);+ assert.equal(requests.length, 1);+ assert.deepEqual(page.rows, []);+ assert.equal(page.hasMore, true);+ assert.equal(page.nextCursor, "20");+ assert.equal(page.total, 99);+});++test("legacy titles are repaired from the rows a page loads, and from the fallback's full list", async () => {+ const paged = loadRowPages({ server: "paged" });+ await paged.mod.fetchSidebarRowPage(sidebarRowQuery({}), 20, null);+ assert.deepEqual(paged.repaired, [[{ id: "gone", title: "" }]]);+ const old = loadRowPages({ server: "old" });+ await old.mod.fetchSidebarRowPage(sidebarRowQuery({}), 20, null);+ await new Promise((resolve) => setTimeout(resolve, 0));+ assert.equal(old.repaired.length, 1);+ assert.equal(old.repaired[0].length, 45);+});++const { ProgressiveRows } = loadWithStubs<typeof ProgressiveRowsModule>(+ new URL("../src/components/progressive-rows.tsx", import.meta.url),+ { react: React, "react/jsx-runtime": jsxRuntime },+);++function renderRows(count: number, hasMore: boolean): string {+ const items = Array.from({ length: count }, (_, i) => ({ id: `chat-${i}` }));+ return renderToStaticMarkup(+ React.createElement(+ "ul",+ null,+ React.createElement(ProgressiveRows<{ id: string }>, {+ items,+ pageSize: 20,+ hasMore,+ onReachEnd: () => undefined,+ renderItem: ({ id }) => React.createElement("li", { "data-row": id }),+ end: React.createElement("li", { "data-end": "" }),+ }),+ ),+ );+}++test("a loaded page with more on the server ends in the sentinel, not the end strip", () => {+ const html = renderRows(20, true);+ assert.equal(html.match(/data-row=/g)?.length, 20);+ assert.doesNotMatch(html, /data-end/);+ assert.match(html.slice(html.lastIndexOf("<li")), /^<li aria-hidden="true"/);+ assert.match(renderRows(20, false), /<li data-end=""><\/li><\/ul>$/);+});++test("a server page is asked for only after a scroll, one page per scroll", () => {+ const src = readSrc("components/progressive-rows.tsx");+ const advance = src.slice(+ src.indexOf("const advance = useCallback("),+ src.indexOf("// re-observe"),+ );+ assert.match(+ advance,+ /if \(!fetchMore \|\| fetchingRef\.current \|\| !root \|\| !armedRef\.current\) return;\n\s*armedRef\.current = false;/,+ );+ assert.ok(+ advance.indexOf("items.length > limit") < advance.indexOf("armedRef"),+ "local mounting waits for a scroll",+ );+ assert.doesNotMatch(src, /new WeakSet/);+ // a scroll while a page is in flight must not arm the next one+ assert.match(+ src,+ /\n(?:\s*\/\/.*\n)?\s*if \(fetchingRef\.current \|\| performance\.now\(\) - drawnAtRef\.current < GESTURE_TAIL_MS\) return;\n\s*armedRef\.current = true;/,+ );+ assert.match(src, /const down = top > lastTop;\n\s*lastTop = top;\n\s*if \(!down\) return;/);+ assert.match(src, /event\.type === "wheel" && \(event as WheelEvent\)\.deltaY <= 0\) \{\n\s*return;/);+ // a touch arms only when the finger moves up (the list scrolls down), not on a swipe up or sideways+ assert.match(src, /const down = touchY !== null && y !== undefined && y < touchY;\n\s*if \(y !== undefined\) touchY = y;\n\s*if \(!down\) return;/);+ assert.match(src, /touchY = \(event as TouchEvent\)\.touches\[0\]\?\.clientY \?\? null;\n\s*return;/);+ assert.match(src, /fetchingRef\.current = false;\n\s*drawnAtRef\.current = performance\.now\(\);/);+ assert.match(+ src,+ /const ARMING_EVENTS = \["scroll", "wheel", "touchstart", "touchmove"\] as const;/,+ );+});++test("the sidebar reads every chat list through the pager, twenty rows at a time", () => {+ const src = readSrc("components/app-sidebar.tsx");+ assert.doesNotMatch(src, /useChatSidebarItems\(/);+ assert.match(src, /const RECENTS_PAGE_SIZE = SIDEBAR_PAGE_SIZE;/);+ assert.match(src, /= useSidebarRowPages\(\n\s*chatRowSlots,/);+ for (const list of [+ "pinnedRows",+ "rows",+ "visibleProjectRecords",+ "visibleChats",+ "runItems",+ "sortedRecentChatItems",+ ]) {+ assert.match(+ src,+ new RegExp(`<ProgressiveRows\\n\\s*items=\\{${list}\\}`),+ list,+ );+ }+ assert.equal(+ readSrc("features/chat/utils/sidebar-row-query.ts").includes(+ "SIDEBAR_PAGE_SIZE = 20",+ ),+ true,+ );+});++test("the thread-row lookup reads the key and the per-request cap the route defines", () => {+ const route = readFileSync(+ new URL("../../backend/routes/chat_history.py", import.meta.url),+ "utf8",+ );+ const client = readSrc("features/chat/api/chat-api.ts");+ assert.match(route, /rowIdByThreadId: dict\[str, str\]/);+ assert.match(client, /data\.rowIdByThreadId/);+ const serverCap = route.match(/_SIDEBAR_THREAD_ROWS_MAX = (\d+)/)?.[1];+ const clientCap = client.match(/const SIDEBAR_THREAD_ROWS_MAX = (\d+)/)?.[1];+ assert.ok(serverCap && clientCap, "both caps are declared");+ assert.ok(Number(clientCap) <= Number(serverCap), "client chunks within the route's cap");+});++test("a drag in a partly loaded list shows its order at once and saves it once every id is known", () => {+ const src = readSrc("components/app-sidebar.tsx");+ const body = src.slice(+ src.indexOf("const writeManualOrder = useCallback("),+ src.indexOf("// Escape leaves a selection"),+ );+ const partial = body.slice(body.indexOf("const saved ="));+ assert.ok(+ partial.indexOf("setPendingManualOrder(") < partial.indexOf("readAllSidebarRowIds("),+ );+ assert.equal(partial.slice(0, partial.indexOf("readAllSidebarRowIds(")).match(/setManualOrder\(scope/g)?.length ?? 0, 1);+ assert.doesNotMatch(partial, /settle\(shown\)/);+ assert.match(partial, /if \(attempt >= 2\) throw error;/);+ assert.match(partial, /setPendingManualOrder\(\(\{ \[scope\]: _dropped, \.\.\.rest \}\) => rest\);\n\s*toast\.error\("Couldn't save the new order"\);/);+ assert.match(src, /applyManualOrder\(items, displayManualOrder\[scope\]/);+});++test("the full-list fallback finds a compare row through one of its pane threads", () => {+ const rows = [+ { id: "pair", threadIds: ["pane-a", "pane-b"], updatedAt: 2 },+ { id: "solo", threadIds: ["solo"], updatedAt: 1 },+ ];+ const found = applySidebarRowQuery(rows, sidebarRowQuery({ includeIds: ["pane-b"] }));+ assert.deepEqual(found.map((row) => row.id), ["pair"]);+});++test("the last 20 opened chats and every running one load their rows, so row lookups never miss them", () => {+ const src = readSrc("components/app-sidebar.tsx");+ assert.match(+ src,+ /\[slotActiveId, \.\.\.openedChatIds, \.\.\.runningThreadKey\.split\("\\n"\)\]\.filter\(/,+ );+ assert.match(src, /isAssistantLocalThreadId\(id\),\n\s*\),\n\s*\),\n\s*\]\.sort\(\);/);+ assert.match(+ src,+ /keepOpenedChats\(touchOpenedChat\(opened, slotActiveId\), running\)/,+ );+ // looked up, never drawn: an opened chat past a list's loaded pages stays out of that list+ assert.match(src, /includeIds: lookupIds \}\),\n\s*initialLimit: Number\.MAX_SAFE_INTEGER,\n\s*lookupOnly: true,/);+ assert.match(src, /Boolean\(id\) && !isAssistantLocalThreadId\(id\)/);+ for (const list of ["recentChatItems", "pinnedChatItems"]) {+ const body = src.slice(src.indexOf(`const ${list} = useMemo(`), src.indexOf(`const ${list} = useMemo(`) + 400);+ assert.match(body, /listedChatItems/, list);+ assert.doesNotMatch(body, /allChatItems/, list);+ }+ assert.match(src, /for \(const \[projectId, items\] of listedChatsByProjectId\)/);+ assert.match(src, /for \(const item of listedChatItems\) \{\n\s*const sectionId = sectionByChatId\[item\.id\];/);+});++test("follow-ups that main got from the full list still happen with paged lists", () => {+ const src = readSrc("components/app-sidebar.tsx");+ // a chord at the last loaded row of any paged list reads its next page; a click there does not+ assert.match(src, /chordTargetRef\.current = item\?\.id \?\? null;\n\s*openChatItemById\(item\);/);+ assert.equal(src.match(/if \(!activeThreadId \|\| chordTargetRef\.current !== activeThreadId\) return;/g)?.length, 2);+ assert.match(src, /if \(list\.rows\[list\.rows\.length - 1\]\?\.id === activeThreadId\) void list\.loadMore\(\);/);+ assert.match(src, /window\.addEventListener\(CHAT_HISTORY_UPDATED_EVENT, probe\);/);+ const write = src.slice(src.indexOf("const writeManualOrder = useCallback("));+ assert.ok(write.indexOf("writes.set(scope, generation);") < write.indexOf("const slot ="));+ // project activity follows chat activity, not only project edits+ const projects = readSrc("features/chat/hooks/use-chat-projects.ts");+ assert.match(projects, /const onActivity = \(\) => onChatActivity\(\);/);+ assert.match(projects, /window\.addEventListener\(CHAT_HISTORY_UPDATED_EVENT, onActivity\);/);+ assert.match(projects, /window\.removeEventListener\(CHAT_HISTORY_UPDATED_EVENT, onActivity\);/);+ assert.match(projects, /if \(projectsLoaded && JSON\.stringify\(projects\) === JSON\.stringify\(cachedProjects\)\) return;/);+});++test("\"No chats yet\" asks for any chat, archived or in an unloaded folder, before it shows", () => {+ const src = readSrc("components/app-sidebar.tsx");+ assert.match(src, /\[sidebarRowQuery\(\{\}\), sidebarRowQuery\(\{ archived: true \}\)\]/);+ // and shows nothing until a probe has answered, so it never flashes while one is in flight+ assert.match(src, /useState<boolean \| null>\(null\);/);+ assert.match(src, /hasUnlistedChats === false && \(/);+ assert.match(src, /\.then\(\(\) => setSelectAllPending\(true\)\)\n\s*\.catch\(\(\) => undefined\);/);+});++test("a hidden or hand-ordered section still loads its newest chat, which orders Move to", () => {+ const src = readSrc("components/app-sidebar.tsx");+ assert.match(src, /\(hiddenSectionSet\.has\(section\.id\) \|\|\n\s*section\.sort === "manual" \|\|\n\s*ids\.length < members\.length\)/);+ assert.match(src, /slots\[`latest:\$\{section\.id\}`\] = \{\n\s*initialLimit: 1,\n\s*query: sidebarRowQuery\(\{ includeIds: members \}\),\n\s*lookupOnly: true,/);+ const latest = src.indexOf("slots[`latest:${section.id}`]");+ assert.ok(latest > 0 && latest < src.indexOf("if (ids.length === 0 || hiddenSectionSet.has(section.id)) continue;"));+ // it is never paged past that row+ assert.match(src, /name === "recents" \|\| name\.startsWith\("latest:"\) \|\| !list\.hasMore/);+});++test("a failed first read never shows \"No chats yet\"; it waits for a read that worked, as main did", () => {+ const src = readSrc("components/app-sidebar.tsx");+ assert.match(src, /const chatItemsLoaded = Boolean\(recentsPages\?\.loaded && !recentsPages\.error\);/);+});++test("a section's Mark all read and Archive act on the whole section, not its loaded pages", () => {+ const src = readSrc("components/app-sidebar.tsx");+ const menu = src.slice(src.indexOf("const unreadInSection"), src.indexOf("const unreadInSection") + 1600);+ assert.match(menu, /inSection\(unreadRowIds\[threadId\] \?\? threadId\)/);+ assert.match(menu, /onSelect=\{\(\) => clearThreadsUnread\(unreadInSection\)\}/);+ assert.match(menu, /sectionPages\?\.hasMore && sectionQuery \? await readAllSidebarRows\(sectionQuery\) : chats/);+});++test("only folder rows on screen read their chats", () => {+ const src = readSrc("components/app-sidebar.tsx");+ assert.match(src, /!collapsedProjectIds\.has\(project\.id\) && mountedFolderIds\.has\(project\.id\)/);+ assert.match(src, /<MountedFolder id=\{project\.id\} onChange=\{setFolderMounted\} \/>/);+});++test("a list that shrank while a page was read frees its paging latch", () => {+ const src = readSrc("components/progressive-rows.tsx");+ assert.match(src, /if \(itemCountRef\.current <= fetchFromRef\.current\) fetchingRef\.current = false;/);+});++test("a running Compare pane keeps its opened pair row, which is opened by pair id", () => {+ const src = readSrc("components/app-sidebar.tsx");+ assert.match(src, /if \(item\.threadIds\?\.some\(\(id\) => running\.has\(id\)\)\) rows\.add\(item\.id\);/);+ assert.match(src, /const running = new Set\(runningRowKey\.split\("\\n"\)\);\n\s*setOpenedChatIds\(\(opened\) => keepOpenedChats\(touchOpenedChat\(opened, slotActiveId\), running\)\);/);+});++test("a page emptied by chats deleted here does not end the list: the next page still loads", async () => {+ const fetchPage = async (_q: unknown, limit: number, cursor: string | null) => {+ const at = cursor ? Number(cursor) : 0;+ return {+ rows: at === 0 ? [] : rows(limit, `p${at}-`),+ nextCursor: String(at + limit),+ hasMore: at < 60,+ };+ };+ const list = new RowPager<unknown, Row>(fetchPage, {}, "q", { initialLimit: 20, pageSize: 20 });+ await list.start();+ assert.equal(list.getState().rows.length, 20, "the emptied first page ended the read");+ assert.equal(list.getState().rows[0].id, "p20-0");+ const stuck = new RowPager<unknown, Row>(+ async () => ({ rows: [], nextCursor: "same", hasMore: true }),+ {},+ "q",+ { initialLimit: 20, pageSize: 20 },+ );+ await stuck.start();+ assert.equal(stuck.getState().rows.length, 0);+});++test("a manual list still paging holds back folders and pages ranked past its last loaded chat", () => {+ const { holdRowsPastLoadedChats } = rowQuery;+ const order = ["c1", "f1", "c2", "c3", "f2", "c4", "p1"];+ const shown = [+ { id: "f0", kind: "project" }, // unranked folders lead+ { id: "c1", kind: "chat" },+ { id: "f1", kind: "project" },+ { id: "c2", kind: "chat" },+ { id: "f2", kind: "project" },+ { id: "p1", kind: "page" },+ ];+ // c3 and c4 are not loaded yet: f2 and p1 come after c3 on main, so they wait+ assert.deepEqual(+ holdRowsPastLoadedChats(shown, order, true).map((row) => row.id),+ ["f0", "c1", "f1", "c2"],+ );+ assert.deepEqual(holdRowsPastLoadedChats(shown, order, false).map((row) => row.id), shown.map((row) => row.id));+ assert.deepEqual(+ holdRowsPastLoadedChats([{ id: "f0", kind: "project" }, { id: "u1", kind: "chat" }, { id: "p0", kind: "page" }], order, true).map((row) => row.id),+ ["f0", "u1"],+ );+});++test("a drag saved from a partly loaded manual list keeps held-back rows where they were", () => {+ const { settledManualOrder } = rowQuery;+ const current = ["c1", "f1", "c2", "c3", "f2", "c4"];+ const all = ["u1", "c1", "c2", "c3", "c4"]; // the server's chat order: unranked u1 first+ assert.deepEqual(settledManualOrder(["u1", "c2", "c1", "f1"], all, current), [+ "u1",+ "c2",+ "c1",+ "f1",+ "c3",+ "f2",+ "c4",+ ]);+ // an unloaded unranked chat (shown first on main) goes ahead of the saved tail, not after it+ assert.deepEqual(settledManualOrder(["c2", "c1"], ["u1", "u2", "c1", "c2", "c3"], ["c1", "c2", "c3"]), [+ "c2",+ "c1",+ "u1",+ "u2",+ "c3",+ ]);+});++test("a training refresh past one request re-reads from the top if the run count moved under it", () => {+ const src = readSrc("features/training/hooks/use-training-history-sidebar.ts");+ assert.match(src, /for \(let attempt = 0; attempt < 3; attempt\+\+\) \{/);+ assert.match(src, /shifted \|\|= result\.total !== firstTotal \|\| fresh\.length < result\.runs\.length;/);+ assert.match(src, /runs\.push\(\.\.\.fresh\);/);+ assert.match(src, /if \(!shifted\) break;/);+});++test("Pinned and manual sections hold rows past their last loaded chat, and a drag saves around them", () => {+ const src = readSrc("components/app-sidebar.tsx");+ assert.match(src, /holdRowsPastLoadedChats\(\n\s*applyManualOrder\(rows, order, \(row\) => row\.id\),\n\s*order,\n\s*Boolean\(pinnedHasMore\),/);+ assert.match(src, /holdRowsPastLoadedChats\(\n\s*applyManualOrder\(list, order, \(row\) => row\.id\),\n\s*order,\n\s*sectionHasMore\.has\(section\.id\),/);+ assert.match(src, /settle\(settledManualOrder\(ids, all, current\)\);/);+});++test("a page asked for just before a history refresh is read by that refresh instead of dropped", async () => {+ let release: (() => void) | null = null;+ const fetchPage = async (_q: unknown, limit: number, cursor: string | null) => {+ const at = cursor ? Number(cursor) : 0;+ if (cursor && !release) await new Promise<void>((resolve) => { release = resolve; });+ return { rows: rows(100).slice(at, at + limit), nextCursor: String(at + limit), hasMore: at + limit < 100 };+ };+ const list = new RowPager<unknown, Row>(fetchPage, {}, "q", { initialLimit: 20, pageSize: 20 });+ await list.start();+ const page = list.loadMore();+ await new Promise((resolve) => setTimeout(resolve, 0));+ const refreshed = list.refresh();+ (release as (() => void) | null)?.();+ await Promise.all([page, refreshed]);+ assert.equal(list.getState().rows.length, 40, "the refresh re-read only the 20 already drawn");+});++test("a Compare row's panes are repaired with their own titles, not the row's", async () => {+ const src = readSrc("features/chat/hooks/use-sidebar-row-pages.ts");+ assert.match(src, /title: row\.threadTitles\?\.\[index\] \?\? row\.title,/);+});++test("a training refresh moves its offset by the server rows read, and a repeated run restarts it", () => {+ const src = readSrc("features/training/hooks/use-training-history-sidebar.ts");+ assert.match(src, /offset \+= result\.runs\.length;/);+ assert.match(src, /shifted \|\|= result\.total !== firstTotal \|\| fresh\.length < result\.runs\.length;/);+ assert.match(src, /if \(result\.runs\.length === 0 \|\| offset >= result\.total\) break;/);+});++test("a failed empty-history probe stops an older answer from showing \"No chats yet\"", () => {+ const src = readSrc("components/app-sidebar.tsx");+ assert.match(src, /\.catch\(\(\) => \{\n\s*if \(!cancelled && mine === latest\) setHasUnlistedChats\(null\);\n\s*\}\);/);+ assert.match(src, /const mine = \+\+latest;/);+ assert.match(src, /if \(!cancelled && mine === latest\) \{\n\s*setHasUnlistedChats\(pages\.some/);+});++test("Pinned and custom sections mount every row they have, so navigation sees only drawn rows", () => {+ const items = Array.from({ length: 27 }, (_, i) => ({ id: `row-${i}` }));+ const html = (mountAll: boolean) =>+ renderToStaticMarkup(+ React.createElement(+ "ul",+ null,+ React.createElement(ProgressiveRows<{ id: string }>, {+ items,+ pageSize: 20,+ mountAll,+ hasMore: true,+ onReachEnd: () => undefined,+ renderItem: ({ id }) => React.createElement("li", { "data-row": id }),+ }),+ ),+ );+ assert.equal(html(false).match(/data-row=/g)?.length, 20);+ assert.equal(html(true).match(/data-row=/g)?.length, 27);+ const src = readSrc("components/app-sidebar.tsx");+ assert.match(src, /items=\{pinnedRows\}\n\s*pageSize=\{SIDEBAR_PAGE_SIZE\}\n\s*mountAll\n/);+ assert.match(src, /items=\{rows\}\n\s*pageSize=\{SIDEBAR_PAGE_SIZE\}\n\s*mountAll\n/);+});++test("an empty spell starts with no answer from an earlier one, from its first paint", () => {+ const src = readSrc("components/app-sidebar.tsx");+ assert.match(src, /if \(!sidebarLooksEmpty\) \{\n\s*setHasUnlistedChats\(null\);\n\s*return;\n\s*\}/);+});++test("a whole-list read goes on past a page emptied by chats deleted here", async () => {+ const row = (id: string) => ({ id, type: "single", threadIds: [id], title: "", createdAt: 1, updatedAt: 1 });+ const { mod } = loadRowPages({+ server: "paged",+ pages: (cursor) =>+ cursor === null+ ? { rows: [row("gone")], nextCursor: "a", hasMore: true }+ : cursor === "a"+ ? { rows: [row("k1"), row("k2")], nextCursor: "b", hasMore: true }+ : { rows: [row("k3")], nextCursor: null, hasMore: false },+ });+ assert.deepEqual(await mod.readAllSidebarRowIds(sidebarRowQuery({})), ["k1", "k2", "k3"]);+ // an empty page that does not move the cursor still ends the read+ const stuck = loadRowPages({+ server: "paged",+ pages: (cursor) => ({ rows: cursor === "same" ? [] : [row("k1")], nextCursor: "same", hasMore: true }),+ });+ assert.deepEqual(await stuck.mod.readAllSidebarRowIds(sidebarRowQuery({})), ["k1"]);+});