xonecas.io
64875
Mc

mini-coder / commit 9fface8

fix: make applyParentChanges truly non-throwing

Wrap the entire function body in try/catch so errors from getRepoRoot
or any downstream call are silently swallowed, consistent with the
docstring's stated intent.

Previously, a getRepoRoot failure would propagate out and be caught by
the shared try/catch in index.ts, which cleared worktreePath without
removing the already-created worktree — leaking the git worktree and
its directory in /tmp.

xonecas2026-03-10 9fface83e0902d23190ef216f5ce9968aa799e2a parent 08ebfa7

3 files changed

src/index.ts+2−0
@@ -28,6 +28,7 @@ import {
2828 } from "./session/db/index.ts";
2929 import { getMostRecentSession, printSessionList } from "./session/manager.ts";
3030 import {
31+ applyParentChanges,
3132 createWorktree,
3233 initializeWorktree,
3334 isGitRepo,
@@ -229,6 +230,7 @@ async function main(): Promise<void> {
229230 try {
230231 await createWorktree(parentCwd, worktreeBranch, worktreePath);
231232 await initializeWorktree(parentCwd, worktreePath);
233+ await applyParentChanges(parentCwd, worktreePath);
232234 runCwd = worktreePath;
233235 } catch {
234236 // Worktree creation failed — fall back to running in the original cwd.
src/tools/worktree.test.ts+60−0
@@ -9,6 +9,7 @@ import {
99 import { tmpdir } from "node:os";
1010 import { join } from "node:path";
1111 import {
12+ applyParentChanges,
1213 cleanupBranch,
1314 createWorktree,
1415 hasDirtyWorkingTree,
@@ -189,4 +190,63 @@ describe("worktree helpers", () => {
189190 await cleanupBranch(repoDir, branch1);
190191 await cleanupBranch(repoDir, branch2);
191192 });
193+
194+ test("applyParentChanges propagates unstaged tracked changes", async () => {
195+ const { branch, wtPath } = makeWorktreeDir("apply-unstaged");
196+ await createWorktree(repoDir, branch, wtPath);
197+
198+ writeFileSync(join(repoDir, "README.md"), "unstaged change\n");
199+
200+ await applyParentChanges(repoDir, wtPath);
201+
202+ expect(await Bun.file(join(wtPath, "README.md")).text()).toBe(
203+ "unstaged change\n",
204+ );
205+
206+ await removeWorktree(repoDir, wtPath);
207+ await cleanupBranch(repoDir, branch);
208+ });
209+
210+ test("applyParentChanges propagates staged changes", async () => {
211+ const { branch, wtPath } = makeWorktreeDir("apply-staged");
212+ await createWorktree(repoDir, branch, wtPath);
213+
214+ writeFileSync(join(repoDir, "README.md"), "staged change\n");
215+ git(repoDir, ["add", "README.md"]);
216+
217+ await applyParentChanges(repoDir, wtPath);
218+
219+ expect(await Bun.file(join(wtPath, "README.md")).text()).toBe(
220+ "staged change\n",
221+ );
222+
223+ await removeWorktree(repoDir, wtPath);
224+ await cleanupBranch(repoDir, branch);
225+ });
226+
227+ test("applyParentChanges copies untracked new files", async () => {
228+ const { branch, wtPath } = makeWorktreeDir("apply-untracked");
229+ await createWorktree(repoDir, branch, wtPath);
230+
231+ writeFileSync(join(repoDir, "new-file.ts"), "export const x = 1;\n");
232+
233+ await applyParentChanges(repoDir, wtPath);
234+
235+ expect(await Bun.file(join(wtPath, "new-file.ts")).text()).toBe(
236+ "export const x = 1;\n",
237+ );
238+
239+ await removeWorktree(repoDir, wtPath);
240+ await cleanupBranch(repoDir, branch);
241+ });
242+
243+ test("applyParentChanges no-ops cleanly on a clean tree", async () => {
244+ const { branch, wtPath } = makeWorktreeDir("apply-clean");
245+ await createWorktree(repoDir, branch, wtPath);
246+
247+ await expect(applyParentChanges(repoDir, wtPath)).resolves.toBeUndefined();
248+
249+ await removeWorktree(repoDir, wtPath);
250+ await cleanupBranch(repoDir, branch);
251+ });
192252 });
src/tools/worktree.ts+56−0
@@ -192,3 +192,59 @@ export async function cleanupBranch(
192192 );
193193 }
194194 }
195+
196+/**
197+ * Copy all working-tree changes from the main repo into a freshly-created
198+ * worktree so the subagent sees current unstaged/staged progress.
199+ *
200+ * Two classes of change are propagated:
201+ * 1. Tracked modifications (staged + unstaged) — applied via `git apply`.
202+ * 2. Untracked, non-ignored new files — copied verbatim.
203+ *
204+ * Errors are silently swallowed so a worktree setup failure never blocks the
205+ * subagent; it will simply run without the parent's in-progress changes.
206+ */
207+export async function applyParentChanges(
208+ mainCwd: string,
209+ worktreeCwd: string,
210+): Promise<void> {
211+ try {
212+ const [mainRoot, worktreeRoot] = await Promise.all([
213+ getRepoRoot(mainCwd),
214+ getRepoRoot(worktreeCwd),
215+ ]);
216+
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+ }
228+
229+ // 2. Copy untracked, non-ignored files (e.g. new source files not yet staged).
230+ const untrackedResult = await runGit(mainRoot, [
231+ "ls-files",
232+ "--others",
233+ "--exclude-standard",
234+ ]);
235+ if (untrackedResult.exitCode === 0) {
236+ for (const file of splitNonEmptyLines(untrackedResult.stdout)) {
237+ try {
238+ const src = join(mainRoot, file);
239+ const dst = join(worktreeRoot, file);
240+ mkdirSync(dirname(dst), { recursive: true });
241+ copyFileSync(src, dst);
242+ } catch {
243+ // Skip files that can't be copied (e.g. symlinks, permissions).
244+ }
245+ }
246+ }
247+ } catch {
248+ // best-effort; subagent runs without parent's in-progress changes.
249+ }
250+}