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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(privacy): never erase an organization's holdings after a failed read
[._.]/ Adam Eivy03771b6819fc
diff --git a/client/src/components/privacy/OrgDrawer.jsx b/client/src/components/privacy/OrgDrawer.jsxindex b61cb2d6c6..9423876aef 100644--- a/client/src/components/privacy/OrgDrawer.jsx+++ b/client/src/components/privacy/OrgDrawer.jsx@@ -5,6 +5,7 @@ import { useAsyncAction } from '../../hooks/useAsyncAction';import {createPrivacyOrg, updatePrivacyOrg, getOrgHoldings, setOrgHoldings, getSocialAccounts,} from '../../services/api';+import ListLoadError from '../ui/ListLoadError';import toast from '../ui/Toast';import { ORG_CATEGORIES, ORG_TRUST_LEVELS, ORG_STATUSES, VAULT_TYPES, INPUT_CLS, labelFor } from './constants';@@ -13,6 +14,9 @@ const EMPTY = {contactEmail: '', contactPhone: '', notes: '', socialAccountId: '',};+const HOLDINGS_READY = Object.freeze({ state: 'ready', error: null });+const HOLDINGS_LOADING = Object.freeze({ state: 'loading', error: null });+// Human label for a Digital Twin social account in the picker.const accountLabel = (a) => {const handle = a.username ? `@${a.username}` : '';@@ -24,10 +28,15 @@ const accountLabel = (a) => {// picker is a checkbox list over the user's vault records (masked values only).// Holdings are saved via the replace-set endpoint after the org exists (on// create we POST the org first, then set holdings against the returned id).-export default function OrgDrawer({ open, org, subjectId, vaultRecords, onClose, onSaved }) {+export default function OrgDrawer({ open, org, subjectId, vaultRecords, vaultError = null, onRetryVault, onClose, onSaved }) {const editing = !!org;const [form, setForm] = useState(EMPTY);const [selected, setSelected] = useState(() => new Set()); // vaultRecordId set+ // The replace-set write is only safe once the org's stored holdings are in+ // `selected`: saving while that read is pending or failed would send [] and+ // erase every holding the org has.+ const [holdingsLoad, setHoldingsLoad] = useState(HOLDINGS_READY);+ const [holdingsAttempt, setHoldingsAttempt] = useState(0);const [socialAccounts, setSocialAccounts] = useState([]); // Digital Twin accounts for the cross-link pickeruseEffect(() => {@@ -47,12 +56,6 @@ export default function OrgDrawer({ open, org, subjectId, vaultRecords, onClose,}: { ...EMPTY });setSelected(new Set());- // Load existing holdings for an org being edited.- if (org?.id) {- getOrgHoldings(org.id, { silent: true })- .then((h) => { if (active) setSelected(new Set((h || []).map((x) => x.vaultRecordId))); })- .catch(() => {});- }// Load Digital Twin social accounts for the cross-link picker (#2147). Degrades// to no picker options if the twin has none — never blocks org editing.getSocialAccounts()@@ -61,6 +64,23 @@ export default function OrgDrawer({ open, org, subjectId, vaultRecords, onClose,return () => { active = false; };}, [open, org]);+ useEffect(() => {+ if (!open || !org?.id) {+ setHoldingsLoad(HOLDINGS_READY);+ return undefined;+ }+ let active = true;+ setHoldingsLoad(HOLDINGS_LOADING);+ getOrgHoldings(org.id, { silent: true })+ .then((h) => {+ if (!active) return;+ setSelected(new Set((h || []).map((x) => x.vaultRecordId)));+ setHoldingsLoad(HOLDINGS_READY);+ })+ .catch((error) => { if (active) setHoldingsLoad({ state: 'error', error }); });+ return () => { active = false; };+ }, [open, org, holdingsAttempt]);+const set = (k, v) => setForm((prev) => ({ ...prev, [k]: v }));const toggleHolding = (id) => setSelected((prev) => {const next = new Set(prev);@@ -94,9 +114,10 @@ export default function OrgDrawer({ open, org, subjectId, vaultRecords, onClose,: await createPrivacyOrg({ ...payload, subjectId }, { silent: true });if (!savedOrg) return null;- // Persist holdings (replace-set). Skip the call on create when nothing is selected.+ // Persist holdings (replace-set). Skip the call on create when nothing is+ // selected, and on edit until the stored holdings have loaded.const holdings = [...selected].map((vaultRecordId) => ({ vaultRecordId }));- if (editing || holdings.length > 0) {+ if (editing ? holdingsLoad.state === 'ready' : holdings.length > 0) {await setOrgHoldings(savedOrg.id, holdings, { silent: true }).catch(() => {toast.error('Organization saved, but holdings could not be updated');});@@ -175,7 +196,13 @@ export default function OrgDrawer({ open, org, subjectId, vaultRecords, onClose,<div><span className="block text-sm text-gray-400 mb-1">Holdings — which vault records does this org have?</span>- {(!vaultRecords || vaultRecords.length === 0) ? (+ {holdingsLoad.state === 'error' ? (+ <ListLoadError what="this organization's holdings" error={holdingsLoad.error} onRetry={() => setHoldingsAttempt((n) => n + 1)} />+ ) : holdingsLoad.state === 'loading' ? (+ <p className="text-xs text-gray-500">Loading holdings…</p>+ ) : vaultError ? (+ <ListLoadError what="vault records" error={vaultError} onRetry={onRetryVault} />+ ) : (!vaultRecords || vaultRecords.length === 0) ? (<p className="text-xs text-gray-500">No vault records yet. Add records in the Vault tab first.</p>) : (<div className="max-h-52 overflow-y-auto border border-port-border rounded divide-y divide-port-border">diff --git a/client/src/components/privacy/OrgDrawer.test.jsx b/client/src/components/privacy/OrgDrawer.test.jsxnew file mode 100644index 0000000000..9265cb07cf--- /dev/null+++ b/client/src/components/privacy/OrgDrawer.test.jsx@@ -0,0 +1,60 @@+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';+import { render, screen, fireEvent, cleanup, waitFor } from '@testing-library/react';++vi.mock('../../services/api', () => ({+ createPrivacyOrg: vi.fn(),+ updatePrivacyOrg: vi.fn(),+ getOrgHoldings: vi.fn(),+ setOrgHoldings: vi.fn(),+ getSocialAccounts: vi.fn(),+}));++import * as api from '../../services/api';+import OrgDrawer from './OrgDrawer';++const ORG = { id: 'org-1', name: 'Acme Bank', category: 'bank' };+const VAULT = [{ id: 'v1', label: 'Example email', type: 'email', maskedValue: 'a***@example.com' }];++const renderDrawer = () => {+ const onSaved = vi.fn();+ render(<OrgDrawer open org={ORG} vaultRecords={VAULT} onClose={() => {}} onSaved={onSaved} />);+ return { onSaved };+};++beforeEach(() => {+ api.getSocialAccounts.mockResolvedValue({ accounts: [] });+ api.updatePrivacyOrg.mockImplementation(async (id, payload) => ({ id, ...payload }));+ api.setOrgHoldings.mockResolvedValue([]);+});++afterEach(() => {+ cleanup();+ vi.clearAllMocks();+});++describe('OrgDrawer holdings (replace-set)', () => {+ it('never erases stored holdings when their read failed', async () => {+ api.getOrgHoldings.mockRejectedValueOnce(new Error('boom'));+ const { onSaved } = renderDrawer();++ expect(await screen.findByText(/Could not load this organization's holdings/)).toBeInTheDocument();+ fireEvent.click(screen.getByRole('button', { name: 'Save changes' }));++ await waitFor(() => expect(onSaved).toHaveBeenCalled());+ expect(api.updatePrivacyOrg).toHaveBeenCalledWith('org-1', expect.objectContaining({ name: 'Acme Bank' }), { silent: true });+ expect(api.setOrgHoldings).not.toHaveBeenCalled();+ });++ it('writes the loaded holdings once a retry succeeds', async () => {+ api.getOrgHoldings.mockRejectedValueOnce(new Error('boom'));+ api.getOrgHoldings.mockResolvedValue([{ vaultRecordId: 'v1' }]);+ const { onSaved } = renderDrawer();++ fireEvent.click(await screen.findByRole('button', { name: 'Retry' }));+ expect(await screen.findByRole('checkbox')).toBeChecked();+ fireEvent.click(screen.getByRole('button', { name: 'Save changes' }));++ await waitFor(() => expect(onSaved).toHaveBeenCalled());+ expect(api.setOrgHoldings).toHaveBeenCalledWith('org-1', [{ vaultRecordId: 'v1' }], { silent: true });+ });+});diff --git a/client/src/components/privacy/PrivacyOrgsTab.jsx b/client/src/components/privacy/PrivacyOrgsTab.jsxindex 96d353567f..012c0c7842 100644--- a/client/src/components/privacy/PrivacyOrgsTab.jsx+++ b/client/src/components/privacy/PrivacyOrgsTab.jsx@@ -3,6 +3,7 @@ import { Plus, Pencil, Trash2, ExternalLink, Building2 } from 'lucide-react';import {getPrivacyOrgs, deletePrivacyOrg, getVaultRecords, getOrgHoldings,} from '../../services/api';+import ListLoadError from '../ui/ListLoadError';import toast from '../ui/Toast';import InlineConfirmRow from '../ui/InlineConfirmRow';import OrgDrawer from './OrgDrawer';@@ -40,6 +41,8 @@ function ChipFilter({ label, options, value, onChange }) {export default function PrivacyOrgsTab({ subjectId }) {const [orgs, setOrgs] = useState([]);const [vaultRecords, setVaultRecords] = useState([]);+ const [orgsError, setOrgsError] = useState(null);+ const [vaultError, setVaultError] = useState(null);const [holdingsByOrg, setHoldingsByOrg] = useState({}); // orgId -> [holdings]const [loading, setLoading] = useState(true);const [filters, setFilters] = useState({ trust: null, category: null, status: null });@@ -51,9 +54,11 @@ export default function PrivacyOrgsTab({ subjectId }) {const load = useCallback(() => {const isCurrent = beginRequest();setLoading(true);+ setOrgsError(null);+ setVaultError(null);// Holdings depend only on the org list, so they chain off it directly — the// vault read runs alongside rather than gating the fan-out.- const orgsThenHoldings = getPrivacyOrgs({}, { subjectId })+ const orgsThenHoldings = getPrivacyOrgs({}, { subjectId, silent: true }).then(async (orgList) => {if (!isCurrent()) return;setOrgs(orgList);@@ -64,11 +69,11 @@ export default function PrivacyOrgsTab({ subjectId }) {}));if (isCurrent()) setHoldingsByOrg(Object.fromEntries(entries));})- .catch(() => { if (isCurrent()) { setOrgs([]); setHoldingsByOrg({}); } });+ .catch((err) => { if (isCurrent()) { setOrgs([]); setHoldingsByOrg({}); setOrgsError(err); } });- const vault = getVaultRecords(undefined, { subjectId })+ const vault = getVaultRecords(undefined, { subjectId, silent: true }).then((v) => { if (isCurrent()) setVaultRecords(v || []); })- .catch(() => { if (isCurrent()) setVaultRecords([]); });+ .catch((err) => { if (isCurrent()) { setVaultRecords([]); setVaultError(err); } });Promise.all([orgsThenHoldings, vault]).then(() => { if (isCurrent()) setLoading(false); });}, [subjectId, beginRequest]);@@ -125,6 +130,8 @@ export default function PrivacyOrgsTab({ subjectId }) {{loading ? (<div className="text-gray-500 text-sm py-8 text-center">Loading organizations…</div>+ ) : orgsError ? (+ <ListLoadError what="organizations" error={orgsError} onRetry={load} />) : visible.length === 0 ? (<div className="border border-dashed border-port-border rounded-lg p-8 text-center text-gray-500 text-sm">{orgs.length === 0@@ -206,6 +213,8 @@ export default function PrivacyOrgsTab({ subjectId }) {org={editing}subjectId={subjectId}vaultRecords={vaultRecords}+ vaultError={vaultError}+ onRetryVault={load}onClose={() => setDrawerOpen(false)}onSaved={handleSaved}/>diff --git a/client/src/components/privacy/PrivacyOrgsTab.test.jsx b/client/src/components/privacy/PrivacyOrgsTab.test.jsxnew file mode 100644index 0000000000..5371ab2646--- /dev/null+++ b/client/src/components/privacy/PrivacyOrgsTab.test.jsx@@ -0,0 +1,37 @@+import { describe, it, expect, vi, afterEach } from 'vitest';+import { render, screen, fireEvent, cleanup } from '@testing-library/react';++vi.mock('../../services/api', () => ({+ getPrivacyOrgs: vi.fn(),+ deletePrivacyOrg: vi.fn(),+ getVaultRecords: vi.fn(),+ getOrgHoldings: vi.fn(),+ createPrivacyOrg: vi.fn(),+ updatePrivacyOrg: vi.fn(),+ setOrgHoldings: vi.fn(),+ getSocialAccounts: vi.fn(),+}));++import * as api from '../../services/api';+import PrivacyOrgsTab from './PrivacyOrgsTab';++afterEach(() => {+ cleanup();+ vi.clearAllMocks();+});++describe('PrivacyOrgsTab', () => {+ it('shows a failed organization read as a retryable error, not "No organizations yet"', async () => {+ api.getPrivacyOrgs.mockRejectedValueOnce(new Error('boom'));+ api.getPrivacyOrgs.mockResolvedValue([{ id: 'org-1', name: 'Acme Bank', category: 'bank', trust: 'trusted', status: 'active' }]);+ api.getVaultRecords.mockResolvedValue([]);+ api.getOrgHoldings.mockResolvedValue([]);+ render(<PrivacyOrgsTab subjectId="self" />);++ expect(await screen.findByText(/Could not load organizations/)).toBeInTheDocument();+ expect(screen.queryByText(/No organizations yet/)).not.toBeInTheDocument();++ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));+ expect(await screen.findByText('Acme Bank')).toBeInTheDocument();+ });+});