-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT
{-# LANGUAGE TypeFamilies #-}

-- | Conditional S3 metadata exchanges bound response bytes before SDK decoding.
module Ecluse.Runtime.Mirror.State.S3.Internal (
    ObjectRead (..),
    ObjectWrite (..),
    ObjectFault (..),
    readObject,
    writeObject,
) where

import Amazonka qualified as AWS
import Amazonka.Env.Hooks qualified as Hooks
import Amazonka.S3 qualified as S3
import Amazonka.S3.Lens qualified as S3L
import Amazonka.Types qualified as Types
import Conduit (ConduitT, MonadResource, ResourceT, await, runResourceT, sealConduitT, yield, ($$++), (.|))
import Data.ByteString qualified as BS
import Data.Conduit.Combinators qualified as C
import Lens.Micro ((%~), (^.))
import Network.HTTP.Client qualified as HTTP
import Network.HTTP.Types.Status (statusCode)
import UnliftIO (withRunInIO)
import UnliftIO.Exception (catch, throwIO)

import Ecluse.Core.Deadline (Deadlines)
import Ecluse.Core.Fault (TransportCause (TransportTimeout), TransportFault, transportFault)
import Ecluse.Core.Mirror.State.Codec (stateExchangeCharge)
import Ecluse.Core.Mirror.State.Internal (StateCharge)
import Ecluse.Core.Registry.Exchange (accumulatingChargeFrom, chargedRead)
import Ecluse.Core.Registry.Progress (Watch, meteredReader, meteredUpload, watched)
import Ecluse.Core.Security (BodyLimit (MetadataBodyLimit), Limits (..), boundedRead)
import Ecluse.Core.Server.MemoryModel (bufferedChunkCharge, decodedDocumentCharge, saturatingCharge)
import Ecluse.Core.Stream (boundBytes)
import Ecluse.Runtime.Aws.Env (singleAttemptEnv)
import Ecluse.Runtime.Aws.Fault (classifyAwsTransport, foldTransportEscapes, serviceStatus)
import Ecluse.Runtime.Aws.S3.Conditional (ConditionalPut (..), WritePrecondition (..), absenceListing, isWriteConflict, listingProvesAbsence)

-- | A 304 carries only the exact revision supplied to this exchange.
data ObjectRead = ObjectAbsent | ObjectUnchanged ByteString | ObjectLoaded ByteString ByteString
    deriving stock (ObjectRead -> ObjectRead -> Bool
(ObjectRead -> ObjectRead -> Bool)
-> (ObjectRead -> ObjectRead -> Bool) -> Eq ObjectRead
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ObjectRead -> ObjectRead -> Bool
== :: ObjectRead -> ObjectRead -> Bool
$c/= :: ObjectRead -> ObjectRead -> Bool
/= :: ObjectRead -> ObjectRead -> Bool
Eq, Int -> ObjectRead -> ShowS
[ObjectRead] -> ShowS
ObjectRead -> String
(Int -> ObjectRead -> ShowS)
-> (ObjectRead -> String)
-> ([ObjectRead] -> ShowS)
-> Show ObjectRead
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ObjectRead -> ShowS
showsPrec :: Int -> ObjectRead -> ShowS
$cshow :: ObjectRead -> String
show :: ObjectRead -> String
$cshowList :: [ObjectRead] -> ShowS
showList :: [ObjectRead] -> ShowS
Show)

-- | Conditional writes never overwrite a revision they did not observe.
data ObjectWrite = ObjectStored ByteString | ObjectConflict
    deriving stock (ObjectWrite -> ObjectWrite -> Bool
(ObjectWrite -> ObjectWrite -> Bool)
-> (ObjectWrite -> ObjectWrite -> Bool) -> Eq ObjectWrite
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ObjectWrite -> ObjectWrite -> Bool
== :: ObjectWrite -> ObjectWrite -> Bool
$c/= :: ObjectWrite -> ObjectWrite -> Bool
/= :: ObjectWrite -> ObjectWrite -> Bool
Eq, Int -> ObjectWrite -> ShowS
[ObjectWrite] -> ShowS
ObjectWrite -> String
(Int -> ObjectWrite -> ShowS)
-> (ObjectWrite -> String)
-> ([ObjectWrite] -> ShowS)
-> Show ObjectWrite
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ObjectWrite -> ShowS
showsPrec :: Int -> ObjectWrite -> ShowS
$cshow :: ObjectWrite -> String
show :: ObjectWrite -> String
$cshowList :: [ObjectWrite] -> ShowS
showList :: [ObjectWrite] -> ShowS
Show)

-- | Missing revision evidence and ambiguous absence remain unavailable.
data ObjectFault
    = ObjectTransport TransportFault
    | ObjectTooLarge Int
    | ObjectNoRevision
    | ObjectUnknownAbsence
    deriving stock (ObjectFault -> ObjectFault -> Bool
(ObjectFault -> ObjectFault -> Bool)
-> (ObjectFault -> ObjectFault -> Bool) -> Eq ObjectFault
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ObjectFault -> ObjectFault -> Bool
== :: ObjectFault -> ObjectFault -> Bool
$c/= :: ObjectFault -> ObjectFault -> Bool
/= :: ObjectFault -> ObjectFault -> Bool
Eq, Int -> ObjectFault -> ShowS
[ObjectFault] -> ShowS
ObjectFault -> String
(Int -> ObjectFault -> ShowS)
-> (ObjectFault -> String)
-> ([ObjectFault] -> ShowS)
-> Show ObjectFault
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ObjectFault -> ShowS
showsPrec :: Int -> ObjectFault -> ShowS
$cshow :: ObjectFault -> String
show :: ObjectFault -> String
$cshowList :: [ObjectFault] -> ShowS
showList :: [ObjectFault] -> ShowS
Show)

-- These wrappers keep generated request construction and capture the raw response, so every body
-- passes the wire ceiling. The associated response types need TypeFamilies, as amazonka's class does.

newtype BoundedPut = BoundedPut ConditionalPut

data BoundedGet = BoundedGet (Maybe ByteString) S3.GetObject

-- The absence fallback limits XML before the generated decoder buffers it.
newtype BoundedList = BoundedList S3.ListObjectsV2

instance AWS.AWSRequest BoundedPut where
    type AWSResponse BoundedPut = RawResponse
    request :: (Service -> Service) -> BoundedPut -> Request BoundedPut
request Service -> Service
overrides (BoundedPut ConditionalPut
conditional) = Request ConditionalPut -> Request BoundedPut
forall a b. Coercible a b => a -> b
coerce ((Service -> Service) -> ConditionalPut -> Request ConditionalPut
forall a. AWSRequest a => (Service -> Service) -> a -> Request a
AWS.request Service -> Service
overrides ConditionalPut
conditional)
    response :: forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy BoundedPut
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse BoundedPut)))
response = (ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy BoundedPut
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse BoundedPut)))
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy BoundedPut
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse RawResponse))
forall {k} (m :: * -> *) (a :: k).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse RawResponse))
captureResponse

