@bakers-build/addon-herdr 0.11.1-next.8 → 0.11.1-next.9

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.
@@ -28,7 +28,10 @@ export interface HerdrWorkspaceOptions {
28
28
  * in `root`, and give runs `push.default=current`. `true` leaves Git alone.
29
29
  */
30
30
  autoSetupMerge?: boolean;
31
- /** The runtime isolates: a fresh run clone per ensure instead of a herdr worktree. */
31
+ /**
32
+ * The runtime isolates: a fresh run clone per ensure instead of a herdr
33
+ * worktree. The default mode; a run's `clone` overrides it.
34
+ */
32
35
  clone?: boolean;
33
36
  /** Run clones borrow the root's objects (`git clone --shared`). */
34
37
  shared?: boolean;
@@ -62,6 +65,12 @@ export declare class HerdrWorkspace implements BbWorkspace {
62
65
  * ticket branch; the host's `.git` there is only the stub that links to it.
63
66
  */
64
67
  private ensureClone;
68
+ /**
69
+ * A host run after an isolated one: close the clone's herdr workspace, move
70
+ * its commits to the ticket branch and remove it, so a worktree takes the
71
+ * path. Uncommitted work refuses, stays and gets its workspace back.
72
+ */
73
+ private leaveClone;
65
74
  /**
66
75
  * Tear down the branch's worktree and its herdr workspace
67
76
  * (`herdr worktree remove --force`); a worktree herdr no longer tracks is
@@ -1,11 +1,11 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import { resolve } from 'node:path';
3
- import { ensureRunClone, loadInstructions, runBranchGit, targetOf, } from '@bakers-build/addon-workspace-git';
4
- import { exec } from '@bakers-build/core';
3
+ import { ensureRunClone, leaveRunClone, loadInstructions, runBranchGit, targetOf, } from '@bakers-build/addon-workspace-git';
4
+ import { exec, loadRunClone } from '@bakers-build/core';
5
5
  import { isRecord, parseJson } from './pane-layout.js';
6
6
  import { deriveHerdrRoot } from './root-anchor.js';
7
7
  import { resolveBaseRef } from './shared.js';
8
- import { openCloneWorkspace, removeHerdrWorktree } from './worktrees.js';
8
+ import { closeCloneWorkspace, openCloneWorkspace, removeHerdrWorktree, } from './worktrees.js';
9
9
  const defaultExec = (args) => exec('herdr', args);
10
10
  const defaultGitExec = (args) => exec('git', args);
11
11
  const defaultRunGit = (args, cwd) => exec('git', args, { cwd });
@@ -79,18 +79,18 @@ export class HerdrWorkspace {
79
79
  if (branch === undefined) {
80
80
  throw new Error('herdr workspace requires a branch name');
81
81
  }
82
- // shortcut: clone mode is fixed per herdr workspace; switch per run like the git workspace when a herdr machine needs a mixed runtime catalog.
83
- if (options?.clone !== undefined && options.clone !== !!this.options.clone)
84
- throw new Error('herdr workspace: the run runtime clone mode differs from the workspace; a herdr machine cannot mix isolating and host runtimes');
82
+ const clone = options?.clone ?? !!this.options.clone;
85
83
  const extra = this.options.autoSetupMerge
86
84
  ? {}
87
85
  : { env: await runBranchGit(this.root, this.logger) };
88
- if (this.options.clone) {
86
+ if (clone) {
87
+ // A host worktree at the path becomes the clone (`ensureRunClone`).
89
88
  return {
90
89
  ...(await this.ensureClone(branch, baseRefOverride)),
91
90
  ...extra,
92
91
  };
93
92
  }
93
+ await this.leaveClone(branch);
94
94
  const worktrees = parseWorktreeList(await this.execHerdr(['worktree', 'list', '--cwd', this.root, '--json']));
95
95
  const existing = worktrees.find((w) => w.branch === branch);
96
96
  if (existing) {
@@ -207,6 +207,32 @@ export class HerdrWorkspace {
207
207
  await openCloneWorkspace(this.execHerdr, this.root, branch, path);
208
208
  return { path, instructions: loadInstructions(path), created, clone };
209
209
  }
210
+ /**
211
+ * A host run after an isolated one: close the clone's herdr workspace, move
212
+ * its commits to the ticket branch and remove it, so a worktree takes the
213
+ * path. Uncommitted work refuses, stays and gets its workspace back.
214
+ */
215
+ async leaveClone(branch) {
216
+ const path = resolve(this.root, this.worktreeDir, branch);
217
+ if (!loadRunClone(path))
218
+ return;
219
+ const deps = {
220
+ root: this.root,
221
+ execHerdr: this.execHerdr,
222
+ runGit: this.runGit,
223
+ logger: this.logger,
224
+ };
225
+ if (!(await closeCloneWorkspace(deps, branch, path)))
226
+ throw new Error(`herdr workspace: ${path} is focused and no parent workspace can take focus; switch focus and dispatch again`);
227
+ try {
228
+ leaveRunClone(path);
229
+ }
230
+ catch (err) {
231
+ // A refusal keeps the clone: give its workspace back before reporting.
232
+ await openCloneWorkspace(this.execHerdr, this.root, branch, path).catch(() => undefined);
233
+ throw err;
234
+ }
235
+ }
210
236
  /**
211
237
  * Tear down the branch's worktree and its herdr workspace
212
238
  * (`herdr worktree remove --force`); a worktree herdr no longer tracks is
@@ -58,6 +58,11 @@ export interface HerdrTeardownDeps {
58
58
  runGit: WorktreeGitExec;
59
59
  logger: ConductorLogger;
60
60
  }
61
+ /**
62
+ * Close the run clone's open herdr workspace before its directory goes.
63
+ * `false` when it is focused and no parent workspace can take focus (BB-166).
64
+ */
65
+ export declare function closeCloneWorkspace(deps: HerdrTeardownDeps, branch: string, path: string): Promise<boolean>;
61
66
  /**
62
67
  * Tear down a ticket's entire worktree (git worktree + hosted workspace).
63
68
  * `herdr worktree remove` is keyed by an open workspace id, so resolve the
@@ -176,6 +176,19 @@ async function ensureFocusSafeToRemove(deps, workspaceId) {
176
176
  const targetNow = after.find((w) => w.workspaceId === workspaceId);
177
177
  return parentNow?.focused === true && targetNow?.focused !== true;
178
178
  }
179
+ /**
180
+ * Close the run clone's open herdr workspace before its directory goes.
181
+ * `false` when it is focused and no parent workspace can take focus (BB-166).
182
+ */
183
+ export async function closeCloneWorkspace(deps, branch, path) {
184
+ const open = await cloneWorkspaceId(deps.execHerdr, deps.root, branch, path);
185
+ if (!open)
186
+ return true;
187
+ if (!(await ensureFocusSafeToRemove(deps, open)))
188
+ return false;
189
+ await deps.execHerdr(['workspace', 'close', open]);
190
+ return true;
191
+ }
179
192
  /**
180
193
  * Tear down a ticket's entire worktree (git worktree + hosted workspace).
181
194
  * `herdr worktree remove` is keyed by an open workspace id, so resolve the
@@ -205,12 +218,8 @@ export async function removeHerdrWorktree(deps, branch, worktreePath) {
205
218
  return 'unverified';
206
219
  }
207
220
  if (worktreePath && loadRunClone(worktreePath)) {
208
- const open = await cloneWorkspaceId(execHerdr, root, branch, worktreePath);
209
- if (open) {
210
- if (!(await ensureFocusSafeToRemove(deps, open)))
211
- return 'unverified';
212
- await execHerdr(['workspace', 'close', open]);
213
- }
221
+ if (!(await closeCloneWorkspace(deps, branch, worktreePath)))
222
+ return 'unverified';
214
223
  return removeRunCloneWorkspace(worktreePath) ?? 'unverified';
215
224
  }
216
225
  const entry = resolveWorktreeEntry(await listWorktrees(execHerdr, root), branch, worktreePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bakers-build/addon-herdr",
3
- "version": "0.11.1-next.8",
3
+ "version": "0.11.1-next.9",
4
4
  "description": "bakers.build herdr integration: spawn executor + per-ticket worktree workspace.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -23,14 +23,14 @@
23
23
  "directory": "bakers-cli/addons/herdr"
24
24
  },
25
25
  "dependencies": {
26
- "@bakers-build/addon-workspace-git": "^0.11.1-next.8"
26
+ "@bakers-build/addon-workspace-git": "^0.11.1-next.9"
27
27
  },
28
28
  "peerDependencies": {
29
- "@bakers-build/conductor": "^0.11.1-next.8",
30
- "@bakers-build/core": "^0.11.1-next.8"
29
+ "@bakers-build/conductor": "^0.11.1-next.9",
30
+ "@bakers-build/core": "^0.11.1-next.9"
31
31
  },
32
32
  "bakersBuild": {
33
- "commit": "8807d1cd2569de7e4555aa0485037cec48ad1d1e",
33
+ "commit": "4c0c7ffab0be833fb9cbb50b0784a949593f08e6",
34
34
  "channel": "next"
35
35
  }
36
36
  }