The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): stop Settings autosave from clobbering out-of-band config edits
chelsealong5361867c6d32
diff --git a/apps/desktop/src/app/settings/config-settings.tsx b/apps/desktop/src/app/settings/config-settings.tsxindex ee69a841fc2..abe6ca2dd42 100644--- a/apps/desktop/src/app/settings/config-settings.tsx+++ b/apps/desktop/src/app/settings/config-settings.tsx@@ -34,6 +34,7 @@ import { PanelEmpty } from '../overlays/panel'import { ConfigField } from './config-field'import {clearsEnabledToolsets,+ diffConfig,enumOptionsFor,getNested,isExternalMemoryProvider,@@ -122,11 +123,17 @@ function ConfigSettingsInner({// Seed the local draft once, the first time the shared record lands.// Background refetches thereafter must not clobber in-progress edits.const configSeeded = useRef(false)+ // Snapshot of the record as it was when the draft was seeded. Autosave+ // diffs the draft against this (not against disk) so a field the user+ // never touched — possibly changed out-of-band by `hermes config set`+ // while this page sat open — is never resent with its stale value.+ const configBaselineRef = useRef<HermesConfigRecord | null>(null)// eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)useEffect(() => {if (loadedConfig && !configSeeded.current) {configSeeded.current = true+ configBaselineRef.current = loadedConfigsavedDiscoverySignatureRef.current = repoDiscoveryPolicySignature(repoDiscoveryPolicyFromConfig(loadedConfig))setConfig(loadedConfig)}@@ -138,6 +145,7 @@ function ConfigSettingsInner({// the pending debounced autosave is cancelled by its effect cleanup.useOnProfileSwitch(() => {configSeeded.current = false+ configBaselineRef.current = nullsavedDiscoverySignatureRef.current = undefinedsetConfig(null)saveVersionRef.current = 0@@ -178,7 +186,8 @@ function ConfigSettingsInner({const t = window.setTimeout(() => {void (async () => {try {- const result = await saveHermesConfig(config, scopeProfile)+ const patch = diffConfig(configBaselineRef.current ?? {}, config)+ const result = await saveHermesConfig(patch, scopeProfile)if (!result.ok) {throw new Error(c.autosaveFailed)diff --git a/apps/desktop/src/app/settings/helpers.test.ts b/apps/desktop/src/app/settings/helpers.test.tsindex 8e7a300038f..90c00e50e6a 100644--- a/apps/desktop/src/app/settings/helpers.test.ts+++ b/apps/desktop/src/app/settings/helpers.test.ts@@ -6,6 +6,7 @@ import { FIELD_DESCRIPTIONS, FIELD_LABELS, SECTIONS } from './constants'import { defineFieldCopy, fieldCopyForSchemaKey, schemaKeyToFieldCopyKey } from './field-copy'import {clearsEnabledToolsets,+ diffConfig,enumOptionsFor,getNested,isExternalMemoryProvider,@@ -404,4 +405,44 @@ describe('settings helpers', () => {expect(clearsEnabledToolsets(prev, next)).toBe(false)})})++ describe('diffConfig', () => {+ it('omits a top-level key the draft never touched', () => {+ // The autosave baseline is a snapshot taken when Settings opened. A key+ // an agent set via `hermes config set` while the page sat open must not+ // come back in the patch just because it's still present in the draft.+ const baseline: HermesConfigRecord = { fallback_providers: ['nara1'], timezone: 'UTC' }+ const draft: HermesConfigRecord = { fallback_providers: ['nara1'], timezone: 'America/New_York' }++ expect(diffConfig(baseline, draft)).toEqual({ timezone: 'America/New_York' })+ })++ it('includes a nested key only when it actually changed, leaving siblings out', () => {+ const baseline: HermesConfigRecord = { display: { personality: 'default', show_reasoning: true } }+ const draft: HermesConfigRecord = { display: { personality: 'default', show_reasoning: false } }++ expect(diffConfig(baseline, draft)).toEqual({ display: { show_reasoning: false } })+ })++ it('sends a new key that was absent from the baseline', () => {+ const baseline: HermesConfigRecord = {}+ const draft: HermesConfigRecord = { timezone: 'UTC' }++ expect(diffConfig(baseline, draft)).toEqual({ timezone: 'UTC' })+ })++ it('returns an empty object when the draft matches the baseline exactly', () => {+ const baseline: HermesConfigRecord = { toolsets: ['memory'], display: { personality: 'default' } }+ const draft: HermesConfigRecord = { toolsets: ['memory'], display: { personality: 'default' } }++ expect(diffConfig(baseline, draft)).toEqual({})+ })++ it('treats an array as a whole value, not diffed element by element', () => {+ const baseline: HermesConfigRecord = { toolsets: ['memory', 'terminal'] }+ const draft: HermesConfigRecord = { toolsets: ['memory'] }++ expect(diffConfig(baseline, draft)).toEqual({ toolsets: ['memory'] })+ })+ })})diff --git a/apps/desktop/src/app/settings/helpers.ts b/apps/desktop/src/app/settings/helpers.tsindex 8da0c92f058..b51162f8549 100644--- a/apps/desktop/src/app/settings/helpers.ts+++ b/apps/desktop/src/app/settings/helpers.ts@@ -97,6 +97,43 @@ export function getNested(obj: HermesConfigRecord, path: string): unknown {return cur}+const isPlainObject = (value: unknown): value is Record<string, unknown> =>+ typeof value === 'object' && value !== null && !Array.isArray(value)++/**+ * Structural diff between two config snapshots: an object holding only the+ * branches of `next` that changed relative to `base`. Plain-object values are+ * compared key by key so editing one field doesn't drag its untouched+ * siblings back into the result; arrays and scalars are compared as whole+ * values.+ *+ * The autosave path sends this instead of the full draft so a field the user+ * never touched — one an agent may have changed via `hermes config set`+ * while Settings was open with a stale snapshot — is never resent with its+ * now-stale value. `PUT /api/config` deep-merges onto disk, so an omitted+ * key keeps whatever is currently there.+ */+export function diffConfig(base: HermesConfigRecord, next: HermesConfigRecord): HermesConfigRecord {+ const patch: HermesConfigRecord = {}++ for (const key of Object.keys(next)) {+ const baseValue = base[key]+ const nextValue = next[key]++ if (isPlainObject(baseValue) && isPlainObject(nextValue)) {+ const nested = diffConfig(baseValue, nextValue)++ if (Object.keys(nested).length > 0) {+ patch[key] = nested+ }+ } else if (JSON.stringify(baseValue) !== JSON.stringify(nextValue)) {+ patch[key] = nextValue+ }+ }++ return patch+}+/*** True when an edit clears the entire "Enabled Toolsets" list — i.e. the* previous config had a non-empty toolsets array and the next one is an