@@ -12,6 +12,7 @@ import * as Result from "effect/Result";
1212import * as Schema from "effect/Schema" ;
1313import * as Stream from "effect/Stream" ;
1414import * as Ndjson from "effect/unstable/encoding/Ndjson" ;
15+ import { FetchHttpClient , HttpClient , HttpClientRequest } from "effect/unstable/http" ;
1516import * as ChildProcess from "effect/unstable/process/ChildProcess" ;
1617import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner" ;
1718import * as AcpErrors from "effect-acp/errors" ;
@@ -33,6 +34,7 @@ import {
3334 parseAntigravityAuthorizationUrl ,
3435 prepareAntigravityProfile ,
3536 resolveAntigravityProfileDirectory ,
37+ serveAntigravityAuthorizationUrlSink ,
3638} from "./antigravityAuthSupport.ts" ;
3739
3840const authorizationUrl =
@@ -75,6 +77,8 @@ describe("Antigravity process environment", () => {
7577 browser : "another-real-browser" ,
7678 PYTHONUNBUFFERED : "0" ,
7779 ELECTRON_RUN_AS_NODE : "0" ,
80+ T3_ANTIGRAVITY_AUTH_SINK : "http://127.0.0.1:1/stale" ,
81+ t3_antigravity_auth_sink : "http://127.0.0.1:2/stale-alias" ,
7882 CUSTOM_SETTING : "keep-this" ,
7983 } ;
8084 const original = { ...baseEnv } ;
@@ -105,6 +109,19 @@ describe("Antigravity process environment", () => {
105109 } ) ;
106110 } ) ;
107111
112+ it ( "passes only the current sign-in listener to the agent" , ( ) => {
113+ const sink = "http://127.0.0.1:46354/current-flow" ;
114+ const spawn = buildAntigravityAcpSpawnInput ( {
115+ installation : { executablePath : "/release/acp" , harnessPath : "/release/harness" } ,
116+ profile,
117+ cwd : "/project" ,
118+ baseEnv : { t3_antigravity_auth_sink : "http://127.0.0.1:1/stale" } ,
119+ authorizationUrlSink : sink ,
120+ } ) ;
121+ expect ( spawn . env ?. T3_ANTIGRAVITY_AUTH_SINK ) . toBe ( sink ) ;
122+ expect ( spawn . env ?. t3_antigravity_auth_sink ) . toBeUndefined ( ) ;
123+ } ) ;
124+
108125 it ( "passes only the configured method's credential and keeps the GCP pair out of the environment" , ( ) => {
109126 const baseEnv = { PATH : "/usr/bin" , GOOGLE_API_KEY : "ambient-key" } ;
110127 const spawnFor = ( auth : AntigravityAuthConfig ) =>
@@ -260,6 +277,69 @@ describe("Antigravity authorization URL", () => {
260277 ) ;
261278} ) ;
262279
280+ it . layer ( FetchHttpClient . layer ) ( "Antigravity authorization URL listener" , ( it ) => {
281+ it . effect (
282+ "accepts only valid URLs on the current flow's POST endpoint and closes with its scope" ,
283+ ( ) =>
284+ Effect . gen ( function * ( ) {
285+ const urls : string [ ] = [ ] ;
286+ const sink = yield * Effect . gen ( function * ( ) {
287+ const sink = yield * serveAntigravityAuthorizationUrlSink ( ( url ) =>
288+ Effect . sync ( ( ) => void urls . push ( url ) ) ,
289+ ) ;
290+ expect ( new URL ( sink ) . hostname ) . toBe ( "127.0.0.1" ) ;
291+ for ( const [ target , method , body , status ] of [
292+ [ sink , "GET" , undefined , 404 ] ,
293+ [ new URL ( "/wrong-flow" , sink ) . href , "POST" , authorizationUrl , 404 ] ,
294+ [ sink , "POST" , "not a Google authorization URL" , 400 ] ,
295+ ] as const ) {
296+ const request = HttpClientRequest . make ( method ) ( target ) ;
297+ const response = yield * HttpClient . execute (
298+ body === undefined ? request : HttpClientRequest . bodyText ( request , body ) ,
299+ ) ;
300+ expect ( response . status ) . toBe ( status ) ;
301+ yield * response . arrayBuffer ;
302+ }
303+ // The body limit can close the connection before an HTTP response is sent.
304+ const oversized = yield * HttpClient . execute (
305+ HttpClientRequest . post ( sink ) . pipe (
306+ HttpClientRequest . bodyText ( `${ authorizationUrl } &scope=${ "a" . repeat ( 16_384 ) } ` ) ,
307+ ) ,
308+ ) . pipe ( Effect . result ) ;
309+ if ( Result . isSuccess ( oversized ) ) {
310+ expect ( oversized . success . status ) . toBe ( 400 ) ;
311+ yield * oversized . success . arrayBuffer ;
312+ }
313+ expect ( urls ) . toEqual ( [ ] ) ;
314+ const response = yield * HttpClient . execute (
315+ HttpClientRequest . post ( sink ) . pipe ( HttpClientRequest . bodyText ( authorizationUrl ) ) ,
316+ ) ;
317+ expect ( response . status ) . toBe ( 204 ) ;
318+ expect ( urls ) . toEqual ( [ authorizationUrl ] ) ;
319+ return sink ;
320+ } ) . pipe ( Effect . scoped ) ;
321+ const closed = yield * HttpClient . execute (
322+ HttpClientRequest . post ( sink ) . pipe ( HttpClientRequest . bodyText ( authorizationUrl ) ) ,
323+ ) . pipe ( Effect . result ) ;
324+ expect ( Result . isFailure ( closed ) ) . toBe ( true ) ;
325+ expect ( urls ) . toEqual ( [ authorizationUrl ] ) ;
326+ } ) ,
327+ ) ;
328+
329+ it . effect ( "does not expose flow-owner failures in the HTTP response" , ( ) =>
330+ Effect . gen ( function * ( ) {
331+ const sink = yield * serveAntigravityAuthorizationUrlSink ( ( ) =>
332+ Effect . fail ( AcpErrors . AcpRequestError . internalError ( "private flow details" ) ) ,
333+ ) ;
334+ const response = yield * HttpClient . execute (
335+ HttpClientRequest . post ( sink ) . pipe ( HttpClientRequest . bodyText ( authorizationUrl ) ) ,
336+ ) ;
337+ expect ( response . status ) . toBe ( 400 ) ;
338+ expect ( yield * response . text ) . toBe ( "" ) ;
339+ } ) . pipe ( Effect . scoped ) ,
340+ ) ;
341+ } ) ;
342+
263343describe ( "Antigravity sign-in errors" , ( ) => {
264344 it ( "recognizes native auth-required errors and the blocked-login transport error" , ( ) => {
265345 expect ( isAntigravitySignInRequiredError ( AcpErrors . AcpRequestError . authRequired ( ) ) ) . toBe ( true ) ;
@@ -615,6 +695,65 @@ it.layer(NodeServices.layer)("Antigravity profile preparation", (it) => {
615695 } ) ,
616696 ) ;
617697
698+ it . effect (
699+ "delivers the helper URL without stdio and exits successfully after the listener closes" ,
700+ ( ) =>
701+ Effect . gen ( function * ( ) {
702+ const fs = yield * FileSystem . FileSystem ;
703+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
704+ const temporaryDirectory = yield * fs . makeTempDirectoryScoped ( ) ;
705+ let helperCommand : ChildProcess . StandardCommand | undefined ;
706+ yield * prepareAntigravityProfile ( { profileDirectory : temporaryDirectory } ) . pipe (
707+ Effect . provideService (
708+ ChildProcessSpawner . ChildProcessSpawner ,
709+ ChildProcessSpawner . make ( ( command ) => {
710+ if ( ChildProcess . isStandardCommand ( command ) ) helperCommand = command ;
711+ return spawner . spawn ( command ) ;
712+ } ) ,
713+ ) ,
714+ ) ;
715+ expect ( helperCommand ) . toBeDefined ( ) ;
716+ if ( ! helperCommand ) return ;
717+ const command = helperCommand ;
718+ const runHelper = ( sink : string ) =>
719+ Effect . gen ( function * ( ) {
720+ // Reproduce a Windows browser launch that loses the helper's stderr.
721+ const child = yield * Effect . acquireRelease (
722+ Effect . sync ( ( ) =>
723+ NodeChildProcess . spawn (
724+ command . command ,
725+ [ ...command . args . slice ( 0 , - 1 ) , authorizationUrl ] ,
726+ {
727+ env : { ...command . options . env , T3_ANTIGRAVITY_AUTH_SINK : sink } ,
728+ stdio : "ignore" ,
729+ } ,
730+ ) ,
731+ ) ,
732+ ( process ) => Effect . sync ( ( ) => void process . kill ( ) ) ,
733+ ) ;
734+ return yield * Effect . promise (
735+ ( ) =>
736+ new Promise < number | null > ( ( resolve , reject ) => {
737+ child . once ( "error" , reject ) ;
738+ child . once ( "exit" , resolve ) ;
739+ } ) ,
740+ ) ;
741+ } ) . pipe ( Effect . scoped ) ;
742+ const urls : string [ ] = [ ] ;
743+ const sink = yield * Effect . gen ( function * ( ) {
744+ const sink = yield * serveAntigravityAuthorizationUrlSink ( ( url ) =>
745+ Effect . sync ( ( ) => void urls . push ( url ) ) ,
746+ ) ;
747+ expect ( yield * runHelper ( sink ) ) . toBe ( 0 ) ;
748+ expect ( urls ) . toEqual ( [ authorizationUrl ] ) ;
749+ return sink ;
750+ } ) . pipe ( Effect . scoped ) ;
751+ // Success prevents Python from falling back to a browser on the server after cancellation.
752+ expect ( yield * runHelper ( sink ) ) . toBe ( 0 ) ;
753+ expect ( urls ) . toEqual ( [ authorizationUrl ] ) ;
754+ } ) ,
755+ ) ;
756+
618757 it . effect ( "fails before creating a profile when the helper cannot start" , ( ) =>
619758 Effect . gen ( function * ( ) {
620759 const fs = yield * FileSystem . FileSystem ;
0 commit comments