-- | Pure renderers and file loading shared by embeddable operator hosts.module Kiroku.Cli.Compaction ( loadCompactionManifest, CompactionCommandError (..), renderCompactionReport, renderCompactionRefusals, renderCompactionRecords, renderStoreIdentity, renderCompactionCommandError, renderCompactionResult, )whereimport Control.Exception (IOException, displayException, try)import Control.Lens ((^.))import Data.Aeson qualified as Aesonimport Data.Aeson.KeyMap qualified as KeyMapimport Data.ByteString qualified as BSimport Data.ByteString.Lazy qualified as LBSimport Data.Generics.Labels ()import Data.Int (Int32)import Data.List.NonEmpty (NonEmpty)import Data.List.NonEmpty qualified as NEimport Data.Text (Text)import Data.Text qualified as Timport Data.Text.Encoding qualified as TEimport Data.UUID qualified as UUIDimport Data.Vector (Vector)import Data.Vector qualified as Vimport Kiroku.Cli.Command (ManifestPath (..), OutputFormat (..))import Kiroku.Store.Compaction.Typesimport Kiroku.Store.Error (StoreError (..))import Kiroku.Store.Types (GlobalPosition (..), StoreIdentity (..), StreamName (..), StreamVersion (..))data CompactionCommandError = ManifestUnreadable FilePath Text | ManifestInvalid FilePath Text | ConfirmDigestUnparseable Text | ConfirmDigestMismatch {typed :: Text, manifest :: Text} | LedgerLimitOutOfRange Int32 | StoreFailure StoreError deriving stock (Eq, Show)loadCompactionManifest :: ManifestPath -> IO (Either CompactionCommandError CompactionManifest)loadCompactionManifest (ManifestPath path) = do readResult <- try (BS.readFile path) :: IO (Either IOException BS.ByteString) pure $ case readResult of Left err -> Left (ManifestUnreadable path (T.pack (displayException err))) Right bytes -> case Aeson.eitherDecodeStrict bytes of Left err -> Left (ManifestInvalid path (T.pack err)) Right value -> Right valuejson :: (Aeson.ToJSON a) => a -> Textjson = TE.decodeUtf8 . LBS.toStrict . Aeson.encodeshown :: (Show a) => a -> Textshown = T.pack . showidentityText :: StoreIdentity -> TextidentityText (StoreIdentity uid) = UUID.toText uidpositionText :: GlobalPosition -> TextpositionText (GlobalPosition n) = shown npolicyText :: (Aeson.ToJSON a) => a -> TextpolicyText value = case Aeson.toJSON value of Aeson.String text -> text other -> json otherrenderCompactionReport :: OutputFormat -> Maybe CompactionRecord -> CompactionReport -> TextrenderCompactionReport OutputJson Nothing report = json reportrenderCompactionReport OutputJson (Just record) _ = json recordrenderCompactionReport OutputTable context report = maybe "" (\record -> "COMPACTION_ID " <> compactionIdText (record ^. #compactionId) <> "\n") context <> table ["FIELD", "VALUE"] [ ["MANIFEST_DIGEST", compactionDigestHex (report ^. #manifestDigest)], ["STORE_IDENTITY", identityText (report ^. #storeIdentity)], ["OPERATION", compactionOperationText (report ^. #operation)], ["DEAD_LETTER_POLICY", policyText (report ^. #deadLetterPolicy)], ["CAUSATION_POLICY", policyText (report ^. #causationPolicy)], ["SELECTED_EVENTS", shown (report ^. #selectedEvents)], ["HOME_MEMBERSHIPS", shown (report ^. #homeMemberships)], ["GLOBAL_MEMBERSHIPS", shown (report ^. #globalMemberships)], ["LINK_MEMBERSHIPS", shown (report ^. #linkMemberships)], ["DEAD_LETTERS_REMOVED", shown (report ^. #deadLettersRemoved)], ["CAUSATION_DEPENDENTS", shown (report ^. #causationDependents)], ["LOWEST_GLOBAL_POSITION", positionText (report ^. #lowestGlobalPosition)], ["HIGHEST_GLOBAL_POSITION", positionText (report ^. #highestGlobalPosition)], ["REPORT_DIGEST", compactionDigestHex (report ^. #reportDigest)] ] <> "\nAFFECTED_STREAMS\n" <> table ["STREAM", "HEAD_VERSION"] [[name, shown version] | StreamHeadWitness (StreamName name) (StreamVersion version) <- V.toList (report ^. #affectedStreams)]renderCompactionRefusals :: OutputFormat -> NonEmpty CompactionRefusal -> TextrenderCompactionRefusals OutputJson refusals = json (NE.toList refusals)renderCompactionRefusals OutputTable refusals = "refused: " <> shown (NE.length refusals) <> " refusal(s); no rows were changed\n" <> table ["KIND", "EVENT_ID", "STREAM", "DETAIL"] (map row (NE.toList refusals)) where row refusal = case Aeson.toJSON refusal of Aeson.Object object -> [tag refusal, optional "event_id" object, optional "stream" object, shown refusal] _ -> [tag refusal, "", "", shown refusal] optional key object = case KeyMap.lookup key object of Just (Aeson.String text) -> text _ -> "" tag = refusalNamerefusalName :: CompactionRefusal -> TextrefusalName = \case CompactionStoreIdentityMismatch {} -> "CompactionStoreIdentityMismatch" CompactionHistoryRetentionActive {} -> "CompactionHistoryRetentionActive" CompactionStreamMissing {} -> "CompactionStreamMissing" CompactionStreamSoftDeleted {} -> "CompactionStreamSoftDeleted" CompactionStreamHeadDrift {} -> "CompactionStreamHeadDrift" CompactionSelectedEventMissing {} -> "CompactionSelectedEventMissing" CompactionWitnessMismatch {} -> "CompactionWitnessMismatch" CompactionUnexpectedLink {} -> "CompactionUnexpectedLink" CompactionAcknowledgedLinkMissing {} -> "CompactionAcknowledgedLinkMissing" CompactionDeadLettersPresent {} -> "CompactionDeadLettersPresent" CompactionCausationDependentsPresent {} -> "CompactionCausationDependentsPresent" CompactionLedgerConflict {} -> "CompactionLedgerConflict"renderCompactionRecords :: OutputFormat -> Vector CompactionRecord -> TextrenderCompactionRecords OutputJson = jsonrenderCompactionRecords OutputTable = table ["COMPACTION_ID", "APPLIED_AT", "APPLIED_BY", "OPERATION", "SELECTED_EVENTS", "MANIFEST_DIGEST"] . map row . V.toList where row record = [ compactionIdText (record ^. #compactionId), shown (record ^. #appliedAt), record ^. #appliedBy, compactionOperationText (record ^. #report . #operation), shown (record ^. #report . #selectedEvents), compactionDigestHex (record ^. #report . #manifestDigest) ]compactionIdText :: CompactionId -> TextcompactionIdText (CompactionId uid) = UUID.toText uidrenderStoreIdentity :: OutputFormat -> StoreIdentity -> TextrenderStoreIdentity OutputTable identity = table ["STORE_IDENTITY"] [[identityText identity]]renderStoreIdentity OutputJson identity = json (Aeson.object ["store_identity" Aeson..= identity])-- | JSON remains a single valid document; human status prefixes are table-only.renderCompactionResult :: OutputFormat -> Bool -> CompactionRecord -> TextrenderCompactionResult OutputJson already record = json (Aeson.object ["result" Aeson..= (if already then "already_applied" else "applied" :: Text), "record" Aeson..= record])renderCompactionResult OutputTable already record = (if already then "already applied: " else "applied: ") <> compactionIdText (record ^. #compactionId) <> " at " <> shown (record ^. #appliedAt) <> " by " <> record ^. #appliedBy <> "\n" <> renderCompactionReport OutputTable Nothing (record ^. #report)renderCompactionCommandError :: OutputFormat -> CompactionCommandError -> TextrenderCompactionCommandError OutputJson err = json (Aeson.object ["error" Aeson..= errorText err])renderCompactionCommandError OutputTable err = "kiroku: " <> errorText errerrorText :: CompactionCommandError -> TexterrorText = \case ManifestUnreadable path detail -> "cannot read manifest " <> T.pack path <> ": " <> detail ManifestInvalid path detail -> "invalid manifest " <> T.pack path <> ": " <> detail ConfirmDigestUnparseable value -> "invalid confirmation digest: " <> value ConfirmDigestMismatch typed expected -> "confirmation digest " <> typed <> " does not match manifest " <> expected LedgerLimitOutOfRange value -> "ledger limit must be 1–1000; got " <> shown value StoreFailure (CompactionConcurrentMutation digest guard) -> "compaction " <> compactionDigestHex digest <> ": " <> guard <> "; a concurrent reference appeared mid-apply; nothing was deleted — re-run preview and apply" StoreFailure err -> shown errtable :: [Text] -> [[Text]] -> Texttable headers rows = T.intercalate "\n" (map render (headers : rows)) where widths = [maximum (map T.length (header : [row !! i | row <- rows])) | (i, header) <- zip [0 ..] headers] render columns = T.stripEnd (T.intercalate " " (zipWith (\width value -> T.justifyLeft width ' ' value) widths columns))