Open History to see the last commit
1import * as Effect from "effect/Effect";
2import type * as FileSystem from "effect/FileSystem";
3import type * as Path from "effect/Path";
4import * as EffectAcpErrors from "effect-acp/errors";
5import type * as EffectAcpSchema from "effect-acp/compat";
6
7/**
8 * The agent gates each write behind `session/request_permission`, so only
9 * path containment is checked here.
10 */
11const CLIENT_FILE_MAX_BYTES = 8 * 1024 * 1024;
12
13function isInsideRoot(path: Path.Path, root: string, candidate: string): boolean {
14 const relative = path.relative(root, candidate);
15 return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
16}
17
18/**
19 * Symlinks resolve before the check, including the final component, so a link
20 * inside the workspace cannot read or write through to a file outside it.
21 */
22const resolveClientFilePath = Effect.fn("AntigravityClientFiles.resolveClientFilePath")(
23 function* (input: {
24 readonly fileSystem: FileSystem.FileSystem;
25 readonly path: Path.Path;
26 readonly allowedRoots: ReadonlyArray<string>;
27 readonly requestPath: string;
28 }) {
29 const { path } = input;
30 const resolved = path.resolve(input.requestPath);
31 const outside = EffectAcpErrors.AcpRequestError.invalidParams(
32 `Path '${input.requestPath}' is outside the session workspace.`,
33 );
34 const real = yield* input.fileSystem.realPath(resolved).pipe(
35 Effect.catch(() =>
36 Effect.gen(function* () {
37 // Only a missing file (a new write) falls back to its parent; a
38 // dangling or unreadable link must not be followed on write.
39 const entryExists = yield* input.fileSystem.readLink(resolved).pipe(
40 Effect.as(true),
41 Effect.catch(() => input.fileSystem.exists(resolved)),
42 Effect.orElseSucceed(() => true),
43 );
44 if (entryExists) return yield* outside;
45 const parent = yield* input.fileSystem
46 .realPath(path.dirname(resolved))
47 .pipe(Effect.orElseSucceed(() => path.dirname(resolved)));
48 return path.join(parent, path.basename(resolved));
49 }),
50 ),
51 );
52 const roots = yield* Effect.forEach(input.allowedRoots, (root) =>
53 input.fileSystem.realPath(root).pipe(Effect.orElseSucceed(() => root)),
54 );
55 if (!roots.some((root) => isInsideRoot(path, root, real))) {
56 return yield* outside;
57 }
58 return real;
59 },
60);
61
62export const readAntigravityClientTextFile = Effect.fn("AntigravityClientFiles.readTextFile")(
63 function* (input: {
64 readonly fileSystem: FileSystem.FileSystem;
65 readonly path: Path.Path;
66 readonly allowedRoots: ReadonlyArray<string>;
67 readonly request: EffectAcpSchema.ReadTextFileRequest;
68 }): Effect.fn.Return<EffectAcpSchema.ReadTextFileResponse, EffectAcpErrors.AcpError> {
69 const filePath = yield* resolveClientFilePath({ ...input, requestPath: input.request.path });
70 const info = yield* input.fileSystem
71 .stat(filePath)
72 .pipe(
73 Effect.mapError(() =>
74 EffectAcpErrors.AcpRequestError.resourceNotFound(
75 `File '${input.request.path}' not found.`,
76 ),
77 ),
78 );
79 if (info.type !== "File" || Number(info.size) > CLIENT_FILE_MAX_BYTES) {
80 return yield* EffectAcpErrors.AcpRequestError.invalidParams(
81 `File '${input.request.path}' is not a readable text file under ${CLIENT_FILE_MAX_BYTES} bytes.`,
82 );
83 }
84 const text = yield* input.fileSystem
85 .readFileString(filePath)
86 .pipe(
87 Effect.mapError(() =>
88 EffectAcpErrors.AcpRequestError.internalError(`Could not read '${input.request.path}'.`),
89 ),
90 );
91 const line = input.request.line ?? undefined;
92 const limit = input.request.limit ?? undefined;
93 if (line === undefined && limit === undefined) {
94 return { content: text };
95 }
96 // ACP lines are 1-indexed. `limit` is a line count.
97 const lines = text.split("\n");
98 const start = Math.max(0, (line ?? 1) - 1);
99 const end = limit === undefined ? lines.length : Math.min(lines.length, start + limit);
100 return { content: lines.slice(start, end).join("\n") };
101 },
102);
103
104export const writeAntigravityClientTextFile = Effect.fn("AntigravityClientFiles.writeTextFile")(
105 function* (input: {
106 readonly fileSystem: FileSystem.FileSystem;
107 readonly path: Path.Path;
108 readonly allowedRoots: ReadonlyArray<string>;
109 readonly request: EffectAcpSchema.WriteTextFileRequest;
110 }): Effect.fn.Return<EffectAcpSchema.WriteTextFileResponse, EffectAcpErrors.AcpError> {
111 const filePath = yield* resolveClientFilePath({ ...input, requestPath: input.request.path });
112 yield* input.fileSystem.makeDirectory(input.path.dirname(filePath), { recursive: true }).pipe(
113 Effect.andThen(input.fileSystem.writeFileString(filePath, input.request.content)),
114 Effect.mapError(() =>
115 EffectAcpErrors.AcpRequestError.internalError(`Could not write '${input.request.path}'.`),
116 ),
117 );
118 return {};
119 },
120);