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: ignore stale inventory refreshes and require operator authority

[._.]/ Adam Eivy4f6269c9373b
diff --git a/client/src/components/cos/tabs/schedule/BranchReconcileOutcome.jsx b/client/src/components/cos/tabs/schedule/BranchReconcileOutcome.jsx
index 7212fe58be..e26d751ee0 100644
--- a/client/src/components/cos/tabs/schedule/BranchReconcileOutcome.jsx
+++ b/client/src/components/cos/tabs/schedule/BranchReconcileOutcome.jsx
@@ -1,6 +1,7 @@
-import { useId, useRef, useState } from 'react';
+import { useId, useLayoutEffect, useRef, useState } from 'react';
import { formatCount, formatDateTime } from '../../../../utils/formatters';
import { retireBranchReconcileCheckout, reviewBranchReconcileCheckout, refreshBranchReconcileInventory } from '../../../../services/api';
+import useMounted from '../../../../hooks/useMounted';
const STATUS_LABELS = { drained: 'No remaining work in last scan', incomplete: 'Incomplete', running: 'Sweep running' };
@@ -104,30 +105,33 @@ export default function BranchReconcileOutcome({ outcomes, apps }) {
// the schedule event's fresh inventory is authoritative once it arrives.
const [retired, setRetired] = useState({ source: outcomes, keys: NONE });
const [refreshed, setRefreshed] = useState({ source: outcomes, values: {} });
- const [refreshStates, setRefreshStates] = useState({});
- const pending = useRef(new Set());
+ const [refreshStates, setRefreshStates] = useState({ source: outcomes, values: {} });
+ const pending = useRef(new Map());
+ const latestOutcomes = useRef(outcomes);
+ const mounted = useMounted();
+ useLayoutEffect(() => { latestOutcomes.current = outcomes; }, [outcomes]);
+ const refreshStatus = refreshStates.source === outcomes ? refreshStates.values : {};
const retiredKeys = retired.source === outcomes ? retired.keys : NONE;
if (!outcomes || !Object.keys(outcomes).length) return null;
const markRetired = key => setRetired(current => ({
source: outcomes, keys: new Set(current.source === outcomes ? current.keys : NONE).add(key),
}));
const refresh = async appId => {
- if (pending.current.has(appId)) return;
- pending.current.add(appId);
- setRefreshStates(current => ({ ...current, [appId]: { pending: true } }));
- const result = await refreshBranchReconcileInventory(appId, { silent: true }).catch(err => {
- setRefreshStates(current => ({ ...current, [appId]: { error: err?.message || 'Inventory refresh failed' } }));
- return null;
- });
- pending.current.delete(appId);
- if (!result) return;
- if (!result.outcome || result.appId !== appId) {
- setRefreshStates(current => ({ ...current, [appId]: { error: 'Refresh returned no matching inventory; previous inventory retained.' } }));
+ if (pending.current.get(appId) === outcomes) return;
+ pending.current.set(appId, outcomes);
+ const setStatus = value => setRefreshStates(current => ({ source: outcomes, values: { ...(current.source === outcomes ? current.values : {}), [appId]: value } }));
+ setStatus({ pending: true });
+ const result = await refreshBranchReconcileInventory(appId, { silent: true })
+ .catch(err => ({ error: err?.message || 'Inventory refresh failed' }));
+ if (pending.current.get(appId) === outcomes) pending.current.delete(appId);
+ if (!mounted.current || latestOutcomes.current !== outcomes) return;
+ if (!result?.outcome || result.appId !== appId) {
+ setStatus({ error: result?.error || 'Refresh returned no matching inventory; previous inventory retained.' });
return;
}
setRefreshed(current => ({ source: outcomes, values: { ...(current.source === outcomes ? current.values : {}), [appId]: result.outcome } }));
setRetired(current => ({ source: outcomes, keys: new Set([...(current.source === outcomes ? current.keys : NONE)].filter(key => !key.startsWith(`${appId}\0`))) }));
- setRefreshStates(current => ({ ...current, [appId]: {} }));
+ setStatus({});
};
return (
<div className="mx-4 mb-3 space-y-2 text-xs">
@@ -151,11 +155,11 @@ export default function BranchReconcileOutcome({ outcomes, apps }) {
<div className="mt-2 space-y-2 text-gray-400">
<p>{outcome.observedAt ? `Last inventory: ${formatDateTime(outcome.observedAt)}` : 'No readable inventory yet.'}</p>
{appId !== 'null' && <div className="space-y-1">
- <button type="button" className="rounded border border-port-border px-2 py-1 disabled:opacity-50" disabled={refreshStates[appId]?.pending} onClick={() => refresh(appId)}>
- {refreshStates[appId]?.pending ? 'Refreshing inventory…' : 'Refresh inventory'}
+ <button type="button" className="rounded border border-port-border px-2 py-1 disabled:opacity-50" disabled={refreshStatus[appId]?.pending} onClick={() => refresh(appId)}>
+ {refreshStatus[appId]?.pending ? 'Refreshing inventory…' : 'Refresh inventory'}
</button>
<p>Rescan this repository without deleting checkouts or starting an AI agent. Missing archive evidence is retained.</p>
- {refreshStates[appId]?.error && <p role="alert" className="break-words text-port-error">{refreshStates[appId].error}</p>}
+ {refreshStatus[appId]?.error && <p role="alert" className="break-words text-port-error">{refreshStatus[appId].error}</p>}
</div>}
{incompleteEmptyInventory && <p>Last sweep: Incomplete</p>}
{outcome.reason && <p className="break-words">Reason: {outcome.reason}</p>}
diff --git a/client/src/components/cos/tabs/schedule/BranchReconcileOutcome.test.jsx b/client/src/components/cos/tabs/schedule/BranchReconcileOutcome.test.jsx
index bb7256bc90..664c10f089 100644
--- a/client/src/components/cos/tabs/schedule/BranchReconcileOutcome.test.jsx
+++ b/client/src/components/cos/tabs/schedule/BranchReconcileOutcome.test.jsx
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
-import { render, screen, fireEvent, waitFor } from '@testing-library/react';
+import { act, render, screen, fireEvent, waitFor, within } from '@testing-library/react';
const retire = vi.hoisted(() => vi.fn());
const prepare = vi.hoisted(() => vi.fn());
@@ -140,3 +140,20 @@ it('keeps the previous inventory and permits retry when a refresh fails', async
expect(screen.getByText('/example/held')).toBeVisible();
expect(screen.getByRole('button', { name: 'Refresh inventory' })).toBeEnabled();
});
+
+it('ignores a late refresh from an older schedule generation without discarding another app’s new result', async () => {
+ let finishOld;
+ let finishNew;
+ refresh.mockImplementationOnce(() => new Promise(resolve => { finishOld = resolve; }))
+ .mockImplementationOnce(() => new Promise(resolve => { finishNew = resolve; }));
+ const source = { ...outcomes, 'app-2': outcomes['app-1'] };
+ const { rerender } = render(<BranchReconcileOutcome outcomes={source} apps={[]} />);
+ fireEvent.click(within(screen.getByText(/app-1 · Incomplete/).closest('details')).getByRole('button', { name: 'Refresh inventory', hidden: true }));
+ rerender(<BranchReconcileOutcome outcomes={{ ...source }} apps={[]} />);
+ fireEvent.click(within(screen.getByText(/app-2 · Incomplete/).closest('details')).getByRole('button', { name: 'Refresh inventory', hidden: true }));
+ await act(async () => finishNew({ appId: 'app-2', outcome: { status: 'drained', remaining: [] } }));
+ expect(screen.getByText(/app-2 · No remaining work in last scan/)).toBeInTheDocument();
+ await act(async () => finishOld({ appId: 'app-1', outcome: { status: 'drained', remaining: [] } }));
+ expect(screen.getByText(/app-2 · No remaining work in last scan/)).toBeInTheDocument();
+ expect(screen.getByText(/app-1 · Incomplete · 2 remaining/)).toBeInTheDocument();
+});
diff --git a/server/lib/hostControlRoutes.js b/server/lib/hostControlRoutes.js
index bc55c366af..9f81eefd01 100644
--- a/server/lib/hostControlRoutes.js
+++ b/server/lib/hostControlRoutes.js
@@ -495,6 +495,7 @@ export const HOST_CONTROL_ROUTES = Object.freeze([
'PUT /api/tools/:id',
'PUT /api/cos/schedule/task/:taskType',
'POST /api/cos/schedule/trigger',
+ 'POST /api/cos/schedule/branch-reconcile/refresh',
// Archives and removes a checkout and its merged branch under a scoped release.
'POST /api/cos/schedule/branch-reconcile/checkouts/retire',
'POST /api/cos/schedule/branch-reconcile/checkouts/review',