The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a sign-in offer from the previous backend never opens
alt-glitch9a39f9e380ed
diff --git a/apps/desktop/src/components/free-tier/sign-in-dialog.tsx b/apps/desktop/src/components/free-tier/sign-in-dialog.tsxindex 1a4f80c1d67..288f4ade819 100644--- a/apps/desktop/src/components/free-tier/sign-in-dialog.tsx+++ b/apps/desktop/src/components/free-tier/sign-in-dialog.tsx@@ -36,6 +36,7 @@ import {freeTierSignInClaim,type FreeTierSignInFailure,releaseFreeTierSignIn,+ sameGatewayRoute,stopFreeTierOffer,syncFreeTierOffer} from '@/store/free-tier-sign-in'@@ -98,7 +99,7 @@ export function FreeTierSignInDialog({ onSelectModel }: FreeTierSignInDialogProp$freeTierStatus.listen(sync),$onboardingGate.listen(sync),$onboardingSurfaces.listen(sync),- $freeTierTurnCompleted.listen(() => void refreshFreeTierStatus(requestGateway))+ $freeTierTurnCompleted.listen(() => void refreshFreeTierStatus(requestGateway, sameGatewayRoute()))]sync()diff --git a/apps/desktop/src/store/free-tier-offer.test.ts b/apps/desktop/src/store/free-tier-offer.test.tsindex 94cbf3ae79b..613a3f28318 100644--- a/apps/desktop/src/store/free-tier-offer.test.ts+++ b/apps/desktop/src/store/free-tier-offer.test.ts@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { $freeTierStatus, type FreeTierRequester } from '@/store/free-tier'import { $freeTierSignIn, closeFreeTierSignIn, stopFreeTierOffer, syncFreeTierOffer } from '@/store/free-tier-sign-in'+import { activeGatewayProfileKey, ensureGatewayForProfile } from '@/store/gateway'import { $onboardingGate } from '@/store/onboarding-gate'import type { FreeTierStatus } from '@/types/hermes'@@ -16,12 +17,20 @@ const status = (nudge_due_in: null | number, available = true): FreeTierStatus =})// The backend's two answers: what `free_tier.status` reads now, and whether this caller won the claim.-function gateway({ claimed = true, statuses }: { claimed?: boolean; statuses: FreeTierStatus[] }) {+function gateway({+ claim,+ claimed = true,+ statuses+}: {+ claim?: Promise<{ claimed: boolean }>+ claimed?: boolean+ statuses: FreeTierStatus[]+}) {const queue = [...statuses]const requestGateway = vi.fn(async (method: string) => {if (method === 'free_tier.claim_nudge') {- return { claimed }+ return claim ?? { claimed }}return queue.length > 1 ? queue.shift() : queue[0]@@ -47,6 +56,7 @@ beforeEach(() => {afterEach(() => {stop()+ stop = () => undefinedstopFreeTierOffer()closeFreeTierSignIn()$freeTierStatus.set(null)@@ -113,4 +123,20 @@ describe('sign-in offer after a finished task', () => {expect(claims(requestGateway)).toBe(0)expect($freeTierSignIn.get()).toEqual({ status: 'closed' })})++ it('a claim answered after the gateway route changed does not open the offer', async () => {+ let answer: (value: { claimed: boolean }) => void = () => undefined+ const claim = new Promise<{ claimed: boolean }>(resolve => (answer = resolve))+ const requestGateway = gateway({ claim, statuses: [status(null)] })++ syncFreeTierOffer(status(0), requestGateway)+ await vi.advanceTimersByTimeAsync(0)+ // Re-selecting the active route is a route change: it starts a new activation.+ await ensureGatewayForProfile(activeGatewayProfileKey())+ answer({ claimed: true })+ await vi.advanceTimersByTimeAsync(0)++ expect(claims(requestGateway)).toBe(1)+ expect($freeTierSignIn.get()).toEqual({ status: 'closed' })+ })})diff --git a/apps/desktop/src/store/free-tier-sign-in.ts b/apps/desktop/src/store/free-tier-sign-in.tsindex 391b25e75d9..c9c364cf0d5 100644--- a/apps/desktop/src/store/free-tier-sign-in.ts+++ b/apps/desktop/src/store/free-tier-sign-in.ts@@ -2,6 +2,7 @@ import type { FreeTierClaimNudgeResult } from '@hermes/shared'import { atom } from 'nanostores'import { cancelOAuthSession, listOAuthProviders, pollOAuthSession, startOAuthLogin } from '@/hermes'+import { gatewayActivationEpoch } from '@/store/gateway'import type { FreeTierStatus } from '@/types/hermes'import { $freeTierStatus, type FreeTierRequester, NOUS_PROVIDER_ID, refreshFreeTierStatus } from './free-tier'@@ -93,6 +94,14 @@ function clearTimers() {const set = (state: FreeTierSignInState) => $freeTierSignIn.set(state)+// `requestGateway` keeps one identity across backend switches, so a reply is tied to the active route+// it was asked under: a late answer from the previous connection or profile must not land on this one.+export function sameGatewayRoute(): () => boolean {+ const epoch = gatewayActivationEpoch()++ return () => gatewayActivationEpoch() === epoch+}+const fail = (kind: FreeTierSignInFailure, message: null | string = null, retryAfter = 0) => {clearTimers()set({@@ -115,6 +124,7 @@ export function openFreeTierSignIn() {let offerTimer: number | null = nulllet offerTimerFor: FreeTierStatus | null = null+let offerTimerRoute: () => boolean = () => falselet offerClaiming = falsefunction clearOfferTimer() {@@ -148,7 +158,7 @@ export function stopFreeTierOffer() {*/export function syncFreeTierOffer(status: FreeTierStatus | null, requestGateway: FreeTierRequester) {// A re-sync on the same read (an onboarding change) keeps its running timer, so it is not pushed back.- if (offerTimer !== null && status === offerTimerFor) {+ if (offerTimer !== null && status === offerTimerFor && offerTimerRoute()) {return}@@ -160,10 +170,15 @@ export function syncFreeTierOffer(status: FreeTierStatus | null, requestGateway:}if (dueIn > 0) {+ const isCurrent = sameGatewayRoute()offerTimerFor = status+ offerTimerRoute = isCurrentofferTimer = window.setTimeout(() => {offerTimer = null- void refreshFreeTierStatus(requestGateway)++ if (isCurrent()) {+ void refreshFreeTierStatus(requestGateway, isCurrent)+ }}, dueIn * 1000)return@@ -180,6 +195,7 @@ async function claimFreeTierOffer(requestGateway: FreeTierRequester) {return}+ const isCurrent = sameGatewayRoute()offerClaiming = truelet claimed: boolean@@ -192,12 +208,16 @@ async function claimFreeTierOffer(requestGateway: FreeTierRequester) {offerClaiming = false}+ if (!isCurrent()) {+ return+ }+if (claimed && $freeTierSignIn.get().status === 'closed') {set({ status: 'offer' })}// The claim settled the offer either way; re-read so the cached `nudge_due_in` drops.- void refreshFreeTierStatus(requestGateway)+ void refreshFreeTierStatus(requestGateway, isCurrent)}/** Close and abandon. Cancels a live device-code session so the backend is not