homelab-codex-ws/packages/kb-mail/src/kb_mail/envelope.py
oskar 1666511475 feat(kb-mail): fundament — pgvector spine, koperta, archiwum, pakiet domeny
- services/kb-postgres: pgvector/pgvector:pg16 na SOLARIA (:5433), named
  volume, init/001_envelope.sql (CREATE EXTENSION vector + zamrożona tabela
  envelope: id/source/ts/geo/raw_ref/entities), service.yaml, healthcheck,
  README z poprawnym mechanizmem deploy (deploy-node.sh składa dwa -f)
- hosts/solaria/runtime/kb-postgres/docker-compose.override.yml: mem_limit 4g
- inventory/topology.yaml + hosts/solaria/services.yaml: kb-postgres wpisany
- packages/kb-mail: nowa konwencja shared lib (pip install /repo/packages/<lib>/)
  envelope.py — @dataclass Envelope, walidacja tz-aware ts
  db.py       — insert_envelope / get_envelope (asyncpg, ON CONFLICT DO NOTHING)
  archive.py  — save_eml append-only (asyncio.to_thread, FileExistsError na dup)
  tests: 15 unit pass + 5 integration (@pytest.mark.integration, wymaga KB_TEST_DSN)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-19 20:02:25 +02:00

26 lines
952 B
Python

from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from typing import Optional
@dataclass
class Envelope:
"""Frozen cross-source envelope — the only schema contract locked in upfront.
Additive: new fields may be added but existing ones must not be removed or renamed.
All fields mirror the `envelope` table in kb-postgres (init/001_envelope.sql).
"""
id: str
source: str # fastmail | gmail | …
ts: datetime # message date, must be UTC-aware
raw_ref: str # relative path to .eml in archive
geo: Optional[dict] = None # null for mails; filled by layer-3 enrich
entities: list = field(default_factory=list)
def __post_init__(self) -> None:
if self.ts.tzinfo is None:
raise ValueError("Envelope.ts must be timezone-aware (UTC)")