instance AWS.AWSRequest BoundedGet where
    type AWSResponse BoundedGet = RawResponse
    request :: (Service -> Service) -> BoundedGet -> Request BoundedGet
request Service -> Service
overrides (BoundedGet Maybe ByteString
revision GetObject
native) = Request GetObject -> Request BoundedGet
forall a b. Coercible a b => a -> b
coerce ((Service -> Service) -> GetObject -> Request GetObject
forall a. AWSRequest a => (Service -> Service) -> a -> Request a
AWS.request Service -> Service
overrides GetObject
native Request GetObject
-> (Request GetObject -> Request GetObject) -> Request GetObject
forall a b. a -> (a -> b) -> b
& ([Header] -> Identity [Header])
-> Request GetObject -> Identity (Request GetObject)
forall a (f :: * -> *).
Functor f =>
([Header] -> f [Header]) -> Request a -> f (Request a)
Types.request_headers (([Header] -> Identity [Header])
 -> Request GetObject -> Identity (Request GetObject))
-> ([Header] -> [Header]) -> Request GetObject -> Request GetObject
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (\[Header]
headers -> [Header]
-> (ByteString -> [Header]) -> Maybe ByteString -> [Header]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Header]
headers (\ByteString
etag -> (HeaderName
"If-None-Match", ByteString
etag) Header -> [Header] -> [Header]
forall a. a -> [a] -> [a]
: [Header]
headers) Maybe ByteString
revision))
    response :: forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy BoundedGet
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse BoundedGet)))
response = (ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy BoundedGet
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse BoundedGet)))
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy BoundedGet
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse RawResponse))
forall {k} (m :: * -> *) (a :: k).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse RawResponse))
captureResponse

instance AWS.AWSRequest BoundedList where
    type AWSResponse BoundedList = RawResponse
    request :: (Service -> Service) -> BoundedList -> Request BoundedList
request Service -> Service
overrides (BoundedList ListObjectsV2
native) = Request ListObjectsV2 -> Request BoundedList
forall a b. Coercible a b => a -> b
coerce ((Service -> Service) -> ListObjectsV2 -> Request ListObjectsV2
forall a. AWSRequest a => (Service -> Service) -> a -> Request a
AWS.request Service -> Service
overrides ListObjectsV2
native)
    response :: forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy BoundedList
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse BoundedList)))
response = (ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy BoundedList
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse BoundedList)))
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy BoundedList
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse RawResponse))
forall {k} (m :: * -> *) (a :: k).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse RawResponse))
captureResponse

data RawResponse = RawResponse AWS.Service (Types.ClientResponse Types.ClientBody)

captureResponse :: (MonadResource m) => (LByteString -> IO LByteString) -> AWS.Service -> Proxy a -> Types.ClientResponse Types.ClientBody -> m (Either AWS.Error (Types.ClientResponse RawResponse))
captureResponse :: forall {k} (m :: * -> *) (a :: k).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse RawResponse))
captureResponse ByteStringLazy -> IO ByteStringLazy
_ Service
service Proxy a
_ ClientResponse ClientBody
response = Either Error (ClientResponse RawResponse)
-> m (Either Error (ClientResponse RawResponse))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ClientResponse RawResponse
-> Either Error (ClientResponse RawResponse)
forall a b. b -> Either a b
Right (Service -> ClientResponse ClientBody -> RawResponse
RawResponse Service
service ClientResponse ClientBody
response RawResponse
-> ClientResponse ClientBody -> ClientResponse RawResponse
forall a b. a -> Response b -> Response a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ClientResponse ClientBody
response))

