@ligeti/step-git-worktree 0.0.0-stage → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +539 -2
  3. package/dist/fast-forward.d.ts +23 -0
  4. package/dist/fast-forward.d.ts.map +1 -0
  5. package/dist/fast-forward.js +126 -0
  6. package/dist/fast-forward.js.map +1 -0
  7. package/dist/git-dir-file.d.ts +34 -0
  8. package/dist/git-dir-file.d.ts.map +1 -0
  9. package/dist/git-dir-file.js +108 -0
  10. package/dist/git-dir-file.js.map +1 -0
  11. package/dist/git-exec.d.ts +33 -0
  12. package/dist/git-exec.d.ts.map +1 -0
  13. package/dist/git-exec.js +214 -0
  14. package/dist/git-exec.js.map +1 -0
  15. package/dist/head-commit.d.ts +17 -0
  16. package/dist/head-commit.d.ts.map +1 -0
  17. package/dist/head-commit.js +65 -0
  18. package/dist/head-commit.js.map +1 -0
  19. package/dist/herdr.d.ts +21 -0
  20. package/dist/herdr.d.ts.map +1 -0
  21. package/dist/herdr.js +175 -0
  22. package/dist/herdr.js.map +1 -0
  23. package/dist/index.d.ts +12 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +14 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/paths.d.ts +18 -0
  28. package/dist/paths.d.ts.map +1 -0
  29. package/dist/paths.js +48 -0
  30. package/dist/paths.js.map +1 -0
  31. package/dist/push.d.ts +9 -0
  32. package/dist/push.d.ts.map +1 -0
  33. package/dist/push.js +205 -0
  34. package/dist/push.js.map +1 -0
  35. package/dist/rebase.d.ts +45 -0
  36. package/dist/rebase.d.ts.map +1 -0
  37. package/dist/rebase.js +340 -0
  38. package/dist/rebase.js.map +1 -0
  39. package/dist/remote-url.d.ts +17 -0
  40. package/dist/remote-url.d.ts.map +1 -0
  41. package/dist/remote-url.js +54 -0
  42. package/dist/remote-url.js.map +1 -0
  43. package/dist/remove-file.d.ts +23 -0
  44. package/dist/remove-file.d.ts.map +1 -0
  45. package/dist/remove-file.js +95 -0
  46. package/dist/remove-file.js.map +1 -0
  47. package/dist/settings.d.ts +7 -0
  48. package/dist/settings.d.ts.map +1 -0
  49. package/dist/settings.js +26 -0
  50. package/dist/settings.js.map +1 -0
  51. package/dist/show-file.d.ts +20 -0
  52. package/dist/show-file.d.ts.map +1 -0
  53. package/dist/show-file.js +88 -0
  54. package/dist/show-file.js.map +1 -0
  55. package/dist/start-branch.d.ts +31 -0
  56. package/dist/start-branch.d.ts.map +1 -0
  57. package/dist/start-branch.js +168 -0
  58. package/dist/start-branch.js.map +1 -0
  59. package/dist/types.d.ts +34 -0
  60. package/dist/types.d.ts.map +1 -0
  61. package/dist/types.js +86 -0
  62. package/dist/types.js.map +1 -0
  63. package/dist/worktree.d.ts +5 -0
  64. package/dist/worktree.d.ts.map +1 -0
  65. package/dist/worktree.js +124 -0
  66. package/dist/worktree.js.map +1 -0
  67. package/package.json +26 -3
