Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(git-clone): clean up the half-built destination after a timeout

liuzixiang6dadeebbd466
diff --git a/apps/desktop/electron/main/git-clone.ts b/apps/desktop/electron/main/git-clone.ts
index 5f7d24a4f..8beda4206 100644
--- a/apps/desktop/electron/main/git-clone.ts
+++ b/apps/desktop/electron/main/git-clone.ts
@@ -1,5 +1,5 @@
import { spawn } from "node:child_process";
-import { existsSync, statSync } from "node:fs";
+import { existsSync, lstatSync, rmSync, statSync, unlinkSync } from "node:fs";
import { isAbsolute, relative, resolve } from "node:path";
import {
isGitCloneRepoName,
@@ -68,6 +68,21 @@ export function runGitClone(
});
}
+/**
+ * A timed-out clone leaves the half-built checkout git was killed inside;
+ * keeping it makes every retry of the same name hit CONFLICT until the user
+ * finds and deletes the residue by hand. A symlink at the destination is
+ * unlinked instead of recursed into.
+ */
+function removeIncompleteClone(dest: string): void {
+ try {
+ if (lstatSync(dest).isSymbolicLink()) unlinkSync(dest);
+ else rmSync(dest, { recursive: true, force: true });
+ } catch {
+ // The clone died before creating anything; nothing to clean up.
+ }
+}
+
export async function cloneGitRepository(input: {
url: string;
parentPath: string;
@@ -114,6 +129,7 @@ export async function cloneGitRepository(input: {
throw codedError("NOT_FOUND", "Git is not installed");
}
if (result.errorCode === "TIMEOUT") {
+ removeIncompleteClone(dest);
throw codedError("TIMEOUT", "git clone timed out");
}
if (result.code !== 0) {
diff --git a/apps/desktop/test/git-clone.test.mjs b/apps/desktop/test/git-clone.test.mjs
index a8047521e..83644673a 100644
--- a/apps/desktop/test/git-clone.test.mjs
+++ b/apps/desktop/test/git-clone.test.mjs
@@ -1,6 +1,6 @@
import assert from "node:assert/strict";
import test from "node:test";
-import { mkdtempSync, mkdirSync, rmSync } from "node:fs";
+import { existsSync, mkdtempSync, mkdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
@@ -269,3 +269,26 @@ test("cloneGitRepository gates a plain http remote on the user's endpoint policy
rmSync(root, { recursive: true, force: true });
}
});
+
+test("cloneGitRepository cleans up a half-built destination after a timeout", async () => {
+ // git was killed mid-clone; keeping the residue made every retry of the
+ // same name hit CONFLICT until the user found and deleted it by hand.
+ const root = mkdtempSync(join(tmpdir(), "pi-clone-"));
+ try {
+ await assert.rejects(
+ cloneGitRepository({
+ url: "https://github.com/org/demo.git",
+ parentPath: root,
+ // git created the checkout before it was killed mid-clone.
+ run: async (_cwd, args) => {
+ mkdirSync(join(String(args[3]), ".git", "objects"), { recursive: true });
+ return { code: 1, stderr: "git clone timed out", errorCode: "TIMEOUT" };
+ },
+ }),
+ { errorCode: "TIMEOUT" },
+ );
+ assert.equal(existsSync(join(root, "demo")), false);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+});