mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:39:47 +08:00
Save Draft and Apply were the same write. Both persisted to the live files
and both invalidated the runtime caches, so nothing but their labels told
them apart — and Save Draft only ever wrote the model catalog, while half
the settings pages (memory, starting points, network, attachments,
document parsing) keep their state outside it.
The result was silent data loss, reproducible in four steps: open Starting
points, change 20 to 44, press Save Draft, read "Draft saved" — the server
still had 20, and navigating away lost the edit with no warning. The
button was enabled, because `hasUnsavedChanges` counted extension state
that `saveCatalog` never touched.
So a draft is now a place rather than a label. One envelope holding the
catalog and a payload per page, stored beside the live settings and read
by nothing else:
Save draft → envelope written, runtime untouched
Apply → live files written, envelope cleared
Discard → envelope dropped, back to what is live
Two other losses closed along the way. Unsaved page state now lives on the
provider instead of the page, so moving around inside Settings keeps it —
a settings page unmounts on every navigation, which is why the edit
vanished. And an edit that exists nowhere else warns before the tab is
closed or reloaded.
The toolbar had no way to say "there is a draft waiting", so a draft saved
yesterday would have sat there unexplained; it now reports which of the
three states you are in. The storage path moved into that line's tooltip —
self-hosted users occasionally want to know which file backs a page, but
it is a developer fact and was on screen for everyone.
111 lines
3.6 KiB
Python
111 lines
3.6 KiB
Python
"""A draft is somewhere the runtime does not look."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from deeptutor.services.config.model_catalog import CATALOG_SECRET_MASK
|
|
from deeptutor.services.config.settings_draft import (
|
|
SettingsDraftService,
|
|
is_empty_draft,
|
|
merge_draft_secrets,
|
|
redact_draft,
|
|
)
|
|
|
|
|
|
def _service(tmp_path: Path) -> SettingsDraftService:
|
|
return SettingsDraftService(path=tmp_path / "settings_draft.json")
|
|
|
|
|
|
def _catalog(api_key: str) -> dict[str, Any]:
|
|
return {
|
|
"version": 1,
|
|
"connections": [],
|
|
"services": {
|
|
"llm": {
|
|
"active_profile_id": "p1",
|
|
"active_model_id": "m1",
|
|
"profiles": [
|
|
{
|
|
"id": "p1",
|
|
"name": "OpenAI",
|
|
"api_key": api_key,
|
|
"models": [{"id": "m1", "model": "gpt-5"}],
|
|
}
|
|
],
|
|
}
|
|
},
|
|
}
|
|
|
|
|
|
def test_missing_file_reads_as_an_empty_draft(tmp_path: Path) -> None:
|
|
assert is_empty_draft(_service(tmp_path).load())
|
|
|
|
|
|
def test_a_corrupt_draft_does_not_take_the_page_down(tmp_path: Path) -> None:
|
|
service = _service(tmp_path)
|
|
service.path.parent.mkdir(parents=True, exist_ok=True)
|
|
service.path.write_text("{ not json", encoding="utf-8")
|
|
|
|
assert is_empty_draft(service.load())
|
|
|
|
|
|
def test_saving_records_both_halves_and_a_timestamp(tmp_path: Path) -> None:
|
|
service = _service(tmp_path)
|
|
|
|
saved = service.save(
|
|
{"catalog": _catalog("sk-live"), "extensions": {"chat-starters": {"trace_count": 44}}}
|
|
)
|
|
|
|
assert saved["extensions"]["chat-starters"] == {"trace_count": 44}
|
|
assert saved["updated_at"]
|
|
reloaded = service.load()
|
|
assert reloaded["extensions"] == saved["extensions"]
|
|
|
|
|
|
def test_redaction_masks_the_catalog_half(tmp_path: Path) -> None:
|
|
service = _service(tmp_path)
|
|
saved = service.save({"catalog": _catalog("sk-live"), "extensions": {}})
|
|
|
|
redacted = redact_draft(saved)
|
|
|
|
profile = redacted["catalog"]["services"]["llm"]["profiles"][0]
|
|
assert profile["api_key"] == CATALOG_SECRET_MASK
|
|
# The stored copy is untouched — redaction is for the wire only.
|
|
assert service.load()["catalog"]["services"]["llm"]["profiles"][0]["api_key"] == "sk-live"
|
|
|
|
|
|
def test_a_key_typed_only_into_a_draft_survives_re_saving_it(tmp_path: Path) -> None:
|
|
"""The placeholder has to resolve against the draft, not just the live file."""
|
|
service = _service(tmp_path)
|
|
stored = service.save({"catalog": _catalog("sk-drafted"), "extensions": {}})
|
|
# What the browser sends back after a round trip: everything masked.
|
|
proposed = redact_draft(stored)
|
|
proposed["catalog"]["services"]["llm"]["profiles"][0]["name"] = "Renamed"
|
|
|
|
merged = merge_draft_secrets(proposed, stored, _catalog("sk-live"))
|
|
|
|
profile = merged["catalog"]["services"]["llm"]["profiles"][0]
|
|
assert profile["api_key"] == "sk-drafted"
|
|
assert profile["name"] == "Renamed"
|
|
|
|
|
|
def test_an_untouched_key_still_falls_back_to_the_live_catalog(tmp_path: Path) -> None:
|
|
service = _service(tmp_path)
|
|
proposed = redact_draft({"catalog": _catalog(CATALOG_SECRET_MASK), "extensions": {}})
|
|
|
|
merged = merge_draft_secrets(proposed, service.load(), _catalog("sk-live"))
|
|
|
|
assert merged["catalog"]["services"]["llm"]["profiles"][0]["api_key"] == "sk-live"
|
|
|
|
|
|
def test_clearing_removes_the_file(tmp_path: Path) -> None:
|
|
service = _service(tmp_path)
|
|
service.save({"catalog": _catalog("sk-live"), "extensions": {}})
|
|
|
|
service.clear()
|
|
|
|
assert not service.path.exists()
|
|
assert is_empty_draft(service.load())
|