diff --git a/src/scribe/services/plugin_context.py b/src/scribe/services/plugin_context.py index 7241838..4f1b4f3 100644 --- a/src/scribe/services/plugin_context.py +++ b/src/scribe/services/plugin_context.py @@ -163,6 +163,22 @@ async def get_autoinject_config(user_id: int) -> dict: return {"enabled": enabled, "threshold": threshold, "top_k": top_k} +def _record_kind(note) -> str: + """The one-word kind marker for an injected menu line. + + The menu is drawn from every record that carries an embedding, so a snippet, + a stored process, an issue and a stray dev-log all arrive looking identical. + Recorded prior art only stands out if the line says what it is — and the kind + is also what tells the reader which tool opens it. + + Task-ness wins over `note_type` because it's the more useful distinction at a + glance: "there's an open issue about this" beats "there's a note about this". + """ + if note.is_task: + return "issue" if note.task_kind == "issue" else "task" + return note.note_type or "note" + + async def build_autoinject_hint( user_id: int, query: str, @@ -175,7 +191,7 @@ async def build_autoinject_hint( 1. high-confidence threshold (stricter than pull) — set per-user; 2. margin gate — keep only hits within _AUTOINJECT_BAND of the top score; 3. session dedup — caller passes already-injected ids as `exclude_ids`; - 4. title-first payload — id + title + score only, never bodies. + 4. title-first payload — id + kind + title + score only, never bodies. Disabled, blank-query, or nothing-clears-the-gates all return empty context, so most turns inject nothing. @@ -222,15 +238,19 @@ async def build_autoinject_hint( int(n.user_id) for _s, n in kept if n.user_id != user_id }) + # "records", not "notes" — the menu can hold snippets, processes and tasks + # too, and the kind marker on each line is only legible if the header doesn't + # already claim they're all one thing. lines = [ - "> Possibly relevant from your Scribe notes — call `get_note(id)` to " - "open any in full (titles only; injected once per session):", + "> Possibly relevant from your Scribe records — open any in full with " + "`get_note(id)`, or `get_snippet` / `get_process` for those kinds " + "(titles only; injected once per session):", ] note_ids: list[int] = [] for score, note in kept: note_ids.append(int(note.id)) title = (note.title or "(untitled)").replace("\n", " ").strip() - line = f"> - #{note.id} \"{title}\" ({score:.2f})" + line = f"> - #{note.id} [{_record_kind(note)}] \"{title}\" ({score:.2f})" if note.user_id != user_id: who = owners.get(int(note.user_id)) or "another user" line += f" — shared by {who}, treat as a suggestion" diff --git a/tests/test_retrieval_scopes.py b/tests/test_retrieval_scopes.py index 5eeaf29..478edce 100644 --- a/tests/test_retrieval_scopes.py +++ b/tests/test_retrieval_scopes.py @@ -17,15 +17,19 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest -def _note(id=1, user_id=7, title="A note"): +def _note(id=1, user_id=7, title="A note", note_type="note", + is_task=False, task_kind="work"): n = MagicMock() n.id = id n.user_id = user_id n.title = title n.body = "body" n.tags = [] - n.is_task = False - n.note_type = "note" + # Real values, not auto-attributes: the menu reads these to label each line, + # and a MagicMock is truthy — every record would render as a task (note 2109). + n.is_task = is_task + n.task_kind = task_kind + n.note_type = note_type return n @@ -146,3 +150,39 @@ async def test_injected_menu_attributes_another_users_note(): assert "suggestion" in theirs_line # The operator's own note stays unadorned — absence of a marker is the signal. assert "shared by" not in mine_line + + +@pytest.mark.asyncio +async def test_injected_menu_labels_the_record_kind(): + """Every injected line renders the same shape, so without a kind marker a + recorded snippet is indistinguishable from a stray dev-log — exactly where + prior art most needs to stand out.""" + from scribe.services import plugin_context + + hits = [ + (0.92, _note(1, title="debounce — rate-limit a callback", note_type="snippet")), + (0.91, _note(2, title="Release checklist", note_type="process")), + (0.90, _note(3, title="Auth token expiry", is_task=True, task_kind="issue")), + (0.89, _note(4, title="Ship the drafter", is_task=True)), + (0.88, _note(5, title="Why we dropped CalDAV")), + ] + with patch.object(plugin_context, "semantic_search_notes", + AsyncMock(return_value=hits)), \ + patch.object(plugin_context, "get_autoinject_config", + AsyncMock(return_value={"enabled": True, "threshold": 0.5, + "top_k": 5})), \ + patch.object(plugin_context, "record_retrieval", MagicMock()): + out = await plugin_context.build_autoinject_hint(7, "anything") + + lines = out["context"].splitlines() + by_id = {n: next(ln for ln in lines if f"#{n} " in ln) for n in (1, 2, 3, 4, 5)} + assert "[snippet]" in by_id[1] + assert "[process]" in by_id[2] + # Task-ness wins over note_type, and an issue says so rather than "task". + assert "[issue]" in by_id[3] + assert "[task]" in by_id[4] + assert "[note]" in by_id[5] + # Still title-first: the marker is metadata, not an excuse to carry bodies. + assert "body" not in out["context"] + # The header can't claim they're all notes when the markers say otherwise. + assert "Scribe records" in lines[0] diff --git a/tests/test_services_plugin_context.py b/tests/test_services_plugin_context.py index 6281a47..42c19b6 100644 --- a/tests/test_services_plugin_context.py +++ b/tests/test_services_plugin_context.py @@ -10,13 +10,15 @@ def _rule(rid, title, topic_id): return r -def _note(nid, title, user_id=1): +def _note(nid, title, user_id=1, note_type="note", is_task=False, task_kind="work"): n = MagicMock() n.id, n.title = nid, title - # Real int, defaulting to the caller used in these tests: the injected menu - # compares it to decide whether the line needs a "shared by …" attribution, - # and an auto-MagicMock would read as another user's note. + # Real values, defaulting to the caller used in these tests: the injected menu + # compares user_id to decide whether the line needs a "shared by …" + # attribution, and reads is_task/task_kind/note_type for the kind marker. An + # auto-MagicMock is truthy, so every line would read as another user's task. n.user_id = user_id + n.note_type, n.is_task, n.task_kind = note_type, is_task, task_kind return n @@ -79,7 +81,7 @@ async def test_build_autoinject_hint_titles_only_with_margin_gate(): exclude_ids=[99]) # Margin gate kept the top two, dropped the straggler. assert out["note_ids"] == [11, 22] - assert '#11 "Pool sizing decision" (0.80)' in out["context"] + assert '#11 [note] "Pool sizing decision" (0.80)' in out["context"] assert "#33" not in out["context"] # Title-first: no body text, ever. assert "get_note(id)" in out["context"]