CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 9s
CI & Build / TypeScript typecheck (push) Successful in 10s
CI & Build / integration (push) Successful in 18s
CI & Build / Python tests (push) Successful in 46s
CI & Build / Build & push image (push) Successful in 25s
Step 4 of #278, product half. The audit that motivated it: one System in project 2, thirty records tagged, nothing since July 28 — three days after the feature landed. Not a discipline failure; retrieval was completely blind to the association (zero references in embeddings, knowledge, search, auto-inject, or enter_project), so tagging was a write-side label with no read-side payoff, and labels nobody reads don't get maintained. Three changes, ordered by what makes the others workable: 1. enter_project returns the project's Systems (id, name, first line of the charter). Load-bearing for the tagging instruction: you cannot ask an agent to check a record against a vocabulary it never sees. Trimmed because it rides on every session start; the full charter stays get_system's job. Present-and-empty rather than absent when a project has none — "no named areas yet" is information the create-the-System instruction acts on. 2. search accepts system_id, MCP and REST (#33). Implemented once in semantic_search_notes as an EXISTS against record_systems — an association filter deciding candidate-set membership before scoring, like project_id, not a ranking signal. The REST route's missing project filter stays #2463's: it carries a default-scope UI decision this change must not preempt. 3. The instructions (#119, _INSTRUCTIONS + using-scribe skill; plugin 0.1.25 for the cache): - Tag as you write, with an executable test — "would someone investigating that subsystem want this in the pile list_system_records returns?" — rather than "tag appropriately", which is what died. - Create the System when the area has no record: the two-or-more test snippets use, plus "don't wait to be asked to name an area that plainly exists", because the agent's default was leaving un-modelled areas un-modelled forever. - State vs chronicle: dev-logs are written once and never rewritten; durable findings live in the System's reference note, updated in place — safe because note versions are the changelog, which has existed since the feature shipped and was never named as one. list_system_records' docstring now sells it as the way to READ a subsystem, reference note first. No auto-inject boost by System — vocabulary and filter first, measure before adding ranking behaviour (the #2486 lesson). Refs #278, #2546
302 lines
11 KiB
Python
302 lines
11 KiB
Python
"""Tests for fable_*_project tools."""
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from scribe.mcp._context import _user_id_ctx
|
|
from scribe.mcp.tools.projects import (
|
|
list_projects, get_project, create_project,
|
|
update_project, enter_project,
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _bind_user():
|
|
token = _user_id_ctx.set(7)
|
|
yield
|
|
_user_id_ctx.reset(token)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _no_systems():
|
|
"""enter_project now surfaces the project's Systems as the tagging
|
|
vocabulary (#2546). These are tool-layer unit tests with no database, so
|
|
the lookup is stubbed to the common case — a project with none. The
|
|
populated shape is asserted in its own test below.
|
|
"""
|
|
with patch("scribe.mcp.tools.projects.systems_svc.list_systems",
|
|
AsyncMock(return_value=[])):
|
|
yield
|
|
|
|
|
|
def _fake_project(design_system_id=None, **overrides) -> MagicMock:
|
|
p = MagicMock()
|
|
base = {"id": 1, "title": "P", "description": "", "goal": "",
|
|
"status": "active", "color": None}
|
|
base.update(overrides)
|
|
p.to_dict.return_value = base
|
|
# Explicit, because a bare MagicMock hands back a truthy auto-attribute —
|
|
# which would route every project in this file through the design-system
|
|
# branch and out to a real database.
|
|
p.design_system_id = design_system_id
|
|
return p
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_projects_wraps_in_dict():
|
|
rows = [_fake_project(id=1), _fake_project(id=2)]
|
|
with patch(
|
|
"scribe.mcp.tools.projects.projects_svc.list_projects",
|
|
AsyncMock(return_value=rows),
|
|
):
|
|
out = await list_projects()
|
|
assert len(out["projects"]) == 2
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_project_enriches_with_milestone_summary():
|
|
p = _fake_project(id=5, title="found")
|
|
milestone_summary = [{"id": 10, "title": "MS", "task_count": 3}]
|
|
applicable_payload = {
|
|
"rules": [], "truncated": False, "subscribed_rulebooks": [],
|
|
}
|
|
with patch(
|
|
"scribe.mcp.tools.projects.projects_svc.get_project",
|
|
AsyncMock(return_value=p),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.milestones_svc.get_project_milestone_summary",
|
|
AsyncMock(return_value=milestone_summary),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.rulebooks_svc.get_applicable_rules",
|
|
AsyncMock(return_value=applicable_payload),
|
|
):
|
|
out = await get_project(project_id=5)
|
|
assert out["id"] == 5
|
|
assert out["milestone_summary"] == milestone_summary
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_project_includes_applicable_rules_and_subscribed_rulebooks():
|
|
"""The augmented get_project response includes applicable_rules and
|
|
subscribed_rulebooks pulled from services/rulebooks.get_applicable_rules.
|
|
"""
|
|
p = _fake_project(id=3, title="Fabled Assistant")
|
|
milestone_summary = []
|
|
applicable_payload = {
|
|
"rules": [
|
|
{"id": 1, "title": "dev is home",
|
|
"statement": "Work directly on dev",
|
|
"topic_title": "git-workflow",
|
|
"rulebook_title": "FabledSword family"},
|
|
],
|
|
"truncated": False,
|
|
"subscribed_rulebooks": [{"id": 1, "title": "FabledSword family"}],
|
|
}
|
|
with patch(
|
|
"scribe.mcp.tools.projects.projects_svc.get_project",
|
|
AsyncMock(return_value=p),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.milestones_svc.get_project_milestone_summary",
|
|
AsyncMock(return_value=milestone_summary),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.rulebooks_svc.get_applicable_rules",
|
|
AsyncMock(return_value=applicable_payload),
|
|
):
|
|
out = await get_project(project_id=3)
|
|
assert out["applicable_rules"][0]["title"] == "dev is home"
|
|
assert out["subscribed_rulebooks"] == [{"id": 1, "title": "FabledSword family"}]
|
|
assert out["applicable_rules_truncated"] is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_project_raises_when_not_found():
|
|
with patch(
|
|
"scribe.mcp.tools.projects.projects_svc.get_project",
|
|
AsyncMock(return_value=None),
|
|
):
|
|
with pytest.raises(ValueError, match="project 999 not found"):
|
|
await get_project(project_id=999)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_project_passes_color_empty_as_none():
|
|
p = _fake_project()
|
|
mock = AsyncMock(return_value=p)
|
|
with patch("scribe.mcp.tools.projects.projects_svc.create_project", mock):
|
|
await create_project(title="P", color="")
|
|
assert mock.call_args.kwargs["color"] is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_project_only_sends_non_default_fields():
|
|
p = _fake_project()
|
|
mock = AsyncMock(return_value=p)
|
|
with patch("scribe.mcp.tools.projects.projects_svc.update_project", mock):
|
|
await update_project(project_id=1, status="archived")
|
|
args, kwargs = mock.call_args
|
|
assert args == (7, 1)
|
|
assert kwargs == {"status": "archived"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_project_raises_when_not_found():
|
|
with patch(
|
|
"scribe.mcp.tools.projects.projects_svc.update_project",
|
|
AsyncMock(return_value=None),
|
|
):
|
|
with pytest.raises(ValueError, match="project 999 not found"):
|
|
await update_project(project_id=999, title="x")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_enter_project_composes_full_context():
|
|
"""enter_project pulls project + rules + milestone summary + open tasks +
|
|
recent notes in one composed call."""
|
|
p = _fake_project(id=5, title="P")
|
|
applicable_payload = {
|
|
"rules": [{"id": 1, "title": "r1", "statement": "s",
|
|
"topic_title": "t", "rulebook_title": "rb"}],
|
|
"project_rules": [{"id": 99, "title": "pr1", "statement": "ps"}],
|
|
"truncated": False,
|
|
"subscribed_rulebooks": [{"id": 2, "title": "rb"}],
|
|
}
|
|
milestone_summary = [{"id": 10, "title": "MS", "task_count": 3}]
|
|
|
|
task1 = MagicMock()
|
|
task1.id = 100; task1.title = "T1"; task1.status = "in_progress"
|
|
task1.priority = "high"; task1.task_kind = "work"; task1.milestone_id = 10
|
|
note1 = MagicMock()
|
|
note1.id = 200; note1.title = "N1"
|
|
note1.updated_at = None # avoids datetime mocking
|
|
|
|
with patch(
|
|
"scribe.mcp.tools.projects.projects_svc.get_project",
|
|
AsyncMock(return_value=p),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.rulebooks_svc.get_applicable_rules",
|
|
AsyncMock(return_value=applicable_payload),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.milestones_svc.get_project_milestone_summary",
|
|
AsyncMock(return_value=milestone_summary),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.notes_svc.list_notes",
|
|
AsyncMock(side_effect=[([task1], 1), ([note1], 1)]),
|
|
):
|
|
out = await enter_project(project_id=5)
|
|
|
|
assert out["project"]["id"] == 5
|
|
assert out["milestone_summary"] == milestone_summary
|
|
assert out["applicable_rules"][0]["title"] == "r1"
|
|
assert out["project_rules"][0]["id"] == 99
|
|
assert out["subscribed_rulebooks"] == [{"id": 2, "title": "rb"}]
|
|
assert out["open_tasks"][0]["id"] == 100
|
|
assert out["open_tasks"][0]["status"] == "in_progress"
|
|
assert out["recent_notes"][0]["id"] == 200
|
|
# No design system on this project -> the key is present and null, not
|
|
# absent. A caller that has to distinguish "no key" from "no system" will
|
|
# eventually get it wrong.
|
|
assert out["design_system"] is None
|
|
# No Systems -> present-and-empty, NOT absent: this key is the tagging
|
|
# vocabulary, and "this project has no named areas yet" is information the
|
|
# create-the-System instruction acts on.
|
|
assert out["systems"] == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_enter_project_surfaces_the_systems_vocabulary():
|
|
"""The tagging instruction is only executable if the vocabulary is in
|
|
front of the agent when it writes. It never was, and tagging stopped three
|
|
days after the feature landed — one System, nothing tagged since July 28
|
|
(#2546's audit). Trimmed to id/name/first-line: it rides on every session
|
|
start, and the full charter is get_system's job."""
|
|
p = _fake_project(id=5)
|
|
sys1 = MagicMock()
|
|
sys1.id = 3
|
|
sys1.name = "retrieval"
|
|
sys1.description = "Embeddings, ranking, auto-inject.\nLong detail below."
|
|
|
|
with patch(
|
|
"scribe.mcp.tools.projects.projects_svc.get_project",
|
|
AsyncMock(return_value=p),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.rulebooks_svc.get_applicable_rules",
|
|
AsyncMock(return_value={"rules": [], "truncated": False,
|
|
"subscribed_rulebooks": []}),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.milestones_svc.get_project_milestone_summary",
|
|
AsyncMock(return_value=[]),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.notes_svc.list_notes",
|
|
AsyncMock(side_effect=[([], 0), ([], 0)]),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.systems_svc.list_systems",
|
|
AsyncMock(return_value=[sys1]),
|
|
):
|
|
out = await enter_project(project_id=5)
|
|
|
|
assert out["systems"] == [
|
|
{"id": 3, "name": "retrieval",
|
|
"description": "Embeddings, ranking, auto-inject."}
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_enter_project_hands_back_the_design_system_when_the_project_has_one():
|
|
"""The handshake is where an agent learns what binds it, and a design
|
|
system binds the same way a rule does. Before this it was reachable only by
|
|
an agent that already knew to call resolve_design_system — so the standards
|
|
were present in the store and absent from the work."""
|
|
p = _fake_project(id=5, design_system_id=9)
|
|
design = {"id": 9, "title": "App kit", "guidance": [{"title": "House"}],
|
|
"token_count": 95, "token_groups": ["surface"],
|
|
"inherits_from": ["House"], "description": ""}
|
|
|
|
with patch(
|
|
"scribe.mcp.tools.projects.projects_svc.get_project",
|
|
AsyncMock(return_value=p),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.rulebooks_svc.get_applicable_rules",
|
|
AsyncMock(return_value={"rules": [], "truncated": False,
|
|
"subscribed_rulebooks": []}),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.milestones_svc.get_project_milestone_summary",
|
|
AsyncMock(return_value=[]),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.notes_svc.list_notes",
|
|
AsyncMock(side_effect=[([], 0), ([], 0)]),
|
|
), patch(
|
|
"scribe.mcp.tools.projects.design_systems_svc.design_context",
|
|
AsyncMock(return_value=design),
|
|
) as ctx:
|
|
out = await enter_project(project_id=5)
|
|
|
|
assert out["design_system"]["token_count"] == 95
|
|
assert out["design_system"]["inherits_from"] == ["House"]
|
|
assert ctx.await_args.args == (7, 9) # caller's id, the project's system
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_enter_project_raises_when_project_not_found():
|
|
with patch(
|
|
"scribe.mcp.tools.projects.projects_svc.get_project",
|
|
AsyncMock(return_value=None),
|
|
):
|
|
with pytest.raises(ValueError, match="project 999 not found"):
|
|
await enter_project(project_id=999)
|
|
|
|
|
|
def test_enter_project_registered_in_register():
|
|
"""register(mcp) registers enter_project alongside the existing tools."""
|
|
from scribe.mcp.tools.projects import register
|
|
registered: list[str] = []
|
|
|
|
class FakeMCP:
|
|
def tool(self, name=None):
|
|
def decorator(fn):
|
|
registered.append(name)
|
|
return fn
|
|
return decorator
|
|
|
|
register(FakeMCP())
|
|
assert "enter_project" in registered
|