@devries-cameron20 / t3code-1 / commits / c8b214158e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): Antigravity 1.5 temporary setup and text-helper sessions clean up their files (#39275)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

MusseJusse committed Nov 19, 2026 at 20:23 UTC c8b214158e0542a80b75100ccb929177d3bb4f7c
3 files changed +353 -13
apps/server/src/provider/acp/AntigravityChildWork.ts
+1 -1
@@ -80,7 +80,7 @@ export function describeAntigravitySubagentLaunches(
80 }
81
82 /** Length-delimited fields only; varints and fixed values are skipped. */
83 -function protoFields(bytes: Uint8Array): ReadonlyArray<readonly [number, Uint8Array]> {
83 +export function protoFields(bytes: Uint8Array): ReadonlyArray<readonly [number, Uint8Array]> {
84 const fields: Array<readonly [number, Uint8Array]> = [];
85 let offset = 0;
86 const varint = () => {
apps/server/src/provider/acp/AntigravitySessionFiles.test.ts
+216 -1
@@ -1,11 +1,226 @@
1 +// @effect-diagnostics nodeBuiltinImport:off -- fixtures write a native session database.
2 +import * as NodeSqlite from "node:sqlite";
3 +import * as NodeURL from "node:url";
4 +
5 import * as NodeServices from "@effect/platform-node/NodeServices";
6 import { expect, it } from "@effect/vitest";
7 +import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
8 import * as Effect from "effect/Effect";
9 import * as FileSystem from "effect/FileSystem";
10 import * as Path from "effect/Path";
11 import * as PlatformError from "effect/PlatformError";
12
8 -import { retireAntigravityRuntimeTempDirs } from "./AntigravitySessionFiles.ts";
13 +import {
14 + removeAntigravitySessionFiles,
15 + retireAntigravityRuntimeTempDirs,
16 +} from "./AntigravitySessionFiles.ts";
17 +
18 +const SESSION_ID = "047c62f6-607b-44db-bfbe-f83b67e9e8b1";
19 +const OTHER_SESSION_ID = "9b2b7a98-28db-4422-9f46-70e106319548";
20 +
21 +const varint = (value: number): number[] => {
22 + const bytes: number[] = [];
23 + while (value >= 0x80) {
24 + bytes.push((value & 0x7f) | 0x80);
25 + value = Math.floor(value / 0x80);
26 + }
27 + return [...bytes, value];
28 +};
29 +const protoField = (number: number, value: string | Uint8Array) => {
30 + const bytes = typeof value === "string" ? new TextEncoder().encode(value) : value;
31 + return [...varint(number * 8 + 2), ...varint(bytes.length), ...bytes];
32 +};
33 +const proto = (...parts: number[][]) => Uint8Array.from(parts.flat());
34 +
35 +function encodeTrajectoryMetadata(input: {
36 + readonly sessionId?: string;
37 + readonly workspaceUris: ReadonlyArray<string>;
38 +}) {
39 + return proto(
40 + ...input.workspaceUris.map((uri) => protoField(1, proto(protoField(1, uri)))),
41 + ...(input.sessionId === undefined ? [] : [protoField(6, input.sessionId)]),
42 + ...input.workspaceUris.map((uri) => protoField(7, uri)),
43 + );
44 +}
45 +
46 +function writeNativeSessionDb(dbPath: string, blob?: Uint8Array) {
47 + const database = new NodeSqlite.DatabaseSync(dbPath);
48 + try {
49 + database.exec("CREATE TABLE trajectory_metadata_blob (id TEXT PRIMARY KEY, data BLOB)");
50 + if (blob !== undefined) {
51 + database
52 + .prepare("INSERT INTO trajectory_metadata_blob (id, data) VALUES (?, ?)")
53 + .run("main", blob);
54 + }
55 + } finally {
56 + database.close();
57 + }
58 +}
59 +
60 +const makeNativeSessionFixture = Effect.fn("makeNativeSessionFixture")(function* (input: {
61 + readonly sessionId?: string;
62 + readonly workspacePaths: ReadonlyArray<string>;
63 + readonly metadataSessionId?: string;
64 + readonly corruptBlob?: boolean;
65 +}) {
66 + const fs = yield* FileSystem.FileSystem;
67 + const path = yield* Path.Path;
68 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-session-files-" });
69 + const profileDirectory = path.join(root, "profile");
70 + const acpDirectory = path.join(profileDirectory, "antigravity-acp");
71 + const conversations = path.join(acpDirectory, "conversations");
72 + const annotations = path.join(acpDirectory, "annotations");
73 + const sessionId = input.sessionId ?? SESSION_ID;
74 + const brain = path.join(acpDirectory, "brain", sessionId);
75 + const base = path.join(conversations, sessionId);
76 + const annotationFile = path.join(annotations, `${sessionId}.pbtxt`);
77 +
78 + yield* fs.makeDirectory(conversations, { recursive: true });
79 + yield* fs.makeDirectory(annotations, { recursive: true });
80 + yield* fs.makeDirectory(brain, { recursive: true });
81 +
82 + const blob = input.corruptBlob
83 + ? Uint8Array.from([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
84 + : encodeTrajectoryMetadata({
85 + sessionId: input.metadataSessionId ?? sessionId,
86 + workspaceUris: input.workspacePaths.map((entry) => NodeURL.pathToFileURL(entry).href),
87 + });
88 + writeNativeSessionDb(`${base}.db`, blob);
89 + yield* fs.writeFileString(`${base}.db-wal`, "wal");
90 + yield* fs.writeFileString(`${base}.db-shm`, "shm");
91 + yield* fs.writeFileString(path.join(brain, "output.txt"), "artifact");
92 + yield* fs.writeFileString(annotationFile, "annotation");
93 +
94 + return { root, profileDirectory, base, brain, annotationFile, sessionId };
95 +});
96 +
97 +it.layer(NodeServices.layer)("removeAntigravitySessionFiles", (it) => {
98 + it.effect(
99 + "removes Antigravity 1.5+ session files without a .meta sidecar when the DB matches cwd",
100 + () =>
101 + Effect.gen(function* () {
102 + const fs = yield* FileSystem.FileSystem;
103 + const path = yield* Path.Path;
104 + const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-antigravity-setup-" });
105 + const realCwd = yield* fs.realPath(cwd);
106 + const fixture = yield* makeNativeSessionFixture({
107 + workspacePaths: [realCwd, cwd],
108 + });
109 +
110 + yield* removeAntigravitySessionFiles({
111 + profileDirectory: fixture.profileDirectory,
112 + sessionId: fixture.sessionId,
113 + cwd,
114 + });
115 +
116 + expect(yield* fs.exists(`${fixture.base}.db`)).toBe(false);
117 + expect(yield* fs.exists(`${fixture.base}.db-wal`)).toBe(false);
118 + expect(yield* fs.exists(`${fixture.base}.db-shm`)).toBe(false);
119 + expect(yield* fs.exists(fixture.brain)).toBe(false);
120 + expect(yield* fs.exists(fixture.annotationFile)).toBe(false);
121 + expect(yield* fs.exists(path.dirname(fixture.base))).toBe(true);
122 + }),
123 + );
124 +
125 + it.effect("matches a symlinked temporary cwd against the canonical path stored in the DB", () =>
126 + Effect.gen(function* () {
127 + if (!symlinksSupported) return;
128 + const fs = yield* FileSystem.FileSystem;
129 + const path = yield* Path.Path;
130 + const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-symlink-" });
131 + const targetCwd = path.join(parent, "real-temp");
132 + const linkCwd = path.join(parent, "link-temp");
133 + yield* fs.makeDirectory(targetCwd, { recursive: true });
134 + yield* fs.symlink(targetCwd, linkCwd);
135 + const fixture = yield* makeNativeSessionFixture({
136 + workspacePaths: [yield* fs.realPath(targetCwd)],
137 + });
138 +
139 + yield* removeAntigravitySessionFiles({
140 + profileDirectory: fixture.profileDirectory,
141 + sessionId: fixture.sessionId,
142 + cwd: linkCwd,
143 + });
144 +
145 + expect(yield* fs.exists(`${fixture.base}.db`)).toBe(false);
146 + expect(yield* fs.exists(fixture.brain)).toBe(false);
147 + expect(yield* fs.exists(fixture.annotationFile)).toBe(false);
148 + }),
149 + );
150 +
151 + it.effect("keeps a 1.5+ session DB whose metadata belongs to a different workspace", () =>
152 + Effect.gen(function* () {
153 + const fs = yield* FileSystem.FileSystem;
154 + const projectCwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-project-" });
155 + const helperCwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-helper-" });
156 + const fixture = yield* makeNativeSessionFixture({
157 + workspacePaths: [projectCwd],
158 + });
159 +
160 + yield* removeAntigravitySessionFiles({
161 + profileDirectory: fixture.profileDirectory,
162 + sessionId: fixture.sessionId,
163 + cwd: helperCwd,
164 + });
165 +
166 + expect(yield* fs.exists(`${fixture.base}.db`)).toBe(true);
167 + expect(yield* fs.exists(fixture.brain)).toBe(true);
168 + expect(yield* fs.exists(fixture.annotationFile)).toBe(true);
169 + }),
170 + );
171 +
172 + it.effect("keeps a 1.5+ session DB that also includes an additional workspace", () =>
173 + Effect.gen(function* () {
174 + const fs = yield* FileSystem.FileSystem;
175 + const helperCwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-helper-" });
176 + const attachmentsDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-attachments-" });
177 + const fixture = yield* makeNativeSessionFixture({
178 + workspacePaths: [helperCwd, attachmentsDir],
179 + });
180 +
181 + yield* removeAntigravitySessionFiles({
182 + profileDirectory: fixture.profileDirectory,
183 + sessionId: fixture.sessionId,
184 + cwd: helperCwd,
185 + });
186 +
187 + expect(yield* fs.exists(`${fixture.base}.db`)).toBe(true);
188 + expect(yield* fs.exists(fixture.brain)).toBe(true);
189 + expect(yield* fs.exists(fixture.annotationFile)).toBe(true);
190 + }),
191 + );
192 +
193 + it.effect(
194 + "keeps a 1.5+ session DB when its embedded session id mismatches or metadata is corrupt",
195 + () =>
196 + Effect.gen(function* () {
197 + const fs = yield* FileSystem.FileSystem;
198 + const helperCwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-helper-" });
199 + const wrongId = yield* makeNativeSessionFixture({
200 + workspacePaths: [helperCwd],
201 + metadataSessionId: OTHER_SESSION_ID,
202 + });
203 + const corrupt = yield* makeNativeSessionFixture({
204 + workspacePaths: [helperCwd],
205 + corruptBlob: true,
206 + });
207 +
208 + yield* removeAntigravitySessionFiles({
209 + profileDirectory: wrongId.profileDirectory,
210 + sessionId: wrongId.sessionId,
211 + cwd: helperCwd,
212 + });
213 + yield* removeAntigravitySessionFiles({
214 + profileDirectory: corrupt.profileDirectory,
215 + sessionId: corrupt.sessionId,
216 + cwd: helperCwd,
217 + });
218 +
219 + expect(yield* fs.exists(`${wrongId.base}.db`)).toBe(true);
220 + expect(yield* fs.exists(`${corrupt.base}.db`)).toBe(true);
221 + }),
222 + );
223 +});
224
225 it.layer(NodeServices.layer)("retireAntigravityRuntimeTempDirs", (it) => {
226 it.effect("moves leftovers aside at once and deletes them, plus older retirees, later", () =>
apps/server/src/provider/acp/AntigravitySessionFiles.ts
+136 -11
@@ -1,13 +1,119 @@
1 +// @effect-diagnostics nodeBuiltinImport:off -- node:sqlite reads the native session database read-only.
2 +import * as NodeSqlite from "node:sqlite";
3 +import * as NodeURL from "node:url";
4 +
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Path from "effect/Path";
8 import * as Schema from "effect/Schema";
9
10 +import { protoFields } from "./AntigravityChildWork.ts";
11 +import { antigravityManagedPath, antigravityProfileRoot } from "./AntigravityProfilePaths.ts";
12 +
13 const isNativeSessionId = Schema.is(Schema.String.check(Schema.isUUID(4)));
14 const decodeSessionMetadata = Schema.decodeEffect(
15 Schema.fromJsonString(Schema.Struct({ cwd: Schema.String })),
16 );
17
18 +// Field numbers from the runtime's TrajectoryMetadata proto in trajectory_metadata_blob.
19 +const TRAJECTORY_WORKSPACES = 1;
20 +const WORKSPACE_URI = 1;
21 +const TRAJECTORY_SESSION_ID = 6;
22 +const TRAJECTORY_WORKSPACE_URIS = 7;
23 +const MAX_METADATA_BLOB_BYTES = 1024 * 1024;
24 +
25 +const utf8Decoder = new TextDecoder("utf-8", { fatal: true });
26 +
27 +function decodeTrajectoryOwnership(blob: Uint8Array):
28 + | {
29 + readonly sessionId: string | undefined;
30 + readonly workspaceUris: ReadonlyArray<string>;
31 + }
32 + | undefined {
33 + try {
34 + let sessionId: string | undefined;
35 + const workspaceUris: Array<string> = [];
36 + for (const [field, value] of protoFields(blob)) {
37 + if (field === TRAJECTORY_SESSION_ID && sessionId === undefined) {
38 + const decoded = utf8Decoder.decode(value).trim();
39 + if (decoded.length > 0) sessionId = decoded;
40 + } else if (field === TRAJECTORY_WORKSPACES) {
41 + for (const [nestedField, nestedValue] of protoFields(value)) {
42 + if (nestedField !== WORKSPACE_URI) continue;
43 + const uri = utf8Decoder.decode(nestedValue).trim();
44 + if (uri.length > 0) workspaceUris.push(uri);
45 + }
46 + } else if (field === TRAJECTORY_WORKSPACE_URIS) {
47 + const uri = utf8Decoder.decode(value).trim();
48 + if (uri.length > 0) workspaceUris.push(uri);
49 + }
50 + }
51 + return { sessionId, workspaceUris };
52 + } catch {
53 + return undefined;
54 + }
55 +}
56 +
57 +async function readDatabaseWorkspacePaths(
58 + profileDirectory: string,
59 + sessionId: string,
60 +): Promise<ReadonlyArray<string> | undefined> {
61 + const profileRoot = await antigravityProfileRoot(profileDirectory);
62 + if (profileRoot === undefined) return undefined;
63 + const databasePath = (
64 + await antigravityManagedPath(
65 + profileRoot,
66 + ["antigravity-acp", "conversations", `${sessionId}.db`],
67 + "file",
68 + )
69 + )?.path;
70 + if (databasePath === undefined) return undefined;
71 +
72 + let database: NodeSqlite.DatabaseSync | undefined;
73 + try {
74 + database = new NodeSqlite.DatabaseSync(databasePath, { readOnly: true });
75 + database.exec("PRAGMA busy_timeout = 100");
76 + const rows = database
77 + .prepare("SELECT data FROM trajectory_metadata_blob WHERE length(data) <= ? LIMIT 2")
78 + .all(MAX_METADATA_BLOB_BYTES);
79 + if (rows.length !== 1) return undefined;
80 + const row = rows[0];
81 + if (
82 + row === null ||
83 + typeof row !== "object" ||
84 + !("data" in row) ||
85 + !(row.data instanceof Uint8Array)
86 + ) {
87 + return undefined;
88 + }
89 + const metadata = decodeTrajectoryOwnership(row.data);
90 + if (
91 + metadata === undefined ||
92 + metadata.workspaceUris.length === 0 ||
93 + (metadata.sessionId !== undefined && metadata.sessionId !== sessionId)
94 + ) {
95 + return undefined;
96 + }
97 + return metadata.workspaceUris.map((uri) => NodeURL.fileURLToPath(uri));
98 + } catch {
99 + return undefined;
100 + } finally {
101 + database?.close();
102 + }
103 +}
104 +
105 +// The runtime may store the resolved path (macOS `/var` -> `/private/var`) or another drive-letter case.
106 +const pathKey = Effect.fnUntraced(function* (
107 + fs: FileSystem.FileSystem,
108 + path: Path.Path,
109 + target: string,
110 +) {
111 + const resolved = yield* fs
112 + .realPath(target)
113 + .pipe(Effect.orElseSucceed(() => path.resolve(target)));
114 + return path.sep === "\\" ? resolved.toLowerCase() : resolved;
115 +});
116 +
117 /** Call after the process closes. The unique temporary cwd proves which session we own. */
118 export const removeAntigravitySessionFiles = Effect.fn("removeAntigravitySessionFiles")(
119 function* (input: {
@@ -15,29 +121,48 @@ export const removeAntigravitySessionFiles = Effect.fn("removeAntigravitySession
121 readonly sessionId: string | undefined;
122 readonly cwd: string;
123 }) {
18 - if (input.sessionId === undefined || !isNativeSessionId(input.sessionId)) {
124 + const { sessionId } = input;
125 + if (sessionId === undefined || !isNativeSessionId(sessionId)) {
126 return;
127 }
128 const fs = yield* FileSystem.FileSystem;
129 const path = yield* Path.Path;
130 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;
131 + const base = path.join(acpDirectory, "conversations", sessionId);
132 +
133 + if (yield* fs.exists(`${base}.meta`)) {
134 + const metadata = yield* fs
135 + .readFileString(`${base}.meta`)
136 + .pipe(Effect.flatMap(decodeSessionMetadata));
137 + if (metadata.cwd !== input.cwd) {
138 + return;
139 + }
140 + } else {
141 + // Antigravity CLI 1.5+ writes no `.meta` sidecar; workspace URIs live in the session DB.
142 + const workspacePaths = yield* Effect.promise(() =>
143 + readDatabaseWorkspacePaths(input.profileDirectory, sessionId),
144 + );
145 + if (workspacePaths === undefined || workspacePaths.length === 0) {
146 + return;
147 + }
148 + const cwdKey = yield* pathKey(fs, path, input.cwd);
149 + for (const workspacePath of workspacePaths) {
150 + if ((yield* pathKey(fs, path, workspacePath)) !== cwdKey) {
151 + return;
152 + }
153 + }
154 }
155 +
156 for (const suffix of [".db", ".db-wal", ".db-shm", ".db-journal", ".meta"]) {
157 yield* fs.remove(`${base}${suffix}`, { force: true });
158 }
37 - yield* fs.remove(path.join(acpDirectory, "brain", input.sessionId), {
159 + yield* fs.remove(path.join(acpDirectory, "brain", sessionId), {
160 recursive: true,
161 force: true,
162 });
163 + yield* fs.remove(path.join(acpDirectory, "annotations", `${sessionId}.pbtxt`), {
164 + force: true,
165 + });
166 },
167 Effect.catch(() => Effect.logWarning("Could not remove temporary Antigravity session files.")),
168 );