- 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>
40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
import structlog
|
|
|
|
_log = structlog.get_logger(__name__)
|
|
|
|
_UNSAFE = str.maketrans({"/": "_", "\\": "_", ":": "_", "<": "", ">": ""})
|
|
|
|
|
|
async def save_eml(
|
|
archive_root: Path,
|
|
envelope_id: str,
|
|
source: str,
|
|
ts: datetime,
|
|
raw: bytes,
|
|
) -> str:
|
|
"""Write raw .eml bytes to the append-only archive.
|
|
|
|
Layout: {archive_root}/{source}/{YYYY}/{MM}/{sanitized_id}.eml
|
|
|
|
Returns the relative raw_ref path (store this in Envelope.raw_ref).
|
|
Raises FileExistsError if envelope_id is already archived (append-only guarantee).
|
|
"""
|
|
safe_id = envelope_id.translate(_UNSAFE)
|
|
rel_path = f"{source}/{ts.year:04d}/{ts.month:02d}/{safe_id}.eml"
|
|
dest = archive_root / rel_path
|
|
|
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
if dest.exists():
|
|
raise FileExistsError(f"archive: {rel_path} already exists — archive is append-only")
|
|
|
|
await asyncio.to_thread(dest.write_bytes, raw)
|
|
_log.info("archive.saved", raw_ref=rel_path, bytes=len(raw))
|
|
return rel_path
|