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

-- | Bounded LF framing scans complete sparse files and quarantines duplicate native release identities.
module Ecluse.Core.Registry.Cargo.Reader (
    CargoRead (..),
    CargoIndexResult (..),
    readCargoIndexCharged,
    cargoIndexSteps,
) where

import Data.Aeson (Value (Null))
import Data.ByteString qualified as BS
import Data.Map.Strict qualified as Map
import Data.Primitive.SmallArray (smallArrayFromList)

import Ecluse.Core.Package (InvalidEntry, InvalidEntryKind (InvalidVersionManifest), PackageDetails (pkgVersion), PackageInfo (..), PackageName, mkInvalidEntry)
import Ecluse.Core.Package.Entry (EntryKey (RecordEntry))
import Ecluse.Core.Registry.CachedDocument (CachedDoc, cargoCached, cargoPacked)
import Ecluse.Core.Registry.Cargo.Discovery (CargoDiscovery)
import Ecluse.Core.Registry.Cargo.Document (PackedIndex (..), cargoDocument)
import Ecluse.Core.Registry.Cargo.Projection (projectCargoRecord)
import Ecluse.Core.Registry.Cargo.Wire (CargoRecordError (..), decodeCargoRecord, recordIdentity)
import Ecluse.Core.Registry.Json.Packed (Packed, docTable)
import Ecluse.Core.Registry.Json.Writer (packTree)
import Ecluse.Core.Registry.JsonStream (Steps (..), isJsonSpace, readStepsWithEnd)
import Ecluse.Core.Registry.Metadata.Fetch.Types (DocumentWalk)
import Ecluse.Core.Registry.Metadata.RecordStream (finishRecordSteps, recordFragmentCharge, recordSteps)
import Ecluse.Core.Security (Limits (maxNestingDepth), checkVersionCountOf)
import Ecluse.Core.Server.MemoryModel (saturatingCharge)
import Ecluse.Core.Strict (strictElements)
import Ecluse.Core.Version (Version, renderVersion)
import Ecluse.Core.Version.Cargo (cargoUniquenessKey, parseCargo)

-- | Selected reads retain one raw version but still inspect every physical record through EOF.
data CargoRead = FullRead | SelectedRead Version
    deriving stock (CargoRead -> CargoRead -> Bool
(CargoRead -> CargoRead -> Bool)
-> (CargoRead -> CargoRead -> Bool) -> Eq CargoRead
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CargoRead -> CargoRead -> Bool
== :: CargoRead -> CargoRead -> Bool
$c/= :: CargoRead -> CargoRead -> Bool
/= :: CargoRead -> CargoRead -> Bool
Eq, Int -> CargoRead -> ShowS
[CargoRead] -> ShowS
CargoRead -> String
(Int -> CargoRead -> ShowS)
-> (CargoRead -> String)
-> ([CargoRead] -> ShowS)
-> Show CargoRead
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CargoRead -> ShowS
showsPrec :: Int -> CargoRead -> ShowS
$cshow :: CargoRead -> String
show :: CargoRead -> String
$cshowList :: [CargoRead] -> ShowS
showList :: [CargoRead] -> ShowS
Show)

-- | Typed facts and retained rows share the same original source coordinates.
data CargoIndexResult = CargoIndexResult
    { CargoIndexResult -> PackageInfo
cargoIndexInfo :: PackageInfo
    , CargoIndexResult -> CachedDoc
cargoIndexDocument :: CachedDoc
    }
    deriving stock (CargoIndexResult -> CargoIndexResult -> Bool
(CargoIndexResult -> CargoIndexResult -> Bool)
-> (CargoIndexResult -> CargoIndexResult -> Bool)
-> Eq CargoIndexResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CargoIndexResult -> CargoIndexResult -> Bool
== :: CargoIndexResult -> CargoIndexResult -> Bool
$c/= :: CargoIndexResult -> CargoIndexResult -> Bool
/= :: CargoIndexResult -> CargoIndexResult -> Bool
Eq, Int -> CargoIndexResult -> ShowS
[CargoIndexResult] -> ShowS
CargoIndexResult -> String
(Int -> CargoIndexResult -> ShowS)
-> (CargoIndexResult -> String)
-> ([CargoIndexResult] -> ShowS)
-> Show CargoIndexResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CargoIndexResult -> ShowS
showsPrec :: Int -> CargoIndexResult -> ShowS
$cshow :: CargoIndexResult -> String
show :: CargoIndexResult -> String
$cshowList :: [CargoIndexResult] -> ShowS
showList :: [CargoIndexResult] -> ShowS
Show)

