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 Jev integration policy through socket events (#8536)

[._.]/ Adam Eivy9aa710f18897
diff --git a/client/src/components/models/JevIntegrations.jsx b/client/src/components/models/JevIntegrations.jsx
index 2b8e85f5ed..3445da4628 100644
--- a/client/src/components/models/JevIntegrations.jsx
+++ b/client/src/components/models/JevIntegrations.jsx
@@ -1,8 +1,11 @@
-import { useCallback, useRef, useState } from 'react';
+import { useState } from 'react';
import { getJevPolicy, updateJevPolicy, updateInstanceFeature } from '../../services/api';
-import { useAutoRefetch } from '../../hooks/useAutoRefetch';
+import { useSocketResource } from '../../hooks/useSocketResource';
import { publishInstanceFeatures, useInstanceFeatures } from '../../hooks/useInstanceFeatures';
+const POLICY_EVENTS = ['jev:policy'];
+const loadPolicy = ({ signal }) => getJevPolicy({ silent: true, signal });
+
const SOURCES = [
['github-issue', 'Issue replies and forge maintenance'],
['email', 'Message triage action and priority'],
@@ -14,19 +17,10 @@ const MODES = { disabled: 'Disabled', off: 'Shadow', prefer: 'Prefer local', onl
export default function JevIntegrations({ registry, status }) {
const { features, error: featureError } = useInstanceFeatures();
const feature = features?.find(item => item.id === 'jev');
- const generation = useRef(0);
- const [policy, setPolicy] = useState(null);
+ const { data: policy, updateData: setPolicy, error, refetch } = useSocketResource(loadPolicy, { events: POLICY_EVENTS });
const [saving, setSaving] = useState(false);
const [saveError, setSaveError] = useState('');
- const load = useCallback(() => {
- const requested = ++generation.current;
- return getJevPolicy({ silent: true }).then(result => {
- if (generation.current === requested) setPolicy(result);
- });
- }, []);
- const { error, refetch } = useAutoRefetch(load, 30000, { enabled: !saving });
const save = (patch) => {
- generation.current += 1;
setSaving(true);
setSaveError('');
return updateJevPolicy(patch, { silent: true }).then(setPolicy)
diff --git a/client/src/components/models/JevIntegrations.test.jsx b/client/src/components/models/JevIntegrations.test.jsx
index ad67b182ff..3b6fb72c1e 100644
--- a/client/src/components/models/JevIntegrations.test.jsx
+++ b/client/src/components/models/JevIntegrations.test.jsx
@@ -1,5 +1,5 @@
-import { beforeEach, describe, expect, it, vi } from 'vitest';
-import { fireEvent, render, screen, waitFor } from '@testing-library/react';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import JevIntegrations from './JevIntegrations';
import { getJevPolicy, updateJevPolicy, updateInstanceFeature } from '../../services/api';
import { publishInstanceFeatures } from '../../hooks/useInstanceFeatures';
@@ -9,6 +9,13 @@ vi.mock('../../hooks/useInstanceFeatures', () => ({
useInstanceFeatures: () => ({ features: [{ id: 'jev', enabled: false }], error: null }),
publishInstanceFeatures: vi.fn(),
}));
+vi.mock('../../services/socket', async () => {
+ const { EventEmitter } = await import('node:events');
+ return { default: new EventEmitter() };
+});
+import socket from '../../services/socket';
+afterEach(() => { cleanup(); vi.useRealTimers(); vi.restoreAllMocks(); });
+
const policy = { scopeAdherenceEnabled: true, sources: {
'github-issue': { jevMode: 'off', jevMinMargin: null },
email: { jevMode: 'prefer', jevMinMargin: 0.3 },
@@ -52,3 +59,56 @@ describe('Jev integration management', () => {
expect(control).toHaveValue('enabled');
});
});
+
+it('updates policy from events without polling and reconciles once on reconnect and tab show', async () => {
+ vi.useFakeTimers();
+ const visibility = vi.spyOn(document, 'visibilityState', 'get').mockReturnValue('visible');
+ const view = render(<JevIntegrations />);
+ await act(async () => {});
+ const control = screen.getByLabelText('Issue / PR scope adherence');
+ expect(control).toHaveValue('enabled');
+ expect(getJevPolicy).toHaveBeenCalledTimes(1);
+ await act(async () => { await vi.advanceTimersByTimeAsync(90000); });
+ expect(getJevPolicy).toHaveBeenCalledTimes(1);
+ getJevPolicy.mockResolvedValue({ ...policy, scopeAdherenceEnabled: false });
+ await act(async () => { socket.emit('jev:policy', {}); });
+ expect(control).toHaveValue('disabled');
+ expect(getJevPolicy).toHaveBeenCalledTimes(2);
+ await act(async () => { socket.emit('connect'); });
+ expect(getJevPolicy).toHaveBeenCalledTimes(3);
+ visibility.mockReturnValue('hidden');
+ act(() => document.dispatchEvent(new Event('visibilitychange')));
+ await act(async () => { socket.emit('jev:policy', {}); socket.emit('connect'); });
+ expect(getJevPolicy).toHaveBeenCalledTimes(3);
+ visibility.mockReturnValue('visible');
+ await act(async () => document.dispatchEvent(new Event('visibilitychange')));
+ expect(getJevPolicy).toHaveBeenCalledTimes(4);
+ await act(async () => document.dispatchEvent(new Event('visibilitychange')));
+ expect(getJevPolicy).toHaveBeenCalledTimes(4);
+ getJevPolicy.mockRejectedValue(new Error('Settings unavailable'));
+ await act(async () => { socket.emit('jev:policy', {}); });
+ expect(screen.getByRole('alert')).toHaveTextContent('Could not refresh');
+ expect(control).toBeDisabled();
+ getJevPolicy.mockResolvedValue(policy);
+ await act(async () => { fireEvent.click(screen.getByRole('button', { name: 'Retry' })); });
+ expect(control).toBeEnabled();
+ expect(control).toHaveValue('enabled');
+ view.unmount();
+ const reads = getJevPolicy.mock.calls.length;
+ await act(async () => { socket.emit('jev:policy', {}); socket.emit('connect'); });
+ expect(getJevPolicy).toHaveBeenCalledTimes(reads);
+});
+
+it('keeps the saved response when an older event read finishes later', async () => {
+ render(<JevIntegrations />);
+ const control = screen.getByLabelText('Issue / PR scope adherence');
+ await waitFor(() => expect(control).toBeEnabled());
+ let finishRead;
+ getJevPolicy.mockImplementationOnce(() => new Promise(resolve => { finishRead = resolve; }));
+ await act(async () => { socket.emit('jev:policy', {}); });
+ updateJevPolicy.mockResolvedValue({ ...policy, scopeAdherenceEnabled: false });
+ await act(async () => { fireEvent.change(control, { target: { value: 'disabled' } }); });
+ expect(control).toHaveValue('disabled');
+ await act(async () => { finishRead(policy); });
+ expect(control).toHaveValue('disabled');
+});
diff --git a/server/lib/socketEventContracts.js b/server/lib/socketEventContracts.js
index 51086864b1..6d766aa186 100644
--- a/server/lib/socketEventContracts.js
+++ b/server/lib/socketEventContracts.js
@@ -57,6 +57,11 @@ export const SOCKET_EVENT_CONTRACTS = Object.freeze({
additionalProperties: false,
},
},
+ 'jev:policy': {
+ direction: 'server-to-client',
+ summary: 'Invalidate Jev integration policy after settings persistence or restore invalidation; no settings content.',
+ payloadSchema: { type: 'object', properties: {}, additionalProperties: false },
+ },
'laya:status': {
direction: 'server-to-client',
summary: 'Invalidate Laya runtime status after install progress, completion, failure or scoring transitions; no experiment content.',
diff --git a/server/services/socket.js b/server/services/socket.js
index d797d60f96..e699ff5a7d 100644
--- a/server/services/socket.js
+++ b/server/services/socket.js
@@ -461,7 +461,13 @@ function setupCosEventForwarding() {
for (const event of ['cos:schedule:changed', 'cos:decisions:changed', 'cos:day:changed']) {
dashboardEvents.on(event, () => broadcastToCos(event, {}));
}
- settingsEvents.on('settings:updated', () => ioInstance?.emit('backup:changed', {}));
+ settingsEvents.on('settings:updated', () => {
+ ioInstance?.emit('backup:changed', {});
+ ioInstance?.emit('jev:policy', {});
+ });
+ // A failed restore also invalidates policy: clients must show the strict
+ // read failure instead of continuing to offer stale safety settings.
+ settingsEvents.on('settings:invalidated', () => ioInstance?.emit('jev:policy', {}));
for (const [source, target] of [
['scheduler:scheduled', 'cos:scheduler:changed'],
['scheduler:ran', 'cos:scheduler:changed'],
diff --git a/server/services/socket.test.js b/server/services/socket.test.js
index a8db988684..4eb4ab0891 100644
--- a/server/services/socket.test.js
+++ b/server/services/socket.test.js
@@ -205,6 +205,12 @@ describe('socket.js — initSocket', () => {
expect(io.emitted).toEqual([['usage-backfill:updated', {}]]);
});
+ it('invalidates Jev policy after an unreadable settings restore without forwarding content', () => {
+ io.emitted.length = 0;
+ settingsEvents.emit('settings:invalidated', { privateContent: 'Example settings' });
+ expect(io.emitted).toEqual([['jev:policy', {}]]);
+ });
+
it('forwards Laya status without experiment content', () => {
io.emitted.length = 0;
layaMlxEvents.emit('updated', { premise: 'Example private experiment' });
@@ -274,12 +280,12 @@ describe('socket.js — initSocket', () => {
previousIo.emitted.length = 0;
io.emitted.length = 0;
settingsEvents.emit('settings:updated', { privateContent: 'Example settings' });
- expect(io.emitted).toEqual([['backup:changed', {}]]);
+ expect(io.emitted).toEqual([['backup:changed', {}], ['jev:policy', {}]]);
dashboardEvents.emit('goals:changed', { privateContent: 'Example goal' });
dashboardEvents.emit('cos:day:changed', { privateContent: 'Example day' });
const taskChange = { action: 'updated', task: { id: 'example-task', status: 'pending' } };
queueListeners.cos.filter(([event]) => event === 'tasks:changed').forEach(([, handler]) => handler(taskChange));
- expect(io.emitted).toEqual([['backup:changed', {}], ['goals:changed', {}], ['review:queue:changed']]);
+ expect(io.emitted).toEqual([['backup:changed', {}], ['jev:policy', {}], ['goals:changed', {}], ['review:queue:changed']]);
expect(previousIo.emitted).toEqual([]);
expect(subscriber.emitted).toEqual([
['cos:day:changed', {}],