Skip to content

worktree ​

Function, in @jr2/orchestrator/src/worktree.ts. It is an export of @jr2/orchestrator, in the Effects group of the Authoring surface page.

ts
function worktree(options: WorktreeOptions = {}): EffectLogic<WorktreeInput, WorktreeOutput>

Make a worktree Effect: it cuts a Worktree of a Repo Slot, or reads the one that is there. Put it in actors, and invoke it from a workspace() body.

With { path, branch, from? }, the branch resolves in this order, after a fetch: a local branch; origin/<branch>, tracked; a new branch from from or the Binding's base, with no upstream. With { path, detach }, the Worktree is forced to that commit and cleaned, each time. path is exactly /work/<slot>/<name>. Each outcome is output: ready, refused, unknown.

With { path, handoff, branch? }, in a nested Workspace, the cut makes a branch Worktree at the Handoff's commit: the ready output of a cut of an enclosing Workspace, which came in through the door (ADR-0080). The branch is branch, or the Handoff's branch, and it tracks origin/<branch> when the clone holds that branch. found is "handoff". A re-cut of the path with the same Handoff is held; with another Handoff, it is refused. Each cut at a Handoff, a held one too, first carries the files from the parent Sandbox: a carry that is refused or unknown settles the cut so. jr2 trusts no field of a Handoff: the cut finds it in the run's ledger, and refuses it when a fact does not agree. The commit must be the head of a ready cut of that path in an enclosing Workspace of this run (an earlier ready cut of the path is good too), on the Handoff's branch, in the same Repo. A Handoff of the nested Workspace's own cut, of a sibling or of another run is refused. A detached Handoff needs branch. expect applies to the branch, as at a branch cut. Each round of a final review is a new nested Workspace: invoke it after the parent leaves the state that invoked the last one, or the new one takes over the last one's Sandbox and its cut.

expect says if the branch can exist: worktree({ expect: "new" }) refuses a branch that exists, and worktree({ expect: "existing" }) refuses a branch that does not exist. A run cannot change it.

A cut needs a Repo Slot. Under a Workspace with no Repo Slot, jr2 up refuses it, and the invoke fails with worktree "<slot>" runs under a Workspace with no Repo Slot: a cut makes a Worktree of a Repo Slot, and there is none — declare one: repos: { app: "<url>" } (ADR-0075).

Parameters

  • options (WorktreeOptions): Optional. expect, and timeout, how long the fetch and the cut can take. Default: "10m". The carry of a Handoff's files, before the cut, has a time of its own.

Returns: The actor logic of the Effect's slot. Its output is a WorktreeOutput.

Example

ts
import { fail, isReady, jr2Setup, worktree, type Workspaced } from "@jr2/orchestrator";

type BodyInput = Workspaced<{ branch: string }, "app">;
const tree = (context: BodyInput) => `${context.workspace.repos.app}/fix` as const;

export const body = jr2Setup({
  types: {} as { input: BodyInput; context: BodyInput },
  actors: { cut: worktree({ expect: "new" }), fail: fail() },
}).createMachine({
  context: ({ input }) => input,
  on: { "workspace.lost": ".notCut" },
  initial: "cutting",
  states: {
    cutting: {
      invoke: {
        src: "cut",
        input: ({ context }) => ({ path: tree(context), branch: context.branch }),
        // The last arm has no guard: it takes `refused`, `unknown` and each later outcome.
        onDone: [{ guard: ({ event }) => isReady(event.output), target: "done" }, { target: "notCut" }],
      },
    },
    done: { type: "final" },
    notCut: { invoke: { src: "fail", input: { reason: "the Worktree was not cut" } } },
  },
});

See also

The docs of Kit version 0.8.0. Each page is also at its .md URL.