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