-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

{- | Operator diagnostics for metadata failures, dropped entries, and integrity divergence.
Access-refusal logs contain no upstream body, headers, or credential.

The bad-upstream warnings carry 'pipelineInternalModule' as their @module@ filter key and every
other line carries 'pipelineModule'.
-}
module Ecluse.Core.Server.Pipeline.Diagnostics (
    -- * Metadata-read failures
    logMetadataFailure,
    logDirectoryFailure,

    -- * Dropped entries and divergence
    logInvalidEntries,
    logPackumentDrops,
    logVersionlessRefusal,
    logDirectoryInvalidEntries,
    warnDivergences,
) where

import Data.Aeson (Value)
import Data.Aeson.Text (encodeToLazyText)
import Data.Map.Strict qualified as Map
import Data.Text qualified as T
import Data.Text.Lazy qualified as TL
import Katip (KatipContext, Severity (DebugS, ErrorS, WarningS), SimpleLogPayload, katipAddContext, logFM, ls, sl)

import Ecluse.Core.Fault (TransportFault (tfCause, tfDetail))
import Ecluse.Core.Fault.Http (isRetryableStatusCode)

import Ecluse.Core.Package (
    HashAlg,
    InvalidEntry (invalidKey, invalidKind, invalidReason, invalidValue),
    PackageInfo (infoInvalidEntries, infoVersions),
    PackageName,
    dropCountsByKind,
    renderHashAlg,
    renderInvalidEntryKind,
    renderPackageName,
 )
import Ecluse.Core.Package.Merge (
    Divergence (divLosing, divVersion, divWinning),
    IntegrityFingerprint,
    MergePlan (mpDivergences),
    integrityHashes,
 )
import Ecluse.Core.Registry (
    FetchFault (FetchBoundExceeded, FetchCooldown, FetchTransport, FetchUrlUnformable),
    UrlFormationError,
    renderUrlFormationError,
 )
import Ecluse.Core.Registry.Metadata (
    MetadataError (MetadataAbsent, MetadataAuthorisationFailure, MetadataBoundExceeded, MetadataFetch, MetadataHttpFailure, MetadataNameMismatch, MetadataUndecodable),
 )
import Ecluse.Core.Security (
    BodyLimit (..),
    LimitError (BodyTooLarge, TooDeeplyNested, TooManyArtifacts, TooManyVersions),
    authorityLabel,
    bodyLimitBytes,
 )
import Ecluse.Core.Server.Pipeline.Internal (pipelineInternalModule, pipelineModule)
import Ecluse.Core.Telemetry.Record (MetricsPort (..))

-- | Log once per real fetch, inside the single-flight leader's request context.
logMetadataFailure :: (KatipContext m) => PackageName -> Text -> MetadataError -> m ()
logMetadataFailure :: forall (m :: * -> *).
KatipContext m =>
PackageName -> Text -> MetadataError -> m ()
logMetadataFailure = Subject -> Text -> MetadataError -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> MetadataError -> m ()
logFailureOf (Subject -> Text -> MetadataError -> m ())
-> (PackageName -> Subject)
-> PackageName
-> Text
-> MetadataError
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageName -> Subject
PackageSubject

-- | 'logMetadataFailure' for a registry-wide directory read. Its lines name no package.
logDirectoryFailure :: (KatipContext m) => Text -> MetadataError -> m ()
logDirectoryFailure :: forall (m :: * -> *).
KatipContext m =>
Text -> MetadataError -> m ()
logDirectoryFailure = Subject -> Text -> MetadataError -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> MetadataError -> m ()
logFailureOf Subject
DirectorySubject

-- What a read was for: one package's packument, or a registry-wide directory with no package to name.
data Subject = PackageSubject PackageName | DirectorySubject

subjectPayload :: Subject -> SimpleLogPayload
subjectPayload :: Subject -> SimpleLogPayload
subjectPayload = \case
    PackageSubject PackageName
name -> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"package" (PackageName -> Text
renderPackageName PackageName
name)
    Subject
DirectorySubject -> SimpleLogPayload
forall a. Monoid a => a
mempty

-- What the read requested, and the document it decodes.
requestedNoun, documentNoun :: Subject -> Text
requestedNoun :: Subject -> Text
requestedNoun = \case
    PackageSubject PackageName
