d4b4e8b0de
- session_search_tool_common/_discover folded back into session_search_tool (single module, no re-export shim) - MemoryStore: one locked _mutate pipeline for add/replace/remove/batch; _reload_target/_read_entries_checked/_commit/_batch_error/_previews inlined - memory_tool: _STORE_ACTIONS table now carries store call + gate text; _target_label/_missing_old_text_error inlined - session_search: _discovery_entry (title + FTS hits), _same_lineage, _loud (error-level DB failure -> tool_error), _session_end_reason/_normalize_title_query inlined - graph client: iterate_pages folded into collect_paginated (its only caller), _decode_json_or, try/else retry loop - docstrings compacted by hand keeping every WHY/invariant; tool schemas byte-identical; SQL untouched
440 lines
23 KiB
Python
440 lines
23 KiB
Python
"""MemoryStore — bounded, file-backed curated memory (MEMORY.md / USER.md).
|
|
Entries are joined by ``ENTRY_DELIMITER``; budgets are in chars (model-independent).
|
|
Module state that tests monkeypatch (``get_memory_dir``, ``fcntl``/``msvcrt``) stays
|
|
in ``tools.memory_tool`` and is read lazily."""
|
|
|
|
import logging
|
|
import time
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
from typing import Any, Dict, List, Optional, Tuple
|
|
|
|
from utils import atomic_write_text
|
|
from tools.threat_patterns import first_threat_message as _first_threat_message
|
|
|
|
logger = logging.getLogger("tools.memory_tool")
|
|
|
|
# Block header prefixes rendered by _render_block; agent/conversation_compression.py
|
|
# matches them to detect a leftover block for an emptied target — keep in lockstep.
|
|
MEMORY_BLOCK_HEADERS = {
|
|
"memory": "MEMORY (your personal notes)", "user": "USER PROFILE (who the user is)"}
|
|
|
|
ENTRY_DELIMITER = "\n§\n"
|
|
|
|
|
|
def _memory_dir() -> Path:
|
|
from tools import memory_tool
|
|
return memory_tool.get_memory_dir()
|
|
|
|
|
|
def _scan_memory_content(content: str) -> Optional[str]:
|
|
"""Error string if *content* matches injection/exfil patterns. Strict scope:
|
|
memory enters the system prompt, so a poisoned entry persists across sessions."""
|
|
return _first_threat_message(content, scope="strict")
|
|
|
|
|
|
def _drift_error(path: "Path", bak_path: str) -> Dict[str, Any]:
|
|
"""External drift: the file wouldn't round-trip, so flushing would discard content."""
|
|
return {"success": False, "error": (
|
|
f"Refusing to write {path.name}: file on disk has content that wouldn't round-trip "
|
|
f"through the memory tool (likely added by the patch tool, a shell append, a manual edit, "
|
|
f"or a concurrent session). A snapshot was saved to {bak_path}. Resolve the drift first — "
|
|
f"either rewrite the file as a clean §-delimited list of entries, or move the extra "
|
|
f"content out — then retry. This guard exists to prevent silent data loss (issue #26045)."
|
|
), "drift_backup": bak_path, "remediation": (
|
|
"Open the .bak file, integrate the missing entries into the memory tool one at a time via "
|
|
"memory(action=add, content=...), then remove or rewrite the original file to a clean state.")}
|
|
|
|
|
|
def _read_failed_error(path: "Path") -> Dict[str, Any]:
|
|
"""Existing-but-unreadable file: saving from an assumed-empty view would wipe it."""
|
|
return {"success": False, "error": (
|
|
f"Refusing to write {path.name}: the file exists on disk but could not be read right now "
|
|
f"(temporarily locked by another program, a permission change, invalid/corrupt text encoding, "
|
|
f"or a filesystem error). Treating an unreadable file as empty and saving would wipe existing "
|
|
f"memory, so the write is refused. Nothing was changed — retry in a moment.")}
|
|
|
|
|
|
def _find_unique_match(entries: List[str], old_text: str) -> Tuple[Optional[int], bool]:
|
|
"""``(index, ambiguous)`` for entries containing *old_text*. Exact-duplicate
|
|
matches are safe (first wins); distinct matches → ``(None, True)``."""
|
|
matches = [i for i, e in enumerate(entries) if old_text in e]
|
|
if len({entries[i] for i in matches}) > 1:
|
|
return None, True
|
|
return (matches[0] if matches else None), False
|
|
|
|
|
|
class MemoryStore:
|
|
"""Bounded curated memory with file persistence; one instance per AIAgent.
|
|
``_system_prompt_snapshot`` is frozen at load time (prefix-cache stable);
|
|
``memory_entries`` / ``user_entries`` are live state persisted to disk."""
|
|
|
|
# Failed consolidation attempts (overflow / zero-match) allowed per turn before
|
|
# a TERMINAL "save skipped" result, so a fragile replace/add can't loop the turn
|
|
# to budget exhaustion and suppress the user's reply.
|
|
_MAX_CONSOLIDATION_FAILURES_PER_TURN = 3
|
|
|
|
def __init__(self, memory_char_limit: int = 2200, user_char_limit: int = 1375, *,
|
|
memory_enabled: bool = True, user_profile_enabled: bool = True):
|
|
self.memory_entries: List[str] = []
|
|
self.user_entries: List[str] = []
|
|
self.memory_char_limit = memory_char_limit
|
|
self.user_char_limit = user_char_limit
|
|
self.memory_enabled = memory_enabled
|
|
self.user_profile_enabled = user_profile_enabled
|
|
self._system_prompt_snapshot: Dict[str, str] = {"memory": "", "user": ""}
|
|
self._consolidation_failures = 0 # per turn; reset by reset_consolidation_failures()
|
|
|
|
def target_enabled(self, target: str) -> bool:
|
|
"""Return whether this session's selected built-in store is writable."""
|
|
return self.user_profile_enabled if target == "user" else self.memory_enabled
|
|
|
|
def reset_consolidation_failures(self) -> None:
|
|
"""Reset the per-turn consolidation-failure counter (call at turn start)."""
|
|
self._consolidation_failures = 0
|
|
|
|
def _consolidation_failure(self, response: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""Count a consolidation failure: under the per-turn cap return ``response``
|
|
(it says how to retry); past it a TERMINAL result so the model stops looping."""
|
|
self._consolidation_failures += 1
|
|
if self._consolidation_failures <= self._MAX_CONSOLIDATION_FAILURES_PER_TURN:
|
|
return response
|
|
return {"success": False, "done": True, "error": (
|
|
f"Memory consolidation failed {self._consolidation_failures} times this turn. Stop retrying "
|
|
"memory calls — leave memory unchanged for now and continue with your reply to the user. "
|
|
"The fact can be saved in a later turn.")}
|
|
|
|
def load_from_disk(self):
|
|
"""Load MEMORY.md / USER.md and capture the frozen system-prompt snapshot.
|
|
Threat hits are replaced by a ``[BLOCKED: …]`` placeholder in the SNAPSHOT only;
|
|
live lists keep the raw text so the user can see and remove poisoned entries
|
|
(dropping them silently would hide the attack)."""
|
|
mem_dir = _memory_dir()
|
|
mem_dir.mkdir(parents=True, exist_ok=True)
|
|
# Deduplicate (order-preserving, first occurrence wins).
|
|
self.memory_entries = list(dict.fromkeys(self._read_file(mem_dir / "MEMORY.md")))
|
|
self.user_entries = list(dict.fromkeys(self._read_file(mem_dir / "USER.md")))
|
|
self._system_prompt_snapshot = {
|
|
"memory": self._render_block("memory", self._sanitize_entries_for_snapshot(self.memory_entries, "MEMORY.md")),
|
|
"user": self._render_block("user", self._sanitize_entries_for_snapshot(self.user_entries, "USER.md"))}
|
|
|
|
@staticmethod
|
|
def _sanitize_entries_for_snapshot(entries: List[str], filename: str) -> List[str]:
|
|
"""*entries* with threat matches replaced by a ``[BLOCKED: …]`` placeholder
|
|
(strict scope, same as writes); empty / already-blocked entries pass through."""
|
|
from tools.threat_patterns import scan_for_threats
|
|
|
|
def _one(entry):
|
|
findings = scan_for_threats(entry, scope="strict") if entry and not entry.startswith("[BLOCKED:") else None
|
|
if not findings:
|
|
return entry
|
|
logger.warning("Memory entry from %s blocked at load time: %s", filename, ", ".join(findings))
|
|
return (f"[BLOCKED: {filename} entry contained threat pattern(s): {', '.join(findings)}. "
|
|
f"Removed from system prompt; use memory(action=remove) to delete the original.]")
|
|
|
|
return [_one(e) for e in entries]
|
|
|
|
@staticmethod
|
|
@contextmanager
|
|
def _file_lock(path: Path):
|
|
"""Exclusive lock on a separate .lock file so the memory file itself can
|
|
still be atomically replaced."""
|
|
from tools import memory_tool as _mt # fcntl/msvcrt live (and are patched) there
|
|
fcntl, msvcrt = _mt.fcntl, _mt.msvcrt
|
|
lock_path = path.with_suffix(path.suffix + ".lock")
|
|
lock_path.parent.mkdir(parents=True, exist_ok=True)
|
|
if fcntl is None and msvcrt is None:
|
|
yield
|
|
return
|
|
with open(lock_path, "a+", encoding="utf-8") as fd:
|
|
def _flock(unlock: bool):
|
|
if fcntl:
|
|
fcntl.flock(fd, fcntl.LOCK_UN if unlock else fcntl.LOCK_EX)
|
|
else:
|
|
fd.seek(0)
|
|
msvcrt.locking(fd.fileno(), msvcrt.LK_UNLCK if unlock else msvcrt.LK_LOCK, 1)
|
|
_flock(False)
|
|
try:
|
|
yield
|
|
finally:
|
|
try:
|
|
_flock(True)
|
|
except OSError:
|
|
pass
|
|
|
|
@staticmethod
|
|
def _path_for(target: str) -> Path:
|
|
return _memory_dir() / ("USER.md" if target == "user" else "MEMORY.md")
|
|
|
|
def _reload_or_error(self, target: str, *, skip_drift: bool = False) -> Optional[Dict[str, Any]]:
|
|
"""Re-read entries from disk (under lock) before mutating; return the abort
|
|
error dict or None. Aborts on external drift (flushing would discard
|
|
un-roundtrippable content) and on an existing-but-unreadable file (even
|
|
append-only ``add`` rewrites the whole file). Drift check and parse use the
|
|
SAME raw snapshot — a failed second read used to count as "no drift"."""
|
|
path = self._path_for(target)
|
|
raw, read_ok = self._read_raw_checked(path)
|
|
if not read_ok:
|
|
return _read_failed_error(path)
|
|
bak = None if skip_drift else self._detect_external_drift(target, raw)
|
|
self._set_entries(target, list(dict.fromkeys(self._parse_entries(raw))))
|
|
return _drift_error(path, bak) if bak else None
|
|
|
|
def save_to_disk(self, target: str):
|
|
"""Persist entries to the appropriate file. Called after every mutation."""
|
|
_memory_dir().mkdir(parents=True, exist_ok=True)
|
|
self._write_file(self._path_for(target), self._entries_for(target))
|
|
|
|
def _entries_for(self, target: str) -> List[str]:
|
|
return self.user_entries if target == "user" else self.memory_entries
|
|
|
|
def _set_entries(self, target: str, entries: List[str]):
|
|
setattr(self, "user_entries" if target == "user" else "memory_entries", entries)
|
|
|
|
def _char_count(self, target: str) -> int:
|
|
return len(ENTRY_DELIMITER.join(self._entries_for(target)))
|
|
|
|
def _char_limit(self, target: str) -> int:
|
|
return self.user_char_limit if target == "user" else self.memory_char_limit
|
|
|
|
def _usage(self, target: str) -> str:
|
|
return f"{self._char_count(target):,}/{self._char_limit(target):,}"
|
|
|
|
def _usage_pct(self, target: str, current: int) -> str:
|
|
"""``"<pct>% — <current>/<limit> chars"`` for the given target."""
|
|
limit = self._char_limit(target)
|
|
pct = min(100, int((current / limit) * 100)) if limit > 0 else 0
|
|
return f"{pct}% — {current:,}/{limit:,} chars"
|
|
|
|
def _failure_with_entries(self, target: str, message: str) -> Dict[str, Any]:
|
|
"""Consolidation failure carrying the live entries so the model can consolidate."""
|
|
return self._consolidation_failure({"success": False, "error": message,
|
|
"current_entries": self._entries_for(target), "usage": self._usage(target)})
|
|
|
|
def _locate(self, target: str, old_text: str, verb: str):
|
|
"""Resolve *old_text* to a unique entry index, or an error dict."""
|
|
entries = self._entries_for(target)
|
|
idx, ambiguous = _find_unique_match(entries, old_text)
|
|
if ambiguous:
|
|
return None, {"success": False, "error": f"Multiple entries matched '{old_text}'. Be more specific.",
|
|
"matches": [e[:80] + ("..." if len(e) > 80 else "") for e in entries if old_text in e]}
|
|
if idx is None:
|
|
return None, self._consolidation_failure({
|
|
"success": False,
|
|
"error": f"No entry matched '{old_text}'. Check current_entries below and retry with the exact text of the entry you want to {verb}.",
|
|
"current_entries": entries})
|
|
return idx, None
|
|
|
|
def _mutate(self, target: str, mutate, *, skip_drift: bool = False) -> Dict[str, Any]:
|
|
"""Lock, reload, run ``mutate(entries, limit)`` -> ``(new_entries, message)`` or an
|
|
error dict, then persist and return the success response."""
|
|
with self._file_lock(self._path_for(target)):
|
|
err = self._reload_or_error(target, skip_drift=skip_drift)
|
|
if err:
|
|
return err
|
|
result = mutate(self._entries_for(target), self._char_limit(target))
|
|
if isinstance(result, dict):
|
|
return result
|
|
entries, message = result
|
|
self._set_entries(target, entries)
|
|
self.save_to_disk(target)
|
|
return self._success_response(target, message)
|
|
|
|
def add(self, target: str, content: str) -> Dict[str, Any]:
|
|
"""Append a new entry. Returns error if it would exceed the char limit."""
|
|
content = content.strip()
|
|
if not content:
|
|
return {"success": False, "error": "Content cannot be empty."}
|
|
scan_error = _scan_memory_content(content)
|
|
if scan_error:
|
|
return {"success": False, "error": scan_error}
|
|
|
|
def _add(entries, limit):
|
|
if content in entries:
|
|
return self._success_response(target, "Entry already exists (no duplicate added).")
|
|
if len(ENTRY_DELIMITER.join(entries + [content])) > limit:
|
|
return self._failure_with_entries(target, (
|
|
f"Memory at {self._char_count(target):,}/{limit:,} chars. Adding this entry "
|
|
f"({len(content)} chars) would exceed the limit. Consolidate now: use 'replace' to merge "
|
|
f"overlapping entries into shorter ones or 'remove' stale or less important entries (see "
|
|
f"current_entries below), then retry this add — all in this turn."))
|
|
return entries + [content], "Entry added."
|
|
|
|
# Append-only: skip the drift guard (appending never clobbers foreign
|
|
# content) but still refuse a failed read — add rewrites the WHOLE file.
|
|
return self._mutate(target, _add, skip_drift=True)
|
|
|
|
def replace(self, target: str, old_text: str, new_content: str) -> Dict[str, Any]:
|
|
"""Find entry containing old_text substring, replace it with new_content."""
|
|
new_content = new_content.strip()
|
|
if not old_text.strip():
|
|
return {"success": False, "error": "old_text cannot be empty."}
|
|
if not new_content:
|
|
return {"success": False, "error": "new_content cannot be empty. Use 'remove' to delete entries."}
|
|
scan_error = _scan_memory_content(new_content)
|
|
if scan_error:
|
|
return {"success": False, "error": scan_error}
|
|
return self._edit(target, old_text.strip(), new_content)
|
|
|
|
def remove(self, target: str, old_text: str) -> Dict[str, Any]:
|
|
"""Remove the entry containing old_text substring."""
|
|
if not old_text.strip():
|
|
return {"success": False, "error": "old_text cannot be empty."}
|
|
return self._edit(target, old_text.strip(), None)
|
|
|
|
def _edit(self, target: str, old_text: str, new_content: Optional[str]) -> Dict[str, Any]:
|
|
"""Locked replace (``new_content`` set) or remove (None) of the unique entry matching *old_text*."""
|
|
def _apply(entries, limit):
|
|
idx, err = self._locate(target, old_text, "replace" if new_content else "remove")
|
|
if err:
|
|
return err
|
|
if new_content is None:
|
|
return entries[:idx] + entries[idx + 1:], "Entry removed."
|
|
replaced = entries[:idx] + [new_content] + entries[idx + 1:]
|
|
new_total = len(ENTRY_DELIMITER.join(replaced))
|
|
if new_total > limit:
|
|
return self._failure_with_entries(target, (
|
|
f"Replacement would put memory at {new_total:,}/{limit:,} chars. Shorten the new content, "
|
|
f"or 'remove' other stale or less important entries to make room (see current_entries "
|
|
f"below), then retry — all in this turn."))
|
|
return replaced, "Entry replaced."
|
|
|
|
return self._mutate(target, _apply)
|
|
|
|
@staticmethod
|
|
def _apply_batch_op(working: List[str], act: str, content: str, old_text: str, pos: str) -> Optional[str]:
|
|
"""Apply one batch op to *working* in place; return an error message or None."""
|
|
if act == "add":
|
|
if not content:
|
|
return f"{pos}: content is required."
|
|
if content not in working: # idempotent -- skip duplicate, don't fail the batch
|
|
working.append(content)
|
|
return None
|
|
if act not in ("replace", "remove"):
|
|
return f"{pos}: unknown action. Use add, replace, or remove."
|
|
if not old_text:
|
|
return f"{pos}: old_text is required."
|
|
if act == "replace" and not content:
|
|
return f"{pos}: content is required (use action='remove' to delete)."
|
|
idx, ambiguous = _find_unique_match(working, old_text)
|
|
if ambiguous:
|
|
return f"{pos}: '{old_text}' matched multiple distinct entries -- be more specific."
|
|
if idx is None:
|
|
return f"{pos}: no entry matched '{old_text}'."
|
|
if act == "replace":
|
|
working[idx] = content
|
|
else:
|
|
working.pop(idx)
|
|
return None
|
|
|
|
def apply_batch(self, target: str, operations: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
"""Apply add/replace/remove ops to one target atomically against the FINAL
|
|
budget, so one call can free space and add entries. All-or-nothing: any
|
|
malformed / unmatched op or an over-limit result writes NOTHING and returns
|
|
the first failure plus live state."""
|
|
if not operations:
|
|
return {"success": False, "error": "operations list is empty."}
|
|
|
|
ops = [op or {} for op in operations]
|
|
# Scan every add/replace content BEFORE touching disk -- one poisoned op rejects the batch.
|
|
for i, op in enumerate(ops):
|
|
scan_error = op.get("action") in {"add", "replace"} and op.get("content") and _scan_memory_content(op["content"])
|
|
if scan_error:
|
|
return {"success": False, "error": f"Operation {i + 1}: {scan_error}"}
|
|
|
|
def _apply(entries, limit):
|
|
working = list(entries) # only committed if the whole batch validates
|
|
for i, op in enumerate(ops):
|
|
act = op.get("action")
|
|
content = (op.get("content") or op.get("new_text") or "").strip()
|
|
old_text = (op.get("old_text") or "").strip()
|
|
pos = f"Operation {i + 1} ({act or 'unknown'})"
|
|
msg = self._apply_batch_op(working, act, content, old_text, pos)
|
|
if msg:
|
|
return self._failure_with_entries(
|
|
target, msg + " No operations were applied (batch is all-or-nothing).")
|
|
# Budget check against the FINAL state only.
|
|
new_total = len(ENTRY_DELIMITER.join(working))
|
|
if new_total > limit:
|
|
return self._failure_with_entries(target, (
|
|
f"After applying all {len(operations)} operations, memory would be at "
|
|
f"{new_total:,}/{limit:,} chars -- over the limit. Remove or shorten more "
|
|
f"entries in the same batch (see current_entries below), then retry."))
|
|
return working, f"Applied {len(operations)} operation(s)."
|
|
|
|
return self._mutate(target, _apply)
|
|
|
|
def format_for_system_prompt(self, target: str) -> Optional[str]:
|
|
"""Frozen load-time snapshot for the system prompt (NOT live state — mid-session
|
|
writes don't touch it, preserving the prefix cache); None if empty."""
|
|
return self._system_prompt_snapshot.get(target, "") or None
|
|
|
|
def _success_response(self, target: str, message: str = None) -> Dict[str, Any]:
|
|
# A successful write is progress: reset the per-turn (consecutive) failure budget.
|
|
self._consolidation_failures = 0
|
|
# TERMINAL and WITHOUT the entries list: echoing entries invites the model to
|
|
# "find more to fix" and re-issue the same ops. Entries only appear on errors.
|
|
return {"success": True, "done": True, "target": target,
|
|
"usage": self._usage_pct(target, self._char_count(target)),
|
|
"entry_count": len(self._entries_for(target)), **({"message": message} if message else {}),
|
|
"note": "Write saved. This update is complete — do not repeat it."}
|
|
|
|
def _render_block(self, target: str, entries: List[str]) -> str:
|
|
"""Render a system prompt block with header and usage indicator."""
|
|
if not entries:
|
|
return ""
|
|
content = ENTRY_DELIMITER.join(entries)
|
|
title = MEMORY_BLOCK_HEADERS["user" if target == "user" else "memory"]
|
|
separator = "═" * 46
|
|
return f"{separator}\n{title} [{self._usage_pct(target, len(content))}]\n{separator}\n{content}"
|
|
|
|
@staticmethod
|
|
def _read_raw_checked(path: Path) -> Tuple[str, bool]:
|
|
"""``(raw, read_ok)``; ``read_ok`` is False ONLY when the file EXISTS but can't
|
|
be read (absent → ``("", True)``). Decoding stays STRICT: ``errors="replace"``
|
|
would hand callers a lossy view that a save then persists. ``utf-8-sig`` strips
|
|
a Notepad BOM that otherwise glues U+FEFF onto the first entry forever."""
|
|
if not path.exists():
|
|
return "", True
|
|
try:
|
|
return path.read_text(encoding="utf-8-sig"), True
|
|
except (OSError, UnicodeDecodeError):
|
|
return "", False
|
|
|
|
@staticmethod
|
|
def _parse_entries(raw: str) -> List[str]:
|
|
"""Stripped, non-empty entries; splits on the FULL delimiter so a bare "§" survives."""
|
|
return [e for e in (x.strip() for x in raw.split(ENTRY_DELIMITER)) if e]
|
|
|
|
@staticmethod
|
|
def _read_file(path: Path) -> List[str]:
|
|
"""Entries of a memory file ([] on any error). Read-only callers only
|
|
(``load_from_disk``, learning_mutations); mutation paths must use
|
|
``_read_raw_checked`` so they can refuse to overwrite an unreadable file."""
|
|
return MemoryStore._parse_entries(MemoryStore._read_raw_checked(path)[0])
|
|
|
|
def _detect_external_drift(self, target: str, raw: str) -> Optional[str]:
|
|
"""Backup path if *raw* shows external drift, else None. Signals: round-trip
|
|
mismatch, or one entry over the whole-file limit (no tool-written entry can —
|
|
an external writer appended free-form text). Snapshots to ``.bak.<ts>``."""
|
|
if not raw.strip():
|
|
return None
|
|
parsed = self._parse_entries(raw)
|
|
if raw.strip() == ENTRY_DELIMITER.join(parsed) and max(map(len, parsed), default=0) <= self._char_limit(target):
|
|
return None
|
|
path = self._path_for(target)
|
|
bak_path = path.with_suffix(path.suffix + f".bak.{int(time.time())}")
|
|
try:
|
|
bak_path.write_text(raw, encoding="utf-8")
|
|
except OSError:
|
|
return str(bak_path) + " (BACKUP FAILED — file unchanged on disk)"
|
|
return str(bak_path)
|
|
|
|
@staticmethod
|
|
def _write_file(path: Path, entries: List[str]):
|
|
"""Atomic temp-file + rename: readers never see a truncated file."""
|
|
try:
|
|
atomic_write_text(path, ENTRY_DELIMITER.join(entries), tmp_prefix=".mem_")
|
|
except OSError as e:
|
|
raise RuntimeError(f"Failed to write memory file {path}: {e}")
|