@@ -136,6 +136,105 @@ describe("waitForQaTransportOutboundSequence", () => {
136136 } ) ;
137137 } ) ;
138138
139+ it . each ( [
140+ { description : "before the final marker" , failureBeforeFinal : true } ,
141+ { description : "after the final marker" , failureBeforeFinal : false } ,
142+ ] ) ( "rejects an owned-account failure reply $description" , async ( { failureBeforeFinal } ) => {
143+ const state = createQaBusState ( ) ;
144+ const preview = state . addOutboundMessage ( {
145+ accountId : "default" ,
146+ to : "dm:alice" ,
147+ text : "owned preview" ,
148+ } ) ;
149+ const addFailure = ( ) =>
150+ state . addOutboundMessage ( {
151+ accountId : "default" ,
152+ to : "dm:alice" ,
153+ text : "⚠️ agent failed before reply: provider rejected this request" ,
154+ } ) ;
155+
156+ if ( failureBeforeFinal ) {
157+ addFailure ( ) ;
158+ }
159+ state . editMessage ( {
160+ accountId : "default" ,
161+ messageId : preview . id ,
162+ text : "final marker" ,
163+ } ) ;
164+ if ( ! failureBeforeFinal ) {
165+ addFailure ( ) ;
166+ }
167+
168+ await expect (
169+ waitForQaTransportOutboundSequence ( {
170+ accountId : "default" ,
171+ input : {
172+ conversationId : "alice" ,
173+ finalSettleMs : 0 ,
174+ finalTextIncludes : "final marker" ,
175+ minimumPreviewEvents : 1 ,
176+ timeoutMs : 25 ,
177+ } ,
178+ readEvents : ( ) => state . getSnapshot ( ) . events ,
179+ } ) ,
180+ ) . rejects . toThrow ( "provider rejected this request" ) ;
181+ } ) ;
182+
183+ it ( "ignores stale, foreign-account, and inbound failure replies" , async ( ) => {
184+ const state = createQaBusState ( ) ;
185+ state . addOutboundMessage ( {
186+ accountId : "default" ,
187+ to : "dm:alice" ,
188+ text : "⚠️ agent failed before reply: stale failure" ,
189+ } ) ;
190+ const sinceCursor = state . getSnapshot ( ) . cursor ;
191+
192+ state . addOutboundMessage ( {
193+ accountId : "other" ,
194+ to : "dm:alice" ,
195+ text : "⚠️ agent failed before reply: foreign account failure" ,
196+ } ) ;
197+ const inbound = state . addInboundMessage ( {
198+ accountId : "default" ,
199+ conversation : { id : "alice" , kind : "direct" } ,
200+ senderId : "alice" ,
201+ text : "⚠️ agent failed before reply: inbound failure" ,
202+ } ) ;
203+ state . editMessage ( {
204+ accountId : "default" ,
205+ messageId : inbound . id ,
206+ text : "⚠️ agent failed before reply: edited inbound failure" ,
207+ } ) ;
208+ const preview = state . addOutboundMessage ( {
209+ accountId : "default" ,
210+ to : "dm:alice" ,
211+ text : "owned preview" ,
212+ } ) ;
213+ state . editMessage ( {
214+ accountId : "default" ,
215+ messageId : preview . id ,
216+ text : "final marker" ,
217+ } ) ;
218+
219+ await expect (
220+ waitForQaTransportOutboundSequence ( {
221+ accountId : "default" ,
222+ input : {
223+ conversationId : "alice" ,
224+ finalSettleMs : 0 ,
225+ finalTextIncludes : "final marker" ,
226+ minimumPreviewEvents : 1 ,
227+ sinceCursor,
228+ timeoutMs : 25 ,
229+ } ,
230+ readEvents : ( ) => state . getSnapshot ( ) . events ,
231+ } ) ,
232+ ) . resolves . toMatchObject ( {
233+ events : [ { kind : "sent" } , { kind : "edited" } ] ,
234+ final : { accountId : "default" , direction : "outbound" , id : preview . id } ,
235+ } ) ;
236+ } ) ;
237+
139238 it ( "does not accept a matching preview that is deleted during final settling" , async ( ) => {
140239 const state = createQaBusState ( ) ;
141240 const preview = state . addOutboundMessage ( {
0 commit comments