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: preserve concurrent Stacker News configuration edits
Adam Eivy35cefc047ca5
diff --git a/scripts/lib/dbTestFiles.js b/scripts/lib/dbTestFiles.jsindex 4ff2b95cca..04693b4617 100644--- a/scripts/lib/dbTestFiles.js+++ b/scripts/lib/dbTestFiles.js@@ -9,6 +9,7 @@ import { posix } from 'node:path';* listed explicitly below (the drift guard fails the build if you forget).*/export const DB_TEST_INCLUDE = [+ 'services/stackerNews.db.test.js','services/peerExecutionLedger.db.test.js','services/voice/profiles.db.test.js','services/voice/studio.db.test.js',diff --git a/server/services/stackerNews.db.test.js b/server/services/stackerNews.db.test.jsnew file mode 100644index 0000000000..0989e39d72--- /dev/null+++ b/server/services/stackerNews.db.test.js@@ -0,0 +1,124 @@+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';+import { randomUUID } from 'node:crypto';+import { checkHealth, close, ensureSchema, query } from '../lib/db.js';+import { requireDbOrSkip } from '../lib/dbTestGate.js';+import { executeStackerNewsBrowserRead } from '../integrations/stackerNews/index.js';++// Stop both UPDATEs just before PostgreSQL executes them. On the old path this+// guarantees both callers already read the same stale configuration snapshot.+// The actual SQL, row locks, constraints and transactions remain real.+const gate = vi.hoisted(() => ({ table: null, waiting: [], release: null }));+vi.mock('../lib/db.js', async (importOriginal) => {+ const actual = await importOriginal();+ const execute = async (run, sql, params) => {+ if (gate.table && sql.startsWith(`UPDATE ${gate.table} SET`)) {+ await new Promise((resolve) => {+ gate.waiting.push(resolve);+ if (gate.waiting.length === 2) gate.release();+ });+ }+ return run(sql, params);+ };+ return {+ ...actual,+ query: (sql, params) => execute(actual.query, sql, params),+ withTransaction: (work) => actual.withTransaction((client) => work({+ query: (sql, params) => execute(client.query.bind(client), sql, params),+ })),+ };+});+vi.mock('../integrations/stackerNews/index.js', () => ({+ executeStackerNewsOperation: vi.fn(), executeStackerNewsBrowserRead: vi.fn(), stackerNewsCapabilities: {},+}));++const { createAccount, createTerritory, getAccount, listTerritories, syncAccount, updateAccount, updateTerritory } = await import('./stackerNews.js');+const health = await checkHealth().catch(() => ({ connected: false }));+const ready = requireDbOrSkip('stackerNews.db.test', health.connected, 'test database unavailable');+let accountId;+let territoryId;++async function overlap(table, work) {+ gate.table = table;+ gate.waiting = [];+ gate.release = () => {+ gate.table = null;+ gate.waiting.forEach((resolve) => resolve());+ };+ try {+ await work();+ } finally {+ gate.release();+ }+}++describe.skipIf(!ready)('Stacker News configuration integrity', () => {+ beforeAll(async () => {+ await ensureSchema();+ const account = await createAccount({ label: 'Example account', username: `example_${randomUUID()}` });+ accountId = account.id;+ territoryId = (await createTerritory({ accountId, slug: 'example-art' })).id;+ });++ afterAll(async () => {+ if (accountId) await query('DELETE FROM stacker_news_accounts WHERE id=$1', [accountId]);+ });++ it('preserves disjoint concurrent account PATCHes, including false and empty values', async () => {+ await updateAccount(accountId, { enabled: true, textModel: 'example-model' });+ await overlap('stacker_news_accounts', () => Promise.all([+ updateAccount(accountId, { label: 'Updated account', textModel: '' }),+ updateAccount(accountId, { enabled: false, syncItemLimit: 17 }),+ ]));+ expect(await getAccount(accountId)).toMatchObject({+ label: 'Updated account', textModel: '', enabled: false, syncItemLimit: 17,+ });+ });++ it('preserves concurrent territory edits and distinguishes omitted monitoring from null', async () => {+ await updateTerritory(territoryId, { monitoringEnabled: true, isOwned: true });+ await overlap('stacker_news_territories', () => Promise.all([+ updateTerritory(territoryId, { label: 'Updated territory', isOwned: false }),+ updateTerritory(territoryId, { monitoringEnabled: null, inheritAccountRules: false }),+ ]));+ await updateTerritory(territoryId, { rules: {} });+ expect((await listTerritories(accountId))[0]).toMatchObject({+ label: 'Updated territory', isOwned: false, monitoringEnabled: null, inheritAccountRules: false,+ });+ });++ it('preserves refreshed remote evidence on edits and clears it only on an actual rename', async () => {+ const evidence = { ownershipVerified: true, name: 'example-art' };+ await query('UPDATE stacker_news_territories SET remote_settings=$2,remote_refreshed_at=NOW() WHERE id=$1', [territoryId, evidence]);+ const sameSlug = await updateTerritory(territoryId, { slug: 'example-art', label: '' });+ expect(sameSlug.remoteSettings).toEqual(evidence);+ expect(sameSlug.remoteRefreshedAt).not.toBeNull();+ const renamed = await updateTerritory(territoryId, { slug: 'example-new-art' });+ expect(renamed.remoteSettings).toEqual({});+ expect(renamed.remoteRefreshedAt).toBeNull();+ });++ it('returns null for deleted targets without attempting a credential write', async () => {+ const missingId = randomUUID();+ expect(await updateAccount(missingId, { apiKey: 'example-key' })).toBeNull();+ expect(await updateTerritory(missingId, { slug: 'example-missing' })).toBeNull();+ });++ it('discards provider data fetched for a territory renamed during sync', async () => {+ const account = await getAccount(accountId);+ const previous = (await listTerritories(accountId))[0];+ executeStackerNewsBrowserRead.mockImplementation(async (name) => {+ if (name === 'me') return { me: { id: 'example-owner', name: account.username } };+ if (name === 'sub') {+ await updateTerritory(territoryId, { slug: 'example-renamed-during-sync' });+ return { sub: { name: previous.slug, userId: 'example-owner' } };+ }+ throw new Error('Old-slug items must not be read after a rename');+ });+ expect(await syncAccount(accountId, { force: true })).toMatchObject({ ingested: 0 });+ expect((await listTerritories(accountId))[0]).toMatchObject({+ slug: 'example-renamed-during-sync', remoteSettings: {}, remoteRefreshedAt: null,+ });+ });+});++afterAll(async () => { await close(); });diff --git a/server/services/stackerNews.js b/server/services/stackerNews.jsindex 1327b20e0d..8ea43c9047 100644--- a/server/services/stackerNews.js+++ b/server/services/stackerNews.js@@ -243,23 +243,28 @@ export async function createAccount({}export async function updateAccount(id, updates) {- const existing = await getAccountRow(id);- if (!existing) return null;- await withTransaction(async (client) => {- await client.query(- `UPDATE stacker_news_accounts SET label=$2,username=$3,enabled=$4,monitoring_enabled=$5,- monitoring_interval_minutes=$6,sync_item_limit=$7,analysis_enabled=$8,text_model=$9,vision_model=$10,rules=$11,policy_version=$12,read_transport=$13,updated_at=NOW()- WHERE id=$1`,- [id, updates.label ?? existing.label, (updates.username ?? existing.username).toLowerCase(), updates.enabled ?? existing.enabled,- updates.monitoringEnabled ?? existing.monitoring_enabled, updates.monitoringIntervalMinutes ?? existing.monitoring_interval_minutes,- normalizeSyncItemLimit(updates.syncItemLimit ?? existing.sync_item_limit), updates.analysisEnabled ?? existing.analysis_enabled,- updates.textModel ?? existing.text_model, updates.visionModel ?? existing.vision_model,- updates.rules === undefined ? existing.rules : normalizeStackerNewsRules(updates.rules), POLICY_VERSION,- normalizeReadTransport(updates.readTransport ?? existing.read_transport)],+ // Merge omitted fields in SQL against the locked current row, not a snapshot+ // read before this transaction. Concurrent PATCHes must keep each other's edits.+ const updated = await withTransaction(async (client) => {+ const result = await client.query(+ `UPDATE stacker_news_accounts SET label=COALESCE($2,label),username=COALESCE($3,username),+ enabled=COALESCE($4,enabled),monitoring_enabled=COALESCE($5,monitoring_enabled),+ monitoring_interval_minutes=COALESCE($6,monitoring_interval_minutes),sync_item_limit=COALESCE($7,sync_item_limit),+ analysis_enabled=COALESCE($8,analysis_enabled),text_model=COALESCE($9,text_model),vision_model=COALESCE($10,vision_model),+ rules=COALESCE($11,rules),policy_version=$12,read_transport=COALESCE($13,read_transport),updated_at=NOW()+ WHERE id=$1 RETURNING id`,+ [id, updates.label ?? null, updates.username?.toLowerCase() ?? null, updates.enabled ?? null,+ updates.monitoringEnabled ?? null, updates.monitoringIntervalMinutes ?? null,+ updates.syncItemLimit == null ? null : normalizeSyncItemLimit(updates.syncItemLimit), updates.analysisEnabled ?? null,+ updates.textModel ?? null, updates.visionModel ?? null,+ updates.rules === undefined ? null : normalizeStackerNewsRules(updates.rules), POLICY_VERSION,+ updates.readTransport == null ? null : normalizeReadTransport(updates.readTransport)],);+ if (!result.rows.length) return false;if (updates.apiKey !== undefined) await saveCredential(client, id, updates.apiKey);+ return true;});- return getAccount(id);+ return updated ? getAccount(id) : null;}export async function deleteAccount(id) {@@ -282,22 +287,21 @@ export async function createTerritory({ accountId, slug, label = '', isOwned = f}export async function updateTerritory(id, updates) {- const previous = await query('SELECT * FROM stacker_news_territories WHERE id=$1', [id]);- const existing = previous.rows[0];- if (!existing) return null;- const nextSlug = updates.slug ?? existing.slug;- const slugChanged = nextSlug !== existing.slug;+ // Remote evidence belongs to the current slug. Preserve a sync's latest+ // evidence on ordinary edits; invalidate it atomically when renaming.const result = await query(- `UPDATE stacker_news_territories SET slug=$2,label=$3,is_owned=$4,monitoring_enabled=$5,- inherit_account_rules=$6,rules=$7,remote_settings=$8,remote_refreshed_at=$9,updated_at=NOW() WHERE id=$1 RETURNING *`,- [id, nextSlug, updates.label ?? existing.label, updates.isOwned ?? existing.is_owned,- updates.monitoringEnabled === undefined ? existing.monitoring_enabled : updates.monitoringEnabled,- updates.inheritAccountRules ?? existing.inherit_account_rules,- updates.rules === undefined ? existing.rules : normalizeStackerNewsRuleOverrides(updates.rules),- slugChanged ? {} : existing.remote_settings,- slugChanged ? null : existing.remote_refreshed_at],+ `UPDATE stacker_news_territories SET slug=COALESCE($2,slug),label=COALESCE($3,label),is_owned=COALESCE($4,is_owned),+ monitoring_enabled=CASE WHEN $8 THEN $5 ELSE monitoring_enabled END,+ inherit_account_rules=COALESCE($6,inherit_account_rules),rules=COALESCE($7,rules),+ remote_settings=CASE WHEN $2 IS NOT NULL AND slug IS DISTINCT FROM $2 THEN '{}'::jsonb ELSE remote_settings END,+ remote_refreshed_at=CASE WHEN $2 IS NOT NULL AND slug IS DISTINCT FROM $2 THEN NULL ELSE remote_refreshed_at END,+ updated_at=NOW() WHERE id=$1 RETURNING *`,+ [id, updates.slug ?? null, updates.label ?? null, updates.isOwned ?? null,+ updates.monitoringEnabled ?? null, updates.inheritAccountRules ?? null,+ updates.rules === undefined ? null : normalizeStackerNewsRuleOverrides(updates.rules),+ updates.monitoringEnabled !== undefined],);- return territoryView(result.rows[0]);+ return result.rows[0] ? territoryView(result.rows[0]) : null;}export async function deleteTerritory(id) {@@ -588,10 +592,13 @@ async function syncAccountUnlocked(accountId, { force = false } = {}) {// profile-link fallback legitimately has no user ID. Ownership evidence// gates territory-settings execution, so it fails closed.const ownershipVerified = Boolean(remote && remote.userId != null && me.id != null && String(remote.userId) === String(me.id));- await query(- 'UPDATE stacker_news_territories SET remote_settings=$2,remote_refreshed_at=NOW(),updated_at=NOW() WHERE id=$1',- [territory.id, { ...(remote || {}), ownershipVerified }],+ const refreshed = await query(+ 'UPDATE stacker_news_territories SET remote_settings=$2,remote_refreshed_at=NOW(),updated_at=NOW() WHERE id=$1 AND slug=$3',+ [territory.id, { ...(remote || {}), ownershipVerified }, territory.slug],);+ // A rename/delete while the provider was responding invalidates this read.+ // Discard this stale evidence and skip its item fetch.+ if (refreshed.rowCount === 0) continue;let cursor = null;const seenCursors = new Set();const syncItemLimit = normalizeSyncItemLimit(account.sync_item_limit);diff --git a/server/services/stackerNewsTerritories.test.js b/server/services/stackerNewsTerritories.test.jsdeleted file mode 100644index f78705d0d1..0000000000--- a/server/services/stackerNewsTerritories.test.js+++ /dev/null@@ -1,37 +0,0 @@-import { describe, expect, it, vi } from 'vitest';--const existing = {- id: 'territory',- account_id: 'account',- slug: 'old-art',- label: 'Art',- is_owned: true,- monitoring_enabled: true,- inherit_account_rules: true,- rules: {},- remote_settings: { ownershipVerified: true, name: 'old-art' },- remote_refreshed_at: new Date(),-};-const query = vi.fn(async (sql, params) => {- if (sql.startsWith('SELECT * FROM stacker_news_territories')) return { rows: [existing] };- if (sql.startsWith('UPDATE stacker_news_territories')) return { rows: [{- ...existing,- slug: params[1],- remote_settings: params[7],- remote_refreshed_at: params[8],- }] };- return { rows: [] };-});-vi.mock('../lib/db.js', () => ({ query, withTransaction: vi.fn() }));-vi.mock('../lib/vaultCrypto.js', () => ({ decryptValue: vi.fn(), encryptValue: vi.fn(), ensureVaultKey: vi.fn() }));-vi.mock('../integrations/stackerNews/index.js', () => ({ executeStackerNewsOperation: vi.fn(), stackerNewsCapabilities: {} }));--const { updateTerritory } = await import('./stackerNews.js');--describe('Stacker News territory ownership evidence', () => {- it('clears remote evidence when the configured slug changes', async () => {- const territory = await updateTerritory(existing.id, { slug: 'new-art' });- expect(territory.remoteSettings).toEqual({});- expect(territory.remoteRefreshedAt).toBeNull();- });-});