Announcing jj-musubi: live sync for Jujutsu

Announcing jj-musubi: live sync for Jujutsu

我很高兴地宣布推出一款新的源代码控制工具 jj-musubi,它可以在多台机器之间实时同步 Jujutsu (jj) 仓库的副本。每当 jj 对我的工作进行快照时,jj-musubi 都会在瞬间将其同步到每一台机器上,并处理提交、变基、获取等操作。

This simple capability is surprisingly powerful. I often work on one machine, and build or test on others. Now they get my changes as I make them, and I don’t need to remember whether I was editing on my laptop or someplace else.

这种简单的功能出奇地强大。我经常在一台机器上工作,而在其他机器上进行构建或测试。现在,当我做出更改时,它们会立即获取这些更改,我无需再费心去记自己是在笔记本电脑上还是在其他地方进行编辑。

If I fetch changes from or push changes to upstream, all of my copies stay up to date. jj-musubi makes repos on multiple machines feel like jj workspaces (worktrees if you speak git). It’s intended for use by one person who works across several machines, so it has no security model (or rather, that is the extent of its security model).

如果我从上游获取更改或向其推送更改,我所有的副本都会保持最新状态。jj-musubi 让多台机器上的仓库感觉就像 jj 工作区(如果你熟悉 git,可以理解为工作树)。它旨在供在多台机器上工作的个人使用,因此它没有安全模型(或者说,这就是其安全模型的全部范围)。

Things you can do

你可以做的事情

Jujutsu already treats the working copy as a commit. Running jj status records a snapshot of my edits in that commit, which jj-musubi then syncs to my other machines. I like to have my shell prompt run jj status, so in practice I rarely have to think about snapshots.

Jujutsu 已经将工作副本视为一次提交。运行 jj status 会在该提交中记录我编辑的快照,然后 jj-musubi 会将其同步到我的其他机器上。我喜欢让 shell 提示符运行 jj status,所以在实践中我几乎不需要考虑快照的问题。

jj-musubi does not write to the working copy. Each machine has its own working copy and workspaces as usual, so syncs of commits, snapshots, and rewrites can bring in new history while a build is running, without changing the files under it. On a laptop, I can work offline, and my machine will sync when I’m back on the network.

jj-musubi 不会写入工作副本。每台机器都像往常一样拥有自己的工作副本和工作区,因此在构建运行时,提交、快照和重写的同步可以引入新的历史记录,而不会更改其下的文件。在笔记本电脑上,我可以离线工作,当重新连接到网络时,我的机器会自动同步。

This has also proven handy when I have agents working on several machines. I can give each agent its own jj workspace on different systems, then run jj diff -r devbox/agent-1@ on the laptop to see what one of them has done so far. If one finds a bug that another needs to fix, they can coordinate and see each other’s work without overhead.

当我有代理在多台机器上工作时,这也证明非常方便。我可以为每个代理在不同系统上分配自己的 jj 工作区,然后在笔记本电脑上运行 jj diff -r devbox/agent-1@ 来查看其中一个代理目前所做的工作。如果一个代理发现了另一个代理需要修复的错误,它们可以进行协调并查看彼此的工作,而无需额外的开销。

Ephemeral uses

临时使用

jj-musubi refers to a collection of copies of a repo as a hive. I often run agents in temporary disposable VMs. jj-musubi supports this by allowing an agent to lease its repo’s membership in the hive: if it doesn’t renew that lease in time, its repo gets forgotten about, so temporary repos or vanished containers can’t just stack up and clutter things.

jj-musubi 将仓库副本的集合称为“蜂巢”(hive)。我经常在临时的、可丢弃的虚拟机中运行代理。jj-musubi 通过允许代理租用其仓库在蜂巢中的成员资格来支持这一点:如果它没有及时续租,其仓库就会被遗忘,因此临时仓库或消失的容器不会堆积并造成混乱。

Why I wanted this

我为什么需要这个

Back when I was at Meta, I kicked off the project that became Sapling and Mononoke. Sapling’s Commit Cloud gives engineers there a similar way to move work between machines, and Google engineers have something similar with an internal jj server.

在 Meta 工作时,我启动了后来演变为 Sapling 和 Mononoke 的项目。Sapling 的 Commit Cloud 为那里的工程师提供了一种在机器之间移动工作的类似方式,而谷歌的工程师通过内部的 jj 服务器也拥有类似的功能。

Since then, I’d missed that everyday convenience for the machines I already use, and felt like I could get it without massive infrastructure or hosting. With jj-musubi, one machine runs a hub, and daemons exchange jj history with it over SSH. That’s almost as lightweight as you can get.

