Increase security and reformat
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from .search_backend import get_memory_search_backend
|
||||
from .retrieval import retrieve_memories_for_prompt
|
||||
from .search_backend import get_memory_search_backend
|
||||
|
||||
__all__ = ["get_memory_search_backend", "retrieve_memories_for_prompt"]
|
||||
|
||||
@@ -224,7 +224,9 @@ def create_memory_change_request(
|
||||
person_id=person_id or (str(memory.person_id or "") if memory else "") or None,
|
||||
action=normalized_action,
|
||||
status="pending",
|
||||
proposed_memory_kind=str(memory_kind or (memory.memory_kind if memory else "")).strip(),
|
||||
proposed_memory_kind=str(
|
||||
memory_kind or (memory.memory_kind if memory else "")
|
||||
).strip(),
|
||||
proposed_content=dict(content or {}),
|
||||
proposed_confidence_score=(
|
||||
float(confidence_score)
|
||||
@@ -335,7 +337,9 @@ def review_memory_change_request(
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def run_memory_hygiene(*, user_id: int | None = None, dry_run: bool = False) -> dict[str, int]:
|
||||
def run_memory_hygiene(
|
||||
*, user_id: int | None = None, dry_run: bool = False
|
||||
) -> dict[str, int]:
|
||||
now = timezone.now()
|
||||
queryset = MemoryItem.objects.filter(status="active")
|
||||
if user_id is not None:
|
||||
@@ -357,7 +361,9 @@ def run_memory_hygiene(*, user_id: int | None = None, dry_run: bool = False) ->
|
||||
for item in queryset.select_related("conversation", "person"):
|
||||
content = item.content or {}
|
||||
field = str(content.get("field") or content.get("key") or "").strip().lower()
|
||||
text = _clean_value(str(content.get("text") or content.get("value") or "")).lower()
|
||||
text = _clean_value(
|
||||
str(content.get("text") or content.get("value") or "")
|
||||
).lower()
|
||||
if not field or not text:
|
||||
continue
|
||||
scope = (
|
||||
|
||||
@@ -59,7 +59,11 @@ def retrieve_memories_for_prompt(
|
||||
limit=safe_limit,
|
||||
include_statuses=statuses,
|
||||
)
|
||||
ids = [str(hit.memory_id or "").strip() for hit in hits if str(hit.memory_id or "").strip()]
|
||||
ids = [
|
||||
str(hit.memory_id or "").strip()
|
||||
for hit in hits
|
||||
if str(hit.memory_id or "").strip()
|
||||
]
|
||||
scoped = _base_queryset(
|
||||
user_id=int(user_id),
|
||||
person_id=person_id,
|
||||
@@ -82,11 +86,17 @@ def retrieve_memories_for_prompt(
|
||||
"content": item.content or {},
|
||||
"provenance": item.provenance or {},
|
||||
"confidence_score": float(item.confidence_score or 0.0),
|
||||
"expires_at": item.expires_at.isoformat() if item.expires_at else "",
|
||||
"last_verified_at": (
|
||||
item.last_verified_at.isoformat() if item.last_verified_at else ""
|
||||
"expires_at": (
|
||||
item.expires_at.isoformat() if item.expires_at else ""
|
||||
),
|
||||
"last_verified_at": (
|
||||
item.last_verified_at.isoformat()
|
||||
if item.last_verified_at
|
||||
else ""
|
||||
),
|
||||
"updated_at": (
|
||||
item.updated_at.isoformat() if item.updated_at else ""
|
||||
),
|
||||
"updated_at": item.updated_at.isoformat() if item.updated_at else "",
|
||||
"search_score": float(hit.score or 0.0),
|
||||
"search_summary": str(hit.summary or ""),
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
@@ -144,9 +143,10 @@ class ManticoreMemorySearchBackend(BaseMemorySearchBackend):
|
||||
self.base_url = str(
|
||||
getattr(settings, "MANTICORE_HTTP_URL", "http://localhost:9308")
|
||||
).rstrip("/")
|
||||
self.table = str(
|
||||
getattr(settings, "MANTICORE_MEMORY_TABLE", "gia_memory_items")
|
||||
).strip() or "gia_memory_items"
|
||||
self.table = (
|
||||
str(getattr(settings, "MANTICORE_MEMORY_TABLE", "gia_memory_items")).strip()
|
||||
or "gia_memory_items"
|
||||
)
|
||||
self.timeout_seconds = int(getattr(settings, "MANTICORE_HTTP_TIMEOUT", 5) or 5)
|
||||
self._table_cache_key = f"{self.base_url}|{self.table}"
|
||||
|
||||
@@ -163,7 +163,9 @@ class ManticoreMemorySearchBackend(BaseMemorySearchBackend):
|
||||
return dict(payload or {})
|
||||
|
||||
def ensure_table(self) -> None:
|
||||
last_ready = float(self._table_ready_cache.get(self._table_cache_key, 0.0) or 0.0)
|
||||
last_ready = float(
|
||||
self._table_ready_cache.get(self._table_cache_key, 0.0) or 0.0
|
||||
)
|
||||
if (time.time() - last_ready) <= float(self._table_ready_ttl_seconds):
|
||||
return
|
||||
self._sql(
|
||||
@@ -254,7 +256,9 @@ class ManticoreMemorySearchBackend(BaseMemorySearchBackend):
|
||||
try:
|
||||
values.append(self._build_upsert_values_clause(item))
|
||||
except Exception as exc:
|
||||
log.warning("memory-search upsert build failed id=%s err=%s", item.id, exc)
|
||||
log.warning(
|
||||
"memory-search upsert build failed id=%s err=%s", item.id, exc
|
||||
)
|
||||
continue
|
||||
if len(values) >= batch_size:
|
||||
self._sql(
|
||||
@@ -290,7 +294,11 @@ class ManticoreMemorySearchBackend(BaseMemorySearchBackend):
|
||||
where_parts = [f"user_id={int(user_id)}", f"MATCH('{self._escape(needle)}')"]
|
||||
if conversation_id:
|
||||
where_parts.append(f"conversation_id='{self._escape(conversation_id)}'")
|
||||
statuses = [str(item or "").strip() for item in include_statuses if str(item or "").strip()]
|
||||
statuses = [
|
||||
str(item or "").strip()
|
||||
for item in include_statuses
|
||||
if str(item or "").strip()
|
||||
]
|
||||
if statuses:
|
||||
in_clause = ",".join(f"'{self._escape(item)}'" for item in statuses)
|
||||
where_parts.append(f"status IN ({in_clause})")
|
||||
|
||||
Reference in New Issue
Block a user