newtype ObjectLimit = ObjectLimit Int
    deriving stock (Int -> ObjectLimit -> ShowS
[ObjectLimit] -> ShowS
ObjectLimit -> String
(Int -> ObjectLimit -> ShowS)
-> (ObjectLimit -> String)
-> ([ObjectLimit] -> ShowS)
-> Show ObjectLimit
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ObjectLimit -> ShowS
showsPrec :: Int -> ObjectLimit -> ShowS
$cshow :: ObjectLimit -> String
show :: ObjectLimit -> String
$cshowList :: [ObjectLimit] -> ShowS
showList :: [ObjectLimit] -> ShowS
Show)

instance Exception ObjectLimit

sendBounded :: (AWS.AWSRequest request, AWS.AWSResponse request ~ RawResponse, AWS.AWSRequest native) => StateCharge -> Int -> Bool -> Deadlines -> AWS.Env -> Limits -> request -> Proxy native -> ResourceT IO (Either AWS.Error (AWS.AWSResponse native))
sendBounded :: forall request native.
(AWSRequest request, AWSResponse request ~ RawResponse,
 AWSRequest native) =>
StateCharge
-> Int
-> Bool
-> Deadlines
-> Env
-> Limits
-> request
-> Proxy native
-> ResourceT IO (Either Error (AWSResponse native))
sendBounded StateCharge
charge Int
requestCharge Bool
consumesBody Deadlines
deadlines Env
env Limits
limits request
request Proxy native
native = do
    IO () -> ResourceT IO ()
forall a. IO a -> ResourceT IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ResourceT IO ()) -> IO () -> ResourceT IO ()
forall a b. (a -> b) -> a -> b
$ StateCharge
charge (Integer -> Int
saturatingCharge (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
stateExchangeCharge Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
requestCharge))
    received <- ((forall a. ResourceT IO a -> IO a)
 -> IO
      (Maybe
         (Either
            Error (Int, ByteString, Service, ClientResponse ClientBody))))
-> ResourceT
     IO
     (Maybe
        (Either
           Error (Int, ByteString, Service, ClientResponse ClientBody)))
forall b.
((forall a. ResourceT IO a -> IO a) -> IO b) -> ResourceT IO b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. ResourceT IO a -> IO a)
  -> IO
       (Maybe
          (Either
             Error (Int, ByteString, Service, ClientResponse ClientBody))))
 -> ResourceT
      IO
      (Maybe
         (Either
            Error (Int, ByteString, Service, ClientResponse ClientBody))))
-> ((forall a. ResourceT IO a -> IO a)
    -> IO
         (Maybe
            (Either
               Error (Int, ByteString, Service, ClientResponse ClientBody))))
-> ResourceT
     IO
     (Maybe
        (Either
           Error (Int, ByteString, Service, ClientResponse ClientBody)))
forall a b. (a -> b) -> a -> b
$ \forall a. ResourceT IO a -> IO a
run -> Deadlines
-> ProgressFloor
-> (Watch
    -> IO
         (Either
            Error (Int, ByteString, Service, ClientResponse ClientBody)))
-> IO
     (Maybe
        (Either
           Error (Int, ByteString, Service, ClientResponse ClientBody)))
forall a.
Deadlines -> ProgressFloor -> (Watch -> IO a) -> IO (Maybe a)
watched Deadlines
deadlines (Limits -> ProgressFloor
progressFloor Limits
limits) ((Watch
  -> IO
       (Either
          Error (Int, ByteString, Service, ClientResponse ClientBody)))
 -> IO
      (Maybe
         (Either
            Error (Int, ByteString, Service, ClientResponse ClientBody))))
-> (Watch
    -> IO
         (Either
            Error (Int, ByteString, Service, ClientResponse ClientBody)))
-> IO
     (Maybe
        (Either
           Error (Int, ByteString, Service, ClientResponse ClientBody)))
forall a b. (a -> b) -> a -> b
$ \Watch
watch -> ResourceT
  IO
  (Either
     Error (Int, ByteString, Service, ClientResponse ClientBody))
-> IO
     (Either
        Error (Int, ByteString, Service, ClientResponse ClientBody))
forall a. ResourceT IO a -> IO a
run (ResourceT
   IO
   (Either
      Error (Int, ByteString, Service, ClientResponse ClientBody))
 -> IO
      (Either
         Error (Int, ByteString, Service, ClientResponse ClientBody)))
-> ResourceT
     IO
     (Either
        Error (Int, ByteString, Service, ClientResponse ClientBody))
-> IO
     (Either
        Error (Int, ByteString, Service, ClientResponse ClientBody))
forall a b. (a -> b) -> a -> b
$ do
        let metered :: Env
metered =
                Env
