From d4f2586625b0fcdf458dc72f3f41ed0931d8db38 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:13:44 +0000 Subject: [PATCH 01/35] TD-5233: the session-preset slug test reads no host keychain and no network set_tier_slug refreshes the new slug's model card before it replies (TD-5185). The test stubbed the config write but not the refresh, so it ran `secret-tool lookup` on the developer's real tst-openrouter item and, with a key there, would have asked the live host with it. Where the Secret Service is slow the reply took about 5 s against the test's 2 s wait. CI has no secret-tool, so it passed there. The test now stubs the key read and the model list and asserts the one list request carried the stub's key. Adds epic E54 to the backlog with this story. Co-Authored-By: Claude Fable 5.2 --- core/tests/test_session_preset.py | 27 ++++++++++++++++++++++++++- docs/tst-desk-backlog.md | 31 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/core/tests/test_session_preset.py b/core/tests/test_session_preset.py index 2f115bbc..2cdf283a 100644 --- a/core/tests/test_session_preset.py +++ b/core/tests/test_session_preset.py @@ -12,8 +12,15 @@ import pytest from websockets.asyncio.client import connect +from tstd import model_cards from tstd.daemon import Daemon -from tstd.protocol import PROTOCOL_VERSION, AssistantDelta, TierState, UserTurn +from tstd.protocol import ( + PROTOCOL_VERSION, + AssistantDelta, + EndpointModels, + TierState, + UserTurn, +) from tstd.provider import ChatMessage from tstd.session_persist import SessionPersist from tstd.session_store import SessionStore @@ -214,11 +221,27 @@ async def test_catalog_slug_edit_does_not_rewrite_an_open_session( def _save(preset: str, tier: str, slug: str, path: Path | None = None) -> Path: return Path("/tmp/fake-config.yaml") + # TD-5233: the handler refreshes the new slug's model card before it + # replies. Unstubbed, that reads the host keychain's real + # ``tst-openrouter`` item (about 5 s where Secret Service is slow, + # past _ask's 2 s) and, with a key there, sends it to the live host. + asked: list[tuple[str, str | None]] = [] + + async def _test_key(name: str = "openrouter") -> str: + return "test-key" + + async def _offline(base_url: str, api_key: str | None, **_: Any) -> EndpointModels: + asked.append((base_url, api_key)) + return EndpointModels(base_url=base_url, error="offline in tests") + + monkeypatch.setattr("tstd.daemon.get_api_key", _test_key) + monkeypatch.setattr("tstd.daemon.save_tier_slug", _save) monkeypatch.setattr( "tstd.daemon.Daemon._reload_user_config", lambda self: None, ) + monkeypatch.setattr(model_cards, "list_endpoint_models", _offline) with tempfile.TemporaryDirectory() as tmp: daemon, task = await _start_daemon(Path(tmp)) try: @@ -241,6 +264,8 @@ def _save(preset: str, tier: str, slug: str, path: Path | None = None) -> Path: }, ) assert resp["type"] == "setup_state" + # One host asked, with the stub's key: nothing left the test. + assert [key for _host, key in asked] == ["test-key"] assert sess.config is opened_obj assert sess.config.tier("brain").slug == opened_slug await ws.close() diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 47ae14f9..d064f357 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13319,3 +13319,34 @@ check on a real Mac, including the Accessibility permission. - [ ] Windows: the CLI ships beside `tstd.exe` in the MSI, checked the same way. + +--- + +## Epic E54 — Builds: one-click model presets + +Scope from the 2026-11-20 discovery pass; the go-ahead to start was relayed +to the building session the same day. A build is a `presets:` entry that +carries everything a switch needs: the model per role, the engine, and its +context settings, so Local, API and Grok are one click each. It extends the +catalog (TD-1721: "Settings remains the catalog"); it is not a second +settings document. Unversioned. Stories are added here as they start. + +--- + +### TD-5233 — The session-preset slug test reads no host keychain and no network + +**Size:** 1 · **Depends on:** TD-1721, TD-5185 · **Status:** Done + +`test_catalog_slug_edit_does_not_rewrite_an_open_session` sends +`set_tier_slug`, whose handler refreshes the new slug's model card before +it replies (TD-5185). The test stubbed the config write but not the +refresh, so it read the developer's real `tst-openrouter` keychain item +and, with a key there, would have asked the live host with it. Where the +Secret Service answers slowly the reply took about 5 s against the test's +2 s wait, and the test failed; CI has no `secret-tool`, so it passed there. + +- [x] The test stubs the key read and the model list, and asserts the one + list request carried the stub's key + (`test_session_preset.py::TestSessionPreset::test_catalog_slug_edit_does_not_rewrite_an_open_session`). +- [x] The file passes with every non-loopback connect and DNS lookup + refused, and spawns no `secret-tool`. From 741d555e53f1f298816ab438800214c0238189fd Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:40:36 +0000 Subject: [PATCH 02/35] TD-5237: duplicate, rename and remove a model in Settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The walkthrough could add a model and nothing could copy, relabel or remove one, so trying a variation meant overwriting the entry already there in Settings → Model. Three narrow verbs, each acked with setup_state: duplicate_preset, set_preset_label and delete_preset. The writers are in core/tstd/preset_write.py. A copy is written from the loaded entry with every role spelled out, so it shares no YAML anchor with its source. Rename changes the label and never the key, which sessions, scheduled pins and computer_use.local_worker_preset store. Every would-be file is loaded as the daemon loads it before it is written, so removing an entry whose anchor another entry uses is refused with the file untouched. delete_preset is loopback-only; none of the three is a machine-peer verb. Settings → Model lists every model with Duplicate, Rename and Remove (ModelCatalog.svelte). The UI walk copies, renames and removes one, and the census lists the new controls. Class B: recorded in DECISIONS.md. Class A: the section heading is "What you have added", because the walk finds a Settings section by a heading that contains its name, and "Model" matched "Your models". Co-Authored-By: Claude Fable 5.2 --- DECISIONS.md | 29 ++ core/scripts/generate_protocol_fixtures.py | 9 + core/tests/test_preset_write.py | 371 ++++++++++++++++++++ core/tstd/daemon.py | 36 ++ core/tstd/peer_policy.py | 4 + core/tstd/preset_write.py | 266 ++++++++++++++ core/tstd/protocol.py | 47 +++ docs/architecture.md | 3 + docs/models.md | 18 + docs/tst-desk-backlog.md | 41 +++ ui/e2e/app-walk.spec.ts | 43 ++- ui/e2e/census.json | 9 + ui/src/lib/components/ModelCatalog.svelte | 265 ++++++++++++++ ui/src/lib/components/SettingsModels.svelte | 3 + ui/src/lib/model-catalog.test.ts | 206 +++++++++++ ui/src/lib/model-catalog.ts | 25 ++ ui/src/lib/protocol-fixtures.json | 15 + ui/src/lib/protocol.ts | 27 ++ 18 files changed, 1416 insertions(+), 1 deletion(-) create mode 100644 core/tests/test_preset_write.py create mode 100644 core/tstd/preset_write.py create mode 100644 ui/src/lib/components/ModelCatalog.svelte create mode 100644 ui/src/lib/model-catalog.test.ts create mode 100644 ui/src/lib/model-catalog.ts diff --git a/DECISIONS.md b/DECISIONS.md index e86b1844..4019e5c7 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -19839,3 +19839,32 @@ name as the owner of its windows, so a name check missed exactly the copy TST Desk ships. The executable check must not catch TST Desk's own windows, which share that owner name. The lock covers set-up only, never the command, so concurrent sessions still act in parallel. + +## 2026-11-20 — TD-5237: three narrow verbs edit the model catalog; rename is the label only (Class B) + +**Decision:** Settings → Model copies, relabels and removes catalog entries +with `duplicate_preset {source, label}`, `set_preset_label {name, label, +description?}` and `delete_preset {name}`, each acked with `setup_state`. +The writers live in `core/tstd/preset_write.py`. A copy is written from the +loaded entry with every role spelled out. "Rename" changes `label` and never +the entry's key. Every would-be file is loaded with `config_from_data` before +it is written. `delete_preset` is in `LOOPBACK_VERBS`; the other two are open +to a paired phone like `add_model`, and none is a machine-peer verb. + +**Rationale:** the key is an id other things store: `sessions.json` +(TD-1721), scheduled-job pins (TD-3812) and +`computer_use.local_worker_preset`. Changing it would need every one of +those rewritten in step, for no gain a person can see, since the picker shows +the label. A copy written from text would repeat the source's anchor names, +and YAML then rebinds every later alias to the copy; writing it expanded +keeps the two entries independent, which is the point of copying. Loading +before writing is what catches the one edit a line-level writer cannot judge +by itself: removing an entry whose anchor another entry uses. + +**Rejected:** one generic "edit preset" verb carrying a whole entry (a +free-form body is where a secret could ride in; the narrow verbs cannot +carry one); renaming the key and migrating its references; deleting the keys +an entry used along with it (a key is shared between entries and is managed +in Settings → API keys); letting a phone remove a model (it is the +computer's config, and nothing on a phone needs it). + diff --git a/core/scripts/generate_protocol_fixtures.py b/core/scripts/generate_protocol_fixtures.py index 37b1d89d..13a5fe0d 100644 --- a/core/scripts/generate_protocol_fixtures.py +++ b/core/scripts/generate_protocol_fixtures.py @@ -74,6 +74,7 @@ DeleteJobTemplate, DeleteMcpSecret, DeleteMcpServer, + DeletePreset, DeleteSession, Deny, DenyVerify, @@ -85,6 +86,7 @@ DiagnosticsReport, Drain, DrainResult, + DuplicatePreset, EmailTestResult, EndpointModel, EndpointModels, @@ -249,6 +251,7 @@ SetMcpServer, SetPlan, SetPreset, + SetPresetLabel, SetRemoteAttach, SetSessionPreset, SetSessionStar, @@ -582,6 +585,12 @@ ), "test_model": TestModel(), "set_preset": SetPreset(name="tst-default"), + # TD-5237: copy, relabel and remove a catalog entry + "duplicate_preset": DuplicatePreset(source="tst-default", label="My copy"), + "set_preset_label": SetPresetLabel( + name="tst-default", label="House model", description="General coding and chat" + ), + "delete_preset": DeletePreset(name="budget"), "set_engine": SetEngine(kind="grok"), "set_grok_mode": SetGrokMode(session_id="sess-1", mode="plan"), "run_grok_command": RunGrokCommand(session_id="sess-1", name="compact", argument="keep tests"), diff --git a/core/tests/test_preset_write.py b/core/tests/test_preset_write.py new file mode 100644 index 00000000..3313e530 --- /dev/null +++ b/core/tests/test_preset_write.py @@ -0,0 +1,371 @@ +"""Copy, relabel and remove catalog entries (TD-5237). + +The writers edit a file a person also edits, so every case checks two +things: the catalog the daemon would load afterwards, and that the lines +the edit does not own are still there byte for byte. +""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +import pytest + +from tests.catalog_helpers import catalog_config_yaml +from tests.test_add_model import _ask, _start, _stop +from tstd.config import ConfigError, default_config_yaml, load_config +from tstd.mock import MockProvider, Script +from tstd.peer_policy import LOOPBACK_VERBS, MACHINE_PEER_VERBS +from tstd.preset_write import delete_preset, duplicate_preset, save_preset_label + +ANCHORED = """\ +# my models +presets: + shared: + label: Shared + description: One model on every role + brain: &one + slug: vendor/model-a + base_url: http://127.0.0.1:8000/v1 + context_window: 32768 + max_output_tokens: 4096 + input_price: 0.0 + output_price: 0.0 + cache_read_price: 0.0 + worker: *one + validator: *one + borrower: + label: Borrower + brain: *one + worker: *one + validator: *one + +# which one new chats use +active_preset: shared +""" + + +def _write(tmp_path: Path, text: str) -> Path: + path = tmp_path / "config.yaml" + path.write_text(text, encoding="utf-8") + return path + + +def _catalog(tmp_path: Path) -> Path: + return _write(tmp_path, catalog_config_yaml()) + + +class TestDuplicate: + def test_the_copy_is_the_same_model_under_a_new_label(self, tmp_path: Path) -> None: + path = _catalog(tmp_path) + duplicate_preset("local", "local-2", "Local, small window", path) + cfg = load_config(path) + copy, source = cfg.presets["local-2"], cfg.presets["local"] + assert copy.label == "Local, small window" + for tier in ("brain", "worker", "validator"): + assert getattr(copy, tier) == getattr(source, tier) + assert list(cfg.presets)[-1] == "local-2" + + def test_only_the_new_block_is_added(self, tmp_path: Path) -> None: + before = catalog_config_yaml() + path = _write(tmp_path, before) + duplicate_preset("local", "local-2", "Copy", path) + after = path.read_text(encoding="utf-8").split("\n") + start = after.index(" local-2:") + # The block is every following line indented under the entry. + end = next(i for i in range(start + 1, len(after)) if not after[i].startswith(" ")) + assert "\n".join(after[:start] + after[end:]) == before + + def test_a_copy_shares_no_anchor_with_its_source(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + duplicate_preset("shared", "mine", "Mine", path) + text = path.read_text(encoding="utf-8") + block = text[text.index(" mine:") : text.index("# which one")] + assert "&" not in block and "*" not in block + assert block.count("slug: vendor/model-a") == 3 + cfg = load_config(path) + assert cfg.presets["mine"].brain == cfg.presets["shared"].brain + assert cfg.presets["mine"].description == "One model on every role" + # The comment that opens the next section stayed with it. + assert "\n# which one new chats use\nactive_preset: shared\n" in text + + def test_a_label_cannot_inject_yaml(self, tmp_path: Path) -> None: + path = _catalog(tmp_path) + duplicate_preset("local", "odd", 'x"\n picker: more', path) + cfg = load_config(path) + assert cfg.presets["odd"].label == 'x"\n picker: more' + assert cfg.presets["odd"].picker == cfg.presets["local"].picker + + @pytest.mark.parametrize( + ("source", "name", "label", "says"), + [ + ("nope", "copy", "Copy", "Unknown preset"), + ("local", "vllm", "Copy", "already exists"), + ("local", "Bad Name", "Copy", "lowercase slug"), + ("local", "copy", " ", "Give the model a name"), + ("local", "copy", "x" * 41, "at most 40"), + ], + ) + def test_refusals_write_nothing( + self, tmp_path: Path, source: str, name: str, label: str, says: str + ) -> None: + path = _catalog(tmp_path) + with pytest.raises(ConfigError, match=says): + duplicate_preset(source, name, label, path) + assert path.read_text(encoding="utf-8") == catalog_config_yaml() + + +class TestLabel: + def test_only_the_label_line_changes(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + save_preset_label("shared", "House model", path=path) + assert path.read_text(encoding="utf-8") == ANCHORED.replace( + " label: Shared", ' label: "House model"' + ) + cfg = load_config(path) + assert cfg.presets["shared"].label == "House model" + assert cfg.presets["shared"].description == "One model on every role" + assert set(cfg.presets) == {"shared", "borrower"} + + def test_a_missing_label_is_added_under_the_entry(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED.replace(" label: Borrower\n", "")) + save_preset_label("borrower", "Second", "Same model again", path) + text = path.read_text(encoding="utf-8") + assert ' borrower:\n label: "Second"\n description: "Same model again"\n' in text + cfg = load_config(path) + assert cfg.presets["borrower"].label == "Second" + assert cfg.presets["borrower"].description == "Same model again" + + def test_an_empty_description_removes_the_line(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + save_preset_label("shared", "Shared", "", path) + assert "description" not in path.read_text(encoding="utf-8") + assert load_config(path).presets["shared"].description is None + + def test_a_label_cannot_inject_yaml(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + save_preset_label("shared", 'a"\n picker: more', path=path) + cfg = load_config(path) + assert cfg.presets["shared"].label == 'a"\n picker: more' + assert cfg.presets["shared"].picker == "primary" + + @pytest.mark.parametrize( + ("name", "label", "description", "says"), + [ + ("nope", "X", None, "Unknown preset"), + ("shared", " ", None, "Give the model a name"), + ("shared", "x" * 41, None, "at most 40"), + ("shared", "X", "d" * 121, "at most 120"), + ], + ) + def test_refusals_write_nothing( + self, tmp_path: Path, name: str, label: str, description: str | None, says: str + ) -> None: + path = _write(tmp_path, ANCHORED) + with pytest.raises(ConfigError, match=says): + save_preset_label(name, label, description, path) + assert path.read_text(encoding="utf-8") == ANCHORED + + +class TestDelete: + def test_the_entry_goes_and_the_rest_stays(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + delete_preset("borrower", path) + gone = ( + " borrower:\n label: Borrower\n brain: *one\n" + " worker: *one\n validator: *one\n" + ) + assert path.read_text(encoding="utf-8") == ANCHORED.replace(gone, "") + cfg = load_config(path) + assert list(cfg.presets) == ["shared"] + assert cfg.active_preset == "shared" + + def test_removing_the_active_entry_moves_active_to_the_first_left(self, tmp_path: Path) -> None: + path = _catalog(tmp_path) + before = load_config(path) + names = list(before.presets) + victim = names[0] + text = path.read_text(encoding="utf-8") + if "active_preset:" not in text: + path.write_text(text + f"\nactive_preset: {victim}\n", encoding="utf-8") + else: + from tstd.config_write import save_active_preset + + save_active_preset(victim, path) + delete_preset(victim, path) + cfg = load_config(path) + assert victim not in cfg.presets + assert cfg.active_preset == names[1] + assert f"active_preset: {names[1]}" in path.read_text(encoding="utf-8") + + def test_removing_the_last_entry_leaves_an_empty_catalog(self, tmp_path: Path) -> None: + path = _write(tmp_path, default_config_yaml()) + from tests.test_add_model import _add + + _add(path) + from tstd.config_write import save_active_preset + + save_active_preset("my-model", path) + delete_preset("my-model", path) + cfg = load_config(path) + assert cfg.presets == {} + assert cfg.active_preset == "" + assert cfg.has_model() is False + assert "\npresets: {}\n" in path.read_text(encoding="utf-8") + + def test_an_entry_another_one_borrows_from_is_refused(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + with pytest.raises(ConfigError, match="reuses part of it"): + delete_preset("shared", path) + assert path.read_text(encoding="utf-8") == ANCHORED + + def test_the_computer_use_helper_model_is_refused(self, tmp_path: Path) -> None: + text = catalog_config_yaml().replace( + 'local_worker_preset: ""', "local_worker_preset: vllm", 1 + ) + assert "local_worker_preset: vllm" in text + path = _write(tmp_path, text) + with pytest.raises(ConfigError, match="computer-use helper"): + delete_preset("vllm", path) + assert path.read_text(encoding="utf-8") == text + + def test_an_unknown_entry_is_refused(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + with pytest.raises(ConfigError, match="Unknown preset"): + delete_preset("nope", path) + assert path.read_text(encoding="utf-8") == ANCHORED + + +def _mock() -> MockProvider: + return MockProvider(default=Script(kind="text")) + + +class TestOverTheWire: + """The three verbs as Settings sends them, against a running daemon.""" + + async def test_duplicate_adds_an_entry_and_leaves_the_active_one(self, tmp_path: Path) -> None: + daemon, task, ws = await _start(tmp_path, _mock()) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + state = await _ask( + ws, + {"type": "duplicate_preset", "source": "local", "label": "Local, tight"}, + "setup_state", + ) + # setup_state lists the catalog sorted by key. + assert state["presets"] == sorted([*before["presets"], "local-tight"]) + assert state["preset_labels"]["local-tight"] == "Local, tight" + assert state["active_preset"] == before["active_preset"] + on_disk = load_config(daemon.config_path) + assert on_disk.presets["local-tight"].brain == on_disk.presets["local"].brain + finally: + await _stop(task, ws) + + async def test_a_second_copy_with_the_same_label_gets_its_own_key(self, tmp_path: Path) -> None: + _daemon, task, ws = await _start(tmp_path, _mock()) + try: + msg = {"type": "duplicate_preset", "source": "local", "label": "Copy"} + first = await _ask(ws, msg, "setup_state") + second = await _ask(ws, msg, "setup_state") + added = [n for n in second["presets"] if n not in first["presets"]] + assert len(added) == 1 and added[0] != "copy" + assert "copy" in first["presets"] + finally: + await _stop(task, ws) + + async def test_relabel_changes_what_the_picker_shows(self, tmp_path: Path) -> None: + _daemon, task, ws = await _start(tmp_path, _mock()) + try: + state = await _ask( + ws, + { + "type": "set_preset_label", + "name": "local", + "label": "On this computer", + "description": "Nothing leaves the machine", + }, + "setup_state", + ) + assert state["preset_labels"]["local"] == "On this computer" + assert state["preset_descriptions"]["local"] == "Nothing leaves the machine" + assert "local" in state["presets"] + finally: + await _stop(task, ws) + + async def test_delete_moves_active_and_an_open_chat_keeps_its_model( + self, tmp_path: Path + ) -> None: + workspace = tmp_path / "ws" + workspace.mkdir() + daemon, task, ws = await _start(tmp_path / "data", _mock()) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + victim = before["active_preset"] + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None and sess.config is not None + snapshot = sess.config + slug = snapshot.tier("brain").slug + + state = await _ask(ws, {"type": "delete_preset", "name": victim}, "setup_state") + assert victim not in state["presets"] + # The first entry left in the file, the loader's own choice. + left = list(load_config(daemon.config_path).presets) + assert victim not in left + assert state["active_preset"] == left[0] + assert state["has_model"] is True + # TD-1721: the open chat still calls the model it opened with. + assert sess.config is snapshot + assert sess.config.tier("brain").slug == slug + finally: + await _stop(task, ws) + + @pytest.mark.parametrize( + "msg", + [ + {"type": "duplicate_preset", "source": "nope", "label": "Copy"}, + {"type": "set_preset_label", "name": "nope", "label": "X"}, + {"type": "delete_preset", "name": "nope"}, + ], + ) + async def test_an_unknown_entry_is_a_typed_error( + self, tmp_path: Path, msg: dict[str, Any] + ) -> None: + daemon, task, ws = await _start(tmp_path, _mock()) + try: + text = daemon.config_path.read_text(encoding="utf-8") + reply = await _ask(ws, msg, "setup_state") + assert reply["type"] == "error" + assert reply["code"] == "bad_request" + assert "Unknown preset" in reply["message"] + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await _stop(task, ws) + + async def test_no_key_is_written_by_a_copy(self, tmp_path: Path) -> None: + daemon, task, ws = await _start(tmp_path, _mock()) + try: + await _ask( + ws, + {"type": "duplicate_preset", "source": "tst-default", "label": "Hosted copy"}, + "setup_state", + ) + copy = load_config(daemon.config_path).presets["hosted-copy"] + dumped = json.dumps(copy.model_dump(mode="json")) + assert "api_key" not in dumped and "sk-" not in dumped + assert ( + copy.brain.credential + == load_config(daemon.config_path).presets["tst-default"].brain.credential + ) + finally: + await _stop(task, ws) + + +def test_another_computer_may_send_none_of_them_and_only_this_one_may_delete() -> None: + verbs = {"duplicate_preset", "set_preset_label", "delete_preset"} + assert not verbs & MACHINE_PEER_VERBS + assert "delete_preset" in LOOPBACK_VERBS diff --git a/core/tstd/daemon.py b/core/tstd/daemon.py index 9b802320..5e543a06 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -224,6 +224,7 @@ stricter_rule, ) from .port_file import release_port_file +from .preset_write import delete_preset, duplicate_preset, save_preset_label from .protocol import ( AddModel, AddPin, @@ -263,6 +264,7 @@ DeleteJobTemplate, DeleteMcpSecret, DeleteMcpServer, + DeletePreset, DeleteSession, Deny, DenyVerify, @@ -274,6 +276,7 @@ DiagnosticsReport, Drain, DrainResult, + DuplicatePreset, EmailNotifyState, EndpointModels, EndSession, @@ -393,6 +396,7 @@ SetMcpServer, SetPlan, SetPreset, + SetPresetLabel, SetRemoteAttach, SetSessionPreset, SetSessionStar, @@ -2150,6 +2154,35 @@ async def _handle_add_model(self, msg: AddModel) -> str: log.info("model added", extra={"extra_fields": {"preset": name}}) return (await self._setup_state_event()).model_dump_json() + async def _handle_catalog_edit( + self, msg: DuplicatePreset | SetPresetLabel | DeletePreset + ) -> str: + """Copy, relabel or remove one catalog entry; ack with setup_state (TD-5237). + + The catalog changes for new chats and for the next switch. A chat + that is open keeps the snapshot it holds, a removed entry's included + (TD-1721), and no key is touched. + """ + try: + if isinstance(msg, DuplicatePreset): + made = preset_name_for(msg.label, set(self.config.presets)) + duplicate_preset(msg.source, made, msg.label, self.config_path) + fields = {"action": "duplicate", "preset": made, "source": msg.source} + elif isinstance(msg, SetPresetLabel): + save_preset_label(msg.name, msg.label, msg.description, self.config_path) + fields = {"action": "label", "preset": msg.name} + else: + delete_preset(msg.name, self.config_path) + fields = {"action": "delete", "preset": msg.name} + self._reload_user_config() + except ConfigError as e: + return build_error("bad_request", str(e)) + if isinstance(msg, DeletePreset): + for key in [k for k in self._card_results if k[0] == msg.name]: + del self._card_results[key] + log.info("model catalog edited", extra={"extra_fields": fields}) + return (await self._setup_state_event()).model_dump_json() + async def _test_model(self, preset: str | None) -> ModelTested: """One short message to *preset*'s model, so the user sees it answer.""" name = preset or self.config.active_preset @@ -4424,6 +4457,9 @@ async def send(reply: str) -> None: ) return (await self._setup_state_event()).model_dump_json() + if isinstance(msg, DuplicatePreset | SetPresetLabel | DeletePreset): + return await self._handle_catalog_edit(msg) + if isinstance(msg, SetTierSlug): try: save_tier_slug(msg.preset, msg.tier, msg.slug, self.config_path) diff --git a/core/tstd/peer_policy.py b/core/tstd/peer_policy.py index 91d306f0..120f3b99 100644 --- a/core/tstd/peer_policy.py +++ b/core/tstd/peer_policy.py @@ -141,8 +141,12 @@ # where keys and tokens are sent, and the phone listener. The narrower pages # a phone may use stay open to it; this one, reading included, is the # computer's own. +# ``delete_preset`` (TD-5237) removes a model from the catalog. Copying or +# relabelling one from a phone is as narrow as ``add_model``; taking one +# away is done at the computer whose config it is. LOOPBACK_VERBS: frozenset[str] = frozenset( { + "delete_preset", "drain", "trust_workspace_config", "steering_accept", diff --git a/core/tstd/preset_write.py b/core/tstd/preset_write.py new file mode 100644 index 00000000..98313b4f --- /dev/null +++ b/core/tstd/preset_write.py @@ -0,0 +1,266 @@ +"""Copy, relabel and remove catalog entries in the user's ``config.yaml`` (TD-5237). + +The walkthrough could only add a model, so the way to try a variation was +to overwrite the one entry already there. These three writers let Settings +keep several: copy an entry, give one a new label, remove one. + +Like :mod:`tstd.config_write`, every edit is surgical: the lines the entry +owns change and the rest of the file stays byte-identical. Unlike a single +value, an entry can be tied to others by a YAML anchor (``&my_model`` in +one, ``*my_model`` in the next), so each would-be file is loaded exactly as +the daemon loads it *before* it is written. An edit that would leave a file +that does not load is refused, and nothing is written. + +An entry's key is its permanent id: sessions, scheduled jobs and +``computer_use.local_worker_preset`` all store it. So "rename" here changes +the ``label`` a person sees and never the key. +""" + +from __future__ import annotations + +import json +import re +from pathlib import Path +from typing import Any + +import yaml + +from .config import ( + CREDENTIAL_ID_RE, + ConfigError, + ModelConfig, + config_from_data, + ensure_user_config, + load_config, +) +from .config_write import _atomic_write, _block_bounds, _find_key + +_ENTRY_INDENT_DEFAULT = 2 + + +def _is_content(line: str) -> bool: + stripped = line.strip() + return bool(stripped) and not stripped.startswith("#") + + +def _indent(line: str) -> int: + return len(line) - len(line.lstrip()) + + +def _content_end(lines: list[str], start: int, end: int) -> int: + """One past the last content line in ``[start, end)``. + + A block's bounds run on through blank lines and comments, and a comment + there usually introduces the *next* section. It stays with that section. + """ + last = start + for i in range(start, end): + if _is_content(lines[i]): + last = i + return last + 1 + + +def _presets_block(lines: list[str], config_path: Path) -> tuple[int, int, int]: + """``(header, end, entry_indent)`` of the top-level ``presets:`` block.""" + header = _find_key(lines, 0, len(lines), "presets", 0) + if header < 0 or lines[header].split(":", 1)[1].split("#", 1)[0].strip(): + # Missing, or written inline (``presets: {a: ...}``). + raise ConfigError(f"{config_path} has a `presets:` value this writer cannot edit") + end = _block_bounds(lines, header, 0) + for i in range(header + 1, end): + if _is_content(lines[i]): + return header, end, _indent(lines[i]) + return header, end, _ENTRY_INDENT_DEFAULT + + +def _entry_bounds( + lines: list[str], name: str, config_path: Path +) -> tuple[int, int, int, tuple[int, int]]: + """``(start, end, entry_indent, (presets_header, presets_end))`` for *name*.""" + header, end, entry_indent = _presets_block(lines, config_path) + start = _find_key(lines, header + 1, end, name, entry_indent) + if start < 0: + # Declared (the caller checked) but not as a plain ``name:`` line, + # e.g. a quoted key or a merge from elsewhere. + raise ConfigError(f"{config_path} declares {name!r} in a way this writer cannot edit") + block_end = min(_block_bounds(lines, start, entry_indent), end) + return start, _content_end(lines, start, block_end), entry_indent, (header, end) + + +def _checked(text: str, config_path: Path, why: str) -> ModelConfig: + """Load the would-be file as the daemon would, or refuse the edit.""" + try: + data = yaml.safe_load(text) + except yaml.YAMLError as e: + raise ConfigError(f"{why} ({e})") from e + if not isinstance(data, dict): + raise ConfigError(why) + try: + return config_from_data(data, config_path) + except ConfigError as e: + raise ConfigError(f"{why} ({e})") from e + + +def _require(config: ModelConfig, name: str) -> None: + if name not in config.presets: + declared = ", ".join(sorted(config.presets)) or "none" + raise ConfigError(f"Unknown preset {name!r}; declared presets: {declared}") + + +def _clean_label(label: str) -> str: + cleaned = label.strip() + if not cleaned: + raise ConfigError("Give the model a name") + if len(cleaned) > 40: + raise ConfigError("A model's name can be at most 40 characters") + return cleaned + + +def _yaml_block(value: dict[str, Any], indent: int) -> list[str]: + """*value* as block-style YAML lines, each indented by *indent* spaces.""" + dumped = yaml.safe_dump( + value, sort_keys=False, default_flow_style=False, allow_unicode=True, width=10_000 + ) + pad = " " * indent + return [pad + line if line else line for line in dumped.rstrip("\n").split("\n")] + + +def duplicate_preset(source: str, name: str, label: str, path: Path | None = None) -> Path: + """Write a copy of *source* as a new entry *name* labelled *label*. + + The copy is written from the loaded entry, not from its text: every + role is spelled out in full, so it shares no YAML anchor with the + original and an edit to one never changes the other. Only keys the + original set are written; defaults stay defaults. It lands straight + after the last entry. Never writes a secret: a role's ``credential`` is + a catalog id. + + Raises ``ConfigError`` when *source* is not declared, *name* is taken + or not a slug, or the result would not load. + """ + if not CREDENTIAL_ID_RE.match(name): + raise ConfigError(f"preset name {name!r} must be a lowercase slug [a-z][a-z0-9-]{{0,31}}") + cleaned = _clean_label(label) + config_path = ensure_user_config(path) + config = load_config(config_path) + _require(config, source) + if name in config.presets: + raise ConfigError(f"A preset named {name!r} already exists") + + lines = config_path.read_text(encoding="utf-8").split("\n") + header, end, entry_indent = _presets_block(lines, config_path) + body = config.presets[source].model_dump(mode="json", exclude_unset=True) + body.pop("label", None) + block = [ + f"{' ' * entry_indent}{name}:", + f"{' ' * (entry_indent * 2)}label: {json.dumps(cleaned)}", + *_yaml_block(body, entry_indent * 2), + ] + at = _content_end(lines, header, end) + lines[at:at] = block + + text = "\n".join(lines) + loaded = _checked(text, config_path, f"Could not copy {source!r}") + if name not in loaded.presets: + raise ConfigError(f"Could not copy {source!r}: the copy did not load as an entry") + _atomic_write(config_path, text) + return config_path + + +def save_preset_label( + name: str, label: str, description: str | None = None, path: Path | None = None +) -> Path: + """Set the ``label`` of entry *name*, and its ``description`` when given. + + *description* None leaves the line as it is; an empty one removes it. + The entry's key does not change. Strings are written JSON-encoded, a + valid YAML double-quoted scalar, so a label cannot inject YAML. + """ + cleaned = _clean_label(label) + if description is not None and len(description.strip()) > 120: + raise ConfigError("A description can be at most 120 characters") + config_path = ensure_user_config(path) + _require(load_config(config_path), name) + + lines = config_path.read_text(encoding="utf-8").split("\n") + start, end, entry_indent, _ = _entry_bounds(lines, name, config_path) + key_indent = entry_indent * 2 + for i in range(start + 1, end): + if _is_content(lines[i]): + key_indent = _indent(lines[i]) + break + pad = " " * key_indent + + wanted: list[tuple[str, str | None]] = [("label", cleaned)] + if description is not None: + wanted.append(("description", description.strip() or None)) + insert_at = start + 1 + for key, value in wanted: + at = _find_key(lines, start + 1, end, key, key_indent) + if at >= 0 and re.search(r":\s*[|>&*]", lines[at]): + raise ConfigError(f"{config_path} writes {name}.{key} in a way this writer cannot edit") + if value is None: + if at >= 0: + del lines[at] + end -= 1 + continue + line = f"{pad}{key}: {json.dumps(value)}" + if at >= 0: + lines[at] = line + insert_at = at + 1 + else: + lines.insert(insert_at, line) + insert_at += 1 + end += 1 + + text = "\n".join(lines) + loaded = _checked(text, config_path, f"Could not rename {name!r}") + if loaded.presets[name].label != cleaned: + raise ConfigError(f"Could not rename {name!r}: the file sets its label somewhere else") + _atomic_write(config_path, text) + return config_path + + +def delete_preset(name: str, path: Path | None = None) -> Path: + """Remove entry *name* from the catalog. + + Refused while ``computer_use.local_worker_preset`` names it, and when + another entry reuses part of it through a YAML anchor: the file would + no longer load. When *name* was ``active_preset``, the first entry left + becomes the active one (none left: the key is cleared), the same choice + the loader would make. Removing the last entry leaves ``presets: {}``. + """ + config_path = ensure_user_config(path) + config = load_config(config_path) + _require(config, name) + if config.computer_use.local_worker_preset.strip() == name: + raise ConfigError( + f"{name!r} is the computer-use helper model (computer_use.local_worker_preset). " + "Point that at another model first" + ) + + lines = config_path.read_text(encoding="utf-8").split("\n") + start, end, _entry_indent, (header, _presets_end) = _entry_bounds(lines, name, config_path) + del lines[start:end] + remaining = [n for n in config.presets if n != name] + if not remaining: + lines[header] = "presets: {}" + + text = "\n".join(lines) + if config.active_preset == name: + successor = remaining[0] if remaining else "" + active_line = f"active_preset: {successor}" if successor else 'active_preset: ""' + active = re.compile(r"^active_preset:.*$", re.MULTILINE) + if active.search(text): + text = active.sub(active_line, text, count=1) + + loaded = _checked( + text, + config_path, + f"Could not remove {name!r}: another model in the file reuses part of it. " + "Edit config.yaml by hand", + ) + if name in loaded.presets: + raise ConfigError(f"Could not remove {name!r}: the file declares it more than once") + _atomic_write(config_path, text) + return config_path diff --git a/core/tstd/protocol.py b/core/tstd/protocol.py index aa6e352f..84399ebb 100644 --- a/core/tstd/protocol.py +++ b/core/tstd/protocol.py @@ -1328,6 +1328,47 @@ class SetPreset(ClientMessage): name: str = Field(min_length=1) +class DuplicatePreset(ClientMessage): + """Copy one catalog entry as a new one (TD-5237). + + Settings' "Duplicate". The copy gets ``label`` and a key the daemon + derives from it; every role is written out in full, so it shares + nothing with ``source``. The active entry does not change. Narrow: a + name and a label cannot carry a secret. Acked with ``setup_state``. + """ + + type: Literal["duplicate_preset"] = "duplicate_preset" + source: str = Field(min_length=1) + label: str = Field(min_length=1, max_length=40) + + +class SetPresetLabel(ClientMessage): + """Give one catalog entry a new label (TD-5237). + + Settings' "Rename". The entry's key is its permanent id (sessions and + scheduled jobs store it) and never changes. ``description`` omitted + keeps the stored one; empty removes it. Acked with ``setup_state``. + """ + + type: Literal["set_preset_label"] = "set_preset_label" + name: str = Field(min_length=1) + label: str = Field(min_length=1, max_length=40) + description: str | None = Field(default=None, max_length=120) + + +class DeletePreset(ClientMessage): + """Remove one catalog entry (TD-5237). + + Refused while ``computer_use.local_worker_preset`` names it or another + entry reuses part of it. Removing the active entry makes the first one + left active. A chat already on it keeps the model it opened with until + it is switched. No key is deleted. Acked with ``setup_state``. + """ + + type: Literal["delete_preset"] = "delete_preset" + name: str = Field(min_length=1) + + class SetEngine(ClientMessage): """Pick the agent engine for new sessions (native TST loop or Grok ACP).""" @@ -4861,6 +4902,9 @@ class GoogleStatus(DaemonEvent): | AddModel | TestModel | SetPreset + | DuplicatePreset + | SetPresetLabel + | DeletePreset | SetEngine | SetGrokMode | RunGrokCommand @@ -5116,6 +5160,9 @@ class GoogleStatus(DaemonEvent): "test_model", "delete_api_key", "set_preset", + "duplicate_preset", + "set_preset_label", + "delete_preset", "set_engine", "set_grok_mode", "run_grok_command", diff --git a/docs/architecture.md b/docs/architecture.md index 316041af..e474897d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -648,6 +648,9 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `reset_config_value` | — | Put one key back: a key at a fixed place to what the packaged `config.yaml` has (else its built-in default), a key inside a list item or map entry to its built-in default by removing its line. A path that ends at a list item or map entry removes it. Acked and refused like `set_config_value`. Loopback only. | | `delete_api_key` | — | Remove an API key from the OS keychain. Only an API key's id, never another keychain item (TD-5182). | | `set_preset` | — | Choose the active model preset. | +| `duplicate_preset` | — | Settings → Model, "Duplicate": copy the `source` entry under a new `label`. The daemon derives the new key and writes every role out in full, so the copy shares no YAML anchor with the original. The active entry does not change. Acked with `setup_state` (TD-5237). | +| `set_preset_label` | — | Settings → Model, "Rename": a new `label` (and `description`, when sent) for one entry. The entry's key never changes. Acked with `setup_state` (TD-5237). | +| `delete_preset` | — | Settings → Model, "Remove": take one entry out of the catalog. This computer's window only. Refused while `computer_use.local_worker_preset` names it or another entry reuses part of it. Removing the active entry makes the first one left active; an open chat keeps the model it opened with. No key is deleted. Acked with `setup_state` (TD-5237). | | `set_engine` | — | Choose the agent engine for new sessions (`native` or `grok`). Acked with `setup_state`. | | `set_grok_mode` | yes | Switch the live Grok ACP session mode. | | `run_grok_command` | yes | Enqueue `/{name} {argument}` as the next Grok prompt. Refused with `session_busy` while the session restores files or closes its worktree (TD-5161). | diff --git a/docs/models.md b/docs/models.md index 18b44c90..40463570 100644 --- a/docs/models.md +++ b/docs/models.md @@ -110,6 +110,24 @@ An entry with no `label` is left out of the picker, unless a chat is already using it. To move an entry under **More models**, add `picker: more`. +## Keep more than one + +Settings → Model lists every model you have added, each with three +buttons: + +- **Duplicate** writes a copy with its own entry in `config.yaml`. Change + the copy (its models, keys or context size under **Build a team**, or by + hand) and the original stays as it was. A copy spells out all three roles + in full, so it never shares a YAML anchor with the entry it came from. +- **Rename** changes the label you see in the picker. The entry's key in + `config.yaml` stays the same, because chats and scheduled jobs remember a + model by that key. +- **Remove** takes the entry out of `config.yaml`. It deletes no key, and a + chat that is already using the model keeps it until you switch that chat. + If it was the model new chats use, the first one left takes over. Remove + is refused when another entry reuses part of this one (an anchor), or when + `computer_use.local_worker_preset` names it; the message says which. + ## A model on your own computer Any server that speaks the OpenAI chat API works, such as llama.cpp's diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index d064f357..023807bf 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13350,3 +13350,44 @@ Secret Service answers slowly the reply took about 5 s against the test's (`test_session_preset.py::TestSessionPreset::test_catalog_slug_edit_does_not_rewrite_an_open_session`). - [x] The file passes with every non-loopback connect and DNS lookup refused, and spawns no `secret-tool`. + +--- + +### TD-5237 — Duplicate, rename and remove a model in Settings + +**Size:** 3 · **Depends on:** TD-5172 · **Status:** Done + +The walkthrough could add a model and nothing could copy, relabel or +remove one, so trying a variation (another context size, another key, a +different model per role) meant overwriting the entry already there in +Settings → Model, and going back meant typing the old values in again. + +- [x] `duplicate_preset` writes a copy of an entry under a new label and a + key derived from it, every role spelled out, sharing no YAML anchor + with its source; the active entry does not change + (`test_preset_write.py::TestDuplicate`, `TestOverTheWire`). +- [x] `set_preset_label` changes an entry's label, and its description + when sent; the key never changes and only those lines change + (`TestLabel`). +- [x] `delete_preset` removes an entry; the active one moving to the first + left; the last one leaving `presets: {}`; an open chat keeps the model + it opened with (`TestDelete`, + `test_delete_moves_active_and_an_open_chat_keeps_its_model`). +- [x] A removal is refused, and nothing written, when another entry reuses + part of the entry or `computer_use.local_worker_preset` names it + (`test_an_entry_another_one_borrows_from_is_refused`, + `test_the_computer_use_helper_model_is_refused`). +- [x] Every would-be file is loaded as the daemon loads it before it is + written; a refusal leaves the file byte-identical + (`test_refusals_write_nothing`). +- [x] No key is written or deleted; another computer's hub may send none + of the three, and only this computer's window may remove + (`test_no_key_is_written_by_a_copy`, + `test_another_computer_may_send_none_of_them_and_only_this_one_may_delete`). +- [x] Settings → Model lists every model with Duplicate, Rename and Remove; + Remove asks first; a refusal shows the daemon's reason + (`model-catalog.test.ts`; the UI walk copies, renames and removes one). + +**Notes:** "Rename" is the label only. The key is the id that +`sessions.json`, scheduled-job pins (TD-3812) and +`computer_use.local_worker_preset` store. Class B entry in DECISIONS.md. diff --git a/ui/e2e/app-walk.spec.ts b/ui/e2e/app-walk.spec.ts index e90d9629..62c95d56 100644 --- a/ui/e2e/app-walk.spec.ts +++ b/ui/e2e/app-walk.spec.ts @@ -425,6 +425,8 @@ test("walks every settings section and its enabled controls", async () => { const content = dialog.locator(".content"); await snap(`settings:${section}`, content); if (section === "MCP servers") await saveWalkSecret(dialog); + // Before `exercise`: its "Add a model" click reopens Settings elsewhere. + if (section === "Model") await walkModelCatalog(dialog); await exercise(page, content); if (section === "MCP servers") await removeWalkServer(dialog); await expect(dialog).toBeVisible(); @@ -610,6 +612,45 @@ async function removeWalkServer(dialog: Locator): Promise { await expect(dialog.getByText("walk", { exact: true })).toHaveCount(0); } +/** + * "Your models" (TD-5237): copy the first model, give the copy a name, and + * remove it, so the catalog ends as it began. `exercise` leaves these rows + * alone (CATALOG_ROW): a blind click on Duplicate would add a row per run. + */ +async function walkModelCatalog(dialog: Locator): Promise { + const list = dialog.getByRole("list", { name: "Your models" }); + const before = await list.getByRole("listitem").count(); + expect(before).toBeGreaterThan(0); + await list.getByRole("button", { name: /^Duplicate / }).first().click(); + await expect(list.getByRole("listitem")).toHaveCount(before + 1); + + // Rows are in key order, and a key never changes, so the copy keeps its + // place through the rename. Find it by a button: its text runs on into them. + const rows = list.getByRole("listitem"); + let at = -1; + for (let i = 0; i <= before; i++) { + if ((await rows.nth(i).getByRole("button", { name: /^Duplicate .+ copy$/ }).count()) === 1) at = i; + } + expect(at).toBeGreaterThanOrEqual(0); + const row = rows.nth(at); + await row.getByRole("button", { name: /^Rename / }).click(); + await row.getByRole("textbox", { name: /^New name for / }).fill("Walk copy"); + await row.getByRole("button", { name: /^Cancel renaming / }).click(); + await row.getByRole("button", { name: /^Rename / }).click(); + await row.getByRole("textbox", { name: /^New name for / }).fill("Walk model"); + await row.getByRole("button", { name: /^Save name for / }).click(); + + await expect(row.getByRole("button", { name: "Remove Walk model", exact: true })).toBeVisible(); + await row.getByRole("button", { name: "Remove Walk model", exact: true }).click(); + await row.getByRole("button", { name: "Keep Walk model", exact: true }).click(); + await row.getByRole("button", { name: "Remove Walk model", exact: true }).click(); + await row.getByRole("button", { name: "Yes, remove Walk model", exact: true }).click(); + await expect(list.getByRole("listitem")).toHaveCount(before); + await expect(dialog.getByRole("alert")).toHaveCount(0); +} + +const CATALOG_ROW = /^(Duplicate|Rename|Remove) .+$/; + const SKIP_CLICK = /^(Close|Sign in with Microsoft|Sign in with Google|Connect GitHub|Disconnect|Send test email|Delete|Remove|Forget|Copy code|Open browser|Try again|How models work)$/; @@ -622,7 +663,7 @@ async function exercise(page: Page, content: Locator): Promise { const name = ((await button.getAttribute("aria-label")) || (await button.innerText())) .replace(/\s+/g, " ") .trim(); - if (name === "" || SKIP_CLICK.test(name)) continue; + if (name === "" || SKIP_CLICK.test(name) || CATALOG_ROW.test(name)) continue; await dismissExtraDialogs(page); await button.click({ timeout: 5_000 }); await dismissExtraDialogs(page); diff --git a/ui/e2e/census.json b/ui/e2e/census.json index 0d16d38c..3a52ac98 100644 --- a/ui/e2e/census.json +++ b/ui/e2e/census.json @@ -221,9 +221,18 @@ "Add a model", "Brain context window", "Brain model", + "Duplicate Fast", + "Duplicate Main", + "Duplicate Walk team", "How models work", "None (no key) OpenRouter", "Refresh from servers", + "Remove Fast", + "Remove Main", + "Remove Walk team", + "Rename Fast", + "Rename Main", + "Rename Walk team", "Validator context window", "Validator model", "Worker context window", diff --git a/ui/src/lib/components/ModelCatalog.svelte b/ui/src/lib/components/ModelCatalog.svelte new file mode 100644 index 00000000..f504c4f0 --- /dev/null +++ b/ui/src/lib/components/ModelCatalog.svelte @@ -0,0 +1,265 @@ + + +{#if settings.presets.length > 0} +

What you have added

+

+ Duplicate one to try a variation without changing the original. Removing one deletes no key, + and a chat already using it keeps it until you switch that chat. +

+
    + {#each settings.presets as name (name)} +
  • + {#if renaming === name} + { + if (e.key === "Enter") commitRename(name); + if (e.key === "Escape") { + e.stopPropagation(); + renaming = null; + } + }} + /> +
    + + +
    + {:else} +
    + {label(name)} + {#if name === settings.activePreset} + New chats use this + {/if} + {#if modelDescription(name, settings.presetDescriptions)} + {modelDescription(name, settings.presetDescriptions)} + {/if} +
    + {#if confirming === name} +
    + Remove {label(name)}? + + +
    + {:else} +
    + + + +
    + {/if} + {/if} +
  • + {/each} +
+ {#if problem} + + {/if} +{/if} + + diff --git a/ui/src/lib/components/SettingsModels.svelte b/ui/src/lib/components/SettingsModels.svelte index 77a62de6..5088b251 100644 --- a/ui/src/lib/components/SettingsModels.svelte +++ b/ui/src/lib/components/SettingsModels.svelte @@ -16,6 +16,7 @@ import { openAddModel } from "../onboarding.svelte.js"; import { onMount } from "svelte"; import { sendToDaemon } from "../connection-status.svelte.js"; + import ModelCatalog from "./ModelCatalog.svelte"; // TD-5197: the daemon refreshes model cards in the background, so the // numbers may have changed since the window last asked. Read them fresh @@ -86,6 +87,8 @@ + + {#if settings.hasModel && settings.activePreset}

Build a team

diff --git a/ui/src/lib/model-catalog.test.ts b/ui/src/lib/model-catalog.test.ts new file mode 100644 index 00000000..431fbf1e --- /dev/null +++ b/ui/src/lib/model-catalog.test.ts @@ -0,0 +1,206 @@ +// @vitest-environment jsdom +// +// TD-5237: Settings → Model lists the catalog with Duplicate, Rename and +// Remove. Each button sends one message; the daemon's reply is the truth. + +import { afterEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, unmount } from "svelte"; +import type { ClientMessageUnion, DaemonEventUnion } from "./protocol"; + +const mocks = vi.hoisted(() => ({ + sent: [] as ClientMessageUnion[], + sendOk: true, + subs: new Set<(event: DaemonEventUnion) => void>(), +})); + +vi.mock("./connection-status.svelte.js", () => { + const onDaemonEvent = (handler: (event: DaemonEventUnion) => void) => { + mocks.subs.add(handler); + return () => mocks.subs.delete(handler); + }; + return { + onEvent: onDaemonEvent, + onDaemonEvent, + sendToDaemon: (msg: ClientMessageUnion) => { + if (mocks.sendOk) mocks.sent.push(msg); + return mocks.sendOk; + }, + }; +}); + +import ModelCatalog from "./components/ModelCatalog.svelte"; +import { copyLabel, LABEL_MAX, renameTarget } from "./model-catalog"; +import { settings } from "./settings.svelte.js"; + +let component: ReturnType | null = null; + +function emit(event: Partial & { type: string }): void { + for (const sub of [...mocks.subs]) sub(event as DaemonEventUnion); + flushSync(); +} + +function open(): HTMLElement { + settings.presets = ["local", "hosted"]; + settings.presetLabels = { local: "Local", hosted: "Hosted" }; + settings.presetDescriptions = { hosted: "A provider's model" }; + settings.activePreset = "hosted"; + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(ModelCatalog, { target }); + flushSync(); + return target; +} + +function click(root: HTMLElement, name: string): void { + const button = root.querySelector(`button[aria-label="${name}"]`); + if (button === null) throw new Error(`no button named ${name}`); + button.click(); + flushSync(); +} + +function names(root: HTMLElement): string[] { + return [...root.querySelectorAll("button")].map((b) => b.getAttribute("aria-label") ?? ""); +} + +afterEach(() => { + if (component) unmount(component); + component = null; + document.body.innerHTML = ""; + mocks.sent = []; + mocks.sendOk = true; + mocks.subs.clear(); + settings.presets = []; + settings.presetLabels = {}; + settings.presetDescriptions = {}; + settings.activePreset = null; +}); + +describe("copyLabel", () => { + it("adds copy, then a number when that is taken", () => { + expect(copyLabel("Local", ["Local"])).toBe("Local copy"); + expect(copyLabel("Local", ["Local", "local COPY"])).toBe("Local copy 2"); + expect(copyLabel("Local", ["Local copy", "Local copy 2"])).toBe("Local copy 3"); + }); + + it("cuts a long label so the copy still fits", () => { + const made = copyLabel("x".repeat(LABEL_MAX), []); + expect(made).toHaveLength(LABEL_MAX); + expect(made.endsWith(" copy")).toBe(true); + }); +}); + +describe("renameTarget", () => { + it("is the trimmed label, or null when there is nothing to save", () => { + expect(renameTarget(" House ", "Local")).toBe("House"); + expect(renameTarget("Local", "Local")).toBeNull(); + expect(renameTarget(" ", "Local")).toBeNull(); + expect(renameTarget("x".repeat(LABEL_MAX + 1), "Local")).toBeNull(); + }); +}); + +describe("Settings → Model → What you have added", () => { + it("lists every model and marks the one new chats use", () => { + const root = open(); + const rows = [...root.querySelectorAll("li")].map((li) => li.textContent ?? ""); + expect(rows).toHaveLength(2); + expect(rows[0]).toContain("Local"); + expect(rows[0]).not.toContain("New chats use this"); + expect(rows[1]).toContain("Hosted"); + expect(rows[1]).toContain("New chats use this"); + expect(rows[1]).toContain("A provider's model"); + }); + + it("shows nothing before a model is set up", () => { + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(ModelCatalog, { target }); + flushSync(); + expect(target.textContent?.trim()).toBe(""); + }); + + it("Duplicate sends the source and a label no model has", () => { + const root = open(); + click(root, "Duplicate Local"); + expect(mocks.sent).toEqual([ + { type: "duplicate_preset", source: "local", label: "Local copy" }, + ]); + }); + + it("holds the other buttons until the daemon answers", () => { + const root = open(); + click(root, "Duplicate Local"); + const hosted = root.querySelector('button[aria-label="Remove Hosted"]'); + expect(hosted?.disabled).toBe(true); + emit({ type: "setup_state" }); + expect( + root.querySelector('button[aria-label="Remove Hosted"]')?.disabled, + ).toBe(false); + }); + + it("Rename sends the new label and keeps the key", () => { + const root = open(); + click(root, "Rename Local"); + const input = root.querySelector('input[aria-label="New name for Local"]'); + expect(input?.value).toBe("Local"); + input!.value = " On this computer "; + input!.dispatchEvent(new Event("input", { bubbles: true })); + flushSync(); + click(root, "Save name for Local"); + expect(mocks.sent).toEqual([ + { type: "set_preset_label", name: "local", label: "On this computer" }, + ]); + }); + + it("Rename with the same label, or cancelled, sends nothing", () => { + const root = open(); + click(root, "Rename Local"); + expect( + root.querySelector('button[aria-label="Save name for Local"]')?.disabled, + ).toBe(true); + click(root, "Cancel renaming Local"); + expect(mocks.sent).toEqual([]); + expect(names(root)).toContain("Rename Local"); + }); + + it("Remove asks first, and Keep sends nothing", () => { + const root = open(); + click(root, "Remove Hosted"); + expect(mocks.sent).toEqual([]); + expect(root.textContent).toContain("Remove Hosted?"); + click(root, "Keep Hosted"); + expect(mocks.sent).toEqual([]); + click(root, "Remove Hosted"); + click(root, "Yes, remove Hosted"); + expect(mocks.sent).toEqual([{ type: "delete_preset", name: "hosted" }]); + }); + + it("shows why the daemon refused, and lets the person try again", () => { + const root = open(); + click(root, "Remove Local"); + click(root, "Yes, remove Local"); + emit({ + type: "error", + code: "bad_request", + message: "Could not remove 'local': another model in the file reuses part of it.", + }); + expect(root.querySelector('[role="alert"]')?.textContent).toContain("reuses part of it"); + expect( + root.querySelector('button[aria-label="Remove Local"]')?.disabled, + ).toBe(false); + }); + + it("does not wait on a message that was never sent", () => { + const root = open(); + mocks.sendOk = false; + click(root, "Duplicate Local"); + expect( + root.querySelector('button[aria-label="Duplicate Hosted"]')?.disabled, + ).toBe(false); + }); + + it("ignores an error that belongs to something else", () => { + const root = open(); + emit({ type: "error", code: "bad_request", message: "not ours" }); + expect(root.querySelector('[role="alert"]')).toBeNull(); + }); +}); diff --git a/ui/src/lib/model-catalog.ts b/ui/src/lib/model-catalog.ts new file mode 100644 index 00000000..279b4010 --- /dev/null +++ b/ui/src/lib/model-catalog.ts @@ -0,0 +1,25 @@ +// TD-5237: Settings → Model lists every model in the catalog with Duplicate, +// Rename and Remove. The daemon does the work; this is the one thing the +// window decides by itself, the label a copy starts with. + +/** Longest label the daemon accepts (`Preset.label`). */ +export const LABEL_MAX = 40; + +/** A label for a copy of `label` that no other model has: "X copy", then + * "X copy 2", and so on. The base is cut so the result fits LABEL_MAX. */ +export function copyLabel(label: string, taken: readonly string[]): string { + const used = new Set(taken.map((name) => name.trim().toLowerCase())); + for (let n = 1; ; n += 1) { + const suffix = n === 1 ? " copy" : ` copy ${n}`; + const candidate = label.trim().slice(0, LABEL_MAX - suffix.length).trimEnd() + suffix; + if (!used.has(candidate.toLowerCase())) return candidate; + } +} + +/** What a typed label would be saved as, or null when it cannot be saved: + * empty, too long, or the label the model already has. */ +export function renameTarget(draft: string, current: string): string | null { + const next = draft.trim(); + if (next === "" || next.length > LABEL_MAX || next === current) return null; + return next; +} diff --git a/ui/src/lib/protocol-fixtures.json b/ui/src/lib/protocol-fixtures.json index 1a5619bf..011bd751 100644 --- a/ui/src/lib/protocol-fixtures.json +++ b/ui/src/lib/protocol-fixtures.json @@ -638,6 +638,21 @@ "type": "set_preset", "name": "tst-default" }, + "duplicate_preset": { + "type": "duplicate_preset", + "source": "tst-default", + "label": "My copy" + }, + "set_preset_label": { + "type": "set_preset_label", + "name": "tst-default", + "label": "House model", + "description": "General coding and chat" + }, + "delete_preset": { + "type": "delete_preset", + "name": "budget" + }, "set_engine": { "type": "set_engine", "kind": "grok" diff --git a/ui/src/lib/protocol.ts b/ui/src/lib/protocol.ts index 9cf6ce66..4eba8135 100644 --- a/ui/src/lib/protocol.ts +++ b/ui/src/lib/protocol.ts @@ -890,6 +890,30 @@ export interface SetPreset extends ClientMessage { name: string; } +// TD-5237: copy a catalog entry under a new label. The daemon derives the +// new entry's key and writes every role out in full. Acked with setup_state. +export interface DuplicatePreset extends ClientMessage { + type: "duplicate_preset"; + source: string; + label: string; +} + +// TD-5237: a new label for an entry. Its key never changes. `description` +// omitted keeps the stored one; empty removes it. Acked with setup_state. +export interface SetPresetLabel extends ClientMessage { + type: "set_preset_label"; + name: string; + label: string; + description?: string | null; +} + +// TD-5237: remove a catalog entry. This computer's window only. A chat +// already on it keeps its model until switched. Acked with setup_state. +export interface DeletePreset extends ClientMessage { + type: "delete_preset"; + name: string; +} + /** Pick native TST loop or Grok Build ACP for new sessions. */ export interface SetEngine extends ClientMessage { type: "set_engine"; @@ -1366,6 +1390,9 @@ export type ClientMessageUnion = | AddModel | TestModel | SetPreset + | DuplicatePreset + | SetPresetLabel + | DeletePreset | SetEngine | SetGrokMode | RunGrokCommand From 80afb2cb6dbdff12fe5b8914b7036f0dc0989069 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:17:51 +0000 Subject: [PATCH 03/35] TD-5234: a model carries its own context settings A catalog entry said which model each role uses and how big its window is. The settings that go with a model's size and speed were global, so moving between a small local model and a large hosted one meant editing them by hand. An entry may now carry `overrides` for six global sections: session (default_effort, default_ultracode), clear_tool_results, compaction, consult, workflow and computer_use (max_prompt_images). Each key has its global key's type and limits; anything else is an error at load, so approvals, shell rules, MCP, hooks, remote attach, autonomy, keys and the engine cannot be set per model. preset_apply.effective_config() is the one resolver and _bind_session_preset its one caller for sessions. A key the entry sets wins, an unset or null key keeps the global value, a list replaces whole. The loop already re-reads the session's snapshot each turn, so clearing, consult, the image cap, workflow limits and the summarizer follow with no change at the call sites. Three daemon-level reads of session.default_effort/default_ultracode now come from the chat's own snapshot: the new router, the revive fallback, and (new) the switch: switching a chat to an entry that names an effort or Ultracode default sets the chat's to it, per the owner's decision. No protocol change. Class B: recorded in DECISIONS.md. Co-Authored-By: Claude Fable 5.2 --- DECISIONS.md | 35 +++ core/tests/test_preset_overrides.py | 458 ++++++++++++++++++++++++++++ core/tstd/config.py | 19 ++ core/tstd/config_help.py | 34 ++- core/tstd/daemon.py | 23 +- core/tstd/preset_apply.py | 32 ++ core/tstd/preset_overrides.py | 148 +++++++++ docs/configuration.md | 1 + docs/models.md | 41 +++ docs/tst-desk-backlog.md | 50 +++ 10 files changed, 836 insertions(+), 5 deletions(-) create mode 100644 core/tests/test_preset_overrides.py create mode 100644 core/tstd/preset_apply.py create mode 100644 core/tstd/preset_overrides.py diff --git a/DECISIONS.md b/DECISIONS.md index 4019e5c7..7590037e 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -19868,3 +19868,38 @@ an entry used along with it (a key is shared between entries and is managed in Settings → API keys); letting a phone remove a model (it is the computer's config, and nothing on a phone needs it). +## 2026-11-20 — TD-5234: a model's `overrides` are a six-section whitelist, merged in one resolver (Class B) + +**Decision:** a catalog entry may carry `overrides` for six global sections: +`session` (`default_effort`, `default_ultracode` only), `clear_tool_results`, +`compaction`, `consult`, `workflow`, and `computer_use` (`max_prompt_images` +only). `preset_apply.effective_config(config, name)` is the one place they +are applied, and `_bind_session_preset` is its one caller for sessions. The +merge is per key, one level deep: a key the entry sets wins, an unset or +null key keeps the global value, a list replaces the global list whole. +Each merged section is rebuilt by the global section's own model, and every +entry is merged once at load so a bad value fails there. Switching a chat +to an entry that names an effort or Ultracode default sets the chat's to it +(owner, 2026-11-20); an entry that names neither leaves the chat's own. No +protocol message or field is added. + +**Rationale:** a session already holds a snapshot of the config it runs +with, and each turn re-reads the snapshot for clearing, consult, the image +cap, the workflow limits and the summarizer. Applying the overrides where +that snapshot is taken reaches all of them with no change at the call +sites, and keeps the TD-1721 rule that an open chat is not rewritten by a +Settings edit. The whitelist is what suits a model's size and speed. +Everything that decides what the agent may do is left out, so choosing a +model can never loosen the trust surface. The override models repeat each +key's limits (so the editor shows them and a bad value names its key), and +a test pins each one to the global key it overrides. + +**Rejected:** a free-form override of any top-level key (it would let a +catalog entry carry `policy`, `shell` or `mcp`); deep or list merging (a +model that wants no excluded tools could not say so); applying the entry's +effort only to new chats (the owner wants one click to set everything the +entry defines); `project_context` and `embeddings` in the list +(`project_context.token_budget` is read per workspace, and the embeddings +endpoint is not a property of the chat model); a separate `builds:` layer +that points at entries (a second catalog to keep in step). + diff --git a/core/tests/test_preset_overrides.py b/core/tests/test_preset_overrides.py new file mode 100644 index 00000000..bd7c9171 --- /dev/null +++ b/core/tests/test_preset_overrides.py @@ -0,0 +1,458 @@ +"""Context settings one model carries with it (TD-5234). + +``effective_config`` is what a chat on an entry runs with: the daemon's +config with that entry's ``overrides`` laid over the global sections. The +cases here are the merge rules, what load refuses, and that a chat really +gets them when it is opened on the entry, switched to it, or revived. +""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +import pytest +import yaml +from pydantic import BaseModel + +from tests.catalog_helpers import catalog_config_yaml +from tests.test_add_model import _ask, _start, _stop +from tstd.config import ( + ClearToolResultsConfig, + CompactionConfig, + ComputerUseConfig, + ConfigError, + ConsultConfig, + ModelConfig, + SessionConfig, + WorkflowConfig, + load_config, +) +from tstd.daemon import Daemon +from tstd.mock import MockProvider, Script +from tstd.preset_apply import effective_config +from tstd.preset_overrides import ( + OVERRIDE_SECTIONS, + ClearToolResultsOverride, + CompactionOverride, + ComputerUseOverride, + ConsultOverride, + PresetOverrides, + SessionOverride, + WorkflowOverride, +) +from tstd.preset_write import duplicate_preset +from tstd.protocol import TierState +from tstd.session_store import SessionStore + +PAIRS: list[tuple[type[BaseModel], type[BaseModel]]] = [ + (SessionOverride, SessionConfig), + (ClearToolResultsOverride, ClearToolResultsConfig), + (CompactionOverride, CompactionConfig), + (ConsultOverride, ConsultConfig), + (WorkflowOverride, WorkflowConfig), + (ComputerUseOverride, ComputerUseConfig), +] + +# What the entries below set. Every other key must keep its global value. +SMALL = """\ + overrides: + session: + default_effort: low + compaction: + summary: extractive + background_percent: 50 + clear_tool_results: + keep_recent_tool_results: 1 + exclude_tools: [fs_read] + consult: + tiers: [] + workflow: + max_parallel: 1 + computer_use: + max_prompt_images: 0 +""" +BIG = """\ + overrides: + session: + default_effort: high + default_ultracode: true +""" + + +def _with(text: str, entry: str, block: str) -> str: + header = f"\n {entry}:\n" + assert text.count(header) == 1, entry + return text.replace(header, header + block, 1) + + +def _catalog_text() -> str: + text = _with(catalog_config_yaml(), "local", SMALL) + return _with(text, "budget", BIG) + + +def _config(tmp_path: Path, text: str | None = None) -> ModelConfig: + path = tmp_path / "config.yaml" + path.write_text(_catalog_text() if text is None else text, encoding="utf-8") + return load_config(path) + + +class TestTheWhitelist: + def test_the_six_sections_are_the_whole_list(self) -> None: + assert set(PresetOverrides.model_fields) == set(OVERRIDE_SECTIONS) + assert set(OVERRIDE_SECTIONS) == { + "session", + "clear_tool_results", + "compaction", + "consult", + "workflow", + "computer_use", + } + # Each is a real global section of the same name. + assert set(OVERRIDE_SECTIONS) <= set(ModelConfig.model_fields) + + @pytest.mark.parametrize(("override", "base"), PAIRS) + def test_every_override_key_has_its_global_keys_type_and_limits( + self, override: type[BaseModel], base: type[BaseModel] + ) -> None: + for name, info in override.model_fields.items(): + assert name in base.model_fields, f"{override.__name__}.{name} overrides nothing" + ours, theirs = info, base.model_fields[name] + assert ours.annotation == theirs.annotation | None, name + assert repr(ours.metadata) == repr(theirs.metadata), name + assert ours.default is None and not ours.is_required(), name + + def test_only_what_suits_a_model_is_overridable_in_the_partial_sections(self) -> None: + assert set(SessionOverride.model_fields) == {"default_effort", "default_ultracode"} + assert set(ComputerUseOverride.model_fields) == {"max_prompt_images"} + # Whole sections: a key added to the global one must be decided here too. + for override, base in PAIRS[1:5]: + assert set(override.model_fields) == set(base.model_fields), base.__name__ + + @pytest.mark.parametrize( + "block", + [ + "policy: {skip_all: true}", + "shell: {read_only: {enabled: false}}", + "mcp: {servers: {}}", + "hooks: {after_write: []}", + "remote: {bind: 100.64.0.1}", + "autonomy: {runtime: none}", + "notify: {}", + "credentials: {}", + "engine: {kind: grok}", + "session: {log_max_events: 5}", + "session: {send_file_max_bytes: 1}", + "computer_use: {command: rm}", + "computer_use: {local_worker_preset: vllm}", + "workflow: {max_parralel: 2}", + ], + ) + def test_anything_else_is_an_error_at_load(self, tmp_path: Path, block: str) -> None: + text = _with(catalog_config_yaml(), "local", f" overrides:\n {block}\n") + with pytest.raises(ConfigError, match=r"presets\.local\.overrides"): + _config(tmp_path, text) + + @pytest.mark.parametrize( + "block", + [ + "workflow: {max_parallel: 0}", + "workflow: {max_parallel: 17}", + "compaction: {background_percent: 100}", + "compaction: {summary: clever}", + "session: {default_effort: enormous}", + "consult: {tiers: [manager]}", + "computer_use: {max_prompt_images: -1}", + ], + ) + def test_a_value_outside_the_global_keys_limits_is_an_error_at_load( + self, tmp_path: Path, block: str + ) -> None: + text = _with(catalog_config_yaml(), "local", f" overrides:\n {block}\n") + with pytest.raises(ConfigError, match=r"presets\.local\.overrides"): + _config(tmp_path, text) + + +class TestEffectiveConfig: + def test_no_overrides_is_the_snapshot_a_session_took_before(self, tmp_path: Path) -> None: + config = _config(tmp_path, catalog_config_yaml()) + for name in config.presets: + assert effective_config(config, name) == config.model_copy( + update={"active_preset": name} + ) + + def test_a_set_key_wins_and_every_other_key_keeps_its_global_value( + self, tmp_path: Path + ) -> None: + config = _config(tmp_path) + seen = effective_config(config, "local") + assert seen.active_preset == "local" + assert seen.session.default_effort == "low" + assert seen.compaction.summary == "extractive" + assert seen.compaction.background_percent == 50 + assert seen.clear_tool_results.keep_recent_tool_results == 1 + assert seen.workflow.max_parallel == 1 + assert seen.computer_use.max_prompt_images == 0 + + expected = { + "session": {"default_effort": "low"}, + "compaction": {"summary": "extractive", "background_percent": 50}, + "clear_tool_results": {"keep_recent_tool_results": 1, "exclude_tools": ["fs_read"]}, + "consult": {"tiers": []}, + "workflow": {"max_parallel": 1}, + "computer_use": {"max_prompt_images": 0}, + } + before, after = config.model_dump(), seen.model_dump() + for section, keys in expected.items(): + for key, value in before[section].items(): + assert after[section][key] == keys.get(key, value), f"{section}.{key}" + untouched = set(before) - set(expected) - {"active_preset"} + for section in untouched: + assert after[section] == before[section], section + + def test_a_list_replaces_the_global_list_whole(self, tmp_path: Path) -> None: + text = _catalog_text().replace(" exclude_tools: []", " exclude_tools: [web_fetch]", 1) + config = _config(tmp_path, text) + assert config.clear_tool_results.exclude_tools == ["web_fetch"] + seen = effective_config(config, "local") + assert seen.clear_tool_results.exclude_tools == ["fs_read"] + assert seen.consult.tiers == [] and config.consult.tiers == ["worker"] + + def test_a_null_or_an_empty_section_keeps_the_global_value(self, tmp_path: Path) -> None: + block = " overrides:\n workflow: {max_parallel: null}\n compaction: {}\n" + config = _config(tmp_path, _with(catalog_config_yaml(), "local", block)) + assert effective_config(config, "local") == config.model_copy( + update={"active_preset": "local"} + ) + + def test_the_global_config_and_other_entries_are_not_changed(self, tmp_path: Path) -> None: + config = _config(tmp_path) + before = config.model_dump() + effective_config(config, "local") + effective_config(config, "budget") + assert config.model_dump() == before + other = effective_config(config, "vllm") + assert other.workflow == config.workflow + assert other.session == config.session + + def test_a_later_global_edit_shows_through_the_keys_the_entry_leaves( + self, tmp_path: Path + ) -> None: + config = _config(tmp_path) + edited = config.model_copy( + update={"workflow": WorkflowConfig(max_parallel=5, max_tasks=20)} + ) + seen = effective_config(edited, "local") + assert seen.workflow.max_parallel == 1 # the entry's + assert seen.workflow.max_tasks == 20 # the new global + + def test_an_unknown_entry_has_no_overrides(self, tmp_path: Path) -> None: + config = _config(tmp_path) + assert effective_config(config, "nope") == config.model_copy( + update={"active_preset": "nope"} + ) + + +def test_a_copy_carries_its_sources_overrides(tmp_path: Path) -> None: + path = tmp_path / "config.yaml" + path.write_text(_catalog_text(), encoding="utf-8") + duplicate_preset("local", "local-2", "Local two", path) + config = load_config(path) + assert config.presets["local-2"].overrides == config.presets["local"].overrides + assert effective_config(config, "local-2").workflow.max_parallel == 1 + written = yaml.safe_load(path.read_text(encoding="utf-8"))["presets"]["local-2"] + # Only the keys the source set, so the copy still follows the global rest. + assert written["overrides"]["workflow"] == {"max_parallel": 1} + + +def _mock() -> MockProvider: + return MockProvider(default=Script(kind="stream", content="done")) + + +async def _seeded(tmp_path: Path) -> tuple[Daemon, Any, Any, Path]: + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_catalog_text(), encoding="utf-8") + workspace = tmp_path / "ws" + workspace.mkdir() + daemon, task, ws = await _start(data, _mock()) + return daemon, task, ws, workspace + + +def _last_tier_state(sess: Any) -> TierState: + states = [e for e in sess.event_log.all_events if isinstance(e, TierState)] + assert states + return states[-1] + + +class TestAChatGetsThem: + async def test_a_chat_opened_on_the_entry_starts_with_its_settings( + self, tmp_path: Path + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path) + try: + await _ask(ws, {"type": "set_preset", "name": "local"}, "setup_state") + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None and sess.config is not None and sess.router is not None + assert sess.config.workflow.max_parallel == 1 + assert sess.config.clear_tool_results.keep_recent_tool_results == 1 + assert sess.router.effort == "low" + # The daemon's own config is still the global one. + assert daemon.config.workflow.max_parallel == 3 + assert daemon.config.session.default_effort == "medium" + finally: + await _stop(task, ws) + + async def test_switching_a_chat_switches_its_settings_and_its_effort( + self, tmp_path: Path + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path) + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sid = opened["session_id"] + sess = daemon.session_registry.get(sid) + assert sess is not None and sess.config is not None and sess.router is not None + assert sess.preset == "tst-default" + assert sess.router.effort == "medium" and sess.router.ultracode is False + + await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "budget"}, + "session_list", + ) + assert sess.router.effort == "high" and sess.router.ultracode is True + state = _last_tier_state(sess) + assert state.effort == "high" and state.ultracode is True + + await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "local"}, + "session_list", + ) + assert sess.config.workflow.max_parallel == 1 + assert sess.config.compaction.summary == "extractive" + assert sess.router.effort == "low" + # "local" names an effort but no Ultracode default: the chat's switch stays. + assert sess.router.ultracode is True + finally: + await _stop(task, ws) + + async def test_an_entry_that_names_no_effort_leaves_the_chats_own(self, tmp_path: Path) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path) + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sid = opened["session_id"] + sess = daemon.session_registry.get(sid) + assert sess is not None and sess.router is not None + await ws.send(json.dumps({"type": "set_effort", "session_id": sid, "effort": "xhigh"})) + await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "vllm"}, + "session_list", + ) + assert sess.preset == "vllm" + assert sess.router.effort == "xhigh" + finally: + await _stop(task, ws) + + async def test_the_next_turn_runs_on_the_switched_settings(self, tmp_path: Path) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path) + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sid = opened["session_id"] + sess = daemon.session_registry.get(sid) + assert sess is not None + await ws.send(json.dumps({"type": "attach", "session_id": sid, "from_seq": 1})) + await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "local"}, + "session_list", + ) + done = await _ask( + ws, + {"type": "user_message", "session_id": sid, "content": "hello"}, + "turn_complete", + ) + assert done["type"] == "turn_complete" + # What the turn's tools were handed: the workflow tool reads its + # limits here, and the loop took the same object for clearing. + runtime = sess.delegate_runtime + assert runtime is not None + assert runtime.config is sess.config + assert runtime.config.workflow.max_parallel == 1 + assert runtime.config.clear_tool_results.keep_recent_tool_results == 1 + assert runtime.config.computer_use.max_prompt_images == 0 + finally: + await _stop(task, ws) + + async def test_a_settings_edit_does_not_rewrite_an_open_chat(self, tmp_path: Path) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path) + try: + await _ask(ws, {"type": "set_preset", "name": "local"}, "setup_state") + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None and sess.config is not None + snapshot = sess.config + await _ask(ws, {"type": "set_preset", "name": "budget"}, "setup_state") + assert sess.config is snapshot + assert sess.config.workflow.max_parallel == 1 + finally: + await _stop(task, ws) + + +class TestRevive: + async def _revived(self, tmp_path: Path, effort: str | None) -> Any: + data = tmp_path / "data" + workspace = tmp_path / "ws" + data.mkdir() + workspace.mkdir() + (data / "config.yaml").write_text(_catalog_text(), encoding="utf-8") + store = SessionStore(data) + await store.upsert("sid-1", str(workspace), "running", preset="local") + if effort is not None: + await store.set_effort("sid-1", effort) + from tstd.session_persist import SessionPersist + + SessionPersist(data).prepare("sid-1") + daemon = Daemon(data_dir=data) + await daemon._restore_sessions() + return daemon + + async def _close(self, daemon: Daemon) -> None: + for live in await daemon.session_registry.list_sessions(): + runner = daemon.session_registry.get_runner(live.id) + if runner is not None: + await runner.cancel() + + async def test_a_revived_chat_has_its_entrys_settings(self, tmp_path: Path) -> None: + daemon = await self._revived(tmp_path, None) + try: + sess = daemon.session_registry.get("sid-1") + assert sess is not None and sess.config is not None and sess.router is not None + assert sess.preset == "local" + assert sess.config.workflow.max_parallel == 1 + # No stored effort: it starts where its model says. + assert sess.router.effort == "low" + finally: + await self._close(daemon) + + async def test_a_revived_chat_keeps_the_effort_it_had(self, tmp_path: Path) -> None: + daemon = await self._revived(tmp_path, "xhigh") + try: + sess = daemon.session_registry.get("sid-1") + assert sess is not None and sess.router is not None + assert sess.router.effort == "xhigh" + finally: + await self._close(daemon) diff --git a/core/tstd/config.py b/core/tstd/config.py index b6f17548..1fac55c6 100644 --- a/core/tstd/config.py +++ b/core/tstd/config.py @@ -40,6 +40,7 @@ from .lsp_config import LspConfig from .mcp_values import coerce_map from .net import is_loopback_url as is_loopback_url +from .preset_overrides import PresetOverrides, merged_sections from .provider import TimeoutConfig from .shell_read_only import READ_ONLY_ENTRIES @@ -520,6 +521,9 @@ class Preset(BaseModel): picker: Literal["primary", "more", "team"] = "primary" # The Fast toggle (TD-5176): None = this model has no fast mode. fast: FastConfig | None = None + # Context settings for chats on this model (TD-5234), laid over the + # global sections of the same names. None = the global values. + overrides: PresetOverrides | None = None @field_validator("description") @classmethod @@ -1711,6 +1715,21 @@ def _null_presets_is_empty(cls, data: Any) -> Any: data = {**data, "local_servers": []} return data + @model_validator(mode="after") + def _preset_overrides_fit(self) -> ModelConfig: + """An entry's ``overrides`` must merge with the global sections (TD-5234). + + Each key has its own limits already. This catches what only the + merged section can show, at load, where the message names the entry, + instead of at the moment a chat switches to it. + """ + for name, preset in self.presets.items(): + try: + merged_sections(self, preset.overrides) + except ValueError as e: + raise ValueError(f"presets.{name}.overrides: {e}") from e + return self + @model_validator(mode="after") def _active_preset_falls_back(self) -> ModelConfig: """A missing or unknown ``active_preset`` lands on the first preset. diff --git a/core/tstd/config_help.py b/core/tstd/config_help.py index e5720c1e..1857b67c 100644 --- a/core/tstd/config_help.py +++ b/core/tstd/config_help.py @@ -10,7 +10,17 @@ from __future__ import annotations +from pydantic import BaseModel + from .config_help_features import FEATURE_HELP +from .preset_overrides import ( + ClearToolResultsOverride, + CompactionOverride, + ComputerUseOverride, + ConsultOverride, + SessionOverride, + WorkflowOverride, +) S = "seconds" TOKENS = "tokens" @@ -109,6 +119,14 @@ "Preset.description": "One line under the label on the picker: what this model is for.", "Preset.picker": "Where the picker lists it: the main list, More models, or Teams.", "Preset.fast": "The model's Fast mode. With this set, the picker shows a Fast switch.", + "Preset.overrides": "Context settings for chats on this model, in place of the global " + "ones of the same names. Nothing here changes approvals, shell rules or keys.", + "PresetOverrides.session": "What a chat on this model starts with.", + "PresetOverrides.clear_tool_results": "Clear stale tool results, for this model.", + "PresetOverrides.compaction": "Compaction, for this model.", + "PresetOverrides.consult": "The consult tool, for this model.", + "PresetOverrides.workflow": "Workflow limits, for this model's server.", + "PresetOverrides.computer_use": "The image cap, for this model.", "TierConfig.slug": "The model id sent as model. Optional on a loopback endpoint, where " "it is read from /v1/models.", "TierConfig.base_url": "The OpenAI-compatible endpoint. A bound key's own host wins over this.", @@ -183,6 +201,20 @@ for key, value in (_MODEL_HELP | FEATURE_HELP).items() } +#: A model's override of a global key (TD-5234) says what that key says. +OVERRIDE_OF: dict[str, tuple[type[BaseModel], str]] = { + "SessionOverride": (SessionOverride, "SessionConfig"), + "ClearToolResultsOverride": (ClearToolResultsOverride, "ClearToolResultsConfig"), + "CompactionOverride": (CompactionOverride, "CompactionConfig"), + "ConsultOverride": (ConsultOverride, "ConsultConfig"), + "WorkflowOverride": (WorkflowOverride, "WorkflowConfig"), + "ComputerUseOverride": (ComputerUseOverride, "ComputerUseConfig"), +} +for _name, (_model, _base) in OVERRIDE_OF.items(): + for _field in _model.model_fields: + _text, _unit = FIELD_HELP[f"{_base}.{_field}"] + FIELD_HELP[f"{_name}.{_field}"] = (f"{_text} Empty keeps the global value.", _unit) + #: Values the generic editor never shows or writes: names only, with where #: each value lives. Their own page stores a value in the keychain (TD-5110). SECRET_FIELDS: dict[str, str] = { @@ -225,7 +257,7 @@ #: Long text gets a multi-line box. MULTILINE_FIELDS: frozenset[str] = frozenset( - {"CompactionConfig.summary_prompt", "HookEntry.command"} + {"CompactionConfig.summary_prompt", "CompactionOverride.summary_prompt", "HookEntry.command"} ) #: The fields a new entry's form asks for, when not just its required ones. diff --git a/core/tstd/daemon.py b/core/tstd/daemon.py index 5e543a06..a3c75995 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -224,6 +224,7 @@ stricter_rule, ) from .port_file import release_port_file +from .preset_apply import effective_config, preset_overrides from .preset_write import delete_preset, duplicate_preset, save_preset_label from .protocol import ( AddModel, @@ -4693,7 +4694,8 @@ async def _revive_session( level, ultracode = read_effort_record(record.effort if record is not None else None) if sess.router is None: sess.router = TierRouter() - session_cfg = self.config.session + # A row with no stored effort starts where its model says (TD-5234). + session_cfg = _session_model_config(sess, self.config).session sess.router.set_effort(level if level is not None else session_cfg.default_effort) sess.router.set_ultracode(ultracode if level is not None else session_cfg.default_ultracode) sess.loaded_mcp_tools[:] = await asyncio.to_thread( @@ -4959,8 +4961,10 @@ async def _save_queue(messages: list[QueuedUserMessage]) -> None: router = sess.router if router is None: router = TierRouter() - router.set_effort(self.config.session.default_effort) - router.set_ultracode(self.config.session.default_ultracode) + # The chat's own snapshot: its model may set what a chat starts on (TD-5234). + starts_with = _session_model_config(sess, self.config).session + router.set_effort(starts_with.default_effort) + router.set_ultracode(starts_with.default_ultracode) sess.router = router # TD-5113: every config load below goes through the trust gate. @@ -5197,7 +5201,8 @@ def _bind_session_preset(self, sess: Session, name: str) -> str: """ chosen = self._resolved_preset(name) sess.preset = chosen - sess.config = self.config.model_copy(update={"active_preset": chosen}) + # TD-5234: the entry's own context settings ride on the snapshot. + sess.config = effective_config(self.config, chosen) # /compact between turns uses the new preset's worker too. if sess.compaction_summarizer is not None: sess.compaction_summarizer.rebind(sess.config.compaction, sess.config.tier("worker")) @@ -5305,6 +5310,16 @@ async def _handle_set_session_preset(self, msg: SetSessionPreset) -> str: f"Unknown preset {msg.name!r}; declared: {', '.join(sorted(self.config.presets))}", ) self._bind_session_preset(found, msg.name) + # TD-5234: one click sets everything the model defines. An effort or + # Ultracode default the entry itself names applies to this chat now; + # one it leaves to the global setting does not touch the chat's own. + chosen = preset_overrides(self.config, found.preset) + starts_with = chosen.session if chosen is not None else None + if starts_with is not None: + if starts_with.default_effort is not None: + found.router.set_effort(starts_with.default_effort) + if starts_with.default_ultracode is not None: + found.router.set_ultracode(starts_with.default_ultracode) await self._session_store.set_preset(found.id, found.preset) await found.event_log.add( tier_state_event(found, _session_model_config(found, self.config)) diff --git a/core/tstd/preset_apply.py b/core/tstd/preset_apply.py new file mode 100644 index 00000000..76712c8a --- /dev/null +++ b/core/tstd/preset_apply.py @@ -0,0 +1,32 @@ +"""The config a chat on one catalog entry runs with (TD-5234). + +One function, used wherever a session takes its snapshot of the catalog +(TD-1721): the daemon's config, pointed at the entry, with that entry's +``overrides`` laid over the global sections. Everything a turn reads from +the snapshot (stale-result clearing, compaction, consult, the image cap, +workflow limits, the effort a chat starts on) follows the entry with no +change at the call sites. +""" + +from __future__ import annotations + +from .config import ModelConfig +from .preset_overrides import PresetOverrides, merged_sections + + +def preset_overrides(config: ModelConfig, name: str) -> PresetOverrides | None: + """The ``overrides`` of entry *name*, or None when it sets none.""" + preset = config.presets.get(name) + return preset.overrides if preset is not None else None + + +def effective_config(config: ModelConfig, name: str) -> ModelConfig: + """*config* as a chat on entry *name* sees it. Pure: *config* is not changed. + + With no ``overrides`` this is ``config`` with ``active_preset`` set, + what a session's snapshot was before TD-5234. An unknown *name* has no + overrides either; choosing a real one is the caller's job. + """ + update: dict[str, object] = {"active_preset": name} + update.update(merged_sections(config, preset_overrides(config, name))) + return config.model_copy(update=update) diff --git a/core/tstd/preset_overrides.py b/core/tstd/preset_overrides.py new file mode 100644 index 00000000..2e48f8a8 --- /dev/null +++ b/core/tstd/preset_overrides.py @@ -0,0 +1,148 @@ +"""Context settings one model carries with it (TD-5234). + +A catalog entry already says which model each role uses and how big its +window is. The settings that go with a model's size and speed did not +travel with it: how compaction writes its summary, when stale tool results +are cleared, how many workers run at once, the effort a chat starts on. +They were global, so moving between a small local model and a large hosted +one meant editing them by hand each time. + +``overrides`` on an entry sets any of them for chats on that entry. It is +a whitelist of six sections, each a partial of the global section with the +same limits. Nothing here can loosen what the agent may do: approvals, the +shell lists, MCP servers, hooks, remote attach, notifications, autonomy and +keys are not in it, so picking a model never changes the trust surface. + +This module imports nothing from :mod:`tstd.config`, which imports it. +""" + +from __future__ import annotations + +from typing import Literal + +from pydantic import BaseModel, ConfigDict, Field, field_validator + +from .effort import EffortLevel + +# Spelled out, not imported: tstd.config imports this module. A test pins +# every field here to the annotation and limits of the field it overrides. +_TierName = Literal["brain", "worker", "validator"] + + +class _Partial(BaseModel): + """A section where every key is optional and an unknown key is an error.""" + + model_config = ConfigDict(extra="forbid") + + def changes(self) -> dict[str, object]: + """The keys this entry sets. A key left out, or null, keeps the global value.""" + return { + key: value + for key, value in self.model_dump(exclude_unset=True).items() + if value is not None + } + + +class CompactionOverride(_Partial): + """``compaction`` for chats on this model. See ``CompactionConfig``.""" + + summary: Literal["model", "extractive"] | None = None + summary_prompt: str | None = None + summary_max_tokens: int | None = Field(default=None, ge=64) + summary_timeout_seconds: float | None = Field(default=None, gt=0) + background_percent: int | None = Field(default=None, ge=0, le=99) + reinject_files: int | None = Field(default=None, ge=0) + reinject_tokens: int | None = Field(default=None, ge=0) + + +class ClearToolResultsOverride(_Partial): + """``clear_tool_results`` for chats on this model.""" + + enabled: bool | None = None + keep_recent_tool_results: int | None = Field(default=None, ge=0) + clear_at_least_tokens: int | None = Field(default=None, ge=0) + exclude_tools: list[str] | None = None + + +class WorkflowOverride(_Partial): + """``workflow`` for chats on this model: size it to this model's server.""" + + max_parallel: int | None = Field(default=None, ge=1, le=16) + max_tasks: int | None = Field(default=None, ge=1, le=32) + + +class ConsultOverride(_Partial): + """``consult`` for chats on this model.""" + + tiers: list[_TierName] | None = None + max_per_turn: int | None = Field(default=None, ge=1, le=20) + + @field_validator("tiers") + @classmethod + def _dedupe_tiers(cls, value: list[_TierName] | None) -> list[_TierName] | None: + return None if value is None else list(dict.fromkeys(value)) + + +class SessionOverride(_Partial): + """What a chat on this model starts with. The log and file caps stay global.""" + + default_effort: EffortLevel | None = None + default_ultracode: bool | None = None + + +class ComputerUseOverride(_Partial): + """How many images this model's requests may keep. The rest stays global.""" + + max_prompt_images: int | None = Field(default=None, ge=0) + + +#: The global sections an entry may override, in config.yaml's order. +OVERRIDE_SECTIONS: tuple[str, ...] = ( + "session", + "clear_tool_results", + "compaction", + "consult", + "workflow", + "computer_use", +) + + +class PresetOverrides(_Partial): + """The ``overrides`` block of one catalog entry.""" + + session: SessionOverride | None = None + clear_tool_results: ClearToolResultsOverride | None = None + compaction: CompactionOverride | None = None + consult: ConsultOverride | None = None + workflow: WorkflowOverride | None = None + computer_use: ComputerUseOverride | None = None + + def sections(self) -> dict[str, dict[str, object]]: + """Each section this entry changes, with the keys it sets.""" + found: dict[str, dict[str, object]] = {} + for name in OVERRIDE_SECTIONS: + part: _Partial | None = getattr(self, name) + if part is not None and part.changes(): + found[name] = part.changes() + return found + + +def merged_sections(config: BaseModel, overrides: PresetOverrides | None) -> dict[str, BaseModel]: + """The global sections of *config* with *overrides* laid over them. + + Only sections the entry changes are returned. A key the entry sets + wins; every other key keeps *config*'s value; a list replaces the + global list whole. Each result is built by the section's own model, so + its rules apply to the merged value too. *config* is not changed. + + Raises: + ValueError: A merged section is not valid. pydantic's message + names the key. + """ + if overrides is None: + return {} + merged: dict[str, BaseModel] = {} + for name, changes in overrides.sections().items(): + base: BaseModel = getattr(config, name) + merged[name] = type(base).model_validate({**base.model_dump(), **changes}) + return merged diff --git a/docs/configuration.md b/docs/configuration.md index 64b385da..9fcc4eb8 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1980,6 +1980,7 @@ Every preset declares all three tiers. Omitting one is a load error naming it. | `description` | string, up to 120 characters | One line under the label in the picker: what this model is for. Default: none. | | `picker` | `primary` \| `more` \| `team` | `primary` puts the preset on the picker's main list; `more` puts it under "More models"; `team` (a preset whose tiers use different models) puts it under "Teams", a submenu shown only when one exists. Default `primary`. | | `fast` | map | The model's Fast mode: the picker shows a Fast switch under Effort only for a preset with this block. `hook_url` is a loopback switch for a local model that gets faster by borrowing hardware: the daemon POSTs `{"model": hook_model, "on": true\|false, "by": "tst-desk"}` and reads the reply (409 with `error` = it will not switch now, and the switch stays off); at the start of each turn it GETs the same URL and turns the toggle off if the switch already ended Fast. `hook_model` names this model at that switch. `extra_body` is merged into every request while Fast is on, for an API model with a priority or fast tier. Either or both. `note` is one line the switch shows while it turns on, e.g. "Borrows all four GPUs; about 2 min to switch". `hook_url` must be loopback. Default: none. The Ultracode switch after Fast is not a preset key: every model has it (§2 `session` `default_ultracode`, §2 `workflow`). | +| `overrides` | map | Context settings for chats on this model, in place of the global ones of the same names (TD-5234). Six sections may appear, each with any of the keys listed: `session` (`default_effort`, `default_ultracode`), `clear_tool_results` (`enabled`, `keep_recent_tool_results`, `clear_at_least_tokens`, `exclude_tools`), `compaction` (`summary`, `summary_prompt`, `summary_max_tokens`, `summary_timeout_seconds`, `background_percent`, `reinject_files`, `reinject_tokens`), `consult` (`tiers`, `max_per_turn`), `workflow` (`max_parallel`, `max_tasks`) and `computer_use` (`max_prompt_images`). Each key means what it means in its §2 section and has the same limits. A key the model sets wins; a key left out, or `null`, keeps the global value; a list replaces the global list whole. A chat takes these when it is opened on the model or switched to it, like the model itself, and keeps them until it is switched again. Switching a chat to a model that sets `session.default_effort` or `session.default_ultracode` sets that chat's effort or Ultracode switch to it; a model that leaves them out leaves the chat's own choice alone. Any other section or key is an error at load: approvals, shell rules, MCP servers, hooks, remote attach, notifications, autonomy and keys cannot be set per model. Default: none. | Routing is turn-count and failure driven; it does not read anything from this file beyond the three tier definitions. The composer has no tier chip (TD-5172); a client can still pin a tier diff --git a/docs/models.md b/docs/models.md index 40463570..0c178d1e 100644 --- a/docs/models.md +++ b/docs/models.md @@ -128,6 +128,47 @@ buttons: is refused when another entry reuses part of this one (an anchor), or when `computer_use.local_worker_preset` names it; the message says which. +## Settings that travel with a model + +Some settings suit a model's size and speed rather than your taste: how +compaction writes its summary, when old tool results are cleared, how many +workers a server can serve at once, the effort a chat starts on. Put them +under `overrides` in the model's entry and they apply to every chat on +that model, in place of the global values. Switching a chat to the model +switches them too. + + +```yaml +presets: + small-local: + label: Small local + description: A small model on this computer + brain: &small + base_url: http://127.0.0.1:8000/v1 + context_window: 32768 + max_output_tokens: 4096 + input_price: 0.0 + output_price: 0.0 + cache_read_price: 0.0 + worker: *small + validator: *small + overrides: + session: + default_effort: low + compaction: + summary: extractive + clear_tool_results: + keep_recent_tool_results: 2 + workflow: + max_parallel: 1 +``` + +Only the keys you write change; the rest keep their global values. The +sections and keys that may appear are listed under `overrides` in +[configuration.md](configuration.md#a-preset). Approvals, shell rules, +MCP servers, hooks and keys are not among them: choosing a model never +changes what the agent is allowed to do. + ## A model on your own computer Any server that speaks the OpenAI chat API works, such as llama.cpp's diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 023807bf..b4840b37 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13391,3 +13391,53 @@ Settings → Model, and going back meant typing the old values in again. **Notes:** "Rename" is the label only. The key is the id that `sessions.json`, scheduled-job pins (TD-3812) and `computer_use.local_worker_preset` store. Class B entry in DECISIONS.md. + +--- + +### TD-5234 — A model carries its own context settings + +**Size:** 5 · **Depends on:** TD-1721, TD-5237 · **Status:** Done +**Approach signed off:** 2026-11-20 (owner, relayed by the coordinating session). + +A catalog entry said which model each role uses and how big its window is. +The settings that go with a model's size and speed were global: how +compaction writes its summary, when stale tool results are cleared, how +many workers run at once, the effort a chat starts on. Moving between a +small local model and a large hosted one meant editing them by hand. + +- [x] An entry may carry `overrides` with six sections (`session`, + `clear_tool_results`, `compaction`, `consult`, `workflow`, + `computer_use`); every key has its global key's type and limits + (`test_preset_overrides.py::TestTheWhitelist`). +- [x] Any other section or key, or a value outside the limits, is an error + at load that names the entry; approvals, shell rules, MCP servers, + hooks, remote attach, notifications, autonomy, keys and the engine + cannot be set per model (`test_anything_else_is_an_error_at_load`, + `test_a_value_outside_the_global_keys_limits_is_an_error_at_load`). +- [x] `effective_config` is pure: a key the entry sets wins, every other + key keeps its global value, a list replaces whole, and an entry with + no `overrides` gives the snapshot a session took before + (`TestEffectiveConfig`). +- [x] A chat opened on an entry, switched to it, or revived on it runs with + its settings; a Settings change does not rewrite an open chat + (`TestAChatGetsThem`, `TestRevive`). +- [x] The turn after a switch hands its tools the switched settings + (`test_the_next_turn_runs_on_the_switched_settings`). +- [x] Switching a chat to an entry that names `default_effort` or + `default_ultracode` sets the chat's to it; an entry that names + neither leaves the chat's own + (`test_switching_a_chat_switches_its_settings_and_its_effort`, + `test_an_entry_that_names_no_effort_leaves_the_chats_own`). +- [x] A copy (TD-5237) carries its source's `overrides` + (`test_a_copy_carries_its_sources_overrides`). +- [x] Settings → Configuration shows the new keys with words + (`test_config_schema.py`), and both guides document them + (`test_docs_config_reference.py`, `test_docs_models_guide.py`). + +**Notes:** No protocol change. `project_context` and `embeddings` are left +global: the first is read per workspace, not per chat. The compaction +trigger itself is a constant, not a key; a role's `context_window` is what +moves it, and an entry already carries that. Class B entry in DECISIONS.md. +TD-5239 adds the engine to the same resolver, and refuses `overrides` on an +entry with no model of its own. + From 3710bdab4b671e11b07730edc78e21046bc0fab3 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:46:08 +0000 Subject: [PATCH 04/35] TD-5239: a model can name its engine, and a Grok one needs no model of its own MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Local, API and Grok are meant to be three entries on one picker. The engine was a separate global switch, and a catalog entry had to name a model for each of three roles, which the Grok CLI does not use. Preset.engine is native, grok or unset (unset = engine.kind, as before). The three roles are optional as a group: all or none, and none only with engine: grok. overrides and fast on a Grok entry are a load error. A session takes its engine at creation: the caller's (a scheduled pin), else the entry's, else the global one. It keeps it: set_session_preset across engines, or onto an entry with no model from TST Desk's own loop, is refused with engine_mismatch and changes nothing. For an entry with no roles, tiers() returns the empty-catalog stand-ins and every walk over the catalog's roles goes through Preset.tier_map(): credential and backup checks, key hosts, model cards, the slug snapshot, the editor checks, named workers, the model test. Memory distill and the doctor's probe use native_default(): the active entry if it has a model, else the first that does, else skipped. A scheduled job's own engine pin still wins; an empty one takes the pinned entry's. A job pinned to the native loop on an entry with no model gets a receipt. setup_state.preset_engines is the one additive wire field. The picker marks a Grok entry and holds an entry on another engine than the chat's, with the reason; Settings → Model shows no roles for one that has none. Also records TD-5259 (distill follows the chat's model) as a possible later story; nothing is built for it. Class B: recorded in DECISIONS.md. Co-Authored-By: Claude Fable 5.2 --- DECISIONS.md | 36 ++ core/tests/test_preset_engine.py | 432 ++++++++++++++++++ core/tstd/config.py | 84 +++- core/tstd/config_editor_checks.py | 4 +- core/tstd/config_help.py | 9 +- core/tstd/daemon.py | 94 +++- core/tstd/key_hosts.py | 4 +- core/tstd/model_cards.py | 6 +- core/tstd/preset_apply.py | 36 ++ core/tstd/protocol.py | 3 + core/tstd/scheduler/pin.py | 36 +- core/tstd/scheduler/runner.py | 2 + core/tstd/tools/delegate_named.py | 9 +- docs/architecture.md | 2 +- docs/configuration.md | 5 +- docs/models.md | 22 + docs/tst-desk-backlog.md | 74 +++ ui/src/lib/components/SettingsModels.svelte | 16 +- .../lib/components/chat/ModelControls.svelte | 7 +- ui/src/lib/components/chat/ModelPicker.svelte | 28 +- ui/src/lib/model-engine.test.ts | 115 +++++ ui/src/lib/model-engine.ts | 37 ++ ui/src/lib/protocol-fixtures.json | 1 + ui/src/lib/protocol.ts | 2 + ui/src/lib/settings.svelte.ts | 4 + 25 files changed, 1019 insertions(+), 49 deletions(-) create mode 100644 core/tests/test_preset_engine.py create mode 100644 ui/src/lib/model-engine.test.ts create mode 100644 ui/src/lib/model-engine.ts diff --git a/DECISIONS.md b/DECISIONS.md index 7590037e..dc36ea33 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -19903,3 +19903,39 @@ entry defines); `project_context` and `embeddings` in the list endpoint is not a property of the chat model); a separate `builds:` layer that points at entries (a second catalog to keep in step). +## 2026-11-20 — TD-5239: an entry names its engine; a Grok entry's roles are optional as a group (Class B) + +**Decision:** `Preset.engine` is `native`, `grok` or unset (unset means +`engine.kind`). `brain`, `worker` and `validator` are optional as a group: +all three or none, and none only with `engine: grok`. `overrides` and `fast` +on a Grok entry are a load error. A session's engine is set once, at +creation: the caller's (a scheduled pin), else the entry's, else the global +one. `set_session_preset` across engines, or onto an entry with no model +from a session on TST Desk's own loop, is refused with `engine_mismatch`. +For an entry with no roles, `ModelConfig.tiers()` returns the empty-catalog +stand-ins (TD-5172), and every walk over the catalog's roles goes through +`Preset.tier_map()`, which is empty for it. Work no chat owns (memory +distill, the doctor's probe) uses `ModelConfig.native_default()`: the active +entry if it has a model, else the first that does, else it is skipped +(owner, 2026-11-20: no `helper` key). `setup_state.preset_engines` is the one +additive wire field; there is no new client message. + +**Rationale:** the Grok CLI brings its own model and sign-in, so requiring +three roles on its entry would mean writing three that are never used. One +catalog with an optional group keeps one YAML block, one picker list and one +set of writers. The stand-in path already existed and is already safe: a +turn that reaches it fails with the "add a model" message before any request +is built. A session's loop cannot be swapped under a conversation, so the +engine is fixed and a cross-engine switch is refused rather than half done; +TD-5235 turns that refusal into "start a new chat on it". Refusing +`overrides` and `fast` on a Grok entry, rather than ignoring them, keeps a +setting that does nothing from sitting in the file looking active. + +**Rejected:** a separate `builds:` layer pointing at entries (a second +catalog to keep in step); dummy roles on a Grok entry; a `helper` key naming +the entry for off-turn work (one more thing to set, and the fallback is +predictable); changing an open session's engine in place; making +`setup_state.engine` mean the active entry's engine (Settings → Engine shows +that field as the global setting, so the window computes a new chat's engine +from `preset_engines` instead). + diff --git a/core/tests/test_preset_engine.py b/core/tests/test_preset_engine.py new file mode 100644 index 00000000..3f8d8676 --- /dev/null +++ b/core/tests/test_preset_engine.py @@ -0,0 +1,432 @@ +"""A catalog entry can name its engine, and a Grok entry needs no model (TD-5239). + +Local, API and Grok are three entries of one catalog. A Grok entry is a +label and ``engine: grok``: the Grok CLI brings its own model, so the three +roles may be left out. These cases cover what load accepts, what the rest +of the code sees for an entry with no roles, which engine a chat gets, and +that a chat keeps the engine it started on. +""" + +from __future__ import annotations + +import asyncio +import json +import re +from pathlib import Path +from typing import Any + +import pytest + +from tests.catalog_helpers import catalog_config_yaml +from tests.test_add_model import _ask, _start, _stop +from tstd.config import ConfigError, ModelConfig, load_config +from tstd.daemon import Daemon, _snapshot_slugs +from tstd.key_hosts import every_tier +from tstd.mock import MockProvider, Script +from tstd.model_cards import targets +from tstd.preset_apply import engine_refusal, no_model_refusal +from tstd.preset_write import delete_preset, duplicate_preset, save_preset_label +from tstd.scheduler.pin import ScheduledPin, pin_failure_summary, pinned_engine +from tstd.session import Session + +GROK = """\ + grok: + label: Grok + description: The Grok CLI, with its own model + engine: grok +""" +PRESETS_HEADER = "\npresets:\n" + + +def _text(extra: str = GROK, *, engine_kind: str | None = None) -> str: + """The test catalog with *extra* entries first and ``tst-default`` active.""" + text = catalog_config_yaml() + assert text.count(PRESETS_HEADER) == 1 + text = text.replace(PRESETS_HEADER, PRESETS_HEADER + extra, 1) + # Named, or the loader would pick the first entry, which is the extra one. + assert not re.search(r"^active_preset:", text, re.MULTILINE) + text += "\nactive_preset: tst-default\n" + if engine_kind is not None: + shipped = "\nengine:\n kind: native\n" + assert text.count(shipped) == 1 + text = text.replace(shipped, f"\nengine:\n kind: {engine_kind}\n", 1) + return text + + +def _config(tmp_path: Path, text: str | None = None) -> ModelConfig: + path = tmp_path / "config.yaml" + path.write_text(_text() if text is None else text, encoding="utf-8") + return load_config(path) + + +def _entry(body: str) -> str: + return " odd:\n" + body + + +TIER = """\ + base_url: http://127.0.0.1:8000/v1 + context_window: 32768 + max_output_tokens: 4096 + input_price: 0.0 + output_price: 0.0 + cache_read_price: 0.0 +""" + + +class TestWhatLoads: + def test_a_grok_entry_needs_no_roles(self, tmp_path: Path) -> None: + config = _config(tmp_path) + grok = config.presets["grok"] + assert grok.engine == "grok" + assert grok.has_tiers is False and grok.tier_map() == {} + assert grok.label == "Grok" + + def test_a_grok_entry_may_still_name_all_three(self, tmp_path: Path) -> None: + body = f" engine: grok\n brain:\n{TIER} worker:\n{TIER} validator:\n{TIER}" + config = _config(tmp_path, _text(_entry(body))) + assert config.presets["odd"].has_tiers is True + assert set(config.presets["odd"].tier_map()) == {"brain", "worker", "validator"} + + def test_a_catalog_from_before_loads_as_it_did(self, tmp_path: Path) -> None: + config = _config(tmp_path, catalog_config_yaml()) + assert all(p.engine is None and p.has_tiers for p in config.presets.values()) + assert {config.preset_engine(name) for name in config.presets} == {"native"} + + @pytest.mark.parametrize( + ("body", "says"), + [ + (" label: No model\n", "Only an entry with `engine: grok`"), + (" label: No model\n engine: native\n", "Only an entry with `engine: grok`"), + ( + f" engine: grok\n brain:\n{TIER} worker:\n{TIER}", + "name all three roles or none of them; missing: validator", + ), + (f" brain:\n{TIER}", "missing: worker, validator"), + ( + " engine: grok\n overrides:\n workflow: {max_parallel: 1}\n", + "`overrides` does nothing on an `engine: grok` entry", + ), + ( + " engine: grok\n fast:\n extra_body: {priority: true}\n", + "`fast` does nothing on an `engine: grok` entry", + ), + (" engine: cursor\n", "presets.odd.engine"), + ], + ) + def test_what_is_refused(self, tmp_path: Path, body: str, says: str) -> None: + with pytest.raises(ConfigError) as caught: + _config(tmp_path, _text(_entry(body))) + assert says in str(caught.value) + assert "odd" in str(caught.value) + + +class TestWhatTheRestSees: + def test_an_active_grok_entry_is_a_model_that_needs_no_key(self, tmp_path: Path) -> None: + config = _config(tmp_path).model_copy(update={"active_preset": "grok"}) + assert config.has_model() is True + assert config.requires_api_key() is False + # Stand-ins, as with an empty catalog: code that reads a window still works. + tiers = config.tiers() + assert set(tiers) == {"brain", "worker", "validator"} + assert all(t.slug is None and t.credential is None for t in tiers.values()) + + def test_every_walk_over_the_catalog_skips_it(self, tmp_path: Path) -> None: + config = _config(tmp_path) + native = [name for name, p in config.presets.items() if p.has_tiers] + assert len(native) == len(config.presets) - 1 + assert targets(config, "grok", None) == [] + assert targets(config, "grok", "brain") == [] + assert len(list(every_tier(config))) >= 3 * len(native) + assert _snapshot_slugs(config)["grok"] == {} + assert set(_snapshot_slugs(config)["local"]) == {"brain", "worker", "validator"} + assert config.fallback_bindings("openrouter") == [] + + def test_the_engine_is_the_entrys_then_the_global_one(self, tmp_path: Path) -> None: + native_global = _config(tmp_path) + assert native_global.preset_engine("grok") == "grok" + assert native_global.preset_engine("local") == "native" + assert native_global.preset_engine("nope") == "native" + grok_global = _config(tmp_path, _text(engine_kind="grok")) + assert grok_global.preset_engine("local") == "grok" + pinned = _text( + _entry( + f" engine: native\n brain:\n{TIER} worker:\n{TIER} validator:\n{TIER}" + ), + engine_kind="grok", + ) + assert _config(tmp_path, pinned).preset_engine("odd") == "native" + + def test_work_no_chat_owns_runs_on_an_entry_that_has_a_model(self, tmp_path: Path) -> None: + config = _config(tmp_path) + assert config.native_default() == config.active_preset + on_grok = config.model_copy(update={"active_preset": "grok"}) + first_native = next(n for n, p in config.presets.items() if p.has_tiers) + assert on_grok.native_default() == first_native + only_grok = config.model_copy( + update={"presets": {"grok": config.presets["grok"]}, "active_preset": "grok"} + ) + assert only_grok.native_default() is None + + +class TestAChatKeepsItsEngine: + def test_which_switches_are_refused(self, tmp_path: Path) -> None: + body = f" engine: native\n brain:\n{TIER} worker:\n{TIER} validator:\n{TIER}" + config = _config(tmp_path, _text(GROK + _entry(body))) + # An entry that names no engine suits a chat on either, as before. + assert engine_refusal(config, "local", "native") is None + assert engine_refusal(config, "local", "grok") is None + assert engine_refusal(config, "grok", "grok") is None + assert engine_refusal(config, "odd", "native") is None + assert engine_refusal(config, "nope", "native") is None + to_grok = engine_refusal(config, "grok", "native") + assert to_grok is not None and "no model of its own" in to_grok + assert "Start a new chat" in to_grok + to_native = engine_refusal(config, "odd", "grok") + assert to_native is not None and "keeps the engine it started on" in to_native + + def test_only_the_native_loop_needs_the_entrys_model(self, tmp_path: Path) -> None: + config = _config(tmp_path) + assert no_model_refusal(config, "grok", "grok") is None + assert no_model_refusal(config, "local", "native") is None + assert no_model_refusal(config, "grok", "native") is not None + + +class TestScheduledPins: + """TD-3812 stores a preset and an engine. The entry's engine fills an empty one.""" + + @pytest.mark.parametrize( + ("pin", "grok_available", "expected_engine", "summary"), + [ + (ScheduledPin(preset="grok"), True, "grok", None), + (ScheduledPin(preset="grok"), False, "grok", "grok engine is unavailable"), + (ScheduledPin(preset="grok", engine="grok"), True, "grok", None), + ( + ScheduledPin(preset="grok", engine="native"), + True, + "native", + "preset 'grok' has no model of its own; it runs only on the grok engine", + ), + (ScheduledPin(preset="local"), False, None, None), + (ScheduledPin(preset="local", engine="grok"), True, "grok", None), + ( + ScheduledPin(preset="local", engine="grok"), + False, + "grok", + "grok engine is unavailable", + ), + (ScheduledPin(), False, None, None), + ( + ScheduledPin(preset="retired", engine="native"), + False, + "native", + "preset 'retired' no longer exists", + ), + ], + ) + def test_the_pin_table( + self, + tmp_path: Path, + pin: ScheduledPin, + grok_available: bool, + expected_engine: str | None, + summary: str | None, + ) -> None: + catalog = _config(tmp_path).presets + assert pinned_engine(pin, catalog) == expected_engine + assert pin_failure_summary(pin, catalog=catalog, grok_available=grok_available) == summary + + +class TestWriters: + """TD-5237's three writers on an entry with no roles.""" + + def test_copy_relabel_and_remove(self, tmp_path: Path) -> None: + path = tmp_path / "config.yaml" + path.write_text(_text(), encoding="utf-8") + duplicate_preset("grok", "grok-2", "Grok two", path) + config = load_config(path) + assert config.presets["grok-2"].engine == "grok" + assert config.presets["grok-2"].has_tiers is False + save_preset_label("grok", "Grok Build", path=path) + assert load_config(path).presets["grok"].label == "Grok Build" + delete_preset("grok", path) + delete_preset("grok-2", path) + # Both gone, and every other line as it was. + assert path.read_text(encoding="utf-8") == _text(extra="") + + +async def _grok_stub(session: Session, **_kwargs: Any) -> None: + """Stands in for the Grok loop: no CLI is started.""" + await asyncio.Event().wait() + + +def _mock() -> MockProvider: + return MockProvider(default=Script(kind="stream", content="done")) + + +async def _seeded( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, text: str | None = None +) -> tuple[Daemon, Any, Any, Path]: + monkeypatch.setattr("tstd.daemon.grok_loop", _grok_stub) + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_text() if text is None else text, encoding="utf-8") + workspace = tmp_path / "ws" + workspace.mkdir() + daemon, task, ws = await _start(data, _mock()) + return daemon, task, ws, workspace + + +class TestOverTheWire: + async def test_setup_state_marks_the_grok_entry_and_shows_no_roles_for_it( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _daemon, task, ws, _workspace = await _seeded(tmp_path, monkeypatch) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + assert before["preset_engines"] == {"grok": "grok"} + assert before["tier_slugs"] != {} + state = await _ask(ws, {"type": "set_preset", "name": "grok"}, "setup_state") + assert state["active_preset"] == "grok" + assert state["has_model"] is True + assert state["key_required"] is False + assert state["tier_slugs"] == {} + assert state["tier_cards"] == {} + assert state["tier_credentials"] == {} and state["tier_loopback"] == {} + # The Engine setting itself is not changed by picking an entry. + assert state["engine"] == "native" + finally: + await _stop(task, ws) + + async def test_a_chat_opened_on_a_grok_entry_is_a_grok_chat( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + native = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + assert native["engine"] == "native" + await _ask(ws, {"type": "set_preset", "name": "grok"}, "setup_state") + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + assert opened["engine"] == "grok" + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None and sess.engine == "grok" and sess.preset == "grok" + record = daemon._session_store.get(opened["session_id"]) + assert record is not None and record.engine == "grok" and record.preset == "grok" + # The first chat is untouched. + first = daemon.session_registry.get(native["session_id"]) + assert first is not None and first.engine == "native" + finally: + await _stop(task, ws) + + async def test_a_switch_across_engines_is_refused_and_changes_nothing( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sid = opened["session_id"] + sess = daemon.session_registry.get(sid) + assert sess is not None + snapshot, was = sess.config, sess.preset + ws_reply = await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "grok"}, + "session_list", + ) + assert ws_reply["type"] == "error" + assert ws_reply["code"] == "engine_mismatch" + assert "Start a new chat" in ws_reply["message"] + assert sess.preset == was and sess.config is snapshot and sess.engine == "native" + record = daemon._session_store.get(sid) + assert record is not None and record.preset == was + # A switch between two native entries still works. + ok = await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "local"}, + "session_list", + ) + assert ok["type"] == "session_list" and sess.preset == "local" + finally: + await _stop(task, ws) + + async def test_the_native_loop_is_never_started_on_an_entry_with_no_model( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + before = len(await daemon.session_registry.list_sessions()) + raw = await daemon._start_session(str(workspace), preset="grok", engine="native") + assert raw is not None + reply = json.loads(raw) + assert reply["type"] == "error" and reply["code"] == "engine_mismatch" + assert len(await daemon.session_registry.list_sessions()) == before + # A caller's engine still wins over an entry's when there is a model. + raw = await daemon._start_session(str(workspace), preset="local", engine="grok") + assert raw is not None and json.loads(raw)["engine"] == "grok" + finally: + await _stop(task, ws) + + async def test_the_doctor_skips_the_probe_for_an_active_grok_entry( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, _workspace = await _seeded(tmp_path, monkeypatch) + try: + await _ask(ws, {"type": "set_preset", "name": "grok"}, "setup_state") + rows = {row.name: row for row in await daemon._doctor_key_provider_rows()} + assert rows["api_key"].status == "skip" and rows["provider"].status == "skip" + assert "Grok CLI" in rows["provider"].detail + finally: + await _stop(task, ws) + + +class TestDistill: + """Distill runs on TST Desk's own client whatever engine the chat used.""" + + async def _run( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, text: str + ) -> list[tuple[str, str | None]]: + seen: list[tuple[str, str | None]] = [] + + async def fake_distill(session: Session, provider: Any, config: ModelConfig) -> None: + seen.append((config.active_preset, config.tier("worker").slug)) + return None + + monkeypatch.setattr("tstd.daemon.distill_if_due", fake_distill) + monkeypatch.setattr("tstd.daemon.completed_turn_count", lambda _s: 1) + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch, text) + try: + await _ask(ws, {"type": "set_preset", "name": "grok"}, "setup_state") + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None and sess.engine == "grok" + await daemon._run_distill(sess) + finally: + await _stop(task, ws) + return seen + + async def test_with_grok_active_it_uses_the_first_entry_that_has_a_model( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + seen = await self._run(tmp_path, monkeypatch, _text()) + config = _config(tmp_path) + first = next(n for n, p in config.presets.items() if p.has_tiers) + worker = config.presets[first].worker + assert worker is not None + # Once from this test, and once more when the daemon shuts down. + assert seen and set(seen) == {(first, worker.slug)} + + async def test_with_no_model_anywhere_it_is_skipped( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + from tstd.config import default_config_yaml + + only_grok = default_config_yaml().replace("\npresets: {}\n", "\npresets:\n" + GROK, 1) + assert "engine: grok" in only_grok + assert await self._run(tmp_path, monkeypatch, only_grok) == [] diff --git a/core/tstd/config.py b/core/tstd/config.py index 1fac55c6..4a8616e8 100644 --- a/core/tstd/config.py +++ b/core/tstd/config.py @@ -506,11 +506,19 @@ def _does_something(self) -> FastConfig: class Preset(BaseModel): - """A complete set of tier configurations.""" + """One entry of the model catalog: a model per role, or the Grok engine. - brain: TierConfig - worker: TierConfig - validator: TierConfig + A native entry names all three roles. An entry with ``engine: grok`` + runs chats on the Grok CLI, which brings its own model, so it may + leave the three roles out (TD-5239): all three, or none. + """ + + brain: TierConfig | None = None + worker: TierConfig | None = None + validator: TierConfig | None = None + # Which agent loop a chat on this entry uses (TD-5239). None = the + # global ``engine.kind``, what every entry meant before. + engine: Literal["native", "grok"] | None = None # How the composer's model picker shows this preset (TD-5172): the name # on the list and chip, a one-line ``description`` shown under it, and # where it sits: the main list (``primary``), "More models" (``more``), @@ -532,6 +540,40 @@ def _blank_description_is_unset(cls, value: str | None) -> str | None: return None return value.strip() or None + @model_validator(mode="after") + def _roles_and_engine_agree(self) -> Preset: + named = [name for name in TIER_NAMES if getattr(self, name) is not None] + if named and len(named) != len(TIER_NAMES): + missing = ", ".join(name for name in TIER_NAMES if name not in named) + raise ValueError(f"name all three roles or none of them; missing: {missing}") + if not named and self.engine != "grok": + raise ValueError( + "brain, worker and validator are required. Only an entry with " + "`engine: grok` may leave them out: the Grok CLI brings its own model" + ) + if self.engine == "grok": + # Both act on TST Desk's own loop, which a Grok chat does not run. + # Refused, not ignored: a setting that does nothing must not sit + # in the file looking as if it were in force. + for key in ("overrides", "fast"): + if getattr(self, key) is not None: + raise ValueError(f"`{key}` does nothing on an `engine: grok` entry; remove it") + return self + + @property + def has_tiers(self) -> bool: + """Whether this entry names a model per role (a Grok entry may not).""" + return self.brain is not None + + def tier_map(self) -> dict[TierName, TierConfig]: + """The entry's own roles; empty for an entry with none.""" + found: dict[TierName, TierConfig] = {} + for name in TIER_NAMES: + tier: TierConfig | None = getattr(self, name) + if tier is not None: + found[name] = tier + return found + class CredentialConfig(BaseModel): """Display name and optional host for one keychain-backed API key. @@ -1746,12 +1788,18 @@ def has_model(self) -> bool: """Whether any model is set up, i.e. the active preset exists.""" return self.active_preset in self.presets + def preset_engine(self, name: str) -> Literal["native", "grok"]: + """The engine a chat on entry *name* uses: its own, else ``engine.kind`` (TD-5239).""" + preset = self.presets.get(name) + if preset is not None and preset.engine is not None: + return preset.engine + return self.engine.kind + @model_validator(mode="after") def _bound_credentials_exist(self) -> ModelConfig: known = set(self.credentials) | {DEFAULT_CREDENTIAL_ID} for preset_name, preset in self.presets.items(): - for tier_name in TIER_NAMES: - tier = getattr(preset, tier_name) + for tier_name, tier in preset.tier_map().items(): bound = [(f"presets.{preset_name}.{tier_name}", tier.credential)] bound += [ (f"presets.{preset_name}.{tier_name}.fallbacks[{i}]", backup.credential) @@ -1771,8 +1819,8 @@ def fallback_bindings(self, credential_id: str) -> list[str]: return [ f"presets.{preset_name}.{tier_name}.fallbacks[{i}]" for preset_name, preset in self.presets.items() - for tier_name in TIER_NAMES - for i, backup in enumerate(getattr(preset, tier_name).fallbacks) + for tier_name, tier in preset.tier_map().items() + for i, backup in enumerate(tier.fallbacks) if backup.credential == credential_id ] @@ -1789,9 +1837,25 @@ def tiers(self) -> dict[TierName, TierConfig]: that only reads windows or prices keeps working on a first run. """ preset = self.presets.get(self.active_preset) - if preset is None: + # An ``engine: grok`` entry may name no model (TD-5239). It gets the + # same stand-ins: a Grok chat never asks TST Desk's router for one. + own = preset.tier_map() if preset is not None else {} + if not own: return {name: _no_model_tier() for name in TIER_NAMES} - return {name: preset.__getattribute__(name) for name in TIER_NAMES} + return own + + def native_default(self) -> str | None: + """The entry whose model does work no chat owns, or None (TD-5239). + + Memory distill and the doctor's probe run on TST Desk's own client + whatever engine a chat used. They take the active entry when it + names a model, else the first entry that does. None means there is + no such model, and that work is skipped. + """ + active = self.presets.get(self.active_preset) + if active is not None and active.has_tiers: + return self.active_preset + return next((name for name, entry in self.presets.items() if entry.has_tiers), None) def requires_api_key(self) -> bool: """Whether the active preset will send at least one key (TD-1801, TD-1717). diff --git a/core/tstd/config_editor_checks.py b/core/tstd/config_editor_checks.py index 8f26689b..88c19042 100644 --- a/core/tstd/config_editor_checks.py +++ b/core/tstd/config_editor_checks.py @@ -321,7 +321,9 @@ def _tier_at(config: ModelConfig, path: list[PathPart]) -> TierConfig | None: preset = config.presets.get(path[1]) if preset is None: return None - tier: TierConfig = getattr(preset, str(path[2])) + tier: TierConfig | None = getattr(preset, str(path[2])) + if tier is None: + return None if path[3:4] == ["fallbacks"] and len(path) == 5 and isinstance(path[4], int): return tier.fallbacks[path[4]] if path[4] < len(tier.fallbacks) else None return tier if len(path) == 3 else None diff --git a/core/tstd/config_help.py b/core/tstd/config_help.py index 1857b67c..fa9982f4 100644 --- a/core/tstd/config_help.py +++ b/core/tstd/config_help.py @@ -114,6 +114,8 @@ "Preset.brain": "Plans and talks to you. A session opens on it for its first turns.", "Preset.worker": "Runs the steps: edits, the tool-call classifier, compaction and summaries.", "Preset.validator": "Reviews diffs and tests, including the review after writes.", + "Preset.engine": "Which agent loop a chat on this one uses: TST Desk's own, or the Grok " + "CLI. Empty uses the Engine setting. With grok, the three roles may be left out.", "Preset.label": "The name on the model picker. A model with no label is left off the " "picker unless it is in use.", "Preset.description": "One line under the label on the picker: what this model is for.", @@ -252,8 +254,11 @@ "their keychain values.", } -#: Keys with no Reset: the picker sets them. -NOT_RESETTABLE: frozenset[str] = frozenset({"ModelConfig.active_preset"}) +#: Keys with no Reset. The picker sets the active model. A model's three +#: roles are all there or all absent (TD-5239), so one is never reset alone. +NOT_RESETTABLE: frozenset[str] = frozenset( + {"ModelConfig.active_preset", "Preset.brain", "Preset.worker", "Preset.validator"} +) #: Long text gets a multi-line box. MULTILINE_FIELDS: frozenset[str] = frozenset( diff --git a/core/tstd/daemon.py b/core/tstd/daemon.py index a3c75995..bb74e889 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -224,7 +224,12 @@ stricter_rule, ) from .port_file import release_port_file -from .preset_apply import effective_config, preset_overrides +from .preset_apply import ( + effective_config, + engine_refusal, + no_model_refusal, + preset_overrides, +) from .preset_write import delete_preset, duplicate_preset, save_preset_label from .protocol import ( AddModel, @@ -478,7 +483,7 @@ pairing_code_event, ) from .rewind_handlers import preview_restore, restore_files -from .router import TIER_NAMES, TierRouter +from .router import TierRouter from .scheduler.calendar_path import CalendarPathError, require_calendar_file from .scheduler.chain import require_chain from .scheduler.chat_source import SessionScheduleSource, session_schedule_source @@ -743,7 +748,7 @@ def _snapshot_slugs(config: ModelConfig) -> dict[str, dict[str, str | None]]: this cannot be derived from ``self.config`` later (TD-1703). """ return { - name: {tier: getattr(preset, tier).slug for tier in TIER_NAMES} + name: {tier: cfg.slug for tier, cfg in preset.tier_map().items()} for name, preset in config.presets.items() } @@ -1455,14 +1460,16 @@ async def rebuild() -> ProviderClient: self._clients[cache_key] = handle return handle - async def _ensure_provider(self) -> ProviderLike: + async def _ensure_provider(self, config: ModelConfig | None = None) -> ProviderLike: """Create the shared provider client on first use. Distill and doctor stay on the active brain. Session turns pass the effective tier into ``get_provider`` so a CU-heavy worker - can land on a different URL (TD-3903). + can land on a different URL (TD-3903). *config* is the catalog + pointed at another entry, for distill when the active one has no + model of its own (TD-5239). """ - return await self._client_for(self.config.tier("brain")) + return await self._client_for((config or self.config).tier("brain")) async def _setup_state_event(self) -> SetupState: """Current onboarding state (TD-1101): key presence + preset choice. @@ -1476,10 +1483,15 @@ async def _setup_state_event(self) -> SetupState: credentials = await self._credential_summaries() has_api_key = any(item.stored for item in credentials) # An empty catalog is a first run, not an error (TD-5172). - tiers = self.config.tiers() if self.config.has_model() else {} + # Nor is an active entry that is the Grok engine with no model of its + # own (TD-5239): Settings shows no roles for it, not the stand-ins. + active = self.config.presets.get(self.config.active_preset) + tiers = self.config.tiers() if active is not None and active.has_tiers else {} grok_path, grok_ok = _probe_grok(self.config.engine.binary) engine_kind = self.config.engine.kind - key_required = self.config.requires_api_key() and engine_kind != "grok" + # What a new chat would run on: the active entry's engine, else the global one. + new_chat_engine = self.config.preset_engine(self.config.active_preset) + key_required = self.config.requires_api_key() and new_chat_engine != "grok" excluded_by_server = {row.server_id: row.excluded for row in self._mcp.doctor_rows()} return SetupState( seq=1, @@ -1502,6 +1514,9 @@ async def _setup_state_event(self) -> SetupState: team_presets=sorted( name for name, p in self.config.presets.items() if p.picker == "team" ), + preset_engines={ + name: p.engine for name, p in sorted(self.config.presets.items()) if p.engine + }, has_model=self.config.has_model(), skip_all_approvals=self.skip_all_approvals, load_global_memory=self.load_global_memory, @@ -1920,8 +1935,7 @@ async def _delete_named_credential(self, credential_id: str) -> None: ) bound: list[tuple[str, str]] = [] for preset_name, preset in self.config.presets.items(): - for tier_name in ("brain", "worker", "validator"): - tier = getattr(preset, tier_name) + for tier_name, tier in preset.tier_map().items(): if tier.credential == cred_id: bound.append((preset_name, tier_name)) try: @@ -1934,8 +1948,8 @@ async def _delete_named_credential(self, credential_id: str) -> None: raise if cred_id in self.config.credentials: delete_credential_entry(cred_id, self.config_path) - for preset_name, tier_name in bound: - save_tier_credential(preset_name, tier_name, None, self.config_path) + for bound_preset, bound_tier in bound: + save_tier_credential(bound_preset, bound_tier, None, self.config_path) self._reload_user_config() def _cu_permissions_session(self, event: DaemonEvent) -> str | None: @@ -2189,7 +2203,16 @@ async def _test_model(self, preset: str | None) -> ModelTested: name = preset or self.config.active_preset if name not in self.config.presets: return ModelTested(preset=name, ok=False, detail="No model is set up yet.") - tier_cfg = apply_credential_host(self.config, self.config.presets[name].brain) + brain = self.config.presets[name].brain + if brain is None: + # TD-5239: a Grok entry with no model of its own. + return ModelTested( + preset=name, + ok=False, + detail="This one runs on the Grok CLI, which brings its own model. " + "There is no model here to test.", + ) + tier_cfg = apply_credential_host(self.config, brain) try: client = await self._client_for(tier_cfg) response = await client.chat_completion( @@ -2230,6 +2253,14 @@ async def _doctor_key_provider_rows(self) -> list[DiagnosticCheck]: transport failure means we never got far enough to judge the key. """ fix_key = "Re-enter a valid key: title-bar gear → Provider API key." + active = self.config.presets.get(self.config.active_preset) + if active is not None and not active.has_tiers: + # TD-5239: the Grok CLI brings its own model and sign-in. + why = "not checked: the active model runs on the Grok CLI" + return [ + DiagnosticCheck(name="api_key", status="skip", detail=why), + DiagnosticCheck(name="provider", status="skip", detail=why), + ] try: err = await self._provider_probe() except KeychainError as e: @@ -4902,10 +4933,24 @@ async def _run_distill( """Park a proposal and emit ``memory_proposal`` when memory changed.""" if session.turn_in_flight or completed_turn_count(session) < 1: return + # TD-5239: distill runs on TST Desk's own client whatever engine the + # chat used. With the Grok engine active and no model of its own, it + # takes the first entry that has one; with none, there is nothing to + # run it on and it is skipped. + native = self.config.native_default() + if native is None: + log.info( + "distill skipped; no model to run it on", + extra={"extra_fields": {"session_id": session.id}}, + ) + return + config = self.config + if native != config.active_preset: + config = config.model_copy(update={"active_preset": native}) try: - provider = await self._ensure_provider() + provider = await self._ensure_provider(config) distill_provider = provider if flight is None else SignalingProvider(provider, flight) - emitted = await distill_if_due(session, distill_provider, self.config) + emitted = await distill_if_due(session, distill_provider, config) except Exception as exc: log.warning( "distill skipped; provider failed", @@ -5123,6 +5168,15 @@ async def _start_session( the session uses the window's current choice. Neither is written to Settings. """ + # TD-5239: the entry names the engine unless the caller did. Checked + # before the session exists, so a refusal leaves nothing behind. + chosen = self._resolved_preset(self.config.active_preset if preset is None else preset) + chosen_engine = self.config.preset_engine(chosen) if engine is None else engine + # A caller's engine wins over the entry's (a scheduled pin, TD-3812), + # except that TST Desk's own loop cannot run an entry with no model. + refusal = no_model_refusal(self.config, chosen, chosen_engine) + if refusal is not None: + return build_error("engine_mismatch", refusal) # Memory templates (TD-2101): plant .tst/memory/ on every session # start so an already-opened workspace still gets the files. Never # overwrites; config scaffold stays OpenWorkspace-only. @@ -5143,8 +5197,8 @@ async def _start_session( await asyncio.to_thread(scaffold_workspace_memory, sess.workspace_path) # A scheduled pin names the loop for this session only. The window's # active preset and engine stay where Settings left them. - sess.engine = self.config.engine.kind if engine is None else engine - self._bind_session_preset(sess, self.config.active_preset if preset is None else preset) + sess.engine = chosen_engine + self._bind_session_preset(sess, chosen) await self._session_store.upsert( sess.id, workspace_path, @@ -5309,6 +5363,10 @@ async def _handle_set_session_preset(self, msg: SetSessionPreset) -> str: "unknown_preset", f"Unknown preset {msg.name!r}; declared: {', '.join(sorted(self.config.presets))}", ) + # TD-5239: a chat keeps the engine it started on. + refusal = engine_refusal(self.config, msg.name, found.engine) + if refusal is not None: + return build_error("engine_mismatch", refusal, session_id=found.id) self._bind_session_preset(found, msg.name) # TD-5234: one click sets everything the model defines. An effort or # Ultracode default the entry itself names applies to this chat now; @@ -6687,7 +6745,7 @@ async def _notify(message: str) -> None: await notify_autonomy_stop(self.config, message) sess.autonomy_notify = _notify - sess.engine = self.config.engine.kind + sess.engine = self.config.preset_engine(self.config.active_preset) self._bind_session_preset(sess, self.config.active_preset) await self._session_store.upsert( sess.id, str(workspace), sess.state, engine=sess.engine, preset=sess.preset diff --git a/core/tstd/key_hosts.py b/core/tstd/key_hosts.py index d74f396e..ecdb8d1b 100644 --- a/core/tstd/key_hosts.py +++ b/core/tstd/key_hosts.py @@ -22,7 +22,6 @@ from .config import ( CREDENTIAL_ID_RE, RESERVED_CREDENTIAL_IDS, - TIER_NAMES, ModelConfig, TierConfig, apply_credential_host, @@ -66,8 +65,7 @@ def unknown_key(config: ModelConfig, credential_id: str) -> str | None: def every_tier(config: ModelConfig) -> Iterator[TierConfig]: """Every tier of every preset, then each one's backups (TD-5120).""" for preset in config.presets.values(): - for name in TIER_NAMES: - tier: TierConfig = getattr(preset, name) + for tier in preset.tier_map().values(): yield tier yield from tier.fallbacks diff --git a/core/tstd/model_cards.py b/core/tstd/model_cards.py index 327ad5eb..64e1581f 100644 --- a/core/tstd/model_cards.py +++ b/core/tstd/model_cards.py @@ -145,8 +145,12 @@ def targets(config: ModelConfig, preset: str, tier: str | None = None) -> list[C raise ConfigError(f"Unknown tier {tier!r}; tiers are: {', '.join(TIER_NAMES)}") names = [tier] if tier is not None else list(TIER_NAMES) found: list[CardTarget] = [] + # An ``engine: grok`` entry may name no model (TD-5239): nothing to refresh. + own = entry.tier_map() for name in names: - tier_cfg: TierConfig = getattr(entry, name) + if name not in own: + continue + tier_cfg: TierConfig = own[name] found.append(CardTarget(preset, name, None, tier_cfg)) found.extend( CardTarget(preset, name, index, backup) diff --git a/core/tstd/preset_apply.py b/core/tstd/preset_apply.py index 76712c8a..bce05e57 100644 --- a/core/tstd/preset_apply.py +++ b/core/tstd/preset_apply.py @@ -30,3 +30,39 @@ def effective_config(config: ModelConfig, name: str) -> ModelConfig: update: dict[str, object] = {"active_preset": name} update.update(merged_sections(config, preset_overrides(config, name))) return config.model_copy(update=update) + + +def no_model_refusal(config: ModelConfig, name: str, engine: str) -> str | None: + """Why TST Desk's own loop cannot run on entry *name*, or None (TD-5239). + + The loop needs the entry's models, and an ``engine: grok`` entry may + have none. The Grok engine needs nothing from the entry. + """ + preset = config.presets.get(name) + if preset is None or preset.has_tiers or engine != "native": + return None + return ( + f"{preset.label or name} runs on the Grok CLI and has no model of its own, so a " + "chat on TST Desk's own loop cannot use it. Start a new chat to use it." + ) + + +def engine_refusal(config: ModelConfig, name: str, engine: str) -> str | None: + """Why an open chat on *engine* cannot switch to entry *name*, or None (TD-5239). + + A chat keeps the engine it started on, so an entry that names another + engine is refused, and so is one with no model for TST Desk's own loop. + An entry that names no engine suits either, as before. + """ + missing = no_model_refusal(config, name, engine) + if missing is not None: + return missing + preset = config.presets.get(name) + if preset is None or preset.engine is None or preset.engine == engine: + return None + ours = "the Grok CLI" if engine == "grok" else "TST Desk's own loop" + theirs = "the Grok CLI" if preset.engine == "grok" else "TST Desk's own loop" + return ( + f"This chat runs on {ours}, and {preset.label or name} runs on {theirs}. A chat " + "keeps the engine it started on. Start a new chat to use it." + ) diff --git a/core/tstd/protocol.py b/core/tstd/protocol.py index 84399ebb..c1ca7c4f 100644 --- a/core/tstd/protocol.py +++ b/core/tstd/protocol.py @@ -3744,6 +3744,9 @@ class SetupState(DaemonEvent): # wizard opens on "Add a model". Additive; an older client ignores them. preset_descriptions: dict[str, str] = Field(default_factory=dict) team_presets: list[str] = Field(default_factory=list) + # TD-5239: the engine of each preset that names one. A preset left out + # runs on ``engine``. Additive; an older client shows no mark. + preset_engines: dict[str, Literal["native", "grok"]] = Field(default_factory=dict) has_model: bool = True # TD-804: skip-all approvals. Additive, default off — an older client # that ignores the field keeps asking, which is the safe read. diff --git a/core/tstd/scheduler/pin.py b/core/tstd/scheduler/pin.py index e0af0db9..553aaa2b 100644 --- a/core/tstd/scheduler/pin.py +++ b/core/tstd/scheduler/pin.py @@ -125,11 +125,38 @@ def pin_failure_summary( """ if pin.preset is not None and pin.preset not in catalog: return f"preset '{pin.preset}' no longer exists" - if pin.engine == "grok" and not grok_available: + if pin.engine == "native" and not pinned_entry_has_model(pin, catalog): + # TD-5239: an entry may be the Grok engine with no model of its own. + return NO_MODEL_SUMMARY.format(preset=pin.preset) + if pinned_engine(pin, catalog) == "grok" and not grok_available: return "grok engine is unavailable" return None +#: The receipt for a job pinned to TST Desk's own loop on an entry that has no model. +NO_MODEL_SUMMARY = "preset '{preset}' has no model of its own; it runs only on the grok engine" + + +def pinned_engine(pin: ScheduledPin, catalog: Mapping[str, object]) -> EngineKind | None: + """The engine this pin asks for: its own, else its entry's, else None (TD-5239). + + None means neither names one, and the session takes the window's. + """ + if pin.engine is not None: + return pin.engine + if pin.preset is None: + return None + named = getattr(catalog.get(pin.preset), "engine", None) + return named if named in ("native", "grok") else None + + +def pinned_entry_has_model(pin: ScheduledPin, catalog: Mapping[str, object]) -> bool: + """False only for a pinned entry that names no model per role (TD-5239).""" + if pin.preset is None: + return True + return bool(getattr(catalog.get(pin.preset), "has_tiers", True)) + + def grok_cli_available(binary: str) -> bool: """Whether the pinned Grok engine can start. The path stays out of the receipt.""" from ..grok_acp import GrokEngineError, find_grok_binary @@ -144,11 +171,12 @@ def grok_cli_available(binary: str) -> bool: async def scheduled_run_block(catalog: Mapping[str, object], *, grok_binary: str) -> str | None: """The receipt for a pin that cannot start, or None to open the session. - The Grok probe runs only when the job pinned Grok. A window that is - on Grok while the job pinned native is not probed. + The Grok probe runs only when the job pinned Grok, itself or through + an entry whose engine is Grok (TD-5239). A window that is on Grok while + the job pinned native is not probed. """ pin = current_scheduled_pin() grok_ok = True - if pin.engine == "grok": + if pinned_engine(pin, catalog) == "grok": grok_ok = await asyncio.to_thread(grok_cli_available, grok_binary) return pin_failure_summary(pin, catalog=catalog, grok_available=grok_ok) diff --git a/core/tstd/scheduler/runner.py b/core/tstd/scheduler/runner.py index da56191a..f5a63317 100644 --- a/core/tstd/scheduler/runner.py +++ b/core/tstd/scheduler/runner.py @@ -688,4 +688,6 @@ def _pin_block_code(summary: str) -> str: """Stable code for a pin that cannot start. The summary stays the receipt.""" if summary == "grok engine is unavailable": return "engine_unavailable" + if summary.endswith("it runs only on the grok engine"): + return "engine_mismatch" return "preset_missing" diff --git a/core/tstd/tools/delegate_named.py b/core/tstd/tools/delegate_named.py index 897f158a..3865d8ee 100644 --- a/core/tstd/tools/delegate_named.py +++ b/core/tstd/tools/delegate_named.py @@ -73,7 +73,14 @@ def worker_tier(config: ModelConfig, worker: WorkerDefinition) -> tuple[TierName f"Error: worker {worker.name!r} names preset {worker.preset!r}, which is not " f"configured. Presets: {', '.join(sorted(config.presets))}.", ) - tier_cfg: TierConfig = getattr(preset, worker.tier) + tier_cfg: TierConfig | None = getattr(preset, worker.tier) + if tier_cfg is None: + # TD-5239: an ``engine: grok`` entry with no model of its own. + raise HandlerRefusal( + "delegate_agent_preset", + f"Error: worker {worker.name!r} names preset {worker.preset!r}, which runs on " + "the Grok CLI and has no model for a worker. Name a preset that has one.", + ) return worker.tier, tier_cfg diff --git a/docs/architecture.md b/docs/architecture.md index e474897d..3497492e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -604,7 +604,7 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `set_fast` | yes | Turn the model's Fast mode on or off (TD-5176). Only a preset with a `fast` block offers it. A local model's `fast.hook_url` (loopback) is called with `{model, on, by}` and may refuse (409 with `error`); an API model's `fast.extra_body` rides on requests while it is on. Acked with `tier_state` (`fast`, `fast_available`, `fast_note`); not kept across a restart. | | `set_ultracode` | yes | Turn the session's Ultracode switch on or off (TD-5195). Works with any effort. While it is on, the model may call the `workflow` tool and each turn's user message carries a note saying so. Allowed mid-turn: the note follows from the next turn, while the `workflow` tool follows the switch at once (turned off, the next call is refused). Acked with `tier_state` (`ultracode`). Kept across a restart in the session row's `effort` string (`max+ultracode`), not a key of its own. Default `session.default_ultracode` (`false`). | | `set_plan` | yes | Turn plan mode on or off. Refused `set_tier` to worker/validator while on (TD-4603). Plan mode is read-only (TD-5123): the classifier refuses, with `error_code` `plan_mode`, every call that could change something (`fs_write`, `fs_edit`, `shell` outside the read-only list, MCP tools not in their server's `read_only_tools`, plugin tools, actuation, GitHub writes, `propose_steering_edit`, `delegate` with `kind: worker`, and any other builtin not on `PLAN_READ_ONLY_BUILTINS`, except a `shell` command on the read-only list, the screenshot tools and `delegate` without `kind: worker`, which `plan_mode_allows` lets through first; TD-5161), ahead of policy rules and skip-all, and again after an approval a call waited on; `delegate` with no `kind` runs the research worker (TD-5117). A `workflow` call with any `kind: worker` task is refused whole the same way; a call of only research tasks runs (TD-5195). The model ends it with `exit_plan_mode`. A restart keeps it: the session row's `plan_mode` is read back on revive. | -| `set_session_preset` | yes | Retarget this session at a catalog preset. Refused mid-turn. Does not write Settings. Acked with `session_list` (TD-1721). | +| `set_session_preset` | yes | Retarget this session at a catalog preset. Refused mid-turn. Refused with `engine_mismatch` when the preset names another engine than the session's, or has no tiers and the session runs TST Desk's own loop: a session keeps the engine it started on (TD-5239). Does not write Settings. Acked with `session_list` (TD-1721). | | `get_instruction_stack` | yes | Ask for the resolved steering stack, token counts, and skills (TD-4502). | | `get_hooks` | yes | Ask for the session's active hooks (user config, and the workspace's once trusted) with each one's last run (TD-5134). Answered with `hooks_state`. | | `list_instructions` | — | List a workspace's Instructions files (`AGENTS.md` / `CLAUDE.md` fallback, then `.tst/rules/*`). Human path (TD-2802). | diff --git a/docs/configuration.md b/docs/configuration.md index 9fcc4eb8..6857142c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1969,13 +1969,16 @@ Hold-to-talk dictation (TD-4701). The Settings toggle is off by default and live ### A preset -Every preset declares all three tiers. Omitting one is a load error naming it. +Every preset declares all three tiers. Omitting one is a load error naming it. The one +exception is a preset with `engine: grok` (TD-5239): the Grok CLI brings its own model, so +it may leave all three out. It is all three or none. | Key | Type | Effect | |---|---|---| | `brain` | tier | Planning turns. The router opens every session here for the first two turns, and escalates back to it after three consecutive worker failures. | | `worker` | tier | Token-heavy execution — edits, and the tool-call classifier. Where turns land once the lead turns are spent. | | `validator` | tier | Reviewing diffs and tests. Interactive mode may call it once after a write turn (`autonomy.verify`, TD-4204). Autonomy supervision is a later story. | +| `engine` | `native` \| `grok` | Which agent loop a chat on this preset uses (TD-5239): TST Desk's own three-tier loop, or the installed Grok CLI. Default: none, which means the `engine.kind` setting, as before. A chat takes its engine when it is created and keeps it: switching an open chat to a preset that names the other engine is refused with `engine_mismatch`, and so is a chat on TST Desk's own loop switching to a preset with no tiers. A scheduled job's own `engine` pin wins over the preset's; a job that pins none takes the preset's. With `grok`, `overrides` and `fast` are a load error: both act on TST Desk's own loop. While the active preset has no tiers, memory distill runs on the first preset that has them, and is skipped when none does. | | `label` | string | The name the composer's model picker, its chip and the account row show for this preset. A preset with no label is left out of the picker (unless it is the current one). Default: none. | | `description` | string, up to 120 characters | One line under the label in the picker: what this model is for. Default: none. | | `picker` | `primary` \| `more` \| `team` | `primary` puts the preset on the picker's main list; `more` puts it under "More models"; `team` (a preset whose tiers use different models) puts it under "Teams", a submenu shown only when one exists. Default `primary`. | diff --git a/docs/models.md b/docs/models.md index 0c178d1e..a07b9af0 100644 --- a/docs/models.md +++ b/docs/models.md @@ -197,6 +197,28 @@ presets: validator: *local_model ``` +## Grok as one of your models + +If you use the Grok CLI, it can sit on the picker beside your models. Its +entry needs only a label and the engine, because the Grok CLI brings its +own model and its own sign-in: + + +```yaml +presets: + grok: + label: Grok + description: The Grok CLI, with its own model + engine: grok +``` + +A new chat started while this entry is the active one runs on the Grok +CLI. A chat keeps the engine it started on, so the picker holds this entry +for a chat that is already running on a model of yours, and holds your +models for a chat that is running on Grok: start a new chat to move +between them. Any other entry may name `engine: native` or `engine: grok` +too; one that names neither follows Settings → Engine, as before. + ## Teams Every turn has three roles: diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index b4840b37..d24a5326 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13441,3 +13441,77 @@ moves it, and an entry already carries that. Class B entry in DECISIONS.md. TD-5239 adds the engine to the same resolver, and refuses `overrides` on an entry with no model of its own. +--- + +### TD-5239 — A model can name its engine, and a Grok one needs no model of its own + +**Size:** 5 · **Depends on:** TD-5234, TD-3812 · **Status:** Done +**Approach signed off:** 2026-11-20 (owner, relayed by the coordinating session). + +Local, API and Grok are meant to be three entries on one picker. The engine +was a separate, global switch in Settings → Engine, and a catalog entry had +to name a model for each of three roles, which the Grok CLI does not use. + +- [x] An entry may name `engine: native` or `engine: grok`; one that names + neither follows `engine.kind`, and a catalog from before loads as it did + (`test_preset_engine.py::TestWhatLoads`, + `test_the_engine_is_the_entrys_then_the_global_one`). +- [x] The three roles are all there or all absent, and absent only with + `engine: grok`; `overrides` and `fast` on a Grok entry are a load + error (`test_what_is_refused`). +- [x] Everything that walks the catalog's roles skips an entry with none: + key checks, backups, model cards, the slug snapshot; an active one + gives the stand-in tiers and needs no key + (`TestWhatTheRestSees`). +- [x] A chat takes its engine when it is created: the caller's, else the + entry's, else the global one; TST Desk's own loop is never started on + an entry with no model (`test_a_chat_opened_on_a_grok_entry_is_a_grok_chat`, + `test_the_native_loop_is_never_started_on_an_entry_with_no_model`). +- [x] Switching an open chat across engines is refused with + `engine_mismatch` and changes nothing + (`test_a_switch_across_engines_is_refused_and_changes_nothing`, + `TestAChatKeepsItsEngine`). +- [x] A scheduled job's own engine pin wins; an empty one takes the pinned + entry's; a job pinned to the native loop on an entry with no model + gets a receipt (`TestScheduledPins`). +- [x] Memory distill runs on the active entry when it has a model, else the + first entry that has one, and is skipped when none does; the doctor + skips its probe for an active entry with none (`TestDistill`, + `test_the_doctor_skips_the_probe_for_an_active_grok_entry`). +- [x] `setup_state.preset_engines` names the engine of each entry that has + one; with an active entry that has no roles, no roles are sent + (`test_setup_state_marks_the_grok_entry_and_shows_no_roles_for_it`). +- [x] The picker marks a Grok entry and holds an entry on another engine + than the chat's, with the reason; Settings → Model shows no roles for + a Grok entry that has none (`model-engine.test.ts`). +- [x] The copy, relabel and remove writers (TD-5237) work on an entry with + no roles (`TestWriters`). + +**Notes:** No new client message. Starting a new chat on an entry from the +picker, in one click, is TD-5235. Making a Grok entry without editing +`config.yaml` is TD-5258. Class B entry in DECISIONS.md. + +--- + +### TD-5259 — Memory distill follows the chat's own model + +**Size:** 2 · **Depends on:** TD-5239 · **Status:** Not started (idea; do not build without the maintainer's say-so) + +Distill at session end and at shutdown runs on the daemon's active entry +(`_run_distill` hands it the daemon's config, and `_ensure_provider` its +brain), not on the entry the chat itself used. A chat on a local model can +so have its conversation sent to a hosted one for distilling, or the other +way round. TD-5239 kept this as it was and only made it work when the +active entry has no model. + +**Acceptance criteria:** +- [ ] Distill for a chat on TST Desk's own loop runs on that chat's entry +- [ ] A chat on the Grok engine, which has no model of its own here, keeps + the TD-5239 rule (the active entry, else the first with a model) +- [ ] A chat whose entry has since been removed falls back the same way +- [ ] Tests for each case with the mock provider, asserting the endpoint asked + +**Notes:** Decide first whether "the chat's entry" means its current one +or the one most of its turns ran on. Recorded 2026-11-20 at the owner's +request as a possible later story. + diff --git a/ui/src/lib/components/SettingsModels.svelte b/ui/src/lib/components/SettingsModels.svelte index 5088b251..a89cd7e0 100644 --- a/ui/src/lib/components/SettingsModels.svelte +++ b/ui/src/lib/components/SettingsModels.svelte @@ -28,6 +28,15 @@ // team": most people run one model in all three roles and never open it. const TIERS = ["brain", "worker", "validator"] as const; + // TD-5239: a model that is the Grok engine may have no roles. The daemon + // then sends none, and there is nothing for "Build a team" to edit. + const noRoles = $derived( + settings.hasModel && + settings.activePreset !== null && + settings.presetEngines[settings.activePreset] === "grok" && + Object.keys(settings.tierSlugs).length === 0, + ); + // Both are modal: close Settings so the walkthrough is on top. function addModel(): void { closeSettings(); @@ -89,7 +98,12 @@ -{#if settings.hasModel && settings.activePreset} +{#if noRoles} +

+ {modelLabel(settings.activePreset ?? "", settings.presetLabels)} runs on the Grok CLI, which + brings its own model, so there are no roles to set here. +

+{:else if settings.hasModel && settings.activePreset}

Build a team

Every turn has three roles. One model can fill all of them, and that is the normal setup. A diff --git a/ui/src/lib/components/chat/ModelControls.svelte b/ui/src/lib/components/chat/ModelControls.svelte index 3d9bcc51..7753d215 100644 --- a/ui/src/lib/components/chat/ModelControls.svelte +++ b/ui/src/lib/components/chat/ModelControls.svelte @@ -7,9 +7,14 @@ import { grok, setGrokMode } from "../../grok.svelte.js"; import { session, setPlan } from "../../session-status.svelte.js"; import { settings } from "../../settings.svelte.js"; + import { newChatEngine } from "../../model-engine"; import ModelPicker from "./ModelPicker.svelte"; - const grokEngine = $derived((session.engine ?? settings.engine) === "grok"); + // Before a chat reports its engine: the one a new chat would get (TD-5239). + const grokEngine = $derived( + (session.engine ?? + newChatEngine(settings.activePreset, settings.presetEngines, settings.engine)) === "grok", + ); const PLAN_HINT = "Plan mode: the agent looks around without changing anything, then shows you a plan. Nothing runs until you approve it."; diff --git a/ui/src/lib/components/chat/ModelPicker.svelte b/ui/src/lib/components/chat/ModelPicker.svelte index f875f6b8..d97f860a 100644 --- a/ui/src/lib/components/chat/ModelPicker.svelte +++ b/ui/src/lib/components/chat/ModelPicker.svelte @@ -22,6 +22,7 @@ modelLabel, } from "../../effort"; import { MODELS_DOC_URL, openDoc } from "../../docs-links"; + import { engineClash, engineMark } from "../../model-engine"; import { openAddModel } from "../../onboarding.svelte.js"; import { session, @@ -48,6 +49,16 @@ ); const locked = $derived(session.turnActive); + const BUSY = "Cannot change model while a turn is running"; + + // TD-5239: a model on another engine than this chat's cannot be switched to. + function blocked(name: string): string { + if (name === current) return ""; + const clash = engineClash(name, settings.presetEngines, session.engine); + if (clash !== null) return clash; + return locked ? BUSY : ""; + } + function close(): void { open = false; view = "main"; @@ -122,12 +133,15 @@ type="button" role="menuitemradio" aria-checked={name === current} - disabled={locked && name !== current} - title={locked && name !== current ? "Cannot change model while a turn is running" : ""} + disabled={blocked(name) !== ""} + title={blocked(name)} onclick={() => pickModel(name)} > - {modelLabel(name, settings.presetLabels)} + {modelLabel(name, settings.presetLabels)}{#if engineMark(name, settings.presetEngines)} + {engineMark(name, settings.presetEngines)}{/if} {#if modelDescription(name, settings.presetDescriptions)} {modelDescription(name, settings.presetDescriptions)} {/if} @@ -233,11 +247,15 @@ type="button" role="menuitemradio" aria-checked={name === current} - disabled={locked && name !== current} + disabled={blocked(name) !== ""} + title={blocked(name)} onclick={() => pickModel(name)} > - {modelLabel(name, settings.presetLabels)} + {modelLabel(name, settings.presetLabels)}{#if engineMark(name, settings.presetEngines)} + {engineMark(name, settings.presetEngines)}{/if} {#if modelDescription(name, settings.presetDescriptions)} {modelDescription(name, settings.presetDescriptions)} {/if} diff --git a/ui/src/lib/model-engine.test.ts b/ui/src/lib/model-engine.test.ts new file mode 100644 index 00000000..dcf82880 --- /dev/null +++ b/ui/src/lib/model-engine.test.ts @@ -0,0 +1,115 @@ +// @vitest-environment jsdom +// +// TD-5239: a model can name its engine, and a chat keeps the engine it +// started on. The picker marks a Grok model and holds a switch across +// engines; Settings shows no roles for a Grok model that has none. + +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { flushSync, mount, tick, unmount } from "svelte"; + +import ModelPicker from "./components/chat/ModelPicker.svelte"; +import { engineClash, engineMark, newChatEngine } from "./model-engine"; +import { resetSession, session } from "./session-status.svelte.js"; +import { resetSettings, settings } from "./settings.svelte.js"; + +let app: ReturnType | null = null; + +beforeEach(() => { + resetSession(); + resetSettings(); +}); + +afterEach(async () => { + if (app !== null) { + await unmount(app); + app = null; + } + document.body.replaceChildren(); + resetSession(); + resetSettings(); +}); + +describe("engineMark", () => { + it("marks only a model that names the Grok engine", () => { + const engines = { grok: "grok", api: "native" } as const; + expect(engineMark("grok", engines)).toBe("Grok"); + expect(engineMark("api", engines)).toBe(""); + expect(engineMark("local", engines)).toBe(""); + }); +}); + +describe("newChatEngine", () => { + it("is the active model's engine, else the Engine setting", () => { + expect(newChatEngine("grok", { grok: "grok" }, "native")).toBe("grok"); + expect(newChatEngine("local", { grok: "grok" }, "native")).toBe("native"); + expect(newChatEngine("local", {}, "grok")).toBe("grok"); + expect(newChatEngine(null, { grok: "grok" }, "native")).toBe("native"); + }); +}); + +describe("engineClash", () => { + const engines = { grok: "grok", api: "native" } as const; + + it("holds a model on another engine than the chat's", () => { + expect(engineClash("grok", engines, "native")).toContain("start a new chat"); + expect(engineClash("api", engines, "grok")).toContain("start a new chat"); + }); + + it("lets through the same engine, a model that names none, and a chat not yet known", () => { + expect(engineClash("grok", engines, "grok")).toBeNull(); + expect(engineClash("api", engines, "native")).toBeNull(); + expect(engineClash("local", engines, "native")).toBeNull(); + expect(engineClash("local", engines, "grok")).toBeNull(); + expect(engineClash("grok", engines, null)).toBeNull(); + }); +}); + +describe("the model picker", () => { + async function openPicker(): Promise { + session.sessionId = "s1"; + session.preset = "local"; + session.engine = "native"; + settings.hasModel = true; + settings.presets = ["api", "grok", "local"]; + settings.presetLabels = { api: "API", grok: "Grok Build", local: "Local" }; + settings.presetEngines = { grok: "grok" }; + app = mount(ModelPicker, { target: document.body }); + await tick(); + document.querySelector('[aria-label="Model and effort"]')!.click(); + flushSync(); + return document.querySelector('[role="menu"]')!; + } + + function row(menu: HTMLElement, label: string): HTMLButtonElement { + const found = [...menu.querySelectorAll('[role="menuitemradio"]')].find( + (button) => button.querySelector(".label")?.textContent?.includes(label), + ); + if (found === undefined) throw new Error(`no row for ${label}`); + return found; + } + + it("marks the Grok model and no other", async () => { + const menu = await openPicker(); + expect(row(menu, "Grok Build").querySelector(".badge")?.textContent).toBe("Grok"); + expect(row(menu, "API").querySelector(".badge")).toBeNull(); + expect(row(menu, "Local").querySelector(".badge")).toBeNull(); + }); + + it("holds the Grok model for a chat on TST Desk's own loop, and says why", async () => { + const menu = await openPicker(); + const grok = row(menu, "Grok Build"); + expect(grok.disabled).toBe(true); + expect(grok.title).toContain("start a new chat"); + expect(row(menu, "API").disabled).toBe(false); + expect(row(menu, "API").title).toBe(""); + }); + + it("still holds every other model while a turn runs", async () => { + const menu = await openPicker(); + session.turnActive = true; + flushSync(); + expect(row(menu, "API").disabled).toBe(true); + expect(row(menu, "API").title).toBe("Cannot change model while a turn is running"); + expect(row(menu, "Local").disabled).toBe(false); + }); +}); diff --git a/ui/src/lib/model-engine.ts b/ui/src/lib/model-engine.ts new file mode 100644 index 00000000..b083e01e --- /dev/null +++ b/ui/src/lib/model-engine.ts @@ -0,0 +1,37 @@ +// TD-5239: a model in the catalog can name its engine, and a chat keeps the +// engine it started on. The daemon sends which models name one +// (`setup_state.preset_engines`) and each chat's engine; these say what the +// picker shows for them. The daemon still refuses a switch across engines; +// the picker only says so before the click. + +export type Engine = "native" | "grok"; + +export type PresetEngines = Readonly>; + +/** The mark on a model's row: "Grok" for one that runs on the Grok CLI. */ +export function engineMark(preset: string, engines: PresetEngines): string { + return engines[preset] === "grok" ? "Grok" : ""; +} + +/** The engine a new chat on `active` would run on: its own, else the setting. */ +export function newChatEngine( + active: string | null, + engines: PresetEngines, + setting: Engine, +): Engine { + return (active !== null ? engines[active] : undefined) ?? setting; +} + +/** Why the chat cannot switch to `preset`, or null. A model that names no + * engine suits a chat on either. */ +export function engineClash( + preset: string, + engines: PresetEngines, + chatEngine: Engine | null, +): string | null { + const wanted = engines[preset]; + if (wanted === undefined || chatEngine === null || wanted === chatEngine) return null; + return wanted === "grok" + ? "Runs on the Grok CLI. A chat keeps the engine it started on, so start a new chat to use it." + : "Runs on TST Desk's own loop. This chat is on the Grok CLI, so start a new chat to use it."; +} diff --git a/ui/src/lib/protocol-fixtures.json b/ui/src/lib/protocol-fixtures.json index 011bd751..1e35560f 100644 --- a/ui/src/lib/protocol-fixtures.json +++ b/ui/src/lib/protocol-fixtures.json @@ -2197,6 +2197,7 @@ "team_presets": [ "budget" ], + "preset_engines": {}, "has_model": true, "skip_all_approvals": false, "load_global_memory": false, diff --git a/ui/src/lib/protocol.ts b/ui/src/lib/protocol.ts index 4eba8135..fbb83611 100644 --- a/ui/src/lib/protocol.ts +++ b/ui/src/lib/protocol.ts @@ -2265,6 +2265,8 @@ export interface SetupState extends DaemonEvent { // (false on a fresh install: the UI starts "Add a model"). preset_descriptions?: Record; team_presets?: string[]; + /** TD-5239: the engine of each preset that names one. */ + preset_engines?: Record; has_model?: boolean; // TD-804: skip-all approvals. Additive, default off. skip_all_approvals?: boolean; diff --git a/ui/src/lib/settings.svelte.ts b/ui/src/lib/settings.svelte.ts index 37173494..60b32b85 100644 --- a/ui/src/lib/settings.svelte.ts +++ b/ui/src/lib/settings.svelte.ts @@ -96,6 +96,8 @@ export const settings = $state({ presetDescriptions: {} as Record, /** Presets the picker lists under "Teams" (config `picker: team`). */ teamPresets: [] as string[], + // TD-5239: the engine of each model that names one. + presetEngines: {} as Record, /** False until a model is set up: the first run starts "Add a model". */ hasModel: true, activePreset: null as string | null, @@ -232,6 +234,7 @@ export function resetSettings(): void { settings.morePresets = []; settings.presetDescriptions = {}; settings.teamPresets = []; + settings.presetEngines = {}; settings.hasModel = true; settings.activePreset = null; settings.tierSlugs = {}; @@ -411,6 +414,7 @@ function reduce(event: DaemonEventUnion): void { settings.morePresets = event.more_presets ?? []; settings.presetDescriptions = event.preset_descriptions ?? {}; settings.teamPresets = event.team_presets ?? []; + settings.presetEngines = event.preset_engines ?? {}; settings.hasModel = event.has_model ?? true; settings.activePreset = event.active_preset; // A daemon too old to send the field leaves the model section empty From 1d5602171e8ae6eaafbdecbb24eaf3aa956a5ebd Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:02:32 +0000 Subject: [PATCH 05/35] =?UTF-8?q?TD-5258:=20add=20Grok=20to=20your=20model?= =?UTF-8?q?s=20from=20Settings=20=E2=86=92=20Engine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After TD-5239 a Grok entry could sit on the picker, but only someone who edited config.yaml could put it there. add_grok_preset {label?} writes one catalog entry that is a label, a fixed description and engine: grok, after the last entry or into the empty shipped catalog. The daemon refuses it with grok_unavailable when the Grok CLI is not found, and when an entry already names the Grok engine. active_preset is not written. Acked with setup_state. Settings → Engine has "Add Grok to your models" (GrokOnPicker.svelte). It is always rendered and held with the reason where the CLI is missing, so the UI walk's census is the same with or without Grok installed; the walk lists it and does not press it. Class B: recorded in DECISIONS.md. Co-Authored-By: Claude Fable 5.2 --- DECISIONS.md | 22 +++ core/scripts/generate_protocol_fixtures.py | 3 + core/tests/test_add_grok_preset.py | 164 ++++++++++++++++++++ core/tstd/daemon.py | 31 +++- core/tstd/preset_write.py | 54 ++++++- core/tstd/protocol.py | 16 ++ docs/architecture.md | 1 + docs/models.md | 3 + docs/tst-desk-backlog.md | 30 ++++ ui/e2e/app-walk.spec.ts | 9 +- ui/e2e/census.json | 1 + ui/src/lib/components/GrokOnPicker.svelte | 104 +++++++++++++ ui/src/lib/components/SettingsEngine.svelte | 3 + ui/src/lib/grok-on-picker.test.ts | 122 +++++++++++++++ ui/src/lib/protocol-fixtures.json | 4 + ui/src/lib/protocol.ts | 9 ++ 16 files changed, 570 insertions(+), 6 deletions(-) create mode 100644 core/tests/test_add_grok_preset.py create mode 100644 ui/src/lib/components/GrokOnPicker.svelte create mode 100644 ui/src/lib/grok-on-picker.test.ts diff --git a/DECISIONS.md b/DECISIONS.md index dc36ea33..9cc71b44 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -19939,3 +19939,25 @@ predictable); changing an open session's engine in place; making that field as the global setting, so the window computes a new chat's engine from `preset_engines` instead). +## 2026-11-20 — TD-5258: one narrow verb puts Grok on the model list (Class B) + +**Decision:** `add_grok_preset {label?}` writes one catalog entry that is a +label, a fixed description and `engine: grok`, and is acked with +`setup_state`. The daemon refuses it with `grok_unavailable` when the Grok +CLI is not found and with `bad_request` when an entry already names the Grok +engine. It does not write `active_preset`. Settings → Engine always renders +the button and disables it, with the reason, when the CLI is missing. + +**Rationale:** the entry has nothing to choose but its name, so the verb +carries nothing else and cannot carry a key or a URL. Checking for the CLI +in the daemon keeps a client from adding an entry that could only fail. +Leaving the active model alone matches `duplicate_preset`: adding to the +list and choosing from it are separate acts. A button that appears only +where the CLI is installed would make the UI walk's census differ between a +developer's machine and CI. + +**Rejected:** reusing `add_model` with an engine field (it requires a URL +and a model and makes the new entry active); allowing several Grok entries +(they would be the same thing twice on the picker; a copy can still be made +with Duplicate). + diff --git a/core/scripts/generate_protocol_fixtures.py b/core/scripts/generate_protocol_fixtures.py index 13a5fe0d..6aee439b 100644 --- a/core/scripts/generate_protocol_fixtures.py +++ b/core/scripts/generate_protocol_fixtures.py @@ -19,6 +19,7 @@ from tstd.protocol import ( CHUNK_MAX, PROTOCOL_VERSION, + AddGrokPreset, AddMachine, AddModel, AddPin, @@ -591,6 +592,8 @@ name="tst-default", label="House model", description="General coding and chat" ), "delete_preset": DeletePreset(name="budget"), + # TD-5258: put the Grok CLI on the model list + "add_grok_preset": AddGrokPreset(), "set_engine": SetEngine(kind="grok"), "set_grok_mode": SetGrokMode(session_id="sess-1", mode="plan"), "run_grok_command": RunGrokCommand(session_id="sess-1", name="compact", argument="keep tests"), diff --git a/core/tests/test_add_grok_preset.py b/core/tests/test_add_grok_preset.py new file mode 100644 index 00000000..79798b35 --- /dev/null +++ b/core/tests/test_add_grok_preset.py @@ -0,0 +1,164 @@ +"""Put the Grok CLI on the model list from Settings → Engine (TD-5258). + +One narrow verb writes an entry that is ``engine: grok`` and nothing else +(TD-5239). It is offered only where the Grok CLI is found, and once. +""" + +from __future__ import annotations + +from pathlib import Path + +import pytest + +from tests.catalog_helpers import catalog_config_yaml +from tests.test_add_model import _ask, _start, _stop +from tstd.config import ConfigError, default_config_yaml, load_config +from tstd.mock import MockProvider, Script +from tstd.peer_policy import MACHINE_PEER_VERBS +from tstd.preset_write import add_grok_preset + +DESCRIPTION = "The Grok CLI, with its own model" + + +def _write(tmp_path: Path, text: str) -> Path: + path = tmp_path / "config.yaml" + path.write_text(text, encoding="utf-8") + return path + + +class TestTheWriter: + def test_into_the_shipped_empty_catalog(self, tmp_path: Path) -> None: + shipped = default_config_yaml() + path = _write(tmp_path, shipped) + add_grok_preset("grok", "Grok", DESCRIPTION, path) + block = ( + '\npresets:\n grok:\n label: "Grok"\n' + f' description: "{DESCRIPTION}"\n engine: grok\n' + ) + assert path.read_text(encoding="utf-8") == shipped.replace("\npresets: {}\n", block, 1) + config = load_config(path) + entry = config.presets["grok"] + assert entry.engine == "grok" and entry.has_tiers is False + assert entry.label == "Grok" and entry.description == DESCRIPTION + # The only entry, so the loader makes it the one new chats use. + assert config.active_preset == "grok" and config.has_model() is True + assert config.requires_api_key() is False + + def test_after_the_last_entry_of_a_catalog_and_nothing_else_changes( + self, tmp_path: Path + ) -> None: + before = catalog_config_yaml() + path = _write(tmp_path, before) + was_active = load_config(path).active_preset + add_grok_preset("grok", "Grok", DESCRIPTION, path) + after = path.read_text(encoding="utf-8").split("\n") + start = after.index(" grok:") + assert after[start : start + 4] == [ + " grok:", + ' label: "Grok"', + f' description: "{DESCRIPTION}"', + " engine: grok", + ] + assert "\n".join(after[:start] + after[start + 4 :]) == before + config = load_config(path) + assert list(config.presets)[-1] == "grok" + assert config.active_preset == was_active + + def test_a_file_with_no_catalog_at_all(self, tmp_path: Path) -> None: + path = _write(tmp_path, default_config_yaml().replace("\npresets: {}\n", "\n", 1)) + add_grok_preset("grok", "Grok", "", path) + entry = load_config(path).presets["grok"] + assert entry.engine == "grok" and entry.description is None + + def test_a_label_cannot_inject_yaml(self, tmp_path: Path) -> None: + path = _write(tmp_path, default_config_yaml()) + add_grok_preset("grok", 'G"\n engine: native', DESCRIPTION, path) + entry = load_config(path).presets["grok"] + assert entry.label == 'G"\n engine: native' and entry.engine == "grok" + + def test_once_is_enough(self, tmp_path: Path) -> None: + path = _write(tmp_path, default_config_yaml()) + add_grok_preset("grok", "Grok Build", DESCRIPTION, path) + text = path.read_text(encoding="utf-8") + with pytest.raises(ConfigError, match=r"already one of your models \(Grok Build\)"): + add_grok_preset("grok-2", "Grok", DESCRIPTION, path) + assert path.read_text(encoding="utf-8") == text + + @pytest.mark.parametrize( + ("name", "label", "says"), + [ + ("local", "Grok", "already exists"), + ("Not A Slug", "Grok", "lowercase slug"), + ("grok", " ", "Give the model a name"), + ], + ) + def test_refusals_write_nothing(self, tmp_path: Path, name: str, label: str, says: str) -> None: + path = _write(tmp_path, catalog_config_yaml()) + with pytest.raises(ConfigError, match=says): + add_grok_preset(name, label, DESCRIPTION, path) + assert path.read_text(encoding="utf-8") == catalog_config_yaml() + + +def _mock() -> MockProvider: + return MockProvider(default=Script(kind="text")) + + +def _grok(monkeypatch: pytest.MonkeyPatch, *, found: bool) -> None: + result = (Path("/usr/bin/grok"), True) if found else (None, False) + monkeypatch.setattr("tstd.daemon._probe_grok", lambda _configured: result) + + +class TestOverTheWire: + async def test_it_adds_the_entry_and_leaves_the_active_model( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _grok(monkeypatch, found=True) + daemon, task, ws = await _start(tmp_path, _mock()) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + assert before["preset_engines"] == {} + state = await _ask(ws, {"type": "add_grok_preset"}, "setup_state") + assert state["type"] == "setup_state" + assert state["presets"] == sorted([*before["presets"], "grok"]) + assert state["preset_engines"] == {"grok": "grok"} + assert state["preset_labels"]["grok"] == "Grok" + assert state["preset_descriptions"]["grok"] == DESCRIPTION + assert state["active_preset"] == before["active_preset"] + assert state["engine"] == before["engine"] + on_disk = load_config(daemon.config_path).presets["grok"] + assert on_disk.engine == "grok" and on_disk.has_tiers is False + finally: + await _stop(task, ws) + + async def test_a_second_ask_is_refused( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _grok(monkeypatch, found=True) + daemon, task, ws = await _start(tmp_path, _mock()) + try: + await _ask(ws, {"type": "add_grok_preset", "label": "Grok Build"}, "setup_state") + text = daemon.config_path.read_text(encoding="utf-8") + reply = await _ask(ws, {"type": "add_grok_preset"}, "setup_state") + assert reply["type"] == "error" and reply["code"] == "bad_request" + assert "already one of your models (Grok Build)" in reply["message"] + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await _stop(task, ws) + + async def test_it_is_refused_where_the_grok_cli_is_not_found( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _grok(monkeypatch, found=False) + daemon, task, ws = await _start(tmp_path, _mock()) + try: + text = daemon.config_path.read_text(encoding="utf-8") + reply = await _ask(ws, {"type": "add_grok_preset"}, "setup_state") + assert reply["type"] == "error" and reply["code"] == "grok_unavailable" + assert "not found" in reply["message"] + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await _stop(task, ws) + + +def test_another_computers_hub_may_not_send_it() -> None: + assert "add_grok_preset" not in MACHINE_PEER_VERBS diff --git a/core/tstd/daemon.py b/core/tstd/daemon.py index bb74e889..41307edf 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -230,8 +230,14 @@ no_model_refusal, preset_overrides, ) -from .preset_write import delete_preset, duplicate_preset, save_preset_label +from .preset_write import ( + add_grok_preset, + delete_preset, + duplicate_preset, + save_preset_label, +) from .protocol import ( + AddGrokPreset, AddModel, AddPin, AlwaysAllow, @@ -928,6 +934,10 @@ def _shipped_typesafe_base_url() -> str: return url.strip() if isinstance(url, str) else "" +#: The line under the label of the entry "Add Grok to your models" writes (TD-5258). +GROK_PRESET_DESCRIPTION = "The Grok CLI, with its own model" + + def _probe_grok(configured: str) -> tuple[Path | None, bool]: """PATH / configured binary probe. Never a credential.""" try: @@ -2170,9 +2180,9 @@ async def _handle_add_model(self, msg: AddModel) -> str: return (await self._setup_state_event()).model_dump_json() async def _handle_catalog_edit( - self, msg: DuplicatePreset | SetPresetLabel | DeletePreset + self, msg: DuplicatePreset | SetPresetLabel | DeletePreset | AddGrokPreset ) -> str: - """Copy, relabel or remove one catalog entry; ack with setup_state (TD-5237). + """Copy, relabel, remove or add-Grok for the catalog; ack with setup_state (TD-5237). The catalog changes for new chats and for the next switch. A chat that is open keeps the snapshot it holds, a removed entry's included @@ -2186,6 +2196,19 @@ async def _handle_catalog_edit( elif isinstance(msg, SetPresetLabel): save_preset_label(msg.name, msg.label, msg.description, self.config_path) fields = {"action": "label", "preset": msg.name} + elif isinstance(msg, AddGrokPreset): + # TD-5258: an entry for an engine that is not here would only + # ever fail, so it is not offered until the CLI is found. + _path, found = await asyncio.to_thread(_probe_grok, self.config.engine.binary) + if not found: + return build_error( + "grok_unavailable", + "The Grok CLI was not found on this computer. Install Grok Build " + "and run `grok login`, or set engine.binary in config.yaml.", + ) + made = preset_name_for(msg.label, set(self.config.presets)) + add_grok_preset(made, msg.label, GROK_PRESET_DESCRIPTION, self.config_path) + fields = {"action": "add_grok", "preset": made} else: delete_preset(msg.name, self.config_path) fields = {"action": "delete", "preset": msg.name} @@ -4489,7 +4512,7 @@ async def send(reply: str) -> None: ) return (await self._setup_state_event()).model_dump_json() - if isinstance(msg, DuplicatePreset | SetPresetLabel | DeletePreset): + if isinstance(msg, DuplicatePreset | SetPresetLabel | DeletePreset | AddGrokPreset): return await self._handle_catalog_edit(msg) if isinstance(msg, SetTierSlug): diff --git a/core/tstd/preset_write.py b/core/tstd/preset_write.py index 98313b4f..83210d15 100644 --- a/core/tstd/preset_write.py +++ b/core/tstd/preset_write.py @@ -1,4 +1,4 @@ -"""Copy, relabel and remove catalog entries in the user's ``config.yaml`` (TD-5237). +"""Copy, relabel, remove and add-Grok for the catalog in the user's ``config.yaml`` (TD-5237). The walkthrough could only add a model, so the way to try a variation was to overwrite the one entry already there. These three writers let Settings @@ -264,3 +264,55 @@ def delete_preset(name: str, path: Path | None = None) -> Path: raise ConfigError(f"Could not remove {name!r}: the file declares it more than once") _atomic_write(config_path, text) return config_path + + +def add_grok_preset(name: str, label: str, description: str, path: Path | None = None) -> Path: + """Add an entry that is the Grok engine and nothing else (TD-5258). + + ``label``, ``description`` and ``engine: grok``: the Grok CLI brings + its own model and sign-in, so the entry names no roles and no key + (TD-5239). It lands after the last entry, or opens the catalog when + there is none. ``active_preset`` is not written. + + Raises ``ConfigError`` when *name* is taken or not a slug, or an entry + already names the Grok engine: one is enough for the picker. + """ + if not CREDENTIAL_ID_RE.match(name): + raise ConfigError(f"preset name {name!r} must be a lowercase slug [a-z][a-z0-9-]{{0,31}}") + cleaned = _clean_label(label) + config_path = ensure_user_config(path) + config = load_config(config_path) + if name in config.presets: + raise ConfigError(f"A preset named {name!r} already exists") + existing = next((n for n, p in config.presets.items() if p.engine == "grok"), None) + if existing is not None: + shown = config.presets[existing].label or existing + raise ConfigError(f"Grok is already one of your models ({shown})") + + lines = config_path.read_text(encoding="utf-8").split("\n") + header = _find_key(lines, 0, len(lines), "presets", 0) + block = [f" {name}:", f" label: {json.dumps(cleaned)}"] + if description.strip(): + block.append(f" description: {json.dumps(description.strip())}") + block.append(" engine: grok") + if header < 0: + if lines and lines[-1].strip(): + lines.append("") + lines += ["presets:", *block, ""] + elif lines[header].split(":", 1)[1].split("#", 1)[0].strip() == "{}": + # The shipped empty catalog. + lines[header] = "presets:" + lines[header + 1 : header + 1] = block + else: + _header, end, entry_indent = _presets_block(lines, config_path) + pad = " " * (entry_indent - 2) + at = _content_end(lines, header, end) + lines[at:at] = [pad + line for line in block] if pad else block + + text = "\n".join(lines) + loaded = _checked(text, config_path, "Could not add Grok") + added = loaded.presets.get(name) + if added is None or added.engine != "grok": + raise ConfigError("Could not add Grok: the entry did not load") + _atomic_write(config_path, text) + return config_path diff --git a/core/tstd/protocol.py b/core/tstd/protocol.py index c1ca7c4f..d21c589a 100644 --- a/core/tstd/protocol.py +++ b/core/tstd/protocol.py @@ -1356,6 +1356,20 @@ class SetPresetLabel(ClientMessage): description: str | None = Field(default=None, max_length=120) +class AddGrokPreset(ClientMessage): + """Put the Grok CLI on the model list (TD-5258). + + Settings → Engine, "Add Grok to your models". Writes one catalog entry + that is ``engine: grok`` and names no model or key (TD-5239). Refused + when the Grok CLI is not found on this computer, or an entry already + names the Grok engine. The model new chats use does not change. Acked + with ``setup_state``. + """ + + type: Literal["add_grok_preset"] = "add_grok_preset" + label: str = Field(default="Grok", min_length=1, max_length=40) + + class DeletePreset(ClientMessage): """Remove one catalog entry (TD-5237). @@ -4906,6 +4920,7 @@ class GoogleStatus(DaemonEvent): | TestModel | SetPreset | DuplicatePreset + | AddGrokPreset | SetPresetLabel | DeletePreset | SetEngine @@ -5164,6 +5179,7 @@ class GoogleStatus(DaemonEvent): "delete_api_key", "set_preset", "duplicate_preset", + "add_grok_preset", "set_preset_label", "delete_preset", "set_engine", diff --git a/docs/architecture.md b/docs/architecture.md index 3497492e..23d7dd17 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -651,6 +651,7 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `duplicate_preset` | — | Settings → Model, "Duplicate": copy the `source` entry under a new `label`. The daemon derives the new key and writes every role out in full, so the copy shares no YAML anchor with the original. The active entry does not change. Acked with `setup_state` (TD-5237). | | `set_preset_label` | — | Settings → Model, "Rename": a new `label` (and `description`, when sent) for one entry. The entry's key never changes. Acked with `setup_state` (TD-5237). | | `delete_preset` | — | Settings → Model, "Remove": take one entry out of the catalog. This computer's window only. Refused while `computer_use.local_worker_preset` names it or another entry reuses part of it. Removing the active entry makes the first one left active; an open chat keeps the model it opened with. No key is deleted. Acked with `setup_state` (TD-5237). | +| `add_grok_preset` | — | Settings → Engine, "Add Grok to your models": write one catalog entry that is `engine: grok` and names no model or key (TD-5239), labelled `label` (default `Grok`). Refused with `grok_unavailable` when the Grok CLI is not found on this computer, and when an entry already names the Grok engine. The active entry does not change. Acked with `setup_state` (TD-5258). | | `set_engine` | — | Choose the agent engine for new sessions (`native` or `grok`). Acked with `setup_state`. | | `set_grok_mode` | yes | Switch the live Grok ACP session mode. | | `run_grok_command` | yes | Enqueue `/{name} {argument}` as the next Grok prompt. Refused with `session_busy` while the session restores files or closes its worktree (TD-5161). | diff --git a/docs/models.md b/docs/models.md index a07b9af0..bc75fb93 100644 --- a/docs/models.md +++ b/docs/models.md @@ -212,6 +212,9 @@ presets: engine: grok ``` +Settings → Engine → **Add Grok to your models** writes this entry for you +once the Grok CLI is installed; by hand it is the four lines above. + A new chat started while this entry is the active one runs on the Grok CLI. A chat keeps the engine it started on, so the picker holds this entry for a chat that is already running on a model of yours, and holds your diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index d24a5326..2d6273f1 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13515,3 +13515,33 @@ active entry has no model. or the one most of its turns ran on. Recorded 2026-11-20 at the owner's request as a possible later story. +--- + +### TD-5258 — Add Grok to your models from Settings → Engine + +**Size:** 2 · **Depends on:** TD-5239 · **Status:** Done + +After TD-5239 a Grok entry could sit on the picker, but only someone who +edited `config.yaml` could put it there. + +- [x] `add_grok_preset` writes one entry that is a label, a description and + `engine: grok`, after the last entry or into the empty shipped + catalog; the rest of the file and `active_preset` are not changed + (`test_add_grok_preset.py::TestTheWriter`). +- [x] It is refused when the Grok CLI is not found on this computer, and + when an entry already names the Grok engine; a refusal writes nothing + (`test_it_is_refused_where_the_grok_cli_is_not_found`, + `test_a_second_ask_is_refused`, `test_once_is_enough`). +- [x] Acked with `setup_state`, which lists the entry and its engine + (`test_it_adds_the_entry_and_leaves_the_active_model`). +- [x] Settings → Engine has "Add Grok to your models": one message on a + click, held with the reason where the CLI is missing, replaced by a + line once Grok is on the list, and the daemon's reason shown on a + refusal (`grok-on-picker.test.ts`). +- [x] The UI walk lists the button and does not press it + (`e2e/census.json`, `GROK_ADD` in `app-walk.spec.ts`). + +**Notes:** The button is always rendered, so the walk's census is the same +on a machine with the Grok CLI and one without. Class B entry in +DECISIONS.md. + diff --git a/ui/e2e/app-walk.spec.ts b/ui/e2e/app-walk.spec.ts index 62c95d56..47191516 100644 --- a/ui/e2e/app-walk.spec.ts +++ b/ui/e2e/app-walk.spec.ts @@ -651,6 +651,12 @@ async function walkModelCatalog(dialog: Locator): Promise { const CATALOG_ROW = /^(Duplicate|Rename|Remove) .+$/; +// "Add Grok to your models" (TD-5258) is held where the Grok CLI is not +// installed, which is CI, and live on a developer's machine that has it. A +// click there would add an entry the Model section's census does not expect, +// so the walk leaves it alone; grok-on-picker.test.ts presses it. +const GROK_ADD = /^Add Grok to your models$/; + const SKIP_CLICK = /^(Close|Sign in with Microsoft|Sign in with Google|Connect GitHub|Disconnect|Send test email|Delete|Remove|Forget|Copy code|Open browser|Try again|How models work)$/; @@ -663,7 +669,8 @@ async function exercise(page: Page, content: Locator): Promise { const name = ((await button.getAttribute("aria-label")) || (await button.innerText())) .replace(/\s+/g, " ") .trim(); - if (name === "" || SKIP_CLICK.test(name) || CATALOG_ROW.test(name)) continue; + if (name === "" || SKIP_CLICK.test(name) || CATALOG_ROW.test(name) || GROK_ADD.test(name)) + continue; await dismissExtraDialogs(page); await button.click({ timeout: 5_000 }); await dismissExtraDialogs(page); diff --git a/ui/e2e/census.json b/ui/e2e/census.json index 3a52ac98..100e6bfd 100644 --- a/ui/e2e/census.json +++ b/ui/e2e/census.json @@ -200,6 +200,7 @@ "toggle" ], "settings:Engine": [ + "Add Grok to your models", "Grok Build Spawns your installed grok CLI over ACP. The CLI stays the engine — this window is a viewer. Auth stays in ~/.grok.", "TST native The built-in three-tier loop. Uses your OpenAI-compatible presets and keys." ], diff --git a/ui/src/lib/components/GrokOnPicker.svelte b/ui/src/lib/components/GrokOnPicker.svelte new file mode 100644 index 00000000..a1ffa274 --- /dev/null +++ b/ui/src/lib/components/GrokOnPicker.svelte @@ -0,0 +1,104 @@ + + +

Grok on the model list

+{#if onList !== null} +

+ {modelLabel(onList, settings.presetLabels)} is on the model list. A new chat started on it + runs on the Grok CLI, whatever the engine above is set to. +

+{:else} +

+ Puts Grok beside your models in the picker. It uses the Grok CLI's own model and sign-in, + so there is no key or model to set here. +

+ + {#if !settings.grokAvailable} +

+ The Grok CLI was not found on this computer. Install Grok Build and run + grok login first. +

+ {/if} + {#if problem} + + {/if} +{/if} + + diff --git a/ui/src/lib/components/SettingsEngine.svelte b/ui/src/lib/components/SettingsEngine.svelte index 857fc4ca..6923ff5a 100644 --- a/ui/src/lib/components/SettingsEngine.svelte +++ b/ui/src/lib/components/SettingsEngine.svelte @@ -1,6 +1,7 @@ +{#snippet row(name: string)} +
+ + {#if name !== current && !clash(name) && !bridged} + + {/if} +
+{/snippet} + @@ -128,26 +201,7 @@ @@ -359,6 +394,39 @@ outline-offset: -1px; } + .row { + display: flex; + align-items: center; + } + + .row .item { + flex: 1; + min-width: 0; + } + + .only { + flex: none; + font-family: var(--font-sans); + font-size: var(--text-xs); + color: var(--color-ink-muted); + background: transparent; + border: none; + border-radius: var(--radius-sm); + padding: var(--space-1) var(--space-2); + cursor: pointer; + white-space: nowrap; + } + + .only:hover, + .only:focus-visible { + color: var(--color-ink); + background: var(--color-sunken); + } + + .pending-tag { + color: var(--color-ink-muted); + } + .item:disabled { opacity: 0.45; cursor: not-allowed; diff --git a/ui/src/lib/model-engine.test.ts b/ui/src/lib/model-engine.test.ts index dcf82880..3f837171 100644 --- a/ui/src/lib/model-engine.test.ts +++ b/ui/src/lib/model-engine.test.ts @@ -95,21 +95,13 @@ describe("the model picker", () => { expect(row(menu, "Local").querySelector(".badge")).toBeNull(); }); - it("holds the Grok model for a chat on TST Desk's own loop, and says why", async () => { + it("says the Grok model opens a new chat for a chat on TST Desk's own loop", async () => { const menu = await openPicker(); const grok = row(menu, "Grok Build"); - expect(grok.disabled).toBe(true); - expect(grok.title).toContain("start a new chat"); - expect(row(menu, "API").disabled).toBe(false); + // TD-5235: no longer held. It cannot move this chat, so it opens one. + expect(grok.disabled).toBe(false); + expect(grok.title).toContain("Opens a new chat"); + expect(grok.querySelector(".sub")?.textContent).toBe("Opens a new chat"); expect(row(menu, "API").title).toBe(""); }); - - it("still holds every other model while a turn runs", async () => { - const menu = await openPicker(); - session.turnActive = true; - flushSync(); - expect(row(menu, "API").disabled).toBe(true); - expect(row(menu, "API").title).toBe("Cannot change model while a turn is running"); - expect(row(menu, "Local").disabled).toBe(false); - }); }); diff --git a/ui/src/lib/model-picker.test.ts b/ui/src/lib/model-picker.test.ts index fa9c58d9..e161bbee 100644 --- a/ui/src/lib/model-picker.test.ts +++ b/ui/src/lib/model-picker.test.ts @@ -30,6 +30,8 @@ function fakeClient(): ProtocolClient { sent.push({ type: "set_ultracode", session_id: sessionId, on }), setSessionPreset: (sessionId: string, name: string) => sent.push({ type: "set_session_preset", session_id: sessionId, name }), + // TD-5235: the picker's click goes out as one switch_preset. + send: (msg: unknown) => (sent.push(msg), true), } as unknown as ProtocolClient; } @@ -110,7 +112,10 @@ describe("model picker", () => { await click(item("More models")); expect(texts(".item")).toEqual(["Claude Opus", "Grok"]); await click(item("Grok")); - expect(sent).toEqual([{ type: "set_session_preset", session_id: "s1", name: "sub-grok" }]); + // TD-5235: a click moves this chat and the model new chats use. + expect(sent).toEqual([ + { type: "switch_preset", name: "sub-grok", session_id: "s1", make_default: true }, + ]); }); it("shows Teams only when a team exists", async () => { diff --git a/ui/src/lib/model-switch.test.ts b/ui/src/lib/model-switch.test.ts new file mode 100644 index 00000000..6d978d4e --- /dev/null +++ b/ui/src/lib/model-switch.test.ts @@ -0,0 +1,189 @@ +// @vitest-environment jsdom +// +// TD-5235: the picker's click. It moves this chat and makes the model the +// one new chats use; "Only this chat" moves just the chat; a model picked +// while a turn runs waits for the turn; a model on the other engine opens a +// new chat on it. + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, tick, unmount } from "svelte"; +import type { ClientMessageUnion } from "./protocol"; + +const mocks = vi.hoisted(() => ({ newChats: [] as (string | undefined)[] })); + +vi.mock("./sessions.svelte.js", () => ({ + newSession: (preset?: string) => { + mocks.newChats.push(preset); + return true; + }, +})); + +import ModelPicker from "./components/chat/ModelPicker.svelte"; +import type { ProtocolClient } from "./client"; +import { bindClient, ingestEvent, resetSession, session } from "./session-status.svelte.js"; +import { resetSettings, settings } from "./settings.svelte.js"; + +let app: ReturnType | null = null; +let sent: ClientMessageUnion[] = []; + +function fakeClient(): ProtocolClient { + return { + send: (msg: ClientMessageUnion) => (sent.push(msg), true), + setSessionPreset: (sessionId: string, name: string) => + sent.push({ type: "set_session_preset", session_id: sessionId, name }), + } as unknown as ProtocolClient; +} + +beforeEach(() => { + resetSession(); + resetSettings(); + sent = []; + mocks.newChats = []; + bindClient(fakeClient()); + session.sessionId = "s1"; + session.preset = "local"; + session.engine = "native"; + settings.hasModel = true; + settings.activePreset = "local"; + settings.presets = ["api", "grok", "local"]; + settings.presetLabels = { api: "API", grok: "Grok Build", local: "Local" }; + settings.presetEngines = { grok: "grok" }; +}); + +afterEach(async () => { + if (app !== null) { + await unmount(app); + app = null; + } + document.body.replaceChildren(); + bindClient(null); + resetSession(); + resetSettings(); +}); + +async function openPicker(): Promise { + app = mount(ModelPicker, { target: document.body }); + await tick(); + chip().click(); + flushSync(); + return document.querySelector('[role="menu"]')!; +} + +function chip(): HTMLButtonElement { + return document.querySelector('[aria-label="Model and effort"]')!; +} + +function row(menu: HTMLElement, label: string): HTMLButtonElement { + const found = [...menu.querySelectorAll('[role="menuitemradio"]')].find( + (button) => button.querySelector(".label")?.textContent?.includes(label), + ); + if (found === undefined) throw new Error(`no row for ${label}`); + return found; +} + +function only(menu: HTMLElement, label: string): HTMLButtonElement | null { + return menu.querySelector(`[aria-label="Only this chat: ${label}"]`); +} + +describe("picking a model", () => { + it("moves this chat and makes it the model new chats use", async () => { + const menu = await openPicker(); + row(menu, "API").click(); + expect(sent).toEqual([ + { type: "switch_preset", name: "api", session_id: "s1", make_default: true }, + ]); + expect(mocks.newChats).toEqual([]); + }); + + it("Only this chat leaves the model new chats use alone", async () => { + const menu = await openPicker(); + only(menu, "API")!.click(); + expect(sent).toEqual([ + { type: "switch_preset", name: "api", session_id: "s1", make_default: false }, + ]); + }); + + it("offers Only this chat on every model but the current one and the other engine's", async () => { + const menu = await openPicker(); + expect(only(menu, "API")).not.toBeNull(); + expect(only(menu, "Local")).toBeNull(); + expect(only(menu, "Grok Build")).toBeNull(); + }); + + it("does nothing for the model the chat and new chats already use", async () => { + const menu = await openPicker(); + row(menu, "Local").click(); + expect(sent).toEqual([]); + }); + + it("makes the chat's own model the default when new chats use another", async () => { + settings.activePreset = "api"; + const menu = await openPicker(); + row(menu, "Local").click(); + expect(sent).toEqual([ + { type: "switch_preset", name: "local", session_id: "s1", make_default: true }, + ]); + }); +}); + +describe("picking a model on the other engine", () => { + it("opens a new chat on it and makes it the model new chats use", async () => { + const menu = await openPicker(); + row(menu, "Grok Build").click(); + expect(sent).toEqual([{ type: "switch_preset", name: "grok", make_default: true }]); + expect(mocks.newChats).toEqual(["grok"]); + }); +}); + +describe("picking a model while a turn runs", () => { + it("is allowed, and says it applies when the turn finishes", async () => { + session.turnActive = true; + const menu = await openPicker(); + const api = row(menu, "API"); + expect(api.disabled).toBe(false); + expect(api.title).toBe("Applies when this turn finishes"); + api.click(); + expect(sent).toEqual([ + { type: "switch_preset", name: "api", session_id: "s1", make_default: true }, + ]); + }); + + it("shows on the chip what is waiting, and clears when it lands", async () => { + app = mount(ModelPicker, { target: document.body }); + await tick(); + const state = { + type: "tier_state", + seq: 2, + session_id: "s1", + tier: "brain", + model_slugs: {}, + preset: "local", + pending_preset: "api", + } as const; + ingestEvent(state as never); + flushSync(); + expect(session.pendingPreset).toBe("api"); + expect(chip().textContent).toContain("Local Medium"); + expect(chip().textContent).toContain("→ API after this turn"); + ingestEvent({ ...state, seq: 3, preset: "api", pending_preset: null } as never); + flushSync(); + expect(session.pendingPreset).toBeNull(); + expect(chip().textContent).not.toContain("after this turn"); + expect(chip().textContent).toContain("API Medium"); + }); +}); + +describe("a chat on another computer", () => { + it("keeps the older verb, which its hub forwards, and its own rules", async () => { + session.sessionId = "s1:mch_0123456789ab"; + session.turnActive = true; + const menu = await openPicker(); + expect(row(menu, "API").disabled).toBe(true); + expect(row(menu, "API").title).toBe("Cannot change model while a turn is running"); + expect(only(menu, "API")).toBeNull(); + session.turnActive = false; + flushSync(); + row(menu, "API").click(); + expect(sent).toEqual([{ type: "set_session_preset", session_id: "s1:mch_0123456789ab", name: "api" }]); + }); +}); diff --git a/ui/src/lib/protocol-fixtures.json b/ui/src/lib/protocol-fixtures.json index e9d92f22..1a3b0e57 100644 --- a/ui/src/lib/protocol-fixtures.json +++ b/ui/src/lib/protocol-fixtures.json @@ -653,6 +653,12 @@ "type": "delete_preset", "name": "budget" }, + "switch_preset": { + "type": "switch_preset", + "name": "budget", + "session_id": "sess-1", + "make_default": true + }, "add_grok_preset": { "type": "add_grok_preset", "label": "Grok" @@ -1489,6 +1495,7 @@ "validator": "test-validator-slug" }, "preset": "test", + "pending_preset": null, "hosts": { "brain": "mock.local", "worker": "mock.local", @@ -1516,6 +1523,7 @@ "validator": "test-validator-slug" }, "preset": "test", + "pending_preset": null, "hosts": { "brain": "mock.local", "worker": "mock.local", @@ -1543,6 +1551,7 @@ "validator": "test-validator-slug" }, "preset": "test", + "pending_preset": null, "hosts": { "brain": "mock.local", "worker": "mock.local", @@ -1570,6 +1579,7 @@ "validator": "test-validator-slug" }, "preset": "test", + "pending_preset": null, "hosts": { "brain": "mock.local", "worker": "mock.local", @@ -2054,6 +2064,7 @@ "validator": "test-validator-slug" }, "preset": "test", + "pending_preset": null, "hosts": { "brain": "backup.local", "worker": "mock.local", diff --git a/ui/src/lib/protocol.ts b/ui/src/lib/protocol.ts index a61070e5..1f412a17 100644 --- a/ui/src/lib/protocol.ts +++ b/ui/src/lib/protocol.ts @@ -655,6 +655,9 @@ export interface SearchSessions extends ClientMessage { export interface NewSession extends ClientMessage { type: "new_session"; session_id: string; + /** TD-5235: the catalog entry the new chat opens on, with its engine. + * Omitted: the model new chats use. */ + preset?: string | null; } /** File a session away, or restore it (TD-1715). Filing, never killing: an @@ -907,6 +910,17 @@ export interface SetPresetLabel extends ClientMessage { description?: string | null; } +// TD-5235: one switch. `session_id` switches that chat (now, or when its +// running turn completes); `make_default` also makes it the model new chats +// use. Checked whole before anything changes. Acked with setup_state when +// the default changed, else session_list. +export interface SwitchPreset extends ClientMessage { + type: "switch_preset"; + name: string; + session_id?: string | null; + make_default?: boolean; +} + // TD-5258: put the Grok CLI on the model list as an entry that is // `engine: grok` and names no model or key. Refused when the CLI is not // found or an entry already names the Grok engine. Acked with setup_state. @@ -1399,6 +1413,7 @@ export type ClientMessageUnion = | TestModel | SetPreset | DuplicatePreset + | SwitchPreset | AddGrokPreset | SetPresetLabel | DeletePreset @@ -1875,6 +1890,8 @@ export interface TierState extends DaemonEvent { model_slugs: Record; /** Preset this session opened with. Omitted by older daemons. */ preset?: string; + /** TD-5235: a switch waiting for the running turn to complete. */ + pending_preset?: string | null; /** Tier → hostname:port the client will call. Omitted by older daemons. */ hosts?: Record; /** Plan mode (brain lock). Omitted by older daemons — treat as off. */ diff --git a/ui/src/lib/session-status.svelte.ts b/ui/src/lib/session-status.svelte.ts index ba95cdc3..616e4b05 100644 --- a/ui/src/lib/session-status.svelte.ts +++ b/ui/src/lib/session-status.svelte.ts @@ -75,6 +75,9 @@ export const session = $state({ engine: null as "native" | "grok" | null, /** Preset this session opened with (TD-1720). Empty until tier_state. */ preset: "" as string, + /** TD-5235: a model picked while a turn runs; it applies when the turn + * completes. Null when nothing is waiting. */ + pendingPreset: null as string | null, /** Tier → hostname:port the daemon will call (TD-1720). */ hosts: {} as Record, /** Tiers on a backup endpoint this turn → its 1-based position (TD-5120). @@ -133,6 +136,7 @@ export function resetSession(): void { session.fastNote = ""; session.ultracode = false; session.ultracodeAvailable = false; + session.pendingPreset = null; session.vision = false; session.turnActive = false; session.context = null; @@ -191,6 +195,7 @@ export function ingestEvent(event: DaemonEventUnion): void { session.tierOverride = event.override ?? null; session.modelSlugs = event.model_slugs; session.preset = event.preset ?? ""; + session.pendingPreset = event.pending_preset ?? null; session.hosts = event.hosts ?? {}; session.fallbacks = event.fallbacks ?? {}; session.planMode = event.plan ?? false; @@ -293,6 +298,7 @@ export function focusSession( session.fastNote = ""; session.ultracode = false; session.ultracodeAvailable = false; + session.pendingPreset = null; session.vision = false; session.turnActive = false; session.context = null; @@ -363,6 +369,29 @@ export function setSessionPreset(name: string): void { client?.setSessionPreset(session.sessionId, name); } +/** TD-5235: the picker's click. This chat moves to `name`, and unless + * `onlyThisChat`, new chats use it too. A chat on another computer keeps + * the older verb, which its hub forwards; that computer's default is its own. */ +export function switchModel(name: string, opts: { onlyThisChat: boolean }): void { + const sessionId = session.sessionId; + if (sessionId === null) return; + if (isBridged(sessionId)) { + client?.setSessionPreset(sessionId, name); + return; + } + client?.send({ + type: "switch_preset", + name, + session_id: sessionId, + make_default: !opts.onlyThisChat, + }); +} + +/** TD-5235: make `name` the model new chats use, leaving every open chat alone. */ +export function useForNewChats(name: string): void { + client?.send({ type: "switch_preset", name, make_default: true }); +} + /** Display name for the workspace row: basename of the path. */ export function workspaceName(path: string): string { const parts = path.split(/[\\/]/).filter((p) => p.length > 0); diff --git a/ui/src/lib/sessions.svelte.ts b/ui/src/lib/sessions.svelte.ts index 2cd57f3b..15c797a8 100644 --- a/ui/src/lib/sessions.svelte.ts +++ b/ui/src/lib/sessions.svelte.ts @@ -425,7 +425,7 @@ export function boundTurn(): BoundTurn { * anchor works. With no rows at all (fresh install) there is nothing to * anchor on, so the wired directory picker runs instead of silently * no-op'ing — the chosen path goes through new_session_in_workspace. */ -export function newSession(): boolean { +export function newSession(preset?: string): boolean { if (pendingNewAnchor() !== null) return false; // A bound session on another computer grows there; unbound, the fallback // anchor is this computer's own newest row (TD-5015). @@ -443,7 +443,12 @@ export function newSession(): boolean { // A computer that isn't connected can't answer; the chat pane already // says it isn't reachable. if (!sessionMachineUp(anchor)) return false; - if (!sendToDaemon({ type: "new_session", session_id: anchor })) return false; + // TD-5235: on a named model, with its engine, when the picker asks for one. + const message = + preset === undefined + ? ({ type: "new_session", session_id: anchor } as const) + : ({ type: "new_session", session_id: anchor, preset } as const); + if (!sendToDaemon(message)) return false; setPendingNew(anchor); return true; } From 254123ad760588859c0b14d7e95c33563e984e08 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 18:12:06 +0000 Subject: [PATCH 07/35] TD-5236: Local, API and Grok sections in the picker, and a shortcut per model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The picker was one flat list and switching always took the mouse. A second open window also kept the old list and default after the first one changed them. Preset.group and Preset.slot are new optional keys. The daemon names every entry's section (ModelConfig.preset_group): its own group, else Grok for the Grok engine, Local when every role resolves to a loopback URL after a bound key's host, else API. A server on the local network reads API until the entry says group: Local. A slot is 1 to 9 and unique; a copy does not take its source's. setup_state carries preset_groups, picker_groups and preset_slots. The picker lists Local, API, Grok and then other sections, with no headings for a single section, and shows each slot beside its model. ⌘⌥1…9 / Ctrl+Alt+1…9 does what a click on that model's row does, read off the physical key, and not over a modal. After add_model, set_preset, switch_preset with make_default and the catalog-edit verbs, the daemon broadcasts setup_state to this computer's other windows, leaving out the connection it is answering. ModelPicker.svelte is back under 400 lines: the row is ModelRow.svelte and what a pick does is model-pick.ts, shared with the shortcut. Class B: recorded in DECISIONS.md. Co-Authored-By: Claude Fable 5.2 --- DECISIONS.md | 34 +++ core/tests/test_picker_groups.py | 253 ++++++++++++++++++ core/tstd/config.py | 51 +++- core/tstd/config_help.py | 4 + core/tstd/daemon.py | 38 ++- core/tstd/preset_write.py | 2 + core/tstd/protocol.py | 7 + core/tstd/ws.py | 13 +- docs/configuration.md | 2 + docs/models.md | 34 +++ docs/tst-desk-backlog.md | 39 +++ ui/src/lib/components/AppShell.svelte | 15 +- ui/src/lib/components/chat/ModelPicker.svelte | 146 ++-------- ui/src/lib/components/chat/ModelRow.svelte | 166 ++++++++++++ ui/src/lib/model-pick.ts | 73 +++++ ui/src/lib/model-sections.test.ts | 206 ++++++++++++++ ui/src/lib/model-sections.ts | 44 +++ ui/src/lib/protocol-fixtures.json | 3 + ui/src/lib/protocol.ts | 5 + ui/src/lib/settings.svelte.ts | 11 + ui/src/lib/shortcuts.ts | 20 +- 21 files changed, 1033 insertions(+), 133 deletions(-) create mode 100644 core/tests/test_picker_groups.py create mode 100644 ui/src/lib/components/chat/ModelRow.svelte create mode 100644 ui/src/lib/model-pick.ts create mode 100644 ui/src/lib/model-sections.test.ts create mode 100644 ui/src/lib/model-sections.ts diff --git a/DECISIONS.md b/DECISIONS.md index 16668f8c..6120eee7 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -19995,3 +19995,37 @@ older, session-only verb); having the daemon open the new chat itself on an engine clash (the window decides what a click on such a row means, and the refusal stays honest for other clients). +## 2026-11-20 — TD-5236: the daemon names each model's picker section; slots use ⌘⌥ digit; catalog changes are broadcast (Class B) + +**Decision:** `Preset.group` (free text, up to 20 characters) and +`Preset.slot` (1 to 9, unique) are new optional keys. +`ModelConfig.preset_group()` gives every entry a section: its own `group`, +else `Grok` for the Grok engine, `Local` when every role resolves to a +loopback URL, else `API`. `setup_state` carries `preset_groups`, +`picker_groups` (the built-in order) and `preset_slots`. The quick-switch +chord is ⌘⌥1…9 on macOS and Ctrl+Alt+1…9 elsewhere (owner, 2026-11-20), read +from `KeyboardEvent.code`. After `add_model`, `set_preset`, `switch_preset` +with `make_default`, and the four catalog-edit verbs, the daemon broadcasts +`setup_state` to this computer's other windows, leaving out the connection +it is answering. + +**Rationale:** "Local" cannot be read off a URL by the window: a server on +the person's own network is theirs, but nothing in the address says so, and +a bound key can send a loopback-looking role to a provider (TD-1718). So the +daemon decides, with the conservative default (not loopback means API), and +the entry can say otherwise. ⌘1…9 already selects a session, so the model +slots add Option; on a Mac Option changes the character a digit key types, +which is why the physical key is read. `setup_state` was only ever a reply +to the asker, so a second window showed a stale list and default; the +presence refresh (TD-5015) already broadcasts the same event to loopback +windows, and this reuses that audience. The asker is left out because its +own reply already carries the state, and a second copy breaks a client that +pairs one reply with one request. + +**Rejected:** inferring the section in the window from the endpoint (the +window must not derive what it was not sent); treating private-network +addresses as Local automatically (a corporate gateway on 10.x is not the +person's own machine); a broadcast to phones as well (a phone asks when it +shows the list, and `setup_state` for a remote peer is trimmed per +connection, which a broadcast would bypass); slots without Option (taken). + diff --git a/core/tests/test_picker_groups.py b/core/tests/test_picker_groups.py new file mode 100644 index 00000000..38d1c655 --- /dev/null +++ b/core/tests/test_picker_groups.py @@ -0,0 +1,253 @@ +"""The picker's sections and quick-switch slots (TD-5236). + +Local, API and Grok are sections of one list. The daemon says which +section each model is in, so the window never guesses from a URL; an entry +can name its own. A slot, 1 to 9, gives an entry a shortcut. And a change +to the catalog reaches every window of this computer, not only the one +that made it. +""" + +from __future__ import annotations + +import asyncio +import json +from pathlib import Path +from typing import Any + +import pytest +from websockets.asyncio.client import connect + +from tests.test_add_model import _ask, _start, _stop +from tests.test_preset_engine import _text as _catalog_with_grok +from tstd.config import PICKER_GROUPS, ConfigError, ModelConfig, load_config +from tstd.mock import MockProvider, Script +from tstd.preset_write import duplicate_preset +from tstd.protocol import PROTOCOL_VERSION + +LAN = """\ + lan-box: + label: The box next door + group: Local + slot: 2 + brain: &lan + slug: house/model + base_url: http://192.168.1.10:4000/v1 + credential: openrouter + context_window: 65536 + max_output_tokens: 8192 + input_price: 0.0 + output_price: 0.0 + cache_read_price: 0.0 + worker: *lan + validator: *lan + mine: + label: Experiments + group: " Trying out " + brain: *lan + worker: *lan + validator: *lan +""" + + +def _text(extra: str = LAN) -> str: + text = _catalog_with_grok() + assert text.count("\npresets:\n") == 1 + return text.replace("\npresets:\n", "\npresets:\n" + extra, 1) + + +def _with(text: str, entry: str, block: str) -> str: + header = f"\n {entry}:\n" + assert text.count(header) == 1, entry + return text.replace(header, header + block, 1) + + +def _config(tmp_path: Path, text: str | None = None) -> ModelConfig: + path = tmp_path / "config.yaml" + path.write_text(_text() if text is None else text, encoding="utf-8") + return load_config(path) + + +class TestTheSectionOfAnEntry: + def test_the_daemon_reads_it_off_the_entry(self, tmp_path: Path) -> None: + config = _config(tmp_path) + # Every role on this computer. + assert config.preset_group("local") == "Local" + assert config.preset_group("vllm") == "Local" + # A provider's server. + assert config.preset_group("tst-default") == "API" + assert config.preset_group("budget") == "API" + # The Grok engine. + assert config.preset_group("grok") == "Grok" + assert config.preset_group("nope") == "API" + + def test_an_entry_can_say_its_own(self, tmp_path: Path) -> None: + config = _config(tmp_path) + # A server on the local network is not this computer: only the + # person knows it is theirs, and here they said so. + assert config.preset_group("lan-box") == "Local" + assert config.presets["mine"].group == "Trying out" + assert config.preset_group("mine") == "Trying out" + + def test_without_saying_so_a_server_on_the_network_is_api(self, tmp_path: Path) -> None: + config = _config(tmp_path, _text(LAN.replace(" group: Local\n", ""))) + assert config.preset_group("lan-box") == "API" + + def test_a_key_that_points_a_local_role_at_a_provider_makes_it_api( + self, tmp_path: Path + ) -> None: + # TD-1718: a bound key's host is where the request really goes. + text = _text().replace( + " base_url: http://127.0.0.1:11434/v1\n", + " base_url: http://127.0.0.1:11434/v1\n credential: openrouter\n", + ) + config = _config(tmp_path, text) + assert config.credentials["openrouter"].base_url is not None + assert config.preset_group("local") == "API" + + def test_the_built_in_sections_come_in_this_order(self) -> None: + assert PICKER_GROUPS == ("Local", "API", "Grok") + + @pytest.mark.parametrize("group", ['""', "' '", "x" * 21]) + def test_a_blank_group_is_unset_and_a_long_one_is_refused( + self, tmp_path: Path, group: str + ) -> None: + text = _with(_text(""), "budget", f" group: {group}\n") + if len(group) > 20: + with pytest.raises(ConfigError, match=r"presets\.budget\.group"): + _config(tmp_path, text) + return + if group == '""': + with pytest.raises(ConfigError, match=r"presets\.budget\.group"): + _config(tmp_path, text) + return + assert _config(tmp_path, text).presets["budget"].group is None + + +class TestSlots: + def test_a_slot_is_one_to_nine(self, tmp_path: Path) -> None: + assert _config(tmp_path).presets["lan-box"].slot == 2 + for bad in ("0", "10", "-1", "two"): + with pytest.raises(ConfigError, match=r"presets\.budget\.slot"): + _config(tmp_path, _with(_text(""), "budget", f" slot: {bad}\n")) + + def test_no_two_entries_share_one(self, tmp_path: Path) -> None: + text = _with(_text(), "budget", " slot: 2\n") + with pytest.raises(ConfigError) as caught: + _config(tmp_path, text) + message = str(caught.value) + assert "slot 2 is already" in message + assert "lan-box" in message and "budget" in message + + def test_a_copy_does_not_take_its_sources_slot(self, tmp_path: Path) -> None: + path = tmp_path / "config.yaml" + path.write_text(_text(), encoding="utf-8") + duplicate_preset("lan-box", "lan-box-2", "The box, again", path) + config = load_config(path) + assert config.presets["lan-box"].slot == 2 + assert config.presets["lan-box-2"].slot is None + # The section travels with the copy; the shortcut cannot. + assert config.presets["lan-box-2"].group == "Local" + + +def _mock() -> MockProvider: + return MockProvider(default=Script(kind="text")) + + +async def _seeded(tmp_path: Path) -> tuple[Any, Any, Any]: + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_text(), encoding="utf-8") + return await _start(data, _mock()) + + +async def _second_window(daemon: Any) -> Any: + ws = await connect(f"ws://127.0.0.1:{daemon.ws_server.port}") + await ws.send( + json.dumps({"type": "hello", "token": daemon.ws_server.token, "version": PROTOCOL_VERSION}) + ) + assert json.loads(await ws.recv())["type"] == "hello_ack" + return ws + + +async def _next_setup_state(ws: Any) -> dict[str, Any]: + for _ in range(40): + event = dict(json.loads(await asyncio.wait_for(ws.recv(), 5))) + if event["type"] == "setup_state": + return event + raise AssertionError("no setup_state reached the other window") + + +class TestOverTheWire: + async def test_setup_state_names_every_models_section_and_each_slot( + self, tmp_path: Path + ) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + state = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + assert state["picker_groups"] == ["Local", "API", "Grok"] + assert set(state["preset_groups"]) == set(state["presets"]) + assert state["preset_groups"]["local"] == "Local" + assert state["preset_groups"]["lan-box"] == "Local" + assert state["preset_groups"]["tst-default"] == "API" + assert state["preset_groups"]["grok"] == "Grok" + assert state["preset_groups"]["mine"] == "Trying out" + assert state["preset_slots"] == {"lan-box": 2} + finally: + await _stop(task, ws) + + @pytest.mark.parametrize( + ("message", "check"), + [ + ( + {"type": "switch_preset", "name": "budget", "make_default": True}, + lambda s: s["active_preset"] == "budget", + ), + ({"type": "set_preset", "name": "budget"}, lambda s: s["active_preset"] == "budget"), + ( + {"type": "duplicate_preset", "source": "local", "label": "Local two"}, + lambda s: "local-two" in s["presets"], + ), + ( + {"type": "set_preset_label", "name": "local", "label": "Here"}, + lambda s: s["preset_labels"]["local"] == "Here", + ), + ({"type": "delete_preset", "name": "mine"}, lambda s: "mine" not in s["presets"]), + ], + ) + async def test_a_change_in_one_window_reaches_the_other( + self, tmp_path: Path, message: dict[str, Any], check: Any + ) -> None: + daemon, task, ws = await _seeded(tmp_path) + other = await _second_window(daemon) + try: + reply = await _ask(ws, message, "setup_state") + assert reply["type"] == "setup_state" and check(reply) + # Unasked: the window that did nothing is told too. + told = await _next_setup_state(other) + assert check(told) + assert told["preset_groups"] == reply["preset_groups"] + finally: + await other.close() + await _stop(task, ws) + + async def test_switching_only_a_chat_tells_no_other_window_about_the_catalog( + self, tmp_path: Path + ) -> None: + workspace = tmp_path / "ws" + workspace.mkdir() + daemon, task, ws = await _seeded(tmp_path) + other = await _second_window(daemon) + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + await _ask( + ws, + {"type": "switch_preset", "name": "budget", "session_id": opened["session_id"]}, + "session_list", + ) + with pytest.raises(asyncio.TimeoutError): + await asyncio.wait_for(_next_setup_state(other), 0.5) + finally: + await other.close() + await _stop(task, ws) diff --git a/core/tstd/config.py b/core/tstd/config.py index 4a8616e8..1a7d40cf 100644 --- a/core/tstd/config.py +++ b/core/tstd/config.py @@ -56,6 +56,13 @@ # declared preset when the configured name is missing. DEFAULT_PRESET = "" +# The picker's built-in sections (TD-5236), in the order it lists them. An +# entry's own ``group`` may be one of these or a name of the person's own. +GROUP_LOCAL = "Local" +GROUP_API = "API" +GROUP_GROK = "Grok" +PICKER_GROUPS: tuple[str, ...] = (GROUP_LOCAL, GROUP_API, GROUP_GROK) + # Implicit keychain account for an unbound remote tier (TD-1717). DEFAULT_CREDENTIAL_ID = "openrouter" RESERVED_CREDENTIAL_IDS = frozenset( @@ -527,13 +534,19 @@ class Preset(BaseModel): label: str | None = Field(default=None, min_length=1, max_length=40) description: str | None = Field(default=None, max_length=120) picker: Literal["primary", "more", "team"] = "primary" + # The picker's section for this entry (TD-5236). None = the daemon says + # Grok, Local or API from the entry itself (``ModelConfig.preset_group``). + group: str | None = Field(default=None, min_length=1, max_length=20) + # A quick-switch slot, 1 to 9 (TD-5236): ⌘⌥ / Ctrl+Alt+ + # picks this entry. None = no shortcut. No two entries share one. + slot: int | None = Field(default=None, ge=1, le=9) # The Fast toggle (TD-5176): None = this model has no fast mode. fast: FastConfig | None = None # Context settings for chats on this model (TD-5234), laid over the # global sections of the same names. None = the global values. overrides: PresetOverrides | None = None - @field_validator("description") + @field_validator("description", "group") @classmethod def _blank_description_is_unset(cls, value: str | None) -> str | None: if value is None: @@ -1788,6 +1801,42 @@ def has_model(self) -> bool: """Whether any model is set up, i.e. the active preset exists.""" return self.active_preset in self.presets + @model_validator(mode="after") + def _slots_are_not_shared(self) -> ModelConfig: + """One shortcut picks one entry (TD-5236).""" + taken: dict[int, str] = {} + for name, preset in self.presets.items(): + if preset.slot is None: + continue + if preset.slot in taken: + raise ValueError( + f"presets.{name}.slot: slot {preset.slot} is already " + f"presets.{taken[preset.slot]}'s; give each entry its own" + ) + taken[preset.slot] = name + return self + + def preset_group(self, name: str) -> str: + """The picker section entry *name* sits in (TD-5236). + + Its own ``group`` when it names one. Otherwise ``Grok`` for an + entry on the Grok engine, ``Local`` when every role is served on + this computer, and ``API`` for the rest. A server on the local + network is not this computer, so it reads ``API`` unless the entry + says ``group: Local``: only the person knows it is theirs. + """ + preset = self.presets.get(name) + if preset is None: + return GROUP_API + if preset.group is not None: + return preset.group + if preset.engine == "grok": + return GROUP_GROK + tiers = preset.tier_map().values() + if tiers and all(is_loopback_url(resolve_base_url(self, tier)) for tier in tiers): + return GROUP_LOCAL + return GROUP_API + def preset_engine(self, name: str) -> Literal["native", "grok"]: """The engine a chat on entry *name* uses: its own, else ``engine.kind`` (TD-5239).""" preset = self.presets.get(name) diff --git a/core/tstd/config_help.py b/core/tstd/config_help.py index fa9982f4..297e9ec2 100644 --- a/core/tstd/config_help.py +++ b/core/tstd/config_help.py @@ -120,6 +120,10 @@ "picker unless it is in use.", "Preset.description": "One line under the label on the picker: what this model is for.", "Preset.picker": "Where the picker lists it: the main list, More models, or Teams.", + "Preset.group": "The picker's section for this one. Empty lets TST Desk choose: Grok, " + "Local when every role is served on this computer, else API.", + "Preset.slot": "A quick-switch slot, 1 to 9: ⌘⌥ and the digit (Ctrl+Alt elsewhere) picks " + "this one. Empty is no shortcut. No two share a slot.", "Preset.fast": "The model's Fast mode. With this set, the picker shows a Fast switch.", "Preset.overrides": "Context settings for chats on this model, in place of the global " "ones of the same names. Nothing here changes approvals, shell rules or keys.", diff --git a/core/tstd/daemon.py b/core/tstd/daemon.py index 9e538edb..65a0cd6d 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -18,6 +18,7 @@ import tempfile import time from collections.abc import Callable, Sequence +from contextvars import ContextVar from dataclasses import dataclass, field from datetime import UTC, datetime, timedelta from pathlib import Path @@ -54,6 +55,7 @@ from .chat_history import restorable_history from .config import ( DEFAULT_CREDENTIAL_ID, + PICKER_GROUPS, ConfigError, CredentialConfig, JudgmentsConfig, @@ -935,6 +937,9 @@ def _shipped_typesafe_base_url() -> str: return url.strip() if isinstance(url, str) else "" +#: The connection whose message is being handled, or None outside a handler. +_ANSWERING: ContextVar[object | None] = ContextVar("tstd_answering", default=None) + #: The line under the label of the entry "Add Grok to your models" writes (TD-5258). GROK_PRESET_DESCRIPTION = "The Grok CLI, with its own model" @@ -1528,6 +1533,11 @@ async def _setup_state_event(self) -> SetupState: preset_engines={ name: p.engine for name, p in sorted(self.config.presets.items()) if p.engine }, + preset_groups={name: self.config.preset_group(name) for name in self.config.presets}, + picker_groups=list(PICKER_GROUPS), + preset_slots={ + name: p.slot for name, p in sorted(self.config.presets.items()) if p.slot + }, has_model=self.config.has_model(), skip_all_approvals=self.skip_all_approvals, load_global_memory=self.load_global_memory, @@ -2178,6 +2188,7 @@ async def _handle_add_model(self, msg: AddModel) -> str: self.config = self.config.model_copy(update={"active_preset": name}) self._slug_snapshot = _snapshot_slugs(self.config) log.info("model added", extra={"extra_fields": {"preset": name}}) + await self._tell_other_windows_the_catalog() return (await self._setup_state_event()).model_dump_json() async def _handle_catalog_edit( @@ -2220,6 +2231,7 @@ async def _handle_catalog_edit( for key in [k for k in self._card_results if k[0] == msg.name]: del self._card_results[key] log.info("model catalog edited", extra={"extra_fields": fields}) + await self._tell_other_windows_the_catalog() return (await self._setup_state_event()).model_dump_json() async def _test_model(self, preset: str | None) -> ModelTested: @@ -3230,7 +3242,14 @@ async def _handle_message(self, raw: str, _connection: Any) -> str | None: Broadcast and replay carry log events, already scrubbed at insertion, so they do not pass through here. """ - reply = await self._dispatch_message(raw, _connection) + # The connection being answered, for a handler that tells the other + # windows what this one's reply already says (TD-5236). A handler + # runs in its connection's own task, so this is that task's value. + token = _ANSWERING.set(_connection) + try: + reply = await self._dispatch_message(raw, _connection) + finally: + _ANSWERING.reset(token) if reply is None: return None # TD-5015: a viewer's session_list carries every computer's rows; @@ -4522,6 +4541,7 @@ async def send(reply: str) -> None: "active preset changed", extra={"extra_fields": {"preset": msg.name}}, ) + await self._tell_other_windows_the_catalog() return (await self._setup_state_event()).model_dump_json() if isinstance(msg, DuplicatePreset | SetPresetLabel | DeletePreset | AddGrokPreset): @@ -5467,6 +5487,7 @@ async def _handle_switch_preset(self, msg: SwitchPreset) -> str: if found is not None: await self._switch_chat(found, msg.name) if msg.make_default: + await self._tell_other_windows_the_catalog() return (await self._setup_state_event()).model_dump_json() return await self._handle_list_sessions() @@ -5776,6 +5797,21 @@ async def _on_device_presence(self, _device_id: str) -> None: state = await self._setup_state_event() await self.ws_server.broadcast(scrub_wire_json(state.model_dump_json()), loopback_only=True) + async def _tell_other_windows_the_catalog(self) -> None: + """A model was added, changed, removed or made the default (TD-5236). + + ``setup_state`` is otherwise only the reply to whoever asked, so a + second window kept showing the old list and the old default until + it reopened Settings. This computer's own windows only, like the + presence refresh above: a phone asks when it needs the list. + """ + state = await self._setup_state_event() + await self.ws_server.broadcast( + scrub_wire_json(state.model_dump_json()), + loopback_only=True, + exclude=_ANSWERING.get(), + ) + def _remember_remote_port(self) -> None: """Save the bound port so the next start reuses it (TD-5005). diff --git a/core/tstd/preset_write.py b/core/tstd/preset_write.py index 83210d15..0ee5b266 100644 --- a/core/tstd/preset_write.py +++ b/core/tstd/preset_write.py @@ -151,6 +151,8 @@ def duplicate_preset(source: str, name: str, label: str, path: Path | None = Non header, end, entry_indent = _presets_block(lines, config_path) body = config.presets[source].model_dump(mode="json", exclude_unset=True) body.pop("label", None) + # A quick-switch slot picks one entry (TD-5236): the copy starts without one. + body.pop("slot", None) block = [ f"{' ' * entry_indent}{name}:", f"{' ' * (entry_indent * 2)}label: {json.dumps(cleaned)}", diff --git a/core/tstd/protocol.py b/core/tstd/protocol.py index 80182457..655b801e 100644 --- a/core/tstd/protocol.py +++ b/core/tstd/protocol.py @@ -3793,6 +3793,13 @@ class SetupState(DaemonEvent): # TD-5239: the engine of each preset that names one. A preset left out # runs on ``engine``. Additive; an older client shows no mark. preset_engines: dict[str, Literal["native", "grok"]] = Field(default_factory=dict) + # TD-5236: the picker section of every preset (its own ``group``, else + # Grok, Local or API as the daemon reads the entry), the order the + # built-in sections come in, and each preset's quick-switch slot, 1 to + # 9, for those that have one. Additive; an older client shows one list. + preset_groups: dict[str, str] = Field(default_factory=dict) + picker_groups: list[str] = Field(default_factory=list) + preset_slots: dict[str, int] = Field(default_factory=dict) has_model: bool = True # TD-804: skip-all approvals. Additive, default off — an older client # that ignores the field keeps asking, which is the safe read. diff --git a/core/tstd/ws.py b/core/tstd/ws.py index 32235d44..5ad98510 100644 --- a/core/tstd/ws.py +++ b/core/tstd/ws.py @@ -414,7 +414,12 @@ async def _ping_loop(self) -> None: await conn.send(frame) async def broadcast( - self, payload: str, *, loopback_only: bool = False, audience: Audience = "all" + self, + payload: str, + *, + loopback_only: bool = False, + audience: Audience = "all", + exclude: object | None = None, ) -> int: """Send one frame to every handshaken client. Returns how many got it. @@ -424,7 +429,9 @@ async def broadcast( rather than to a session, such as the scheduled-job list. *loopback_only* skips remote peers (TD-5014's ``pairing_completed``); it is ``audience="loopback"``. ``"viewers"`` reaches viewers only. - Machine peers never get a broadcast (TD-5015). + Machine peers never get a broadcast (TD-5015). *exclude* is one + connection to leave out: the one whose own reply already says it + (TD-5236). A client that closed between the snapshot and the send is skipped, not an error: it will re-read the state when it reconnects. @@ -433,6 +440,8 @@ async def broadcast( if loopback_only: audience = "loopback" for conn in list(self._handshaken): + if conn is exclude: + continue peer = self.peer_view(conn) if peer.machine or (audience == "viewers" and not peer.viewer): continue diff --git a/docs/configuration.md b/docs/configuration.md index 6857142c..c05d413a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1982,6 +1982,8 @@ it may leave all three out. It is all three or none. | `label` | string | The name the composer's model picker, its chip and the account row show for this preset. A preset with no label is left out of the picker (unless it is the current one). Default: none. | | `description` | string, up to 120 characters | One line under the label in the picker: what this model is for. Default: none. | | `picker` | `primary` \| `more` \| `team` | `primary` puts the preset on the picker's main list; `more` puts it under "More models"; `team` (a preset whose tiers use different models) puts it under "Teams", a submenu shown only when one exists. Default `primary`. | +| `group` | string, up to 20 characters | The section of the picker's main list this preset sits in (TD-5236). Default: none, and TST Desk then chooses: `Grok` for a preset on the Grok engine, `Local` when every tier is served on this computer (after a bound key's host is taken into account), else `API`. A server elsewhere on your network reads `API` until you write `group: Local`. Any other name makes a section of its own, listed after those three. With one section in all, the picker shows no headings. | +| `slot` | 1 to 9 | A quick-switch slot (TD-5236): ⌘⌥ and that digit on macOS, Ctrl+Alt and that digit elsewhere, does what a click on this preset's row in the picker does. Default: none. Two presets with the same slot are a load error naming both. A copy made with Duplicate does not take its source's slot. | | `fast` | map | The model's Fast mode: the picker shows a Fast switch under Effort only for a preset with this block. `hook_url` is a loopback switch for a local model that gets faster by borrowing hardware: the daemon POSTs `{"model": hook_model, "on": true\|false, "by": "tst-desk"}` and reads the reply (409 with `error` = it will not switch now, and the switch stays off); at the start of each turn it GETs the same URL and turns the toggle off if the switch already ended Fast. `hook_model` names this model at that switch. `extra_body` is merged into every request while Fast is on, for an API model with a priority or fast tier. Either or both. `note` is one line the switch shows while it turns on, e.g. "Borrows all four GPUs; about 2 min to switch". `hook_url` must be loopback. Default: none. The Ultracode switch after Fast is not a preset key: every model has it (§2 `session` `default_ultracode`, §2 `workflow`). | | `overrides` | map | Context settings for chats on this model, in place of the global ones of the same names (TD-5234). Six sections may appear, each with any of the keys listed: `session` (`default_effort`, `default_ultracode`), `clear_tool_results` (`enabled`, `keep_recent_tool_results`, `clear_at_least_tokens`, `exclude_tools`), `compaction` (`summary`, `summary_prompt`, `summary_max_tokens`, `summary_timeout_seconds`, `background_percent`, `reinject_files`, `reinject_tokens`), `consult` (`tiers`, `max_per_turn`), `workflow` (`max_parallel`, `max_tasks`) and `computer_use` (`max_prompt_images`). Each key means what it means in its §2 section and has the same limits. A key the model sets wins; a key left out, or `null`, keeps the global value; a list replaces the global list whole. A chat takes these when it is opened on the model or switched to it, like the model itself, and keeps them until it is switched again. Switching a chat to a model that sets `session.default_effort` or `session.default_ultracode` sets that chat's effort or Ultracode switch to it; a model that leaves them out leaves the chat's own choice alone. Any other section or key is an error at load: approvals, shell rules, MCP servers, hooks, remote attach, notifications, autonomy and keys cannot be set per model. Default: none. | diff --git a/docs/models.md b/docs/models.md index e723f781..48b2668d 100644 --- a/docs/models.md +++ b/docs/models.md @@ -24,6 +24,40 @@ Click it to open the picker: Before any model is set up, the chip itself reads **Add a model**. +With models of more than one kind, the main list is in sections: **Local** +(served on this computer), **API** (a provider's server) and **Grok**. +TST Desk reads the section off each entry; `group:` in the entry sets it by +hand, which is how a server elsewhere on your own network gets under +Local, or how you make a section of your own. + +Give an entry `slot: 1` (up to 9) and ⌘⌥1 on macOS, or Ctrl+Alt+1 +elsewhere, picks it from anywhere in the window, exactly as a click on its +row would. The digit shows beside the model in the picker. + + +```yaml +presets: + house: + label: House server + description: The GPU box on my network + group: Local + slot: 1 + brain: &house + slug: house/model + base_url: http://192.168.1.20:8000/v1 + credential: my-provider + context_window: 65536 + max_output_tokens: 8192 + input_price: 0.0 + output_price: 0.0 + cache_read_price: 0.0 + worker: *house + validator: *house +credentials: + my-provider: + name: My provider +``` + Next to the chip, **Plan** turns on plan mode: the agent looks around without changing anything, then shows you a plan. Nothing runs until you approve it. diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 7f063a42..b46e8c9d 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13593,3 +13593,42 @@ event log awaits its subscribers before the loop takes its next message, which is what makes binding at `turn_complete` safe. Class B entry in DECISIONS.md. +--- + +### TD-5236 — Local, API and Grok sections in the picker, and a shortcut per model + +**Size:** 3 · **Depends on:** TD-5235 · **Status:** Done + +The picker was one flat list, so a local model, a provider's and Grok read +as the same kind of thing, and switching always took the mouse. A second +open window also kept showing the old list and the old default after the +first one changed them. + +- [x] `setup_state.preset_groups` gives every entry a section: its own + `group`, else Grok for the Grok engine, Local when every role is served + on this computer (after a bound key's host), else API + (`test_picker_groups.py::TestTheSectionOfAnEntry`). +- [x] A server on the local network reads API until the entry says + `group: Local` (`test_without_saying_so_a_server_on_the_network_is_api`, + `test_an_entry_can_say_its_own`). +- [x] An entry may have `slot` 1 to 9; two entries with one slot are a load + error naming both; a copy does not take its source's slot + (`TestSlots`). +- [x] The picker lists Local, API, Grok, then other sections by name, with + no headings when there is one section; each slot shows beside its + model (`model-sections.test.ts`). +- [x] ⌘⌥1…9 / Ctrl+Alt+1…9 does what a click on that model's row does, + read off the physical key, not over a modal, and without taking ⌘1…9 + from the session slots (`model-sections.test.ts`). +- [x] A model added, relabelled, removed or made the default in one window + reaches this computer's other windows unasked; switching only a chat + does not (`test_a_change_in_one_window_reaches_the_other`, + `test_switching_only_a_chat_tells_no_other_window_about_the_catalog`). +- [x] `ModelPicker.svelte` is under 400 lines; the row is `ModelRow.svelte` + and what a pick does is `model-pick.ts`. + +**Notes:** The window never infers a section from a URL: the daemon sends +it. Other Settings writes to the active entry (a role's model, key or +window) still reach only the window that made them. Class B entry in +DECISIONS.md. + diff --git a/ui/src/lib/components/AppShell.svelte b/ui/src/lib/components/AppShell.svelte index cf14578f..84e629b2 100644 --- a/ui/src/lib/components/AppShell.svelte +++ b/ui/src/lib/components/AppShell.svelte @@ -91,7 +91,8 @@ import { startShellJobs } from '../shell-jobs-store.svelte.js'; import { startGoals } from '../goal-store.svelte.js'; import { startVoice } from '../voice.svelte.js'; - import { resolveShortcut, sessionSlot } from '../shortcuts'; + import { modelSlot, resolveShortcut, sessionSlot } from '../shortcuts'; + import { pickSlot } from '../model-pick'; import { sessionIdAtSlot, stepSessionId } from '../rail'; import { sessionOrderAcross } from '../machines'; import { machines } from '../machines.svelte.js'; @@ -140,6 +141,7 @@ ctrlKey: event.ctrlKey, shiftKey: event.shiftKey, altKey: event.altKey, + code: event.code, }, { workspaceMenuOpen: workspaces.menuOpen, @@ -177,6 +179,17 @@ }); const id = slot === null ? null : sessionIdAtSlot(sessionOrder(), slot); if (id !== null) selectRow(id); + } else if (action === 'pick-model') { + // TD-5236: the same as a click on that model's row in the picker. + const slot = modelSlot({ + key: event.key, + code: event.code, + metaKey: event.metaKey, + ctrlKey: event.ctrlKey, + shiftKey: event.shiftKey, + altKey: event.altKey, + }); + if (slot !== null) pickSlot(slot); } else openSettings(); } diff --git a/ui/src/lib/components/chat/ModelPicker.svelte b/ui/src/lib/components/chat/ModelPicker.svelte index 6467cf4a..0db54384 100644 --- a/ui/src/lib/components/chat/ModelPicker.svelte +++ b/ui/src/lib/components/chat/ModelPicker.svelte @@ -18,22 +18,13 @@ EFFORT_OPTIONS, effortLabel, groupModels, - modelDescription, modelLabel, } from "../../effort"; import { MODELS_DOC_URL, openDoc } from "../../docs-links"; - import { engineClash, engineMark } from "../../model-engine"; import { openAddModel } from "../../onboarding.svelte.js"; - import { isBridged } from "../../bridged-id"; - import { - session, - setEffort, - setFast, - setUltracode, - switchModel, - useForNewChats, - } from "../../session-status.svelte.js"; - import { newSession } from "../../sessions.svelte.js"; + import ModelRow from "./ModelRow.svelte"; + import { sectionModels } from "../../model-sections"; + import { session, setEffort, setFast, setUltracode } from "../../session-status.svelte.js"; import { settings } from "../../settings.svelte.js"; type View = "main" | "effort" | "more" | "teams"; @@ -50,38 +41,11 @@ current, }), ); - const locked = $derived(session.turnActive); - // A chat on another computer switches through its hub, which still - // refuses while a turn runs (TD-5235 keeps the older verb as it was). - const bridged = $derived(isBridged(session.sessionId)); - - const BUSY = "Cannot change model while a turn is running"; - const AFTER_TURN = "Applies when this turn finishes"; - const NEW_CHAT = "Opens a new chat"; - - // TD-5239: a model on another engine than this chat's cannot be switched - // to. TD-5235: its row opens a new chat on it instead. - function clash(name: string): boolean { - return ( - name !== current && engineClash(name, settings.presetEngines, session.engine) !== null - ); - } - - function held(name: string): boolean { - return name !== current && locked && bridged; - } - - function hint(name: string): string { - if (name === current) return ""; - const reason = engineClash(name, settings.presetEngines, session.engine); - if (reason !== null) return `${NEW_CHAT}. ${reason}`; - if (held(name)) return BUSY; - return locked ? AFTER_TURN : ""; - } - - function label(name: string): string { - return modelLabel(name, settings.presetLabels); - } + // TD-5236: the main list in sections (Local, API, Grok, then a model's + // own). The daemon says which section each model is in. + const sections = $derived( + sectionModels(groups.main, settings.presetGroups, settings.pickerGroups), + ); function close(): void { open = false; @@ -107,24 +71,6 @@ void openDoc(MODELS_DOC_URL); } - // TD-5235: a click moves this chat and makes the model the one new chats - // use, so the next new chat does not go back to the old one. A model on - // the other engine cannot move this chat, so it opens a new one. - function pickModel(name: string): void { - if (clash(name)) { - useForNewChats(name); - newSession(name); - } else if (name !== current || name !== settings.activePreset) { - switchModel(name, { onlyThisChat: false }); - } - close(); - } - - function pickOnlyHere(name: string): void { - switchModel(name, { onlyThisChat: true }); - close(); - } - // Close on a click outside or Escape (Escape steps back from a submenu first). $effect(() => { if (!open) return; @@ -148,42 +94,6 @@ }); -{#snippet row(name: string)} -
- - {#if name !== current && !clash(name) && !bridged} - - {/if} -
-{/snippet} - {#if open} @@ -394,33 +309,10 @@ outline-offset: -1px; } - .row { - display: flex; - align-items: center; - } - - .row .item { - flex: 1; - min-width: 0; - } - - .only { - flex: none; - font-family: var(--font-sans); + .section { + padding: var(--space-2) var(--space-2) var(--space-1); font-size: var(--text-xs); color: var(--color-ink-muted); - background: transparent; - border: none; - border-radius: var(--radius-sm); - padding: var(--space-1) var(--space-2); - cursor: pointer; - white-space: nowrap; - } - - .only:hover, - .only:focus-visible { - color: var(--color-ink); - background: var(--color-sunken); } .pending-tag { diff --git a/ui/src/lib/components/chat/ModelRow.svelte b/ui/src/lib/components/chat/ModelRow.svelte new file mode 100644 index 00000000..09576629 --- /dev/null +++ b/ui/src/lib/components/chat/ModelRow.svelte @@ -0,0 +1,166 @@ + + +
+ + {#if offersOnlyHere(name)} + + {/if} +
+ + diff --git a/ui/src/lib/model-pick.ts b/ui/src/lib/model-pick.ts new file mode 100644 index 00000000..8e294240 --- /dev/null +++ b/ui/src/lib/model-pick.ts @@ -0,0 +1,73 @@ +// TD-5235, TD-5236: what picking a model does, in one place. The picker's +// rows and the quick-switch shortcut (⌘⌥1…9, Ctrl+Alt+1…9) both come here, +// so a key press and a click mean the same thing. + +import { isBridged } from "./bridged-id"; +import { engineClash } from "./model-engine"; +import { modelAtSlot } from "./model-sections"; +import { session, switchModel, useForNewChats } from "./session-status.svelte.js"; +import { newSession } from "./sessions.svelte.js"; +import { settings } from "./settings.svelte.js"; + +export const BUSY = "Cannot change model while a turn is running"; +export const AFTER_TURN = "Applies when this turn finishes"; +export const NEW_CHAT = "Opens a new chat"; + +/** A chat on another computer switches through its hub, which keeps the + * older verb and its rules. */ +function bridged(): boolean { + return isBridged(session.sessionId); +} + +/** TD-5239: `name` is on another engine than this chat's, so the chat + * cannot move to it. TD-5235: its row opens a new chat on it instead. */ +export function opensNewChat(name: string): boolean { + return ( + name !== session.preset && + engineClash(name, settings.presetEngines, session.engine) !== null + ); +} + +/** The row cannot be used right now: another computer's chat, mid-turn. */ +export function held(name: string): boolean { + return name !== session.preset && session.turnActive && bridged(); +} + +/** Whether "Only this chat" is offered for `name`. */ +export function offersOnlyHere(name: string): boolean { + return name !== session.preset && !opensNewChat(name) && !bridged(); +} + +/** The row's tooltip: why it is held, or what the click will do. */ +export function pickHint(name: string): string { + if (name === session.preset) return ""; + const reason = engineClash(name, settings.presetEngines, session.engine); + if (reason !== null) return `${NEW_CHAT}. ${reason}`; + if (held(name)) return BUSY; + return session.turnActive ? AFTER_TURN : ""; +} + +/** A click, or the model's shortcut: this chat moves to `name` and new + * chats use it too. A model on the other engine opens a new chat. */ +export function pickModel(name: string): void { + if (held(name)) return; + if (opensNewChat(name)) { + useForNewChats(name); + newSession(name); + } else if (name !== session.preset || name !== settings.activePreset) { + switchModel(name, { onlyThisChat: false }); + } +} + +/** "Only this chat": the model new chats use stays as it is. */ +export function pickOnlyHere(name: string): void { + if (offersOnlyHere(name)) switchModel(name, { onlyThisChat: true }); +} + +/** The quick-switch shortcut. False when no model has that slot. */ +export function pickSlot(slot: number): boolean { + const name = modelAtSlot(slot, settings.presetSlots); + if (name === null) return false; + pickModel(name); + return true; +} diff --git a/ui/src/lib/model-sections.test.ts b/ui/src/lib/model-sections.test.ts new file mode 100644 index 00000000..450f6ea0 --- /dev/null +++ b/ui/src/lib/model-sections.test.ts @@ -0,0 +1,206 @@ +// @vitest-environment jsdom +// +// TD-5236: the picker's main list in sections, and the quick-switch slots. +// The daemon says which section each model is in and which slot it has; a +// slot's shortcut does what a click on that model's row does. + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, tick, unmount } from "svelte"; +import type { ClientMessageUnion } from "./protocol"; + +const mocks = vi.hoisted(() => ({ newChats: [] as (string | undefined)[] })); + +vi.mock("./sessions.svelte.js", () => ({ + newSession: (preset?: string) => { + mocks.newChats.push(preset); + return true; + }, +})); + +import ModelPicker from "./components/chat/ModelPicker.svelte"; +import type { ProtocolClient } from "./client"; +import { pickSlot } from "./model-pick"; +import { modelAtSlot, sectionModels } from "./model-sections"; +import { bindClient, resetSession, session } from "./session-status.svelte.js"; +import { resetSettings, settings } from "./settings.svelte.js"; +import { modelSlot, resolveShortcut, sessionSlot } from "./shortcuts"; + +const ORDER = ["Local", "API", "Grok"]; +let app: ReturnType | null = null; +let sent: ClientMessageUnion[] = []; + +beforeEach(() => { + resetSession(); + resetSettings(); + sent = []; + mocks.newChats = []; + bindClient({ + send: (msg: ClientMessageUnion) => (sent.push(msg), true), + } as unknown as ProtocolClient); + session.sessionId = "s1"; + session.preset = "local"; + session.engine = "native"; + settings.hasModel = true; + settings.activePreset = "local"; + settings.presets = ["api", "grok", "lab", "local"]; + settings.presetLabels = { api: "Hosted", grok: "Grok Build", lab: "Lab box", local: "Laptop" }; + settings.presetEngines = { grok: "grok" }; + settings.presetGroups = { api: "API", grok: "Grok", lab: "Trying out", local: "Local" }; + settings.pickerGroups = ORDER; + settings.presetSlots = { local: 1, api: 2, grok: 3 }; +}); + +afterEach(async () => { + if (app !== null) { + await unmount(app); + app = null; + } + document.body.replaceChildren(); + bindClient(null); + resetSession(); + resetSettings(); +}); + +describe("sectionModels", () => { + it("puts the built-in sections first, in the daemon's order, then the rest by name", () => { + const groups = { a: "Grok", b: "Zeta", c: "API", d: "Local", e: "Alpha", f: "API" }; + expect(sectionModels(["a", "b", "c", "d", "e", "f"], groups, ORDER)).toEqual([ + { title: "Local", names: ["d"] }, + { title: "API", names: ["c", "f"] }, + { title: "Grok", names: ["a"] }, + { title: "Alpha", names: ["e"] }, + { title: "Zeta", names: ["b"] }, + ]); + }); + + it("gives a single section no heading", () => { + expect(sectionModels(["a", "b"], { a: "API", b: "API" }, ORDER)).toEqual([ + { title: "", names: ["a", "b"] }, + ]); + }); + + it("lists everything once when the daemon sent no sections", () => { + expect(sectionModels(["a", "b"], {}, [])).toEqual([{ title: "", names: ["a", "b"] }]); + }); + + it("keeps a model the daemon gave no section, ahead of the titled ones", () => { + expect(sectionModels(["a", "b"], { b: "API" }, ORDER)).toEqual([ + { title: "", names: ["a"] }, + { title: "API", names: ["b"] }, + ]); + }); +}); + +describe("the model slot chord", () => { + const press = (over: Partial[0]>) => ({ + key: "1", + code: "Digit1", + metaKey: true, + ctrlKey: false, + altKey: true, + shiftKey: false, + ...over, + }); + const idle = { workspaceMenuOpen: false, paletteOpen: false, modalOpen: false, turnLive: false }; + + it("is ⌘⌥ or Ctrl+Alt and a digit, read off the physical key", () => { + expect(modelSlot(press({}))).toBe(1); + expect(modelSlot(press({ metaKey: false, ctrlKey: true, code: "Digit9", key: "9" }))).toBe(9); + // On a Mac, Option+1 types "¡": the key no longer says which digit. + expect(modelSlot(press({ key: "¡" }))).toBe(1); + expect(modelSlot(press({ code: undefined, key: "4" }))).toBe(4); + }); + + it("is nothing without Option, with Shift, or off the digit row", () => { + expect(modelSlot(press({ altKey: false }))).toBeNull(); + expect(modelSlot(press({ shiftKey: true }))).toBeNull(); + expect(modelSlot(press({ metaKey: false }))).toBeNull(); + expect(modelSlot(press({ code: "Digit0", key: "0" }))).toBeNull(); + expect(modelSlot(press({ code: "KeyA", key: "a" }))).toBeNull(); + }); + + it("does not take the session slots, and they do not take it", () => { + expect(resolveShortcut(press({}), idle)).toBe("pick-model"); + expect(sessionSlot(press({}))).toBeNull(); + expect(resolveShortcut(press({ altKey: false }), idle)).toBe("pick-session"); + }); + + it("does nothing over a modal", () => { + expect(resolveShortcut(press({}), { ...idle, modalOpen: true })).toBeNull(); + }); +}); + +describe("a slot's shortcut", () => { + it("names the model in that slot", () => { + expect(modelAtSlot(2, settings.presetSlots)).toBe("api"); + expect(modelAtSlot(7, settings.presetSlots)).toBeNull(); + }); + + it("does what a click on that model does", () => { + expect(pickSlot(2)).toBe(true); + expect(sent).toEqual([ + { type: "switch_preset", name: "api", session_id: "s1", make_default: true }, + ]); + }); + + it("opens a new chat for a model on the other engine", () => { + expect(pickSlot(3)).toBe(true); + expect(sent).toEqual([{ type: "switch_preset", name: "grok", make_default: true }]); + expect(mocks.newChats).toEqual(["grok"]); + }); + + it("does nothing for the model already in use, or an empty slot", () => { + expect(pickSlot(1)).toBe(true); + expect(pickSlot(7)).toBe(false); + expect(sent).toEqual([]); + }); +}); + +describe("the picker", () => { + async function openPicker(): Promise { + app = mount(ModelPicker, { target: document.body }); + await tick(); + document.querySelector('[aria-label="Model and effort"]')!.click(); + flushSync(); + return document.querySelector('[role="menu"]')!; + } + + it("lists the models under Local, API, Grok, then a model's own section", async () => { + const menu = await openPicker(); + // A label's own text, without the engine mark that sits inside it. + const own = (el: Element): string => (el.firstChild?.textContent ?? "").trim(); + const seen = [...menu.querySelectorAll(".section, [role='menuitemradio'] .label")].map( + (el) => (el.classList.contains("section") ? `# ${el.textContent}` : own(el)), + ); + expect(seen).toEqual([ + "# Local", + "Laptop", + "# API", + "Hosted", + "# Grok", + "Grok Build", + "# Trying out", + "Lab box", + ]); + }); + + it("shows each model's slot beside it", async () => { + const menu = await openPicker(); + const slots = [...menu.querySelectorAll("kbd.slot")].map((el) => [ + el.textContent, + el.title, + ]); + expect(slots).toEqual([ + ["1", "Quick switch: ⌘⌥1 (Ctrl+Alt+1)"], + ["2", "Quick switch: ⌘⌥2 (Ctrl+Alt+2)"], + ["3", "Quick switch: ⌘⌥3 (Ctrl+Alt+3)"], + ]); + }); + + it("shows no headings when every model is in one section", async () => { + settings.presetGroups = { api: "API", grok: "API", lab: "API", local: "API" }; + const menu = await openPicker(); + expect(menu.querySelectorAll(".section")).toHaveLength(0); + expect(menu.querySelectorAll('[role="menuitemradio"]')).toHaveLength(4); + }); +}); diff --git a/ui/src/lib/model-sections.ts b/ui/src/lib/model-sections.ts new file mode 100644 index 00000000..107aa953 --- /dev/null +++ b/ui/src/lib/model-sections.ts @@ -0,0 +1,44 @@ +// TD-5236: the picker's main list in sections: Local, API, Grok, then any +// section a model names itself. The daemon says which section each model is +// in (`setup_state.preset_groups`) and the order of the built-in ones; this +// only arranges what it was sent. + +export interface ModelSection { + /** The section's heading, or "" when the list has a single section and + * so needs none. */ + title: string; + names: string[]; +} + +/** `names` in sections. Built-in sections come first, in the daemon's + * order; others follow by name. A model the daemon gave no section (an + * older daemon) goes in an untitled one at the top. One section in all + * means no headings: a short list stays the plain list it was. */ +export function sectionModels( + names: readonly string[], + groups: Readonly>, + order: readonly string[], +): ModelSection[] { + const bySection = new Map(); + for (const name of names) { + const title = groups[name] ?? ""; + bySection.set(title, [...(bySection.get(title) ?? []), name]); + } + const rank = (title: string): number => { + if (title === "") return -1; + const at = order.indexOf(title); + return at === -1 ? order.length : at; + }; + const sections = [...bySection.entries()] + .sort(([a], [b]) => rank(a) - rank(b) || a.localeCompare(b)) + .map(([title, members]) => ({ title, names: members })); + return sections.length === 1 ? [{ title: "", names: sections[0].names }] : sections; +} + +/** The model in quick-switch slot `slot`, or null. */ +export function modelAtSlot( + slot: number, + slots: Readonly>, +): string | null { + return Object.keys(slots).find((name) => slots[name] === slot) ?? null; +} diff --git a/ui/src/lib/protocol-fixtures.json b/ui/src/lib/protocol-fixtures.json index 1a3b0e57..959be0ef 100644 --- a/ui/src/lib/protocol-fixtures.json +++ b/ui/src/lib/protocol-fixtures.json @@ -2213,6 +2213,9 @@ "budget" ], "preset_engines": {}, + "preset_groups": {}, + "picker_groups": [], + "preset_slots": {}, "has_model": true, "skip_all_approvals": false, "load_global_memory": false, diff --git a/ui/src/lib/protocol.ts b/ui/src/lib/protocol.ts index 1f412a17..08628a1a 100644 --- a/ui/src/lib/protocol.ts +++ b/ui/src/lib/protocol.ts @@ -2293,6 +2293,11 @@ export interface SetupState extends DaemonEvent { team_presets?: string[]; /** TD-5239: the engine of each preset that names one. */ preset_engines?: Record; + /** TD-5236: every preset's picker section, the order the built-in + * sections come in, and each preset's quick-switch slot (1 to 9). */ + preset_groups?: Record; + picker_groups?: string[]; + preset_slots?: Record; has_model?: boolean; // TD-804: skip-all approvals. Additive, default off. skip_all_approvals?: boolean; diff --git a/ui/src/lib/settings.svelte.ts b/ui/src/lib/settings.svelte.ts index 60b32b85..18ba1856 100644 --- a/ui/src/lib/settings.svelte.ts +++ b/ui/src/lib/settings.svelte.ts @@ -98,6 +98,11 @@ export const settings = $state({ teamPresets: [] as string[], // TD-5239: the engine of each model that names one. presetEngines: {} as Record, + // TD-5236: each model's picker section, the built-in sections in order, + // and each model's quick-switch slot. All as the daemon sent them. + presetGroups: {} as Record, + pickerGroups: [] as string[], + presetSlots: {} as Record, /** False until a model is set up: the first run starts "Add a model". */ hasModel: true, activePreset: null as string | null, @@ -235,6 +240,9 @@ export function resetSettings(): void { settings.presetDescriptions = {}; settings.teamPresets = []; settings.presetEngines = {}; + settings.presetGroups = {}; + settings.pickerGroups = []; + settings.presetSlots = {}; settings.hasModel = true; settings.activePreset = null; settings.tierSlugs = {}; @@ -415,6 +423,9 @@ function reduce(event: DaemonEventUnion): void { settings.presetDescriptions = event.preset_descriptions ?? {}; settings.teamPresets = event.team_presets ?? []; settings.presetEngines = event.preset_engines ?? {}; + settings.presetGroups = event.preset_groups ?? {}; + settings.pickerGroups = event.picker_groups ?? []; + settings.presetSlots = event.preset_slots ?? {}; settings.hasModel = event.has_model ?? true; settings.activePreset = event.active_preset; // A daemon too old to send the field leaves the model section empty diff --git a/ui/src/lib/shortcuts.ts b/ui/src/lib/shortcuts.ts index 68c95488..ab67e079 100644 --- a/ui/src/lib/shortcuts.ts +++ b/ui/src/lib/shortcuts.ts @@ -13,6 +13,9 @@ export interface ShortcutEvent { shiftKey?: boolean; /** ⌘⌥↑ / ⌘⌥↓ step between sessions (navigation round, 2026-09). */ altKey?: boolean; + /** The physical key (`Digit1`). Option changes what a digit key types on + * a Mac, so the model slots read this (TD-5236). */ + code?: string; } export interface ShortcutContext { @@ -37,7 +40,8 @@ export type ShortcutAction = | "stop-computer-use" | "prev-session" | "next-session" - | "pick-session"; + | "pick-session" + | "pick-model"; /** The 1-based rail slot a ⌘1…⌘9 / Ctrl+1…9 press names, or null. Shift * and Option are excluded so ⌘⇧1-style system combos and the characters @@ -49,6 +53,17 @@ export function sessionSlot(event: ShortcutEvent): number | null { return Number(event.key); } +/** The 1-based model slot a ⌘⌥1…⌘⌥9 / Ctrl+Alt+1…9 press names, or null + * (TD-5236). Option is what sets it apart from ⌘1…9, the session slots. + * Read from `code`, with `key` for an event that has none: on a Mac, + * Option+1 types "¡". Shift stays out of it. */ +export function modelSlot(event: ShortcutEvent): number | null { + if (!(event.metaKey || event.ctrlKey) || event.altKey !== true) return null; + if (event.shiftKey === true) return null; + const digit = /^Digit([1-9])$/.exec(event.code ?? "")?.[1] ?? event.key; + return /^[1-9]$/.test(digit) ? Number(digit) : null; +} + /** Map a keydown to one app-level action, or null when nothing applies. */ export function resolveShortcut( event: ShortcutEvent, @@ -91,6 +106,9 @@ export function resolveShortcut( if (event.key === "ArrowDown") return "next-session"; if (event.key === "ArrowUp") return "prev-session"; } + // ⌘⌥1…⌘⌥9 — the model in that quick-switch slot (TD-5236). Not over a + // modal: the chat it would switch is behind it. + if (modelSlot(event) !== null) return context.modalOpen ? null : "pick-model"; // ⌘1…⌘9 — straight to a rail slot, counted from the top. The shell // reads the digit back with `sessionSlot`. if (sessionSlot(event) !== null) return "pick-session"; From 015091fa6fbcd76835832bed6c86a6f47a054801 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 18:29:40 +0000 Subject: [PATCH 08/35] TD-5240: the picker says when a model cannot run yet A switch succeeded and the next message then failed: the local server was not started, the key was never stored, the Grok CLI was not installed. check_presets is answered with preset_readiness, one row per entry: ready, no_key, server_down or no_grok, and one sentence. A key is checked by keychain presence only. A role whose resolved URL is on this computer is asked for /models with no key (local_servers.loopback_answers, beside the "Find models on this computer" scan and under its rules); any reply counts as there. A provider's server is never contacted. An entry on the Grok engine is checked for the CLI only. The picker asks each time it opens and shows a model's problem under its name. It is a warning, not a gate: the row can still be picked. Class B: recorded in DECISIONS.md. Co-Authored-By: Claude Fable 5.2 --- DECISIONS.md | 25 +++ core/scripts/generate_protocol_fixtures.py | 15 ++ core/tests/test_preset_ready.py | 195 ++++++++++++++++++ core/tstd/daemon.py | 16 +- core/tstd/local_servers.py | 36 ++++ core/tstd/preset_ready.py | 98 +++++++++ core/tstd/protocol.py | 38 ++++ docs/architecture.md | 2 + docs/models.md | 6 + docs/tst-desk-backlog.md | 32 +++ ui/src/lib/client.ts | 1 + ui/src/lib/components/chat/ModelPicker.svelte | 22 +- ui/src/lib/components/chat/ModelRow.svelte | 12 +- ui/src/lib/model-ready.svelte.ts | 36 ++++ ui/src/lib/model-ready.test.ts | 133 ++++++++++++ ui/src/lib/protocol-fixtures.json | 19 ++ ui/src/lib/protocol.ts | 22 ++ 17 files changed, 696 insertions(+), 12 deletions(-) create mode 100644 core/tests/test_preset_ready.py create mode 100644 core/tstd/preset_ready.py create mode 100644 ui/src/lib/model-ready.svelte.ts create mode 100644 ui/src/lib/model-ready.test.ts diff --git a/DECISIONS.md b/DECISIONS.md index 6120eee7..473683ab 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -20029,3 +20029,28 @@ person's own machine); a broadcast to phones as well (a phone asks when it shows the list, and `setup_state` for a remote peer is trimmed per connection, which a broadcast would bypass); slots without Option (taken). +## 2026-11-20 — TD-5240: readiness is three local checks, asked when the picker opens, and never a gate (Class B) + +**Decision:** `check_presets` (no fields) is answered with +`preset_readiness`: per entry, `ready`, `no_key`, `server_down` or +`no_grok` and one sentence. A key is checked by keychain presence. A role +whose resolved URL is loopback is asked for `/models` with no key and a 1.5 s +limit, and any HTTP reply counts. A provider's server is not contacted. An +entry on the Grok engine is checked for the CLI only. The window sends it +each time the picker opens and shows the sentence on the row; it never +disables the row. + +**Rationale:** these are the three causes of "the switch worked and the +next message failed" that can be known without spending anything or +sending a key. Asking a provider would be a request to a third party the +person did not make (directive 3), and a key's presence is what can be +known without one. Opening the picker is the person's act, which is what +makes the loopback ask theirs too, as "Find models on this computer" is +(TD-5180). It is a warning because the check can be wrong in the harmless +direction: a server that is still loading answers nothing for a moment. + +**Rejected:** checking as part of `switch_preset` and refusing the switch +(a model whose server is starting could not be selected ahead of time); +probing on a timer (traffic nobody asked for); validating a key with a live +call (that is the existing Test button's job, on a click). + diff --git a/core/scripts/generate_protocol_fixtures.py b/core/scripts/generate_protocol_fixtures.py index 86bb77fe..2e1db06a 100644 --- a/core/scripts/generate_protocol_fixtures.py +++ b/core/scripts/generate_protocol_fixtures.py @@ -50,6 +50,7 @@ CharterDocument, CheckCuPermissions, CheckpointNotice, + CheckPresets, CloseWorktree, CommandEntry, CommandList, @@ -189,6 +190,8 @@ PlanReviewed, PolicyRules, PolicyRuleSummary, + PresetReadiness, + PresetReady, PreviewRestoreFiles, Question, QuestionAnswer, @@ -593,6 +596,7 @@ name="tst-default", label="House model", description="General coding and chat" ), "delete_preset": DeletePreset(name="budget"), + "check_presets": CheckPresets(), # TD-5235: this chat, the model new chats use, or both "switch_preset": SwitchPreset(name="budget", session_id="sess-1", make_default=True), # TD-5258: put the Grok CLI on the model list @@ -1637,6 +1641,17 @@ error="The server refused the key. Check it and try again.", ), "model_tested": ModelTested(preset="tst-default", ok=True, reply="I am working."), + # TD-5240: whether each model on the picker can run right now + "preset_readiness": PresetReadiness( + rows=[ + PresetReady(preset="tst-default", status="ready"), + PresetReady( + preset="local", + status="server_down", + detail="Nothing is answering at 127.0.0.1:11434. Start the model server.", + ), + ] + ), # TD-5203: a two-model slice of the editor's schema, and an edit's ack. "config_editor": ConfigEditor( root="ModelConfig", diff --git a/core/tests/test_preset_ready.py b/core/tests/test_preset_ready.py new file mode 100644 index 00000000..189798c3 --- /dev/null +++ b/core/tests/test_preset_ready.py @@ -0,0 +1,195 @@ +"""Whether each model on the picker can run right now (TD-5240). + +Three things can be known without sending a key or asking a provider: the +key is in the keychain, a server on this computer answers, the Grok CLI is +there. The picker shows the first one that is not true. +""" + +from __future__ import annotations + +from collections.abc import Mapping +from pathlib import Path +from typing import Any + +import httpx +import pytest + +from tests.test_add_model import _ask, _start, _stop +from tests.test_preset_engine import _text as _catalog_with_grok +from tstd.config import ModelConfig, load_config +from tstd.local_servers import loopback_answers +from tstd.mock import MockProvider, Script +from tstd.peer_policy import MACHINE_PEER_VERBS +from tstd.preset_ready import check_presets, loopback_urls + +OLLAMA = "http://127.0.0.1:11434/v1" +VLLM = "http://127.0.0.1:8000/v1" + + +def _config(tmp_path: Path) -> ModelConfig: + path = tmp_path / "config.yaml" + path.write_text(_catalog_with_grok(), encoding="utf-8") + return load_config(path) + + +async def _rows( + config: ModelConfig, + *, + stored: set[str] | None = None, + down: set[str] | None = None, + grok: bool = True, +) -> dict[str, tuple[str, str]]: + asked_keys: list[str] = [] + asked_urls: list[str] = [] + + async def key_stored(credential: str) -> bool: + asked_keys.append(credential) + return credential in (stored if stored is not None else {"openrouter"}) + + async def answers(urls: list[str]) -> Mapping[str, bool]: + asked_urls.extend(urls) + return {url: url not in (down or set()) for url in urls} + + rows = await check_presets(config, key_stored=key_stored, answers=answers, grok_found=grok) + assert [row.preset for row in rows] == list(config.presets) + # Only servers on this computer are ever asked. + assert all(url.startswith("http://127.0.0.1") for url in asked_urls) + return {row.preset: (row.status, row.detail) for row in rows} + + +class TestTheChecks: + async def test_everything_in_place_is_ready(self, tmp_path: Path) -> None: + rows = await _rows(_config(tmp_path)) + assert {status for status, _detail in rows.values()} == {"ready"} + assert all(detail == "" for _status, detail in rows.values()) + + async def test_a_key_that_is_not_stored(self, tmp_path: Path) -> None: + rows = await _rows(_config(tmp_path), stored=set()) + assert rows["tst-default"] == ("no_key", "No key is stored for OpenRouter.") + assert rows["budget"][0] == "no_key" + # A model on this computer needs no key, and Grok brings its own. + assert rows["local"][0] == "ready" and rows["grok"][0] == "ready" + + async def test_a_server_on_this_computer_that_does_not_answer(self, tmp_path: Path) -> None: + rows = await _rows(_config(tmp_path), down={OLLAMA}) + assert rows["local"] == ( + "server_down", + "Nothing is answering at 127.0.0.1:11434. Start the model server.", + ) + assert rows["vllm"][0] == "ready" + assert rows["tst-default"][0] == "ready" + + async def test_a_grok_entry_without_the_grok_cli(self, tmp_path: Path) -> None: + rows = await _rows(_config(tmp_path), grok=False) + assert rows["grok"] == ("no_grok", "The Grok CLI was not found on this computer.") + assert rows["local"][0] == "ready" + + async def test_the_first_problem_a_person_would_fix_is_the_one_shown( + self, tmp_path: Path + ) -> None: + # A local role bound to a key that is missing, on a server that is down. + text = ( + _catalog_with_grok() + .replace( + f" base_url: {OLLAMA}\n", + f" base_url: {OLLAMA}\n credential: house\n", + ) + .replace("\ncredentials:\n", "\ncredentials:\n house:\n name: House key\n", 1) + ) + path = tmp_path / "config.yaml" + path.write_text(text, encoding="utf-8") + rows = await _rows(load_config(path), down={OLLAMA}) + assert rows["local"] == ("no_key", "No key is stored for House key.") + + async def test_a_providers_server_is_never_asked(self, tmp_path: Path) -> None: + config = _config(tmp_path) + assert loopback_urls(config) == [OLLAMA, VLLM] + + async def test_with_the_grok_engine_as_the_setting_every_entry_is_a_grok_one( + self, tmp_path: Path + ) -> None: + text = _catalog_with_grok(engine_kind="grok") + path = tmp_path / "config.yaml" + path.write_text(text, encoding="utf-8") + rows = await _rows(load_config(path), stored=set(), down={OLLAMA}, grok=False) + assert {status for status, _detail in rows.values()} == {"no_grok"} + + +class TestTheProbe: + async def _ask(self, handler: Any, urls: list[str]) -> tuple[dict[str, bool], list[str]]: + seen: list[str] = [] + + def record(request: httpx.Request) -> httpx.Response: + seen.append(str(request.url)) + assert "authorization" not in {k.lower() for k in request.headers} + return handler(request) + + async with httpx.AsyncClient(transport=httpx.MockTransport(record)) as client: + return await loopback_answers(urls, client=client), seen + + async def test_any_reply_means_a_server_is_there(self) -> None: + def handler(request: httpx.Request) -> httpx.Response: + return httpx.Response(404 if "11434" in str(request.url) else 200, json={}) + + found, seen = await self._ask(handler, [OLLAMA, VLLM + "/"]) + assert found == {OLLAMA: True, VLLM: True} + assert sorted(seen) == sorted([f"{VLLM}/models", f"{OLLAMA}/models"]) + + async def test_a_refused_connection_or_a_timeout_means_it_is_not(self) -> None: + def handler(request: httpx.Request) -> httpx.Response: + if "11434" in str(request.url): + raise httpx.ConnectError("refused", request=request) + raise httpx.ReadTimeout("slow", request=request) + + found, _seen = await self._ask(handler, [OLLAMA, VLLM]) + assert found == {OLLAMA: False, VLLM: False} + + async def test_a_url_off_this_computer_is_not_asked_and_carries_no_key(self) -> None: + def handler(request: httpx.Request) -> httpx.Response: + return httpx.Response(200, json={}) + + found, seen = await self._ask(handler, ["https://api.example.com/v1", OLLAMA]) + assert seen == [f"{OLLAMA}/models"] + assert found == {"https://api.example.com/v1": True, OLLAMA: True} + + async def test_nothing_local_asks_nothing(self) -> None: + def handler(request: httpx.Request) -> httpx.Response: + raise AssertionError("no request expected") + + found, seen = await self._ask(handler, ["https://api.example.com/v1"]) + assert seen == [] and found == {"https://api.example.com/v1": True} + + +class TestOverTheWire: + async def test_check_presets_answers_with_a_row_per_model( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + asked: list[list[str]] = [] + + async def answers(urls: list[str]) -> dict[str, bool]: + asked.append(list(urls)) + return {url: "8000" in url for url in urls} + + monkeypatch.setattr("tstd.daemon.loopback_answers", answers) + monkeypatch.setattr("tstd.daemon._probe_grok", lambda _configured: (None, False)) + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_catalog_with_grok(), encoding="utf-8") + _daemon, task, ws = await _start(data, MockProvider(default=Script(kind="text"))) + try: + reply = await _ask(ws, {"type": "check_presets"}, "preset_readiness") + assert reply["type"] == "preset_readiness" + rows = {row["preset"]: row for row in reply["rows"]} + assert rows["local"]["status"] == "server_down" + assert "127.0.0.1:11434" in rows["local"]["detail"] + assert rows["vllm"]["status"] == "ready" + assert rows["grok"]["status"] == "no_grok" + # The suite stores no key, so the hosted entries say so. + assert rows["tst-default"]["status"] == "no_key" + assert asked == [[OLLAMA, VLLM]] + finally: + await _stop(task, ws) + + +def test_another_computers_hub_may_not_send_it() -> None: + assert "check_presets" not in MACHINE_PEER_VERBS diff --git a/core/tstd/daemon.py b/core/tstd/daemon.py index 65a0cd6d..639018e0 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -161,7 +161,7 @@ store_api_key, ) from .line_comments import LineCommentError, build_comments_turn -from .local_servers import scan_local_servers +from .local_servers import loopback_answers, scan_local_servers from .local_worker import ( effective_tier, last_cu_surface, @@ -232,6 +232,7 @@ no_model_refusal, preset_overrides, ) +from .preset_ready import check_presets from .preset_write import ( add_grok_preset, delete_preset, @@ -257,6 +258,7 @@ CancelPairing, CharterDocument, CheckCuPermissions, + CheckPresets, ClientMessageT, CloseWorktree, CommandEntry, @@ -362,6 +364,7 @@ PairingCompleted, ParseJob, PolicyRules, + PresetReadiness, PreviewRestoreFiles, ReadArtifactChunk, RefreshModelCards, @@ -3706,6 +3709,17 @@ async def send(reply: str) -> None: return await self._handle_set_session_preset(msg) if isinstance(msg, SwitchPreset): return await self._handle_switch_preset(msg) + if isinstance(msg, CheckPresets): + # TD-5240: keys by presence, this computer's servers by a keyless + # ask, the Grok CLI by its path. Nothing leaves the machine. + _grok_path, grok_found = await asyncio.to_thread(_probe_grok, self.config.engine.binary) + readiness = await check_presets( + self.config, + key_stored=self._credential_is_stored, + answers=loopback_answers, + grok_found=grok_found, + ) + return PresetReadiness(rows=readiness).model_dump_json() if isinstance(msg, TrustWorkspaceConfig): return await self._handle_trust_workspace_config(msg) diff --git a/core/tstd/local_servers.py b/core/tstd/local_servers.py index 0063a749..db9d88a9 100644 --- a/core/tstd/local_servers.py +++ b/core/tstd/local_servers.py @@ -75,3 +75,39 @@ async def scan_local_servers( if reply.error is None and reply.models ] return LocalServers(servers=found, checked=[url for _name, url in wanted]) + + +async def loopback_answers( + urls: Sequence[str], *, client: httpx.AsyncClient | None = None +) -> dict[str, bool]: + """Whether a server answers at each loopback URL (TD-5240). + + The picker's readiness check for a model served on this computer. The + same three rules as the scan above: asked only when the person opens + the picker, loopback only with no key (an off-box URL is not asked and + reads as answering: it is not this function's to judge), and nothing + written. Any HTTP reply counts, a refused connection or a timeout does + not: this says a server is there, not that the model will run. + """ + wanted = list(dict.fromkeys(url.strip().rstrip("/") for url in urls)) + local = [url for url in wanted if is_loopback_url(url)] + found = {url: True for url in wanted} + if not local: + return found + owned = client is None + http = client or httpx.AsyncClient(timeout=SCAN_TIMEOUT, transport=direct_transport(local[0])) + + async def _ask(url: str) -> bool: + try: + await http.get(f"{url}/models", headers={"Accept": "application/json"}) + except httpx.HTTPError: + return False + return True + + try: + answers = await asyncio.gather(*(_ask(url) for url in local)) + finally: + if owned: + await http.aclose() + found.update(dict(zip(local, answers, strict=True))) + return found diff --git a/core/tstd/preset_ready.py b/core/tstd/preset_ready.py new file mode 100644 index 00000000..e80c2f15 --- /dev/null +++ b/core/tstd/preset_ready.py @@ -0,0 +1,98 @@ +"""Whether each model on the picker can run right now (TD-5240). + +A switch used to succeed and the next message then fail: the local server +was not started, the key was never stored, the Grok CLI was not installed. +This says so on the picker's row, before the click. + +Three checks, none of which sends a key or leaves the machine: + +* a role bound to a key: is that key in the keychain (presence only); +* a role served on this computer: does anything answer there; +* an entry on the Grok engine: is the Grok CLI found. + +A provider's server is not asked: that would be a request to a third party +the person did not make, and a key's presence is what can be known without +one. The first problem found is the entry's status, in the order a person +would fix them: the engine, then a key, then a server. + +Pure but for the three callables it is handed, so every case is a table row. +""" + +from __future__ import annotations + +from collections.abc import Awaitable, Callable, Mapping +from urllib.parse import urlsplit + +from .config import ModelConfig, is_loopback_url, resolve_base_url, resolve_credential_id +from .protocol import PresetReady + +KeyStored = Callable[[str], Awaitable[bool]] +Answers = Callable[[list[str]], Awaitable[Mapping[str, bool]]] + + +def _where(url: str) -> str: + """``host:port`` of *url*, for a message. Never the path or a password.""" + try: + parts = urlsplit(url) + return f"{parts.hostname}:{parts.port}" if parts.port else str(parts.hostname) + except ValueError: + return "its server" + + +def loopback_urls(config: ModelConfig) -> list[str]: + """Every distinct URL on this computer that some entry's role is served at.""" + found: list[str] = [] + for preset in config.presets.values(): + for tier in preset.tier_map().values(): + url = resolve_base_url(config, tier).strip().rstrip("/") + if is_loopback_url(url) and url not in found: + found.append(url) + return found + + +async def check_presets( + config: ModelConfig, *, key_stored: KeyStored, answers: Answers, grok_found: bool +) -> list[PresetReady]: + """One row per catalog entry, in catalog order.""" + up = await answers(loopback_urls(config)) + rows: list[PresetReady] = [] + for name in config.presets: + rows.append(await _check(config, name, key_stored, up, grok_found)) + return rows + + +async def _check( + config: ModelConfig, + name: str, + key_stored: KeyStored, + up: Mapping[str, bool], + grok_found: bool, +) -> PresetReady: + preset = config.presets[name] + if config.preset_engine(name) == "grok": + if not grok_found: + return PresetReady( + preset=name, + status="no_grok", + detail="The Grok CLI was not found on this computer.", + ) + # The Grok CLI brings its own model and sign-in: nothing else to check. + return PresetReady(preset=name, status="ready") + for tier in preset.tier_map().values(): + credential = resolve_credential_id(tier) + if credential is not None and not await key_stored(credential): + shown = config.credentials[credential].name if credential in config.credentials else "" + return PresetReady( + preset=name, + status="no_key", + detail=f"No key is stored for {shown or credential}.", + ) + for tier in preset.tier_map().values(): + url = resolve_base_url(config, tier).strip().rstrip("/") + if is_loopback_url(url) and not up.get(url, True): + return PresetReady( + preset=name, + status="server_down", + detail=f"Nothing is answering at {_where(url)}. Start the model server.", + ) + return PresetReady(preset=name, status="ready") diff --git a/core/tstd/protocol.py b/core/tstd/protocol.py index 655b801e..7f14d667 100644 --- a/core/tstd/protocol.py +++ b/core/tstd/protocol.py @@ -889,6 +889,19 @@ class SetSessionPreset(ClientMessage): name: str = Field(min_length=1) +class CheckPresets(ClientMessage): + """Ask whether each model on the picker can run right now (TD-5240). + + Sent when the person opens the picker. The daemon looks for each bound + key in the keychain (presence only), asks each server on this computer + whether it is there, and looks for the Grok CLI for a Grok entry. No + key is sent anywhere and no provider is contacted. Answered with + ``preset_readiness``. + """ + + type: Literal["check_presets"] = "check_presets" + + class SwitchPreset(ClientMessage): """One switch: this chat, the model new chats use, or both (TD-5235). @@ -4136,6 +4149,27 @@ class ModelTested(DaemonEvent): detail: str = "" +class PresetReady(BaseModel): + """Whether one catalog entry can run right now (TD-5240).""" + + preset: str + # ``no_grok``: on the Grok engine and the CLI is not found. ``no_key``: + # a role's key is not in the keychain. ``server_down``: a role is served + # on this computer and nothing answers there. + status: Literal["ready", "no_key", "server_down", "no_grok"] + # One sentence for the picker's row. Empty when ready. Never a key, and + # of a URL only the host and port. + detail: str = "" + + +class PresetReadiness(DaemonEvent): + """Reply to ``check_presets`` (TD-5240): one row per catalog entry.""" + + type: Literal["preset_readiness"] = "preset_readiness" + seq: int = 1 + rows: list[PresetReady] = Field(default_factory=list) + + class ConfigEditor(DaemonEvent): """Settings → Configuration's view of config.yaml (TD-5203). @@ -4920,6 +4954,7 @@ class GoogleStatus(DaemonEvent): | SetPlan | SetSessionPreset | SwitchPreset + | CheckPresets | GetInstructionStack | GetHooks | ListInstructions @@ -5093,6 +5128,7 @@ class GoogleStatus(DaemonEvent): | EndpointModels | LocalServers | ModelTested + | PresetReadiness | ConfigEditor | ConfigValueRejected | DiagnosticsReport @@ -5179,6 +5215,7 @@ class GoogleStatus(DaemonEvent): "set_plan", "set_session_preset", "switch_preset", + "check_presets", "get_instruction_stack", "get_hooks", "list_instructions", @@ -5352,6 +5389,7 @@ class GoogleStatus(DaemonEvent): "endpoint_models", "local_servers", "model_tested", + "preset_readiness", "config_editor", "config_value_rejected", "diagnostics_report", diff --git a/docs/architecture.md b/docs/architecture.md index 6af03092..43d9e52a 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -606,6 +606,7 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `set_plan` | yes | Turn plan mode on or off. Refused `set_tier` to worker/validator while on (TD-4603). Plan mode is read-only (TD-5123): the classifier refuses, with `error_code` `plan_mode`, every call that could change something (`fs_write`, `fs_edit`, `shell` outside the read-only list, MCP tools not in their server's `read_only_tools`, plugin tools, actuation, GitHub writes, `propose_steering_edit`, `delegate` with `kind: worker`, and any other builtin not on `PLAN_READ_ONLY_BUILTINS`, except a `shell` command on the read-only list, the screenshot tools and `delegate` without `kind: worker`, which `plan_mode_allows` lets through first; TD-5161), ahead of policy rules and skip-all, and again after an approval a call waited on; `delegate` with no `kind` runs the research worker (TD-5117). A `workflow` call with any `kind: worker` task is refused whole the same way; a call of only research tasks runs (TD-5195). The model ends it with `exit_plan_mode`. A restart keeps it: the session row's `plan_mode` is read back on revive. | | `set_session_preset` | yes | Retarget this session at a catalog preset. Refused mid-turn. Refused with `engine_mismatch` when the preset names another engine than the session's, or has no tiers and the session runs TST Desk's own loop: a session keeps the engine it started on (TD-5239). Does not write Settings. Acked with `session_list` (TD-1721). | | `switch_preset` | yes | The picker's click (TD-5235): `session_id` switches that chat to preset `name`, `make_default` also (or only) makes it the preset new chats use. Everything is checked before anything changes. An idle chat switches now; a chat with a turn running keeps the switch as pending and takes it when that turn completes (`tier_state.pending_preset` names it; a later switch replaces it, one back to the chat's own preset cancels it; it is not kept across a daemon restart); a chat with no live loop has the name recorded. Refused with `engine_mismatch` like `set_session_preset`. Not a machine-peer verb. Acked with `setup_state` when the default changed, else `session_list`. | +| `check_presets` | — | Sent when the picker opens (TD-5240): ask whether each catalog preset can run right now. The daemon looks for each bound key in the keychain (presence only), asks each server on this computer whether anything answers at its `/models` (no key, loopback only), and looks for the Grok CLI for a preset on the Grok engine. No provider is contacted. Not a machine-peer verb. Answered with `preset_readiness`. | | `get_instruction_stack` | yes | Ask for the resolved steering stack, token counts, and skills (TD-4502). | | `get_hooks` | yes | Ask for the session's active hooks (user config, and the workspace's once trusted) with each one's last run (TD-5134). Answered with `hooks_state`. | | `list_instructions` | — | List a workspace's Instructions files (`AGENTS.md` / `CLAUDE.md` fallback, then `.tst/rules/*`). Human path (TD-2802). | @@ -776,6 +777,7 @@ are stamped by a session's event log, `connection` events fix it at 1, and `ping | `endpoint_models` | connection | A server's model list for "Add a model", or why it could not be read. Never carries the key. | | `local_servers` | connection | The servers on this computer that answered `scan_local_servers` with models, in config order, and every URL asked. | | `model_tested` | connection | The result of `test_model`: the reply, or what went wrong. | +| `preset_readiness` | connection | Reply to `check_presets` (TD-5240): one row per catalog preset with `status` (`ready`, `no_key`, `server_down` or `no_grok`) and one sentence of `detail` for the picker's row. The detail names a key by its display name and a server by host and port only. | | `config_editor` | connection | Reply to `get_config_editor`, and the ack of each config edit (TD-5203). `defs` (every config model and its fields: kind, bounds, choices, default, description, unit) and `defaults` (reset values by JSON-encoded path) come on the reply to `get_config_editor` only; an edit's ack leaves both empty. `values` is the effective config with each `secrets` map's values replaced by where they live (`keychain` or `config`), never the value, and a password in a URL masked. `set_paths` lists every key path the file sets, and `locked_paths` each one it writes through a YAML anchor, alias or merge (those, and the keys under them, are edited in the file by hand); `version` identifies its bytes; `edit` names the edit acknowledged. | | `config_value_rejected` | connection | A config edit that was not written: `code` (`invalid`, `not_editable`, `secret`, `stale`, `not_found`, `write_failed`) and a message for the field at `path`, never echoing the value. The file is untouched (TD-5203). | | `diagnostics_report` | connection | Doctor results: one row per check, with a fix when it failed. The `skills` row names each skill's ignored frontmatter keys (TD-5107). | diff --git a/docs/models.md b/docs/models.md index 48b2668d..5ae2bb51 100644 --- a/docs/models.md +++ b/docs/models.md @@ -305,6 +305,12 @@ key per role. ## When something goes wrong +Each time the picker opens, TST Desk checks what it can without sending a +key or contacting a provider, and says so under a model's name: **No key +is stored for …**, **Nothing is answering at 127.0.0.1:…** for a model +served on this computer, or **The Grok CLI was not found**. It is a +warning, not a lock: the model can still be picked. + | You see | What to do | |---|---| | "No model is set up yet" | Add one: picker → **Add a model**, or Settings → Models. | diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index b46e8c9d..1ad2a2fd 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13632,3 +13632,35 @@ it. Other Settings writes to the active entry (a role's model, key or window) still reach only the window that made them. Class B entry in DECISIONS.md. +--- + +### TD-5240 — The picker says when a model cannot run yet + +**Size:** 3 · **Depends on:** TD-5236, TD-5239 · **Status:** Done + +A switch succeeded and the next message then failed: the local server was +not started, the key was never stored, the Grok CLI was not installed. The +first sign was a failed turn. + +- [x] `check_presets` is answered with `preset_readiness`, one row per + entry: `ready`, `no_key`, `server_down` or `no_grok`, and one sentence + (`test_preset_ready.py::TestOverTheWire`). +- [x] A key is checked by presence in the keychain only; a server is asked + only when it is on this computer, with no key, and any reply counts + as there; an entry on the Grok engine is checked for the CLI and + nothing else (`TestTheChecks`, `TestTheProbe`). +- [x] A provider's server is never contacted + (`test_a_providers_server_is_never_asked`, + `test_a_url_off_this_computer_is_not_asked_and_carries_no_key`). +- [x] The first problem a person would fix is the one shown: the engine, + then a key, then a server + (`test_the_first_problem_a_person_would_fix_is_the_one_shown`). +- [x] The picker asks each time it opens and shows a model's problem under + its name; the model can still be picked (`model-ready.test.ts`). +- [x] Not a machine-peer verb (`test_another_computers_hub_may_not_send_it`). + +**Notes:** A warning, not a gate: a server may be starting, and the turn's +own error is still the authority. The loopback probe lives in +`local_servers.py` beside the "Find models on this computer" scan and +follows its rules. Class B entry in DECISIONS.md. + diff --git a/ui/src/lib/client.ts b/ui/src/lib/client.ts index 0be92a7a..8fe3c406 100644 --- a/ui/src/lib/client.ts +++ b/ui/src/lib/client.ts @@ -106,6 +106,7 @@ const KNOWN_EVENT_TYPES = new Set([ "endpoint_models", // TD-5172 "local_servers", // TD-5180 "model_tested", // TD-5172 + "preset_readiness", // TD-5240 "config_editor", // TD-5203 "config_value_rejected", // TD-5203 "diagnostics_report", // TD-1104 doctor diff --git a/ui/src/lib/components/chat/ModelPicker.svelte b/ui/src/lib/components/chat/ModelPicker.svelte index 0db54384..872a8f1f 100644 --- a/ui/src/lib/components/chat/ModelPicker.svelte +++ b/ui/src/lib/components/chat/ModelPicker.svelte @@ -23,6 +23,8 @@ import { MODELS_DOC_URL, openDoc } from "../../docs-links"; import { openAddModel } from "../../onboarding.svelte.js"; import ModelRow from "./ModelRow.svelte"; + import { onMount } from "svelte"; + import { askReadiness, watchReadiness } from "../../model-ready.svelte.js"; import { sectionModels } from "../../model-sections"; import { session, setEffort, setFast, setUltracode } from "../../session-status.svelte.js"; import { settings } from "../../settings.svelte.js"; @@ -41,24 +43,24 @@ current, }), ); - // TD-5236: the main list in sections (Local, API, Grok, then a model's - // own). The daemon says which section each model is in. - const sections = $derived( - sectionModels(groups.main, settings.presetGroups, settings.pickerGroups), - ); + // TD-5236: the main list in sections; the daemon says which each model is in. + const sections = $derived(sectionModels(groups.main, settings.presetGroups, settings.pickerGroups)); function close(): void { open = false; view = "main"; } + // TD-5240: opening the picker asks the daemon which models can run now. + onMount(watchReadiness); + function toggle(): void { - if (!settings.hasModel) { - openAddModel(); - return; + if (!settings.hasModel) openAddModel(); + else if (open) close(); + else { + open = true; + askReadiness(); } - if (open) close(); - else open = true; } function addModel(): void { diff --git a/ui/src/lib/components/chat/ModelRow.svelte b/ui/src/lib/components/chat/ModelRow.svelte index 09576629..9f5d99af 100644 --- a/ui/src/lib/components/chat/ModelRow.svelte +++ b/ui/src/lib/components/chat/ModelRow.svelte @@ -13,6 +13,7 @@ pickModel, pickOnlyHere, } from "../../model-pick"; + import { problemWith } from "../../model-ready.svelte.js"; import { session } from "../../session-status.svelte.js"; import { settings } from "../../settings.svelte.js"; @@ -22,6 +23,8 @@ const mark = $derived(engineMark(name, settings.presetEngines)); const slot = $derived(settings.presetSlots[name] ?? null); const description = $derived(modelDescription(name, settings.presetDescriptions)); + // TD-5240: what would stop this model running, as the daemon last said. + const problem = $derived(problemWith(name));
@@ -42,7 +45,9 @@ >{label}{#if mark} {mark}{/if} - {#if opensNewChat(name)} + {#if problem} + {problem} + {:else if opensNewChat(name)} {NEW_CHAT} {:else if description} {description} @@ -119,6 +124,11 @@ white-space: nowrap; } + .sub--problem { + color: var(--color-warn); + white-space: normal; + } + .badge { font-size: var(--text-xs); color: var(--color-ink-muted); diff --git a/ui/src/lib/model-ready.svelte.ts b/ui/src/lib/model-ready.svelte.ts new file mode 100644 index 00000000..859a1af7 --- /dev/null +++ b/ui/src/lib/model-ready.svelte.ts @@ -0,0 +1,36 @@ +// TD-5240: whether each model on the picker can run right now, as the +// daemon last said. The picker asks when it opens (`check_presets`) and +// shows a model's problem on its row, before the click: no key stored, its +// local server not answering, the Grok CLI not found. A row the daemon has +// said nothing about shows nothing; the window does not guess. + +import { onEvent, sendToDaemon } from "./connection-status.svelte.js"; +import type { DaemonEventUnion, PresetReady } from "./protocol"; + +export const readiness = $state<{ rows: Record }>({ rows: {} }); + +/** Keep the daemon's latest answer. Any other event is ignored. */ +export function ingestReadiness(event: DaemonEventUnion): void { + if (event.type !== "preset_readiness") return; + readiness.rows = Object.fromEntries(event.rows.map((row) => [row.preset, row])); +} + +/** What is wrong with `preset`, in one sentence, or "" when nothing is known to be. */ +export function problemWith(preset: string): string { + const row = readiness.rows[preset]; + return row === undefined || row.status === "ready" ? "" : (row.detail ?? ""); +} + +export function resetReadiness(): void { + readiness.rows = {}; +} + +/** Listen for the daemon's answers while the picker is mounted. */ +export function watchReadiness(): () => void { + return onEvent(ingestReadiness); +} + +/** Ask the daemon again. The picker calls this each time it opens. */ +export function askReadiness(): void { + sendToDaemon({ type: "check_presets" }); +} diff --git a/ui/src/lib/model-ready.test.ts b/ui/src/lib/model-ready.test.ts new file mode 100644 index 00000000..79d61480 --- /dev/null +++ b/ui/src/lib/model-ready.test.ts @@ -0,0 +1,133 @@ +// @vitest-environment jsdom +// +// TD-5240: the picker asks the daemon, each time it opens, which models can +// run now, and shows a model's problem on its row before the click. + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, tick, unmount } from "svelte"; +import type { ClientMessageUnion, DaemonEventUnion } from "./protocol"; + +const mocks = vi.hoisted(() => ({ + sent: [] as ClientMessageUnion[], + subs: new Set<(event: DaemonEventUnion) => void>(), +})); + +vi.mock("./connection-status.svelte.js", () => { + const onDaemonEvent = (handler: (event: DaemonEventUnion) => void) => { + mocks.subs.add(handler); + return () => mocks.subs.delete(handler); + }; + return { + onEvent: onDaemonEvent, + onDaemonEvent, + sendToDaemon: (msg: ClientMessageUnion) => (mocks.sent.push(msg), true), + }; +}); +vi.mock("./sessions.svelte.js", () => ({ newSession: () => true })); + +import ModelPicker from "./components/chat/ModelPicker.svelte"; +import { problemWith, readiness, resetReadiness } from "./model-ready.svelte.js"; +import { resetSession, session } from "./session-status.svelte.js"; +import { resetSettings, settings } from "./settings.svelte.js"; + +let app: ReturnType | null = null; + +function emit(event: Partial & { type: string }): void { + for (const sub of [...mocks.subs]) sub(event as DaemonEventUnion); + flushSync(); +} + +const chip = (): HTMLButtonElement => + document.querySelector('[aria-label="Model and effort"]')!; + +function row(label: string): HTMLButtonElement { + const found = [...document.querySelectorAll('[role="menuitemradio"]')].find( + (button) => button.querySelector(".label")?.textContent?.includes(label), + ); + if (found === undefined) throw new Error(`no row for ${label}`); + return found; +} + +beforeEach(async () => { + resetSession(); + resetSettings(); + resetReadiness(); + mocks.sent = []; + session.sessionId = "s1"; + session.preset = "api"; + session.engine = "native"; + settings.hasModel = true; + settings.activePreset = "api"; + settings.presets = ["api", "local"]; + settings.presetLabels = { api: "Hosted", local: "Laptop" }; + settings.presetDescriptions = { local: "Served on this computer" }; + app = mount(ModelPicker, { target: document.body }); + await tick(); +}); + +afterEach(async () => { + if (app !== null) await unmount(app); + app = null; + document.body.replaceChildren(); + mocks.subs.clear(); + resetReadiness(); + resetSession(); + resetSettings(); +}); + +describe("the picker's readiness check", () => { + it("asks each time the picker opens, and not when it closes", () => { + chip().click(); + flushSync(); + expect(mocks.sent).toEqual([{ type: "check_presets" }]); + chip().click(); + flushSync(); + expect(mocks.sent).toHaveLength(1); + chip().click(); + flushSync(); + expect(mocks.sent).toEqual([{ type: "check_presets" }, { type: "check_presets" }]); + }); + + it("shows a model's problem on its row in place of its description", () => { + chip().click(); + flushSync(); + expect(row("Laptop").querySelector(".sub")?.textContent).toBe("Served on this computer"); + emit({ + type: "preset_readiness", + rows: [ + { preset: "api", status: "ready", detail: "" }, + { + preset: "local", + status: "server_down", + detail: "Nothing is answering at 127.0.0.1:8000. Start the model server.", + }, + ], + }); + const sub = row("Laptop").querySelector(".sub"); + expect(sub?.textContent).toContain("Nothing is answering at 127.0.0.1:8000"); + expect(sub?.classList.contains("sub--problem")).toBe(true); + expect(row("Hosted").querySelector(".sub--problem")).toBeNull(); + // A warning, not a wall: the model can still be picked. + expect(row("Laptop").disabled).toBe(false); + }); + + it("clears the problem when the daemon next says the model is ready", () => { + chip().click(); + emit({ + type: "preset_readiness", + rows: [{ preset: "local", status: "no_key", detail: "No key is stored for House." }], + }); + expect(problemWith("local")).toBe("No key is stored for House."); + emit({ type: "preset_readiness", rows: [{ preset: "local", status: "ready" }] }); + expect(problemWith("local")).toBe(""); + expect(row("Laptop").querySelector(".sub")?.textContent).toBe("Served on this computer"); + }); + + it("says nothing about a model the daemon said nothing about", () => { + emit({ type: "preset_readiness", rows: [] }); + expect(readiness.rows).toEqual({}); + expect(problemWith("api")).toBe(""); + emit({ type: "setup_state" }); + expect(readiness.rows).toEqual({}); + }); +}); diff --git a/ui/src/lib/protocol-fixtures.json b/ui/src/lib/protocol-fixtures.json index 959be0ef..eb519d0c 100644 --- a/ui/src/lib/protocol-fixtures.json +++ b/ui/src/lib/protocol-fixtures.json @@ -653,6 +653,9 @@ "type": "delete_preset", "name": "budget" }, + "check_presets": { + "type": "check_presets" + }, "switch_preset": { "type": "switch_preset", "name": "budget", @@ -2452,6 +2455,22 @@ "reply": "I am working.", "detail": "" }, + "preset_readiness": { + "type": "preset_readiness", + "seq": 1, + "rows": [ + { + "preset": "tst-default", + "status": "ready", + "detail": "" + }, + { + "preset": "local", + "status": "server_down", + "detail": "Nothing is answering at 127.0.0.1:11434. Start the model server." + } + ] + }, "config_editor": { "type": "config_editor", "seq": 1, diff --git a/ui/src/lib/protocol.ts b/ui/src/lib/protocol.ts index 08628a1a..42aba5f2 100644 --- a/ui/src/lib/protocol.ts +++ b/ui/src/lib/protocol.ts @@ -921,6 +921,12 @@ export interface SwitchPreset extends ClientMessage { make_default?: boolean; } +// TD-5240: ask whether each model on the picker can run right now. Sent +// when the picker opens. Answered with preset_readiness. +export interface CheckPresets extends ClientMessage { + type: "check_presets"; +} + // TD-5258: put the Grok CLI on the model list as an entry that is // `engine: grok` and names no model or key. Refused when the CLI is not // found or an entry already names the Grok engine. Acked with setup_state. @@ -1414,6 +1420,7 @@ export type ClientMessageUnion = | SetPreset | DuplicatePreset | SwitchPreset + | CheckPresets | AddGrokPreset | SetPresetLabel | DeletePreset @@ -2586,6 +2593,20 @@ export interface LocalServers extends DaemonEvent { } // TD-5172: reply to test_model. +/** TD-5240: whether one model can run right now. `detail` is one sentence + * for the picker's row, empty when ready. */ +export interface PresetReady { + preset: string; + status: "ready" | "no_key" | "server_down" | "no_grok"; + detail?: string; +} + +/** TD-5240: reply to check_presets, one row per model. */ +export interface PresetReadiness extends DaemonEvent { + type: "preset_readiness"; + rows: PresetReady[]; +} + export interface ModelTested extends DaemonEvent { type: "model_tested"; seq: number; @@ -3425,6 +3446,7 @@ export type DaemonEventUnion = | EndpointModels | LocalServers | ModelTested + | PresetReadiness | ConfigEditor | ConfigValueRejected | DiagnosticsReport From 169d5793f503869413a9a553d07827e6fab33feb Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 18:36:40 +0000 Subject: [PATCH 09/35] =?UTF-8?q?TD-5241:=20Settings=20=E2=86=92=20Model?= =?UTF-8?q?=20edits=20the=20model=20you=20choose?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "Build a team" edited the model new chats use and no other. To change a second model's context size or key, a person first had to make it the default, which also moved every new chat onto it. get_preset_roles {name} is answered with preset_roles: for that entry, the four maps setup_state carries for the active one. It is read-only. setup_state itself is unchanged, so Settings → API keys and older clients still read the active model's roles from it. Settings → Model has an "Editing" chooser. The store keeps the chosen model and its roles beside the active model's, never over them, and every write names the chosen one. A chosen model's roles are shown only as the daemon sent them: none until its answer arrives, read again after each write, and let go when the model is removed or becomes active. Class B: recorded in DECISIONS.md. Co-Authored-By: Claude Fable 5.2 --- DECISIONS.md | 20 ++ core/scripts/generate_protocol_fixtures.py | 10 + core/tests/test_preset_roles.py | 112 +++++++++ core/tstd/daemon.py | 31 ++- core/tstd/protocol.py | 34 +++ docs/architecture.md | 2 + docs/models.md | 6 +- docs/tst-desk-backlog.md | 30 +++ ui/e2e/census.json | 1 + ui/src/lib/client.ts | 1 + ui/src/lib/components/SettingsModels.svelte | 64 ++++-- ui/src/lib/model-edit.test.ts | 243 ++++++++++++++++++++ ui/src/lib/protocol-fixtures.json | 22 ++ ui/src/lib/protocol.ts | 20 ++ ui/src/lib/settings.svelte.ts | 83 ++++++- 15 files changed, 650 insertions(+), 29 deletions(-) create mode 100644 core/tests/test_preset_roles.py create mode 100644 ui/src/lib/model-edit.test.ts diff --git a/DECISIONS.md b/DECISIONS.md index 473683ab..0bdee980 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -20054,3 +20054,23 @@ direction: a server that is still loading answers nothing for a moment. probing on a timer (traffic nobody asked for); validating a key with a live call (that is the existing Test button's job, on a click). +## 2026-11-20 — TD-5241: a read-only `get_preset_roles` for the entry being edited; `setup_state` is unchanged (Class B) + +**Decision:** `get_preset_roles {name}` is answered with `preset_roles`, +which carries for that entry the four maps `setup_state` carries for the +active one. The Settings store keeps the chosen entry (`editPreset`) and its +roles (`editRoles`) beside the active entry's, never over them, and every +write names `editTarget()`. The write verbs already took a `preset`. + +**Rationale:** the alternative was to make `setup_state`'s `tier_*` fields +describe "whichever entry this connection is looking at", which would turn +a stateless reply into per-connection state and break Settings → API keys +and older clients, which read those fields as the active entry's. A separate +read keeps `setup_state` meaning one thing. Showing nothing until the answer +arrives is the "window never derives what it was not sent" rule: the active +entry's roles must not appear under another entry's name. + +**Rejected:** sending every entry's roles in every `setup_state` (it grows +with the catalog and is sent on every Settings write); making the entry +active in order to edit it (that is the behaviour being fixed). + diff --git a/core/scripts/generate_protocol_fixtures.py b/core/scripts/generate_protocol_fixtures.py index 2e1db06a..59f4d950 100644 --- a/core/scripts/generate_protocol_fixtures.py +++ b/core/scripts/generate_protocol_fixtures.py @@ -104,6 +104,7 @@ GetConfigEditor, GetHooks, GetInstructionStack, + GetPresetRoles, GetSessionJobSource, GetSetupState, GetUsage, @@ -192,6 +193,7 @@ PolicyRuleSummary, PresetReadiness, PresetReady, + PresetRoles, PreviewRestoreFiles, Question, QuestionAnswer, @@ -597,6 +599,8 @@ ), "delete_preset": DeletePreset(name="budget"), "check_presets": CheckPresets(), + # TD-5241: the roles of the entry Settings → Model is editing + "get_preset_roles": GetPresetRoles(name="budget"), # TD-5235: this chat, the model new chats use, or both "switch_preset": SwitchPreset(name="budget", session_id="sess-1", make_default=True), # TD-5258: put the Grok CLI on the model list @@ -1641,6 +1645,12 @@ error="The server refused the key. Check it and try again.", ), "model_tested": ModelTested(preset="tst-default", ok=True, reply="I am working."), + "preset_roles": PresetRoles( + preset="budget", + tier_slugs={"brain": "vendor/model-a", "worker": None}, + tier_credentials={"brain": "openrouter", "worker": None}, + tier_loopback={"brain": False, "worker": True}, + ), # TD-5240: whether each model on the picker can run right now "preset_readiness": PresetReadiness( rows=[ diff --git a/core/tests/test_preset_roles.py b/core/tests/test_preset_roles.py new file mode 100644 index 00000000..bb43e71a --- /dev/null +++ b/core/tests/test_preset_roles.py @@ -0,0 +1,112 @@ +"""Settings → Model edits the entry you choose, not only the active one (TD-5241). + +``setup_state`` describes the active entry's roles. ``get_preset_roles`` +gives the same four maps for any entry, so the page can show and edit one +without first making it the model new chats use. +""" + +from __future__ import annotations + +from pathlib import Path + +from tests.test_add_model import _ask, _start, _stop +from tests.test_preset_engine import _text as _catalog_with_grok +from tstd.config import load_config +from tstd.mock import MockProvider, Script +from tstd.peer_policy import MACHINE_PEER_VERBS + +ROLE_MAPS = ("tier_slugs", "tier_credentials", "tier_loopback", "tier_cards") + + +async def _seeded(tmp_path: Path) -> tuple: # type: ignore[type-arg] + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_catalog_with_grok(), encoding="utf-8") + return await _start(data, MockProvider(default=Script(kind="text"))) + + +async def test_an_entrys_roles_are_what_setup_state_says_when_it_is_active( + tmp_path: Path, +) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + assert before["active_preset"] == "tst-default" + roles = await _ask(ws, {"type": "get_preset_roles", "name": "budget"}, "preset_roles") + assert roles["type"] == "preset_roles" and roles["preset"] == "budget" + assert set(roles["tier_slugs"]) == {"brain", "worker", "validator"} + # Asking changed nothing: the active entry is still the active one. + again = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + assert again["active_preset"] == "tst-default" + assert {k: again[k] for k in ROLE_MAPS} == {k: before[k] for k in ROLE_MAPS} + + active = await _ask(ws, {"type": "set_preset", "name": "budget"}, "setup_state") + assert {k: roles[k] for k in ROLE_MAPS} == {k: active[k] for k in ROLE_MAPS} + finally: + await _stop(task, ws) + + +async def test_a_role_on_this_computer_and_one_at_a_provider(tmp_path: Path) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + local = await _ask(ws, {"type": "get_preset_roles", "name": "local"}, "preset_roles") + assert set(local["tier_loopback"].values()) == {True} + assert set(local["tier_credentials"].values()) == {None} + # Discovered from the server each run: no model is named. + assert set(local["tier_slugs"].values()) == {None} + hosted = await _ask(ws, {"type": "get_preset_roles", "name": "tst-default"}, "preset_roles") + assert set(hosted["tier_loopback"].values()) == {False} + assert hosted["tier_credentials"]["brain"] == "openrouter" + assert hosted["tier_cards"]["brain"]["context_window"] == 1000000 + finally: + await _stop(task, ws) + + +async def test_an_edit_to_a_chosen_entry_shows_in_its_roles_and_not_the_active_ones( + tmp_path: Path, +) -> None: + daemon, task, ws = await _seeded(tmp_path) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + ack = await _ask( + ws, + { + "type": "set_tier_context_window", + "preset": "local", + "tier": "worker", + "context_window": 49152, + }, + "setup_state", + ) + # The ack still describes the active entry, untouched. + assert ack["active_preset"] == "tst-default" + assert ack["tier_cards"] == before["tier_cards"] + roles = await _ask(ws, {"type": "get_preset_roles", "name": "local"}, "preset_roles") + assert roles["tier_cards"]["worker"]["context_window"] == 49152 + worker = load_config(daemon.config_path).presets["local"].worker + assert worker is not None and worker.context_window == 49152 + finally: + await _stop(task, ws) + + +async def test_an_entry_with_no_roles_has_none(tmp_path: Path) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + roles = await _ask(ws, {"type": "get_preset_roles", "name": "grok"}, "preset_roles") + assert roles["preset"] == "grok" + assert all(roles[key] == {} for key in ROLE_MAPS) + finally: + await _stop(task, ws) + + +async def test_an_unknown_entry_is_a_typed_error(tmp_path: Path) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + reply = await _ask(ws, {"type": "get_preset_roles", "name": "nope"}, "preset_roles") + assert reply["type"] == "error" and reply["code"] == "unknown_preset" + finally: + await _stop(task, ws) + + +def test_another_computers_hub_may_not_send_it() -> None: + assert "get_preset_roles" not in MACHINE_PEER_VERBS diff --git a/core/tstd/daemon.py b/core/tstd/daemon.py index 639018e0..92589952 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -304,6 +304,7 @@ GetConfigEditor, GetHooks, GetInstructionStack, + GetPresetRoles, GetSessionJobSource, GetSetupState, GetUsage, @@ -365,6 +366,7 @@ ParseJob, PolicyRules, PresetReadiness, + PresetRoles, PreviewRestoreFiles, ReadArtifactChunk, RefreshModelCards, @@ -1861,11 +1863,14 @@ def _set_tier_context_window(self, preset: str, tier: str, window: int) -> str | ) return None - def _tier_cards(self, tiers: dict[str, TierConfig]) -> dict[str, TierCard]: - """The active preset's stored numbers and last refresh, per tier.""" + def _tier_cards( + self, tiers: dict[str, TierConfig], preset: str | None = None + ) -> dict[str, TierCard]: + """An entry's stored numbers and last refresh, per tier. Default: the active one.""" + owner = self.config.active_preset if preset is None else preset cards: dict[str, TierCard] = {} for name, tier in tiers.items(): - last = self._card_results.get((self.config.active_preset, name)) + last = self._card_results.get((owner, name)) cards[name] = TierCard( input_price=tier.input_price, output_price=tier.output_price, @@ -3709,6 +3714,26 @@ async def send(reply: str) -> None: return await self._handle_set_session_preset(msg) if isinstance(msg, SwitchPreset): return await self._handle_switch_preset(msg) + if isinstance(msg, GetPresetRoles): + # TD-5241: the roles of the entry Settings → Model is editing, + # in the shape setup_state gives the active one. + entry = self.config.presets.get(msg.name) + if entry is None: + return build_error( + "unknown_preset", + f"Unknown preset {msg.name!r}; declared: " + f"{', '.join(sorted(self.config.presets))}", + ) + own = entry.tier_map() + return PresetRoles( + preset=msg.name, + tier_slugs=dict(self._slug_snapshot.get(msg.name, {})), + tier_credentials={name: tier.credential for name, tier in own.items()}, + tier_loopback={name: is_loopback_url(tier.base_url) for name, tier in own.items()}, + tier_cards=self._tier_cards( + {str(name): tier for name, tier in own.items()}, msg.name + ), + ).model_dump_json() if isinstance(msg, CheckPresets): # TD-5240: keys by presence, this computer's servers by a keyless # ask, the Grok CLI by its path. Nothing leaves the machine. diff --git a/core/tstd/protocol.py b/core/tstd/protocol.py index 7f14d667..86d6b79e 100644 --- a/core/tstd/protocol.py +++ b/core/tstd/protocol.py @@ -889,6 +889,18 @@ class SetSessionPreset(ClientMessage): name: str = Field(min_length=1) +class GetPresetRoles(ClientMessage): + """Ask for one catalog entry's roles, to edit it (TD-5241). + + ``setup_state`` describes the active entry's roles. Settings → Model can + edit any entry, so it asks for the one it is showing. Read-only. + Answered with ``preset_roles``. + """ + + type: Literal["get_preset_roles"] = "get_preset_roles" + name: str = Field(min_length=1) + + class CheckPresets(ClientMessage): """Ask whether each model on the picker can run right now (TD-5240). @@ -4149,6 +4161,24 @@ class ModelTested(DaemonEvent): detail: str = "" +class PresetRoles(DaemonEvent): + """Reply to ``get_preset_roles`` (TD-5241): one entry's roles. + + The same four maps ``setup_state`` carries for the active entry, for + entry ``preset``: each role's model, its bound key, whether it is + served on this computer, and its stored numbers. All empty for an + entry with no roles (the Grok engine, TD-5239). Never a key's value. + """ + + type: Literal["preset_roles"] = "preset_roles" + seq: int = 1 + preset: str + tier_slugs: dict[str, str | None] = Field(default_factory=dict) + tier_credentials: dict[str, str | None] = Field(default_factory=dict) + tier_loopback: dict[str, bool] = Field(default_factory=dict) + tier_cards: dict[str, TierCard] = Field(default_factory=dict) + + class PresetReady(BaseModel): """Whether one catalog entry can run right now (TD-5240).""" @@ -4955,6 +4985,7 @@ class GoogleStatus(DaemonEvent): | SetSessionPreset | SwitchPreset | CheckPresets + | GetPresetRoles | GetInstructionStack | GetHooks | ListInstructions @@ -5129,6 +5160,7 @@ class GoogleStatus(DaemonEvent): | LocalServers | ModelTested | PresetReadiness + | PresetRoles | ConfigEditor | ConfigValueRejected | DiagnosticsReport @@ -5216,6 +5248,7 @@ class GoogleStatus(DaemonEvent): "set_session_preset", "switch_preset", "check_presets", + "get_preset_roles", "get_instruction_stack", "get_hooks", "list_instructions", @@ -5390,6 +5423,7 @@ class GoogleStatus(DaemonEvent): "local_servers", "model_tested", "preset_readiness", + "preset_roles", "config_editor", "config_value_rejected", "diagnostics_report", diff --git a/docs/architecture.md b/docs/architecture.md index 43d9e52a..42003632 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -607,6 +607,7 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `set_session_preset` | yes | Retarget this session at a catalog preset. Refused mid-turn. Refused with `engine_mismatch` when the preset names another engine than the session's, or has no tiers and the session runs TST Desk's own loop: a session keeps the engine it started on (TD-5239). Does not write Settings. Acked with `session_list` (TD-1721). | | `switch_preset` | yes | The picker's click (TD-5235): `session_id` switches that chat to preset `name`, `make_default` also (or only) makes it the preset new chats use. Everything is checked before anything changes. An idle chat switches now; a chat with a turn running keeps the switch as pending and takes it when that turn completes (`tier_state.pending_preset` names it; a later switch replaces it, one back to the chat's own preset cancels it; it is not kept across a daemon restart); a chat with no live loop has the name recorded. Refused with `engine_mismatch` like `set_session_preset`. Not a machine-peer verb. Acked with `setup_state` when the default changed, else `session_list`. | | `check_presets` | — | Sent when the picker opens (TD-5240): ask whether each catalog preset can run right now. The daemon looks for each bound key in the keychain (presence only), asks each server on this computer whether anything answers at its `/models` (no key, loopback only), and looks for the Grok CLI for a preset on the Grok engine. No provider is contacted. Not a machine-peer verb. Answered with `preset_readiness`. | +| `get_preset_roles` | — | Settings → Model (TD-5241): ask for one catalog preset's roles, `name`, so the page can edit a preset that is not the active one. Read-only; it switches nothing. Not a machine-peer verb. Answered with `preset_roles`. | | `get_instruction_stack` | yes | Ask for the resolved steering stack, token counts, and skills (TD-4502). | | `get_hooks` | yes | Ask for the session's active hooks (user config, and the workspace's once trusted) with each one's last run (TD-5134). Answered with `hooks_state`. | | `list_instructions` | — | List a workspace's Instructions files (`AGENTS.md` / `CLAUDE.md` fallback, then `.tst/rules/*`). Human path (TD-2802). | @@ -778,6 +779,7 @@ are stamped by a session's event log, `connection` events fix it at 1, and `ping | `local_servers` | connection | The servers on this computer that answered `scan_local_servers` with models, in config order, and every URL asked. | | `model_tested` | connection | The result of `test_model`: the reply, or what went wrong. | | `preset_readiness` | connection | Reply to `check_presets` (TD-5240): one row per catalog preset with `status` (`ready`, `no_key`, `server_down` or `no_grok`) and one sentence of `detail` for the picker's row. The detail names a key by its display name and a server by host and port only. | +| `preset_roles` | connection | Reply to `get_preset_roles` (TD-5241): for preset `preset`, the four maps `setup_state` carries for the active one: `tier_slugs`, `tier_credentials`, `tier_loopback` and `tier_cards`. All empty for a preset with no tiers (TD-5239). Never a key's value. | | `config_editor` | connection | Reply to `get_config_editor`, and the ack of each config edit (TD-5203). `defs` (every config model and its fields: kind, bounds, choices, default, description, unit) and `defaults` (reset values by JSON-encoded path) come on the reply to `get_config_editor` only; an edit's ack leaves both empty. `values` is the effective config with each `secrets` map's values replaced by where they live (`keychain` or `config`), never the value, and a password in a URL masked. `set_paths` lists every key path the file sets, and `locked_paths` each one it writes through a YAML anchor, alias or merge (those, and the keys under them, are edited in the file by hand); `version` identifies its bytes; `edit` names the edit acknowledged. | | `config_value_rejected` | connection | A config edit that was not written: `code` (`invalid`, `not_editable`, `secret`, `stale`, `not_found`, `write_failed`) and a message for the field at `path`, never echoing the value. The file is untouched (TD-5203). | | `diagnostics_report` | connection | Doctor results: one row per check, with a fix when it failed. The `skills` row names each skill's ignored frontmatter keys (TD-5107). | diff --git a/docs/models.md b/docs/models.md index 5ae2bb51..c5b1a581 100644 --- a/docs/models.md +++ b/docs/models.md @@ -300,8 +300,10 @@ credentials: name: My provider ``` -Settings → Models → **Build a team** edits the active entry's model and -key per role. +Settings → Model → **Build a team** edits a model's roles: the model, key +and context size of each. **Editing**, above it, chooses which of your +models that is. It starts on the one new chats use, and choosing another +there switches no chat and changes no default. ## When something goes wrong diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 1ad2a2fd..684ba8ff 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13664,3 +13664,33 @@ own error is still the authority. The loopback probe lives in `local_servers.py` beside the "Find models on this computer" scan and follows its rules. Class B entry in DECISIONS.md. +--- + +### TD-5241 — Settings → Model edits the model you choose + +**Size:** 2 · **Depends on:** TD-5237, TD-5239 · **Status:** Done + +"Build a team" edited the model new chats use and no other. To change a +second model's context size or key, a person first had to make it the +default, which also moved every new chat onto it. + +- [x] `get_preset_roles {name}` is answered with `preset_roles`: that + entry's `tier_slugs`, `tier_credentials`, `tier_loopback` and + `tier_cards`, equal to what `setup_state` carries once it is active; + empty for an entry with no roles; it changes nothing + (`test_preset_roles.py`). +- [x] Settings → Model has an "Editing" chooser of every model, starting on + the active one; choosing another switches no chat and no default + (`model-edit.test.ts::Settings → Model`). +- [x] Every save (a role's model, key, context size, and Refresh) names the + chosen model, and a context size is compared with the chosen model's + (`model-edit.test.ts::saving a role of the chosen model`). +- [x] The chosen model's roles are shown only as the daemon sent them: none + until its answer arrives; read again after each write; let go when + the model is removed or becomes the active one. +- [x] The UI walk lists the chooser (`e2e/census.json`). + +**Notes:** `setup_state` and its meaning are unchanged, so Settings → API +keys and every older client still read the active model's roles from it. +Class B entry in DECISIONS.md. + diff --git a/ui/e2e/census.json b/ui/e2e/census.json index 100e6bfd..8d0a7b48 100644 --- a/ui/e2e/census.json +++ b/ui/e2e/census.json @@ -226,6 +226,7 @@ "Duplicate Main", "Duplicate Walk team", "How models work", + "Model to edit", "None (no key) OpenRouter", "Refresh from servers", "Remove Fast", diff --git a/ui/src/lib/client.ts b/ui/src/lib/client.ts index 8fe3c406..4c9550f8 100644 --- a/ui/src/lib/client.ts +++ b/ui/src/lib/client.ts @@ -107,6 +107,7 @@ const KNOWN_EVENT_TYPES = new Set([ "local_servers", // TD-5180 "model_tested", // TD-5172 "preset_readiness", // TD-5240 + "preset_roles", // TD-5241 "config_editor", // TD-5203 "config_value_rejected", // TD-5203 "diagnostics_report", // TD-1104 doctor diff --git a/ui/src/lib/components/SettingsModels.svelte b/ui/src/lib/components/SettingsModels.svelte index a89cd7e0..62c56c26 100644 --- a/ui/src/lib/components/SettingsModels.svelte +++ b/ui/src/lib/components/SettingsModels.svelte @@ -9,6 +9,9 @@ closeSettings, refreshModelCards, saveContextWindow, + chooseEditPreset, + editTarget, + roleView, } from "../settings.svelte.js"; import { cardLine, cardNote } from "../model-card"; import { modelLabel } from "../effort"; @@ -30,13 +33,24 @@ // TD-5239: a model that is the Grok engine may have no roles. The daemon // then sends none, and there is nothing for "Build a team" to edit. + // TD-5241: the page edits the model chosen here, not only the one new + // chats use. Its roles come from the daemon either way. + const target = $derived(editTarget()); + const roles = $derived(roleView()); const noRoles = $derived( settings.hasModel && - settings.activePreset !== null && - settings.presetEngines[settings.activePreset] === "grok" && - Object.keys(settings.tierSlugs).length === 0, + target !== null && + settings.presetEngines[target] === "grok" && + Object.keys(roles.slugs).length === 0, ); + // What was typed for one model's role is not a draft for another's. + function pickEdit(name: string): void { + drafts = {}; + windowDrafts = {}; + chooseEditPreset(name); + } + // Both are modal: close Settings so the walkthrough is on top. function addModel(): void { closeSettings(); @@ -52,12 +66,12 @@ let drafts = $state>({}); function slugValue(tier: string): string { - return drafts[tier] ?? settings.tierSlugs[tier] ?? ""; + return drafts[tier] ?? roles.slugs[tier] ?? ""; } function commitSlug(tier: string): void { const next = slugValue(tier).trim(); - if (next === "" || next === settings.tierSlugs[tier]) return; + if (next === "" || next === roles.slugs[tier]) return; saveSlug(tier, next); } @@ -65,7 +79,7 @@ let windowDrafts = $state>({}); function windowValue(tier: string): string { - return windowDrafts[tier] ?? String(settings.tierCards[tier]?.context_window ?? ""); + return windowDrafts[tier] ?? String(roles.cards[tier]?.context_window ?? ""); } function commitWindow(tier: string): void { @@ -98,18 +112,36 @@ +{#if settings.hasModel && target !== null && settings.presets.length > 1} + +

+ Choosing a model here does not switch any chat or the model new chats use. +

+{/if} {#if noRoles}

- {modelLabel(settings.activePreset ?? "", settings.presetLabels)} runs on the Grok CLI, which - brings its own model, so there are no roles to set here. + {modelLabel(target ?? "", settings.presetLabels)} runs on the Grok CLI, which brings its own + model, so there are no roles to set here.

-{:else if settings.hasModel && settings.activePreset} +{:else if settings.hasModel && target !== null}

Build a team

Every turn has three roles. One model can fill all of them, and that is the normal setup. A team gives each role its own model: worth it when a strong, costly model should plan while a - cheaper or local one does the bulk of the work. Edits here change the active model's entry in - your config.yaml and apply to new sessions. + cheaper or local one does the bulk of the work. Edits here change the entry of the model + chosen above in your config.yaml and apply to new sessions.

{#each TIERS as tier (tier)}
" + + "

Hi

", + ); + expect(clean).toContain("h1{color:blue}"); + expect(clean).toContain("p{margin:0}"); + expect(clean).toContain("

Hi

"); + expect(clean).not.toContain(" { + expect(sanitizeArtifactHtml("just words")).toBe("just words"); + expect(sanitizeArtifactHtml("")).toBe(""); + }); +}); + +describe("artifactPreviewSrcdoc", () => { + it("still puts the policy first, and only once, around the cleaned markup", () => { + const srcdoc = artifactPreviewSrcdoc( + 'x', + ); + expect(srcdoc.match(/Content-Security-Policy/g)).toHaveLength(1); + expect(srcdoc).toContain(HTML_PREVIEW_CSP); + expect(srcdoc).not.toContain("default-src *"); + expect(srcdoc).not.toContain("evil.example"); + expect(srcdoc.indexOf("Content-Security-Policy")).toBeLessThan(srcdoc.indexOf("")); + }); + + it("does not loosen the frame", () => { + expect(HTML_PREVIEW_SANDBOX).toBe(""); + expect(STATIC_PREVIEW_NOTE).toBe("Static preview. Links and scripts are off."); + }); +}); diff --git a/ui/src/lib/artifact-html.ts b/ui/src/lib/artifact-html.ts new file mode 100644 index 00000000..dc98f747 --- /dev/null +++ b/ui/src/lib/artifact-html.ts @@ -0,0 +1,86 @@ +// TD-5262: the HTML an artifact preview shows is written by a model, and a +// model can be talked into writing anything. The frame already runs no +// script (an empty sandbox) and loads nothing (a `default-src 'none'` +// policy, first in the document). Two things were left: a link could send +// the frame to any address when clicked, carrying whatever the model put +// in it, and tags such as `` or `` are +// not all governed by the policy in every webview. So the markup is +// cleaned first: nothing that navigates, loads, posts or refreshes. +// +// This is a second layer. The sandbox and the policy stay exactly as they +// were; `htmlPreviewSrcdoc` still wraps the result. + +import DOMPurify from "dompurify"; + +import { htmlPreviewSrcdoc } from "./artifacts"; + +/** Shown above an HTML preview so a dead link is not a surprise. */ +export const STATIC_PREVIEW_NOTE = "Static preview. Links and scripts are off."; + +const FORBID_TAGS = [ + "script", + "iframe", + "frame", + "frameset", + "object", + "embed", + "form", + "base", + "meta", + "link", + "template", + "noscript", +]; + +// Everything that names an address the frame could go to or fetch, and the +// form attributes that would post. Event handlers go by DOMPurify's default. +const FORBID_ATTR = [ + "href", + "xlink:href", + "ping", + "action", + "formaction", + "srcset", + "background", + "poster", + "target", +]; + +// Its own instance: the hook below must not reach markdown or mermaid. +let purifier: ReturnType | null = null; + +function instance(): ReturnType { + if (purifier !== null) return purifier; + purifier = DOMPurify(window); + // `src` survives only as an inline `data:` image. Anything else is an + // address, and the policy's own `img-src data:` says the same. + purifier.addHook("uponSanitizeAttribute", (_node, data) => { + if (data.attrName === "src" && !/^data:image\//i.test(data.attrValue.trim())) { + data.keepAttr = false; + } + }); + return purifier; +} + +/** `html` with everything that could navigate, load, post or refresh taken + * out. Styles stay: the frame's policy already confines what they reach. */ +export function sanitizeArtifactHtml(html: string): string { + const root = instance().sanitize(html, { + WHOLE_DOCUMENT: true, + RETURN_DOM: true, + ADD_TAGS: ["style"], + FORBID_TAGS, + FORBID_ATTR, + ALLOW_DATA_ATTR: false, + ALLOW_UNKNOWN_PROTOCOLS: false, + }) as unknown as Element; + // A whole document keeps its styles in the head; the preview wraps a body. + const styles = [...root.querySelectorAll("head style")].map((el) => el.outerHTML).join(""); + return styles + (root.querySelector("body")?.innerHTML ?? ""); +} + +/** The `srcdoc` an HTML artifact is shown with: cleaned, then wrapped with + * the first-wins policy. */ +export function artifactPreviewSrcdoc(html: string): string { + return htmlPreviewSrcdoc(sanitizeArtifactHtml(html)); +} diff --git a/ui/src/lib/components/ArtifactPane.svelte b/ui/src/lib/components/ArtifactPane.svelte index 61180d9d..979d9583 100644 --- a/ui/src/lib/components/ArtifactPane.svelte +++ b/ui/src/lib/components/ArtifactPane.svelte @@ -15,11 +15,11 @@ asFencedMarkdown, artifactsEmptyCopy, copyArtifactSource, - htmlPreviewSrcdoc, HTML_PREVIEW_SANDBOX, openArtifactInEditor, workspaceOpenPath, } from '../artifacts'; + import { artifactPreviewSrcdoc, STATIC_PREVIEW_NOTE } from '../artifact-html'; import { artifacts, selectArtifact } from '../artifacts.svelte.js'; import { machineNameFor } from '../machines.svelte.js'; import { staysOnCopy } from '../machines'; @@ -118,11 +118,12 @@ {:else if preview.source === null} {:else if preview.kind === 'html'} +

{STATIC_PREVIEW_NOTE}

{:else if preview.kind === 'markdown'} @@ -270,6 +271,13 @@ min-height: 0; } + .static-note { + margin: 0; + padding: var(--space-1) var(--space-3); + font-size: var(--text-xs); + color: var(--color-ink-muted); + } + .html-frame { flex: 1; width: 100%; From 2029c08f602d80a49a474161c9a277a08423f588 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:02:13 +0000 Subject: [PATCH 18/35] TD-5267: an MCP server does not get the daemon's secrets from its environment A stdio MCP server was started with the daemon's whole environment, or inherited it outright when it had no values of its own. A key exported in the person's shell profile for some other tool went to every MCP server they listed. A stdio server now starts with the scrubbed environment the shell tool already uses (sanitized_env): nothing whose name looks like a key, token, password or auth setting, nothing whose value carries a credential in a URL. Ordinary variables pass, and a name a server is given in its own env is passed whatever it looks like. Hygiene, not containment: a deny-list, with the socket variables left in place. The per-OS allow-list needs trying with real servers on three systems first and is a separate story. Co-Authored-By: Claude Fable 5.2 --- core/tests/test_mcp_secrets.py | 40 +++++++++++++++++++++++++++++++++- core/tstd/mcp/stdio.py | 9 ++++++-- docs/configuration.md | 2 +- docs/tst-desk-backlog.md | 32 +++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/core/tests/test_mcp_secrets.py b/core/tests/test_mcp_secrets.py index ed1edcfe..5e074f2e 100644 --- a/core/tests/test_mcp_secrets.py +++ b/core/tests/test_mcp_secrets.py @@ -238,11 +238,49 @@ async def test_a_secret_reaches_only_its_own_child( assert await _getenv(dispatcher, "beta", "TD5110_TOKEN") == "" assert await _getenv(dispatcher, "beta", "TD5110_MODE") == "" assert "TD5110_TOKEN" not in os.environ - # The child still has the daemon's environment underneath. + # The child still has the ordinary variables it needs to start. assert await _getenv(dispatcher, "alpha", "PATH") == os.environ["PATH"] finally: await supervisor.aclose() + async def test_a_server_does_not_get_the_daemons_own_secrets( + self, + memory_keychain: MemoryKeychain, + fake_server: list[str], + monkeypatch: pytest.MonkeyPatch, + ) -> None: + """TD-5267: a key in the daemon's environment is not every server's key.""" + monkeypatch.setenv("OPENAI_API_KEY", "sk-not-for-a-server") + monkeypatch.setenv("GITHUB_TOKEN", "ghp-not-for-a-server") + monkeypatch.setenv("MYSQL_PWD", "not-for-a-server") + monkeypatch.setenv("DATABASE_URL", "postgres://user:hunter2@db.internal/app") + monkeypatch.setenv("TD5267_ORDINARY", "fine") + config = McpConfig( + servers={ + # One with values of its own, one with none: both are scrubbed. + "alpha": McpServerConfig( + transport="stdio", command=fake_server, env={"TD5110_MODE": "plain"} + ), + "beta": McpServerConfig(transport="stdio", command=fake_server), + # A server that is given one of those names, by name, still gets it. + "gamma": McpServerConfig( + transport="stdio", command=fake_server, env={"GITHUB_TOKEN": "its-own"} + ), + } + ) + supervisor, _, dispatcher = await _load(config) + try: + assert [row.status for row in supervisor.doctor_rows()] == ["ok", "ok", "ok"] + for server in ("alpha", "beta"): + for name in ("OPENAI_API_KEY", "GITHUB_TOKEN", "MYSQL_PWD", "DATABASE_URL"): + assert await _getenv(dispatcher, server, name) == "", (server, name) + assert await _getenv(dispatcher, server, "TD5267_ORDINARY") == "fine" + assert await _getenv(dispatcher, server, "PATH") == os.environ["PATH"] + assert await _getenv(dispatcher, "gamma", "GITHUB_TOKEN") == "its-own" + assert await _getenv(dispatcher, "gamma", "OPENAI_API_KEY") == "" + finally: + await supervisor.aclose() + async def test_a_missing_item_fails_that_server_and_names_it( self, memory_keychain: MemoryKeychain, fake_server: list[str] ) -> None: diff --git a/core/tstd/mcp/stdio.py b/core/tstd/mcp/stdio.py index 31b8f1e1..277cf3aa 100644 --- a/core/tstd/mcp/stdio.py +++ b/core/tstd/mcp/stdio.py @@ -5,11 +5,11 @@ import asyncio import contextlib import json -import os from collections.abc import Mapping from typing import Any from ..logging import get_logger +from ..tools.shell import sanitized_env from .calls import ( CallTimedOut, ToolsChanged, @@ -93,7 +93,12 @@ async def start(self) -> None: stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, limit=STREAM_LIMIT, - env={**os.environ, **self._env} if self._env else None, + # TD-5267: never the daemon's whole environment. A server + # is somebody else's program; it gets the ordinary + # variables it needs to start and none that look like a + # key, a token, a password or a credential in a URL, plus + # what this server was given by name. + env={**sanitized_env(), **self._env}, ) except OSError as e: raise McpError(f"failed to start stdio MCP: {e}") from e diff --git a/docs/configuration.md b/docs/configuration.md index 77de64a4..c6eb933c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1747,7 +1747,7 @@ loaded five tools. | `startup_timeout_seconds` | number > 0 | `8` | Time allowed for each step of the handshake (`server/discover` or `initialize`, then `tools/list`), whatever `timeout_seconds` says. Raise it for a server that loads a model before it answers. | | `max_call_seconds` | number > 0 | `600` | Hard cap on one tool call, stdio or http, progress or not. Never below `timeout_seconds`. | | `read_only_tools` | list of strings | `[]` | The server's tools, by the name the server gives them (not the `__` name), that only read (TD-5123). Read-only plan mode lets these run and refuses every other tool of the server. Only this list counts: a server's own `readOnlyHint` is not trusted. Names are matched exactly, no globs. It changes nothing outside plan mode: a listed tool still asks like any MCP tool. Settings keeps it when it toggles or edits the server. | -| `env` | mapping of name → string | `{}` | stdio only. Extra environment for this server's child, on top of the daemon's. Names are `[A-Za-z_][A-Za-z0-9_]*`. A value is a literal or `keychain:` (entry: lowercase letters, digits, `_` and `-`, at most 64). Numbers become strings; a YAML boolean is refused, so quote it. A value may not hold a line break or NUL. | +| `env` | mapping of name → string | `{}` | stdio only. Extra environment for this server's child, on top of the daemon's. Names are `[A-Za-z_][A-Za-z0-9_]*`. A value is a literal or `keychain:` (entry: lowercase letters, digits, `_` and `-`, at most 64). Numbers become strings; a YAML boolean is refused, so quote it. A value may not hold a line break or NUL. A stdio server does not get the rest of the daemon's environment as it is (TD-5267): variables whose name looks like a key, token, password or auth setting, and any whose value carries a credential in a URL, are left out, the same scrub the shell tool uses. `PATH`, `HOME`, proxy and locale variables pass through. A name listed here is passed whatever it looks like. | | `headers` | mapping of name → string | `{}` | http only. Extra headers on every request to this server, same value rules as `env`. Names are HTTP field names, one spelling each (case is ignored). `Accept`, `Connection`, `Content-Length`, `Content-Type`, `Host`, `Transfer-Encoding` and MCP's own `MCP-Protocol-Version`, `Mcp-Session-Id`, `Mcp-Method`, `Mcp-Name` and `Mcp-Param-*` are TST Desk's own and are refused. Another `Mcp-` name, such as a server's `Mcp-Api-Key`, is allowed (TD-5161). | diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 5e4854a5..b5952445 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13925,3 +13925,35 @@ in fact have followed a clicked link was not run; the fix does not depend on the answer. The sanitizer has its own DOMPurify instance, so its rule for `src` does not reach markdown or diagrams. +--- + +### TD-5267 — An MCP server does not get the daemon's secrets from its environment + +**Size:** 1 · **Depends on:** TD-4401, TD-5110, TD-605 · **Status:** Done + +A stdio MCP server was started with the daemon's whole environment, or +inherited it outright when it had no values of its own. A key the person +had exported in their shell profile for some other tool (`OPENAI_API_KEY`, +`GITHUB_TOKEN`, a database URL with a password) went to every MCP server +they listed. TD-5110 had made a server's *own* token private to it; the +daemon's were still shared with all of them. + +- [x] A stdio server starts with the scrubbed environment the shell tool + uses (`sanitized_env`): no variable whose name looks like a key, token, + password or auth setting, none whose value carries a credential in a + URL (`test_mcp_secrets.py::TestSpawn::test_a_server_does_not_get_the_daemons_own_secrets`). +- [x] It applies to a server with values of its own and to one with none. +- [x] Ordinary variables still pass (`PATH`), and a name a server is given + in its own `env` is passed whatever it looks like. +- [x] A server's own keychain value still reaches only that server + (the existing TD-5110 tests). + +**Notes:** Hygiene, not containment. It is a deny-list, so an unknown name +gets through, and it leaves the agent and bus socket variables in place; a +same-user process can read other things besides. The fuller fix is an +allow-list per operating system, as the MCP SDKs use, with a per-server +list of extra names and a working directory. That needs each list tried +with real servers on macOS, Windows and Linux before it ships, because a +list that is one variable short stops a server starting; it is a separate +story. Sandboxing the server process is the containment step. + From d6e6c44e128c6b6c1894e1fe150e08aca687d1a2 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:08:24 +0000 Subject: [PATCH 19/35] TD-5268: an approval card says which child is asking A delegate or workflow child's call parks on its parent's session, and the card read as if the session's own agent had asked. A child's card also came back on every reattach, because the child's call never reports a result on the session's log. approval_request gains an optional worker: the child's id, the call that started it, delegate or workflow, its kind and name, a workflow task's number and label, the start of its task, its tier's name and its worktree's branch. It is filled where the child's dispatcher is built, only when the handler is the parent session's own. The child's summary starts with who is asking, so every surface that shows the summary says it, an older phone build included. No Always allow is offered on a named worker's card or for a path tool in a child's own worktree: the saved rule would not do what it says. The card shows a line naming the child, and goes when the call that started the child returns. Class B: recorded in DECISIONS.md. Co-Authored-By: Claude Fable 5.2 --- DECISIONS.md | 23 +++ core/tests/test_approval_worker.py | 209 ++++++++++++++++++++++ core/tstd/protocol.py | 29 +++ core/tstd/session.py | 22 +++ core/tstd/tools/delegate.py | 93 +++++++++- core/tstd/tools/workflow.py | 6 + docs/tst-desk-backlog.md | 37 ++++ ui/src/lib/approval-store.svelte.ts | 13 +- ui/src/lib/approval-worker.test.ts | 75 ++++++++ ui/src/lib/approval.test.ts | 2 + ui/src/lib/approval.ts | 40 ++++- ui/src/lib/components/ApprovalCard.svelte | 17 +- ui/src/lib/protocol-fixtures.json | 6 +- ui/src/lib/protocol.ts | 18 ++ 14 files changed, 583 insertions(+), 7 deletions(-) create mode 100644 core/tests/test_approval_worker.py create mode 100644 ui/src/lib/approval-worker.test.ts diff --git a/DECISIONS.md b/DECISIONS.md index 9685833b..7f6d6647 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -20130,3 +20130,26 @@ policy verbs, `add_model`). They need the owner's call one by one; an explicit phone allowlist with a test that classifies every verb would stop the next one being missed. +## 2026-11-20 — TD-5268: an optional `worker` on `approval_request`, and who is asking in the summary (Class B) + +**Decision:** `ApprovalRequest` gains `worker: ApprovalWorker | None`, +filled where a child's dispatcher is built (`_name_the_child`), only when +the approval handler is the parent session's own. The child's `summary` is +prefixed with who is asking. `Session.request_approval` takes a +keyword-only `worker` and withholds the Always-allow proposal for a named +worker and for a path tool in a child's own worktree. + +**Rationale:** the child is deliberately not a registered session, so the +parent's event is the only place its identity can travel. Putting it in +`summary` as well as in a field means every surface that already shows the +summary is correct today, including a phone app built before the field +existed. Always allow keeps its workspace scope (a per-worker allow would +be a loosening for one worker, and worker rules are tighten-only), so the +two cases where a workspace rule would be saved and then not apply are +simply not offered. + +**Rejected:** registering children as sessions to give them cards of their +own (reverses TD-4602); deriving the child from the tool-call id's prefix +in the window (the window would be parsing an id format it was never +promised). + diff --git a/core/tests/test_approval_worker.py b/core/tests/test_approval_worker.py new file mode 100644 index 00000000..e2fbf245 --- /dev/null +++ b/core/tests/test_approval_worker.py @@ -0,0 +1,209 @@ +"""An approval card says which child is asking (TD-5268). + +A ``delegate`` or ``workflow`` child's call parks on its parent's session. +The card used to read as if the session's own agent had asked. These cases +run a real child against the session's own approval path and read the +event a window would get. +""" + +from __future__ import annotations + +import asyncio +from pathlib import Path +from typing import Any + +import pytest + +from tests.test_delegate import _bind_runtime, _parent_dispatcher, _workspace +from tstd.mock import MockProvider, Script +from tstd.policy import PolicyConfig +from tstd.protocol import ApprovalRequest, ApprovalWorker, parse_daemon_event +from tstd.session import Session +from tstd.tools.delegate import ChildTask, run_child_task, worker_summary + +SHELL = Script(kind="tool_call", tool_name="shell", tool_arguments='{"command": "make test"}') +DONE = Script(kind="text", content="ran it") + + +async def _asking_parent(tmp_path: Path) -> tuple[Session, Any]: + """A parent whose dispatcher asks through the session, as the daemon wires it.""" + ws = _workspace(tmp_path) + parent = Session(str(ws)) + dispatcher = _parent_dispatcher(ws) + dispatcher.policy = PolicyConfig() + parent.policy = dispatcher.policy + dispatcher.approval_handler = parent.request_approval + # A card parks a session that is in a turn. + await parent.set_state("running") + return parent, dispatcher + + +async def _first_card(parent: Session) -> ApprovalRequest: + for _ in range(300): + cards = [e for e in parent.event_log.all_events if isinstance(e, ApprovalRequest)] + if cards: + return cards[0] + await asyncio.sleep(0.01) + raise AssertionError("no approval card was raised") + + +async def _run_and_approve(parent: Session, spec: ChildTask) -> ApprovalRequest: + running = asyncio.create_task(run_child_task(parent, spec)) + try: + card = await _first_card(parent) + parent.resolve_approval(card.tool_call_id, approved=True) + await asyncio.wait_for(running, 10) + finally: + if not running.done(): + running.cancel() + return card + + +class TestTheCard: + async def test_a_delegate_childs_card_names_the_child(self, tmp_path: Path) -> None: + parent, dispatcher = await _asking_parent(tmp_path) + mock = MockProvider(sequences={"test-worker": [SHELL, DONE]}) + _bind_runtime(parent, dispatcher, mock) + spec = ChildTask(task="Run the test suite\nand report what fails", call_id="call_ab12") + + card = await _run_and_approve(parent, spec) + + assert card.worker is not None + assert card.worker.via == "delegate" and card.worker.kind == "worker" + assert card.worker.id == "call_ab12" and card.worker.parent_call_id == "call_ab12" + assert card.worker.index is None and card.worker.agent is None + assert card.worker.label == "Run the test suite and report what fails" + assert card.worker.tier == "worker" + assert card.worker.worktree_branch is None + # The child's call id still starts with the child's own. + assert card.tool_call_id.startswith("call_ab12/") + # Every surface that only shows the summary says who asked. + assert card.summary.startswith("Worker: ") + assert "make test" in card.summary + + async def test_a_workflow_tasks_card_has_its_number_and_label(self, tmp_path: Path) -> None: + parent, dispatcher = await _asking_parent(tmp_path) + mock = MockProvider(sequences={"test-worker": [SHELL, DONE]}) + _bind_runtime(parent, dispatcher, mock) + spec = ChildTask( + task="Check the API module", + call_id="call_wf.2", + parent_call_id="call_wf", + index=2, + label="API module", + ) + + card = await _run_and_approve(parent, spec) + + assert card.worker is not None + assert card.worker.via == "workflow" + assert card.worker.index == 2 and card.worker.label == "API module" + assert card.worker.parent_call_id == "call_wf" and card.worker.id == "call_wf.2" + assert card.summary.startswith("Worker 2: ") + + async def test_the_sessions_own_card_is_as_it_was(self, tmp_path: Path) -> None: + parent, dispatcher = await _asking_parent(tmp_path) + tool = dispatcher.registry.require("shell") + from tstd.autonomy.classifier import DecisionClass + + asking = asyncio.create_task( + parent.request_approval( + "call_own", tool, {"command": "make test"}, DecisionClass.B, "Run `make test`", "B" + ) + ) + card = await _first_card(parent) + parent.resolve_approval("call_own", approved=True) + await asyncio.wait_for(asking, 5) + assert card.worker is None + assert card.summary == "Run `make test`" + + async def test_a_long_task_is_cut_and_stays_one_line(self, tmp_path: Path) -> None: + parent, dispatcher = await _asking_parent(tmp_path) + mock = MockProvider(sequences={"test-worker": [SHELL, DONE]}) + _bind_runtime(parent, dispatcher, mock) + card = await _run_and_approve(parent, ChildTask(task="word " * 200, call_id="c1")) + assert card.worker is not None + assert len(card.worker.label) <= 60 and "\n" not in card.worker.label + assert len(card.worker.task) <= 300 and card.worker.task.endswith("…") + + async def test_the_event_crosses_the_wire_and_an_older_one_still_parses( + self, tmp_path: Path + ) -> None: + parent, dispatcher = await _asking_parent(tmp_path) + mock = MockProvider(sequences={"test-worker": [SHELL, DONE]}) + _bind_runtime(parent, dispatcher, mock) + card = await _run_and_approve(parent, ChildTask(task="t", call_id="c1")) + back = parse_daemon_event(card.model_dump_json()) + assert isinstance(back, ApprovalRequest) and back.worker == card.worker + old = card.model_dump(mode="json") + del old["worker"] + import json + + again = parse_daemon_event(json.dumps(old)) + assert isinstance(again, ApprovalRequest) and again.worker is None + + +class TestTheSummary: + @pytest.mark.parametrize( + ("worker", "expected"), + [ + ({"kind": "worker"}, "Worker: Run `x`"), + ({"kind": "research"}, "Worker (research): Run `x`"), + ({"kind": "worker", "index": 3}, "Worker 3: Run `x`"), + ({"kind": "research", "index": 3}, "Worker 3 (research): Run `x`"), + ({"kind": "worker", "agent": "db-migrator"}, 'Worker "db-migrator": Run `x`'), + ({"kind": "check", "index": 2}, "Check 2: Run `x`"), + ( + {"kind": "worker", "index": 1, "worktree_branch": "tst/wt/abc"}, + "Worker 1: Run `x` in its worktree", + ), + ], + ) + def test_who_is_asking_comes_first(self, worker: dict[str, Any], expected: str) -> None: + full = ApprovalWorker(id="c", parent_call_id="c", via="workflow", **worker) + assert worker_summary(full, "Run `x`") == expected + + +class TestAlwaysAllow: + async def _proposal(self, tmp_path: Path, worker: ApprovalWorker | None) -> str | None: + parent, dispatcher = await _asking_parent(tmp_path) + + async def writable() -> bool: + return True + + parent._config_writable = writable # type: ignore[method-assign] + tool = dispatcher.registry.require("fs_write") + from tstd.autonomy.classifier import DecisionClass + + asking = asyncio.create_task( + parent.request_approval( + "c1", + tool, + {"path": "notes.md", "content": "x"}, + DecisionClass.B, + "Write notes.md", + "B", + worker=worker, + ) + ) + card = await _first_card(parent) + parent.resolve_approval("c1", approved=False) + await asyncio.wait_for(asking, 5) + return card.proposed_always_allow + + def _worker(self, **over: Any) -> ApprovalWorker: + return ApprovalWorker(id="c", parent_call_id="c", via="delegate", kind="worker", **over) + + async def test_the_sessions_own_card_and_a_plain_childs_offer_it(self, tmp_path: Path) -> None: + assert await self._proposal(tmp_path, None) is not None + + async def test_a_plain_child_offers_it_too(self, tmp_path: Path) -> None: + assert await self._proposal(tmp_path, self._worker()) is not None + + async def test_a_named_workers_card_does_not(self, tmp_path: Path) -> None: + # Its own rule may be why it asked, and a workspace rule cannot lift that. + assert await self._proposal(tmp_path, self._worker(agent="db-migrator")) is None + + async def test_a_path_in_a_childs_own_worktree_does_not(self, tmp_path: Path) -> None: + # The folder goes when the child finishes: the rule would never match again. + assert await self._proposal(tmp_path, self._worker(worktree_branch="tst/wt/x")) is None diff --git a/core/tstd/protocol.py b/core/tstd/protocol.py index 86d6b79e..ae8bdd80 100644 --- a/core/tstd/protocol.py +++ b/core/tstd/protocol.py @@ -2595,6 +2595,32 @@ class PolicyRuleSummary(BaseModel): exact: str | None = None +class ApprovalWorker(BaseModel): + """Which child is asking, on a child's approval card (TD-5268). + + A ``delegate`` or ``workflow`` child's call parks on its parent's + session, and the card used to read as if the session's own agent had + asked. ``id`` is the prefix of the child's tool-call ids; + ``parent_call_id`` is the ``delegate`` or ``workflow`` call that started + it, so a window can drop the card when that call returns. ``tier`` is + the tier's name, never a model slug. + """ + + id: str + parent_call_id: str + via: Literal["delegate", "workflow"] + kind: Literal["worker", "research", "check"] + agent: str | None = None + # A workflow task's number, from 1. None for a ``delegate`` child. + index: int | None = None + label: str = "" + task: str = "" + tier: str = "" + # Set when the child works in a worktree of its own: the paths on the + # card are in that copy, not in the session's folder. + worktree_branch: str | None = None + + class ApprovalRequest(DaemonEvent): """A request for user approval of a tool call (TD-802). @@ -2620,6 +2646,9 @@ class ApprovalRequest(DaemonEvent): summary: str reason: str proposed_always_allow: PolicyRuleSummary | None = None + # TD-5268: the child that is asking. None: the session's own agent. + # Additive; an older viewer reads the same thing in ``summary``. + worker: ApprovalWorker | None = None class QuestionOption(BaseModel): diff --git a/core/tstd/session.py b/core/tstd/session.py index 1af70755..92211f35 100644 --- a/core/tstd/session.py +++ b/core/tstd/session.py @@ -34,6 +34,7 @@ stricter_rule, ) from .protocol import ( + ApprovalWorker, ConversationReset, DaemonEvent, LineCommentAnchor, @@ -195,6 +196,20 @@ class EventSubscriber(Protocol): async def __call__(self, event: DaemonEvent, log: SessionEventLog) -> None: ... +def _no_rule_from(worker: ApprovalWorker, tool: Tool) -> bool: + """Whether a child's card must not offer Always allow (TD-5268). + + Two cases where the saved rule would not do what the person thinks. A + named worker may be asking because of its *own* rule, which a workspace + ``auto`` rule cannot override, so it would be saved and the worker + would ask again. And a path in a child's own worktree is in a folder + that goes when the child finishes: the rule would never match again. + """ + return worker.agent is not None or ( + worker.worktree_branch is not None and bool(tool.path_fields) + ) + + class SessionEventLog: """Monotonic event log with a seq per session. @@ -880,9 +895,13 @@ async def request_approval( decision_class: DecisionClass, summary: str, reason: str, + *, + worker: ApprovalWorker | None = None, ) -> ApprovalOutcome: """Park a tool call awaiting user approval. + *worker* is the child that is asking (TD-5268); the card names it. + Logs the ``approval_request`` event (so reattaching clients replay it), parks the session in ``awaiting_approval``, and awaits a bare future — no spin, no poll. With ``approval_timeout_seconds`` set, @@ -911,6 +930,8 @@ async def request_approval( proposed = None if proposed is not None and not await self._config_writable(): proposed = None + if proposed is not None and worker is not None and _no_rule_from(worker, tool): + proposed = None await self.event_log.add( ApprovalRequestEvent( session_id=self.id, @@ -921,6 +942,7 @@ async def request_approval( summary=summary, reason=reason, proposed_always_allow=rule_summary(proposed) if proposed is not None else None, + worker=worker, seq=1, ) ) diff --git a/core/tstd/tools/delegate.py b/core/tstd/tools/delegate.py index 78bd987b..e4ba41a8 100644 --- a/core/tstd/tools/delegate.py +++ b/core/tstd/tools/delegate.py @@ -34,6 +34,7 @@ from dataclasses import dataclass, replace from typing import TYPE_CHECKING, Any +from ..autonomy import DecisionClass from ..config import DEFAULT_CHILD_MAX_ITERATIONS, ModelConfig, TierConfig, TierName from ..context import PromptAssembler from ..context.tokens import make_token_counter @@ -46,8 +47,9 @@ split_cut_calls, stub_call, ) +from ..policy import ApprovalOutcome from ..prompt_images import cap_prompt_images -from ..protocol import WorkerStep +from ..protocol import ApprovalWorker, WorkerStep from ..provider import ChatCompletionRequest, ChatMessage, ProviderError, ToolCall from ..session import Session from ..worktree import SessionWorktree @@ -160,6 +162,11 @@ class ChildTask: tier: TierName | None = None source: str = "worker" transcript_budget: int = TRANSCRIPT_CHAR_BUDGET + # TD-5268: what an approval card says about this child. A workflow sets + # all three; a ``delegate`` child is its call, with no number. + parent_call_id: str = "" + index: int | None = None + label: str = "" @dataclass(frozen=True) @@ -354,6 +361,9 @@ async def run_child_task(parent: Session, spec: ChildTask) -> ChildDone: tier=spec.tier, source=spec.source, transcript_budget=spec.transcript_budget, + parent_call_id=spec.parent_call_id, + index=spec.index, + label=spec.label, ) except HandlerRefusal as e: await close_child_tree(own_tree, spec.task, steps) @@ -427,6 +437,9 @@ async def _child_loop( tier: TierName | None = None, source: str = "worker", transcript_budget: int = TRANSCRIPT_CHAR_BUDGET, + parent_call_id: str = "", + index: int | None = None, + label: str = "", ) -> _ChildEnd: """:func:`run_worker_child`'s loop, also saying why it stopped early.""" parent, runtime = bound_runtime(parent, "delegate") @@ -453,6 +466,23 @@ async def _child_loop( names=allow_list, ) child_dispatcher = _child_dispatcher(parent, parent_dispatcher, child_registry, named) + _name_the_child( + parent, + parent_dispatcher, + child_dispatcher, + ApprovalWorker( + id=call_id, + parent_call_id=parent_call_id or call_id, + via="workflow" if index is not None or source == "check" else "delegate", + kind="check" if source == "check" else kind, + agent=named.name if named is not None else None, + index=index, + label=_one_line(label or task, 60), + task=_one_line(task, 300), + tier=tier or (named.tier if named is not None else "worker"), + worktree_branch=worktree.branch if worktree is not None else None, + ), + ) # TD-5126: a child in its own worktree is told that root, not the parent's. assembler = runtime.assembler @@ -680,6 +710,67 @@ def _child_cap_violation( return _cap_violation(parent, tracker, session_start, used, skip_all=False) +def _one_line(text: str, limit: int) -> str: + flat = " ".join(text.split()) + return flat if len(flat) <= limit else flat[: limit - 1].rstrip() + "…" + + +def worker_summary(worker: ApprovalWorker, summary: str) -> str: + """*summary* with who is asking in front (TD-5268). + + The card's title, the OS banner, the timeline row and every notify + channel show ``summary``, so a viewer that does not know the ``worker`` + field (an older phone build) still reads which child it is. + """ + if worker.kind == "check": + who = f"Check {worker.index}" if worker.index is not None else "Check" + elif worker.agent is not None: + who = f'Worker "{worker.agent}"' + elif worker.index is not None: + who = f"Worker {worker.index}" + else: + who = "Worker" + if worker.kind == "research" and worker.agent is None: + who += " (research)" + where = " in its worktree" if worker.worktree_branch is not None else "" + return f"{who}: {summary}{where}" + + +def _name_the_child( + parent: Session, + parent_dispatcher: ToolDispatcher, + child_dispatcher: ToolDispatcher, + worker: ApprovalWorker, +) -> None: + """Make the child's approval cards say which child is asking (TD-5268). + + Only when the handler is the parent session's own: a handler a test or + another host supplied keeps the six arguments it was written for. + """ + if parent_dispatcher.approval_handler != parent.request_approval: + return + + async def ask( + tool_call_id: str, + tool: Tool, + arguments: dict[str, Any], + decision_class: DecisionClass, + summary: str, + reason: str, + ) -> ApprovalOutcome: + return await parent.request_approval( + tool_call_id, + tool, + arguments, + decision_class, + worker_summary(worker, summary), + reason, + worker=worker, + ) + + child_dispatcher.approval_handler = ask + + def _child_dispatcher( parent: Session, parent_dispatcher: ToolDispatcher, diff --git a/core/tstd/tools/workflow.py b/core/tstd/tools/workflow.py index 333a1b16..912dcc01 100644 --- a/core/tstd/tools/workflow.py +++ b/core/tstd/tools/workflow.py @@ -208,6 +208,9 @@ async def one(plan: _Plan) -> _Outcome: isolate_writer=len(plans) >= 2, call_id=f"{base}.{plan.number}", transcript_budget=budget, + parent_call_id=base, + index=plan.number, + label=plan.label, ) outcome = await _run(parent, spec) if not check or outcome.failed is not None: @@ -294,6 +297,9 @@ async def _check( tier=tier, source="check", transcript_budget=budget, + parent_call_id=call_id.split(".", 1)[0], + index=plan.number, + label=plan.label, ) checked = await _run(parent, spec) if checked.failed is not None: diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index b5952445..e827ebd7 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13957,3 +13957,40 @@ with real servers on macOS, Windows and Linux before it ships, because a list that is one variable short stops a server starting; it is a separate story. Sandboxing the server process is the containment step. +--- + +### TD-5268 — An approval card says which child is asking + +**Size:** 3 · **Depends on:** TD-4602, TD-5195, TD-803 · **Status:** Done + +A `delegate` or `workflow` child's call parks on its parent's session, +and the card read as if the session's own agent had asked: nothing said +which of several workers wanted to run a command, that a path was in a +child's own worktree, or that the request came from a child at all. A +child's card also came back on every reattach, because the child's call +never reports a result on the session's log. + +- [x] `approval_request` carries an optional `worker`: the child's id, + the call that started it, `delegate` or `workflow`, its kind, its + name when it has one, a workflow task's number and label, the first + 300 characters of its task, its tier's name, and its worktree's + branch (`test_approval_worker.py::TestTheCard`). +- [x] A child's `summary` starts with who is asking, so the card title, + the OS banner, the timeline and every notify channel say it, an + older phone build included (`TestTheSummary`). +- [x] The session's own cards are unchanged, and an event without the + field still parses. +- [x] No Always allow is offered on a named worker's card, or for a path + tool in a child's own worktree: the saved rule would not do what it + says (`TestAlwaysAllow`). +- [x] The card shows a line saying which child is asking, on what, and + when its paths are in a copy; the tick box says the rule is for the + main agent and every worker (`approval-worker.test.ts`). +- [x] A child's card goes when the `delegate` or `workflow` call that + started it returns (`settledBy`). + +**Notes:** The minimal step from the research note. Not done here: grouping +a worker's cards, keeping keyboard focus on the card being read when a +second arrives, and an audit row for a child's own tool calls. Class B +entry in DECISIONS.md. + diff --git a/ui/src/lib/approval-store.svelte.ts b/ui/src/lib/approval-store.svelte.ts index 003d0645..3ae441de 100644 --- a/ui/src/lib/approval-store.svelte.ts +++ b/ui/src/lib/approval-store.svelte.ts @@ -16,7 +16,14 @@ // a bridged session is decided only while its computer is connected, and one // already sent comes back if the hub reports it never got there. -import { alwaysAllowMessage, approvalFromEvent, approveMessage, denyMessage, type PendingApproval } from './approval'; +import { + alwaysAllowMessage, + approvalFromEvent, + approveMessage, + denyMessage, + settledBy, + type PendingApproval, +} from './approval'; import { isBridged } from './bridged-id'; import { onDaemonEvent, sendToDaemon } from './connection-status.svelte.js'; import { sessionMachineUp } from './machines.svelte.js'; @@ -43,6 +50,10 @@ onDaemonEvent((event) => { if (boundSessionId === null || event.session_id !== boundSessionId) return; sentRemote.delete(event.tool_call_id); dismiss(event.tool_call_id); + // TD-5268: a child's cards go when the call that started the child returns. + for (const card of pending.filter((p) => settledBy(p, event.tool_call_id))) { + dismiss(card.toolCallId); + } } else if (event.type === 'error' && event.code === 'machine_unreachable') { if (boundSessionId === null || event.session_id !== boundSessionId) return; restoreUndelivered(); diff --git a/ui/src/lib/approval-worker.test.ts b/ui/src/lib/approval-worker.test.ts new file mode 100644 index 00000000..1e98080c --- /dev/null +++ b/ui/src/lib/approval-worker.test.ts @@ -0,0 +1,75 @@ +// TD-5268: a child's approval card says which child is asking, and goes +// when the call that started the child returns. + +import { describe, expect, it } from "vitest"; + +import { approvalFromEvent, settledBy, workerLine } from "./approval"; +import type { ApprovalRequest, ApprovalWorker } from "./protocol"; + +function event(worker?: Partial | null): ApprovalRequest { + return { + type: "approval_request", + seq: 7, + session_id: "s1", + tool_call_id: worker ? "call_wf.2/call_0" : "call_own", + tool_name: "shell", + arguments: { command: "make test" }, + decision_class: "B", + summary: "Run `make test`", + reason: "class B", + ...(worker === undefined + ? {} + : { + worker: + worker === null + ? null + : { id: "call_wf.2", parent_call_id: "call_wf", via: "workflow", kind: "worker", ...worker }, + }), + } as ApprovalRequest; +} + +describe("workerLine", () => { + it("is empty for the session's own agent, and for an older daemon's event", () => { + expect(workerLine(approvalFromEvent(event()))).toBe(""); + expect(workerLine(approvalFromEvent(event(null)))).toBe(""); + expect(approvalFromEvent(event()).worker).toBeNull(); + }); + + it("says which child, on what", () => { + expect(workerLine(approvalFromEvent(event({ index: 2, label: "API module" })))).toBe( + "A worker is asking, on: API module.", + ); + expect(workerLine(approvalFromEvent(event({ kind: "research", label: "Read the docs" })))).toBe( + "A research worker is asking, on: Read the docs.", + ); + expect(workerLine(approvalFromEvent(event({ kind: "check", index: 2 })))).toBe( + "A check is asking.", + ); + expect(workerLine(approvalFromEvent(event({ agent: "db-migrator", label: "Migrate" })))).toBe( + 'The worker "db-migrator" is asking, on: Migrate.', + ); + }); + + it("says when the paths are in the child's own copy", () => { + const line = workerLine( + approvalFromEvent(event({ label: "Refactor", worktree_branch: "tst/wt/abc" })), + ); + expect(line).toContain("a copy of its own (tst/wt/abc)"); + expect(line).toContain("the paths here are in that copy"); + }); +}); + +describe("settledBy", () => { + it("is the call itself for the session's own card", () => { + const own = approvalFromEvent(event()); + expect(settledBy(own, "call_own")).toBe(true); + expect(settledBy(own, "call_wf")).toBe(false); + }); + + it("is also the delegate or workflow call for a child's card", () => { + const child = approvalFromEvent(event({})); + expect(settledBy(child, "call_wf.2/call_0")).toBe(true); + expect(settledBy(child, "call_wf")).toBe(true); + expect(settledBy(child, "call_other")).toBe(false); + }); +}); diff --git a/ui/src/lib/approval.test.ts b/ui/src/lib/approval.test.ts index 95a639e0..ebab92dc 100644 --- a/ui/src/lib/approval.test.ts +++ b/ui/src/lib/approval.test.ts @@ -46,6 +46,8 @@ describe("approvalFromEvent", () => { summary: "Write src/app.py", reason: "decision class B requires approval", proposedAlwaysAllow: null, + // TD-5268: no child is asking. + worker: null, }); }); diff --git a/ui/src/lib/approval.ts b/ui/src/lib/approval.ts index 157ad8ec..c9ae261f 100644 --- a/ui/src/lib/approval.ts +++ b/ui/src/lib/approval.ts @@ -5,7 +5,14 @@ // Kept free of runes and Tauri imports so it unit-tests under vitest's node // environment (like timeline.ts). The runes layer lives in approval-store.svelte.ts. -import type { AlwaysAllow, ApprovalRequest, Approve, Deny, PolicyRuleSummary } from './protocol'; +import type { + AlwaysAllow, + ApprovalRequest, + ApprovalWorker, + Approve, + Deny, + PolicyRuleSummary, +} from './protocol'; /** A pending approval, reduced from the daemon event to what the card shows. */ export interface PendingApproval { @@ -18,6 +25,8 @@ export interface PendingApproval { reason: string; /** Rule the daemon proposes to auto-approve this call forever (TD-803), or null. */ proposedAlwaysAllow: PolicyRuleSummary | null; + /** TD-5268: the child that is asking, or null for the session's own agent. */ + worker: ApprovalWorker | null; } /** Map an approval_request event to the card's pending-approval shape. */ @@ -31,9 +40,38 @@ export function approvalFromEvent(event: ApprovalRequest): PendingApproval { summary: event.summary, reason: event.reason, proposedAlwaysAllow: event.proposed_always_allow ?? null, + worker: event.worker ?? null, }; } +/** The line under a child's card title: which child, doing what, and where + * its files are. "" for the session's own agent (TD-5268). */ +export function workerLine(approval: PendingApproval): string { + const worker = approval.worker; + if (worker === null) return ""; + const who = + worker.kind === "check" + ? "A check" + : worker.agent + ? `The worker "${worker.agent}"` + : worker.kind === "research" + ? "A research worker" + : "A worker"; + const what = worker.label ? `, on: ${worker.label}` : ""; + const where = worker.worktree_branch + ? `. It works in a copy of its own (${worker.worktree_branch}), so the paths here are in that copy` + : ""; + return `${who} is asking${what}${where}.`; +} + +/** Whether a finished call takes this card with it: the call itself, or the + * delegate or workflow call whose child was asking (TD-5268). A child's own + * call never reports a result on the session's log, so without this its card + * came back on every reattach. */ +export function settledBy(approval: PendingApproval, toolCallId: string): boolean { + return approval.toolCallId === toolCallId || approval.worker?.parent_call_id === toolCallId; +} + /** * Class-C calls are the dangerous ones (the wall — policy can never run * them automatically); class B merely requires approval. Class A never diff --git a/ui/src/lib/components/ApprovalCard.svelte b/ui/src/lib/components/ApprovalCard.svelte index a7198678..f2ae4dbe 100644 --- a/ui/src/lib/components/ApprovalCard.svelte +++ b/ui/src/lib/components/ApprovalCard.svelte @@ -6,7 +6,7 @@ // decision they must make: ⏎ approves, Esc denies, from the card itself. import { onMount } from 'svelte'; import Icon from './Icon.svelte'; - import { alwaysAllowLabel, isAlwaysAllowable, isDangerous } from '../approval'; + import { alwaysAllowLabel, isAlwaysAllowable, isDangerous, workerLine } from '../approval'; import { alwaysAllow, approve, canDecide, deny } from '../approval-store.svelte.js'; import { approvalWaitCopy } from '../machines'; import { machineNameFor } from '../machines.svelte.js'; @@ -94,6 +94,9 @@
{approval.summary} + {#if workerLine(approval)} + {workerLine(approval)} + {/if} {approval.toolName} @@ -130,7 +133,11 @@ bind:checked={remember} aria-label={`Always allow in this workspace: ${ruleLabel}`} /> - Always allow this in this workspace + {approval.worker === null + ? 'Always allow this in this workspace' + : 'Always allow this in this workspace, for the main agent and every worker'} {/if}
@@ -205,6 +212,12 @@ min-width: 0; } + .who { + font-size: var(--text-sm); + color: var(--color-ink-secondary); + overflow-wrap: anywhere; + } + .title { font-size: var(--text-base); font-weight: var(--weight-medium); diff --git a/ui/src/lib/protocol-fixtures.json b/ui/src/lib/protocol-fixtures.json index 692b4d72..325b64dd 100644 --- a/ui/src/lib/protocol-fixtures.json +++ b/ui/src/lib/protocol-fixtures.json @@ -1343,7 +1343,8 @@ "decision_class": "C", "summary": "Write to test.txt", "reason": "decision class C requires approval", - "proposed_always_allow": null + "proposed_always_allow": null, + "worker": null }, "approval_request_always_allow": { "type": "approval_request", @@ -1362,7 +1363,8 @@ "args": "npm test", "effect": "auto", "exact": "npm test" - } + }, + "worker": null }, "decision_logged": { "type": "decision_logged", diff --git a/ui/src/lib/protocol.ts b/ui/src/lib/protocol.ts index 7b4d3e6e..f1f8bcf5 100644 --- a/ui/src/lib/protocol.ts +++ b/ui/src/lib/protocol.ts @@ -1782,6 +1782,22 @@ export interface PolicyRuleSummary { exact?: string | null; } +/** TD-5268: which child is asking, on a child's approval card. */ +export interface ApprovalWorker { + id: string; + parent_call_id: string; + via: "delegate" | "workflow"; + kind: "worker" | "research" | "check"; + agent?: string | null; + /** A workflow task's number, from 1. */ + index?: number | null; + label?: string; + task?: string; + tier?: string; + /** Set when the child works in a worktree of its own. */ + worktree_branch?: string | null; +} + export interface ApprovalRequest extends DaemonEvent { type: "approval_request"; session_id: string; @@ -1792,6 +1808,8 @@ export interface ApprovalRequest extends DaemonEvent { summary: string; reason: string; proposed_always_allow?: PolicyRuleSummary | null; + /** TD-5268: the child that is asking; absent for the session's own agent. */ + worker?: ApprovalWorker | null; } export interface QuestionOption { From 8cceb70ce56f6b3be5f0e7da882074236973dda9 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:11:15 +0000 Subject: [PATCH 20/35] TD-5269: one count of calls per model server, across the daemon The only limit on model calls at once was a semaphore inside one workflow call. Two chats, a scheduled run and a workflow could all ask one local server together. model_concurrency gives a server on this computer and any other a default number, and a server named by URL its own. Nothing is set unless config sets it, so an upgrade changes nothing. A server is its scheme, host and port: every spelling of this computer on one port is one, whatever the path, model or key. The provider client holds one slot for one HTTP exchange, a streamed reply until the stream ends, and the wait between retries is spent without one, so a call holding a slot never waits on another for the same server. The daemon gives every client it builds for one server the same gate. The minimal step: no priority lane, no limit on the wait for a slot, no doctor line yet. Co-Authored-By: Claude Fable 5.2 --- core/tests/test_endpoint_gate.py | 253 ++++++++++++++++++++++++++++++ core/tstd/config.py | 3 + core/tstd/config.yaml | 12 ++ core/tstd/config_help.py | 3 + core/tstd/config_help_features.py | 7 + core/tstd/daemon.py | 16 +- core/tstd/endpoint_gate.py | 111 +++++++++++++ core/tstd/provider.py | 28 ++++ docs/configuration.md | 31 ++++ docs/tst-desk-backlog.md | 31 ++++ 10 files changed, 491 insertions(+), 4 deletions(-) create mode 100644 core/tests/test_endpoint_gate.py create mode 100644 core/tstd/endpoint_gate.py diff --git a/core/tests/test_endpoint_gate.py b/core/tests/test_endpoint_gate.py new file mode 100644 index 00000000..a474ad71 --- /dev/null +++ b/core/tests/test_endpoint_gate.py @@ -0,0 +1,253 @@ +"""How many model calls one server answers at a time (TD-5269). + +One count per server, shared by every client in the daemon. These cases +are which URLs are one server, what config gives each, and that the count +really holds calls back, streams included, and lets go when a call ends, +fails or is cancelled. +""" + +from __future__ import annotations + +import asyncio +from pathlib import Path +from typing import Any + +import httpx +import pytest + +from tests.catalog_helpers import catalog_config_yaml +from tstd.config import ConfigError, load_config +from tstd.endpoint_gate import ( + EndpointGates, + EndpointLimit, + ModelConcurrencyConfig, + endpoint_key, +) +from tstd.provider import ChatCompletionRequest, ChatMessage, ProviderClient, ProviderError + +LOCAL = "http://127.0.0.1:8080/v1" + + +class TestWhichUrlsAreOneServer: + @pytest.mark.parametrize( + "url", + [ + "http://127.0.0.1:8080/v1", + "http://localhost:8080", + "http://127.0.0.1:8080/other/path/", + "http://[::1]:8080/v1", + "http://user:secret@127.0.0.1:8080/v1", + ], + ) + def test_every_spelling_of_this_computer_on_one_port(self, url: str) -> None: + assert endpoint_key(url) == "http://this-computer:8080" + assert "secret" not in (endpoint_key(url) or "") + + def test_another_port_scheme_or_host_is_another_server(self) -> None: + keys = { + endpoint_key("http://127.0.0.1:8080/v1"), + endpoint_key("http://127.0.0.1:8081/v1"), + endpoint_key("https://127.0.0.1:8080/v1"), + endpoint_key("https://api.example.com/v1"), + endpoint_key("https://API.example.com:443/v2"), + endpoint_key("https://other.example.com/v1"), + } + assert len(keys) == 5 # the two api.example.com spellings are one + + @pytest.mark.parametrize("url", ["", "not a url", "ftp://host/x", "http://"]) + def test_what_names_no_server_has_no_gate(self, url: str) -> None: + assert endpoint_key(url) is None + assert EndpointGates(ModelConcurrencyConfig(loopback_default=1)).for_url(url) is None + + +class TestWhatConfigGives: + def test_no_limit_unless_config_sets_one(self) -> None: + gates = EndpointGates() + assert gates.for_url(LOCAL) is None + assert gates.for_url("https://api.example.com/v1") is None + + def test_a_default_for_this_computer_and_one_for_everything_else(self) -> None: + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=2, remote_default=8)) + assert gates.limit_for(LOCAL) == 2 + assert gates.limit_for("https://api.example.com/v1") == 8 + only_local = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + assert only_local.limit_for("https://api.example.com/v1") is None + + def test_a_server_named_by_url_has_its_own_number(self) -> None: + config = ModelConcurrencyConfig( + loopback_default=4, + endpoints=[EndpointLimit(url="http://localhost:8080", max_concurrent=1)], + ) + gates = EndpointGates(config) + assert gates.limit_for(LOCAL) == 1 + assert gates.limit_for("http://127.0.0.1:11434/v1") == 4 + + def test_every_client_for_one_server_shares_one_gate(self) -> None: + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + assert gates.for_url(LOCAL) is gates.for_url("http://localhost:8080/v2") + assert gates.for_url(LOCAL) is not gates.for_url("http://127.0.0.1:11434/v1") + + def test_a_reload_keeps_a_gate_whose_number_did_not_change(self) -> None: + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + first = gates.for_url(LOCAL) + gates.configure(ModelConcurrencyConfig(loopback_default=1, remote_default=3)) + assert gates.for_url(LOCAL) is first + gates.configure(ModelConcurrencyConfig(loopback_default=2)) + assert gates.for_url(LOCAL) is not first + gates.configure(ModelConcurrencyConfig()) + assert gates.for_url(LOCAL) is None + + @pytest.mark.parametrize("block", ["loopback_default: 0", "remote_default: 65", "limit: 1"]) + def test_a_bad_section_is_an_error_at_load(self, tmp_path: Path, block: str) -> None: + path = tmp_path / "config.yaml" + path.write_text(catalog_config_yaml() + f"\nmodel_concurrency:\n {block}\n") + with pytest.raises(ConfigError, match="model_concurrency"): + load_config(path) + + def test_the_shipped_config_sets_no_limit(self, tmp_path: Path) -> None: + path = tmp_path / "config.yaml" + path.write_text(catalog_config_yaml(), encoding="utf-8") + section = load_config(path).model_concurrency + assert section.loopback_default is None and section.remote_default is None + assert section.endpoints == [] + + +REPLY = { + "id": "x", + "choices": [ + {"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"} + ], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, +} +STREAM = ( + b'data: {"id":"x","choices":[{"index":0,"delta":{"content":"ok"}}]}\n\n' + b'data: {"id":"x","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n' + b"data: [DONE]\n\n" +) + + +class _Server: + """A model server that counts how many calls it is answering at once.""" + + def __init__(self, hold: float = 0.05) -> None: + self.now = 0 + self.most = 0 + self.calls = 0 + self.hold = hold + self.fail = False + + async def __call__(self, request: httpx.Request) -> httpx.Response: + self.calls += 1 + self.now += 1 + self.most = max(self.most, self.now) + try: + await asyncio.sleep(self.hold) + if self.fail: + return httpx.Response(400, json={"error": {"message": "no"}}) + if b'"stream": true' in request.content or b'"stream":true' in request.content: + return httpx.Response( + 200, content=STREAM, headers={"content-type": "text/event-stream"} + ) + return httpx.Response(200, json=REPLY) + finally: + self.now -= 1 + + def client(self, base_url: str = LOCAL) -> ProviderClient: + http = httpx.AsyncClient(transport=httpx.MockTransport(self)) + return ProviderClient(base_url=base_url, api_key=None, client=http) + + +def _request(stream: bool = False) -> ChatCompletionRequest: + return ChatCompletionRequest( + model="m", messages=[ChatMessage(role="user", content="hi")], stream=stream + ) + + +async def _drain(client: ProviderClient) -> list[Any]: + return [chunk async for chunk in client.chat_completion_stream(_request(stream=True))] + + +class TestTheCountHolds: + async def test_with_no_gate_calls_run_side_by_side(self) -> None: + server = _Server() + clients = [server.client() for _ in range(4)] + await asyncio.gather(*(c.chat_completion(_request()) for c in clients)) + assert server.most == 4 + + @pytest.mark.parametrize("limit", [1, 2]) + async def test_calls_from_every_client_for_one_server_share_the_limit(self, limit: int) -> None: + server = _Server() + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=limit)) + clients = [server.client() for _ in range(5)] + for client in clients: + client.gate = gates.for_url(client.base_url) + replies = await asyncio.gather(*(c.chat_completion(_request()) for c in clients)) + assert all(not isinstance(r, ProviderError) for r in replies) + assert server.calls == 5 + assert server.most == limit + + async def test_a_stream_holds_its_slot_until_it_ends(self) -> None: + server = _Server() + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + clients = [server.client() for _ in range(3)] + for client in clients: + client.gate = gates.for_url(client.base_url) + streams = await asyncio.gather(*(_drain(c) for c in clients)) + assert all(streams) and server.calls == 3 + assert server.most == 1 + + async def test_another_server_is_not_held_back(self) -> None: + server = _Server() + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + here = server.client(LOCAL) + there = server.client("http://127.0.0.1:11434/v1") + here.gate, there.gate = gates.for_url(here.base_url), gates.for_url(there.base_url) + await asyncio.gather(here.chat_completion(_request()), there.chat_completion(_request())) + assert server.most == 2 + + async def test_a_failed_call_and_a_cancelled_one_give_their_slot_back(self) -> None: + server = _Server(hold=0.2) + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + gate = gates.for_url(LOCAL) + assert gate is not None + first, waiting, later = server.client(), server.client(), server.client() + for client in (first, waiting, later): + client.gate = gate + + running = asyncio.create_task(first.chat_completion(_request())) + queued = asyncio.create_task(waiting.chat_completion(_request())) + await asyncio.sleep(0.05) + # One is being answered, one is waiting for the slot. + assert server.now == 1 and gate.locked() + queued.cancel() + running.cancel() + await asyncio.gather(running, queued, return_exceptions=True) + assert not gate.locked() + + server.fail = True + server.hold = 0.0 + failed = await later.chat_completion(_request()) + assert isinstance(failed, ProviderError) + assert not gate.locked() + + +async def test_the_daemon_gives_clients_for_one_server_one_gate(tmp_path: Path) -> None: + from tstd.daemon import Daemon + + text = catalog_config_yaml().replace( + "\nmodel_concurrency:\n loopback_default: null\n", + "\nmodel_concurrency:\n loopback_default: 1\n", + 1, + ) + assert "loopback_default: 1" in text + (tmp_path / "config.yaml").write_text(text, encoding="utf-8") + daemon = Daemon(data_dir=tmp_path) + local = daemon.config.presets["local"] + vllm = daemon.config.presets["vllm"] + assert local.brain is not None and local.worker is not None and vllm.brain is not None + brain = await daemon._build_client(local.brain) + worker = await daemon._build_client(local.worker) + other = await daemon._build_client(vllm.brain) + assert brain.gate is not None and brain.gate is worker.gate + # Another port on this computer is another server with a count of its own. + assert other.gate is not None and other.gate is not brain.gate diff --git a/core/tstd/config.py b/core/tstd/config.py index 7a7d6d74..fac27dfb 100644 --- a/core/tstd/config.py +++ b/core/tstd/config.py @@ -35,6 +35,7 @@ ) from .effort import DEFAULT_EFFORT, LEGACY_ULTRACODE, EffortLevel +from .endpoint_gate import ModelConcurrencyConfig from .hooks_config import HooksConfig from .logging import get_logger, user_data_dir from .lsp_config import LspConfig @@ -1740,6 +1741,8 @@ class ModelConfig(BaseModel): consult: ConsultConfig = Field(default_factory=ConsultConfig) delegate: DelegateConfig = Field(default_factory=DelegateConfig) workflow: WorkflowConfig = Field(default_factory=WorkflowConfig) + # How many calls one model server answers at once, daemon-wide (TD-5269). + model_concurrency: ModelConcurrencyConfig = Field(default_factory=ModelConcurrencyConfig) model_refresh: ModelRefreshConfig = Field(default_factory=ModelRefreshConfig) shell: ShellConfig = Field(default_factory=ShellConfig) provider_retry: ProviderRetryConfig = Field(default_factory=ProviderRetryConfig) diff --git a/core/tstd/config.yaml b/core/tstd/config.yaml index 9eab6937..cf2e270b 100644 --- a/core/tstd/config.yaml +++ b/core/tstd/config.yaml @@ -225,6 +225,18 @@ consult: delegate: max_iterations: 8 +# How many calls one model server answers at once, across every chat, +# worker and scheduled run (TD-5269). null is no limit. A single GPU +# usually wants loopback_default: 1 or 2. endpoints gives one server, by +# URL, a number of its own: +# endpoints: +# - url: http://127.0.0.1:8080 +# max_concurrent: 1 +model_concurrency: + loopback_default: null + remote_default: null + endpoints: [] + # The workflow tool (TD-5195), used only while a session's Ultracode switch # is on: it runs several workers at once on independent pieces of a job, and # each result can be checked before it comes back. max_parallel (1-16) is how diff --git a/core/tstd/config_help.py b/core/tstd/config_help.py index 77b19eca..42f93a35 100644 --- a/core/tstd/config_help.py +++ b/core/tstd/config_help.py @@ -41,6 +41,7 @@ "compaction": "Compaction", "consult": "Consult", "delegate": "Delegate", + "model_concurrency": "Calls at once per model server", "workflow": "Workflow", "model_refresh": "Model refresh", "shell": "Shell", @@ -68,6 +69,8 @@ "the first one.", "ModelConfig.credentials": "Named API keys: the name Settings shows and the host each key " "talks to. The keys themselves live in the OS keychain.", + "ModelConfig.model_concurrency": "How many calls one model server answers at once, " + "across every chat, worker and scheduled run.", "ModelConfig.delegate": "Limits for a child the agent delegates part of a task to.", "ModelConfig.search": "Where web_search sends queries, and how web_fetch reads pages.", "ModelConfig.local_servers": "Model servers on this computer that Add a model's search " diff --git a/core/tstd/config_help_features.py b/core/tstd/config_help_features.py index 07f710da..0afc3994 100644 --- a/core/tstd/config_help_features.py +++ b/core/tstd/config_help_features.py @@ -85,6 +85,13 @@ "CompactionConfig.reinject_files": "Recent files read again after a compaction. 0 brings " "none back.", "CompactionConfig.reinject_tokens": ("Most those files may take.", TOKENS), + "ModelConcurrencyConfig.loopback_default": "Calls at once for a server on this computer. " + "Empty is no limit. A single GPU usually wants 1 or 2.", + "ModelConcurrencyConfig.remote_default": "Calls at once for any other server. Empty is " + "no limit.", + "ModelConcurrencyConfig.endpoints": "Servers with a number of their own, by URL.", + "EndpointLimit.url": "The server, by URL. Only its scheme, host and port matter.", + "EndpointLimit.max_concurrent": "Calls that server answers at once.", "DelegateConfig.max_iterations": "Model calls one delegated child may make before it " "stops with what it has. The session's caps still bound the total.", "ConsultConfig.tiers": "Tiers offered the consult tool. None turns it off.", diff --git a/core/tstd/daemon.py b/core/tstd/daemon.py index 01095b33..4f835a9e 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -142,6 +142,7 @@ from .desktop.protocol import DesktopError from .discovery import resolve_tier_slugs from .effort import effort_record, read_effort_record +from .endpoint_gate import EndpointGates from .grok_acp import GrokEngineError, find_grok_binary from .grok_home import ( grok_configured_model, @@ -1194,6 +1195,8 @@ def __init__( known_session=lambda sid: self.session_registry.get(sid) is not None, ) self._slug_snapshot = _snapshot_slugs(self.config) + # TD-5269: one count per model server, shared across sessions. + self._gates = EndpointGates(self.config.model_concurrency) # TD-5185: last model-card refresh per (preset, tier), this run only. self._card_results: dict[tuple[str, str], CardResult] = {} # A background refresh and a Settings one never write at once (TD-5197). @@ -1405,12 +1408,17 @@ async def _build_client(self, tier_cfg: TierConfig) -> ProviderClient: base_url = resolve_base_url(self.config, tier_cfg) timeouts = tier_cfg.timeouts() if cred_id is None: - return ProviderClient( + client = ProviderClient( base_url=base_url, api_key=None, timeout=timeouts, retry_config=retry_cfg ) - return await ProviderClient.from_keychain( - base_url, provider_name=cred_id, timeout_config=timeouts, retry_config=retry_cfg - ) + else: + client = await ProviderClient.from_keychain( + base_url, provider_name=cred_id, timeout_config=timeouts, retry_config=retry_cfg + ) + # TD-5269: every client for one server shares that server's count. + self._gates.configure(self.config.model_concurrency) + client.gate = self._gates.for_url(base_url) + return client async def _brain_client(self) -> ProviderClient: """Build a client for the active brain tier.""" diff --git a/core/tstd/endpoint_gate.py b/core/tstd/endpoint_gate.py new file mode 100644 index 00000000..6d0adfb1 --- /dev/null +++ b/core/tstd/endpoint_gate.py @@ -0,0 +1,111 @@ +"""How many model calls one server answers at a time (TD-5269). + +Nothing bounded model calls across the daemon. A ``workflow`` call limits +its own children, and that was all: two chats, a scheduled run and a +workflow could all ask one local server at once, and a single GPU then +serves all of them slowly or fails some. Here each server gets one count, +shared by every caller in the daemon. + +A gate is keyed by the server, not the model or the key: scheme, host and +port, with every spelling of this computer folded to one host. Two paths +on one port are one server, and a GPU does not care which key asked. + +No limit is set unless config sets one, so nothing changes on upgrade. A +slot is held for one HTTP exchange only (see ``ProviderClient``): never +across a retry's wait, and never while a tool runs, so a call that holds a +slot is never waiting on another call for the same server. +""" + +from __future__ import annotations + +import asyncio +from urllib.parse import urlsplit + +from pydantic import BaseModel, ConfigDict, Field + +from .net import is_loopback_url + +_LOOPBACK = "this-computer" +_DEFAULT_PORTS = {"http": 80, "https": 443} + + +class EndpointLimit(BaseModel): + """One server's own limit, in place of the default for its kind.""" + + model_config = ConfigDict(extra="forbid") + + url: str = Field(min_length=1) + max_concurrent: int = Field(ge=1, le=64) + + +class ModelConcurrencyConfig(BaseModel): + """The ``model_concurrency`` section of ``config.yaml`` (TD-5269). + + ``loopback_default`` is how many calls a server on this computer + answers at once; ``remote_default`` the same for any other. ``null`` + is no limit, which is how it was before the key existed. ``endpoints`` + names a server by URL and gives it a number of its own. + """ + + model_config = ConfigDict(extra="forbid") + + loopback_default: int | None = Field(default=None, ge=1, le=64) + remote_default: int | None = Field(default=None, ge=1, le=64) + endpoints: list[EndpointLimit] = Field(default_factory=list) + + +def endpoint_key(url: str) -> str | None: + """The server *url* names: its scheme, host and port. None when it names none. + + Every loopback spelling (``localhost``, ``127.0.0.1``, ``::1``) is one + host. The path and any credential in the URL are left out. + """ + try: + parts = urlsplit(url.strip()) + host = parts.hostname + port = parts.port + except ValueError: + return None + if parts.scheme not in _DEFAULT_PORTS or not host: + return None + name = _LOOPBACK if is_loopback_url(url) else host.lower() + return f"{parts.scheme}://{name}:{port or _DEFAULT_PORTS[parts.scheme]}" + + +class EndpointGates: + """One count per server, shared by every client in the daemon.""" + + def __init__(self, config: ModelConcurrencyConfig | None = None) -> None: + self._config = config or ModelConcurrencyConfig() + # key -> (limit, the semaphore every client for that server shares) + self._gates: dict[str, tuple[int, asyncio.Semaphore]] = {} + + def configure(self, config: ModelConcurrencyConfig) -> None: + """Take a reloaded config. A server whose number did not change keeps + its gate, calls in flight included; one whose number changed gets a + new gate the next time a client is built for it.""" + self._config = config + + def limit_for(self, base_url: str) -> int | None: + """The limit config gives the server at *base_url*, or None for none.""" + key = endpoint_key(base_url) + if key is None: + return None + for entry in self._config.endpoints: + if endpoint_key(entry.url) == key: + return entry.max_concurrent + if is_loopback_url(base_url): + return self._config.loopback_default + return self._config.remote_default + + def for_url(self, base_url: str) -> asyncio.Semaphore | None: + """The gate for the server at *base_url*, or None when it has no limit.""" + limit = self.limit_for(base_url) + key = endpoint_key(base_url) + if limit is None or key is None: + return None + held = self._gates.get(key) + if held is None or held[0] != limit: + held = (limit, asyncio.Semaphore(limit)) + self._gates[key] = held + return held[1] diff --git a/core/tstd/provider.py b/core/tstd/provider.py index 2f9c5b10..9e6052bc 100644 --- a/core/tstd/provider.py +++ b/core/tstd/provider.py @@ -11,6 +11,7 @@ from __future__ import annotations import asyncio +import contextlib import json import random import re @@ -1098,6 +1099,9 @@ def __init__( self.timeout = timeout or TimeoutConfig() self.retry_config = retry_config or RetryConfig() + # TD-5269: the count this server's calls share, daemon-wide. None is + # no limit. Set by whoever builds the client (``Daemon._build_client``). + self.gate: asyncio.Semaphore | None = None self._client = client or httpx.AsyncClient( timeout=self.timeout.to_httpx_timeout(), follow_redirects=True, @@ -1138,6 +1142,12 @@ async def chat_completion( body.pop("stream_options", None) async def _attempt() -> ChatCompletionResponse | ProviderError: + # One slot for this one exchange (TD-5269): the wait before a + # retry is spent outside it. + async with self._slot(): + return await _exchange() + + async def _exchange() -> ChatCompletionResponse | ProviderError: try: async with asyncio.timeout(self.timeout.total): response = await self._client.post( @@ -1242,6 +1252,14 @@ async def chat_completion_stream( body["stream_options"] = {"include_usage": True} async def _stream_once() -> AsyncIterator[StreamChunk | ProviderError]: + # Held from the request to the stream's end (TD-5269): the server + # is busy with this call for that long. Waiting for a slot is not + # part of the reply's own time limit. + async with self._slot(): + async for chunk in _stream_exchange(): + yield chunk + + async def _stream_exchange() -> AsyncIterator[StreamChunk | ProviderError]: started = asyncio.get_running_loop().time() try: async with aconnect_sse( @@ -1618,6 +1636,16 @@ def _inband_stream_error( resendable=error.retryable, ) + @contextlib.asynccontextmanager + async def _slot(self) -> AsyncIterator[None]: + """Hold one of this server's slots, or nothing when it has no limit (TD-5269).""" + gate = self.gate + if gate is None: + yield + return + async with gate: + yield + def _headers(self) -> dict[str, str]: """Build the request headers. diff --git a/docs/configuration.md b/docs/configuration.md index c6eb933c..9f24bdd7 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -117,6 +117,7 @@ with the reason under the field. Line endings stay as the file has them. | `compaction` | mapping | see below | How compaction writes its summary and what it brings back (TD-5128): a worker-written structured summary, written ahead in the background, and the recent files read again. Omitted in an older user copy is filled from the shipped file at load, which turns the worker summary on. A config with the block but no `summary` key is `extractive`. | | `consult` | mapping | see below | Which tiers get the `consult` tool, which asks the brain or the validator one question mid-task, and how many consults one turn may make (TD-5119). Omitted in an older user copy is filled from the shipped file at load: the worker only, two a turn. | | `delegate` | mapping | see below | How many model calls one `delegate` child may make (TD-5265). Omitted in an older user copy defaults to 8, what it was before the key existed. | +| `model_concurrency` | mapping | no limits | How many calls one model server answers at once, across the whole daemon (TD-5269). Omitted in an older user copy means no limit, as before. | | `workflow` | mapping | see below | Limits for the `workflow` tool, which runs workers side by side while a session's Ultracode switch is on (TD-5195). Omitted means the defaults: 3 at once, 8 a call. | | `model_refresh` | mapping | see below | Refresh every model's prices, windows and slug spelling in the background (TD-5197). Omitted means on, every 24 hours. | | `shell` | mapping | see below | The read-only shell list (TD-5103): commands that run without asking. Omitted in an older user copy means the whole built-in list, on. Config can shrink it or switch it off, never add to it. | @@ -909,6 +910,36 @@ delegate: max_iterations: 8 ``` +### `model_concurrency` + +One count per model server, shared by every chat, delegated worker, +workflow task and scheduled run in the daemon (TD-5269). Without it the +only limit was inside a single `workflow` call, so two chats and a +scheduled run could all ask one local server at once. + +| Key | Type | Default | Effect | +|---|---|---|---| +| `loopback_default` | int 1 to 64, or null | `null` | Calls at once for a server on this computer. `null` is no limit. A single GPU usually wants 1 or 2. | +| `remote_default` | int 1 to 64, or null | `null` | The same for any other server. | +| `endpoints` | list of `url` + `max_concurrent` | `[]` | A server named by URL gets its own number in place of the default for its kind. | + +A server is its scheme, host and port: every spelling of this computer +(`localhost`, `127.0.0.1`, `::1`) on one port is one server, whatever the +path, model or key. A call waits for a free slot and holds it for one +request only, a streamed reply until the stream ends; the wait between +retries is spent without one. `workflow.max_parallel` still caps one +workflow call; this caps everything together. + + +```yaml +model_concurrency: + loopback_default: 1 + remote_default: null + endpoints: + - url: http://127.0.0.1:11434 + max_concurrent: 2 +``` + ### `workflow` The `workflow` tool (TD-5195) runs several workers at once on independent diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index e827ebd7..8b790314 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13994,3 +13994,34 @@ a worker's cards, keeping keyboard focus on the card being read when a second arrives, and an audit row for a child's own tool calls. Class B entry in DECISIONS.md. +--- + +### TD-5269 — One count of calls per model server, across the daemon + +**Size:** 3 · **Depends on:** TD-301, TD-5195 · **Status:** Done + +The only limit on model calls at once was a semaphore inside one +`workflow` call. Two chats, a scheduled run and a workflow could all ask +one local server together, and a single GPU then answers all of them +slowly or fails some. It is also the first thing children that run +alongside their parent need. + +- [x] `model_concurrency` gives a server on this computer and any other a + default number, and a server named by URL its own; nothing is set + unless config sets it (`test_endpoint_gate.py::TestWhatConfigGives`). +- [x] A server is its scheme, host and port: every spelling of this + computer on one port is one, whatever the path or key + (`TestWhichUrlsAreOneServer`). +- [x] Calls from every client for one server share the limit, a streamed + reply holds its slot until the stream ends, and another server is not + held back (`TestTheCountHolds`). +- [x] A failed call and a cancelled one give their slot back. +- [x] The daemon gives every client it builds for one server the same + gate (`test_the_daemon_gives_clients_for_one_server_one_gate`). + +**Notes:** The minimal step from the research note. Not done here: a lane +that lets a chat's own reply go ahead of background work, a limit on how +long a call waits for a slot, a line in the doctor, and streaming for +children so a timed-out child frees the server at once. A chat that is +open keeps the gate its client was built with until the client is rebuilt. + From 0f5087fb83634bfa513465d3d4492126c65abf69 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:13:41 +0000 Subject: [PATCH 21/35] TD-5270: fs_edit replaces every occurrence, or several things in one call Renaming a repeated name cost one fs_edit call per occurrence, and three changes to one file were three calls. replace_all replaces every occurrence and reports how many. edits (up to 20) applies several replacements in order, each to the text the one before left, and writes the file once; if any cannot apply, nothing is written and the error names which one. Exactly one of the two forms. The tool is still one file per call, named in path, so the classifier judges it exactly as before. No tool was added. Co-Authored-By: Claude Fable 5.2 --- core/tests/test_fs_edit_batch.py | 201 +++++++++++++++++++++++++++++++ core/tstd/tools/registry.py | 28 ++++- core/tstd/tools/write.py | 108 ++++++++++++++--- docs/tst-desk-backlog.md | 30 +++++ 4 files changed, 350 insertions(+), 17 deletions(-) create mode 100644 core/tests/test_fs_edit_batch.py diff --git a/core/tests/test_fs_edit_batch.py b/core/tests/test_fs_edit_batch.py new file mode 100644 index 00000000..45e014e9 --- /dev/null +++ b/core/tests/test_fs_edit_batch.py @@ -0,0 +1,201 @@ +"""``fs_edit`` replaces every occurrence, or several things at once (TD-5270). + +A rename used to cost one call per occurrence. The tool stays one file per +call, on the same path the classifier already judges; what changes is how +much one call can do, and that a batch either all lands or none of it does. +""" + +from __future__ import annotations + +from pathlib import Path +from typing import Any + +import pytest + +from tests.test_read_tools import make_dispatcher +from tstd.autonomy import DecisionClass +from tstd.tools import create_registry, fs_edit +from tstd.tools.write import MAX_EDITS, Edit, apply_edits, edits_from_arguments + +TEXT = "alpha = 1\nbeta = alpha + 1\ngamma = alpha * beta\n" + + +def _file(tmp_path: Path, text: str = TEXT) -> Path: + target = tmp_path / "a.py" + target.write_text(text, encoding="utf-8") + return target + + +class TestReplaceAll: + async def test_every_occurrence_goes_and_the_count_is_reported(self, tmp_path: Path) -> None: + target = _file(tmp_path) + out = await fs_edit(None, str(target), "alpha", "first", replace_all=True) + assert target.read_text() == TEXT.replace("alpha", "first") + assert "replaced 3 occurrences" in out + + async def test_none_found_still_fails(self, tmp_path: Path) -> None: + target = _file(tmp_path) + with pytest.raises(ValueError, match="not found"): + await fs_edit(None, str(target), "delta", "x", replace_all=True) + assert target.read_text() == TEXT + + async def test_without_it_two_occurrences_are_still_ambiguous(self, tmp_path: Path) -> None: + target = _file(tmp_path) + with pytest.raises(ValueError, match="ambiguous: 3 occurrences") as caught: + await fs_edit(None, str(target), "alpha", "x") + assert "replace_all" in str(caught.value) + assert target.read_text() == TEXT + + +class TestABatch: + async def test_edits_apply_in_order_each_to_what_the_last_left(self, tmp_path: Path) -> None: + target = _file(tmp_path) + out = await fs_edit( + None, + str(target), + edits=[ + {"old_string": "alpha", "new_string": "a", "replace_all": True}, + {"old_string": "beta = a + 1", "new_string": "beta = a + 2"}, + {"old_string": "gamma", "new_string": "c"}, + ], + ) + assert target.read_text() == "a = 1\nbeta = a + 2\nc = a * beta\n" + assert "3 edits" in out and "replaced 3, 1, 1" in out and "5 occurrences" in out + + @pytest.mark.parametrize( + ("bad", "says"), + [ + ({"old_string": "nowhere", "new_string": "x"}, "edit 2: target string not found"), + ({"old_string": "a", "new_string": "x"}, "edit 2: target string is ambiguous"), + ], + ) + async def test_one_edit_that_cannot_apply_leaves_the_file_as_it_was( + self, tmp_path: Path, bad: dict[str, Any], says: str + ) -> None: + target = _file(tmp_path) + before = target.read_bytes() + with pytest.raises(ValueError, match=says) as caught: + await fs_edit( + None, + str(target), + edits=[ + {"old_string": "alpha = 1", "new_string": "alpha = 9"}, + bad, + {"old_string": "gamma", "new_string": "c"}, + ], + ) + assert "nothing was changed" in str(caught.value) + assert target.read_bytes() == before + + async def test_the_file_is_written_once( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + from tstd.tools import write as write_module + + writes: list[str] = [] + real = write_module._atomic_write + + def counting(path: Path, text: str) -> None: + writes.append(text) + real(path, text) + + monkeypatch.setattr(write_module, "_atomic_write", counting) + target = _file(tmp_path) + await fs_edit( + None, + str(target), + edits=[ + {"old_string": "alpha = 1", "new_string": "alpha = 2"}, + {"old_string": "gamma", "new_string": "c"}, + ], + ) + assert len(writes) == 1 and writes[0] == target.read_text() + + +class TestTheTwoForms: + @pytest.mark.parametrize( + ("kwargs", "says"), + [ + ({}, "give old_string and new_string, or a list of edits"), + ({"old_string": "alpha"}, "give old_string and new_string, or a list of edits"), + ( + { + "old_string": "a", + "new_string": "b", + "edits": [{"old_string": "a", "new_string": "b"}], + }, + "not both", + ), + ({"edits": []}, "edits is empty"), + ({"edits": [{"old_string": "", "new_string": "x"}]}, "edit 1 needs old_string"), + ({"edits": [{"new_string": "x"}]}, "edit 1 needs old_string"), + ( + {"edits": [{"old_string": "a", "new_string": "b"}] * (MAX_EDITS + 1)}, + f"too many edits: {MAX_EDITS + 1}", + ), + ], + ) + async def test_anything_else_changes_nothing( + self, tmp_path: Path, kwargs: dict[str, Any], says: str + ) -> None: + target = _file(tmp_path) + with pytest.raises(ValueError, match=says): + await fs_edit(None, str(target), **kwargs) + assert target.read_text() == TEXT + + def test_the_single_form_is_one_edit(self) -> None: + assert edits_from_arguments("a", "b", False, None) == [Edit("a", "b", False)] + assert edits_from_arguments("a", "", True, None) == [Edit("a", "", True)] + + def test_applying_is_pure(self) -> None: + text, counts = apply_edits("x x y", [Edit("x", "z", True), Edit("y", "x")], "f") + assert text == "z z x" and counts == [2, 1] + + +class TestTheSchemaAndTheGate: + def test_the_tool_is_still_one_file_judged_by_its_path(self) -> None: + tool = create_registry().require("fs_edit") + assert tool.path_fields == ("path",) + assert tool.side_effect_class == "ask" and tool.mutates is True + props = tool.parameters["properties"] + assert tool.parameters["required"] == ["path"] + assert props["edits"]["maxItems"] == MAX_EDITS == 20 + assert props["edits"]["items"]["required"] == ["old_string", "new_string"] + assert props["replace_all"]["type"] == "boolean" + + async def test_a_batch_goes_through_the_dispatcher_as_one_call( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + dispatcher = make_dispatcher(tmp_path) + monkeypatch.chdir(tmp_path) + (tmp_path / "a.txt").write_text("one two one\n", encoding="utf-8") + result = await dispatcher.dispatch( + "c1", + "fs_edit", + { + "path": "a.txt", + "edits": [ + {"old_string": "one", "new_string": "1", "replace_all": True}, + {"old_string": "two", "new_string": "2"}, + ], + }, + ) + assert result.status == "success", result.output + assert result.diff is not None and "-one two one" in result.diff + assert (tmp_path / "a.txt").read_text() == "1 2 1\n" + assert "2 edits" in result.output + + async def test_a_batch_on_a_steering_file_is_still_outside_the_wall( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + dispatcher = make_dispatcher(tmp_path) + monkeypatch.chdir(tmp_path) + (tmp_path / "AGENTS.md").write_text("rules\n", encoding="utf-8") + result = await dispatcher.dispatch( + "c1", + "fs_edit", + {"path": "AGENTS.md", "edits": [{"old_string": "rules", "new_string": "none"}]}, + ) + assert result.status != "success" + assert (tmp_path / "AGENTS.md").read_text() == "rules\n" + assert result.decision_class == DecisionClass.C.value diff --git a/core/tstd/tools/registry.py b/core/tstd/tools/registry.py index b0a92edc..2d559671 100644 --- a/core/tstd/tools/registry.py +++ b/core/tstd/tools/registry.py @@ -19,6 +19,7 @@ from ..config import SearchConfig from ..provider import FunctionDefinition, ToolDefinition +from .write import MAX_EDITS # ── Side-effect classification ────────────────────────────────────────── @@ -577,7 +578,9 @@ def _search_hosts() -> tuple[str, ...]: "Use it for every change inside an existing file, instead of " "sed -i or other shell edits. " "Fails if the target string is absent or appears more than once — " - "include enough surrounding context to match exactly one place. " + "include enough surrounding context to match exactly one place, " + "or set replace_all. Several changes to one file go in edits, " + "in one call. " "For creating or rewriting whole files, use fs_write.", parameters={ "type": "object", @@ -595,8 +598,29 @@ def _search_hosts() -> tuple[str, ...]: "type": "string", "description": "Replacement text (may be empty to delete)", }, + "replace_all": { + "type": "boolean", + "description": "Replace every occurrence instead of exactly one", + }, + "edits": { + "type": "array", + "minItems": 1, + "maxItems": MAX_EDITS, + "description": "Several replacements in this file, applied in " + "order; nothing is written if one fails. Use instead of " + "old_string and new_string.", + "items": { + "type": "object", + "properties": { + "old_string": {"type": "string", "minLength": 1}, + "new_string": {"type": "string"}, + "replace_all": {"type": "boolean"}, + }, + "required": ["old_string", "new_string"], + }, + }, }, - "required": ["path", "old_string", "new_string"], + "required": ["path"], }, side_effect_class="ask", parallel_safe=False, diff --git a/core/tstd/tools/write.py b/core/tstd/tools/write.py index 19e31962..42f6c777 100644 --- a/core/tstd/tools/write.py +++ b/core/tstd/tools/write.py @@ -18,7 +18,9 @@ import contextlib import os import tempfile +from dataclasses import dataclass from pathlib import Path +from typing import Any def _atomic_write(target: Path, content: str) -> None: @@ -69,24 +71,92 @@ def _fs_write(path: Path, content: str, append: bool, session: object | None) -> return f"wrote {len(content.encode('utf-8'))} bytes; {verb} {path}" -def _fs_edit(path: Path, old_string: str, new_string: str, session: object | None) -> str: +#: Most replacements one ``fs_edit`` call may carry (TD-5270). +MAX_EDITS = 20 + + +@dataclass(frozen=True) +class Edit: + """One replacement: an exact string, its replacement, and whether every + occurrence goes or exactly one must exist.""" + + old_string: str + new_string: str + replace_all: bool = False + + +def edits_from_arguments( + old_string: str | None, + new_string: str | None, + replace_all: bool, + edits: list[dict[str, Any]] | None, +) -> list[Edit]: + """The call's replacements, from whichever of its two forms it used. + + One pair at the top level, or a list under ``edits``: exactly one of the + two. Anything else is the caller's mistake and changes nothing. + """ + single = old_string is not None or new_string is not None + if edits is not None and single: + raise ValueError("give old_string and new_string, or edits, not both") + if edits is None: + if old_string is None or new_string is None: + raise ValueError("give old_string and new_string, or a list of edits") + return [Edit(old_string, new_string, replace_all)] + if not edits: + raise ValueError("edits is empty — nothing to change") + if len(edits) > MAX_EDITS: + raise ValueError(f"too many edits: {len(edits)} (at most {MAX_EDITS} in one call)") + out: list[Edit] = [] + for index, item in enumerate(edits, start=1): + old, new = item.get("old_string"), item.get("new_string") + if not isinstance(old, str) or not old or not isinstance(new, str): + raise ValueError(f"edit {index} needs old_string (not empty) and new_string") + out.append(Edit(old, new, bool(item.get("replace_all", False)))) + return out + + +def apply_edits(content: str, edits: list[Edit], where: str) -> tuple[str, list[int]]: + """*content* after *edits*, in order, and how many places each changed. + + Each edit sees the text the one before it left. The first that cannot + apply stops everything, and names its place in the list when there is + more than one, so the caller writes nothing. + """ + counts: list[int] = [] + for index, edit in enumerate(edits, start=1): + which = f"edit {index}: " if len(edits) > 1 else "" + found = content.count(edit.old_string) + if found == 0: + raise ValueError(f"{which}target string not found in '{where}' — nothing was changed") + if found > 1 and not edit.replace_all: + raise ValueError( + f"{which}target string is ambiguous: {found} occurrences in '{where}' — " + "include more surrounding context so exactly one matches, or set " + "replace_all — nothing was changed" + ) + content = content.replace(edit.old_string, edit.new_string) + counts.append(found) + return content, counts + + +def _fs_edit(path: Path, edits: list[Edit], session: object | None) -> str: if not path.exists(): raise FileNotFoundError(f"'{path}' does not exist — use fs_write to create it") if path.is_dir(): raise ValueError(f"'{path}' is a directory, not a file") - content = path.read_text(encoding="utf-8") - count = content.count(old_string) - if count == 0: - raise ValueError(f"target string not found in '{path}' — nothing was changed") - if count > 1: - raise ValueError( - f"target string is ambiguous: {count} occurrences in '{path}' — " - "include more surrounding context so exactly one matches" - ) - proposed = content.replace(old_string, new_string, 1) + proposed, counts = apply_edits(path.read_text(encoding="utf-8"), edits, str(path)) _enforce_memory_cap(path, proposed, session) + # One write for the whole call: a batch is one change to the file. _atomic_write(path, proposed) - return f"edited {path}: replaced 1 occurrence" + if len(counts) == 1: + return f"edited {path}: replaced {_occurrences(counts[0])}" + each = ", ".join(str(n) for n in counts) + return f"edited {path}: {len(counts)} edits, replaced {each} ({_occurrences(sum(counts))})" + + +def _occurrences(count: int) -> str: + return "1 occurrence" if count == 1 else f"{count} occurrences" async def fs_write( @@ -97,7 +167,15 @@ async def fs_write( async def fs_edit( - session: object, path: str, old_string: str, new_string: str, tool_call_id: str = "" + session: object, + path: str, + old_string: str | None = None, + new_string: str | None = None, + replace_all: bool = False, + edits: list[dict[str, Any]] | None = None, + tool_call_id: str = "", ) -> str: - """Exact single-occurrence string replacement in a file (TD-604).""" - return await asyncio.to_thread(_fs_edit, Path(path), old_string, new_string, session) + """Exact string replacement in one file (TD-604): one, every occurrence + of one, or several in order with nothing written if any fails (TD-5270).""" + wanted = edits_from_arguments(old_string, new_string, replace_all, edits) + return await asyncio.to_thread(_fs_edit, Path(path), wanted, session) diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 8b790314..b4e787f8 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -14025,3 +14025,33 @@ long a call waits for a slot, a line in the doctor, and streaming for children so a timed-out child frees the server at once. A chat that is open keeps the gate its client was built with until the client is rebuilt. +--- + +### TD-5270 — `fs_edit` replaces every occurrence, or several things in one call + +**Size:** 2 · **Depends on:** TD-604 · **Status:** Done + +Renaming a repeated name cost one `fs_edit` call per occurrence, and three +changes to one file were three calls. Harness answers that with a patch +tool; a patch hides its paths inside one string, where the classifier +cannot see them before a parser runs. Widening the tool that already +exists adds no tool and keeps one file, named in `path`, per call. + +- [x] `replace_all: true` replaces every occurrence and reports how many; + none found still fails; without it, more than one is still ambiguous + (`test_fs_edit_batch.py::TestReplaceAll`). +- [x] `edits` (up to 20) applies several replacements in order, each to the + text the one before left, and writes the file once + (`TestABatch`). +- [x] If any edit cannot apply, nothing is written and the error names + which one (`test_one_edit_that_cannot_apply_leaves_the_file_as_it_was`). +- [x] Exactly one of the two forms; both, neither, an empty list or too + many change nothing (`TestTheTwoForms`). +- [x] The tool is still judged by its one `path`: a batch on a steering + file is still Class C, and a batch is one call, one diff + (`TestTheSchemaAndTheGate`). +- [x] Every existing `fs_edit` test passes unchanged. + +**Notes:** The memory-file cap is checked on the final text. The schema +grew by about 600 characters; no new tool was added. + From 472a613cce0fd8280e3bd2f0fed009e9427dc323 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:14:46 +0000 Subject: [PATCH 22/35] TD-5277: tests that fail when an MCP server key is left off a list An MCP server's block is written from a hand-kept list of keys, and a Settings save carries the hand-set keys across from a second list in the set_mcp_server handler. Both are complete today, so nothing is fixed here. A key added to the model and to neither list would be dropped by the next Settings save with nothing to say so. A table names every field per transport and fails when the model gains one; the writer round-trips every field; every field is classed as sent, bound to the address, or carried; a Settings save changes only what it sent. Tests only. Co-Authored-By: Claude Fable 5.2 --- core/tests/test_mcp_config_parity.py | 164 +++++++++++++++++++++++++++ docs/tst-desk-backlog.md | 26 +++++ 2 files changed, 190 insertions(+) create mode 100644 core/tests/test_mcp_config_parity.py diff --git a/core/tests/test_mcp_config_parity.py b/core/tests/test_mcp_config_parity.py new file mode 100644 index 00000000..92ab45c4 --- /dev/null +++ b/core/tests/test_mcp_config_parity.py @@ -0,0 +1,164 @@ +"""Settings does not lose an MCP server's keys (TD-5277). + +``_mcp_server_lines`` writes a server's block from a list of keys kept by +hand, and the ``set_mcp_server`` handler copies the hand-set keys across +from a second list. Both are complete today. These tests are what makes a +new key on ``McpServerConfig`` fail here until someone has decided, for +each list, what happens to it: nothing was wrong, and nothing would have +said so the day something was. +""" + +from __future__ import annotations + +from pathlib import Path +from typing import Any + +import pytest +import yaml + +from tests.catalog_helpers import catalog_config_yaml +from tests.test_add_model import _ask, _start, _stop +from tstd.config import McpServerConfig, load_config +from tstd.config_write import save_mcp_server +from tstd.mock import MockProvider, Script + +# Every field with a value that is not its default, per transport. A field +# added to the model must be added here, which is the point. +EVERY_FIELD: dict[str, dict[str, Any]] = { + "stdio": { + "transport": "stdio", + # Harmless if it ever ran: these tests start no server. + "command": ["true", "--root", "/srv/data"], + "url": "", + "enabled": False, + "timeout_seconds": 12.5, + "startup_timeout_seconds": 45.0, + "max_call_seconds": 900.0, + "env": {"LOG_LEVEL": "debug", "API_TOKEN": "keychain:token"}, + "headers": {}, + "read_only_tools": ["list_files", "read_file"], + "allow_remote": False, + }, + "http": { + "transport": "http", + "command": [], + "url": "https://mcp.example.com/mcp", + "enabled": False, + "timeout_seconds": 12.5, + "startup_timeout_seconds": 45.0, + "max_call_seconds": 900.0, + "env": {}, + "headers": {"Authorization": "keychain:bearer", "X-Team": "tools"}, + "read_only_tools": ["search"], + "allow_remote": True, + }, +} + +# What the Settings page sends, what belongs to one server's address and is +# dropped when the transport or host changes, and what is carried across a +# Settings save untouched. Every field is in exactly one. +SENT_BY_SETTINGS = {"transport", "command", "url", "enabled"} +BOUND_TO_THE_HOST = {"allow_remote", "env", "headers"} +CARRIED = {"timeout_seconds", "startup_timeout_seconds", "max_call_seconds", "read_only_tools"} + + +def _config(tmp_path: Path) -> Path: + path = tmp_path / "config.yaml" + path.write_text(catalog_config_yaml(), encoding="utf-8") + return path + + +def test_the_table_names_every_field_of_the_model() -> None: + for transport, values in EVERY_FIELD.items(): + assert set(values) == set(McpServerConfig.model_fields), transport + + +def test_every_field_is_sent_bound_or_carried_and_only_one_of_them() -> None: + assert set(McpServerConfig.model_fields) == SENT_BY_SETTINGS | BOUND_TO_THE_HOST | CARRIED + assert not SENT_BY_SETTINGS & BOUND_TO_THE_HOST + assert not SENT_BY_SETTINGS & CARRIED + assert not BOUND_TO_THE_HOST & CARRIED + + +@pytest.mark.parametrize("transport", ["stdio", "http"]) +def test_the_writer_writes_every_field_and_it_reads_back_the_same( + tmp_path: Path, transport: str +) -> None: + path = _config(tmp_path) + spec = McpServerConfig(**EVERY_FIELD[transport]) + save_mcp_server("walk", spec, path) + + block = yaml.safe_load(path.read_text(encoding="utf-8"))["mcp"]["servers"]["walk"] + # The one of command and url the transport does not use is left out, and + # a value at its default may be: everything set here is not. + unused = "url" if transport == "stdio" else "command" + empty = {name for name, value in EVERY_FIELD[transport].items() if value in ({}, [], "")} + expected = set(McpServerConfig.model_fields) - {unused} - empty + if not EVERY_FIELD[transport]["allow_remote"]: + expected -= {"allow_remote"} + assert expected <= set(block), sorted(expected - set(block)) + + assert load_config(path).mcp.servers["walk"] == spec + + +@pytest.mark.parametrize("transport", ["stdio", "http"]) +async def test_a_settings_save_changes_only_what_settings_sent( + tmp_path: Path, transport: str +) -> None: + """The handler's own list: save what Settings sends and nothing else moves. + + The server is stored switched off and stays off, so nothing is started. + """ + data = tmp_path / "data" + data.mkdir() + path = _config(data) + spec = McpServerConfig(**EVERY_FIELD[transport]) + assert spec.enabled is False + save_mcp_server("walk", spec, path) + _daemon, task, ws = await _start(data, MockProvider(default=Script(kind="text"))) + try: + wire = {name: getattr(spec, name) for name in SENT_BY_SETTINGS} + reply = await _ask(ws, {"type": "set_mcp_server", "id": "walk", **wire}, "setup_state") + assert reply["type"] == "setup_state", reply + after = load_config(path).mcp.servers["walk"] + changed = { + name + for name in McpServerConfig.model_fields + if getattr(after, name) != getattr(spec, name) + } + assert changed == set() + finally: + await _stop(task, ws) + + +async def test_a_new_address_drops_what_was_bound_to_the_old_one_and_keeps_the_rest( + tmp_path: Path, +) -> None: + data = tmp_path / "data" + data.mkdir() + path = _config(data) + spec = McpServerConfig(**EVERY_FIELD["stdio"]) + save_mcp_server("walk", spec, path) + _daemon, task, ws = await _start(data, MockProvider(default=Script(kind="text"))) + try: + reply = await _ask( + ws, + { + "type": "set_mcp_server", + "id": "walk", + "transport": "http", + "command": [], + "url": "http://127.0.0.1:9000/mcp", + "enabled": False, + }, + "setup_state", + ) + assert reply["type"] == "setup_state", reply + after = load_config(path).mcp.servers["walk"] + default = McpServerConfig(transport="http", url="http://127.0.0.1:9000/mcp") + for name in BOUND_TO_THE_HOST: + assert getattr(after, name) == getattr(default, name), name + for name in CARRIED: + assert getattr(after, name) == getattr(spec, name), name + finally: + await _stop(task, ws) diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index b4e787f8..581cafa2 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -14055,3 +14055,29 @@ exists adds no tool and keeps one file, named in `path`, per call. **Notes:** The memory-file cap is checked on the final text. The schema grew by about 600 characters; no new tool was added. +--- + +### TD-5277 — Tests that fail when an MCP server key is left off a list + +**Size:** 1 · **Depends on:** TD-4403, TD-5105 · **Status:** Done + +An MCP server's block is written from a list of keys kept by hand +(`_mcp_server_lines`), and a Settings save copies the hand-set keys across +from a second list in the `set_mcp_server` handler. Both lists are complete +today: this is not a fix. But a key added to `McpServerConfig` and to +neither list would be dropped by the next Settings save, silently, and no +test would have said so. + +- [x] A table gives every field of the model a value per transport, and + fails when the model gains a field it does not name. +- [x] The writer writes every field and it loads back equal, for stdio and + for http. +- [x] Every field is sent by Settings, bound to the server's address, or + carried across a save, and only one of the three. +- [x] A Settings save changes nothing it did not send; a new address drops + what was bound to the old one and keeps the rest + (`test_mcp_config_parity.py`). + +**Notes:** Tests only; no product code changed. The fuller step in the +research note, deriving the writer's keys from the model, is not taken. + From 76fa87a5fe0247b7e1ab042f284bcf178557e908 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:18:01 +0000 Subject: [PATCH 23/35] TD-5278: the worktree fix holds for a symlinked data dir, and git's own files stay read-only The list of hidden paths holds a symlinked directory under both its names. TD-5261 compared the names as written, so the unresolved one was still masked last, over the workspace. A hidden directory that contains the workspace is now found by its resolved path and masked once. Once the workspace is bound into it, that mask is remounted read-only. A real-git, real-bubblewrap test shows that from inside a worktree session the worktree's admin directory, commondir, config.worktree, the repository's config and hooks cannot be written and git add fails, with the repository's metadata byte-identical afterwards. Co-Authored-By: Claude Fable 5.2 --- core/tests/test_shell_sandbox.py | 124 ++++++++++++++++++++++++++++++- core/tstd/shell_sandbox.py | 16 +++- docs/tst-desk-backlog.md | 37 +++++++++ 3 files changed, 172 insertions(+), 5 deletions(-) diff --git a/core/tests/test_shell_sandbox.py b/core/tests/test_shell_sandbox.py index 3728fa10..b87235dd 100644 --- a/core/tests/test_shell_sandbox.py +++ b/core/tests/test_shell_sandbox.py @@ -123,11 +123,37 @@ def at(*run: str) -> int: bind = at("--bind", str(ws), str(ws)) inside = at("--perms", "0000", "--tmpfs", str(ws / "secrets")) elsewhere = at("--perms", "0000", "--tmpfs", str(fake_home / ".ssh")) + closed = at("--remount-ro", str(data)) # Around the workspace: first, and passable. Inside it or apart: last, and closed. - assert around < bind < inside + assert around < bind < closed < inside assert bind < elsewhere - # Masked once, never a second time over the workspace. - assert argv.count(str(data)) == 1 + # Once as the passable mask and once to close it to writes (TD-5278): + # never a second mask over the workspace. + assert argv.count(str(data)) == 2 + closed_masks = [ + argv[i + 3] + for i in range(len(argv) - 3) + if argv[i : i + 3] == ["--perms", "0000", "--tmpfs"] + ] + assert str(data) not in closed_masks + + def test_a_symlinked_hidden_dir_around_the_workspace_is_masked_once( + self, tmp_path: Path + ) -> None: + """TD-5278: the hidden list holds such a directory under both names.""" + fake_home = tmp_path / "home" + fake_home.mkdir() + real_data = (tmp_path / "disk" / "data").resolve() + (real_data / "worktrees" / "wt1").mkdir(parents=True) + link = tmp_path / "data-link" + link.symlink_to(real_data) + # The daemon knows its data dir by the link; the workspace is under it. + workspace = link / "worktrees" / "wt1" + argv = build_plan(str(workspace), "true", ON, home=fake_home, extra_hidden=[link]).argv + masks = [argv[i + 1] for i, arg in enumerate(argv) if arg == "--tmpfs"] + assert masks.count(str(real_data)) == 1 + assert str(link) not in masks + assert argv.index(str(real_data)) < argv.index("--bind") def test_masks_come_after_the_workspace_bind(self, fake_home: Path) -> None: # The workspace is the home directory itself: the masks must still win. @@ -359,6 +385,98 @@ async def test_a_worktree_inside_the_data_dir_is_still_the_workspace(self) -> No shutil.rmtree(data / "worktrees", ignore_errors=True) (data / "sessions.json").unlink(missing_ok=True) + async def test_a_worktree_under_a_symlinked_hidden_dir_still_works(self) -> None: + """TD-5278: the mask is laid once, under the directory's real name.""" + # Not under /tmp: the sandbox gives a command a /tmp of its own, and + # the link has to be there for the command to come in through it. + host_dir = Path(tempfile.mkdtemp(dir="/var/tmp")) + try: + real_data = host_dir / "disk" / "data" + ws = real_data / "worktrees" / "wt1" + ws.mkdir(parents=True) + (real_data / "secret.txt").write_text("DATA-DIR-SECRET", encoding="utf-8") + link = host_dir / "data-link" + link.symlink_to(real_data) + section = SandboxSection(enabled=True, hidden_paths=[str(link)]) + out = await run_shell( + _session(link / "worktrees" / "wt1", section), + "echo in > made.txt && cat made.txt; " + f"cat {link}/secret.txt 2>/dev/null || echo NO-DATA; " + f"touch {real_data}/planted 2>/dev/null || echo NO-PLANT; " + f"chmod 755 {real_data} 2>/dev/null || echo NO-CHMOD", + ) + assert (ws / "made.txt").read_text() == "in\n", out + assert "NO-DATA" in out and "DATA-DIR-SECRET" not in out + # The mask cannot be written into or opened up from inside. + assert "NO-PLANT" in out and "NO-CHMOD" in out + assert not (real_data / "planted").exists() + finally: + shutil.rmtree(host_dir, ignore_errors=True) + + @pytest.mark.skipif(shutil.which("git") is None, reason="needs git") + async def test_a_worktrees_git_metadata_cannot_be_written_from_inside( + self, tmp_path: Path + ) -> None: + """TD-5278: what git reads later, on the host, stays as it was. + + A writable admin dir is enough for a sandboxed command to plant a + ``core.fsmonitor`` or redirect ``commondir``; the person's next + ``git status`` outside the sandbox would then run it. + """ + from tstd.logging import user_data_dir + + def git(*args: str, cwd: Path) -> str: + done = subprocess.run( + ["git", "-c", "user.name=t", "-c", "user.email=t@example.com", *args], + cwd=cwd, + capture_output=True, + text=True, + check=True, + ) + return done.stdout.strip() + + repo = tmp_path / "project" + repo.mkdir() + git("init", "-q", "-b", "main", cwd=repo) + (repo / "a.txt").write_text("one\n", encoding="utf-8") + git("add", "a.txt", cwd=repo) + git("commit", "-q", "-m", "first", cwd=repo) + ws = user_data_dir() / "worktrees" / "wt-git" + ws.parent.mkdir(parents=True, exist_ok=True) + git("worktree", "add", "-q", "-b", "tst/wt/x", str(ws), cwd=repo) + admin = Path(git("rev-parse", "--absolute-git-dir", cwd=ws)) + common = repo / ".git" + assert admin.parent == common / "worktrees" + + def snapshot() -> dict[str, bytes]: + return { + str(path.relative_to(common)): path.read_bytes() + for path in sorted(common.rglob("*")) + if path.is_file() + } + + before = snapshot() + try: + out = await run_shell( + _session(ws, ON), + "git status --short >/dev/null 2>&1 && echo STATUS-OK || echo STATUS-FAILED; " + "echo two > a.txt; " + f"echo '[core]' >> {admin}/config.worktree 2>/dev/null || echo NO-WT-CONFIG; " + f"echo /tmp/elsewhere > {admin}/commondir 2>/dev/null || echo NO-COMMONDIR; " + f"echo x >> {common}/config 2>/dev/null || echo NO-CONFIG; " + f"echo x > {common}/hooks/pre-commit 2>/dev/null || echo NO-HOOK; " + "git add a.txt 2>/dev/null && echo ADDED || echo NO-ADD", + ) + # The work itself lands in the worktree. + assert (ws / "a.txt").read_text() == "two\n" + for refused in ("NO-WT-CONFIG", "NO-COMMONDIR", "NO-CONFIG", "NO-HOOK", "NO-ADD"): + assert refused in out, out + assert snapshot() == before, "git metadata changed from inside the sandbox" + # And the host's git still sees an ordinary, uncommitted change. + assert git("status", "--short", cwd=ws).strip() == "M a.txt" + finally: + shutil.rmtree(user_data_dir() / "worktrees", ignore_errors=True) + async def test_timeout_kills_the_sandboxed_tree(self, tmp_path: Path) -> None: ws = tmp_path / "ws" ws.mkdir() diff --git a/core/tstd/shell_sandbox.py b/core/tstd/shell_sandbox.py index fccde648..98a04ceb 100644 --- a/core/tstd/shell_sandbox.py +++ b/core/tstd/shell_sandbox.py @@ -452,7 +452,15 @@ def build_plan( real_ws = Path(workspace).resolve() filter_path = seccomp if seccomp is not None else seccomp_file() hidden = hidden_paths(section, home, extra_hidden) - around = [p for p in hidden if p.is_dir() and p != real_ws and real_ws.is_relative_to(p)] + # Judged on resolved paths, each directory once (TD-5278): the hidden + # list holds a symlinked directory under both its names, and the + # unresolved one, left for last, would cover the workspace again. + around: list[Path] = [] + for path in hidden: + real = path.resolve() + contains = real.is_dir() and real != real_ws and real_ws.is_relative_to(real) + if contains and real not in around: + around.append(real) # Outermost first, so an inner one is mounted inside the outer. around.sort(key=lambda p: len(p.parts)) argv = [ @@ -477,8 +485,12 @@ def build_plan( argv += ["--bind", str(real_ws), str(real_ws)] for pin in _pins(real_ws): argv += ["--ro-bind", str(pin), str(pin)] + # Once the workspace is bound into it, the mask itself is closed to + # writes: nothing can be put beside the workspace or re-moded (TD-5278). + for path in around: + argv += ["--remount-ro", str(path)] for path in hidden: - if path in around: + if path.resolve() in around: continue if path.is_dir(): argv += ["--perms", "0000", "--tmpfs", str(path)] diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 581cafa2..93c9f928 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -14081,3 +14081,40 @@ test would have said so. **Notes:** Tests only; no product code changed. The fuller step in the research note, deriving the writer's keys from the model, is not taken. +--- + +### TD-5278 — The worktree fix holds for a symlinked data dir, and git's own files stay read-only + +**Size:** 2 · **Depends on:** TD-5261 · **Status:** Done + +Checking TD-5261 against the research note written after it found one hole +in the fix and two things it had not shown. The list of hidden paths holds +a symlinked directory under both its names; TD-5261 compared the names as +written, so the unresolved one was still masked last, over the workspace. +A data dir reached through a symlink is common (a home directory on +another volume). And nothing yet showed that a worktree session's git +metadata cannot be written from inside, which matters more than it looks: +a command that can write the worktree's admin directory can plant a +setting that the person's next `git status` on the host runs. + +- [x] A hidden directory that contains the workspace is found by its + resolved path and masked once, whichever name the daemon knows it by + (`test_a_symlinked_hidden_dir_around_the_workspace_is_masked_once`, + and with real bubblewrap + `test_a_worktree_under_a_symlinked_hidden_dir_still_works`). +- [x] Once the workspace is bound into it, that mask is remounted + read-only: a command cannot put a file beside the workspace or change + the mask's mode (same real test). +- [x] From inside a worktree session, with real git and real bubblewrap, + the worktree's admin directory, its `commondir`, a `config.worktree`, + the repository's `config` and its hooks cannot be written, `git add` + fails, the repository's metadata is byte-identical afterwards, and the + host's git sees an ordinary uncommitted change + (`test_a_worktrees_git_metadata_cannot_be_written_from_inside`). + +**Notes:** Whether a command inside the sandbox can make a user namespace +of its own and undo a read-only bind depends on the kernel's policy; on +this machine it cannot. Turning that off in bubblewrap itself +(`--disable-userns`) needs a version check and belongs with the sandbox +backend interface (TD-5264). + From 5257ec6e728d3916e04dd66ec753239f7c7fe684 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:31:00 +0000 Subject: [PATCH 24/35] TD-5269: an abandoned stream is closed at once, not when collected Holding a server's slot wrapped the stream in a second generator, so closing the outer one no longer closed the exchange there and then. A reader that stopped early, or a stall that was returned, left the connection open until the garbage collector reached it: the three hang-up tests in test_provider_timeouts.py failed in the full suite. The exchange is now closed with contextlib.aclosing when the outer generator is, so the connection is hung up and the slot given back at once. A test covers an abandoned stream's slot. Co-Authored-By: Claude Fable 5.2 --- core/tests/test_endpoint_gate.py | 20 ++++++++++++++++++++ core/tstd/provider.py | 12 ++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/core/tests/test_endpoint_gate.py b/core/tests/test_endpoint_gate.py index a474ad71..9891c82f 100644 --- a/core/tests/test_endpoint_gate.py +++ b/core/tests/test_endpoint_gate.py @@ -251,3 +251,23 @@ async def test_the_daemon_gives_clients_for_one_server_one_gate(tmp_path: Path) assert brain.gate is not None and brain.gate is worker.gate # Another port on this computer is another server with a count of its own. assert other.gate is not None and other.gate is not brain.gate + + +async def test_a_stream_the_reader_abandons_gives_its_slot_back_at_once() -> None: + """The reader stops after one chunk: the exchange is closed there and + then, so the slot is free and the connection hung up, not left to the + garbage collector (found by test_provider_timeouts after TD-5269).""" + server = _Server(hold=0.0) + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + client = server.client() + client.gate = gates.for_url(client.base_url) + assert client.gate is not None + stream = client.chat_completion_stream(_request(stream=True)) + async for _chunk in stream: + assert client.gate.locked() + break + await stream.aclose() # type: ignore[attr-defined] + assert not client.gate.locked() + # And the next call is not left waiting. + again = await asyncio.wait_for(client.chat_completion(_request()), 2) + assert not isinstance(again, ProviderError) diff --git a/core/tstd/provider.py b/core/tstd/provider.py index 9e6052bc..bb338914 100644 --- a/core/tstd/provider.py +++ b/core/tstd/provider.py @@ -15,7 +15,7 @@ import json import random import re -from collections.abc import AsyncIterator, Awaitable, Callable, Sequence +from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Sequence from dataclasses import dataclass, field, replace from datetime import UTC, datetime from email.utils import parsedate_to_datetime @@ -1255,11 +1255,15 @@ async def _stream_once() -> AsyncIterator[StreamChunk | ProviderError]: # Held from the request to the stream's end (TD-5269): the server # is busy with this call for that long. Waiting for a slot is not # part of the reply's own time limit. - async with self._slot(): - async for chunk in _stream_exchange(): + # ``aclosing``: when the consumer stops early, or a stall is + # returned, the exchange is closed here and now, which is what + # hangs up the connection. Left to the garbage collector, the + # server would go on generating into a socket nobody reads. + async with self._slot(), contextlib.aclosing(_stream_exchange()) as exchange: + async for chunk in exchange: yield chunk - async def _stream_exchange() -> AsyncIterator[StreamChunk | ProviderError]: + async def _stream_exchange() -> AsyncGenerator[StreamChunk | ProviderError, None]: started = asyncio.get_running_loop().time() try: async with aconnect_sse( From 55ef89d00695cc576324e45f8c673737d75273dc Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:03:33 +0000 Subject: [PATCH 25/35] TD-5279: a paired phone changes MCP servers only if the computer said so Phone permissions, stage 1. TD-5276 made the MCP page the computer's own; the owner's decision is that a phone keeps the ability as an opt-in the person at the computer switches on after reading what it allows. - /phone-permissions.yaml holds the grants, each with an optional expiry. It is no config key, and everything about it fails closed. - set_phone_permission is loopback-only: a phone can never grant itself. - The four MCP verbs move from LOOPBACK_VERBS to PHONE_OPT_IN. A paired phone without the grant gets phone_permission_off; the older remote token never gets a grant; another computer stays not_allowed. - peer_policy.check takes the live grants, defaulting to none. - Each use by a phone sends phone_change to the computer's windows and records the server's origin in /mcp-origins.yaml. - Removing a phone lists what it added and offers to remove it too. - Doctor gains a warn status and uses it while a phone permission is on. - Settings, Phones and devices: "What paired phones may change", with the disclosure and Keep off / 1 hour / until I turn it off. Co-Authored-By: Claude Fable 5.2 --- DECISIONS.md | 49 +++ android/README.md | 13 +- core/scripts/generate_protocol_fixtures.py | 11 + core/tests/test_mcp_peer.py | 386 ++++++++++++++++-- core/tests/test_mcp_settings.py | 3 + core/tests/test_pairing.py | 11 +- core/tests/test_peer_policy.py | 104 ++++- core/tests/test_phone_permissions.py | 238 +++++++++++ core/tests/test_remote_devices.py | 10 +- core/tstd/daemon.py | 180 +++++++- core/tstd/hub.py | 9 +- core/tstd/peer_policy.py | 63 ++- core/tstd/phone_permissions.py | 233 +++++++++++ core/tstd/phone_store.py | 130 ++++++ core/tstd/protocol.py | 67 ++- docs/architecture.md | 19 +- docs/tst-desk-backlog.md | 100 +++++ ui/e2e/app-walk.spec.ts | 13 +- ui/e2e/census.json | 1 + ui/src/lib/client.ts | 1 + ui/src/lib/components/DoctorPane.svelte | 7 +- ui/src/lib/components/PairedDevices.svelte | 38 +- ui/src/lib/components/PhonePermissions.svelte | 254 ++++++++++++ ui/src/lib/components/SettingsMcp.svelte | 14 +- ui/src/lib/components/SettingsPane.svelte | 8 +- ui/src/lib/doctor.svelte.ts | 1 + ui/src/lib/error-copy.ts | 5 + ui/src/lib/mcp-read-only.test.ts | 10 +- ui/src/lib/notifications.svelte.ts | 6 + ui/src/lib/pair-device.svelte.ts | 9 +- ui/src/lib/phone-permissions.test.ts | 349 ++++++++++++++++ ui/src/lib/phone-permissions.ts | 97 +++++ ui/src/lib/protocol-fixtures.json | 24 +- ui/src/lib/protocol.ts | 39 +- ui/src/lib/settings.svelte.ts | 25 ++ ui/src/lib/settings.test.ts | 3 + 36 files changed, 2431 insertions(+), 99 deletions(-) create mode 100644 core/tests/test_phone_permissions.py create mode 100644 core/tstd/phone_permissions.py create mode 100644 core/tstd/phone_store.py create mode 100644 ui/src/lib/components/PhonePermissions.svelte create mode 100644 ui/src/lib/phone-permissions.test.ts create mode 100644 ui/src/lib/phone-permissions.ts diff --git a/DECISIONS.md b/DECISIONS.md index 7f6d6647..59c21660 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -20153,3 +20153,52 @@ own (reverses TD-4602); deriving the child from the tool-call id's prefix in the window (the window would be parsing an id format it was never promised). +## 2026-11-20 — TD-5279: phone permissions, with MCP servers as the first (Class B; supersedes part of TD-5276) + +**Decision:** a paired phone may send `set_mcp_server`, `delete_mcp_server`, +`set_mcp_secret` and `delete_mcp_secret` only while the capability +`mcp_servers` is switched on, and it is switched on only by +`set_phone_permission`, a new verb in `LOOPBACK_VERBS`. The four verbs leave +`LOOPBACK_VERBS` for a new table, `PHONE_OPT_IN` (verb to capability). +`peer_policy.check` gains `grants`, defaulting to none, and `PeerView` gains +`phone`. Without the grant the refusal is `phone_permission_off`. Grants +live in `/phone-permissions.yaml` and may carry an expiry. Each +use by a phone sends `phone_change` to the computer's windows and records +the server's origin in `/mcp-origins.yaml`. Removing a phone +offers to remove what it added. Doctor gains a `warn` row status and uses +it while any phone permission is on. + +**Rationale:** this is the owner's decision, relayed by the planning +session: the ability stays, optional, default off, with the risk stated +before it is switched on. Three rules make that safe to offer. Only the +computer can grant, which is why the switch is the one verb with no +setting. Only a paired phone can use a grant: the older remote token is no +device, so nothing switches these verbs on for it, and another computer +stays deny-by-default. And use is visible and leaves a trail. The grants +are in the data dir, beside `remote-attach.yaml` and `approvals.yaml`, and +not a `config.yaml` key, so no repository and no config editor can set +one. Everything about the file fails closed. + +**What the default changes for a phone:** nothing since TD-5276 (refused); +against v0.4.6, a phone can no longer add or change an MCP server until +the computer allows it. + +**Rejected:** a single "trust my phones" switch (one sentence cannot +disclose six different risks, and the owner asked for this not to be +built); a permanent never-list for the most dangerous verbs (the owner +decided every one is opt-in, with stronger words for the two that can +send a stored secret to a host the phone names); keeping the grant in +`config.yaml` (the workspace layer and the config editor could then reach +it); an audit-database record now (every record is keyed to a session; +the log line and the origins file are the trail until that is designed). + +**Wording:** the refusal and the phone's page say "Settings, Phones and +devices", the section's real name, where the design note said "Settings, +Devices". + +**Still to do, one story each:** the same switch for turning approvals +off, model and key setup, computer-use control, unattended runs, the +connector sign-ins, `shutdown`, releasing the computer-use stop, changing +a saved key's host and changing the mail server; then the inventory that +makes a new verb start refused for a phone. + diff --git a/android/README.md b/android/README.md index ce332db3..10a64eb0 100644 --- a/android/README.md +++ b/android/README.md @@ -24,10 +24,15 @@ Still works as a fallback. On the computer, turn on **Allow remote attach** in S The shell refuses any host that is not a Tailscale address (`100.64.0.0/10`, `fd7a:115c:a1e0::/48`) or a `*.ts.net` name. It does not listen on the network. The UI is served to the WebView on `127.0.0.1` only, on the same port each launch when it is free. -Some things stay with the computer and are refused from a phone: adding, changing or -removing an MCP server or its token (an MCP server is a command the computer runs), the -raw Settings → Configuration editor, trusting a workspace's own config, accepting an -instruction-file edit, and removing a model. The phone shows the MCP list read-only. +Some things stay with the computer and are refused from a phone: the raw Settings → +Configuration editor, trusting a workspace's own config, accepting an instruction-file +edit, and removing a model. + +Adding, changing or removing an MCP server or its token is off for phones unless you turn +it on at the computer (**Settings → Phones and devices → What paired phones may change**). +An MCP server is a command the computer runs, so read what that switch says before +turning it on. While it is off the phone shows the MCP list read-only. A phone cannot turn +it on, and the computer's window is told each time a phone changes a server (TD-5279). ## Build diff --git a/core/scripts/generate_protocol_fixtures.py b/core/scripts/generate_protocol_fixtures.py index 59f4d950..daa039f7 100644 --- a/core/scripts/generate_protocol_fixtures.py +++ b/core/scripts/generate_protocol_fixtures.py @@ -185,6 +185,7 @@ PairingCode, PairingCompleted, ParseJob, + PhoneChange, Ping, PlanPinned, PlanReview, @@ -255,6 +256,7 @@ SetLoadGlobalMemory, SetMcpSecret, SetMcpServer, + SetPhonePermission, SetPlan, SetPreset, SetPresetLabel, @@ -650,6 +652,10 @@ "start_pairing": StartPairing(), "cancel_pairing": CancelPairing(), "remove_remote_device": RemoveRemoteDevice(device_id="dev_0123456789ab"), + # What a paired phone may change (TD-5279): on for an hour. + "set_phone_permission": SetPhonePermission( + capability="mcp_servers", enabled=True, expires_in_seconds=3600 + ), # Other computers (TD-5015). Loopback only; the code is a placeholder. "add_machine": AddMachine(link="tstdesk://pair?h=100.64.1.5&p=39079&c=ABCD2345&n=tower"), "add_machine_address": AddMachine(host="100.64.1.5", port=39079, code="ABCD-2345"), @@ -1590,6 +1596,11 @@ id="dev_0123456789ab", name="Pixel", paired_at="2026-10-14T10:41:00Z" ) ), + "phone_change": PhoneChange( + device_name="Pixel", + capability="mcp_servers", + summary="added MCP server 'files': npx files-server", + ), # One connected computer and one self-pair that is unreachable (TD-5015). "machine_list": MachineList( machines=[ diff --git a/core/tests/test_mcp_peer.py b/core/tests/test_mcp_peer.py index 0ae0882e..efc76840 100644 --- a/core/tests/test_mcp_peer.py +++ b/core/tests/test_mcp_peer.py @@ -1,24 +1,66 @@ -"""Only the computer changes its MCP servers (TD-5276). +"""A paired phone and the MCP page, at the socket (TD-5279). -A stdio MCP server is a command the daemon runs. A paired phone could add -one: the verb was open to it and the handler saved and started whatever it -was sent. These cases are a phone trying, at the socket, and the computer's -own window still being able to. +A stdio MCP server is a command the daemon runs. On main a paired phone +could add one: the verb was open to it and the handler saved and started +whatever it was sent. Now a phone is refused unless the person at the +computer switched "MCP servers" on for phones, and when a phone does use +that, the computer's window is told and the server remembers its origin. """ from __future__ import annotations import asyncio +import contextlib +import json +from datetime import UTC, datetime, timedelta from pathlib import Path from typing import Any import pytest +import yaml +from websockets.asyncio.client import connect from tests.test_add_model import _ask, _stop -from tests.test_models_on_phone import _phone, _seeded +from tests.test_mcp_secrets import MemoryKeychain, memory_keychain # noqa: F401 +from tests.test_models_on_phone import PHONE, _seeded +from tests.test_models_on_phone import _phone as _legacy_token_client +from tstd import phone_permissions as perms from tstd.config import load_config +from tstd.protocol import PROTOCOL_VERSION SERVER = {"type": "set_mcp_server", "id": "walk", "transport": "stdio", "command": ["true"]} +GRANT = {"type": "set_phone_permission", "capability": "mcp_servers", "enabled": True} +VALUE = { + "type": "set_mcp_secret", + "server_id": "walk", + "target": "env", + "name": "TOKEN", + "value": "a-test-value-0001", +} + + +async def _pair(daemon: Any, name: str = "Pixel 8", kind: str = "phone") -> tuple[Any, str]: + """A connection signed in with a paired device's own key, and the device id.""" + device, key = daemon.remote_devices.add(name, kind=kind) + ws = await connect(f"ws://127.0.0.1:{daemon.ws_server.port}{PHONE}") + await ws.send(json.dumps({"type": "hello", "token": key, "version": PROTOCOL_VERSION})) + ack = json.loads(await ws.recv()) + assert ack["type"] == "hello_ack", ack + return ws, device.id + + +async def _drain(ws: Any) -> list[dict[str, Any]]: + """Everything already on its way to *ws*: broadcasts that are not a reply.""" + seen: list[dict[str, Any]] = [] + with contextlib.suppress(TimeoutError): + while True: + seen.append(json.loads(await asyncio.wait_for(ws.recv(), 0.3))) + return seen + + +async def _fresh(ws: Any, msg: dict[str, Any], want: str = "setup_state") -> dict[str, Any]: + await _drain(ws) + return await _ask(ws, msg, want) def _spy_on_reload(daemon: Any, monkeypatch: pytest.MonkeyPatch) -> list[int]: @@ -33,36 +75,43 @@ async def reload(config: Any) -> Any: return calls -async def test_a_phone_cannot_add_a_server(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: +async def test_a_phone_cannot_add_an_mcp_server_by_default( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: daemon, task, ws = await _seeded(tmp_path) - phone = await _phone(daemon) + phone, _ = await _pair(daemon) reloads = _spy_on_reload(daemon, monkeypatch) try: text = daemon.config_path.read_text(encoding="utf-8") - # Before the fix this ran: the marker appeared on the computer. + # On main this ran: the marker appeared on the computer. marker = tmp_path / "ran-on-the-computer" hostile = {**SERVER, "command": ["sh", "-c", f"touch {marker}"]} - reply = await _ask(phone, hostile, "setup_state") - assert reply["type"] == "error" and reply["code"] == "loopback_only" + reply = await _fresh(phone, hostile) + assert reply["type"] == "error" and reply["code"] == "phone_permission_off" assert reply["in_reply_to"] == "set_mcp_server" - # Nothing saved, nothing started. + assert "Settings, Phones and devices on the computer" in reply["message"] + # Nothing saved, nothing started, nothing recorded. assert daemon.config_path.read_text(encoding="utf-8") == text assert load_config(daemon.config_path).mcp.servers == {} assert reloads == [] + assert not perms.origins_path(daemon.data_dir).exists() await asyncio.sleep(0.3) assert not marker.exists(), "a phone made this computer run a command" + # And the phone is told the switch is off, so its page can say why. + state = await _fresh(phone, {"type": "get_setup_state"}) + assert state["phone_permissions"] == {"mcp_servers": {"enabled": False, "expires_at": None}} finally: await phone.close() await _stop(task, ws) -async def test_a_phone_cannot_change_remove_or_set_a_value_on_one( +async def test_a_phone_cannot_change_remove_or_set_a_value_by_default( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: daemon, task, ws = await _seeded(tmp_path) - phone = await _phone(daemon) + phone, _ = await _pair(daemon) try: - saved = await _ask(ws, SERVER, "setup_state") + saved = await _fresh(ws, SERVER) assert [row["id"] for row in saved["mcp_servers"]] == ["walk"] text = daemon.config_path.read_text(encoding="utf-8") reloads = _spy_on_reload(daemon, monkeypatch) @@ -70,19 +119,13 @@ async def test_a_phone_cannot_change_remove_or_set_a_value_on_one( {**SERVER, "command": ["sh", "-c", "id"]}, {**SERVER, "enabled": False}, {"type": "delete_mcp_server", "id": "walk"}, - { - "type": "set_mcp_secret", - "server_id": "walk", - "target": "env", - "name": "TOKEN", - "value": "v", - }, + VALUE, {"type": "delete_mcp_secret", "server_id": "walk", "target": "env", "name": "TOKEN"}, ] for attempt in attempts: - reply = await _ask(phone, attempt, "setup_state") + reply = await _fresh(phone, attempt) assert reply["type"] == "error", attempt["type"] - assert reply["code"] == "loopback_only", attempt["type"] + assert reply["code"] == "phone_permission_off", attempt["type"] assert daemon.config_path.read_text(encoding="utf-8") == text assert load_config(daemon.config_path).mcp.servers["walk"].command == ["true"] assert reloads == [] @@ -91,12 +134,291 @@ async def test_a_phone_cannot_change_remove_or_set_a_value_on_one( await _stop(task, ws) +async def test_a_phone_cannot_grant_itself(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + token_client = await _legacy_token_client(daemon) + try: + for peer in (phone, token_client): + reply = await _fresh(peer, GRANT) + assert reply["type"] == "error" and reply["code"] == "loopback_only" + assert reply["in_reply_to"] == "set_phone_permission" + assert not perms.permissions_path(daemon.data_dir).exists() + assert daemon.phone_store.live() == frozenset() + # Still refused afterwards. + assert (await _fresh(phone, SERVER))["code"] == "phone_permission_off" + finally: + await token_client.close() + await phone.close() + await _stop(task, ws) + + +async def test_a_phone_can_add_one_after_the_computer_grants( + tmp_path: Path, + memory_keychain: MemoryKeychain, # noqa: F811 +) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, device_id = await _pair(daemon) + try: + granted = await _fresh(ws, GRANT) + assert granted["type"] == "setup_state" + assert granted["phone_permissions"]["mcp_servers"] == {"enabled": True, "expires_at": None} + on_disk = yaml.safe_load( + perms.permissions_path(daemon.data_dir).read_text(encoding="utf-8") + ) + assert on_disk["grants"]["mcp_servers"]["enabled"] is True + # The phone hears the switch without asking. + told = [e for e in await _drain(phone) if e["type"] == "setup_state"] + assert told and told[-1]["phone_permissions"]["mcp_servers"]["enabled"] is True + assert "remote_devices" not in told[-1] + + reply = await _fresh(phone, SERVER) + assert reply["type"] == "setup_state" + assert [row["id"] for row in reply["mcp_servers"]] == ["walk"] + assert reply["mcp_servers"][0]["origin_device"] == "Pixel 8" + assert load_config(daemon.config_path).mcp.servers["walk"].command == ["true"] + + # The computer's window is told which phone did what, and sees the new list. + heard = await _drain(ws) + notices = [e for e in heard if e["type"] == "phone_change"] + assert notices == [ + { + "type": "phone_change", + "seq": 1, + "device_name": "Pixel 8", + "capability": "mcp_servers", + "summary": "added MCP server 'walk': true", + } + ] + states = [e for e in heard if e["type"] == "setup_state"] + assert states and [row["id"] for row in states[-1]["mcp_servers"]] == ["walk"] + device = next(d for d in states[-1]["remote_devices"] if d["id"] == device_id) + assert device["mcp_servers_added"] == ["walk"] + + # The origin is on file, in the data dir and not in config.yaml. + origins = perms.load_origins(daemon.data_dir) + assert origins["walk"].device_id == device_id + assert "origin" not in daemon.config_path.read_text(encoding="utf-8") + + # A value set from the phone is announced by name, never by value. + stored = await _fresh(phone, VALUE) + assert stored["type"] == "setup_state", stored + notices = [e for e in await _drain(ws) if e["type"] == "phone_change"] + assert [n["summary"] for n in notices] == [ + "stored a value for variable 'TOKEN' of MCP server 'walk'." + ] + assert "a-test-value-0001" not in json.dumps(notices) + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_phone_added_server_never_gets_allow_remote(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + await _fresh(ws, GRANT) + frame = {**SERVER, "allow_remote": True} + reply = await _fresh(phone, frame) + # The verb has no such field; whatever the reply, the opt-in stays off. + if reply["type"] == "setup_state": + assert load_config(daemon.config_path).mcp.servers["walk"].allow_remote is False + else: + assert load_config(daemon.config_path).mcp.servers == {} + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_grant_is_for_paired_phones_only(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + token_client = await _legacy_token_client(daemon) + machine, _ = await _pair(daemon, "Other computer", kind="machine") + try: + await _fresh(ws, GRANT) + text = daemon.config_path.read_text(encoding="utf-8") + reply = await _fresh(token_client, SERVER) + assert reply["type"] == "error" and reply["code"] == "phone_permission_off" + reply = await _fresh(machine, SERVER) + assert reply["type"] == "error" and reply["code"] == "not_allowed" + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await machine.close() + await token_client.close() + await _stop(task, ws) + + +async def test_a_timed_grant_runs_out(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + clock = [datetime(2026, 11, 20, 18, 0, 0, tzinfo=UTC)] + monkeypatch.setattr(perms, "utc_now", lambda: clock[0]) + try: + granted = await _fresh(ws, {**GRANT, "expires_in_seconds": 3600}) + assert granted["phone_permissions"]["mcp_servers"] == { + "enabled": True, + "expires_at": "2026-11-20T19:00:00Z", + } + assert (await _fresh(phone, SERVER))["type"] == "setup_state" + clock[0] += timedelta(hours=1, seconds=1) + reply = await _fresh(phone, {**SERVER, "command": ["false"]}) + assert reply["type"] == "error" and reply["code"] == "phone_permission_off" + assert load_config(daemon.config_path).mcp.servers["walk"].command == ["true"] + state = await _fresh(ws, {"type": "get_setup_state"}) + assert state["phone_permissions"]["mcp_servers"]["enabled"] is False + finally: + await phone.close() + await _stop(task, ws) + + +async def test_the_windows_are_told_when_a_timed_grant_runs_out(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + await _fresh(ws, {**GRANT, "expires_in_seconds": 1}) + await _drain(phone) + for peer in (ws, phone): + for _ in range(40): + event = json.loads(await asyncio.wait_for(peer.recv(), 10)) + if event["type"] == "setup_state": + break + assert event["phone_permissions"]["mcp_servers"]["enabled"] is False + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_bad_grant_is_refused(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + try: + unknown = await _fresh(ws, {**GRANT, "capability": "everything"}) + assert unknown["type"] == "error" and unknown["code"] == "bad_request" + too_long = await _fresh(ws, {**GRANT, "expires_in_seconds": 8 * 24 * 3600}) + assert too_long["type"] == "error" and too_long["code"] == "bad_request" + assert daemon.phone_store.live() == frozenset() + assert not perms.permissions_path(daemon.data_dir).exists() + finally: + await _stop(task, ws) + + +async def test_turning_the_grant_off_keeps_existing_servers(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + await _fresh(ws, GRANT) + assert (await _fresh(phone, SERVER))["type"] == "setup_state" + off = await _fresh(ws, {**GRANT, "enabled": False}) + assert off["phone_permissions"]["mcp_servers"]["enabled"] is False + # As the disclosure says: it stops new changes, it removes nothing. + assert [row["id"] for row in off["mcp_servers"]] == ["walk"] + assert off["mcp_servers"][0]["origin_device"] == "Pixel 8" + assert load_config(daemon.config_path).mcp.servers["walk"].command == ["true"] + reply = await _fresh(phone, {"type": "delete_mcp_server", "id": "walk"}) + assert reply["type"] == "error" and reply["code"] == "phone_permission_off" + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_grant_survives_a_restart_of_the_store(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + try: + await _fresh(ws, GRANT) + from tstd.phone_store import PhoneStore + + assert PhoneStore(daemon.data_dir).live() == {"mcp_servers"} + finally: + await _stop(task, ws) + + +async def test_removing_a_device_reports_and_can_remove_what_it_added(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, device_id = await _pair(daemon) + try: + await _fresh(ws, GRANT) + await _fresh(ws, {**SERVER, "id": "mine"}) + assert (await _fresh(phone, SERVER))["type"] == "setup_state" + state = await _fresh(ws, {"type": "get_setup_state"}) + device = next(d for d in state["remote_devices"] if d["id"] == device_id) + assert device["mcp_servers_added"] == ["walk"] + + # Removing the phone alone leaves its server, still marked. + kept = await _fresh(ws, {"type": "remove_remote_device", "device_id": device_id}) + assert sorted(row["id"] for row in kept["mcp_servers"]) == ["mine", "walk"] + walk = next(row for row in kept["mcp_servers"] if row["id"] == "walk") + assert walk["origin_device"] == "Pixel 8" + assert perms.load_origins(daemon.data_dir)["walk"].device_id == device_id + finally: + with contextlib.suppress(Exception): + await phone.close() + await _stop(task, ws) + + +async def test_removing_a_device_with_what_it_added(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, device_id = await _pair(daemon) + try: + await _fresh(ws, GRANT) + await _fresh(ws, {**SERVER, "id": "mine"}) + assert (await _fresh(phone, SERVER))["type"] == "setup_state" + gone = await _fresh( + ws, {"type": "remove_remote_device", "device_id": device_id, "remove_added": True} + ) + assert gone["type"] == "setup_state" + # The phone's server goes; the computer's own stays. + assert [row["id"] for row in gone["mcp_servers"]] == ["mine"] + assert list(load_config(daemon.config_path).mcp.servers) == ["mine"] + assert perms.load_origins(daemon.data_dir) == {} + assert gone["remote_devices"] == [] + finally: + with contextlib.suppress(Exception): + await phone.close() + await _stop(task, ws) + + +async def test_a_phone_forgetting_itself_cannot_remove_servers(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, device_id = await _pair(daemon) + try: + await _fresh(ws, GRANT) + assert (await _fresh(phone, SERVER))["type"] == "setup_state" + await _fresh(ws, {**GRANT, "enabled": False}) + await _drain(phone) + await phone.send( + json.dumps( + {"type": "remove_remote_device", "device_id": device_id, "remove_added": True} + ) + ) + await asyncio.sleep(0.5) + # The grant is off: the flag from a phone removes nothing. + assert list(load_config(daemon.config_path).mcp.servers) == ["walk"] + assert daemon.remote_devices.summaries() == [] + finally: + with contextlib.suppress(Exception): + await phone.close() + await _stop(task, ws) + + +async def test_doctor_warns_while_a_phone_permission_is_on(tmp_path: Path) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + report = await _fresh(ws, {"type": "run_diagnostics"}, "diagnostics_report") + assert "phone_permissions" not in [c["name"] for c in report["checks"]] + await _fresh(ws, GRANT) + report = await _fresh(ws, {"type": "run_diagnostics"}, "diagnostics_report") + row = next(c for c in report["checks"] if c["name"] == "phone_permissions") + assert row["status"] == "warn" + assert row["detail"] == "paired phones may change: MCP servers ON (until turned off)" + finally: + await _stop(task, ws) + + async def test_a_phone_still_sees_the_list(tmp_path: Path) -> None: daemon, task, ws = await _seeded(tmp_path) - phone = await _phone(daemon) + phone, _ = await _pair(daemon) try: - await _ask(ws, SERVER, "setup_state") - state = await _ask(phone, {"type": "get_setup_state"}, "setup_state") + await _fresh(ws, SERVER) + state = await _fresh(phone, {"type": "get_setup_state"}) assert [row["id"] for row in state["mcp_servers"]] == ["walk"] assert state["mcp_servers"][0]["command"] == ["true"] finally: @@ -104,13 +426,17 @@ async def test_a_phone_still_sees_the_list(tmp_path: Path) -> None: await _stop(task, ws) -async def test_the_computers_own_window_still_manages_them(tmp_path: Path) -> None: +async def test_the_computer_can_still_do_all_of_it(tmp_path: Path) -> None: daemon, task, ws = await _seeded(tmp_path) try: - added = await _ask(ws, SERVER, "setup_state") + added = await _fresh(ws, SERVER) assert added["type"] == "setup_state" + assert added["mcp_servers"][0]["origin_device"] == "" assert load_config(daemon.config_path).mcp.servers["walk"].command == ["true"] - gone = await _ask(ws, {"type": "delete_mcp_server", "id": "walk"}, "setup_state") + # A change at the computer is no phone change: nobody is told, nothing recorded. + assert [e for e in await _drain(ws) if e["type"] == "phone_change"] == [] + assert not perms.origins_path(daemon.data_dir).exists() + gone = await _fresh(ws, {"type": "delete_mcp_server", "id": "walk"}) assert gone["type"] == "setup_state" and gone["mcp_servers"] == [] finally: await _stop(task, ws) diff --git a/core/tests/test_mcp_settings.py b/core/tests/test_mcp_settings.py index 92013bab..b62ac9d8 100644 --- a/core/tests/test_mcp_settings.py +++ b/core/tests/test_mcp_settings.py @@ -199,6 +199,9 @@ async def test_set_disable_delete_appear_on_setup_state( "excluded_tools": [], "env": [], "headers": [], + # TD-5279: added at the computer, so no phone to name. + "origin_device": "", + "origin_at": "", } ] row = _servers(path)["example"] diff --git a/core/tests/test_pairing.py b/core/tests/test_pairing.py index 4d6817ec..b65b30b1 100644 --- a/core/tests/test_pairing.py +++ b/core/tests/test_pairing.py @@ -475,6 +475,7 @@ def test_pairing_completed_carries_a_summary_and_no_key() -> None: "last_seen", "connected", "kind", + "mcp_servers_added", } @@ -626,7 +627,15 @@ async def test_pairing_completed_reaches_the_computer_and_not_other_phones(tmp_p assert row["connected"] is True and row["last_seen"] is not None state = await ask(window, {"type": "get_setup_state"}) assert state["remote_devices"] == [row] - assert set(row) == {"id", "name", "paired_at", "last_seen", "connected", "kind"} + assert set(row) == { + "id", + "name", + "paired_at", + "last_seen", + "connected", + "kind", + "mcp_servers_added", + } assert reply["token"] not in json.dumps(state) (row,) = await presence(window) assert row["connected"] is False diff --git a/core/tests/test_peer_policy.py b/core/tests/test_peer_policy.py index e9a43a5c..fc099af3 100644 --- a/core/tests/test_peer_policy.py +++ b/core/tests/test_peer_policy.py @@ -28,10 +28,12 @@ LOOPBACK_VERBS, MACHINE_PEER_VERBS, MACHINE_VERBS, + PHONE_OPT_IN, PeerView, check, classify, ) +from tstd.phone_permissions import CAPABILITIES from tstd.protocol import parse_client_message # Checked one by one for a way to switch approvals off, widen a rule, or @@ -70,7 +72,8 @@ ) VIEWER = PeerView(remote=False, machine=False, viewer=True) -PHONE_VIEWER = PeerView(remote=True, machine=False, viewer=True) +PHONE_VIEWER = PeerView(remote=True, machine=False, viewer=True, phone=True) +PHONE_PLAIN = PeerView(remote=True, machine=False, viewer=False, phone=True) MACHINE = PeerView(remote=True, machine=True, viewer=False) PLAIN_REMOTE = PeerView(remote=True, machine=False, viewer=False) BRIDGED = "s1:mch_0123456789ab" @@ -90,7 +93,7 @@ def _msg(kind: str, **fields: object) -> protocol.ClientMessage: (False, None, [], LOOPBACK_PLAIN), (True, "phone", ["machines"], PHONE_VIEWER), (True, None, ["machines"], PLAIN_REMOTE), # the legacy token is no device - (True, "phone", [], PLAIN_REMOTE), + (True, "phone", [], PHONE_PLAIN), (True, "machine", ["machines"], MACHINE), (True, "phone", ["machines", "bridge"], MACHINE), (False, None, ["bridge"], PeerView(remote=False, machine=True, viewer=False)), @@ -342,26 +345,82 @@ async def test_a_remote_peer_removing_itself_hears_no_devices_or_address(daemon: assert [d.name for d in daemon.remote_devices.summaries()] == ["Other phone"] -def test_the_mcp_page_is_loopback_only() -> None: - """TD-5276: a server is a command this computer runs; only it changes them.""" - frames = [ - _msg("set_mcp_server", id="x", transport="stdio", command=["sh", "-c", "id"]), - _msg("delete_mcp_server", id="x"), - _msg("set_mcp_secret", server_id="x", target="env", name="TOKEN", value="v"), - _msg("delete_mcp_secret", server_id="x", target="env", name="TOKEN"), - ] - self_paired = PeerView(remote=False, machine=True, viewer=False) - for frame in frames: - assert check(frame, LOOPBACK_PLAIN, bridged=False) is None, frame.type - assert check(frame, VIEWER, bridged=False) is None, frame.type - for peer in (PHONE_VIEWER, PLAIN_REMOTE, MACHINE, self_paired): - refusal = check(frame, peer, bridged=False) - assert refusal is not None, (frame.type, peer) - assert json.loads(refusal)["code"] == "loopback_only", (frame.type, peer) - assert json.loads(refusal)["in_reply_to"] == frame.type +MCP_FRAMES = [ + _msg("set_mcp_server", id="x", transport="stdio", command=["sh", "-c", "id"]), + _msg("delete_mcp_server", id="x"), + _msg("set_mcp_secret", server_id="x", target="env", name="TOKEN", value="v"), + _msg("delete_mcp_secret", server_id="x", target="env", name="TOKEN"), +] +SELF_PAIRED = PeerView(remote=False, machine=True, viewer=False) +GRANTED = frozenset({"mcp_servers"}) + + +def _code(refusal: str | None) -> str | None: + return None if refusal is None else json.loads(refusal)["code"] + + +def test_mcp_verbs_need_the_phone_permission() -> None: + """TD-5279: a server is a command this computer runs; a phone needs the grant.""" + for frame in MCP_FRAMES: + for phone in (PHONE_VIEWER, PHONE_PLAIN): + refusal = check(frame, phone, bridged=False, grants=()) + assert _code(refusal) == "phone_permission_off", frame.type + assert json.loads(refusal or "{}")["in_reply_to"] == frame.type + assert "MCP servers" in json.loads(refusal or "{}")["message"] + assert check(frame, phone, bridged=False, grants=GRANTED) is None, frame.type + + +def test_a_grant_never_applies_to_the_legacy_token_or_a_machine() -> None: + for frame in MCP_FRAMES: + # The legacy remote token is no device: nothing switches this on for it. + assert _code(check(frame, PLAIN_REMOTE, bridged=False, grants=GRANTED)) == ( + "phone_permission_off" + ), frame.type + for machine in (MACHINE, SELF_PAIRED): + assert _code(check(frame, machine, bridged=False, grants=GRANTED)) == ("not_allowed"), ( + frame.type, + machine, + ) + + +def test_the_computer_needs_no_grant() -> None: + for frame in MCP_FRAMES: + assert check(frame, LOOPBACK_PLAIN, bridged=False, grants=()) is None, frame.type + assert check(frame, VIEWER, bridged=False, grants=()) is None, frame.type + + +def test_check_refuses_when_grants_are_not_passed() -> None: + """A caller that forgets the argument fails in the safe direction.""" + for frame in MCP_FRAMES: + assert _code(check(frame, PHONE_VIEWER, bridged=False)) == "phone_permission_off" + + +def test_a_grant_for_something_else_opens_nothing() -> None: + for frame in MCP_FRAMES: + refusal = check(frame, PHONE_VIEWER, bridged=False, grants={"not_this_one"}) + assert _code(refusal) == "phone_permission_off", frame.type + + +def test_set_phone_permission_is_loopback_only() -> None: + """A phone must never grant itself; nor may the legacy token or another computer.""" + frame = _msg("set_phone_permission", capability="mcp_servers", enabled=True) + assert check(frame, LOOPBACK_PLAIN, bridged=False) is None + assert check(frame, VIEWER, bridged=False) is None + for peer in (PHONE_VIEWER, PHONE_PLAIN, PLAIN_REMOTE, MACHINE, SELF_PAIRED): + # Holding every grant changes nothing: it is not a grantable verb. + refusal = check(frame, peer, bridged=False, grants=GRANTED | {"set_phone_permission"}) + assert _code(refusal) == "loopback_only", peer + + +def test_every_opt_in_verb_names_a_known_capability_and_no_other_set() -> None: + assert set(PHONE_OPT_IN) <= protocol._KNOWN_CLIENT_TYPES + assert set(PHONE_OPT_IN.values()) <= set(CAPABILITIES) + # One classification per verb: opt-in is neither computer-only nor a machine verb. + assert LOOPBACK_VERBS.isdisjoint(PHONE_OPT_IN) + assert MACHINE_PEER_VERBS.isdisjoint(PHONE_OPT_IN) -def test_verbs_that_make_this_computer_run_a_command_stay_loopback_only() -> None: +def test_verbs_that_make_this_computer_run_a_command_are_never_open_to_a_phone() -> None: """A later tidy-up must not reopen any of these to a phone.""" runs_a_command = { "set_mcp_server", @@ -376,4 +435,7 @@ def test_verbs_that_make_this_computer_run_a_command_stay_loopback_only() -> Non "steering_edit", } assert runs_a_command <= protocol._KNOWN_CLIENT_TYPES - assert runs_a_command <= LOOPBACK_VERBS + assert runs_a_command <= LOOPBACK_VERBS | set(PHONE_OPT_IN) + # The switch itself has no setting. + assert "set_phone_permission" in LOOPBACK_VERBS + assert "set_phone_permission" not in PHONE_OPT_IN diff --git a/core/tests/test_phone_permissions.py b/core/tests/test_phone_permissions.py new file mode 100644 index 00000000..1a6d200c --- /dev/null +++ b/core/tests/test_phone_permissions.py @@ -0,0 +1,238 @@ +"""The phone-permissions file and the origins sidecar (TD-5279). + +Everything fails closed: whatever is not plainly a live grant reads as off. +""" + +from __future__ import annotations + +from datetime import UTC, datetime, timedelta +from pathlib import Path + +import pytest +import yaml + +from tstd import phone_permissions as perms +from tstd.config import load_config +from tstd.phone_store import PhoneStore, server_saved_summary, value_summary + +NOW = datetime(2026, 11, 20, 18, 0, 0, tzinfo=UTC) + + +def _write(tmp_path: Path, body: object) -> None: + perms.permissions_path(tmp_path).write_text(yaml.safe_dump(body), encoding="utf-8") + + +def test_absent_file_means_everything_off(tmp_path: Path) -> None: + assert perms.load_grants(tmp_path) == {} + store = PhoneStore(tmp_path) + assert store.live() == frozenset() + assert {name: s.enabled for name, s in store.states().items()} == {"mcp_servers": False} + assert store.doctor_row() is None + + +@pytest.mark.parametrize( + "text", + ["{{{ not yaml", "- a list\n", "just a string", "", "version: 1\ngrants: nope\n"], +) +def test_corrupt_file_means_everything_off(tmp_path: Path, text: str) -> None: + perms.permissions_path(tmp_path).write_text(text, encoding="utf-8") + assert perms.load_grants(tmp_path) == {} + + +def test_bytes_that_are_not_text_mean_everything_off(tmp_path: Path) -> None: + perms.permissions_path(tmp_path).write_bytes(b"\xff\xfe\x00grants") + assert perms.load_grants(tmp_path) == {} + + +@pytest.mark.parametrize( + "entry", + [ + {"enabled": "yes"}, # only a real true + {"enabled": 1}, + {"enabled": False}, + {"enabled": True, "expires_at": "tomorrow"}, # unreadable expiry is not forever + {"enabled": True, "expires_at": 12}, + "true", + None, + ], +) +def test_an_entry_that_is_not_plainly_a_grant_is_off(tmp_path: Path, entry: object) -> None: + _write(tmp_path, {"version": 1, "grants": {"mcp_servers": entry}}) + assert perms.load_grants(tmp_path) == {} + + +def test_another_file_version_is_off(tmp_path: Path) -> None: + _write(tmp_path, {"version": 2, "grants": {"mcp_servers": {"enabled": True}}}) + assert perms.load_grants(tmp_path) == {} + _write(tmp_path, {"grants": {"mcp_servers": {"enabled": True}}}) + assert perms.load_grants(tmp_path) == {} + + +def test_unknown_capability_in_file_is_ignored(tmp_path: Path) -> None: + _write( + tmp_path, + { + "version": 1, + "grants": {"everything": {"enabled": True}, "mcp_servers": {"enabled": True}}, + }, + ) + assert set(perms.load_grants(tmp_path)) == {"mcp_servers"} + + +def test_a_grant_round_trips_and_lists_every_capability(tmp_path: Path) -> None: + grants = perms.changed({}, "mcp_servers", True, None, NOW) + perms.save_grants(tmp_path, grants) + assert perms.load_grants(tmp_path) == grants + on_disk = yaml.safe_load(perms.permissions_path(tmp_path).read_text(encoding="utf-8")) + assert on_disk == { + "version": 1, + "grants": { + "mcp_servers": { + "enabled": True, + "granted_at": "2026-11-20T18:00:00Z", + "expires_at": None, + } + }, + } + perms.save_grants(tmp_path, perms.changed(grants, "mcp_servers", False, None, NOW)) + off = yaml.safe_load(perms.permissions_path(tmp_path).read_text(encoding="utf-8")) + assert off["grants"] == {"mcp_servers": {"enabled": False}} + assert perms.load_grants(tmp_path) == {} + + +def test_expired_grant_reads_as_off() -> None: + grants = perms.changed({}, "mcp_servers", True, 3600, NOW) + assert grants["mcp_servers"].expires_at == "2026-11-20T19:00:00Z" + assert perms.live(grants, NOW + timedelta(minutes=59, seconds=59)) == {"mcp_servers"} + assert perms.live(grants, NOW + timedelta(hours=1)) == frozenset() + assert perms.next_expiry(grants, NOW) == NOW + timedelta(hours=1) + assert perms.next_expiry(grants, NOW + timedelta(hours=2)) is None + assert perms.next_expiry(perms.changed({}, "mcp_servers", True, None, NOW), NOW) is None + + +def test_a_bad_ask_is_refused() -> None: + with pytest.raises(ValueError, match="Unknown phone permission"): + perms.changed({}, "everything", True, None, NOW) + with pytest.raises(ValueError, match="seven days"): + perms.changed({}, "mcp_servers", True, perms.MAX_EXPIRY_SECONDS + 1, NOW) + with pytest.raises(ValueError, match="seven days"): + perms.changed({}, "mcp_servers", True, 0, NOW) + + +def test_save_is_atomic(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + """A write that dies part-way leaves the old file, and no stray temp file.""" + perms.save_grants(tmp_path, perms.changed({}, "mcp_servers", True, None, NOW)) + before = perms.permissions_path(tmp_path).read_bytes() + + def boom(*_args: object, **_kwargs: object) -> None: + raise OSError("disk full") + + monkeypatch.setattr(perms.yaml, "safe_dump", boom) + with pytest.raises(OSError, match="disk full"): + perms.save_grants(tmp_path, {}) + assert perms.permissions_path(tmp_path).read_bytes() == before + assert [p.name for p in tmp_path.iterdir()] == ["phone-permissions.yaml"] + + +def test_a_failed_save_leaves_the_store_as_it_was( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + store = PhoneStore(tmp_path) + + def boom(*_args: object, **_kwargs: object) -> None: + raise OSError("read-only") + + monkeypatch.setattr(perms, "save_grants", boom) + with pytest.raises(OSError, match="read-only"): + store.set("mcp_servers", True, None) + assert store.live() == frozenset() + + +def test_the_store_follows_the_clock(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + clock = [NOW] + monkeypatch.setattr(perms, "utc_now", lambda: clock[0]) + store = PhoneStore(tmp_path) + store.set("mcp_servers", True, 3600) + assert store.live() == {"mcp_servers"} + state = store.states()["mcp_servers"] + assert state.enabled and state.expires_at == "2026-11-20T19:00:00Z" + row = store.doctor_row() + assert row is not None and row.status == "warn" + assert row.detail == "paired phones may change: MCP servers ON (until 2026-11-20T19:00:00Z)" + clock[0] = NOW + timedelta(hours=1, seconds=1) + assert store.live() == frozenset() + assert store.states()["mcp_servers"].enabled is False + assert store.doctor_row() is None + # A second store reads the same file and reaches the same answer. + assert PhoneStore(tmp_path).live() == frozenset() + + +def test_the_doctor_row_says_until_turned_off(tmp_path: Path) -> None: + store = PhoneStore(tmp_path) + store.set("mcp_servers", True, None) + row = store.doctor_row() + assert row is not None + assert row.detail == "paired phones may change: MCP servers ON (until turned off)" + assert row.fix is None + + +def test_workspace_config_cannot_grant(tmp_path: Path) -> None: + """The grants are no config key: one written into a config file does nothing.""" + data = tmp_path / "data" + data.mkdir() + workspace = tmp_path / "ws" / ".tst" + workspace.mkdir(parents=True) + text = "phone_permissions:\n mcp_servers:\n enabled: true\n" + (workspace / "config.yaml").write_text(text, encoding="utf-8") + (data / "config.yaml").write_text(text, encoding="utf-8") + for path in (workspace / "config.yaml", data / "config.yaml"): + assert not hasattr(load_config(path), "phone_permissions") + # Only phone-permissions.yaml in the data dir is read, and it is absent. + assert PhoneStore(data).live() == frozenset() + assert PhoneStore(workspace).live() == frozenset() + + +def test_origins_round_trip_and_ignore_what_is_malformed(tmp_path: Path) -> None: + store = PhoneStore(tmp_path) + store.record_origin("probe", "dev_1", "Pixel 8") + store.record_origin("other", "dev_2", "Tablet") + again = PhoneStore(tmp_path) + origin = again.origin_of("probe") + assert origin is not None and (origin.device_id, origin.device_name) == ("dev_1", "Pixel 8") + assert again.servers_from("dev_1") == ["probe"] + again.forget_origin("probe") + assert PhoneStore(tmp_path).servers_from("dev_1") == [] + perms.origins_path(tmp_path).write_text( + "version: 1\nservers:\n bad: nope\n worse: {device_id: 3}\n", encoding="utf-8" + ) + assert perms.load_origins(tmp_path) == {} + perms.origins_path(tmp_path).write_text("{{{", encoding="utf-8") + assert perms.load_origins(tmp_path) == {} + + +def test_a_failed_origin_write_is_not_an_error( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + store = PhoneStore(tmp_path) + + def boom(*_args: object, **_kwargs: object) -> None: + raise OSError("disk full") + + monkeypatch.setattr(perms, "save_origins", boom) + store.record_origin("probe", "dev_1", "Pixel 8") + assert store.origin_of("probe") is None + + +def test_the_notice_names_the_command_and_never_a_value() -> None: + added = server_saved_summary("probe", ["/usr/bin/touch", "/tmp/a b"], "", existed=False) + assert added == "added MCP server 'probe': /usr/bin/touch '/tmp/a b'" + changed = server_saved_summary("web", [], "https://mcp.example.test/x", existed=True) + assert changed == "changed MCP server 'web': https://mcp.example.test/x" + long = server_saved_summary("big", ["x" * 500], "", existed=False) + assert len(long) < 240 and long.endswith("…") + assert value_summary("probe", "env", "TOKEN", stored=True) == ( + "stored a value for variable 'TOKEN' of MCP server 'probe'." + ) + assert value_summary("web", "header", "Authorization", stored=False) == ( + "removed the value of header 'Authorization' of MCP server 'web'." + ) diff --git a/core/tests/test_remote_devices.py b/core/tests/test_remote_devices.py index b2771ce8..8cd9c21b 100644 --- a/core/tests/test_remote_devices.py +++ b/core/tests/test_remote_devices.py @@ -371,7 +371,15 @@ def test_summaries_carry_the_connected_flag_and_never_a_hash(tmp_path: Path) -> rows = {s.id: s.model_dump() for s in reg.summaries(connected={a.id})} assert rows[a.id]["connected"] is True and rows[b.id]["connected"] is False for row in rows.values(): - assert set(row) == {"id", "name", "paired_at", "last_seen", "connected", "kind"} + assert set(row) == { + "id", + "name", + "paired_at", + "last_seen", + "connected", + "kind", + "mcp_servers_added", + } @pytest.mark.parametrize( diff --git a/core/tstd/daemon.py b/core/tstd/daemon.py index 4f835a9e..8461733e 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -211,6 +211,8 @@ from .notify.slack import send as send_slack_notify from .notify.telegram import schedule as schedule_telegram_notify from .peer_policy import outbound_event, service_hosts +from .phone_permissions import MCP_SERVERS, utc_now +from .phone_store import PhoneStore, server_saved_summary, value_summary from .plan_review import answer as answer_plan from .plan_review import keep_pinned_plan_visible, restore_pinned_plan, restored_plan_mode from .policy import ( @@ -365,6 +367,7 @@ OpenWorkspace, PairingCompleted, ParseJob, + PhoneChange, PolicyRules, PresetReadiness, PresetRoles, @@ -414,6 +417,7 @@ SetLoadGlobalMemory, SetMcpSecret, SetMcpServer, + SetPhonePermission, SetPlan, SetPreset, SetPresetLabel, @@ -1267,8 +1271,16 @@ def __init__( ) # TD-5015: paired computers. Only the bridge's links dial, only to # paired Tailscale hosts (dial_policy is the tests' seam). + # TD-5279: what a paired phone may change. Off unless the person at + # this computer switched it on; the hub asks on every message. + self.phone_store = PhoneStore(self.data_dir) + self._phone_expiry_task: asyncio.Task[None] | None = None self.hub = Hub( - self.data_dir, self.ws_server, self._handle_list_sessions, policy=dial_policy + self.data_dir, + self.ws_server, + self._handle_list_sessions, + policy=dial_policy, + phone_grants=self.phone_store.live, ) self._cu_server: Any = None # TD-4835: keychain presence cache — see _credential_is_stored. @@ -1562,7 +1574,11 @@ async def _setup_state_event(self) -> SetupState: remote_attach_enabled=self.remote_attach_enabled, remote_bind=self.ws_server.extra_host, remote_port=self.ws_server.port if self.ws_server.extra_host else None, - remote_devices=self.remote_devices.summaries(self.ws_server.connected_device_ids()), + remote_devices=[ + device.model_copy(update={"mcp_servers_added": self._servers_added_by(device.id)}) + for device in self.remote_devices.summaries(self.ws_server.connected_device_ids()) + ], + phone_permissions=self.phone_store.states(), credentials=credentials, tier_credentials={name: tiers[name].credential for name in tiers}, tier_loopback={name: is_loopback_url(tiers[name].base_url) for name in tiers}, @@ -1599,8 +1615,11 @@ async def _setup_state_event(self) -> SetupState: ], env=await value_summaries(sid, spec.env, self._mcp_secret_is_stored), headers=await value_summaries(sid, spec.headers, self._mcp_secret_is_stored), + origin_device=origin.device_name if origin else "", + origin_at=origin.at if origin else "", ) for sid, spec in sorted(self.config.mcp.servers.items()) + for origin in (self.phone_store.origin_of(sid),) ], speech_enabled=self.config.speech.enabled, speech_ready=self.config.speech.enabled and bool(self.config.speech.base_url), @@ -2468,6 +2487,11 @@ async def _diagnostics_report(self) -> DiagnosticsReport: ) ) + # TD-5279: a phone permission left on is worth seeing outside Settings. + phone_row = self.phone_store.doctor_row() + if phone_row is not None: + checks.append(phone_row) + return DiagnosticsReport(seq=1, checks=checks) # ── Usage and cost (TD-1706) ─────────────────────────────────────── @@ -2677,6 +2701,7 @@ async def _serve(self) -> None: if await asyncio.to_thread(cu_stopfile.stop_engaged): self.set_computer_use_killed(True, write_file=False) self._tasks.append(asyncio.create_task(self._cu_stop_watch(), name="cu-stop-watch")) + self._arm_phone_permission_expiry() await self._shutdown_event.wait() async def _shutdown(self) -> None: @@ -4106,6 +4131,9 @@ async def _not_asked(_urls: list[str]) -> dict[str, bool]: if isinstance(msg, SetRemoteAttach): return await self._handle_set_remote_attach(msg) + + if isinstance(msg, SetPhonePermission): + return await self._handle_set_phone_permission(msg) if isinstance(msg, RotateRemoteToken): return await self._handle_rotate_remote_token() if isinstance(msg, StartPairing | CancelPairing | RemoveRemoteDevice): @@ -4679,6 +4707,11 @@ async def _not_asked(_urls: list[str]) -> dict[str, bool]: return build_error("bad_request", f"Saved, but the config no longer loads: {e}") await self._mcp.reload(self.config.mcp) log.info("mcp server saved", extra={"extra_fields": {"server_id": msg.id}}) + await self._note_phone_change( + MCP_SERVERS, + server_saved_summary(msg.id, msg.command, msg.url, existed=prior is not None), + server_id=msg.id, + ) return (await self._setup_state_event()).model_dump_json() if isinstance(msg, DeleteMcpServer): @@ -4696,6 +4729,9 @@ async def _not_asked(_urls: list[str]) -> dict[str, bool]: return build_error("bad_request", f"Saved, but the config no longer loads: {e}") await self._mcp.reload(self.config.mcp) log.info("mcp server removed", extra={"extra_fields": {"server_id": msg.id}}) + # Whoever removed it, there is nothing left to attribute. + self.phone_store.forget_origin(msg.id) + await self._note_phone_change(MCP_SERVERS, f"removed MCP server '{msg.id}'.") return (await self._setup_state_event()).model_dump_json() if isinstance(msg, SetMcpSecret | DeleteMcpSecret): @@ -5780,6 +5816,10 @@ async def _handle_pairing_verb( if isinstance(msg, StartPairing): return await self._handle_start_pairing() if isinstance(msg, RemoveRemoteDevice): + if msg.remove_added: + refused = await self._remove_servers_added_by(msg.device_id) + if refused is not None: + return refused await self._remove_remote_device(msg.device_id, connection) return None self.pairing.cancel() @@ -5806,6 +5846,137 @@ async def _remove_remote_device(self, device_id: str, connection: Any) -> None: await connection.send(scrub_wire_json(self.hub.finish(reply, connection))) await self.ws_server.close_device(device_id) + # ── phone permissions (TD-5279) ─────────────────────────────────── + + async def _handle_set_phone_permission(self, msg: SetPhonePermission) -> str: + """Switch what a paired phone may change. The peer policy let only loopback in.""" + try: + self.phone_store.set(msg.capability, msg.enabled, msg.expires_in_seconds) + except ValueError as e: + return build_error("bad_request", str(e), in_reply_to=msg.type) + except OSError as e: + return build_error( + "phone_permission_failed", + f"Couldn't save the phone permissions file: {e.strerror or e}", + in_reply_to=msg.type, + ) + log.info( + "phone permission set", + extra={ + "extra_fields": { + "capability": msg.capability, + "enabled": msg.enabled, + "expires_in_seconds": msg.expires_in_seconds, + } + }, + ) + self._arm_phone_permission_expiry() + # A phone's pages follow the switch, and so do this computer's other windows. + await self._tell_other_windows_the_catalog() + return (await self._setup_state_event()).model_dump_json() + + def _arm_phone_permission_expiry(self) -> None: + """Tell every window when the soonest timed grant runs out. + + The policy reads the clock on every message, so a grant stops + working at its expiry with or without this; this only keeps what + the windows show true. + """ + if self._phone_expiry_task is not None: + self._phone_expiry_task.cancel() + self._phone_expiry_task = None + until = self.phone_store.next_expiry() + if until is None: + return + + async def _wait() -> None: + # A task copies the context it was made in: without this the + # window that granted would be left out as "already answered". + _ANSWERING.set(None) + await asyncio.sleep(max(0.0, (until - utc_now()).total_seconds()) + 0.5) + self._phone_expiry_task = None + log.info("phone permission expired") + await self._tell_other_windows_the_catalog() + self._arm_phone_permission_expiry() + + self._phone_expiry_task = asyncio.create_task(_wait(), name="phone-permission-expiry") + self._tasks.append(self._phone_expiry_task) + + def _servers_added_by(self, device_id: str) -> list[str]: + """The MCP servers a device added or changed that are still listed.""" + return [ + sid + for sid in self.phone_store.servers_from(device_id) + if sid in self.config.mcp.servers + ] + + def _phone_actor(self) -> tuple[str, str] | None: + """``(device id, name)`` when the message being answered came from a paired phone.""" + connection: Any = _ANSWERING.get() + if connection is None or not self.ws_server.peer_view(connection).phone: + return None + device_id = self.ws_server.device_of(connection) + if device_id is None: + return None + for device in self.remote_devices.summaries(): + if device.id == device_id: + return device_id, device.name + return device_id, "A paired phone" + + async def _note_phone_change( + self, capability: str, summary: str, *, server_id: str | None = None + ) -> None: + """A phone used a grant: record where it came from and tell the computer. + + Nothing happens for a change made at the computer. The audit + database keys every record to a session and this belongs to none, + so the trail is the daemon log, with the device named. + """ + actor = self._phone_actor() + if actor is None: + return + device_id, device_name = actor + if server_id is not None: + self.phone_store.record_origin(server_id, device_id, device_name) + log.warning( + "phone used a phone permission", + extra={ + "extra_fields": { + "capability": capability, + "device_id": device_id, + "device_name": device_name, + "summary": summary, + } + }, + ) + event = PhoneChange(device_name=device_name, capability=capability, summary=summary) + await self.ws_server.broadcast(scrub_wire_json(event.model_dump_json()), loopback_only=True) + # The computer's open Settings page shows the change without a reopen. + await self._tell_other_windows_the_catalog() + + async def _remove_servers_added_by(self, device_id: str) -> str | None: + """The unpair review's "also remove these". An error frame when a removal fails.""" + for server_id in self._servers_added_by(device_id): + removed = self.config.mcp.servers.get(server_id) + try: + delete_mcp_server_entry(server_id, self.config_path) + except ConfigError as e: + return build_error("bad_request", str(e), in_reply_to="remove_remote_device") + if removed is not None: + await forget_server_secrets(server_id, removed) + self._invalidate_stored_probe() + self.phone_store.forget_origin(server_id) + log.info( + "mcp server removed with its phone", + extra={"extra_fields": {"server_id": server_id, "device_id": device_id}}, + ) + try: + self._reload_user_config() + except ConfigError as e: + return build_error("bad_request", f"Saved, but the config no longer loads: {e}") + await self._mcp.reload(self.config.mcp) + return None + async def _handle_start_pairing(self) -> str: """Mint a code; turn remote attach on first, as the toggle would. @@ -6619,6 +6790,11 @@ async def _handle_mcp_secret(self, msg: SetMcpSecret | DeleteMcpSecret) -> str: } }, ) + await self._note_phone_change( + MCP_SERVERS, + value_summary(msg.server_id, msg.target, msg.name, stored=verb == "store"), + server_id=msg.server_id, + ) return (await self._setup_state_event()).model_dump_json() async def _handle_set_email_notify(self, msg: SetEmailNotify) -> str: diff --git a/core/tstd/hub.py b/core/tstd/hub.py index ffa72935..43f93247 100644 --- a/core/tstd/hub.py +++ b/core/tstd/hub.py @@ -21,7 +21,7 @@ import contextlib import json import re -from collections.abc import Awaitable, Callable +from collections.abc import Awaitable, Callable, Collection from pathlib import Path from typing import Any @@ -88,8 +88,12 @@ def __init__( *, policy: DialPolicy | None = None, timings: Timings = DEFAULT_TIMINGS, + phone_grants: Callable[[], Collection[str]] | None = None, ) -> None: self._ws = ws_server + # TD-5279: the phone permissions switched on right now. Without a + # source there are none, so every opt-in verb is refused. + self._phone_grants = phone_grants self._own_session_list = own_session_list self.registry = MachineRegistry(data_dir) self.registry.load() @@ -127,7 +131,8 @@ async def intercept(self, msg: ClientMessage, connection: Any) -> object: peer = self._ws.peer_view(connection) sid = message_session_id(msg) bridged = sid is not None and is_bridged(sid) - refusal = check(msg, peer, bridged=bridged) + grants = self._phone_grants() if self._phone_grants is not None else () + refusal = check(msg, peer, bridged=bridged, grants=grants) if refusal is not None: return refusal if msg.type in MACHINE_VERBS: diff --git a/core/tstd/peer_policy.py b/core/tstd/peer_policy.py index 27225f5a..fc321765 100644 --- a/core/tstd/peer_policy.py +++ b/core/tstd/peer_policy.py @@ -27,6 +27,7 @@ from typing import Literal from urllib.parse import urlsplit +from .phone_permissions import LABELS, MCP_SERVERS from .protocol import ClientMessage, DaemonEvent, ModelFallback, TierState, build_error DeviceKind = Literal["phone", "machine"] @@ -144,19 +145,12 @@ # ``delete_preset`` (TD-5237) removes a model from the catalog. Copying or # relabelling one from a phone is as narrow as ``add_model``; taking one # away is done at the computer whose config it is. -# The MCP page (TD-5276). A stdio server is a command this computer runs, -# now and at every session start, and it never meets the classifier. A -# paired phone could add one with no word from the person at the computer, -# and it stayed in config.yaml after the phone was removed. So the page is -# the computer's own, as the commands in the config editor already were: a -# phone still sees the list in ``setup_state``. A server's values go with -# it: they are that command's environment, or the headers sent to its host. +# ``set_phone_permission`` (TD-5279) decides what a phone may change, so a +# phone never sends it: it could otherwise grant itself. It is the one +# phone permission with no setting. LOOPBACK_VERBS: frozenset[str] = frozenset( { - "set_mcp_server", - "delete_mcp_server", - "set_mcp_secret", - "delete_mcp_secret", + "set_phone_permission", "delete_preset", "drain", "trust_workspace_config", @@ -178,6 +172,9 @@ class PeerView: machine: bool viewer: bool history: bool = False + # A paired phone: remote, with a device record of ``kind: phone``. Not + # the legacy remote token, which is no device, and never a machine. + phone: bool = False @property def loopback(self) -> bool: @@ -197,7 +194,29 @@ def classify(remote: bool, device_kind: DeviceKind | None, features: Iterable[st machine = device_kind == "machine" or BRIDGE_FEATURE in wanted viewer = not machine and VIEWER_FEATURE in wanted and (not remote or device_kind == "phone") history = HISTORY_FEATURE in wanted - return PeerView(remote=remote, machine=machine, viewer=viewer, history=history) + phone = remote and not machine and device_kind == "phone" + return PeerView(remote=remote, machine=machine, viewer=viewer, history=history, phone=phone) + + +# What a phone may send only after the computer switched the capability on +# (TD-5279), verb to capability. The MCP page: a stdio server is a command +# this computer runs, now and at every session start, and it never meets +# the classifier. A paired phone could add one with no word from the person +# at the computer, and it stayed in config.yaml after the phone was +# removed. A server's values go with it: they are that command's +# environment, or the headers sent to its host. A phone still sees the list +# in ``setup_state`` either way. +PHONE_OPT_IN: dict[str, str] = { + "set_mcp_server": MCP_SERVERS, + "delete_mcp_server": MCP_SERVERS, + "set_mcp_secret": MCP_SERVERS, + "delete_mcp_secret": MCP_SERVERS, +} + + +def required_capability(msg: ClientMessage) -> str | None: + """The phone permission *msg* needs from a remote peer, if any.""" + return PHONE_OPT_IN.get(msg.type) # Loopback names say nothing about where a service lives. @@ -304,17 +323,33 @@ def message_session_id(msg: ClientMessage) -> str | None: return value if isinstance(value, str) else None -def check(msg: ClientMessage, peer: PeerView, *, bridged: bool) -> str | None: +def check( + msg: ClientMessage, peer: PeerView, *, bridged: bool, grants: Collection[str] = () +) -> str | None: """A ready error frame when *peer* may not send *msg*, else None. *bridged* says the message's ``session_id`` is in the bridged grammar. - Forwarding itself is the daemon's call; this only refuses. + Forwarding itself is the daemon's call; this only refuses. *grants* are + the phone permissions switched on right now (TD-5279); a caller that + passes none refuses every opt-in verb from a phone. """ sid = message_session_id(msg) if msg.type in LOOPBACK_VERBS and (peer.remote or peer.machine): return build_error( "loopback_only", "Only the computer itself can do that.", in_reply_to=msg.type ) + # TD-5279: only a paired phone can use a grant. The legacy remote token + # is no device, so nothing switches these on for it; another computer + # is refused below as it always was. + capability = required_capability(msg) + wants_grant = capability is not None and peer.remote and not peer.machine + if wants_grant and not (peer.phone and capability in grants): + return build_error( + "phone_permission_off", + f"Turn on '{LABELS[capability or '']}' under Settings, Phones and devices " + "on the computer to do this from a phone.", + in_reply_to=msg.type, + ) # TD-5118: a turn's own allow rules switch approvals off for it, so they # come only from ``tst run`` on this computer, for its own sessions; a # phone or another computer still approves call by call. diff --git a/core/tstd/phone_permissions.py b/core/tstd/phone_permissions.py new file mode 100644 index 00000000..aca3c461 --- /dev/null +++ b/core/tstd/phone_permissions.py @@ -0,0 +1,233 @@ +"""What a paired phone may change, as granted at the computer (TD-5279). + +Some things a phone could ask for reach further than the session it is +looking at: an MCP server is a command this computer runs, at once and at +every session start, and it never meets the classifier. Those verbs are +refused from a phone unless the person at the computer switched the +matching capability on, after reading what it lets a phone do. + +The grants live in ``/phone-permissions.yaml``, beside +``remote-attach.yaml`` and for the same reason: it is this computer's own +state. It is not a ``config.yaml`` key and no workspace file is read for +it, so a repository can never switch a grant on. + +Everything here fails closed. An absent, unreadable or malformed file, an +unknown capability, an entry that is not exactly ``enabled: true``, an +expiry that does not parse and an expiry in the past all read as off. + +Also here: ``mcp-origins.yaml``, which remembers the phone a server was +added or changed from. Removing a phone does not undo what it changed, so +the record is what lets the computer show it and offer to remove it. +""" + +from __future__ import annotations + +import contextlib +import os +import tempfile +from collections.abc import Mapping +from dataclasses import dataclass +from datetime import UTC, datetime, timedelta +from pathlib import Path +from typing import Any, Final + +import yaml + +# The closed set. A capability is added here together with its verbs in +# ``peer_policy.PHONE_OPT_IN`` and its own disclosure in Settings, Phones and devices. +MCP_SERVERS: Final = "mcp_servers" +CAPABILITIES: Final[tuple[str, ...]] = (MCP_SERVERS,) + +# What the refusal and the Doctor row call each one. +LABELS: Final[Mapping[str, str]] = {MCP_SERVERS: "MCP servers"} + +# A timed grant is for "while I am out", not a second way to say forever. +MAX_EXPIRY_SECONDS: Final = 7 * 24 * 60 * 60 + +_FILE_VERSION: Final = 1 +_TIME_FORMAT: Final = "%Y-%m-%dT%H:%M:%SZ" + + +def utc_now() -> datetime: + """Now, in UTC. The one clock this module reads; tests replace it.""" + return datetime.now(UTC) + + +def stamp(moment: datetime) -> str: + return moment.astimezone(UTC).strftime(_TIME_FORMAT) + + +def parse_stamp(value: object) -> datetime | None: + if not isinstance(value, str): + return None + try: + return datetime.strptime(value, _TIME_FORMAT).replace(tzinfo=UTC) + except ValueError: + return None + + +@dataclass(frozen=True) +class Grant: + """One capability switched on. Off is the absence of a grant.""" + + granted_at: str + expires_at: str | None = None + + def live(self, now: datetime) -> bool: + if self.expires_at is None: + return True + until = parse_stamp(self.expires_at) + return until is not None and now < until + + +Grants = dict[str, Grant] + + +def permissions_path(data_dir: str | Path) -> Path: + return Path(data_dir) / "phone-permissions.yaml" + + +def _read_mapping(path: Path) -> dict[str, Any]: + try: + data: Any = yaml.safe_load(path.read_text(encoding="utf-8")) + except (OSError, UnicodeDecodeError, yaml.YAMLError): + return {} + return data if isinstance(data, dict) else {} + + +def _write_mapping(path: Path, fields: dict[str, Any]) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=f".{path.stem}.", suffix=".tmp") + try: + with os.fdopen(fd, "w", encoding="utf-8") as f: + yaml.safe_dump(fields, f, sort_keys=False, allow_unicode=True) + os.replace(tmp, path) + except BaseException: + with contextlib.suppress(OSError): + os.unlink(tmp) + raise + + +def load_grants(data_dir: str | Path) -> Grants: + """The grants on file. Anything that is not plainly a grant is left out.""" + data = _read_mapping(permissions_path(data_dir)) + if data.get("version") != _FILE_VERSION: + return {} + raw = data.get("grants") + if not isinstance(raw, dict): + return {} + grants: Grants = {} + for name in CAPABILITIES: + entry = raw.get(name) + if not isinstance(entry, dict) or entry.get("enabled") is not True: + continue + expires = entry.get("expires_at") + if expires is not None and parse_stamp(expires) is None: + continue + granted = entry.get("granted_at") + grants[name] = Grant( + granted_at=granted if isinstance(granted, str) else "", + expires_at=expires, + ) + return grants + + +def save_grants(data_dir: str | Path, grants: Grants) -> None: + """Write *grants* atomically. Every capability is listed, on or off.""" + body: dict[str, Any] = {} + for name in CAPABILITIES: + grant = grants.get(name) + if grant is None: + body[name] = {"enabled": False} + else: + body[name] = { + "enabled": True, + "granted_at": grant.granted_at, + "expires_at": grant.expires_at, + } + _write_mapping(permissions_path(data_dir), {"version": _FILE_VERSION, "grants": body}) + + +def changed( + grants: Grants, + capability: str, + enabled: bool, + expires_in_seconds: int | None, + now: datetime, +) -> Grants: + """*grants* with one capability switched. Raises ValueError on a bad ask.""" + if capability not in CAPABILITIES: + raise ValueError(f"Unknown phone permission: {capability}") + after = dict(grants) + if not enabled: + after.pop(capability, None) + return after + expires_at: str | None = None + if expires_in_seconds is not None: + if not 0 < expires_in_seconds <= MAX_EXPIRY_SECONDS: + raise ValueError("A timed phone permission lasts at most seven days.") + expires_at = stamp(now + timedelta(seconds=expires_in_seconds)) + after[capability] = Grant(granted_at=stamp(now), expires_at=expires_at) + return after + + +def live(grants: Grants, now: datetime) -> frozenset[str]: + """The capabilities a phone may use right now.""" + return frozenset(name for name, grant in grants.items() if grant.live(now)) + + +def next_expiry(grants: Grants, now: datetime) -> datetime | None: + """When the soonest still-live timed grant runs out.""" + ends = [ + until + for grant in grants.values() + if grant.live(now) and (until := parse_stamp(grant.expires_at)) is not None + ] + return min(ends) if ends else None + + +# ── where a server came from ────────────────────────────────────────── + + +@dataclass(frozen=True) +class Origin: + """The phone an MCP server was last added or changed from.""" + + device_id: str + device_name: str + at: str + + +Origins = dict[str, Origin] + + +def origins_path(data_dir: str | Path) -> Path: + return Path(data_dir) / "mcp-origins.yaml" + + +def load_origins(data_dir: str | Path) -> Origins: + data = _read_mapping(origins_path(data_dir)) + raw = data.get("servers") if data.get("version") == _FILE_VERSION else None + if not isinstance(raw, dict): + return {} + found: Origins = {} + for server_id, entry in raw.items(): + if not isinstance(server_id, str) or not isinstance(entry, dict): + continue + device_id, name, at = entry.get("device_id"), entry.get("device_name"), entry.get("at") + if isinstance(device_id, str) and isinstance(name, str) and isinstance(at, str): + found[server_id] = Origin(device_id=device_id, device_name=name, at=at) + return found + + +def save_origins(data_dir: str | Path, origins: Origins) -> None: + servers = { + server_id: {"device_id": o.device_id, "device_name": o.device_name, "at": o.at} + for server_id, o in sorted(origins.items()) + } + _write_mapping(origins_path(data_dir), {"version": _FILE_VERSION, "servers": servers}) + + +def servers_from(origins: Origins, device_id: str) -> list[str]: + """The servers one device added or changed, by id.""" + return sorted(sid for sid, origin in origins.items() if origin.device_id == device_id) diff --git a/core/tstd/phone_store.py b/core/tstd/phone_store.py new file mode 100644 index 00000000..9f65cf25 --- /dev/null +++ b/core/tstd/phone_store.py @@ -0,0 +1,130 @@ +"""The daemon's phone permissions, held in memory and on disk (TD-5279). + +One :class:`PhoneStore` per daemon. ``phone_permissions`` is the file +format and the rules; this is the daemon's copy of both files plus the +shapes it hands to ``setup_state``, Doctor and the peer policy. + +A write that fails leaves memory as it was: a grant that could not be +saved is not in force, and one that could not be cleared is still shown +as on, so what the person sees is what the next start will read. +""" + +from __future__ import annotations + +import shlex +from collections.abc import Sequence +from datetime import datetime +from pathlib import Path + +from . import phone_permissions as perms +from .logging import get_logger +from .protocol import DiagnosticCheck, PhonePermissionState + +log = get_logger("tstd.phone_store") + +# What the notice quotes of a command or address: enough to recognise it. +_QUOTE_LIMIT = 200 + + +def _clip(text: str) -> str: + return text if len(text) <= _QUOTE_LIMIT else text[: _QUOTE_LIMIT - 1] + "…" + + +def server_saved_summary(server_id: str, command: Sequence[str], url: str, *, existed: bool) -> str: + """What a phone did to a server, without the phone's name.""" + what = shlex.join(command) if command else url + verb = "changed" if existed else "added" + return f"{verb} MCP server '{server_id}': {_clip(what)}" + + +def value_summary(server_id: str, target: str, name: str, *, stored: bool) -> str: + """A value stored or removed. The name only; the value is never here.""" + kind = "header" if target == "header" else "variable" + verb = "stored a value for" if stored else "removed the value of" + return f"{verb} {kind} '{name}' of MCP server '{server_id}'." + + +class PhoneStore: + def __init__(self, data_dir: str | Path) -> None: + self._data_dir = Path(data_dir) + self._grants = perms.load_grants(self._data_dir) + self._origins = perms.load_origins(self._data_dir) + + # ── grants ──────────────────────────────────────────────────────── + + def live(self) -> frozenset[str]: + """The capabilities a phone may use right now.""" + return perms.live(self._grants, perms.utc_now()) + + def set(self, capability: str, enabled: bool, expires_in_seconds: int | None) -> None: + """Switch one capability. ValueError on a bad ask, OSError on a failed write.""" + after = perms.changed( + self._grants, capability, enabled, expires_in_seconds, perms.utc_now() + ) + perms.save_grants(self._data_dir, after) + self._grants = after + + def states(self) -> dict[str, PhonePermissionState]: + """Every capability, on or off, as ``setup_state`` shows it.""" + now = perms.utc_now() + out: dict[str, PhonePermissionState] = {} + for name in perms.CAPABILITIES: + grant = self._grants.get(name) + if grant is not None and grant.live(now): + out[name] = PhonePermissionState(enabled=True, expires_at=grant.expires_at) + else: + out[name] = PhonePermissionState() + return out + + def next_expiry(self) -> datetime | None: + return perms.next_expiry(self._grants, perms.utc_now()) + + def doctor_row(self) -> DiagnosticCheck | None: + """A warning while any phone permission is on; no row when all are off.""" + now = perms.utc_now() + on: list[str] = [] + for name in perms.CAPABILITIES: + grant = self._grants.get(name) + if grant is None or not grant.live(now): + continue + until = f"until {grant.expires_at}" if grant.expires_at else "until turned off" + on.append(f"{perms.LABELS[name]} ON ({until})") + if not on: + return None + return DiagnosticCheck( + name="phone_permissions", + status="warn", + detail="paired phones may change: " + "; ".join(on), + ) + + # ── where a server came from ────────────────────────────────────── + + def origin_of(self, server_id: str) -> perms.Origin | None: + return self._origins.get(server_id) + + def servers_from(self, device_id: str) -> list[str]: + return perms.servers_from(self._origins, device_id) + + def record_origin(self, server_id: str, device_id: str, device_name: str) -> None: + after = dict(self._origins) + after[server_id] = perms.Origin( + device_id=device_id, device_name=device_name, at=perms.stamp(perms.utc_now()) + ) + self._save_origins(after) + + def forget_origin(self, server_id: str) -> None: + if server_id not in self._origins: + return + after = dict(self._origins) + del after[server_id] + self._save_origins(after) + + def _save_origins(self, after: perms.Origins) -> None: + # The server is already saved by now; a record that cannot be + # written must not turn that into an error. It is logged instead. + try: + perms.save_origins(self._data_dir, after) + except OSError: + log.exception("could not write the MCP origins file") + return + self._origins = after diff --git a/core/tstd/protocol.py b/core/tstd/protocol.py index ae8bdd80..0cd33c25 100644 --- a/core/tstd/protocol.py +++ b/core/tstd/protocol.py @@ -698,6 +698,28 @@ class RemoveRemoteDevice(ClientMessage): type: Literal["remove_remote_device"] = "remove_remote_device" device_id: str = Field(min_length=1) + #: Also remove the MCP servers this device added or changed (TD-5279). + #: Honoured from the computer only; a phone forgetting itself leaves them. + remove_added: bool = False + + +class SetPhonePermission(ClientMessage): + """Switch one phone permission on or off (TD-5279). Loopback only. + + Machine-wide, no session. A phone permission lets a paired phone send + verbs that are otherwise refused from one with ``phone_permission_off`` + (``mcp_servers``: add, change and remove MCP servers and their values). + ``expires_in_seconds`` makes the grant run out on its own; absent, it + lasts until switched off. Persists + ``{user_data_dir}/phone-permissions.yaml``; acked with ``setup_state``. + An unknown capability or an expiry over seven days is ``bad_request``. + No phone, legacy-token client or other computer may send it. + """ + + type: Literal["set_phone_permission"] = "set_phone_permission" + capability: str = Field(min_length=1) + enabled: bool + expires_in_seconds: int | None = Field(default=None, ge=1) class AddMachine(ClientMessage): @@ -3732,6 +3754,10 @@ class McpServerSummary(BaseModel): #: Names only (TD-5110). Additive; an older client ignores them. env: list[McpValueSummary] = Field(default_factory=list) headers: list[McpValueSummary] = Field(default_factory=list) + #: The phone this server was last added or changed from, and when + #: (TD-5279). Empty for one only the computer has touched. + origin_device: str = "" + origin_at: str = "" class RemoteDeviceSummary(BaseModel): @@ -3749,6 +3775,20 @@ class RemoteDeviceSummary(BaseModel): # TD-5015: "machine" is another computer's hub. A file entry written # before the field reads as a phone. kind: Literal["phone", "machine"] = "phone" + #: MCP servers this device added or changed that are still listed + #: (TD-5279): what removing it would leave behind. + mcp_servers_added: list[str] = Field(default_factory=list) + + +class PhonePermissionState(BaseModel): + """One phone permission as ``setup_state`` shows it (TD-5279). + + ``enabled`` is whether a phone may use it right now: a grant whose + ``expires_at`` has passed reads as off. + """ + + enabled: bool = False + expires_at: str | None = None class EmailNotifyState(BaseModel): @@ -3921,6 +3961,10 @@ class SetupState(DaemonEvent): # TD-4403: listed MCP servers. Additive, default empty. Never a secret; # env and headers are names only (TD-5110). An older client ignores it. mcp_servers: list[McpServerSummary] = Field(default_factory=list) + # TD-5279: what a paired phone may change, by capability. Every known + # capability is listed, on or off. A phone reads it too, so its pages + # can say why a control is unavailable. Additive, default empty. + phone_permissions: dict[str, PhonePermissionState] = Field(default_factory=dict) # TD-4701: hold-to-talk. Additive, default off. ``speech_ready`` is # enabled plus a configured base_url — the URL itself never leaves # the daemon. An older client ignores both fields. @@ -3966,6 +4010,21 @@ class PairingCompleted(DaemonEvent): device: RemoteDeviceSummary +class PhoneChange(DaemonEvent): + """A paired phone used a phone permission (TD-5279). + + Broadcast to loopback clients only, each time: the person at the + computer is told what a phone changed and which phone. ``summary`` is + one sentence without the device's name. Connection-scoped, seq 1. + """ + + type: Literal["phone_change"] = "phone_change" + seq: int = 1 + device_name: str + capability: str + summary: str + + MachineStatus = Literal["connected", "connecting", "unreachable", "refused", "locked", "outdated"] @@ -4282,10 +4341,12 @@ class DiagnosticCheck(BaseModel): ``skip`` means the check did not apply (no key to validate, no workspace open) — it is not a failure and should not alarm. ``fix`` is the concrete remedy; present exactly when status is ``fail``. + ``warn`` (TD-5279) is a setting that works as chosen and is worth + knowing about, such as a phone permission left on. """ name: str - status: Literal["ok", "fail", "skip"] + status: Literal["ok", "fail", "skip", "warn"] detail: str fix: str | None = None @@ -5093,6 +5154,7 @@ class GoogleStatus(DaemonEvent): | StartPairing | CancelPairing | RemoveRemoteDevice + | SetPhonePermission | AddMachine | RemoveMachine | ListMachines @@ -5182,6 +5244,7 @@ class GoogleStatus(DaemonEvent): | SetupState | PairingCode | PairingCompleted + | PhoneChange | MachineList | DrainResult | ApiKeyValidated @@ -5356,6 +5419,7 @@ class GoogleStatus(DaemonEvent): "start_pairing", "cancel_pairing", "remove_remote_device", + "set_phone_permission", "add_machine", "remove_machine", "list_machines", @@ -5445,6 +5509,7 @@ class GoogleStatus(DaemonEvent): "setup_state", "pairing_code", "pairing_completed", + "phone_change", "machine_list", "drain_result", "api_key_validated", diff --git a/docs/architecture.md b/docs/architecture.md index 7fabe8d7..cfd8be84 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -488,6 +488,17 @@ exactly what it saw before. | Machine peer | A device whose record says `kind: machine`, or any hello with `"bridge"` (the hub always sends it) | Deny by default: only `MACHINE_PEER_VERBS`, own sessions only, no broadcasts. A bridged id is `bridged_session`; any other verb is `not_allowed`. Never a viewer, so it never forwards. | | Everyone else | The CLI, the legacy `remote-token`, an old client | Own rows only, no `machine_list`: unchanged. | +A paired phone (a remote peer whose device record says `kind: phone`) may send anything +not listed against it, with two lists against it. `LOOPBACK_VERBS` are the computer's own +and have no setting. `PHONE_OPT_IN` (TD-5279) maps a verb to a *phone permission*: the verb +is refused with `phone_permission_off` unless the person at the computer switched that +capability on with `set_phone_permission`, which is itself in `LOOPBACK_VERBS` so a phone +can never grant itself. Today the one capability is `mcp_servers` (the four MCP server +verbs). A grant applies to paired phones only, never to the legacy `remote-token` and +never to a machine peer; it may carry an expiry; and it lives in +`{user_data_dir}/phone-permissions.yaml`, which no config file sets. `Hub.intercept` passes +the live grants to `peer_policy.check`, whose default is none. + `MACHINE_PEER_VERBS` is the session verbs a hub needs (list, attach, detach, send, stop, the approval answers, verify, new and fork, branch, tier, plan, effort, Fast and Ultracode (`set_effort`, `set_fast`, `set_ultracode`), preset, rename, star, archive, @@ -668,7 +679,7 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `set_credential` | — | Create or rename a named API key without touching the secret (TD-1717). Optional `base_url` sets or clears the host that key talks to (TD-1722). | | `delete_credential` | — | Remove a named key, its secret, and tier bindings (TD-1717). | | `set_tier_credential` | — | Bind a named API key to one tier of one preset (TD-1717). That key's `base_url` is the host the tier calls when set (TD-1718). | -| `set_mcp_server` | — | Create or replace one listed MCP server (id, transport, command argv and/or url, enabled). No `env` or `headers` field; the server's existing ones are kept, unless the transport changes, which drops them and deletes their keychain items (TD-5110). A hand-set `allow_remote` is kept the same way, and dropped with a transport change (TD-5136). A url on another host or port drops the headers (deleting their items) and `allow_remote` too (TD-5161). Acked with `setup_state` (TD-4403). This computer's own window only, with the three verbs below (TD-5276): a stdio server is a command the computer runs from then on, so a paired phone or another computer is refused with `loopback_only`; a phone still sees the list in `setup_state`. | +| `set_mcp_server` | — | Create or replace one listed MCP server (id, transport, command argv and/or url, enabled). No `env` or `headers` field; the server's existing ones are kept, unless the transport changes, which drops them and deletes their keychain items (TD-5110). A hand-set `allow_remote` is kept the same way, and dropped with a transport change (TD-5136). A url on another host or port drops the headers (deleting their items) and `allow_remote` too (TD-5161). Acked with `setup_state` (TD-4403). A stdio server is a command the computer runs from then on, so this and the three verbs below are a phone permission (TD-5279): a paired phone is refused with `phone_permission_off` unless the computer switched `mcp_servers` on with `set_phone_permission`, the legacy `remote-token` is always refused that way, and another computer gets `not_allowed`. A phone still sees the list in `setup_state`. When a phone does change a server, the computer's windows get `phone_change` and the server's `origin_device` / `origin_at` say which phone and when. | | `delete_mcp_server` | — | Remove one listed MCP server by id, and delete the keychain items its `env` / `headers` named (TD-5110). Unknown id is a typed error. Acked with `setup_state` (TD-4403). | | `set_mcp_secret` | — | Store one env var (stdio) or header (http) of a listed MCP server (`server_id`, `target`, `name`, `value`) in the keychain item `tst-mcp..`; `config.yaml` gets only `keychain:`. The value never appears in any event, log or audit record. Acked with `setup_state` (TD-5110). | | `delete_mcp_secret` | — | Remove one env var or header from a listed MCP server, and the keychain item it named. Acked with `setup_state` (TD-5110). | @@ -688,7 +699,8 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `rotate_remote_token` | — | Replace `remote-token` and drop the remote clients that used it. Paired devices keep their own keys. Machine-wide; a no-op while remote attach is off. The token never crosses the wire. Acked with `setup_state` (TD-5005). | | `start_pairing` | — | Show a one-time pairing code for a new phone. Turns remote attach on first when it is off. Loopback only. Acked with `pairing_code`, or the error `pairing_unavailable` when no Tailscale address can be bound (TD-5014). | | `cancel_pairing` | — | Drop the active pairing code. Loopback only. Acked with `setup_state` (TD-5014). | -| `remove_remote_device` | — | Forget one paired device and close only its connections. Loopback, or a phone removing its own entry; any other id from a remote peer gets `loopback_only`. An unknown id changes nothing; a file that cannot be written gets `remove_failed`. Acked with `setup_state` (TD-5014). A hub removing itself from another computer uses the same self-removal (TD-5015). | +| `remove_remote_device` | — | Forget one paired device and close only its connections. Loopback, or a phone removing its own entry; any other id from a remote peer gets `loopback_only`. An unknown id changes nothing; a file that cannot be written gets `remove_failed`. Acked with `setup_state` (TD-5014). A hub removing itself from another computer uses the same self-removal (TD-5015). With `remove_added: true` from the computer, the MCP servers that device added or changed (its `mcp_servers_added`) are removed first; from a phone the flag does nothing (TD-5279). | +| `set_phone_permission` | — | Switch one phone permission on or off (`capability`, `enabled`, optional `expires_in_seconds` up to seven days). `mcp_servers` lets a paired phone send the four MCP server verbs. Persists `{user_data_dir}/phone-permissions.yaml`, which no config file or workspace can set; an absent or unreadable file means everything off. Loopback only: a phone, the legacy `remote-token` or another computer gets `loopback_only`, so a phone can never grant itself. An unknown capability or a longer expiry is `bad_request`; a file that cannot be written is `phone_permission_failed`. Acked with `setup_state`, which every other window and paired phone is sent too (TD-5279). | | `add_machine` | — | Pair another computer so its sessions show here: a `tstdesk://pair` `link`, or `host`, `port` and `code`. The host must be a Tailscale address or a `*.ts.net` name. Loopback only. Runs as a task; the answer is a pushed `machine_list`, or `pair_failed`, `pair_locked`, `machine_unreachable`, `not_tailscale`, `machine_limit`, `machine_busy` or `keychain_locked`. The code is never logged or echoed (TD-5015). | | `remove_machine` | — | Forget a paired computer: first, best effort, remove this hub from that computer's paired devices, then drop its key and its `computers.yaml` entry. Loopback only. Acked with `machine_list`; an unknown id is `machine_not_found` (TD-5015). | | `list_machines` | — | The paired computers. Loopback or a viewer. Acked with `machine_list` (TD-5015). | @@ -771,9 +783,10 @@ are stamped by a session's event log, `connection` events fix it at 1, and `ping | `machine_list` | connection | The paired computers: id, name, host, port, `status` (`connected`, `connecting`, `unreachable`, `refused`, `locked`, `outdated`), last seen, paired at, `same_computer` for a self-pair, and a `detail` line. Never a key. The reply to the four machine verbs, and pushed to viewers when one changes; never sent to a machine peer (TD-5015). | | `drain_result` | connection | Reply to `drain`, to that connection only: `drained` (no turn open at all), the session ids still `running`, `paused` for the person (the progress guard or a cap, TD-5155) or in `awaiting_approval` (ids only), and `hold_until` (null once released) (TD-5023). Only `running` turns can finish by themselves; `tst drain` exits 0 when none is left unless `--strict`. | | `policy_rules` | connection | The workspace's saved policy rules. An `auto` rule with no wildcard carries `exact`, the one text its `args` matches (`app/[[]id]/page.tsx` → `app/[id]/page.tsx`; for `shell`, the command segment once wrappers such as `timeout 60` are stripped). `exact` is null for a glob, and for every `ask` and `never` rule, which also match in another case (TD-5149, TD-5158). | -| `setup_state` | connection | Onboarding state, and the ack for `set_api_key` / `set_preset` / `set_engine` / `set_tier_slug` / `refresh_model_cards` / `set_skip_all_approvals` / `set_load_global_memory` / `set_coworker` / `set_voice` / `set_cu_indicators` / `set_workspace_pin` / `set_remote_attach` / `rotate_remote_token` / `cancel_pairing` / `remove_remote_device` / `set_mcp_server` / `delete_mcp_server` / `set_mcp_secret` / `delete_mcp_secret` / `set_judgments` / `set_email_notify`. `remote_bind` is the bound Tailscale address, never a token. `remote_devices` lists paired devices (id, name, paired_at, last_seen, connected), never a key or its hash (TD-5014). `tier_cards` is the active preset's stored prices and limits per tier with the last card refresh's outcome (TD-5185). Carries `engine`, `grok_available`, `grok_binary`, `voice_enabled`, and `voice_has_endpoint`. `mcp_servers` is the listed MCP servers (id, transport, command, url, enabled, and `env` / `headers` as name, source, entry and stored presence, TD-5110) — never a secret or a literal value. `speech_enabled` / `speech_ready` are hold-to-talk flags (TD-4701); the speech URL never appears. `judgments_*` echoes the judgment-seam toggles (TD-708 dev). A remote peer (a phone or another computer) gets it without `remote_devices`, `remote_bind` and `remote_port` (TD-5015). `email` is the mail form (enabled, method, host, port, security, username, from_address, password_stored) for TD-3820, TD-4853, and TD-4854; the password never appears. `github` is the connector snapshot (enabled, connected, phase, user code, verification URL, login, message) for TD-4852; tokens and the device code never appear. `microsoft` is the sign-in snapshot (enabled, connected, phase, account, message) for TD-4853; tokens and the authorization code never appear. `google` is the sign-in snapshot (enabled, connected, phase, account, message, client_secret_stored) for TD-4854; the secret, the tokens, and the authorization code never appear. | +| `setup_state` | connection | Onboarding state, and the ack for `set_api_key` / `set_preset` / `set_engine` / `set_tier_slug` / `refresh_model_cards` / `set_skip_all_approvals` / `set_load_global_memory` / `set_coworker` / `set_voice` / `set_cu_indicators` / `set_workspace_pin` / `set_remote_attach` / `rotate_remote_token` / `cancel_pairing` / `remove_remote_device` / `set_mcp_server` / `delete_mcp_server` / `set_mcp_secret` / `delete_mcp_secret` / `set_phone_permission` / `set_judgments` / `set_email_notify`. `remote_bind` is the bound Tailscale address, never a token. `remote_devices` lists paired devices (id, name, paired_at, last_seen, connected), never a key or its hash (TD-5014). `tier_cards` is the active preset's stored prices and limits per tier with the last card refresh's outcome (TD-5185). Carries `engine`, `grok_available`, `grok_binary`, `voice_enabled`, and `voice_has_endpoint`. `mcp_servers` is the listed MCP servers (id, transport, command, url, enabled, and `env` / `headers` as name, source, entry and stored presence, TD-5110) — never a secret or a literal value. `speech_enabled` / `speech_ready` are hold-to-talk flags (TD-4701); the speech URL never appears. `judgments_*` echoes the judgment-seam toggles (TD-708 dev). A remote peer (a phone or another computer) gets it without `remote_devices`, `remote_bind` and `remote_port` (TD-5015). `phone_permissions` lists every phone permission as `{enabled, expires_at}`, with an expired grant reading as off; a phone gets it too, so its pages can say why a control is unavailable (TD-5279). `email` is the mail form (enabled, method, host, port, security, username, from_address, password_stored) for TD-3820, TD-4853, and TD-4854; the password never appears. `github` is the connector snapshot (enabled, connected, phase, user code, verification URL, login, message) for TD-4852; tokens and the device code never appear. `microsoft` is the sign-in snapshot (enabled, connected, phase, account, message) for TD-4853; tokens and the authorization code never appear. `google` is the sign-in snapshot (enabled, connected, phase, account, message, client_secret_stored) for TD-4854; the secret, the tokens, and the authorization code never appear. | | `pairing_code` | connection | Reply to `start_pairing`: the code (and `ABCD-2345` display form), host, port, desktop name, the `tstdesk://pair` link, a QR of it as standalone SVG, and when it expires (TD-5014). | | `pairing_completed` | connection | A phone redeemed the code. Carries that device's summary (never its key). Sent to the computer's own loopback windows only (TD-5014). | +| `phone_change` | connection | A paired phone used a phone permission: `device_name`, `capability` and a one-sentence `summary` (for an MCP server the command or address; for a value its name, never the value). Sent to the computer's own loopback windows only, each time (TD-5279). | | `api_key_validated` | connection | The result of a key probe. `credential` echoes the request's, so Settings → API keys shows the verdict on that key's row; null when the request named none (TD-5200). Sent within 20 s (`KEY_TEST_LIMIT_S`): a server that has not answered by then is reported as no answer. Replies come in request order, and the window ties each verdict to its request by that order, which also places an older daemon's unnamed verdict. Never carries the key. | | `endpoint_models` | connection | A server's model list for "Add a model", or why it could not be read. Never carries the key. | | `local_servers` | connection | The servers on this computer that answered `scan_local_servers` with models, in config order, and every URL asked. | diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 93c9f928..e442aa2d 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13856,6 +13856,11 @@ been missed. saying where to make changes, with nothing to press (`mcp-read-only.test.ts`). +**Superseded in part by TD-5279:** the owner chose to keep the phone's +ability as an opt-in. The four verbs are now refused from a phone with +`phone_permission_off` unless the computer switched "MCP servers" on for +phones; the default (refused) is what this story shipped. + **Notes:** With the verb reopened, the socket test's command ran on the test machine: the fault was real, not theoretical. Other verbs a phone may send that deserve a decision (turning approvals off, `shutdown`, changing @@ -14118,3 +14123,98 @@ this machine it cannot. Turning that off in bubblewrap itself (`--disable-userns`) needs a version check and belongs with the sandbox backend interface (TD-5264). +--- + +### TD-5279 — Phone permissions: a paired phone changes MCP servers only if the computer said so + +**Size:** 5 (approach signed off by the owner) · **Depends on:** TD-5276, TD-5014, TD-5015 · **Status:** Done (stage 1 of 3) + +TD-5276 closed the hole by making the MCP page the computer's own. The +owner's decision was to keep the phone's ability, but as something the +person at the computer switches on after reading what it allows. This is +the framework for that, with the MCP servers as its first capability. +Every other sensitive thing a phone can send today moves onto the same +framework one at a time, each default off with its own disclosure; the +list is in the notes. + +- [x] `/phone-permissions.yaml` holds the grants. Absent, + unreadable, malformed, another version, an unknown capability, an + entry that is not exactly `enabled: true`, an unreadable expiry and + a past expiry all read as off (`test_phone_permissions.py`). +- [x] It is no `config.yaml` key: one written into the user config or a + workspace's `.tst/config.yaml` does nothing + (`test_workspace_config_cannot_grant`). +- [x] `set_phone_permission` is computer-only. A paired phone, the older + remote token and another computer get `loopback_only`, whatever + grants are on, so a phone can never grant itself + (`test_set_phone_permission_is_loopback_only`, + `test_mcp_peer.py::test_a_phone_cannot_grant_itself`). +- [x] With no grant, a paired phone's `set_mcp_server`, + `delete_mcp_server`, `set_mcp_secret` and `delete_mcp_secret` are + refused with `phone_permission_off`; nothing is saved, nothing is + started and the command it carried does not run + (`test_a_phone_cannot_add_an_mcp_server_by_default`, + `test_a_phone_cannot_change_remove_or_set_a_value_by_default`). +- [x] `peer_policy.check` takes the live grants as an argument that + defaults to none, so a caller that forgets it refuses + (`test_check_refuses_when_grants_are_not_passed`). +- [x] A grant is for a paired phone only: the older remote token is still + refused with it on, and another computer still gets `not_allowed` + (`test_a_grant_is_for_paired_phones_only`). +- [x] After the computer grants, a phone can add a server; each use sends + `phone_change` to the computer's windows naming the phone and the + command (for a value, its name and never the value), and the + computer's open Settings page gets the new list + (`test_a_phone_can_add_one_after_the_computer_grants`). +- [x] Where a server came from is recorded in `/mcp-origins.yaml` + (not `config.yaml`) and shown on its row as "Added or changed from + Pixel 8 on 20 Nov". +- [x] A grant can be timed ("Turn on for 1 hour", up to seven days over + the wire). The policy reads the clock on every message, and every + window and phone is sent the new state when it runs out + (`test_a_timed_grant_runs_out`, + `test_the_windows_are_told_when_a_timed_grant_runs_out`). +- [x] Turning a grant off stops new changes and removes nothing + (`test_turning_the_grant_off_keeps_existing_servers`). +- [x] Removing a phone lists the MCP servers it added or changed, and + "Also remove these servers" takes them with it; the flag from a + phone removing itself does nothing + (`test_removing_a_device_reports_and_can_remove_what_it_added`, + `test_removing_a_device_with_what_it_added`, + `test_a_phone_forgetting_itself_cannot_remove_servers`). +- [x] Doctor shows a warning row while any phone permission is on + (`test_doctor_warns_while_a_phone_permission_is_on`); `warn` is a new + row status. +- [x] Settings → Phones and devices, on the computer, has "What paired + phones may change". Turning one on opens its disclosure with Keep + off, Turn on for 1 hour and Turn on until I turn it off; Keep off and + Escape send nothing. The disclosure's words are pinned whole + (`phone-permissions.test.ts`). +- [x] On a phone with the permission off, Settings → MCP servers is the + list and the sentence saying where to turn it on; with it on, the + page works as on the computer. + +**Not built in this stage:** + +- The one-time notice after upgrading with a phone already paired + ("Paired phones can no longer add or change MCP servers unless you allow + it"). It needs a remembered "shown" flag and a place in the window. +- An audit-database record. Every audit record is keyed to a session and + this belongs to none, so the trail is the daemon log line (with the + device's id and name) and the origins file. +- Letting a phone switch an existing server *off* without the permission + (it only reduces what runs). It needs a comparison in the handler. +- The classify-every-verb inventory: a new verb is still open to a phone + unless it is listed. That is stage 2, and it should land before any new + MCP import verb. + +**Notes:** the refusal and the phone's page say "Settings, Phones and +devices", which is what the section is called; the design note said +"Settings, Devices". The remaining capabilities, each its own story: +turning approvals off for every session; adding a model server and +managing keys; turning computer use on or to full control; starting an +unattended run; the connector sign-ins; and, with extra-strong disclosure +because they can send a stored key or password to a host the phone names, +changing a saved key's host and changing the mail server. `shutdown` and +releasing the computer-use stop are opt-in too by the owner's decision. +Only `set_phone_permission` has no setting. Class B entry in DECISIONS.md. diff --git a/ui/e2e/app-walk.spec.ts b/ui/e2e/app-walk.spec.ts index 9cb1e0c8..6450cac7 100644 --- a/ui/e2e/app-walk.spec.ts +++ b/ui/e2e/app-walk.spec.ts @@ -676,6 +676,11 @@ const CATALOG_ROW = /^(Duplicate|Rename|Remove) .+$/; // so the walk leaves it alone; grok-on-picker.test.ts presses it. const GROK_ADD = /^Add Grok to your models$/; +// A phone permission (TD-5279) is switched on through its disclosure. The +// walk must not open one and then press whatever button comes next: that +// could be "Turn on". phone-permissions.test.ts presses each choice. +const PHONE_PERMISSION = /^Turn (on|off): .+$/; + const SKIP_CLICK = /^(Close|Sign in with Microsoft|Sign in with Google|Connect GitHub|Disconnect|Send test email|Delete|Remove|Forget|Copy code|Open browser|Try again|How models work)$/; @@ -688,7 +693,13 @@ async function exercise(page: Page, content: Locator): Promise { const name = ((await button.getAttribute("aria-label")) || (await button.innerText())) .replace(/\s+/g, " ") .trim(); - if (name === "" || SKIP_CLICK.test(name) || CATALOG_ROW.test(name) || GROK_ADD.test(name)) + if ( + name === "" || + SKIP_CLICK.test(name) || + CATALOG_ROW.test(name) || + GROK_ADD.test(name) || + PHONE_PERMISSION.test(name) + ) continue; await dismissExtraDialogs(page); await button.click({ timeout: 5_000 }); diff --git a/ui/e2e/census.json b/ui/e2e/census.json index 8d0a7b48..adce883e 100644 --- a/ui/e2e/census.json +++ b/ui/e2e/census.json @@ -243,6 +243,7 @@ "settings:Phones and devices": [ "Older way: token", "Pair A Phone", + "Turn on: Let paired phones add and change MCP servers", "toggle" ], "settings:Policy": [ diff --git a/ui/src/lib/client.ts b/ui/src/lib/client.ts index 4c9550f8..47acae2f 100644 --- a/ui/src/lib/client.ts +++ b/ui/src/lib/client.ts @@ -101,6 +101,7 @@ const KNOWN_EVENT_TYPES = new Set([ "setup_state", // TD-1101 first-run wizard "pairing_code", // TD-5014 "pairing_completed", // TD-5014 + "phone_change", // TD-5279 "machine_list", // TD-5015 "api_key_validated", // TD-1101 "endpoint_models", // TD-5172 diff --git a/ui/src/lib/components/DoctorPane.svelte b/ui/src/lib/components/DoctorPane.svelte index 8d14b325..dfb3ba84 100644 --- a/ui/src/lib/components/DoctorPane.svelte +++ b/ui/src/lib/components/DoctorPane.svelte @@ -9,10 +9,11 @@ /** Row status → glyph. The copied text report keeps its own ASCII marks (STATUS_MARK in the store); these are the pane's visual analog. */ - const STATUS_ICON: Record = { + const STATUS_ICON: Record = { ok: 'check', fail: 'x', skip: 'minus', + warn: 'alert', }; let copied = $state(false); @@ -170,6 +171,10 @@ color: var(--color-ink-secondary); } + .mark--warn { + color: var(--color-warn); + } + .row-body { display: flex; flex-direction: column; diff --git a/ui/src/lib/components/PairedDevices.svelte b/ui/src/lib/components/PairedDevices.svelte index 3bdeb04b..f170be83 100644 --- a/ui/src/lib/components/PairedDevices.svelte +++ b/ui/src/lib/components/PairedDevices.svelte @@ -13,6 +13,9 @@ let { canManage }: { canManage: boolean } = $props(); let confirming = $state(null); + // TD-5279: removing a phone does not undo what it changed. The review + // lists the MCP servers it added and offers to take them with it. + let alsoRemove = $state(false); let now = $state(Date.now()); // The daemon pushes presence from here on; this is the first read. @@ -30,8 +33,17 @@ return `Last seen ${relativeWords(device.last_seen, now)}`; } + function added(device: RemoteDeviceSummary): string[] { + return device.mcp_servers_added ?? []; + } + + function ask(device: RemoteDeviceSummary): void { + alsoRemove = false; + confirming = device.id; + } + function remove(device: RemoteDeviceSummary): void { - removeRemoteDevice(device.id); + removeRemoteDevice(device.id, alsoRemove && added(device).length > 0); confirming = null; } @@ -61,6 +73,16 @@ {#if confirming === device.id} It will need to pair again. + {#if added(device).length > 0} + + This phone added or changed MCP servers that stay after it is removed: + {added(device).join(', ')}. + + + {/if} @@ -71,7 +93,7 @@ class="choice" type="button" aria-label={`Remove ${device.name}`} - onclick={() => (confirming = device.id)}>Remove ask(device)}>Remove {/if} {/if} @@ -142,10 +164,22 @@ .confirm { display: inline-flex; + flex-wrap: wrap; align-items: center; + justify-content: flex-end; gap: var(--space-2); } + .review, + .also { + flex: 1 1 100%; + text-align: right; + } + + .review { + color: var(--color-warn); + } + .confirm-text { font-size: var(--text-xs); color: var(--color-ink-secondary); diff --git a/ui/src/lib/components/PhonePermissions.svelte b/ui/src/lib/components/PhonePermissions.svelte new file mode 100644 index 00000000..e06645e8 --- /dev/null +++ b/ui/src/lib/components/PhonePermissions.svelte @@ -0,0 +1,254 @@ + + +
+

What paired phones may change

+

+ Each of these is off until you turn it on here. A phone cannot turn one on. +

+
    + {#each PHONE_PERMISSIONS as permission (permission.capability)} + {@const on = isGranted(settings.phonePermissions, permission.capability, now)} +
  • + + {permission.title} + {stateWords(settings.phonePermissions, permission.capability, now)} + + {#if on} + + {:else} + + {/if} +
  • + {/each} +
+
+ +{#if asking !== null} + +{/if} + + diff --git a/ui/src/lib/components/SettingsMcp.svelte b/ui/src/lib/components/SettingsMcp.svelte index bb80019c..279ebe28 100644 --- a/ui/src/lib/components/SettingsMcp.svelte +++ b/ui/src/lib/components/SettingsMcp.svelte @@ -12,11 +12,14 @@ type McpServerRow, type McpValueRow, } from "../settings.svelte.js"; + import { originWords, permissionCopy } from "../phone-permissions"; - // TD-5276: a server is a command the computer runs, so only the computer - // adds, changes or removes one. Anywhere else (a paired phone, a browser) - // the page is the list, and the daemon refuses the rest. + // A server is a command the computer runs, so off the computer the page + // is the list and nothing else, and the daemon refuses the rest, unless + // the computer switched "MCP servers" on for phones (TD-5279). The + // caller decides; told nothing, this is the computer's own page. let { canManage = true }: { canManage?: boolean } = $props(); + const offNote = permissionCopy("mcp_servers")?.offOnPhone ?? ""; let draftId = $state(""); let draftTransport = $state<"stdio" | "http">("stdio"); @@ -110,6 +113,9 @@ · {server.url || "(no url)"} {/if}

+ {#if server.originDevice} +

{originWords(server.originDevice, server.originAt)}

+ {/if} {#each server.excludedTools ?? [] as tool (tool.name)}

Left out {tool.name}: {tool.reason}

{/each} @@ -237,7 +243,7 @@
{:else} -

Add or change MCP servers in Settings on the computer.

+

{offNote}

{/if} diff --git a/ui/src/lib/mcp-import.svelte.ts b/ui/src/lib/mcp-import.svelte.ts new file mode 100644 index 00000000..2451c355 --- /dev/null +++ b/ui/src/lib/mcp-import.svelte.ts @@ -0,0 +1,145 @@ +// Paste a server's JSON, see what importing would do, then import (TD-5253). +// +// The daemon reads the text; this only holds it, shows the preview and +// collects the choices. The text can hold a token, so it is kept in memory +// only and cleared the moment an import succeeds. The preview never holds +// a value: the daemon does not send one. + +import { onEvent, sendToDaemon } from "./connection-status.svelte.js"; +import type { DaemonEventUnion, McpImportChoice, McpImportServer } from "./protocol"; + +export interface ImportRow { + server: McpImportServer; + /** Ticked to import. A refused server can never be ticked. */ + chosen: boolean; + id: string; + allowRemote: boolean; + /** Values the preview asked for, by name. */ + inputs: Record; +} + +export const mcpImport = $state({ + text: "", + /** The text the preview was made from; a later edit makes the preview stale. */ + previewedText: null as string | null, + rows: [] as ImportRow[], + error: null as string | null, + busy: false, + done: null as string | null, +}); + +const ID_RE = /^[a-z][a-z0-9-]{0,31}$/; +let started = false; +let importing = false; + +export function startMcpImport(): () => void { + if (started) return () => {}; + started = true; + const off = onEvent(reduce); + return () => { + started = false; + off(); + }; +} + +export function resetMcpImport(): void { + mcpImport.text = ""; + mcpImport.previewedText = null; + mcpImport.rows = []; + mcpImport.error = null; + mcpImport.busy = false; + mcpImport.done = null; + importing = false; +} + +/** Why this row cannot be imported as it stands, or "" when it can. */ +export function rowProblem(row: ImportRow, taken: readonly string[]): string { + if (row.server.refused) return `Cannot be imported: ${row.server.refused}.`; + if (!ID_RE.test(row.id)) return "The id must be lowercase letters, digits and -, starting with a letter."; + if (taken.includes(row.id)) return "A server with this id is already listed. Give it another id."; + if (row.server.off_box && !row.allowRemote) return "Tick the box to allow connecting to this server."; + const missing = row.server.values.find( + (value) => value.disposition === "needs_input" && !(row.inputs[value.name] ?? "").trim(), + ); + if (missing) return `Type a value for ${missing.name}.`; + return ""; +} + +/** Ids this import may not use: the ones listed already, and the other ticked rows'. */ +export function takenIds(row: ImportRow, rows: readonly ImportRow[], listed: readonly string[]): string[] { + return [...listed, ...rows.filter((other) => other !== row && other.chosen).map((other) => other.id)]; +} + +export function canImport(rows: readonly ImportRow[], listed: readonly string[], stale: boolean): boolean { + const chosen = rows.filter((row) => row.chosen); + if (stale || chosen.length === 0) return false; + return chosen.every((row) => rowProblem(row, takenIds(row, rows, listed)) === ""); +} + +export function isStale(): boolean { + return mcpImport.previewedText !== null && mcpImport.previewedText !== mcpImport.text; +} + +export function previewImport(): void { + const text = mcpImport.text; + if (text.trim() === "") return; + mcpImport.error = null; + mcpImport.done = null; + mcpImport.busy = true; + importing = false; + mcpImport.previewedText = text; + if (!sendToDaemon({ type: "preview_mcp_import", text })) { + mcpImport.busy = false; + mcpImport.error = "Not connected."; + } +} + +export function runImport(listed: readonly string[]): void { + if (mcpImport.previewedText === null || !canImport(mcpImport.rows, listed, isStale())) return; + const servers: McpImportChoice[] = mcpImport.rows + .filter((row) => row.chosen) + .map((row) => ({ + source_name: row.server.source_name, + id: row.id, + ...(row.allowRemote ? { allow_remote: true } : {}), + ...(Object.keys(row.inputs).length > 0 ? { inputs: { ...row.inputs } } : {}), + })); + mcpImport.error = null; + mcpImport.busy = true; + importing = true; + if (!sendToDaemon({ type: "import_mcp_servers", text: mcpImport.previewedText, servers })) { + mcpImport.busy = false; + importing = false; + mcpImport.error = "Not connected."; + } +} + +function reduce(event: DaemonEventUnion): void { + if (event.type === "mcp_import_preview") { + mcpImport.busy = false; + mcpImport.error = event.error || null; + mcpImport.rows = event.servers.map((server) => ({ + server, + chosen: !server.refused, + id: server.id, + allowRemote: false, + inputs: {}, + })); + return; + } + if (event.type === "setup_state" && importing) { + // The ack. The text may hold a token: drop it, and the typed values. + const count = mcpImport.rows.filter((row) => row.chosen).length; + resetMcpImport(); + mcpImport.done = count === 1 ? "Imported 1 server." : `Imported ${count} servers.`; + return; + } + if ( + event.type === "error" && + (event.in_reply_to === "import_mcp_servers" || event.in_reply_to === "preview_mcp_import") + ) { + mcpImport.busy = false; + importing = false; + mcpImport.error = event.message; + } +} diff --git a/ui/src/lib/mcp-import.test.ts b/ui/src/lib/mcp-import.test.ts new file mode 100644 index 00000000..579a9662 --- /dev/null +++ b/ui/src/lib/mcp-import.test.ts @@ -0,0 +1,220 @@ +// @vitest-environment jsdom +// +// TD-5253: paste a server's JSON, see what would run, then import. The +// daemon reads the text and decides; these pin what the window sends, when +// Import is allowed, and that the pasted text is dropped once it is used. + +import { afterEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, unmount } from "svelte"; +import type { ClientMessageUnion, DaemonEventUnion, McpImportServer } from "./protocol"; + +const mocks = vi.hoisted(() => ({ + sent: [] as ClientMessageUnion[], + listeners: new Set<(event: DaemonEventUnion) => void>(), +})); + +vi.mock("./connection-status.svelte.js", () => ({ + onEvent: (fn: (event: DaemonEventUnion) => void) => { + mocks.listeners.add(fn); + return () => mocks.listeners.delete(fn); + }, + sendToDaemon: (msg: ClientMessageUnion) => (mocks.sent.push(msg), true), +})); + +import SettingsMcpImport from "./components/SettingsMcpImport.svelte"; +import { settings, type McpServerRow } from "./settings.svelte.js"; +import { + canImport, + mcpImport, + resetMcpImport, + rowProblem, + type ImportRow, +} from "./mcp-import.svelte.js"; + +let component: ReturnType | null = null; + +function emit(event: unknown): void { + for (const fn of [...mocks.listeners]) fn(event as DaemonEventUnion); + flushSync(); +} + +function server(over: Partial = {}): McpImportServer { + return { + source_name: "GitHub", + id: "github", + transport: "stdio", + command: ["npx", "-y", "server-github"], + url: "", + off_box: false, + exists: false, + values: [{ name: "GITHUB_TOKEN", target: "env", disposition: "keychain" }], + not_imported: [], + notes: [], + refused: "", + ...over, + }; +} + +function row(over: Partial = {}, more: Partial = {}): ImportRow { + const s = server(over); + return { server: s, chosen: true, id: s.id, allowRemote: false, inputs: {}, ...more }; +} + +function show(): HTMLElement { + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(SettingsMcpImport, { target }); + flushSync(); + return target; +} + +function press(root: ParentNode, label: string): void { + const button = [...root.querySelectorAll("button")].find((b) => b.textContent?.trim() === label); + if (!button) throw new Error(`no button "${label}"`); + button.click(); + flushSync(); +} + +function type(root: ParentNode, label: string, value: string): void { + const field = root.querySelector(`[aria-label="${label}"]`); + if (!field) throw new Error(`no field "${label}"`); + field.value = value; + field.dispatchEvent(new Event("input", { bubbles: true })); + flushSync(); +} + +afterEach(() => { + if (component) unmount(component); + component = null; + document.body.innerHTML = ""; + mocks.sent = []; + settings.mcpServers = []; + resetMcpImport(); +}); + +const TEXT = '{"mcpServers": {"GitHub": {"command": "npx"}}}'; + +describe("when Import is allowed", () => { + it("needs a valid id that is not taken", () => { + expect(rowProblem(row(), [])).toBe(""); + expect(rowProblem(row({}, { id: "Not A Slug" }), [])).toContain("lowercase"); + expect(rowProblem(row(), ["github"])).toContain("already listed"); + }); + + it("needs the tick for a server on another machine", () => { + const remote = { transport: "http" as const, url: "https://mcp.example.test/x", off_box: true }; + expect(rowProblem(row(remote), [])).toContain("Tick the box"); + expect(rowProblem(row(remote, { allowRemote: true }), [])).toBe(""); + }); + + it("needs every asked-for value typed", () => { + const asks = { + values: [{ name: "API_KEY", target: "env" as const, disposition: "needs_input" as const }], + }; + expect(rowProblem(row(asks), [])).toBe("Type a value for API_KEY."); + expect(rowProblem(row(asks, { inputs: { API_KEY: " " } }), [])).toBe("Type a value for API_KEY."); + expect(rowProblem(row(asks, { inputs: { API_KEY: "typed-value" } }), [])).toBe(""); + }); + + it("never for a refused server, a stale preview, or nothing ticked", () => { + expect(rowProblem(row({ refused: "it uses SSE" }), [])).toContain("Cannot be imported"); + expect(canImport([row()], [], false)).toBe(true); + expect(canImport([row()], [], true)).toBe(false); + expect(canImport([row({}, { chosen: false })], [], false)).toBe(false); + // One bad ticked row holds back the lot; an unticked bad row does not. + expect(canImport([row(), row({ source_name: "B", id: "github" })], [], false)).toBe(false); + expect(canImport([row(), row({ refused: "no" }, { chosen: false })], [], false)).toBe(true); + }); +}); + +describe("Settings → MCP servers, Paste JSON", () => { + it("previews, shows what would run, and sends nothing else until Import", () => { + const root = show(); + press(root, "Paste JSON"); + type(root, "MCP server JSON", TEXT); + press(root, "Preview"); + expect(mocks.sent).toEqual([{ type: "preview_mcp_import", text: TEXT }]); + emit({ type: "mcp_import_preview", seq: 1, servers: [server()], error: "" }); + expect(root.textContent).toContain("npx -y server-github"); + expect((root.textContent ?? "").replace(/\s+/g, " ")).toContain( + "GITHUB_TOKEN · goes to the keychain", + ); + expect(mocks.sent).toHaveLength(1); + press(root, "Import"); + expect(mocks.sent[1]).toEqual({ + type: "import_mcp_servers", + text: TEXT, + servers: [{ source_name: "GitHub", id: "github" }], + }); + }); + + it("drops the pasted text and the typed values once the import is acknowledged", () => { + const root = show(); + press(root, "Paste JSON"); + type(root, "MCP server JSON", TEXT); + press(root, "Preview"); + emit({ + type: "mcp_import_preview", + seq: 1, + error: "", + servers: [ + server({ values: [{ name: "API_KEY", target: "env", disposition: "needs_input", reason: "Type it." }] }), + ], + }); + const importButton = [...root.querySelectorAll("button")].find((b) => b.textContent?.trim() === "Import"); + expect(importButton?.disabled).toBe(true); + type(root, "Value for API_KEY", "typed-value-0001"); + press(root, "Import"); + expect(mocks.sent[1]).toMatchObject({ servers: [{ inputs: { API_KEY: "typed-value-0001" } }] }); + emit({ type: "setup_state", seq: 1, has_api_key: false, active_preset: "" }); + expect(mcpImport.text).toBe(""); + expect(mcpImport.rows).toEqual([]); + expect(mcpImport.previewedText).toBeNull(); + expect(root.textContent).toContain("Imported 1 server."); + expect(root.innerHTML).not.toContain("typed-value-0001"); + }); + + it("a setup_state that is not the ack leaves the text alone", () => { + const root = show(); + press(root, "Paste JSON"); + type(root, "MCP server JSON", TEXT); + emit({ type: "setup_state", seq: 1, has_api_key: false, active_preset: "" }); + expect(mcpImport.text).toBe(TEXT); + }); + + it("says why a server cannot be imported, and it cannot be ticked", () => { + const root = show(); + press(root, "Paste JSON"); + type(root, "MCP server JSON", TEXT); + press(root, "Preview"); + emit({ + type: "mcp_import_preview", + seq: 1, + error: "", + servers: [server({ refused: "it needs an OAuth sign-in" })], + }); + expect(root.textContent).toContain("Cannot be imported: it needs an OAuth sign-in."); + expect(root.querySelector('[aria-label="Import GitHub"]')?.disabled).toBe(true); + const importButton = [...root.querySelectorAll("button")].find((b) => b.textContent?.trim() === "Import"); + expect(importButton?.disabled).toBe(true); + }); + + it("makes a changed text be previewed again, and shows the daemon's reason on a refusal", () => { + settings.mcpServers = [{ id: "github" } as McpServerRow]; + const root = show(); + press(root, "Paste JSON"); + type(root, "MCP server JSON", TEXT); + press(root, "Preview"); + emit({ type: "mcp_import_preview", seq: 1, servers: [server()], error: "" }); + expect(root.textContent).toContain("already listed"); + type(root, "Id for GitHub", "github-2"); + type(root, "MCP server JSON", TEXT + " "); + expect(root.textContent).toContain("Preview it again"); + const importButton = [...root.querySelectorAll("button")].find((b) => b.textContent?.trim() === "Import"); + expect(importButton?.disabled).toBe(true); + emit({ type: "mcp_import_preview", seq: 1, servers: [], error: "That is not JSON (line 1, column 2)." }); + expect(root.textContent).toContain("That is not JSON (line 1, column 2)."); + emit({ type: "error", code: "bad_request", message: "'GitHub': type a value for X.", in_reply_to: "import_mcp_servers" }); + expect(root.textContent).toContain("'GitHub': type a value for X."); + }); +}); diff --git a/ui/src/lib/protocol-fixtures.json b/ui/src/lib/protocol-fixtures.json index 7196aaa4..751a8634 100644 --- a/ui/src/lib/protocol-fixtures.json +++ b/ui/src/lib/protocol-fixtures.json @@ -815,6 +815,22 @@ "device_id": "dev_0123456789ab", "remove_added": false }, + "preview_mcp_import": { + "type": "preview_mcp_import", + "text": "{\"mcpServers\": {\"files\": {\"command\": \"uvx\"}}}" + }, + "import_mcp_servers": { + "type": "import_mcp_servers", + "text": "{\"mcpServers\": {\"files\": {\"command\": \"uvx\"}}}", + "servers": [ + { + "source_name": "files", + "id": "files", + "allow_remote": false, + "inputs": {} + } + ] + }, "set_phone_permission": { "type": "set_phone_permission", "capability": "mcp_servers", @@ -2374,6 +2390,37 @@ "mcp_servers_added": [] } }, + "mcp_import_preview": { + "type": "mcp_import_preview", + "seq": 1, + "servers": [ + { + "source_name": "GitHub", + "id": "github", + "transport": "stdio", + "command": [ + "npx", + "-y", + "server-github" + ], + "url": "", + "off_box": false, + "exists": false, + "values": [ + { + "name": "GITHUB_TOKEN", + "target": "env", + "disposition": "keychain", + "reason": "" + } + ], + "not_imported": [], + "notes": [], + "refused": "" + } + ], + "error": "" + }, "phone_change": { "type": "phone_change", "seq": 1, diff --git a/ui/src/lib/protocol.ts b/ui/src/lib/protocol.ts index 5d84e282..a7b5dd7e 100644 --- a/ui/src/lib/protocol.ts +++ b/ui/src/lib/protocol.ts @@ -326,6 +326,27 @@ export interface RemoveRemoteDevice extends ClientMessage { remove_added?: boolean; } +/** Read MCP servers out of pasted JSON (TD-5253). Saves nothing. */ +export interface PreviewMcpImport extends ClientMessage { + type: "preview_mcp_import"; + text: string; +} + +export interface McpImportChoice { + source_name: string; + id: string; + allow_remote?: boolean; + /** Values the preview asked for, by name. They go to the keychain. */ + inputs?: Record; +} + +/** Save the chosen servers from the same pasted JSON (TD-5253). */ +export interface ImportMcpServers extends ClientMessage { + type: "import_mcp_servers"; + text: string; + servers: McpImportChoice[]; +} + /** Switch what a paired phone may change (TD-5279). Loopback only; a phone * can never send it. No `expires_in_seconds` means until switched off. */ export interface SetPhonePermission extends ClientMessage { @@ -1478,6 +1499,8 @@ export type ClientMessageUnion = | CancelPairing | RemoveRemoteDevice | SetPhonePermission + | PreviewMcpImport + | ImportMcpServers | AddMachine | RemoveMachine | ListMachines @@ -2551,6 +2574,36 @@ export interface PairingCompleted extends DaemonEvent { device: RemoteDeviceSummary; } +/** One env var or header in an import preview (TD-5253). Never its value. */ +export interface McpImportValue { + name: string; + target: "env" | "header"; + disposition: "keychain" | "literal" | "needs_input"; + reason?: string; +} + +export interface McpImportServer { + source_name: string; + id: string; + transport: "stdio" | "http"; + command: string[]; + url: string; + off_box: boolean; + exists: boolean; + values: McpImportValue[]; + not_imported: string[]; + notes: string[]; + /** Why this server cannot be imported; empty when it can. */ + refused: string; +} + +/** Reply to preview_mcp_import (TD-5253). `error` set: the text was not readable. */ +export interface McpImportPreview extends DaemonEvent { + type: "mcp_import_preview"; + servers: McpImportServer[]; + error: string; +} + /** A paired phone used a phone permission (TD-5279). Loopback windows only. */ export interface PhoneChange extends DaemonEvent { type: "phone_change"; @@ -3513,6 +3566,7 @@ export type DaemonEventUnion = | PairingCode | PairingCompleted | PhoneChange + | McpImportPreview | MachineList | ApiKeyValidated | EndpointModels From 4da96cd3fd04294ce8708f771c1b261989a9d45f Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:18:47 +0000 Subject: [PATCH 35/35] TD-5293: read_text_file no longer takes the caller's word for the workspace The command read any UTF-8 file under a workspace the caller named, so a script in the webview could name / and read what the user can. A workspace is now a root only when it is exactly a folder the daemon recorded as a session's workspace or worktree. No record means no workspace root. Co-Authored-By: Claude Fable 5.2 --- docs/tst-desk-backlog.md | 30 ++++++++ shell/src/read_text.rs | 149 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 176 insertions(+), 3 deletions(-) diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 2adec30a..943a2d4e 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -14474,3 +14474,33 @@ preview (edit the pasted text instead); VS Code `inputs` descriptions as prompts (an `${input:id}` value is simply asked for by its name); `.mcpb` bundles. +--- + +### TD-5293 — `read_text_file` no longer takes the caller's word for the workspace + +**Size:** 1 · **Depends on:** TD-3202 · **Status:** Done + +The shell's `read_text_file` command reads a UTF-8 file of up to 2 MiB +when it sits under "the workspace", and the workspace was an argument. A +caller is whatever script runs in the webview, so a caller that passed `/` +could read any text file the user can. Nothing in the app does that; the +check simply was not one. + +- [x] A workspace is a root only when it is exactly a folder the daemon + recorded as a session's workspace or worktree in + `{data_dir}/sessions.json`. Not a parent of one, not a child + (`read_text::tests::a_workspace_the_daemon_never_recorded_is_no_root`, + `a_sessions_worktree_folder_is_a_root_too`). +- [x] No record, an unreadable one or a malformed one allows no workspace + (`no_record_or_a_broken_one_allows_no_workspace`). +- [x] The session-dir root is unchanged + (`the_session_dir_root_does_not_depend_on_the_record`). +- [x] `cargo clippy -- -D warnings` and `cargo test --lib` pass on Linux + (139 tests). + +**Not checked:** macOS and Windows builds; the packaged app (a preview of +a workspace artifact right after a session is first opened depends on the +daemon having written `sessions.json`, which it does when the session is +created). Other shell commands that act on a caller's path (`open_path`) +were not reviewed here. + diff --git a/shell/src/read_text.rs b/shell/src/read_text.rs index f06e997b..ef397572 100644 --- a/shell/src/read_text.rs +++ b/shell/src/read_text.rs @@ -4,6 +4,12 @@ //! `fetch()` arbitrary URLs. This command reads a file only when the //! resolved path sits under the session workspace or //! `{data_dir}/sessions/{id}/`. Everything else is refused. +//! +//! The caller names the workspace, and a caller is whatever script runs +//! in the webview, so the name is not taken on trust (TD-5293): it must be +//! a folder the daemon itself recorded as a session's workspace or +//! worktree in `{data_dir}/sessions.json`. Before this, a caller could +//! pass `/` and read any UTF-8 file the user can. use std::fs; use std::path::{Path, PathBuf}; @@ -44,14 +50,55 @@ pub fn read_text_file( } fn allowed_roots(workspace: Option<&str>, session_id: Option<&str>) -> Vec { + allowed_roots_in(&daemon::data_dir(), workspace, session_id) +} + +/// Every folder the daemon recorded as a session's workspace or worktree, +/// canonical. An absent, unreadable or malformed file gives none: the +/// workspace root is then refused rather than believed. +pub(crate) fn known_workspaces(data_dir: &Path) -> Vec { + let Ok(text) = fs::read_to_string(data_dir.join("sessions.json")) else { + return Vec::new(); + }; + let Ok(serde_json::Value::Array(records)) = serde_json::from_str(&text) else { + return Vec::new(); + }; + let mut known = Vec::new(); + for record in &records { + let named = [ + record.get("workspace_path"), + record.get("worktree").and_then(|w| w.get("path")), + ]; + for path in named.into_iter().flatten().filter_map(|v| v.as_str()) { + if path.is_empty() { + continue; + } + if let Ok(root) = fs::canonicalize(path) { + if !known.contains(&root) { + known.push(root); + } + } + } + } + known +} + +fn allowed_roots_in( + data_dir: &Path, + workspace: Option<&str>, + session_id: Option<&str>, +) -> Vec { let mut roots = Vec::new(); if let Some(ws) = workspace.filter(|s| !s.is_empty()) { if let Ok(root) = fs::canonicalize(ws) { - roots.push(root); + // Exactly a recorded folder: not a parent of one, not a child. + if known_workspaces(data_dir).contains(&root) { + roots.push(root); + } } } if let Some(sid) = session_id.and_then(safe_session_id) { - let persist = daemon::data_dir().join("sessions").join(sid); + let persist = data_dir.join("sessions").join(sid); if let Ok(root) = fs::canonicalize(persist) { roots.push(root); } @@ -100,7 +147,103 @@ pub(crate) fn is_within(root: &Path, candidate: &Path) -> bool { #[cfg(test)] mod tests { - use super::safe_session_id; + use super::{allowed_roots_in, known_workspaces, safe_session_id}; + use std::fs; + use std::path::PathBuf; + + fn scratch(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!( + "tst-read-text-{}-{}-{}", + name, + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or(0) + )); + fs::create_dir_all(&dir).unwrap(); + fs::canonicalize(dir).unwrap() + } + + fn record(data: &std::path::Path, body: &str) { + fs::write(data.join("sessions.json"), body).unwrap(); + } + + #[test] + fn a_workspace_the_daemon_never_recorded_is_no_root() { + let base = scratch("unknown"); + let data = base.join("data"); + let opened = base.join("opened"); + let other = base.join("other"); + for dir in [&data, &opened, &other] { + fs::create_dir_all(dir).unwrap(); + } + let body = serde_json::json!([ + {"session_id": "s1", "workspace_path": opened.to_str().unwrap(), "worktree": null} + ]); + record(&data, &body.to_string()); + + let ok = allowed_roots_in(&data, opened.to_str(), None); + assert_eq!(ok, vec![opened.clone()]); + // What the hole was: any folder the caller cared to name. + for named in [other.to_str().unwrap(), base.to_str().unwrap(), "/"] { + assert!(allowed_roots_in(&data, Some(named), None).is_empty(), "{named}"); + } + // A folder inside a recorded workspace is not itself a root either. + let inner = opened.join("src"); + fs::create_dir_all(&inner).unwrap(); + assert!(allowed_roots_in(&data, inner.to_str(), None).is_empty()); + fs::remove_dir_all(base).unwrap(); + } + + #[test] + fn a_sessions_worktree_folder_is_a_root_too() { + let base = scratch("worktree"); + let data = base.join("data"); + let project = base.join("project"); + let tree = base.join("tree"); + for dir in [&data, &project, &tree] { + fs::create_dir_all(dir).unwrap(); + } + let body = serde_json::json!([{ + "session_id": "s1", + "workspace_path": project.to_str().unwrap(), + "worktree": {"path": tree.to_str().unwrap(), "branch": "tst/wt/x"} + }]); + record(&data, &body.to_string()); + let known = known_workspaces(&data); + assert!(known.contains(&project) && known.contains(&tree)); + assert_eq!(allowed_roots_in(&data, tree.to_str(), None), vec![tree.clone()]); + fs::remove_dir_all(base).unwrap(); + } + + #[test] + fn no_record_or_a_broken_one_allows_no_workspace() { + let base = scratch("broken"); + let data = base.join("data"); + let opened = base.join("opened"); + fs::create_dir_all(&data).unwrap(); + fs::create_dir_all(&opened).unwrap(); + assert!(allowed_roots_in(&data, opened.to_str(), None).is_empty()); + for body in ["", "{not json", "{\"sessions\": []}", "[1, \"x\", {\"workspace_path\": 3}]"] { + record(&data, body); + assert!(known_workspaces(&data).is_empty(), "{body}"); + assert!(allowed_roots_in(&data, opened.to_str(), None).is_empty(), "{body}"); + } + fs::remove_dir_all(base).unwrap(); + } + + #[test] + fn the_session_dir_root_does_not_depend_on_the_record() { + let base = scratch("session"); + let data = base.join("data"); + let persist = data.join("sessions").join("abc"); + fs::create_dir_all(&persist).unwrap(); + let roots = allowed_roots_in(&data, None, Some("abc")); + assert_eq!(roots, vec![fs::canonicalize(&persist).unwrap()]); + assert!(allowed_roots_in(&data, None, Some("../abc")).is_empty()); + fs::remove_dir_all(base).unwrap(); + } #[test] fn safe_session_id_keeps_a_plain_uuid() {