@@ -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);