Skip to content

Commit b28f7bb

Browse files
committed
Revive logic to reject components with ambiguous element orderings
1 parent 4e0bcf2 commit b28f7bb

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

share-api/src/Share/Web/UCM/Sync/Impl.hs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import Share.Web.Errors
5252
import Share.Web.UCM.Sync.HashJWT qualified as HashJWT
5353
import Share.Web.UCM.Sync.Types (EntityBunch (..), RepoInfoKind (..), entityKind)
5454
import U.Codebase.Causal qualified as Causal
55+
import U.Codebase.Sqlite.HashHandle qualified as HH
5556
import U.Codebase.Sqlite.Orphans ()
5657
import Unison.Codebase.Path qualified as Path
5758
import Unison.Hash32 (Hash32)
@@ -259,11 +260,11 @@ insertEntitiesToCodebase codebase entities = do
259260
mayErrs <- PG.transactionUnsafeIO $ batchValidateEntities maxParallelismPerUploadRequest isComponentHashMismatchAllowedIO isCausalHashMismatchAllowedIO unsavedEntities
260261
case mayErrs of
261262
Nothing -> pure ()
262-
Just (err :| _errs) -> throwError err
263-
-- case err of
264-
-- Right e -> throwError e
265-
-- Left () ->
266-
-- throwError $ Sync.InvalidByteEncoding (Hash32.fromHash hash) Sync.TermComponentType "Incomplete element ordering in term components"
263+
Just (err :| _errs) ->
264+
case err of
265+
Right e -> throwError e
266+
Left (HH.IncompleteElementOrderingError (ComponentHash hash)) ->
267+
throwError $ Sync.InvalidByteEncoding (Hash32.fromHash hash) Sync.TermComponentType "Incomplete element ordering in term components"
267268
SyncQ.saveTempEntities codebase unsavedEntities
268269
let hashesNowInTemp = Set.fromList (fst <$> Foldable.toList unsavedEntities) <> (Set.fromList . Foldable.toList $ hashesAlreadyInTemp)
269270
pure hashesNowInTemp
@@ -397,7 +398,7 @@ batchValidateEntities ::
397398
(ComponentHash -> ComponentHash -> IO Bool) ->
398399
(CausalHash -> CausalHash -> IO Bool) ->
399400
f (Hash32, Sync.Entity Text Hash32 Hash32) ->
400-
IO (Maybe (NonEmpty Sync.EntityValidationError))
401+
IO (Maybe (NonEmpty (Either HH.HashingFailure Sync.EntityValidationError)))
401402
batchValidateEntities maxParallelism checkIfComponentHashMismatchIsAllowed checkIfCausalHashMismatchIsAllowed entities = do
402403
errs <- UnliftIO.pooledForConcurrentlyN maxParallelism entities \(hash, entity) ->
403404
validateEntity checkIfComponentHashMismatchIsAllowed checkIfCausalHashMismatchIsAllowed hash entity
@@ -409,16 +410,16 @@ validateEntity ::
409410
(CausalHash -> CausalHash -> m Bool) ->
410411
Hash32 ->
411412
Share.Entity Text Hash32 Hash32 ->
412-
m (Maybe Sync.EntityValidationError)
413+
m (Maybe (Either HH.HashingFailure Sync.EntityValidationError))
413414
validateEntity checkIfComponentHashMismatchIsAllowed checkIfCausalHashMismatchIsAllowed hash entity = do
414415
case (Sync.validateEntity hash entity) of
415-
Just (err@(Sync.EntityHashMismatch Sync.TermComponentType (Sync.HashMismatchForEntity {supplied = expectedHash, computed = actualHash}))) ->
416+
Just (Right (err@(Sync.EntityHashMismatch Sync.TermComponentType (Sync.HashMismatchForEntity {supplied = expectedHash, computed = actualHash})))) ->
416417
checkIfComponentHashMismatchIsAllowed (ComponentHash . Hash32.toHash $ expectedHash) (ComponentHash . Hash32.toHash $ actualHash) >>= \case
417-
False -> pure (Just err)
418+
False -> pure (Just $ Right err)
418419
True -> pure Nothing
419-
Just (err@(Sync.EntityHashMismatch Sync.CausalType (Sync.HashMismatchForEntity {supplied = expectedHash, computed = actualHash}))) ->
420+
Just (Right (err@(Sync.EntityHashMismatch Sync.CausalType (Sync.HashMismatchForEntity {supplied = expectedHash, computed = actualHash})))) ->
420421
checkIfCausalHashMismatchIsAllowed (CausalHash . Hash32.toHash $ expectedHash) (CausalHash . Hash32.toHash $ actualHash) >>= \case
421-
False -> pure (Just err)
422+
False -> pure (Just $ Right err)
422423
True -> pure Nothing
423424
Just err ->
424425
-- This shouldn't happen unless the ucm client is buggy or malicious

share-task-runner/src/Share/Tasks/AmbiguousComponentCheck.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Share.Postgres.Cursors qualified as PG
88
import Share.Prelude
99
import Share.Utils.Logging (Loggable (..))
1010
import Share.Utils.Logging qualified as Logging
11+
import U.Codebase.Sqlite.HashHandle qualified as HH
1112
import U.Codebase.Sqlite.TempEntity
1213
import Unison.Hash32
1314
import Unison.Sync.EntityValidation qualified as EV
@@ -17,7 +18,7 @@ import Unison.Util.Servant.CBOR qualified as CBOR
1718

1819
data AmbiguousComponentCheckError
1920
= TaskAmbiguousComponentCheckError Hash32
20-
| TaskEntityValidationError Hash32 (Sync.EntityValidationError)
21+
| TaskEntityValidationError Hash32 (Either HH.HashingFailure Sync.EntityValidationError)
2122
| TaskEntityDecodingError Hash32 CBOR.DeserialiseFailure
2223
deriving (Show, Eq)
2324

0 commit comments

Comments
 (0)