mirror of
https://github.com/turnstonelabs/turnstone.git
synced 2026-08-12 23:12:23 -06:00
9be155b97a
* Quality overhaul: code tooling, CI/CD, architecture diagrams, UI redesign, and legacy cleanup - Add ruff (lint+format) and mypy (strict) with zero errors across 37 source files - Add GitHub Actions CI (lint, typecheck, test matrix 3.11/3.12/3.13) and PyPI publish workflow - Create 12 PlantUML architecture diagrams with PNG renders covering all subsystems - Refresh README and docs with badges, diagram links, and current descriptions - Refactor test_server_live.py with mock streaming helpers for deterministic CI testing - Update dependencies to current versions (openai>=2.24, httpx>=0.28, redis>=7.2) Console dashboard: - Move state indicators from top cards to fixed bottom status bar with cluster metrics - Replace flat 50-node list with hostname-prefix grouped nodes (expand/collapse, up to 1000) - Apply "Instrument Panel" visual redesign: IBM Plex Mono + Outfit fonts, warm amber accent, LED glow state indicators, deep charcoal surfaces, WCAG AA contrast compliance - Add render cache, stale indicator, active filter highlight, loading states Server web UI: - Apply matching Instrument Panel aesthetic for visual consistency with console - Fix branding (pcode → turnstone), extract inline styles to CSS classes - Rename pcode localStorage keys and history state to turnstone Legacy cleanup: - Remove persona-model-specific --persona flag and /persona slash command - Remove model_identity from chat_template_kwargs (vLLM-specific mechanism) - Refactor plan agent to use standard developer message instead of model_identity - Remove dead code (unused date/has_tools variables, noqa suppressions) * Fix CI typecheck: add mypy overrides for optional sympy/numpy imports The math sandbox optionally imports sympy and numpy at runtime (try/except ImportError). In CI these packages are not installed, so mypy raises import-not-found rather than import-untyped. Add mypy overrides to ignore missing imports for these optional dependencies. * Fix Copilot review findings: ARIA role, status bar cache, and pulse opacity - Change #node-table from role="tree" to role="list" and group elements from role="treeitem" to role="listitem" (proper ARIA semantics) - Include currentView and currentFilter.state in renderStatusBar cache key so active pill highlight updates when switching views - Align pulse animation to 0.35 opacity (already applied in CSS)
265 lines
9.2 KiB
Python
265 lines
9.2 KiB
Python
"""Tests for turnstone.core.session — ChatSession construction."""
|
|
|
|
import json
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from turnstone.core.session import ChatSession
|
|
|
|
|
|
class NullUI:
|
|
"""UI adapter that discards all output. Used for testing."""
|
|
|
|
def on_thinking_start(self):
|
|
pass
|
|
|
|
def on_thinking_stop(self):
|
|
pass
|
|
|
|
def on_reasoning_token(self, text):
|
|
pass
|
|
|
|
def on_content_token(self, text):
|
|
pass
|
|
|
|
def on_stream_end(self):
|
|
pass
|
|
|
|
def approve_tools(self, items):
|
|
return True, None
|
|
|
|
def on_tool_result(self, name, output):
|
|
pass
|
|
|
|
def on_status(self, usage, context_window, effort):
|
|
pass
|
|
|
|
def on_plan_review(self, content):
|
|
return ""
|
|
|
|
def on_info(self, message):
|
|
pass
|
|
|
|
def on_error(self, message):
|
|
pass
|
|
|
|
def on_state_change(self, state):
|
|
pass
|
|
|
|
def on_rename(self, name):
|
|
pass
|
|
|
|
|
|
def _make_session(
|
|
mock_openai_client=None,
|
|
instructions=None,
|
|
**kwargs,
|
|
):
|
|
"""Helper to construct a ChatSession with minimal setup."""
|
|
client = mock_openai_client or MagicMock()
|
|
defaults = dict(
|
|
client=client,
|
|
model="test-model",
|
|
ui=NullUI(),
|
|
instructions=instructions,
|
|
temperature=0.5,
|
|
max_tokens=4096,
|
|
tool_timeout=30,
|
|
)
|
|
defaults.update(kwargs)
|
|
return ChatSession(**defaults)
|
|
|
|
|
|
class TestChatSessionConstruction:
|
|
def test_system_messages_created(self, tmp_db):
|
|
session = _make_session()
|
|
assert len(session.system_messages) >= 1
|
|
# At least one developer message
|
|
roles = [m["role"] for m in session.system_messages]
|
|
assert "developer" in roles
|
|
|
|
def test_instructions_appended_to_developer_message(self, tmp_db):
|
|
session = _make_session(instructions="Always be concise.")
|
|
dev_msgs = [m for m in session.system_messages if m["role"] == "developer"]
|
|
assert len(dev_msgs) >= 1
|
|
assert "Always be concise." in dev_msgs[0]["content"]
|
|
|
|
def test_full_messages_returns_system_plus_conversation(self, tmp_db):
|
|
session = _make_session()
|
|
# Initially no conversation messages
|
|
full = session._full_messages()
|
|
assert len(full) == len(session.system_messages)
|
|
|
|
# Add a user message
|
|
session.messages.append({"role": "user", "content": "hello"})
|
|
full = session._full_messages()
|
|
assert len(full) == len(session.system_messages) + 1
|
|
assert full[-1]["role"] == "user"
|
|
|
|
def test_msg_char_count_content_only(self, tmp_db):
|
|
session = _make_session()
|
|
msg = {"role": "assistant", "content": "hello world"}
|
|
assert session._msg_char_count(msg) == 11
|
|
|
|
def test_msg_char_count_with_tool_calls(self, tmp_db):
|
|
session = _make_session()
|
|
msg = {
|
|
"role": "assistant",
|
|
"content": "hi",
|
|
"tool_calls": [
|
|
{
|
|
"id": "tc_1",
|
|
"function": {
|
|
"name": "bash",
|
|
"arguments": '{"command": "ls"}',
|
|
},
|
|
}
|
|
],
|
|
}
|
|
# "hi" (2) + "bash" (4) + '{"command": "ls"}' (17) = 23
|
|
assert session._msg_char_count(msg) == 23
|
|
|
|
def test_msg_char_count_none_content(self, tmp_db):
|
|
session = _make_session()
|
|
msg = {"role": "assistant", "content": None}
|
|
assert session._msg_char_count(msg) == 0
|
|
|
|
def test_reasoning_effort_stored(self, tmp_db):
|
|
session = _make_session(reasoning_effort="high")
|
|
assert session.reasoning_effort == "high"
|
|
|
|
def test_default_reasoning_effort(self, tmp_db):
|
|
session = _make_session()
|
|
assert session.reasoning_effort == "medium"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Tests — _exec_plan (session-scoped plan files + existing-plan re-read)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestPlanExec:
|
|
"""Tests for _exec_plan: unique session-scoped plan file and existing-plan injection."""
|
|
|
|
def _run_plan(self, session, prompt, agent_return="# Plan\n\nDo the thing."):
|
|
"""Invoke _exec_plan with _run_agent patched to avoid LLM calls.
|
|
|
|
Returns (call_id_returned, content_returned, captured_messages) where
|
|
captured_messages is the agent_messages list passed to _run_agent.
|
|
"""
|
|
captured = {}
|
|
|
|
def fake_run_agent(messages, **kwargs):
|
|
captured["messages"] = list(messages)
|
|
return agent_return
|
|
|
|
item = {"call_id": "test-call-1", "prompt": prompt}
|
|
with patch.object(session, "_run_agent", side_effect=fake_run_agent):
|
|
call_id, content = session._exec_plan(item)
|
|
|
|
return call_id, content, captured.get("messages", [])
|
|
|
|
def test_plan_file_uses_session_id(self, tmp_db, tmp_path, monkeypatch):
|
|
"""Plan file is named .plan-<session_id>.md, not .plan.md."""
|
|
monkeypatch.chdir(tmp_path)
|
|
session = _make_session()
|
|
self._run_plan(session, "add feature")
|
|
expected = tmp_path / f".plan-{session._session_id}.md"
|
|
assert expected.exists(), f"Expected {expected} to be created"
|
|
assert not (tmp_path / ".plan.md").exists()
|
|
|
|
def test_plan_file_contains_agent_output(self, tmp_db, tmp_path, monkeypatch):
|
|
"""Written plan file contains the agent's output verbatim."""
|
|
monkeypatch.chdir(tmp_path)
|
|
session = _make_session()
|
|
plan_content = "## Goal\n\nAdd a new endpoint."
|
|
self._run_plan(session, "add endpoint", agent_return=plan_content)
|
|
plan_file = tmp_path / f".plan-{session._session_id}.md"
|
|
assert plan_file.read_text() == plan_content
|
|
|
|
def test_two_sessions_produce_different_files(self, tmp_db, tmp_path, monkeypatch):
|
|
"""Two ChatSession instances never collide on the same plan file."""
|
|
monkeypatch.chdir(tmp_path)
|
|
s1 = _make_session()
|
|
s2 = _make_session()
|
|
assert s1._session_id != s2._session_id
|
|
self._run_plan(s1, "feature A")
|
|
self._run_plan(s2, "feature B")
|
|
files = list(tmp_path.glob(".plan-*.md"))
|
|
assert len(files) == 2
|
|
|
|
def _seed_prior_plan(self, session, prior_prompt, prior_content):
|
|
"""Simulate a completed plan tool call in session.messages."""
|
|
tc_id = "call_prior_plan"
|
|
session.messages.append(
|
|
{
|
|
"role": "assistant",
|
|
"content": None,
|
|
"tool_calls": [
|
|
{
|
|
"id": tc_id,
|
|
"type": "function",
|
|
"function": {
|
|
"name": "plan",
|
|
"arguments": json.dumps({"prompt": prior_prompt}),
|
|
},
|
|
}
|
|
],
|
|
}
|
|
)
|
|
session.messages.append(
|
|
{
|
|
"role": "tool",
|
|
"tool_call_id": tc_id,
|
|
"content": prior_content,
|
|
}
|
|
)
|
|
|
|
def test_no_prior_plan_no_extra_messages(self, tmp_db, tmp_path, monkeypatch):
|
|
"""First invocation: no prior plan in history, agent gets no tool pair."""
|
|
monkeypatch.chdir(tmp_path)
|
|
session = _make_session()
|
|
_, _, messages = self._run_plan(session, "build something")
|
|
roles = [m["role"] for m in messages]
|
|
assert "tool" not in roles
|
|
|
|
def test_prior_plan_from_messages_injected(self, tmp_db, tmp_path, monkeypatch):
|
|
"""Second invocation: prior plan from session.messages arrives as real tool result."""
|
|
monkeypatch.chdir(tmp_path)
|
|
session = _make_session()
|
|
self._seed_prior_plan(session, "build feature X", "## Goal\n\nOriginal plan.")
|
|
|
|
_, _, messages = self._run_plan(session, "also handle edge case Y")
|
|
|
|
# The real assistant tool_calls message is forwarded
|
|
assistant_with_tc = [
|
|
m for m in messages if m["role"] == "assistant" and m.get("tool_calls")
|
|
]
|
|
assert len(assistant_with_tc) == 1
|
|
assert assistant_with_tc[0]["tool_calls"][0]["function"]["name"] == "plan"
|
|
|
|
# The real tool result is forwarded with its original content
|
|
tool_msgs = [m for m in messages if m["role"] == "tool"]
|
|
assert len(tool_msgs) == 1
|
|
assert "Original plan." in tool_msgs[0]["content"]
|
|
|
|
def test_prior_plan_appears_before_user_prompt(self, tmp_db, tmp_path, monkeypatch):
|
|
"""The prior plan tool pair appears before the new user prompt."""
|
|
monkeypatch.chdir(tmp_path)
|
|
session = _make_session()
|
|
self._seed_prior_plan(session, "original", "Old plan.")
|
|
|
|
_, _, messages = self._run_plan(session, "refinement prompt")
|
|
|
|
tool_idx = next(i for i, m in enumerate(messages) if m["role"] == "tool")
|
|
user_idx = next(i for i, m in enumerate(messages) if m["role"] == "user")
|
|
assert tool_idx < user_idx
|
|
|
|
def test_exec_plan_returns_content(self, tmp_db, tmp_path, monkeypatch):
|
|
"""_exec_plan returns (call_id, agent_output)."""
|
|
monkeypatch.chdir(tmp_path)
|
|
session = _make_session()
|
|
agent_output = "## Goal\n\nBuild it."
|
|
call_id, content, _ = self._run_plan(session, "do stuff", agent_return=agent_output)
|
|
assert call_id == "test-call-1"
|
|
assert content == agent_output
|