从那时起,我就一直怀念在现有机器上使用这种日常便利的感觉,并觉得无需庞大的基础设施或托管服务也能实现它。使用 jj-musubi,一台机器运行中心(hub),守护进程通过 SSH 与其交换 jj 历史记录。这几乎是你能做到的最轻量级的方案了。

Safety

安全性

What’s tricky is getting concurrent work right. That’s already hard in any distributed source control system, and when syncs are live and we plan for disconnected work in an unreliable network, the difficulty goes way up.

棘手的是如何正确处理并发工作。这在任何分布式源代码控制系统中都已经很难了,而当同步是实时的,并且我们还要考虑在不可靠网络中的离线工作时,难度会大幅增加。

As a user, I shouldn’t have to care about when exactly I can safely close my laptop after finishing an edit; all of my changes should make it through either just before I close it, or right after I reopen. No work should get lost or corrupted in the presence of interrupts, resends, delays, or machines sleeping, and the system should stay reliable even when machines go offline forever.

作为用户,我不应该关心在完成编辑后到底什么时候可以安全地合上笔记本电脑;我所有的更改都应该在我合上电脑之前或重新打开之后立即生效。在出现中断、重发、延迟或机器休眠的情况下,不应有任何工作丢失或损坏,即使机器永久离线,系统也应保持可靠。

I used Lean to prove key properties of jj-musubi’s network protocol, and tests compare the Rust code against that model. An extensive body of other tests run real jj repositories through concurrent edits, interrupted syncs, hub restarts, and a myriad of other nasty situations. Mutation and adversarial testing ensure that when bugs are injected, sensible things happen. The safety guide details all the checks (and the parts I don’t yet cover).

我使用 Lean 证明了 jj-musubi 网络协议的关键属性,并通过测试将 Rust 代码与该模型进行了对比。大量的其他测试在真实的 jj 仓库中进行了并发编辑、中断同步、中心重启以及无数其他棘手情况的模拟。变异测试和对抗性测试确保了在注入错误时,系统能做出合理的反应。安全指南详细说明了所有的检查项(以及我尚未覆盖的部分)。

Concurrent edits without commits…!?

没有提交的并发编辑……!?

It might sound spooky or dangerous to be able to see in-progress work on one machine from another. How can that be safe?

能够从一台机器上看到另一台机器上正在进行的工作,听起来可能有些诡异或危险。这怎么可能是安全的呢?

If you use jj in multiple workspaces, you’re already used to the novelty of being able to check out the same change in two workspaces and modify it. jj simply marks the change as divergent, but it won’t update either working copy with changes from the other until you decide to, e.g. by calling jj workspace update-stale. In jj, dealing with divergent versions of a change is similar to any other kind of conflicted merge.

如果你在多个工作区中使用 jj,你已经习惯了能够在两个工作区中检出同一个更改并对其进行修改的新奇体验。jj 只是将该更改标记为“分歧”(divergent),但在你决定更新之前(例如通过调用 jj workspace update-stale),它不会用另一个工作区的更改来更新任何一个工作副本。在 jj 中,处理更改的分歧版本类似于处理任何其他类型的冲突合并。

jj-musubi uses jj’s existing workspace machinery, so everything will feel familiar. The only difference is that when you run jj-musubi, a workspace’s name is prefixed with the name of the machine it lives on.

jj-musubi 使用了 jj 现有的工作区机制,所以一切都会让你感到熟悉。唯一的区别是,当你运行 jj-musubi 时,工作区的名称会加上它所在机器名称的前缀。

Try it

试用一下

Start with e.g. a laptop and another machine that’s always on (let’s call it devbox), with a copy of a jj repo on each. On each machine, download the binary or build it yourself: cargo install --locked jj-musubi. Run musubi hub on devbox; it will keep your sync data in ~/musubi-hub. In the laptop’s repo, run musubi create --hub devbox. In devbox’s repo, run musubi join.

从一台笔记本电脑和另一台始终开启的机器(我们称之为 devbox)开始,每台机器上都有一个 jj 仓库的副本。在每台机器上,下载二进制文件或自行构建:cargo install --locked jj-musubi。在 devbox 上运行 musubi hub;它会将你的同步数据保存在 ~/musubi-hub 中。在笔记本电脑的仓库中,运行 musubi create --hub devbox。在 devbox 的仓库中,运行 musubi join。

I provide builds for Linux, macOS and Windows, and the quick start walks through setup and the first sync. If you try it and something surprises you, I’d like to hear about it.

我提供了适用于 Linux、macOS 和 Windows 的构建版本,快速入门指南将引导你完成设置和首次同步。如果你在试用过程中遇到任何让你感到意外的情况,欢迎随时反馈。