Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as FileSystem from "effect/FileSystem";
3import * as Path from "effect/Path";
4import * as Schema from "effect/Schema";
5
6const isNativeSessionId = Schema.is(Schema.String.check(Schema.isUUID(4)));
7const decodeSessionMetadata = Schema.decodeEffect(
8 Schema.fromJsonString(Schema.Struct({ cwd: Schema.String })),
9);
10
11/** Call after the process closes. The unique temporary cwd proves which session we own. */
12export const removeAntigravitySessionFiles = Effect.fn("removeAntigravitySessionFiles")(
13 function* (input: {
14 readonly profileDirectory: string;
15 readonly sessionId: string | undefined;
16 readonly cwd: string;
17 }) {
18 if (input.sessionId === undefined || !isNativeSessionId(input.sessionId)) {
19 return;
20 }
21 const fs = yield* FileSystem.FileSystem;
22 const path = yield* Path.Path;
23 const acpDirectory = path.join(input.profileDirectory, "antigravity-acp");
24 const base = path.join(acpDirectory, "conversations", input.sessionId);
25 if (!(yield* fs.exists(`${base}.meta`))) {
26 return;
27 }
28 const metadata = yield* fs
29 .readFileString(`${base}.meta`)
30 .pipe(Effect.flatMap(decodeSessionMetadata));
31 if (metadata.cwd !== input.cwd) {
32 return;
33 }
34 for (const suffix of [".db", ".db-wal", ".db-shm", ".db-journal", ".meta"]) {
35 yield* fs.remove(`${base}${suffix}`, { force: true });
36 }
37 yield* fs.remove(path.join(acpDirectory, "brain", input.sessionId), {
38 recursive: true,
39 force: true,
40 });
41 },
42 Effect.catch(() => Effect.logWarning("Could not remove temporary Antigravity session files.")),
43);
44
45const warnRuntimeTempLeftovers = Effect.logWarning(
46 "Could not remove leftover Antigravity runtime temp files.",
47);
48
49/**
50 * Sweep only private runtimes before launch; Windows does not protect live extracted data files from deletion.
51 * Moves leftovers aside and returns their slow deletion (seconds on Windows) to run off the startup path.
52 */
53export const retireAntigravityRuntimeTempDirs = Effect.fn("retireAntigravityRuntimeTempDirs")(
54 function* (tempDirectory: string) {
55 const fs = yield* FileSystem.FileSystem;
56 const path = yield* Path.Path;
57 const parent = path.dirname(tempDirectory);
58 const prefix = `${path.basename(tempDirectory)}.old-`;
59 if (yield* fs.exists(tempDirectory)) {
60 yield* fs.makeTempDirectory({ directory: parent, prefix }).pipe(
61 Effect.flatMap((retired) => fs.rename(tempDirectory, path.join(retired, "runtime"))),
62 Effect.catch(() => fs.remove(tempDirectory, { recursive: true, force: true })),
63 );
64 }
65 const entries = yield* fs.readDirectory(parent).pipe(Effect.orElseSucceed(() => []));
66 return Effect.forEach(
67 entries.filter((entry) => entry.startsWith(prefix)),
68 (entry) =>
69 fs
70 .remove(path.join(parent, entry), { recursive: true, force: true })
71 .pipe(Effect.catch(() => warnRuntimeTempLeftovers)),
72 { discard: true },
73 );
74 },
75 Effect.catch(() => warnRuntimeTempLeftovers.pipe(Effect.as(Effect.void))),
76);