11import copy
22import threading
3- import uuid
43from datetime import datetime , timezone
54from typing import Any , Callable
65
7- from pydantic import BaseModel , Field , ValidationError
6+ from pydantic import BaseModel , ValidationError
87
98from .backends .base import StorageBackend
109from .constants import Operation
1110from .exceptions import ConflictError , HookError , MemoryStoreError , ValidationFailed
11+ from .schemas import Fact , TxEntry
1212
13-
14- class Fact (BaseModel ):
15- id : str = Field (default_factory = lambda : str (uuid .uuid4 ()))
16- type : str
17- payload : dict [str , Any ]
18- source : str | None = None
19- session_id : str | None = None
20- ts : datetime = Field (default_factory = lambda : datetime .now (timezone .utc ))
21-
22-
23- class TxEntry (BaseModel ):
24- uuid : str = Field (default_factory = lambda : str (uuid .uuid4 ()))
25- seq : int
26- ts : datetime
27- op : Operation
28- fact_id : str | None
29- fact_before : dict [str , Any ] | None = None
30- fact_after : dict [str , Any ] | None = None
31- actor : str | None = None
32- reason : str | None = None
33-
34-
35- MemoryHook = Callable [[str , str , dict [str , Any ] | None ], None ]
13+ MemoryHook = Callable [[Operation , str , Fact | None ], None ]
3614
3715
3816class SchemaRegistry :
@@ -76,7 +54,7 @@ def register_schema(self, typename: str, model: type[BaseModel], constraint: Con
7654 def add_hook (self , hook : MemoryHook ):
7755 self ._hooks .append (hook )
7856
79- def _notify_hooks (self , op : Operation , fact_id : str , data : dict [ str , Any ] | None ) -> None :
57+ def _notify_hooks (self , op : Operation , fact_id : str , data : Fact | None ) -> None :
8058 for hook in self ._hooks :
8159 try :
8260 hook (op , fact_id , data )
@@ -126,7 +104,7 @@ def commit(
126104
127105 self .storage .save (fact .model_dump ())
128106 self ._log_tx (op , fact .id , existing , fact .model_dump (), actor , reason )
129- self ._notify_hooks (op , fact .id , fact . model_dump () )
107+ self ._notify_hooks (op , fact .id , fact )
130108 return fact .id
131109
132110 def update (self , fact_id : str , patch : dict [str , Any ], actor : str | None = None , reason : str | None = None ) -> str :
@@ -142,7 +120,7 @@ def update(self, fact_id: str, patch: dict[str, Any], actor: str | None = None,
142120
143121 self .storage .save (existing )
144122 self ._log_tx (Operation .UPDATE , fact_id , before , existing , actor , reason )
145- self ._notify_hooks (Operation .UPDATE , fact_id , existing )
123+ self ._notify_hooks (Operation .UPDATE , fact_id , Fact ( ** existing ) )
146124 return fact_id
147125
148126 def delete (self , fact_id : str , actor : str | None = None , reason : str | None = None ) -> str :
@@ -153,7 +131,7 @@ def delete(self, fact_id: str, actor: str | None = None, reason: str | None = No
153131
154132 self .storage .delete (fact_id )
155133 self ._log_tx (Operation .DELETE , fact_id , existing , None , actor , reason )
156- self ._notify_hooks (Operation .DELETE , fact_id , existing )
134+ self ._notify_hooks (Operation .DELETE , fact_id , Fact ( ** existing ) )
157135 return fact_id
158136
159137 def get (self , fact_id : str ) -> dict [str , Any ] | None :
@@ -189,7 +167,7 @@ def promote_session(
189167
190168 promoted .append (fact_dict ["id" ])
191169 self ._log_tx (Operation .PROMOTE , fact_dict ["id" ], before , fact_dict , actor , reason )
192- self ._notify_hooks (Operation .PROMOTE , fact_dict ["id" ], fact_dict )
170+ self ._notify_hooks (Operation .PROMOTE , fact_dict ["id" ], Fact ( ** fact_dict ) )
193171
194172 return promoted
195173
0 commit comments