env
                    { AWS.hooks =
                        Hooks.clientRequestHook
                            ( \Hook ClientRequest
previous Env' withAuth
current ClientRequest
outgoing -> do
                                requestWithHooks <- Env' withAuth -> ClientRequest -> IO ClientRequest
Hook ClientRequest
previous Env' withAuth
current ClientRequest
outgoing
                                pure requestWithHooks{HTTP.requestBody = meteredUpload watch (HTTP.requestBody requestWithHooks)}
                            )
                            (AWS.hooks env)
                    }
        Env -> request -> ResourceT IO (Either Error (AWSResponse request))
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (Either Error (AWSResponse a))
AWS.sendEither (Env -> Env
singleAttemptEnv Env
metered) request
request ResourceT IO (Either Error RawResponse)
-> (Either Error RawResponse
    -> ResourceT
         IO
         (Either
            Error (Int, ByteString, Service, ClientResponse ClientBody)))
-> ResourceT
     IO
     (Either
        Error (Int, ByteString, Service, ClientResponse ClientBody))
forall a b.
ResourceT IO a -> (a -> ResourceT IO b) -> ResourceT IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Left Error
err -> Either Error (Int, ByteString, Service, ClientResponse ClientBody)
-> ResourceT
     IO
     (Either
        Error (Int, ByteString, Service, ClientResponse ClientBody))
forall a. a -> ResourceT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error
-> Either
     Error (Int, ByteString, Service, ClientResponse ClientBody)
forall a b. a -> Either a b
Left Error
err)
            Right (RawResponse Service
service ClientResponse ClientBody
response) -> do
                (wireCharge, bytes) <- StateCharge
-> Int
-> Watch
-> Limits
-> ClientBody
-> ResourceT IO (Int, ByteString)
readSource StateCharge
charge Int
requestCharge Watch
watch Limits
limits (ClientResponse ClientBody -> ClientBody
forall body. Response body -> body
HTTP.responseBody ClientResponse ClientBody
response)
                pure (Right (wireCharge, bytes, service, fmap (const (yield bytes)) response))
    case received of
        Maybe
  (Either
     Error (Int, ByteString, Service, ClientResponse ClientBody))
Nothing -> ObjectProgress -> ResourceT IO (Either Error (AWSResponse native))
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO ObjectProgress
ObjectProgress
        Just (Left Error
err) -> Either Error (AWSResponse native)
-> ResourceT IO (Either Error (AWSResponse native))
forall a. a -> ResourceT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error (AWSResponse native)
forall a b. a -> Either a b
Left Error
err)
        Just (Right (Int
wireCharge, ByteString
bytes, Service
service, ClientResponse ClientBody
response)) -> do
            let decodeCharge :: Int
decodeCharge = if Bool
consumesBody Bool -> Bool -> Bool
|| Status -> Int
statusCode (ClientResponse ClientBody -> Status
forall body. Response body -> Status
HTTP.responseStatus ClientResponse ClientBody
response) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
300 then ByteString -> Int
decodedDocumentCharge ByteString
bytes else Int
0
            IO () -> ResourceT IO ()
forall a. IO a -> ResourceT IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ResourceT IO ()) -> IO () -> ResourceT IO ()
forall a b. (a -> b) -> a -> b
$ StateCharge
charge (Integer -> Int
saturatingCharge (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
wireCharge Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
decodeCharge))
            (Response (AWSResponse native) -> AWSResponse native)
-> Either Error (Response (AWSResponse native))
-> Either Error (AWSResponse native)
forall a b. (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Response (AWSResponse native) -> AWSResponse native
forall body. Response body -> body
HTTP.responseBody (Either Error (Response (AWSResponse native))
 -> Either Error (AWSResponse native))
-> ResourceT IO (Either Error (Response (AWSResponse native)))
-> ResourceT IO (Either Error (AWSResponse native))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy native
-> ClientResponse ClientBody
-> ResourceT IO (Either Error (Response (AWSResponse native)))
forall a (m :: * -> *).
(AWSRequest a, MonadResource m) =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy native
-> ClientResponse ClientBody
-> m (Either Error (Response (AWSResponse native)))
AWS.response ByteStringLazy -> IO ByteStringLazy
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Service
service Proxy native
native ClientResponse ClientBody
response

readSource :: StateCharge -> Int -> Watch -> Limits -> Types.ClientBody -> ResourceT IO (Int, ByteString)
readSource :: StateCharge
-> Int
-> Watch
-> Limits
-> ClientBody
-> ResourceT IO (Int, ByteString)
readSource StateCharge
charge Int
requestCharge Watch
watch Limits
limits ClientBody
source = ((forall a. ResourceT IO a -> IO a) -> IO (Int, ByteString))
-> ResourceT IO (Int, ByteString)
forall b.
((forall a. ResourceT IO a -> IO a) -> IO b) -> ResourceT IO b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. ResourceT IO a -> IO a) -> IO (Int, ByteString))
 -> ResourceT IO (Int, ByteString))
-> ((forall a. ResourceT IO a -> IO a) -> IO (Int, ByteString))
-> ResourceT IO (Int, ByteString)
forall a b. (a -> b) -> a -> b
$ \forall a. ResourceT IO a -> IO a
run -> do
    (addCharge, wireCharge) <- Integer -> StateCharge -> IO (StateCharge, IO Int)
accumulatingChargeFrom (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
stateExchangeCharge Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
requestCharge) StateCharge
charge
    let payChunk = StateCharge
