@feigi/fleet-ctl 3.74.0 → 3.74.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feigi/fleet-ctl",
3
- "version": "3.74.0",
3
+ "version": "3.74.1",
4
4
  "description": "Agent fleet: run-team controller, merge bot, PR reviewer, and the ticket pipeline they share",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -159,8 +159,8 @@ function workspaceHash(key) {
159
159
  //
160
160
  // A non-empty string, and nothing more: no trim(). A wholly-whitespace
161
161
  // answer to `--git-common-dir` is already refused one layer down, by
162
- // git-env.mjs's workspaceDirFromGitCommonDir() inside fleet-dir.mjs's
163
- // fleetFile(), so no identity reaching either call site can be whitespace —
162
+ // git-env.mjs's resolveWorkspace() inside fleet-dir.mjs's fleetFile(), so no
163
+ // identity reaching either call site can be whitespace —
164
164
  // the construction side gets the workspace fleetFile() resolved and the
165
165
  // parsing side gets a payload some process wrote from it. A second whitespace
166
166
  // rule here would be one more guard of exactly the kind this predicate exists
@@ -17,8 +17,11 @@
17
17
  # specialists). Two independent readers is what drifts.
18
18
  #
19
19
  # THE CACHE is `<workspace>/.fleet/recipe.json`, where <workspace> is the
20
- # directory holding the repository's COMMON git dir — the main checkout, the
21
- # same place the ledger and the heartbeat live — so a claimed worktree and a
20
+ # directory git-env.mjs's resolveWorkspace() names for the repository: the
21
+ # common git dir's parent when the common dir is named `.git` or that parent's
22
+ # own `.git` resolves to it, otherwise the common git dir itself (a bare
23
+ # repository, a submodule). That is the same place the
24
+ # ledger and the heartbeat live — so a claimed worktree and a
22
25
  # review worktree read the one cache the main checkout holds rather than a
23
26
  # private copy each. A single JSON object:
24
27
  #
@@ -158,15 +161,38 @@ if [ -n "$rev" ]; then
158
161
  || die "--at '$rev' does not resolve to a commit in $repo"
159
162
  fi
160
163
 
161
- # `--path-format=absolute` so the workspace is a real directory whatever the
162
- # caller's cwd; the workspace is the common dir's parent, the same rule
163
- # git-env.mjs's workspaceDirFromGitCommonDir() applies for the ledger.
164
+ # `--path-format=absolute` so the common dir is a real path whatever the
165
+ # caller's cwd. The workspace it names is git-env.mjs's resolveWorkspace() rule,
166
+ # written out below in shell.
164
167
  common=$(git -C "$repo" rev-parse --path-format=absolute --git-common-dir 2>&1) || {
165
168
  gitrc=$?
166
169
  case $gitrc in 128) die "cannot resolve the common git dir of $repo — $common" ;; esac
167
170
  unrunnable "git did not resolve the common git dir of $repo (exit $gitrc), so the Recipe cache was not read${common:+ — $common}"
168
171
  }
