mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-19 13:05:49 +08:00
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:
parent
2b32d11f43
commit
d14e9d3596
103 changed files with 7574 additions and 1641 deletions
108
svc/persistence/sa_repos/trace_events.py
Normal file
108
svc/persistence/sa_repos/trace_events.py
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
"""trace_event insert + list queries via SQLAlchemy Core."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import insert, select
|
||||
from sqlalchemy.engine import Engine
|
||||
|
||||
from svc.persistence.db.tables import trace_event
|
||||
|
||||
|
||||
class TraceEventsSaRepository:
|
||||
__slots__ = ("_engine",)
|
||||
|
||||
def __init__(self, engine: Engine) -> None:
|
||||
self._engine = engine
|
||||
|
||||
def insert_one(
|
||||
self,
|
||||
*,
|
||||
session_id: str,
|
||||
trace_id: str,
|
||||
span_id: str,
|
||||
parent_span_id: str | None,
|
||||
event_type: str,
|
||||
payload: str,
|
||||
timestamp: str,
|
||||
) -> None:
|
||||
with self._engine.begin() as conn:
|
||||
conn.execute(
|
||||
insert(trace_event).values(
|
||||
session_id=str(session_id),
|
||||
trace_id=str(trace_id),
|
||||
span_id=str(span_id),
|
||||
parent_span_id=parent_span_id,
|
||||
event_type=str(event_type),
|
||||
payload=str(payload),
|
||||
timestamp=str(timestamp),
|
||||
)
|
||||
)
|
||||
|
||||
def insert_many(self, rows: list[dict[str, Any]]) -> None:
|
||||
if not rows:
|
||||
return
|
||||
with self._engine.begin() as conn:
|
||||
conn.execute(insert(trace_event), rows)
|
||||
|
||||
def list_trace_events_desc(self, *, session_id: str, limit: int) -> list[dict[str, Any]]:
|
||||
sid = str(session_id or "").strip()
|
||||
lim = max(1, int(limit))
|
||||
stmt = (
|
||||
select(
|
||||
trace_event.c.trace_id,
|
||||
trace_event.c.span_id,
|
||||
trace_event.c.parent_span_id,
|
||||
trace_event.c.event_type,
|
||||
trace_event.c.payload,
|
||||
trace_event.c.timestamp,
|
||||
)
|
||||
.where(trace_event.c.session_id == sid)
|
||||
.order_by(trace_event.c.id.desc())
|
||||
.limit(lim)
|
||||
)
|
||||
with self._engine.connect() as conn:
|
||||
return [dict(r) for r in conn.execute(stmt).mappings().all()]
|
||||
|
||||
def list_trace_events_for_trace_asc(
|
||||
self, *, session_id: str, trace_id: str, limit: int
|
||||
) -> list[dict[str, Any]]:
|
||||
sid = str(session_id or "").strip()
|
||||
tid = str(trace_id or "").strip()
|
||||
lim = max(1, int(limit))
|
||||
if not sid or not tid:
|
||||
return []
|
||||
stmt = (
|
||||
select(
|
||||
trace_event.c.trace_id,
|
||||
trace_event.c.span_id,
|
||||
trace_event.c.parent_span_id,
|
||||
trace_event.c.event_type,
|
||||
trace_event.c.payload,
|
||||
trace_event.c.timestamp,
|
||||
)
|
||||
.where(trace_event.c.session_id == sid, trace_event.c.trace_id == tid)
|
||||
.order_by(trace_event.c.id.asc())
|
||||
.limit(lim)
|
||||
)
|
||||
with self._engine.connect() as conn:
|
||||
return [dict(r) for r in conn.execute(stmt).mappings().all()]
|
||||
|
||||
def list_event_type_timestamp_for_trace(
|
||||
self, *, session_id: str, trace_id: str
|
||||
) -> list[dict[str, Any]]:
|
||||
sid = str(session_id or "").strip()
|
||||
tid = str(trace_id or "").strip()
|
||||
if not sid or not tid:
|
||||
return []
|
||||
stmt = (
|
||||
select(trace_event.c.event_type, trace_event.c.timestamp)
|
||||
.where(trace_event.c.session_id == sid, trace_event.c.trace_id == tid)
|
||||
.order_by(trace_event.c.id.asc())
|
||||
)
|
||||
with self._engine.connect() as conn:
|
||||
return [dict(r) for r in conn.execute(stmt).mappings().all()]
|
||||
|
||||
|
||||
__all__ = ["TraceEventsSaRepository"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue