File size: 10,845 Bytes
9b02e81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""
memory.py β€” Typed, versioned, scoped, reversible memory system.

Replaces the flat heuristic library with a structured memory store where
every memory has a kind, status, scope, trust score, and full provenance.

Memory lifecycle:
    candidate β†’ quarantined (immune scan) β†’ promoted (replay-tested) β†’ archived
                                         β†’ rejected (if scan/test fails)

Memory kinds:
    purpose_contract   β€” the user's stated goal and constraints
    user_preference    β€” learned user-specific preferences ("always cite sources")
    skill_card         β€” reusable procedure extracted from successful trajectories
    episodic_case      β€” a specific (state, action, outcome) triple worth remembering
    failure_pattern    β€” a pattern that led to failure (negative heuristic)
    critic_calibration β€” learned adjustments to the Purpose Function's scoring
    tool_policy        β€” usage constraints and tips for specific tools
"""
from __future__ import annotations

import json
import logging
import math
import time
import uuid
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import Any

from purpose_agent.v2_types import MemoryScope

logger = logging.getLogger(__name__)


class MemoryKind(Enum):
    PURPOSE_CONTRACT = "purpose_contract"
    USER_PREFERENCE = "user_preference"
    SKILL_CARD = "skill_card"
    EPISODIC_CASE = "episodic_case"
    FAILURE_PATTERN = "failure_pattern"
    CRITIC_CALIBRATION = "critic_calibration"
    TOOL_POLICY = "tool_policy"


class MemoryStatus(Enum):
    CANDIDATE = "candidate"
    QUARANTINED = "quarantined"
    PROMOTED = "promoted"
    REJECTED = "rejected"
    ARCHIVED = "archived"


@dataclass
class MemoryCard:
    """
    A single unit of agent memory. Every field is tracked for provenance.
    """
    id: str = field(default_factory=lambda: uuid.uuid4().hex[:12])
    kind: MemoryKind = MemoryKind.SKILL_CARD
    status: MemoryStatus = MemoryStatus.CANDIDATE
    scope: MemoryScope = field(default_factory=MemoryScope)

    # Content
    content: str = ""               # The actual knowledge (human readable)
    pattern: str = ""               # When does this apply?
    strategy: str = ""              # What to do?
    steps: list[str] = field(default_factory=list)

    # Trust & utility
    trust_score: float = 0.5        # 0=untrusted, 1=fully trusted
    utility_score: float = 0.5      # how useful is this when retrieved?
    times_retrieved: int = 0
    times_helped: int = 0
    times_hurt: int = 0

    # Provenance
    source_trace_id: str = ""
    source_step: int = 0
    created_at: float = field(default_factory=time.time)
    created_by: str = ""            # agent name or "human" or "system"
    version: int = 1
    parent_id: str = ""             # if this is a revision of another memory
    rejection_reason: str = ""
    immune_scan_result: dict[str, Any] = field(default_factory=dict)

    # Embedding for retrieval
    embedding: list[float] | None = None

    def update_utility(self, helped: bool, alpha: float = 0.1) -> None:
        """Monte Carlo utility update: U_new = U + Ξ±(reward - U)."""
        self.times_retrieved += 1
        if helped:
            self.times_helped += 1
            self.utility_score += alpha * (1.0 - self.utility_score)
        else:
            self.times_hurt += 1
            self.utility_score += alpha * (0.0 - self.utility_score)
        self.utility_score = max(0.0, min(1.0, self.utility_score))

    @property
    def empirical_help_rate(self) -> float:
        if self.times_retrieved == 0:
            return 0.5
        return self.times_helped / self.times_retrieved

    def to_dict(self) -> dict[str, Any]:
        return {
            "id": self.id,
            "kind": self.kind.value,
            "status": self.status.value,
            "scope": {
                "agent_roles": self.scope.agent_roles,
                "tool_names": self.scope.tool_names,
                "task_categories": self.scope.task_categories,
                "user_id": self.scope.user_id,
            },
            "content": self.content,
            "pattern": self.pattern,
            "strategy": self.strategy,
            "steps": self.steps,
            "trust_score": self.trust_score,
            "utility_score": self.utility_score,
            "times_retrieved": self.times_retrieved,
            "times_helped": self.times_helped,
            "times_hurt": self.times_hurt,
            "source_trace_id": self.source_trace_id,
            "created_at": self.created_at,
            "created_by": self.created_by,
            "version": self.version,
            "parent_id": self.parent_id,
            "status_detail": self.rejection_reason,
            "immune_scan": self.immune_scan_result,
        }

    @classmethod
    def from_dict(cls, d: dict) -> "MemoryCard":
        scope_d = d.get("scope", {})
        return cls(
            id=d.get("id", uuid.uuid4().hex[:12]),
            kind=MemoryKind(d.get("kind", "skill_card")),
            status=MemoryStatus(d.get("status", "candidate")),
            scope=MemoryScope(
                agent_roles=scope_d.get("agent_roles", []),
                tool_names=scope_d.get("tool_names", []),
                task_categories=scope_d.get("task_categories", []),
                user_id=scope_d.get("user_id", ""),
            ),
            content=d.get("content", ""),
            pattern=d.get("pattern", ""),
            strategy=d.get("strategy", ""),
            steps=d.get("steps", []),
            trust_score=d.get("trust_score", 0.5),
            utility_score=d.get("utility_score", 0.5),
            times_retrieved=d.get("times_retrieved", 0),
            times_helped=d.get("times_helped", 0),
            times_hurt=d.get("times_hurt", 0),
            source_trace_id=d.get("source_trace_id", ""),
            created_at=d.get("created_at", time.time()),
            created_by=d.get("created_by", ""),
            version=d.get("version", 1),
            parent_id=d.get("parent_id", ""),
            rejection_reason=d.get("status_detail", ""),
            immune_scan_result=d.get("immune_scan", {}),
        )


class MemoryStore:
    """
    Persistent, queryable store of MemoryCards.

    Supports:
    - Add/retrieve/update/reject memories
    - Filter by kind, status, scope
    - Ranked retrieval (relevance Γ— trust Γ— utility)
    - Persistence to JSON
    """

    def __init__(self, persistence_path: str | None = None):
        self._cards: dict[str, MemoryCard] = {}
        self._path = Path(persistence_path) if persistence_path else None
        if self._path and self._path.exists():
            self._load()

    def add(self, card: MemoryCard) -> MemoryCard:
        """Add a memory card. Returns the card (with id assigned)."""
        self._cards[card.id] = card
        self._save()
        return card

    def get(self, card_id: str) -> MemoryCard | None:
        return self._cards.get(card_id)

    def update_status(self, card_id: str, status: MemoryStatus, reason: str = "") -> None:
        card = self._cards.get(card_id)
        if card:
            card.status = status
            if reason:
                card.rejection_reason = reason
            self._save()

    def retrieve(
        self,
        query_text: str = "",
        scope: MemoryScope | None = None,
        kinds: list[MemoryKind] | None = None,
        statuses: list[MemoryStatus] | None = None,
        top_k: int = 10,
    ) -> list[MemoryCard]:
        """
        Retrieve memories ranked by composite score.
        Default: only promoted memories.
        """
        statuses = statuses or [MemoryStatus.PROMOTED]
        candidates = []
        query_emb = self._embed(query_text) if query_text else None

        for card in self._cards.values():
            if card.status not in statuses:
                continue
            if kinds and card.kind not in kinds:
                continue
            if scope and not card.scope.matches(scope):
                continue

            # Composite score: relevance Γ— trust Γ— utility
            relevance = 0.5
            if query_emb and card.embedding:
                relevance = self._cosine(query_emb, card.embedding)
            elif query_emb:
                card.embedding = self._embed(card.content or card.pattern)
                relevance = self._cosine(query_emb, card.embedding)

            score = 0.4 * relevance + 0.3 * card.trust_score + 0.3 * card.utility_score
            candidates.append((score, card))

        candidates.sort(key=lambda x: -x[0])
        return [c for _, c in candidates[:top_k]]

    def get_by_status(self, status: MemoryStatus) -> list[MemoryCard]:
        return [c for c in self._cards.values() if c.status == status]

    def get_all(self) -> list[MemoryCard]:
        return list(self._cards.values())

    @property
    def size(self) -> int:
        return len(self._cards)

    def stats(self) -> dict[str, Any]:
        by_status = {}
        by_kind = {}
        for c in self._cards.values():
            by_status[c.status.value] = by_status.get(c.status.value, 0) + 1
            by_kind[c.kind.value] = by_kind.get(c.kind.value, 0) + 1
        return {"total": self.size, "by_status": by_status, "by_kind": by_kind}

    # --- Persistence ---

    def _save(self) -> None:
        if not self._path:
            return
        self._path.parent.mkdir(parents=True, exist_ok=True)
        data = [c.to_dict() for c in self._cards.values()]
        with open(self._path, "w") as f:
            json.dump(data, f, indent=2, default=str)

    def _load(self) -> None:
        if not self._path or not self._path.exists():
            return
        try:
            with open(self._path) as f:
                data = json.load(f)
            for d in data:
                card = MemoryCard.from_dict(d)
                self._cards[card.id] = card
            logger.info(f"MemoryStore: loaded {len(self._cards)} cards")
        except Exception as e:
            logger.error(f"MemoryStore: load failed: {e}")

    # --- Embedding (lightweight, swappable) ---

    @staticmethod
    def _embed(text: str) -> list[float]:
        dim = 128
        vec = [0.0] * dim
        for i in range(len(text) - 2):
            h = hash(text[i:i+3].lower()) % dim
            vec[h] += 1.0
        mag = math.sqrt(sum(x*x for x in vec))
        return [x/mag for x in vec] if mag > 0 else vec

    @staticmethod
    def _cosine(a: list[float], b: list[float]) -> float:
        if not a or not b or len(a) != len(b):
            return 0.0
        dot = sum(x*y for x, y in zip(a, b))
        ma = math.sqrt(sum(x*x for x in a))
        mb = math.sqrt(sum(x*x for x in b))
        return dot / (ma * mb) if ma > 0 and mb > 0 else 0.0