Coverage for app / memory / kinds.py: 96%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1"""The kinds of memory an agent keeps, and how each is recalled. 

2 

3One vocabulary, read by the store, the extraction prompt, the recall ranking 

4and the API: a kind is added here and nowhere else. Each kind's policy says 

5whether it is always recalled, how fast it goes stale, and how much a match 

6counts — the differences that made four kinds worth having rather than one. 

7""" 

8 

9from __future__ import annotations 

10 

11from dataclasses import dataclass 

12from enum import StrEnum 

13 

14 

15class MemoryKind(StrEnum): 

16 """What a memory is.""" 

17 

18 #: One turn: what the person asked and the gist of the answer. 

19 episode = "episode" 

20 #: Something stable the person said about themselves or their setting. 

21 fact = "fact" 

22 #: How the person wants to be answered, or how a task should be done. 

23 preference = "preference" 

24 #: An objective, decision, constraint or deadline of ongoing work. 

25 goal = "goal" 

26 

27 

28#: The kinds written only from the person's own words (``extraction``). 

29CURATED: tuple[MemoryKind, ...] = ( 

30 MemoryKind.fact, 

31 MemoryKind.preference, 

32 MemoryKind.goal, 

33) 

34 

35#: What each curated kind holds, as the extraction step is told it. Checked 

36#: against ``CURATED`` at import (``vocab.check``) by ``extraction``. 

37CURATED_GUIDANCE: dict[MemoryKind, str] = { 

38 MemoryKind.fact: ( 

39 "something stable the person states about themselves, their role, " 

40 "their organisation or their setup (where they work, what they use, " 

41 "who they report to)" 

42 ), 

43 MemoryKind.preference: ( 

44 "how the person wants to be answered or how a kind of task should be " 

45 "done for them (language, length, format, tone, what to include or " 

46 "leave out, what to call them)" 

47 ), 

48 MemoryKind.goal: ( 

49 "an objective, decision, constraint, budget or deadline of work the " 

50 "person is doing over more than this conversation" 

51 ), 

52} 

53 

54 

55@dataclass(frozen=True) 

56class RecallPolicy: 

57 """How one kind of memory is recalled.""" 

58 

59 #: Recalled for every request, relevant or not: a preference shapes how 

60 #: any answer is written, and rarely shares words with the request. 

61 always: bool 

62 #: Days for a match to lose half its weight; None: no decay. Recency 

63 #: only ever scales a relevant match, so an old memory the request is 

64 #: plainly about still comes back. 

65 half_life_days: float | None 

66 #: How much a match counts beside the other kinds'. What the person 

67 #: stated outranks a model's summary of a turn. 

68 weight: float 

69 

70 

71POLICIES: dict[MemoryKind, RecallPolicy] = { 

72 MemoryKind.preference: RecallPolicy(always=True, half_life_days=None, weight=1.0), 

73 MemoryKind.fact: RecallPolicy(always=False, half_life_days=None, weight=1.0), 

74 MemoryKind.goal: RecallPolicy(always=False, half_life_days=90, weight=1.0), 

75 MemoryKind.episode: RecallPolicy(always=False, half_life_days=45, weight=0.8), 

76} 

77 

78if set(POLICIES) != set(MemoryKind): 

79 raise RuntimeError("Every memory kind needs a recall policy") 

80 

81 

82def parse(value: object) -> MemoryKind: 

83 """``value`` as a kind; anything unknown — a row from before kinds — is 

84 an episode, which is what every such row was.""" 

85 try: 

86 return MemoryKind(str(value)) 

87 except ValueError: 

88 return MemoryKind.episode