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: an edited pre-existing drink or nicotine row no longer comes back from a peer's unedited copy (#8143)

[._.]/ Adam Eivy29b96296b4a5
diff --git a/server/services/dataSync.js b/server/services/dataSync.js
index 7e59938bc2..8e350b489b 100644
--- a/server/services/dataSync.js
+++ b/server/services/dataSync.js
@@ -165,12 +165,16 @@ const preferLogEventCopy = (a, b) => {
// one logged event: copies of it collapse to the preferred copy, and two
// identical rows with different ids stay two events. A row without an `id` was
// logged before events had identity, so it keeps the old content-keyed dedupe —
-// its identical twin on a peer is the same row synced earlier, not a new event.
+// its identical twin on a peer is the same row synced earlier, not a new event —
+// and is dropped once an edit has turned it into an identified event (`replaces`).
const mergeDailyLogEvents = (local, remote) => {
+ const records = [...(Array.isArray(local) ? local : []), ...(Array.isArray(remote) ? remote : [])];
const merged = [];
const indexById = new Map();
- const seenLegacy = new Set();
- for (const record of [...(Array.isArray(local) ? local : []), ...(Array.isArray(remote) ? remote : [])]) {
+ const seenLegacy = new Set(
+ records.filter((r) => logEventId(r) && isPlainObject(r.replaces)).map((r) => canonicalStringify(r.replaces)),
+ );
+ for (const record of records) {
const id = logEventId(record);
if (id) {
const index = indexById.get(id);
diff --git a/server/services/meatspaceAlcohol.js b/server/services/meatspaceAlcohol.js
index d528dc5d57..badd7fd0ee 100644
--- a/server/services/meatspaceAlcohol.js
+++ b/server/services/meatspaceAlcohol.js
@@ -260,12 +260,11 @@ export async function updateDrink(date, index, updates) {
const entry = log.entries.find(e => e.date === date);
if (!entry?.alcohol?.drinks?.[index]) return null;
- const drink = entry.alcohol.drinks[index];
+ const drink = stampDailyLogEventEdit(entry.alcohol.drinks[index]);
if (updates.name !== undefined) drink.name = updates.name;
if (updates.oz !== undefined) drink.oz = updates.oz;
if (updates.abv !== undefined) drink.abv = updates.abv;
if (updates.count !== undefined) drink.count = updates.count;
- stampDailyLogEventEdit(drink);
// Move to different date if requested
const newDate = updates.date;
diff --git a/server/services/meatspaceDailyLog.js b/server/services/meatspaceDailyLog.js
index 1b613d02fb..ecd7887c4c 100644
--- a/server/services/meatspaceDailyLog.js
+++ b/server/services/meatspaceDailyLog.js
@@ -120,11 +120,16 @@ export function newDailyLogEvent(fields, now = new Date().toISOString()) {
}
/**
- * Restamp an edited event so the edit wins over a peer's older copy. A legacy row
- * (no `id`) gets one here, so later edits to it merge by identity too.
+ * Restamp an event about to be edited so the edit wins over a peer's older copy.
+ * Call it BEFORE changing any field: a legacy row (no `id`) gets an id here and
+ * records its pre-edit content in `replaces`, so a peer that still holds the
+ * unedited legacy row does not merge it back in as a second event.
*/
export function stampDailyLogEventEdit(event, now = new Date().toISOString()) {
- if (typeof event.id !== 'string' || !event.id) event.id = randomUUID();
+ if (typeof event.id !== 'string' || !event.id) {
+ event.replaces = { ...event };
+ event.id = randomUUID();
+ }
event.updatedAt = now;
return event;
}
diff --git a/server/services/meatspaceNicotine.js b/server/services/meatspaceNicotine.js
index 8e66e6e96d..acb5ae68b0 100644
--- a/server/services/meatspaceNicotine.js
+++ b/server/services/meatspaceNicotine.js
@@ -206,11 +206,10 @@ export async function updateNicotine(date, index, updates) {
const entry = log.entries.find(e => e.date === date);
if (!entry?.nicotine?.items?.[index]) return null;
- const item = entry.nicotine.items[index];
+ const item = stampDailyLogEventEdit(entry.nicotine.items[index]);
if (updates.product !== undefined) item.product = updates.product;
if (updates.mgPerUnit !== undefined) item.mgPerUnit = updates.mgPerUnit;
if (updates.count !== undefined) item.count = updates.count;
- stampDailyLogEventEdit(item);
// Move to different date if requested
const newDate = updates.date;
diff --git a/server/services/sharingDurability.test.js b/server/services/sharingDurability.test.js
index e99189383e..bcf0ad69a9 100644
--- a/server/services/sharingDurability.test.js
+++ b/server/services/sharingDurability.test.js
@@ -239,6 +239,23 @@ describe('daily-log event identity (#8143)', () => {
}
});
+ it('does not merge a legacy row back in after one peer edits it', async () => {
+ const legacyLog = { entries: [day([{ name: 'Example Stout', oz: 12, abv: 5, count: 1 }])] };
+ await seed(path(), legacyLog);
+ await alcohol.updateDrink(date, 0, { count: 2 });
+ const edited = JSON.parse(await readFile(path(), 'utf8'));
+
+ // Pulling the unedited peer's copy leaves the edit as the only row...
+ await dataSync.applyRemote('meatspace', { 'daily-log.json': legacyLog });
+ expect((await savedDay()).alcohol.drinks).toEqual(edited.entries[0].alcohol.drinks);
+ // ...and the unedited peer drops its legacy copy when it pulls the edit.
+ await seed(path(), legacyLog);
+ await dataSync.applyRemote('meatspace', { 'daily-log.json': edited });
+ const merged = await savedDay();
+ expect(merged.alcohol.drinks).toEqual(edited.entries[0].alcohol.drinks);
+ expect(merged.alcohol.standardDrinks).toBe(2);
+ });
+
it('refuses a daily log from a peer on a newer meatspace schema', async () => {
await seed(path(), { entries: [day([lager('drink-a')])] });
const before = await readFile(path(), 'utf8');