diff --git a/plugin/.claude-plugin/plugin.json b/plugin/.claude-plugin/plugin.json index eb2846fe..afbee0bf 100644 --- a/plugin/.claude-plugin/plugin.json +++ b/plugin/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "grimoire", "description": "Grimoire for Claude Code: connects the grimoire MCP server, adds the hooks that deliver live project state and relevant records at the right moment, ships the shared client-neutral Grimoire skills (using-grimoire, writing-plans, reporting-back, systematic-debugging, verification, brainstorming, reusing-code, shape-accounting, family-canon, reviewing-tests), and syncs your saved Grimoire Processes as skills (/grimoire:sync).", - "version": "2026.12.01.0748", + "version": "2026.12.02.1822", "author": { "name": "Bryan Van Deusen" }, diff --git a/plugin/hooks/grimoire_session_context.sh b/plugin/hooks/grimoire_session_context.sh index 8b730d98..b36237a9 100755 --- a/plugin/hooks/grimoire_session_context.sh +++ b/plugin/hooks/grimoire_session_context.sh @@ -183,6 +183,11 @@ if [ -n "$url" ] && [ -n "$token" ] && command -v curl >/dev/null 2>&1; then q="source=$(printf '%s' "$source" | grimoire_urlenc)" [ -n "$sid_now" ] && q="${q}&session_id=$(printf '%s' "$sid_now" | grimoire_urlenc)" [ -n "$scope" ] && q="${q}&${scope}" + # A marker named the project: send the remote too. The marker still wins, + # and the server says so when the remote is bound to a different project. + case "$scope" in + project_id=*) [ -n "$repo" ] && q="${q}&repo=$(printf '%s' "$repo" | grimoire_urlenc)" ;; + esac q="?${q}" # ONE FETCH, NAMED WHEN IT FAILS. A timeout, an HTTP error and a refused key # need different fixes, so each gets its own sentence. curl's write-out still diff --git a/src/grimoire/routes/plugin.py b/src/grimoire/routes/plugin.py index 0daaa564..b957510d 100644 --- a/src/grimoire/routes/plugin.py +++ b/src/grimoire/routes/plugin.py @@ -116,16 +116,24 @@ async def session_context(): send it when a `.grimoire` marker file names a project. Takes precedence over `repo`. Access-checked like any other read — an id this account cannot read loads no project rather than failing. + Sent with `repo`, the remote's binding is still looked up, and a + binding to a different project is reported in the context. source (optional str) — the host's SessionStart source (startup, resume, compact, clear, fork); decides what the claim section says. session_id (optional str) — the session's id, so a claim bound to it reads as this session's own. """ - project_id, _repo, unbound_repo = await _project_scope() + project_id, repo, unbound_repo = await _project_scope() + # A marker named the project and the hook sent the remote beside it: look + # the remote up too, so a disagreement between the two can be said. + repo_project_id = 0 + if project_id and repo: + repo_project_id = await repo_bindings_svc.resolve_project(g.user.id, repo) or 0 result = await plugin_ctx_svc.build_session_context( g.user.id, project_id, unbound_repo=unbound_repo, source=(request.args.get("source") or "").strip()[:20], session_id=(request.args.get("session_id") or "").strip()[:200], + repo_project_id=repo_project_id, ) return jsonify(result) diff --git a/src/grimoire/services/plugin_context.py b/src/grimoire/services/plugin_context.py index 94be4dce..474e3068 100644 --- a/src/grimoire/services/plugin_context.py +++ b/src/grimoire/services/plugin_context.py @@ -2339,9 +2339,26 @@ def _goal_line(goal: str, project_id: int) -> str: return f"Goal: {short} (full goal: `enter_project({project_id})`)" +async def _marker_conflict_lines(user_id: int, project, repo_project_id: int) -> list[str]: + """The section saying a `.grimoire` marker and the repo's binding name + different projects, and that the marker is the one in force.""" + bound = await projects_svc.get_project(user_id, repo_project_id) + bound_name = f"{bound.title} (id {bound.id})" if bound is not None else f"project {repo_project_id}" + return [ + "", + "## ⚠️ This folder's `.grimoire` marker overrides its repo binding", + f"The marker file names {project.title} (id {project.id}), but this " + f"repo's remote is bound to {bound_name}. The marker wins, so every " + f"hook this session (rules, recall, the project fence) works as " + f"{project.title}, and writes to {bound_name} stop on a permission " + "prompt. If this folder belongs to the bound project, correct the " + "marker's `project_id` or delete the file so the binding decides.", + ] + + async def build_session_context( user_id: int, project_id: int = 0, unbound_repo: str = "", - source: str = "", session_id: str = "", + source: str = "", session_id: str = "", repo_project_id: int = 0, ) -> dict: """Render the SessionStart context for a user, optionally project-scoped. @@ -2360,6 +2377,12 @@ async def build_session_context( claim section says (`task_claims.render_claims`): a compaction gets back the work it had claimed with its latest logs; a new session hears about other sessions' live and abandoned claims; a resume hears nothing. + repo_project_id: the project the working repo's remote is bound to, + when a `.grimoire` marker named `project_id` instead. The marker + wins, but when the two differ the session is told which project + each names: a marker copied along with a repo outlives the + project it was written for, and without this line a session is + held to the old project with nothing saying why. Returns {"context": str, "project": dict | None}. @@ -2389,6 +2412,8 @@ async def build_session_context( _goal_line(goal, project.id), f"Open todo tasks: {open_count}", ] + if repo_project_id and repo_project_id != project.id: + lines += await _marker_conflict_lines(user_id, project, repo_project_id) # A design system binds the same way a rule does, and without this # the standards would reach only an agent that already knew to look diff --git a/tests/test_services_plugin_context.py b/tests/test_services_plugin_context.py index 0d5bd4e6..3114c2f9 100644 --- a/tests/test_services_plugin_context.py +++ b/tests/test_services_plugin_context.py @@ -136,6 +136,59 @@ async def test_build_session_context_includes_project_when_scoped(): assert "Reflex:" not in out["context"] +@pytest.mark.parametrize("repo_project_id, says", [(43, True), (38, False), (0, False)]) +async def test_a_marker_that_disagrees_with_the_repo_binding_is_named(repo_project_id, says): + """A `.grimoire` marker names project 38 while the repo's remote is bound + to 43: the marker wins, and the session is told which project each names + and how to fix the file. Agreeing, or no binding to compare, says nothing.""" + projects = {38: MagicMock(id=38, title="Cartographer", goal="", design_system_id=None), + 43: MagicMock(id=43, title="Throne", goal="", design_system_id=None)} + + async def get_project(_uid, pid): + return projects.get(pid) + + with patch("grimoire.services.plugin_context.projects_svc.get_project", get_project), \ + patch("grimoire.services.plugin_context.notes_svc.list_notes", + AsyncMock(return_value=([], 0))): + from grimoire.services.plugin_context import build_session_context + out = await build_session_context(user_id=7, project_id=38, repo_project_id=repo_project_id) + + assert out["project"] == {"id": 38, "title": "Cartographer"} + ctx = out["context"] + assert ("overrides its repo binding" in ctx) is says + if says: + assert "names Cartographer (id 38)" in ctx and "bound to Throne (id 43)" in ctx + assert "delete the file" in ctx + + +@pytest.mark.parametrize("query, resolves, passed", [ + ({"project_id": "38", "repo": "https://git.example.com/o/r.git"}, 43, 43), + ({"project_id": "38"}, 43, 0), + ({"repo": "https://git.example.com/o/r.git"}, 43, 0), +]) +async def test_the_route_looks_up_the_remote_beside_a_marker(query, resolves, passed): + """With a marker's project_id AND the remote, the route resolves the + remote's binding too and hands it on; the project stays the marker's. + With only one key there is nothing to compare.""" + import inspect + from types import SimpleNamespace + + from quart import Quart, g + + from grimoire.routes import plugin as routes + + build = AsyncMock(return_value={"context": "", "project": None}) + app = Quart(__name__) + async with app.test_request_context("/api/plugin/context", query_string=query): + g.user = SimpleNamespace(id=7) + with patch.object(routes.repo_bindings_svc, "resolve_project", AsyncMock(return_value=resolves)), \ + patch.object(routes.plugin_ctx_svc, "build_session_context", build): + await inspect.unwrap(routes.session_context)() + args, kwargs = build.await_args + assert args[1] == (38 if "project_id" in query else resolves) + assert kwargs["repo_project_id"] == passed + + def test_a_short_goal_is_shown_whole(): from grimoire.services.plugin_context import _goal_line assert _goal_line("ship it", 2) == "Goal: ship it" diff --git a/tests/test_session_context_failure.py b/tests/test_session_context_failure.py index bf6ef10c..5acca812 100644 --- a/tests/test_session_context_failure.py +++ b/tests/test_session_context_failure.py @@ -52,3 +52,22 @@ def test_the_fallback_is_a_first_step_not_an_option(tmp_path): def test_a_marker_names_the_exact_call(tmp_path): out = run_hook(tmp_path, DEAD, marker_project=31) assert "Start by calling `enter_project(31)`" in out + + +def test_a_marker_sends_the_remote_beside_it_so_a_disagreement_can_be_said(tmp_path): + """The marker decides the project, but a marker copied along with a repo + can outlive the project it was written for. The remote goes too, so the + server can compare it with the repo's binding; a folder outside any repo + sends the marker alone.""" + from tests.helpers import http_sink + + need_tools("bash", "curl", "awk", "git") + subprocess.run(["git", "init", "-q", str(tmp_path)], check=True) + subprocess.run(["git", "-C", str(tmp_path), "remote", "add", "origin", + "https://git.example.com/owner/split-off.git"], check=True) + with http_sink(b'{"context":"","project":null}') as (port, seen): + url = f"http://127.0.0.1:{port}" + run_hook(tmp_path, url, marker_project=38) + ask = seen[0] + assert ask["project_id"] == ["38"] + assert ask["repo"] == ["https://git.example.com/owner/split-off.git"]