Files
LLeMbas/src/lembas/services/library/skills.py
T
Jaroslav Beneš 816f2ae957 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>
2026-08-03 11:22:03 +02:00

225 lines
7.3 KiB
Python

"""Skills: named instructions the model can choose to follow.
Two fields carry the design.
`description` is what gets injected -- one line per skill, for every skill --
and is therefore the only thing the model has to go on when deciding whether a
skill is relevant. A description that does not say *when* to use the skill makes
it invisible in practice.
`body` is fetched only when the model decides to use it. That split is what
makes a hundred skills affordable: the index costs a line each, the instructions
cost nothing until wanted.
**A model may rewrite its own skills**, which is the point -- it is how it
learns a procedure once instead of being told every time. The safety story is
not a gate but a record: every write snapshots what was there first, so a change
can be read and undone. A skill written after reading a hostile web page is a
real risk, and the honest mitigation is that it is visible, attributed and
revertible rather than that it was somehow prevented.
"""
from __future__ import annotations
import logging
import re
from collections.abc import Iterable
from sqlalchemy import select
from sqlalchemy.orm import Session as DBSession
from lembas.db.models import AUTHOR_MODEL, AUTHOR_USER, Skill, SkillRevision, User
from lembas.services import sharing
from lembas.services.library.fts import search_ids
log = logging.getLogger(__name__)
INDEX = "skills_fts"
# A name the model can quote back without getting it wrong.
SKILL_NAME_PATTERN = re.compile(r"^[a-z0-9][a-z0-9-]{1,60}$")
MAX_DESCRIPTION_CHARS = 400
MAX_BODY_CHARS = 20_000
# The index goes into every request, so it has a ceiling like memory does.
MAX_INDEX_SKILLS = 60
class SkillError(Exception):
"""A rejected skill write, with a message fit for the model or the user."""
def slugify(name: str) -> str:
cleaned = re.sub(r"[^a-z0-9]+", "-", (name or "").strip().lower()).strip("-")
return cleaned[:60]
def visible(db: DBSession, user: User | None):
return select(Skill).where(sharing.visible_to(Skill, user))
def get(db: DBSession, skill_id: str, user: User | None) -> Skill | None:
skill = db.get(Skill, skill_id)
if skill is None or not sharing.can_read(db, skill, user):
return None
return skill
def by_name(db: DBSession, name: str, user: User | None) -> Skill | None:
"""Look one up the way the model refers to it."""
if user is None:
return None
return db.scalar(visible(db, user).where(Skill.name == slugify(name)))
def enabled_for(
db: DBSession, user: User | None, *, exclude: Iterable[str] = ()
) -> list[Skill]:
"""Skills that should appear in the index, oldest first for a stable order.
`exclude` is what one chat has switched off by name -- a narrowing of what
the library already allows, never a widening of it.
"""
if user is None:
return []
hidden = {slugify(name) for name in exclude}
rows = db.scalars(
visible(db, user)
.where(Skill.enabled.is_(True))
.order_by(Skill.name)
.limit(MAX_INDEX_SKILLS + len(hidden))
)
return [skill for skill in rows if skill.name not in hidden][:MAX_INDEX_SKILLS]
def count_enabled(db: DBSession, user: User | None, *, exclude: Iterable[str] = ()) -> int:
"""How many skills are available here at all.
Zero is what withdraws `skill_get` and `skill_edit`: reading and improving
are meaningless with nothing to read, and a model told to "read one with
skill_get" above a list that is not there spends a round finding out.
"""
return len(enabled_for(db, user, exclude=exclude))
def search(db: DBSession, user: User | None, needle: str, *, limit: int = 10) -> list[Skill]:
hits = search_ids(db, INDEX, needle, limit=limit * 4)
if not hits:
return []
order = {hit.id: position for position, hit in enumerate(hits)}
rows = list(db.scalars(visible(db, user).where(Skill.id.in_(list(order)))))
rows.sort(key=lambda skill: order.get(skill.id, len(order)))
return rows[:limit]
def snapshot(db: DBSession, skill: Skill, *, author: str, note: str = "") -> SkillRevision:
"""Record what a skill looked like before it is changed."""
revision = SkillRevision(
skill_id=skill.id,
description=skill.description,
body=skill.body,
author=author,
note=note[:200],
)
db.add(revision)
return revision
def create(
db: DBSession,
*,
owner: User,
name: str,
description: str,
body: str,
author: str = AUTHOR_USER,
) -> Skill:
slug = slugify(name)
if not SKILL_NAME_PATTERN.match(slug):
raise SkillError(
"A skill name must be two or more letters, numbers or hyphens, "
"such as 'weekly-report'."
)
if by_name(db, slug, owner) is not None:
raise SkillError(f"A skill called {slug!r} already exists. Edit it instead.")
if not description.strip():
raise SkillError(
"A skill needs a description saying when to use it — it is the only "
"thing shown until the skill is opened."
)
skill = Skill(
owner_id=owner.id,
name=slug,
description=description.strip()[:MAX_DESCRIPTION_CHARS],
body=body.strip()[:MAX_BODY_CHARS],
author=author if author in (AUTHOR_USER, AUTHOR_MODEL) else AUTHOR_USER,
)
db.add(skill)
db.commit()
log.info("skill %r created by %s", slug, author)
return skill
def update(
db: DBSession,
skill: Skill,
*,
description: str | None = None,
body: str | None = None,
enabled: bool | None = None,
author: str = AUTHOR_USER,
note: str = "",
) -> Skill:
"""Change a skill, keeping what it was.
The snapshot happens before the change and in the same transaction, so
there is no window where a skill has been rewritten with no record of what
it used to say.
"""
changing = (description is not None and description.strip() != skill.description) or (
body is not None and body.strip() != skill.body
)
if changing:
snapshot(db, skill, author=author, note=note)
if description is not None and description.strip():
skill.description = description.strip()[:MAX_DESCRIPTION_CHARS]
if body is not None:
skill.body = body.strip()[:MAX_BODY_CHARS]
if enabled is not None:
skill.enabled = enabled
if changing:
skill.author = author if author in (AUTHOR_USER, AUTHOR_MODEL) else skill.author
db.commit()
return skill
def revert(db: DBSession, skill: Skill, revision: SkillRevision, *, author: str) -> Skill:
"""Put a skill back to an earlier revision.
The revert is itself a change, so the current state is snapshotted first --
going back is undoable too.
"""
snapshot(db, skill, author=author, note="before revert")
skill.description = revision.description
skill.body = revision.body
db.commit()
return skill
def delete(db: DBSession, skill: Skill) -> None:
sharing.forget_resource(db, skill)
db.delete(skill)
db.commit()
def index_block(db: DBSession, user: User | None, *, exclude: Iterable[str] = ()) -> str:
"""The one-line-per-skill listing that goes into the prompt."""
skills = enabled_for(db, user, exclude=exclude)
if not skills:
return ""
return "\n".join(f"- {skill.name}: {skill.description}" for skill in skills)