data NativeRow = NativeRow !Text !Text !EntryKey !PackageDetails !NativeRecord

data NativeRecord = PackedRecord !Packed | SelectedRecord !Value

data Scan = Scan
    { Scan -> Int
scanOrdinal :: !Int
    , Scan -> Int
scanCount :: !Int
    , Scan -> Map Text Int
scanSeen :: !(Map Text Int)
    , Scan -> [NativeRow]
scanRows :: ![NativeRow]
    , Scan -> [InvalidEntry]
scanInvalid :: ![InvalidEntry]
    , Scan -> Int
scanKeptCharge :: !Int
    }

-- | Reserve current-row workspace and any retained selected row before buffering or decoding.
readCargoIndexCharged :: (Int -> IO ()) -> CargoDiscovery -> Limits -> PackageName -> CargoRead -> DocumentWalk CargoIndexResult
readCargoIndexCharged :: (Int -> IO ())
-> CargoDiscovery
-> Limits
-> PackageName
-> CargoRead
-> DocumentWalk CargoIndexResult
readCargoIndexCharged Int -> IO ()
pay CargoDiscovery
discovery Limits
limits PackageName
name CargoRead
mode BodyLimit
bound InternTable
_ =
    (forall a. IO a -> IO a)
-> (Steps IO CargoIndexResult -> IO (Steps IO CargoIndexResult))
-> BodyLimit
-> Steps IO CargoIndexResult
-> IO ByteString
-> IO (Either LimitError (StreamResult CargoIndexResult))
forall (n :: * -> *) (m :: * -> *) s.
Monad n =>
(forall a. m a -> n a)
-> (Steps m s -> n (Steps m s))
-> BodyLimit
-> Steps m s
-> n ByteString
-> n (Either LimitError (StreamResult s))
readStepsWithEnd IO a -> IO a
forall a. a -> a
forall a. IO a -> IO a
id Steps IO CargoIndexResult -> IO (Steps IO CargoIndexResult)
forall (m :: * -> *) a. Monad m => Steps m a -> m (Steps m a)
finishRecordSteps BodyLimit
bound ((Int -> IO ())
-> CargoDiscovery
-> Limits
-> PackageName
-> CargoRead
-> Steps IO CargoIndexResult
forall (m :: * -> *).
Monad m =>
(Int -> m ())
-> CargoDiscovery
-> Limits
-> PackageName
-> CargoRead
-> Steps m CargoIndexResult
cargoIndexSteps Int -> IO ()
pay CargoDiscovery
discovery Limits
limits PackageName
name CargoRead
mode)

-- | Frame and project native records while reserving current and retained selected-row workspace.
cargoIndexSteps :: (Monad m) => (Int -> m ()) -> CargoDiscovery -> Limits -> PackageName -> CargoRead -> Steps m CargoIndexResult
cargoIndexSteps :: forall (m :: * -> *).
Monad m =>
(Int -> m ())
-> CargoDiscovery
-> Limits
-> PackageName
-> CargoRead
-> Steps m CargoIndexResult
cargoIndexSteps Int -> m ()
pay CargoDiscovery
discovery Limits
limits PackageName
name CargoRead
mode = (Scan -> Int -> ByteString -> m Int)
-> (Scan -> Int -> ByteString -> Either LimitError Scan)
-> (Scan -> Steps m CargoIndexResult)
-> Scan
-> Steps m CargoIndexResult
forall (m :: * -> *) s a.
Monad m =>
(s -> Int -> ByteString -> m Int)
-> (s -> Int -> ByteString -> Either LimitError s)
-> (s -> Steps m a)
-> s
-> Steps m a
recordSteps Scan -> Int -> ByteString -> m Int
reserve Scan -> Int -> ByteString -> Either LimitError Scan
consume (CargoIndexResult -> Steps m CargoIndexResult
forall (m :: * -> *) s. s -> Steps m s
Finished (CargoIndexResult -> Steps m CargoIndexResult)
-> (Scan -> CargoIndexResult) -> Scan -> Steps m CargoIndexResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scan -> CargoIndexResult
complete) Scan
initial
  where
    initial :: Scan