addCharge StateCharge -> (Int -> Int) -> StateCharge
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int
bufferedChunkCharge
    pending <- newIORef (sealConduitT source)
    let next = do
            current <- IORef (SealedConduitT () ByteString (ResourceT IO) ())
-> IO (SealedConduitT () ByteString (ResourceT IO) ())
forall (m :: * -> *) a. MonadIO m => IORef a -> m a
readIORef IORef (SealedConduitT () ByteString (ResourceT IO) ())
pending
            (rest, chunk) <- run (current $$++ await)
            writeIORef pending rest
            case chunk of
                Maybe ByteString
Nothing -> ByteString -> IO ByteString
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
BS.empty
                Just ByteString
bytes
                    | ByteString -> Bool
BS.null ByteString
bytes -> IO ByteString
next
                    | Bool
otherwise -> ByteString -> IO ByteString
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
bytes
    boundedRead (MetadataBodyLimit (maxMetadataBytes limits)) (chargedRead payChunk (meteredReader watch next)) >>= \case
        Left LimitError
_ -> ObjectLimit -> IO (Int, ByteString)
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO (Int -> ObjectLimit
ObjectLimit (Limits -> Int
maxMetadataBytes Limits
limits))
        Right (Int
_, ByteString
bytes) -> (,ByteString
bytes) (Int -> (Int, ByteString)) -> IO Int -> IO (Int, ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Int
wireCharge

data ObjectProgress = ObjectProgress
    deriving stock (Int -> ObjectProgress -> ShowS
[ObjectProgress] -> ShowS
ObjectProgress -> String
(Int -> ObjectProgress -> ShowS)
-> (ObjectProgress -> String)
-> ([ObjectProgress] -> ShowS)
-> Show ObjectProgress
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ObjectProgress -> ShowS
showsPrec :: Int -> ObjectProgress -> ShowS
$cshow :: ObjectProgress -> String
show :: ObjectProgress -> String
$cshowList :: [ObjectProgress] -> ShowS
showList :: [ObjectProgress] -> ShowS
Show)

instance Exception ObjectProgress

byteCap :: (MonadIO m) => Int -> ConduitT ByteString ByteString m ()
byteCap :: forall (m :: * -> *).
MonadIO m =>
Int -> ConduitT ByteString ByteString m ()
byteCap Int
cap = Int -> (Int -> m ()) -> ConduitT ByteString ByteString m ()
forall (m :: * -> *).
Monad m =>
Int -> (Int -> m ()) -> ConduitT ByteString ByteString m ()
boundBytes Int
cap (m () -> Int -> m ()
forall a b. a -> b -> a
const (ObjectLimit -> m ()
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO (Int -> ObjectLimit
ObjectLimit Int
cap)))

-- | GET returns bytes and their revision in the same resource lifetime.
readObject :: StateCharge -> Deadlines -> AWS.Env -> Limits -> Text -> Text -> Maybe ByteString -> IO (Either ObjectFault ObjectRead)
readObject :: StateCharge
-> Deadlines
-> Env
-> Limits
-> Text
-> Text
-> Maybe ByteString
-> IO (Either ObjectFault ObjectRead)
readObject StateCharge
charge Deadlines
deadlines Env
env Limits
limits Text
bucket Text
key Maybe ByteString
revision = IO (Either ObjectFault ObjectRead)
-> IO (Either ObjectFault ObjectRead)
forall a. IO (Either ObjectFault a) -> IO (Either ObjectFault a)
boundedCall (IO (Either ObjectFault ObjectRead)
 -> IO (Either ObjectFault ObjectRead))
-> IO (Either ObjectFault ObjectRead)
-> IO (Either ObjectFault ObjectRead)
forall a b. (a -> b) -> a -> b
$ do
    result <- ResourceT IO (Either Error (Either ObjectFault ObjectRead))
-> IO (Either Error (Either ObjectFault ObjectRead))
forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
runResourceT (ResourceT IO (Either Error (Either ObjectFault ObjectRead))
 -> IO (Either Error (Either ObjectFault ObjectRead)))
-> ResourceT IO (Either Error (Either ObjectFault ObjectRead))
-> IO (Either Error (Either ObjectFault ObjectRead))
forall a b. (a -> b) -> a -> b
$ do
        response <- StateCharge
-> Int
-> Bool
-> Deadlines
-> Env
-> Limits
-> BoundedGet
-> Proxy GetObject
-> ResourceT IO (Either Error (AWSResponse GetObject))
forall request native.
(AWSRequest request, AWSResponse request ~ RawResponse,
 AWSRequest native) =>
StateCharge
-> Int
-> Bool
-> Deadlines
-> Env
-> Limits
-> request
-> Proxy native
-> ResourceT IO (Either Error (AWSResponse native))
sendBounded StateCharge
charge Int
0 Bool
False Deadlines
deadlines Env
env Limits
limits (Maybe ByteString -> GetObject -> BoundedGet
BoundedGet Maybe ByteString
revision GetObject
request) (Proxy GetObject
forall {k} (t :: k). Proxy t
Proxy :: Proxy S3.GetObject)
        case response of
            Left Error
err -> Either Error (Either ObjectFault ObjectRead)
-> ResourceT IO (Either Error (Either ObjectFault ObjectRead))
forall a. a -> ResourceT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error (Either ObjectFault ObjectRead)
forall a b. a -> Either a b
Left Error
err)
            Right GetObjectResponse
