Skip to content
Python

fix(memhub): metadata repair adds the SDK's memory_at column itself

alvinfeicb7df2a02217
diff --git a/memhub/apps/core/src/one_memory/hy_sdk_migration.py b/memhub/apps/core/src/one_memory/hy_sdk_migration.py
index b4bd56b..df3d6c0 100644
--- a/memhub/apps/core/src/one_memory/hy_sdk_migration.py
+++ b/memhub/apps/core/src/one_memory/hy_sdk_migration.py
@@ -426,6 +426,7 @@ def _repair_coding(
path = root / name
if not path.is_file():
continue
+ _ensure_coding_schema(path)
with closing(sqlite3.connect(path)) as database:
tables = {
str(row[0])
@@ -437,7 +438,8 @@ def _repair_coding(
):
if table not in tables:
continue
- # hy-memory 2.0.7 added the conversation time; older stores lack it.
+ # _ensure_coding_schema added it; an SDK without the column
+ # (before 2.0.7) leaves the time to the vector repair alone.
has_memory_at = "memory_at" in _columns(database, table)
rows = database.execute(
f"SELECT {identifier},source,created_at,"
@@ -480,6 +482,32 @@ def _repair_coding(
return repaired
+def _ensure_coding_schema(path: Path) -> None:
+ """Bring a Coding/Working store to the installed SDK's schema.
+
+ A migrated home's stores were written by an older SDK and may lack columns
+ the repair writes (``memory_at`` since hy-memory 2.0.7). The repair runs
+ before any SDK client opens them, so the SDK's own schema step is run here
+ rather than repeating its DDL. ``CodingMemoryStore`` has no public
+ schema-only entry point: ``initialize()`` also opens its Chroma
+ collections, which must stay closed during the offline repair. So only its
+ SQLite step runs, on a store object that carries nothing but that path.
+ """
+ from hy_memory.coding.store import ( # type: ignore[import-untyped]
+ CodingMemoryStore,
+ )
+
+ # Relies on the pinned SDK's internals (hy-memory==2.0.7 is pinned exactly).
+ store = CodingMemoryStore.__new__(CodingMemoryStore)
+ store._db_path = str(path)
+ store._conn = None
+ try:
+ store._init_sqlite_sync()
+ finally:
+ if store._conn is not None:
+ store._conn.close()
+
+
def _batches[T](values: list[T] | tuple[T, ...]) -> Iterator[tuple[T, ...]]:
for start in range(0, len(values), _BATCH):
yield tuple(values[start : start + _BATCH])
diff --git a/memhub/apps/core/tests/test_sdk_data_plane_migration.py b/memhub/apps/core/tests/test_sdk_data_plane_migration.py
index bdf9cf8..e628e2a 100644
--- a/memhub/apps/core/tests/test_sdk_data_plane_migration.py
+++ b/memhub/apps/core/tests/test_sdk_data_plane_migration.py
@@ -137,21 +137,38 @@ def test_post_cutover_metadata_repair_restores_source_and_original_times(store):
)
coding = data_dir / "hy-memory" / "governed-v2" / "coding_cv.sqlite3"
with sqlite3.connect(coding) as database:
- # hy-memory 2.0.7 stores the conversation time; working_fact below is
- # left without it, as a store not yet opened by 2.0.7.
- database.execute(
- "CREATE TABLE coding_memory_meta ("
- "experience_id TEXT,user_id TEXT,agent_id TEXT,solution TEXT,"
- "source TEXT,memory_at INTEGER,created_at TEXT,updated_at TEXT)"
- )
- database.execute(
- "CREATE TABLE working_fact ("
- "fact_id TEXT,user_id TEXT,agent_id TEXT,statement TEXT,"
- "source TEXT,created_at TEXT)"
+ # A store written by hy-memory 2.0.6, before the conversation time
+ # column: the repair must add it through the SDK and then fill it.
+ database.executescript(
+ """
+ CREATE TABLE coding_memory_meta (
+ experience_id TEXT PRIMARY KEY, user_id TEXT NOT NULL,
+ agent_id TEXT NOT NULL DEFAULT 'default_agent', task TEXT NOT NULL,
+ search_keys TEXT NOT NULL DEFAULT '[]', solution TEXT NOT NULL,
+ boundary_envs TEXT NOT NULL DEFAULT '', workspace_id TEXT,
+ branch TEXT, session_id TEXT, files TEXT NOT NULL DEFAULT '[]',
+ source_fact_ids TEXT NOT NULL DEFAULT '[]',
+ confidence REAL NOT NULL DEFAULT 0.7,
+ source TEXT NOT NULL DEFAULT 'default',
+ extraction_source TEXT NOT NULL DEFAULT 'auto_extract',
+ created_at TEXT NOT NULL, updated_at TEXT NOT NULL
+ );
+ CREATE TABLE working_fact (
+ fact_id TEXT PRIMARY KEY, user_id TEXT NOT NULL,
+ agent_id TEXT NOT NULL DEFAULT 'default_agent', session_id TEXT,
+ workspace_id TEXT, branch TEXT, source TEXT NOT NULL DEFAULT 'default',
+ statement TEXT NOT NULL, files TEXT NOT NULL DEFAULT '[]',
+ subject TEXT NOT NULL DEFAULT '', details TEXT NOT NULL DEFAULT '',
+ fact_link TEXT, derived_experience_ids TEXT NOT NULL DEFAULT '[]',
+ status TEXT NOT NULL DEFAULT 'active',
+ fact_chain_summary TEXT NOT NULL DEFAULT '', created_at TEXT NOT NULL
+ );
+ """
)
database.execute(
- "INSERT INTO coding_memory_meta VALUES "
- "('sdk-1','local','scope','remember','default',NULL,'now','now')"
+ "INSERT INTO coding_memory_meta "
+ "(experience_id,user_id,agent_id,task,solution,source,created_at,updated_at) "
+ "VALUES ('sdk-1','local','scope','remember','remember','default','now','now')"
)
result = repair_sdk_metadata_from_bundle(