initial = Int
-> Int
-> Map Text Int
-> [NativeRow]
-> [InvalidEntry]
-> Int
-> Scan
Scan Int
0 Int
0 Map Text Int
forall k a. Map k a
Map.empty [] [] Int
0
    requestedIdentity :: Maybe Text
requestedIdentity = case CargoRead
mode of
        CargoRead
FullRead -> Maybe Text
forall a. Maybe a
Nothing
        SelectedRead Version
version -> CargoKey -> Text
cargoUniquenessKey (CargoKey -> Text) -> Maybe CargoKey -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe CargoKey
parseCargo (Version -> Text
renderVersion Version
version)
    trackIdentity :: Text -> Bool
trackIdentity Text
unique = case CargoRead
mode of
        CargoRead
FullRead -> Bool
True
        SelectedRead Version
_ -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
unique Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe Text
requestedIdentity
    retainAnother :: Scan -> Bool
retainAnother Scan
scan = case CargoRead
mode of
        CargoRead
FullRead -> Bool
True
        SelectedRead Version
_ -> [NativeRow] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Scan -> [NativeRow]
scanRows Scan
scan)
    rememberInvalid :: InvalidEntry -> Scan -> Scan
rememberInvalid InvalidEntry
invalid Scan
scan = case CargoRead
mode of
        CargoRead
FullRead -> Scan
scan{scanInvalid = invalid : scanInvalid scan}
        SelectedRead Version
_ -> Scan
scan
    reserve :: Scan -> Int -> ByteString -> m Int
reserve Scan
scan Int
previous ByteString
fragment = do
        let cost :: Int
cost = Int -> ByteString -> Int
recordFragmentCharge Int
previous ByteString
fragment
        Int -> m ()
pay (Integer -> Int
saturatingCharge (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Scan -> Int
scanKeptCharge Scan
scan) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
cost))
        Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
cost
    consume :: Scan -> Int -> ByteString -> Either LimitError Scan
consume !Scan
scan Int
rowCharge ByteString
bytes
        | (Word8 -> Bool) -> ByteString -> Bool
BS.all Word8 -> Bool
isJsonSpace ByteString
bytes = Scan -> Either LimitError Scan
forall a b. b -> Either a b
Right Scan
scan{scanOrdinal = scanOrdinal scan + 1}
        | Bool
otherwise = do
            let count :: Int
count = Scan -> Int
scanCount Scan
scan Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
                key :: EntryKey
key = Int -> EntryKey
RecordEntry (Scan -> Int
scanOrdinal Scan
scan)
                next :: Scan
next = Scan
scan{scanOrdinal = scanOrdinal scan + 1, scanCount = count}
            Limits -> Int -> Either LimitError ()
checkVersionCountOf Limits
limits Int
count
            case Int -> ByteString -> Either CargoRecordError Value
decodeCargoRecord (Limits -> Int
maxNestingDepth Limits
limits) ByteString
bytes of
                Left (CargoRecordBound LimitError
fault) -> LimitError -> Either LimitError Scan
forall a b. a -> Either a b
Left LimitError
fault
                Left (CargoRecordMalformed Text
reason) -> Scan -> Either LimitError Scan
forall a b. b -> Either a b
Right (EntryKey -> Text -> Scan -> Scan
forall {a}. Show a => a -> Text -> Scan -> Scan
dropRecord EntryKey
key Text
reason Scan
next)
                Right Value