_ -> Text
"package"
    Subject
DirectorySubject -> Text
"directory"
documentNoun :: Subject -> Text
documentNoun = \case
    PackageSubject PackageName
_ -> Text
"packument"
    Subject
DirectorySubject -> Text
"directory"

logFailureOf :: (KatipContext m) => Subject -> Text -> MetadataError -> m ()
logFailureOf :: forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> MetadataError -> m ()
logFailureOf Subject
subject Text
baseUrl = \case
    MetadataError
MetadataAbsent -> Subject -> Text -> Int -> Text -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> Int -> Text -> m ()
logHttpFailure Subject
subject Text
baseUrl Int
404 (Text
"the upstream has no metadata for the requested " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Subject -> Text
requestedNoun Subject
subject)
    MetadataHttpFailure Int
code Maybe RetryAfter
_ -> Subject -> Text -> Int -> Text -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> Int -> Text -> m ()
logHttpFailure Subject
subject Text
baseUrl Int
code Text
"the upstream refused the metadata read"
    MetadataAuthorisationFailure Int
code -> Subject -> Text -> Int -> Text -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> Int -> Text -> m ()
logHttpFailure Subject
subject Text
baseUrl Int
code Text
"the upstream refused metadata access"
    MetadataBoundExceeded LimitError
err -> Subject -> LimitError -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> LimitError -> m ()
logBreach Subject
subject LimitError
err
    MetadataError
MetadataUndecodable -> Subject -> m ()
forall (m :: * -> *). KatipContext m => Subject -> m ()
warnDecodeFailure Subject
subject
    MetadataNameMismatch Text
reported -> Subject -> Text -> Text -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> Text -> m ()
warnNameMismatch Subject
subject Text
baseUrl Text
reported
    MetadataFetch (FetchBoundExceeded LimitError
err) -> Subject -> LimitError -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> LimitError -> m ()
logBreach Subject
subject LimitError
err
    MetadataFetch (FetchUrlUnformable UrlFormationError
urlErr) -> Subject -> Text -> UrlFormationError -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> UrlFormationError -> m ()
warnUnformable Subject
subject Text
baseUrl UrlFormationError
urlErr
    MetadataFetch (FetchTransport TransportFault
fault) -> Subject -> Text -> TransportFault -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> TransportFault -> m ()
warnUnreachable Subject
subject Text
baseUrl TransportFault
fault
    MetadataFetch (FetchCooldown RetryAfter
_) -> m ()
forall (f :: * -> *). Applicative f => f ()
pass

-- A package 404 is a genuine absence. A directory 404 leaves discovery without its source, so it is a fault.
logHttpFailure :: (KatipContext m) => Subject -> Text -> Int -> Text -> m ()
logHttpFailure :: forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> Int -> Text -> m ()
logHttpFailure Subject
subject Text
baseUrl Int
code Text
message =
    SimpleLogPayload -> m () -> m ()
forall i (m :: * -> *) a.
(LogItem i, KatipContext m) =>
i -> m a -> m a
katipAddContext SimpleLogPayload
payload (Severity -> LogStr -> m ()
forall (m :: * -> *).
(Applicative m, KatipContext m) =>
Severity -> LogStr -> m ()
logFM Severity
severity (Text -> LogStr
forall a. StringConv a Text => a -> LogStr
ls Text
message))
  where
    severity :: Severity
severity
        | Int
code Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
404, PackageSubject PackageName
_ <- Subject
subject = Severity
DebugS
        | Int -> Bool
isRetryableStatusCode Int
code = Severity
ErrorS
        | Bool
otherwise = Severity
WarningS
    payload :: SimpleLogPayload
payload =
        Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"module" Text
pipelineModule
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Subject -> SimpleLogPayload
subjectPayload Subject
subject
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"upstream" (Text -> Text
authorityLabel Text
baseUrl)
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Int -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"status" Int
code

logBreach :: (KatipContext m) => Subject -> LimitError -> m ()
logBreach :: forall (m :: * -> *).
KatipContext m =>
Subject -> LimitError -> m ()
logBreach Subject
subject LimitError
err =
    SimpleLogPayload -> m () -> m ()
