8fe571e175
The SessionStart hook asked for a project_id via plugin userConfig, which pins one install to a single project — wrong for an operator working across many repos/projects. Resolve the active project server-side from the working repo's git remote instead (a stable identifier, not a dir-name guess). - repo_bindings table (migration 0064) + RepoBinding model: (user, repo_key) -> project, FKs CASCADE. - services/repo_bindings: normalize_repo_key collapses ssh/https/scp/creds/port/ .git to host/owner/repo; resolve/set/list/delete. - GET /api/plugin/context takes ?repo=<remote>; unbound repo -> a "bind this repo" hint with a ready bind_repo() call. project_id kept as manual override. - MCP tools: bind_repo / list_repo_bindings / unbind_repo. - Hook sends ?repo=$(git remote get-url origin) URL-encoded; all project_id handling removed. plugin.json drops the project_id userConfig (0.1.2 -> 0.1.3). - Tests: normalize equivalence classes + unbound-hint rendering. Refs task 755 (Scribe-as-plugin push channel). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
89 lines
3.7 KiB
Python
89 lines
3.7 KiB
Python
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
def _rule(rid, title, topic_id):
|
|
r = MagicMock()
|
|
r.id, r.title, r.topic_id = rid, title, topic_id
|
|
r.statement = "FULL STATEMENT SHOULD NOT BE INJECTED"
|
|
return r
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_session_context_renders_titles_grouped_by_topic():
|
|
rules = [
|
|
_rule(1, "`dev` is home", 1),
|
|
_rule(2, "Release — never without explicit request", 1),
|
|
_rule(3, "No GitHub — Fabled-Git only", 2),
|
|
]
|
|
with patch("scribe.services.plugin_context.rulebooks_svc.list_always_on_rules",
|
|
AsyncMock(return_value=rules)), \
|
|
patch("scribe.services.plugin_context._topic_titles",
|
|
AsyncMock(return_value={1: "git-workflow", 2: "fabled-git"})):
|
|
from scribe.services.plugin_context import build_session_context
|
|
out = await build_session_context(user_id=7, project_id=0)
|
|
|
|
ctx = out["context"]
|
|
assert out["rule_count"] == 3
|
|
assert out["project"] is None
|
|
# Titles present, grouped under topic headings
|
|
assert "### git-workflow" in ctx
|
|
assert "### fabled-git" in ctx
|
|
assert "- [1] `dev` is home" in ctx
|
|
assert "- [3] No GitHub — Fabled-Git only" in ctx
|
|
# Full statements must NOT be dumped (push channel injects titles only)
|
|
assert "FULL STATEMENT SHOULD NOT BE INJECTED" not in ctx
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_session_context_includes_project_when_scoped():
|
|
project = MagicMock(id=2, title="FabledScribe", goal="ship it")
|
|
with patch("scribe.services.plugin_context.rulebooks_svc.list_always_on_rules",
|
|
AsyncMock(return_value=[_rule(1, "rule", 1)])), \
|
|
patch("scribe.services.plugin_context._topic_titles",
|
|
AsyncMock(return_value={1: "git-workflow"})), \
|
|
patch("scribe.services.plugin_context.projects_svc.get_project",
|
|
AsyncMock(return_value=project)), \
|
|
patch("scribe.services.plugin_context.notes_svc.list_notes",
|
|
AsyncMock(return_value=([], 4))):
|
|
from scribe.services.plugin_context import build_session_context
|
|
out = await build_session_context(user_id=7, project_id=2)
|
|
|
|
assert out["project"] == {"id": 2, "title": "FabledScribe"}
|
|
assert "## Active project: FabledScribe (id 2)" in out["context"]
|
|
assert "Open todo tasks: 4" in out["context"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_session_context_unbound_repo_emits_bind_hint():
|
|
with patch("scribe.services.plugin_context.rulebooks_svc.list_always_on_rules",
|
|
AsyncMock(return_value=[_rule(1, "rule", 1)])), \
|
|
patch("scribe.services.plugin_context._topic_titles",
|
|
AsyncMock(return_value={1: "git-workflow"})):
|
|
from scribe.services.plugin_context import build_session_context
|
|
out = await build_session_context(
|
|
user_id=7, project_id=0, unbound_repo="host/owner/repo",
|
|
)
|
|
|
|
ctx = out["context"]
|
|
assert out["project"] is None
|
|
assert "## Repository not yet bound" in ctx
|
|
assert 'bind_repo(repo_url="host/owner/repo"' in ctx
|
|
# No project block when unbound.
|
|
assert "## Active project" not in ctx
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_session_context_caps_length():
|
|
many = [_rule(i, "x" * 200, 1) for i in range(200)]
|
|
with patch("scribe.services.plugin_context.rulebooks_svc.list_always_on_rules",
|
|
AsyncMock(return_value=many)), \
|
|
patch("scribe.services.plugin_context._topic_titles",
|
|
AsyncMock(return_value={1: "git-workflow"})):
|
|
from scribe.services.plugin_context import build_session_context
|
|
out = await build_session_context(user_id=7)
|
|
|
|
assert len(out["context"]) <= 9000 + 60 # cap + truncation note
|
|
assert "truncated" in out["context"]
|