Skip to content

Commit 5d48a2e

Browse files
committed
refactor(auth): dedupe blocked profile stats construction
1 parent b49395d commit 5d48a2e

1 file changed

Lines changed: 42 additions & 32 deletions

File tree

‎src/agents/auth-profiles/usage.ts‎

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,34 @@ export async function markAuthProfileFailure(params: {
834834
}
835835
}
836836

837+
function buildBlockedProfileUsageStats(params: {
838+
previousStats: ProfileUsageStats | undefined;
839+
blockedUntil: number;
840+
source: AuthProfileBlockedSource;
841+
modelId: string | undefined;
842+
now: number;
843+
}): ProfileUsageStats {
844+
const activeBlockedUntil = resolveActiveWindowUntil(
845+
params.previousStats?.blockedUntil,
846+
params.now,
847+
);
848+
return {
849+
...params.previousStats,
850+
blockedUntil: Math.max(activeBlockedUntil, params.blockedUntil),
851+
blockedReason: "subscription_limit",
852+
blockedSource: params.source,
853+
blockedModel: params.modelId,
854+
cooldownUntil: undefined,
855+
cooldownReason: undefined,
856+
cooldownModel: undefined,
857+
lastFailureAt: params.now,
858+
failureCounts: {
859+
...params.previousStats?.failureCounts,
860+
rate_limit: (params.previousStats?.failureCounts?.rate_limit ?? 0) + 1,
861+
},
862+
};
863+
}
864+
837865
/** Marks a profile blocked until a provider-reported reset timestamp. */
838866
export async function markAuthProfileBlockedUntil(params: {
839867
store: AuthProfileStore;
@@ -870,22 +898,13 @@ export async function markAuthProfileBlockedUntil(params: {
870898
}
871899
previousStats = freshStore.usageStats?.[profileId];
872900
updateTime = now;
873-
const activeBlockedUntil = resolveActiveWindowUntil(previousStats?.blockedUntil, now);
874-
nextStats = {
875-
...previousStats,
876-
blockedUntil: Math.max(activeBlockedUntil, blockedUntil),
877-
blockedReason: "subscription_limit",
878-
blockedSource: source,
879-
blockedModel: modelId,
880-
cooldownUntil: undefined,
881-
cooldownReason: undefined,
882-
cooldownModel: undefined,
883-
lastFailureAt: now,
884-
failureCounts: {
885-
...previousStats?.failureCounts,
886-
rate_limit: (previousStats?.failureCounts?.rate_limit ?? 0) + 1,
887-
},
888-
};
901+
nextStats = buildBlockedProfileUsageStats({
902+
previousStats,
903+
blockedUntil,
904+
source,
905+
modelId,
906+
now,
907+
});
889908
updateUsageStatsEntry(freshStore, profileId, () => nextStats as ProfileUsageStats);
890909
return true;
891910
},
@@ -914,22 +933,13 @@ export async function markAuthProfileBlockedUntil(params: {
914933
return;
915934
}
916935
previousStats = store.usageStats?.[profileId];
917-
const activeBlockedUntil = resolveActiveWindowUntil(previousStats?.blockedUntil, now);
918-
nextStats = {
919-
...previousStats,
920-
blockedUntil: Math.max(activeBlockedUntil, blockedUntil),
921-
blockedReason: "subscription_limit",
922-
blockedSource: source,
923-
blockedModel: modelId,
924-
cooldownUntil: undefined,
925-
cooldownReason: undefined,
926-
cooldownModel: undefined,
927-
lastFailureAt: now,
928-
failureCounts: {
929-
...previousStats?.failureCounts,
930-
rate_limit: (previousStats?.failureCounts?.rate_limit ?? 0) + 1,
931-
},
932-
};
936+
nextStats = buildBlockedProfileUsageStats({
937+
previousStats,
938+
blockedUntil,
939+
source,
940+
modelId,
941+
now,
942+
});
933943
updateUsageStatsEntry(store, profileId, () => nextStats as ProfileUsageStats);
934944
authProfileUsageDeps.saveAuthProfileStore(store, agentDir);
935945
logAuthProfileFailureStateChange({

0 commit comments

Comments
 (0)