169
- cache="${common%/*}/.fleet/recipe.json"
172
+ # The workspace is the rule in git-env.mjs's resolveWorkspace(), written out in
173
+ # shell rather than asked of node: the interpreter probe below must stay the
174
+ # first node call, since its refusals are pinned in that order. A common dir
175
+ # named `.git` belongs to its parent checkout. Otherwise the parent's `.git`,
176
+ # a symlink or a gitfile, that resolves to the common dir makes the parent the
177
+ # workspace. Otherwise the common dir is the workspace itself (a bare
178
+ # repository, a submodule's git dir).
179
+ real=$(cd -P "$common" 2>/dev/null && pwd -P) || real=$common
180
+ parent=${common%/*}
181
+ case ${real##*/} in
182
+ .git) ws=$parent ;;
183
+ *)
184
+ ws=$common
185
+ target=${real%/*}/.git
186
+ if [ -f "$target" ]; then
187
+ IFS= read -r gl < "$target" || :
188
+ gl=${gl#gitdir: }
189
+ case $gl in /*) target=$gl ;; *) target=${real%/*}/$gl ;; esac
190
+ fi
191
+ named=$(cd -P "$target" 2>/dev/null && pwd -P) || named=
192
+ if [ "$named" = "$real" ]; then ws=$parent; fi
193
+ ;;
194
+ esac
195
+ cache="$ws/.fleet/recipe.json"
170
196
 
171
197
  # Absent is its own refusal, ahead of the interpreter probe: it is the one
172
198
  # state every fresh repository starts in, and its message must name the step
@@ -7,11 +7,13 @@
7
7
  // private copy of it.
8
8
  //
9
9
  // The answer is git's: `rev-parse --git-common-dir` names the common git dir
10
- // every worktree of one repository shares, and git-env.mjs's
11
- // workspaceDirFromGitCommonDir() turns that answer into the workspace
12
- // holding it. This module is the spawn around that function — the scrubbed
13
- // env, the bound, the join onto `.fleet/` — so a new `.fleet/`-rooted file is
14
- // one `fleetFile("name")` call rather than another hand-spelled probe.
10
+ // every worktree of one repository shares, and git-env.mjs's resolveWorkspace()
11
+ // turns that answer into the workspace it names: the common git dir's parent
12
+ // when the common dir is named `.git` or that parent's own `.git` resolves to
13
+ // it, otherwise the common git dir itself. This module is the spawn around
14
+ // that function — the scrubbed env, the bound, the join onto `.fleet/` — so a
15
+ // new `.fleet/`-rooted file is one `fleetFile("name")` call rather than
16
+ // another hand-spelled probe.
15
17
  //
16
18
  // One error policy: anything that leaves no workspace THROWS
17
19
  // FleetDirUnresolvable, never a null or an empty path. What a caller does
@@ -32,13 +34,7 @@ import { createHash } from "node:crypto";
32
34
  import { existsSync } from "node:fs";
33
35
  import { homedir } from "node:os";
34
36
  import { basename, dirname, join } from "node:path";
35
- import { gitEnv, workspaceDirFromGitCommonDir } from "./git-env.mjs";
36
-
37
- // A `rev-parse` answers in milliseconds; this bound exists so a git that
38
- // never answers cannot hang its caller, not to time a healthy one. A caller
39
- // with an override of its own (ledger.mjs's LEDGER_GIT_TIMEOUT) passes the
40
- // value it settled on as `timeoutMs`.
41
- const DEFAULT_GIT_TIMEOUT_MS = 10_000;
37
+ import { DEFAULT_GIT_TIMEOUT_MS, WorkspaceProbeFailed, gitEnv, resolveWorkspace } from "./git-env.mjs";
42
38
 
43
39
  // The cause reaches stderr inside a single warning line, so it is capped,
44
40
  // keeping the END — the part of a child's stderr that says why it stopped.
@@ -73,6 +69,33 @@ function ended(r) {
73
69
  return r.status === null ? "" : `exit ${r.status}`;
74
70
  }
75
71
 
72
+ // The probe every `.fleet/` answer and the scratch root share: git's common
73
+ // dir, then the workspace it names. Every failure throws FleetDirUnresolvable
74
+ // with git's own reason.
75
+ function workspaceOf(cwd, timeoutMs, canonicalise) {
76
+ const r = spawnSync("git", ["rev-parse", "--git-common-dir"], {
77
+ cwd, encoding: "utf8", timeout: timeoutMs ?? DEFAULT_GIT_TIMEOUT_MS, env: gitEnv(),
78
+ });
79
+ // No separate `r.status === 0` gate: every failure mode (no repository, an
80
+ // unresolvable GIT_DIR, a permission-denied `.git`, a corrupt worktree
81
+ // pointer, a missing `git` binary, a timed-out probe) leaves `r.stdout`
82
+ // empty, which resolveWorkspace() reads as `null` on its own.
83
+ let found;
84
+ try {
85
+ found = resolveWorkspace(r.stdout, cwd, { canonicalise, timeoutMs });
86
+ } catch (e) {
87
+ // `--git-common-dir` answered; the rule's own probe is what failed, and
88
+ // its message names that probe.
89
+ if (!(e instanceof WorkspaceProbeFailed)) throw e;
90
+ throw new FleetDirUnresolvable(`could not resolve the workspace: ${e.message}`);
91
+ }
92
+ if (found === null) {
93
+ const why = cause(r.error && r.error.message, r.stderr) || ended(r);
94
+ throw new FleetDirUnresolvable(`could not resolve --git-common-dir${why ? `: ${why}` : ""}`);
95
+ }
96
+ return found;
97
+ }
98
+
76
99
  /**
77
100
  * The absolute path of `name` under the run's `.fleet/` directory, resolved
78
101
  * from `cwd` — or the `.fleet/` directory itself when `name` is null.
@@ -81,7 +104,7 @@ function ended(r) {
81
104
  * makes `--git-common-dir` answer for a DIFFERENT repository, which would put
82
105
  * the run's file in someone else's workspace, silently, at exit 0.
83
106
  *
84
- * `canonicalise` is workspaceDirFromGitCommonDir()'s opt-in, passed through:
107
+ * `canonicalise` is git-env.mjs's resolveWorkspace() opt-in, passed through:
85
108
  * only a caller that keys something on the path (the cockpit's port) takes
86
109
  * it. Everyone else prints the path it resolves, and realpath would change
87
110
  * that spelling without changing which file it names.
@@ -91,19 +114,8 @@ function ended(r) {
91
114
  * `timeoutMs` — with git's own reason in the message.
92
115
  */
93
116
  export function fleetFile(name, { cwd = process.cwd(), timeoutMs, canonicalise = false } = {}) {
94
- const r = spawnSync("git", ["rev-parse", "--git-common-dir"], {
95
- cwd, encoding: "utf8", timeout: timeoutMs ?? DEFAULT_GIT_TIMEOUT_MS, env: gitEnv(),
96
- });
97
- // No separate `r.status === 0` gate: every failure mode (no repository, an
98
- // unresolvable GIT_DIR, a permission-denied `.git`, a corrupt worktree
99
- // pointer, a missing `git` binary, a timed-out probe) leaves `r.stdout`
100
- // empty, which workspaceDirFromGitCommonDir() reads as `null` on its own.
101
- const workspace = workspaceDirFromGitCommonDir(r.stdout, cwd, { canonicalise });
102
- if (workspace === null) {
103
- const why = cause(r.error && r.error.message, r.stderr) || ended(r);
104
- throw new FleetDirUnresolvable(`could not resolve --git-common-dir${why ? `: ${why}` : ""}`);
105
- }
106
- return name === null ? join(workspace, ".fleet") : join(workspace, ".fleet", name);
117
+ const { dir } = workspaceOf(cwd, timeoutMs, canonicalise);
118
+ return name === null ? join(dir, ".fleet") : join(dir, ".fleet", name);
107
119
  }
108
120
 
109
121
  /**
@@ -113,8 +125,9 @@ export function fleetFile(name, { cwd = process.cwd(), timeoutMs, canonicalise =
113
125
  * point that has no dispatcher to name one.
114
126
  *
115
127
  * `<repo-key>` is `<basename>-<8 hex>`. `<basename>` is the folder name of the
116
- * workspace — the directory holding the COMMON git dir, the same lookup
117
- * fleetFile() makes, so a linked worktree maps to its main checkout — and the
128
+ * workspace fleetFile() resolves — a checkout's directory, so a linked worktree
129
+ * maps to its main checkout, or a bare repository's or submodule's git dir
130
+ * (a trailing `.git` on that name is dropped) — and the
118
131
  * hex is the first 8 characters of the sha256 of that directory's realpath,
119
132
  * so two clones that share a name get different roots. Both come from the
120
133
  * canonical form, so a symlinked route to one workspace is not a second root.
@@ -123,9 +136,10 @@ export function fleetFile(name, { cwd = process.cwd(), timeoutMs, canonicalise =
123
136
  * FleetDirUnresolvable exactly as fleetFile() does.
124
137
  */
125
138
  export function scratchRoot({ cwd = process.cwd(), timeoutMs, home = homedir() } = {}) {
126
- const workspace = dirname(fleetFile(null, { cwd, timeoutMs, canonicalise: true }));
127
- const hex = createHash("sha256").update(workspace).digest("hex").slice(0, 8);
128
- return join(scratchParent(home), `${basename(workspace)}-${hex}`);
139
+ const { dir, gitDir } = workspaceOf(cwd, timeoutMs, true);
140
+ const folder = basename(dir);
141
+ const hex = createHash("sha256").update(dir).digest("hex").slice(0, 8);
142
+ return join(scratchParent(home), `${gitDir ? folder.replace(/\.git$/, "") : folder}-${hex}`);
129
143
  }
130
144
 
131
145
  /** The directory every run's scratch root sits directly in: `<home>/.fleet/scratch`. */
@@ -1,9 +1,11 @@
1
- // Shared by every `.mjs` script under this directory that shells out to git —
2
- // two exports, one per half of the same spawn. `gitEnv()` builds the env the
3
- // git child is given; `workspaceDirFromGitCommonDir()` turns what the most
4
- // common of those children answers with, `rev-parse --git-common-dir`, into
5
- // the workspace directory a run's files live under. The first half's
6
- // reasoning is below; the second's sits beside its own function.
1
+ // Shared by every `.mjs` script under this directory that shells out to git.
2
+ // `gitEnv()` builds the env a git child is given. The rest turns what the
3
+ // most common of those children answers with, `rev-parse --git-common-dir`,
4
+ // into a directory: `resolveWorkspace()` (and `workspaceDirFromGitCommonDir()`,
5
+ // its directory alone) names where a run's files live, spawning one more
6
+ // probe of its own when the answer needs it; `mainCheckoutDirFromGitCommonDir()`
7
+ // names the main checkout's work tree. `gitEnv()`'s reasoning is below; the
8
+ // rest sits beside its own functions.
7
9
  //
8
10
  // GIT_DIR and GIT_WORK_TREE outrank both the child's cwd and an explicit `-C`
9
11
  // argument — measured directly: a `git rev-parse`/`ls-files` call
@@ -29,9 +31,6 @@
29
31
  // would be churn with no behavioural change. The suite's census of
30
32
  // ambient-var scrubs carries it by name instead of by import.
31
33
 
32
- import { realpathSync } from "node:fs";
33
- import { dirname, resolve } from "node:path";
34
-
35
34
  /**
36
35
  * `base` (defaulting to `process.env`) with GIT_DIR and GIT_WORK_TREE
37
36
  * removed, so a git child spawned with the result cannot be retargeted by
@@ -55,61 +54,154 @@ export function gitEnv(overrides = {}, base = process.env) {
55
54
  // answer the same way, because the answer is the same shape wherever it is
56
55
  // asked: the common dir is shared by every worktree of one repository (git
57
56
  // answers with the MAIN checkout's git dir from inside a linked worktree), so
58
- // its PARENT is the one workspace a run has, whichever worktree asked. That
59
- // is why a member running from `.worktrees/<n>-slug` and the controller
60
- // running from the checkout root agree about the run's single ledger, its
61
- // single heartbeat and its single board.
57
+ // every worktree of one repository resolves to ONE workspace, whichever
58
+ // worktree asked. That is why a member running from `.worktrees/<n>-slug` and
59
+ // the controller running from the checkout root agree about the run's single
60
+ // ledger, its single heartbeat and its single board.
61
+ //
62
+ // The workspace is the common dir's parent when the common dir is named
63
+ // `.git` or when that parent's own `.git` resolves to it (the `.bare`
64
+ // layout's gitfile); otherwise it is the common dir itself — a bare
65
+ // repository's, a submodule's or a `--separate-git-dir` clone's git dir — so
66
+ // two such dirs sharing a parent do not collapse onto one workspace. The
67
+ // rule's full statement sits beside `resolveWorkspace()` below. The main
68
+ // checkout's work tree is a separate question with its own answer,
69
+ // `mainCheckoutDirFromGitCommonDir()`: a `.git` symlinked to a git dir
70
+ // elsewhere moves the workspace out of the checkout, never the work tree.
62
71
  //
63
72
  // For a run's `.fleet/` files the question is asked in one place,
64
- // fleet-dir.mjs's fleetFile(), which spawns the probe and hands its answer to
65
- // this function. What is NOT shared is the part each caller owns: the
73
+ // fleet-dir.mjs's private workspaceOf(), which spawns `--git-common-dir`,
74
+ // hands the answer to `resolveWorkspace()` and serves both fleetFile() and
75
+ // scratchRoot(). What is NOT shared is the part each caller owns: the
66
76
  // filename it joins on, the wording of the warning it degrades with, and what
67
- // its own degraded answer is. Hence the seam: this function answers the
68
- // directory or `null`, and every caller composes the rest around it.
77
+ // its own degraded answer is. Hence the seam: this module answers
78
+ // `{dir, gitDir}` or `null`, or throws WorkspaceProbeFailed, and every caller
79
+ // composes the rest around it.
80
+
81
+ import { spawnSync } from "node:child_process";
82
+ import { realpathSync } from "node:fs";
83
+ import { basename, dirname, join, resolve } from "node:path";
84
+
85
+ // A `rev-parse` answers in milliseconds; this bound exists so a git that never
86
+ // answers cannot hang its caller, not to time a healthy one.
87
+ export const DEFAULT_GIT_TIMEOUT_MS = 10_000;
88
+
89
+ // Thrown when the one probe the rule needs beyond `--git-common-dir` could not
90
+ // run to an answer: git missing, or killed at its bound. Not thrown for a git
91
+ // that ran and refused — that is an answer, and the rule reads it as "no".
92
+ export class WorkspaceProbeFailed extends Error {
93
+ constructor(message) {
94
+ super(message);
95
+ this.name = "WorkspaceProbeFailed";
96
+ }
97
+ }
98
+
99
+ /** The realpath of `path`, or `path` itself when it does not resolve. */
100
+ function realpathOr(path) {
101
+ try { return realpathSync(path); } catch { return path; }
102
+ }
103
+
104
+ /** The realpath of `path`; when `path` itself does not exist, the realpath of its parent joined with its basename. */
105
+ function canonicalPath(path) {
106
+ try { return realpathSync(path); } catch { return join(realpathOr(dirname(path)), basename(path)); }
107
+ }
108
+
109
+ // Why a probe that did not answer did not: the spawn error, the signal that
110
+ // killed it, or its stderr. Its caller reaches it only for a spawn error or a
111
+ // probe with no exit status, so no exit code is ever left to name.
112
+ function probeWhy(r) {
113
+ if (r.error && r.error.message) return r.error.message;
114
+ if (r.signal) return `killed by ${r.signal}`;
115
+ return String(r.stderr ?? "").trim() || "no answer";
116
+ }
117
+
118
+ // Does `<parent>/.git` name `gitDir` (a realpath)? `--resolve-git-dir` follows
119
+ // a gitfile, so the `.bare` layout's `.git` file answers with `.bare`.
120
+ function parentGitDirNames(parent, gitDir, cwd, timeoutMs) {
121
+ const candidate = join(parent, ".git");
122
+ const r = spawnSync("git", ["rev-parse", "--resolve-git-dir", candidate], {
123
+ cwd, encoding: "utf8", timeout: timeoutMs ?? DEFAULT_GIT_TIMEOUT_MS, env: gitEnv(),
124
+ });
125
+ if (r.error || r.status === null) {
126
+ throw new WorkspaceProbeFailed(`could not resolve ${candidate}: ${probeWhy(r)}`);
127
+ }
128
+ if (r.status !== 0) return false;
129
+ return realpathOr(resolve(parent, r.stdout.trim())) === gitDir;
130
+ }
69
131
 
70
132
  /**
71
- * The workspace directory a `git rev-parse --git-common-dir` answer names —
72
- * the directory HOLDING the common git dir — resolved against `cwd`, or
73
- * `null` when the answer carries nothing usable.
133
+ * The workspace a `git rev-parse --git-common-dir` answer names, resolved
134
+ * against `cwd`, plus `gitDir`: true when the workspace IS the common git dir,
135
+ * false when it is the directory holding a checkout's `.git`. `null` when the
136
+ * answer is empty.
137
+ *
138
+ * The rule, on the common dir `C` (the realpath of the answer):
139
+ * 1. `C` is named `.git`: the workspace is its parent. No spawn.
140
+ * 2. Otherwise `<parent of C>/.git` is probed with `--resolve-git-dir`. If it
141
+ * names `C`, the workspace is that parent (the `.bare` layout). A git that
142
+ * refuses, or names something else, is "no".
143
+ * 3. Otherwise the workspace is `C` itself, with `gitDir: true`.
144
+ *
145
+ * `gitDir` is what the scratch root's key reads: a git dir's `.git` suffix
146
+ * (`x.git`) is dropped from the key; a checkout's folder name is not touched,
147
+ * so a plain clone in a folder named `x.git` keeps its key.
74
148
  *
75
- * `null` is the signal every caller's degrade arm branches on, and the reason
76
- * this returns it rather than a cwd-relative fallback of its own: what a
77
- * degraded answer should be is the caller's to say (fleet-dir.mjs turns it
78
- * into a thrown error its own callers each degrade from in their own way),
79
- * and so is the warning that must accompany it — a degrade nobody announces
80
- * is the failure class those warnings exist to close.
149
+ * The answer is resolved against `cwd` (default `process.cwd()`): git answers
150
+ * RELATIVE — `.git` — from a checkout's top level.
81
151
  *
82
- * `resolve(cwd, …)` rather than resolve()'s implicit `process.cwd()`: git
83
- * answers this RELATIVE — a bare `.git` — when it runs from a checkout's top
84
- * level, and the cwd that was relative to is an argument here, not ambient,
85
- * so a caller that spawned git in a cwd of its choosing (fleet-dir.mjs's
86
- * `cwd` option) resolves the answer against that same cwd. `cwd` defaults to
87
- * `process.cwd()`, which is what a caller that spawns git without one
88
- * resolved against.
152
+ * Return spelling, when `canonicalise` is off: the path as the answer spelled
153
+ * it, resolved against `cwd`. `canonicalise: true` returns the realpath form.
154
+ * The cockpit's key needs the canonical form; the ledger and the heartbeat
155
+ * print the path they resolve, so they keep the spelling. The probe itself
156
+ * always works from the realpath, so a symlinked `.git` and the checkout it
157
+ * serves agree on the shape.
89
158
  *
90
- * The `trim()` is what makes a newline-only answer the `null` case rather
91
- * than a path; a trailing newline riding on a real answer would be discarded
92
- * by `dirname()` anyway, together with the rest of the final segment.
159
+ * A path that does not resolve is not a failure: the caller gets back the
160
+ * uncanonicalised path.
161
+ *
162
+ * Throws WorkspaceProbeFailed only when the step-2 probe could not run to an
163
+ * answer.
164
+ */
165
+ export function resolveWorkspace(gitCommonDir, cwd = process.cwd(), { canonicalise = false, timeoutMs } = {}) {
166
+ const common = String(gitCommonDir ?? "").trim();
167
+ if (!common) return null;
168
+ const spelled = resolve(cwd, common);
169
+ const real = canonicalPath(spelled);
170
+ const chosen = canonicalise ? real : spelled;
171
+ if (basename(real) === ".git" || parentGitDirNames(dirname(real), real, cwd, timeoutMs)) {
172
+ return { dir: dirname(chosen), gitDir: false };
173
+ }
174
+ return { dir: chosen, gitDir: true };
175
+ }
176
+
177
+ /** `resolveWorkspace()`'s directory, without its shape. Same rule, same throw. */
178
+ export function workspaceDirFromGitCommonDir(gitCommonDir, cwd = process.cwd(), options = {}) {
179
+ return resolveWorkspace(gitCommonDir, cwd, options)?.dir ?? null;
180
+ }
181
+
182
+ /**
183
+ * The main checkout's WORK TREE a `git rev-parse --git-common-dir` answer
184
+ * names: the answer's parent, resolved against `cwd`. `null` when the answer
185
+ * is empty. No spawn, no throw.
93
186
  *
94
- * `canonicalise` is OPT-IN, and deliberately so. The cockpit takes it,
95
- * through fleet-dir.mjs: a symlinked route to one workspace derives a SECOND
96
- * port and a second state directory for a cockpit already being served, so
97
- * the cockpit's key has to be the canonical form. The ledger and the
98
- * heartbeat do not — both PRINT the path they resolve (the ledger in
99
- * `check`'s JSON, both in their degrade warnings), and realpath would change
100
- * that output without changing which file they reach, since the canonical and
101
- * symlinked spellings of one path are one file. Left off, this function reads
102
- * nothing: `canonicalise: true` is its only filesystem access.
187
+ * This is a different question from `resolveWorkspace()`'s, which asks where
188
+ * a repository's stores live, and the two answers part for a `.git`
189
+ * symlinked to a git dir elsewhere: asked from that checkout, git spells the
190
+ * common dir through the link (`.git`, `../.git`), so this answers the
191
+ * folder holding the link, while the store rule follows the link out of the
192
+ * checkout. A caller that runs `git status` in the main checkout, hashes its
193
+ * files or guards writes into it asks this one. From a linked worktree of
194
+ * such a checkout git answers with the link's target instead, so its parent
195
+ * is not the checkout — a known limit of this formula. In a bare, submodule
196
+ * or `--separate-git-dir` layout the parent is not a work tree at all;
197
+ * running a run from one of those is not supported.
103
198
  *
104
- * A path that does not resolve is not a failure when it is asked for: the
105
- * caller gets a usable key back, just an uncanonicalised one. A workspace
106
- * directory removed out from under a running cockpit must not turn resolution
107
- * into a throw.
199
+ * `canonicalise: true` returns the realpath form, or the path as resolved
200
+ * when it does not resolve.
108
201
  */
109
- export function workspaceDirFromGitCommonDir(gitCommonDir, cwd = process.cwd(), { canonicalise = false } = {}) {
202
+ export function mainCheckoutDirFromGitCommonDir(gitCommonDir, cwd = process.cwd(), { canonicalise = false } = {}) {
110
203
  const common = String(gitCommonDir ?? "").trim();
111
204
  if (!common) return null;
112
- const workspace = dirname(resolve(cwd, common));
113
- if (!canonicalise) return workspace;
114
- try { return realpathSync(workspace); } catch { return workspace; }
205
+ const dir = dirname(resolve(cwd, common));
206
+ return canonicalise ? realpathOr(dir) : dir;
115
207
  }
@@ -10,7 +10,7 @@
10
10
  //
11
11
  // THE BASELINE. Recorded once per run, in Phase 0 beside `instruments.sh
12
12
  // --pin` (after the fast-forward, before anything reads), at
13
- // `<workspace>/.fleet/main-checkout.sha` — instruments.sh's own
13
+ // `<main checkout>/.fleet/main-checkout.sha` (ROOT below) — instruments.sh's own
14
14
  // `.fleet/instruments.sha` precedent. It holds the main checkout's
15
15
  // `git status --porcelain -uall` entries AND a content hash of every path
16
16
  // they name, so the maintainer's own uncommitted work at run start is allowed
@@ -102,10 +102,11 @@
102
102
  // any ancestry read.
103
103
  //
104
104
  // ROOT. The main checkout is the parent of `git rev-parse --git-common-dir`
105
- // (git-env.mjs's `workspaceDirFromGitCommonDir`, as fleet-tick.mjs resolves
106
- // the shortlist) — never `--show-toplevel`, which answers a WORKTREE's root
107
- // from inside one — and every git child gets `gitEnv()`, so an ambient
108
- // GIT_DIR/GIT_WORK_TREE cannot answer for another repository.
105
+ // (git-env.mjs's `mainCheckoutDirFromGitCommonDir`) — the work tree, which a
106
+ // symlinked `.git` does not move, never git-env.mjs's store rule — and never
107
+ // `--show-toplevel`, which answers a WORKTREE's root from inside one. Every
108
+ // git child gets `gitEnv()`, so an ambient GIT_DIR/GIT_WORK_TREE cannot answer
109
+ // for another repository.
109
110
  //
110
111
  // CLI:
111
112
  // main-checkout.mjs --record write the baseline (Phase 0; or the
@@ -122,7 +123,7 @@ import { lstatSync, mkdirSync, readFileSync, readlinkSync, renameSync, writeFile
122
123
  import { join } from "node:path";
123
124
  import { isCLI } from "./is-cli.mjs";
124
125
  import { makeDie, defineFlags } from "./arg.mjs";
125
- import { gitEnv, workspaceDirFromGitCommonDir } from "./git-env.mjs";
126
+ import { gitEnv, mainCheckoutDirFromGitCommonDir } from "./git-env.mjs";
126
127
 
127
128
  export const BASELINE_FILE = "main-checkout.sha";
128
129
  export const RECORD_COMMAND = "~/.fleet/bin/fleet-run main-checkout.mjs --record";
@@ -157,7 +158,7 @@ function git(what, args, cwd, env) {
157
158
  export function resolveMainCheckout(cwd = process.cwd(), env = process.env) {
158
159
  const r = git("git rev-parse --git-common-dir", ["rev-parse", "--git-common-dir"], cwd, env);
159
160
  if (!r.ok) return r;
160
- const root = workspaceDirFromGitCommonDir(r.stdout, cwd);
161
+ const root = mainCheckoutDirFromGitCommonDir(r.stdout, cwd);
161
162
  return root === null ? { ok: false, why: "git rev-parse --git-common-dir answered nothing" } : { ok: true, root };
162
163
  }
163
164
 
@@ -57,10 +57,10 @@
57
57
  // `decide()` is the whole policy and touches nothing: the handler resolves the
58
58
  // main checkout, then hands it and a `check-ignore` probe in. The main
59
59
  // checkout is the parent of `git rev-parse --git-common-dir` (git-env.mjs's
60
- // `workspaceDirFromGitCommonDir`, as `fleet-tick.mjs` resolves the run's
61
- // workspace) — never `--show-toplevel`, which answers a WORKTREE's root from
62
- // inside one — and every git child gets `gitEnv()`, so an ambient GIT_DIR
63
- // cannot answer for another repository.
60
+ // `mainCheckoutDirFromGitCommonDir`) — the work tree, which a symlinked `.git`
61
+ // does not move, never git-env.mjs's store rule — and never `--show-toplevel`,
62
+ // which answers a WORKTREE's root from inside one. Every git child gets
63
+ // `gitEnv()`, so an ambient GIT_DIR cannot answer for another repository.
64
64
  //
65
65
  // Zero deps: node builtins and sibling scripts only.
66
66
 
@@ -69,7 +69,7 @@ import { realpathSync } from "node:fs";
69
69
  import { homedir } from "node:os";
70
70
  import { basename, dirname, isAbsolute, join, resolve, sep } from "node:path";
71
71
  import { fileURLToPath } from "node:url";
72
- import { gitEnv, workspaceDirFromGitCommonDir } from "./git-env.mjs";
72
+ import { gitEnv, mainCheckoutDirFromGitCommonDir } from "./git-env.mjs";
73
73
  import { CELL_DEF } from "./ledger-grammar.mjs";
74
74
 
75
75
  /** The task names the controller gives fleet members; a subagent whose parent carries one is a member's child, and a member whose own id matches this is caught directly, whatever definition name its dispatch gave it. */
@@ -238,7 +238,7 @@ async function resolveMainRoot(cwd) {
238
238
  // by the dead path rather than "(or any …": that is a broken repo, and blocks.
239
239
  if (!r.error && r.status !== 0 && /not a git repository \(or any/i.test(r.stderr)) return null;
240
240
  if (r.error || r.status !== 0) return new Error(failure(r, "git rev-parse --git-common-dir"));
241
- return workspaceDirFromGitCommonDir(r.stdout, cwd, { canonicalise: true })
241
+ return mainCheckoutDirFromGitCommonDir(r.stdout, cwd, { canonicalise: true })
242
242
  ?? new Error("git rev-parse --git-common-dir answered nothing");
243
243
  }
244
244
 
@@ -81,7 +81,7 @@ import { mkdirSync, writeFileSync } from "node:fs";
81
81
  import { dirname, join } from "node:path";
82
82
  import { fileURLToPath } from "node:url";
83
83
  import { makeDie, defineFlags, writeAll } from "./arg.mjs";
84
- import { gitEnv, workspaceDirFromGitCommonDir } from "./git-env.mjs";
84
+ import { gitEnv, mainCheckoutDirFromGitCommonDir } from "./git-env.mjs";
85
85
 
86
86
  const NAME = "merge-gate";
87
87
 
@@ -214,8 +214,9 @@ const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v)
214
214
  const CHILD = { encoding: "utf8", stdio: ["ignore", "pipe", "inherit"] };
215
215
 
216
216
  // --- instruments.sh -------------------------------------------------------
217
- // `--repo` is the directory holding the git COMMON dir — the main checkout,
218
- // where the run's baseline `.fleet/instruments.sha` lives — never the cwd's
217
+ // `--repo` is the main checkout — the directory holding the git COMMON dir,
218
+ // git-env.mjs's `mainCheckoutDirFromGitCommonDir` — where the run's baseline
219
+ // `.fleet/instruments.sha` lives, never the cwd's
219
220
  // own top level: from a worktree that has no baseline, and instruments.sh
220
221
  // exits 2 on a missing baseline instead of comparing anything. gitEnv():
221
222
  // an ambient GIT_DIR would otherwise answer `--git-common-dir` for a
@@ -226,7 +227,7 @@ const CHILD = { encoding: "utf8", stdio: ["ignore", "pipe", "inherit"] };
226
227
  // `root` is returned too: main-gain.mjs reads the same main checkout.
227
228
  function readInstruments() {
228
229
  const common = spawnSync("git", ["rev-parse", "--git-common-dir"], { ...CHILD, env: gitEnv() });
229
- const root = common.error || common.status !== 0 ? null : workspaceDirFromGitCommonDir(common.stdout);
230
+ const root = !common.error && common.status === 0 ? mainCheckoutDirFromGitCommonDir(common.stdout) : null;
230
231
  if (root === null) return { exit: null, digest: null, root };
231
232
  const r = spawnSync("sh", [join(SCRIPT_DIR, "instruments.sh"), "--repo", root], CHILD);
232
233
  const digest = (r.stdout ?? "").trim().split("\n")[0] || null;
@@ -53,7 +53,7 @@
53
53
  // the bound and the command's log.
54
54
  //
55
55
  // On proof, the cache is written atomically to <workspace>/.fleet/recipe.json
56
- // — <workspace> being the directory holding the repository's common git dir,
56
+ // — <workspace> being the directory workspaceDirFromGitCommonDir() names for the repository,
57
57
  // the place derive-testcmd.sh reads it from — and then read back through
58
58
  // derive-testcmd.sh for every field, `uncovered` byte for byte as the compact
59
59
  // JSON of the list given, the Test entrypoint against `derivedAt`'s
@@ -87,7 +87,7 @@ import { tmpdir } from "node:os";
87
87
  import { dirname, join } from "node:path";
88
88
  import { fileURLToPath } from "node:url";
89
89
  import { isCLI } from "./is-cli.mjs";
90
- import { gitEnv, workspaceDirFromGitCommonDir } from "./git-env.mjs";
90
+ import { WorkspaceProbeFailed, gitEnv, workspaceDirFromGitCommonDir } from "./git-env.mjs";
91
91
  import { isDigits } from "./arg.mjs";
92
92
 
93
93
  const NAME = "recipe-prove";
@@ -454,7 +454,11 @@ function main(argv) {
454
454
  const gitDir = git(["rev-parse", "--git-dir"], o.repo);
455
455
  if (!gitDir.ok) throw cannot(`${o.repo} is not a git repository: ${gitDir.err}`);
456
456
  const common = git(["rev-parse", "--path-format=absolute", "--git-common-dir"], o.repo);
457
- const workspace = common.ok ? workspaceDirFromGitCommonDir(common.out, o.repo) : null;
457
+ let workspace = null;
458
+ if (common.ok) {
459
+ try { workspace = workspaceDirFromGitCommonDir(common.out, o.repo); }
460
+ catch (e) { if (!(e instanceof WorkspaceProbeFailed)) throw e; throw cannot(`cannot resolve the common git dir of ${o.repo}: ${e.message}`); }
461
+ }
458
462
  if (!workspace) throw cannot(`cannot resolve the common git dir of ${o.repo}: ${common.err}`);
459
463
  const sha = git(["rev-parse", "--verify", "origin/main^{commit}"], o.repo);
460
464
  if (!sha.ok) throw cannot(`origin/main does not resolve to a commit (${sha.err}) — fetch it; the proof runs against it`);
@@ -5,7 +5,8 @@
5
5
  //
6
6
  // `~/.fleet/scratch/<repo-key>/`, computed by fleet-dir.mjs's scratchRoot() —
7
7
  // the same path from the main checkout and from any linked worktree, a
8
- // different one for a second clone with the same folder name. Nothing is
8
+ // different one for each workspace — a second clone, a bare repository, a
9
+ // submodule — even where folder names collide. Nothing is
9
10
  // written to `.fleet/` or anywhere else to record it: the controller (and any
10
11
  // standalone entry point with no dispatcher) runs this again and gets the same
11
12
  // answer. The controller writes the printed path, absolute, into the prompt of