Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ flowchart TB
| `llm` | One interface, `complete(prompt) -> str`, with a CLI-backed and an API-backed implementation. Spend cap lives here. | nothing |
| `extract` | Builds the prompt from owner turns, validates the reply into `Memory` + `Evidence`, and counts what it rejects and why. | `model`, `llm` |
| `sweep` | Runs ingest, extract and store over new sessions. Skips unchanged files, stops only on budget or model error, never because unreviewed memories are piling up. | `ingest`, `extract`, `llm`, `store` |
| `retrieve` | The one read policy: approved and unconfirmed memories are served, restricted and archived never are. Word-overlap ranking, a baseline to be measured. | `model` |
| `retrieve` | The one read policy: approved and unconfirmed memories are served, restricted and archived never are. It also shapes the evidence quotes an assistant receives (email addresses masked, cut at a word boundary). Word-overlap ranking, a baseline to be measured. | `model` |
| `store` | The only module that touches SQLite. Schema, migrations, queries, state changes. | `model` |
| `review` | Terminal UI over the store: approve, edit, restrict, remove. Optional by design. | `store` |
| `mcp_server` | Two tools for assistants: `personakit_read` and `personakit_propose`. Assistants cannot approve, restrict, hide or forget; only the owner can, in `review`. | `store`, `retrieve`, `extract` |
| `mcp_server` | Two tools for assistants: `personakit_read` and `personakit_propose`. Holds no read-policy constants of its own. Assistants cannot approve, restrict, hide or forget; only the owner can, in `review`. | `store`, `retrieve`, `extract` |
| `stats` | Counts only (memories by state, sessions extracted, reads and how many returned something). Never content. | `store` |
| `cli` | Argument parsing and wiring; no logic. | all |

Expand Down
22 changes: 1 addition & 21 deletions personakit/mcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,12 @@

from __future__ import annotations

import re

from personakit import extract, retrieve
from personakit.model import Evidence, Memory, State
from personakit.review import PROPOSED_BY_ASSISTANT
from personakit.store import Store

NOTE_UNCONFIRMED = "Unconfirmed: extracted from the owner's conversations but not yet reviewed by them."
SERVED_QUOTE_CHARS = 240
_EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+")


def served_quote(quote: str) -> str:
"""The form of a quote an assistant receives: emails masked, cut at a word boundary.

The store keeps the quote verbatim for the owner's own review; assistants get a shorter,
masked copy.
"""
text = _EMAIL.sub("[email]", " ".join(quote.split()))
if len(text) <= SERVED_QUOTE_CHARS:
return text
cut = text[:SERVED_QUOTE_CHARS]
ends = [cut.rfind(mark) for mark in (". ", "? ", "! ")]
if max(ends) >= SERVED_QUOTE_CHARS // 2:
return cut[: max(ends) + 1]
return cut.rsplit(" ", 1)[0].rstrip(",;:") + "…"


def read_memories(store: Store, question: str, limit: int = 7) -> dict:
Expand All @@ -47,7 +27,7 @@ def read_memories(store: Store, question: str, limit: int = 7) -> dict:
"text": m.text,
"category": m.category,
"confirmed": m.state == State.APPROVED,
"evidence": [served_quote(e.quote) for e in m.evidence[:2]],
"evidence": [retrieve.served_quote(e.quote) for e in m.evidence[:2]],
}
for m in result.memories
],
Expand Down
20 changes: 20 additions & 0 deletions personakit/retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
# assistant always gets the owner's confirmed standing preferences.
STANDING_CONTEXT = 3

# Evidence quotes are shortened and email-masked before an assistant sees them.
SERVED_QUOTE_CHARS = 240
_EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+")

_WORD = re.compile(r"[a-z0-9']{3,}")
_STOP = frozenset(
"the and for you your with that this have from what how should about are was were not but "
Expand Down Expand Up @@ -58,3 +62,19 @@ def search(memories: Iterable[Memory], question: str, limit: int = 7) -> Result:
if fill > 0:
chosen.extend(sorted(standing, key=lambda m: (m.created_at or "", m.id))[:fill])
return Result(chosen, withheld)


def served_quote(quote: str) -> str:
"""The form of a quote an assistant receives: emails masked, cut at a word boundary.

The store keeps the quote verbatim for the owner's own review; assistants get a shorter,
masked copy.
"""
text = _EMAIL.sub("[email]", " ".join(quote.split()))
if len(text) <= SERVED_QUOTE_CHARS:
return text
cut = text[:SERVED_QUOTE_CHARS]
ends = [cut.rfind(mark) for mark in (". ", "? ", "! ")]
if max(ends) >= SERVED_QUOTE_CHARS // 2:
return cut[: max(ends) + 1]
return cut.rsplit(" ", 1)[0].rstrip(",;:") + "…"
11 changes: 0 additions & 11 deletions tests/test_mcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,6 @@ def test_unconfirmed_note_only_when_an_unconfirmed_memory_is_served(self):
mixed = mcp_server.read_memories(self.store, "feedback and short answers")
self.assertEqual(mixed["note"], mcp_server.NOTE_UNCONFIRMED)

def test_served_quotes_mask_emails_and_end_at_a_word_boundary(self):
self.assertEqual(mcp_server.served_quote("write to me at someone@example.com please"),
"write to me at [email] please")
long_quote = "word " * 100
served = mcp_server.served_quote(long_quote)
self.assertLessEqual(len(served), mcp_server.SERVED_QUOTE_CHARS + 1)
self.assertTrue(served.endswith("word…"))
first = "I like " + "very " * 25 + "direct feedback."
self.assertEqual(mcp_server.served_quote(first + " " + "filler " * 60), first)
self.assertEqual(mcp_server.served_quote("Short one."), "Short one.")

def test_restricted_text_never_appears_in_the_response(self):
result = mcp_server.read_memories(self.store, "career change quit")
self.assertNotIn("career", str(result))
Expand Down
14 changes: 14 additions & 0 deletions tests/test_retrieve.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

from personakit.model import Evidence, Memory, State
from personakit import retrieve
from personakit.retrieve import SERVED, STANDING_CONTEXT, search


Expand Down Expand Up @@ -47,5 +48,18 @@ def test_stop_words_alone_do_not_match(self):
self.assertEqual(search([mem("You prefer the quiet.")], "what should the").memories, [])


class ServedQuoteTest(unittest.TestCase):
def test_served_quotes_mask_emails_and_end_at_a_word_boundary(self):
self.assertEqual(retrieve.served_quote("write to me at someone@example.com please"),
"write to me at [email] please")
long_quote = "word " * 100
served = retrieve.served_quote(long_quote)
self.assertLessEqual(len(served), retrieve.SERVED_QUOTE_CHARS + 1)
self.assertTrue(served.endswith("word…"))
first = "I like " + "very " * 25 + "direct feedback."
self.assertEqual(retrieve.served_quote(first + " " + "filler " * 60), first)
self.assertEqual(retrieve.served_quote("Short one."), "Short one.")


if __name__ == "__main__":
unittest.main()
Loading