Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: refresh reviewer health after local bridge writes (#12569)

[._.]/ Adam Eivye5c2461d253d
diff --git a/server/services/codeReview.backendLoading.test.js b/server/services/codeReview.backendLoading.test.js
index 97da59e945..5e2f243d30 100644
--- a/server/services/codeReview.backendLoading.test.js
+++ b/server/services/codeReview.backendLoading.test.js
@@ -3,6 +3,7 @@ import { mockJsonResponse } from '../lib/testHelper.js';
vi.mock('./settings.js', () => ({
getSettings: async () => ({}),
+ refreshSettingsIfChangedOnDisk: vi.fn(),
settingsEvents: { on: vi.fn() },
}));
vi.mock('./lmStudioManager.js', () => { throw new Error('Manager unavailable'); });
diff --git a/server/services/codeReview.diskSettings.test.js b/server/services/codeReview.diskSettings.test.js
new file mode 100644
index 0000000000..3f8031b8b1
--- /dev/null
+++ b/server/services/codeReview.diskSettings.test.js
@@ -0,0 +1,80 @@
+import { beforeEach, afterEach, expect, it, vi } from 'vitest';
+import { mkdtemp, writeFile, rm, utimes } from 'node:fs/promises';
+import { join } from 'node:path';
+import { tmpdir } from 'node:os';
+
+const context = vi.hoisted(() => ({ data: '' }));
+vi.mock('../lib/paths.js', async importActual => {
+ const actual = await importActual();
+ return { ...actual, PATHS: { ...actual.PATHS, get data() { return context.data; } } };
+});
+vi.mock('./userActions.js', () => ({ recordUserAction: vi.fn() }));
+let settings;
+let getCodeReviewDefaults;
+let file;
+beforeEach(async () => {
+ context.data = await mkdtemp(join(tmpdir(), 'review-settings-'));
+ file = join(context.data, 'settings.json');
+ vi.resetModules();
+ settings = await import('./settings.js');
+ ({ getCodeReviewDefaults } = await import('./codeReview.js'));
+});
+afterEach(async () => {
+ await settings.flushSettingsLedger();
+ await rm(context.data, { recursive: true, force: true });
+});
+
+it('rotates fallback reviewers after bridge writes and restores them after health clears', async () => {
+ const config = { reviewerFallbackGroups: [['codex'], ['claude']] };
+ await writeFile(file, JSON.stringify({ codeReview: config }));
+ expect((await getCodeReviewDefaults()).reviewers).toEqual(['codex']);
+
+ const health = { codex: { pausedUntil: Date.now() + 60_000, reason: 'quota', lastFailureAt: Date.now() } };
+ await writeFile(file, JSON.stringify({ codeReview: { ...config, reviewerHealth: health } }));
+ // Ordinary settings reads keep their hot cache until a reviewer-selection read.
+ expect((await settings.getSettings()).codeReview.reviewerHealth).toBeUndefined();
+ expect(await getCodeReviewDefaults()).toMatchObject({ reviewers: ['claude'], reviewerHealth: health });
+ expect((await settings.getSettings()).codeReview.reviewerHealth).toEqual(health);
+
+ await writeFile(file, JSON.stringify({ codeReview: config }));
+ expect((await getCodeReviewDefaults()).reviewers).toEqual(['codex']);
+});
+
+it('detects same-size edits by mtime and serializes refresh behind an admitted save', async () => {
+ await writeFile(file, JSON.stringify({ codeReview: { reviewers: ['codex'] } }).padEnd(80));
+ expect((await getCodeReviewDefaults()).reviewers).toEqual(['codex']);
+ await writeFile(file, JSON.stringify({ codeReview: { reviewers: ['claude'] } }).padEnd(80));
+ const future = new Date(Date.now() + 2000);
+ await utimes(file, future, future);
+ expect((await getCodeReviewDefaults()).reviewers).toEqual(['claude']);
+
+ let release;
+ const admitted = new Promise(resolve => { release = resolve; });
+ let entered;
+ const started = new Promise(resolve => { entered = resolve; });
+ const save = settings.updateSettingsWith(async current => {
+ entered();
+ await admitted;
+ return { ...current, codeReview: { reviewers: ['codex'] } };
+ }, { skipUserAction: true });
+ await started;
+ const refresh = getCodeReviewDefaults();
+ release();
+ await save;
+ expect((await refresh).reviewers).toEqual(['codex']);
+});
+
+it('invalidates corrupt settings without broadcasting an empty update, then observes repair', async () => {
+ await writeFile(file, JSON.stringify({ codeReview: { reviewers: ['codex'] } }));
+ expect((await getCodeReviewDefaults()).reviewers).toEqual(['codex']);
+ const updated = vi.fn();
+ const invalidated = vi.fn();
+ settings.settingsEvents.on('settings:updated', updated);
+ settings.settingsEvents.on('settings:invalidated', invalidated);
+ await writeFile(file, '{ truncated');
+ expect((await getCodeReviewDefaults()).reviewers).toEqual([]);
+ expect(updated).not.toHaveBeenCalled();
+ expect(invalidated).toHaveBeenCalledTimes(1);
+ await writeFile(file, JSON.stringify({ codeReview: { reviewers: ['claude'] } }));
+ expect((await getCodeReviewDefaults()).reviewers).toEqual(['claude']);
+});
diff --git a/server/services/codeReview.js b/server/services/codeReview.js
index dbc82fb235..dcebb4b707 100644
--- a/server/services/codeReview.js
+++ b/server/services/codeReview.js
@@ -58,7 +58,7 @@ import {
import { MAX_SCREENSHOT_BYTES } from '../lib/uploadLimits.js'
import { normalizeGoalFidelityFollowUpTrigger } from '../lib/goalFidelityFollowUp.js'
import { activeReviewerGroupIndex, isReviewerConfigFault, reviewerCommandPermissionFailureCode, normalizeReviewFinishReason, reviewFailureDiagnostics } from '../lib/reviewerHealth.js'
-import { getSettings, updateSettingsWith, settingsEvents } from './settings.js'
+import { getSettings, updateSettingsWith, refreshSettingsIfChangedOnDisk, settingsEvents } from './settings.js'
export const REVIEWER_PAUSE_MS = 24 * 60 * 60 * 1000
const QUOTA_FAILURE = /quota|rate.?limit|usage.?limit|too many requests|\b(?:HTTP(?:\/[\d.]+)?|status[_ ]?code|status|code|error)\W{0,3}(?:429|402)\b|payment required|limit for the day|daily.?limit|session.?limit|plan.?limit|free.?limit|free.?tier|FreeTierError|err_free_used|allowance is used up|is used up|top up your balance|wallet billing|out of credits|credit balance (?:is )?too low|insufficient.?(?:credits|funds|balance)|exceeded (?:your )?(?:daily |current )?(?:quota|limit|allowance)|upgrade (?:your )?subscription|Upgrade to Pro/i
@@ -430,18 +430,20 @@ export function pickCodeReviewDefaults(settings) {
* Cached so the per-agent-completion fallback (`finalizeAgent`) doesn't pay
* a `readFile + JSON.parse + stripStoreKeys` round-trip on every sweep —
* during a busy CoS evaluation that's dozens of redundant disk reads. The
- * cache invalidates on any `settings:updated` event so the panel's save
- * takes effect immediately without a restart.
+ * cache invalidates on `settings:updated`; the metadata check also observes
+ * reviewer health written by the local review bridge in another process.
*/
let cachedSettings = null
let cachedDefaults = null
let cachedDefaultsExpiresAt = Infinity
settingsEvents.on('settings:updated', () => { cachedSettings = null; cachedDefaults = null })
+settingsEvents.on('settings:invalidated', () => { cachedSettings = null; cachedDefaults = null })
/** Test-only: reset the memoized defaults cache to its uninitialized sentinel. */
export function __resetCodeReviewDefaultsCache() { cachedSettings = null; cachedDefaults = null }
export async function getCodeReviewDefaults() {
+ await refreshSettingsIfChangedOnDisk()
const now = Date.now()
if (cachedDefaults && now < cachedDefaultsExpiresAt) return cachedDefaults
if (!cachedSettings) cachedSettings = await getSettings()
diff --git a/server/services/codeReview.test.js b/server/services/codeReview.test.js
index 0baaa3bf7e..bd38f6de8b 100644
--- a/server/services/codeReview.test.js
+++ b/server/services/codeReview.test.js
@@ -7,6 +7,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
const mockedSettings = { current: {}, writes: Promise.resolve() }
vi.mock('./settings.js', () => ({
getSettings: () => Promise.resolve(mockedSettings.current),
+ refreshSettingsIfChangedOnDisk: vi.fn(),
updateSettingsWith: vi.fn((mutate) => {
const write = mockedSettings.writes.then(async () => {
mockedSettings.current = await mutate(mockedSettings.current)
diff --git a/server/services/settings.js b/server/services/settings.js
index 139b96992b..c6b180d443 100644
--- a/server/services/settings.js
+++ b/server/services/settings.js
@@ -1,5 +1,6 @@
import { hydratePrivateKeys, persistPrivateKeys } from './privateKeyStore.js';
import { join } from 'path';
+import { stat } from 'node:fs/promises';
import { EventEmitter } from 'events';
import { safeJSONParse, PATHS, atomicWrite, tryReadFile, tryReadFileStrict } from '../lib/fileUtils.js';
import { createFileWriteQueue, drainDetachedWrites, trackDetachedWrite } from '../lib/fileWriteQueue.js';
@@ -137,9 +138,21 @@ export const readSettingsStrict = async (filePath = SETTINGS_FILE) => {
// same `cleaned` object it also returns to its caller). structuredClone is the
// right tool for this JSON-shaped data and stays far cheaper than the disk
// read + full-tree JSON.parse it replaces.
-// A manual edit of settings.json while the server runs is not observed until
-// the next app-driven save or a restart — the standard trade-off for a cache,
-// and app-driven changes (the only ones the schedulers care about) refresh it.
+// The local review bridge writes from another process. Reviewer-selection
+// reads explicitly refresh from disk metadata; ordinary getSettings() calls
+// keep this hot cache without paying a stat on every read.
+const readSettingsSignature = async () => {
+ try {
+ const { mtimeMs, size } = await stat(SETTINGS_FILE);
+ return { mtimeMs, size };
+ } catch (error) {
+ if (error.code === 'ENOENT') return null;
+ // Metadata is advisory; the strict read below owns unreadable/corrupt
+ // handling and invalidates consumers without broadcasting auth-off data.
+ return undefined;
+ }
+};
+let settingsSignature;
let settingsCache = null;
settingsEvents.on('settings:updated', (cleaned) => {
settingsCache = structuredClone(cleaned);
@@ -150,6 +163,7 @@ settingsEvents.on('settings:updated', (cleaned) => {
// the cache coherent via the `settings:updated` event above.
export const __resetSettingsCache = () => {
settingsCache = null;
+ settingsSignature = undefined;
};
// Re-sync the read cache with the current on-disk settings.json and notify every
@@ -157,8 +171,8 @@ export const __resetSettingsCache = () => {
// OUTSIDE the normal save() path — e.g. a backup restore rsyncs it into place —
// since such a write bypasses the save()-emitted event the cache rides on and
// would otherwise leave every settings consumer serving pre-restore values.
-export const reloadSettings = async () => {
- const { corrupt, settings } = await readSettingsStrict();
+const reloadSettingsFromDisk = async (signaturePromise = readSettingsSignature()) => {
+ const [signature, { corrupt, settings }] = await Promise.all([signaturePromise, readSettingsStrict()]);
if (corrupt) {
// Do NOT broadcast a corrupt on-disk settings.json as `{}` (issue #2684). The
// concrete path is a backup restore of a malformed snapshot: backup.js calls
@@ -169,20 +183,34 @@ export const reloadSettings = async () => {
// consumers (auth) to drop theirs, so the next read goes to disk and fails
// closed / re-reads. Self-heals on the next clean read.
settingsCache = null;
+ settingsSignature = undefined;
settingsEvents.emit('settings:invalidated');
return {};
}
const cleaned = stripStoreKeys(await hydratePrivateKeys(settings, PATHS.data));
+ settingsSignature = signature;
settingsEvents.emit('settings:updated', cleaned);
return cleaned;
};
+export const reloadSettings = () => reloadSettingsFromDisk();
+
// Serialize all writes to settings.json on a single tail so an updateSettings
// read-merge-write can't interleave with a concurrent save (two browser tabs,
-// a background job racing a user save) and clobber the other's patch. Reads
-// stay off the queue — atomicWrite's temp-file+rename keeps every read whole.
+// a background job racing a user save) and clobber the other's patch. Ordinary
+// reads stay off the queue; explicit disk refreshes serialize their cache events.
const queueWrite = createFileWriteQueue();
+// Share the write queue so an older disk snapshot cannot emit after a newer
+// in-process save. reloadSettings itself stays usable inside the restore lock.
+export const refreshSettingsIfChangedOnDisk = () => queueWrite(async () => {
+ const signature = await readSettingsSignature();
+ if (settingsCache !== null && settingsSignature !== undefined
+ && signature?.mtimeMs === settingsSignature?.mtimeMs
+ && signature?.size === settingsSignature?.size) return;
+ await reloadSettingsFromDisk(signature);
+});
+
/**
* Drain admitted settings writes and hold later writes through an out-of-band
* restore and its cache reconciliation. The callback must not re-enter any
@@ -283,6 +311,7 @@ const save = async (settings, { actor = 'system', skipUserAction = false } = {},
// newline; atomicWrite's own JSON.stringify omits it.
await atomicWrite(SETTINGS_FILE, JSON.stringify(await persistPrivateKeys(cleaned, PATHS.data, { previousSettings: prev }), null, 2) + '\n');
await hydratePrivateKeys(cleaned, PATHS.data);
+ settingsSignature = await readSettingsSignature();
// Warn AFTER the successful write so a thrown write never produces
// a misleading "stripped" log line for a write that didn't happen.
if (isPlainObject(settings)) {
@@ -361,7 +390,7 @@ export const getSettingsWithStatus = async () => {
// defeating the no-restart self-heal the auth fail-closed path promises. This
// covers BOTH failure modes (malformed content and an unreadable-but-present
// file); only a genuinely ABSENT file (fresh install) caches `{}`.
- const { corrupt, settings } = await readSettingsStrict();
+ const [signature, { corrupt, settings }] = await Promise.all([readSettingsSignature(), readSettingsStrict()]);
const loaded = stripStoreKeys(await hydratePrivateKeys(settings, PATHS.data));
// A save()/reloadSettings() may have populated the cache via the
// settings:updated listener while this cold read was awaiting the disk read.
@@ -371,6 +400,7 @@ export const getSettingsWithStatus = async () => {
// very next call re-reads and picks up a repair immediately.
if (corrupt) return { corrupt: true, settings: structuredClone(loaded) };
settingsCache = loaded;
+ settingsSignature = signature;
}
// Hand out a private deep copy so a caller mutating nested settings in place
// can't corrupt the shared cache — matching the prior per-call