@@ -3,6 +3,8 @@ import fs from "node:fs";
33import path from "node:path" ;
44import { DatabaseSync } from "node:sqlite" ;
55import { afterEach , describe , expect , it , vi } from "vitest" ;
6+ import { listAuthProfileStoresRequiringMigration } from "../agents/auth-profiles/legacy-source-diagnostic.js" ;
7+ import { resolveAuthProfileEligibility } from "../agents/auth-profiles/order.js" ;
68import { loadPersistedAuthProfileStore } from "../agents/auth-profiles/persisted.js" ;
79import { clearRuntimeAuthProfileStoreSnapshots } from "../agents/auth-profiles/runtime-snapshots.js" ;
810import {
@@ -285,8 +287,9 @@ describe("maybeMigrateAuthProfileJsonStoresToSqlite", () => {
285287 type : "oauth" ,
286288 provider : "openai" ,
287289 oauthRef : {
290+ source : "openclaw-credentials" ,
288291 id : "0123456789abcdef0123456789abcdef" ,
289- provider : "openai" ,
292+ provider : "openai-codex " ,
290293 } ,
291294 } ,
292295 } ,
@@ -305,16 +308,21 @@ describe("maybeMigrateAuthProfileJsonStoresToSqlite", () => {
305308 env : state . env ,
306309 } ) ;
307310
308- expect ( result . warnings ) . toEqual ( [
309- expect . stringContaining ( "legacy OAuth sidecar profile" ) ,
310- expect . stringContaining ( "no importable auth profiles or state" ) ,
311- expect . stringContaining ( "Deferred shared legacy OAuth migration" ) ,
312- ] ) ;
313- expect ( loadPersistedAuthProfileStore ( state . agentDir ( ) ) ) . toBeNull ( ) ;
314- expect ( fs . existsSync ( authPath ) ) . toBe ( true ) ;
315- expect ( fs . existsSync ( oauthPath ) ) . toBe ( true ) ;
316- expectNoMigratedArchive ( authPath ) ;
317- expectNoMigratedArchive ( oauthPath ) ;
311+ expect ( result . warnings ) . toEqual ( [ expect . stringContaining ( "legacy OAuth sidecar profile" ) ] ) ;
312+ expect (
313+ loadPersistedAuthProfileStore ( state . agentDir ( ) ) ?. profiles [ "openai:default" ] ,
314+ ) . toMatchObject ( {
315+ type : "oauth" ,
316+ provider : "openai" ,
317+ oauthRef : {
318+ source : "openclaw-credentials" ,
319+ provider : "openai-codex" ,
320+ } ,
321+ } ) ;
322+ expect ( fs . existsSync ( authPath ) ) . toBe ( false ) ;
323+ expect ( fs . existsSync ( oauthPath ) ) . toBe ( false ) ;
324+ expectMigratedArchive ( authPath ) ;
325+ expectMigratedArchive ( oauthPath ) ;
318326 } ) ;
319327
320328 it ( "preserves state-only profile IDs while migrating shared OAuth" , async ( ) => {
@@ -701,7 +709,7 @@ describe("maybeMigrateAuthProfileJsonStoresToSqlite", () => {
701709 expect ( fs . existsSync ( authPath ) ) . toBe ( false ) ;
702710 } ) ;
703711
704- it ( "leaves unresolved legacy OAuth sidecar refs in JSON " , async ( ) => {
712+ it ( "migrates unresolved legacy OAuth sidecar refs so startup can proceed cold " , async ( ) => {
705713 const state = await makeTestState ( ) ;
706714 const authPath = await writeLegacyAuthProfilesJson ( state , {
707715 version : 1 ,
@@ -711,8 +719,9 @@ describe("maybeMigrateAuthProfileJsonStoresToSqlite", () => {
711719 provider : "openai" ,
712720713721 oauthRef : {
722+ source : "openclaw-credentials" ,
714723 id : "0123456789abcdef0123456789abcdef" ,
715- provider : "openai" ,
724+ provider : "openai-codex " ,
716725 } ,
717726 } ,
718727 } ,
@@ -725,14 +734,36 @@ describe("maybeMigrateAuthProfileJsonStoresToSqlite", () => {
725734 } ) ;
726735
727736 expect ( result . detected ) . toEqual ( [ authPath ] ) ;
728- expect ( result . changes ) . toEqual ( [ ] ) ;
737+ expect ( result . changes ) . toEqual ( [ expect . stringContaining ( "Migrated auth profile JSON" ) ] ) ;
729738 expect ( result . warnings ) . toEqual ( [
730- expect . stringContaining ( "legacy OAuth sidecar profile" ) ,
731- expect . stringContaining ( "no importable auth profiles or state" ) ,
739+ expect . stringContaining ( "Migrated 1 legacy OAuth sidecar profile" ) ,
732740 ] ) ;
733- expect ( loadPersistedAuthProfileStore ( state . agentDir ( ) ) ) . toBeNull ( ) ;
734- expect ( fs . existsSync ( authPath ) ) . toBe ( true ) ;
735- expectNoMigratedArchive ( authPath ) ;
741+ expect ( result . warnings [ 0 ] ) . toContain ( "re-authenticate" ) ;
742+ const store = loadPersistedAuthProfileStore ( state . agentDir ( ) ) ;
743+ expect ( store ?. profiles [ "openai:[email protected] " ] ) . toMatchObject ( { 744+ type : "oauth" ,
745+ provider : "openai" ,
746+ 747+ oauthRef : {
748+ source : "openclaw-credentials" ,
749+ provider : "openai-codex" ,
750+ } ,
751+ } ) ;
752+ expect (
753+ resolveAuthProfileEligibility ( {
754+ store : store ! ,
755+ provider : "openai" ,
756+ profileId :
"openai:[email protected] " , 757+ } ) ,
758+ ) . toEqual ( { eligible : false , reasonCode : "unresolved_ref" } ) ;
759+ expect ( fs . existsSync ( authPath ) ) . toBe ( false ) ;
760+ expectMigratedArchive ( authPath ) ;
761+ expect (
762+ listAuthProfileStoresRequiringMigration ( {
763+ agentDirs : [ state . agentDir ( ) ] ,
764+ env : state . env ,
765+ } ) ,
766+ ) . toEqual ( [ ] ) ;
736767 } ) ;
737768
738769 it ( "imports valid profiles when one legacy OAuth sidecar ref is unresolved" , async ( ) => {
@@ -750,8 +781,8 @@ describe("maybeMigrateAuthProfileJsonStoresToSqlite", () => {
750781 provider : "openai" ,
751782752783 oauthRef : {
753- id : "0123456789abcdef0123456789abcdef " ,
754- provider : "openai" ,
784+ source : "openclaw-credentials " ,
785+ provider : "openai-codex " ,
755786 } ,
756787 } ,
757788 } ,
@@ -774,19 +805,28 @@ describe("maybeMigrateAuthProfileJsonStoresToSqlite", () => {
774805 provider : "openai" ,
775806 key : "sk-imported" ,
776807 } ,
808+ 809+ type : "oauth" ,
810+ provider : "openai" ,
811+ 812+ oauthRef : {
813+ source : "openclaw-credentials" ,
814+ id : "0123456789abcdef0123456789abcdef" ,
815+ provider : "openai-codex" ,
816+ } ,
817+ } ,
777818 } ,
778- order : { openai : [ "openai:default" ] } ,
819+ order :
{ openai :
[ "openai:default" , "openai:[email protected] " ] } , 779820 lastGood : { openai : "openai:default" } ,
780821 } ) ;
781- expect ( fs . existsSync ( authPath ) ) . toBe ( true ) ;
782- expectNoMigratedArchive ( authPath ) ;
783- const remaining = JSON . parse ( fs . readFileSync ( authPath , "utf8" ) ) ;
784- expect ( remaining . profiles ) . toHaveProperty ( "openai:default" ) ;
785- expect ( remaining . profiles ) . toHaveProperty ( "openai:[email protected] " ) ; 786- expect ( remaining . order ) . toEqual ( {
787- openai :
[ "openai:default" , "openai:[email protected] " ] , 788- } ) ;
789- expect ( remaining . lastGood ) . toEqual ( { openai : "openai:default" } ) ;
822+ expect ( fs . existsSync ( authPath ) ) . toBe ( false ) ;
823+ expectMigratedArchive ( authPath ) ;
824+ expect (
825+ listAuthProfileStoresRequiringMigration ( {
826+ agentDirs : [ state . agentDir ( ) ] ,
827+ env : state . env ,
828+ } ) ,
829+ ) . toEqual ( [ ] ) ;
790830 } ) ;
791831
792832 it ( "keeps existing SQLite credentials when importing stale JSON" , async ( ) => {
@@ -2031,7 +2071,7 @@ describe("legacy OpenAI auth profiles through the canonical migration owner", ()
20312071 } ) ;
20322072 } ) ;
20332073
2034- it ( "does not rebind unresolved sidecar accounts when another Codex account migrates " , async ( ) => {
2074+ it ( "canonicalizes unresolved sidecar accounts while keeping them cold " , async ( ) => {
20352075 const state = await makeTestState ( ) ;
20362076 const storePath = path . join ( state . sessionsDir ( ) , "sessions.json" ) ;
20372077 const readySessionKey = "agent:main:main" ;
@@ -2089,7 +2129,7 @@ describe("legacy OpenAI auth profiles through the canonical migration owner", ()
20892129 authProfileIdMap : profileIdMap ,
20902130 } ) ;
20912131
2092- expect ( result . repairedSessions ) . toBe ( 1 ) ;
2132+ expect ( result . repairedSessions ) . toBe ( 2 ) ;
20932133 expect (
20942134 loadSessionEntry ( { storePath, sessionKey : readySessionKey , env : state . env } ) ,
20952135 ) . toMatchObject ( {
@@ -2098,9 +2138,17 @@ describe("legacy OpenAI auth profiles through the canonical migration owner", ()
20982138 expect (
20992139 loadSessionEntry ( { storePath, sessionKey : pendingSessionKey , env : state . env } ) ,
21002140 ) . toMatchObject ( {
2101- authProfileOverride : "openai-codex :pending" ,
2141+ authProfileOverride : "openai:pending" ,
21022142 authProfileOverrideSource : "user" ,
21032143 } ) ;
2144+ const coldStore = loadPersistedAuthProfileStore ( state . agentDir ( ) ) ;
2145+ expect (
2146+ resolveAuthProfileEligibility ( {
2147+ store : coldStore ! ,
2148+ provider : "openai" ,
2149+ profileId : "openai:pending" ,
2150+ } ) ,
2151+ ) . toEqual ( { eligible : false , reasonCode : "missing_credential" } ) ;
21042152 } ) ;
21052153
21062154 it ( "repairs an already-migrated selected account from its verified auth archive" , async ( ) => {
@@ -2265,6 +2313,7 @@ describe("legacy OpenAI auth profiles through the canonical migration owner", ()
22652313 provider : "openai-codex" ,
22662314 accountId : "failed-different-account" ,
22672315 oauthRef : {
2316+ source : "openclaw-credentials" ,
22682317 id : "0123456789abcdef0123456789abcdef" ,
22692318 provider : "openai-codex" ,
22702319 } ,
@@ -2326,8 +2375,8 @@ describe("legacy OpenAI auth profiles through the canonical migration owner", ()
23262375 shouldRepair : true ,
23272376 authProfileIdMap : profileIdMap ,
23282377 } ) ;
2329- expect ( repair . repairedSessions ) . toBe ( 3 ) ;
2330- for ( const agentId of [ "main" , "inherited" , "dedup" ] ) {
2378+ expect ( repair . repairedSessions ) . toBe ( 4 ) ;
2379+ for ( const agentId of [ "main" , "failed" , " inherited", "dedup" ] ) {
23312380 expect (
23322381 loadSessionEntry ( {
23332382 storePath : path . join ( state . sessionsDir ( agentId ) , "sessions.json" ) ,
@@ -2339,15 +2388,16 @@ describe("legacy OpenAI auth profiles through the canonical migration owner", ()
23392388 authProfileOverrideSource : "user" ,
23402389 } ) ;
23412390 }
2342- expect (
2343- loadSessionEntry ( {
2344- storePath : path . join ( state . sessionsDir ( "failed" ) , "sessions.json" ) ,
2345- sessionKey : "agent:failed:main" ,
2346- env : state . env ,
2347- } ) ,
2348- ) . toMatchObject ( {
2349- authProfileOverride : "openai-codex:shared" ,
2350- authProfileOverrideSource : "user" ,
2391+ expect ( loadPersistedAuthProfileStore ( state . agentDir ( "failed" ) ) ?. profiles ) . toMatchObject ( {
2392+ "openai:shared" : {
2393+ type : "oauth" ,
2394+ provider : "openai" ,
2395+ accountId : "failed-different-account" ,
2396+ oauthRef : {
2397+ source : "openclaw-credentials" ,
2398+ provider : "openai-codex" ,
2399+ } ,
2400+ } ,
23512401 } ) ;
23522402 } ) ;
23532403
0 commit comments