The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 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 | |
| 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", () => |
| 227 | Effect.gen(function* () { |
| 228 | const fs = yield* FileSystem.FileSystem; |
| 229 | const path = yield* Path.Path; |
| 230 | const parent = yield* fs.makeTempDirectoryScoped(); |
| 231 | const runtimeTemp = path.join(parent, "abc123"); |
| 232 | const orphan = path.join(runtimeTemp, "run-orphan", "_MEI123"); |
| 233 | yield* fs.makeDirectory(orphan, { recursive: true }); |
| 234 | yield* fs.writeFileString(path.join(orphan, "payload.bin"), "stale"); |
| 235 | yield* fs.makeDirectory(path.join(parent, "abc123.old-unfinished", "runtime"), { |
| 236 | recursive: true, |
| 237 | }); |
| 238 | yield* fs.makeDirectory(path.join(parent, "other-instance")); |
| 239 | |
| 240 | const removeRetired = yield* retireAntigravityRuntimeTempDirs(runtimeTemp); |
| 241 | expect(yield* fs.exists(runtimeTemp)).toBe(false); |
| 242 | const retired = (yield* fs.readDirectory(parent)).filter((entry) => |
| 243 | entry.startsWith("abc123.old-"), |
| 244 | ); |
| 245 | expect(retired).toHaveLength(2); |
| 246 | |
| 247 | yield* removeRetired; |
| 248 | expect((yield* fs.readDirectory(parent)).toSorted()).toEqual(["other-instance"]); |
| 249 | }), |
| 250 | ); |
| 251 | |
| 252 | it.effect( |
| 253 | "deletes leftovers inline when they cannot be moved aside, such as on a full disk", |
| 254 | () => |
| 255 | Effect.gen(function* () { |
| 256 | const removed: Array<string> = []; |
| 257 | const fs = FileSystem.makeNoop({ |
| 258 | exists: () => Effect.succeed(true), |
| 259 | makeTempDirectory: () => |
| 260 | Effect.fail( |
| 261 | PlatformError.systemError({ |
| 262 | _tag: "Unknown", |
| 263 | module: "FileSystem", |
| 264 | method: "makeTempDirectory", |
| 265 | description: "ENOSPC", |
| 266 | }), |
| 267 | ), |
| 268 | remove: (path) => Effect.sync(() => void removed.push(path)), |
| 269 | readDirectory: () => Effect.succeed([]), |
| 270 | }); |
| 271 | |
| 272 | const removeRetired = yield* retireAntigravityRuntimeTempDirs( |
| 273 | "/state/antigravity-tmp/abc123", |
| 274 | ).pipe(Effect.provideService(FileSystem.FileSystem, fs)); |
| 275 | yield* removeRetired; |
| 276 | expect(removed).toEqual(["/state/antigravity-tmp/abc123"]); |
| 277 | }), |
| 278 | ); |
| 279 | }); |