88 getAgentInvocationTurnId ,
99} from "@/chat/agent-invocations/store" ;
1010import {
11+ buildAgentInvocationInboundMessage ,
1112 createAgentInvocationWorker ,
1213 routeAgentInvocationWork ,
1314 createAndEnqueueAgentInvocation ,
@@ -22,6 +23,7 @@ import { completedAgentRun } from "@/chat/runtime/agent-run-outcome";
2223import { processConversationQueueMessage } from "@/chat/task-execution/vercel-callback" ;
2324import { recoverPendingAgentInvocationMailboxAppends } from "@/chat/agent-dispatch/heartbeat" ;
2425import { CONVERSATION_WORK_MAX_DELIVERY_ATTEMPTS } from "@/chat/task-execution/store" ;
26+ import type { ConversationWorkerContext } from "@/chat/task-execution/worker" ;
2527import {
2628 getAgentTurnSessionRecord ,
2729 upsertAgentTurnSessionRecord ,
@@ -668,4 +670,59 @@ describe("agent invocation conversation work", () => {
668670 await fixture . close ( ) ;
669671 }
670672 } ) ;
673+
674+ it ( "persists invariant failures on the final delivery attempt" , async ( ) => {
675+ const { fixture } = await prepareParentConversation ( ) ;
676+ try {
677+ const created = await createAgentInvocation ( {
678+ ...invocationInput ,
679+ agentName : "researcher" ,
680+ idempotencyKey : "invalid-child-1" ,
681+ } ) ;
682+ const run = vi . fn ( ) ;
683+ const ack = vi . fn ( ) ;
684+ const worker = createAgentInvocationWorker ( { agentRunner : { run } } ) ;
685+ const context = {
686+ attempt : {
687+ ack,
688+ conversationId : created . childConversationId ,
689+ destination,
690+ drain : vi . fn ( ) ,
691+ isFinalAttempt : true ,
692+ messages : [ buildAgentInvocationInboundMessage ( created ) ] ,
693+ } ,
694+ checkIn : vi . fn ( ) ,
695+ conversationId : created . childConversationId ,
696+ destination,
697+ shouldYield : ( ) => false ,
698+ } satisfies ConversationWorkerContext ;
699+
700+ await expect ( worker ( context , created . invocationId ) ) . resolves . toEqual ( {
701+ status : "completed" ,
702+ } ) ;
703+
704+ expect ( run ) . not . toHaveBeenCalled ( ) ;
705+ expect ( ack ) . toHaveBeenCalledOnce ( ) ;
706+ await expect (
707+ getAgentInvocation ( created . invocationId ) ,
708+ ) . resolves . toMatchObject ( {
709+ errorMessage : expect . stringContaining (
710+ "must not own a provider destination" ,
711+ ) ,
712+ status : "failed" ,
713+ } ) ;
714+ await expect (
715+ createAgentInvocation ( {
716+ ...invocationInput ,
717+ agentName : "researcher" ,
718+ idempotencyKey : "invalid-child-2" ,
719+ } ) ,
720+ ) . resolves . toMatchObject ( {
721+ childConversationId : created . childConversationId ,
722+ status : "pending" ,
723+ } ) ;
724+ } finally {
725+ await fixture . close ( ) ;
726+ }
727+ } ) ;
671728} ) ;
0 commit comments