-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmemory.py
More file actions
1387 lines (1253 loc) · 47.1 KB
/
Copy pathmemory.py
File metadata and controls
1387 lines (1253 loc) · 47.1 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Persistent memory and semantic retrieval engine for Home Assistant Pyscript automations."""
import asyncio
import re
import sqlite3
import threading
import time
import unicodedata
from contextlib import closing
from datetime import UTC, datetime, timedelta
from pathlib import Path
from typing import Any
DB_PATH = Path("/config/memory.db")
RESULT_ENTITY = "sensor.memory_result"
EXPIRATION_MAX_DAYS = 3650
SEARCH_LIMIT_MAX = 50
NEAR_DISTANCE = 5
CANDIDATE_CHECK_LIMIT = 5
HOUSEKEEPING_GRACE_DAYS = 10
HOUSEKEEPING_GRACE_MAX_DAYS = 365
VALUE_PREVIEW_CHARS = 120
BM25_WEIGHT = 0.5
EXTRA_CHAR_REPLACEMENTS = {
"đ": "d",
"Đ": "d",
"ı": "i", # noqa: RUF001
"İ": "i",
"ñ": "n",
"Ñ": "n",
"ç": "c",
"Ç": "c",
"ğ": "g",
"Ğ": "g",
"ş": "s",
"Ş": "s",
"ø": "o",
"Ø": "o",
"ł": "l",
"Ł": "l",
"ß": "ss",
"Æ": "AE",
"æ": "ae",
"Œ": "OE",
"œ": "oe",
"Þ": "th",
"þ": "th",
"Ð": "d",
"ð": "d",
"Å": "a",
"å": "a",
"Ä": "a",
"ä": "a",
"Ö": "o",
"ö": "o",
"Ü": "u",
"ü": "u",
}
_DB_READY = False
_DB_READY_LOCK = threading.Lock()
result_entity_name: dict[str, str] = {}
def _build_result_entity_name() -> dict[str, str]:
"""Generate a friendly name dictionary for the result entity."""
tail = RESULT_ENTITY.split(".")[-1]
parts = [part.capitalize() for part in tail.split("_") if part]
friendly = " ".join(parts) or tail
return {"friendly_name": friendly}
def _ensure_result_entity_name(force: bool = False) -> None:
"""Ensure the result entity name is populated."""
global result_entity_name
if force or not result_entity_name:
result_entity_name = _build_result_entity_name()
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _utcnow_iso() -> str:
"""Return current UTC time in ISO 8601 format."""
return datetime.now(UTC).isoformat()
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _dt_from_iso(s: str) -> datetime | None:
"""Parse an ISO 8601 string into a datetime object."""
try:
return datetime.fromisoformat(s)
except (TypeError, ValueError):
return None
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _get_db_connection() -> sqlite3.Connection:
"""Create a configured SQLite connection with optimized PRAGMAs."""
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA synchronous=NORMAL;")
conn.execute("PRAGMA temp_store=MEMORY;")
conn.execute("PRAGMA busy_timeout=3000;")
return conn
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _ensure_db() -> None:
"""Initialize the database schema and indices."""
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
with closing(_get_db_connection()) as conn:
conn.execute("PRAGMA journal_mode=WAL;")
conn.execute(
"""
CREATE TABLE IF NOT EXISTS mem
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
key TEXT UNIQUE NOT NULL,
value TEXT NOT NULL,
scope TEXT NOT NULL,
tags TEXT NOT NULL,
tags_search TEXT NOT NULL,
created_at TEXT NOT NULL,
last_used_at TEXT NOT NULL,
expires_at TEXT
);
"""
)
conn.execute(
"""
CREATE VIRTUAL TABLE IF NOT EXISTS mem_fts USING fts5(
key, value, tags,
content='mem',
content_rowid='id',
tokenize = 'unicode61 remove_diacritics 2'
);
"""
)
conn.execute("CREATE INDEX IF NOT EXISTS idx_mem_scope ON mem(scope);")
conn.executescript(
"""
CREATE TRIGGER IF NOT EXISTS mem_ai
AFTER INSERT
ON mem
BEGIN
INSERT INTO mem_fts(rowid, key, value, tags)
VALUES (new.id,
new.key,
new.value,
new.tags_search);
END;
CREATE TRIGGER IF NOT EXISTS mem_ad
AFTER DELETE
ON mem
BEGIN
INSERT INTO mem_fts(mem_fts, rowid, key, value, tags)
VALUES ('delete', old.id, old.key, old.value, old.tags_search);
END;
CREATE TRIGGER IF NOT EXISTS mem_au
AFTER UPDATE OF key, value, tags_search
ON mem
WHEN (old.key IS NOT new.key)
OR (old.value IS NOT new.value)
OR (old.tags_search IS NOT new.tags_search)
BEGIN
INSERT INTO mem_fts(mem_fts, rowid, key, value, tags)
VALUES ('delete', old.id, old.key, old.value, old.tags_search);
INSERT INTO mem_fts(rowid, key, value, tags)
VALUES (new.id,
new.key,
new.value,
new.tags_search);
END;
"""
)
conn.execute("PRAGMA optimize;")
conn.commit()
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _ensure_db_once(force: bool = False) -> None:
"""Ensure the database is initialized, optionally forcing a rebuild."""
global _DB_READY
if force:
_DB_READY = False
if _DB_READY and DB_PATH.exists():
return
with _DB_READY_LOCK:
if force:
_DB_READY = False
if not _DB_READY or not DB_PATH.exists():
_ensure_db()
_DB_READY = True
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _normalize_value(s: str) -> str:
"""Normalize text value to Unicode NFC form."""
return "" if s is None else unicodedata.normalize("NFC", s)
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _strip_diacritics(value: str) -> str:
"""Remove diacritics and normalize locale-specific characters."""
if value is None:
return ""
decomposed = unicodedata.normalize("NFKD", value)
filtered: list[str] = []
for ch in decomposed:
replacement = EXTRA_CHAR_REPLACEMENTS.get(ch)
if replacement is not None:
if replacement:
filtered.extend(replacement)
continue
if unicodedata.category(ch) == "Mn":
continue
filtered.append(ch)
return "".join(filtered)
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _normalize_search_text(value: str | None) -> str:
"""Normalize text for search indexing and querying."""
if value is None:
return ""
lowered = str(value).lower()
stripped = _strip_diacritics(lowered)
cleaned = re.sub(r"[,/_]+", " ", stripped)
cleaned = re.sub(r"[^a-z0-9]+", " ", cleaned)
return re.sub(r"\s+", " ", cleaned).strip()
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _normalize_tags(s: str) -> str:
"""Normalize space-separated tags for consistent searching."""
return _normalize_search_text(s)
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _normalize_key(s: str) -> str:
"""Normalize a memory key to a standard alphanumeric format."""
if s is None:
return ""
s = s.strip().lower()
s = _strip_diacritics(s)
s = re.sub(r"[^a-z0-9_]", "_", s)
return re.sub(r"_+", "_", s).strip("_")
def _condense_candidate_for_selection(entry: dict[str, Any], *, score: float | None = None) -> dict[str, Any]:
"""Condense a database entry for inclusion in result lists."""
value = entry.get("value")
if isinstance(value, str) and len(value) > VALUE_PREVIEW_CHARS:
value = f"{value[: VALUE_PREVIEW_CHARS - 3]}..."
data = {
"key": entry.get("key"),
"value": value,
"scope": entry.get("scope"),
"tags": entry.get("tags"),
"created_at": entry.get("created_at"),
"last_used_at": entry.get("last_used_at"),
"expires_at": entry.get("expires_at"),
}
if score is not None:
data["match_score"] = score
return data
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _calculate_match_score(source_tokens: set[str], candidate_tokens: set[str], bm25_raw: float | None) -> float:
"""Calculate a combined match score using Jaccard similarity and BM25."""
if not source_tokens or not candidate_tokens:
jaccard_score = 0.0
else:
intersection = source_tokens.intersection(candidate_tokens)
if not intersection:
return 0.0
union = source_tokens.union(candidate_tokens)
union_size = len(union) or 1
jaccard_score = len(intersection) / union_size
if isinstance(bm25_raw, (int, float)):
if bm25_raw < 0:
# SQLite FTS5 exposes rank as a negative BM25 score; more negative is better.
bm25_magnitude = -bm25_raw
bm25_score = bm25_magnitude / (1 + bm25_magnitude)
else:
bm25_score = 1 / (1 + bm25_raw)
jaccard_weight = 1 - BM25_WEIGHT
return BM25_WEIGHT * bm25_score + jaccard_weight * jaccard_score
return jaccard_score
async def _search_tag_candidates(
source: str,
*,
exclude_keys: set[str] | None = None,
limit: int | None = None,
log_context: str = "tag lookup",
) -> list[tuple[dict[str, Any], float]]:
"""Find memory records with similar tags using normalized token matching."""
tags_search = _normalize_tags(source or "")
if not tags_search:
return []
tag_tokens = {token for token in tags_search.split() if token}
if not tag_tokens:
return []
limit_value = limit if limit is not None else min(CANDIDATE_CHECK_LIMIT, SEARCH_LIMIT_MAX)
limit_value = max(1, min(limit_value, SEARCH_LIMIT_MAX))
try:
raw_matches = await _memory_search_db(tags_search, limit=limit_value)
except sqlite3.Error as lookup_err:
log.error(f"memory {log_context} failed for '{tags_search}': {lookup_err}") # noqa: F821 # ty:ignore[unresolved-reference]
return []
if not raw_matches:
return []
exclude_norm = {_normalize_key(item) for item in exclude_keys if item} if exclude_keys else set()
dedup: dict[str, tuple[dict[str, Any], float]] = {}
for item in raw_matches:
existing_key = _normalize_key(item.get("key", ""))
if not existing_key or existing_key in exclude_norm or existing_key in dedup:
continue
score_raw = item.get("match_score", "")
score_val: float | None
if isinstance(score_raw, (int, float)):
score_val = float(score_raw)
else:
try:
score_val = float(score_raw)
except (TypeError, ValueError):
existing_tags_norm = _normalize_tags(item.get("tags", ""))
candidate_tokens = {token for token in existing_tags_norm.split() if token}
score_val = _calculate_match_score(tag_tokens, candidate_tokens, None)
if score_val is None or score_val <= 0:
continue
dedup[existing_key] = (item, score_val)
if not dedup:
return []
sorted_candidates = sorted(dedup.values(), key=lambda pair: pair[1], reverse=True)
return sorted_candidates[:limit_value]
async def _find_tag_matches_for_query(
source: str,
*,
exclude_keys: set[str] | None = None,
limit: int | None = None,
) -> list[dict[str, Any]]:
"""Search for potential key matches based on tag similarity."""
candidates = await _search_tag_candidates(
source,
exclude_keys=exclude_keys,
limit=limit,
log_context="tag lookup",
)
if not candidates:
return []
return [_condense_candidate_for_selection(entry, score=score) for entry, score in candidates]
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _tokenize_query(q: str) -> list[str]:
"""Tokenize query string into normalized word tokens."""
normalized = _normalize_search_text(q)
return normalized.split() if normalized else []
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _near_distance_for_tokens(n: int) -> int:
"""Calculate NEAR distance threshold based on token count."""
if n <= 1:
return 0
val = 2 * n - 1
val = max(val, 3)
return min(val, NEAR_DISTANCE)
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _build_fts_queries(raw_query: str) -> list[str]:
"""Generate prioritized FTS5 query variants for improved recall."""
normalized_query = _normalize_search_text(raw_query)
tokens = normalized_query.split() if normalized_query else []
variants = []
if tokens:
if len(tokens) >= 2:
phrase = " ".join(tokens)
variants.append(f'"{phrase}"')
near_inner = " ".join(tokens)
near_dist = _near_distance_for_tokens(len(tokens))
variants.append(f"NEAR({near_inner}, {near_dist})")
if len(tokens) == 1:
variants.append(tokens[0])
else:
variants.append(" AND ".join(tokens))
or_tokens = [f"{t}*" for t in tokens]
variants.append(" OR ".join(or_tokens))
if normalized_query:
variants.append(normalized_query)
if rq := (raw_query or "").strip():
variants.append(rq)
seen = set()
out = []
for v in variants:
if v not in seen:
out.append(v)
seen.add(v)
return out
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _fetch_with_expiry(cur: sqlite3.Cursor, key: str) -> tuple[bool, sqlite3.Row | None]:
"""Retrieve a row and check if its expiration date has passed."""
row = cur.execute(
"""
SELECT key,
value,
scope,
tags,
created_at,
last_used_at,
expires_at
FROM mem
WHERE key = ?;
""",
(key,),
).fetchone()
if not row:
return False, None
if expires_at := row["expires_at"]:
dt = _dt_from_iso(expires_at)
if dt and datetime.now(UTC) > dt:
return True, row
return False, row
def _set_result(state_value: str = "ok", **attrs: Any) -> None:
"""Update the memory result sensor state and attributes."""
_ensure_result_entity_name()
attrs |= result_entity_name
state.set(RESULT_ENTITY, value=state_value, new_attributes=attrs) # noqa: F821 # ty:ignore[unresolved-reference]
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _reset_db_ready() -> None:
"""Reset the database initialization flag."""
global _DB_READY
with _DB_READY_LOCK:
_DB_READY = False
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _memory_set_db_sync(
key_norm: str,
value_norm: str,
scope_norm: str,
tags_raw: str,
tags_search: str,
now_iso: str,
expires_at: str | None,
) -> bool:
"""Synchronously persist a memory record to the database."""
for attempt in range(2):
try:
_ensure_db_once(force=attempt == 1)
with closing(_get_db_connection()) as conn:
cur = conn.cursor()
cur.execute(
"""
INSERT INTO mem(key, value, scope, tags, tags_search, created_at, last_used_at, expires_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(key) DO UPDATE SET value=excluded.value,
scope=excluded.scope,
tags=excluded.tags,
tags_search=excluded.tags_search,
last_used_at=excluded.last_used_at,
expires_at=excluded.expires_at
""",
(
key_norm,
value_norm,
scope_norm,
tags_raw,
tags_search,
now_iso,
now_iso,
expires_at,
),
)
conn.commit()
return True
except sqlite3.OperationalError:
_reset_db_ready()
if attempt == 0:
time.sleep(0.1)
continue
raise
return False
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _memory_key_exists_db_sync(key_norm: str) -> bool:
"""Synchronously check if a memory key exists."""
for attempt in range(2):
try:
_ensure_db_once(force=attempt == 1)
with closing(_get_db_connection()) as conn:
cur = conn.cursor()
row = cur.execute(
"SELECT 1 FROM mem WHERE key = ? LIMIT 1",
(key_norm,),
).fetchone()
return row is not None
except sqlite3.OperationalError:
_reset_db_ready()
if attempt == 0:
time.sleep(0.1)
continue
raise
return False
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _memory_get_db_sync(key_norm: str) -> tuple[str, dict[str, Any] | None]:
"""Synchronously fetch a memory record and update its last-used timestamp."""
for attempt in range(2):
try:
_ensure_db_once(force=attempt == 1)
with closing(_get_db_connection()) as conn:
cur = conn.cursor()
expired, row = _fetch_with_expiry(cur, key_norm)
if row is None:
return "not_found", None
row_data = {
"key": row["key"],
"value": row["value"],
"scope": row["scope"],
"tags": row["tags"],
"created_at": row["created_at"],
"last_used_at": row["last_used_at"],
"expires_at": row["expires_at"],
}
if expired:
return "expired", row_data
last_used_iso = _utcnow_iso()
cur.execute(
"UPDATE mem SET last_used_at=? WHERE key=?",
(last_used_iso, key_norm),
)
conn.commit()
row_data["last_used_at"] = last_used_iso
return "ok", row_data
except sqlite3.OperationalError:
_reset_db_ready()
if attempt == 0:
time.sleep(0.1)
continue
raise
return "error", None
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _memory_search_db_sync(query: str, limit: int) -> list[dict[str, Any]]:
"""Synchronously search for memory records matching the provided query."""
normalized_query = _normalize_search_text(query)
if not normalized_query:
return []
query_tokens = set(normalized_query.split()) if normalized_query else set()
for attempt in range(2):
try:
_ensure_db_once(force=attempt == 1)
with closing(_get_db_connection()) as conn:
cur = conn.cursor()
found_by_key: dict[str, sqlite3.Row] = {}
total_rows: list[sqlite3.Row] = []
match_variants = _build_fts_queries(query)
for mv in match_variants:
if len(found_by_key) >= limit:
break
try:
fetched = cur.execute(
"""
SELECT DISTINCT m.key,
m.value,
m.scope,
m.tags,
m.tags_search,
m.created_at,
m.last_used_at,
m.expires_at,
mem_fts.rank AS rank
FROM mem_fts
JOIN mem AS m
ON m.id = mem_fts.rowid
WHERE mem_fts MATCH ?
ORDER BY rank, m.last_used_at DESC
LIMIT ?;
""",
(mv, limit),
).fetchall()
except sqlite3.Error as error:
log.warning(f"FTS variant failed: {error}") # noqa: F821 # ty:ignore[unresolved-reference]
continue
for row in fetched:
key = row["key"]
if key not in found_by_key:
found_by_key[key] = row
total_rows.append(row)
if len(found_by_key) >= limit:
break
if not total_rows:
like_q = f"%{normalized_query}%"
total_rows = cur.execute(
"""
SELECT DISTINCT m.key,
m.value,
m.scope,
m.tags,
m.tags_search,
m.created_at,
m.last_used_at,
m.expires_at,
NULL AS rank
FROM mem AS m
WHERE m.value LIKE ?
OR m.tags LIKE ?
OR m.tags_search LIKE ?
OR m.key LIKE ?
ORDER BY m.last_used_at DESC
LIMIT ?;
""",
(like_q, like_q, like_q, like_q, limit),
).fetchall()
results: list[dict[str, Any]] = []
for row in total_rows:
candidate_source = row["tags_search"] or _normalize_tags(row["tags"])
candidate_tokens = {token for token in candidate_source.split() if token}
match_score = _calculate_match_score(query_tokens, candidate_tokens, row["rank"])
results.append(
{
"key": row["key"],
"value": row["value"],
"scope": row["scope"],
"tags": row["tags"],
"created_at": row["created_at"],
"last_used_at": row["last_used_at"],
"expires_at": row["expires_at"],
"match_score": match_score,
}
)
return results
except sqlite3.OperationalError:
_reset_db_ready()
if attempt == 0:
time.sleep(0.1)
continue
raise
return []
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _memory_forget_db_sync(key_norm: str) -> int:
"""Synchronously delete a memory record by its key."""
for attempt in range(2):
try:
_ensure_db_once(force=attempt == 1)
with closing(_get_db_connection()) as conn:
cur = conn.cursor()
cur.execute("DELETE FROM mem WHERE key=?", (key_norm,))
rowcount = getattr(cur, "rowcount", -1)
deleted = rowcount if rowcount and rowcount > 0 else 0
conn.commit()
return deleted
except sqlite3.OperationalError:
_reset_db_ready()
if attempt == 0:
time.sleep(0.1)
continue
raise
return 0
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _memory_purge_expired_db_sync(grace_days: int = 0) -> int:
"""Synchronously remove expired memory records."""
grace = max(grace_days, 0)
cutoff_dt = datetime.now(UTC) - timedelta(days=grace)
cutoff_iso = cutoff_dt.isoformat()
for attempt in range(2):
try:
_ensure_db_once(force=attempt == 1)
with closing(_get_db_connection()) as conn:
cur = conn.cursor()
cur.execute(
"DELETE FROM mem WHERE expires_at IS NOT NULL AND expires_at < ?",
(cutoff_iso,),
)
rowcount = getattr(cur, "rowcount", -1)
removed = rowcount if rowcount and rowcount > 0 else 0
conn.commit()
return removed
except sqlite3.OperationalError:
_reset_db_ready()
if attempt == 0:
time.sleep(0.1)
continue
raise
return 0
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _memory_reindex_fts_db_sync() -> tuple[int, int]:
"""Synchronously rebuild the FTS index from the main memory table."""
for attempt in range(2):
try:
_ensure_db_once(force=attempt == 1)
with closing(_get_db_connection()) as conn:
cur = conn.cursor()
cur.execute("BEGIN IMMEDIATE")
try:
cur.execute("SELECT COUNT(*) FROM mem_fts")
before = cur.fetchone()[0]
except sqlite3.Error:
before = 0
cur.execute("DROP TABLE IF EXISTS mem_fts")
cur.execute(
"""
CREATE VIRTUAL TABLE mem_fts USING fts5(
key, value, tags,
content='mem',
content_rowid='id',
tokenize = 'unicode61 remove_diacritics 2'
);
"""
)
cur.executescript(
"""
CREATE TRIGGER IF NOT EXISTS mem_ai
AFTER INSERT
ON mem
BEGIN
INSERT INTO mem_fts(rowid, key, value, tags)
VALUES (new.id,
new.key,
new.value,
new.tags_search);
END;
CREATE TRIGGER IF NOT EXISTS mem_ad
AFTER DELETE
ON mem
BEGIN
INSERT INTO mem_fts(mem_fts, rowid, key, value, tags)
VALUES ('delete', old.id, old.key, old.value, old.tags_search);
END;
CREATE TRIGGER IF NOT EXISTS mem_au
AFTER UPDATE OF key, value, tags_search
ON mem
WHEN (old.key IS NOT new.key)
OR (old.value IS NOT new.value)
OR (old.tags_search IS NOT new.tags_search)
BEGIN
INSERT INTO mem_fts(mem_fts, rowid, key, value, tags)
VALUES ('delete', old.id, old.key, old.value, old.tags_search);
INSERT INTO mem_fts(rowid, key, value, tags)
VALUES (new.id,
new.key,
new.value,
new.tags_search);
END;
"""
)
cur.execute("INSERT INTO mem_fts(mem_fts) VALUES('rebuild')")
cur.execute("SELECT COUNT(*) FROM mem_fts")
after = cur.fetchone()[0]
conn.commit()
return before, after
except sqlite3.OperationalError:
_reset_db_ready()
if attempt == 0:
time.sleep(0.1)
continue
raise
return 0, 0
@pyscript_compile # noqa: F821 # ty:ignore[unresolved-reference]
def _memory_health_check_db_sync() -> tuple[int, int, int]:
"""Synchronously perform a health check on the memory database."""
for attempt in range(2):
try:
_ensure_db_once(force=attempt == 1)
with closing(_get_db_connection()) as conn:
cur = conn.cursor()
cur.execute("SELECT COUNT(*) FROM mem")
rows = cur.fetchone()[0]
now_iso = _utcnow_iso()
cur.execute(
"SELECT COUNT(*) FROM mem WHERE expires_at IS NOT NULL AND expires_at < ?",
(now_iso,),
)
expired = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM mem_fts")
fts_rows = cur.fetchone()[0]
return rows, expired, fts_rows
except sqlite3.OperationalError:
_reset_db_ready()
if attempt == 0:
time.sleep(0.1)
continue
raise
return 0, 0, 0
async def _memory_set_db(
key_norm: str,
value_norm: str,
scope_norm: str,
tags_raw: str,
tags_search: str,
now_iso: str,
expires_at: str | None,
) -> bool:
"""Async wrapper for persisting a memory record."""
return await asyncio.to_thread(
_memory_set_db_sync,
key_norm,
value_norm,
scope_norm,
tags_raw,
tags_search,
now_iso,
expires_at,
)
async def _memory_key_exists_db(key_norm: str) -> bool:
"""Async wrapper for checking key existence."""
return await asyncio.to_thread(_memory_key_exists_db_sync, key_norm)
async def _memory_get_db(key_norm: str) -> tuple[str, dict[str, Any] | None]:
"""Async wrapper for fetching a memory record."""
return await asyncio.to_thread(_memory_get_db_sync, key_norm)
async def _memory_search_db(query: str, limit: int) -> list[dict[str, Any]]:
"""Async wrapper for searching memory records."""
return await asyncio.to_thread(_memory_search_db_sync, query, limit)
async def _memory_forget_db(key_norm: str) -> int:
"""Async wrapper for deleting a memory record."""
return await asyncio.to_thread(_memory_forget_db_sync, key_norm)
async def _memory_purge_expired_db(grace_days: int = 0) -> int:
"""Async wrapper for removing expired memory records."""
return await asyncio.to_thread(_memory_purge_expired_db_sync, grace_days)
async def _memory_reindex_fts_db() -> tuple[int, int]:
"""Async wrapper for rebuilding the FTS index."""
return await asyncio.to_thread(_memory_reindex_fts_db_sync)
async def _memory_health_check_db() -> tuple[int, int, int]:
"""Async wrapper for checking database health."""
return await asyncio.to_thread(_memory_health_check_db_sync)
@service(supports_response="only") # noqa: F821 # ty:ignore[unresolved-reference]
async def memory_set(
key: str,
value: str,
scope: str = "user",
expiration_days: int | str = 180,
tags: str = "",
force_new: bool = False,
):
"""
yaml
name: Memory Set
description: >-
Create or update a memory entry with optional expiration and tags.
When creating a brand-new key, tag overlaps trigger a duplicate_tags error;
successful responses include key_exists to clarify whether the entry was updated or newly inserted.
fields:
key:
name: Key
description: Unique key of the entry.
required: true
example: "car_parking_slot"
selector:
text:
value:
name: Value
description: Value to store (string or JSON-encoded structure).
required: true
example: "Column B2E9"
selector:
text:
scope:
name: Scope
description: Arbitrary grouping label for organization.
default: user
example: user
selector:
select:
options:
- user
- household
- session
expiration_days:
name: Expiration (days)
description: Days until expiration; 0 keeps forever.
default: 180
example: 30
selector:
number:
min: 0
max: 3650
mode: box
tags:
name: Tags
description: Optional space-separated tags for improved search.
example: "car parking slot"
selector:
text:
force_new:
name: Force New
description: Proceed even when tags overlap with other entries.
example: false
selector:
boolean:
"""
key_norm = _normalize_key(key)
if not key_norm or value is None:
_set_result(
"error",
op="set",
key=key_norm or "",
error="key_or_value_missing",
)
log.error("memory_set: missing key or value") # noqa: F821 # ty:ignore[unresolved-reference]
return {
"status": "error",
"op": "set",
"key": key_norm or "",
"error": "key_or_value_missing",
}
try:
expiration_days_i = int(expiration_days)
except (TypeError, ValueError):
expiration_days_i = 0
expiration_days_i = max(expiration_days_i, 0)
expiration_days_i = min(expiration_days_i, EXPIRATION_MAX_DAYS)
if isinstance(force_new, str):
force_new_bool = force_new.strip().lower() in {"1", "true", "yes", "y", "on"}
else:
force_new_bool = force_new
forced_duplicate_override = False
try:
scope_norm = ("" if scope is None else scope.strip()).lower() or "user"
value_norm = _normalize_value(value)
tags_raw = _normalize_value(tags) if tags else _normalize_value(key)
tags_search = _normalize_tags(tags_raw)
now = datetime.now(UTC)
now_iso = now.isoformat()
expires_at = (now + timedelta(days=expiration_days_i)).isoformat() if expiration_days_i else None
key_exists = await _memory_key_exists_db(key_norm)
duplicate_matches: list[tuple[dict[str, Any], float]] = []
if not key_exists and tags_search:
duplicate_matches = await _search_tag_candidates(
tags_search,
exclude_keys={key_norm},
limit=CANDIDATE_CHECK_LIMIT,
log_context="set: duplicate lookup",
)
duplicate_options: list[dict[str, Any]] = []
if duplicate_matches:
duplicate_options = [