The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Option from "effect/Option"; |
| 3 | import type * as FileSystem from "effect/FileSystem"; |
| 4 | import type * as Path from "effect/Path"; |
| 5 | import * as EffectAcpErrors from "effect-acp/errors"; |
| 6 | import type * as EffectAcpSchema from "effect-acp/compat"; |
| 7 | |
| 8 | const CLIENT_FILE_MAX_BYTES = 8 * 1024 * 1024; |
| 9 | |
| 10 | function isInsideRoot(path: Path.Path, root: string, candidate: string): boolean { |
| 11 | const relative = path.relative(root, candidate); |
| 12 | return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative)); |
| 13 | } |
| 14 | |
| 15 | /** Resolve all symlinks, including the final component, before checking workspace containment. */ |
| 16 | const resolveClientFilePath = Effect.fn("AntigravityClientFiles.resolveClientFilePath")( |
| 17 | function* (input: { |
| 18 | readonly fileSystem: FileSystem.FileSystem; |
| 19 | readonly path: Path.Path; |
| 20 | readonly allowedRoots: ReadonlyArray<string>; |
| 21 | readonly requestPath: string; |
| 22 | }) { |
| 23 | const { path } = input; |
| 24 | const resolved = path.resolve(input.requestPath); |
| 25 | const outside = EffectAcpErrors.AcpRequestError.invalidParams( |
| 26 | `Path '${input.requestPath}' is outside the session workspace.`, |
| 27 | ); |
| 28 | // Canonicalize the deepest existing ancestor, so a link above missing directories cannot escape. |
| 29 | let candidate = resolved; |
| 30 | const missingSuffix: Array<string> = []; |
| 31 | let real: string; |
| 32 | while (true) { |
| 33 | const canonical = yield* input.fileSystem.realPath(candidate).pipe(Effect.option); |
| 34 | if (Option.isSome(canonical)) { |
| 35 | real = path.join(canonical.value, ...missingSuffix); |
| 36 | break; |
| 37 | } |
| 38 | // Walk up only past missing entries, never dangling or unreadable links. |
| 39 | const entryExists = yield* input.fileSystem.readLink(candidate).pipe( |
| 40 | Effect.as(true), |
| 41 | Effect.catch(() => input.fileSystem.exists(candidate)), |
| 42 | Effect.orElseSucceed(() => true), |
| 43 | ); |
| 44 | const parent = path.dirname(candidate); |
| 45 | if (entryExists || parent === candidate) return yield* outside; |
| 46 | missingSuffix.unshift(path.basename(candidate)); |
| 47 | candidate = parent; |
| 48 | } |
| 49 | const roots = yield* Effect.forEach(input.allowedRoots, (root) => |
| 50 | input.fileSystem.realPath(root).pipe(Effect.orElseSucceed(() => root)), |
| 51 | ); |
| 52 | if (!roots.some((root) => isInsideRoot(path, root, real))) { |
| 53 | return yield* outside; |
| 54 | } |
| 55 | return real; |
| 56 | }, |
| 57 | ); |
| 58 | |
| 59 | export const readAntigravityClientTextFile = Effect.fn("AntigravityClientFiles.readTextFile")( |
| 60 | function* (input: { |
| 61 | readonly fileSystem: FileSystem.FileSystem; |
| 62 | readonly path: Path.Path; |
| 63 | readonly allowedRoots: ReadonlyArray<string>; |
| 64 | readonly request: EffectAcpSchema.ReadTextFileRequest; |
| 65 | }): Effect.fn.Return<EffectAcpSchema.ReadTextFileResponse, EffectAcpErrors.AcpError> { |
| 66 | const filePath = yield* resolveClientFilePath({ ...input, requestPath: input.request.path }); |
| 67 | const info = yield* input.fileSystem |
| 68 | .stat(filePath) |
| 69 | .pipe( |
| 70 | Effect.mapError(() => |
| 71 | EffectAcpErrors.AcpRequestError.resourceNotFound( |
| 72 | `File '${input.request.path}' not found.`, |
| 73 | ), |
| 74 | ), |
| 75 | ); |
| 76 | if (info.type !== "File" || Number(info.size) > CLIENT_FILE_MAX_BYTES) { |
| 77 | return yield* EffectAcpErrors.AcpRequestError.invalidParams( |
| 78 | `File '${input.request.path}' is not a readable text file under ${CLIENT_FILE_MAX_BYTES} bytes.`, |
| 79 | ); |
| 80 | } |
| 81 | const bytes = yield* input.fileSystem |
| 82 | .readFile(filePath) |
| 83 | .pipe( |
| 84 | Effect.mapError(() => |
| 85 | EffectAcpErrors.AcpRequestError.internalError(`Could not read '${input.request.path}'.`), |
| 86 | ), |
| 87 | ); |
| 88 | const text = yield* Effect.try({ |
| 89 | try: () => new TextDecoder("utf-8", { fatal: true }).decode(bytes), |
| 90 | catch: () => |
| 91 | EffectAcpErrors.AcpRequestError.invalidParams( |
| 92 | `File '${input.request.path}' is not a UTF-8 text file.`, |
| 93 | ), |
| 94 | }); |
| 95 | if (text.includes("\0")) { |
| 96 | return yield* EffectAcpErrors.AcpRequestError.invalidParams( |
| 97 | `File '${input.request.path}' contains binary data.`, |
| 98 | ); |
| 99 | } |
| 100 | const line = input.request.line ?? undefined; |
| 101 | const limit = input.request.limit ?? undefined; |
| 102 | if (line === undefined && limit === undefined) { |
| 103 | return { content: text }; |
| 104 | } |
| 105 | // ACP lines are 1-indexed. `limit` is a line count. |
| 106 | const lines = text.split("\n"); |
| 107 | const start = Math.max(0, (line ?? 1) - 1); |
| 108 | const end = limit === undefined ? lines.length : Math.min(lines.length, start + limit); |
| 109 | return { content: lines.slice(start, end).join("\n") }; |
| 110 | }, |
| 111 | ); |