package/README.md CHANGED
@@ -1,3 +1,540 @@
1
- # Temporary Holding Version
1
+ # @ligeti/step-git-worktree
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Creates a git worktree so every step in a run shares one working directory, and removes it when the
4
+ run is done, as the `git.worktree` resource step. Also provides `git.push` (pushes an
5
+ already-committed sha to a branch on a remote), `git.start-branch` (starts a local branch at a
6
+ remote branch's current tip), `git.head-commit` (reports a worktree's HEAD sha once it is confirmed
7
+ to be on a given branch), and `git.rebase` (rebases a branch onto a freshly fetched target branch
8
+ before a push, reporting a conflict as a result rather than throwing), `git.fast-forward`
9
+ (fast-forwards a checked-out branch to its remote tip), `git.show-file` (reads a file at a
10
+ commit), `git.remote-url` (reads the URL a remote points at), `git.remove-file` (removes one tracked file in a commit of its own),
11
+ `git.read-git-dir-file` and `git.write-git-dir-file` (read and write a file inside the worktree's own
12
+ git directory).
13
+
14
+ This README covers `git.worktree` first, then `git.push`, `git.start-branch`, `git.head-commit`,
15
+ `git.rebase`, `git.fast-forward`, `git.show-file`, `git.remove-file`, `git.read-git-dir-file` and
16
+ `git.write-git-dir-file`.
17
+
18
+ ## Vocabulary
19
+
20
+ These words are binding on this package. The project-wide vocabulary is
21
+ [`docs/glossary.md`](../../docs/glossary.md); a word that only this step uses is defined here
22
+ instead, so the project glossary does not grow a row per step.
23
+
24
+ | Term | Definition | It is NOT |
25
+ | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
26
+ | **Worktree** | One git worktree, reserved as a resource so every step in a run shares one working directory, and removed when the resource closes (`src/worktree.ts`). Identified by its path, never by the herdr workspace id recorded alongside it. | Not a **workspace**. The herdr workspace is a separate handle with its own lifetime — it may be closed out of band while the worktree still exists on disk, which is why removal re-derives the path rather than trusting the recorded handle. Not the repository checkout the shell itself runs from. |
27
+ | **Source repository** | The existing checkout a worktree is created from, given as the `repoPath` input and resolved through `realpath`. | Not the worktree. It is never removed by this step. |
28
+
29
+ **Workspace** is the platform's word for a herdr workspace and is defined in
30
+ [`docs/glossary.md`](../../docs/glossary.md); this package uses it with that meaning.
31
+
32
+ ## Inputs
33
+
34
+ | Field | Required | Purpose |
35
+ | -------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
36
+ | `repoPath` | yes | The source repository to create the worktree from. Resolved through `realpath`, because herdr reports fully symlink-resolved paths and reconciliation compares them by string equality. |
37
+ | `worktreeName` | yes | Directory name for the worktree. Restricted to `[A-Za-z0-9._-]+`, and may not be `.` or `..`. |
38
+ | `branchName` | yes | Branch to create. Must be a safe git ref: no leading `-`, no whitespace, no `..`, none of `~^:?*[\`, no leading or trailing `/`, and not ending in `.lock`. |
39
+
40
+ Output is `{ repoPath, worktreePath, branchName }`.
41
+
42
+ ## Environment variables (`git.worktree`)
43
+
44
+ Settings are read from the environment of the **worker process** that runs the step, parsed with
45
+ Zod on every invocation. An invalid environment fails the step with an error naming every offending
46
+ variable.
47
+
48
+ | Variable | Required | Default | Purpose |
49
+ | ------------------ | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
50
+ | `LIGETI_STATE_DIR` | no | `./var` | Where the step records each worktree's metadata. A relative value is resolved once, against the worker process's own cwd — never later against whatever cwd a spawned agent happens to have. |
51
+ | `LIGETI_HERDR_BIN` | no | `herdr` | The `herdr` binary to invoke. |
52
+
53
+ ## Retry and recovery (`git.worktree`)
54
+
55
+ The step sets `retry: { maxAttempts: 1 }` deliberately. A partially created worktree — metadata
56
+ written, `herdr worktree create` interrupted — must not be blindly retried, because a retried spawn
57
+ would race the crash-recovery `find` path over the same metadata file and directory. A failed
58
+ attempt surfaces as a step failure instead.
59
+
60
+ Removal never trusts the recorded herdr workspace id on its own: herdr ids can be reused, so
61
+ passing a stale handle could close an unrelated live workspace. The worktree path is re-derived and
62
+ matched first.
63
+
64
+ ## `git.push`
65
+
66
+ A plain step (`src/push.ts`) that pushes an already-committed commit sha to a branch on a remote,
67
+ run from an existing worktree — intended for a workflow that has already gated and published its own
68
+ commit (`docs/attestation.md`) and pushes it itself, rather than leaving that to an agent's own
69
+ commit/push inside the worktree. Pair it with `git.head-commit` when the sha to push was produced by
70
+ an agent's own commit inside the worktree, rather than being already known to the workflow.
71
+
72
+ ### Inputs
73
+
74
+ | Field | Required | Purpose |
75
+ | ------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
76
+ | `worktreePath` | yes | The worktree (or any git checkout) to run `git push` from. |
77
+ | `remote` | yes | The remote's NAME (as registered by `git remote add`), never a URL. Rejects a leading `-`, whitespace, `..`, and anything containing `://` or `@`. |
78
+ | `sha` | yes | The commit to push. Lowercase hex, 40 characters (sha-1) or 64 (sha-256). |
79
+ | `branch` | yes | The branch to push to. Reuses `git.worktree`'s own safe-branch-name rule (no leading `-`, no whitespace, no `..`, none of `~^:?*[\`, no leading/trailing `/`, not ending in `.lock`). |
80
+ | `noVerify` | no | When `true`, passes `--no-verify` so git skips the target repository's `pre-push` hook. Defaults to `false`. |
81
+ | `expectedRemoteSha` | no | When set, pushes with `--force-with-lease=refs/heads/<branch>:<expectedRemoteSha>` instead of a plain push -- see "Rebase and the push lease" below. Absent, a plain non-force push. |
82
+
83
+ Output is `{ remote, branch, sha }`.
84
+
85
+ The step runs `git -C <worktreePath> push <remote> <sha>:refs/heads/<branch>` — a plain, **non-force**
86
+ push, unless `expectedRemoteSha` is set. Hooks stay enabled in every worktree `git.worktree` creates,
87
+ and the step runs them unless the caller sets `noVerify`. A workflow sets it only for a push of a
88
+ branch still exactly at its base — a commit that was gated before it became the base, so the hook
89
+ would only repeat that gate.
90
+
91
+ ### Rebase and the push lease
92
+
93
+ `expectedRemoteSha` exists for a caller that rebased `branch` (`git.rebase`, below) before pushing
94
+ it: a rebase rewrites `branch`'s history, so a plain push would be rejected as non-fast-forward even
95
+ though nobody else touched the branch. `--force-with-lease=refs/heads/<branch>:<expectedRemoteSha>`
96
+ lets the push replace the remote branch only if it is still exactly at `expectedRemoteSha` — the sha
97
+ `git.rebase` reported the remote had for `branch` right before it rebased. If someone else pushed to
98
+ `branch` in the meantime, the remote no longer matches the lease and git rejects the push with
99
+ `(stale info)`; this step reports that case distinctly from an ordinary non-fast-forward rejection —
100
+ `git.push: push of <sha> to <remote>/<branch> was rejected: remote moved since the lease was taken
101
+ (expected <remote>/<branch> to still be at <expectedRemoteSha>): ...` — since a stale lease means
102
+ someone else moved `branch` since `git.rebase` read it, while an ordinary non-fast-forward rejection
103
+ means this push's own sha simply doesn't descend from what the remote has. A push that needs no
104
+ update at all — the remote already has exactly the sha being pushed — succeeds regardless of the
105
+ lease: git
106
+ reports "Everything up-to-date" without ever needing to satisfy `--force-with-lease`, so retrying an
107
+ already-successful lease push is still a success.
108
+
109
+ ### Idempotency and retry
110
+
111
+ A plain, non-force `git push <remote> <sha>:refs/heads/<branch>` is itself idempotent: when the
112
+ remote branch already points at `sha`, git reports "Everything up-to-date" and exits 0 rather than
113
+ erroring — the step needs no special-case pre-check for this, so replaying the step after a
114
+ worker restart is safe.
115
+
116
+ The step sets `retry: { maxAttempts: 1 }` deliberately. A rejected push is not transient: a remote's
117
+ pre-push gate or a non-fast-forward rejection fails the same way every time for the same sha,
118
+ and a retry re-runs the whole gate.
119
+
120
+ A rejected push throws an error naming the remote, branch and sha
121
+ (`git.push: push of <sha> to <remote>/<branch> was rejected (not a fast-forward): ...`), per
122
+ ADR-0005 — never a `{ ok: false }` return.
123
+
124
+ ### Environment (`git.push`)
125
+
126
+ `git.push` builds its child environment from `childEnv` (`src/herdr.ts`)'s base allow-list —
127
+ `PATH`, `HOME`, `USER`, `LOGNAME`, `LANG`, `LC_ALL`, `TMPDIR`, `SHELL`, `TERM`, and the `XDG_*`
128
+ variables — plus these additions, needed only for `git push` to authenticate against a real remote:
129
+
130
+ | Variable | Why |
131
+ | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
132
+ | `SSH_AUTH_SOCK` | The running ssh-agent's socket. Without it, an SSH remote authenticated by a key already loaded into the agent fails outright. |
133
+ | `GIT_SSH_COMMAND` | Overrides the `ssh` binary/args git invokes (a deploy-key-specific `-i` flag, a custom `ProxyCommand`). Without it, a workflow-selected key is silently dropped. |
134
+ | `SSH_ASKPASS` / `GIT_ASKPASS` | The askpass helper ssh/git call for a credential prompt. Some setups point this at a non-interactive script that supplies a fixed answer. |
135
+ | `HTTP_PROXY` / `HTTPS_PROXY` / `NO_PROXY` (both cases), `ALL_PROXY` (both cases) | Needed whenever the network path to the remote goes through a proxy — git/libcurl read these directly from the environment. |
136
+ | `LIGETI_STEP_GIT_PUSH_ENV` | Names of further env vars, comma-separated, that `git push` passes on to the target worktree's own `pre-push` hook (example below). Empty by default, so a hook sees only what the rows above give it. Each name must also be set in the environment `ligeti serve` runs in, and is optional, so a missing name is logged, not fatal. Restart to apply it. |
137
+
138
+ Pushes to GitHub use SSH, or whatever credentials the operator's git configuration provides;
139
+ `GITHUB_TOKEN` is for the `github-*` step packages only and is not passed to git.
140
+
141
+ An SSH agent that asks a person to approve each signature, such as 1Password's, refuses to sign
142
+ while the screen is locked and shows no prompt, so a `git fetch` or `git push` late in a long run
143
+ fails with `sign_and_send_pubkey: signing failed … communication with agent failed`. Give the
144
+ workers a key of their own, kept out of any agent, in the shell's `.env`:
145
+
146
+ ```sh
147
+ GIT_SSH_COMMAND="ssh -o BatchMode=yes -o IdentityAgent=none -o IdentitiesOnly=yes -i /path/to/ligeti_key"
148
+ ```
149
+
150
+ The shell passes `GIT_SSH_COMMAND` to every worker whose workflow uses `git.push`,
151
+ `git.start-branch`, `git.fast-forward` or `git.rebase`, which declare it and all use it. Restart
152
+ `ligeti serve` after changing `.env`.
153
+
154
+ A target repository's `pre-push` hook comes from the worktree being pushed from, so only the
155
+ operator decides what it receives. Ligeti's own hook (`scripts/pre-push-gate.sh`) reads the
156
+ gitlab-registry store, so an operator pushing it sets
157
+ `LIGETI_STEP_GIT_PUSH_ENV=GITLAB_TOKEN,LIGETI_PUBLIC_BASE_URL` where `ligeti serve` runs.
158
+ The hook uses `LIGETI_PUBLIC_BASE_URL` with either store, to ask the shell to gate.
159
+ `git.push` itself never reads them; see `docs/attestation.md`'s pre-push hook section.
160
+
161
+ The step also forces `GIT_TERMINAL_PROMPT=0`: it runs inside a worker process with no tty, so a real
162
+ interactive credential prompt would otherwise hang forever instead of failing fast.
163
+
164
+ `git` (and the pre-push hook it runs) is spawned with `detached: true`, giving it its own process
165
+ group so a timeout or an aborted `sctx.signal` can kill that whole group -- including anything the
166
+ hook itself spawned -- rather than leaving orphaned children behind. The trade-off: `git`/the hook
167
+ are no longer part of the worker process's own session or foreground process group, so a terminal
168
+ `Ctrl-C` sent to `pnpm serve` does not reach them directly. Only this step's own timeout
169
+ (`PUSH_TIMEOUT_MS`) and `sctx.signal` can stop a running push and its hook.
170
+
171
+ `PUSH_TIMEOUT_MS` is 25 minutes, because the push must outlast the target repository's own
172
+ pre-push hook, which may ask the ligeti shell to run the whole gate; 25 minutes is comfortably above
173
+ the gate's observed worst case of about 17 minutes.
174
+
175
+ `GIT_CONFIG_GLOBAL`/`GIT_CONFIG_NOSYSTEM`/`GIT_CONFIG_SYSTEM` are deliberately **not** passed
176
+ through: a real `credential.helper` (macOS Keychain, `git-credential-manager`, ...) lives in the
177
+ user's real global config, and this step must see it to authenticate for real. Test isolation from
178
+ that same config is done in the test's own direct `git` calls
179
+ (`test/push.git.int.test.ts`), never through this step's env-building.
180
+
181
+ ## Fetching beside other worktrees
182
+
183
+ `git.start-branch`, `git.rebase` and `git.fast-forward` fetch into `refs/remotes/<remote>/<branch>`,
184
+ which every worktree of one repository shares, so sibling workflows fetching the same branch
185
+ concurrently contend for the same `.lock` files. Each fetch therefore runs with
186
+ `core.filesRefLockTimeout` and `core.packedRefsTimeout` of 15 seconds, so git itself waits out a
187
+ brief lock instead of failing with `cannot lock ref`; with `gc.auto=0` and `maintenance.auto=false`,
188
+ so an auto-gc triggered by one fetch cannot collide with another. Each worktree has its own
189
+ `FETCH_HEAD`, so fetches in sibling worktrees do not race over it. A lock held longer than 15 seconds
190
+ still fails the step, which its `retry` then re-runs.
191
+
192
+ ## `git.start-branch`
193
+
194
+ Starts a local branch at a remote branch's current tip via a real `git fetch` followed by
195
+ `git switch -C`, so a plan step's implementation starts from the spec branch as it is on the
196
+ remote -- not from whatever the local checkout's own (possibly stale, or entirely absent) copy of
197
+ that branch happens to be. Not a resource step: nothing it creates needs closing.
198
+
199
+ | Field | Required | Default | Purpose |
200
+ | -------------------- | -------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
201
+ | `worktreePath` | yes | -- | Absolute path of the git working tree to start the branch in. |
202
+ | `branch` | yes | -- | The local branch to create (or reset). Same safe-ref-name check as `git.push`'s `branch`. |
203
+ | `from` | yes | -- | The remote branch to start from, as it currently is on `remote`. Same safe-ref-name check. |
204
+ | `remote` | no | `"origin"` | The git remote to fetch from. |
205
+ | `timeoutSeconds` | no | `300` | Bounds the `git fetch` call. `git fetch` runs no hook, unlike `git.push`, so the default only needs to cover a real network round-trip. |
206
+ | `continueFromBranch` | no | `false` | When `true` and `branch` exists on `remote`, start from that remote branch's tip instead of from `from`; when it does not, start from `from`. |
207
+ | `discardChanges` | no | `false` | When `true`, a dirty worktree is discarded (`git reset --hard` then `git clean -fd`) instead of failing the step. |
208
+
209
+ Output is `{ branch, commit, continued }`, where `commit` is the SHA of `branch`'s new tip (via
210
+ `git rev-parse HEAD`, resolved after `switch` completes) and `continued` is `true` when the branch
211
+ was started from its own remote branch, `false` when from `from`.
212
+
213
+ `worktreePath`'s local copy of `from`, if any, is never trusted: `git.start-branch` always fetches
214
+ `refs/heads/<from>` from `remote` into `refs/remotes/<remote>/<from>` first, then switches `branch`
215
+ onto that freshly fetched ref with `git switch -C` (not plain `-c`) -- `-C` resets `branch` to that
216
+ ref even if it already exists locally, which is what makes a re-execution of this step (workflow
217
+ recovery replaying an unrecorded attempt, or the remote's `from` having moved since) idempotent
218
+ rather than failing with "branch already exists" or leaving `branch` pointed at a stale commit.
219
+
220
+ With `continueFromBranch`, the step first fetches `refs/heads/<branch>` from `remote`; "couldn't
221
+ find remote ref" means the remote has no such branch and the step falls back to `from`, any other
222
+ fetch failure throws. The decision comes from that fetch, never from a remote-tracking ref already
223
+ on disk, which may be stale.
224
+
225
+ Before switching, `git.start-branch` checks `worktreePath` for a clean state via
226
+ `git status --porcelain` (which also reports untracked files) and throws a descriptive error if it
227
+ is non-empty, rather than letting `switch -C` decide -- `switch -C` only rejects a change that
228
+ actually conflicts with the target branch, so a dirty file whose content happens to agree with
229
+ `branch`'s version (or an untracked file, which never conflicts) would otherwise be carried onto
230
+ the new branch silently instead of being refused. `retry: { maxAttempts: 3 }`: unlike
231
+ `git.push`, nothing here is pushed to a remote, so there is no "the retry would just repeat a
232
+ rejection" risk -- fetch-then-switch-`-C` is safe to retry on a transient failure (e.g. a network
233
+ blip during `fetch`), and, with `discardChanges: false`, the one failure a retry cannot paper over
234
+ (a dirty worktree) reports the same, correct error on every attempt.
235
+
236
+ With `discardChanges: true`, a non-empty `git status --porcelain` is not an error: the step logs
237
+ the paths it found (through the step's own contextual logger) and discards them with
238
+ `git reset --hard` (tracked changes) followed by `git clean -fd` (untracked files), then proceeds
239
+ to switch as usual. `-fd`, not `-fdx`: an ignored file -- `node_modules`, a build output directory
240
+ -- is never something a prior step's own work left behind for this run to keep; it is build
241
+ tooling state that is expensive to regenerate, so it survives the clean. This exists for a caller
242
+ whose own worktree state is never worth keeping between runs -- e.g. `implement-bead`, which pushes
243
+ only its own gated commit and never trusts whatever a review command left in the worktree
244
+ afterward.
245
+
246
+ `git.start-branch` runs `git` with the same environment as `git.head-commit` (`buildGitEnv`, shared
247
+ via `git-exec.ts`): `childEnv`'s base allow-list plus `SSH_AUTH_SOCK`/`GIT_SSH_COMMAND` when set and
248
+ non-empty, `GIT_TERMINAL_PROMPT=0`, and a default `GIT_SSH_COMMAND` of `ssh -o BatchMode=yes` -- `git fetch`
249
+ authenticates against the same remote a push would, even though it runs no hook of its own.
250
+
251
+ ## `git.head-commit`
252
+
253
+ Reports `worktreePath`'s `HEAD` sha, after confirming `HEAD` is on `branch` -- so a workflow that
254
+ lets an agent commit inside a worktree (the spec phase, small-factory) can learn the sha to hand to
255
+ `git.push`, rather than trusting the agent to report it. Not a resource step: it only reads.
256
+
257
+ | Field | Required | Purpose |
258
+ | -------------- | -------- | ------------------------------------------------------------------------------------------ |
259
+ | `worktreePath` | yes | Absolute path of the git working tree to read `HEAD` from. |
260
+ | `branch` | yes | The branch `HEAD` is expected to be on. Same safe-ref-name check as `git.push`'s `branch`. |
261
+
262
+ Output is `{ sha }`.
263
+
264
+ `git.head-commit` checks `worktreePath`'s current branch (`git symbolic-ref -q HEAD`, compared as
265
+ the full `refs/heads/<branch>` ref rather than `--short`'s abbreviated form, which a same-named tag
266
+ can make ambiguous) before resolving `HEAD`, and throws a descriptive error if `HEAD` is detached or
267
+ checked out onto some other branch, rather than silently reporting a commit from the wrong branch.
268
+ It then resolves `sha` via `git rev-parse HEAD`. Both calls run under `QUICK_GIT_TIMEOUT_MS` (30s,
269
+ shared with `git.start-branch`'s own quick calls via `git-exec.ts`): neither touches a hook or the
270
+ network, so neither has a legitimate reason to run long.
271
+
272
+ `retry: { maxAttempts: 3 }`: both calls are read-only, so a retry on a transient failure (e.g. the
273
+ worktree is momentarily locked by another git process) is safe and produces the same result a fresh
274
+ attempt would.
275
+
276
+ `git.head-commit` runs `git` with the same environment as `git.start-branch` (`buildGitEnv`, shared
277
+ via `git-exec.ts`).
278
+
279
+ ## `git.rebase`
280
+
281
+ Rebases a worktree's branch onto a freshly fetched target branch before a push -- so a factory
282
+ workflow can rebase onto the current tip of its base branch, resolve any conflict, and only then
283
+ push, instead of letting a stale branch collide with the base on the remote. Not a resource step;
284
+ nothing it creates needs closing. A conflict is a result (`status: "conflicted"`), not a thrown
285
+ error, per ADR-0005 -- it is expected traffic a caller (a workflow that launches an agent to resolve
286
+ it) is required to branch on, not an incident.
287
+
288
+ | Field | Required | Default | Purpose |
289
+ | ------------------- | -------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
290
+ | `worktreePath` | yes | -- | Absolute path of the git working tree to rebase in. |
291
+ | `branch` | yes | -- | The local branch to rebase. `HEAD` must already be on it. Same safe-ref-name check as `git.push`'s `branch`. |
292
+ | `onto` | yes | -- | The remote branch to rebase onto, as it currently is on `remote`. Same safe-ref-name check. |
293
+ | `remote` | no | `"origin"` | The git remote to fetch `onto` from and read `branch`'s current tip from. Same safe-remote-name check as `git.push`'s `remote`. |
294
+ | `timeoutSeconds` | no | `300` | Bounds each of the two fetches and the rebase itself, individually -- not their sum. Defaults to 300 (5 minutes) per command. |
295
+ | `expectedRemoteSha` | no | -- | The sha the caller itself already knows `remote`/`branch` to be at -- see "expectedRemoteSha and the ancestry check" below. |
296
+ | `discardChanges` | no | `false` | When `true`, a dirty worktree is discarded (`git reset --hard` then `git clean -fd`) instead of failing the step -- same semantics as `git.start-branch`'s own `discardChanges`. |
297
+
298
+ Output is a discriminated union on `status`:
299
+
300
+ - `{ status: "rebased", headSha, ontoSha, remoteBranchSha }` -- the rebase completed (a no-op rebase,
301
+ when `branch` was already up to date with `onto`, reports the same shape). `headSha` is `HEAD`'s
302
+ sha after the rebase. `ontoSha` is the sha `onto` resolved to on `remote` after the fetch.
303
+ `remoteBranchSha` is what `remote` had for `branch` right before the rebase (`null` if `branch`
304
+ doesn't exist on `remote` yet) -- pass it as `git.push`'s `expectedRemoteSha` so the push that
305
+ follows can only replace exactly that remote state.
306
+ - `{ status: "conflicted", headSha, ontoSha, remoteBranchSha, conflictedPaths }` -- the rebase hit a
307
+ conflict; `git.rebase` already ran `git rebase --abort`, so the worktree is back at its pre-rebase
308
+ `HEAD` (`headSha`, restored, and re-verified after the abort) and clean. `conflictedPaths` lists
309
+ the paths git reported as unmerged (`git diff --name-only -z --diff-filter=U`) at the first commit
310
+ in the rebase that stopped -- a later commit further along the same rebase may conflict on other
311
+ paths too, which only surface once this conflict is resolved and the rebase (or a fresh
312
+ `git.rebase` run) continues past it.
313
+
314
+ ### Behavior
315
+
316
+ 1. If a rebase is already in progress in `worktreePath` (a crash mid-rebase on a previous attempt --
317
+ detected via `git rev-parse --git-path rebase-merge`/`rebase-apply`, resolved against
318
+ `worktreePath` since that path is relative to it for the main working tree but already absolute
319
+ for a linked worktree), `git.rebase` logs what it is about to discard (`git status --porcelain`,
320
+ at `warn`) and runs `git rebase --abort`, so a fresh attempt always starts from a clean slate.
321
+ 2. Confirms `HEAD` is on `branch` (the same `git symbolic-ref -q HEAD`, full-ref check as
322
+ `git.head-commit`'s own), throwing otherwise -- including when `HEAD` is detached.
323
+ 3. With `discardChanges: false` (the default), refuses (throws) if the worktree has tracked changes
324
+ (`git status --porcelain --untracked-files=no`). An untracked file is tolerated at this point, but
325
+ is not immune from every failure mode: if a commit the rebase replays would create a file at the
326
+ same path, git refuses to overwrite the untracked file and stops that pick -- see step 6's
327
+ "stopped without a conflict" case. With `discardChanges: true`, the same check instead covers
328
+ untracked files too (a plain `git status --porcelain`), and a non-empty result is discarded --
329
+ `git reset --hard` for tracked changes, then `git clean -fd` (an ignored file survives) for
330
+ untracked ones -- logged at `warn` with the paths discarded, exactly like `git.start-branch`'s own
331
+ `discardChanges`.
332
+ 4. Fetches `+refs/heads/<onto>:refs/remotes/<remote>/<onto>`, forced exactly like
333
+ `git.start-branch`'s own refspec -- `onto` may have been rebased or force-pushed on the remote
334
+ since this worktree last fetched it.
335
+ 5. Fetches `+refs/heads/<branch>:refs/remotes/<remote>/<branch>` the same way, to learn
336
+ `remoteBranchSha` and to bring the commit it names into this worktree's object database.
337
+ "couldn't find remote ref" (git's own wording for a nonexistent ref) is not a failure here --
338
+ it means `branch` doesn't exist on `remote` yet, and `remoteBranchSha` is `null` -- unless
339
+ `expectedRemoteSha` was given, in which case a missing `branch` on `remote` is itself a throw (see
340
+ "expectedRemoteSha and the ancestry check" below). When `remoteBranchSha` is not `null` and
341
+ `expectedRemoteSha` was not given, `git.rebase` then requires
342
+ `git merge-base --is-ancestor <remoteBranchSha> HEAD` before it will rebase at all: this is what
343
+ makes the `remoteBranchSha` lease meaningful. `--force-with-lease` on `git.push`'s side only
344
+ checks that the remote is STILL at `remoteBranchSha` by the time it pushes -- it never checks that
345
+ `remoteBranchSha` was safe to discard in the first place. Without this check, a remote commit the
346
+ local branch never fetched before now (a human's own push, or a suggestion applied on GitLab)
347
+ would simply be rebased over and force-pushed away. When the check fails, `git.rebase` throws
348
+ (`git.rebase: remote <remote>/<branch> at <sha> has commits the local branch lacks; refusing to
349
+ rebase and force-push over them`) without touching the worktree.
350
+ 6. Runs `git rebase <ontoSha>` non-interactively: `GIT_EDITOR`/`GIT_SEQUENCE_EDITOR=true` so nothing
351
+ ever opens an editor, and `-c rebase.autoStash=false -c rebase.autoSquash=false -c
352
+ rebase.updateRefs=false -c commit.gpgSign=false -c rebase.rebaseMerges=false -c
353
+ rerere.enabled=false` so no implicit git config on the machine running this can change the
354
+ rebase's shape, require a GPG prompt, preserve merge commits, or silently replay a locally
355
+ recorded conflict resolution. Default rebase behavior (merge commits get linearized) is otherwise
356
+ unchanged. The target repository's own hooks (`pre-rebase`, `post-checkout`, `post-rewrite`) still
357
+ run during the rebase, under the same environment and the same `timeoutSeconds` bound as the
358
+ rebase command itself.
359
+ - Exit 0 -> `status: "rebased"`.
360
+ - Non-zero, and no rebase is left in progress -> throws with git's own detail: the rebase failed
361
+ outright rather than stopping mid-pick.
362
+ - Non-zero, a rebase is left in progress, and the command timed out -> the leftover rebase is
363
+ aborted and the step throws, naming the timeout.
364
+ - Non-zero, a rebase is left in progress, it did not time out, but
365
+ `git diff --name-only -z --diff-filter=U` reports no unmerged paths -> not a real conflict (an
366
+ untracked file in the way of a replayed commit, a missing committer identity failing with exit
367
+ 128, a rerere-autoupdate oddity, ...): the leftover rebase is aborted and the step throws with
368
+ git's own detail, rather than reporting `status: "conflicted"` with nothing for a
369
+ conflict-resolution agent to act on.
370
+ - Non-zero, a rebase is left in progress, it did not time out, and unmerged paths exist -> a real
371
+ conflict: collects `conflictedPaths`, runs `git rebase --abort`, re-reads `HEAD` to confirm it
372
+ is genuinely back on `refs/heads/<branch>` at its pre-rebase sha (throwing if not), and returns
373
+ `status: "conflicted"`.
374
+
375
+ ### `expectedRemoteSha` and the ancestry check
376
+
377
+ The ancestry check (step 5 above) reasons about the pre-rebase `HEAD` in this worktree, which only
378
+ works while that `HEAD` is genuinely the commit `remoteBranchSha` was pushed from. Two situations
379
+ break that assumption without the remote itself ever moving:
380
+
381
+ - A caller runs `git.rebase`, gets `status: "conflicted"` (this step aborts back to the pre-rebase
382
+ `HEAD`), launches an agent to resolve the conflict, and the agent runs `git rebase` (and
383
+ `git rebase --continue`) itself, directly in the worktree -- rewriting `HEAD`. The caller's
384
+ following `git.rebase` call, meant only to confirm the conflict is actually gone, now has a `HEAD`
385
+ that is the _rebased_ result, not the commit the old `remoteBranchSha` descends from.
386
+ - A DBOS crash-replay re-runs a `git.rebase` call whose rebase had already completed locally before
387
+ the crash. `HEAD` is again the post-rebase commit, not the pre-rebase one the ancestry check wants.
388
+
389
+ In both cases `git merge-base --is-ancestor <remoteBranchSha> HEAD` would now fail even though
390
+ nobody ever pushed anything unsafe to `remote`. `expectedRemoteSha` lets a caller that already knows
391
+ this to sidestep the check: pass the sha the caller itself last established `remote`/`branch` to be
392
+ at -- its own earlier push, or the `remoteBranchSha` an earlier `git.rebase` call on this branch
393
+ already put through the ancestry check. When given, `git.rebase` requires the freshly fetched remote
394
+ branch to be exactly at `expectedRemoteSha`:
395
+
396
+ - If `branch` does not exist on `remote` at all, it throws (`git.rebase: expected remote
397
+ <remote>/<branch> to be at <expectedRemoteSha>, but it does not exist on <remote> -- refusing to
398
+ rebase without confirming what's on the remote`).
399
+ - If the remote branch has moved to some other sha, it throws (`git.rebase: remote
400
+ <remote>/<branch> moved to <sha> since <expectedRemoteSha>, the sha the caller last knew it to be
401
+ at -- refusing to rebase without confirming what's on the remote`).
402
+ - Otherwise, it skips the ancestry check entirely and proceeds -- `remoteBranchSha` in the output is
403
+ `expectedRemoteSha`.
404
+
405
+ A caller holding a `git.push` lease from an earlier `git.rebase` call (that call's own
406
+ `remoteBranchSha`) should pass that same sha as `expectedRemoteSha` on any later `git.rebase` call
407
+ for the same branch, and a caller re-verifying a rebase after an out-of-band rebase (an agent's own
408
+ conflict resolution, or a crash-replay) must pass it, or the ancestry check throws spuriously as
409
+ described above.
410
+
411
+ ### Idempotency and retry
412
+
413
+ Re-running `git.rebase` after it already completed is a no-op only while `onto` has not moved since:
414
+ `branch` is already based on the same `ontoSha`, so `git rebase <ontoSha>` reports "up to date" and
415
+ exits 0, exactly like a fresh, already-up-to-date rebase. If `onto` moved on the remote between the
416
+ two runs, a re-run rebases onto the new tip instead -- still correct, just not a no-op.
417
+ `retry: { maxAttempts: 3 }`: nothing here is pushed to a remote -- both fetches only write this
418
+ worktree's own remote-tracking refs, and the rebase itself only rewrites `branch` locally -- so a
419
+ transient network failure during either fetch is safe to retry, the same reasoning as
420
+ `git.start-branch`'s own retry policy. A genuine conflict never throws, so it never reaches this
421
+ policy at all; the one failure a retry cannot paper over -- `HEAD` not on `branch`, tracked dirty
422
+ changes, or the remote branch having commits the local branch lacks -- reports the same, correct
423
+ error on every attempt.
424
+
425
+ `git.rebase` runs `git` with the same environment as `git.start-branch` and `git.head-commit`
426
+ (`buildGitEnv`, shared via `git-exec.ts`), plus `GIT_EDITOR`/`GIT_SEQUENCE_EDITOR=true` for the
427
+ rebase call specifically.
428
+
429
+ ## `git.fast-forward`
430
+
431
+ Fetches a branch from a remote and fast-forwards the worktree's checked-out copy of it to that tip,
432
+ so a workflow picks up commits that landed on the remote (a reviewer's fixup, a rebase done in the
433
+ web UI) without ever rewriting local history. Not a resource step.
434
+
435
+ | Field | Required | Default | Purpose |
436
+ | ---------------- | -------- | ---------- | ------------------------------------------------------------------------------------------------------------ |
437
+ | `worktreePath` | yes | -- | Absolute path of the git working tree to fast-forward in. |
438
+ | `branch` | yes | -- | The branch to fast-forward. `HEAD` must already be on it. Same safe-ref-name check as `git.push`'s `branch`. |
439
+ | `remote` | no | `"origin"` | The git remote to fetch from. Same safe-remote-name check as `git.push`'s `remote`. |
440
+ | `timeoutSeconds` | no | `300` | Bounds the `git fetch`. Defaults to 300 (5 minutes), like `git.start-branch`. |
441
+
442
+ Output is `{ headSha, remoteSha }`: `headSha` is `HEAD` after the merge and `remoteSha` is the tip
443
+ the fetch brought to `refs/remotes/<remote>/<branch>`. They are equal unless the local branch is
444
+ _ahead_ of the remote, which is left as it is (`git merge --ff-only` reports "Already up to date").
445
+
446
+ The step confirms `HEAD` is on `refs/heads/<branch>` (as `git.head-commit` does), runs
447
+ `git fetch <remote> +refs/heads/<branch>:refs/remotes/<remote>/<branch>` under `timeoutSeconds`, then
448
+ `git merge --ff-only refs/remotes/<remote>/<branch>` under `QUICK_GIT_TIMEOUT_MS`. It throws when
449
+ `HEAD` is detached or on another branch, when the remote branch does not exist (the fetch fails),
450
+ and when the local branch has diverged or a local change would be overwritten (the merge fails).
451
+ The Rules are in [`test/git-worktree.feature`](test/git-worktree.feature).
452
+
453
+ `retry: { maxAttempts: 3 }`, like `git.start-branch`: re-running after the branch is up to date
454
+ changes nothing, and the fetch only refreshes the worktree's own remote-tracking ref.
455
+
456
+ ## `git.show-file`
457
+
458
+ Reads a file's content at a commit from the worktree's repository (`git show <commit>:<path>`),
459
+ regardless of what the working tree or any branch currently holds. Read-only.
460
+
461
+ | Field | Required | Purpose |
462
+ | -------------- | -------- | ---------------------------------------------------------------------------------------------------------------------- |
463
+ | `worktreePath` | yes | Absolute path of the git working tree whose repository holds the commit. |
464
+ | `commit` | yes | The commit to read from: a lowercase 40- (or 64-) character hex sha, the same check as `git.push`'s `sha`. |
465
+ | `path` | yes | The file's repo-relative path. Absolute paths, `..` and `.` segments and backslashes are rejected by the input schema. |
466
+
467
+ Output is `{ content }`, where `content` is the file's text, or `null` when the file does not exist
468
+ at that commit -- expected traffic a caller branches on, not an error (ADR-0005). An empty file is
469
+ `""`, never `null`. Everything else throws: a commit the repository does not have, a `path` that
470
+ names a directory rather than a file, a `worktreePath` that is not a repository. The content is
471
+ decoded as UTF-8, so the step is meant for text files.
472
+
473
+ The step checks the commit exists (`git cat-file -e <commit>^{commit}`), then looks the path up with
474
+ `git rev-parse --verify --quiet <commit>:<path>` -- whose exit status 1 is git's own "no such path"
475
+ signal, so no error message is parsed -- then confirms the object is a blob before running
476
+ `git show`. Every call runs under `QUICK_GIT_TIMEOUT_MS`.
477
+
478
+ `retry: { maxAttempts: 3 }`: every call is read-only, so a retry is safe.
479
+
480
+ ## `git.remote-url`
481
+
482
+ Reports the URL a named remote of a repository points at (`git remote get-url <remote>`, so a
483
+ configured `insteadOf` is applied). Read-only.
484
+
485
+ | Field | Required | Purpose |
486
+ | -------------- | -------- | ------------------------------------------------------------------------------- |
487
+ | `worktreePath` | yes | Absolute path of the git working tree or repository to read the remote from. |
488
+ | `remote` | no | The remote's NAME, with `git.push`'s checks, never a URL. Defaults to `origin`. |
489
+
490
+ Output is `{ url }`. A remote the repository does not have, or a `worktreePath` that is not a
491
+ repository, throws. `retry: { maxAttempts: 3 }`: every call is read-only.
492
+
493
+ It exists so a workflow body, which may not touch the filesystem or run `git` itself, can learn
494
+ which forge hosts a repository and choose a publication channel accordingly.
495
+
496
+ ## `git.remove-file`
497
+
498
+ Removes one tracked file in a commit of its own on the branch HEAD is on (`git rm`, then
499
+ `git commit -- <path>`), leaving every other change in the working tree uncommitted.
500
+
501
+ | Field | Required | Purpose |
502
+ | -------------- | -------- | ---------------------------------------------------------------------------------------------------------- |
503
+ | `worktreePath` | yes | Absolute path of the git working tree to commit in. |
504
+ | `branch` | yes | The branch HEAD must be on; the step throws without committing when HEAD is detached or on another branch. |
505
+ | `path` | yes | The file's repo-relative path, with the same input checks as `git.show-file`'s. |
506
+ | `message` | yes | The removal commit's message. |
507
+
508
+ Output is `{ sha, removed }`: HEAD after the step, and whether this call made the commit. A file
509
+ HEAD does not track is not an error: the step returns HEAD unchanged with `removed: false`. The
510
+ commit runs the repository's own commit hooks, with `commit.gpgSign=false` as `git.rebase` sets it,
511
+ and the author and committer come from the repository's git config.
512
+
513
+ `retry: { maxAttempts: 3 }`: a retry after a lost result finds the file gone from HEAD and changes
514
+ nothing.
515
+
516
+ ## `git.read-git-dir-file` and `git.write-git-dir-file`
517
+
518
+ Read and write a file at a path relative to the worktree's own git directory, resolved with
519
+ `git rev-parse --absolute-git-dir` run in the worktree. For a linked worktree that is
520
+ `<repo>/.git/worktrees/<name>`, so the file belongs to that worktree alone. Nothing in a git
521
+ directory can be staged or committed, which makes it the place for a hand-off file between an agent
522
+ turn and a later step -- for example `ligeti/pull-request.md` -- without the workflow body doing any
523
+ file I/O.
524
+
525
+ | Field | Steps | Required | Purpose |
526
+ | -------------- | ---------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
527
+ | `worktreePath` | both | yes | Absolute path of the git working tree whose git directory holds the file. |
528
+ | `path` | both | yes | The file's path relative to the git directory, which must be under `ligeti/`. Absolute paths, `..` and `.` segments, backslashes and any path outside `ligeti/` are rejected by the input schema. |
529
+ | `content` | write only | yes | The file's new content, written as UTF-8. |
530
+
531
+ `git.read-git-dir-file` outputs `{ content }`. A file that does not exist throws, naming the full
532
+ path: the caller reads a file an earlier step was supposed to have written, so its absence is a
533
+ failure, not traffic to branch on (ADR-0005). A `path` that names a directory and a `worktreePath`
534
+ that is not a repository throw too.
535
+
536
+ `git.write-git-dir-file` outputs `{ path }`, the absolute path written. It creates missing parent
537
+ directories and replaces an existing file.
538
+
539
+ `retry: { maxAttempts: 3 }` for both: a read is read-only and a write replaces the whole file, so a
540
+ retry after a lost result changes nothing.
@@ -0,0 +1,23 @@
1
+ import { z } from "@ligeti/sdk";
2
+ export declare const GitFastForwardInput: z.ZodObject<{
3
+ worktreePath: z.ZodString;
4
+ branch: z.ZodString;
5
+ remote: z.ZodDefault<z.ZodString>;
6
+ timeoutSeconds: z.ZodDefault<z.ZodNumber>;
7
+ }, z.core.$strip>;
8
+ export type GitFastForwardInput = z.infer<typeof GitFastForwardInput>;
9
+ export declare const GitFastForwardOutput: z.ZodObject<{
10
+ headSha: z.ZodString;
11
+ remoteSha: z.ZodString;
12
+ }, z.core.$strip>;
13
+ export type GitFastForwardOutput = z.infer<typeof GitFastForwardOutput>;
14
+ export declare const gitFastForward: import("@ligeti/sdk").StepDefinition<z.ZodObject<{
15
+ worktreePath: z.ZodString;
16
+ branch: z.ZodString;
17
+ remote: z.ZodDefault<z.ZodString>;
18
+ timeoutSeconds: z.ZodDefault<z.ZodNumber>;
19
+ }, z.core.$strip>, z.ZodObject<{
20
+ headSha: z.ZodString;
21
+ remoteSha: z.ZodString;
22
+ }, z.core.$strip>>;
23
+ //# sourceMappingURL=fast-forward.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fast-forward.d.ts","sourceRoot":"","sources":["../src/fast-forward.ts"],"names":[],"mappings":"AAEA,OAAO,EAAc,CAAC,EAAE,MAAM,aAAa,CAAC;AAe5C,eAAO,MAAM,mBAAmB;;;;;iBAwB9B,CAAC;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEtE,eAAO,MAAM,oBAAoB;;;iBAG/B,CAAC;AACH,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AASxE,eAAO,MAAM,cAAc;;;;;;;;kBAoGzB,CAAC"}