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 feature-agent output by request sequence, not agent id (#3294)

[._.]/ Adam Eivy717de3fb0ca9
diff --git a/client/src/components/feature-agents/OutputTab.jsx b/client/src/components/feature-agents/OutputTab.jsx
index d84acd36ec..83b5d41ee5 100644
--- a/client/src/components/feature-agents/OutputTab.jsx
+++ b/client/src/components/feature-agents/OutputTab.jsx
@@ -7,14 +7,18 @@ export default function OutputTab({ agent }) {
const [output, setOutput] = useState('');
const [agentId, setAgentId] = useState(null);
const bottomRef = useRef(null);
- // Always holds the currently-selected agent so a response can be matched
- // against it. The refresh button fires outside the fetch effect, so an
- // effect-local `cancelled` flag would not cover it.
- const selectedIdRef = useRef(agent.id);
+ // Monotonic request counter: only the most recently issued fetch may write
+ // state. An effect-local `cancelled` flag can't cover this, because the
+ // refresh button fires outside the effect — and comparing agent ids alone
+ // wouldn't either, since two fetches for the *same* agent (a run starting,
+ // or a second refresh click) can still resolve out of order and repaint an
+ // older run's output.
+ const requestSeqRef = useRef(0);
const loadOutput = useCallback((id) => {
+ const seq = ++requestSeqRef.current;
api.getFeatureAgentOutput(id).then(data => {
- if (selectedIdRef.current !== id || !data) return;
+ if (seq !== requestSeqRef.current || !data) return;
setOutput(data.output || '');
setAgentId(data.agentId);
}).catch(() => {});
@@ -25,7 +29,6 @@ export default function OutputTab({ agent }) {
// newly selected agent had already produced it. Keyed on `agent.id` alone so
// a run starting (`currentAgentId` change) doesn't wipe streamed output.
useEffect(() => {
- selectedIdRef.current = agent.id;
setOutput('');
setAgentId(null);
}, [agent.id]);
diff --git a/client/src/components/feature-agents/OutputTab.test.jsx b/client/src/components/feature-agents/OutputTab.test.jsx
index fdaf05c72d..f3d5f712df 100644
--- a/client/src/components/feature-agents/OutputTab.test.jsx
+++ b/client/src/components/feature-agents/OutputTab.test.jsx
@@ -77,6 +77,30 @@ describe('OutputTab staleness', () => {
expect(screen.getByText(/B output/)).toBeInTheDocument();
});
+ it('drops an out-of-order response for the same agent when a new run starts', async () => {
+ // Same feature agent, two runs — so an agent-id comparison alone would let
+ // the slow first response repaint over the newer run's output.
+ let resolveFirst;
+ const responses = [
+ new Promise((res) => { resolveFirst = res; }),
+ Promise.resolve({ output: 'run-2 output', agentId: 'run-2' }),
+ ];
+ getFeatureAgentOutput.mockImplementation(() => responses.shift());
+
+ const { rerender } = render(<OutputTab agent={{ id: 'agent-a', currentAgentId: 'run-1' }} />);
+ rerender(<OutputTab agent={{ id: 'agent-a', currentAgentId: 'run-2' }} />);
+ await act(async () => {});
+ expect(screen.getByText(/run-2 output/)).toBeInTheDocument();
+
+ await act(async () => {
+ resolveFirst({ output: 'run-1 output', agentId: 'run-1' });
+ await Promise.resolve();
+ });
+
+ expect(screen.queryByText(/run-1 output/)).not.toBeInTheDocument();
+ expect(screen.getByText(/run-2 output/)).toBeInTheDocument();
+ });
+
it('clears the prior agent output while the new agent fetch is still in flight', async () => {
let resolveB;
getFeatureAgentOutput.mockImplementation((agentId) => {
diff --git a/client/src/components/feature-agents/RunsTab.jsx b/client/src/components/feature-agents/RunsTab.jsx
index b045091df5..4c31a308bb 100644
--- a/client/src/components/feature-agents/RunsTab.jsx
+++ b/client/src/components/feature-agents/RunsTab.jsx
@@ -13,7 +13,10 @@ export default function RunsTab({ agent }) {
let cancelled = false;
// Clear the previous agent's runs while the new fetch is in flight —
// otherwise they stay on screen as though they belonged to this agent.
+ // `expanded` is keyed by row index, so it has to be dropped too or the new
+ // agent's list opens whichever rows the previous agent had expanded.
setRuns([]);
+ setExpanded({});
setLoading(true);
api.getFeatureAgentRuns(agent.id, 50).then(data => {
if (cancelled) return;