@@ -16,6 +16,7 @@ import { GatewayClientRequestError } from "../../gateway/client.js";
1616import { isAgentSessionModelPatchOrigin } from "../../gateway/session-model-patch-origin.js" ;
1717import { beginSessionWorkAdmission } from "../../sessions/session-lifecycle-admission.js" ;
1818import { withTestDir } from "../../test-helpers/temp-dir.js" ;
19+ import * as failoverErrors from "../failover-error.js" ;
1920import { createAgentPatchedSessionModelRunGuard } from "../session-model-auto-revert.js" ;
2021import { withGatewayToolCallerIdentity } from "./gateway-caller-context.js" ;
2122import type { AgentToolGatewayRequestCaller } from "./in-process-gateway.js" ;
@@ -606,28 +607,26 @@ describe("sessions tool", () => {
606607 session : { store : storePath } ,
607608 agents : { defaults : { model : { primary : "openai/good" } } } ,
608609 } ;
609- await upsertSessionEntryCore (
610- { agentId : "main" , sessionKey, storePath } ,
611- {
612- sessionId : "session-main" ,
613- updatedAt : 1 ,
614- model : "good" ,
615- modelProvider : "openai" ,
616- modelOverride : "good" ,
617- providerOverride : "openai" ,
618- modelOverrideSource : "auto" ,
619- modelOverrideFallbackOriginProvider : "openai" ,
620- modelOverrideFallbackOriginModel : "primary" ,
621- authProfileOverride : "good-profile" ,
622- authProfileOverrideSource : "user" ,
623- thinkingLevel : "high" ,
624- } ,
625- ) ;
610+ const sessionScope = { agentId : "main" , sessionKey, storePath } ;
611+ await upsertSessionEntryCore ( sessionScope , {
612+ sessionId : "session-main" ,
613+ updatedAt : 1 ,
614+ model : "good" ,
615+ modelProvider : "openai" ,
616+ modelOverride : "good" ,
617+ providerOverride : "openai" ,
618+ modelOverrideSource : "auto" ,
619+ modelOverrideFallbackOriginProvider : "openai" ,
620+ modelOverrideFallbackOriginModel : "primary" ,
621+ authProfileOverride : "good-profile" ,
622+ authProfileOverrideSource : "user" ,
623+ thinkingLevel : "high" ,
624+ } ) ;
626625 const callGateway = vi . fn (
627626 async ( request : { method : string ; params : Record < string , unknown > } ) => {
628627 expect ( request . method ) . toBe ( "sessions.patch" ) ;
629628 expect ( isAgentSessionModelPatchOrigin ( ) ) . toBe ( true ) ;
630- await patchSessionEntryCore ( { agentId : "main" , sessionKey , storePath } , ( ) => ( {
629+ await patchSessionEntryCore ( sessionScope , ( ) => ( {
631630 label : request . params . label as string ,
632631 model : "bad" ,
633632 modelProvider : "broken" ,
@@ -662,12 +661,9 @@ describe("sessions tool", () => {
662661 config : cfg ,
663662 callGateway : callGateway as never ,
664663 } ) ;
665- const currentRunGuard = createAgentPatchedSessionModelRunGuard ( {
666- cfg,
667- agentId : "main" ,
668- sessionKey,
669- storePath,
670- } ) ;
664+ const guardParams = { cfg, ...sessionScope } ;
665+ const currentRunGuard = createAgentPatchedSessionModelRunGuard ( guardParams ) ;
666+ const failedCurrentRunGuard = createAgentPatchedSessionModelRunGuard ( guardParams ) ;
671667
672668 await tool . execute ( "patch-model" , {
673669 action : "patch" ,
@@ -683,7 +679,7 @@ describe("sessions tool", () => {
683679 model : "broken/bad" ,
684680 } ,
685681 } ) ;
686- expect ( loadSessionEntry ( { agentId : "main" , sessionKey , storePath } ) ) . toMatchObject ( {
682+ expect ( loadSessionEntry ( sessionScope ) ) . toMatchObject ( {
687683 label : "Research" ,
688684 modelFallback : {
689685 prevModel : "good" ,
@@ -697,18 +693,36 @@ describe("sessions tool", () => {
697693 } ,
698694 } ) ;
699695 await currentRunGuard . finish ( true ) ;
700- expect ( loadSessionEntry ( { agentId : "main" , sessionKey, storePath } ) ) . toHaveProperty (
701- "modelFallback" ,
702- ) ;
696+ expect ( loadSessionEntry ( sessionScope ) ) . toHaveProperty ( "modelFallback" ) ;
697+
698+ const failure = { status : 404 , message : "No endpoints found for broken/bad." } ;
699+ const entryBeforeFailure = loadSessionEntry ( sessionScope ) ;
700+ const transcriptScope = { ...sessionScope , sessionId : "session-main" } ;
701+ const transcriptBeforeFailure = await loadTranscriptEvents ( transcriptScope ) ;
702+ const classifyFailure = vi
703+ . spyOn ( failoverErrors , "resolveFailoverReasonFromError" )
704+ . mockImplementation ( ( ) => {
705+ throw new Error ( "An unarmed model-patch guard must not classify failures" ) ;
706+ } ) ;
707+ try {
708+ expect ( failedCurrentRunGuard . captureFallbackFailure ( [ ] ) ) . toBeUndefined ( ) ;
709+ expect ( failedCurrentRunGuard . captureFailure ( failure ) ) . toBe ( false ) ;
710+ expect (
711+ failedCurrentRunGuard . captureFallbackFailure ( [
712+ { error : failure . message , reason : "model_not_found" } ,
713+ ] ) ,
714+ ) . toBe ( false ) ;
715+ await failedCurrentRunGuard . fail ( failure ) ;
716+ expect ( classifyFailure ) . not . toHaveBeenCalled ( ) ;
717+ } finally {
718+ classifyFailure . mockRestore ( ) ;
719+ }
720+ expect ( loadSessionEntry ( sessionScope ) ) . toEqual ( entryBeforeFailure ) ;
721+ expect ( await loadTranscriptEvents ( transcriptScope ) ) . toEqual ( transcriptBeforeFailure ) ;
703722
704- const runGuard = createAgentPatchedSessionModelRunGuard ( {
705- cfg,
706- agentId : "main" ,
707- sessionKey,
708- storePath,
709- } ) ;
710- await runGuard . fail ( { status : 404 , message : "No endpoints found for broken/bad." } ) ;
711- expect ( loadSessionEntry ( { agentId : "main" , sessionKey, storePath } ) ) . toMatchObject ( {
723+ const runGuard = createAgentPatchedSessionModelRunGuard ( guardParams ) ;
724+ await runGuard . fail ( failure ) ;
725+ expect ( loadSessionEntry ( sessionScope ) ) . toMatchObject ( {
712726 model : "good" ,
713727 modelProvider : "openai" ,
714728 modelOverrideSource : "auto" ,
@@ -717,15 +731,8 @@ describe("sessions tool", () => {
717731 authProfileOverride : "good-profile" ,
718732 thinkingLevel : "high" ,
719733 } ) ;
720- expect ( loadSessionEntry ( { agentId : "main" , sessionKey, storePath } ) ) . not . toHaveProperty (
721- "modelFallback" ,
722- ) ;
723- const events = await loadTranscriptEvents ( {
724- agentId : "main" ,
725- sessionId : "session-main" ,
726- sessionKey,
727- storePath,
728- } ) ;
734+ expect ( loadSessionEntry ( sessionScope ) ) . not . toHaveProperty ( "modelFallback" ) ;
735+ const events = await loadTranscriptEvents ( transcriptScope ) ;
729736 expect ( events ) . toContainEqual (
730737 expect . objectContaining ( {
731738 message : expect . objectContaining ( {
0 commit comments