Skip to content

Commit

Permalink
Merge pull request #5570 from unisonweb/syncv2/causal-negotiation
Browse files Browse the repository at this point in the history
Add SyncV2 Causal Negotiation
  • Loading branch information
ChrisPenner authored Feb 5, 2025
2 parents 5282521 + c8d360d commit c4149a9
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 55 deletions.
13 changes: 11 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
* [`UNISON_SHARE_ACCESS_TOKEN`](#unison_share_access_token)
* [`UNISON_READONLY`](#unison_readonly)
* [`UNISON_ENTITY_VALIDATION`](#unison_entity_validation)
* [`UNISON_SYNC_VERSION`](#unison_sync_version)
* [Local Codebase Server](#local-codebase-server)
* [Codebase Configuration](#codebase-configuration)

## UCM Configuration

### `UNISON_DEBUG`

Enable debugging output for various portions of the application.
Enable debugging output for various portions of the application.
See `lib/unison-prelude/src/Unison/Debug.hs` for the full list of supported flags.

E.g.
Expand Down Expand Up @@ -62,7 +63,7 @@ Note for Windows users: Due to an outstanding issue with GHC's IO manager on Win
Enabling the LSP on windows can cause UCM to hang on exit and may require the process to be killed by the operating system or via Ctrl-C.
Note that this doesn't pose any risk of codebase corruption or cause any known issues, it's simply an annoyance.

If you accept this annoyance, you can enable the LSP server on Windows by exporting the `UNISON_LSP_ENABLED=true` environment variable.
If you accept this annoyance, you can enable the LSP server on Windows by exporting the `UNISON_LSP_ENABLED=true` environment variable.

You can set this persistently in powershell using:

Expand Down Expand Up @@ -117,6 +118,14 @@ Defaults to enabled.
$ UNISON_ENTITY_VALIDATION="false" ucm
```

### `UNISON_SYNC_VERSION`

Allows enabling the experimental Sync Version 2 protocol when downloading code from Share.

```sh
$ UNISON_ENTITY_VALIDATION="2" ucm
```

### `UNISON_PULL_WORKERS`

Allows setting the number of workers to use when pulling from a codebase server.
Expand Down
22 changes: 15 additions & 7 deletions unison-cli/src/Unison/Cli/DownloadUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ where
import Control.Concurrent.STM (atomically)
import Control.Concurrent.STM.TVar (modifyTVar', newTVarIO, readTVar, readTVarIO)
import Data.List.NonEmpty (pattern (:|))
import Data.Set qualified as Set
import System.Console.Regions qualified as Console.Regions
import System.IO.Unsafe (unsafePerformIO)
import U.Codebase.HashTags (CausalHash)
import U.Codebase.Sqlite.Queries qualified as Queries
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.Share.Projects qualified as Share
import Unison.Codebase.Editor.HandleInput.AuthLogin (ensureAuthenticatedWithCodeserver)
import Unison.Codebase.Editor.Input (SyncVersion (..))
import Unison.Codebase.Editor.Output qualified as Output
import Unison.Codebase.Editor.RemoteRepo (ReadShareLooseCode, shareUserHandleToText)
import Unison.Codebase.Editor.RemoteRepo qualified as RemoteRepo
Expand All @@ -36,15 +35,26 @@ import Unison.Share.Types (codeserverBaseURL)
import Unison.Sync.Common qualified as Sync.Common
import Unison.Sync.Types qualified as Share
import Unison.SyncV2.Types qualified as SyncV2
import UnliftIO.Environment qualified as UnliftIO

data SyncVersion = SyncV1 | SyncV2
deriving (Eq, Show)

-- | The version of the sync protocol to use.
syncVersion :: SyncVersion
syncVersion = unsafePerformIO do
UnliftIO.lookupEnv "UNISON_SYNC_VERSION"
<&> \case
Just "2" -> SyncV2
_ -> SyncV1

-- | Download a project/branch from Share.
downloadProjectBranchFromShare ::
(HasCallStack) =>
SyncVersion ->
Share.IncludeSquashedHead ->
Share.RemoteProjectBranch ->
Cli (Either Output.ShareError CausalHash)
downloadProjectBranchFromShare syncVersion useSquashed branch =
downloadProjectBranchFromShare useSquashed branch =
Cli.labelE \done -> do
let remoteProjectBranchName = branch.branchName
causalHashJwt <-
Expand All @@ -67,11 +77,9 @@ downloadProjectBranchFromShare syncVersion useSquashed branch =
Cli.respond (Output.DownloadedEntities numDownloaded)
SyncV2 -> do
let branchRef = SyncV2.BranchRef (into @Text (ProjectAndBranch branch.projectName remoteProjectBranchName))
-- TODO: Fill this in.
let knownHashes = Set.empty
let downloadedCallback = \_ -> pure ()
let shouldValidate = not $ Codeserver.isCustomCodeserver Codeserver.defaultCodeserver
result <- SyncV2.syncFromCodeserver shouldValidate Share.hardCodedBaseUrl branchRef causalHashJwt knownHashes downloadedCallback
result <- SyncV2.syncFromCodeserver shouldValidate Share.hardCodedBaseUrl branchRef causalHashJwt downloadedCallback
result & onLeft \err0 -> do
done case err0 of
Share.SyncError pullErr ->
Expand Down
2 changes: 1 addition & 1 deletion unison-cli/src/Unison/Codebase/Editor/HandleInput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ loop e = do
_ <- Cli.updateAtM description pp \destb ->
liftIO (Branch.merge'' (Codebase.lca codebase) Branch.RegularMerge srcb destb)
Cli.respond Success
PullI syncVersion sourceTarget pullMode -> handlePull syncVersion sourceTarget pullMode
PullI sourceTarget pullMode -> handlePull sourceTarget pullMode
PushRemoteBranchI pushRemoteBranchInput -> handlePushRemoteBranch pushRemoteBranchInput
SyncToFileI syncFileDest projectBranchName -> SyncV2.handleSyncToFile syncFileDest projectBranchName
SyncFromFileI syncFileSrc projectBranchName -> do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ handleInstallLib remind (ProjectAndBranch libdepProjectName unresolvedLibdepBran
Cli.Env {codebase} <- ask

causalHash <-
downloadProjectBranchFromShare SyncV1 Share.IncludeSquashedHead libdepProjectBranch
downloadProjectBranchFromShare Share.IncludeSquashedHead libdepProjectBranch
& onLeftM (Cli.returnEarly . Output.ShareError)

remoteBranchObject <- liftIO (Codebase.expectBranchForHash codebase causalHash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import U.Codebase.Sqlite.Project qualified as Sqlite (Project (..))
import U.Codebase.Sqlite.ProjectBranch qualified as Sqlite
import U.Codebase.Sqlite.Queries qualified as Q
import U.Codebase.Sqlite.Queries qualified as Queries
import Unison.Cli.DownloadUtils (SyncVersion (..), downloadProjectBranchFromShare)
import Unison.Cli.DownloadUtils (downloadProjectBranchFromShare)
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.MonadUtils qualified as Cli (getCurrentProjectAndBranch)
Expand Down Expand Up @@ -225,7 +225,7 @@ cloneInto localProjectBranch remoteProjectBranch = do
let remoteProjectBranchNames = ProjectAndBranch remoteProjectName remoteBranchName

branchHead <-
downloadProjectBranchFromShare SyncV1 Share.NoSquashedHead remoteProjectBranch
downloadProjectBranchFromShare Share.NoSquashedHead remoteProjectBranch
& onLeftM (Cli.returnEarly . Output.ShareError)

localProjectAndBranch <-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import U.Codebase.Sqlite.Project (Project (..))
import U.Codebase.Sqlite.ProjectBranch (ProjectBranch (..))
import U.Codebase.Sqlite.Queries (expectCausalHashIdByCausalHash)
import U.Codebase.Sqlite.Queries qualified as Queries
import Unison.Cli.DownloadUtils (SyncVersion (..), downloadProjectBranchFromShare)
import Unison.Cli.DownloadUtils (downloadProjectBranchFromShare)
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.Share.Projects qualified as Share
Expand Down Expand Up @@ -108,7 +108,7 @@ projectCreate tryDownloadingBase maybeProjectName = do
Share.GetProjectBranchResponseBranchNotFound -> done Nothing
Share.GetProjectBranchResponseProjectNotFound -> done Nothing
Share.GetProjectBranchResponseSuccess branch -> pure branch
downloadProjectBranchFromShare SyncV1 Share.NoSquashedHead baseLatestReleaseBranch
downloadProjectBranchFromShare Share.NoSquashedHead baseLatestReleaseBranch
& onLeftM (Cli.returnEarly . Output.ShareError)
Cli.Env {codebase} <- ask
baseLatestReleaseBranchObject <-
Expand Down
5 changes: 2 additions & 3 deletions unison-cli/src/Unison/Codebase/Editor/HandleInput/Pull.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ import Unison.Prelude
import Unison.Project (ProjectAndBranch (..), ProjectBranchNameOrLatestRelease (..), ProjectName)
import Witch (unsafeFrom)

handlePull :: SyncVersion -> PullSourceTarget -> PullMode -> Cli ()
handlePull syncVersion unresolvedSourceAndTarget pullMode = do
handlePull :: PullSourceTarget -> PullMode -> Cli ()
handlePull unresolvedSourceAndTarget pullMode = do
let includeSquashed = case pullMode of
Input.PullWithHistory -> Share.NoSquashedHead
Input.PullWithoutHistory -> Share.IncludeSquashedHead
Expand All @@ -59,7 +59,6 @@ handlePull syncVersion unresolvedSourceAndTarget pullMode = do
ReadShare'LooseCode repo -> downloadLooseCodeFromShare repo & onLeftM (Cli.returnEarly . Output.ShareError)
ReadShare'ProjectBranch remoteBranch ->
downloadProjectBranchFromShare
syncVersion
( case pullMode of
Input.PullWithHistory -> Share.NoSquashedHead
Input.PullWithoutHistory -> Share.IncludeSquashedHead
Expand Down
4 changes: 2 additions & 2 deletions unison-cli/src/Unison/Codebase/Editor/HandleInput/SyncV2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Control.Lens
import Control.Monad.Reader (MonadReader (..))
import U.Codebase.HashTags (CausalHash)
import U.Codebase.Sqlite.Queries qualified as Q
import Unison.Cli.DownloadUtils (SyncVersion (..), downloadProjectBranchFromShare)
import Unison.Cli.DownloadUtils (downloadProjectBranchFromShare)
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.MonadUtils qualified as Cli
Expand Down Expand Up @@ -75,4 +75,4 @@ handleSyncFromCodebase description srcCodebasePath srcBranch destBranch = do
Cli.respond (Output.SyncPullError syncErr)

handleSyncFromCodeserver :: Projects.IncludeSquashedHead -> Projects.RemoteProjectBranch -> Cli (Either Output.ShareError CausalHash)
handleSyncFromCodeserver = downloadProjectBranchFromShare SyncV2
handleSyncFromCodeserver = downloadProjectBranchFromShare
6 changes: 1 addition & 5 deletions unison-cli/src/Unison/Codebase/Editor/Input.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module Unison.Codebase.Editor.Input
-- * Type aliases
ErrorMessageOrName,
RawQuery,
SyncVersion (..),
)
where

Expand Down Expand Up @@ -60,9 +59,6 @@ data Event
= UnisonFileChanged SourceName Source
deriving stock (Show)

data SyncVersion = SyncV1 | SyncV2
deriving (Eq, Show)

type Source = Text -- "id x = x\nconst a b = a"

type SourceName = Text -- "foo.u" or "buffer 7"
Expand Down Expand Up @@ -138,7 +134,7 @@ data Input
MergeLocalBranchI BranchRelativePath (Maybe BranchRelativePath) Branch.MergeMode
| PreviewMergeLocalBranchI BranchRelativePath (Maybe BranchRelativePath)
| DiffNamespaceI BranchId2 BranchId2 -- old new
| PullI !SyncVersion !PullSourceTarget !PullMode
| PullI !PullSourceTarget !PullMode
| PushRemoteBranchI PushRemoteBranchInput
| SyncToFileI FilePath (ProjectAndBranch (Maybe ProjectName) (Maybe ProjectBranchName))
| SyncFromFileI FilePath UnresolvedProjectBranch
Expand Down
21 changes: 6 additions & 15 deletions unison-cli/src/Unison/CommandLine/InputPatterns.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ module Unison.CommandLine.InputPatterns
projectSwitch,
projectsInputPattern,
pull,
pullV2,
pullWithoutHistory,
push,
pushCreate,
Expand Down Expand Up @@ -1788,13 +1787,7 @@ reset =

pull :: InputPattern
pull =
pullImpl "pull" [] Input.PullWithHistory "" Input.SyncV1

pullV2 :: InputPattern
pullV2 =
(pullImpl "pull.v2" [] Input.PullWithHistory "" Input.SyncV2)
{I.visibility = I.Hidden
}
pullImpl "pull" [] Input.PullWithHistory ""

pullWithoutHistory :: InputPattern
pullWithoutHistory =
Expand All @@ -1803,10 +1796,9 @@ pullWithoutHistory =
[]
Input.PullWithoutHistory
"without including the remote's history. This usually results in smaller codebase sizes."
Input.SyncV1

pullImpl :: String -> [String] -> Input.PullMode -> P.Pretty CT.ColorText -> Input.SyncVersion -> InputPattern
pullImpl name aliases pullMode addendum syncVersion = do
pullImpl :: String -> [String] -> Input.PullMode -> P.Pretty CT.ColorText -> InputPattern
pullImpl name aliases pullMode addendum = do
self
where
self =
Expand Down Expand Up @@ -1850,18 +1842,18 @@ pullImpl name aliases pullMode addendum syncVersion = do
explainRemote Pull
],
parse = \case
[] -> pure $ Input.PullI syncVersion Input.PullSourceTarget0 pullMode
[] -> pure $ Input.PullI Input.PullSourceTarget0 pullMode
[sourceArg] -> do
source <- handlePullSourceArg sourceArg
pure (Input.PullI syncVersion (Input.PullSourceTarget1 source) pullMode)
pure (Input.PullI (Input.PullSourceTarget1 source) pullMode)
[sourceArg, targetArg] ->
-- You used to be able to pull into a path, so this arg parser is a little complicated, because
-- we want to provide helpful suggestions if you are doing a deprecated or invalid thing.
case ( handlePullSourceArg sourceArg,
handleMaybeProjectBranchArg targetArg,
handlePath'Arg targetArg
) of
(Right source, Right target, _) -> Right (Input.PullI syncVersion (Input.PullSourceTarget2 source target) pullMode)
(Right source, Right target, _) -> Right (Input.PullI (Input.PullSourceTarget2 source target) pullMode)
(Left err, _, _) -> Left err
-- Parsing as a path didn't work either; just show the branch parse error
(Right _, Left err, Left _) -> Left err
Expand Down Expand Up @@ -3821,7 +3813,6 @@ validInputs =
projectSwitch,
projectsInputPattern,
pull,
pullV2,
pullWithoutHistory,
push,
pushCreate,
Expand Down
Loading

0 comments on commit c4149a9

Please sign in to comment.