forall i (m :: * -> *) a.
(LogItem i, KatipContext m) =>
i -> m a -> m a
katipAddContext SimpleLogPayload
payload (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
        Severity -> LogStr -> m ()
forall (m :: * -> *).
(Applicative m, KatipContext m) =>
Severity -> LogStr -> m ()
logFM Severity
WarningS (Text -> LogStr
forall a. StringConv a Text => a -> LogStr
ls Text
message)
  where
    payload :: SimpleLogPayload
payload =
        Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"module" Text
pipelineModule
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Subject -> SimpleLogPayload
subjectPayload Subject
subject
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"bound" Text
boundName
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"observed" Text
observed
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"cap" Text
cap

    message :: Text
    message :: Text
message = Text
"refused an upstream metadata document: it exceeded the " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
boundName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" response bound (observed " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
observed Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", cap " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
cap Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"

    boundName :: Text
    observed :: Text
    cap :: Text
    (Text
boundName, Text
observed, Text
cap) = case LimitError
err of
        BodyTooLarge BodyLimit
bound ->
            let c :: Int
c = BodyLimit -> Int
bodyLimitBytes BodyLimit
bound
                role :: Text
role = case BodyLimit
bound of
                    MetadataBodyLimit Int
_ -> Text
"metadata-body-size"
                    PublishRequestBodyLimit Int
_ -> Text
"publish-request-body-size"
                    MirrorArtifactBodyLimit Int
_ -> Text
"mirror-artifact-body-size"
             in (Text
role, Text
"over " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
c Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" bytes", Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
c Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" bytes")
        TooManyVersions Int
seen Int
c -> (Text
"version-count", Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
seen, Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
c)
        TooManyArtifacts Int
seen Int
c -> (Text
"artifact-count", Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
seen, Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
c)
        TooDeeplyNested Int
c -> (Text
"nesting-depth", Text
"over " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
c Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" levels", Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
c Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" levels")

-- The fields every bad-upstream warning carries, before the caller's own. The response-bound
-- guards leave these conditions silent, so an operator would otherwise see nothing at all.
warnUpstream :: (KatipContext m) => Subject -> SimpleLogPayload -> Text -> m ()
warnUpstream :: forall (m :: * -> *).
KatipContext m =>
Subject -> SimpleLogPayload -> Text -> m ()
warnUpstream Subject
subject SimpleLogPayload
extra Text
message =
    SimpleLogPayload -> m () -> m ()
forall i (m :: * -> *) a.
(LogItem i, KatipContext m) =>
i -> m a -> m a
katipAddContext (Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"module" Text
pipelineInternalModule SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Subject -> SimpleLogPayload
subjectPayload Subject
subject SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> SimpleLogPayload
extra) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Severity -> LogStr -> m ()
forall (m :: * -> *).
(Applicative m, KatipContext m) =>
Severity -> LogStr -> m ()
logFM Severity
WarningS (Text -> LogStr
forall a. StringConv a Text => a -> LogStr
ls Text
message)

warnDecodeFailure :: (KatipContext m) => Subject -> m ()
warnDecodeFailure :: forall (m :: * -> *). KatipContext m => Subject -> m ()
warnDecodeFailure Subject
subject =
    Subject -> SimpleLogPayload -> Text -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> SimpleLogPayload -> Text -> m ()
warnUpstream Subject
subject SimpleLogPayload
forall a. Monoid a => a
mempty (Text
"refused an upstream metadata document: it did not decode into a usable " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Subject -> Text
documentNoun Subject
subject)

warnNameMismatch :: (KatipContext m) => Subject -> Text -> Text -> m ()
warnNameMismatch :: forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> Text -> m ()
warnNameMismatch Subject
subject Text
origin Text
reported =
    Subject -> SimpleLogPayload -> Text -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> SimpleLogPayload -> Text -> m ()
warnUpstream
        Subject
subject
        (Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"origin" (Text -> Text
authorityLabel Text
origin) SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"upstreamName" Text
reported)
        Text
"refused an upstream metadata document: its packument self-reported a name for a different package"

warnUnformable :: (KatipContext m) => Subject -> Text -> UrlFormationError -> m ()
warnUnformable :: forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> UrlFormationError -> m ()
warnUnformable Subject
subject Text
origin UrlFormationError
urlErr =
    Subject -> SimpleLogPayload -> Text -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> SimpleLogPayload -> Text -> m ()
warnUpstream
        Subject
subject
        (Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"origin" (Text -> Text
authorityLabel Text
origin) SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"urlError" (UrlFormationError -> Text
renderUrlFormationError UrlFormationError
urlErr))
        Text
"refused an upstream metadata fetch: the configured base URL could not be formed into a request"

warnUnreachable :: (KatipContext m) => Subject -> Text -> TransportFault -> m ()
warnUnreachable :: forall (m :: * -> *).
KatipContext m =>
Subject -> Text -> TransportFault -> m ()
warnUnreachable Subject
subject Text
origin TransportFault
fault =
    Subject -> SimpleLogPayload -> Text -> m ()
forall (m :: * -> *).
KatipContext m =>
Subject -> SimpleLogPayload -> Text -> m ()
warnUpstream
        Subject
subject
        ( Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"origin" (Text -> Text
authorityLabel Text
origin)
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"transportCause" (TransportCause -> Text
forall b a. (Show a, IsString b) => a -> b
show (TransportFault -> TransportCause
tfCause TransportFault
fault) :: Text)
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"transportDetail" (TransportFault -> Text
tfDetail TransportFault
fault)
        )
        Text
"an upstream metadata fetch could not reach the origin; its contribution degrades this request"

-- | The malformed packument entries the projection dropped rather than failing the whole document.
logInvalidEntries :: (KatipContext m) => PackageName -> Text -> [InvalidEntry] -> m ()
logInvalidEntries :: forall (m :: * -> *).
KatipContext m =>
PackageName -> Text -> [InvalidEntry] -> m ()
logInvalidEntries = Text -> Subject -> Text -> [InvalidEntry] -> m ()
forall (m :: * -> *).
KatipContext m =>
Text -> Subject -> Text -> [InvalidEntry] -> m ()
logDroppedEntries Text
"the rest is served" (Subject -> Text -> [InvalidEntry] -> m ())
-> (PackageName -> Subject)
-> PackageName
-> Text
-> [InvalidEntry]
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageName -> Subject
PackageSubject

-- | The entries one packument read dropped, naming a document that kept no version as such.
logPackumentDrops :: (KatipContext m) => PackageName -> Text -> PackageInfo -> m ()
logPackumentDrops :: forall (m :: * -> *).
KatipContext m =>
PackageName -> Text -> PackageInfo -> m ()
logPackumentDrops PackageName
name Text
baseUrl PackageInfo
info
    | Map Text PackageDetails -> Bool
forall k a. Map k a -> Bool
Map.null (PackageInfo -> Map Text PackageDetails
infoVersions PackageInfo
info) = Text -> Subject -> Text -> [InvalidEntry] -> m ()
forall (m :: * -> *).
KatipContext m =>
Text -> Subject -> Text -> [InvalidEntry] -> m ()
logDroppedEntries Text
"none of its versions remain" (PackageName -> Subject
PackageSubject PackageName
name) Text
baseUrl (PackageInfo -> [InvalidEntry]
infoInvalidEntries PackageInfo
info)
    | Bool
otherwise = PackageName -> Text -> [InvalidEntry] -> m ()
forall (m :: * -> *).
KatipContext m =>
PackageName -> Text -> [InvalidEntry] -> m ()
logInvalidEntries PackageName
name Text
baseUrl (PackageInfo -> [InvalidEntry]
infoInvalidEntries PackageInfo
info)

-- | A request refused because this upstream's packument kept no version. Its read logged the dropped entries.
logVersionlessRefusal :: (KatipContext m) => PackageName -> Text -> Text -> Int -> m ()
logVersionlessRefusal :: forall (m :: * -> *).
KatipContext m =>
PackageName -> Text -> Text -> Int -> m ()
logVersionlessRefusal PackageName
name Text
upstream Text
baseUrl Int
dropped =
    SimpleLogPayload -> m () -> m ()
forall i (m :: * -> *) a.
(LogItem i, KatipContext m) =>
i -> m a -> m a
katipAddContext (Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"module" Text
pipelineModule SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Subject -> SimpleLogPayload
subjectPayload (PackageName -> Subject
PackageSubject PackageName
name) SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"upstream" (Text -> Text
authorityLabel Text
baseUrl) SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Int -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"dropped" Int
dropped) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
        Severity -> LogStr -> m ()
forall (m :: * -> *).
(Applicative m, KatipContext m) =>
Severity -> LogStr -> m ()
logFM Severity
WarningS (Text -> LogStr
forall a. StringConv a Text => a -> LogStr
ls (Text
"refused the request: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
upstream Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'s packument kept no version after dropping " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
malformedCount Int
dropped))

-- | The malformed rows a registry-wide directory dropped. The line names no package.
logDirectoryInvalidEntries :: (KatipContext m) => Text -> [InvalidEntry] -> m ()
logDirectoryInvalidEntries :: forall (m :: * -> *).
KatipContext m =>
Text -> [InvalidEntry] -> m ()
logDirectoryInvalidEntries = Text -> Subject -> Text -> [InvalidEntry] -> m ()
forall (m :: * -> *).
KatipContext m =>
Text -> Subject -> Text -> [InvalidEntry] -> m ()
logDroppedEntries Text
"the rest is served" Subject
DirectorySubject

logDroppedEntries :: (KatipContext m) => Text -> Subject -> Text -> [InvalidEntry] -> m ()
logDroppedEntries :: forall (m :: * -> *).
KatipContext m =>
Text -> Subject -> Text -> [InvalidEntry] -> m ()
logDroppedEntries Text
outcome Subject
subject Text
baseUrl [InvalidEntry]
entries =
    SimpleLogPayload -> m () -> m ()
forall i (m :: * -> *) a.
(LogItem i, KatipContext m) =>
i -> m a -> m a
katipAddContext SimpleLogPayload
payload (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
        Severity -> LogStr -> m ()
forall (m :: * -> *).
(Applicative m, KatipContext m) =>
Severity -> LogStr -> m ()
logFM Severity
WarningS (Text -> LogStr
forall a. StringConv a Text => a -> LogStr
ls Text
message)
  where
    payload :: SimpleLogPayload
payload =
        Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"module" Text
pipelineModule
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Subject -> SimpleLogPayload
subjectPayload Subject
subject
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"upstream" (Text -> Text
authorityLabel Text
baseUrl)
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Map Text Int -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"droppedByKind" ([InvalidEntry] -> Map Text Int
dropCountsByKind [InvalidEntry]
entries)
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"droppedEntries" ((InvalidEntry -> Text) -> [InvalidEntry] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map InvalidEntry -> Text
renderDroppedEntry (Int -> [InvalidEntry] -> [InvalidEntry]
forall a. Int -> [a] -> [a]
take Int
maxRenderedDrops [InvalidEntry]
entries))
    message :: Text
message = Text
"dropped " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
malformedCount ([InvalidEntry] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [InvalidEntry]
entries) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" from an upstream " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Subject -> Text
documentNoun Subject
subject Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
outcome Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"

malformedCount :: Int -> Text
malformedCount :: Int -> Text
malformedCount Int
count = Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
count Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" malformed entr" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> if Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 then Text
"y" else Text
"ies"

renderDroppedEntry :: InvalidEntry -> Text
renderDroppedEntry :: InvalidEntry -> Text
renderDroppedEntry InvalidEntry
e =
    InvalidEntryKind -> Text
renderInvalidEntryKind (InvalidEntry -> InvalidEntryKind
invalidKind InvalidEntry
e)
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" "
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> InvalidEntry -> Text
invalidKey InvalidEntry
e
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" = "
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Value -> Text
truncatedValue (InvalidEntry -> Value
invalidValue InvalidEntry
e)
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" ("
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> InvalidEntry -> Text
invalidReason InvalidEntry
e
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"

-- Only 'maxRenderedValueChars' characters are ever forced, so a huge value never balloons
-- the log line.
truncatedValue :: Value -> Text
truncatedValue :: Value -> Text
truncatedValue Value
v =
    let rendered :: Text
rendered = LazyText -> Text
TL.toStrict (Int64 -> LazyText -> LazyText
TL.take (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
maxRenderedValueChars Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
1) (Value -> LazyText
forall a. ToJSON a => a -> LazyText
encodeToLazyText Value
v))
     in if Text -> Int -> Ordering
T.compareLength Text
rendered Int
maxRenderedValueChars Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT
            then Int -> Text -> Text
T.take Int
maxRenderedValueChars Text
rendered Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"…"
            else Text
rendered

-- How many dropped entries the log renders in full, and how many characters of each raw value, so
-- a flood of drops or one huge value cannot bloat a log line. The per-kind counts stay complete.
maxRenderedDrops :: Int
maxRenderedDrops :: Int
maxRenderedDrops = Int
20

maxRenderedValueChars :: Int
maxRenderedValueChars :: Int
maxRenderedValueChars = Int
200

-- | Warn and increment the divergence metric when shared digests disagree across origins.
warnDivergences :: (KatipContext m) => MetricsPort -> PackageName -> MergePlan -> m ()
warnDivergences :: forall (m :: * -> *).
KatipContext m =>
MetricsPort -> PackageName -> MergePlan -> m ()
warnDivergences MetricsPort
metrics PackageName
name MergePlan
plan =
    case Set Divergence -> [Divergence]
forall a. Set a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (MergePlan -> Set Divergence
mpDivergences MergePlan
plan) of
        [] -> m ()
forall (f :: * -> *). Applicative f => f ()
pass
        [Divergence]
divs -> do
            IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ([Divergence] -> (Divergence -> IO ()) -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Divergence]
divs (IO () -> Divergence -> IO ()
forall a b. a -> b -> a
const (MetricsPort -> IO ()
mpMergeDivergence MetricsPort
metrics)))
            SimpleLogPayload -> m () -> m ()
forall i (m :: * -> *) a.
(LogItem i, KatipContext m) =>
i -> m a -> m a
katipAddContext ([Divergence] -> SimpleLogPayload
payload [Divergence]
divs) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Severity -> LogStr -> m ()
forall (m :: * -> *).
(Applicative m, KatipContext m) =>
Severity -> LogStr -> m ()
logFM Severity
WarningS (Text -> LogStr
forall a. StringConv a Text => a -> LogStr
ls ([Divergence] -> Text
message [Divergence]
divs))
  where
    payload :: [Divergence] -> SimpleLogPayload
payload [Divergence]
divs =
        Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"module" Text
pipelineModule
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"package" (PackageName -> Text
renderPackageName PackageName
name)
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"versions" (Text -> [Text] -> Text
T.intercalate Text
"," ((Divergence -> Text) -> [Divergence] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Divergence -> Text
divVersion [Divergence]
divs))
    message :: [Divergence] -> Text
message [Divergence]
divs =
        Text
"cross-upstream integrity divergence: the trusted copy of "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> PackageName -> Text
renderPackageName PackageName
name
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is served, but a public copy contradicts it on a shared integrity algorithm for "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall b a. (Show a, IsString b) => a -> b
show ([Divergence] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Divergence]
divs)
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" version(s): "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
"; " ((Divergence -> Text) -> [Divergence] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Divergence -> Text
renderDivergence [Divergence]
divs)

