[STAGE5] Agent memory store + TTL/search tests
lint / hygiene (push) Failing after 23s
lint / hygiene (pull_request) Failing after 26s
standard / check (pull_request) Successful in 12s

This commit is contained in:
architect committed 2026-11-29 18:03:36 +03:00
1 parent 6cd7984910
commit f2bdccc45b
3 files changed
+101 -1

No files matched your search

+14 -1
View File
@@ -24,7 +24,7 @@ GET /v1/models
| S5-01 | Model Registry v0 | done |
| S5-02 | GPU/model leases | in_progress: model lease runtime, нет GPU-планировщика |
| S5-03 | Evals / golden set | done: baseline 1/10, runner+golden set |
| S5-04 | Memory | open |
| S5-04 | Memory | done: SQLite namespaced memory + TTL/search, tests 3/3 |
| S5-05 | Night Shift | open |
| S5-06 | Tool Gateway / agent approvals | in_progress: runtime есть, агенты не переключены |
| S5-07 | Gate G5 | open |
@@ -60,6 +60,19 @@ score=1/10
Это не «модель готова», а зафиксированный baseline: qwen2.5:3b плохо знает домен Buryans.
## Memory
`ops/ai/memory.py`:
```text
namespace + key + value + source
TTL
search
SQLite /var/lib/hub-memory/memory.db
```
Тесты: 3/3 OK.
## Model leases
```text
+68
View File
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
"""Buryans agent memory: namespaced key/value with TTL and search."""
import argparse, json, os, sqlite3, time
from pathlib import Path
DB = os.environ.get("HUB_MEMORY_DB", "/var/lib/hub-memory/memory.db")
def connect(db=None):
db = db or DB
Path(db).parent.mkdir(parents=True, exist_ok=True)
c = sqlite3.connect(db)
c.execute("""create table if not exists memory(
namespace text not null, key text not null, value text not null,
source text default '', updated_at real not null, expires_at real,
primary key(namespace,key))""")
return c
def put(namespace, key, value, source="", ttl=None, db=None):
now = time.time()
exp = now + ttl if ttl else None
with connect(db) as c:
c.execute("""insert into memory(namespace,key,value,source,updated_at,expires_at)
values(?,?,?,?,?,?)
on conflict(namespace,key) do update set value=excluded.value,
source=excluded.source, updated_at=excluded.updated_at, expires_at=excluded.expires_at""",
(namespace, key, value, source, now, exp))
return {"namespace": namespace, "key": key, "expires_at": exp}
def get(namespace, key, db=None):
with connect(db) as c:
r = c.execute("select value,source,updated_at,expires_at from memory where namespace=? and key=?",
(namespace, key)).fetchone()
if not r:
return None
if r[3] and r[3] < time.time():
c.execute("delete from memory where namespace=? and key=?", (namespace, key))
return None
return {"value": r[0], "source": r[1], "updated_at": r[2], "expires_at": r[3]}
def search(namespace, query, limit=20, db=None):
with connect(db) as c:
rows = c.execute("""select namespace,key,value,source,updated_at,expires_at from memory
where namespace=? and (key like ? or value like ?) order by updated_at desc limit ?""",
(namespace, "%" + query + "%", "%" + query + "%", int(limit))).fetchall()
now = time.time()
return [{"namespace": x[0], "key": x[1], "value": x[2], "source": x[3],
"updated_at": x[4], "expires_at": x[5]} for x in rows
if not x[5] or x[5] > now]
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("cmd", choices=["put", "get", "search"])
ap.add_argument("namespace")
ap.add_argument("key")
ap.add_argument("value", nargs="?")
ap.add_argument("--ttl", type=int)
a = ap.parse_args()
if a.cmd == "put":
print(json.dumps(put(a.namespace, a.key, a.value or "", ttl=a.ttl)))
elif a.cmd == "get":
print(json.dumps(get(a.namespace, a.key)))
else:
print(json.dumps(search(a.namespace, a.key)))
+19
View File
@@ -0,0 +1,19 @@
import os, sys, tempfile, unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from memory import put, get, search
class MemoryTests(unittest.TestCase):
def setUp(self):
self.db = tempfile.mktemp(suffix='.db')
def test_put_get(self):
put('stage5','camera','Frigate test',source='test',db=self.db)
self.assertEqual(get('stage5','camera',db=self.db)['value'],'Frigate test')
def test_search(self):
put('stage5','event_bus','NATS JetStream',db=self.db)
self.assertEqual(len(search('stage5','NATS',db=self.db)),1)
def test_ttl(self):
put('stage5','tmp','x',ttl=-1,db=self.db)
self.assertIsNone(get('stage5','tmp',db=self.db))
if __name__ == '__main__':
unittest.main()