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: reset shared process logs before replaying server history

[._.]/ Adam Eivyfef8572e484d
diff --git a/client/src/hooks/useProcessLogs.js b/client/src/hooks/useProcessLogs.js
index 06608e0de4..0e0d67d863 100644
--- a/client/src/hooks/useProcessLogs.js
+++ b/client/src/hooks/useProcessLogs.js
@@ -33,7 +33,7 @@ const createEntry = (processName, lines, appId) => ({
processName,
appId,
lines,
- consumers: new Set(), // Set<{ onLine, onSubscribed }>
+ consumers: new Set(), // Set<{ onLine, onSubscribed, onReset }>
buffer: [], // shared tail buffer for late joiners, capped at MAX_LINES
subscribed: false,
});
@@ -80,7 +80,14 @@ socket.on('logs:error', (data) => {
// entry, or every mounted consumer freezes with no error frame ever emitted.
socket.on('connect', () => {
registry.forEach((entry) => {
- if (entry.consumers.size > 0) subscribeEntry(entry);
+ if (entry.consumers.size > 0) {
+ // The server replays the requested PM2 tail on every subscription.
+ // Replace each consumer's old tail before that replay arrives so a
+ // reconnect cannot append duplicate history to the shared buffer/UI.
+ entry.buffer = [];
+ entry.consumers.forEach((consumer) => consumer.onReset());
+ subscribeEntry(entry);
+ }
});
});
@@ -148,15 +155,33 @@ export function useProcessLogs(processName, options = {}) {
const consumer = {
onLine: queueLine,
onSubscribed: () => setSubscribed(true),
+ onReset: () => {
+ pendingRef.current = [];
+ if (flushTimerRef.current != null) clearTimeout(flushTimerRef.current);
+ flushTimerRef.current = null;
+ setLogs([]);
+ setSubscribed(false);
+ },
};
entry.consumers.add(consumer);
if (isFirstConsumer) {
subscribeEntry(entry);
} else {
+ // The PM2 stream is shared, so the first consumer's tail depth must not
+ // cap a later consumer that asked for more history. Reset every view
+ // before replacing the stream: the server replays the expanded tail on
+ // subscribe, and retaining existing lines would duplicate that replay.
+ if (lines > entry.lines) {
+ entry.lines = lines;
+ entry.buffer = [];
+ entry.consumers.forEach((existing) => existing.onReset());
+ subscribeEntry(entry);
+ }
// Late joiner: seed from the existing entry's tail (capped to this
// consumer's own `lines`) instead of re-subscribing, which would
- // replay the tail into every consumer already watching this stream.
+ // replay the tail into every consumer already watching this stream. An
+ // expanded stream was reset above and will seed from its replay instead.
if (entry.subscribed) setSubscribed(true);
const seeded = entry.buffer.slice(-lines);
if (seeded.length > 0) setLogs(seeded);
diff --git a/client/src/hooks/useProcessLogs.test.jsx b/client/src/hooks/useProcessLogs.test.jsx
index ce5b490beb..c7b24eb0c7 100644
--- a/client/src/hooks/useProcessLogs.test.jsx
+++ b/client/src/hooks/useProcessLogs.test.jsx
@@ -248,6 +248,35 @@ describe('useProcessLogs', () => {
second.unmount();
});
+ it('expands the shared PM2 tail when a later consumer requests more history', () => {
+ const first = renderHook(() => useProcessLogs('game', { lines: 200 }));
+ fire('logs:line', { processName: 'game', line: 'short tail', type: 'stdout', timestamp: 1 });
+
+ const second = renderHook(() => useProcessLogs('game', { lines: 500 }));
+
+ expect(emitsOf('logs:subscribe')).toEqual([
+ { processName: 'game', lines: 200 },
+ { processName: 'game', lines: 500 },
+ ]);
+ expect(first.result.current.logs).toEqual([]);
+ expect(second.result.current.logs).toEqual([]);
+
+ act(() => {
+ for (let index = 0; index < 500; index += 1) {
+ handlers.get('logs:line')?.forEach(fn => fn({
+ processName: 'game', line: `expanded replay ${index}`, type: 'stdout', timestamp: index + 2,
+ }));
+ }
+ });
+ flushLines();
+
+ expect(first.result.current.logs).toHaveLength(500);
+ expect(second.result.current.logs).toEqual(first.result.current.logs);
+
+ first.unmount();
+ second.unmount();
+ });
+
it('clear() on one consumer does not empty another consumer\'s lines', () => {
const first = renderHook(() => useProcessLogs('game'));
const second = renderHook(() => useProcessLogs('game'));
@@ -261,17 +290,26 @@ describe('useProcessLogs', () => {
second.unmount();
});
- it('re-subscribes and resumes streaming after a reconnect', () => {
+ it('replaces the replayed tail and resumes streaming after a reconnect', () => {
const { result } = renderHook(() => useProcessLogs('game'));
expect(emitsOf('logs:subscribe')).toHaveLength(1);
+ fire('logs:line', { processName: 'game', line: 'recent', type: 'stdout', timestamp: 1 });
+ expect(result.current.logs).toHaveLength(1);
+
// Server drops every stream owned by a disconnected socket
// (cleanupSocketStreams) — a mounted consumer must re-subscribe on
- // 'connect' or it freezes with no error frame ever emitted.
+ // 'connect' or it freezes with no error frame ever emitted. The new stream
+ // replays its tail, so clear the old tail before those lines arrive.
fireRaw('connect', undefined);
expect(emitsOf('logs:subscribe')).toHaveLength(2);
+ expect(result.current.logs).toEqual([]);
+ fire('logs:line', { processName: 'game', line: 'recent', type: 'stdout', timestamp: 2 });
fire('logs:line', { processName: 'game', line: 'resumed', type: 'stdout', timestamp: 3 });
- expect(result.current.logs).toEqual([{ line: 'resumed', type: 'stdout', timestamp: 3 }]);
+ expect(result.current.logs).toEqual([
+ { line: 'recent', type: 'stdout', timestamp: 2 },
+ { line: 'resumed', type: 'stdout', timestamp: 3 },
+ ]);
});
});