xonecas.io
64875
Mc

mini-coder / commit eedb398

fix: initialize subagent worktree bun dependencies

Sean2026-03-05 eedb3988cfa291224d3ea1a11008d7df17ce481c parent e2a5df6

3 files changed

src/agent/subagent-runner.ts+3−1
@@ -9,6 +9,7 @@ import {
99 MergeInProgressError,
1010 cleanupBranch,
1111 createWorktree,
12+ initializeWorktree,
1213 isGitRepo,
1314 mergeWorktree,
1415 removeWorktree,
@@ -70,9 +71,10 @@ export function createSubagentRunner(
7071 const nextBranch = makeWorktreeBranch(laneId);
7172 const nextPath = makeWorktreePath(laneId);
7273 await createWorktree(cwd, nextBranch, nextPath);
73- await syncDirtyStateToWorktree(cwd, nextPath);
7474 worktreeBranch = nextBranch;
7575 worktreePath = nextPath;
76+ await syncDirtyStateToWorktree(cwd, nextPath);
77+ await initializeWorktree(cwd, nextPath);
7678 subagentCwd = nextPath;
7779 }
7880
src/tools/worktree.test.ts+37−1
@@ -1,5 +1,11 @@
11 import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2-import { mkdirSync, rmSync, writeFileSync } from "node:fs";
2+import {
3+ lstatSync,
4+ mkdirSync,
5+ realpathSync,
6+ rmSync,
7+ writeFileSync,
8+} from "node:fs";
39 import { tmpdir } from "node:os";
410 import { join } from "node:path";
511 import {
@@ -7,6 +13,7 @@ import {
713 cleanupBranch,
814 createWorktree,
915 hasDirtyWorkingTree,
16+ initializeWorktree,
1017 isGitRepo,
1118 mergeWorktree,
1219 removeWorktree,
@@ -115,6 +122,35 @@ describe("worktree helpers", () => {
115122 await cleanupBranch(repoDir, branch);
116123 });
117124
125+ test("initializes bun dependencies in worktree", async () => {
126+ const branch = `mc-sub-init-${Date.now()}`;
127+ const wtPath = join(
128+ tmpdir(),
129+ `mc-wt-test-${Date.now()}-${Math.random().toString(36).slice(2)}`,
130+ );
131+ extraDirs.push(wtPath);
132+
133+ writeFileSync(join(repoDir, "package.json"), "{}\n");
134+ writeFileSync(join(repoDir, "bun.lock"), "lock\n");
135+ mkdirSync(join(repoDir, "node_modules", "bun-types"), { recursive: true });
136+ writeFileSync(
137+ join(repoDir, "node_modules", "bun-types", "index.d.ts"),
138+ "\n",
139+ );
140+
141+ await createWorktree(repoDir, branch, wtPath);
142+ await initializeWorktree(repoDir, wtPath);
143+
144+ expect(await Bun.file(join(wtPath, "bun.lock")).text()).toBe("lock\n");
145+ expect(realpathSync(join(wtPath, "node_modules"))).toBe(
146+ realpathSync(join(repoDir, "node_modules")),
147+ );
148+ expect(lstatSync(join(wtPath, "node_modules")).isSymbolicLink()).toBe(true);
149+
150+ await removeWorktree(repoDir, wtPath);
151+ await cleanupBranch(repoDir, branch);
152+ });
153+
118154 test("creates a worktree and merges successfully", async () => {
119155 const branch = `mc-sub-1-${Date.now()}`;
120156 const wtPath = join(
src/tools/worktree.ts+37−0
@@ -1,6 +1,7 @@
11 import {
22 chmodSync,
33 copyFileSync,
4+ existsSync,
45 lstatSync,
56 mkdirSync,
67 mkdtempSync,
@@ -127,6 +128,42 @@ function copyUntrackedPath(source: string, destination: string): void {
127128 chmodSync(destination, stat.mode);
128129 }
129130
131+function copyFileIfMissing(source: string, destination: string): void {
132+ if (!existsSync(source) || existsSync(destination)) return;
133+ mkdirSync(dirname(destination), { recursive: true });
134+ copyFileSync(source, destination);
135+}
136+
137+function linkDirectoryIfMissing(source: string, destination: string): void {
138+ if (!existsSync(source) || existsSync(destination)) return;
139+ mkdirSync(dirname(destination), { recursive: true });
140+ symlinkSync(
141+ source,
142+ destination,
143+ process.platform === "win32" ? "junction" : "dir",
144+ );
145+}
146+
147+export async function initializeWorktree(
148+ mainCwd: string,
149+ worktreeCwd: string,
150+): Promise<void> {
151+ const [mainRoot, worktreeRoot] = await Promise.all([
152+ getRepoRoot(mainCwd),
153+ getRepoRoot(worktreeCwd),
154+ ]);
155+ if (!existsSync(join(mainRoot, "package.json"))) return;
156+
157+ for (const lockfile of ["bun.lock", "bun.lockb"]) {
158+ copyFileIfMissing(join(mainRoot, lockfile), join(worktreeRoot, lockfile));
159+ }
160+
161+ linkDirectoryIfMissing(
162+ join(mainRoot, "node_modules"),
163+ join(worktreeRoot, "node_modules"),
164+ );
165+}
166+
130167 export async function syncDirtyStateToWorktree(
131168 mainCwd: string,
132169 worktreeCwd: string,