diff --git a/DECISIONS.md b/DECISIONS.md index 0051e699..d01f65df 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -20522,3 +20522,26 @@ reported as a lapse. **Rejected:** telling the phone too (its pages already follow `setup_state`, and the event names nothing a phone needs); a new shorter preset in the dialog (one hour is the owner's own example and is already there). + +## 2026-11-24 — TD-5303: a chat with no entry is distilled only on this computer (Class B) + +**Decision:** `_distill_entry` keeps TD-5259 for a chat whose entry exists +and TD-5239 for a Grok chat. A chat whose entry was removed, or that names +none, now uses `ModelConfig.local_default()`: the active entry if every role +and backup resolves to a loopback server, else the first such entry, else +none, and distill is skipped. + +**Found at runtime:** on main, `test_a_chat_whose_entry_was_removed_falls_back` +asserted that a chat on `vllm` (127.0.0.1:8000), once that entry was deleted, +was distilled on `tst-default`, which resolves to a hosted server. + +**Rationale:** the owner's rule is that a local chat never sends its +conversation to a hosted model. Once the entry is gone nothing says where +the chat went, so the only target safe for every chat is this computer. A +hosted chat in that state loses one distill at most; a local chat sent to a +hosted model cannot be undone. + +**Rejected:** recording the servers each chat's turns used, so a removed +entry could fall back to one the chat already reached. It needs per-turn +bookkeeping that is persisted across restarts, for a rare case; a skipped +distill is cheap. diff --git a/core/tests/test_distill_entry.py b/core/tests/test_distill_entry.py index 6f1a345b..31232d3c 100644 --- a/core/tests/test_distill_entry.py +++ b/core/tests/test_distill_entry.py @@ -1,4 +1,4 @@ -"""Memory distill runs on the chat's own model (TD-5259). +"""Memory distill runs on the chat's own model (TD-5259, TD-5303). Distill used to take the daemon's active entry whatever the chat was on, so a chat kept on a local model had its conversation sent to a hosted one @@ -137,9 +137,10 @@ async def test_a_grok_chat_keeps_the_active_entry( await _stop(task, ws) -async def test_a_chat_whose_entry_was_removed_falls_back( +async def test_a_chat_whose_entry_was_removed_stays_on_this_computer( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: + """TD-5303: before, this local chat went to the active entry, a hosted one.""" daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) try: seen = _watch(daemon, monkeypatch) @@ -149,12 +150,50 @@ async def test_a_chat_whose_entry_was_removed_falls_back( assert gone["type"] == "setup_state", gone assert "vllm" not in daemon.config.presets sess.preset = "vllm" + assert daemon.config.active_preset == "tst-default" await daemon._run_distill(sess) - assert seen == [_where(daemon, "tst-default")] + # The active entry is hosted, so the first local one runs it. + assert seen == [_where(daemon, "local")] + assert seen[0][1].startswith("http://127.0.0.1:") + finally: + await _stop(task, ws) + + +async def test_a_chat_with_no_entry_is_skipped_when_no_model_is_local( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + seen = _watch(daemon, monkeypatch) + sess = await _chat(daemon, ws, workspace) + for name in ("local", "vllm"): + done = await _ask(ws, {"type": "delete_preset", "name": name}, "setup_state") + assert done["type"] == "setup_state", done + sess.preset = "gone" + await daemon._run_distill(sess) + assert seen == [] + # A chat on a hosted entry of its own is still distilled there. + daemon._bind_session_preset(sess, "budget") + await daemon._run_distill(sess) + assert seen == [_where(daemon, "budget")] finally: await _stop(task, ws) +def test_a_local_entry_with_a_hosted_backup_is_not_local(tmp_path: Path) -> None: + from tests.test_preset_engine import _config + + config = _config(tmp_path) + assert config.local_default() == "local" + brain = config.presets["local"].brain + assert brain is not None + hosted = brain.model_copy(update={"base_url": "https://models.example.com/v1"}) + brain.fallbacks = [hosted] + assert config.local_default() == "vllm" + config.active_preset = "vllm" + assert config.local_default() == "vllm" + + async def test_no_model_anywhere_is_still_skipped( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: diff --git a/core/tstd/config.py b/core/tstd/config.py index 89b61469..4480b703 100644 --- a/core/tstd/config.py +++ b/core/tstd/config.py @@ -1999,6 +1999,25 @@ def native_default(self) -> str | None: return self.active_preset return next((name for name, entry in self.presets.items() if entry.has_tiers), None) + def local_default(self) -> str | None: + """Like ``native_default``, among entries whose models are all on this computer. + + Every role of the entry, and every backup, must call a loopback + server, so nothing it is given leaves the computer (TD-5303). None + when there is none. + """ + + def local(name: str) -> bool: + entry = self.presets.get(name) + if entry is None or not entry.has_tiers: + return False + tiers = [t for tier in entry.tier_map().values() for t in (tier, *tier.fallbacks)] + return all(is_loopback_url(resolve_base_url(self, tier)) for tier in tiers) + + if local(self.active_preset): + return self.active_preset + return next((name for name in self.presets if local(name)), 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/daemon.py b/core/tstd/daemon.py index 9db6ce99..4d2e538a 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -5252,16 +5252,20 @@ def _distill_entry(self, session: Session) -> str | None: moment of distilling, not the one most turns ran on: the person's latest choice of where this chat goes is the one to respect. - A chat on the Grok engine has no model of its own here, and a chat - whose entry has since been removed has none either. Both keep the - TD-5239 rule: the active entry when it names a model, else the - first that does; with none there is nothing to run it on. + A chat on the Grok engine has no model of its own here: it keeps + the TD-5239 rule, the active entry when it names a model, else the + first that does. A chat whose entry has since been removed (or that + never named one) is distilled only by a model on this computer + (TD-5303): nothing says where its conversation went, and it may + have been kept on a local model, so it is never sent to a hosted + one for this. With no such model it is skipped. """ - if session.engine != "grok": - entry = self.config.presets.get(session.preset) - if entry is not None and entry.has_tiers: - return session.preset - return self.config.native_default() + if session.engine == "grok": + return self.config.native_default() + entry = self.config.presets.get(session.preset) + if entry is not None and entry.has_tiers: + return session.preset + return self.config.local_default() async def _distill_live_sessions(self, flight: ProviderFlight | None = None) -> None: """Graceful quit: distill every live idle session that had a turn.""" diff --git a/docs/configuration.md b/docs/configuration.md index b23b8392..75e74115 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -3937,6 +3937,13 @@ memory placeholder. A file at `memory.max_lines` (default 200, see §4.8) is distilled, not appended forever. The agent tools refuse a write that would go over, or that would replace a file already at the cap. +Distill runs at the end of a chat and at shutdown, on the model entry the chat is on at that +moment (TD-5259), so its conversation goes only to the server it was already going to. A chat +whose entry has since been removed is distilled only by an entry whose models, backups +included, are all on this computer: the active one if it is, else the first such entry, else +not at all (TD-5303). A chat on the Grok engine uses the active entry, or the first with a +model. + --- ## 6. When a config is wrong diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 68740dec..d9575459 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -14779,3 +14779,24 @@ row just read "Off" again. - [x] Switching a timed grant off by hand sends no lapse. **Size:** 2 · **Depends on:** TD-5279 · **Status:** Done + +### TD-5303 — A chat whose model entry is gone is distilled only on this computer + +Owner decision #9: memory distill follows the chat's own model, so a local +chat never sends its conversation to a hosted model. TD-5259 did this for a +chat whose entry still exists. Checked at runtime, one case was still open: +a chat whose entry had been removed fell back to the active entry, so a chat +kept on a local model (the test catalog's `vllm`) was distilled on the +active hosted entry. Nothing records where that chat's conversation went. + +- [x] A chat whose entry is gone, or that names none, is distilled only by + an entry whose roles and backups all call a server on this computer: + the active entry if it is one, else the first + (`test_a_chat_whose_entry_was_removed_stays_on_this_computer`). +- [x] With no such entry it is skipped + (`test_a_chat_with_no_entry_is_skipped_when_no_model_is_local`). +- [x] An entry with a hosted backup is not local + (`test_a_local_entry_with_a_hosted_backup_is_not_local`). +- [x] A chat on an entry of its own, and a Grok chat, are unchanged. + +**Size:** 1 · **Depends on:** TD-5259 · **Status:** Done