Revert "refactor: remove legacy history and proxy migrations (#6518)"

This reverts commit cb3ba45317.

Keep JSONL history and legacy proxy migration available throughout 0.4.x. Mark their removal for 0.5.0 in code and release guidance, preserving later context graph, archive and recovery changes.
This commit is contained in:
chengyongru authored and chengyongru committed 2026-11-06 10:58:10 +08:00
1 parent 3918ece17f
commit 812b92d734
47 files changed
+1932 -204

No files matched your search

+50 -17
View File
@@ -1,3 +1,4 @@
import json
import sqlite3
from contextlib import closing
from pathlib import Path
@@ -35,6 +36,22 @@ def _session() -> Session:
)
def _write_legacy(workspace: Path, session: Session) -> Path:
directory = workspace / "sessions"
directory.mkdir(parents=True)
path = directory / "session.jsonl"
assert session.provider_state is not None
records = [
{"_type": "metadata", "key": session.key, "metadata": session.metadata,
"created_at": session.created_at.isoformat(), "updated_at": session.updated_at.isoformat(),
"last_archived": session.last_archived},
{"_type": "provider_state", "state": session.provider_state.to_private_record()},
*session.messages,
]
path.write_text("\n".join(json.dumps(record) for record in records) + "\n", encoding="utf-8")
return path
def _database_snapshot(path: Path) -> tuple[int, str, str]:
with closing(sqlite3.connect(path)) as db:
return (
@@ -44,8 +61,10 @@ def _database_snapshot(path: Path) -> tuple[int, str, str]:
)
def test_ordered_upgrades_preserve_records(tmp_path, monkeypatch):
def test_ordered_upgrades_preserve_records_and_do_not_repeat_import(tmp_path, monkeypatch):
workspace = tmp_path / "workspace"
original = _session()
source = _write_legacy(workspace, original)
store = SqliteSessionStore(tmp_path)
with closing(sqlite3.connect(store.path)) as db:
schema = Path(__file__).with_name("fixtures") / "sqlite_v1.sql"
@@ -62,8 +81,9 @@ def test_ordered_upgrades_preserve_records(tmp_path, monkeypatch):
monkeypatch.setattr(sqlite_store, "_SCHEMA_MIGRATIONS", (
*sqlite_store._SCHEMA_MIGRATIONS, add_archived, index_archived,
))
source.write_text("stale malformed source must not be imported again", encoding="utf-8")
upgraded = SqliteSessionStore(tmp_path)
upgraded.initialize()
upgraded.initialize(workspace)
assert _database_snapshot(store.path)[0] == len(sqlite_store._SCHEMA_MIGRATIONS)
loaded = upgraded.load(original.key)
assert loaded is not None
@@ -73,7 +93,7 @@ def test_ordered_upgrades_preserve_records(tmp_path, monkeypatch):
upgraded.save(loaded)
reopened = SqliteSessionStore(tmp_path)
reopened.initialize()
reopened.initialize(workspace)
roundtrip = reopened.load(original.key)
assert roundtrip is not None
assert roundtrip.messages == original.messages
@@ -82,11 +102,13 @@ def test_ordered_upgrades_preserve_records(tmp_path, monkeypatch):
with closing(sqlite3.connect(store.path)) as db:
assert db.execute("SELECT archived FROM sessions").fetchall() == [(0,)]
assert db.execute("SELECT name FROM sqlite_master WHERE name='sessions_archived'").fetchone()
assert db.execute("SELECT value FROM storage_meta WHERE key='jsonl_import'").fetchone() == ("complete",)
def test_failed_upgrade_rolls_back_schema_version_and_records_then_can_retry(tmp_path, monkeypatch):
workspace = tmp_path / "workspace"
store = SqliteSessionStore(tmp_path)
store.initialize()
store.initialize(workspace)
original = _session()
store.save(original)
before = _database_snapshot(store.path)
@@ -104,7 +126,7 @@ def test_failed_upgrade_rolls_back_schema_version_and_records_then_can_retry(tmp
*migrations, add_archived, fail_upgrade,
))
with pytest.raises(RuntimeError, match="migration interrupted"):
store.initialize()
store.initialize(workspace)
assert _database_snapshot(store.path) == before
def complete_upgrade(db: sqlite3.Connection) -> None:
@@ -113,7 +135,7 @@ def test_failed_upgrade_rolls_back_schema_version_and_records_then_can_retry(tmp
monkeypatch.setattr(sqlite_store, "_SCHEMA_MIGRATIONS", (
*migrations, add_archived, complete_upgrade,
))
store.initialize()
store.initialize(workspace)
assert _database_snapshot(store.path)[0] == len(sqlite_store._SCHEMA_MIGRATIONS)
loaded = store.load(original.key)
assert loaded is not None
@@ -122,7 +144,7 @@ def test_failed_upgrade_rolls_back_schema_version_and_records_then_can_retry(tmp
assert loaded.provider_state == original.provider_state
def test_added_columns_allow_all_session_write_paths(tmp_path, monkeypatch):
def test_added_columns_allow_import_and_all_session_write_paths(tmp_path, monkeypatch):
def add_optional_columns(db: sqlite3.Connection) -> None:
for table in ("storage_meta", "sessions", "messages", "checkpoints", "pending_inputs"):
db.execute(f"ALTER TABLE {table} ADD COLUMN extension TEXT")
@@ -131,8 +153,16 @@ def test_added_columns_allow_all_session_write_paths(tmp_path, monkeypatch):
monkeypatch.setattr(sqlite_store, "_SCHEMA_MIGRATIONS", (
*sqlite_store._SCHEMA_MIGRATIONS, add_optional_columns,
))
workspace = tmp_path / "workspace"
imported = _session()
_write_legacy(workspace, imported)
store = SqliteSessionStore(tmp_path)
store.initialize()
store.initialize(workspace)
loaded = store.load(imported.key)
assert loaded is not None
assert loaded.messages == imported.messages
assert loaded.metadata == imported.metadata
assert loaded.provider_state == imported.provider_state
created = _session()
created.key = "chat:new"
@@ -151,14 +181,15 @@ def test_added_columns_allow_all_session_write_paths(tmp_path, monkeypatch):
assert roundtrip.metadata == current.metadata
assert roundtrip.provider_state == current.provider_state
with closing(sqlite3.connect(store.path)) as db:
for table in ("sessions", "messages", "checkpoints", "pending_inputs"):
for table in ("storage_meta", "sessions", "messages", "checkpoints", "pending_inputs"):
rows = db.execute(f"SELECT extension, format_version FROM {table}").fetchall()
assert rows and all(row == (None, 1) for row in rows)
def test_future_schema_is_rejected_without_modifying_database(tmp_path):
workspace = tmp_path / "workspace"
store = SqliteSessionStore(tmp_path)
store.initialize()
store.initialize(workspace)
store.save(_session())
future_version = len(sqlite_store._SCHEMA_MIGRATIONS) + 1
with closing(sqlite3.connect(store.path)) as db:
@@ -169,15 +200,16 @@ def test_future_schema_is_rejected_without_modifying_database(tmp_path):
before_files = {path.name for path in tmp_path.iterdir()}
with pytest.raises(RuntimeError, match=f"unsupported session database schema: {future_version}"):
store.initialize()
store.initialize(workspace)
assert _database_snapshot(store.path) == before
assert store.path.read_bytes() == before_bytes
assert {path.name for path in tmp_path.iterdir()} == before_files
def test_missing_sqlite_json_support_is_rejected_before_database_changes(tmp_path, monkeypatch):
workspace = tmp_path / "workspace"
store = SqliteSessionStore(tmp_path)
store.initialize()
store.initialize(workspace)
store.save(_session())
with closing(sqlite3.connect(store.path)) as db:
db.execute("PRAGMA journal_mode = DELETE")
@@ -197,16 +229,17 @@ def test_missing_sqlite_json_support_is_rejected_before_database_changes(tmp_pat
with monkeypatch.context() as patch:
patch.setattr(sqlite3, "connect", connect)
with pytest.raises(RuntimeError, match="requires SQLite JSON support"):
store.initialize()
store.initialize(workspace)
assert _database_snapshot(store.path) == before
assert store.path.read_bytes() == before_bytes
def test_v4_archive_upgrade_removes_orphans_and_preserves_existing_membership(tmp_path, monkeypatch):
workspace = tmp_path / "workspace"
store = SqliteSessionStore(tmp_path)
with monkeypatch.context() as patch:
patch.setattr(sqlite_store, "_SCHEMA_MIGRATIONS", sqlite_store._SCHEMA_MIGRATIONS[:3])
store.initialize()
store.initialize(workspace)
original = _session()
store.save(original)
store.save(Session(key="chat:empty-fork"))
@@ -216,8 +249,8 @@ def test_v4_archive_upgrade_removes_orphans_and_preserves_existing_membership(tm
[(key, "2026-11-01T12:00:00") for key in ("chat:extensions", "chat:empty-fork", "chat:draft")],
)
db.execute("INSERT INTO storage_meta (key, value) VALUES ('session_archives_imported', '1')")
store.initialize()
store.initialize()
store.initialize(workspace)
store.initialize(workspace)
assert _database_snapshot(store.path)[0] == 4
assert store.archived_keys() == ["chat:empty-fork", "chat:extensions"]
assert store.archives_imported()
@@ -233,7 +266,7 @@ def test_v4_archive_upgrade_removes_orphans_and_preserves_existing_membership(tm
def test_archive_reference_allows_atomic_session_replacement_but_not_orphan_commit(tmp_path):
store = SqliteSessionStore(tmp_path)
store.initialize()
store.initialize(tmp_path / "workspace")
original = _session()
store.save(original)
assert store.set_archived_keys([original.key]) == [original.key]