[F011] playbook: de-index rejected/archived playbooks from the PLAYBOOKS RAG index

This commit is contained in:
Renn F
2026-06-28 10:38:28 +02:00
parent fc3ec109e7
commit 7cba2f9793
6 changed files with 248 additions and 1 deletions
@@ -38,3 +38,24 @@ def test_build_source_uri_with_id() -> None:
def test_build_source_uri_none_when_missing() -> None:
assert _plugin().build_source_uri(doc_id=None) is None
def test_delete_playbook_removes_its_chunks_by_source() -> None:
"""F011: deleting a playbook removes its embedded chunks from the vector
store by the playbook's source URI (idempotent — no-op if absent). A
rejected/archived playbook must not stay retrievable in the PLAYBOOKS index."""
from unittest.mock import AsyncMock, MagicMock
plugin = PlaybooksIndexPlugin.__new__(PlaybooksIndexPlugin)
store = MagicMock()
store.delete_by_source = AsyncMock(return_value=None)
# Bypass the initialized-guard property so the unit test doesn't need a
# live pgvector store.
object.__setattr__(plugin, "_initialized", True)
object.__setattr__(plugin, "_store", store)
import asyncio
asyncio.run(plugin.delete_playbook("pb-42"))
store.delete_by_source.assert_awaited_once_with("roboco://playbooks/pb-42")
@@ -0,0 +1,128 @@
"""F011 — PlaybookService.reject must de-index the playbook.
A rejected/archived playbook that was previously approved stays in the
PLAYBOOKS RAG index (no de-index on reject/archive), so it keeps surfacing
in agent briefings as a stale, no-longer-canonical procedure. The fix
mirrors the index-on-approve path: ``reject`` calls an
``_unindex_playbook`` helper (gated on ``org_memory_enabled``, best-effort)
that removes the playbook's chunks + tracking row.
"""
from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock, patch
from uuid import uuid4
import pytest
from roboco.config import settings
from roboco.models.base import PlaybookStatus
from roboco.services.playbook import PlaybookService
def _mock_playbook(playbook_id, *, status=PlaybookStatus.APPROVED.value):
pb = MagicMock()
pb.id = playbook_id
pb.status = status
pb.title = "Retry a flaky pg test"
pb.problem = "..."
pb.procedure = "..."
pb.tags = ["backend"]
pb.team = "backend"
pb.scope = "org"
return pb
@pytest.mark.asyncio
async def test_reject_deindexes_approved_playbook(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Rejecting an approved (indexed) playbook removes it from the index."""
monkeypatch.setattr(settings, "org_memory_enabled", True)
playbook_id = uuid4()
pb = _mock_playbook(playbook_id, status=PlaybookStatus.APPROVED.value)
session = MagicMock()
# _get_or_raise returns the playbook; flush is a no-op.
result = MagicMock()
result.scalar_one_or_none.return_value = pb
session.execute = AsyncMock(return_value=result)
session.flush = AsyncMock()
optimal = MagicMock()
optimal.unindex_playbook = AsyncMock(return_value=None)
svc = PlaybookService(session)
with (
patch(
"roboco.services.optimal.get_optimal_service",
AsyncMock(return_value=optimal),
),
):
out = await svc.reject(playbook_id, approver_id=uuid4(), reason="stale")
assert out.status == PlaybookStatus.ARCHIVED.value
optimal.unindex_playbook.assert_awaited_once_with(str(playbook_id))
@pytest.mark.asyncio
async def test_reject_of_unindexed_draft_does_not_error(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A draft (never indexed) reject still de-indexes (idempotent no-op) and
never errors the curation."""
monkeypatch.setattr(settings, "org_memory_enabled", True)
playbook_id = uuid4()
pb = _mock_playbook(playbook_id, status=PlaybookStatus.DRAFT.value)
session = MagicMock()
result = MagicMock()
result.scalar_one_or_none.return_value = pb
session.execute = AsyncMock(return_value=result)
session.flush = AsyncMock()
optimal = MagicMock()
optimal.unindex_playbook = AsyncMock(return_value=None)
svc = PlaybookService(session)
with (
patch(
"roboco.services.optimal.get_optimal_service",
AsyncMock(return_value=optimal),
),
):
out = await svc.reject(playbook_id, approver_id=uuid4(), reason="nope")
assert out.status == PlaybookStatus.ARCHIVED.value
# De-index is still called (idempotent — the store no-ops on an absent
# source), so a draft reject mirrors the approved reject path uniformly.
optimal.unindex_playbook.assert_awaited_once_with(str(playbook_id))
@pytest.mark.asyncio
async def test_reject_skips_deindex_when_org_memory_disabled(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""org_memory_enabled=False ⇒ the index is inert; reject must not touch it."""
monkeypatch.setattr(settings, "org_memory_enabled", False)
playbook_id = uuid4()
pb = _mock_playbook(playbook_id)
session = MagicMock()
result = MagicMock()
result.scalar_one_or_none.return_value = pb
session.execute = AsyncMock(return_value=result)
session.flush = AsyncMock()
optimal = MagicMock()
optimal.unindex_playbook = AsyncMock(return_value=None)
svc = PlaybookService(session)
with (
patch(
"roboco.services.optimal.get_optimal_service",
AsyncMock(return_value=optimal),
),
):
await svc.reject(playbook_id, approver_id=uuid4(), reason="nope")
optimal.unindex_playbook.assert_not_awaited()