Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.

Commit 34e5082

Browse files
authored
fix(git): clean up handoff artifact directories
Generated-By: PostHog Code Task-Id: b73fc361-26b5-4bae-8c3c-6e4682d0db91
1 parent 86389ce commit 34e5082

4 files changed

Lines changed: 29 additions & 12 deletions

File tree

packages/agent/src/handoff-checkpoint.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { readdir } from "node:fs/promises";
2+
import path from "node:path";
13
import { afterEach, describe, expect, it } from "vitest";
24
import { HandoffCheckpointTracker } from "./handoff-checkpoint";
35
import {
@@ -147,6 +149,18 @@ describe("HandoffCheckpointTracker", () => {
147149
expect(checkpoint).not.toBeNull();
148150
if (!checkpoint) return;
149151
expect(Object.keys(store.artifacts).length).toBeGreaterThan(0);
152+
const gitCommonDirRaw = await cloudRepo.git([
153+
"rev-parse",
154+
"--git-common-dir",
155+
]);
156+
const gitCommonDir = path.isAbsolute(gitCommonDirRaw)
157+
? gitCommonDirRaw
158+
: path.resolve(cloudRepo.path, gitCommonDirRaw);
159+
expect(
160+
(await readdir(gitCommonDir)).filter((entry) =>
161+
entry.startsWith("posthog-code-handoff-"),
162+
),
163+
).toEqual([]);
150164

151165
const applyTracker = createTracker(localRepo.path, apiClient);
152166
await applyTracker.applyFromHandoff(checkpoint);

packages/agent/src/handoff-checkpoint.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
22
import { tmpdir } from "node:os";
3-
import { dirname, join } from "node:path";
3+
import { join } from "node:path";
44
import {
55
type GitHandoffBranchDivergence,
66
type GitHandoffCheckpoint,
@@ -101,12 +101,10 @@ export class HandoffCheckpointTracker {
101101
indexArtifactPath: uploads.index?.storagePath,
102102
};
103103
} finally {
104-
const tempDir = capture.headPack?.path
105-
? dirname(capture.headPack.path)
106-
: dirname(capture.indexFile.path);
107-
await this.removeIfPresent(capture.headPack?.path);
108-
await this.removeIfPresent(capture.indexFile.path);
109-
await rm(tempDir, { recursive: true, force: true }).catch(() => {});
104+
await rm(capture.artifactDirectory, {
105+
recursive: true,
106+
force: true,
107+
}).catch(() => {});
110108
}
111109
}
112110

packages/git/src/handoff.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,9 @@ async function makeCloudChanges(
103103
}
104104

105105
async function cleanupCapture(capture: GitHandoffCaptureResult): Promise<void> {
106-
if (capture.headPack?.path) {
107-
await rm(capture.headPack.path, { force: true }).catch(() => {});
108-
}
109-
await rm(capture.indexFile.path, { force: true }).catch(() => {});
106+
await rm(capture.artifactDirectory, { recursive: true, force: true }).catch(
107+
() => {},
108+
);
110109
}
111110

112111
async function captureAndApply(
@@ -164,6 +163,7 @@ describe("GitHandoffTracker", () => {
164163
).trim();
165164

166165
try {
166+
expect(path.dirname(capture.artifactDirectory)).toBe(gitCommonDir);
167167
expect(path.dirname(path.dirname(capture.indexFile.path))).toBe(
168168
gitCommonDir,
169169
);

packages/git/src/handoff.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface GitHandoffArtifactFile {
2525

2626
export interface GitHandoffCaptureResult {
2727
checkpoint: GitHandoffCheckpoint;
28+
artifactDirectory: string;
2829
headPack?: GitHandoffArtifactFile;
2930
indexFile: GitHandoffArtifactFile;
3031
totalBytes: number;
@@ -136,10 +137,14 @@ export class GitHandoffTracker {
136137
upstreamMergeRef: tracking.upstreamMergeRef,
137138
remoteUrl: tracking.remoteUrl,
138139
},
140+
artifactDirectory: tempDir,
139141
headPack,
140142
indexFile,
141143
totalBytes: (headPack?.rawBytes ?? 0) + indexFile.rawBytes,
142144
};
145+
} catch (error) {
146+
await rm(tempDir, { recursive: true, force: true }).catch(() => {});
147+
throw error;
143148
} finally {
144149
await deleteCheckpoint(git, checkpoint.checkpointId).catch(() => {});
145150
}
@@ -560,7 +565,7 @@ export class GitHandoffTracker {
560565
"--path-format=absolute",
561566
"--git-common-dir",
562567
]);
563-
const resolved = raw.trim();
568+
const resolved = raw.trim() || ".git";
564569
return path.isAbsolute(resolved)
565570
? resolved
566571
: path.resolve(this.repositoryPath, resolved);

0 commit comments

Comments
 (0)