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

address /simplify: reuse getProcessesList, guard log-fetch race, de-dup

[._.]/ Adam Eivy7bd889febe19
diff --git a/client/src/components/ui/ProcessLogModal.jsx b/client/src/components/ui/ProcessLogModal.jsx
index 316fd7287a..a6b0a0b744 100644
--- a/client/src/components/ui/ProcessLogModal.jsx
+++ b/client/src/components/ui/ProcessLogModal.jsx
@@ -6,21 +6,17 @@
* RunsHistoryPage). Fetches a static tail via `getProcessLogs` (not the live SSE
* stream — a past failure's context is already written), with a process picker,
* a tail-length selector, and a manual refresh.
+ *
+ * Fully generic: the caller passes the process to open (`processName`) — no run
+ * or domain coupling lives here.
*/
-import { useState, useEffect, useCallback } from 'react';
+import { useState, useEffect, useCallback, useRef } from 'react';
import { RefreshCw, X } from 'lucide-react';
import Modal from './Modal';
import BrailleSpinner from '../BrailleSpinner';
import * as api from '../../services/api';
-// Map an AI run's source to the PM2 process whose system log holds the full
-// context for that run. CoS-agent runs are driven by the `portos-cos` process;
-// everything else (DevTools runs) is driven by the main `portos-server`.
-export function runLogProcessName(source) {
- return source === 'cos-agent' ? 'portos-cos' : 'portos-server';
-}
-
const TAIL_OPTIONS = [100, 250, 500, 1000, 2000];
export default function ProcessLogModal({ open, onClose, processName, title = 'System Logs' }) {
@@ -30,38 +26,47 @@ export default function ProcessLogModal({ open, onClose, processName, title = 'S
const [logs, setLogs] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
+ // Monotonic request id — a superseded (or post-close) fetch is a no-op so a
+ // slow response can't clobber the newer process's logs.
+ const reqIdRef = useRef(0);
+
+ // Seed the selected process from the caller's hint whenever the modal opens.
+ useEffect(() => {
+ if (open) setSelected(processName || '');
+ }, [open, processName]);
- // When the modal opens, seed the selected process from the caller's hint and
- // load the picker options. Guard against a late response landing after close.
+ // Load the PM2 process picker options once per open (static across a session).
useEffect(() => {
if (!open) return;
let cancelled = false;
- setSelected(processName || '');
- api.getLogProcesses()
+ api.getProcessesList({ silent: true })
.then((list) => { if (!cancelled) setProcesses(Array.isArray(list) ? list : []); })
.catch(() => { if (!cancelled) setProcesses([]); });
return () => { cancelled = true; };
- }, [open, processName]);
+ }, [open]);
const loadLogs = useCallback(async () => {
if (!selected) return;
+ const reqId = ++reqIdRef.current;
setLoading(true);
setError('');
const res = await api.getProcessLogs(selected, tailLines).catch((err) => {
- setError(err?.message || 'Failed to load logs');
+ if (reqId === reqIdRef.current) setError(err?.message || 'Failed to load logs');
return null;
});
+ if (reqId !== reqIdRef.current) return; // superseded by a newer request
if (res) setLogs(res.logs || '');
setLoading(false);
}, [selected, tailLines]);
// Fetch whenever the modal is open and the process / tail length changes.
+ // The cleanup bumps the request id so an in-flight fetch is invalidated when
+ // the deps change or the modal closes.
useEffect(() => {
if (!open || !selected) return;
loadLogs();
- }, [open, selected, tailLines, loadLogs]);
-
- if (!open) return null;
+ return () => { reqIdRef.current++; };
+ }, [open, selected, loadLogs]);
return (
<Modal open={open} onClose={onClose} size="3xl" align="top" ariaLabel={title}>
diff --git a/client/src/components/ui/ProcessLogModal.test.jsx b/client/src/components/ui/ProcessLogModal.test.jsx
index b5f42d1d89..3f58e5b7f2 100644
--- a/client/src/components/ui/ProcessLogModal.test.jsx
+++ b/client/src/components/ui/ProcessLogModal.test.jsx
@@ -2,28 +2,17 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, waitFor, within } from '@testing-library/react';
vi.mock('../../services/api', () => ({
- getLogProcesses: vi.fn(),
+ getProcessesList: vi.fn(),
getProcessLogs: vi.fn(),
}));
-import { getLogProcesses, getProcessLogs } from '../../services/api';
-import ProcessLogModal, { runLogProcessName } from './ProcessLogModal';
-
-describe('runLogProcessName', () => {
- it('maps cos-agent runs to the portos-cos process', () => {
- expect(runLogProcessName('cos-agent')).toBe('portos-cos');
- });
-
- it('maps devtools and unknown sources to the main portos-server process', () => {
- expect(runLogProcessName('devtools')).toBe('portos-server');
- expect(runLogProcessName(undefined)).toBe('portos-server');
- });
-});
+import { getProcessesList, getProcessLogs } from '../../services/api';
+import ProcessLogModal from './ProcessLogModal';
describe('ProcessLogModal', () => {
beforeEach(() => {
vi.clearAllMocks();
- getLogProcesses.mockResolvedValue([{ name: 'portos-server' }, { name: 'portos-cos' }]);
+ getProcessesList.mockResolvedValue([{ name: 'portos-server' }, { name: 'portos-cos' }]);
getProcessLogs.mockResolvedValue({ processName: 'portos-server', lines: 200, logs: 'boot ok\nerror: boom' });
});
@@ -53,7 +42,7 @@ describe('ProcessLogModal', () => {
});
it('keeps the hinted process selectable even when PM2 does not report it', async () => {
- getLogProcesses.mockResolvedValueOnce([{ name: 'portos-cos' }]);
+ getProcessesList.mockResolvedValueOnce([{ name: 'portos-cos' }]);
render(<ProcessLogModal open onClose={() => {}} processName="portos-server" />);
await waitFor(() => expect(getProcessLogs).toHaveBeenCalledWith('portos-server', 200));
const select = screen.getByLabelText('Process');
diff --git a/client/src/pages/RunsHistoryPage.jsx b/client/src/pages/RunsHistoryPage.jsx
index a42a37e107..6b0281ffca 100644
--- a/client/src/pages/RunsHistoryPage.jsx
+++ b/client/src/pages/RunsHistoryPage.jsx
@@ -5,9 +5,16 @@ import * as api from '../services/api';
import { formatTime, formatRuntime, formatBytes, formatDateTime } from '../utils/formatters';
import BrailleSpinner from '../components/BrailleSpinner';
import Banner from '../components/ui/Banner';
-import ProcessLogModal, { runLogProcessName } from '../components/ui/ProcessLogModal';
+import ProcessLogModal from '../components/ui/ProcessLogModal';
import { writeClipboardSilently } from '../lib/clipboard';
+// Map an AI run's source to the PM2 process whose system log holds the full
+// context for that run. CoS-agent runs are driven by the `portos-cos` process;
+// everything else (DevTools runs) is driven by the main `portos-server`.
+export function runLogProcessName(source) {
+ return source === 'cos-agent' ? 'portos-cos' : 'portos-server';
+}
+
export function RunsHistoryPage() {
const navigate = useNavigate();
const [runs, setRuns] = useState([]);
@@ -141,6 +148,7 @@ export function RunsHistoryPage() {
}
const failedCount = runs.filter(r => r.success === false).length;
+ const logProc = logModalRun ? runLogProcessName(logModalRun.source) : '';
const handleClearFailed = async () => {
const ok = await api.deleteFailedRuns().then(() => true).catch(() => false);
@@ -473,8 +481,8 @@ export function RunsHistoryPage() {
<ProcessLogModal
open={!!logModalRun}
onClose={() => setLogModalRun(null)}
- processName={logModalRun ? runLogProcessName(logModalRun.source) : ''}
- title={logModalRun ? `System Logs — ${runLogProcessName(logModalRun.source)}` : 'System Logs'}
+ processName={logProc}
+ title={logProc ? `System Logs — ${logProc}` : 'System Logs'}
/>
</div>
);
diff --git a/client/src/pages/RunsHistoryPage.test.jsx b/client/src/pages/RunsHistoryPage.test.jsx
new file mode 100644
index 0000000000..efe6e4d97d
--- /dev/null
+++ b/client/src/pages/RunsHistoryPage.test.jsx
@@ -0,0 +1,18 @@
+import { describe, it, expect, vi } from 'vitest';
+
+// RunsHistoryPage pulls in the API barrel transitively; stub it so importing the
+// module for the pure-helper test doesn't drag real HTTP wrappers into scope.
+vi.mock('../services/api', () => ({}));
+
+import { runLogProcessName } from './RunsHistoryPage';
+
+describe('runLogProcessName', () => {
+ it('maps cos-agent runs to the portos-cos process', () => {
+ expect(runLogProcessName('cos-agent')).toBe('portos-cos');
+ });
+
+ it('maps devtools and unknown sources to the main portos-server process', () => {
+ expect(runLogProcessName('devtools')).toBe('portos-server');
+ expect(runLogProcessName(undefined)).toBe('portos-server');
+ });
+});
diff --git a/client/src/services/README.md b/client/src/services/README.md
index e82c175779..b7109aff2a 100644
--- a/client/src/services/README.md
+++ b/client/src/services/README.md
@@ -47,7 +47,7 @@ toasts on throw). **Custom catch ⇒ `silent: true`** — otherwise toasts fire
| `apiGit.js` | Git operations. |
| `apiGithub.js` | GitHub repo metadata. |
| `apiHistory.js` | Historical logs / runs. |
-| `apiLogs.js` | Live PM2 system logs: list processes + fetch a process's recent log tail. |
+| `apiLogs.js` | PM2 system logs: fetch a process's recent log tail (process list comes from `apiCommands.getProcessesList`). |
| `apiPorts.js` | Port forwarding / allocation. |
| `apiProviders.js` | AI provider config. |
| `apiReferenceRepos.js` | Per-app reference-repo registry. |
diff --git a/client/src/services/apiLogs.js b/client/src/services/apiLogs.js
index 44102ea5fd..9387bc58a7 100644
--- a/client/src/services/apiLogs.js
+++ b/client/src/services/apiLogs.js
@@ -1,10 +1,7 @@
import { request } from './apiCore.js';
-// System (PM2) logs
-// List every PM2 process so a log viewer can offer a process picker.
-export const getLogProcesses = () => request('/logs/processes');
-
// Fetch the recent tail of a PM2 process's system log. Returns
// `{ processName, lines, logs }` where `logs` is the raw multi-line text.
+// (The PM2 process *list* comes from the existing `getProcessesList` wrapper.)
export const getProcessLogs = (processName, lines = 200) =>
request(`/logs/${encodeURIComponent(processName)}?lines=${lines}`);