@@ -75,6 +75,33 @@ function hexBytes(value: string): Uint8Array {
7575 Number . parseInt ( value . slice ( index * 2 , index * 2 + 2 ) , 16 ) ) ;
7676}
7777
78+ function cursorProvider (
79+ fetchImpl : typeof fetch ,
80+ baseUrl = "https://api2.cursor.sh" ,
81+ ) : OcxProviderConfig & { fetch : typeof fetch } {
82+ return {
83+ adapter : "cursor" ,
84+ baseUrl,
85+ apiKey : "test-token" ,
86+ upstreamHttpVersion : "http1.1" ,
87+ fetch : fetchImpl ,
88+ } ;
89+ }
90+
91+ function heldRunSseResponse ( signal : AbortSignal | null | undefined ) : Response {
92+ const body = new ReadableStream < Uint8Array > ( {
93+ start ( controller ) {
94+ const abort = ( ) => controller . error ( signal ?. reason ?? new DOMException ( "aborted" , "AbortError" ) ) ;
95+ if ( signal ?. aborted ) abort ( ) ;
96+ else signal ?. addEventListener ( "abort" , abort , { once : true } ) ;
97+ } ,
98+ } ) ;
99+ return new Response ( body , {
100+ status : 200 ,
101+ headers : { "content-type" : "application/connect+proto" } ,
102+ } ) ;
103+ }
104+
78105describe ( "Cursor HTTP/1.1 compatibility transport" , ( ) => {
79106 test ( "encodes the Cursor BidiAppend hex fallback wire shape" , ( ) => {
80107 const payload = Uint8Array . of ( 0xde , 0xad , 0xbe , 0xef ) ;
@@ -126,15 +153,8 @@ describe("Cursor HTTP/1.1 compatibility transport", () => {
126153 throw new Error ( `unexpected Cursor compatibility endpoint ${ url . pathname } ` ) ;
127154 } ) as typeof fetch ;
128155
129- const provider = {
130- adapter : "cursor" ,
131- baseUrl : "https://api2.cursor.sh" ,
132- apiKey : "test-token" ,
133- upstreamHttpVersion : "http1.1" ,
134- fetch : fetchImpl ,
135- } as OcxProviderConfig & { fetch : typeof fetch } ;
136156 const transport = createLiveCursorTransport ( {
137- provider,
157+ provider : cursorProvider ( fetchImpl ) ,
138158 translatorBudget : createTestTranslatorBudget ( ) ,
139159 firstFrameTimeoutMs : 2_000 ,
140160 } ) ;
@@ -166,6 +186,88 @@ describe("Cursor HTTP/1.1 compatibility transport", () => {
166186 expect ( transport . requestCommitted ?.( ) ) . toBe ( true ) ;
167187 } ) ;
168188
189+ test ( "keeps a proven pre-connect BidiAppend failure uncommitted" , async ( ) => {
190+ const fetchImpl = ( async ( input : Parameters < typeof fetch > [ 0 ] , init ?: RequestInit ) => {
191+ const url = new URL ( String ( input ) ) ;
192+ if ( url . pathname === "/agent.v1.AgentService/RunSSE" ) return heldRunSseResponse ( init ?. signal ) ;
193+ throw Object . assign ( new Error ( "fixture append connect failed" ) , { code : "ECONNREFUSED" } ) ;
194+ } ) as typeof fetch ;
195+ const transport = createLiveCursorTransport ( {
196+ provider : cursorProvider ( fetchImpl ) ,
197+ translatorBudget : createTestTranslatorBudget ( ) ,
198+ firstFrameTimeoutMs : 2_000 ,
199+ } ) ;
200+
201+ try {
202+ await expect ( ( async ( ) => {
203+ for await ( const _message of transport . run ( {
204+ modelId : "claude-opus-5" ,
205+ conversationId : "cursor_http1_preconnect_failure" ,
206+ system : [ ] ,
207+ messages : [ { role : "user" , content : "hello" } ] ,
208+ } ) ) { /* drain */ }
209+ } ) ( ) ) . rejects . toThrow ( "fixture append connect failed" ) ;
210+ expect ( transport . requestCommitted ?.( ) ) . toBe ( false ) ;
211+ } finally {
212+ await transport . close ?.( ) ;
213+ }
214+ } ) ;
215+
216+ test ( "keeps ambiguous BidiAppend failures committed to prevent replay" , async ( ) => {
217+ const fetchImpl = ( async ( input : Parameters < typeof fetch > [ 0 ] , init ?: RequestInit ) => {
218+ const url = new URL ( String ( input ) ) ;
219+ if ( url . pathname === "/agent.v1.AgentService/RunSSE" ) return heldRunSseResponse ( init ?. signal ) ;
220+ throw new Error ( "fixture ambiguous append failure" ) ;
221+ } ) as typeof fetch ;
222+ const transport = createLiveCursorTransport ( {
223+ provider : cursorProvider ( fetchImpl ) ,
224+ translatorBudget : createTestTranslatorBudget ( ) ,
225+ firstFrameTimeoutMs : 2_000 ,
226+ } ) ;
227+
228+ try {
229+ await expect ( ( async ( ) => {
230+ for await ( const _message of transport . run ( {
231+ modelId : "claude-opus-5" ,
232+ conversationId : "cursor_http1_ambiguous_failure" ,
233+ system : [ ] ,
234+ messages : [ { role : "user" , content : "hello" } ] ,
235+ } ) ) { /* drain */ }
236+ } ) ( ) ) . rejects . toThrow ( "fixture ambiguous append failure" ) ;
237+ expect ( transport . requestCommitted ?.( ) ) . toBe ( true ) ;
238+ } finally {
239+ await transport . close ?.( ) ;
240+ }
241+ } ) ;
242+
243+ test ( "rejects a cleartext HTTP/1.1 transport before invoking fetch" , async ( ) => {
244+ let fetchCalls = 0 ;
245+ const fetchImpl = ( async ( ) => {
246+ fetchCalls += 1 ;
247+ throw new Error ( "fetch must not run" ) ;
248+ } ) as typeof fetch ;
249+ const transport = createLiveCursorTransport ( {
250+ provider : cursorProvider ( fetchImpl , "http://api2.cursor.sh" ) ,
251+ translatorBudget : createTestTranslatorBudget ( ) ,
252+ firstFrameTimeoutMs : 2_000 ,
253+ } ) ;
254+
255+ try {
256+ await expect ( ( async ( ) => {
257+ for await ( const _message of transport . run ( {
258+ modelId : "claude-opus-5" ,
259+ conversationId : "cursor_http1_cleartext" ,
260+ system : [ ] ,
261+ messages : [ { role : "user" , content : "hello" } ] ,
262+ } ) ) { /* drain */ }
263+ } ) ( ) ) . rejects . toThrow ( "requires an HTTPS base URL" ) ;
264+ expect ( fetchCalls ) . toBe ( 0 ) ;
265+ expect ( transport . requestCommitted ?.( ) ) . toBe ( false ) ;
266+ } finally {
267+ await transport . close ?.( ) ;
268+ }
269+ } ) ;
270+
169271 test ( "preserves the proto3 zero and later append sequence values" , ( ) => {
170272 const payload = Uint8Array . of ( 1 , 2 , 3 ) ;
171273 expect ( decodeAppend ( encodeCursorBidiAppendRequest ( payload , "req" , 0n ) ) . appendSeqno ) . toBe ( 0n ) ;
0 commit comments