received | GetObjectResponse
received GetObjectResponse -> Getting Int GetObjectResponse Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int GetObjectResponse Int
Lens' GetObjectResponse Int
S3L.getObjectResponse_httpStatus Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
304 -> Either Error (Either ObjectFault ObjectRead)
-> ResourceT IO (Either Error (Either ObjectFault ObjectRead))
forall a. a -> ResourceT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ObjectFault ObjectRead
-> Either Error (Either ObjectFault ObjectRead)
forall a b. b -> Either a b
Right Either ObjectFault ObjectRead
unchanged)
            Right GetObjectResponse
received -> do
                Maybe Integer -> (Integer -> ResourceT IO ()) -> ResourceT IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (GetObjectResponse
received GetObjectResponse
-> Getting (Maybe Integer) GetObjectResponse (Maybe Integer)
-> Maybe Integer
forall s a. s -> Getting a s a -> a
^. Getting (Maybe Integer) GetObjectResponse (Maybe Integer)
Lens' GetObjectResponse (Maybe Integer)
S3L.getObjectResponse_contentLength) ((Integer -> ResourceT IO ()) -> ResourceT IO ())
-> (Integer -> ResourceT IO ()) -> ResourceT IO ()
forall a b. (a -> b) -> a -> b
$ \Integer
size ->
                    Bool -> ResourceT IO () -> ResourceT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
size Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
cap) (ObjectLimit -> ResourceT IO ()
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO (Int -> ObjectLimit
ObjectLimit Int
cap))
                chunks <- ResponseBody
-> ConduitM ByteString Void (ResourceT IO) [ByteString]
-> ResourceT IO [ByteString]
forall (m :: * -> *) a.
MonadIO m =>
ResponseBody -> ConduitM ByteString Void (ResourceT IO) a -> m a
AWS.sinkBody (GetObjectResponse
received GetObjectResponse
-> Getting ResponseBody GetObjectResponse ResponseBody
-> ResponseBody
forall s a. s -> Getting a s a -> a
^. Getting ResponseBody GetObjectResponse ResponseBody
Lens' GetObjectResponse ResponseBody
S3L.getObjectResponse_body) (Int -> ConduitT ByteString ByteString (ResourceT IO) ()
forall (m :: * -> *).
MonadIO m =>
Int -> ConduitT ByteString ByteString m ()
byteCap Int
cap ConduitT ByteString ByteString (ResourceT IO) ()
-> ConduitM ByteString Void (ResourceT IO) [ByteString]
-> ConduitM ByteString Void (ResourceT IO) [ByteString]
forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| ConduitM ByteString Void (ResourceT IO) [ByteString]
forall (m :: * -> *) a o. Monad m => ConduitT a o m [a]
C.sinkList)
                pure (Right (ObjectLoaded <$> objectRevision (received ^. S3L.getObjectResponse_eTag) <*> pure (BS.concat chunks)))
    case result of
        Right Either ObjectFault ObjectRead
loaded -> Either ObjectFault ObjectRead -> IO (Either ObjectFault ObjectRead)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Either ObjectFault ObjectRead
loaded
        Left Error
err
            | Error -> Maybe Int
serviceStatus Error
err Maybe Int -> Maybe Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Maybe Int
forall a. a -> Maybe a
Just Int
304 -> Either ObjectFault ObjectRead -> IO (Either ObjectFault ObjectRead)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Either ObjectFault ObjectRead
unchanged
            | Error -> Maybe Int
serviceStatus Error
err Maybe Int -> Maybe Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Maybe Int
forall a. a -> Maybe a
Just Int
404 Bool -> Bool -> Bool
&& Error -> Maybe Text
serviceCode Error
err Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"NoSuchKey" -> Either ObjectFault ObjectRead -> IO (Either ObjectFault ObjectRead)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ObjectRead -> Either ObjectFault ObjectRead
forall a b. b -> Either a b
Right ObjectRead
ObjectAbsent)
            | Error -> Maybe Int
serviceStatus Error
err Maybe Int -> Maybe Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Maybe Int
forall a. a -> Maybe a
Just Int
403 -> StateCharge
-> Deadlines
-> Env
-> Limits
-> Text
-> Text
-> IO (Either ObjectFault ObjectRead)
absenceFallback StateCharge
charge Deadlines
deadlines Env
env Limits
limits Text
bucket Text
key
            | Bool
otherwise -> Either ObjectFault ObjectRead -> IO (Either ObjectFault ObjectRead)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ObjectFault -> Either ObjectFault ObjectRead
forall a b. a -> Either a b
Left (TransportFault -> ObjectFault
ObjectTransport (Error -> TransportFault
classifyAwsTransport Error
err)))
  where
    cap :: Int
cap = Limits -> Int
maxMetadataBytes Limits
limits
    unchanged :: Either ObjectFault ObjectRead
