Give memories a working tier, a long-term tier, and a record of what disagrees

A memory used to have one state: present. A session could not be reloaded
without searching, nothing separated what a person said today from what the
store ingested a year ago, and two statements that disagree about a number
were both handed to the reader as if the store believed both.

Schema v10. Atoms gain session_id, importance, recall_count, last_recalled_at
and archived_at; two tables arrive beside them — consolidations (one watermark
per holder+scenario lane) and conflicts (a pair, the term they disagree on,
both values, and whether anyone has settled it). v_atoms_current now also
hides archived rows, and apply() drops the view before rebuilding it because
CREATE VIEW IF NOT EXISTS would keep the old body and ignore the archive.

Working memory is a read, not a store: `working <holder> <session>` returns
what that session recorded, in order, with no search involved. Consolidation
rolls a lane's new memories into one standing summary and moves the watermark
to the newest memory actually rolled up — not to the clock of the pass, which
made a second pass count the same memories twice. The archive rule is stated
in the command that runs it (recorded before N days, never returned by a
search, importance at or under a floor) rather than hidden in a decay formula
nobody measured; --dry-run names the victims first.

A conflict is recorded when a new memory repeats a label with a different
number in the same holder's same scenario. Retrieval drops the older side
while the conflict is open and both sides answer again once it is settled:
settling is not superseding, and a word-only disagreement is left alone
because a reworded sentence would fire on every paraphrase. Numbers are keyed
to both the memory's subject and the nearest preceding label, measured: the
two thermostat sentences share only their subject, so a nearest-word-only rule
found nothing.

Search now counts the memories it handed back, which is the only use estimate
the store has, and the archive rule reads it. The dense index applies the same
currency rules as the view, so an archived memory cannot stay findable through
one leg while the other hides it.

Two reads over HTTP (working, conflicts) and two tools over MCP
(working_set, memory_conflicts); curation stays with the operator because an
agent should not decide what its own past is worth. remember and supersede
carry a session id; importance does not cross MCP, because recall_count is
observed and importance is stated.

Gates: 366 passed; ruff clean; mypy 24 errors in 9 files, the recorded 9cb46c1
baseline, net 0; pyright 0 on the touched files; go build/vet/test and gofmt
clean; mcp_write_smoke PASS.
This commit is contained in:
ZacharyZhang-NY committed 2026-10-09 07:13:11 -04:00
1 parent 4c6af87fe0
commit dc9cfb5a30
30 files changed
+1814 -229

No files matched your search

