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(memory): keep federated edits newer than prior clocks (#12522)

[._.]/ Adam Eivyedf6b52e38cd
diff --git a/server/lib/db.ddlParity.test.js b/server/lib/db.ddlParity.test.js
index 446e48d154..7254822607 100644
--- a/server/lib/db.ddlParity.test.js
+++ b/server/lib/db.ddlParity.test.js
@@ -26,7 +26,7 @@
import { describe, it, expect } from 'vitest';
import { FTS_PAYLOAD_FIELDS } from './catalogTypes.js';
import { syncFeedDdl, syncFeedTables, buildSyncFeedTriggers } from './db/schema/syncFeed.js';
-import { memorySearchDdl } from './db/schema/core.js';
+import { memorySearchDdl, memoryTimestampDdl } from './db/schema/core.js';
import { catalogIdentityDdl } from './db/schema/catalogIdentity.js';
import { readFileSync, readdirSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
@@ -371,6 +371,13 @@ describe('DDL parity (init-db.sql ↔ db/schema ensureSchema)', () => {
for (const table of jsAuditTables) expect(shared, `audited table ${table} is not declared in both sources`).toContain(table);
});
+ it('memory clock upgrade matches the fresh-install trigger body', () => {
+ const flat = s => s.replace(/--[^\n]*(?:\n|$)/g, ' ').replace(/\s+/g, ' ').trim();
+ for (const statement of memoryTimestampDdl) {
+ expect(flat(INIT_SQL)).toContain(flat(statement));
+ }
+ });
+
// Commit-ordered sync feed (#8315). The constraint triggers carry
// DEFERRABLE/WHEN clauses the generic trigger extractor above does not
// parse, and syncFeed.js generates its statements from `syncFeedTables`, so
diff --git a/server/lib/db/schema/core.js b/server/lib/db/schema/core.js
index 4eb5e16875..26ee999062 100644
--- a/server/lib/db/schema/core.js
+++ b/server/lib/db/schema/core.js
@@ -89,6 +89,52 @@ BEGIN
END$$`,
];
+// Existing installs already have the trigger; replacing its function at boot
+// upgrades the memory clock without rebuilding tables or rewriting records.
+// Keep this body identical to the fresh-install definition in init-db.sql.
+export const memoryTimestampDdl = [
+ `CREATE OR REPLACE FUNCTION update_memory_timestamp()
+RETURNS TRIGGER AS $$
+DECLARE
+ content_changed BOOLEAN;
+BEGIN
+ content_changed := (
+ NEW.type IS DISTINCT FROM OLD.type OR
+ NEW.content IS DISTINCT FROM OLD.content OR
+ NEW.summary IS DISTINCT FROM OLD.summary OR
+ NEW.category IS DISTINCT FROM OLD.category OR
+ NEW.tags IS DISTINCT FROM OLD.tags OR
+ NEW.embedding IS DISTINCT FROM OLD.embedding OR
+ NEW.embedding_model IS DISTINCT FROM OLD.embedding_model OR
+ NEW.confidence IS DISTINCT FROM OLD.confidence OR
+ NEW.importance IS DISTINCT FROM OLD.importance OR
+ NEW.status IS DISTINCT FROM OLD.status OR
+ NEW.expires_at IS DISTINCT FROM OLD.expires_at OR
+ NEW.source_task_id IS DISTINCT FROM OLD.source_task_id OR
+ NEW.source_agent_id IS DISTINCT FROM OLD.source_agent_id OR
+ NEW.source_app_id IS DISTINCT FROM OLD.source_app_id OR
+ NEW.updated_at IS DISTINCT FROM OLD.updated_at
+ );
+
+ -- Access-stat-only update: skip sync_sequence and updated_at bump
+ IF NOT content_changed THEN
+ RETURN NEW;
+ END IF;
+
+ IF NEW.updated_at IS NULL OR NEW.updated_at = OLD.updated_at THEN
+ -- Wire clocks are milliseconds; advance even within one transaction or
+ -- after a future peer clock. NOW() would reuse transaction-start time.
+ NEW.updated_at := GREATEST(
+ date_trunc('milliseconds', clock_timestamp()),
+ date_trunc('milliseconds', OLD.updated_at) + INTERVAL '1 millisecond'
+ );
+ END IF;
+ NEW.sync_sequence := nextval(pg_get_serial_sequence('memories', 'sync_sequence'));
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql`,
+];
+
export const coreDdl = [
...memorySearchDdl,
// Deliberately no FK: the bridge repairs a purged target on its next sync.
@@ -121,6 +167,7 @@ export const coreDdl = [
`ALTER TABLE memories ADD COLUMN IF NOT EXISTS origin_instance_id VARCHAR(36)`,
`CREATE INDEX IF NOT EXISTS idx_memories_origin_instance ON memories (origin_instance_id)`,
`CREATE INDEX IF NOT EXISTS idx_memories_sync_sequence ON memories (sync_sequence)`,
+ ...memoryTimestampDdl,
// Versioned DB-migration tracker (#1029). Records which ordered migration
// files in server/scripts/db-migrations/ have been applied on THIS install.
// It's part of the base schema (created here AND in init-db.sql, parity-
diff --git a/server/scripts/init-db.sql b/server/scripts/init-db.sql
index e19c124271..9a0503710d 100644
--- a/server/scripts/init-db.sql
+++ b/server/scripts/init-db.sql
@@ -375,7 +375,12 @@ BEGIN
END IF;
IF NEW.updated_at IS NULL OR NEW.updated_at = OLD.updated_at THEN
- NEW.updated_at := NOW();
+ -- Wire clocks are milliseconds; advance even within one transaction or
+ -- after a future peer clock. NOW() would reuse transaction-start time.
+ NEW.updated_at := GREATEST(
+ date_trunc('milliseconds', clock_timestamp()),
+ date_trunc('milliseconds', OLD.updated_at) + INTERVAL '1 millisecond'
+ );
END IF;
NEW.sync_sequence := nextval(pg_get_serial_sequence('memories', 'sync_sequence'));
RETURN NEW;
diff --git a/server/services/memoryDB.js b/server/services/memoryDB.js
index 05af4b1da5..1a1676d4a2 100644
--- a/server/services/memoryDB.js
+++ b/server/services/memoryDB.js
@@ -401,7 +401,7 @@ async function updateMemoryWithClient(client, id, updates) {
.map(([key, column]) => { params.push(values[key]); return `${column} = $${params.length}`; });
// The row trigger saves OLD and increments version in this same transaction,
// including writes from sync. Operational-only writes do not create history.
- const updated = fields.length ? (await client.query(
+ let updated = fields.length ? (await client.query(
`UPDATE memories SET ${fields.join(', ')} WHERE id = $${params.length + 1} RETURNING *`,
[...params, id]
)).rows[0] : row;
@@ -413,7 +413,8 @@ async function updateMemoryWithClient(client, id, updates) {
await client.query(`INSERT INTO memory_links (source_id, target_id) VALUES ${slots} ON CONFLICT DO NOTHING`,
[id, ...updates.relatedMemories]);
}
- await client.query('UPDATE memories SET updated_at = NOW() WHERE id = $1', [id]);
+ // Ask the trigger for a local clock; explicit clocks belong to peer replay.
+ updated = (await client.query('UPDATE memories SET updated_at = NULL WHERE id = $1 RETURNING *', [id])).rows[0];
}
const links = await client.query('SELECT target_id FROM memory_links WHERE source_id = $1', [id]);
return { ...rowToMemory(updated), relatedMemories: links.rows.map(link => link.target_id) };
diff --git a/server/services/memorySync.db.test.js b/server/services/memorySync.db.test.js
index 6d04f95c85..56fb6b1812 100644
--- a/server/services/memorySync.db.test.js
+++ b/server/services/memorySync.db.test.js
@@ -26,7 +26,7 @@
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
-import { checkHealth, ensureSchema, close, query } from '../lib/db.js';
+import { checkHealth, ensureSchema, close, query, withTransaction } from '../lib/db.js';
import { requireDbOrSkip } from '../lib/dbTestGate.js';
let dbReady = false;
@@ -106,6 +106,65 @@ describe.skipIf(!runDb)('memorySync.applyRemoteChanges', () => {
await close();
});
+ // Replay two successive exported states into the same row to exercise the
+ // real receiver's strict clock comparison, including JavaScript wire precision.
+ const expectPeerAccepts = async (first, second) => {
+ await query('DELETE FROM memories WHERE id = $1', [ID_A]);
+ await memorySync.applyRemoteChanges([first]);
+ expect(await memorySync.applyRemoteChanges([second])).toEqual({ inserted: 0, updated: 1, skipped: 0 });
+ expect(await contentOf(ID_A)).toBe(second.content);
+ expect((await memorySync.getChangesSince()).memories[0].updatedAt).toBe(second.updatedAt);
+ };
+
+ it('exports overlapping transaction edits with clocks peers accept in commit order', async () => {
+ await memorySync.applyRemoteChanges([remoteMemory(ID_A, '2026-01-01T00:00:00.000Z')]);
+ let first;
+ await withTransaction(async client => {
+ // Pin the older transaction clock before a separate connection commits.
+ const { rows } = await client.query('SELECT NOW() AS started');
+ await query(`UPDATE memories SET content = 'first commit', updated_at = $2::timestamptz + INTERVAL '1 millisecond' WHERE id = $1`, [ID_A, rows[0].started]);
+ first = (await memorySync.getChangesSince()).memories[0];
+ await client.query(`UPDATE memories SET content = 'late commit' WHERE id = $1`, [ID_A]);
+ });
+ const page = await memorySync.getChangesSince(first.syncSequence);
+ expect(page.memories).toHaveLength(1);
+ await expectPeerAccepts(first, page.memories[0]);
+ });
+
+ it('advances successive edits in one transaction at millisecond wire precision', async () => {
+ await memorySync.applyRemoteChanges([remoteMemory(ID_A, '2026-01-01T00:00:00.000Z')]);
+ let first;
+ await withTransaction(async client => {
+ const { rows } = await client.query(`UPDATE memories SET content = 'first edit' WHERE id = $1 RETURNING updated_at`, [ID_A]);
+ first = remoteMemory(ID_A, rows[0].updated_at.toISOString(), { content: 'first edit' });
+ await client.query(`UPDATE memories SET content = 'second edit' WHERE id = $1`, [ID_A]);
+ });
+ await expectPeerAccepts(first, (await memorySync.getChangesSince()).memories[0]);
+ });
+
+ it('advances past a future peer clock while keeping access statistics out of the feed', async () => {
+ const future = '2099-01-01T00:00:00.123Z';
+ await memorySync.applyRemoteChanges([remoteMemory(ID_A, future)]);
+ const first = (await memorySync.getChangesSince()).memories[0];
+ expect(first.updatedAt).toBe(future);
+ await query('UPDATE memories SET access_count = access_count + 1, last_accessed = NOW() WHERE id = $1', [ID_A]);
+ expect((await memorySync.getChangesSince(first.syncSequence)).memories).toEqual([]);
+ expect((await memorySync.getChangesSince()).memories[0].updatedAt).toBe(future);
+ await query(`UPDATE memories SET content = 'local edit after future peer', updated_at = NULL WHERE id = $1`, [ID_A]);
+ const second = (await memorySync.getChangesSince(first.syncSequence)).memories[0];
+ expect(second.updatedAt).toBe('2099-01-01T00:00:00.124Z');
+ await expectPeerAccepts(first, second);
+ });
+
+ it('keeps local relation edits monotonic and returns their persisted clock', async () => {
+ await memorySync.applyRemoteChanges([remoteMemory(ID_A, '2099-01-01T00:00:00.000Z')]);
+ const { updateMemory } = await import('./memoryDB.js');
+ const updated = await updateMemory(ID_A, { content: 'edited with relations', relatedMemories: [] });
+ const exported = (await memorySync.getChangesSince()).memories[0];
+ expect(Date.parse(exported.updatedAt)).toBeGreaterThan(Date.parse('2099-01-01T00:00:00.000Z'));
+ expect(updated.updatedAt).toBe(exported.updatedAt);
+ });
+
it('keeps chosen identity local through outbound and incoming federation, including id collisions', async () => {
await memorySync.applyRemoteChanges([remoteMemory(ID_A, '2026-01-02T00:00:00.000Z')]);
await query(`UPDATE memories SET source_agent_id = $1, content = $2, tags = $3 WHERE id = $4`, ['cos-persistent-mind', 'Example Star', ['mind:chosen-name', 'mind:core-identity'], ID_A]);