feat(persistence): PostgreSQL assistant store, chat persist fixes, gateway scripts

- Add SQLAlchemy Core repos, pg adapter/compat, assistant_store factory, Alembic bootstrap and migration/cutover scripts.

- Harden chat_message writes (NUL scrub for PG), turn_uuid on attempt failure, WS turn_runner fallbacks and gateway executed_turn_uuid init.

- start_gateway: log paths, PS7 stderr handling via cmd, background stdout/stderr redirect; runtime assistant_runtime_log_dir export.

- Ops: clear_all_chat_sessions with PG-only --postgresql and env-gated wipe; clear_postgres_chat_sessions.ps1.

- Tests: SA repos, pg compat, persist fallback, smoke env isolation; CI and docs touch-ups.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-05-14 09:33:50 +08:00
parent 2b32d11f43
commit d14e9d3596
103 changed files with 7574 additions and 1641 deletions

View file

@ -0,0 +1,67 @@
"""Alembic environment (assistant DB URL from env)."""
from __future__ import annotations
import os
import sys
from logging.config import fileConfig
from pathlib import Path
from sqlalchemy import engine_from_config, pool, text
from sqlalchemy.engine import Connection
from alembic import context
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
os.chdir(ROOT)
from svc.config.bootstrap_env import load_system_env # noqa: E402
load_system_env()
from svc.config.database import assistant_db_backend, assistant_sqlalchemy_url # noqa: E402
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = None
def get_url() -> str:
return assistant_sqlalchemy_url()
def run_migrations_offline() -> None:
context.configure(
url=get_url(),
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
ini_section = config.get_section(config.config_ini_section) or {}
ini_section["sqlalchemy.url"] = get_url()
connectable = engine_from_config(
ini_section,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View file

@ -0,0 +1,26 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View file

@ -0,0 +1,34 @@
"""Initial PostgreSQL schema for assistant store (from SQLite parity DDL)."""
from __future__ import annotations
from pathlib import Path
from alembic import op
from sqlalchemy import text
revision = "001_assistant_pg_initial"
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
bind = op.get_bind()
if bind.dialect.name != "postgresql":
return
root = Path(__file__).resolve().parents[2]
sql_path = root / "svc" / "persistence" / "ddl" / "postgresql_bootstrap.sql"
raw = sql_path.read_text(encoding="utf-8")
for part in raw.split(";"):
stmt = part.strip()
if not stmt or stmt.startswith("--"):
continue
op.execute(text(stmt))
def downgrade() -> None:
bind = op.get_bind()
if bind.dialect.name != "postgresql":
return
raise NotImplementedError("Assistant PG downgrade not supported; restore from backup.")