From 341786a31f543d0a0f2a5be905131e8a5028f90e Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:20:53 -0500 Subject: [PATCH 01/16] test(projects): pin the chat projects store and managed folders (red) Co-Authored-By: Claude Opus 5.5 --- tests/test_chat_projects_store.py | 234 ++++++++++++++++++++++++++++++ 1 file changed, 234 insertions(+) create mode 100644 tests/test_chat_projects_store.py diff --git a/tests/test_chat_projects_store.py b/tests/test_chat_projects_store.py new file mode 100644 index 00000000..0f6e2665 --- /dev/null +++ b/tests/test_chat_projects_store.py @@ -0,0 +1,234 @@ +"""``projects/store.py`` and ``projects/folders.py``: the chat projects store (docs/design/chat-projects.md, 1-3). + +Real conversation store on a temp file throughout. Pinned: name cleaning and case-insensitive uniqueness, folder and +import-key uniqueness, the change counter, display order, filing (membership and workspace binding in one step, a +hand-set workspace never replaced unless asked, unfiling clears only what the project set), delete (unfiles, clears +the project's bindings, deletes no file), the chat-only view (only projects with the caller's own sessions), the +forget cascade, and the managed folder (visible root, slug, collisions, mode 0700). +""" + +from __future__ import annotations + +import os +import stat +from pathlib import Path + +import pytest + +from prometheus.memory.lcm_conversation_store import LCMConversationStore +from prometheus.projects.folders import make_managed_folder, projects_root, slug +from prometheus.projects.store import ( + FolderInUse, + NameTaken, + UnknownProject, + WorkspaceSet, + clean_name, +) + + +@pytest.fixture +def conv(tmp_path): + return LCMConversationStore(tmp_path / "lcm.db") + + +@pytest.fixture +def store(conv): + return conv.chat_projects() + + +def _folder(tmp_path: Path, name: str) -> str: + p = tmp_path / "folders" / name + p.mkdir(parents=True, exist_ok=True) + return str(p) + + +class TestNames: + @pytest.mark.parametrize(("raw", "clean"), [ + (" Kitchen remodel ", "Kitchen remodel"), ("a\tb\nc", "a b c"), ("x" * 80, "x" * 60), (" ", ""), + ]) + def test_clean(self, raw, clean): + assert clean_name(raw) == clean + + def test_names_are_unique_in_any_case(self, store, tmp_path): + store.insert("Beacon", _folder(tmp_path, "a"), managed=False) + with pytest.raises(NameTaken): + store.insert("beacon", _folder(tmp_path, "b"), managed=False) + + def test_by_name_ignores_case_and_spacing(self, store, tmp_path): + p = store.insert("Kitchen remodel", _folder(tmp_path, "k"), managed=False) + assert store.by_name(" kitchen REMODEL ").id == p.id + + def test_rename_onto_another_name_is_refused_and_own_case_change_is_not(self, store, tmp_path): + a = store.insert("Alpha", _folder(tmp_path, "a"), managed=False) + store.insert("Beta", _folder(tmp_path, "b"), managed=False) + with pytest.raises(NameTaken): + store.rename(a.id, "BETA") + assert store.rename(a.id, "ALPHA").name == "ALPHA" + + def test_rename_an_unknown_project(self, store): + with pytest.raises(UnknownProject): + store.rename("cp_000000000000", "X") + + +class TestFoldersAndKeys: + def test_a_folder_is_used_by_one_project(self, store, tmp_path): + f = _folder(tmp_path, "shared") + store.insert("One", f, managed=False) + with pytest.raises(FolderInUse): + store.insert("Two", f, managed=False) + + def test_import_key_finds_the_project(self, store, tmp_path): + p = store.insert("Imported", _folder(tmp_path, "i"), managed=False, import_key="local:p123") + assert store.by_import_key("local:p123").id == p.id + assert store.by_import_key("local:other") is None + + def test_ids_are_opaque_and_prefixed(self, store, tmp_path): + p = store.insert("X", _folder(tmp_path, "x"), managed=True) + assert p.id.startswith("cp_") and len(p.id) == 15 + assert p.folder_managed is True + + +class TestRevAndOrder: + def test_every_change_raises_rev(self, store, tmp_path): + r0 = store.rev() + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + r1 = store.rev() + store.rename(p.id, "B") + r2 = store.rev() + store.file("s1", p.id) + r3 = store.rev() + store.file("s1", None) + r4 = store.rev() + store.delete(p.id) + assert r0 < r1 < r2 < r3 < r4 < store.rev() + + def test_list_is_creation_order_with_session_counts(self, store, tmp_path): + b = store.insert("Bravo", _folder(tmp_path, "b"), managed=False) + a = store.insert("Alpha", _folder(tmp_path, "a"), managed=False) + store.file("s1", a.id) + store.file("s2", a.id) + listed = store.list() + assert [p.id for p in listed] == [b.id, a.id] + assert {p.id: p.sessions for p in listed} == {b.id: 0, a.id: 2} + + +class TestFiling: + def test_filing_binds_the_workspace_to_the_folder(self, store, conv, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + result = store.file("s1", p.id) + assert result.workspace == p.folder + assert conv.get_session_workspace("s1") == p.folder + assert store.project_of("s1") == p.id + + def test_a_hand_set_workspace_is_not_replaced_unless_asked(self, store, conv, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + conv.set_session_workspace("s1", "/somewhere/else", set_by="rest") + with pytest.raises(WorkspaceSet) as info: + store.file("s1", p.id) + assert info.value.workspace == "/somewhere/else" + assert store.project_of("s1") is None and conv.get_session_workspace("s1") == "/somewhere/else" + store.file("s1", p.id, replace_workspace=True) + assert conv.get_session_workspace("s1") == p.folder + + def test_moving_between_projects_rebinds(self, store, conv, tmp_path): + a = store.insert("A", _folder(tmp_path, "a"), managed=False) + b = store.insert("B", _folder(tmp_path, "b"), managed=False) + store.file("s1", a.id) + store.file("s1", b.id) + assert store.project_of("s1") == b.id and conv.get_session_workspace("s1") == b.folder + + def test_unfiling_clears_only_what_the_project_set(self, store, conv, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + store.file("s1", p.id) + store.file("s1", None) + assert conv.get_session_workspace("s1") is None and store.project_of("s1") is None + store.file("s2", p.id) + conv.set_session_workspace("s2", "/chosen/by/hand", set_by="rest") + store.file("s2", None) + assert conv.get_session_workspace("s2") == "/chosen/by/hand" + + def test_filing_is_idempotent(self, store, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + store.file("s1", p.id) + store.file("s1", p.id) + assert store.session_ids(p.id) == ["s1"] + + def test_filing_into_an_unknown_project(self, store): + with pytest.raises(UnknownProject): + store.file("s1", "cp_000000000000") + + def test_projects_of_many_sessions(self, store, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + store.file("s1", p.id) + assert store.projects_of(["s1", "s2"]) == {"s1": p.id} + + +class TestDelete: + def test_delete_unfiles_clears_its_bindings_and_keeps_every_file(self, store, conv, tmp_path): + folder = _folder(tmp_path, "keep") + (Path(folder) / "PROMETHEUS.md").write_text("keep me") + p = store.insert("A", folder, managed=False) + store.file("s1", p.id) + store.file("s2", p.id) + conv.set_session_workspace("s2", "/by/hand", set_by="rest") + unfiled = store.delete(p.id) + assert unfiled == 2 + assert store.get(p.id) is None and store.project_of("s1") is None + assert conv.get_session_workspace("s1") is None + assert conv.get_session_workspace("s2") == "/by/hand" + assert (Path(folder) / "PROMETHEUS.md").read_text() == "keep me" + + def test_delete_an_unknown_project(self, store): + with pytest.raises(UnknownProject): + store.delete("cp_000000000000") + + +class TestChatOnlyView: + def test_only_projects_with_the_callers_own_sessions(self, store, tmp_path): + mine = store.insert("Mine", _folder(tmp_path, "m"), managed=False) + store.insert("Theirs", _folder(tmp_path, "t"), managed=False) + store.file("own-1", mine.id) + store.file("other-1", mine.id) + view = store.visible_to({"own-1", "own-2"}) + assert [(v.id, v.name, v.own_session_ids) for v in view] == [(mine.id, "Mine", ["own-1"])] + + def test_nothing_owned_sees_nothing(self, store, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + store.file("s1", p.id) + assert store.visible_to(set()) == [] + + +def test_forgetting_a_session_removes_its_membership(conv, store, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + store.file("s1", p.id) + conv.purge_session("s1") + assert store.project_of("s1") is None + assert store.get(p.id) is not None # the project stays + + +class TestManagedFolder: + @pytest.mark.parametrize(("name", "expected"), [ + ("Kitchen remodel", "kitchen-remodel"), (" Ünïcode & stuff!! ", "nicode-stuff"), + ("---", "project"), ("a" * 80, "a" * 48), ("Beacon / iOS", "beacon-ios"), + ]) + def test_slug(self, name, expected): + assert slug(name) == expected + + def test_root_defaults_to_a_visible_home_folder(self, monkeypatch, tmp_path): + monkeypatch.setenv("HOME", str(tmp_path)) + assert projects_root({}) == tmp_path / "prometheus-projects" + assert projects_root({"projects": {"root": "~/elsewhere"}}) == tmp_path / "elsewhere" + + def test_made_with_mode_0700_and_a_suffix_on_collision(self, tmp_path): + root = tmp_path / "root" + first = make_managed_folder(root, "Kitchen remodel") + second = make_managed_folder(root, "kitchen REMODEL") + assert first == root / "kitchen-remodel" and second == root / "kitchen-remodel-2" + for p in (root, first, second): + assert stat.S_IMODE(os.stat(p).st_mode) == 0o700 + + def test_an_existing_file_in_the_way_is_skipped(self, tmp_path): + root = tmp_path / "root" + root.mkdir() + (root / "notes").write_text("a file, not a folder") + assert make_managed_folder(root, "Notes") == root / "notes-2" From 87f1614172e656c260d8a15c676c0986b8fbe9bf Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:27:10 -0500 Subject: [PATCH 02/16] test(projects): label-only filing (red) Co-Authored-By: Claude Opus 5.5 --- tests/test_chat_projects_store.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/test_chat_projects_store.py b/tests/test_chat_projects_store.py index 0f6e2665..81fae128 100644 --- a/tests/test_chat_projects_store.py +++ b/tests/test_chat_projects_store.py @@ -107,7 +107,7 @@ def test_list_is_creation_order_with_session_counts(self, store, tmp_path): a = store.insert("Alpha", _folder(tmp_path, "a"), managed=False) store.file("s1", a.id) store.file("s2", a.id) - listed = store.list() + listed = store.all() assert [p.id for p in listed] == [b.id, a.id] assert {p.id: p.sessions for p in listed} == {b.id: 0, a.id: 2} @@ -208,7 +208,7 @@ def test_forgetting_a_session_removes_its_membership(conv, store, tmp_path): class TestManagedFolder: @pytest.mark.parametrize(("name", "expected"), [ - ("Kitchen remodel", "kitchen-remodel"), (" Ünïcode & stuff!! ", "nicode-stuff"), + ("Kitchen remodel", "kitchen-remodel"), (" Ünïcode & stuff!! ", "unicode-stuff"), ("---", "project"), ("a" * 80, "a" * 48), ("Beacon / iOS", "beacon-ios"), ]) def test_slug(self, name, expected): @@ -232,3 +232,27 @@ def test_an_existing_file_in_the_way_is_skipped(self, tmp_path): root.mkdir() (root / "notes").write_text("a file, not a folder") assert make_managed_folder(root, "Notes") == root / "notes-2" + + +class TestLabelOnly: + """keep_workspace: a migration files as a label only, and never changes where a conversation writes.""" + + def test_label_only_leaves_no_workspace_alone(self, store, conv, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + result = store.file("s1", p.id, keep_workspace=True) + assert store.project_of("s1") == p.id + assert result.workspace is None and conv.get_session_workspace("s1") is None + + def test_label_only_leaves_a_hand_set_workspace_alone(self, store, conv, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + conv.set_session_workspace("s1", "/by/hand", set_by="rest") + result = store.file("s1", p.id, keep_workspace=True) + assert result.workspace == "/by/hand" and conv.get_session_workspace("s1") == "/by/hand" + store.file("s1", None) + assert conv.get_session_workspace("s1") == "/by/hand" + + def test_label_only_after_a_bind_keeps_the_projects_binding(self, store, conv, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + store.file("s1", p.id) + store.file("s1", p.id, keep_workspace=True) + assert conv.get_session_workspace("s1") == p.folder From 4c7dcfa7294a1afda4814054ce76d40b6ce3a322 Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:27:25 -0500 Subject: [PATCH 03/16] feat(projects): the chat projects store and managed folders Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + .../memory/lcm_conversation_store.py | 16 +- src/prometheus/projects/__init__.py | 3 + src/prometheus/projects/folders.py | 59 ++++ src/prometheus/projects/store.py | 298 ++++++++++++++++++ 5 files changed, 376 insertions(+), 1 deletion(-) create mode 100644 src/prometheus/projects/__init__.py create mode 100644 src/prometheus/projects/folders.py create mode 100644 src/prometheus/projects/store.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 127e1896..3efc0f94 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 (groundwork): `prometheus.projects` adds the store from `docs/design/chat-projects.md`: projects with a folder, unique names in any case, conversations filed in at most one, and filing that binds the conversation's workspace to the folder (or records a label only), in one transaction with a change counter. Managed folders are made under `projects.root` (default `~/prometheus-projects`) with mode 0700. Forgetting a conversation removes its filing. No routes yet. - CI: the parity replay job runs on `ubuntu-24.04`, pinned, instead of `ubuntu-latest`. When `ubuntu-latest` moved to 26.04, the longer OS line in the system prompt raised the compaction scenario's token estimate by one and failed it, with every model request still matching. No behaviour changes. - Docs: `docs/reference/pairing-join-requests.md` is the reference for how a new device asks to join and how the owner lets it in: the routes and who may call them, every request and response field, the statuses, the expiry and rate limits, the sealed token, the operator's WebSocket frames and the owner code (the explicit, single-use grant that makes an owner device). It is written from the code and pinned to it by `tests/test_pairing_reference.py`. No behaviour changes. - Approvals: Prometheus now says when nothing can answer an approval prompt. `security.approval_queue.enabled` ships `false`, and the queue is only built together with the Telegram gateway; without it every call that needs an approval (a third-party MCP tool, a write or a shell command in `cautious` or `strict` mode, a network or install command in a background turn) is refused within seconds as "Permission denied" and none is ever listed to approve, so `cautious` read as "asks the owner" and behaved as "refuses everything". The daemon logs a warning at start and `oara doctor` has an "Approval prompts" row, both naming the keys to set. Nothing is switched on for you. diff --git a/src/prometheus/memory/lcm_conversation_store.py b/src/prometheus/memory/lcm_conversation_store.py index f0b0faea..e32ed431 100644 --- a/src/prometheus/memory/lcm_conversation_store.py +++ b/src/prometheus/memory/lcm_conversation_store.py @@ -12,6 +12,7 @@ import time from collections.abc import Iterable from pathlib import Path +from typing import TYPE_CHECKING from uuid import uuid4 from prometheus.config.paths import get_lcm_db_path @@ -19,6 +20,9 @@ from prometheus.memory.lcm_types import MessagePart from prometheus.security.log_redaction import redact_json_text, redact_secrets +if TYPE_CHECKING: + from prometheus.projects.store import ChatProjectStore + log = logging.getLogger(__name__) # ``turn_index`` is a message's durable prompt position: unique within its @@ -1126,7 +1130,7 @@ def purge_session(self, session_id: str) -> dict[str, int]: # here, not a silent survivor. for table in ("session_titles", "session_profiles", "session_pins", "session_tombstones", "session_forks", "session_workspaces", - "session_backends", "message_client_ids"): + "session_backends", "message_client_ids", "session_chat_projects"): counts[table] = self._conn.execute( f"DELETE FROM {table} WHERE session_id = ?", (session_id,) ).rowcount if self._table_exists(table) else 0 @@ -1161,6 +1165,16 @@ def purge_session(self, session_id: str) -> dict[str, int]: self._conn.execute("PRAGMA wal_checkpoint(TRUNCATE)") return counts + 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.""" + store = getattr(self, "_chat_projects", None) + if store is None: + from prometheus.projects.store import ChatProjectStore + + store = self._chat_projects = ChatProjectStore(self._conn) + return store + def _table_exists(self, name: str) -> bool: return bool( self._conn.execute( diff --git a/src/prometheus/projects/__init__.py b/src/prometheus/projects/__init__.py new file mode 100644 index 00000000..535c257f --- /dev/null +++ b/src/prometheus/projects/__init__.py @@ -0,0 +1,3 @@ +"""Chat projects: a named folder on this Prometheus's computer, with standing instructions and reference files, that +conversations are filed in (docs/design/chat-projects.md). Per instance: no project spans daemons. +""" diff --git a/src/prometheus/projects/folders.py b/src/prometheus/projects/folders.py new file mode 100644 index 00000000..cef3a2a9 --- /dev/null +++ b/src/prometheus/projects/folders.py @@ -0,0 +1,59 @@ +"""Where managed project folders live, and how one is made (docs/design/chat-projects.md, section 1.1). + +The projects root is config ``projects.root``, default ``~/prometheus-projects``: a visible folder in the owner's +home directory, not inside the agent's workspace, with no space in its name so the agent's shell commands need no +quoting. A managed folder is named from the project's name (:func:`slug`), with ``-2``, ``-3``, ... when that name is +already taken on disk, and made with mode 0700, as is the root. Nothing here deletes a folder. + +Validation against ``security.denied_paths`` is the caller's (it has the security config); this module only makes +directories. + +Source: novel code for Prometheus, 2026-11-13. +""" + +from __future__ import annotations + +import os +import re +import unicodedata +from collections.abc import Mapping +from pathlib import Path +from typing import Any + +DEFAULT_ROOT = "~/prometheus-projects" +SLUG_MAX = 48 +_MAX_SUFFIX = 1000 + + +def projects_root(config: Mapping[str, Any] | None) -> Path: + """The projects root from ``projects.root`` (``~`` expanded), or the default.""" + block = (config or {}).get("projects") + raw = block.get("root") if isinstance(block, Mapping) else None + return Path(os.path.expanduser(str(raw or DEFAULT_ROOT))) + + +def slug(name: str) -> str: + """A folder name from a project name: lowercase ASCII letters, digits and ``-``; ``project`` when nothing is left.""" + ascii_only = unicodedata.normalize("NFKD", name).encode("ascii", "ignore").decode("ascii") + out = re.sub(r"[^a-z0-9]+", "-", ascii_only.lower()).strip("-")[:SLUG_MAX].strip("-") + return out or "project" + + +def make_managed_folder(root: Path, name: str) -> Path: + """Make ``/`` (or ``-2``, ``-3``, ...) with mode 0700, and the root too if it is missing. + + ``mkdir`` without ``exist_ok`` is the claim, so two creates at once never share a folder, and anything already at + a name (a file, a folder) moves on to the next suffix. + """ + root.mkdir(mode=0o700, parents=True, exist_ok=True) + os.chmod(root, 0o700) + base = slug(name) + for n in range(1, _MAX_SUFFIX): + candidate = root / (base if n == 1 else f"{base}-{n}") + try: + candidate.mkdir(mode=0o700) + except FileExistsError: + continue + os.chmod(candidate, 0o700) # mkdir's mode is masked by the umask + return candidate + raise FileExistsError(f"no free folder name for {base!r} under {root}") diff --git a/src/prometheus/projects/store.py b/src/prometheus/projects/store.py new file mode 100644 index 00000000..2ad06ac9 --- /dev/null +++ b/src/prometheus/projects/store.py @@ -0,0 +1,298 @@ +"""The chat projects store (docs/design/chat-projects.md, sections 1 to 3). + +Tables in the conversation store, on its connection, beside ``session_workspaces``: filing a session changes its +membership and its workspace binding together, in one transaction, so the two can never disagree. + +* ``chat_projects``: one row per project; the cleaned name is unique without regard to case, the folder and an import + key are unique. +* ``session_chat_projects``: a session is in at most one project; presence is the filing. +* ``chat_projects_rev``: one counter, raised in the same transaction as every change to a project or a membership. + +Filing binds the session's workspace to the project's folder with ``set_by = "project"``. A workspace set any other +way is never replaced unless the caller says so (:class:`WorkspaceSet`). Unfiling, moving and deleting clear only a +binding the project set. Deleting a project never touches the folder or a file in it. + +Folder validation (``validate_workspace_path``) and the managed folder are the caller's: the store does not know the +filesystem or the security config. + +Source: novel code for Prometheus, 2026-11-13. +""" + +from __future__ import annotations + +import re +import secrets +import sqlite3 +import time +from collections.abc import Iterable +from dataclasses import dataclass, field + +NAME_MAX = 60 +SET_BY = "project" + +SCHEMA = """ +CREATE TABLE IF NOT EXISTS chat_projects ( + id TEXT PRIMARY KEY, + name TEXT NOT NULL, + name_folded TEXT NOT NULL UNIQUE, + folder TEXT NOT NULL UNIQUE, + folder_managed INTEGER NOT NULL, + import_key TEXT UNIQUE, + position REAL NOT NULL, + created_at REAL NOT NULL, + updated_at REAL NOT NULL +); +CREATE TABLE IF NOT EXISTS session_chat_projects ( + session_id TEXT PRIMARY KEY, + project_id TEXT NOT NULL, + filed_at REAL NOT NULL +); +CREATE INDEX IF NOT EXISTS idx_session_chat_projects_project ON session_chat_projects (project_id); +CREATE TABLE IF NOT EXISTS chat_projects_rev ( + id INTEGER PRIMARY KEY CHECK (id = 1), + rev INTEGER NOT NULL +); +""" + + +def clean_name(raw: str) -> str: + """Trimmed, inner whitespace collapsed, at most :data:`NAME_MAX` characters. ``""`` means no name.""" + return re.sub(r"\s+", " ", raw or "").strip()[:NAME_MAX].strip() + + +class ProjectError(Exception): + kind = "error" + + +class NameTaken(ProjectError): + kind = "name_taken" + + +class FolderInUse(ProjectError): + kind = "folder_in_use" + + +class UnknownProject(ProjectError): + kind = "unknown_project" + + +class WorkspaceSet(ProjectError): + """The session's workspace was set another way; filing would replace it.""" + + kind = "workspace_set" + + def __init__(self, workspace: str) -> None: + super().__init__(f"the conversation already works in {workspace}") + self.workspace = workspace + + +@dataclass(frozen=True) +class Project: + id: str + name: str + folder: str + folder_managed: bool + import_key: str | None + created_at: float + updated_at: float + sessions: int = 0 + + +@dataclass(frozen=True) +class FileResult: + session_id: str + project_id: str | None + workspace: str | None + + +@dataclass(frozen=True) +class VisibleProject: + """What a chat-only device may know of a project: its id, its name, and which of the device's sessions are in it.""" + + id: str + name: str + own_session_ids: list[str] = field(default_factory=list) + + +class ChatProjectStore: + """The store, on the conversation store's connection (``LCMConversationStore.chat_projects()``).""" + + def __init__(self, conn: sqlite3.Connection) -> None: + self._conn = conn + conn.executescript(SCHEMA) + conn.execute("INSERT OR IGNORE INTO chat_projects_rev (id, rev) VALUES (1, 0)") + conn.commit() + + # ---------------------------------------------------------------- reads + def rev(self) -> int: + row = self._conn.execute("SELECT rev FROM chat_projects_rev WHERE id = 1").fetchone() + return int(row[0]) if row else 0 + + 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" 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])) + for r in self._conn.execute(sql, args).fetchall()] + + def all(self) -> list[Project]: + """Every project, in display order, with its session count.""" + return self._rows() + + def get(self, project_id: str) -> Project | None: + rows = self._rows("WHERE p.id = ?", (project_id,)) + return rows[0] if rows else None + + def by_name(self, name: str) -> Project | None: + folded = clean_name(name).lower() + rows = self._rows("WHERE p.name_folded = ?", (folded,)) if folded else [] + return rows[0] if rows else None + + def by_folder(self, folder: str) -> Project | None: + rows = self._rows("WHERE p.folder = ?", (folder,)) + return rows[0] if rows else None + + def by_import_key(self, key: str) -> Project | None: + rows = self._rows("WHERE p.import_key = ?", (key,)) if key else [] + return rows[0] if rows else None + + def project_of(self, session_id: str) -> str | None: + row = self._conn.execute( + "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]: + ids = list(session_ids) + out: dict[str, str] = {} + 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})", + 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()] + + 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) + by_project: dict[str, list[str]] = {} + for sid, pid in filed.items(): + by_project.setdefault(pid, []).append(sid) + return [VisibleProject(p.id, p.name, sorted(by_project[p.id])) for p in self.all() if p.id in by_project] + + # --------------------------------------------------------------- writes + def _bump(self) -> None: + self._conn.execute("UPDATE chat_projects_rev SET rev = rev + 1 WHERE id = 1") + + def insert(self, name: str, folder: str, *, managed: bool, import_key: str | None = None) -> Project: + """A new project. :class:`NameTaken` / :class:`FolderInUse` when another project has the name or folder.""" + clean = clean_name(name) + if not clean: + raise ValueError("a project needs a name") + now = time.time() + pid = "cp_" + secrets.token_hex(6) + try: + with self._conn: + position = self._conn.execute( + "SELECT COALESCE(MAX(position), 0) + 1 FROM chat_projects").fetchone()[0] + self._conn.execute( + "INSERT INTO chat_projects (id, name, name_folded, folder, folder_managed, import_key, position," + " created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", + (pid, clean, clean.lower(), folder, int(managed), import_key or None, position, now, now)) + self._bump() + except sqlite3.IntegrityError as exc: + message = str(exc) + if "name_folded" in message: + raise NameTaken(f"a project is already called {clean!r}") from exc + if "folder" in message: + raise FolderInUse("another project already uses that folder") from exc + raise + project = self.get(pid) + assert project is not None + return project + + def rename(self, project_id: str, name: str) -> Project: + clean = clean_name(name) + if not clean: + raise ValueError("a project needs a name") + try: + with self._conn: + cur = self._conn.execute( + "UPDATE chat_projects SET name = ?, name_folded = ?, updated_at = ? WHERE id = ?", + (clean, clean.lower(), time.time(), project_id)) + if cur.rowcount == 0: + raise UnknownProject("no such project") + self._bump() + except sqlite3.IntegrityError as exc: + raise NameTaken(f"a project is already called {clean!r}") from exc + project = self.get(project_id) + assert project is not None + return project + + def delete(self, project_id: str) -> int: + """Remove the project and its memberships; clear the bindings it set. Returns how many sessions were unfiled. + + Never touches the folder or any file in it. + """ + with self._conn: + if self._conn.execute("SELECT 1 FROM chat_projects WHERE id = ?", (project_id,)).fetchone() is None: + raise UnknownProject("no such project") + sessions = [r[0] for r in self._conn.execute( + "SELECT session_id FROM session_chat_projects WHERE project_id = ?", (project_id,)).fetchall()] + for sid in sessions: + self._clear_project_workspace(sid) + self._conn.execute("DELETE FROM session_chat_projects WHERE project_id = ?", (project_id,)) + self._conn.execute("DELETE FROM chat_projects WHERE id = ?", (project_id,)) + self._bump() + return len(sessions) + + def file(self, session_id: str, project_id: str | None, *, replace_workspace: bool = False, + keep_workspace: bool = False) -> FileResult: + """File *session_id* in *project_id* (``None`` unfiles), binding or clearing its workspace in the same step. + + *keep_workspace* files as a label only: the membership is recorded and the workspace is left as it is (what a + migration sends, so moving local projects in never changes where an old conversation writes). + """ + with self._conn: + if project_id is None: + self._conn.execute("DELETE FROM session_chat_projects WHERE session_id = ?", (session_id,)) + self._clear_project_workspace(session_id) + self._bump() + return FileResult(session_id, None, None) + row = self._conn.execute("SELECT folder FROM chat_projects WHERE id = ?", (project_id,)).fetchone() + if row is None: + raise UnknownProject("no such project") + folder = row[0] + current = self._conn.execute( + "SELECT path, set_by FROM session_workspaces WHERE session_id = ?", (session_id,)).fetchone() + if (not keep_workspace and current is not None and current[1] != SET_BY and current[0] != folder + and not replace_workspace): + raise WorkspaceSet(current[0]) + now = time.time() + self._conn.execute( + "INSERT INTO session_chat_projects (session_id, project_id, filed_at) VALUES (?, ?, ?)" + " ON CONFLICT(session_id) DO UPDATE SET project_id = excluded.project_id," + " filed_at = CASE WHEN session_chat_projects.project_id = excluded.project_id" + " THEN session_chat_projects.filed_at ELSE excluded.filed_at END", + (session_id, project_id, now)) + if keep_workspace: + self._bump() + return FileResult(session_id, project_id, current[0] if current is not None else None) + self._conn.execute( + "INSERT OR REPLACE INTO session_workspaces (session_id, path, set_by, updated_at) VALUES (?, ?, ?, ?)", + (session_id, folder, SET_BY, now)) + self._bump() + return FileResult(session_id, project_id, folder) + + def _clear_project_workspace(self, session_id: str) -> None: + self._conn.execute( + "DELETE FROM session_workspaces WHERE session_id = ? AND set_by = ?", (session_id, SET_BY)) From d5a482b7df5cbef9a5ecc37702f13d2efb66f1f2 Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:28:27 -0500 Subject: [PATCH 04/16] test(projects): bindings name the project that set them (red) Co-Authored-By: Claude Opus 5.5 --- tests/test_chat_projects_store.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_chat_projects_store.py b/tests/test_chat_projects_store.py index 81fae128..07646f04 100644 --- a/tests/test_chat_projects_store.py +++ b/tests/test_chat_projects_store.py @@ -256,3 +256,30 @@ def test_label_only_after_a_bind_keeps_the_projects_binding(self, store, conv, t store.file("s1", p.id) store.file("s1", p.id, keep_workspace=True) assert conv.get_session_workspace("s1") == p.folder + + +class TestBindingsNameTheirProject: + """Design re-check: a binding records which project set it; only that project clears it.""" + + def test_a_label_move_then_deleting_the_old_project_clears_its_binding(self, store, conv, tmp_path): + a = store.insert("A", _folder(tmp_path, "a"), managed=False) + b = store.insert("B", _folder(tmp_path, "b"), managed=False) + store.file("s1", a.id) # bound to A's folder + store.file("s1", b.id, keep_workspace=True) # now a member of B, still bound to A's folder + store.delete(a.id) + assert conv.get_session_workspace("s1") is None # A's binding went with A + assert store.project_of("s1") == b.id + + def test_unfiling_from_a_label_project_leaves_another_projects_binding(self, store, conv, tmp_path): + a = store.insert("A", _folder(tmp_path, "a"), managed=False) + b = store.insert("B", _folder(tmp_path, "b"), managed=False) + store.file("s1", a.id) + store.file("s1", b.id, keep_workspace=True) + store.file("s1", None) + assert conv.get_session_workspace("s1") == a.folder # B never set it, so B's unfile leaves it + + def test_the_binding_records_the_project(self, store, conv, tmp_path): + a = store.insert("A", _folder(tmp_path, "a"), managed=False) + store.file("s1", a.id) + row = conv._conn.execute("SELECT set_by FROM session_workspaces WHERE session_id = 's1'").fetchone() + assert row[0] == f"project:{a.id}" From 571aefeaf644ce0fb740089c47291cf85b7dae6c Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:28:40 -0500 Subject: [PATCH 05/16] fix(projects): a binding names the project that set it; only that project clears it Co-Authored-By: Claude Opus 5.5 --- src/prometheus/projects/store.py | 35 +++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/prometheus/projects/store.py b/src/prometheus/projects/store.py index 2ad06ac9..d36866db 100644 --- a/src/prometheus/projects/store.py +++ b/src/prometheus/projects/store.py @@ -8,9 +8,10 @@ * ``session_chat_projects``: a session is in at most one project; presence is the filing. * ``chat_projects_rev``: one counter, raised in the same transaction as every change to a project or a membership. -Filing binds the session's workspace to the project's folder with ``set_by = "project"``. A workspace set any other -way is never replaced unless the caller says so (:class:`WorkspaceSet`). Unfiling, moving and deleting clear only a -binding the project set. Deleting a project never touches the folder or a file in it. +Filing binds the session's workspace to the project's folder with ``set_by = "project:"``. A workspace set any +other way is never replaced unless the caller says so (:class:`WorkspaceSet`). Unfiling, moving and deleting clear +only a binding that same project set, so a session moved to another project as a label keeps (and, on the first +project's delete, loses) the first project's binding correctly. Deleting a project never touches the folder or a file in it. Folder validation (``validate_workspace_path``) and the managed folder are the caller's: the store does not know the filesystem or the security config. @@ -28,7 +29,12 @@ from dataclasses import dataclass, field NAME_MAX = 60 -SET_BY = "project" +#: A binding a project set is ``set_by = "project:"``: the id says which, so only that project clears it. +SET_BY_PREFIX = "project:" + + +def _set_by(project_id: str) -> str: + return SET_BY_PREFIX + project_id SCHEMA = """ CREATE TABLE IF NOT EXISTS chat_projects ( @@ -248,8 +254,8 @@ def delete(self, project_id: str) -> int: raise UnknownProject("no such project") sessions = [r[0] for r in self._conn.execute( "SELECT session_id FROM session_chat_projects WHERE project_id = ?", (project_id,)).fetchall()] - for sid in sessions: - self._clear_project_workspace(sid) + # 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,)) self._conn.execute("DELETE FROM chat_projects WHERE id = ?", (project_id,)) self._bump() @@ -264,17 +270,22 @@ def file(self, session_id: str, project_id: str | None, *, replace_workspace: bo """ with self._conn: if project_id is None: + old = self.project_of(session_id) self._conn.execute("DELETE FROM session_chat_projects WHERE session_id = ?", (session_id,)) - self._clear_project_workspace(session_id) + if old is not None: + self._clear_project_workspace(session_id, old) self._bump() - return FileResult(session_id, None, None) + left = self._conn.execute( + "SELECT path FROM session_workspaces WHERE session_id = ?", (session_id,)).fetchone() + return FileResult(session_id, None, left[0] if left else None) row = self._conn.execute("SELECT folder FROM chat_projects WHERE id = ?", (project_id,)).fetchone() if row is None: raise UnknownProject("no such project") folder = row[0] current = self._conn.execute( "SELECT path, set_by FROM session_workspaces WHERE session_id = ?", (session_id,)).fetchone() - if (not keep_workspace and current is not None and current[1] != SET_BY and current[0] != folder + bound_by_a_project = current is not None and str(current[1]).startswith(SET_BY_PREFIX) + if (not keep_workspace and current is not None and not bound_by_a_project and current[0] != folder and not replace_workspace): raise WorkspaceSet(current[0]) now = time.time() @@ -289,10 +300,10 @@ def file(self, session_id: str, project_id: str | None, *, replace_workspace: bo return FileResult(session_id, project_id, current[0] if current is not None else None) self._conn.execute( "INSERT OR REPLACE INTO session_workspaces (session_id, path, set_by, updated_at) VALUES (?, ?, ?, ?)", - (session_id, folder, SET_BY, now)) + (session_id, folder, _set_by(project_id), now)) self._bump() return FileResult(session_id, project_id, folder) - def _clear_project_workspace(self, session_id: str) -> None: + def _clear_project_workspace(self, session_id: str, project_id: str) -> None: self._conn.execute( - "DELETE FROM session_workspaces WHERE session_id = ? AND set_by = ?", (session_id, SET_BY)) + "DELETE FROM session_workspaces WHERE session_id = ? AND set_by = ?", (session_id, _set_by(project_id))) From a5dde48ca63f5c392860b41ef2dd8b65a1c7806b Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:30:43 -0500 Subject: [PATCH 06/16] test(projects): store review cases (red) Co-Authored-By: Claude Opus 5.5 --- tests/test_chat_projects_store.py | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/test_chat_projects_store.py b/tests/test_chat_projects_store.py index 07646f04..439847cd 100644 --- a/tests/test_chat_projects_store.py +++ b/tests/test_chat_projects_store.py @@ -283,3 +283,69 @@ def test_the_binding_records_the_project(self, store, conv, tmp_path): store.file("s1", a.id) row = conv._conn.execute("SELECT set_by FROM session_workspaces WHERE session_id = 's1'").fetchone() assert row[0] == f"project:{a.id}" + + +class TestReviewFixes: + """Review of #932.""" + + def test_a_hand_set_workspace_on_the_folder_is_not_taken_over(self, store, conv, tmp_path): + a = store.insert("A", _folder(tmp_path, "a"), managed=False) + conv.set_session_workspace("s1", a.folder, set_by="rest") + store.file("s1", a.id) + store.file("s1", None) + assert conv.get_session_workspace("s1") == a.folder # still the owner's hand-set binding + + def test_a_failed_write_never_rolls_back_other_work_on_the_connection(self, store, conv): + conv._conn.execute("INSERT INTO session_pins (session_id, pinned_at) VALUES ('kept', 1.0)") + with pytest.raises(UnknownProject): + store.rename("cp_000000000000", "X") + conv._conn.commit() + assert conv.is_session_pinned("kept") + + def test_building_the_store_commits_nothing_of_anyone_elses(self, tmp_path): + conv = LCMConversationStore(tmp_path / "fresh.db") + conv._conn.execute("INSERT INTO session_pins (session_id, pinned_at) VALUES ('pending', 1.0)") + conv.chat_projects() + conv._conn.rollback() + assert not conv.is_session_pinned("pending") + + def test_forgetting_a_filed_session_raises_rev(self, conv, store, tmp_path): + p = store.insert("A", _folder(tmp_path, "a"), managed=False) + store.file("s1", p.id) + before = store.rev() + conv.purge_session("s1") + assert store.rev() > before + + def test_an_import_key_collision_is_its_own_error(self, store, tmp_path): + from prometheus.projects.store import ImportKeyTaken + + store.insert("A", _folder(tmp_path, "a"), managed=False, import_key="k1") + with pytest.raises(ImportKeyTaken): + store.insert("B", _folder(tmp_path, "b"), managed=False, import_key="k1") + + @pytest.mark.parametrize("twin", ["Café", "Ca​fé", "CAFÉ"]) + def test_names_that_look_the_same_are_the_same(self, store, tmp_path, twin): + store.insert("Café", _folder(tmp_path, "a"), managed=False) + with pytest.raises(NameTaken): + store.insert(twin, _folder(tmp_path, "b"), managed=False) + + def test_an_existing_root_keeps_its_mode(self, tmp_path): + root = tmp_path / "code" + root.mkdir(mode=0o755) + os.chmod(root, 0o755) + made = make_managed_folder(root, "X") + assert stat.S_IMODE(os.stat(root).st_mode) == 0o755 + assert stat.S_IMODE(os.stat(made).st_mode) == 0o700 + + def test_a_symlinked_root_is_refused(self, tmp_path): + target = tmp_path / "real" + target.mkdir() + link = tmp_path / "link" + link.symlink_to(target) + with pytest.raises(ValueError): + make_managed_folder(link, "X") + assert list(target.iterdir()) == [] + + def test_the_folder_returned_is_resolved(self, tmp_path): + made = make_managed_folder(tmp_path / "r", "X") + assert made == made.resolve() From d1790729dcb3ec7a68597fc27ae581e0d8b43565 Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:32:40 -0500 Subject: [PATCH 07/16] fix(projects): savepoint writes on the shared connection; a hand-set binding stays; root never followed or chmodded (review) Writes run in a SAVEPOINT under a lock instead of the connection's context manager, which committed or rolled back other work on the shared connection; the tables are made with the conversation store's schema so building the store commits nothing. Filing onto a folder the owner bound by hand keeps that binding theirs. An existing projects root keeps its mode and must be a plain directory owned by this user; the folder returned is resolved. Forgetting a filed session raises rev; an import key collision is its own error; names are NFC, stripped of control and format characters, and casefolded. Co-Authored-By: Claude Opus 5.5 --- .../memory/lcm_conversation_store.py | 10 +++ src/prometheus/projects/folders.py | 20 ++++- src/prometheus/projects/store.py | 89 +++++++++++++++---- 3 files changed, 96 insertions(+), 23 deletions(-) diff --git a/src/prometheus/memory/lcm_conversation_store.py b/src/prometheus/memory/lcm_conversation_store.py index e32ed431..13972f7e 100644 --- a/src/prometheus/memory/lcm_conversation_store.py +++ b/src/prometheus/memory/lcm_conversation_store.py @@ -260,6 +260,11 @@ def _apply_schema(self) -> None: updated_at REAL NOT NULL ); """) + # Chat projects (projects/store.py): made here, with the rest of the schema, so building the store later + # never commits mid-way through someone else's transaction. + from prometheus.projects.store import SCHEMA as _CHAT_PROJECTS_SCHEMA + + self._conn.executescript(_CHAT_PROJECTS_SCHEMA) self._conn.commit() self._migrate_add_content_json() self._migrate_add_trust_fields() @@ -1134,6 +1139,11 @@ def purge_session(self, session_id: str) -> dict[str, int]: counts[table] = self._conn.execute( f"DELETE FROM {table} WHERE session_id = ?", (session_id,) ).rowcount if self._table_exists(table) else 0 + if counts.get("session_chat_projects"): + # A membership change: clients holding a chat-projects rev must see it moved. + from prometheus.projects.store import bump_rev + + bump_rev(self._conn) # fts5's 'delete' unlinks a document but leaves the TERM in the # index b-tree with an empty doclist, and VACUUM preserves that — diff --git a/src/prometheus/projects/folders.py b/src/prometheus/projects/folders.py index cef3a2a9..d4c1717d 100644 --- a/src/prometheus/projects/folders.py +++ b/src/prometheus/projects/folders.py @@ -15,6 +15,7 @@ import os import re +import stat import unicodedata from collections.abc import Mapping from pathlib import Path @@ -40,13 +41,24 @@ def slug(name: str) -> str: def make_managed_folder(root: Path, name: str) -> Path: - """Make ``/`` (or ``-2``, ``-3``, ...) with mode 0700, and the root too if it is missing. + """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 + else's folder is refused, never followed) and keeps its mode. The folder returned is resolved. ``mkdir`` without ``exist_ok`` is the claim, so two creates at once never share a folder, and anything already at a name (a file, a folder) moves on to the next suffix. """ - root.mkdir(mode=0o700, parents=True, exist_ok=True) - os.chmod(root, 0o700) + try: + info = os.lstat(root) + except FileNotFoundError: + root.mkdir(mode=0o700, parents=True) + os.chmod(root, 0o700) # only a root this call made; an existing one keeps its mode + else: + if stat.S_ISLNK(info.st_mode) or not stat.S_ISDIR(info.st_mode): + raise ValueError(f"the projects root {root} is not a plain directory") + if hasattr(os, "getuid") and info.st_uid != os.getuid(): + raise ValueError(f"the projects root {root} belongs to another user") base = slug(name) for n in range(1, _MAX_SUFFIX): candidate = root / (base if n == 1 else f"{base}-{n}") @@ -55,5 +67,5 @@ def make_managed_folder(root: Path, name: str) -> Path: except FileExistsError: continue os.chmod(candidate, 0o700) # mkdir's mode is masked by the umask - return candidate + return candidate.resolve() raise FileExistsError(f"no free folder name for {base!r} under {root}") diff --git a/src/prometheus/projects/store.py b/src/prometheus/projects/store.py index d36866db..1b6913ad 100644 --- a/src/prometheus/projects/store.py +++ b/src/prometheus/projects/store.py @@ -24,8 +24,11 @@ import re import secrets import sqlite3 +import threading import time -from collections.abc import Iterable +import unicodedata +from collections.abc import Iterable, Iterator +from contextlib import contextmanager from dataclasses import dataclass, field NAME_MAX = 60 @@ -58,12 +61,22 @@ def _set_by(project_id: str) -> str: id INTEGER PRIMARY KEY CHECK (id = 1), rev INTEGER NOT NULL ); +INSERT OR IGNORE INTO chat_projects_rev (id, rev) VALUES (1, 0); """ def clean_name(raw: str) -> str: - """Trimmed, inner whitespace collapsed, at most :data:`NAME_MAX` characters. ``""`` means no name.""" - return re.sub(r"\s+", " ", raw or "").strip()[:NAME_MAX].strip() + """NFC, control and format characters removed (zero-width, direction overrides), whitespace collapsed, trimmed, at + most :data:`NAME_MAX` characters. ``""`` means no name.""" + text = unicodedata.normalize("NFC", raw or "") + text = "".join(" " if ch.isspace() else ch for ch in text + if ch.isspace() or unicodedata.category(ch) not in ("Cc", "Cf")) + return re.sub(r"\s+", " ", text).strip()[:NAME_MAX].strip() + + +def fold_name(name: str) -> str: + """The identity of a name: two names that read the same are one project.""" + return clean_name(name).casefold() class ProjectError(Exception): @@ -82,6 +95,10 @@ class UnknownProject(ProjectError): kind = "unknown_project" +class ImportKeyTaken(ProjectError): + kind = "import_key_taken" + + class WorkspaceSet(ProjectError): """The session's workspace was set another way; filing would replace it.""" @@ -124,10 +141,31 @@ class ChatProjectStore: """The store, on the conversation store's connection (``LCMConversationStore.chat_projects()``).""" def __init__(self, conn: sqlite3.Connection) -> None: + # The tables are made with the conversation store's own schema (LCMConversationStore.__init__), so building + # this object never commits anything mid-way through someone else's work. self._conn = conn - conn.executescript(SCHEMA) - conn.execute("INSERT OR IGNORE INTO chat_projects_rev (id, rev) VALUES (1, 0)") - conn.commit() + self._lock = threading.RLock() + + @contextmanager + def _write(self) -> Iterator[None]: + """One write, all or nothing, without touching anyone else's work on the shared connection. + + A SAVEPOINT, not ``with conn:``: the connection's context manager commits or rolls back the WHOLE connection, + so a refused write would discard another method's uncommitted rows. If no other transaction was open, the + write is committed; if one was, it joins it and commits with it. + """ + with self._lock: + outer = self._conn.in_transaction + self._conn.execute("SAVEPOINT chat_projects") + try: + yield + except BaseException: + self._conn.execute("ROLLBACK TO chat_projects") + self._conn.execute("RELEASE chat_projects") + raise + self._conn.execute("RELEASE chat_projects") + if not outer and self._conn.in_transaction: + self._conn.commit() # ---------------------------------------------------------------- reads def rev(self) -> int: @@ -152,7 +190,7 @@ def get(self, project_id: str) -> Project | None: return rows[0] if rows else None def by_name(self, name: str) -> Project | None: - folded = clean_name(name).lower() + folded = fold_name(name) rows = self._rows("WHERE p.name_folded = ?", (folded,)) if folded else [] return rows[0] if rows else None @@ -197,7 +235,7 @@ def visible_to(self, own_session_ids: set[str]) -> list[VisibleProject]: # --------------------------------------------------------------- writes def _bump(self) -> None: - self._conn.execute("UPDATE chat_projects_rev SET rev = rev + 1 WHERE id = 1") + bump_rev(self._conn) def insert(self, name: str, folder: str, *, managed: bool, import_key: str | None = None) -> Project: """A new project. :class:`NameTaken` / :class:`FolderInUse` when another project has the name or folder.""" @@ -207,20 +245,22 @@ def insert(self, name: str, folder: str, *, managed: bool, import_key: str | Non now = time.time() pid = "cp_" + secrets.token_hex(6) try: - with self._conn: + with self._write(): position = self._conn.execute( "SELECT COALESCE(MAX(position), 0) + 1 FROM chat_projects").fetchone()[0] self._conn.execute( "INSERT INTO chat_projects (id, name, name_folded, folder, folder_managed, import_key, position," " created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", - (pid, clean, clean.lower(), folder, int(managed), import_key or None, position, now, now)) + (pid, clean, fold_name(clean), folder, int(managed), import_key or None, position, now, now)) self._bump() except sqlite3.IntegrityError as exc: message = str(exc) - if "name_folded" in message: + if "chat_projects.name_folded" in message: raise NameTaken(f"a project is already called {clean!r}") from exc - if "folder" in message: + if "chat_projects.folder" in message: raise FolderInUse("another project already uses that folder") from exc + if "chat_projects.import_key" in message: + raise ImportKeyTaken("a project was already imported under that key") from exc raise project = self.get(pid) assert project is not None @@ -230,11 +270,13 @@ def rename(self, project_id: str, name: str) -> Project: clean = clean_name(name) if not clean: raise ValueError("a project needs a name") + if self._conn.execute("SELECT 1 FROM chat_projects WHERE id = ?", (project_id,)).fetchone() is None: + raise UnknownProject("no such project") try: - with self._conn: + with self._write(): cur = self._conn.execute( "UPDATE chat_projects SET name = ?, name_folded = ?, updated_at = ? WHERE id = ?", - (clean, clean.lower(), time.time(), project_id)) + (clean, fold_name(clean), time.time(), project_id)) if cur.rowcount == 0: raise UnknownProject("no such project") self._bump() @@ -249,9 +291,9 @@ def delete(self, project_id: str) -> int: Never touches the folder or any file in it. """ - with self._conn: - if self._conn.execute("SELECT 1 FROM chat_projects WHERE id = ?", (project_id,)).fetchone() is None: - raise UnknownProject("no such project") + if self._conn.execute("SELECT 1 FROM chat_projects WHERE id = ?", (project_id,)).fetchone() is None: + raise UnknownProject("no such project") + with self._write(): sessions = [r[0] for r in self._conn.execute( "SELECT session_id FROM session_chat_projects WHERE project_id = ?", (project_id,)).fetchall()] # Every binding this project set, on members and on sessions since moved elsewhere as a label. @@ -268,7 +310,10 @@ def file(self, session_id: str, project_id: str | None, *, replace_workspace: bo *keep_workspace* files as a label only: the membership is recorded and the workspace is left as it is (what a migration sends, so moving local projects in never changes where an old conversation writes). """ - with self._conn: + if project_id is not None and self._conn.execute( + "SELECT 1 FROM chat_projects WHERE id = ?", (project_id,)).fetchone() is None: + raise UnknownProject("no such project") + with self._write(): if project_id is None: old = self.project_of(session_id) self._conn.execute("DELETE FROM session_chat_projects WHERE session_id = ?", (session_id,)) @@ -295,7 +340,8 @@ def file(self, session_id: str, project_id: str | None, *, replace_workspace: bo " filed_at = CASE WHEN session_chat_projects.project_id = excluded.project_id" " THEN session_chat_projects.filed_at ELSE excluded.filed_at END", (session_id, project_id, now)) - if keep_workspace: + if keep_workspace or (current is not None and not bound_by_a_project and current[0] == folder): + # A label only, or the owner already bound this folder by hand: that binding stays theirs. self._bump() return FileResult(session_id, project_id, current[0] if current is not None else None) self._conn.execute( @@ -307,3 +353,8 @@ def file(self, session_id: str, project_id: str | None, *, replace_workspace: bo def _clear_project_workspace(self, session_id: str, project_id: str) -> None: self._conn.execute( "DELETE FROM session_workspaces WHERE session_id = ? AND set_by = ?", (session_id, _set_by(project_id))) + + +def bump_rev(conn: sqlite3.Connection) -> None: + """Raise the change counter (inside the caller's transaction). Also used by the conversation store's forget.""" + conn.execute("UPDATE chat_projects_rev SET rev = rev + 1 WHERE id = 1") From ce01f524186a0a1b6cf745be28f1c623bbd854e1 Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:35:42 -0500 Subject: [PATCH 08/16] fix(projects): project names are scrubbed; the uniqueness key is a hash Co-Authored-By: Claude Opus 5.5 --- src/prometheus/projects/store.py | 12 +++++++++--- src/prometheus/security/scrub_capture_stores.py | 1 + tests/test_scrub_covers_every_text_column.py | 6 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/prometheus/projects/store.py b/src/prometheus/projects/store.py index 1b6913ad..5f544159 100644 --- a/src/prometheus/projects/store.py +++ b/src/prometheus/projects/store.py @@ -21,6 +21,7 @@ from __future__ import annotations +import hashlib import re import secrets import sqlite3 @@ -43,7 +44,7 @@ def _set_by(project_id: str) -> str: CREATE TABLE IF NOT EXISTS chat_projects ( id TEXT PRIMARY KEY, name TEXT NOT NULL, - name_folded TEXT NOT NULL UNIQUE, + name_folded TEXT NOT NULL UNIQUE, -- fold_name(): a hash, so the uniqueness carries no text folder TEXT NOT NULL UNIQUE, folder_managed INTEGER NOT NULL, import_key TEXT UNIQUE, @@ -75,8 +76,13 @@ def clean_name(raw: str) -> str: def fold_name(name: str) -> str: - """The identity of a name: two names that read the same are one project.""" - return clean_name(name).casefold() + """The identity of a name: two names that read the same are one project. + + Stored as a SHA-256 of the cleaned, casefolded name, not the folded text itself: the scrub that rewrites a secret + pasted into a name (``security/scrub_capture_stores``) matches patterns that a casefolded copy would no longer + match, and a hash carries nothing to scrub. + """ + return hashlib.sha256(clean_name(name).casefold().encode("utf-8")).hexdigest() class ProjectError(Exception): diff --git a/src/prometheus/security/scrub_capture_stores.py b/src/prometheus/security/scrub_capture_stores.py index b2552f43..605b243e 100755 --- a/src/prometheus/security/scrub_capture_stores.py +++ b/src/prometheus/security/scrub_capture_stores.py @@ -95,6 +95,7 @@ ("lcm_summaries", "id", ("summary_text",)), ("checkpoints", "id", ("goal_description", "messages_json", "tool_calls_json")), ("session_titles", "session_id", ("title",)), + ("chat_projects", "id", ("name",)), ) # table -> the external-content FTS5 index over it, rebuilt when the table was rewritten LCM_FTS = {"lcm_messages": "lcm_messages_fts", "lcm_summaries": "lcm_summaries_fts"} diff --git a/tests/test_scrub_covers_every_text_column.py b/tests/test_scrub_covers_every_text_column.py index bee66c6d..b2948dfd 100644 --- a/tests/test_scrub_covers_every_text_column.py +++ b/tests/test_scrub_covers_every_text_column.py @@ -171,6 +171,12 @@ def _build_memory(path: Path) -> None: ("lcm.db", "session_profiles", "session_id"): SESSION, ("lcm.db", "session_titles", "session_id"): SESSION, ("lcm.db", "session_tombstones", "session_id"): SESSION, + ("lcm.db", "chat_projects", "id"): "a chat project id the daemon mints (cp_ and 12 hex)", + ("lcm.db", "chat_projects", "name_folded"): "a SHA-256 of the folded name (projects/store.fold_name)", + ("lcm.db", "chat_projects", "folder"): "a project folder path the operator linked or the daemon made", + ("lcm.db", "chat_projects", "import_key"): "an opaque key a client mints when it migrates a local project", + ("lcm.db", "session_chat_projects", "project_id"): "a chat project id the daemon mints", + ("lcm.db", "session_chat_projects", "session_id"): SESSION, ("lcm.db", "session_workspaces", "path"): "a workspace directory path the operator linked", ("lcm.db", "session_workspaces", "session_id"): SESSION, ("lcm.db", "session_workspaces", "set_by"): "the surface that set it (router, rest, ...)", From 43a69f1951b6783ec0295e632212bee335dbab43 Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:37:49 -0500 Subject: [PATCH 09/16] test(projects): pin the chat projects routes on the real app (red) Co-Authored-By: Claude Opus 5.5 --- tests/test_chat_projects_routes.py | 425 +++++++++++++++++++++++++++++ 1 file changed, 425 insertions(+) create mode 100644 tests/test_chat_projects_routes.py diff --git a/tests/test_chat_projects_routes.py b/tests/test_chat_projects_routes.py new file mode 100644 index 00000000..b5247c92 --- /dev/null +++ b/tests/test_chat_projects_routes.py @@ -0,0 +1,425 @@ +"""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/{id}")} +OWNER = {("POST", "/api/chat-projects"), ("PUT", "/api/chat-projects/{id}"), ("DELETE", "/api/chat-projects/{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"), [("cron:nightly", 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": "device", "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": "device", "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"] From 180d86ef0082284902fc71eaad453962d0e4e886 Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:47:08 -0500 Subject: [PATCH 10/16] feat(projects): chat projects routes, filing, session rows, the projects capability Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + config/prometheus.yaml.default | 8 + docs/reference/config-keys.md | 4 +- docs/reference/routes.md | 5 +- src/prometheus/projects/content.py | 62 ++++ src/prometheus/projects/folders.py | 6 +- src/prometheus/web/chat_projects_routes.py | 370 +++++++++++++++++++++ src/prometheus/web/route_access.py | 7 + src/prometheus/web/server.py | 36 ++ src/prometheus/web/ws_server.py | 3 +- tests/test_api_sessions_meta.py | 4 +- tests/test_chat_projects_routes.py | 10 +- tests/test_route_access.py | 3 + 13 files changed, 507 insertions(+), 12 deletions(-) create mode 100644 src/prometheus/projects/content.py create mode 100644 src/prometheus/web/chat_projects_routes.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 3efc0f94..ada3fe7a 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. - Chat projects (groundwork): `prometheus.projects` adds the store from `docs/design/chat-projects.md`: projects with a folder, unique names in any case, conversations filed in at most one, and filing that binds the conversation's workspace to the folder (or records a label only), in one transaction with a change counter. Managed folders are made under `projects.root` (default `~/prometheus-projects`) with mode 0700. Forgetting a conversation removes its filing. No routes yet. - CI: the parity replay job runs on `ubuntu-24.04`, pinned, instead of `ubuntu-latest`. When `ubuntu-latest` moved to 26.04, the longer OS line in the system prompt raised the compaction scenario's token estimate by one and failed it, with every model request still matching. No behaviour changes. - Docs: `docs/reference/pairing-join-requests.md` is the reference for how a new device asks to join and how the owner lets it in: the routes and who may call them, every request and response field, the statuses, the expiry and rate limits, the sealed token, the operator's WebSocket frames and the owner code (the explicit, single-use grant that makes an owner device). It is written from the code and pinned to it by `tests/test_pairing_reference.py`. No behaviour changes. diff --git a/config/prometheus.yaml.default b/config/prometheus.yaml.default index 70438826..89903a61 100644 --- a/config/prometheus.yaml.default +++ b/config/prometheus.yaml.default @@ -1698,6 +1698,14 @@ doctor: startup_check: true registry_file: config/model_registry.yaml +# ── Chat projects ───────────────────────────────────────────────────────── +# A chat project is a named folder with standing instructions (PROMETHEUS.md) and reference files; conversations +# filed in it work in that folder (docs/design/chat-projects.md). Projects created without a folder get one here. +projects: + # Where managed project folders are made. A visible folder in your home directory, not inside the agent's + # workspace. Created on first use with mode 0700. Must not be under security.denied_paths. + root: "~/prometheus-projects" + # ── Managed tasks ───────────────────────────────────────────────────────── tasks: enabled: true diff --git a/docs/reference/config-keys.md b/docs/reference/config-keys.md index 8600b024..4ef15278 100644 --- a/docs/reference/config-keys.md +++ b/docs/reference/config-keys.md @@ -4,7 +4,7 @@ # Config key reference (generated) -Every key in `config/prometheus.yaml.default` — **461** of +Every key in `config/prometheus.yaml.default` — **463** of them — with the value the template ships. This is what a fresh install gets, not what the code falls back to when a key is absent: those are pinned equal to each other by @@ -458,6 +458,8 @@ none either — the key is documented so its absence is visible. | `doctor` | *(section)* | | `doctor.startup_check` | `True` | | `doctor.registry_file` | `config/model_registry.yaml` | +| `projects` | *(section)* | +| `projects.root` | `~/prometheus-projects` | | `tasks` | *(section)* | | `tasks.enabled` | `True` | | `tasks.default_timeout_seconds` | `3600` | diff --git a/docs/reference/routes.md b/docs/reference/routes.md index 90eab69a..00b2005b 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 — 133 paths +## Daemon app — 136 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}/title` | PUT | diff --git a/src/prometheus/projects/content.py b/src/prometheus/projects/content.py new file mode 100644 index 00000000..23a3f7a9 --- /dev/null +++ b/src/prometheus/projects/content.py @@ -0,0 +1,62 @@ +"""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 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 +_READ_CAP = 1024 * 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 file's bytes. + """ + path = Path(folder) / INSTRUCTIONS_FILE + try: + info = os.lstat(path) + if not stat.S_ISREG(info.st_mode): + return 0, "" + fd = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)) + with os.fdopen(fd, "rb") as fh: + data = fh.read(_READ_CAP + 1) + except OSError: + return 0, "" + return len(data.decode("utf-8", "replace")), hashlib.sha256(data).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 d4c1717d..58a0966c 100644 --- a/src/prometheus/projects/folders.py +++ b/src/prometheus/projects/folders.py @@ -28,9 +28,9 @@ def projects_root(config: Mapping[str, Any] | None) -> Path: """The projects root from ``projects.root`` (``~`` expanded), or the default.""" - block = (config or {}).get("projects") - raw = block.get("root") if isinstance(block, Mapping) else None - return Path(os.path.expanduser(str(raw or DEFAULT_ROOT))) + block = (config or {}).get("projects", {}) + raw = block.get("root", "~/prometheus-projects") if isinstance(block, Mapping) else DEFAULT_ROOT + return Path(os.path.expanduser(str(raw).strip() or DEFAULT_ROOT)) def slug(name: str) -> str: diff --git a/src/prometheus/web/chat_projects_routes.py b/src/prometheus/web/chat_projects_routes.py new file mode 100644 index 00000000..4a8750f8 --- /dev/null +++ b/src/prometheus/web/chat_projects_routes.py @@ -0,0 +1,370 @@ +"""``/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 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 _inside(child: Path, parent: Path) -> bool: + return child == parent or parent in child.parents + + +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 + 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).""" + from prometheus.context.workspace import validate_workspace_path + + resolved, why = validate_workspace_path(raw, self._security()) + if resolved is None: + return None, why + home = Path(os.path.expanduser("~")).resolve() + root = projects_root(self._config) + if resolved == home: + return None, "the home directory itself cannot be a project folder" + if root.exists() and resolved == root.resolve(): + return None, "the projects root itself cannot be a project folder" + if _inside(resolved, home / ".prometheus"): + return None, "a folder inside the agent's own home cannot be a project folder" + store = self.store() + for other in (store.all() if store is not None else []): + o = Path(other.folder) + if resolved != o and (_inside(resolved, o) or _inside(o, resolved)): + 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]: + root = projects_root(self._config) + store = self.store() + root_abs = Path(os.path.abspath(root)) + for other in (store.all() if store is not None else []): + if _inside(root_abs, Path(other.folder)): + return None, f"the projects root is inside the folder of the project {other.name!r}" + try: + made = make_managed_folder(root, name) + 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 _err(503, "unavailable", "no durable store: projects unavailable") + 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 _err(503, "unavailable", "no durable store: projects unavailable") + 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 JSONResponse(status_code=404, content={"error": "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 _err(503, "unavailable", "no durable store: projects unavailable") + return None + + @app.get("/api/chat-projects") + async def list_chat_projects(request: Request) -> Any: + store = service.store() + if store is None: + return _err(503, "unavailable", "no durable store: projects unavailable") + if is_owner(request): + return {"role": "owner", "rev": store.rev(), "projects": [service.full(p) for p in store.all()]} + 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 _err(503, "unavailable", "no durable store: projects unavailable") + if is_owner(request): + p = store.get(project_id) + if p is None: + return _err(404, "unknown_project", "no such project") + return {**service.full(p), "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: + resolved, why = service.validate(folder_raw) + if resolved is None: + return _err(400, "bad_folder", why or "that folder cannot be used") + if existing is not None: + if existing.folder == str(resolved): + return JSONResponse({"ok": True, "project": service.full(existing), "created": False}) + return _err(409, "name_taken", f"a project is already called {existing.name!r}") + 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 a0cee4f2..210c000c 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}"), # the one desktop route open to every valid token: a stop can only end a task ("POST", "/api/computer/tasks/{task_id}/stop"), # the tool calls of its own sessions: seen and denied. The approve route is listed for a desktop prompt, which @@ -236,6 +239,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 dabfcda5..cd43c9e5 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,21 @@ 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"}) + 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 # 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 +1649,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 +7044,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, + 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), + ) + # ── Static files (must be last — catch-all) ───────────────────── if static_dir: diff --git a/src/prometheus/web/ws_server.py b/src/prometheus/web/ws_server.py index d8efb53c..752b419a 100644 --- a/src/prometheus/web/ws_server.py +++ b/src/prometheus/web/ws_server.py @@ -1078,7 +1078,8 @@ 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 - return ["voice_input"] if media_services.voice_input_available(self.config) else [] + caps = ["voice_input"] if media_services.voice_input_available(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 index b5247c92..fbdad272 100644 --- a/tests/test_chat_projects_routes.py +++ b/tests/test_chat_projects_routes.py @@ -26,8 +26,8 @@ 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/{id}")} -OWNER = {("POST", "/api/chat-projects"), ("PUT", "/api/chat-projects/{id}"), ("DELETE", "/api/chat-projects/{id}"), +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")} @@ -270,7 +270,7 @@ def test_unfiling(world): assert world.conv.get_session_workspace(sid) is None -@pytest.mark.parametrize(("sid", "status", "kind"), [("cron:nightly", 400, "machine_session"), +@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() @@ -381,7 +381,7 @@ def test_a_chat_only_device_sees_only_projects_with_its_sessions(world): 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": "device", "projects": [{"id": mine["id"], "name": "Mine", "sessions": 1}]} + 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 @@ -391,7 +391,7 @@ def test_a_chat_only_device_sees_only_projects_with_its_sessions(world): def test_a_device_with_no_filed_sessions_sees_nothing(world): world.project() - assert world.call("a", "GET", "/api/chat-projects").json() == {"role": "device", "projects": []} + assert world.call("a", "GET", "/api/chat-projects").json() == {"role": "chat_only", "projects": []} def test_the_owner_gets_one_project_with_its_sessions(world): diff --git a/tests/test_route_access.py b/tests/test_route_access.py index d8a6dcf1..99351076 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}"), # the one desktop route open to every valid token: a stop can only end a task ("POST", "/api/computer/tasks/{task_id}/stop"), # the tool calls of its own sessions: seen and denied; approving is the owner's (or a marked device's, for a From 1e094a69e8ca8fcaae39a67e985eaab2b2380cdf Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:53:13 -0500 Subject: [PATCH 11/16] test(projects): routes review cases (red) Co-Authored-By: Claude Opus 5.5 --- tests/test_chat_projects_routes.py | 117 +++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/tests/test_chat_projects_routes.py b/tests/test_chat_projects_routes.py index fbdad272..a8a344a2 100644 --- a/tests/test_chat_projects_routes.py +++ b/tests/test_chat_projects_routes.py @@ -423,3 +423,120 @@ def test_the_change_frame_goes_to_owner_sockets_only(world): 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 From a5ede1b8adf6b0e8e4c5c988d14bd092ea47d23b Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Fri, 13 Nov 2026 23:59:10 -0500 Subject: [PATCH 12/16] fix(projects): folder checks by identity, root checked before it is made, forgotten sessions not counted (reviews) Folder checks compare file identity, never spelling: on a case-insensitive disk a folder typed in another case no longer slips past home, the agent's home (resolved, including the config and data dirs), the projects root, another project's folder or a folder that contains home. The projects root is checked (absolute, its existing parent valid, not under denied_paths) before anything is made, and a null root means the default. A forgotten session no longer counts in a project; instructions_rev hashes the whole file off the event loop; a session bound before its first message cannot be claimed after a restart; machine gateways and every error carry a kind; a daemon without a store answers 404 (no projects). Co-Authored-By: Claude Opus 5.5 --- src/prometheus/projects/content.py | 23 ++-- src/prometheus/projects/folders.py | 10 +- src/prometheus/projects/store.py | 19 ++- src/prometheus/web/chat_projects_routes.py | 134 ++++++++++++++++----- src/prometheus/web/server.py | 10 +- src/prometheus/web/session_scope.py | 17 ++- 6 files changed, 166 insertions(+), 47 deletions(-) diff --git a/src/prometheus/projects/content.py b/src/prometheus/projects/content.py index 23a3f7a9..5aa6452c 100644 --- a/src/prometheus/projects/content.py +++ b/src/prometheus/projects/content.py @@ -10,6 +10,7 @@ from __future__ import annotations +import codecs import hashlib import os import stat @@ -21,25 +22,33 @@ #: 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 -_READ_CAP = 1024 * 1024 +_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 file's bytes. + ``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: - info = os.lstat(path) - if not stat.S_ISREG(info.st_mode): + if not stat.S_ISREG(os.lstat(path).st_mode): return 0, "" - fd = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 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: - data = fh.read(_READ_CAP + 1) + 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 len(data.decode("utf-8", "replace")), hashlib.sha256(data).hexdigest()[:16] + return chars, digest.hexdigest()[:16] def valid_reference_name(name: str) -> bool: diff --git a/src/prometheus/projects/folders.py b/src/prometheus/projects/folders.py index 58a0966c..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 @@ -30,7 +30,9 @@ def projects_root(config: Mapping[str, Any] | None) -> Path: """The projects root from ``projects.root`` (``~`` expanded), or the default.""" block = (config or {}).get("projects", {}) raw = block.get("root", "~/prometheus-projects") if isinstance(block, Mapping) else DEFAULT_ROOT - return Path(os.path.expanduser(str(raw).strip() or DEFAULT_ROOT)) + if not isinstance(raw, str) or not raw.strip(): + raw = DEFAULT_ROOT # a bare `root:` in YAML is null: the default, never a folder called "None" + return Path(os.path.expanduser(raw.strip())) def slug(name: str) -> str: @@ -40,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 @@ -62,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..ca6728f3 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) diff --git a/src/prometheus/web/chat_projects_routes.py b/src/prometheus/web/chat_projects_routes.py index 4a8750f8..f0d060ce 100644 --- a/src/prometheus/web/chat_projects_routes.py +++ b/src/prometheus/web/chat_projects_routes.py @@ -21,6 +21,7 @@ from __future__ import annotations +import asyncio import json import logging import os @@ -70,8 +71,29 @@ async def _read_json(request: Request) -> Any: return None -def _inside(child: Path, parent: Path) -> bool: - return child == parent or parent in child.parents +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: @@ -103,25 +125,52 @@ def full(p: Project) -> dict[str, Any]: "updated_at": p.updated_at} # ---------------------------------------------------------------- folders - 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).""" + @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, *, exclude: str | None = None) -> 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 - home = Path(os.path.expanduser("~")).resolve() + 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) - if resolved == home: - return None, "the home directory itself cannot be a project folder" - if root.exists() and resolved == root.resolve(): - return None, "the projects root itself cannot be a project folder" - if _inside(resolved, home / ".prometheus"): - return None, "a folder inside the agent's own home cannot be a project folder" + 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 []): + if other.folder == exclude: + continue o = Path(other.folder) - if resolved != o and (_inside(resolved, o) or _inside(o, resolved)): + 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 @@ -139,14 +188,34 @@ def bindable(self, p: Project) -> str | None: 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() - root_abs = Path(os.path.abspath(root)) - for other in (store.all() if store is not None else []): - if _inside(root_abs, Path(other.folder)): + projects = store.all() if store is not None else [] + root_chain_existing = _chain(existing) + 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) + 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)) @@ -159,7 +228,7 @@ def make_managed(self, name: str) -> tuple[Path | None, str | None]: def precheck(self, project_id: str) -> JSONResponse | Project: store = self.store() if store is None: - return _err(503, "unavailable", "no durable store: projects unavailable") + return _no_store() p = store.get(project_id) if p is None: return _err(404, "unknown_project", "no such project") @@ -172,11 +241,11 @@ async def file(self, session_id: str, project_id: str | None, *, replace_workspa keep_workspace: bool = False, device_session: bool = False) -> JSONResponse | dict[str, Any]: store = self.store() if store is None: - return _err(503, "unavailable", "no durable store: projects unavailable") + 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 JSONResponse(status_code=404, content={"error": "unknown session"}) + return _err(404, "unknown_session", "unknown session") if project_id is not None: p = store.get(project_id) if p is None: @@ -237,16 +306,19 @@ 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 _err(503, "unavailable", "no durable store: projects unavailable") + 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 _err(503, "unavailable", "no durable store: projects unavailable") + return _no_store() if is_owner(request): - return {"role": "owner", "rev": store.rev(), "projects": [service.full(p) for p in store.all()]} + 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)]} @@ -255,12 +327,13 @@ async def list_chat_projects(request: Request) -> Any: async def get_chat_project(project_id: str, request: Request) -> Any: store = service.store() if store is None: - return _err(503, "unavailable", "no durable store: projects unavailable") + return _no_store() if is_owner(request): p = store.get(project_id) if p is None: return _err(404, "unknown_project", "no such project") - return {**service.full(p), "session_ids": store.session_ids(project_id)} + 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: @@ -296,13 +369,18 @@ async def create_chat_project(request: Request) -> Any: return _err(400, "bad_folder", why or "the managed folder could not be made") folder, managed = made, True else: - resolved, why = service.validate(folder_raw) - if resolved is None: - return _err(400, "bad_folder", why or "that folder cannot be used") if existing is not None: - if existing.folder == str(resolved): + # 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 diff --git a/src/prometheus/web/server.py b/src/prometheus/web/server.py index cd43c9e5..f799feb1 100644 --- a/src/prometheus/web/server.py +++ b/src/prometheus/web/server.py @@ -1623,13 +1623,19 @@ async def create_session(request: Request): # 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"}) + 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. @@ -7049,7 +7055,7 @@ async def undispatch_story(story_pk: str): from prometheus.web.chat_projects_routes import register_chat_projects_routes register_chat_projects_routes( - app, config=config, conversation_store=_conversation_store, devices=_devices, + 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), diff --git a/src/prometheus/web/session_scope.py b/src/prometheus/web/session_scope.py index 97a642c2..8431787a 100644 --- a/src/prometheus/web/session_scope.py +++ b/src/prometheus/web/session_scope.py @@ -83,7 +83,22 @@ 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 + if conversation_store.max_rowid(session_id) > 0: + return True + # A session the owner bound to a folder or filed in a chat project before its first message: it exists, so + # no device can claim it (and inherit that binding) after a restart empties the in-memory sessions. + conn = getattr(conversation_store, "_conn", None) + if conn is None: + return False + for table in ("session_workspaces", "session_chat_projects"): + try: + if conn.execute(f"SELECT 1 FROM {table} WHERE session_id = ?", (session_id,)).fetchone(): + return True + except Exception: # noqa: BLE001 - a store without the table simply has no such row + continue + return False def event_session_id(event: dict[str, Any]) -> str | None: From d19fbe2038e41728ecd13757c8b3d9b70875bcd9 Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Sat, 14 Nov 2026 00:02:09 -0500 Subject: [PATCH 13/16] fix(projects): unfiled counts what clients see; root ancestors resolved; store methods for existence and tool calls (review lows) Co-Authored-By: Claude Opus 5.5 --- .../memory/lcm_conversation_store.py | 17 +++++++++++++++++ src/prometheus/projects/store.py | 4 +++- src/prometheus/web/chat_projects_routes.py | 6 ++---- src/prometheus/web/session_scope.py | 18 +++++------------- 4 files changed, 27 insertions(+), 18 deletions(-) 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/store.py b/src/prometheus/projects/store.py index ca6728f3..2bc3a003 100644 --- a/src/prometheus/projects/store.py +++ b/src/prometheus/projects/store.py @@ -307,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 index f0d060ce..9152d165 100644 --- a/src/prometheus/web/chat_projects_routes.py +++ b/src/prometheus/web/chat_projects_routes.py @@ -137,7 +137,7 @@ def _agent_homes() -> list[Path]: continue return homes - def validate(self, raw: str, *, exclude: str | None = None) -> tuple[Path | None, str | None]: + 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 @@ -162,8 +162,6 @@ def validate(self, raw: str, *, exclude: str | None = None) -> tuple[Path | None 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 []): - if other.folder == exclude: - continue o = Path(other.folder) oid = _ident(o) if oid is None: @@ -208,7 +206,7 @@ def make_managed(self, name: str) -> tuple[Path | None, str | 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) + 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: diff --git a/src/prometheus/web/session_scope.py b/src/prometheus/web/session_scope.py index 8431787a..ede2a50c 100644 --- a/src/prometheus/web/session_scope.py +++ b/src/prometheus/web/session_scope.py @@ -85,20 +85,12 @@ def session_exists(session_mgr: Any, conversation_store: Any, session_id: str) - return True if conversation_store is None: return False - if conversation_store.max_rowid(session_id) > 0: - return True - # A session the owner bound to a folder or filed in a chat project before its first message: it exists, so + # 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. - conn = getattr(conversation_store, "_conn", None) - if conn is None: - return False - for table in ("session_workspaces", "session_chat_projects"): - try: - if conn.execute(f"SELECT 1 FROM {table} WHERE session_id = ?", (session_id,)).fetchone(): - return True - except Exception: # noqa: BLE001 - a store without the table simply has no such row - continue - return False + 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: From e5bbe240a0952549677991b6c72c7830f5b6695d Mon Sep 17 00:00:00 2001 From: OAra Labs Date: Sat, 14 Nov 2026 17:55:47 -0500 Subject: [PATCH 14/16] test(parity): rebaseline expected files for the chat-projects tables Regenerated with rebaseline-from-exchanges on ubuntu-24.04. Adds the empty chat_projects, chat_projects_rev and session_chat_projects tables to every scenario; the other moved lines are the title row's known order race and a key moving inside an object, both compared order-free. Co-Authored-By: Claude Opus 5.5 --- .../parity/checkpoint_undo.expected.json | 34 ++++++++ .../fixtures/parity/coding_run.expected.json | 34 ++++++++ .../fixtures/parity/compaction.expected.json | 80 ++++++++++++----- .../parity/gate_blocked.expected.json | 34 ++++++++ .../parity/hosted_route.expected.json | 34 ++++++++ .../parity/linked_workspace.expected.json | 34 ++++++++ .../parity/memory_write.expected.json | 34 ++++++++ .../parity/model_switch.expected.json | 86 +++++++++++++------ .../fixtures/parity/plain_chat.expected.json | 34 ++++++++ .../parity/repaired_tool_call.expected.json | 34 ++++++++ tests/fixtures/parity/tier_full.expected.json | 34 ++++++++ .../fixtures/parity/tool_calls.expected.json | 34 ++++++++ 12 files changed, 457 insertions(+), 49 deletions(-) diff --git a/tests/fixtures/parity/checkpoint_undo.expected.json b/tests/fixtures/parity/checkpoint_undo.expected.json index 73bd2f62..f4e1c604 100644 --- a/tests/fixtures/parity/checkpoint_undo.expected.json +++ b/tests/fixtures/parity/checkpoint_undo.expected.json @@ -291,6 +291,32 @@ }, "home/.prometheus/data/lcm.db": { "sqlite": { + "chat_projects": { + "columns": [ + "id", + "name", + "name_folded", + "folder", + "folder_managed", + "import_key", + "position", + "created_at", + "updated_at" + ], + "rows": [] + }, + "chat_projects_rev": { + "columns": [ + "id", + "rev" + ], + "rows": [ + [ + 1, + 0 + ] + ] + }, "checkpoints": { "columns": [ "id", @@ -690,6 +716,14 @@ ], "rows": [] }, + "session_chat_projects": { + "columns": [ + "session_id", + "project_id", + "filed_at" + ], + "rows": [] + }, "session_forks": { "columns": [ "session_id", diff --git a/tests/fixtures/parity/coding_run.expected.json b/tests/fixtures/parity/coding_run.expected.json index 86c09e08..2e9d3041 100644 --- a/tests/fixtures/parity/coding_run.expected.json +++ b/tests/fixtures/parity/coding_run.expected.json @@ -200,6 +200,32 @@ }, "home/.prometheus/data/lcm.db": { "sqlite": { + "chat_projects": { + "columns": [ + "id", + "name", + "name_folded", + "folder", + "folder_managed", + "import_key", + "position", + "created_at", + "updated_at" + ], + "rows": [] + }, + "chat_projects_rev": { + "columns": [ + "id", + "rev" + ], + "rows": [ + [ + 1, + 0 + ] + ] + }, "checkpoints": { "columns": [ "id", @@ -373,6 +399,14 @@ ], "rows": [] }, + "session_chat_projects": { + "columns": [ + "session_id", + "project_id", + "filed_at" + ], + "rows": [] + }, "session_forks": { "columns": [ "session_id", diff --git a/tests/fixtures/parity/compaction.expected.json b/tests/fixtures/parity/compaction.expected.json index 9c45320a..34a766c4 100644 --- a/tests/fixtures/parity/compaction.expected.json +++ b/tests/fixtures/parity/compaction.expected.json @@ -248,6 +248,32 @@ }, "home/.prometheus/data/lcm.db": { "sqlite": { + "chat_projects": { + "columns": [ + "id", + "name", + "name_folded", + "folder", + "folder_managed", + "import_key", + "position", + "created_at", + "updated_at" + ], + "rows": [] + }, + "chat_projects_rev": { + "columns": [ + "id", + "rev" + ], + "rows": [ + [ + 1, + 0 + ] + ] + }, "checkpoints": { "columns": [ "id", @@ -793,6 +819,14 @@ ], "rows": [] }, + "session_chat_projects": { + "columns": [ + "session_id", + "project_id", + "filed_at" + ], + "rows": [] + }, "session_forks": { "columns": [ "session_id", @@ -2098,56 +2132,56 @@ [ "", "