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

+28
View File
@@ -67,3 +67,31 @@ def test_a_forget_that_names_nothing_is_rejected(client: TestClient) -> None:
response = client.post("/api/memory/forget", json={"reason": "vague"})
assert response.status_code == 422
assert "exactly one" in response.json()["detail"]
def test_a_session_survives_the_http_face(client: TestClient) -> None:
first = client.post("/api/memory", json={"text": "烤箱要除垢", "session_id": "cooking"}).json()
second = client.post("/api/memory", json={"text": "面粉在储物架第二层", "session_id": "cooking"}).json()
client.post("/api/memory", json={"text": "周报周五交"})
working = client.get("/api/memory/working", params={"holder": "wife", "session": "cooking"})
assert working.status_code == 200
assert [atom["atom_id"] for atom in working.json()] == [first["atom_id"], second["atom_id"]]
def test_working_refuses_a_session_it_cannot_name(client: TestClient) -> None:
response = client.get("/api/memory/working", params={"holder": "wife", "session": ""})
assert response.status_code == 422
assert "a working set needs a session" in response.json()["detail"]
def test_conflicts_reports_both_sides_of_a_disagreement(client: TestClient) -> None:
client.post("/api/memory", json={"text": "恒温器放在书房,设定 22 度", "scenario": "home"})
client.post("/api/memory", json={"text": "恒温器调到 24 度", "scenario": "home"})
listed = client.get("/api/memory/conflicts").json()
assert [(row["older_value"], row["newer_value"]) for row in listed] == [("22", "24")]
assert listed[0]["holder_id"] == "wife"
assert client.get("/api/memory/conflicts", params={"holder": "husband"}).json() == []
+52
View File
@@ -106,3 +106,55 @@ def test_the_cli_answers_in_json_a_machine_can_read(db: Database) -> None:
result = runner.invoke(app, ["remember", "钥匙在玄关抽屉", "--holder", "wife"])
assert result.exit_code == 0
assert json.loads(result.output)["text"] == "钥匙在玄关抽屉"
def test_working_lists_one_session_in_the_order_it_was_recorded(db: Database) -> None:
first = run("remember", "烤箱要除垢", "--session", "cooking")
second = run("remember", "面粉在储物架第二层", "--session", "cooking")
run("remember", "周报周五交")
working = run("working", "husband", "cooking")
assert [atom["atom_id"] for atom in working] == [first["atom_id"], second["atom_id"]]
def test_working_refuses_to_guess_which_session_is_working(db: Database) -> None:
result = runner.invoke(app, ["working", "husband", ""])
assert result.exit_code != 0
assert "a working set needs a session" in str(result.exception)
def test_consolidate_rolls_a_lane_up_once_and_then_says_nothing_new(db: Database) -> None:
run("remember", "烤箱要除垢", "--scenario", "home")
record = run("consolidate", "--scenario", "home", "--holder", "husband")
assert record["atom_count"] == 1 and record["rollup_atom_id"]
result = runner.invoke(app, ["consolidate", "--scenario", "home", "--holder", "husband"])
assert result.exit_code == 0
assert result.output.strip() == "nothing new to roll up"
def test_the_archive_can_be_read_before_it_moves_anything(db: Database) -> None:
atom = run("remember", "前租户的 wifi 密码")
preview = run("archive", "--older-than-days", "0", "--dry-run")
assert preview["dry_run"] is True and preview["atom_ids"] == [atom["atom_id"]]
assert run("archived") == []
moved = run("archive", "--older-than-days", "0")
assert moved["dry_run"] is False and moved["atom_ids"] == [atom["atom_id"]]
assert [row["atom_id"] for row in run("archived")] == [atom["atom_id"]]
assert run("memory-restore", atom["atom_id"]) == {"restored": 1}
assert run("archived") == []
def test_the_cli_shows_and_settles_a_disagreement(db: Database) -> None:
run("remember", "恒温器放在书房,设定 22 度", "--scenario", "home")
run("remember", "恒温器调到 24 度", "--scenario", "home")
listed = run("conflicts", "--holder", "husband")
assert len(listed) == 1
assert listed[0]["older_value"] == "22" and listed[0]["newer_value"] == "24"
assert run("resolve-conflicts", str(listed[0]["conflict_id"])) == {"resolved": 1}
assert run("conflicts", "--holder", "husband") == []
+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"
)
+70
View File
@@ -242,3 +242,73 @@ def test_a_second_version_database_rebuilds_its_index_on_upgrade(tmp_path: Path)
assert [atom_id for atom_id, _ in search_fts(upgraded, "钥匙", 5)] == ["key"]
with upgraded.connect() as connection:
assert int(connection.execute("PRAGMA user_version").fetchone()[0]) == SCHEMA_VERSION
def test_a_fresh_database_carries_the_tier_columns_and_their_tables(tmp_path: Path) -> None:
database = _database(tmp_path)
columns = _atom_columns(database)
for column in (
"session_id",
"importance",
"recall_count",
"last_recalled_at",
"archived_at",
):
assert column in columns, column
with database.connect() as connection:
tables = {
row[0]
for row in connection.execute("SELECT name FROM sqlite_master WHERE type = 'table'").fetchall()
}
assert {"consolidations", "conflicts"} <= tables, (
"a lane's watermark and a store's disagreements are state, so they live in tables"
)
def test_the_current_view_hides_an_archived_memory_without_deleting_it(tmp_path: Path) -> None:
database = _database(tmp_path)
_seed_source(database)
database.upsert_atoms([_atom("kept"), _atom("archived")])
with database.connect() as connection:
connection.execute(
"UPDATE atoms SET archived_at = ? WHERE atom_id = 'archived'", (NOW.isoformat(),)
)
visible = connection.execute("SELECT atom_id FROM v_atoms_current ORDER BY atom_id").fetchall()
assert [row[0] for row in visible] == ["kept"], "the archive is a tier, not a deletion"
assert database.atom_count() == 2
def test_a_ninth_version_database_gains_the_tiers_in_place(tmp_path: Path) -> None:
"""A ninth-version store had no tiers at all; the upgrade must add them and rebuild the view."""
database = _database(tmp_path)
_seed_source(database)
database.upsert_atoms([_atom("key")])
with database.connect() as connection:
# The downgrade removes what a ninth build never had, including the view and indexes
# that reference those columns, so the reopened store really is a ninth-version one.
connection.executescript(
"""
DROP VIEW IF EXISTS v_atoms_current;
DROP INDEX IF EXISTS idx_atoms_session;
DROP INDEX IF EXISTS idx_atoms_archived;
ALTER TABLE atoms DROP COLUMN session_id;
ALTER TABLE atoms DROP COLUMN importance;
ALTER TABLE atoms DROP COLUMN recall_count;
ALTER TABLE atoms DROP COLUMN last_recalled_at;
ALTER TABLE atoms DROP COLUMN archived_at;
DROP TABLE consolidations;
DROP TABLE conflicts;
PRAGMA user_version = 9;
"""
)
upgraded = _database(tmp_path)
assert "archived_at" in _atom_columns(upgraded)
assert [atom.atom_id for atom in upgraded.list_atoms()] == ["key"], "the upgrade kept the memories"
with upgraded.connect() as connection:
assert int(connection.execute("PRAGMA user_version").fetchone()[0]) == SCHEMA_VERSION
connection.execute("UPDATE atoms SET archived_at = ? WHERE atom_id = 'key'", (NOW.isoformat(),))
hidden = connection.execute("SELECT count(*) FROM v_atoms_current").fetchone()[0]
assert hidden == 0, (
"the old view body survived the upgrade, so an archived memory would still answer"
)
+206
View File
@@ -0,0 +1,206 @@
"""Memory tiers: a session reloads in order, a lane rolls up, and what nobody uses steps aside.
Nothing here deletes. Every tier move is reversible, because the store's guess about what
mattered is a guess, and a wrong guess should cost a query, not a memory.
"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
import pytest
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)
SESSION = "cooking-2026-10"
def _service(tmp_path, holder: str = "wife") -> tuple[MemoryService, Database]:
settings = Settings(home=tmp_path, default_holder=holder)
database = Database(settings)
database.init()
return MemoryService(settings=settings, database=database), database
def _remember(service: MemoryService, text: str, **kwargs) -> str:
return service.remember(text, holder_id="wife", origin=MemoryOrigin.self_, **kwargs).atom_id
def test_a_session_comes_back_in_the_order_it_was_recorded(tmp_path) -> None:
service, _ = _service(tmp_path)
first = _remember(service, "烤箱要除垢", session_id=SESSION)
second = _remember(service, "面粉在储物架第二层", session_id=SESSION)
working = service.working_set("wife", SESSION)
assert [atom.atom_id for atom in working] == [first, second], (
"a working set is the session's own timeline, not a ranking of it"
)
assert all(atom.session_id == SESSION for atom in working)
def test_a_working_set_refuses_to_guess_which_session_is_working(tmp_path) -> None:
service, _ = _service(tmp_path)
_remember(service, "面粉在储物架第二层")
with pytest.raises(ValueError, match="a working set needs a session"):
service.working_set("wife", "")
def test_one_session_name_does_not_merge_two_holders(tmp_path) -> None:
service, _ = _service(tmp_path)
hers = _remember(service, "面粉在储物架第二层", session_id=SESSION)
service.remember("车库里有备用钥匙", holder_id="husband", origin=MemoryOrigin.self_, session_id=SESSION)
assert [atom.atom_id for atom in service.working_set("wife", SESSION)] == [hers], (
"a shared session name is not a shared memory"
)
def test_a_lane_rolls_up_once_and_keeps_rolling_from_where_it_stopped(tmp_path) -> None:
service, database = _service(tmp_path)
assert service.consolidate("wife", "home") is None, "an empty lane has nothing to summarise"
_remember(service, "烤箱要除垢", scenario="home")
_remember(service, "面粉在储物架第二层", scenario="home")
first = service.consolidate("wife", "home", at=NOW)
assert first is not None and first.atom_count == 2
rollup = database.get_atoms([first.rollup_atom_id])[0]
assert "烤箱要除垢" in rollup.text and "面粉在储物架第二层" in rollup.text
assert service.consolidate("wife", "home", at=NOW) is None, (
"the watermark is what stops the same memory from being counted twice"
)
_remember(service, "恒温器调到 24 度", scenario="home")
second = service.consolidate("wife", "home", at=NOW + timedelta(days=1))
assert second is not None and second.atom_count == 3, (
"the standing summary grows; it does not start over and forget what it already held"
)
grown = database.get_atoms([second.rollup_atom_id])[0]
assert grown.atom_id == first.rollup_atom_id, "one lane holds one summary, replaced in place"
assert "烤箱要除垢" in grown.text and "恒温器调到 24 度" in grown.text
def test_another_scenario_is_a_different_lane(tmp_path) -> None:
service, _ = _service(tmp_path)
_remember(service, "烤箱要除垢", scenario="home")
_remember(service, "周报周五交", scenario="work")
home = service.consolidate("wife", "home", at=NOW)
assert home is not None and home.atom_count == 1
work = service.consolidate("wife", "work", at=NOW)
assert work is not None and work.atom_count == 1
assert home.rollup_atom_id != work.rollup_atom_id
def test_the_archive_rule_states_its_own_terms_and_can_be_read_before_it_acts(tmp_path) -> None:
service, database = _service(tmp_path)
forgotten = _remember(service, "前租户的 wifi 密码")
valued = _remember(service, "妈妈的生日是 3 月 4 日", importance=0.9)
future = NOW + timedelta(days=365)
preview = service.archive_stale(older_than_days=0, max_importance=0.5, at=future, dry_run=True)
assert preview.dry_run and preview.atom_ids == [forgotten]
assert "recorded before" in preview.rule, "the rule is stated, not hidden in a decay curve"
assert service.archived() == []
moved = service.archive_stale(older_than_days=0, max_importance=0.5, at=future)
assert moved.atom_ids == [forgotten] and not moved.dry_run
assert [atom.atom_id for atom in service.archived("wife")] == [forgotten]
assert database.get_atoms([forgotten])[0].archived_at is not None
assert database.get_atoms([valued])[0].archived_at is None, (
"a memory somebody marked important is not the archive's to move"
)
assert database.atom_count() == 2, "the archive steps a memory aside; it does not erase it"
def test_an_archive_that_moves_nothing_says_so_rather_than_reporting_a_win(tmp_path) -> None:
service, _ = _service(tmp_path)
_remember(service, "烤箱要除垢")
moved = service.archive_stale(older_than_days=365, at=NOW)
assert moved.atom_ids == [], "a memory recorded yesterday is not older than a year"
def test_restoring_brings_the_memory_back_to_the_current_tier(tmp_path) -> None:
service, database = _service(tmp_path)
atom_id = _remember(service, "前租户的 wifi 密码")
service.archive_stale(older_than_days=0, at=NOW + timedelta(days=365))
assert service.restore([atom_id]) == 1
assert service.archived() == []
assert database.get_atoms([atom_id])[0].archived_at is None
with pytest.raises(ValueError, match="restore needs atom ids"):
service.restore([])
def test_a_search_counts_the_memory_it_handed_back(tmp_path) -> None:
service, database = _service(tmp_path)
settings = Settings(home=tmp_path, default_holder="wife")
retrieval = RetrievalService(settings=settings, database=database)
atom_id = _remember(service, "我们的钥匙放在玄关抽屉里")
hits = retrieval.search("钥匙", filters=SearchFilters(as_holder="wife")).hits
assert any(hit.atom.atom_id == atom_id for hit in hits)
used = database.get_atoms([atom_id])[0]
assert used.recall_count == 1, "the store records that this memory was used, which is the only use estimate it has"
assert used.last_recalled_at is not None
retrieval.search("钥匙", filters=SearchFilters(as_holder="wife"))
assert database.get_atoms([atom_id])[0].recall_count == 2
def test_a_memory_a_search_already_returned_is_not_stale(tmp_path) -> None:
service, database = _service(tmp_path)
settings = Settings(home=tmp_path, default_holder="wife")
retrieval = RetrievalService(settings=settings, database=database)
atom_id = _remember(service, "我们的钥匙放在玄关抽屉里")
assert retrieval.search("钥匙", filters=SearchFilters(as_holder="wife")).hits
moved = service.archive_stale(older_than_days=0, at=NOW + timedelta(days=365))
assert moved.atom_ids == [], (
"a memory that keeps coming back was used; the archive takes what nobody ever asked for"
)
assert database.get_atoms([atom_id])[0].archived_at is None
def test_an_archived_memory_stops_answering_without_leaving_the_store(tmp_path) -> None:
service, database = _service(tmp_path)
settings = Settings(home=tmp_path, default_holder="wife")
retrieval = RetrievalService(settings=settings, database=database)
atom_id = _remember(service, "我们的钥匙放在玄关抽屉里")
service.archive_stale(older_than_days=0, at=NOW + timedelta(days=365))
assert retrieval.search("钥匙", filters=SearchFilters(as_holder="wife")).hits == []
assert database.get_atoms([atom_id])[0].archived_at is not None, (
"the row is still there; only the current tier stopped carrying it"
)
def test_importance_is_a_stated_value_and_not_a_free_number(tmp_path) -> None:
from pydantic import ValidationError
from ely_eye.schemas import EvidenceAtom, Modality, TrustScores
with pytest.raises(ValidationError, match="less than or equal to 1"):
EvidenceAtom(
atom_id="mem-1:text",
modality=Modality.text,
source_id="mem-1",
source="memory://wife/home",
text="恒温器调到 24 度",
trust=TrustScores(parser=1.0),
holder_id="wife",
importance=1.5,
)
+2
View File
@@ -40,8 +40,10 @@ HTTP_SURFACE = {
"/api/ingest/path",
"/api/ingest/upload",
"/api/memory",
"/api/memory/conflicts",
"/api/memory/forget",
"/api/memory/supersede",
"/api/memory/working",
"/api/mounts",
"/api/proof-suite",
"/api/proof-suite/latest",