Skip to content

Commit 6045d63

Browse files
committed
fix(antigravity): resume failed local harnesses in a fresh process
1 parent f054d1b commit 6045d63

5 files changed

Lines changed: 159 additions & 4 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,18 @@ const program = Effect.gen(function* () {
962962
const requestedSessionId = String(request.sessionId ?? sessionId);
963963
beginAcpMockPrompt(cancelledSessions, requestedSessionId);
964964
promptCount += 1;
965+
if (process.env.T3_ACP_ANTIGRAVITY_HARNESS_FAILURE === "1") {
966+
yield* Effect.promise(
967+
() =>
968+
new Promise<void>((resolve) => {
969+
process.stderr.write(
970+
"I1022 11:04:37.910207 1865358 local_connection.py:551] harness stderr: Agent run failed: failed to handle call hook response: no pending hook for ID hook_request_2\n",
971+
() => resolve(),
972+
);
973+
}),
974+
);
975+
return yield* Effect.never;
976+
}
965977
if (
966978
process.env.T3_ACP_CRASH_PROMPT === "1" &&
967979
request.prompt.some((part) => part.type === "text" && part.text === "crash now")

‎apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as Exit from "effect/Exit";
1919
import * as FileSystem from "effect/FileSystem";
2020
import * as Layer from "effect/Layer";
2121
import * as Path from "effect/Path";
22+
import * as Queue from "effect/Queue";
2223
import * as Stream from "effect/Stream";
2324
import { ChildProcessSpawner } from "effect/process";
2425
import type * as EffectAcpSchema from "effect-acp/compat";
@@ -31,6 +32,7 @@ import { makeAntigravityAcpRuntime } from "../../provider/acp/AntigravityAcpSupp
3132
import { normalizeAntigravitySessionUpdate } from "../../provider/acp/AntigravityProtocol.ts";
3233
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
3334
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
35+
import { makeTurnInput } from "./AcpAdapterV2.testkit.ts";
3436
import {
3537
makeAntigravityAcpAdapterFlavor,
3638
makeAntigravityAdapterV2,
@@ -122,6 +124,100 @@ const layerSession = Layer.mergeAll(
122124
TestProviderHost.layer().pipe(Layer.provide(NodeServices.layer)),
123125
);
124126

127+
describe("AntigravityAdapterV2 harness recovery", () => {
128+
it.effect("fails a broken harness and resumes the same thread in a fresh process", () =>
129+
Effect.gen(function* () {
130+
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
131+
const fileSystem = yield* FileSystem.FileSystem;
132+
const path = yield* Path.Path;
133+
const crypto = yield* Crypto.Crypto;
134+
const mockAgentPath = yield* path.fromFileUrl(
135+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
136+
);
137+
const workspace = yield* fileSystem.makeTempDirectoryScoped();
138+
const requestLogPath = path.join(workspace, "requests.jsonl");
139+
let processCount = 0;
140+
const instanceId = ProviderInstanceId.make("antigravity-recovery-test");
141+
const adapter = yield* makeAntigravityAdapterV2({
142+
instanceId,
143+
selfInvocation: yield* resolveSelfInvocation(),
144+
makeRuntime: (input) =>
145+
Effect.suspend(() => {
146+
processCount += 1;
147+
return makeAntigravityAcpRuntime({
148+
...input,
149+
childProcessSpawner,
150+
spawn: {
151+
command: process.execPath,
152+
args: [mockAgentPath],
153+
cwd: input.cwd,
154+
env: {
155+
T3_ACP_ANTIGRAVITY: "1",
156+
T3_ACP_REQUEST_LOG_PATH: requestLogPath,
157+
...(processCount === 1 ? { T3_ACP_ANTIGRAVITY_HARNESS_FAILURE: "1" } : {}),
158+
},
159+
},
160+
}).pipe(Effect.provideService(Crypto.Crypto, crypto));
161+
}),
162+
withProcess: (_stop, task) => task,
163+
defaultModel: Effect.succeed(undefined),
164+
});
165+
const threadId = ThreadId.make("thread-antigravity-recovery");
166+
const modelSelection = { instanceId, model: "gemini-test-low" } as const;
167+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
168+
runtimeMode: "full-access",
169+
interactionMode: "default",
170+
cwd: workspace,
171+
});
172+
const session = yield* adapter.openSession({
173+
threadId,
174+
providerSessionId: ProviderSessionId.make("provider-session-antigravity-recovery"),
175+
modelSelection,
176+
runtimePolicy,
177+
});
178+
const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
179+
yield* session.events.pipe(
180+
Stream.runForEach((event) => Queue.offer(events, event)),
181+
Effect.forkScoped,
182+
);
183+
const providerThread = yield* session.ensureThread({
184+
threadId,
185+
modelSelection,
186+
runtimePolicy,
187+
});
188+
const now = yield* DateTime.now;
189+
const terminal = Stream.fromQueue(events).pipe(
190+
Stream.filter((event) => event.type === "turn.terminal"),
191+
Stream.runHead,
192+
);
193+
yield* session.startTurn(
194+
makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, modelSelection, now }),
195+
);
196+
const failed = yield* terminal;
197+
assert.equal(failed._tag, "Some");
198+
if (failed._tag === "Some") assert.equal(failed.value.status, "failed");
199+
yield* session.startTurn(
200+
makeTurnInput({
201+
threadId,
202+
providerThread,
203+
instanceId,
204+
runtimePolicy,
205+
modelSelection,
206+
now,
207+
ordinal: 2,
208+
}),
209+
);
210+
const completed = yield* terminal;
211+
assert.equal(completed._tag, "Some");
212+
if (completed._tag === "Some") assert.equal(completed.value.status, "completed");
213+
assert.equal(processCount, 2);
214+
const requests = yield* fileSystem.readFileString(requestLogPath);
215+
assert.include(requests, "session/resume");
216+
assert.include(requests, "mock-session-1");
217+
}).pipe(Effect.provide(layerSession), Effect.scoped),
218+
);
219+
});
220+
125221
describe("AntigravityAdapterV2 client file system", () => {
126222
it.effect("confines agent file requests to the workspace under full access", () =>
127223
Effect.gen(function* () {

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe("Antigravity process environment", () => {
8484
expect(baseEnv).toEqual(original);
8585
expect(spawn).toEqual({
8686
command: "/release/acp",
87-
args: ["--uid="],
87+
args: ["--uid=", "--alsologtostderr"],
8888
cwd: "/project",
8989
extendEnv: false,
9090
env: {
@@ -366,6 +366,37 @@ describe("Antigravity stdout compatibility", () => {
366366
});
367367

368368
describe("Antigravity stderr compatibility", () => {
369+
it.effect.each([
370+
"I1022 11:04:37.910207 1865358 local_connection.py:551] harness stderr: Agent run failed: failed to handle call hook response: no pending hook for ID hook_request_2",
371+
"E1022 11:04:37.910207 1865358 server.py:5275] Failed to rebuild agent for session native-session; connection unrecoverable",
372+
"E1022 11:04:37.910207 1865358 server.py:5266] Agent connection lost for session native-session after rebuild retry",
373+
])("retires the transport on a native harness failure: %s", (line) =>
374+
Effect.gen(function* () {
375+
const handleStderr = makeAntigravityStderrHandler();
376+
yield* handleStderr(line.slice(0, 70));
377+
const error = yield* handleStderr(`${line.slice(70)}\r\n`).pipe(Effect.flip);
378+
expect(error._tag).toBe("AcpTransportError");
379+
expect(error.message).not.toContain("native-session");
380+
expect(error.message).not.toContain("hook_request_2");
381+
}),
382+
);
383+
384+
it.effect(
385+
"does not classify task prose or unrelated native warnings as a transport failure",
386+
() =>
387+
Effect.gen(function* () {
388+
const handleStderr = makeAntigravityStderrHandler();
389+
yield* handleStderr("Agent run failed: example from a task\n");
390+
yield* handleStderr("Agent connection was lost and could not be re-established\n");
391+
yield* handleStderr(
392+
"I1022 11:04:37.910207 1865358 server.py:5142] Agent connection closed for session native-session; rebuilding agent and retrying the turn once.\n",
393+
);
394+
yield* handleStderr(
395+
"W1022 11:04:37.910207 1865358 telemetry.py:431] No business auth manager configured\n",
396+
);
397+
}),
398+
);
399+
369400
it.effect("forwards fragmented native sign-in URLs from runtime 1.1.1", () =>
370401
Effect.gen(function* () {
371402
const urls: string[] = [];

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,9 @@ export function buildAntigravityAcpSpawnInput(input: {
445445
}): AcpSessionRuntime.AcpSpawnInput {
446446
return {
447447
command: input.installation.executablePath,
448-
args: input.profile.platform === "linux" ? ["--uid="] : [],
448+
// The ACP process can survive a failed local harness and return end_turn.
449+
// Its native logs are the failure signal; the transport must receive them.
450+
args: [...(input.profile.platform === "linux" ? ["--uid="] : []), "--alsologtostderr"],
449451
cwd: input.cwd,
450452
env: {
451453
...antigravityEnvironment(
@@ -576,6 +578,20 @@ export function makeAntigravityStderrHandler(
576578
if (message.length > maxBrowserHelperLineLength) {
577579
return Effect.void;
578580
}
581+
if (
582+
/^[IWEF]\d{4} \d{2}:\d{2}:\d{2}\.\d+ +\d+ (?:local_connection\.py:\d+\] harness stderr: Agent run failed:|server\.py:\d+\] (?:Failed to rebuild agent for session |Agent connection lost for session ))/.test(
583+
message,
584+
)
585+
) {
586+
return Effect.fail(
587+
new AcpErrors.AcpTransportError({
588+
method: "session/prompt",
589+
detail:
590+
"Antigravity's local agent connection failed. Send a follow-up to resume with a fresh process.",
591+
cause: undefined,
592+
}),
593+
);
594+
}
579595
const url = message.startsWith(ANTIGRAVITY_AUTH_STDOUT_PREFIX)
580596
? Effect.succeed(message.slice(ANTIGRAVITY_AUTH_STDOUT_PREFIX.length))
581597
: message.startsWith(ANTIGRAVITY_AUTH_BROWSER_MARKER)

‎packages/provider-acp/src/server/AcpSessionRuntime.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
13561356
}
13571357
yield* closeActiveAssistantSegment({ queue: eventQueue, assistantSegmentRef });
13581358
yield* Queue.offer(eventQueue, { _tag: "ConnectionTerminated", error: enriched });
1359+
yield* options.onTermination?.(enriched) ?? Effect.void;
13591360
});
13601361

13611362
const logRequest = (event: AcpSessionRequestLogEvent) =>
@@ -1649,8 +1650,8 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
16491650
),
16501651
Effect.catch((error) =>
16511652
Effect.gen(function* () {
1652-
yield* Deferred.fail(stderrFailure, error);
16531653
yield* recordTermination(error);
1654+
yield* Deferred.fail(stderrFailure, error);
16541655
yield* child.kill({ forceKillAfter: "1 second" }).pipe(Effect.ignore);
16551656
}),
16561657
),
@@ -1677,7 +1678,6 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
16771678
...(options.onIncomingRequest ? { onIncomingRequest: options.onIncomingRequest } : {}),
16781679
onTermination: (error) =>
16791680
recordTermination(error).pipe(
1680-
Effect.andThen(options.onTermination?.(error) ?? Effect.void),
16811681
Effect.ensuring(
16821682
Scope.close(runtimeScope, Exit.fail(error)).pipe(Effect.forkDetach, Effect.asVoid),
16831683
),

0 commit comments

Comments
 (0)