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

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:
2026-12-05 22:21:49 -05:00
co-authored by Claude Opus 5.5
parent 72ef1bb45d
commit 58991038d8
8 changed files with 386 additions and 5 deletions
+32 -3
View File
@@ -29,7 +29,8 @@ from sqlalchemy import and_, case, delete, exists, func, select, update
from grimoire.models import async_session
from grimoire.models.gate_stop import (
ASKED, FENCE, GATES, LET_THROUGH, OUTCOMES, STOPPED, GateStop,
ASKED, FENCE, GATES, LET_THROUGH, OUTCOMES, PROJECT_CHECK, RULELESS_GATES, STOPPED,
GateStop,
)
from grimoire.models.rule_adherence import MISFIRE, RuleAdherence
from grimoire.models.rulebook import Rule
@@ -59,7 +60,7 @@ async def record(
sid = rule_deliveries.clean_session(session_id)
if not sid or gate not in GATES or outcome not in OUTCOMES or not _field(tool):
return False
if gate != FENCE and not rule_id:
if gate not in RULELESS_GATES and not rule_id:
return False
async with async_session() as session:
session.add(GateStop(
@@ -112,6 +113,21 @@ async def mark_ran(user_id: int, session_id: str | None, call_id: str | None) ->
return bool(result.rowcount)
async def asked(user_id: int, session_id: str | None, *, gate: str, target: str) -> bool:
"""Whether this gate already stood in front of this session about this
target, so a session is asked about one other project once."""
sid = rule_deliveries.clean_session(session_id)
if not sid:
return False
async with async_session() as session:
return (await session.execute(
select(exists().where(
GateStop.user_id == user_id, GateStop.session_id == sid,
GateStop.gate == gate, GateStop.target == _field(target),
))
)).scalar_one()
def _misfired():
"""Whether the session reported the stopped rule as a misfire."""
return exists().where(
@@ -139,7 +155,9 @@ async def summary(user_id: int, days: int = 30, *, since: datetime | None = None
for the fence, the operator's yes) and `sessions`.
`by_rule`: the same per rule and place, most stops first. `fence`: the
asks per tool and target project. `complete_from` is the first stop ever
asks per tool and target project. `project_check`: per other project, how
often a message was flagged as reading like its work rather than the
session's. `complete_from` is the first stop ever
recorded, so a window reaching further back says so.
"""
until = until or datetime.now(timezone.utc)
@@ -180,6 +198,12 @@ async def summary(user_id: int, days: int = 30, *, since: datetime | None = None
.group_by(GateStop.tool, GateStop.target)
.order_by(func.count(GateStop.id).desc(), GateStop.tool)
)).all()
check_rows = (await session.execute(
select(GateStop.target, func.count(GateStop.id), func.max(GateStop.stopped_at))
.where(window, GateStop.gate == PROJECT_CHECK)
.group_by(GateStop.target)
.order_by(func.count(GateStop.id).desc(), GateStop.target)
)).all()
first = (await session.execute(
select(func.min(GateStop.stopped_at)).where(GateStop.user_id == user_id)
)).scalar_one_or_none()
@@ -205,6 +229,11 @@ async def summary(user_id: int, days: int = 30, *, since: datetime | None = None
{"tool": tool, "target": target, "asks": int(n or 0), "ran": int(ran or 0)}
for tool, target, n, ran in fence_rows
],
"project_check": [
{"target": target, "asks": int(n or 0),
"last_asked_at": last.isoformat() if last else None}
for target, n, last in check_rows
],
"complete_from": first.isoformat() if first else None,
"covers_window": None if first is None else first <= since,
}