feat(identity): add server sessions and audited mail recovery

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 11:03:46 +03:00
1 parent 20c03288d6
commit 4ee53d9a74
58 files changed
+5359 -567

No files matched your search

+60 -6
View File
@@ -19,6 +19,11 @@ var (
// exists. Nothing was written; read the delivery again.
ErrDeliveryVersionConflict = errors.New("delivery changed concurrently")
// ErrDeliveryRetryNotAllowed reports that an operator retry did not match
// the current terminal unknown delivery, its provider account, the
// deduplication cutoff, or the required duplicate-risk acknowledgement.
ErrDeliveryRetryNotAllowed = errors.New("unknown delivery retry is not allowed in its current state")
// ErrDeliveryContentImmutable reports a statement that tried to change
// the recipient, content, provider account, or idempotency key of a
// stored delivery, or a recorded attempt. The database refuses it; it
@@ -67,6 +72,11 @@ type DeliveryRepository interface {
// no longer matches its hash.
Get(ctx context.Context, id uuid.UUID) (domain.Delivery, error)
// GetRetrySnapshot returns operational state for one delivery without its
// recipient address or message content. It is used by the bounded local
// operator retry interface and logs.
GetRetrySnapshot(ctx context.Context, id uuid.UUID) (DeliveryRetrySnapshot, error)
// ListAttempts returns the recorded attempts in attempt order; an empty
// list for a delivery without attempts or an unknown ID.
ListAttempts(ctx context.Context, id uuid.UUID) ([]domain.DeliveryAttempt, error)
@@ -100,6 +110,22 @@ type DeliveryRepository interface {
// the sending worker's lease.
Cancel(ctx context.Context, next domain.Delivery) error
// AuthorizeUnknownRetry atomically schedules one operator-approved retry
// and appends its immutable audit record. It succeeds only for an unknown
// delivery with no automatic retry or live lease, after the safe provider
// window, pinned to account, with an operator actor and explicit risk ack.
// It returns ErrDeliveryRetryNotAllowed when that state no longer matches.
AuthorizeUnknownRetry(ctx context.Context, id uuid.UUID, account domain.MailProviderAccount, actor domain.MailRetryActor, acknowledgeDuplicateRisk bool, at time.Time, safeWindow time.Duration) error
// StopUnknownRetry stores next as returned by domain.Delivery.StopAutomaticRetry
// and releases owner lease without appending a provider attempt.
StopUnknownRetry(ctx context.Context, owner uuid.UUID, next domain.Delivery) error
// ScheduleUnknownRetry stores next as returned by
// domain.Delivery.ScheduleUnknownRetry and releases owner lease without an
// attempt, for a quota wait that applies to an uncertain retry.
ScheduleUnknownRetry(ctx context.Context, owner uuid.UUID, next domain.Delivery) error
// ClaimDue gives the claim's owner a lease on the most overdue delivery
// it may send and returns it, or returns ErrNoDeliveryDue. A claimed row
// that cannot be restored is reported as an *UnreadableDeliveryError;
@@ -122,9 +148,12 @@ type DeliveryRepository interface {
// lock before the quota rows, as the claim does.
LockClaimed(ctx context.Context, id, owner uuid.UUID) (bool, error)
// MarkSendStarted records, by the database clock and only the first time,
// that the first request of the claimed delivery is about to start.
MarkSendStarted(ctx context.Context, id, owner uuid.UUID) error
// MarkSendStarted records, by the database clock, that the request is about
// to start. When requireDedupWindow is true it returns false if the provider
// safety window expired after claim; this check is the last storage action
// before the network call. An authorized manual cycle records a separate
// first-start timestamp without replacing the original history.
MarkSendStarted(ctx context.Context, id, owner uuid.UUID, requireDedupWindow bool, safeWindow time.Duration) (started bool, err error)
// RecordFailure counts a failure of the sending worker on the delivery
// (not a provider answer), ends its lease, and makes it due again at
@@ -151,6 +180,19 @@ type DeliveryRepository interface {
ServiceMailState(ctx context.Context, id uuid.UUID) (domain.ServiceMailState, error)
}
// DeliveryRetrySnapshot contains only the metadata needed to inspect an
// unknown delivery before an operator decision.
type DeliveryRetrySnapshot struct {
ID uuid.UUID
Provider string
ProviderAccount string
Status domain.DeliveryStatus
AttemptCount int32
NextAttemptAt time.Time
FirstSendStartedAt time.Time
ManualRetryStartedAt time.Time
}
// DeliveryClaim selects and leases the next delivery for a sending worker.
type DeliveryClaim struct {
// Owner identifies the worker; a lease belongs to it.
@@ -180,11 +222,23 @@ type ClaimedDelivery struct {
// and let expire without finishing, so an attempt may be in doubt; zero
// when no worker held the delivery before.
PreviousClaimedUntil time.Time
// SinceFirstSend is how long ago, by the database clock, the first
// request of the delivery started; meaningful only when FirstSendStarted.
// The provider keeps the idempotency key for a window counted from then.
// SinceFirstSend is how long ago, by the database clock, the original
// first provider request started. A manual decision never resets this
// provider idempotency age. Meaningful only when FirstSendStarted.
SinceFirstSend time.Duration
FirstSendStarted bool
// ManualRetryPending is true after operator authorization and until the
// first provider call of that manual cycle starts.
ManualRetryPending bool
// ManualRetryCycleStarted is true after that first manual provider call
// starts. It is a single-use and recovery marker, not a new provider window.
ManualRetryCycleStarted bool
// SendStartUnsettled is true when the latest provider call started with the
// current attempt_count and no result was recorded afterward.
SendStartUnsettled bool
// HasUncertainAttempt is true when this delivery has any recorded unknown
// provider result, even if a later confirmed refusal moved it to pending.
HasUncertainAttempt bool
// FailureCount is the number of consecutive failures of the worker on
// this delivery so far.
FailureCount int32