Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
} from "./AntigravityAdapterV2.ts";

const flavor = makeAntigravityAcpAdapterFlavor({
userHome: process.cwd(),
instanceId: ProviderInstanceId.make("antigravity-test"),
random: undefined as never,
fileSystem: undefined as never,
Expand Down Expand Up @@ -163,6 +164,7 @@ describe("AntigravityAdapterV2 compaction", () => {
const startupMilestone = yield* Deferred.make<void>();
const consumeStartupCommands = yield* Deferred.make<void>();
const adapter = yield* makeAntigravityAdapterV2({
userHome: process.cwd(),
instanceId,
selfInvocation: yield* resolveSelfInvocation(),
makeRuntime: (input) =>
Expand Down Expand Up @@ -245,6 +247,7 @@ describe("AntigravityAdapterV2 session open", () => {
Effect.gen(function* () {
const instanceId = ProviderInstanceId.make("antigravity-sign-in-test");
const adapter = yield* makeAntigravityAdapterV2({
userHome: process.cwd(),
instanceId,
selfInvocation: yield* resolveSelfInvocation(),
makeRuntime: () => Effect.fail(cause),
Expand Down Expand Up @@ -293,6 +296,7 @@ describe("AntigravityAdapterV2 client file system", () => {
const crypto = yield* SecureRandom.SecureRandom;
const instanceId = ProviderInstanceId.make("antigravity-containment-test");
const adapter = yield* makeAntigravityAdapterV2({
userHome: process.cwd(),
instanceId,
selfInvocation: yield* resolveSelfInvocation(),
makeRuntime: (input) =>
Expand Down Expand Up @@ -445,6 +449,7 @@ describe("AntigravityAdapterV2 workspace changes", () => {
let readTextFile: Parameters<RuntimeService["handleReadTextFile"]>[0] | undefined;
const instanceId = ProviderInstanceId.make("antigravity-workspace-change-test");
const adapter = yield* makeAntigravityAdapterV2({
userHome: process.cwd(),
instanceId,
selfInvocation: yield* resolveSelfInvocation(),
makeRuntime: (input) =>
Expand Down Expand Up @@ -615,6 +620,7 @@ describe("AntigravityAdapterV2 html_render pages", () => {
const failed = { ...report, html: "<p>Revenue</p>" };
const errored = { ...report, html: "<p>Revenue error</p>" };
const adapter = yield* makeAntigravityAdapterV2({
userHome: process.cwd(),
instanceId,
selfInvocation: yield* resolveSelfInvocation(),
makeRuntime: (input) =>
Expand Down Expand Up @@ -899,6 +905,7 @@ describe("AntigravityAdapterV2 subagent reads", () => {
const instanceId = ProviderInstanceId.make("antigravity-subagent-reads-test");
const threadId = ThreadId.make("thread-antigravity-subagent-reads");
const adapter = yield* makeAntigravityAdapterV2({
userHome: process.cwd(),
instanceId,
selfInvocation: yield* resolveSelfInvocation(),
makeRuntime: (input) =>
Expand Down Expand Up @@ -1091,6 +1098,7 @@ describe("AntigravityAdapterV2 runtime mode ceiling", () => {
const modesSet: Array<string> = [];
const instanceId = ProviderInstanceId.make("antigravity-ceiling-test");
const adapter = yield* makeAntigravityAdapterV2({
userHome: process.cwd(),
instanceId,
selfInvocation: yield* resolveSelfInvocation(),
makeRuntime: (input) =>
Expand Down Expand Up @@ -1161,8 +1169,10 @@ describe("AntigravityAdapterV2 session system prompt", () => {
type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
const promptReceived = yield* Deferred.make<Parameters<RuntimeService["prompt"]>[0]>();
const instanceId = ProviderInstanceId.make("antigravity-system-prompt-test");
const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-ag-home-" });
const adapter = yield* makeAntigravityAdapterV2({
instanceId,
userHome: home,
selfInvocation: yield* resolveSelfInvocation(),
makeRuntime: (input) =>
makeAntigravityAcpRuntime({
Expand Down Expand Up @@ -1193,6 +1203,10 @@ describe("AntigravityAdapterV2 session system prompt", () => {
defaultModel: Effect.undefined,
});
const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-ag-sp-" });
yield* fileSystem.makeDirectory(path.join(home, ".gemini"));
yield* fileSystem.writeFileString(path.join(home, ".gemini", "GEMINI.md"), "GLOBAL_RULE");
yield* fileSystem.writeFileString(path.join(workspace, "AGENTS.md"), "AGENTS_RULE");
yield* fileSystem.writeFileString(path.join(workspace, "GEMINI.md"), "GEMINI_RULE");
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
runtimeMode: "full-access",
interactionMode: "default",
Expand Down Expand Up @@ -1266,10 +1280,15 @@ describe("AntigravityAdapterV2 session system prompt", () => {
assert.include(sent, "through the Antigravity harness, as gemini-test-low");
if (agentVersion === "1.4.5") {
assert.include(append, "pull_request_linking");
assert.isBelow(append.indexOf("GLOBAL_RULE"), append.indexOf("AGENTS_RULE"));
assert.isBelow(append.indexOf("AGENTS_RULE"), append.indexOf("GEMINI_RULE"));
assert.notInclude(sent, "AGENTS_RULE");
assert.notInclude(sent, "<pull_request_linking>");
} else {
assert.notInclude(append, "systemPrompt");
assert.include(sent, "<pull_request_linking>");
assert.isBelow(sent.indexOf("GLOBAL_RULE"), sent.indexOf("AGENTS_RULE"));
assert.isBelow(sent.indexOf("AGENTS_RULE"), sent.indexOf("GEMINI_RULE"));
}
}).pipe(Effect.provide(layerSession)),
);
Expand Down
72 changes: 63 additions & 9 deletions apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const AntigravityProviderCapabilitiesV2 = {

export interface AntigravityAdapterV2Options {
readonly instanceId: ProviderInstanceId;
readonly userHome: string;
readonly selfInvocation: SelfInvocation;
readonly makeRuntime: (
input: Omit<AntigravityAcpRuntimeInput, "spawn" | "childProcessSpawner">,
Expand Down Expand Up @@ -124,6 +125,21 @@ const extractAntigravitySubagentUpdate: NonNullable<AcpAdapterV2Flavor["extractS
};
};

const RULES_FILE_MAX_BYTES = 256n * 1024n;

type PromptPayload = Parameters<AcpSessionRuntime.AcpSessionRuntime["Service"]["prompt"]>[0];

// The ACP adapter resends runtime instructions on a session's first turn, after compaction and on
// change; rules follow them so the agent's history keeps one current copy.
const withTurnRules = (payload: PromptPayload, rules: string): PromptPayload => {
const index = payload.prompt.findIndex(
(block) => block.type === "text" && block.text.startsWith("<runtime_info>"),
);
return index === -1
? payload
: { ...payload, prompt: payload.prompt.toSpliced(index, 0, { type: "text", text: rules }) };
};

interface AntigravityFlavorServices {
readonly random: SecureRandom.SecureRandom["Service"];
readonly fileSystem: FileSystem.FileSystem;
Expand All @@ -137,16 +153,47 @@ export function makeAntigravityAcpAdapterFlavor(
const { profileDirectory } = options;
const antigravityClientFileRoots = (cwd: string | null) =>
cwd === null ? [options.host.paths.attachmentsDir] : [cwd, options.host.paths.attachmentsDir];
// agy acp loads no rules files itself, so they ride the session system prompt append.
const readRulesFile = (filePath: string) =>
options.fileSystem.stat(filePath).pipe(
Effect.flatMap((info) =>
info.type === "File" && info.size <= RULES_FILE_MAX_BYTES
? options.fileSystem.readFileString(filePath)
: Effect.logWarning("Skipping Antigravity rules file", {
filePath,
reason: `not a regular file of at most ${RULES_FILE_MAX_BYTES} bytes`,
}).pipe(Effect.as("")),
),
Effect.catch((error) =>
error.reason._tag === "NotFound"
? Effect.succeed("")
: Effect.logWarning("Skipping Antigravity rules file", { filePath, error }).pipe(
Effect.as(""),
),
),
);
const makeRuntime = Effect.fn("AntigravityAdapterV2.makeRuntime")(
function* (input: AcpAdapterV2RuntimeInput) {
const rules: Array<string> = [];
for (const filePath of [
options.path.join(options.userHome, ".gemini", "GEMINI.md"),
options.path.join(input.cwd, "AGENTS.md"),
options.path.join(input.cwd, "GEMINI.md"),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
]) {
const contents = yield* readRulesFile(filePath);
if (contents.trim()) rules.push(`# ${filePath}\n\n${contents}`);
}
const rulesText = rules.join("\n\n");
// Set on start: whether this agent drops the system-prompt append, so rules ride the turns.
let rulesInTurns = false;
const scope = yield* Scope.fork(yield* Effect.scope);
const runtime = yield* options
.withProcess(
Scope.close(scope, Exit.void),
options.makeRuntime({
...input,
clientFileReads: true,
sessionSystemPromptAppend: PULL_REQUEST_LINKING_INSTRUCTIONS,
sessionSystemPromptAppend: [...rules, PULL_REQUEST_LINKING_INSTRUCTIONS].join("\n\n"),
additionalDirectories: [options.host.paths.attachmentsDir],
}),
)
Expand Down Expand Up @@ -180,21 +227,28 @@ export function makeAntigravityAcpAdapterFlavor(
),
),
prompt: (payload, promptOptions) =>
Effect.sync(reads.reset).pipe(Effect.andThen(runtime.prompt(payload, promptOptions))),
Effect.sync(reads.reset).pipe(
Effect.andThen(
runtime.prompt(
rulesInTurns ? withTurnRules(payload, rulesText) : payload,
promptOptions,
),
),
),
handleSessionUpdate: (handler) => {
projectUpdate = handler;
return runtime.handleSessionUpdate((notification) =>
updates.withPermit(handler(reads.repair(notification))),
);
},
start: () =>
runtime
.start()
.pipe(
Effect.tap(
(started) => options.onSessionStarted?.(started, input.cwd) ?? Effect.void,
),
),
runtime.start().pipe(
Effect.tap((started) => {
rulesInTurns =
rulesText !== "" && !antigravityAppendsSystemPrompt(started.initializeResult);
return options.onSessionStarted?.(started, input.cwd) ?? Effect.void;
}),
),
} satisfies typeof runtime;
},
Effect.provideService(SecureRandom.SecureRandom, options.random),
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/provider/Drivers/AntigravityDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ export const AntigravityDriver: ProviderDriver<
const defaultModel = currentCatalog.pipe(Effect.map((catalog) => catalog?.defaultChatModel));
const orchestrationAdapter = yield* makeAntigravityAdapterV2({
instanceId,
userHome,
selfInvocation,
makeRuntime,
withProcess: authFlow.withProcess,
Expand Down
Loading