Files
FabledGrimoire/tests/test_session_context_failure.py
T
bvandeusenandClaude Opus 5.5 f4d6486255
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / Python lint (push) Successful in 2s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Failing after 1m58s
CI & Build / integration (push) Successful in 1m58s
CI & Build / Build & push image (push) Skipped
fix(8321): a .grimoire marker that disagrees with the repo's binding is named at session start
A marker copied along with a repo outlived the project it was written for:
the Throne folder's marker still named project 38 after the split, so every
hook held the session to Cartographer and writes to Throne stopped on the
project fence, with nothing saying why.

The SessionStart hook now sends the remote beside a marker's project_id. The
marker still decides the project; /context also resolves the remote's binding,
and when it names a different project the context says which project each
names and how to fix the file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 13:22:50 -05:00

74 lines
3.0 KiB
Python

"""A SessionStart that loads no project says why, and names the first move.
A fetch written as `curl -f … || body=""` collapses a timeout, an HTTP error
and a refused key into one sentence, and a fallback that suggests
`enter_project()` "as needed" reads as optional. A session that skips it starts
with no recent milestones or open tasks, and so has no way to know which prior
work exists to look for. These pin both halves on the path that needs no
server: the cause is named, and the fallback is a concrete first step.
"""
from __future__ import annotations
import json
import os
import subprocess
from pathlib import Path
from tests.helpers import need_tools
HOOK = Path(__file__).resolve().parents[1] / "plugin" / "hooks" / "grimoire_session_context.sh"
def run_hook(tmp_path: Path, url: str, marker_project: int | None = None) -> str:
need_tools("bash", "curl", "awk")
if marker_project is not None:
(tmp_path / ".grimoire").write_text(json.dumps({
"instance": url, "project_id": marker_project, "project": "P",
}))
env = {**os.environ, "GRIMOIRE_URL": url, "GRIMOIRE_TOKEN": "t",
"CLAUDE_PROJECT_DIR": str(tmp_path)}
r = subprocess.run(["bash", str(HOOK)], input=b'{"source":"startup"}',
capture_output=True, env=env, timeout=60)
assert r.returncode == 0, r.stderr.decode()
return json.loads(r.stdout)["hookSpecificOutput"]["additionalContext"]
# Port 9 (discard) on loopback: refused at once, so both tries fail fast.
DEAD = "http://127.0.0.1:9"
def test_an_unreachable_instance_is_named_as_one(tmp_path):
out = run_hook(tmp_path, DEAD)
assert "could not be reached (curl exit 7, after a retry)" in out
def test_the_fallback_is_a_first_step_not_an_option(tmp_path):
out = run_hook(tmp_path, DEAD)
assert "as needed" not in out
assert "Start by finding this repo's project" in out
assert "before any other work" in out
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"]