@@ -17,7 +17,11 @@ import {
1717} from "./sqlite.js" ;
1818import { saveAuthProfileStore , updateAuthProfileStoreWithLock } from "./store.js" ;
1919import type { ApiKeyCredential } from "./types.js" ;
20- import { persistAuthProfileBatch , upsertAuthProfileWithLockOrThrow } from "./upsert-with-lock.js" ;
20+ import {
21+ persistAuthProfileBatch ,
22+ upsertAuthProfileAfterLoginWithLockOrThrow ,
23+ upsertAuthProfileWithLockOrThrow ,
24+ } from "./upsert-with-lock.js" ;
2125
2226const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
2327
@@ -170,6 +174,134 @@ describe("auth profile batch persistence", () => {
170174 } ) ;
171175 } ) ;
172176
177+ it ( "replaces one completed login and resets only its existing failure state" , async ( ) => {
178+ await withAgentDir ( async ( agentDir ) => {
179+ const targetId = "openai:target" ;
180+ const siblingId = "openai:sibling" ;
181+ const targetLastUsed = Date . now ( ) - 10_000 ;
182+ const targetLastFailureAt = Date . now ( ) - 5_000 ;
183+ const targetLastProbeAt = Date . now ( ) - 2_000 ;
184+ const siblingBlockedUntil = Date . now ( ) + 120_000 ;
185+ saveAuthProfileStore (
186+ {
187+ version : 1 ,
188+ profiles : {
189+ [ targetId ] : apiKey ( "sk-stale" ) ,
190+ [ siblingId ] : apiKey ( "sk-sibling" ) ,
191+ } ,
192+ order : { openai : [ siblingId , targetId ] } ,
193+ lastGood : { openai : siblingId } ,
194+ usageStats : {
195+ [ targetId ] : {
196+ errorCount : 4 ,
197+ failureCounts : { auth_permanent : 4 } ,
198+ blockedUntil : Date . now ( ) + 60_000 ,
199+ blockedReason : "subscription_limit" ,
200+ blockedSource : "wham" ,
201+ blockedModel : "gpt-5.6" ,
202+ blockedScope : "model" ,
203+ cooldownUntil : Date . now ( ) + 60_000 ,
204+ cooldownReason : "auth_permanent" ,
205+ cooldownModel : "gpt-5.6" ,
206+ disabledUntil : Date . now ( ) + 60_000 ,
207+ disabledReason : "auth_permanent" ,
208+ lastUsed : targetLastUsed ,
209+ lastFailureAt : targetLastFailureAt ,
210+ lastProbeAt : targetLastProbeAt ,
211+ } ,
212+ [ siblingId ] : {
213+ errorCount : 2 ,
214+ blockedUntil : siblingBlockedUntil ,
215+ blockedReason : "subscription_limit" ,
216+ } ,
217+ } ,
218+ } ,
219+ agentDir ,
220+ ) ;
221+
222+ await upsertAuthProfileAfterLoginWithLockOrThrow ( {
223+ agentDir,
224+ profileId : targetId ,
225+ credential : apiKey ( "sk-fresh" ) ,
226+ } ) ;
227+
228+ expect ( loadPersistedAuthProfileStore ( agentDir ) ) . toEqual ( {
229+ version : 1 ,
230+ profiles : {
231+ [ targetId ] : apiKey ( "sk-fresh" ) ,
232+ [ siblingId ] : apiKey ( "sk-sibling" ) ,
233+ } ,
234+ order : { openai : [ siblingId , targetId ] } ,
235+ lastGood : { openai : siblingId } ,
236+ usageStats : {
237+ [ targetId ] : {
238+ errorCount : 0 ,
239+ lastUsed : targetLastUsed ,
240+ lastFailureAt : targetLastFailureAt ,
241+ lastProbeAt : targetLastProbeAt ,
242+ } ,
243+ [ siblingId ] : {
244+ errorCount : 2 ,
245+ blockedUntil : siblingBlockedUntil ,
246+ blockedReason : "subscription_limit" ,
247+ } ,
248+ } ,
249+ } ) ;
250+ } ) ;
251+ } ) ;
252+
253+ it ( "rolls the completed-login credential and state back together on write failure" , async ( ) => {
254+ await withAgentDir ( async ( agentDir ) => {
255+ const profileId = "openai:existing" ;
256+ const disabledUntil = Date . now ( ) + 60_000 ;
257+ saveAuthProfileStore (
258+ {
259+ version : 1 ,
260+ profiles : { [ profileId ] : apiKey ( "sk-stale" ) } ,
261+ usageStats : {
262+ [ profileId ] : {
263+ errorCount : 3 ,
264+ disabledUntil,
265+ disabledReason : "auth_permanent" ,
266+ } ,
267+ } ,
268+ } ,
269+ agentDir ,
270+ ) ;
271+ const database = openOpenClawAgentDatabase ( {
272+ agentId : "work" ,
273+ path : resolveAuthProfileDatabasePath ( agentDir ) ,
274+ } ) ;
275+ database . db . exec ( `
276+ CREATE TRIGGER reject_completed_login_state
277+ BEFORE UPDATE ON auth_profile_state
278+ BEGIN
279+ SELECT RAISE(ABORT, 'injected completed login state failure');
280+ END;
281+ ` ) ;
282+
283+ await expect (
284+ upsertAuthProfileAfterLoginWithLockOrThrow ( {
285+ agentDir,
286+ profileId,
287+ credential : apiKey ( "sk-fresh" ) ,
288+ } ) ,
289+ ) . rejects . toThrow ( "injected completed login state failure" ) ;
290+
291+ expect ( loadPersistedAuthProfileStore ( agentDir ) ) . toEqual ( {
292+ version : 1 ,
293+ profiles : { [ profileId ] : apiKey ( "sk-stale" ) } ,
294+ usageStats : {
295+ [ profileId ] : {
296+ errorCount : 3 ,
297+ disabledUntil,
298+ disabledReason : "auth_permanent" ,
299+ } ,
300+ } ,
301+ } ) ;
302+ } ) ;
303+ } ) ;
304+
173305 it ( "reports the migration remediation instead of lock-contention retry advice" , async ( ) => {
174306 await withAgentDir ( async ( agentDir ) => {
175307 // Credentials still living in the retired JSON store: the write can never
0 commit comments