File size: 27,359 Bytes
8eaad14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
"""
postgres_db.py
──────────────
PostgreSQL persistence layer for enterprise-grade durability.
Stores chat sessions, messages, document metadata, and heavy document index trees (JSONB).
Even if the container or app restarts or sleeps, all documents, chats, and indices remain intact.
"""

import os
import json
import time
import uuid
import base64
from contextlib import contextmanager
from typing import Any, Dict, List, Optional
import psycopg2
import psycopg2.extras
import psycopg2.pool
from utils.logger import app_logger

# Register JSON/JSONB handlers so JSON columns are transparently converted to/from dict/list
psycopg2.extras.register_default_jsonb()

_PG_DDL = """
CREATE TABLE IF NOT EXISTS chats (
    chat_id     TEXT PRIMARY KEY,
    title       TEXT,
    doc_id      TEXT,
    doc_name    TEXT,
    created_at  DOUBLE PRECISION NOT NULL,
    updated_at  DOUBLE PRECISION NOT NULL
);

CREATE TABLE IF NOT EXISTS messages (
    id          BIGSERIAL PRIMARY KEY,
    chat_id     TEXT NOT NULL REFERENCES chats(chat_id) ON DELETE CASCADE,
    role        TEXT NOT NULL CHECK(role IN ('user','assistant','system')),
    content     TEXT NOT NULL,
    created_at  DOUBLE PRECISION NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_pg_messages_chat ON messages(chat_id, created_at);

CREATE TABLE IF NOT EXISTS doc_meta (
    doc_id          TEXT PRIMARY KEY,
    filename        TEXT,
    status          TEXT,
    page_count      INTEGER DEFAULT 0,
    section_count   INTEGER DEFAULT 0,
    progress        INTEGER DEFAULT 0,
    current_stage   TEXT DEFAULT 'uploaded',
    indexing_method TEXT,
    duration        DOUBLE PRECISION,
    error           TEXT,
    content_hash    TEXT,
    file_path       TEXT,
    created_at      DOUBLE PRECISION NOT NULL,
    updated_at      DOUBLE PRECISION NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_pg_doc_meta_hash ON doc_meta(content_hash);

CREATE TABLE IF NOT EXISTS doc_indices (
    doc_id          TEXT PRIMARY KEY REFERENCES doc_meta(doc_id) ON DELETE CASCADE,
    content_hash    TEXT,
    tree            JSONB,
    node_map        JSONB,
    flat_sections   JSONB,
    file_base64     TEXT,
    created_at      DOUBLE PRECISION NOT NULL,
    updated_at      DOUBLE PRECISION NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_pg_doc_indices_hash ON doc_indices(content_hash);

CREATE TABLE IF NOT EXISTS chat_documents (
    chat_id     TEXT NOT NULL REFERENCES chats(chat_id) ON DELETE CASCADE,
    doc_id      TEXT NOT NULL REFERENCES doc_meta(doc_id) ON DELETE CASCADE,
    doc_name    TEXT,
    attached_at DOUBLE PRECISION NOT NULL,
    PRIMARY KEY (chat_id, doc_id)
);

CREATE INDEX IF NOT EXISTS idx_pg_chat_docs ON chat_documents(chat_id);

CREATE TABLE IF NOT EXISTS chat_summaries (
    chat_id     TEXT PRIMARY KEY REFERENCES chats(chat_id) ON DELETE CASCADE,
    summary     TEXT NOT NULL,
    turn_count  INTEGER DEFAULT 0,
    updated_at  DOUBLE PRECISION NOT NULL
);

CREATE TABLE IF NOT EXISTS knowledge_memory (
    doc_id            TEXT PRIMARY KEY REFERENCES doc_meta(doc_id) ON DELETE CASCADE,
    filename          TEXT NOT NULL,
    executive_summary TEXT,
    key_entities      JSONB,
    key_findings      JSONB,
    topics            JSONB,
    updated_at        DOUBLE PRECISION NOT NULL
);

CREATE TABLE IF NOT EXISTS global_knowledge_summary (
    id                  TEXT PRIMARY KEY,
    collective_summary  TEXT NOT NULL,
    total_documents     INTEGER DEFAULT 0,
    key_insights        JSONB,
    updated_at          DOUBLE PRECISION NOT NULL
);
"""


