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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: honor fresh observations during an in-flight read (#8668)
[._.]/ Adam Eivybacba7a066af
diff --git a/server/services/modelObservation.js b/server/services/modelObservation.jsindex f1aaebeda9..1489bc375e 100644--- a/server/services/modelObservation.js+++ b/server/services/modelObservation.js@@ -13,6 +13,7 @@ export function observeModelResource(namespace, probe, intervalMs = 20_000) {const subscribers = groups.get(namespace) ?? new Set();groups.set(namespace, subscribers);let pending = null;+ let pendingFresh = false;let sample = null;let sampled = false;let revision = 0;@@ -35,10 +36,19 @@ export function observeModelResource(namespace, probe, intervalMs = 20_000) {timer.unref?.();};const read = ({ fresh = false } = {}) => {- if (pending) return pending;+ if (pending) {+ // A forced reconcile must not inherit a cache-backed probe already in+ // flight (notably the account read immediately following sign-in).+ if (fresh && !pendingFresh) {+ pendingFresh = true;+ revision += 1;+ }+ return pending;+ }if (!fresh && subscribers.size && sampled && Date.now() - sampledAt < intervalMs) {return sample.error ? Promise.reject(sample.error) : Promise.resolve(sample.value);}+ pendingFresh = fresh;pending = Promise.resolve().then(async () => {let result;let started;@@ -46,7 +56,7 @@ export function observeModelResource(namespace, probe, intervalMs = 20_000) {// Every waiter gets the post-mutation sample, never the discarded result.do {started = revision;- result = await Promise.resolve().then(() => probe({ fresh })).then(value => ({ value }), error => ({ error }));+ result = await Promise.resolve().then(() => probe({ fresh: pendingFresh })).then(value => ({ value }), error => ({ error }));} while (started !== revision);const nextSignature = result.error ? 'probe-unavailable' : signature(result.value);sample = result;@@ -119,7 +129,7 @@ export function observeModelMutations(...namespaces) {};}-export function resetModelObservationsForTests() {+export function __resetModelObservationsForTests() {for (const resource of resources.values()) resource.stop();resources.clear();groups.clear();diff --git a/server/services/modelObservation.test.js b/server/services/modelObservation.test.jsindex 505e751feb..4e94d804b9 100644--- a/server/services/modelObservation.test.js+++ b/server/services/modelObservation.test.js@@ -1,6 +1,6 @@import { EventEmitter } from 'node:events';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';-import { observeModelResource, registerModelObservationSocket, resetModelObservationsForTests } from './modelObservation.js';+import { observeModelResource, registerModelObservationSocket, __resetModelObservationsForTests } from './modelObservation.js';function viewer() {const socket = new EventEmitter();@@ -14,7 +14,7 @@ const flush = () => vi.advanceTimersByTimeAsync(0);describe('shared model observations through socket subscriptions and HTTP reads', () => {beforeEach(() => vi.useFakeTimers());- afterEach(() => { resetModelObservationsForTests(); vi.useRealTimers(); });+ afterEach(() => { __resetModelObservationsForTests(); vi.useRealTimers(); });it('shares one external sample across viewers and stops after the final disconnect', async () => {const probe = vi.fn().mockResolvedValue({ ollama: [] });@@ -73,6 +73,24 @@ describe('shared model observations through socket subscriptions and HTTP reads'expect(socket.frames).toEqual([{}]);});+ it('upgrades a pending cache-backed read when a caller requests a fresh account sample', async () => {+ let resolveCached;+ const probe = vi.fn().mockImplementationOnce(() => new Promise(resolve => { resolveCached = resolve; }))+ .mockResolvedValue({ status: 'ready' });+ const resource = observeModelResource('codex-account', probe);+ const normal = resource.read();+ await flush();+ expect(probe).toHaveBeenLastCalledWith({ fresh: false });+ const fresh = resource.read({ fresh: true });+ const otherViewer = resource.read({ fresh: true });+ resolveCached({ status: 'signed-out' });+ await expect(normal).resolves.toEqual({ status: 'ready' });+ await expect(fresh).resolves.toEqual({ status: 'ready' });+ await expect(otherViewer).resolves.toEqual({ status: 'ready' });+ expect(probe).toHaveBeenCalledTimes(2);+ expect(probe).toHaveBeenLastCalledWith({ fresh: true });+ });+it('does not restart observation when the last viewer leaves during a read', async () => {let resolve;const resource = observeModelResource('loaded-models', () => new Promise(done => { resolve = done; }), 2000);