Files
mtusi2026labs/src/messenger/attachments/domain/models.py
T
ArtyomandClaude Opus 5.5 91731fd317 Reconcile attachment metadata with the bucket in one merge pass
StorageReconciler walks the sorted bucket listing and keyset pages of
attachments ordered by object key side by side, so a pass is linear
and keeps one page of each side in memory. An object without a row is
deleted only when it is older than grace plus skew and a lookup by key
right before the delete still finds no row; a ready row whose object
is missing, has another size or, with the deep check, another digest
is put in quarantine after a HEAD recheck and is never deleted; a stale
intent is claimed with SKIP LOCKED and dropped object first; a
quarantined row whose object matches again is released. Deep checks
hash objects in a bounded task group, each action uses its own unit of
work, and failures of single actions are counted instead of aborting.
An advisory lock lets one pass run at a time. Content of a quarantined
attachment answers 404 without touching S3, its metadata is unchanged.

The storage port gains listing pages and HEAD, the repository keyset
pages, lookup by key, claim and quarantine marking; the platform logger
can write to another stream. The one-off entry point
python -m messenger.attachments.entrypoints.reconcile is a dry run
unless --apply is given, assembles the adapters with the API key of S3
and prints a JSON report with counts per class, samples and the cost
of the pass to stdout while the journal goes to stderr.

Tests run the reconciler against its own migrated database with every
class, apply and repeat, the row that appears after the listing, the
busy lock and the release, and check paged byte-order listing and HEAD
against RustFS.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
2026-10-29 03:12:10 +03:00

98 lines
2.6 KiB
Python

from dataclasses import dataclass, field
from datetime import datetime
from enum import StrEnum
from uuid import UUID
from messenger.attachments.domain.events import AttachmentDiscarded, AttachmentStored
from messenger.attachments.domain.values import Filename, ImageInfo
from messenger.shared.domain.events import DomainEvent
OBJECT_PREFIX = "attachments/"
class AttachmentStatus(StrEnum):
PENDING = "pending"
READY = "ready"
class Quarantine(StrEnum):
MISSING = "missing"
SIZE_MISMATCH = "size_mismatch"
SHA_MISMATCH = "sha_mismatch"
@dataclass(slots=True)
class Attachment:
id: UUID
chat_id: UUID
owner_id: UUID
filename: Filename
image: ImageInfo
size_bytes: int
sha256: str
object_key: str
status: AttachmentStatus
created_at: datetime
updated_at: datetime
quarantine: Quarantine | None = None
_events: list[DomainEvent] = field(init=False, default_factory=list, repr=False, compare=False)
@classmethod
def stage(
cls,
*,
attachment_id: UUID,
chat_id: UUID,
owner_id: UUID,
filename: Filename,
image: ImageInfo,
size_bytes: int,
sha256: str,
now: datetime,
) -> Attachment:
return cls(
id=attachment_id,
chat_id=chat_id,
owner_id=owner_id,
filename=filename,
image=image,
size_bytes=size_bytes,
sha256=sha256,
object_key=f"{OBJECT_PREFIX}{attachment_id}",
status=AttachmentStatus.PENDING,
created_at=now,
updated_at=now,
)
@property
def content_type(self) -> str:
return self.image.kind.value
def store(self, now: datetime) -> None:
self.status = AttachmentStatus.READY
self.updated_at = max(now, self.created_at)
self._events.append(
AttachmentStored(
attachment_id=self.id,
chat_id=self.chat_id,
owner_id=self.owner_id,
size_bytes=self.size_bytes,
)
)
def discard(self, reason: str) -> None:
self._events.append(
AttachmentDiscarded(attachment_id=self.id, chat_id=self.chat_id, reason=reason)
)
def isolate(self, reason: Quarantine) -> None:
if self.status is AttachmentStatus.READY:
self.quarantine = reason
def release(self) -> None:
self.quarantine = None
def collect_events(self) -> list[DomainEvent]:
events, self._events = self._events, []
return events