fix(desktop): stop Settings autosave from clobbering out-of-band config edits

ConfigSettingsInner seeds its local draft once from the config record and
never re-seeds it while the page stays open, but every autosave PUT still
sent the entire draft. Since PUT /api/config deep-merges onto disk, that
degenerates into a full overwrite for every field the UI's schema knows
about: if `hermes config set` (or another profile/session) changes a
schema-known key like fallback_providers while Settings is open, the next
autosave — triggered by editing any unrelated field — writes the stale
seed-time value back over it.

Diff the draft against the seed-time baseline and send only the changed
branches, so an untouched key is never resent and the backend's deep-merge
actually protects it.
This commit is contained in:
chelsealong
2026-08-19 02:06:23 +00:00
committed by Teknium
parent c8329384a9
commit 5361867c6d
3 changed files with 88 additions and 1 deletions
@@ -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 = loadedConfig
savedDiscoverySignatureRef.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 = null
savedDiscoverySignatureRef.current = undefined
setConfig(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)
@@ -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'] })
})
})
})
+37
View File
@@ -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