feat(8815): a message that reads as another project's work asks the session to check before acting
CI & Build / TypeScript typecheck (push) Successful in 10s
CI & Build / Python lint (push) Successful in 1s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / integration (push) Successful in 2m57s
CI & Build / Build & push image (push) Successful in 35s
CI & Build / TypeScript typecheck (push) Successful in 10s
CI & Build / Python lint (push) Successful in 1s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / integration (push) Successful in 2m57s
CI & Build / Build & push image (push) Successful in 35s
An operator running one session per project can type into the wrong window, and nothing caught it: the message usually asks for work the session can do in its own repo, so the project fence never fires. The prompt lookup (/api/plugin/retrieve) now ranks the operator's own words against every project's records (services/project_check.py). When another project's records clearly win (best match >= 0.78, ahead of this project's by 0.05, with at least 3 of the 12 nearest), a line leads the payload, ahead of every rule, telling the session to ask the operator whether the message was meant for it. Naming the other project halves the margin but never flags alone; naming this project rules a flag out. A message with no subject, or a session with no project, is not checked. Each flag is a new `project_check` gate stop with both scores, asked once per session per other project, read back in retrieval_telemetry's gates block and in the web UI's gate readout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
"""The check on whether the operator's message belongs to the session's project.
|
||||
|
||||
Pins, with the database stubbed:
|
||||
- the nearest records decide: another project flags only when its best match
|
||||
is high, clearly ahead of this project's, and backed by several records;
|
||||
- a name helps but never flags alone, and naming this project rules a flag out;
|
||||
- a session is asked about one other project once, and the ask is recorded;
|
||||
- the line leads the prompt's payload, and a session with no project is never
|
||||
checked.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from quart import Quart, g
|
||||
|
||||
from grimoire.services import project_check as pc
|
||||
|
||||
HERE, OTHER, THIRD = 2, 6, 9
|
||||
TITLES = {HERE: "FabledGrimoire", OTHER: "Steward", THIRD: "Fabled Librarian"}
|
||||
|
||||
|
||||
def _hits(*pairs):
|
||||
return [(score, pid) for score, pid in pairs]
|
||||
|
||||
|
||||
def test_a_message_whose_nearest_records_are_another_projects_is_flagged():
|
||||
verdict = pc.judge(_hits((0.86, OTHER), (0.84, OTHER), (0.83, OTHER), (0.74, HERE)), HERE)
|
||||
assert verdict == pc.Verdict(other_id=OTHER, other_score=0.86, here_score=0.74)
|
||||
|
||||
|
||||
def test_a_message_that_fits_this_project_is_not_flagged():
|
||||
assert pc.judge(_hits((0.86, HERE), (0.85, OTHER), (0.84, OTHER), (0.83, OTHER)), HERE) is None
|
||||
|
||||
|
||||
def test_each_bar_on_its_own_keeps_a_flag_back():
|
||||
# Below the floor: nobody's records match the message well.
|
||||
assert pc.judge(_hits((0.76, OTHER), (0.75, OTHER), (0.75, OTHER)), HERE) is None
|
||||
# Ahead by less than the margin.
|
||||
assert pc.judge(_hits((0.85, OTHER), (0.84, OTHER), (0.84, OTHER), (0.82, HERE)), HERE) is None
|
||||
# One stray record is not a project's worth of support.
|
||||
assert pc.judge(_hits((0.90, OTHER), (0.80, THIRD), (0.79, THIRD), (0.70, HERE)), HERE) is None
|
||||
|
||||
|
||||
def test_records_in_no_project_say_nothing_about_where_a_message_belongs():
|
||||
assert pc.judge(_hits((0.95, None), (0.94, None), (0.93, None)), HERE) is None
|
||||
|
||||
|
||||
def test_a_name_narrows_the_margin_but_naming_this_project_rules_a_flag_out():
|
||||
hits = _hits((0.85, OTHER), (0.84, OTHER), (0.83, OTHER), (0.82, HERE))
|
||||
assert pc.judge(hits, HERE) is None
|
||||
assert pc.judge(hits, HERE, named=frozenset({OTHER})) is not None
|
||||
assert pc.judge(hits, HERE, named=frozenset({OTHER, HERE})) is None
|
||||
|
||||
|
||||
def test_names_are_matched_as_whole_phrases_with_or_without_fabled():
|
||||
assert pc.named_projects("the librarian sidebar flickers", TITLES) == {THIRD}
|
||||
assert pc.named_projects("check Steward's backups", TITLES) == {OTHER}
|
||||
assert pc.named_projects("stewardship of the data", TITLES) == frozenset()
|
||||
|
||||
|
||||
def test_the_line_names_both_projects_and_asks_before_acting():
|
||||
text = pc.line(pc.Verdict(OTHER, 0.86, 0.74), TITLES, HERE)
|
||||
assert "Steward" in text and "FabledGrimoire" in text
|
||||
assert "0.86" in text and "0.74" in text
|
||||
assert "ask them" in text
|
||||
|
||||
|
||||
async def _check(hits, *, asked=False, session="s-1"):
|
||||
record = AsyncMock(return_value=True)
|
||||
notes = [(score, SimpleNamespace(project_id=pid)) for score, pid in hits]
|
||||
with patch("grimoire.services.embeddings.semantic_search_notes", AsyncMock(return_value=notes)), \
|
||||
patch.object(pc, "_titles", AsyncMock(return_value=TITLES)), \
|
||||
patch.object(pc, "_asked_before", AsyncMock(return_value=asked)), \
|
||||
patch("grimoire.services.gate_stops.record", record):
|
||||
out = await pc.check(1, "the backup job missed last night", HERE, session)
|
||||
return out, record
|
||||
|
||||
|
||||
async def test_a_flag_is_recorded_as_a_project_check_ask_with_both_scores():
|
||||
out, record = await _check(_hits((0.86, OTHER), (0.84, OTHER), (0.83, OTHER), (0.74, HERE)))
|
||||
assert out.startswith("> **This message may be meant for another session.**")
|
||||
kw = record.await_args.kwargs
|
||||
assert kw["gate"] == "project_check" and kw["outcome"] == "asked"
|
||||
assert kw["target"] == str(OTHER) and kw["at"] == "0.860 vs 0.740"
|
||||
|
||||
|
||||
async def test_a_session_is_asked_about_one_other_project_once():
|
||||
out, record = await _check(_hits((0.86, OTHER), (0.84, OTHER), (0.83, OTHER)), asked=True)
|
||||
assert out == ""
|
||||
record.assert_not_awaited()
|
||||
|
||||
|
||||
async def test_a_failed_search_lets_the_prompt_through_silently():
|
||||
with patch("grimoire.services.embeddings.semantic_search_notes", AsyncMock(side_effect=RuntimeError)):
|
||||
assert await pc.check(1, "the backup job", HERE, "s-1") == ""
|
||||
assert await pc.check(1, "the backup job", 0, "s-1") == ""
|
||||
|
||||
|
||||
async def _retrieve(query_string, wrong="WRONG PROJECT LINE"):
|
||||
from grimoire.routes import plugin as routes
|
||||
|
||||
check = AsyncMock(return_value=wrong)
|
||||
app = Quart(__name__)
|
||||
async with app.test_request_context("/api/plugin/retrieve", query_string=query_string):
|
||||
g.user = SimpleNamespace(id=7)
|
||||
with patch.object(routes.project_check_svc, "check", check), \
|
||||
patch.object(routes.plugin_ctx_svc, "build_prompt_rule_hint",
|
||||
AsyncMock(return_value={"context": "RULE LINE", "rule_ids": [11],
|
||||
"shown_rule_ids": [11]})), \
|
||||
patch.object(routes.plugin_ctx_svc, "build_autoinject_hint",
|
||||
AsyncMock(return_value={"context": "NOTES MENU", "note_ids": []})), \
|
||||
patch.object(routes.moment_delivery_svc, "deliver_for_turn",
|
||||
AsyncMock(return_value={"context": "", "rule_ids": [], "shape_forms": {}})):
|
||||
resp = await inspect.unwrap(routes.autoinject_retrieve)()
|
||||
return await resp.get_json(), check
|
||||
|
||||
|
||||
async def test_the_flag_leads_the_payload_ahead_of_every_rule():
|
||||
body, check = await _retrieve({"q": "the backup job missed", "project_id": "2", "session_id": "s-1"})
|
||||
assert body["context"].index("WRONG PROJECT LINE") < body["context"].index("RULE LINE")
|
||||
assert check.await_args.args == (7, "the backup job missed", 2, "s-1")
|
||||
|
||||
|
||||
async def test_a_session_with_no_project_or_a_message_with_no_subject_is_not_checked():
|
||||
_body, check = await _retrieve({"q": "the backup job missed"})
|
||||
check.assert_not_awaited()
|
||||
_body, check = await _retrieve({"q": "yes go ahead", "project_id": "2"})
|
||||
check.assert_not_awaited()
|
||||
Reference in New Issue
Block a user