@@ -374,14 +374,15 @@ async function serveStatus(status: number) {
374374 } ;
375375}
376376
377- /** `text` as a stream body: sent with `duplex: "half"`, it goes out without a `Content-Length`. */
378- function streamOf ( text : string ) : ReadableStream < Uint8Array > {
379- return new ReadableStream ( {
377+ /** Sends the same body without a `Content-Length`. */
378+ function streamRequest ( init : RequestInit | undefined ) : RequestInit {
379+ const body = new ReadableStream ( {
380380 start ( controller ) {
381- controller . enqueue ( new TextEncoder ( ) . encode ( text ) ) ;
381+ controller . enqueue ( new TextEncoder ( ) . encode ( String ( init ?. body ) ) ) ;
382382 controller . close ( ) ;
383383 } ,
384384 } ) ;
385+ return { ...init , body, duplex : "half" } as RequestInit ;
385386}
386387
387388/** Whether a request is a `.in` append of the stop chunk. */
@@ -429,6 +430,8 @@ describe("the stop written when an owning send is aborted", () => {
429430 lostResponse ?: { url ?: string ; asStream ?: boolean ; active : ( ) => boolean } ;
430431 /** While `active()`, holds an append other than the stop until `until`; `onHeld` runs as it is held. */
431432 hold ?: { active : ( ) => boolean ; until : Promise < void > ; onHeld : ( ) => void } ;
433+ /** While true, send non-stop appends without a length so the webapp returns 413. */
434+ omitAppendLength ?: ( ) => boolean ;
432435 /** Runs after the key is rotated, just before the send is aborted. */
433436 beforeAbort ?: ( ctx : { transport : TriggerChatTransport ; chatId : string } ) => Promise < void > ;
434437 /** Close the session after the send, so the webapp answers the stop with a 409. */
@@ -471,11 +474,12 @@ describe("the stop written when an owning send is aborted", () => {
471474 // A refused attempt (the stale token's 401) is the answer; one that landed is lost.
472475 if ( ! first . ok ) return first ;
473476 await first . arrayBuffer ( ) ;
474- const retry = options . lostResponse . asStream
475- ? ( { ...init , body : streamOf ( String ( init ?. body ) ) , duplex : "half" } as RequestInit )
476- : init ;
477+ const retry = options . lostResponse . asStream ? streamRequest ( init ) : init ;
477478 return fetch ( options . lostResponse . url ?? url , retry ) ;
478479 }
480+ if ( options . omitAppendLength ?.( ) && url . endsWith ( "/in/append" ) && ! isStopAppend ( init ) ) {
481+ return fetch ( url , streamRequest ( init ) ) ;
482+ }
479483 if ( options . detour ?. active ( ) && url . endsWith ( "/in/append" ) && ! isStopAppend ( init ) ) {
480484 return fetch ( options . detour . url , init ) ;
481485 }
@@ -720,30 +724,25 @@ describe("the stop written when an owning send is aborted", () => {
720724 it . each ( [ "sendMessages" , "sendAction" ] ) (
721725 "gives up the stop when the newer %s is refused with a 413" ,
722726 async ( sender ) => {
723- // Catches: the stop going out after a follow-up that was refused (here a 413 for a body
724- // over the append limit). A 413 can follow an attempt that landed (see the retry sent
725- // without a length below), so a refused send is no proof that no turn started. Losing a
726- // stop is the safe side; cancelling the newer reply is not.
727+ // A 413 does not prove that no turn started: an earlier attempt could have landed.
728+ // Use a small body without a length to avoid a reset during a large upload.
729+ let refuse = false ;
727730 let release ! : ( ) => void ;
728731 const gate = new Promise < void > ( ( resolve ) => ( release = resolve ) ) ;
729732 const { flush, transport, chatId, env, calls, stops } = await abortAfterASend ( {
730733 accessToken : async ( call , renewed ) => {
731734 if ( call === 1 ) await gate ;
732735 return renewed ;
733736 } ,
737+ omitAppendLength : ( ) => refuse ,
734738 } ) ;
735739 await vi . waitFor ( ( ) => expect ( calls ( ) ) . toBe ( 1 ) ) ;
736740
741+ refuse = true ;
737742 const refused =
738743 sender === "sendMessages"
739- ? transport . sendMessages ( {
740- trigger : "submit-message" ,
741- chatId,
742- messageId : undefined ,
743- messages : [ userMessage ( "x" . repeat ( 1_050_000 ) ) ] ,
744- abortSignal : undefined ,
745- } )
746- : transport . sendAction ( chatId , { type : "undo" , payload : "x" . repeat ( 1_050_000 ) } ) ;
744+ ? sendHi ( transport , chatId )
745+ : transport . sendAction ( chatId , { type : "undo" } ) ;
747746 await expect ( refused ) . rejects . toMatchObject ( { status : 413 } ) ;
748747 release ( ) ;
749748 await flush ( ) ;
@@ -762,6 +761,7 @@ describe("the stop written when an owning send is aborted", () => {
762761 let releaseP ! : ( ) => void ;
763762 const hold = new Promise < void > ( ( resolve ) => ( releaseP = resolve ) ) ;
764763 let holding = false ;
764+ let refuse = false ;
765765 let heldP ! : ( ) => void ;
766766 const pHeld = new Promise < void > ( ( resolve ) => ( heldP = resolve ) ) ;
767767 let refused ! : Promise < unknown > ;
@@ -779,16 +779,12 @@ describe("the stop written when an owning send is aborted", () => {
779779 heldP ( ) ;
780780 } ,
781781 } ,
782+ omitAppendLength : ( ) => refuse ,
782783 beforeAbort : async ( { transport, chatId } ) => {
783784 // Counted, and on its way, when the turn is aborted.
785+ refuse = true ;
784786 holding = true ;
785- refused = transport . sendMessages ( {
786- trigger : "submit-message" ,
787- chatId,
788- messageId : undefined ,
789- messages : [ userMessage ( "x" . repeat ( 1_050_000 ) ) ] ,
790- abortSignal : undefined ,
791- } ) ;
787+ refused = sendHi ( transport , chatId ) ;
792788 refused . catch ( ( ) => { } ) ;
793789 await pHeld ;
794790 } ,
@@ -799,6 +795,7 @@ describe("the stop written when an owning send is aborted", () => {
799795 // renewal still held.
800796 releaseP ( ) ;
801797 await expect ( refused ) . rejects . toMatchObject ( { status : 413 } ) ;
798+ refuse = false ;
802799 await senders . sendMessages ! ( { transport, chatId, env } ) ;
803800 release ( ) ;
804801 await flush ( ) ;
@@ -889,10 +886,7 @@ describe("the stop written when an owning send is aborted", () => {
889886
890887 // A later send that fails after a stop is pending, and what each outcome does to that stop.
891888 // This table holds the outcomes only it covers; the rest have a test of their own, which
892- // also reads the wire: a 413 for a body over the limit, in the follow-up test over both
893- // senders; a 413 for a retry sent without a length, in "gives up the stop when the newer
894- // send's landed append is retried without a length"; a 429, in the lost-response 429 test;
895- // a 502, in the 502 gateway test.
889+ // also reads the wire: 413 for a missing length, 429 after a lost response, and 502.
896890 it . each ( [
897891 {
898892 name : "a 401 that its renewed token gets again" ,
0 commit comments