Skip to content

The agent that grows with you

refactor(desktop): add retainConfigReadOrigin for derived config records

kshitijk4poor078af0a49094
diff --git a/apps/desktop/src/api/config.ts b/apps/desktop/src/api/config.ts
index 0fe7bd2109a..7423e7ec151 100644
--- a/apps/desktop/src/api/config.ts
+++ b/apps/desktop/src/api/config.ts
@@ -29,6 +29,17 @@ export function peekConfigReadOrigin(record: object | undefined | null): ConfigR
return record ? configReadOrigins.get(record) : undefined
}
+/** Carry `source`'s read origin onto a record derived from it, so the next write still routes to the gateway that served the GET. */
+export function retainConfigReadOrigin<T extends object>(next: T, source: object | null | undefined): T {
+ const origin = peekConfigReadOrigin(source)
+
+ if (origin) {
+ bindConfigReadOrigin(next, origin)
+ }
+
+ return next
+}
+
/**
* Route a config write to the identity that served the matching read.
* An explicit `{ connectionId, profile }` pin wins. A GET-derived record
diff --git a/apps/desktop/src/app/hooks/use-config-record.ts b/apps/desktop/src/app/hooks/use-config-record.ts
index 3b291b254db..162a8e38d0a 100644
--- a/apps/desktop/src/app/hooks/use-config-record.ts
+++ b/apps/desktop/src/app/hooks/use-config-record.ts
@@ -1,7 +1,7 @@
import { useQuery } from '@tanstack/react-query'
import { type ProfileScope, profileScopeKey } from '@/api/client'
-import { bindConfigReadOrigin, peekConfigReadOrigin } from '@/api/config'
+import { peekConfigReadOrigin, retainConfigReadOrigin } from '@/api/config'
import { getHermesConfigRecord } from '@/hermes'
import { queryClient } from '@/lib/query-client'
import type { HermesConfigRecord } from '@/types/hermes'
@@ -66,13 +66,8 @@ const writeHermesConfigCache = (key: ReturnType<typeof hermesConfigKey>) =>
(next: HermesConfigRecord | undefined | ((previous: HermesConfigRecord | undefined) => HermesConfigRecord | undefined)) =>
void queryClient.setQueryData<HermesConfigRecord>(key, previous => {
const record = typeof next === 'function' ? next(previous) : next
- const origin = peekConfigReadOrigin(previous)
- if (record && origin) {
- bindConfigReadOrigin(record, origin)
- }
-
- return record
+ return record ? retainConfigReadOrigin(record, previous) : record
})
export const setHermesConfigCache = writeHermesConfigCache(HERMES_CONFIG_KEY)
diff --git a/apps/desktop/src/app/settings/sessions-settings.tsx b/apps/desktop/src/app/settings/sessions-settings.tsx
index 6595b8f1c68..8e8f08da5dd 100644
--- a/apps/desktop/src/app/settings/sessions-settings.tsx
+++ b/apps/desktop/src/app/settings/sessions-settings.tsx
@@ -5,11 +5,11 @@ import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Tip } from '@/components/ui/tooltip'
import {
- bindConfigReadOrigin,
deleteSession,
getHermesConfigRecord,
listAllProfileSessions,
peekConfigReadOrigin,
+ retainConfigReadOrigin,
saveHermesConfig,
setSessionArchived
} from '@/hermes'
@@ -258,13 +258,8 @@ function AutoArchiveSetting() {
// the replacement snapshot so the next save still targets the gateway
// that served the original GET.
const writeScope = peekConfigReadOrigin(config)
- const updated = { ...config, sessions }
- if (writeScope) {
- bindConfigReadOrigin(updated, writeScope)
- }
-
- setConfig(updated)
+ setConfig(retainConfigReadOrigin({ ...config, sessions }, config))
try {
// Sparse patch: PUT /api/config deep-merges, and echoing the cached
diff --git a/apps/desktop/src/i18n/context.tsx b/apps/desktop/src/i18n/context.tsx
index cb863a70d88..9935dc4312d 100644
--- a/apps/desktop/src/i18n/context.tsx
+++ b/apps/desktop/src/i18n/context.tsx
@@ -2,10 +2,10 @@ import { applyDocumentLocale, isRecord } from '@hermes/shared/i18n'
import { createContext, type ReactNode, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'
import {
- bindConfigReadOrigin,
getHermesConfigRecord,
type HermesConfigRecord,
peekConfigReadOrigin,
+ retainConfigReadOrigin,
saveHermesConfig
} from '@/hermes'
@@ -53,20 +53,16 @@ export function getConfigDisplayLanguage(config: HermesConfigRecord): unknown {
export function withConfigDisplayLanguage(config: HermesConfigRecord, locale: Locale): HermesConfigRecord {
const display = isRecord(config.display) ? config.display : {}
- const next = {
- ...config,
- display: {
- ...display,
- language: localeConfigValue(locale)
- }
- }
- const origin = peekConfigReadOrigin(config)
-
- if (origin) {
- bindConfigReadOrigin(next, origin)
- }
-
- return next
+ return retainConfigReadOrigin(
+ {
+ ...config,
+ display: {
+ ...display,
+ language: localeConfigValue(locale)
+ }
+ },
+ config
+ )
}
function toError(error: unknown): Error {