xonecas.io
64875
Mc

mini-coder / commit 3f08230

fix(worktree): sync tracked renames and defer symlink handling

xonecas2026-03-11 3f08230049779be98cf746ded464048404d21d00 parent 38c8609

3 files changed

TODO.md+1−0
@@ -201,3 +201,4 @@ We need to do research first, but maybe we can leverage the hooks feature to ach
201201 - model-info: in `resolveFromProviderRow`, when canonical capability exists but `contextWindow` is null, fall back to provider row context (`capability.contextWindow ?? row.contextWindow`).
202202 - Worktrees share bun lockfile and node_modules. Question this choice, is this really a good idea, or just an opportunity for things to go wrong?
203203 - `subagent-runner.ts`: `Bun.file(proc.stdio[3] as unknown as number).text()` — the double-cast signals a type mismatch. Investigate whether `new Response(proc.stdio[3]).text()` is more correct and whether the current form breaks silently across Bun versions.
204+- `worktree.ts` tracked-change sync: `copyFileSync` turns symlinks into regular files. Preserve symlinks via `lstatSync(...).isSymbolicLink()` + `readlinkSync`/`symlinkSync` (or restore git-native apply for tracked changes).
src/tools/worktree.test.ts+36−0
@@ -1,5 +1,6 @@
11 import { afterEach, beforeEach, describe, expect, test } from "bun:test";
22 import {
3+ existsSync,
34 lstatSync,
45 mkdirSync,
56 realpathSync,
@@ -240,6 +241,41 @@ describe("worktree helpers", () => {
240241 await cleanupBranch(repoDir, branch);
241242 });
242243
244+ test("applyParentChanges propagates tracked renames", async () => {
245+ const { branch, wtPath } = makeWorktreeDir("apply-rename");
246+ await createWorktree(repoDir, branch, wtPath);
247+
248+ git(repoDir, ["mv", "README.md", "GUIDE.md"]);
249+
250+ await applyParentChanges(repoDir, wtPath);
251+
252+ expect(await Bun.file(join(wtPath, "GUIDE.md")).text()).toBe("hello\n");
253+ expect(existsSync(join(wtPath, "README.md"))).toBe(false);
254+
255+ await removeWorktree(repoDir, wtPath);
256+ await cleanupBranch(repoDir, branch);
257+ });
258+
259+ test("applyParentChanges skips untracked node_modules contents", async () => {
260+ const { branch, wtPath } = makeWorktreeDir("apply-node-modules");
261+ await createWorktree(repoDir, branch, wtPath);
262+
263+ mkdirSync(join(repoDir, "node_modules", "fake"), { recursive: true });
264+ writeFileSync(
265+ join(repoDir, "node_modules", "fake", "index.js"),
266+ "module.exports = 1;\n",
267+ );
268+
269+ await applyParentChanges(repoDir, wtPath);
270+
271+ expect(existsSync(join(wtPath, "node_modules", "fake", "index.js"))).toBe(
272+ false,
273+ );
274+
275+ await removeWorktree(repoDir, wtPath);
276+ await cleanupBranch(repoDir, branch);
277+ });
278+
243279 test("applyParentChanges no-ops cleanly on a clean tree", async () => {
244280 const { branch, wtPath } = makeWorktreeDir("apply-clean");
245281 await createWorktree(repoDir, branch, wtPath);
src/tools/worktree.ts+59−13
@@ -1,4 +1,10 @@
1-import { copyFileSync, existsSync, mkdirSync, symlinkSync } from "node:fs";
1+import {
2+ copyFileSync,
3+ existsSync,
4+ mkdirSync,
5+ rmSync,
6+ symlinkSync,
7+} from "node:fs";
28 import { dirname, join } from "node:path";
39
410 interface GitResult {
@@ -198,12 +204,60 @@ export async function cleanupBranch(
198204 * worktree so the subagent sees current unstaged/staged progress.
199205 *
200206 * Two classes of change are propagated:
201- * 1. Tracked modifications (staged + unstaged) — applied via `git apply`.
207+ * 1. Tracked modifications (staged + unstaged) — synced from `git diff --name-status HEAD`.
202208 * 2. Untracked, non-ignored new files — copied verbatim.
203209 *
204210 * Errors are silently swallowed so a worktree setup failure never blocks the
205211 * subagent; it will simply run without the parent's in-progress changes.
206212 */
213+function shouldSkipUntrackedPath(path: string): boolean {
214+ return path === "node_modules" || path.startsWith("node_modules/");
215+}
216+
217+async function syncTrackedChanges(
218+ mainRoot: string,
219+ worktreeRoot: string,
220+): Promise<void> {
221+ const trackedResult = await runGit(mainRoot, [
222+ "diff",
223+ "--name-status",
224+ "-M",
225+ "HEAD",
226+ ]);
227+ if (trackedResult.exitCode !== 0) return;
228+
229+ for (const line of splitNonEmptyLines(trackedResult.stdout)) {
230+ const [statusToken, firstPath, secondPath] = line.split("\t");
231+ const status = statusToken?.trim() ?? "";
232+ if (!status || !firstPath) continue;
233+
234+ if (status.startsWith("R") || status.startsWith("C")) {
235+ if (!secondPath) continue;
236+ const src = join(mainRoot, secondPath);
237+ const dst = join(worktreeRoot, secondPath);
238+ if (existsSync(src)) {
239+ mkdirSync(dirname(dst), { recursive: true });
240+ copyFileSync(src, dst);
241+ }
242+ if (status.startsWith("R")) {
243+ rmSync(join(worktreeRoot, firstPath), { force: true });
244+ }
245+ continue;
246+ }
247+
248+ if (status.startsWith("D")) {
249+ rmSync(join(worktreeRoot, firstPath), { force: true });
250+ continue;
251+ }
252+
253+ const src = join(mainRoot, firstPath);
254+ const dst = join(worktreeRoot, firstPath);
255+ if (!existsSync(src)) continue;
256+ mkdirSync(dirname(dst), { recursive: true });
257+ copyFileSync(src, dst);
258+ }
259+}
260+
207261 export async function applyParentChanges(
208262 mainCwd: string,
209263 worktreeCwd: string,
@@ -214,17 +268,8 @@ export async function applyParentChanges(
214268 getRepoRoot(worktreeCwd),
215269 ]);
216270
217- // 1. Apply tracked changes (staged + unstaged) via a binary-safe patch.
218- const diffResult = await runGit(mainRoot, ["diff", "HEAD", "--binary"]);
219- if (diffResult.exitCode === 0 && diffResult.stdout.trim()) {
220- const proc = Bun.spawn(["git", "apply", "-"], {
221- cwd: worktreeRoot,
222- stdin: Buffer.from(diffResult.stdout),
223- stdout: "pipe",
224- stderr: "pipe",
225- });
226- await proc.exited;
227- }
271+ // 1. Sync tracked changes (staged + unstaged) by status.
272+ await syncTrackedChanges(mainRoot, worktreeRoot);
228273
229274 // 2. Copy untracked, non-ignored files (e.g. new source files not yet staged).
230275 const untrackedResult = await runGit(mainRoot, [
@@ -234,6 +279,7 @@ export async function applyParentChanges(
234279 ]);
235280 if (untrackedResult.exitCode === 0) {
236281 for (const file of splitNonEmptyLines(untrackedResult.stdout)) {
282+ if (shouldSkipUntrackedPath(file)) continue;
237283 try {
238284 const src = join(mainRoot, file);
239285 const dst = join(worktreeRoot, file);