+149
View File
@@ -0,0 +1,149 @@
"""Two memories cannot both be true: the store notices, answers with the newer side, and waits.
Settling a disagreement is the holder's act. Until then the store chooses, but it records that
it chose, and it keeps both sides so the choice can be undone.
"""
from __future__ import annotations
from datetime import UTC, datetime
from ely_eye.config import Settings
from ely_eye.db import Database
from ely_eye.memory import MemoryService
from ely_eye.retrieval import RetrievalService
from ely_eye.schemas import MemoryOrigin, SearchFilters
NOW = datetime(2026, 5, 1, 12, 0, tzinfo=UTC)
OLD = "恒温器放在书房,设定 22 度"
NEW = "恒温器调到 24 度"
def _pair(tmp_path, *, scenario: str = "home", newer_holder: str = "wife") -> tuple[MemoryService, Database, list[str]]:
settings = Settings(home=tmp_path, default_holder="wife")
database = Database(settings)
database.init()
service = MemoryService(settings=settings, database=database)
older = service.remember(OLD, holder_id="wife", origin=MemoryOrigin.self_, scenario=scenario).atom_id
newer = service.remember(NEW, holder_id=newer_holder, origin=MemoryOrigin.self_, scenario=scenario).atom_id
return service, database, [older, newer]
def test_a_number_that_changed_leaves_a_conflict_naming_both_sides(tmp_path) -> None:
service, _, ids = _pair(tmp_path)
clashes = service.conflicts("wife")
assert len(clashes) == 1
record = clashes[0]
assert (record.older_atom_id, record.newer_atom_id) == (ids[0], ids[1])
assert record.older_value == "22" and record.newer_value == "24"
assert record.terms, "the record names what the two memories disagree about"
assert record.resolved_at is None
def test_repeating_the_same_statement_adds_no_memory_and_no_conflict(tmp_path) -> None:
service, database, _ = _pair(tmp_path)
before = len(service.conflicts("wife"))
service.remember(NEW, holder_id="wife", origin=MemoryOrigin.self_, scenario="home")
assert database.atom_count() == 2, "the same holder stating the same sentence holds one memory"
assert len(service.conflicts("wife")) == before, (
"a repeat cannot manufacture a second disagreement"
)
def test_a_memory_that_agrees_leaves_nothing_to_settle(tmp_path) -> None:
settings = Settings(home=tmp_path, default_holder="wife")
database = Database(settings)
database.init()
service = MemoryService(settings=settings, database=database)
service.remember(OLD, holder_id="wife", origin=MemoryOrigin.self_, scenario="home")
service.remember("恒温器设定 22 度", holder_id="wife", origin=MemoryOrigin.self_, scenario="home")
assert service.conflicts("wife") == [], (
"restating the same reading in other words is agreement, and the rule must not flinch at it"
)
def test_two_holders_who_disagree_are_not_a_conflict_in_one_memory(tmp_path) -> None:
service, _, _ = _pair(tmp_path, newer_holder="husband")
assert service.conflicts("wife") == [], "the husband changing his own mind is not the wife's contradiction"
def test_a_change_in_another_scenario_is_a_different_question(tmp_path) -> None:
settings = Settings(home=tmp_path, default_holder="wife")
database = Database(settings)
database.init()
service = MemoryService(settings=settings, database=database)
service.remember(OLD, holder_id="wife", origin=MemoryOrigin.self_, scenario="home")
service.remember(NEW, holder_id="wife", origin=MemoryOrigin.self_, scenario="office")
assert service.conflicts("wife") == [], (
"the office thermostat and the home thermostat are two thermostats"
)
def test_a_supersede_already_chose_a_side_and_records_no_conflict(tmp_path) -> None:
service, _, ids = _pair(tmp_path, scenario="")
before = len(service.conflicts("wife"))
service.supersede(ids[0], text="恒温器调到 26 度", holder_id="wife")
assert len(service.conflicts("wife")) == before, (
"a replacement is a decision, not a disagreement waiting to be settled"
)
def test_a_disagreement_about_words_rather_than_numbers_is_left_alone(tmp_path) -> None:
settings = Settings(home=tmp_path, default_holder="wife")
database = Database(settings)
database.init()
service = MemoryService(settings=settings, database=database)
service.remember("钥匙在玄关抽屉里", holder_id="wife", origin=MemoryOrigin.self_, scenario="home")
service.remember("钥匙在车里", holder_id="wife", origin=MemoryOrigin.self_, scenario="home")
assert service.conflicts("wife") == [], (
"the numeric rule is the one that can be checked; a reworded sentence would fire on every paraphrase"
)
def test_an_open_conflict_makes_the_store_answer_with_the_newer_side(tmp_path) -> None:
_, database, ids = _pair(tmp_path)
retrieval = RetrievalService(settings=Settings(home=tmp_path, default_holder="wife"), database=database)
hits = retrieval.search("恒温器", filters=SearchFilters(as_holder="wife")).hits
assert [hit.atom.atom_id for hit in hits] == [ids[1]], (
"both memories match the words; only the disagreement decides which one answers"
)
def test_settling_a_conflict_gives_both_sides_back(tmp_path) -> None:
service, database, ids = _pair(tmp_path)
retrieval = RetrievalService(settings=Settings(home=tmp_path, default_holder="wife"), database=database)
clash = service.conflicts("wife")[0]
assert service.resolve_conflicts([clash.conflict_id], at=NOW) == 1
assert service.conflicts("wife") == []
hits = retrieval.search("恒温器", filters=SearchFilters(as_holder="wife")).hits
assert {hit.atom.atom_id for hit in hits} == set(ids), (
"a settled disagreement stops the store from choosing; both memories are current again"
)
assert database.get_atoms(ids)[0].valid_to is None, "settling is not the same as superseding"
def test_a_conflict_belongs_to_the_memory_it_was_found_in(tmp_path) -> None:
service, _, ids = _pair(tmp_path)
assert service.conflicts("wife"), "the disagreement exists before the forget"
forgotten = service.forget(atom_id=ids[1], reason="the reading was wrong")
assert forgotten.atoms_deleted == 1
assert service.conflicts("wife") == [], (
"one side is gone, so there is nothing left to settle and no ghost row to list"
)