diff --git a/CHANGELOG.md b/CHANGELOG.md index c137a62a..fe204b27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Notable changes in each release, newest first. Releases before 0.9.7 are describ ## Unreleased +- Chat projects: projects you file conversations in now live on the daemon, so every device sees the same ones. A project is a named folder (made under `projects.root`, default `~/prometheus-projects`, or one you already have); a conversation filed in it works in that folder. New routes: `GET|POST /api/chat-projects`, `GET|PUT|DELETE /api/chat-projects/{id}` and `PUT /api/sessions/{id}/project`; session rows carry `chat_project_id`, and `POST /api/sessions` can start a conversation in a project. The owner does everything; a chat-only device sees only the projects its own conversations are in, as a name, and cannot create, rename, delete or file. Filing never replaces a folder you chose by hand unless asked, and `keep_workspace` files as a label only. Deleting a project never deletes a file. The `subscribed` ack lists `projects` in `server_capabilities`, and owner sockets get a `chat_project_changed` frame on every change. - Speech: an app can now have a reply read aloud by the voice box behind Prometheus, phrase by phrase, so the phone starts speaking before the reply has finished. New routes: `GET /api/speech` (what works now), `POST /api/speech/transcribe` (audio in, text out), `GET /api/sessions/{session_id}/speech` (the session's reply as an NDJSON stream of spoken phrases) and `POST /api/speech/speak` (any text, the same way). The owner and chat-only devices may use them with chat's limits: a device hears only its own sessions, a revoked token's streams end at once, and streams are capped per caller. The `subscribed` ack lists `speech` in `server_capabilities` when a voice box is set up; an app that does not see it keeps its own voice. When the box is down the app is told why and where the unspoken text starts, and reads the rest with its own voice; the daemon's own voice engines are not used for this. Nothing new to configure. The bridge hands every frame to registered observers (one small hook in `ws_server.py`). - Speech (groundwork): `prometheus.speech.replies` holds each session's reply from the frames the bridge already broadcasts (bound by its `message_id`, ended by its own `chat_done`, an `error`, or the session's last open turn closing), and `prometheus.speech.stream` turns a reply or any text into the speech stream's lines: phrases with their WAV as soon as the voice box makes them, a dropped round, keepalives, and one final `done` with its reason (`unavailable` says where the unspoken text starts). Nothing uses them yet; the routes follow. - Docs: `docs/design/chat-projects.md` is the design for chat projects on the daemon: one store for grouping conversations, a project's folder and its instructions; the routes, who may call them (a chat-only device sees only the projects its own sessions are in), the `projects` capability, suggestions, and how a client moves its local projects in, per instance. No behaviour changes. diff --git a/docs/reference/routes.md b/docs/reference/routes.md index 73dc5a54..f851fe8b 100644 --- a/docs/reference/routes.md +++ b/docs/reference/routes.md @@ -9,7 +9,7 @@ app. For what each one is *for*, see the curated [API guide](../guide/api.md) — this file is the complete list, that one is the explanation. -## Daemon app — 137 paths +## Daemon app — 140 paths | Path | Methods | |---|---| @@ -26,6 +26,8 @@ one is the explanation. | `/api/backends/{name}/probe` | POST | | `/api/benchmarks/run` | POST | | `/api/chat` | POST | +| `/api/chat-projects` | GET, POST | +| `/api/chat-projects/{project_id}` | DELETE, GET, PUT | | `/api/chat/interrupt` | POST | | `/api/chat/send` | POST | | `/api/code` | POST | @@ -115,6 +117,7 @@ one is the explanation. | `/api/sessions/{session_id}/model` | DELETE, GET, POST | | `/api/sessions/{session_id}/pin` | PUT | | `/api/sessions/{session_id}/profile` | GET, PUT | +| `/api/sessions/{session_id}/project` | PUT | | `/api/sessions/{session_id}/purge` | POST | | `/api/sessions/{session_id}/purge-preview` | GET | | `/api/sessions/{session_id}/speech` | GET | diff --git a/src/prometheus/memory/lcm_conversation_store.py b/src/prometheus/memory/lcm_conversation_store.py index 13972f7e..fbbdfae1 100644 --- a/src/prometheus/memory/lcm_conversation_store.py +++ b/src/prometheus/memory/lcm_conversation_store.py @@ -1175,6 +1175,23 @@ def purge_session(self, session_id: str) -> dict[str, int]: self._conn.execute("PRAGMA wal_checkpoint(TRUNCATE)") return counts + def has_session_rows(self, session_id: str) -> bool: + """True when *session_id* has messages, or a workspace binding or chat-project filing made before its first + message: either way it exists, and no device may claim it (web/session_scope.session_exists).""" + if self.max_rowid(session_id) > 0: + return True + for table in ("session_workspaces", "session_chat_projects"): + if self._table_exists(table) and self._conn.execute( + f"SELECT 1 FROM {table} WHERE session_id = ?", (session_id,)).fetchone(): + return True + return False + + def tool_use_json(self, session_id: str) -> list[str]: + """The ``content_json`` of each assistant row that holds a tool call, in order (chat-project suggestions).""" + return [r[0] for r in self._conn.execute( + "SELECT content_json FROM lcm_messages WHERE session_id = ? AND role = 'assistant'" + " AND content_json LIKE '%tool_use%' ORDER BY rowid", (session_id,)).fetchall()] + def chat_projects(self) -> ChatProjectStore: """The chat projects store, on this connection (``projects/store.py``): filing a session changes its membership and its workspace binding in one transaction. Made once, then kept.""" diff --git a/src/prometheus/projects/content.py b/src/prometheus/projects/content.py new file mode 100644 index 00000000..5aa6452c --- /dev/null +++ b/src/prometheus/projects/content.py @@ -0,0 +1,71 @@ +"""What is in a project's folder: its instructions file and its reference files (docs/design/chat-projects.md, 3.4-3.5). + +Read without following links: ``PROMETHEUS.md`` counts only as a regular file at the folder's top level, and a +reference file only as a regular file directly in ``reference/`` whose name passes :func:`valid_reference_name`. A +symlinked ``reference/`` lists nothing. This module only reads; the write routes come with the instructions and files +routes. + +Source: novel code for Prometheus, 2026-11-13. +""" + +from __future__ import annotations + +import codecs +import hashlib +import os +import stat +import unicodedata +from pathlib import Path + +INSTRUCTIONS_FILE = "PROMETHEUS.md" +REFERENCE_DIR = "reference" +#: The instruction loader's per-file cap (context/prometheusmd.py), so what is saved is what the model sees. +MAX_INSTRUCTIONS_CHARS = 12_000 +NAME_MAX = 120 +_CHUNK = 256 * 1024 + + +def instructions_info(folder: str | Path) -> tuple[int, str]: + """``(characters, rev)`` of the folder's ``PROMETHEUS.md``; ``(0, "")`` when there is no regular file there. + + ``rev`` is the first 16 hex of the SHA-256 of the whole file's bytes, read in chunks. + """ + path = Path(folder) / INSTRUCTIONS_FILE + try: + if not stat.S_ISREG(os.lstat(path).st_mode): + return 0, "" + # O_NONBLOCK so a FIFO swapped in after the lstat cannot hang the read; fstat confirms what was opened. + fd = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0)) + with os.fdopen(fd, "rb") as fh: + if not stat.S_ISREG(os.fstat(fh.fileno()).st_mode): + return 0, "" + digest = hashlib.sha256() + decoder = codecs.getincrementaldecoder("utf-8")("replace") + chars = 0 + while chunk := fh.read(_CHUNK): + digest.update(chunk) + chars += len(decoder.decode(chunk)) + chars += len(decoder.decode(b"", final=True)) + except OSError: + return 0, "" + return chars, digest.hexdigest()[:16] + + +def valid_reference_name(name: str) -> bool: + """One path segment: no separator, no leading dot, no control or format character, at most 120 characters.""" + if not name or len(name) > NAME_MAX or name.startswith(".") or "/" in name or "\\" in name: + return False + return not any(unicodedata.category(ch) in ("Cc", "Cf") for ch in name) + + +def reference_names(folder: str | Path) -> list[str]: + """The reference files listed: regular files directly in ``reference/`` with valid names, sorted.""" + ref = Path(folder) / REFERENCE_DIR + try: + if not stat.S_ISDIR(os.lstat(ref).st_mode): + return [] + with os.scandir(ref) as entries: + return sorted(e.name for e in entries + if valid_reference_name(e.name) and e.is_file(follow_symlinks=False)) + except OSError: + return [] diff --git a/src/prometheus/projects/folders.py b/src/prometheus/projects/folders.py index 53ffd614..27dd2ee1 100644 --- a/src/prometheus/projects/folders.py +++ b/src/prometheus/projects/folders.py @@ -17,7 +17,7 @@ import re import stat import unicodedata -from collections.abc import Mapping +from collections.abc import Callable, Mapping from pathlib import Path from typing import Any @@ -42,7 +42,7 @@ def slug(name: str) -> str: return out or "project" -def make_managed_folder(root: Path, name: str) -> Path: +def make_managed_folder(root: Path, name: str, *, skip: Callable[[Path], bool] | None = None) -> Path: """Make ``/`` (or ``-2``, ``-3``, ...) with mode 0700, and the root too (0700) if it is missing. An existing root must be a plain directory owned by this user (``ValueError`` otherwise: a symlink or someone @@ -64,6 +64,8 @@ def make_managed_folder(root: Path, name: str) -> Path: base = slug(name) for n in range(1, _MAX_SUFFIX): candidate = root / (base if n == 1 else f"{base}-{n}") + if skip is not None and skip(candidate): + continue # a stored project's folder, even one since removed on disk try: candidate.mkdir(mode=0o700) except FileExistsError: diff --git a/src/prometheus/projects/store.py b/src/prometheus/projects/store.py index 5f544159..2bc3a003 100644 --- a/src/prometheus/projects/store.py +++ b/src/prometheus/projects/store.py @@ -33,6 +33,11 @@ from dataclasses import dataclass, field NAME_MAX = 60 +# A membership row ``s`` whose session is not forgotten: no tombstone, or activity newer than it (the session list's +# own rule: a tombstoned session stays hidden until newer activity). The membership itself is kept, so a session +# that comes back is still filed. +_SHOWN = ("NOT EXISTS (SELECT 1 FROM session_tombstones t WHERE t.session_id = s.session_id AND t.deleted_at >= " + "COALESCE((SELECT MAX(m.timestamp) FROM lcm_messages m WHERE m.session_id = s.session_id), 0))") #: A binding a project set is ``set_by = "project:"``: the id says which, so only that project clears it. SET_BY_PREFIX = "project:" @@ -181,7 +186,7 @@ def rev(self) -> int: def _rows(self, where: str = "", args: tuple = ()) -> list[Project]: sql = ( "SELECT p.id, p.name, p.folder, p.folder_managed, p.import_key, p.created_at, p.updated_at," - " (SELECT COUNT(*) FROM session_chat_projects s WHERE s.project_id = p.id)" + f" (SELECT COUNT(*) FROM session_chat_projects s WHERE s.project_id = p.id AND {_SHOWN})" f" FROM chat_projects p {where} ORDER BY p.position, p.created_at" ) return [Project(r[0], r[1], r[2], bool(r[3]), r[4], r[5], r[6], int(r[7])) @@ -213,27 +218,29 @@ def project_of(self, session_id: str) -> str | None: "SELECT project_id FROM session_chat_projects WHERE session_id = ?", (session_id,)).fetchone() return row[0] if row else None - def projects_of(self, session_ids: Iterable[str]) -> dict[str, str]: + def projects_of(self, session_ids: Iterable[str], *, shown_only: bool = False) -> dict[str, str]: + """``{session_id: project_id}`` for those filed; *shown_only* leaves out forgotten (tombstoned) sessions.""" ids = list(session_ids) out: dict[str, str] = {} + extra = f" AND {_SHOWN}" if shown_only else "" for i in range(0, len(ids), 500): chunk = ids[i:i + 500] marks = ",".join("?" * len(chunk)) out.update(self._conn.execute( - f"SELECT session_id, project_id FROM session_chat_projects WHERE session_id IN ({marks})", + f"SELECT session_id, project_id FROM session_chat_projects s WHERE session_id IN ({marks}){extra}", chunk).fetchall()) return out def session_ids(self, project_id: str) -> list[str]: return [r[0] for r in self._conn.execute( - "SELECT session_id FROM session_chat_projects WHERE project_id = ? ORDER BY filed_at, session_id", - (project_id,)).fetchall()] + f"SELECT session_id FROM session_chat_projects s WHERE project_id = ? AND {_SHOWN}" + " ORDER BY filed_at, session_id", (project_id,)).fetchall()] def visible_to(self, own_session_ids: set[str]) -> list[VisibleProject]: """The projects a chat-only device may see: those with at least one of *own_session_ids*, in display order.""" if not own_session_ids: return [] - filed = self.projects_of(own_session_ids) + filed = self.projects_of(own_session_ids, shown_only=True) by_project: dict[str, list[str]] = {} for sid, pid in filed.items(): by_project.setdefault(pid, []).append(sid) @@ -300,8 +307,10 @@ def delete(self, project_id: str) -> int: if self._conn.execute("SELECT 1 FROM chat_projects WHERE id = ?", (project_id,)).fetchone() is None: raise UnknownProject("no such project") with self._write(): + # The count a client sees: forgotten sessions are not shown, so they are not counted as unfiled. sessions = [r[0] for r in self._conn.execute( - "SELECT session_id FROM session_chat_projects WHERE project_id = ?", (project_id,)).fetchall()] + f"SELECT session_id FROM session_chat_projects s WHERE project_id = ? AND {_SHOWN}", + (project_id,)).fetchall()] # Every binding this project set, on members and on sessions since moved elsewhere as a label. self._conn.execute("DELETE FROM session_workspaces WHERE set_by = ?", (_set_by(project_id),)) self._conn.execute("DELETE FROM session_chat_projects WHERE project_id = ?", (project_id,)) diff --git a/src/prometheus/web/chat_projects_routes.py b/src/prometheus/web/chat_projects_routes.py new file mode 100644 index 00000000..9152d165 --- /dev/null +++ b/src/prometheus/web/chat_projects_routes.py @@ -0,0 +1,446 @@ +"""``/api/chat-projects`` and filing a conversation in one (docs/design/chat-projects.md, sections 3.1-3.3, 4-6). + +* ``GET /api/chat-projects`` and ``GET /api/chat-projects/{id}``: the owner sees every project in full; a chat-only + device sees only the projects one of its own sessions is in, as id and name and its own sessions, and a project + without one is ``404`` ("not yours" is "not there"). +* ``POST /api/chat-projects``, ``PUT|DELETE /api/chat-projects/{id}``, ``PUT /api/sessions/{session_id}/project``: + the owner's only. Default-deny in ``route_access`` refuses them to a chat-only device, and every handler checks + again: a path that happens to match a chat-only pattern must not open an owner route. + +Filing binds the conversation's workspace to the project's folder unless ``keep_workspace`` (a label only, what a +migration sends). A hand-set workspace is never replaced without ``replace_workspace``; a chat-only device's session is +bound only with ``device_session``; a machine session never; the session must exist; the folder is validated again and +must still resolve to the stored path. + +Every change raises the store's ``rev`` and sends ``chat_project_changed`` to owner sockets only (ids and a change +kind, never a name or a path). Never in an answer to a chat-only device: a folder path, instructions, other projects, +other sessions, the ``rev``. + +Source: novel code for Prometheus, 2026-11-13. +""" + +from __future__ import annotations + +import asyncio +import json +import logging +import os +import re +from collections.abc import Callable +from pathlib import Path +from typing import Any + +from fastapi import Request +from fastapi.responses import JSONResponse + +from prometheus.memory.session_kind import is_machine_session +from prometheus.projects.content import instructions_info, reference_names +from prometheus.projects.folders import make_managed_folder, projects_root +from prometheus.projects.store import ( + FolderInUse, + ImportKeyTaken, + NameTaken, + Project, + UnknownProject, + WorkspaceSet, + clean_name, +) + +logger = logging.getLogger(__name__) + +_BODY_CAP = 64 * 1024 +_IMPORT_KEY = re.compile(r"[\x21-\x7e]{1,200}") + + +def _err(status: int, kind: str, error: str, **extra: Any) -> JSONResponse: + return JSONResponse(status_code=status, content={"error": error, "kind": kind, **extra}) + + +async def _read_json(request: Request) -> Any: + """The JSON body, read with a cap (a chunked body has no Content-Length for the middleware). None if unusable.""" + body = bytearray() + async for chunk in request.stream(): + body.extend(chunk) + if len(body) > _BODY_CAP: + return None + if not body: + return {} + try: + return json.loads(body) + except (ValueError, RecursionError): + return None + + +def _ident(path: Path) -> tuple[int, int] | None: + """The file's identity (device, inode), or None. Case and spelling do not matter to it (macOS is + case-insensitive), nor does a symlink in the path.""" + try: + st = os.stat(path) + except OSError: + return None + return st.st_dev, st.st_ino + + +def _chain(path: Path) -> list[tuple[int, int]]: + """The identities of *path* and every directory above it.""" + out = [] + for p in (path, *path.parents): + ident = _ident(p) + if ident is not None: + out.append(ident) + return out + + +def _no_store() -> JSONResponse: + # 404, not 503: the design tells a client that 404 here means "this daemon has no projects". + return _err(404, "unavailable", "this daemon has no projects (no durable store)") + + +class ChatProjectsService: + """What the routes and ``POST /api/sessions`` share. On ``app.state.chat_projects``.""" + + def __init__(self, app: Any, config: dict[str, Any], conversation_store: Callable[[], Any], + devices: Callable[[], Any], session_exists: Callable[[str], bool]) -> None: + self._app = app + self._config = config + self._conversation_store = conversation_store + self._devices = devices + self._session_exists = session_exists + + def store(self) -> Any: + conv = self._conversation_store() + return conv.chat_projects() if conv is not None else None + + def _security(self) -> dict[str, Any]: + sec = self._config.get("security") + return sec if isinstance(sec, dict) else {} + + # ------------------------------------------------------------------ views + @staticmethod + def full(p: Project) -> dict[str, Any]: + chars, rev = instructions_info(p.folder) + return {"id": p.id, "name": p.name, "folder": p.folder, "folder_managed": p.folder_managed, + "instructions_chars": chars, "instructions_rev": rev, "files": len(reference_names(p.folder)), + "sessions": p.sessions, "import_key": p.import_key, "created_at": p.created_at, + "updated_at": p.updated_at} + + # ---------------------------------------------------------------- folders + @staticmethod + def _agent_homes() -> list[Path]: + from prometheus.config.paths import config_dir_path, data_dir_path + + homes = [Path(os.path.expanduser("~/.prometheus"))] + for get in (config_dir_path, data_dir_path): + try: + homes.append(get()) + except Exception: # noqa: BLE001 - a path that cannot be computed is simply not compared + continue + return homes + + def validate(self, raw: str) -> tuple[Path | None, str | None]: + """A folder a project may use, resolved, or why not (design 3.1 rule 3). Compared by file identity, never by + spelling: macOS is case-insensitive, and a symlinked agent home must not hide what it points at.""" + from prometheus.context.workspace import validate_workspace_path + + resolved, why = validate_workspace_path(raw, self._security()) + if resolved is None: + return None, why + chain = _chain(resolved) + here = chain[0] if chain else None + home = _ident(Path(os.path.expanduser("~"))) + if home is not None and (here == home or here in _chain(Path(os.path.expanduser("~")))[1:]): + return None, "the home directory, or a folder that contains it, cannot be a project folder" + for agent_home in self._agent_homes(): + ident = _ident(agent_home) + if ident is None: + continue + if ident in chain or (here is not None and here in _chain(agent_home)): + return None, "a folder inside or around the agent's own home cannot be a project folder" + root = projects_root(self._config) + root_ident = _ident(root) + if root_ident is not None and (here == root_ident or (here is not None and here in _chain(root)[1:])): + return None, "the projects root, or a folder that contains it, cannot be a project folder" + store = self.store() + for other in (store.all() if store is not None else []): + o = Path(other.folder) + oid = _ident(o) + if oid is None: + continue + if oid == here: + return None, f"the project {other.name!r} already uses that folder" + if oid in chain or (here is not None and here in _chain(o)): + return None, f"it is inside or around the folder of the project {other.name!r}" + return resolved, None + + def bindable(self, p: Project) -> str | None: + """Why *p*'s folder may not be bound now, or None: validated again, and it must still be the stored path.""" + from prometheus.context.workspace import validate_workspace_path + + if os.path.islink(p.folder): + return "the project's folder is now a link" + resolved, why = validate_workspace_path(p.folder, self._security()) + if resolved is None: + return why + if str(resolved) != p.folder: + return "the project's folder no longer resolves to where it was" + return None + + def make_managed(self, name: str) -> tuple[Path | None, str | None]: + from prometheus.config.shipped_defaults import resolve_denied_paths + from prometheus.context.workspace import validate_workspace_path + from prometheus.security.path_guard import denying_entry + + root = projects_root(self._config) + if not root.is_absolute(): + return None, "projects.root must be an absolute path (or start with ~)" + # Checked BEFORE anything is made: the nearest folder that exists must be one a project could use, and no + # denied entry may cover the root's own path, so a refused root leaves nothing behind. + existing = next((p for p in (root, *root.parents) if p.exists()), None) + if existing is None: + return None, "the projects root has no existing parent" + _ok, why = validate_workspace_path(str(existing), self._security()) + if _ok is None: + return None, why + entries = [os.path.expanduser(e) for e in resolve_denied_paths(self._security())] + if denying_entry(str(existing.resolve() / root.relative_to(existing)), entries) is not None: + return None, "the projects root is under security.denied_paths" + store = self.store() + projects = store.all() if store is not None else [] + root_chain_existing = _chain(existing.resolve()) + for other in projects: + oid = _ident(Path(other.folder)) + if oid is not None and oid in root_chain_existing: + return None, f"the projects root is inside the folder of the project {other.name!r}" + stored = {other.folder for other in projects} + try: + made = make_managed_folder(root, name, skip=lambda c: str(c.resolve()) in stored or str(c) in stored) + except (OSError, ValueError) as exc: + return None, str(exc) + resolved, why = self.validate(str(made)) + if resolved is None: + _remove_empty(made) + return None, why + return resolved, None + + # ---------------------------------------------------------------- filing + def precheck(self, project_id: str) -> JSONResponse | Project: + store = self.store() + if store is None: + return _no_store() + p = store.get(project_id) + if p is None: + return _err(404, "unknown_project", "no such project") + why = self.bindable(p) + if why is not None: + return _err(400, "bad_folder", why) + return p + + async def file(self, session_id: str, project_id: str | None, *, replace_workspace: bool = False, + keep_workspace: bool = False, device_session: bool = False) -> JSONResponse | dict[str, Any]: + store = self.store() + if store is None: + return _no_store() + if is_machine_session(session_id): + return _err(400, "machine_session", "a machine session cannot be filed in a project") + if not self._session_exists(session_id): + return _err(404, "unknown_session", "unknown session") + if project_id is not None: + p = store.get(project_id) + if p is None: + return _err(404, "unknown_project", "no such project") + if not keep_workspace: + devices = self._devices() + owner = devices.session_owner(session_id) if devices is not None else None + if owner and owner not in devices.owner_device_ids() and not device_session: + return _err(409, "device_session", "this conversation belongs to a chat-only device; binding it " + "to the project's folder needs device_session: true") + why = self.bindable(p) + if why is not None: + return _err(400, "bad_folder", why) + try: + result = store.file(session_id, project_id, replace_workspace=replace_workspace, + keep_workspace=keep_workspace) + except WorkspaceSet as exc: + return _err(409, "workspace_set", str(exc), workspace=exc.workspace) + except UnknownProject: + return _err(404, "unknown_project", "no such project") + if project_id is not None and not keep_workspace: + logger.warning("session workspace SET for %s → %s (project %s) — this is now that conversation's write " + "boundary", session_id, result.workspace, project_id) + await self.notify("membership", project_id, session_id) + return {"ok": True, "session_id": session_id, "chat_project_id": project_id, "workspace": result.workspace} + + async def notify(self, change: str, project_id: str | None, session_id: str | None = None) -> None: + """``chat_project_changed`` to owner sockets only (ids and a change kind).""" + bridge = getattr(self._app.state, "ws_bridge", None) + send = getattr(bridge, "send_to_operators", None) + store = self.store() + if send is None or store is None: + return + try: + await send({"type": "chat_project_changed", "payload": { + "rev": store.rev(), "project_id": project_id, "change": change, "session_id": session_id}}) + except Exception: # noqa: BLE001 - a frame is a hint; clients refetch on refresh + logger.debug("chat_project_changed not sent", exc_info=True) + + +def _remove_empty(folder: Path) -> None: + """Remove a folder this request made a moment ago, if it is still empty (never anything else).""" + try: + folder.rmdir() + except OSError: + pass + + +def register_chat_projects_routes( + app: Any, *, config: dict[str, Any], conversation_store: Callable[[], Any], devices: Callable[[], Any], + is_owner: Callable[[Request], bool], owned_ids: Callable[[Request], set[str] | None], + session_exists: Callable[[str], bool], +) -> ChatProjectsService: + service = ChatProjectsService(app, config, conversation_store, devices, session_exists) + app.state.chat_projects = service + + def _refuse(request: Request) -> JSONResponse | None: + if not is_owner(request): + return _err(403, "operator_only", "only the owner may do this") + if service.store() is None: + return _no_store() + return None + + @app.get("/api/chat-projects") + async def list_chat_projects(request: Request) -> Any: + store = service.store() + if store is None: + return _no_store() + if is_owner(request): + projects = store.all() + # Off the event loop: each project's folder is read (instructions, reference listing). + full = await asyncio.to_thread(lambda: [service.full(p) for p in projects]) + return {"role": "owner", "rev": store.rev(), "projects": full} + own = owned_ids(request) or set() + return {"role": "chat_only", "projects": [ + {"id": v.id, "name": v.name, "sessions": len(v.own_session_ids)} for v in store.visible_to(own)]} + + @app.get("/api/chat-projects/{project_id}") + async def get_chat_project(project_id: str, request: Request) -> Any: + store = service.store() + if store is None: + return _no_store() + if is_owner(request): + p = store.get(project_id) + if p is None: + return _err(404, "unknown_project", "no such project") + full = await asyncio.to_thread(service.full, p) + return {**full, "session_ids": store.session_ids(project_id)} + own = owned_ids(request) or set() + for v in store.visible_to(own): + if v.id == project_id: + return {"id": v.id, "name": v.name, "session_ids": v.own_session_ids} + return _err(404, "unknown_project", "no such project") + + @app.post("/api/chat-projects", response_model=None) + async def create_chat_project(request: Request) -> Any: + refusal = _refuse(request) + if refusal is not None: + return refusal + store = service.store() + body = await _read_json(request) + if not isinstance(body, dict): + return _err(400, "bad_request", "send a JSON object") + raw_name, folder_raw, import_key = body.get("name"), body.get("folder"), body.get("import_key") + name = clean_name(raw_name) if isinstance(raw_name, str) else "" + if not name: + return _err(400, "name_required", "a project needs a name") + if folder_raw is not None and not isinstance(folder_raw, str): + return _err(400, "bad_folder", "folder is an absolute path or null") + if import_key is not None and not (isinstance(import_key, str) and _IMPORT_KEY.fullmatch(import_key)): + return _err(400, "bad_request", "import_key is 1 to 200 printable characters") + if import_key and (found := store.by_import_key(import_key)) is not None: + return JSONResponse({"ok": True, "project": service.full(found), "created": False}) + existing = store.by_name(name) + made: Path | None = None + if folder_raw is None: + if existing is not None: + return JSONResponse({"ok": True, "project": service.full(existing), "created": False}) + made, why = service.make_managed(name) + if made is None: + return _err(400, "bad_folder", why or "the managed folder could not be made") + folder, managed = made, True + else: + if existing is not None: + # The name first (design 3.1 rule 2): the same folder by identity is the same project; anything + # else, even a folder that would not validate, is a name clash. + same = _ident(Path(os.path.expanduser(folder_raw))) + if same is not None and same == _ident(Path(existing.folder)): + return JSONResponse({"ok": True, "project": service.full(existing), "created": False}) + return _err(409, "name_taken", f"a project is already called {existing.name!r}") + resolved, why = service.validate(folder_raw) + if resolved is None: + if why and "already uses that folder" in why: + return _err(409, "folder_in_use", why) + return _err(400, "bad_folder", why or "that folder cannot be used") + if store.by_folder(str(resolved)) is not None: + return _err(409, "folder_in_use", "another project already uses that folder") + folder, managed = resolved, False + try: + project = store.insert(name, str(folder), managed=managed, import_key=import_key) + except (NameTaken, FolderInUse, ImportKeyTaken) as exc: + if made is not None: + _remove_empty(made) + # Two creates at once: the winner's project when this one would have returned it. + winner = store.by_import_key(import_key) if isinstance(exc, ImportKeyTaken) and import_key else None + if winner is None and isinstance(exc, NameTaken): + same = store.by_name(name) + if same is not None and (folder_raw is None or same.folder == str(folder)): + winner = same + if winner is not None: + return JSONResponse({"ok": True, "project": service.full(winner), "created": False}) + return _err(409, exc.kind, str(exc)) + await service.notify("created", project.id) + return JSONResponse(status_code=201, content={"ok": True, "project": service.full(project), "created": True}) + + @app.put("/api/chat-projects/{project_id}", response_model=None) + async def rename_chat_project(project_id: str, request: Request) -> Any: + refusal = _refuse(request) + if refusal is not None: + return refusal + body = await _read_json(request) + raw = body.get("name") if isinstance(body, dict) else None + if not isinstance(raw, str) or not clean_name(raw): + return _err(400, "name_required", "a project needs a name") + try: + project = service.store().rename(project_id, raw) + except UnknownProject: + return _err(404, "unknown_project", "no such project") + except NameTaken as exc: + return _err(409, "name_taken", str(exc)) + await service.notify("updated", project_id) + return {"ok": True, "project": service.full(project)} + + @app.delete("/api/chat-projects/{project_id}", response_model=None) + async def delete_chat_project(project_id: str, request: Request) -> Any: + refusal = _refuse(request) + if refusal is not None: + return refusal + try: + unfiled = service.store().delete(project_id) + except UnknownProject: + return _err(404, "unknown_project", "no such project") + await service.notify("deleted", project_id) + return {"ok": True, "id": project_id, "unfiled": unfiled} + + @app.put("/api/sessions/{session_id}/project", response_model=None) + async def file_session(session_id: str, request: Request) -> Any: + refusal = _refuse(request) + if refusal is not None: + return refusal + body = await _read_json(request) + if not isinstance(body, dict) or "chat_project_id" not in body: + return _err(400, "bad_request", 'send {"chat_project_id": "" | null}') + pid = body["chat_project_id"] + flags = {k: body.get(k, False) for k in ("replace_workspace", "keep_workspace", "device_session")} + if (pid is not None and not isinstance(pid, str)) or not all(isinstance(v, bool) for v in flags.values()): + return _err(400, "bad_request", "chat_project_id is a string or null; the flags are true or false") + return await service.file(session_id, pid, **flags) + + return service diff --git a/src/prometheus/web/route_access.py b/src/prometheus/web/route_access.py index c1495d1f..7170052a 100644 --- a/src/prometheus/web/route_access.py +++ b/src/prometheus/web/route_access.py @@ -87,6 +87,9 @@ ("GET", "/api/events/recent"), ("GET", "/api/activity/recent"), # session-filtered for a scoped caller, like events ("POST", "/api/search"), + # chat projects: only those its own sessions are in, as id and name (the handlers filter) + ("GET", "/api/chat-projects"), + ("GET", "/api/chat-projects/{project_id}"), # speech, with chat's limits: a session's reply only for a session it owns (the session path guard) ("GET", "/api/speech"), ("POST", "/api/speech/transcribe"), @@ -241,6 +244,10 @@ ("PUT", "/api/project-file"), # /api/projects ("GET", "/api/projects"), + ("POST", "/api/chat-projects"), + ("PUT", "/api/chat-projects/{project_id}"), + ("DELETE", "/api/chat-projects/{project_id}"), + ("PUT", "/api/sessions/{session_id}/project"), ("POST", "/api/projects"), ("DELETE", "/api/projects/{project_id}"), ("PUT", "/api/projects/{project_id}"), diff --git a/src/prometheus/web/server.py b/src/prometheus/web/server.py index cd1bbe19..39388a7e 100644 --- a/src/prometheus/web/server.py +++ b/src/prometheus/web/server.py @@ -1574,6 +1574,11 @@ def _gateway(sid: str) -> str: _mine = _access.owned_ids(_scope(request)) if _mine is not None: by_id = {sid: row for sid, row in by_id.items() if sid in _mine} + # Chat projects (docs/design/chat-projects.md 3.3): one lookup AFTER both branches, so the live overlay, + # which replaces the durable dict, cannot drop it. + _filed = store.chat_projects().projects_of(by_id.keys()) if store is not None and by_id else {} + for _sid, _row in by_id.items(): + _row["chat_project_id"] = _filed.get(_sid) return sorted( by_id.values(), key=lambda s: s["last_active"] or 0.0, reverse=True ) @@ -1602,6 +1607,7 @@ async def create_session(request: Request): import uuid gateway = "desktop" + chat_project_id: Any = None try: body = await request.json() # PRESENT-but-empty is a caller bug, not "unspecified" — it falls @@ -1609,8 +1615,27 @@ async def create_session(request: Request): # becoming "desktop". Absent key / no body → the default. if isinstance(body, dict) and "gateway" in body: gateway = str(body["gateway"] or "").strip() + if isinstance(body, dict): + chat_project_id = body.get("chat_project_id") except Exception: pass # no/invalid body → the default; this route has no required input + # Starting in a chat project (docs/design/chat-projects.md 3.2): the owner's only, checked before an id is + # minted, so a refusal creates nothing. + if chat_project_id is not None: + if not isinstance(chat_project_id, str): + return JSONResponse(status_code=400, content={"error": "chat_project_id must be a string", + "kind": "bad_request"}) + if not _scope(request).unrestricted: + return JSONResponse(status_code=403, content={"error": "only the owner may start a conversation " + "in a project", "kind": "operator_only"}) + _checked = app.state.chat_projects.precheck(chat_project_id) + if isinstance(_checked, JSONResponse): + return _checked + from prometheus.memory.session_kind import is_machine_session as _is_machine + + if _is_machine(f"{str(gateway).strip().lower()}:x"): + return JSONResponse(status_code=400, content={ + "error": "a machine session cannot be started in a project", "kind": "machine_session"}) # The id is parsed by splitting on the FIRST colon, so a colon in the # gateway would silently truncate it. Keep the charset boring. @@ -1630,6 +1655,12 @@ async def create_session(request: Request): return _unknown_session() if session_mgr: session_mgr.get_or_create(sid) + if chat_project_id is not None: + _filed = await app.state.chat_projects.file(sid, chat_project_id) + if isinstance(_filed, JSONResponse): + return _filed + return {"session_id": sid, "gateway": gateway.lower(), "chat_project_id": chat_project_id, + "workspace": _filed["workspace"]} return {"session_id": sid, "gateway": gateway.lower()} @app.post("/api/chat/send") @@ -7019,6 +7050,17 @@ async def undispatch_story(story_pk: str): register_pieces_routes(app, devices=_devices_or_create, auth_on=lambda: bool(_api_token)) + # ── Chat projects: a named folder with instructions, conversations filed in it (docs/design/chat-projects.md) ── + # Owner for everything; a chat-only device sees only the projects its own sessions are in. + from prometheus.web.chat_projects_routes import register_chat_projects_routes + + register_chat_projects_routes( + app, config=config, conversation_store=_conversation_store, devices=_devices_or_create, + is_owner=lambda request: _scope(request).unrestricted, + owned_ids=lambda request: _access.owned_ids(_scope(request)), + session_exists=lambda sid: session_exists(session_mgr, _conversation_store(), sid), + ) + # ── Speech: a reply read aloud through the voice box (docs/design/speech-route.md) ──── # The owner's and chat-only devices', like chat (web/route_access.py). The launcher lets the reply holder hear # the bridge's frames (speech_routes.attach_bridge). diff --git a/src/prometheus/web/session_scope.py b/src/prometheus/web/session_scope.py index 97a642c2..ede2a50c 100644 --- a/src/prometheus/web/session_scope.py +++ b/src/prometheus/web/session_scope.py @@ -83,7 +83,14 @@ def session_exists(session_mgr: Any, conversation_store: Any, session_id: str) - still has its rows, so it still exists; a PURGED one does not.""" if session_mgr is not None and session_mgr.get(session_id) is not None: return True - return conversation_store is not None and conversation_store.max_rowid(session_id) > 0 + if conversation_store is None: + return False + # A session the owner bound to a folder or filed in a chat project before its first message exists too, so + # no device can claim it (and inherit that binding) after a restart empties the in-memory sessions. + has_rows = getattr(conversation_store, "has_session_rows", None) + if callable(has_rows): + return bool(has_rows(session_id)) + return conversation_store.max_rowid(session_id) > 0 def event_session_id(event: dict[str, Any]) -> str | None: diff --git a/src/prometheus/web/ws_server.py b/src/prometheus/web/ws_server.py index 65f41d7f..efc25361 100644 --- a/src/prometheus/web/ws_server.py +++ b/src/prometheus/web/ws_server.py @@ -1080,11 +1080,11 @@ async def _handle_file_upload( def _server_capabilities(self) -> list[str]: """What the ``subscribed`` ack tells a client this daemon can do (see the ack).""" from prometheus.gateway import media_services - from prometheus.speech.voice import speech_configured caps = ["voice_input"] if media_services.voice_input_available(self.config) else [] - return caps + (["speech"] if speech_configured(self.config) else []) + caps += ["speech"] if speech_configured(self.config) else [] + return [*caps, "projects"] # chat projects (web/chat_projects_routes.py): always served async def _system_note(self, session_id: str, content: str) -> None: await self.broadcast({ diff --git a/tests/test_api_sessions_meta.py b/tests/test_api_sessions_meta.py index 38b380c7..31a2254e 100644 --- a/tests/test_api_sessions_meta.py +++ b/tests/test_api_sessions_meta.py @@ -86,9 +86,11 @@ def test_shape_is_consistent_and_keeps_count_and_created_at(tmp_path): # first exchange or set via PUT /api/sessions/{id}/title. # "pinned" joined with feat/session-pinning: a bool, false unless someone # pinned the session — daemon-side so every client agrees (tests/test_session_pins.py). - assert set(row.keys()) == {"session_id", "gateway", "created_at", "last_active", "message_count", "watermark", "live", "title", "pinned"} + assert set(row.keys()) == {"session_id", "gateway", "created_at", "last_active", "message_count", "watermark", "live", "title", "pinned", "chat_project_id"} assert row["live"] is True assert row["pinned"] is False + # chat_project_id joined with chat projects: null unless the owner filed it (docs/design/chat-projects.md 3.3) + assert row["chat_project_id"] is None assert isinstance(row["watermark"], int) assert isinstance(row["last_active"], (int, float)) assert row["message_count"] == 1 # kept diff --git a/tests/test_chat_projects_routes.py b/tests/test_chat_projects_routes.py new file mode 100644 index 00000000..a8a344a2 --- /dev/null +++ b/tests/test_chat_projects_routes.py @@ -0,0 +1,542 @@ +"""The chat projects routes (docs/design/chat-projects.md, sections 3.1-3.3, 4-6), on the real app. + +Real throughout: ``create_app``, the bearer middleware and device tokens (the global token, an owner device, two +chat-only devices), the session path guard, ``route_access``, the conversation store and its projects store, and a +real ``WebSocketBridge`` for the frame. Folders are real directories under a temp root. +""" + +from __future__ import annotations + +import json +import os +import secrets +import stat +from pathlib import Path + +import pytest + +pytest.importorskip("fastapi") +from fastapi.testclient import TestClient # noqa: E402 + +from prometheus.config.device_store import DeviceStore # noqa: E402 +from prometheus.engine.session import SessionManager # noqa: E402 +from prometheus.memory.lcm_conversation_store import LCMConversationStore # noqa: E402 +from prometheus.web import route_access # noqa: E402 +from prometheus.web.server import create_app # noqa: E402 +from tests.support.device_world import engine_over # noqa: E402 + +GLOBAL = "projects-global-" + secrets.token_hex(8) +SCOPED = {("GET", "/api/chat-projects"), ("GET", "/api/chat-projects/{project_id}")} +OWNER = {("POST", "/api/chat-projects"), ("PUT", "/api/chat-projects/{project_id}"), ("DELETE", "/api/chat-projects/{project_id}"), + ("PUT", "/api/sessions/{session_id}/project")} + + +class _Socket: + def __init__(self) -> None: + self.frames: list[dict] = [] + + async def send(self, raw: str) -> None: + self.frames.append(json.loads(raw)) + + +class World: + def __init__(self, tmp_path: Path) -> None: + self.root = tmp_path / "projects-root" + self.home = tmp_path / "home" + self.home.mkdir() + self.devices = DeviceStore(tmp_path / "devices.db") + self.conv = LCMConversationStore(tmp_path / "lcm.db") + self.mgr = SessionManager() + self.mgr.lcm_engine = engine_over(self.conv) + config = {"web": {"api_token": GLOBAL}, "projects": {"root": str(self.root)}, + "security": {"denied_paths": [str(tmp_path / "secret")]}} + self.app = create_app(config, session_mgr=self.mgr, lcm_engine=self.mgr.lcm_engine, device_store=self.devices) + from prometheus.web.ws_server import WebSocketBridge + + self.bridge = WebSocketBridge(session_mgr=self.mgr, api_token=GLOBAL, device_store=self.devices, + loop_context=None) + self.app.state.ws_bridge = self.bridge + self.client = TestClient(self.app) + self.a = self.devices.mint("device-a", "ios") + self.b = self.devices.mint("device-b", "ios") + self.owner = self.devices.mint_owner("owner-phone", "ios", by="test") + (tmp_path / "secret").mkdir() + + def hdr(self, who: str) -> dict: + token = {"op": GLOBAL, "a": self.a["token"], "b": self.b["token"], "owner": self.owner["token"]}[who] + return {"Authorization": f"Bearer {token}"} + + def call(self, who: str, method: str, url: str, **kw): + return self.client.request(method, url, headers=self.hdr(who), **kw) + + def session(self, who: str = "op") -> str: + sid = self.call(who, "POST", "/api/sessions").json()["session_id"] + self.conv.add_message(sid, _part(sid)) + return sid + + def project(self, name: str = "Kitchen", **body) -> dict: + r = self.call("op", "POST", "/api/chat-projects", json={"name": name, **body}) + assert r.status_code in (200, 201), r.text + return r.json()["project"] + + def folder(self, name: str) -> str: + p = self.home / name + p.mkdir(parents=True, exist_ok=True) + return str(p.resolve()) + + +def _part(sid: str): + from prometheus.memory.lcm_types import MessagePart + + return MessagePart(role="user", content="hello", session_id=sid, turn_index=0) + + +@pytest.fixture +def world(tmp_path, monkeypatch): + w = World(tmp_path) + monkeypatch.setenv("HOME", str(w.home)) + return w + + +# ------------------------------------------------------------------------- access + + +def test_the_routes_are_classified(): + assert SCOPED <= route_access.SCOPED_ALLOWED + assert OWNER <= route_access.OPERATOR_ONLY + + +def test_no_owner_only_route_matches_a_chat_only_pattern(): + """A path shape that happens to match a chat-only entry must not open an owner route (design 4).""" + import re + + for method, pattern in route_access.OPERATOR_ONLY: + concrete = re.sub(r"\{[^}]+\}", "x1", pattern) + assert not route_access.is_scoped_allowed(method, concrete), (method, pattern) + + +@pytest.mark.parametrize(("method", "url", "body"), [ + ("POST", "/api/chat-projects", {"name": "X"}), + ("PUT", "/api/chat-projects/cp_000000000000", {"name": "X"}), + ("DELETE", "/api/chat-projects/cp_000000000000", None), +]) +def test_a_chat_only_device_cannot_change_projects(world, method, url, body): + r = world.call("a", method, url, **({"json": body} if body else {})) + assert r.status_code == 403 + + +def test_a_chat_only_device_cannot_file(world): + sid = world.session("a") + p = world.project() + r = world.call("a", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + assert r.status_code == 403 + + +# ------------------------------------------------------------------------- create + + +def test_create_makes_a_managed_folder(world): + r = world.call("op", "POST", "/api/chat-projects", json={"name": "Kitchen remodel"}) + assert r.status_code == 201 + body = r.json() + assert body["ok"] is True and body["created"] is True + p = body["project"] + assert p["folder_managed"] is True and p["name"] == "Kitchen remodel" + assert Path(p["folder"]) == (world.root / "kitchen-remodel").resolve() + assert stat.S_IMODE(os.stat(p["folder"]).st_mode) == 0o700 + assert {"id", "folder", "instructions_chars", "instructions_rev", "files", "sessions", "import_key", + "created_at", "updated_at"} <= set(p) + + +def test_the_same_name_returns_the_existing_project(world): + first = world.project("Beacon") + r = world.call("op", "POST", "/api/chat-projects", json={"name": " BEACON "}) + assert r.status_code == 200 and r.json()["created"] is False + assert r.json()["project"]["id"] == first["id"] + + +def test_the_same_name_with_another_folder_is_taken(world): + world.project("Beacon") + r = world.call("op", "POST", "/api/chat-projects", json={"name": "beacon", "folder": world.folder("repo")}) + assert r.status_code == 409 and r.json()["kind"] == "name_taken" + + +def test_an_import_key_twice_is_one_project(world): + a = world.project("Old name", import_key="local:p1") + r = world.call("op", "POST", "/api/chat-projects", json={"name": "Other", "import_key": "local:p1"}) + assert r.status_code == 200 and r.json()["project"]["id"] == a["id"] + + +def test_an_existing_folder_is_used_as_it_is(world): + f = world.folder("repo") + p = world.project("Repo", folder=f) + assert p["folder"] == f and p["folder_managed"] is False + r = world.call("op", "POST", "/api/chat-projects", json={"name": "Again", "folder": f}) + assert r.status_code == 409 and r.json()["kind"] == "folder_in_use" + + +@pytest.mark.parametrize("which", ["missing", "relative", "denied", "home", "root", "agent_home", "inside", "around"]) +def test_a_bad_folder_is_refused(world, tmp_path, which): + outer = world.folder("outer") + world.project("Outer", folder=outer) + world.root.mkdir(exist_ok=True) + (world.home / ".prometheus").mkdir(exist_ok=True) + folder = { + "missing": str(world.home / "nope"), + "relative": "some/folder", + "denied": str(tmp_path / "secret"), + "home": str(world.home), + "root": str(world.root), + "agent_home": world.folder(".prometheus/workspace"), + "inside": world.folder("outer/inner"), + "around": str(world.home), + }[which] + r = world.call("op", "POST", "/api/chat-projects", json={"name": f"Bad {which}", "folder": folder}) + assert r.status_code == 400 and r.json()["kind"] == "bad_folder", r.text + + +def test_a_folder_around_another_project_is_refused(world): + inner = world.folder("work/inner") + world.project("Inner", folder=inner) + r = world.call("op", "POST", "/api/chat-projects", json={"name": "Work", "folder": world.folder("work")}) + assert r.status_code == 400 and r.json()["kind"] == "bad_folder" + + +def test_an_empty_name_is_refused(world): + r = world.call("op", "POST", "/api/chat-projects", json={"name": " "}) + assert r.status_code == 400 and r.json()["kind"] == "name_required" + + +def test_a_symlinked_root_is_refused(world, tmp_path): + real = tmp_path / "real-root" + real.mkdir() + world.root.symlink_to(real) + r = world.call("op", "POST", "/api/chat-projects", json={"name": "X"}) + assert r.status_code == 400 and r.json()["kind"] == "bad_folder" + assert list(real.iterdir()) == [] + + +# ------------------------------------------------------------------------- rename, delete + + +def test_rename(world): + a = world.project("Alpha") + world.project("Beta") + assert world.call("op", "PUT", f"/api/chat-projects/{a['id']}", json={"name": "beta"}).status_code == 409 + r = world.call("op", "PUT", f"/api/chat-projects/{a['id']}", json={"name": "ALPHA"}) + assert r.status_code == 200 and r.json()["project"]["name"] == "ALPHA" + assert world.call("op", "PUT", "/api/chat-projects/cp_000000000000", json={"name": "Z"}).status_code == 404 + + +def test_delete_unfiles_and_keeps_every_file(world): + p = world.project() + (Path(p["folder"]) / "PROMETHEUS.md").write_text("keep") + sid = world.session() + world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + r = world.call("op", "DELETE", f"/api/chat-projects/{p['id']}") + assert r.status_code == 200 and r.json() == {"ok": True, "id": p["id"], "unfiled": 1} + assert (Path(p["folder"]) / "PROMETHEUS.md").read_text() == "keep" + assert world.conv.get_session_workspace(sid) is None + + +# ------------------------------------------------------------------------- filing + + +def test_filing_binds_the_workspace(world): + p = world.project() + sid = world.session() + r = world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + assert r.status_code == 200 + assert r.json() == {"ok": True, "session_id": sid, "chat_project_id": p["id"], "workspace": p["folder"]} + assert world.conv.get_session_workspace(sid) == p["folder"] + + +def test_a_hand_set_workspace_needs_a_flag(world): + p = world.project() + sid = world.session() + world.conv.set_session_workspace(sid, world.folder("mine"), set_by="rest") + r = world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + assert r.status_code == 409 and r.json()["kind"] == "workspace_set" and r.json()["workspace"].endswith("mine") + r = world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"], "keep_workspace": True}) + assert r.status_code == 200 and r.json()["workspace"].endswith("mine") + + +def test_unfiling(world): + p = world.project() + sid = world.session() + world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + r = world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": None}) + assert r.status_code == 200 and r.json()["chat_project_id"] is None + assert world.conv.get_session_workspace(sid) is None + + +@pytest.mark.parametrize(("sid", "status", "kind"), [("coding:run-1", 400, "machine_session"), + ("desktop:never-seen", 404, None)]) +def test_machine_and_unknown_sessions(world, sid, status, kind): + p = world.project() + r = world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + assert r.status_code == status + if kind: + assert r.json()["kind"] == kind + + +def test_an_unknown_project(world): + sid = world.session() + r = world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": "cp_000000000000"}) + assert r.status_code == 404 and r.json()["kind"] == "unknown_project" + + +def test_a_chat_only_devices_session_needs_a_flag_to_bind(world): + p = world.project() + sid = world.session("a") + r = world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + assert r.status_code == 409 and r.json()["kind"] == "device_session" + assert world.call("op", "PUT", f"/api/sessions/{sid}/project", + json={"chat_project_id": p["id"], "keep_workspace": True}).status_code == 200 + assert world.call("op", "PUT", f"/api/sessions/{sid}/project", + json={"chat_project_id": p["id"], "device_session": True}).status_code == 200 + + +def test_an_owner_devices_session_needs_no_flag(world): + p = world.project() + sid = world.session("owner") + assert world.call("op", "PUT", f"/api/sessions/{sid}/project", + json={"chat_project_id": p["id"]}).status_code == 200 + + +def test_a_folder_swapped_for_a_symlink_is_not_bound(world, tmp_path): + f = world.folder("swap") + p = world.project("Swap", folder=f) + os.rmdir(f) + elsewhere = tmp_path / "elsewhere" + elsewhere.mkdir() + os.symlink(elsewhere, f) + sid = world.session() + r = world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + assert r.status_code == 400 and r.json()["kind"] == "bad_folder" + + +@pytest.mark.parametrize("body", [{}, {"chat_project_id": 5}, {"chat_project_id": "x", "keep_workspace": "yes"}]) +def test_a_bad_filing_body(world, body): + sid = world.session() + assert world.call("op", "PUT", f"/api/sessions/{sid}/project", json=body).status_code == 400 + + +# ------------------------------------------------------------------------- session rows, new sessions + + +def test_session_rows_carry_the_project_durable_and_live(world): + p = world.project() + sid = world.session() + world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + rows = {r["session_id"]: r for r in world.call("op", "GET", "/api/sessions").json()} + assert rows[sid]["chat_project_id"] == p["id"] and rows[sid]["live"] is True + world.mgr._sessions.pop(sid, None) + rows = {r["session_id"]: r for r in world.call("op", "GET", "/api/sessions").json()} + assert rows[sid]["chat_project_id"] == p["id"] and rows[sid]["live"] is False + other = world.session() + rows = {r["session_id"]: r for r in world.call("op", "GET", "/api/sessions").json()} + assert rows[other]["chat_project_id"] is None + + +def test_a_new_session_can_start_in_a_project(world): + p = world.project() + r = world.call("op", "POST", "/api/sessions", json={"chat_project_id": p["id"]}) + assert r.status_code == 200 + sid = r.json()["session_id"] + assert r.json()["chat_project_id"] == p["id"] + assert world.conv.get_session_workspace(sid) == p["folder"] + + +def test_a_chat_only_device_cannot_start_in_a_project(world): + p = world.project() + before = len(world.call("op", "GET", "/api/sessions").json()) + r = world.call("a", "POST", "/api/sessions", json={"chat_project_id": p["id"]}) + assert r.status_code == 403 + assert len(world.call("op", "GET", "/api/sessions").json()) == before + + +def test_a_new_session_in_an_unknown_project(world): + r = world.call("op", "POST", "/api/sessions", json={"chat_project_id": "cp_000000000000"}) + assert r.status_code == 404 + + +# ------------------------------------------------------------------------- what each caller sees + + +def test_the_owner_sees_everything(world): + p = world.project() + for who in ("op", "owner"): + body = world.call(who, "GET", "/api/chat-projects").json() + assert body["role"] == "owner" and isinstance(body["rev"], int) + assert [x["id"] for x in body["projects"]] == [p["id"]] and "folder" in body["projects"][0] + + +def test_a_chat_only_device_sees_only_projects_with_its_sessions(world): + mine = world.project("Mine") + theirs = world.project("Theirs") + own = world.session("a") + other = world.session("b") + for sid, pid in ((own, mine["id"]), (other, mine["id"]), (world.session("b"), theirs["id"])): + world.call("op", "PUT", f"/api/sessions/{sid}/project", + json={"chat_project_id": pid, "keep_workspace": True}) + body = world.call("a", "GET", "/api/chat-projects").json() + assert body == {"role": "chat_only", "projects": [{"id": mine["id"], "name": "Mine", "sessions": 1}]} + one = world.call("a", "GET", f"/api/chat-projects/{mine['id']}").json() + assert one == {"id": mine["id"], "name": "Mine", "session_ids": [own]} + assert world.call("a", "GET", f"/api/chat-projects/{theirs['id']}").status_code == 404 + assert world.call("a", "GET", "/api/chat-projects/cp_000000000000").status_code == 404 + assert world.call("b", "GET", f"/api/chat-projects/{mine['id']}").json()["session_ids"] == [other] + + +def test_a_device_with_no_filed_sessions_sees_nothing(world): + world.project() + assert world.call("a", "GET", "/api/chat-projects").json() == {"role": "chat_only", "projects": []} + + +def test_the_owner_gets_one_project_with_its_sessions(world): + p = world.project() + sid = world.session() + world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + one = world.call("op", "GET", f"/api/chat-projects/{p['id']}").json() + assert one["session_ids"] == [sid] and one["folder"] == p["folder"] + + +# ------------------------------------------------------------------------- capability and frame + + +def test_the_capability(): + from prometheus.web.ws_server import WebSocketBridge + + assert "projects" in WebSocketBridge(loop_context=object(), config={})._server_capabilities() + + +def test_the_change_frame_goes_to_owner_sockets_only(world): + from prometheus.config.api_token import GLOBAL_IDENTITY, DeviceIdentity + + owner_sock, device_sock = _Socket(), _Socket() + world.bridge._clients.update({owner_sock, device_sock}) + world.bridge._ws_identity[owner_sock] = GLOBAL_IDENTITY + world.bridge._ws_identity[device_sock] = DeviceIdentity(id=world.a["id"]) + p = world.project("Framed") + frames = [f for f in owner_sock.frames if f["type"] == "chat_project_changed"] + assert frames and frames[-1]["payload"]["project_id"] == p["id"] and frames[-1]["payload"]["change"] == "created" + assert "Framed" not in json.dumps(frames) and p["folder"] not in json.dumps(frames) + assert not [f for f in device_sock.frames if f["type"] == "chat_project_changed"] + + +# ------------------------------------------------------------------------- reviews of #933 + + +@pytest.mark.parametrize("spelling", ["home_upper", "agent_upper", "around_home"]) +def test_folder_checks_go_by_identity_not_spelling(world, spelling): + (world.home / ".prometheus" / "state").mkdir(parents=True, exist_ok=True) + folder = { + "home_upper": str(world.home).upper() if os.path.exists(str(world.home).upper()) else None, + "agent_upper": str(world.home / ".PROMETHEUS" / "state") + if os.path.exists(world.home / ".PROMETHEUS") else None, + "around_home": str(world.home.parent), + }[spelling] + if folder is None: + pytest.skip("a case-sensitive filesystem") + r = world.call("op", "POST", "/api/chat-projects", json={"name": "Sneaky", "folder": folder}) + assert r.status_code == 400 and r.json()["kind"] == "bad_folder" + + +def test_the_same_folder_in_another_case_is_in_use(world): + f = world.folder("work/a") + world.project("A", folder=f) + other = f.replace("work/a", "WORK/A") + if not os.path.exists(other): + pytest.skip("a case-sensitive filesystem") + r = world.call("op", "POST", "/api/chat-projects", json={"name": "B", "folder": other}) + assert r.status_code in (400, 409) + assert len(world.call("op", "GET", "/api/chat-projects").json()["projects"]) == 1 + + +def test_a_symlinked_agent_home_is_followed_for_the_check(world, tmp_path): + real = tmp_path / "real-agent-home" + (real / "skills").mkdir(parents=True) + os.symlink(real, world.home / ".prometheus") + r = world.call("op", "POST", "/api/chat-projects", json={"name": "Skills", "folder": str(real / "skills")}) + assert r.status_code == 400 and r.json()["kind"] == "bad_folder" + + +@pytest.mark.parametrize("root", [None, "", "relative/root"]) +def test_a_null_empty_or_relative_root(world, root): + world.app.state.chat_projects._config["projects"] = {"root": root} + r = world.call("op", "POST", "/api/chat-projects", json={"name": "Rooted"}) + if root == "relative/root": + assert r.status_code == 400 and r.json()["kind"] == "bad_folder" + else: + assert r.status_code == 201 + assert Path(r.json()["project"]["folder"]).parent == (world.home / "prometheus-projects").resolve() + + +def test_a_root_inside_a_denied_path_makes_nothing(world, tmp_path): + world.app.state.chat_projects._config["projects"] = {"root": str(tmp_path / "secret" / "deep" / "root")} + r = world.call("op", "POST", "/api/chat-projects", json={"name": "Nope"}) + assert r.status_code == 400 and r.json()["kind"] == "bad_folder" + assert not (tmp_path / "secret" / "deep").exists() + + +def test_a_forgotten_session_no_longer_counts(world): + p = world.project() + a, b = world.session(), world.session() + for sid in (a, b): + world.call("op", "PUT", f"/api/sessions/{sid}/project", json={"chat_project_id": p["id"]}) + world.call("op", "DELETE", f"/api/sessions/{b}") + one = world.call("op", "GET", f"/api/chat-projects/{p['id']}").json() + assert one["sessions"] == 1 and one["session_ids"] == [a] + + +def test_a_large_instructions_file_has_the_true_rev(world): + import hashlib + + p = world.project() + data = ("x" * 1023 + "\n").encode() * 2048 # 2 MB + (Path(p["folder"]) / "PROMETHEUS.md").write_bytes(data) + got = world.call("op", "GET", f"/api/chat-projects/{p['id']}").json() + assert got["instructions_rev"] == hashlib.sha256(data).hexdigest()[:16] + assert got["instructions_chars"] == len(data) + + +def test_a_managed_create_never_collides_with_a_stored_folder(world): + p = world.project("garden") + os.rmdir(p["folder"]) + r = world.call("op", "POST", "/api/chat-projects", json={"name": "garden!"}) + assert r.status_code == 409 and r.json()["kind"] == "name_taken" or r.status_code in (200, 201) + r = world.call("op", "POST", "/api/chat-projects", json={"name": "garden two"}) + assert r.status_code == 201 + + +def test_an_existing_name_with_a_bad_folder_is_name_taken(world): + world.project("Beacon") + r = world.call("op", "POST", "/api/chat-projects", json={"name": "beacon", "folder": "/no/such/folder"}) + assert r.status_code == 409 and r.json()["kind"] == "name_taken" + + +def test_a_machine_gateway_in_a_project_creates_nothing(world): + p = world.project() + before = set(world.mgr._sessions) + r = world.call("op", "POST", "/api/sessions", json={"gateway": "smoke", "chat_project_id": p["id"]}) + assert r.status_code == 400 and r.json()["kind"] == "machine_session" + assert set(world.mgr._sessions) == before + + +def test_every_error_has_a_kind(world): + p = world.project() + r = world.call("op", "PUT", "/api/sessions/desktop:nobody/project", json={"chat_project_id": p["id"]}) + assert r.status_code == 404 and r.json()["kind"] == "unknown_session" + r = world.call("op", "POST", "/api/sessions", json={"chat_project_id": 5}) + assert r.status_code == 400 and r.json()["kind"] == "bad_request" + + +def test_a_session_with_a_project_binding_is_not_claimable(world): + from prometheus.web.session_scope import session_exists + + p = world.project() + sid = world.call("op", "POST", "/api/sessions", json={"chat_project_id": p["id"]}).json()["session_id"] + world.mgr._sessions.pop(sid, None) # as after a restart, with no message yet + assert session_exists(world.mgr, world.conv, sid) + assert world.call("a", "PUT", f"/api/sessions/{sid}/title", json={"title": "mine now"}).status_code == 404 diff --git a/tests/test_route_access.py b/tests/test_route_access.py index 624c2f42..f8e3a457 100644 --- a/tests/test_route_access.py +++ b/tests/test_route_access.py @@ -53,6 +53,9 @@ ("GET", "/api/events/recent"), ("GET", "/api/activity/recent"), ("POST", "/api/search"), + # chat projects: only those its own sessions are in, as id and name (the handlers filter) + ("GET", "/api/chat-projects"), + ("GET", "/api/chat-projects/{project_id}"), # speech, with chat's limits: a session's reply only for a session it owns (the session path guard) ("GET", "/api/speech"), ("POST", "/api/speech/transcribe"),