@@ -12,7 +12,7 @@ import {
1212import { getConfigDir , readConfigDiagnostics , saveConfig } from "../src/config" ;
1313import type { ProviderAdapter } from "../src/adapters/base" ;
1414import { handleManagementAPI } from "../src/server/management-api" ;
15- import { saveCredential } from "../src/oauth/store" ;
15+ import { getAccountSet , saveCredential } from "../src/oauth/store" ;
1616import { XAI_OAUTH_DISCOVERY_URL } from "../src/oauth/xai" ;
1717import { XAI_GROK_CLI_BASE_URL } from "../src/providers/xai-transport" ;
1818import type { AdapterEvent , OcxConfig , OcxProviderConfig , OcxProviderContinuationState } from "../src/types" ;
@@ -1895,7 +1895,11 @@ describe("server combo failover 030 activation matrix", () => {
18951895
18961896 test ( "Kiro OAuth continuation follows the account across refresh but not account replacement" , async ( ) => {
18971897 const seen : Array < string | undefined > = [ ] ;
1898- const { clearResponseStateForTests, flushResponseState } = await import ( "../src/responses/state" ) ;
1898+ const {
1899+ clearResponseStateForTests,
1900+ clearResponseStateMemoryForTests,
1901+ flushResponseState,
1902+ } = await import ( "../src/responses/state" ) ;
18991903 clearResponseStateForTests ( ) ;
19001904 customRunTurn = async ( parsed , _incoming , emit ) => {
19011905 const conversationId = parsed . _providerContinuation ?. kiro ?. conversationId ;
@@ -1930,6 +1934,7 @@ describe("server combo failover 030 activation matrix", () => {
19301934 expect ( persisted ) . not . toContain ( "xai-access-one" ) ;
19311935 expect ( persisted ) . not . toContain ( "xai-refresh-one" ) ;
19321936 expect ( persisted ) . not . toContain ( "acct-xai-a" ) ;
1937+ clearResponseStateMemoryForTests ( ) ;
19331938
19341939 await saveCredential ( "xai" , {
19351940 access : "xai-access-two" ,
@@ -1975,6 +1980,78 @@ describe("server combo failover 030 activation matrix", () => {
19751980 expect ( seen ) . toEqual ( [ undefined , "kiro-oauth-conversation" , undefined , undefined ] ) ;
19761981 } ) ;
19771982
1983+ test ( "OAuth credentials without immutable account ids do not inherit provider continuation state" , async ( ) => {
1984+ const seen : Array < string | undefined > = [ ] ;
1985+ const {
1986+ clearResponseStateForTests,
1987+ clearResponseStateMemoryForTests,
1988+ flushResponseState,
1989+ } = await import ( "../src/responses/state" ) ;
1990+ clearResponseStateForTests ( ) ;
1991+ customRunTurn = async ( parsed , _incoming , emit ) => {
1992+ const conversationId = parsed . _providerContinuation ?. kiro ?. conversationId ;
1993+ seen . push ( conversationId ) ;
1994+ emit ( { type : "text_delta" , text : "continued" } ) ;
1995+ emit ( {
1996+ type : "done" ,
1997+ providerState : { kiro : { conversationId : conversationId ?? "identityless-oauth-conversation" } } ,
1998+ } ) ;
1999+ } ;
2000+ const config = comboConfig ( {
2001+ xai : provider ( "openai-chat" , "https://api.x.ai/v1" , "unused" , {
2002+ authMode : "oauth" ,
2003+ note : "test-kiro-oauth" ,
2004+ } ) ,
2005+ } , [ { provider : "xai" , model : "m1" } ] ) ;
2006+ const cases : Array < { name : string ; email ?: string } > = [
2007+ { name : "identityless" } ,
2008+ { name :
"email-only" , email :
"[email protected] " } , 2009+ ] ;
2010+ for ( const testCase of cases ) {
2011+ clearResponseStateForTests ( ) ;
2012+ seen . length = 0 ;
2013+ await saveCredential ( "xai" , {
2014+ access : `${ testCase . name } -access-one` ,
2015+ refresh : `${ testCase . name } -refresh-one` ,
2016+ expires : Date . now ( ) + 3_600_000 ,
2017+ source : "oauth" ,
2018+ ...( testCase . email ? { email : testCase . email } : { } ) ,
2019+ } ) ;
2020+ const firstSlotId = getAccountSet ( "xai" ) ?. activeAccountId ;
2021+ expect ( firstSlotId ) . toBeDefined ( ) ;
2022+
2023+ const first = await post ( config , { store : false , input : "first" } ) ;
2024+ expect ( first . status ) . toBe ( 200 ) ;
2025+ const firstJson = await first . json ( ) as { id : string } ;
2026+ await flushResponseState ( ) ;
2027+ clearResponseStateMemoryForTests ( ) ;
2028+
2029+ await saveCredential ( "xai" , {
2030+ access : `${ testCase . name } -access-two` ,
2031+ refresh : `${ testCase . name } -refresh-two` ,
2032+ expires : Date . now ( ) + 3_600_000 ,
2033+ source : "oauth" ,
2034+ ...( testCase . email ? { email : testCase . email } : { } ) ,
2035+ } ) ;
2036+ expect ( getAccountSet ( "xai" ) ?. activeAccountId ) . toBe ( firstSlotId ) ;
2037+ const second = await post ( config , {
2038+ store : false ,
2039+ previous_response_id : firstJson . id ,
2040+ input : "second" ,
2041+ } ) ;
2042+
2043+ expect ( second . status ) . toBe ( 200 ) ;
2044+ expect ( seen ) . toEqual ( [ undefined , undefined ] ) ;
2045+ await flushResponseState ( ) ;
2046+ const persisted = readFileSync ( join ( getConfigDir ( ) , "responses-state.json" ) , "utf8" ) ;
2047+ expect ( persisted ) . not . toContain ( `${ testCase . name } -access-one` ) ;
2048+ expect ( persisted ) . not . toContain ( `${ testCase . name } -refresh-one` ) ;
2049+ expect ( persisted ) . not . toContain ( `${ testCase . name } -access-two` ) ;
2050+ expect ( persisted ) . not . toContain ( `${ testCase . name } -refresh-two` ) ;
2051+ if ( testCase . email ) expect ( persisted ) . not . toContain ( testCase . email ) ;
2052+ }
2053+ } ) ;
2054+
19782055 test ( "disabled image input rejects an image restored from previous_response_id before dispatch" , async ( ) => {
19792056 const { rememberResponseState } = await import ( "../src/responses/state" ) ;
19802057 rememberResponseState (
0 commit comments