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: guard async fetch effects against stale responses overwriting a newer selection

[._.]/ Adam Eivyc3fc2d43aca2
diff --git a/client/src/components/feature-agents/OutputTab.jsx b/client/src/components/feature-agents/OutputTab.jsx
index 82f8e99f42..6e8442cf78 100644
--- a/client/src/components/feature-agents/OutputTab.jsx
+++ b/client/src/components/feature-agents/OutputTab.jsx
@@ -9,8 +9,10 @@ export default function OutputTab({ agent }) {
const bottomRef = useRef(null);
useEffect(() => {
+ let cancelled = false;
const fetch = () => {
api.getFeatureAgentOutput(agent.id).then(data => {
+ if (cancelled) return;
if (data) {
setOutput(data.output || '');
setAgentId(data.agentId);
@@ -28,6 +30,7 @@ export default function OutputTab({ agent }) {
socket.on('cos:feature-agent:output', handler);
return () => {
+ cancelled = true;
socket.off('cos:agent:output', handler);
socket.off('cos:feature-agent:output', handler);
};
diff --git a/client/src/components/feature-agents/OutputTab.test.jsx b/client/src/components/feature-agents/OutputTab.test.jsx
new file mode 100644
index 0000000000..88dff950fb
--- /dev/null
+++ b/client/src/components/feature-agents/OutputTab.test.jsx
@@ -0,0 +1,54 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { act, render, screen } from '@testing-library/react';
+
+const getFeatureAgentOutput = vi.fn();
+
+vi.mock('../../services/api', () => ({
+ getFeatureAgentOutput: (...args) => getFeatureAgentOutput(...args),
+}));
+
+vi.mock('../../services/socket', () => ({
+ default: {
+ on: vi.fn(),
+ off: vi.fn(),
+ },
+}));
+
+import OutputTab from './OutputTab.jsx';
+
+beforeEach(() => {
+ getFeatureAgentOutput.mockReset();
+});
+
+describe('OutputTab staleness', () => {
+ it('does not let a slow prior agent response overwrite the newer agent selection', async () => {
+ // Agent A's fetch resolves late; agent B's fetch resolves fast.
+ let resolveA;
+ const aPromise = new Promise((res) => { resolveA = res; });
+ getFeatureAgentOutput.mockImplementation((agentId) => {
+ if (agentId === 'agent-a') return aPromise;
+ if (agentId === 'agent-b') return Promise.resolve({ output: 'B output', agentId: 'run-b' });
+ return Promise.resolve(null);
+ });
+
+ const { rerender } = render(<OutputTab agent={{ id: 'agent-a', currentAgentId: null }} />);
+
+ // Switch to agent B before A's fetch resolves — B's effect cleanup marks
+ // A's in-flight fetch cancelled.
+ rerender(<OutputTab agent={{ id: 'agent-b', currentAgentId: null }} />);
+ await act(async () => {});
+
+ expect(screen.getByText(/B output/)).toBeInTheDocument();
+ expect(screen.getByText(/run-b/)).toBeInTheDocument();
+
+ // Now the stale agent-A response finally arrives — it must be ignored.
+ await act(async () => {
+ resolveA({ output: 'A output', agentId: 'run-a' });
+ await Promise.resolve();
+ });
+
+ expect(screen.getByText(/B output/)).toBeInTheDocument();
+ expect(screen.getByText(/run-b/)).toBeInTheDocument();
+ expect(screen.queryByText(/A output/)).not.toBeInTheDocument();
+ });
+});
diff --git a/client/src/components/feature-agents/RunsTab.jsx b/client/src/components/feature-agents/RunsTab.jsx
index e2cea6252b..adfb0be9f2 100644
--- a/client/src/components/feature-agents/RunsTab.jsx
+++ b/client/src/components/feature-agents/RunsTab.jsx
@@ -10,10 +10,13 @@ export default function RunsTab({ agent }) {
const [expanded, setExpanded] = useState({});
useEffect(() => {
+ let cancelled = false;
api.getFeatureAgentRuns(agent.id, 50).then(data => {
+ if (cancelled) return;
setRuns(data || []);
setLoading(false);
- }).catch(() => setLoading(false));
+ }).catch(() => { if (!cancelled) setLoading(false); });
+ return () => { cancelled = true; };
}, [agent.id]);
if (loading) {
diff --git a/client/src/components/messages/MessageDetail.jsx b/client/src/components/messages/MessageDetail.jsx
index 805208a63c..5eb11d529d 100644
--- a/client/src/components/messages/MessageDetail.jsx
+++ b/client/src/components/messages/MessageDetail.jsx
@@ -98,11 +98,13 @@ export default function MessageDetail({ message, accounts, onBack }) {
// Load thread messages if this message is part of a thread
useEffect(() => {
if (!message.threadId || !message.accountId) return;
+ let cancelled = false;
setThreadLoading(true);
api.getMessageThread(message.accountId, message.threadId)
- .then(data => setThreadMessages(data?.messages || []))
- .catch(() => setThreadMessages([]))
- .finally(() => setThreadLoading(false));
+ .then(data => { if (!cancelled) setThreadMessages(data?.messages || []); })
+ .catch(() => { if (!cancelled) setThreadMessages([]); })
+ .finally(() => { if (!cancelled) setThreadLoading(false); });
+ return () => { cancelled = true; };
}, [message.threadId, message.accountId]);
const handleRefresh = async () => {