value -> do
                    let nativeIdentity :: Maybe Text
nativeIdentity = Value -> Maybe Text
recordIdentity Value
value
                        seen :: Map Text Int
seen = case Maybe Text
nativeIdentity of
                            Just Text
unique | Text -> Bool
trackIdentity Text
unique -> (Int -> Int -> Int) -> Text -> Int -> Map Text Int -> Map Text Int
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) Text
unique Int
1 (Scan -> Map Text Int
scanSeen Scan
next)
                            Maybe Text
_ -> Scan -> Map Text Int
scanSeen Scan
next
                        observed :: Scan
observed = Scan
next{scanSeen = seen}
                    case PackageName
-> EntryKey
-> CargoDiscovery
-> Value
-> Either InvalidEntry PackageDetails
projectCargoRecord PackageName
name EntryKey
key CargoDiscovery
discovery Value
value of
                        Left InvalidEntry
invalid -> Scan -> Either LimitError Scan
forall a b. b -> Either a b
Right (InvalidEntry -> Scan -> Scan
rememberInvalid InvalidEntry
invalid Scan
observed)
                        Right PackageDetails
details -> do
                            let raw :: Text
raw = Version -> Text
renderVersion (PackageDetails -> Version
pkgVersion PackageDetails
details)
                                selected :: Bool
selected = case CargoRead
mode of CargoRead
FullRead -> Bool
True; SelectedRead Version
version -> Version -> Text
renderVersion Version
version Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
raw
                                rows :: [NativeRow]
rows = case Maybe Text
nativeIdentity of
                                    Just Text
unique
                                        | Bool
selected Bool -> Bool -> Bool
&& Scan -> Bool
retainAnother Scan
observed ->
                                            let retained :: NativeRecord
retained = case CargoRead
mode of CargoRead
FullRead -> Packed -> NativeRecord
PackedRecord ([Text] -> Value -> Packed
packTree [] Value
value); SelectedRead Version
_ -> Value -> NativeRecord
SelectedRecord Value
value
                                                !row :: NativeRow
row = Text
-> Text -> EntryKey -> PackageDetails -> NativeRecord -> NativeRow
NativeRow Text
unique Text
raw EntryKey
key PackageDetails
details NativeRecord
retained
                                             in NativeRow
row NativeRow -> [NativeRow] -> [NativeRow]
forall a. a -> [a] -> [a]
: Scan -> [NativeRow]
scanRows Scan
observed
                                    Maybe Text
_ -> Scan -> [NativeRow]
scanRows Scan
observed
                            Scan -> Either LimitError Scan
forall a. a -> Either LimitError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Scan
observed{scanRows = rows, scanKeptCharge = case mode of SelectedRead Version
_ | [NativeRow] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Scan -> [NativeRow]
scanRows Scan
observed) Bool -> Bool -> Bool
&& Bool -> Bool
not ([NativeRow] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [NativeRow]
rows) -> Int
rowCharge; CargoRead
_ -> Scan -> Int
scanKeptCharge Scan
observed}
    dropRecord :: a -> Text -> Scan -> Scan
dropRecord a
key Text
reason = InvalidEntry -> Scan -> Scan
rememberInvalid (InvalidEntryKind -> Text -> Value -> Text -> InvalidEntry
mkInvalidEntry InvalidEntryKind
InvalidVersionManifest (a -> Text
forall b a. (Show a, IsString b) => a -> b
show a
key) Value
Null Text
reason)
    complete :: Scan -> CargoIndexResult
complete Scan
scan =
        let ([NativeRow]
rows, [InvalidEntry]
duplicates) = (([NativeRow], [InvalidEntry])
 -> NativeRow -> ([NativeRow], [InvalidEntry]))
