Skip to content

Commit a2cab15

Browse files
committed
fix(antigravity): preserve temp marker after cleanup failure
1 parent 7d40ee2 commit a2cab15

2 files changed

Lines changed: 95 additions & 3 deletions

File tree

‎apps/server/src/provider/acp/AntigravityAcpSupport.test.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import * as Effect from "effect/Effect";
1313
import * as FileSystem from "effect/FileSystem";
1414
import * as Fiber from "effect/Fiber";
1515
import * as Path from "effect/Path";
16+
import * as PlatformError from "effect/PlatformError";
17+
import * as TestClock from "effect/testing/TestClock";
1618
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
1719
import * as EffectAcpErrors from "effect-acp/errors";
1820
import type * as EffectAcpSchema from "effect-acp/schema";
@@ -31,6 +33,85 @@ const runtimeTempDirectoryMarkerContents = (directory: string, ownerProcessId: n
3133
`${JSON.stringify({ schemaVersion: 1, ownerProcessId, directory })}\n`;
3234

3335
it.layer(NodeServices.layer)("makeAntigravityAcpRuntime", (it) => {
36+
it.effect(
37+
"preserves ownership after exhausted cleanup retries so a later sweep can recover",
38+
() =>
39+
Effect.gen(function* () {
40+
const fileSystem = yield* FileSystem.FileSystem;
41+
const path = yield* Path.Path;
42+
const crypto = yield* Crypto.Crypto;
43+
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
44+
const orphan = yield* Effect.acquireRelease(
45+
fileSystem.makeTempDirectory({ prefix: "t3-antigravity-runtime-2147483647-" }),
46+
(directory) =>
47+
fileSystem.remove(directory, { recursive: true, force: true }).pipe(Effect.orDie),
48+
);
49+
const marker = path.join(orphan, runtimeTempDirectoryMarkerName);
50+
const markerContents = runtimeTempDirectoryMarkerContents(
51+
path.basename(orphan),
52+
2147483647,
53+
);
54+
yield* fileSystem.writeFileString(marker, markerContents);
55+
const payload = path.join(orphan, "_MEIlocked");
56+
yield* fileSystem.makeDirectory(payload);
57+
yield* fileSystem.writeFileString(path.join(payload, "payload"), "locked fixture");
58+
const attempts = yield* Effect.forEach([0, 1, 2, 3, 4, 5], () => Deferred.make<void>());
59+
const lockedError = PlatformError.systemError({
60+
_tag: "PermissionDenied",
61+
module: "FileSystem",
62+
method: "remove",
63+
description: "EPERM: extraction is in use",
64+
});
65+
let locked = true;
66+
let failures = 0;
67+
const runtimeFileSystem = FileSystem.FileSystem.of({
68+
...fileSystem,
69+
readDirectory: (directory) =>
70+
directory === path.dirname(orphan)
71+
? Effect.succeed([path.basename(orphan)])
72+
: fileSystem.readDirectory(directory),
73+
remove: (directory, options) =>
74+
Effect.gen(function* () {
75+
if (locked && (directory === orphan || directory === payload)) {
76+
// Whole-directory removal can unlink the marker before a locked sibling fails.
77+
if (directory === orphan && options?.recursive) {
78+
yield* fileSystem.remove(marker, { force: true });
79+
}
80+
yield* Deferred.succeed(attempts[failures++]!, undefined);
81+
return yield* lockedError;
82+
}
83+
return yield* fileSystem.remove(directory, options);
84+
}),
85+
// Stop after the sweep, before allocating or launching an unrelated runtime.
86+
makeTempDirectory: () => Effect.fail(lockedError),
87+
});
88+
const sweep = makeAntigravityAcpRuntime({
89+
childProcessSpawner,
90+
fileSystem: runtimeFileSystem,
91+
path,
92+
platform: "win32",
93+
spawn: { command: process.execPath, args: [], cwd: process.cwd() },
94+
cwd: process.cwd(),
95+
clientInfo: { name: "t3-code-test", version: "0.0.0" },
96+
}).pipe(Effect.provideService(Crypto.Crypto, crypto), Effect.scoped, Effect.exit);
97+
const firstSweep = yield* sweep.pipe(Effect.forkChild);
98+
for (const [index, attempt] of attempts.entries()) {
99+
yield* Deferred.await(attempt);
100+
if (index < attempts.length - 1) {
101+
yield* TestClock.adjust(`${100 * 2 ** index} millis`);
102+
}
103+
}
104+
yield* Fiber.join(firstSweep);
105+
expect(failures).toBe(6);
106+
expect(yield* fileSystem.exists(payload)).toBe(true);
107+
expect(yield* fileSystem.readFileString(marker)).toBe(markerContents);
108+
109+
locked = false;
110+
yield* sweep;
111+
expect(yield* fileSystem.exists(orphan)).toBe(false);
112+
}),
113+
);
114+
34115
it.effect("reclaims only owned orphans and removes the current extraction when interrupted", () =>
35116
Effect.gen(function* () {
36117
const fileSystem = yield* FileSystem.FileSystem;

‎apps/server/src/provider/acp/AntigravityAcpSupport.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,20 @@ function withWindowsRuntimeTempDirectory(
8282

8383
const removeRuntimeTempDirectory = Effect.fn("removeRuntimeTempDirectory")(function* (
8484
fileSystem: FileSystem.FileSystem,
85+
path: Path.Path,
8586
directory: string,
8687
) {
87-
yield* fileSystem.remove(directory, { recursive: true, force: true }).pipe(
88+
yield* Effect.gen(function* () {
89+
const entries = yield* fileSystem
90+
.readDirectory(directory)
91+
.pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed([])));
92+
// Keep proof of ownership if a locked payload survives all cleanup retries.
93+
for (const entry of entries) {
94+
if (entry === runtimeTempDirectoryMarkerName) continue;
95+
yield* fileSystem.remove(path.join(directory, entry), { recursive: true, force: true });
96+
}
97+
yield* fileSystem.remove(directory, { recursive: true, force: true });
98+
}).pipe(
8899
Effect.retry({ times: 5, schedule: Schedule.exponential("100 millis") }),
89100
Effect.catch((cause) =>
90101
Effect.logWarning("antigravity runtime temp cleanup failed", { directory, cause }),
@@ -117,7 +128,7 @@ const reclaimOrphanedRuntimeTempDirectories = Effect.fn("reclaimOrphanedRuntimeT
117128
Effect.orElseSucceed(() => false),
118129
);
119130
if (!markerMatches) continue;
120-
yield* removeRuntimeTempDirectory(fileSystem, candidate);
131+
yield* removeRuntimeTempDirectory(fileSystem, path, candidate);
121132
}
122133
},
123134
);
@@ -139,7 +150,7 @@ const makeWindowsRuntimeTempDirectory = Effect.fn("makeWindowsRuntimeTempDirecto
139150
}),
140151
),
141152
);
142-
yield* Effect.addFinalizer(() => removeRuntimeTempDirectory(fileSystem, directory));
153+
yield* Effect.addFinalizer(() => removeRuntimeTempDirectory(fileSystem, path, directory));
143154
yield* fileSystem
144155
.writeFileString(
145156
path.join(directory, runtimeTempDirectoryMarkerName),

0 commit comments

Comments
 (0)