Coverage for app / memory / cues.py: 95%
21 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
1"""Whether a message is worth an extraction call: the write policy's gate.
3Most turns tell an agent nothing it should keep — a question, a request for
4this answer, thanks. Asking a model after every turn what to remember would
5cost a call per turn to find nothing, so a deterministic screen goes first:
6the call is made only when the person's words carry a cue that they are
7telling the agent about themselves, how they want to be answered, or their
8ongoing work, or that they are correcting or asking it to remember or forget
9something.
11The screen decides only whether to *ask*. What is kept is the extraction
12step's proposal, validated in code (``extraction.validate``). So a cue is
13cheap to over-fire (one call that finds nothing) and costly to miss (a
14memory never written) — it leans towards recall, and
15``tests/memory/test_write_policy.py`` measures both on labelled messages.
16"""
18from __future__ import annotations
20import re
21from enum import StrEnum
24class Cue(StrEnum):
25 """Why a message was sent for extraction."""
27 #: "Remember that …", "forget what I said about …".
28 explicit = "explicit"
29 #: "From now on …", "always include …", "no emojis please".
30 standing_instruction = "standing_instruction"
31 #: "I'm the DPO at …", "my pronouns are …", "we use Jira".
32 self_description = "self_description"
33 #: "I prefer …", "I'd rather you didn't …".
34 preference = "preference"
35 #: "We've decided …", "cutover is in December", "our budget is …".
36 ongoing_work = "ongoing_work"
37 #: "Actually I report to …", "not any more", "the deadline moved".
38 correction = "correction"
41_FLAGS = re.IGNORECASE
43#: Each cue's patterns. Phrased as how people say these things, not as the
44#: labelled cases' words: a pattern earns its place by a family of messages.
45_PATTERNS: dict[Cue, re.Pattern[str]] = {
46 Cue.explicit: re.compile(
47 r"\b(remember (that|this|my|our|i|we)|don'?t forget|forget (what|that|about|my|our)|"
48 r"keep in mind|bear in mind|note that|for future reference|make a note)\b",
49 _FLAGS,
50 ),
51 Cue.standing_instruction: re.compile(
52 r"\b(from now on|going forward|in (the )?future|every time|each time|"
53 r"by default|in everything|for everything|all my|whenever (i|you))\b"
54 r"|(^|[.!?;]\s+|please\s+|,\s*)(always|never)\b"
55 r"|\b(always|never) (use|include|add|write|answer|reply|respond|give|cite|"
56 r"show|put|ask|start|end|mention|suggest|format|send|call)\b"
57 r"|\b(stop|quit) (adding|using|including|giving|writing|suggesting|putting|sending)\b"
58 r"|\bdon'?t (use|include|add|give|suggest|write|put|send|mention) \w+(,| because| as| since|$)"
59 r"|^\s*no \w+( \w+)?,? please\b"
60 r"|\b(call me|address me as)\b",
61 _FLAGS,
62 ),
63 Cue.self_description: re.compile(
64 r"\bi(?:'m| am) (?!a bit|a little|not|just|afraid|sorry|sure|confused|"
65 r"looking|trying|wondering|curious|asking|getting|seeing|having|going to ask)"
66 r"(a|an|the|on (leave|holiday|annual leave|sabbatical|parental)|based|located|"
67 r"from|responsible|in charge|vegetarian|vegan|blind|deaf|dyslexic|colou?r ?blind|"
68 r"left-handed|new to|covering)\b"
69 r"|\bi (work|teach|live|lead|manage|run|own|study) "
70 r"|\bmy (name|pronouns|role|job|title|team|manager|boss|company|employer|"
71 r"organi[sz]ation|department|background|screen reader|stack|setup)\b"
72 r"|\b(we(?:'re| are)|our (team|company) is) (a|an|the)\b"
73 r"|\bwe(?:'re| are)? (use|run|have|host|deploy|build)\b"
74 r"|\bour (team|company|org|organi[sz]ation|ci|stack|servers|platform|board|"
75 r"policy|process|clients?|customers?|budget|deadline|office|product) "
76 r"(is|are|uses?|runs?|meets?|has|have|was)\b",
77 _FLAGS,
78 ),
79 Cue.preference: re.compile(
80 r"\bi (prefer|hate|dislike|don'?t like|can'?t stand|would rather|'d rather)\b"
81 r"|\bi'?d rather\b"
82 r"|\bi (like|love|want|need) (my|answers|responses|replies|things|it when|you to (always|never))\b"
83 r"|\bkeep (it|answers|responses|replies|them|everything) "
84 r"(short|brief|concise|simple|formal|informal|casual)\b",
85 _FLAGS,
86 ),
87 Cue.ongoing_work: re.compile(
88 r"\bwe(?:'ve| have)? (decided|chose|picked|agreed|settled|switched|moved|migrated|"
89 r"dropped|launched|signed)\b"
90 r"|\bwe(?:'re| are) (going (to|with)|moving|migrating|launching|building|planning|"
91 r"switching|rolling out|hiring)\b"
92 r"|\bi(?:'m| am) (writing|building|preparing|planning|working on|migrating|"
93 r"launching|running|leading|organi[sz]ing)\b"
94 r"|\b(the|our|my) (deadline|cutover|launch|go-live|review|budget|plan)\b.{0,40}"
95 r"\b(is|are|was|moved|on|by|in|for)\b"
96 r"|\bdeadline\b|\bdue (on|by|in)\b|\brenews?\b|\bthe plan is\b"
97 r"|\b(the|our) client is\b"
98 r"|\bmy manager wants\b",
99 _FLAGS,
100 ),
101 Cue.correction: re.compile(
102 r"(^|[.!?]\s+)actually\b|\bcorrection\b|\bthat'?s (wrong|not right|incorrect|outdated)\b"
103 r"|\bno longer\b|\bnot any ?more\b|\banymore\b|\bchanged my mind\b"
104 r"|\b(moved|changed|switched) to\b|\binstead of\b|\binstead\b[.!]?\s*$",
105 _FLAGS,
106 ),
107}
109if set(_PATTERNS) != set(Cue):
110 raise RuntimeError("Every cue needs its patterns")
112#: A message that says the instruction is for this answer alone is not a
113#: standing one, whatever else it says.
114_ONE_OFF = re.compile(
115 r"\b(for this (one )?(answer|reply|response|question|time|message)|just this once|"
116 r"this time only|only this time)\b",
117 _FLAGS,
118)
121def cues(text: str) -> list[Cue]:
122 """The cues in ``text`` (the person's own words), in vocabulary order —
123 empty when nothing in it is worth an extraction call."""
124 if not text or not text.strip() or _ONE_OFF.search(text):
125 return []
126 return [cue for cue, pattern in _PATTERNS.items() if pattern.search(text)]
129def worth_extracting(text: str) -> bool:
130 """Whether the write policy asks a model what to remember from ``text``."""
131 return bool(cues(text))