unchanged = Either ObjectFault ObjectRead
-> (ByteString -> Either ObjectFault ObjectRead)
-> Maybe ByteString
-> Either ObjectFault ObjectRead
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ObjectFault -> Either ObjectFault ObjectRead
forall a b. a -> Either a b
Left ObjectFault
ObjectNoRevision) (ObjectRead -> Either ObjectFault ObjectRead
forall a b. b -> Either a b
Right (ObjectRead -> Either ObjectFault ObjectRead)
-> (ByteString -> ObjectRead)
-> ByteString
-> Either ObjectFault ObjectRead
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ObjectRead
ObjectUnchanged) Maybe ByteString
revision
    request :: GetObject
request = BucketName -> ObjectKey -> GetObject
S3.newGetObject (Text -> BucketName
S3.BucketName Text
bucket) (Text -> ObjectKey
S3.ObjectKey Text
key)

-- | A failed or uncertain write returns to the caller for reread and union before another attempt.
writeObject :: StateCharge -> Deadlines -> AWS.Env -> Limits -> Text -> Text -> Maybe ByteString -> ByteString -> IO (Either ObjectFault ObjectWrite)
writeObject :: StateCharge
-> Deadlines
-> Env
-> Limits
-> Text
-> Text
-> Maybe ByteString
-> ByteString
-> IO (Either ObjectFault ObjectWrite)
writeObject StateCharge
charge Deadlines
deadlines Env
env Limits
limits Text
bucket Text
key Maybe ByteString
revision ByteString
bytes
    | ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Limits -> Int
maxMetadataBytes Limits
limits = Either ObjectFault ObjectWrite
-> IO (Either ObjectFault ObjectWrite)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ObjectFault -> Either ObjectFault ObjectWrite
forall a b. a -> Either a b
Left (Int -> ObjectFault
ObjectTooLarge (Limits -> Int
maxMetadataBytes Limits
limits)))
    | Bool
otherwise = IO (Either ObjectFault ObjectWrite)
-> IO (Either ObjectFault ObjectWrite)
forall a. IO (Either ObjectFault a) -> IO (Either ObjectFault a)
boundedCall (IO (Either ObjectFault ObjectWrite)
 -> IO (Either ObjectFault ObjectWrite))
-> IO (Either ObjectFault ObjectWrite)
-> IO (Either ObjectFault ObjectWrite)
forall a b. (a -> b) -> a -> b
$ do
        result <- ResourceT IO (Either Error PutObjectResponse)
-> IO (Either Error PutObjectResponse)
forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
runResourceT (StateCharge
-> Int
-> Bool
-> Deadlines
-> Env
-> Limits
-> BoundedPut
-> Proxy PutObject
-> ResourceT IO (Either Error (AWSResponse PutObject))
forall request native.
(AWSRequest request, AWSResponse request ~ RawResponse,
 AWSRequest native) =>
StateCharge
-> Int
-> Bool
-> Deadlines
-> Env
-> Limits
-> request
-> Proxy native
-> ResourceT IO (Either Error (AWSResponse native))
sendBounded StateCharge
charge (Int -> Int
bufferedChunkCharge (ByteString -> Int
BS.length ByteString
bytes)) Bool
True Deadlines
deadlines Env
env Limits
limits BoundedPut
request (Proxy PutObject
forall {k} (t :: k). Proxy t
Proxy :: Proxy S3.PutObject))
        pure $ case result of
            Right PutObjectResponse
response -> ByteString -> ObjectWrite
ObjectStored (ByteString -> ObjectWrite)
-> Either ObjectFault ByteString -> Either ObjectFault ObjectWrite
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe ETag -> Either ObjectFault ByteString
objectRevision (PutObjectResponse
response PutObjectResponse
-> Getting (Maybe ETag) PutObjectResponse (Maybe ETag)
-> Maybe ETag
forall s a. s -> Getting a s a -> a
^. Getting (Maybe ETag) PutObjectResponse (Maybe ETag)
Lens' PutObjectResponse (Maybe ETag)
S3L.putObjectResponse_eTag)
            Left Error
err
                | Error -> Bool
isWriteConflict Error
err -> ObjectWrite -> Either ObjectFault ObjectWrite
forall a b. b -> Either a b
Right ObjectWrite
ObjectConflict
                | Bool
otherwise -> ObjectFault -> Either ObjectFault ObjectWrite
forall a b. a -> Either a b
Left (TransportFault -> ObjectFault
ObjectTransport (Error -> TransportFault
classifyAwsTransport Error
err))
  where
    precondition :: WritePrecondition
precondition = WritePrecondition
-> (ByteString -> WritePrecondition)
-> Maybe ByteString
-> WritePrecondition
forall b a. b -> (a -> b) -> Maybe a -> b
maybe WritePrecondition
IfAbsent ByteString -> WritePrecondition
IfRevision Maybe ByteString
revision
    request :: BoundedPut
request = ConditionalPut -> BoundedPut
BoundedPut (WritePrecondition -> PutObject -> ConditionalPut
ConditionalPut WritePrecondition
precondition (BucketName -> ObjectKey -> RequestBody -> PutObject
S3.newPutObject (Text -> BucketName
S3.BucketName Text
bucket) (Text -> ObjectKey
S3.ObjectKey Text
key) (ByteString -> RequestBody
forall a. ToBody a => a -> RequestBody
AWS.toBody ByteString
bytes)))

