A menu for what a chat may use, and three keys
Six smaller things, all of them about the interface not saying what is true.
The @ button only ever inserted the character, which the @ key already does
without a button. It becomes the scope menu: what this chat may use, switched
off per chat. Chat.scope_json is filtered inside resolve_tools AFTER the
capability, permission and instance gates -- exactly as chat.knowledge_bases
narrows knowledge_search -- so a crafted POST turning something on reaches a
tool the gates already removed, and there is a test that writes the column
directly to prove it. Absent means on, for every key, so "why is this off?" has
one answer. It is keyed on the gate rather than the tool name, so notes is one
switch rather than five. The switches carry no role="menuitem", deliberately:
ui.js closes a picker when a menuitem is clicked, which is right for an action
menu and wrong for a list you want to set several of -- which is why the menu
needs no JavaScript at all. Typing @ is untouched.
With no skills, nothing should mention them. tool.skills was gated on the family
alone, so somebody with an empty library was told "the list below gives each
one's name" above no list, handed skill_get, and watched the model spend a round
finding out. It requires skills now; the writing half moved to
tool.skills_write, which is deliberately not gated, because saving the first one
is what somebody with none most needs. And core.tool_list finally reads
tool_names, which had been resolved and documented with no fragment using it.
The composer's toolbar is one row again. .composer__actions is last in the DOM
with margin-left:auto, so the moment an agent chat added a connection, a
directory and a mode, Send and the microphone dropped to a second line.
chat.css has no media queries by design and the fix is not to add one:
.composer__context is the single child allowed to shrink and scroll sideways.
There is a test asserting the file still contains no @media.
The effort picker shows the level in force. "Effort: default" named no level and
was true of nothing in particular; chat.resolved_effort is the chat's own value
and build_request reads the same field, so what is shown is what is sent. The
model's default is a seed, copied onto the row at creation and on a model
change, and never consulted at request time -- a fallback would resurrect it
underneath a cleared effort and make "off" silently do nothing. "off" is a
sentinel and not an empty value, because start_chat declares Form("") and cannot
tell absent from empty: with value="" the reader picks off and gets high.
Alt+M dictates, Alt+R reads the last reply aloud, Ctrl+Enter sends from
anywhere. All three click the button that already does the job, so audio.js
keeps its one delegated listener. Alt+M and not Alt+D, which is the address bar
in Chrome and Firefox. Ctrl+Enter never means Stop -- Send and Stop are the same
element, and Esc already stops. Driven under a DOM stub before committing, per
the rule in CLAUDE.md, and tests/test_commands_js.py pins that every key has a
row in SHORTCUTS, since /help reads that list.
And the memory tooling, which had seven defects. The worst: memory_forget was a
case-insensitive substring first-match delete with nothing warning about it, so
forgetting "coffee" against "Drinks coffee black" and "Allergic to coffee"
silently removed whichever was older -- a wrong deletion nobody would ever find
out about, from a tool whose description invited exactly the short fragment that
misfires. It matches exactly first, then by substring, and refuses an ambiguous
one while naming what it matched. add() refuses an exact duplicate. The
at-the-limit refusal no longer tells the model to delete one to make room: past
the block's budget it is not shown all of them and would be guessing, which
feeds straight back into the first defect. And context.memories no longer claims
the memories "still apply", which nothing checks and which taught a model to
trust a stale one over what the person had just said.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -686,7 +686,7 @@ async def test_an_ordinary_chat_is_told_none_of_it(db, user_id, machine):
|
||||
|
||||
|
||||
async def test_an_agent_chat_is_told_its_real_round_budget(db, user_id, machine):
|
||||
"""MAX_ROUNDS is three. An agent chat gets forty, and telling it three would
|
||||
"""MAX_ROUNDS is one. An agent chat gets hundreds, and telling it one would
|
||||
be a false fact about its own budget on every turn."""
|
||||
from lembas.services import harness
|
||||
|
||||
|
||||
@@ -699,3 +699,79 @@ def test_the_sidebar_shows_an_unread_dot(client: TestClient, db, registered, mak
|
||||
page = client.get("/chat").text
|
||||
assert f'id="unread-{chat_id}" class="unread-dot"' in page
|
||||
assert 'hx-get="/api/chats/unread"' in page
|
||||
|
||||
|
||||
# --- The composer's one row --------------------------------------------------
|
||||
def test_the_send_button_is_the_last_thing_in_the_toolbar(client: TestClient, db, registered):
|
||||
"""What the layout depends on. `.composer__actions` is pushed right by
|
||||
`margin-left: auto` and refuses to shrink, and both only work while it is
|
||||
the last child -- when the row wrapped instead, it was the last child that
|
||||
dropped to a second line, so an agent chat pushed Send and the microphone
|
||||
off the row entirely."""
|
||||
_add_connection(db)
|
||||
html = client.get("/chat").text
|
||||
toolbar = html.split('class="composer__toolbar"', 1)[1]
|
||||
|
||||
assert 'class="composer__actions"' in toolbar
|
||||
assert toolbar.index("composer__actions") > toolbar.index("composer__tools")
|
||||
assert "data-composer-action" in toolbar
|
||||
|
||||
|
||||
def test_the_agent_controls_shrink_rather_than_pushing_send_off_the_row(
|
||||
client: TestClient, db, registered
|
||||
):
|
||||
"""They live in `.composer__context`, which is the only flex child allowed
|
||||
to shrink and scroll. Anything moved out of it stops shrinking and starts
|
||||
pushing Send onto a second line again -- which is what this whole row was
|
||||
rearranged to stop."""
|
||||
from lembas.db.models import SshProfile
|
||||
from lembas.services import settings_store
|
||||
|
||||
_add_connection(db)
|
||||
settings_store.update(db, {"enabled": True}, key=settings_store.AGENTS)
|
||||
db.add(
|
||||
SshProfile(
|
||||
owner_id=_user_id(db),
|
||||
name="Test box",
|
||||
host="127.0.0.1",
|
||||
port=22,
|
||||
username="t",
|
||||
host_key="k",
|
||||
host_fingerprint="f",
|
||||
default_dir="/work",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
html = client.get("/chat").text
|
||||
if "composer__context" not in html:
|
||||
pytest.skip("agent chats are unavailable here")
|
||||
|
||||
# The three that appear when Agent is chosen sit between the start of
|
||||
# `.composer__context` and the start of `.composer__actions` -- which is
|
||||
# what puts them inside the one child that is allowed to give, and keeps
|
||||
# the actions last.
|
||||
opens = html.index('class="composer__context"')
|
||||
actions = html.index('class="composer__actions"')
|
||||
for control in ("ssh_profile_id", "data-dir-browse", 'name="agent_mode"'):
|
||||
assert opens < html.index(control) < actions, control
|
||||
|
||||
|
||||
def test_the_chat_stylesheet_has_no_media_queries(client: TestClient):
|
||||
"""A stated design constraint, pinned so nobody 'fixes' a layout with a
|
||||
breakpoint later. The composer fits at every width by saying which child
|
||||
gives, not by rearranging itself at a threshold."""
|
||||
from pathlib import Path
|
||||
|
||||
import lembas
|
||||
|
||||
css = Path(lembas.__file__).parent / "web/static/css/chat.css"
|
||||
assert "@media" not in css.read_text()
|
||||
|
||||
|
||||
def _user_id(db):
|
||||
from sqlalchemy import select
|
||||
|
||||
from lembas.db.models import User
|
||||
|
||||
return db.scalar(select(User.id))
|
||||
|
||||
@@ -0,0 +1,284 @@
|
||||
"""What one chat may use, and the rule that it can only ever be less.
|
||||
|
||||
The security-shaped test here is `test_a_chat_cannot_widen_what_it_was_not_given`.
|
||||
The scope is applied inside `resolve_tools` *after* the model's capabilities,
|
||||
the reader's permissions and the instance configuration, so a crafted POST
|
||||
turning something on reaches a tool those gates have already removed. Asserting
|
||||
that against the UI path alone would prove nothing, so it is asserted against a
|
||||
directly-written column.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from lembas.db.models import Chat, Connection, Model, User
|
||||
from lembas.services import settings_store
|
||||
from lembas.services import tools as tools_service
|
||||
from lembas.services.library import skills as skills_service
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def chat(db, user_id):
|
||||
connection = Connection(name="c", base_url="http://127.0.0.1:1", api_key_encrypted="")
|
||||
db.add(connection)
|
||||
db.commit()
|
||||
db.add(Model(connection_id=connection.id, model_id="m", capabilities_json={"tools": True}))
|
||||
db.commit()
|
||||
row = Chat(user_id=user_id, model_id="m", connection_id=connection.id)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
return row
|
||||
|
||||
|
||||
def _names(db, chat, user) -> set[str]:
|
||||
return set(tools_service.resolve_tools(db, chat, user).by_name)
|
||||
|
||||
|
||||
# --- The route ------------------------------------------------------------------
|
||||
def test_switching_a_family_off_writes_it_to_the_row(client: TestClient, db, chat, registered):
|
||||
response = client.post(
|
||||
f"/api/chats/{chat.id}/scope", data={"kind": "family", "name": "web_search"}
|
||||
)
|
||||
assert response.status_code == 204
|
||||
|
||||
db.expire_all()
|
||||
assert db.get(Chat, chat.id).scope_json["families"]["web_search"] is False
|
||||
|
||||
|
||||
def test_switching_it_back_on_removes_the_key(client: TestClient, db, chat, registered):
|
||||
"""On is stored by *removing* the key, so absent stays the single
|
||||
representation of on and the column cannot grow a row per family per chat."""
|
||||
client.post(f"/api/chats/{chat.id}/scope", data={"kind": "family", "name": "notes"})
|
||||
client.post(
|
||||
f"/api/chats/{chat.id}/scope",
|
||||
data={"kind": "family", "name": "notes", "on": "true"},
|
||||
)
|
||||
|
||||
db.expire_all()
|
||||
assert "families" not in db.get(Chat, chat.id).scope_json
|
||||
|
||||
|
||||
def test_the_route_refuses_a_kind_it_does_not_know(client: TestClient, chat, registered):
|
||||
response = client.post(
|
||||
f"/api/chats/{chat.id}/scope", data={"kind": "everything", "name": "x"}
|
||||
)
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_the_route_refuses_the_wrong_verb(client: TestClient, chat, registered):
|
||||
"""The half of `tests/test_agent_mode.py`'s lesson that actually caught the
|
||||
bug: a control wired to a method a route does not serve fails silently."""
|
||||
assert client.get(f"/api/chats/{chat.id}/scope").status_code == 405
|
||||
|
||||
|
||||
def test_somebody_elses_chat_is_not_reachable(client: TestClient, db, chat, registered):
|
||||
from lembas.security.passwords import hash_password
|
||||
|
||||
other = User(name="Sam", email="s@shire.test", password_hash=hash_password("secret123"))
|
||||
db.add(other)
|
||||
db.commit()
|
||||
chat.user_id = other.id
|
||||
db.commit()
|
||||
|
||||
response = client.post(
|
||||
f"/api/chats/{chat.id}/scope", data={"kind": "family", "name": "notes"}
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
# --- What it does to the offer ------------------------------------------------------
|
||||
def test_a_family_switched_off_is_not_offered(db, chat, user_id):
|
||||
settings_store.update(db, {"enabled": True}, key=settings_store.SEARCH)
|
||||
user = db.get(User, user_id)
|
||||
assert "web_search" in _names(db, chat, user)
|
||||
|
||||
chat.scope_json = {"families": {"web_search": False}}
|
||||
db.commit()
|
||||
|
||||
assert "web_search" not in _names(db, chat, user)
|
||||
|
||||
|
||||
def test_switching_a_gate_off_takes_every_tool_in_it(db, chat, user_id):
|
||||
"""A gate is one switch, not five. `notes` covers search, get, create, edit
|
||||
and delete -- which is the same reasoning the per-model capability
|
||||
checkboxes carry."""
|
||||
user = db.get(User, user_id)
|
||||
chat.scope_json = {"families": {"notes": False}}
|
||||
db.commit()
|
||||
|
||||
offered = _names(db, chat, user)
|
||||
assert not [name for name in offered if name.startswith("notes_")]
|
||||
|
||||
|
||||
def test_a_chat_cannot_widen_what_it_was_not_given(db, chat, user_id):
|
||||
"""The one that matters. Scope is applied AFTER the gates and never instead
|
||||
of them, so writing `True` into the column reaches a tool the model's
|
||||
capabilities had already removed."""
|
||||
user = db.get(User, user_id)
|
||||
model = db.scalar(tools_service.select(Model))
|
||||
model.capabilities_json = {"tools": True, "tool_notes": False}
|
||||
chat.scope_json = {"families": {"notes": True}}
|
||||
db.commit()
|
||||
|
||||
assert "notes_search" not in _names(db, chat, user)
|
||||
|
||||
|
||||
def test_an_unknown_family_in_the_column_changes_nothing(db, chat, user_id):
|
||||
user = db.get(User, user_id)
|
||||
before = _names(db, chat, user)
|
||||
chat.scope_json = {"families": {"not-a-family": False}}
|
||||
db.commit()
|
||||
|
||||
assert _names(db, chat, user) == before
|
||||
|
||||
|
||||
# --- Skills -------------------------------------------------------------------------
|
||||
@pytest.fixture
|
||||
def skill(db, user_id):
|
||||
return skills_service.create(
|
||||
db,
|
||||
owner=db.get(User, user_id),
|
||||
name="weekly-report",
|
||||
description="When asked for the weekly report.",
|
||||
body="Do the thing.",
|
||||
)
|
||||
|
||||
|
||||
def test_a_skill_switched_off_leaves_the_index(db, chat, user_id, skill):
|
||||
user = db.get(User, user_id)
|
||||
assert "weekly-report" in skills_service.index_block(db, user)
|
||||
assert "weekly-report" not in skills_service.index_block(
|
||||
db, user, exclude=["weekly-report"]
|
||||
)
|
||||
|
||||
|
||||
def test_a_skill_switched_off_cannot_be_fetched_anyway(db, chat, user_id, skill):
|
||||
"""Without this the narrowing is advisory: a model can name a skill it was
|
||||
never shown -- from an earlier turn, from a note -- and the runner would
|
||||
happily fetch it. Same rule as "what may be run is what was offered"."""
|
||||
import asyncio
|
||||
|
||||
user = db.get(User, user_id)
|
||||
chat.scope_json = {"skills": {"weekly-report": False}}
|
||||
db.commit()
|
||||
|
||||
context = tools_service.context_for(db, user, chat)
|
||||
outcome = asyncio.run(
|
||||
tools_service.run_tool(context, "skill_get", '{"name": "weekly-report"}')
|
||||
)
|
||||
assert outcome.event["status"] == "error"
|
||||
|
||||
|
||||
def test_the_last_skill_switched_off_withdraws_skill_get(db, chat, user_id, skill):
|
||||
user = db.get(User, user_id)
|
||||
assert "skill_get" in _names(db, chat, user)
|
||||
|
||||
chat.scope_json = {"skills": {"weekly-report": False}}
|
||||
db.commit()
|
||||
|
||||
offered = _names(db, chat, user)
|
||||
assert "skill_get" not in offered
|
||||
assert "skill_create" in offered, "writing the first one is still possible"
|
||||
|
||||
|
||||
# --- The zero-skills asymmetry --------------------------------------------------------
|
||||
def test_with_no_skills_nothing_tells_the_model_to_read_one(db, chat, user_id):
|
||||
"""The complaint this fixes. `tool.skills` was gated on the family alone, so
|
||||
a person with no skills got "read the full instructions with skill_get"
|
||||
above a list that was not there -- and got skill_get in the tools array, so
|
||||
the model spent a round finding out."""
|
||||
from lembas.services import harness
|
||||
|
||||
user = db.get(User, user_id)
|
||||
offered = tools_service.resolve_tools(db, chat, user).schemas
|
||||
text = harness.compose(db, user, offered, chat)
|
||||
|
||||
assert "skill_get" not in _names(db, chat, user)
|
||||
assert "skill_get" not in text
|
||||
assert "Skills available" not in text
|
||||
# The half that is most useful with none: you can save the first one.
|
||||
assert "save it with skill_create" in text
|
||||
|
||||
|
||||
def test_with_a_skill_the_reading_guidance_comes_back(db, chat, user_id, skill):
|
||||
from lembas.services import harness
|
||||
|
||||
user = db.get(User, user_id)
|
||||
offered = tools_service.resolve_tools(db, chat, user).schemas
|
||||
text = harness.compose(db, user, offered, chat)
|
||||
|
||||
assert "skill_get" in text
|
||||
assert "weekly-report" in text
|
||||
assert "save it with skill_create" in text
|
||||
|
||||
|
||||
# --- The tool list --------------------------------------------------------------------
|
||||
def test_the_model_is_told_what_it_actually_has(db, chat, user_id):
|
||||
"""`tool_names` was resolved and documented with no fragment reading it. A
|
||||
model that has to discover its own list by calling something and being told
|
||||
it does not exist spends a round finding out -- and with one round, that is
|
||||
the whole reply."""
|
||||
from lembas.services import harness
|
||||
|
||||
user = db.get(User, user_id)
|
||||
offered = tools_service.resolve_tools(db, chat, user).schemas
|
||||
text = harness.compose(db, user, offered, chat)
|
||||
|
||||
assert "The tools you have on this request are:" in text
|
||||
for name in tools_service.resolve_tools(db, chat, user).by_name:
|
||||
assert name in text
|
||||
|
||||
|
||||
def test_a_family_switched_off_disappears_from_the_list_too(db, chat, user_id):
|
||||
from lembas.services import harness
|
||||
|
||||
settings_store.update(db, {"enabled": True}, key=settings_store.SEARCH)
|
||||
user = db.get(User, user_id)
|
||||
chat.scope_json = {"families": {"web_search": False}}
|
||||
db.commit()
|
||||
|
||||
offered = tools_service.resolve_tools(db, chat, user).schemas
|
||||
text = harness.compose(db, user, offered, chat)
|
||||
|
||||
assert "web_search" not in text
|
||||
|
||||
|
||||
def test_no_tools_means_no_list(db, chat, user_id):
|
||||
from lembas.services import harness
|
||||
|
||||
text = harness.compose(db, db.get(User, user_id), [])
|
||||
assert "The tools you have on this request" not in text
|
||||
|
||||
|
||||
# --- The control that writes ------------------------------------------------------------
|
||||
def test_the_verb_is_on_every_checkbox(client: TestClient, db, chat, registered):
|
||||
"""The element carrying `name` has to be the element carrying the request.
|
||||
Two selects lost an entire release to getting this wrong -- their verb was
|
||||
on a form the event never reached, and the tests passed throughout because
|
||||
they asserted the markup rather than the property.
|
||||
|
||||
`conftest.control_named` is the helper for this and wants exactly one match;
|
||||
there is one checkbox per family here, so the same check is made over all of
|
||||
them, which is the stronger claim anyway.
|
||||
"""
|
||||
from html.parser import HTMLParser
|
||||
|
||||
settings_store.update(db, {"enabled": True}, key=settings_store.SEARCH)
|
||||
html = client.get(f"/chat/{chat.id}").text
|
||||
|
||||
found: list[dict[str, str]] = []
|
||||
|
||||
class Finder(HTMLParser):
|
||||
def handle_starttag(self, tag, attrs):
|
||||
got = {key: (value or "") for key, value in attrs}
|
||||
if got.get("name") == "on":
|
||||
found.append(got)
|
||||
|
||||
Finder().feed(html)
|
||||
|
||||
assert found, "the scope menu rendered no switches"
|
||||
for box in found:
|
||||
assert box.get("hx-post") == f"/api/chats/{chat.id}/scope"
|
||||
assert "kind" in box.get("hx-vals", ""), "and says which thing it is"
|
||||
@@ -0,0 +1,60 @@
|
||||
"""The keyboard, checked without a runtime.
|
||||
|
||||
There is no JavaScript test runner here and hard rule 1 keeps Node out of the
|
||||
project, so the behaviour is driven by hand under a DOM stub before committing.
|
||||
What can be pinned in the suite is the invariant the file states about itself:
|
||||
`/help` reads `SHORTCUTS`, so a shortcut that is not in that list is a shortcut
|
||||
nobody can discover. That is the direction this actually rots -- a key gets
|
||||
added to the handler and the sheet is forgotten.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
import lembas
|
||||
|
||||
SOURCE = (
|
||||
Path(lembas.__file__).parent / "web/static/js/commands.js"
|
||||
).read_text(encoding="utf-8")
|
||||
|
||||
# The declared list, up to where the command table starts.
|
||||
SHORTCUTS = SOURCE[SOURCE.index("var SHORTCUTS") : SOURCE.index("/* --- The table")]
|
||||
|
||||
|
||||
def test_every_letter_key_the_handlers_match_is_described():
|
||||
letters = {match[-1] for match in re.findall(r'event\.code === "Key([A-Z])"', SOURCE)}
|
||||
assert letters, "no letter shortcuts found at all, which means the regex is wrong"
|
||||
|
||||
missing = [letter for letter in sorted(letters) if f"+ {letter}" not in SHORTCUTS]
|
||||
assert not missing, f"not in /help: {missing}"
|
||||
|
||||
|
||||
def test_the_three_new_ones_are_there():
|
||||
"""Named rather than only counted, because the point of them is being
|
||||
findable: Enter already sends *inside the box*, and dictation and read-aloud
|
||||
were click-only."""
|
||||
assert "Ctrl/⌘ + Enter" in SHORTCUTS
|
||||
assert "Alt + M" in SHORTCUTS
|
||||
assert "Alt + R" in SHORTCUTS
|
||||
|
||||
|
||||
def test_dictation_is_not_bound_to_alt_d():
|
||||
"""Alt+D is the address bar in Chrome and Firefox on Windows and Linux. A
|
||||
shortcut the browser wins is a shortcut that looks broken."""
|
||||
assert 'event.code === "KeyD"' not in SOURCE
|
||||
|
||||
|
||||
def test_the_shortcuts_are_matched_on_the_physical_key():
|
||||
"""The file's own stated rule: `event.code`, so a Dvorak or Slovak layout
|
||||
gets the same shortcuts rather than whichever letters sit there."""
|
||||
assert "event.key ===" not in SOURCE
|
||||
|
||||
|
||||
def test_send_from_anywhere_never_means_stop():
|
||||
"""Send and Stop are the same element. Ctrl+Enter reaching it while it is
|
||||
Stop would abandon a reply on a key people press to send -- and Esc already
|
||||
stops."""
|
||||
window = SOURCE[SOURCE.index('event.code === "Enter"') :][:600]
|
||||
assert 'composerAction === "send"' in window
|
||||
@@ -230,3 +230,139 @@ def test_a_nonsense_effort_at_the_start_falls_back(client: TestClient, db, regis
|
||||
)
|
||||
|
||||
assert db.scalars(select(Chat)).one().params_json["reasoning_effort"] == "high"
|
||||
|
||||
|
||||
# --- What the picker says is what is sent ---------------------------------------
|
||||
def test_the_resolver_is_what_the_request_carries(client: TestClient, db, registered):
|
||||
"""One resolver, so the control and the request cannot disagree. That
|
||||
disagreement is the whole reason the picker said "default": it named no
|
||||
level, and was true of nothing in particular."""
|
||||
chat = _chat(db, "high")
|
||||
|
||||
assert chat_service.resolved_effort(chat) == "high"
|
||||
assert chat_service.build_request(db, chat)["reasoning_effort"] == "high"
|
||||
|
||||
|
||||
def test_a_cleared_effort_is_not_resurrected_by_the_models_default(
|
||||
client: TestClient, db, registered
|
||||
):
|
||||
"""What the no-fallback decision buys. If `build_request` fell back to the
|
||||
model, `update_chat` storing None for a cleared effort would be undone
|
||||
underneath it and the off option would silently do nothing."""
|
||||
model = _model(db)
|
||||
model.params_json = {"reasoning_effort": "high"}
|
||||
user = db.scalars(select(User)).first()
|
||||
chat = Chat(
|
||||
user_id=user.id,
|
||||
model_id=model.model_id,
|
||||
connection_id=model.connection_id,
|
||||
params_json={"reasoning_effort": None},
|
||||
)
|
||||
db.add(chat)
|
||||
db.commit()
|
||||
|
||||
assert chat_service.resolved_effort(chat) == ""
|
||||
body = chat_service.build_request(db, chat)
|
||||
assert "reasoning_effort" not in body
|
||||
assert "chat_template_kwargs" not in body
|
||||
|
||||
|
||||
def test_choosing_off_before_the_chat_exists_sends_nothing(
|
||||
client: TestClient, db, registered
|
||||
):
|
||||
"""The one that would otherwise ship broken. `start_chat` declares
|
||||
`Form("")`, so an absent field and an empty one are the same thing there --
|
||||
with `value=""` on the off option the reader picks off, the value falls out
|
||||
of EFFORTS, and the model's default seeded onto the row stays. They get
|
||||
"high"."""
|
||||
model = _model(db)
|
||||
model.params_json = {"reasoning_effort": "high"}
|
||||
db.commit()
|
||||
|
||||
client.post(
|
||||
"/api/chats/start",
|
||||
data={"content": "hello", "model_id": "m", "reasoning_effort": "off"},
|
||||
)
|
||||
chat = db.scalars(select(Chat)).first()
|
||||
|
||||
assert not (chat.params_json or {}).get("reasoning_effort")
|
||||
assert "reasoning_effort" not in chat_service.build_request(db, chat)
|
||||
|
||||
|
||||
def test_patching_off_clears_it(client: TestClient, db, registered):
|
||||
chat = _chat(db, "high")
|
||||
client.patch(f"/api/chats/{chat.id}", data={"reasoning_effort": "off"})
|
||||
|
||||
db.expire_all()
|
||||
assert chat_service.resolved_effort(db.get(Chat, chat.id)) == ""
|
||||
|
||||
|
||||
def test_switching_model_seeds_an_effort_that_was_never_chosen(
|
||||
client: TestClient, db, registered
|
||||
):
|
||||
"""So "what the picker shows is what is sent" stays true after a switch."""
|
||||
chat = _chat(db)
|
||||
second = Model(
|
||||
connection_id=chat.connection_id,
|
||||
model_id="m2",
|
||||
capabilities_json={"reasoning": True},
|
||||
params_json={"reasoning_effort": "medium"},
|
||||
)
|
||||
db.add(second)
|
||||
db.commit()
|
||||
|
||||
client.patch(f"/api/chats/{chat.id}", data={"model_id": "m2"})
|
||||
|
||||
db.expire_all()
|
||||
assert chat_service.resolved_effort(db.get(Chat, chat.id)) == "medium"
|
||||
|
||||
|
||||
def test_switching_model_does_not_overwrite_a_chosen_effort(
|
||||
client: TestClient, db, registered
|
||||
):
|
||||
chat = _chat(db, "low")
|
||||
second = Model(
|
||||
connection_id=chat.connection_id,
|
||||
model_id="m2",
|
||||
capabilities_json={"reasoning": True},
|
||||
params_json={"reasoning_effort": "high"},
|
||||
)
|
||||
db.add(second)
|
||||
db.commit()
|
||||
|
||||
client.patch(f"/api/chats/{chat.id}", data={"model_id": "m2"})
|
||||
|
||||
db.expire_all()
|
||||
assert chat_service.resolved_effort(db.get(Chat, chat.id)) == "low"
|
||||
|
||||
|
||||
def test_switching_model_does_not_resurrect_a_cleared_effort(
|
||||
client: TestClient, db, registered
|
||||
):
|
||||
"""`None` means somebody cleared it deliberately. Only an ABSENT key is
|
||||
seeded, or "off" would silently undo itself on the next model change."""
|
||||
chat = _chat(db, "high")
|
||||
client.patch(f"/api/chats/{chat.id}", data={"reasoning_effort": "off"})
|
||||
second = Model(
|
||||
connection_id=chat.connection_id,
|
||||
model_id="m2",
|
||||
capabilities_json={"reasoning": True},
|
||||
params_json={"reasoning_effort": "high"},
|
||||
)
|
||||
db.add(second)
|
||||
db.commit()
|
||||
|
||||
client.patch(f"/api/chats/{chat.id}", data={"model_id": "m2"})
|
||||
|
||||
db.expire_all()
|
||||
assert chat_service.resolved_effort(db.get(Chat, chat.id)) == ""
|
||||
|
||||
|
||||
def test_the_picker_never_says_default(client: TestClient, db, registered):
|
||||
"""The one markup assertion. It named no level and was true of nothing."""
|
||||
chat = _chat(db, "medium")
|
||||
html = client.get(f"/chat/{chat.id}").text
|
||||
|
||||
assert "Effort: default" not in html
|
||||
assert "Effort: off" in html
|
||||
assert '<option value="medium" selected>' in html.replace("\n", "").replace(" ", "")
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
"""Memory: the two ways it used to lose somebody's facts quietly.
|
||||
|
||||
`memory_forget` was a case-insensitive substring FIRST-match delete with nothing
|
||||
warning about it, so a short fragment removed whichever memory happened to be
|
||||
older -- and a wrong deletion here is not something anybody finds out about.
|
||||
`memory_add` had no defence against the same fact being stored four times in
|
||||
slightly different words, which costs the window forever *and* makes every
|
||||
forget after it ambiguous.
|
||||
|
||||
Both are asserted on the rows, not on the wording.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import func, select
|
||||
|
||||
from lembas.db.models import Memory, User
|
||||
from lembas.security.passwords import hash_password
|
||||
from lembas.services import tools as tools_service
|
||||
from lembas.services.library import memories as memories_service
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def owner(db):
|
||||
user = User(name="Frodo", email="f@shire.test", password_hash=hash_password("x"))
|
||||
db.add(user)
|
||||
db.commit()
|
||||
return user
|
||||
|
||||
|
||||
def _count(db, owner) -> int:
|
||||
return db.scalar(
|
||||
select(func.count()).select_from(Memory).where(Memory.owner_id == owner.id)
|
||||
)
|
||||
|
||||
|
||||
def _forget(owner, text: str):
|
||||
import asyncio
|
||||
|
||||
context = tools_service.ToolContext(owner_id=owner.id)
|
||||
return asyncio.run(tools_service.run_tool(context, "memory_forget", f'{{"content": "{text}"}}'))
|
||||
|
||||
|
||||
# --- Forgetting ----------------------------------------------------------------
|
||||
def test_an_ambiguous_forget_removes_nothing(db, owner):
|
||||
"""Two memories about coffee; "coffee" names neither of them."""
|
||||
memories_service.add(db, owner=owner, content="Drinks coffee black.")
|
||||
memories_service.add(db, owner=owner, content="Allergic to coffee.")
|
||||
|
||||
outcome = _forget(owner, "coffee")
|
||||
|
||||
assert _count(db, owner) == 2
|
||||
assert outcome.event["status"] == "error"
|
||||
assert "Drinks coffee black." in outcome.content
|
||||
assert "Allergic to coffee." in outcome.content
|
||||
|
||||
|
||||
def test_quoting_a_memory_in_full_removes_that_one(db, owner):
|
||||
"""Exact-first is what makes this work. "Drinks coffee." is a substring of
|
||||
"Drinks coffee. Never tea." too, so a substring-only match would call the
|
||||
unambiguous case ambiguous and refuse to do anything at all."""
|
||||
short = memories_service.add(db, owner=owner, content="Drinks coffee.")
|
||||
long = memories_service.add(db, owner=owner, content="Drinks coffee. Never tea.")
|
||||
short_id, long_id = short.id, long.id
|
||||
|
||||
_forget(owner, "Drinks coffee.")
|
||||
|
||||
db.expire_all()
|
||||
assert db.get(Memory, short_id) is None
|
||||
assert db.get(Memory, long_id) is not None
|
||||
|
||||
|
||||
def test_forgetting_something_that_is_not_there_says_so(db, owner):
|
||||
memories_service.add(db, owner=owner, content="Drinks coffee black.")
|
||||
outcome = _forget(owner, "tea")
|
||||
assert _count(db, owner) == 1
|
||||
assert outcome.event["status"] == "error"
|
||||
|
||||
|
||||
def test_an_unambiguous_fragment_still_works(db, owner):
|
||||
"""Quoting in full is what the description asks for, but a fragment that
|
||||
genuinely names one memory should not be made to fail."""
|
||||
memories_service.add(db, owner=owner, content="Drinks coffee black.")
|
||||
memories_service.add(db, owner=owner, content="Lives in Bree.")
|
||||
|
||||
_forget(owner, "Bree")
|
||||
|
||||
db.expire_all()
|
||||
assert _count(db, owner) == 1
|
||||
|
||||
|
||||
# --- Adding --------------------------------------------------------------------
|
||||
def test_an_exact_duplicate_creates_nothing(db, owner):
|
||||
first = memories_service.add(db, owner=owner, content="Prefers metric units.")
|
||||
again = memories_service.add(db, owner=owner, content=" Prefers metric units. ")
|
||||
|
||||
assert _count(db, owner) == 1
|
||||
assert again.id == first.id
|
||||
|
||||
|
||||
def test_the_limit_refuses_and_does_not_tell_the_model_to_guess(db, owner, monkeypatch):
|
||||
"""Past MAX_TOTAL_CHARS the injected block is truncated, so the model is not
|
||||
shown every memory. Telling it to remove one to make room asks it to choose
|
||||
blind -- and the forget path above is exactly where blind guessing bites."""
|
||||
monkeypatch.setattr(memories_service, "MAX_RECORDS", 3)
|
||||
for index in range(3):
|
||||
memories_service.add(db, owner=owner, content=f"Fact {index}")
|
||||
|
||||
with pytest.raises(ValueError) as caught:
|
||||
memories_service.add(db, owner=owner, content="One too many")
|
||||
|
||||
assert _count(db, owner) == 3
|
||||
message = str(caught.value)
|
||||
assert "note instead" in message
|
||||
assert "Remove one first" not in message
|
||||
Reference in New Issue
Block a user