-> ([NativeRow], [InvalidEntry])
-> [NativeRow]
-> ([NativeRow], [InvalidEntry])
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (Map Text Int
-> ([NativeRow], [InvalidEntry])
-> NativeRow
-> ([NativeRow], [InvalidEntry])
forall {a}.
(Eq a, Num a) =>
Map Text a
-> ([NativeRow], [InvalidEntry])
-> NativeRow
-> ([NativeRow], [InvalidEntry])
finishRow (Scan -> Map Text Int
scanSeen Scan
scan)) ([], []) (Scan -> [NativeRow]
scanRows Scan
scan)
         in CargoIndexResult
                { cargoIndexInfo :: PackageInfo
cargoIndexInfo = PackageName
-> Map Text PackageDetails
-> Map Text Version
-> [InvalidEntry]
-> PackageInfo
PackageInfo PackageName
name ([(Text, PackageDetails)] -> Map Text PackageDetails
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Text
raw, PackageDetails
details) | NativeRow Text
_ Text
raw EntryKey
_ PackageDetails
details NativeRecord
_ <- [NativeRow]
rows]) Map Text Version
forall k a. Map k a
Map.empty ([InvalidEntry] -> [InvalidEntry]
forall (t :: * -> *) a. Foldable t => t a -> t a
strictElements ([InvalidEntry] -> [InvalidEntry]
forall a. [a] -> [a]
reverse (Scan -> [InvalidEntry]
scanInvalid Scan
scan) [InvalidEntry] -> [InvalidEntry] -> [InvalidEntry]
forall a. Semigroup a => a -> a -> a
<> [InvalidEntry]
duplicates))
                , cargoIndexDocument :: CachedDoc
cargoIndexDocument = case CargoRead
mode of
                    CargoRead
FullRead -> (PackedIndex -> CachedDoc, CachedDoc -> Maybe PackedIndex)
-> PackedIndex -> CachedDoc
forall a b. (a, b) -> a
fst (PackedIndex -> CachedDoc, CachedDoc -> Maybe PackedIndex)
cargoPacked (DocTable -> [(EntryKey, Packed)] -> PackedIndex
PackedIndex (SmallArray Text -> DocTable
docTable ([Text] -> SmallArray Text
forall a. [a] -> SmallArray a
smallArrayFromList [])) ([(EntryKey, Packed)] -> [(EntryKey, Packed)]
forall (t :: * -> *) a. Foldable t => t a -> t a
strictElements [(EntryKey
key, Packed
packed) | NativeRow Text
_ Text
_ EntryKey
key PackageDetails
_ (PackedRecord Packed
packed) <- [NativeRow]
rows]))
                    SelectedRead Version
_ -> (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
-> CargoDocument -> CachedDoc
forall a b. (a, b) -> a
fst (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
cargoCached ([(EntryKey, Value)] -> CargoDocument
cargoDocument [(EntryKey
key, Value
value) | NativeRow Text
_ Text
_ EntryKey
key PackageDetails
_ (SelectedRecord Value
value) <- [NativeRow]
rows])
                }
    finishRow :: Map Text a
-> ([NativeRow], [InvalidEntry])
-> NativeRow
-> ([NativeRow], [InvalidEntry])
finishRow Map Text a
seen ([NativeRow]
rows, [InvalidEntry]
invalid) row :: NativeRow
row@(NativeRow Text
unique Text
_ EntryKey
key PackageDetails
_ NativeRecord
_)
        | Text -> Map Text a -> Maybe a
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
unique Map Text a
seen Maybe a -> Maybe a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> Maybe a
forall a. a -> Maybe a
Just a
1 = (NativeRow
row NativeRow -> [NativeRow] -> [NativeRow]
forall a. a -> [a] -> [a]
: [NativeRow]
rows, [InvalidEntry]
invalid)
        | Bool
otherwise = ([NativeRow]
rows, InvalidEntryKind -> Text -> Value -> Text -> InvalidEntry
mkInvalidEntry InvalidEntryKind
InvalidVersionManifest (EntryKey -> Text
forall b a. (Show a, IsString b) => a -> b
show EntryKey
key) Value
Null Text
"duplicate Cargo release identity" InvalidEntry -> [InvalidEntry] -> [InvalidEntry]
forall a. a -> [a] -> [a]
: [InvalidEntry]
invalid)