HEL-833: restore runtime factory and verify R3 memory contracts

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
白栖知
2026-10-24 13:07:43 +08:00
co-authored by multica-agent
parent 78cab0456c
commit 89d53b6c53
23 changed files with 1772 additions and 19 deletions
+6 -3
View File
@@ -208,10 +208,13 @@ def create_app(
app.state.sharing = Sharing(store, router, models)
app.state.persona = Persona(store, router, models, enabled=life_config.enabled)
app.state.public = PublicView(store)
app.state.memories = Memories(store, router, models, timezone=life_config.timezone)
app.state.life = LifeRuntime(store, router, models, life_config, app.state.sharing, app.state.persona)
# Preserve the original runtime factory contract. Assemble only after its
# clock is known, before any recovery or background work can run.
app.state.memories = Memories(store, router, models, timezone=life_config.timezone,
clock=lambda: app.state.life.clock())
app.state.life.memories = app.state.memories
app.state.memories.recover()
app.state.life = LifeRuntime(store, router, models, life_config, app.state.sharing, app.state.persona,
memories=app.state.memories)
life_task = asyncio.create_task(app.state.life.run())
try:
yield
+11
View File
@@ -6,6 +6,17 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_mem_message
CREATE VIRTUAL TABLE IF NOT EXISTS mem_fts_zh USING fts5(
raw, gist, content='mem_episodic', content_rowid='id', tokenize='trigram'
);
-- Keep the additive index valid even while rolled-back code writes old rows.
CREATE TRIGGER IF NOT EXISTS mem_zh_insert AFTER INSERT ON mem_episodic BEGIN
INSERT INTO mem_fts_zh(rowid,raw,gist) VALUES(new.id,new.raw,new.gist);
END;
CREATE TRIGGER IF NOT EXISTS mem_zh_delete AFTER DELETE ON mem_episodic BEGIN
INSERT INTO mem_fts_zh(mem_fts_zh,rowid,raw,gist) VALUES('delete',old.id,old.raw,old.gist);
END;
CREATE TRIGGER IF NOT EXISTS mem_zh_update AFTER UPDATE OF raw,gist ON mem_episodic BEGIN
INSERT INTO mem_fts_zh(mem_fts_zh,rowid,raw,gist) VALUES('delete',old.id,old.raw,old.gist);
INSERT INTO mem_fts_zh(rowid,raw,gist) VALUES(new.id,new.raw,new.gist);
END;
CREATE TABLE IF NOT EXISTS mem_job (
source TEXT NOT NULL, ref_id INTEGER NOT NULL REFERENCES chat_message(id),
memory_id INTEGER NOT NULL REFERENCES mem_episodic(id),
+5 -8
View File
@@ -140,8 +140,7 @@ class Store:
"INSERT INTO mem_fts(rowid, raw, gist) VALUES (?,?,?)",
(cur.lastrowid, raw, None),
)
self.execute("INSERT INTO mem_fts_zh(rowid, raw, gist) VALUES (?,?,?)",
(cur.lastrowid, raw, None))
# R3's SQL trigger maintains the Chinese index, including legacy writers.
if commit:
self.commit()
return int(cur.lastrowid or 0)
@@ -149,14 +148,12 @@ class Store:
def set_gist(self, epi_id: int, gist: str, affect: dict) -> None:
"""Caller owns the transaction, including job completion and both indexes."""
row = self.query_one("SELECT * FROM mem_episodic WHERE id=?", (epi_id,))
for table in ("mem_fts", "mem_fts_zh"):
self.execute(f"INSERT INTO {table}({table},rowid,raw,gist) VALUES('delete',?,?,?)",
(epi_id, row["raw"], row["gist"]))
self.execute("INSERT INTO mem_fts(mem_fts,rowid,raw,gist) VALUES('delete',?,?,?)",
(epi_id, row["raw"], row["gist"]))
self.execute("UPDATE mem_episodic SET gist=?,affect=? WHERE id=?",
(gist, json.dumps(affect), epi_id))
for table in ("mem_fts", "mem_fts_zh"):
self.execute(f"INSERT INTO {table}(rowid,raw,gist) VALUES(?,?,?)",
(epi_id, row["raw"], gist))
self.execute("INSERT INTO mem_fts(rowid,raw,gist) VALUES(?,?,?)",
(epi_id, row["raw"], gist))
def decay_accessibility(self, dt_hours: float, *, floor: float = 0.03, commit: bool = True) -> int:
"""遗忘:指数衰减,但不下探到 0。