class PostgresDB:
    """Robust, thread-safe PostgreSQL database client with connection pool and auto-reconnect."""

    def __init__(self, dsn: str):
        # Normalize postgres:// to postgresql:// if needed
        if dsn.startswith("postgres://"):
            dsn = "postgresql://" + dsn[len("postgres://"):]
        self._dsn = dsn
        self._pool: Optional[psycopg2.pool.ThreadedConnectionPool] = None
        self._init_pool()
        self._init_schema()

    def _init_pool(self) -> None:
        try:
            self._pool = psycopg2.pool.ThreadedConnectionPool(
                minconn=1,
                maxconn=20,
                dsn=self._dsn,
            )
            app_logger.info("[PostgresDB] Connection pool created successfully.")
        except Exception as e:
            app_logger.error(f"[PostgresDB] Failed to initialize connection pool: {e}")
            raise

    @contextmanager
    def _conn(self):
        """Context manager that borrows a healthy connection from the pool and handles commits/rollbacks."""
        conn = None
        try:
            conn = self._pool.getconn()
            # Test connection liveness
            if conn.closed:
                conn = psycopg2.connect(self._dsn)
            else:
                try:
                    with conn.cursor() as cur:
                        cur.execute("SELECT 1;")
                except Exception:
                    # Connection is dead, recreate
                    try:
                        self._pool.putconn(conn, close=True)
                    except Exception:
                        pass
                    conn = psycopg2.connect(self._dsn)

            yield conn
            conn.commit()
        except Exception:
            if conn and not conn.closed:
                conn.rollback()
            raise
        finally:
            if conn and self._pool:
                try:
                    self._pool.putconn(conn)
                except Exception:
                    pass

    def _init_schema(self) -> None:
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(_PG_DDL)
        app_logger.info("[PostgresDB] Schema initialized / verified successfully.")

    # ── chat CRUD ───────────────────────────────────────────────────────────
    def create_chat(self, chat_id: Optional[str] = None, title: Optional[str] = None) -> str:
        cid = chat_id or str(uuid.uuid4())
        now = time.time()
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    """
                    INSERT INTO chats(chat_id, title, created_at, updated_at)
                    VALUES (%s, %s, %s, %s)
                    ON CONFLICT (chat_id) DO NOTHING
                    """,
                    (cid, title, now, now),
                )
        return cid

    def get_chat(self, chat_id: str) -> Optional[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute("SELECT * FROM chats WHERE chat_id = %s", (chat_id,))
                row = cur.fetchone()
                return dict(row) if row else None

    def list_chats(self) -> List[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute("SELECT * FROM chats ORDER BY updated_at DESC")
                rows = cur.fetchall()
                return [dict(r) for r in rows]

    def update_chat_title(self, chat_id: str, title: str) -> None:
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    "UPDATE chats SET title = %s, updated_at = %s WHERE chat_id = %s",
                    (title, time.time(), chat_id),
                )

    def set_chat_doc(self, chat_id: str, doc_id: str, doc_name: Optional[str] = None) -> None:
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    "UPDATE chats SET doc_id = %s, doc_name = %s, updated_at = %s WHERE chat_id = %s",
                    (doc_id, doc_name, time.time(), chat_id),
                )

    def remove_chat_doc(self, chat_id: str) -> None:
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    "UPDATE chats SET doc_id = NULL, doc_name = NULL, updated_at = %s WHERE chat_id = %s",
                    (time.time(), chat_id),
                )

    def delete_chat(self, chat_id: str) -> None:
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute("DELETE FROM chats WHERE chat_id = %s", (chat_id,))

    # ── message CRUD ────────────────────────────────────────────────────────
    def add_message(self, chat_id: str, role: str, content: str) -> None:
        now = time.time()
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    """
                    INSERT INTO chats(chat_id, title, created_at, updated_at)
                    VALUES (%s, 'New research', %s, %s)
                    ON CONFLICT (chat_id) DO NOTHING
                    """,
                    (chat_id, now, now),
                )
                cur.execute(
                    "INSERT INTO messages(chat_id, role, content, created_at) VALUES (%s, %s, %s, %s)",
                    (chat_id, role, content, now),
                )
                cur.execute(
                    "UPDATE chats SET updated_at = %s WHERE chat_id = %s",
                    (now, chat_id),
                )

    def get_messages(self, chat_id: str, limit: int = 0) -> List[Dict[str, str]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                if limit > 0:
                    cur.execute(
                        "SELECT role, content FROM messages WHERE chat_id = %s ORDER BY created_at DESC LIMIT %s",
                        (chat_id, limit),
                    )
                    rows = cur.fetchall()
                    return [{"role": r["role"], "content": r["content"]} for r in reversed(rows)]
                cur.execute(
                    "SELECT role, content FROM messages WHERE chat_id = %s ORDER BY created_at ASC",
                    (chat_id,),
                )
                rows = cur.fetchall()
                return [{"role": r["role"], "content": r["content"]} for r in rows]

    def clear_messages(self, chat_id: str) -> None:
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute("DELETE FROM messages WHERE chat_id = %s", (chat_id,))

    # ── document metadata ────────────────────────────────────────────────────
    def upsert_doc_meta(
        self,
        doc_id: str,
        filename: str,
        status: str,
        page_count: int = 0,
        section_count: int = 0,
        progress: int = 0,
        current_stage: str = "uploaded",
        indexing_method: Optional[str] = None,
        duration: Optional[float] = None,
        error: Optional[str] = None,
        content_hash: Optional[str] = None,
        file_path: Optional[str] = None,
    ) -> None:
        now = time.time()
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    """
                    INSERT INTO doc_meta(
                        doc_id, filename, status, page_count, section_count,
                        progress, current_stage, indexing_method, duration,
                        error, content_hash, file_path, created_at, updated_at
                    )
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
                    ON CONFLICT (doc_id) DO UPDATE SET
                        filename = EXCLUDED.filename,
                        status = EXCLUDED.status,
                        page_count = EXCLUDED.page_count,
                        section_count = EXCLUDED.section_count,
                        progress = EXCLUDED.progress,
                        current_stage = EXCLUDED.current_stage,
                        indexing_method = COALESCE(EXCLUDED.indexing_method, doc_meta.indexing_method),
                        duration = COALESCE(EXCLUDED.duration, doc_meta.duration),
                        error = EXCLUDED.error,
                        content_hash = COALESCE(EXCLUDED.content_hash, doc_meta.content_hash),
                        file_path = COALESCE(EXCLUDED.file_path, doc_meta.file_path),
                        updated_at = EXCLUDED.updated_at
                    """,
                    (
                        doc_id,
                        filename,
                        status,
                        page_count,
                        section_count,
                        progress,
                        current_stage,
                        indexing_method,
                        duration,
                        error,
                        content_hash,
                        file_path,
                        now,
                        now,
                    ),
                )

    def get_doc_meta(self, doc_id: str) -> Optional[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute("SELECT * FROM doc_meta WHERE doc_id = %s", (doc_id,))
                row = cur.fetchone()
                return dict(row) if row else None

    def list_docs(self) -> List[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute("SELECT * FROM doc_meta ORDER BY created_at DESC")
                rows = cur.fetchall()
                return [dict(r) for r in rows]

    def find_doc_by_hash(self, content_hash: str) -> Optional[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute(
                    "SELECT * FROM doc_meta WHERE content_hash = %s ORDER BY updated_at DESC LIMIT 1",
                    (content_hash,),
                )
                row = cur.fetchone()
                return dict(row) if row else None

    def delete_doc(self, doc_id: str) -> bool:
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute("DELETE FROM doc_meta WHERE doc_id = %s", (doc_id,))
                rc = cur.rowcount
                cur.execute("UPDATE chats SET doc_id = NULL, doc_name = NULL WHERE doc_id = %s", (doc_id,))
                cur.execute("DELETE FROM chat_documents WHERE doc_id = %s", (doc_id,))
                cur.execute("DELETE FROM knowledge_memory WHERE doc_id = %s", (doc_id,))
                cur.execute("DELETE FROM doc_indices WHERE doc_id = %s", (doc_id,))
                return rc > 0

    # ── document heavy indices store (JSONB) ─────────────────────────────────
    def save_doc_index(
        self,
        doc_id: str,
        content_hash: Optional[str],
        tree: Any,
        node_map: Optional[Dict[str, Any]] = None,
        flat_sections: Optional[List[Dict[str, Any]]] = None,
        file_base64: Optional[str] = None,
    ) -> None:
        now = time.time()
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    """
                    INSERT INTO doc_indices (
                        doc_id, content_hash, tree, node_map, flat_sections, file_base64, created_at, updated_at
                    )
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
                    ON CONFLICT (doc_id) DO UPDATE SET
                        content_hash = COALESCE(EXCLUDED.content_hash, doc_indices.content_hash),
                        tree = EXCLUDED.tree,
                        node_map = EXCLUDED.node_map,
                        flat_sections = EXCLUDED.flat_sections,
                        file_base64 = COALESCE(EXCLUDED.file_base64, doc_indices.file_base64),
                        updated_at = EXCLUDED.updated_at
                    """,
                    (
                        doc_id,
                        content_hash,
                        psycopg2.extras.Json(tree) if tree is not None else None,
                        psycopg2.extras.Json(node_map) if node_map is not None else None,
                        psycopg2.extras.Json(flat_sections) if flat_sections is not None else None,
                        file_base64,
                        now,
                        now,
                    ),
                )

    def get_doc_index(self, doc_id: str) -> Optional[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute("SELECT * FROM doc_indices WHERE doc_id = %s", (doc_id,))
                row = cur.fetchone()
                return dict(row) if row else None

    def get_doc_index_by_hash(self, content_hash: str) -> Optional[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute(
                    "SELECT * FROM doc_indices WHERE content_hash = %s ORDER BY updated_at DESC LIMIT 1",
                    (content_hash,),
                )
                row = cur.fetchone()
                return dict(row) if row else None

    def delete_doc_index(self, doc_id: str) -> None:
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute("DELETE FROM doc_indices WHERE doc_id = %s", (doc_id,))

    # ── multi-doc session associations ──────────────────────────────────────
    def add_chat_document(self, chat_id: str, doc_id: str, doc_name: Optional[str] = None) -> None:
        now = time.time()
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    """
                    INSERT INTO chat_documents(chat_id, doc_id, doc_name, attached_at)
                    VALUES (%s, %s, %s, %s)
                    ON CONFLICT (chat_id, doc_id) DO UPDATE SET
                        doc_name = COALESCE(EXCLUDED.doc_name, chat_documents.doc_name),
                        attached_at = EXCLUDED.attached_at
                    """,
                    (chat_id, doc_id, doc_name, now),
                )
                # Maintain backward compatibility on chats table
                cur.execute(
                    "UPDATE chats SET doc_id = %s, doc_name = %s, updated_at = %s WHERE chat_id = %s",
                    (doc_id, doc_name, now, chat_id),
                )

    def remove_chat_document(self, chat_id: str, doc_id: str) -> None:
        now = time.time()
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute("DELETE FROM chat_documents WHERE chat_id = %s AND doc_id = %s", (chat_id, doc_id))
                cur.execute(
                    "SELECT doc_id, doc_name FROM chat_documents WHERE chat_id = %s ORDER BY attached_at DESC LIMIT 1",
                    (chat_id,),
                )
                rem = cur.fetchone()
                if rem:
                    cur.execute(
                        "UPDATE chats SET doc_id = %s, doc_name = %s, updated_at = %s WHERE chat_id = %s",
                        (rem["doc_id"], rem["doc_name"], now, chat_id),
                    )
                else:
                    cur.execute(
                        "UPDATE chats SET doc_id = NULL, doc_name = NULL, updated_at = %s WHERE chat_id = %s",
                        (now, chat_id),
                    )

    def get_chat_documents(self, chat_id: str) -> List[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute(
                    """
                    SELECT cd.chat_id, cd.doc_id, cd.doc_name, cd.attached_at,
                           dm.status, dm.page_count, dm.section_count, dm.filename
                    FROM chat_documents cd
                    LEFT JOIN doc_meta dm ON cd.doc_id = dm.doc_id
                    WHERE cd.chat_id = %s
                    ORDER BY cd.attached_at ASC
                    """,
                    (chat_id,),
                )
                rows = cur.fetchall()
                return [dict(r) for r in rows]

    def clear_chat_documents(self, chat_id: str) -> None:
        now = time.time()
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute("DELETE FROM chat_documents WHERE chat_id = %s", (chat_id,))
                cur.execute(
                    "UPDATE chats SET doc_id = NULL, doc_name = NULL, updated_at = %s WHERE chat_id = %s",
                    (now, chat_id),
                )

    # ── session long memory & rolling summaries ─────────────────────────────
    def set_chat_summary(self, chat_id: str, summary: str, turn_count: int = 0) -> None:
        now = time.time()
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    """
                    INSERT INTO chat_summaries(chat_id, summary, turn_count, updated_at)
                    VALUES (%s, %s, %s, %s)
                    ON CONFLICT (chat_id) DO UPDATE SET
                        summary = EXCLUDED.summary,
                        turn_count = EXCLUDED.turn_count,
                        updated_at = EXCLUDED.updated_at
                    """,
                    (chat_id, summary, turn_count, now),
                )

    def get_chat_summary(self, chat_id: str) -> Optional[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute("SELECT * FROM chat_summaries WHERE chat_id = %s", (chat_id,))
                row = cur.fetchone()
                return dict(row) if row else None

    # ── global knowledge memory & document dossiers ────────────────────────
    def save_knowledge_dossier(
        self,
        doc_id: str,
        filename: str,
        executive_summary: str,
        key_entities: Optional[List[str]] = None,
        key_findings: Optional[List[str]] = None,
        topics: Optional[List[str]] = None,
    ) -> None:
        now = time.time()
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    """
                    INSERT INTO knowledge_memory(
                        doc_id, filename, executive_summary, key_entities, key_findings, topics, updated_at
                    )
                    VALUES (%s, %s, %s, %s, %s, %s, %s)
                    ON CONFLICT (doc_id) DO UPDATE SET
                        filename = EXCLUDED.filename,
                        executive_summary = EXCLUDED.executive_summary,
                        key_entities = EXCLUDED.key_entities,
                        key_findings = EXCLUDED.key_findings,
                        topics = EXCLUDED.topics,
                        updated_at = EXCLUDED.updated_at
                    """,
                    (
                        doc_id,
                        filename,
                        executive_summary,
                        psycopg2.extras.Json(key_entities or []),
                        psycopg2.extras.Json(key_findings or []),
                        psycopg2.extras.Json(topics or []),
                        now,
                    ),
                )

    def get_knowledge_dossier(self, doc_id: str) -> Optional[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute("SELECT * FROM knowledge_memory WHERE doc_id = %s", (doc_id,))
                row = cur.fetchone()
                if not row:
                    return None
                res = dict(row)
                for k in ("key_entities", "key_findings", "topics"):
                    if res.get(k) is None:
                        res[k] = []
                return res

    def list_knowledge_dossiers(self) -> List[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute("SELECT * FROM knowledge_memory ORDER BY updated_at DESC")
                rows = cur.fetchall()
                result = []
                for r in rows:
                    item = dict(r)
                    for k in ("key_entities", "key_findings", "topics"):
                        if item.get(k) is None:
                            item[k] = []
                    result.append(item)
                return result

    def delete_knowledge_dossier(self, doc_id: str) -> None:
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute("DELETE FROM knowledge_memory WHERE doc_id = %s", (doc_id,))

    def save_global_knowledge_summary(
        self,
        collective_summary: str,
        total_documents: int,
        key_insights: Optional[List[str]] = None,
    ) -> None:
        now = time.time()
        gid = "global"
        with self._conn() as conn:
            with conn.cursor() as cur:
                cur.execute(
                    """
                    INSERT INTO global_knowledge_summary(
                        id, collective_summary, total_documents, key_insights, updated_at
                    )
                    VALUES (%s, %s, %s, %s, %s)
                    ON CONFLICT (id) DO UPDATE SET
                        collective_summary = EXCLUDED.collective_summary,
                        total_documents = EXCLUDED.total_documents,
                        key_insights = EXCLUDED.key_insights,
                        updated_at = EXCLUDED.updated_at
                    """,
                    (
                        gid,
                        collective_summary,
                        total_documents,
                        psycopg2.extras.Json(key_insights or []),
                        now,
                    ),
                )

    def get_global_knowledge_summary(self) -> Optional[Dict[str, Any]]:
        with self._conn() as conn:
            with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
                cur.execute("SELECT * FROM global_knowledge_summary WHERE id = 'global'")
                row = cur.fetchone()
                if not row:
                    return None
                res = dict(row)
                if res.get("key_insights") is None:
                    res["key_insights"] = []
                return res