Skip to content

Commit e923b5d

Browse files
fix(server): Antigravity threads follow the user's global AGENTS.md and GEMINI.md (pingdotgg#39531)
1 parent be91767 commit e923b5d

2 files changed

Lines changed: 144 additions & 10 deletions

File tree

‎apps/server/src/provider/antigravityAuthSupport.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,71 @@ it.layer(NodeServices.layer)("Antigravity profile preparation", (it) => {
681681
}),
682682
);
683683

684+
it.effect.skipIf(!symlinksSupported)(
685+
"links the user's global rule files and never replaces the profile's own",
686+
() =>
687+
Effect.gen(function* () {
688+
const fs = yield* FileSystem.FileSystem;
689+
const path = yield* Path.Path;
690+
const temporaryDirectory = yield* fs.makeTempDirectoryScoped();
691+
const userHome = path.join(temporaryDirectory, "home");
692+
const profileDirectory = path.join(temporaryDirectory, "profile");
693+
const userGemini = path.join(userHome, ".gemini");
694+
yield* fs.makeDirectory(userGemini, { recursive: true });
695+
yield* fs.writeFileString(path.join(userGemini, "AGENTS.md"), "global agents rule");
696+
yield* fs.writeFileString(path.join(userGemini, "GEMINI.md"), "global gemini rule");
697+
698+
yield* prepareAntigravityProfile({ profileDirectory, userHome });
699+
expect(yield* fs.readLink(path.join(profileDirectory, "AGENTS.md"))).toBe(
700+
path.join(userGemini, "AGENTS.md"),
701+
);
702+
expect(yield* fs.readLink(path.join(profileDirectory, "GEMINI.md"))).toBe(
703+
path.join(userGemini, "GEMINI.md"),
704+
);
705+
706+
yield* fs.remove(path.join(userGemini, "AGENTS.md"));
707+
yield* fs.remove(path.join(profileDirectory, "GEMINI.md"));
708+
yield* fs.writeFileString(path.join(profileDirectory, "GEMINI.md"), "profile's own rule");
709+
yield* prepareAntigravityProfile({ profileDirectory, userHome });
710+
expect(yield* fs.exists(path.join(profileDirectory, "AGENTS.md"))).toBe(false);
711+
expect(yield* fs.readFileString(path.join(profileDirectory, "GEMINI.md"))).toBe(
712+
"profile's own rule",
713+
);
714+
}),
715+
);
716+
717+
it.effect("copies the user's global rule files on Windows and keeps the profile's own", () =>
718+
Effect.gen(function* () {
719+
const fs = yield* FileSystem.FileSystem;
720+
const path = yield* Path.Path;
721+
const temporaryDirectory = yield* fs.makeTempDirectoryScoped();
722+
const userHome = path.join(temporaryDirectory, "home");
723+
const profileDirectory = path.join(temporaryDirectory, "profile");
724+
const userGemini = path.join(userHome, ".gemini");
725+
const prepare = prepareAntigravityProfile({ profileDirectory, userHome, platform: "win32" });
726+
const profileAgents = path.join(profileDirectory, "AGENTS.md");
727+
const profileGemini = path.join(profileDirectory, "GEMINI.md");
728+
yield* fs.makeDirectory(userGemini, { recursive: true });
729+
yield* fs.writeFileString(path.join(userGemini, "AGENTS.md"), "global agents rule");
730+
yield* fs.writeFileString(path.join(userGemini, "GEMINI.md"), "global gemini rule");
731+
732+
yield* prepare;
733+
expect(yield* fs.readFileString(profileAgents)).toBe("global agents rule");
734+
expect(yield* fs.readFileString(profileGemini)).toBe("global gemini rule");
735+
736+
yield* fs.writeFileString(path.join(userGemini, "AGENTS.md"), "edited agents rule");
737+
yield* fs.remove(path.join(userGemini, "GEMINI.md"));
738+
yield* prepare;
739+
expect(yield* fs.readFileString(profileAgents)).toBe("edited agents rule");
740+
expect(yield* fs.exists(profileGemini)).toBe(false);
741+
742+
yield* fs.writeFileString(profileGemini, "profile's own rule");
743+
yield* fs.writeFileString(path.join(userGemini, "GEMINI.md"), "global gemini rule");
744+
yield* prepare;
745+
expect(yield* fs.readFileString(profileGemini)).toBe("profile's own rule");
746+
}),
747+
);
748+
684749
it.effect("rewrites the GCP block on every launch and never stores the API key", () =>
685750
Effect.gen(function* () {
686751
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/provider/antigravityAuthSupport.ts‎

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Digest from "effect/crypto/Digest";
88
import * as Effect from "effect/Effect";
99
import * as Hex from "effect/encoding/Hex";
1010
import * as FileSystem from "effect/FileSystem";
11+
import * as Option from "effect/Option";
1112
import * as Path from "effect/Path";
1213
import type * as PlatformError from "effect/PlatformError";
1314
import * as Schema from "effect/Schema";
@@ -243,23 +244,34 @@ class AntigravitySkillLinkError extends Schema.TaggedError<AntigravitySkillLinkE
243244
{ link: Schema.String, cause: Schema.Defect() },
244245
) {
245246
override get message(): string {
246-
return `Could not link Antigravity user skills at ${this.link}.`;
247+
return `Could not link Antigravity user configuration at ${this.link}.`;
247248
}
248249
}
249250

250-
/** Private GEMINI_HOME isolates credentials/hooks/MCP; link global skills from the real ~/.gemini. */
251-
const linkAntigravityUserSkills = Effect.fn("linkAntigravityUserSkills")(function* (input: {
251+
const antigravityUserRuleFiles = ["AGENTS.md", "GEMINI.md"] as const;
252+
253+
/** Private GEMINI_HOME isolates credentials/hooks/MCP; link global skills and rules from the real ~/.gemini. */
254+
const linkAntigravityUserConfig = Effect.fn("linkAntigravityUserConfig")(function* (input: {
252255
readonly profileDirectory: string;
253256
readonly userHome: string;
254257
readonly platform: NodeJS.Platform;
255258
}): Effect.fn.Return<void, never, FileSystem.FileSystem | Path.Path> {
256259
const fs = yield* FileSystem.FileSystem;
257260
const path = yield* Path.Path;
261+
const userGemini = path.join(input.userHome, ".gemini");
258262
const links = antigravityUserSkillDirectories(path, input.profileDirectory);
259-
const targets = antigravityUserSkillDirectories(path, path.join(input.userHome, ".gemini"));
260-
for (const [link, target] of [
261-
[links[0], targets[0]],
262-
[links[1], targets[1]],
263+
const targets = antigravityUserSkillDirectories(path, userGemini);
264+
const directoryType = input.platform === "win32" ? "junction" : "dir";
265+
for (const [link, target, type] of [
266+
[links[0], targets[0], directoryType],
267+
[links[1], targets[1], directoryType],
268+
// File symlinks need elevation or Developer Mode on Windows, so rules are copied there.
269+
...(input.platform === "win32"
270+
? []
271+
: antigravityUserRuleFiles.map(
272+
(name) =>
273+
[path.join(input.profileDirectory, name), path.join(userGemini, name), "file"] as const,
274+
)),
263275
] as const) {
264276
yield* Effect.gen(function* () {
265277
// The harness creates these folders itself, and mkdir fails on a dangling link.
@@ -292,19 +304,73 @@ const linkAntigravityUserSkills = Effect.fn("linkAntigravityUserSkills")(functio
292304
}
293305
yield* fs.makeDirectory(path.dirname(link), { recursive: true });
294306
yield* Effect.tryPromise({
295-
try: () => NodeFSP.symlink(target, link, input.platform === "win32" ? "junction" : "dir"),
307+
try: () => NodeFSP.symlink(target, link, type),
296308
catch: (cause) => new AntigravitySkillLinkError({ link, cause }),
297309
});
298310
}).pipe(
299311
Effect.catch((error) =>
300-
Effect.logWarning("Antigravity user skills are not linked into the profile.", {
312+
Effect.logWarning("Antigravity user configuration is not linked into the profile.", {
313+
link,
314+
target,
315+
error,
316+
}),
317+
),
318+
);
319+
}
320+
});
321+
322+
const RuleCopiesJson = Schema.fromJsonString(Schema.Record(Schema.String, Schema.String));
323+
const decodeRuleCopies = Schema.decodeUnknownOption(RuleCopiesJson);
324+
const encodeRuleCopies = Schema.encodeSync(RuleCopiesJson);
325+
326+
/** Copies refresh each launch; the recorded copy keeps a file the profile owns or edited untouched. */
327+
const copyAntigravityUserRuleFiles = Effect.fn("copyAntigravityUserRuleFiles")(function* (input: {
328+
readonly profileDirectory: string;
329+
readonly userHome: string;
330+
}): Effect.fn.Return<void, never, FileSystem.FileSystem | Path.Path> {
331+
const fs = yield* FileSystem.FileSystem;
332+
const path = yield* Path.Path;
333+
const manifestPath = path.join(input.profileDirectory, ".t3-rule-copies.json");
334+
const readOptional = (file: string) =>
335+
fs.readFileString(file).pipe(Effect.orElseSucceed((): string | undefined => undefined));
336+
const manifestText = yield* readOptional(manifestPath);
337+
const copies: Record<string, string> = {
338+
...Option.getOrElse(decodeRuleCopies(manifestText), () => ({})),
339+
};
340+
for (const name of antigravityUserRuleFiles) {
341+
const link = path.join(input.profileDirectory, name);
342+
const target = path.join(input.userHome, ".gemini", name);
343+
yield* Effect.gen(function* () {
344+
const current = yield* readOptional(link);
345+
if (current !== undefined && current !== copies[name]) {
346+
delete copies[name];
347+
return;
348+
}
349+
const source = yield* readOptional(target);
350+
if (source === undefined) {
351+
if (current !== undefined) yield* fs.remove(link);
352+
delete copies[name];
353+
return;
354+
}
355+
yield* fs.writeFileString(link, source);
356+
copies[name] = source;
357+
}).pipe(
358+
Effect.catch((error) =>
359+
Effect.logWarning("Antigravity user rules are not copied into the profile.", {
301360
link,
302361
target,
303362
error,
304363
}),
305364
),
306365
);
307366
}
367+
yield* fs
368+
.writeFileString(manifestPath, encodeRuleCopies(copies))
369+
.pipe(
370+
Effect.catch((error) =>
371+
Effect.logWarning("Antigravity rule copy record could not be written.", { error }),
372+
),
373+
);
308374
});
309375

310376
export const prepareAntigravityProfile = Effect.fn("prepareAntigravityProfile")(function* (input: {
@@ -453,7 +519,10 @@ export const prepareAntigravityProfile = Effect.fn("prepareAntigravityProfile")(
453519
authSupportError("The Antigravity profile settings could not be written."),
454520
),
455521
);
456-
yield* linkAntigravityUserSkills({ profileDirectory: geminiHome, userHome, platform });
522+
yield* linkAntigravityUserConfig({ profileDirectory: geminiHome, userHome, platform });
523+
if (platform === "win32") {
524+
yield* copyAntigravityUserRuleFiles({ profileDirectory: geminiHome, userHome });
525+
}
457526
return profile;
458527
});
459528

0 commit comments

Comments
 (0)