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
address review (codex): FOR UPDATE row-lock to serialize concurrent holdings replace
Adam Eivyc58c6099ae18
diff --git a/server/services/privacyOrgs.js b/server/services/privacyOrgs.jsindex 9fb8176d39..e135abd000 100644--- a/server/services/privacyOrgs.js+++ b/server/services/privacyOrgs.js@@ -164,10 +164,17 @@ export async function getOrgsHoldingRecord(vaultRecordId) {* a bad `vaultRecordId` partway through the upsert loop (foreign key* violation, see below) would leave the DELETE committed but only some of* the new rows written: neither the old nor the requested holdings set.+ * `FOR UPDATE` row-locks the org for the duration of the transaction (same+ * pattern as `updateVaultRecord` in privacyVault.js) — without it, two+ * concurrent replace calls for the same org (e.g. a double-click, or two+ * browser tabs saving holdings at once) can both pass the existence check,+ * each delete against the pre-existing set, and each upsert their own rows —+ * leaving the UNION of both requests rather than the last full replacement,+ * silently defeating the documented replace-set semantics.*/export async function setOrgHoldings(orgId, holdings) {await withTransaction(async (client) => {- const { rows: orgRows } = await client.query(`SELECT id FROM privacy_orgs WHERE id = $1`, [orgId]);+ const { rows: orgRows } = await client.query(`SELECT id FROM privacy_orgs WHERE id = $1 FOR UPDATE`, [orgId]);if (!orgRows[0]) throw new ServerError('Organization not found', { status: 404, code: 'NOT_FOUND' });const ids = holdings.map((h) => h.vaultRecordId);diff --git a/server/services/privacyOrgs.test.js b/server/services/privacyOrgs.test.jsindex 665aaa7b4c..72e8696c83 100644--- a/server/services/privacyOrgs.test.js+++ b/server/services/privacyOrgs.test.js@@ -151,6 +151,18 @@ describe('setOrgHoldings', () => {expect(queryMock).not.toHaveBeenCalled(); // getHoldingsForOrg never reached});+ it('row-locks the org with FOR UPDATE to serialize concurrent replace calls', async () => {+ const client = mockTransaction(async (sql) => {+ if (/FROM privacy_orgs WHERE id/.test(sql)) return { rows: [orgRow()] };+ if (/DELETE FROM privacy_org_holdings/.test(sql)) return { rows: [] };+ return { rows: [] };+ });+ queryMock.mockResolvedValue({ rows: [] });+ await setOrgHoldings('o1', []);+ const lockCall = client.query.mock.calls.find(([sql]) => /FROM privacy_orgs WHERE id/.test(sql));+ expect(lockCall[0]).toMatch(/FOR UPDATE/);+ });+it('deletes the complement and upserts the given set, all inside one transaction', async () => {const client = mockTransaction(async (sql) => {if (/FROM privacy_orgs WHERE id/.test(sql)) return { rows: [orgRow()] };