Skip to content

The agent that grows with you

Merge origin/main into bb/skill-chip-only

Brooklyn Nicholsonb3ba5570c997
diff --cc tests/test_tui_gateway_server.py
index 849c3397ed7,1425e6dd75e..7e638493020
--- a/tests/test_tui_gateway_server.py
+++ b/tests/test_tui_gateway_server.py
@@@ -1753,93 -1753,29 +1753,116 @@@ def test_history_to_messages_drops_disp
assert all("api_content" not in m for m in projected)
+def test_history_to_messages_projects_a_skill_turn_to_its_invocation():
+ # A /skill invocation is persisted EXPANDED: the activation note plus the
+ # entire skill body. That payload is model-facing scaffolding -- this
+ # projection is the single display source every client reads, so it must
+ # hand back the invocation the user typed and never the body. Without it a
+ # chat bubble renders the whole skill as if the user had written it.
+ scaffolded = (
+ '[IMPORTANT: The user has invoked the "work" skill, indicating they '
+ "want you to follow its instructions. The full skill content is "
+ "loaded below.]\n\n"
+ "# /work\n\nSPIN UP A WORKTREE, never the primary checkout.\n\n"
+ "The user has provided the following instruction alongside the skill "
+ "invocation: fix the title leak"
+ )
+
+ history = [
+ {"role": "user", "content": scaffolded},
+ {"role": "assistant", "content": "on it"},
+ ]
+
+ assert server._history_to_messages(history) == [
+ {
+ "role": "user",
+ "text": "/work fix the title leak",
+ "display_kind": "skill_invocation",
+ },
+ {"role": "assistant", "text": "on it"},
+ ]
+
+
+def test_history_to_messages_projects_a_bare_skill_turn_to_the_command():
+ scaffolded = (
+ '[IMPORTANT: The user has invoked the "work" skill, indicating they '
+ "want you to follow its instructions. The full skill content is "
+ "loaded below.]\n\n# /work\n\nSPIN UP A WORKTREE."
+ )
+
+ assert server._history_to_messages([{"role": "user", "content": scaffolded}]) == [
+ {"role": "user", "text": "/work", "display_kind": "skill_invocation"}
+ ]
+
+
+def test_expand_skill_invocation_for_replay_round_trips_the_projection(
+ tmp_path, monkeypatch
+):
+ # Rewind/regenerate replays a turn from what the transcript SHOWS, and a
+ # skill turn shows its invocation. Re-running that verbatim would send the
+ # agent the literal "/work fix it" instead of the skill, so the server
+ # re-expands it — the exact inverse of _skill_scaffold_projection, with the
+ # body never leaving the server.
+ import agent.skill_commands as skill_commands
+ import agent.skill_utils as skill_utils
+ import tools.skills_tool as skills_tool
+
+ skills_dir = tmp_path / "skills"
+ (skills_dir / "worktree-kickoff").mkdir(parents=True)
+ (skills_dir / "worktree-kickoff" / "SKILL.md").write_text(
+ "---\nname: worktree-kickoff\ndescription: Spin up a worktree\n---\n\n"
+ "# kickoff\n\nSPIN UP A WORKTREE, never the primary checkout.\n"
+ )
+ monkeypatch.setattr(skills_tool, "SKILLS_DIR", skills_dir)
+ monkeypatch.setattr(skill_utils, "get_external_skills_dirs", lambda *a, **k: [])
+ monkeypatch.setattr(skill_commands, "_skill_commands", {})
+ monkeypatch.setattr(skill_commands, "_skill_commands_platform", None)
+ skill_commands.scan_skill_commands()
+
+ expanded = server._expand_skill_invocation_for_replay(
+ "/worktree-kickoff fix it", "task-1"
+ )
+
+ assert "SPIN UP A WORKTREE" in expanded
+ assert server._skill_scaffold_projection(expanded) == "/worktree-kickoff fix it"
+
+
+def test_expand_skill_invocation_for_replay_leaves_ordinary_text_alone(monkeypatch):
+ import agent.skill_commands as skill_commands
+ import agent.skill_utils as skill_utils
+
+ monkeypatch.setattr(skill_utils, "get_external_skills_dirs", lambda *a, **k: [])
+ monkeypatch.setattr(skill_commands, "_skill_commands", {})
+ monkeypatch.setattr(skill_commands, "_skill_commands_platform", None)
+
+ assert server._expand_skill_invocation_for_replay("just words", "t") == "just words"
+ # A core slash command is not a skill — nothing to expand.
+ assert server._expand_skill_invocation_for_replay("/status", "t") == "/status"
+
+
+ def test_history_to_messages_types_a_legacy_auto_continue_row():
+ # A crash-interrupted turn used to be typed only AFTER it finished, so a
+ # turn killed a second time (or any row written before turn-start typing
+ # landed) sits in the DB untyped and painted the raw recovery note as a
+ # user bubble. The projection recognizes the synthetic note's fixed
+ # prefix so those rows still read as a timeline event.
+ history = [
+ {"role": "user", "content": "keep going"},
+ {"role": "user", "content": server._auto_continue_note("keep going")},
+ ]
+
+ projected = server._history_to_messages(history)
+
+ assert projected == [
+ {"role": "user", "text": "keep going"},
+ {
+ "role": "user",
+ "text": server._auto_continue_note("keep going"),
+ "display_kind": "auto_continue",
+ },
+ ]
+
+
def test_history_to_messages_keeps_real_user_bracket_text():
# Only role=user rows whose text OPENS with the [System: marker sentinel are
# bookkeeping notices. A genuine user turn that merely mentions the token is
diff --cc tui_gateway/server.py
index 9e78b64e8e4,f6f7241da89..b555504dfe2
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@@ -6238,51 -6237,28 +6238,73 @@@ def _is_display_hidden_marker(role: st
return role == "user" and text.lstrip().startswith("[System:")
+def _skill_scaffold_projection(content_text: str) -> str:
+ """Return the invocation a slash-skill-expanded turn came from, else "".
+
+ A ``/skill`` invocation expands into a model-facing message that embeds the
+ whole skill body. That payload belongs to the agent — every UI renders the
+ invocation (``/work fix the leak``) instead, so no surface can leak the
+ body into a chat bubble.
+ """
+ return describe_skill_invocation(content_text, separator=" ") or ""
+
+
+def _expand_skill_invocation_for_replay(text: str, task_id: str) -> str:
+ """Re-expand a projected `/skill` invocation before re-running that turn.
+
+ The inverse of :func:`_skill_scaffold_projection`. Because a skill turn is
+ displayed as its invocation, a rewind/regenerate hands us back
+ ``/work fix the leak`` rather than the body the agent originally saw —
+ re-running that verbatim would drop the skill. Re-expanding here keeps the
+ body server-side (no client ever holds it) and makes the replayed turn
+ identical to the original.
+
+ Returns *text* unchanged when it isn't a resolvable skill invocation.
+ """
+ head, _, arg = (text or "").strip().partition(" ")
+ if not head.startswith("/"):
+ return text
+
+ try:
+ from agent.skill_commands import (
+ build_skill_invocation_message,
+ resolve_skill_command_key,
+ )
+
+ cmd_key = resolve_skill_command_key(head.lstrip("/"))
+ if cmd_key is None:
+ return text
+
+ return build_skill_invocation_message(cmd_key, arg.strip(), task_id=task_id) or text
+ except Exception:
+ # A skill that no longer resolves (renamed, disabled, external dir
+ # gone) must not break the rewind — replay the text as typed.
+ logger.debug("skill re-expansion failed for replay", exc_info=True)
+ return text
+
+
+ # Opening of the crash-recovery note synthesized by _auto_continue_note.
+ # Matched (not just built) so a row persisted before the display type was
+ # stamped at turn start still reads as a timeline event, and to recognize the
+ # messaging gateway's twin note.
+ _AUTO_CONTINUE_NOTE_PREFIX = "[System note: Your previous turn was interrupted mid-run"
+
+
+ def _legacy_display_kind(role: str, text: str) -> str | None:
+ """Infer the display type of a synthetic row persisted without one.
+
+ Turn-start typing (see ``persist_user_display_kind``) covers everything
+ written from here on. Sessions already on disk carry untyped rows — and a
+ turn killed mid-run never reached the post-turn stamp at all, which is
+ exactly the auto-continue case — so the raw recovery note would paint as a
+ user bubble forever. Sniffing the one fixed synthetic prefix is the
+ migration for those rows; it is not how new rows get typed.
+ """
+ if role == "user" and text.lstrip().startswith(_AUTO_CONTINUE_NOTE_PREFIX):
+ return "auto_continue"
+ return None
+
+
def _history_to_messages(history: list[dict]) -> list[dict]:
messages = []
tool_call_args = {}