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.
98 lines
4.1 KiB
Python
98 lines
4.1 KiB
Python
"""The HTTP face stamps provenance itself: a client cannot say who owns a memory."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
import ely_eye.api as api_module
|
|
from ely_eye.config import Settings
|
|
|
|
KEYS = "我们的钥匙放在玄关抽屉里"
|
|
|
|
|
|
@pytest.fixture
|
|
def client(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, write_token: str) -> TestClient:
|
|
settings = Settings(home=tmp_path, default_holder="wife", auth_token=write_token)
|
|
monkeypatch.setattr(api_module, "get_settings", lambda: settings)
|
|
return TestClient(api_module.create_app(), headers={"Authorization": f"Bearer {write_token}"})
|
|
|
|
|
|
def test_post_memory_stamps_the_holder_the_server_knows(client: TestClient) -> None:
|
|
response = client.post("/api/memory", json={"text": KEYS, "scenario": "home"})
|
|
assert response.status_code == 200
|
|
atom = response.json()
|
|
assert atom["holder_id"] == "wife"
|
|
assert atom["origin"] == "self"
|
|
assert atom["scenario"] == "home"
|
|
assert atom["ingested_at"], "ingestion time is stamped, never supplied"
|
|
|
|
|
|
def test_a_client_cannot_claim_another_holder(client: TestClient) -> None:
|
|
response = client.post(
|
|
"/api/memory",
|
|
json={"text": KEYS, "holder_id": "attacker", "origin": "third_party", "ingested_at": "2020-01-01T00:00:00+00:00"},
|
|
)
|
|
assert response.status_code == 200
|
|
atom = response.json()
|
|
assert atom["holder_id"] == "wife", "the request body has no holder field to forge"
|
|
assert atom["origin"] == "self"
|
|
|
|
|
|
def test_an_empty_memory_is_rejected_with_its_reason(client: TestClient) -> None:
|
|
response = client.post("/api/memory", json={"text": " "})
|
|
assert response.status_code == 422
|
|
assert "needs text" in response.json()["detail"]
|
|
|
|
|
|
def test_supersede_then_forget_reports_what_it_removed(client: TestClient) -> None:
|
|
old = client.post("/api/memory", json={"text": KEYS, "scenario": "home"}).json()
|
|
new = client.post(
|
|
"/api/memory/supersede",
|
|
json={"atom_id": old["atom_id"], "text": "钥匙现在在书房第二个抽屉", "scenario": "home"},
|
|
).json()
|
|
assert new["supersedes"] == [old["atom_id"]]
|
|
|
|
forgotten = client.post("/api/memory/forget", json={"atom_id": old["atom_id"], "reason": "moved"})
|
|
assert forgotten.status_code == 200
|
|
report = forgotten.json()
|
|
assert report["atoms_deleted"] == 1
|
|
assert report["atom_ids"] == [old["atom_id"]]
|
|
assert report["reason"] == "moved"
|
|
|
|
|
|
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() == []
|