Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

fix(config): announce every config write so the UI stops showing stale values

HundredBillion23485fd54fd9
diff --git a/src/main/config.ts b/src/main/config.ts
index ba385d8b..d0f4255f 100644
--- a/src/main/config.ts
+++ b/src/main/config.ts
@@ -621,10 +621,37 @@ function normalizeStoredHomes(cfg: HarnessConfig): HarnessConfig {
return cfg;
}
+/** Every persisted config write is announced here.
+ *
+ * The renderer holds the config as a snapshot loaded once at start-up, so
+ * before this existed a write reached disk but nothing told the UI, and any
+ * view seeded from that snapshot kept showing the pre-write value until the
+ * app restarted. Settings was the visible casualty: a toggle wrote correctly,
+ * then re-read the stale snapshot on reopen and displayed the old state (#263).
+ *
+ * This sits on `persistConfig` rather than on `writeConfig` deliberately —
+ * `writeConfig` is one of 23 callers across the main process, and Slack,
+ * freeflow and notifications each persist through their own path. The file
+ * write is the only point every one of them has in common, so subscribing here
+ * is the difference between "the fields we remembered" and "all of them". */
+type ConfigWriteListener = (next: HarnessConfig) => void;
+const configWriteListeners = new Set<ConfigWriteListener>();
+
+export function onConfigWritten(listener: ConfigWriteListener): () => void {
+ configWriteListeners.add(listener);
+ return () => { configWriteListeners.delete(listener); };
+}
+
function persistConfig(next: HarnessConfig): HarnessConfig {
const p = configPath();
mkdirSync(dirname(p), { recursive: true });
writeFileSync(p, JSON.stringify(next, null, 2), 'utf8');
+ // A listener is a renderer send, which can fail while a window is closing.
+ // The config is already on disk by this point, so a throwing subscriber must
+ // neither fail the write for its caller nor starve the subscribers after it.
+ for (const listener of configWriteListeners) {
+ try { listener(next); } catch { /* a broken listener is not a failed write */ }
+ }
return next;
}
@@ -693,9 +720,9 @@ export function setAgentTokenCap(agentId: unknown, tokenCap: unknown): HarnessCo
/** Wipe the persisted config back to first-run defaults so the app boots into
* onboarding again. Used by the "reset & start over" flow. */
export function resetConfig(): HarnessConfig {
- const p = configPath();
- mkdirSync(dirname(p), { recursive: true });
- writeFileSync(p, JSON.stringify(DEFAULTS, null, 2), 'utf8');
+ // Routed through persistConfig so the reset announces itself like any other
+ // write; the bytes written are the same DEFAULTS as before.
+ persistConfig({ ...DEFAULTS });
// Drop the migration latch too: the file on disk is back to `triggersMigratedV1:
// false`, and a latch left set would keep the flag from ever being written again
// in this process. The migration itself is a no-op on defaults either way.
diff --git a/src/main/index.ts b/src/main/index.ts
index 5944af45..50e865a6 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -14,7 +14,7 @@ import { resolveCommand as resolveCliCommand } from './shellEnv';
import { initAutoUpdater, abortPendingRestart } from './updater';
import { RealtimeFloorWatcher } from './realtimeFloorWatcher';
import {
- readConfig, writeConfig, setAgentTokenCap, resetConfig, ensureHarnessHome, ensureClaudePermissionsAccepted,
+ readConfig, writeConfig, setAgentTokenCap, resetConfig, onConfigWritten, ensureHarnessHome, ensureClaudePermissionsAccepted,
modelForRole, OPS_STANDUP_MISSION, HEARTBEAT_MISSION, COMPACT_MAINTENANCE_MISSION, type HarnessConfig, type ScheduledMission
} from './config';
import { listDir, readFileText, readFileBinary, writeFileText, statAbs, expandTilde } from './fs';
@@ -5161,6 +5161,16 @@ app.on('before-quit', (e) => {
}
});
+// Keep the renderer's copy of the config current. The renderer loads the config
+// once at start-up and hands it down as a prop, so without this a write reached
+// disk while every view seeded from that prop kept rendering the pre-write value
+// until the next launch (#263). Subscribing to the write itself covers Slack,
+// freeflow, notifications and the rest, each of which persists by its own route.
+onConfigWritten((next) => {
+ if (!mainWindow || mainWindow.isDestroyed()) return;
+ mainWindow.webContents.send('config:changed', next);
+});
+
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
// Full teardown, not a bare killAll: this path must also stop the proxy
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 82605467..2f818185 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -930,6 +930,15 @@ const api = {
}> =>
ipcRenderer.invoke('hire:openFile'),
+ // ─── Config changes ──────────────────────────────────────────────────────
+ /** Fired after every persisted config write, with the config as written, so a
+ * renderer holding it as state never drifts from what is on disk. */
+ onConfigChanged: (cb: (config: HarnessConfig) => void): (() => void) => {
+ const listener = (_e: IpcRendererEvent, config: HarnessConfig) => cb(config);
+ ipcRenderer.on('config:changed', listener);
+ return () => ipcRenderer.removeListener('config:changed', listener);
+ },
+
// ─── Quit confirmation ───────────────────────────────────────────────────
onCloseRequested: (cb: (info: { ptyCount: number }) => void): (() => void) => {
const listener = (_e: IpcRendererEvent, info: { ptyCount: number }) => cb(info);
diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx
index 396403ca..92084435 100644
--- a/src/renderer/src/App.tsx
+++ b/src/renderer/src/App.tsx
@@ -125,6 +125,11 @@ export function App() {
// (solo-hold threshold, aborts on any other key). See freeflow/holdOption.ts.
useHoldOptionToTalk();
+ // The config is loaded once above and handed down as a prop, so anything that
+ // writes it — Settings, Slack, freeflow, onboarding — has to be able to tell us,
+ // or those views keep rendering the value from start-up (#263).
+ useEffect(() => window.cth.onConfigChanged(setConfig), []);
+
// Quit warning subscription
useEffect(() => window.cth.onCloseRequested((info) => setQuitWarn(info)), []);
diff --git a/test/config-write-notify.test.cjs b/test/config-write-notify.test.cjs
new file mode 100644
index 00000000..9a59c704
--- /dev/null
+++ b/test/config-write-notify.test.cjs
@@ -0,0 +1,97 @@
+'use strict';
+
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+const os = require('node:os');
+const path = require('node:path');
+const loadTs = require('./load-ts.cjs');
+
+// config.ts resolves its file through Electron's app.getPath(). Point that one
+// dependency at a throwaway userData root so this test never touches the real
+// application config.
+const userData = fs.mkdtempSync(path.join(os.tmpdir(), 'md-config-notify-'));
+const electron = require.resolve('electron');
+require.cache[electron] = {
+ id: electron,
+ filename: electron,
+ loaded: true,
+ exports: { app: { getPath: () => userData } }
+};
+
+const { writeConfig, readConfig, onConfigWritten, setAgentTokenCap, resetConfig } = loadTs('src/main/config.ts');
+
+test.after(() => fs.rmSync(userData, { recursive: true, force: true }));
+
+test('a config write notifies subscribers with the persisted config', () => {
+ const seen = [];
+ const off = onConfigWritten((next) => seen.push(next));
+
+ const returned = writeConfig({ orchestratorMaySpawn: true });
+
+ // A single logical write can persist more than once — reading the config runs
+ // any pending migration, which persists in its own right. The contract is
+ // that subscribers end up holding what the caller was handed back, not that
+ // exactly one notification arrives.
+ assert.ok(seen.length >= 1);
+ assert.deepEqual(seen[seen.length - 1], returned);
+ assert.equal(seen[seen.length - 1].orchestratorMaySpawn, true);
+ off();
+});
+
+// Not every field is written through writeConfig — agent token caps, and the
+// reset flow below, each persist by their own route. Subscribing at the file
+// write is what makes them all announce; these two tests are what stop a future
+// refactor from quietly moving the hook back up into writeConfig.
+test('a token-cap write notifies too, not just writeConfig', () => {
+ const seen = [];
+ const off = onConfigWritten((next) => seen.push(next));
+
+ const returned = setAgentTokenCap('jim', 100);
+
+ assert.ok(seen.length >= 1);
+ assert.deepEqual(seen[seen.length - 1], returned);
+ assert.equal(seen[seen.length - 1].agentTokenCaps.jim, 100);
+ off();
+});
+
+test('resetting the config notifies as well', () => {
+ const seen = [];
+ const off = onConfigWritten((next) => seen.push(next));
+
+ resetConfig();
+
+ // Listeners receive the config as PERSISTED. resetConfig hands its caller a
+ // trigger-enriched view of the same defaults, so the two differ by design and
+ // this asserts the reset reached subscribers rather than equality with it.
+ assert.ok(seen.length >= 1);
+ assert.equal(seen[seen.length - 1].onboardingComplete, false);
+ off();
+});
+
+test('unsubscribing stops the notifications', () => {
+ const seen = [];
+ const off = onConfigWritten((next) => seen.push(next));
+ writeConfig({ notifications: true });
+ const countWhileSubscribed = seen.length;
+ assert.ok(countWhileSubscribed >= 1);
+
+ off();
+ writeConfig({ notifications: false });
+
+ assert.equal(seen.length, countWhileSubscribed);
+});
+
+test('a listener that throws neither fails the write nor starves the next listener', () => {
+ const reached = [];
+ const offBad = onConfigWritten(() => { throw new Error('window is gone'); });
+ const offGood = onConfigWritten((next) => reached.push(next));
+
+ // The write itself must still return normally and still land on disk.
+ const returned = writeConfig({ autoMode: true });
+
+ assert.equal(returned.autoMode, true);
+ assert.equal(readConfig().autoMode, true);
+ assert.ok(reached.length >= 1);
+ offBad(); offGood();
+});