absenceFallback :: StateCharge -> Deadlines -> AWS.Env -> Limits -> Text -> Text -> IO (Either ObjectFault ObjectRead)
absenceFallback :: StateCharge
-> Deadlines
-> Env
-> Limits
-> Text
-> Text
-> IO (Either ObjectFault ObjectRead)
absenceFallback StateCharge
charge Deadlines
deadlines Env
env Limits
limits Text
bucket Text
key = do
    result <- ResourceT IO (Either Error ListObjectsV2Response)
-> IO (Either Error ListObjectsV2Response)
forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
runResourceT (StateCharge
-> Int
-> Bool
-> Deadlines
-> Env
-> Limits
-> BoundedList
-> Proxy ListObjectsV2
-> ResourceT IO (Either Error (AWSResponse ListObjectsV2))
forall request native.
(AWSRequest request, AWSResponse request ~ RawResponse,
 AWSRequest native) =>
StateCharge
-> Int
-> Bool
-> Deadlines
-> Env
-> Limits
-> request
-> Proxy native
-> ResourceT IO (Either Error (AWSResponse native))
sendBounded StateCharge
charge Int
0 Bool
True Deadlines
deadlines Env
env Limits
limits BoundedList
request (Proxy ListObjectsV2
forall {k} (t :: k). Proxy t
Proxy :: Proxy S3.ListObjectsV2))
    pure $ case result of
        Right ListObjectsV2Response
response
            | ListObjectsV2Response -> Bool
listingProvesAbsence ListObjectsV2Response
response -> ObjectRead -> Either ObjectFault ObjectRead
forall a b. b -> Either a b
Right ObjectRead
ObjectAbsent
            | Bool
otherwise -> ObjectFault -> Either ObjectFault ObjectRead
forall a b. a -> Either a b
Left ObjectFault
ObjectUnknownAbsence
        Left Error
err -> ObjectFault -> Either ObjectFault ObjectRead
forall a b. a -> Either a b
Left (TransportFault -> ObjectFault
ObjectTransport (Error -> TransportFault
classifyAwsTransport Error
err))
  where
    request :: BoundedList
request = ListObjectsV2 -> BoundedList
BoundedList (Text -> Text -> ListObjectsV2
absenceListing Text
bucket Text
key)

objectRevision :: Maybe S3.ETag -> Either ObjectFault ByteString
objectRevision :: Maybe ETag -> Either ObjectFault ByteString
objectRevision (Just (S3.ETag ByteString
value)) | Bool -> Bool
not (ByteString -> Bool
BS.null ByteString
value) = ByteString -> Either ObjectFault ByteString
forall a b. b -> Either a b
Right ByteString
value
objectRevision Maybe ETag
_ = ObjectFault -> Either ObjectFault ByteString
forall a b. a -> Either a b
Left ObjectFault
ObjectNoRevision

serviceCode :: AWS.Error -> Maybe Text
serviceCode :: Error -> Maybe Text
serviceCode (AWS.ServiceError ServiceError
err) = case ServiceError
err ServiceError
-> Getting ErrorCode ServiceError ErrorCode -> ErrorCode
forall s a. s -> Getting a s a -> a
^. Getting ErrorCode ServiceError ErrorCode
Lens' ServiceError ErrorCode
AWS.serviceError_code of
    AWS.ErrorCode Text
value -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
value
serviceCode Error
_ = Maybe Text
forall a. Maybe a
Nothing

boundedCall :: IO (Either ObjectFault a) -> IO (Either ObjectFault a)
boundedCall :: forall a. IO (Either ObjectFault a) -> IO (Either ObjectFault a)
boundedCall IO (Either ObjectFault a)
action =
    (TransportFault -> ObjectFault)
-> IO (Either ObjectFault a) -> IO (Either ObjectFault a)
forall fault a.
(TransportFault -> fault)
-> IO (Either fault a) -> IO (Either fault a)
foldTransportEscapes TransportFault -> ObjectFault
ObjectTransport (IO (Either ObjectFault a) -> IO (Either ObjectFault a))
-> IO (Either ObjectFault a) -> IO (Either ObjectFault a)
forall a b. (a -> b) -> a -> b
$
        IO (Either ObjectFault a)
action
            IO (Either ObjectFault a)
-> (ObjectLimit -> IO (Either ObjectFault a))
-> IO (Either ObjectFault a)
forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
`catch` (\(ObjectLimit Int
cap) -> Either ObjectFault a -> IO (Either ObjectFault a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ObjectFault -> Either ObjectFault a
forall a b. a -> Either a b
Left (Int -> ObjectFault
ObjectTooLarge Int
cap)))
            IO (Either ObjectFault a)
-> (ObjectProgress -> IO (Either ObjectFault a))
-> IO (Either ObjectFault a)
forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
`catch` (\ObjectProgress
ObjectProgress -> Either ObjectFault a -> IO (Either ObjectFault a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ObjectFault -> Either ObjectFault a
forall a b. a -> Either a b
Left (TransportFault -> ObjectFault
ObjectTransport (TransportCause -> Text -> TransportFault
transportFault TransportCause
TransportTimeout Text
"the mirror state exchange fell below its progress floor"))))