renderDivergence :: Divergence -> Text
renderDivergence :: Divergence -> Text
renderDivergence Divergence
d =
    Divergence -> Text
divVersion Divergence
d
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (trusted "
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> IntegrityFingerprint -> Text
renderFingerprint (Divergence -> IntegrityFingerprint
divWinning Divergence
d)
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" vs public "
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> IntegrityFingerprint -> Text
renderFingerprint (Divergence -> IntegrityFingerprint
divLosing Divergence
d)
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"

renderFingerprint :: IntegrityFingerprint -> Text
renderFingerprint :: IntegrityFingerprint -> Text
renderFingerprint IntegrityFingerprint
fp = Text
"{" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
", " (((Text, Maybe HashAlg, Text) -> Text)
-> [(Text, Maybe HashAlg, Text)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text, Maybe HashAlg, Text) -> Text
renderHash (IntegrityFingerprint -> [(Text, Maybe HashAlg, Text)]
integrityHashes IntegrityFingerprint
fp)) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"}"

renderHash :: (Text, Maybe HashAlg, Text) -> Text
renderHash :: (Text, Maybe HashAlg, Text) -> Text
renderHash (Text
file, Maybe HashAlg
alg, Text
body) = Text
file Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> (HashAlg -> Text) -> Maybe HashAlg -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"none" HashAlg -> Text
renderHashAlg Maybe HashAlg
alg Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
body