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: scope Google Calendar prune to the synced date range (#10104)

Adam Eivyfb41beab34cc
diff --git a/server/services/calendarGoogleApiSync.js b/server/services/calendarGoogleApiSync.js
index 2e44261159..e1d4b7600b 100644
--- a/server/services/calendarGoogleApiSync.js
+++ b/server/services/calendarGoogleApiSync.js
@@ -89,7 +89,7 @@ async function runApiSyncAccount(accountId, io) {
}));
for (const { sc, allEvents } of fetchResults) {
- const syncResult = await pushSyncEvents(accountId, sc.calendarId, sc.name, allEvents, null);
+ const syncResult = await pushSyncEvents(accountId, sc.calendarId, sc.name, allEvents, null, { dateRange: { pastDate, futureDate } });
totalNew += syncResult.newEvents;
totalUpdated += syncResult.updated;
totalPruned += syncResult.pruned;
diff --git a/server/services/calendarGoogleSync.js b/server/services/calendarGoogleSync.js
index d392a0913c..80c79befa1 100644
--- a/server/services/calendarGoogleSync.js
+++ b/server/services/calendarGoogleSync.js
@@ -86,9 +86,14 @@ export function getSyncDateRange(pastDays = 7, futureDays = 30) {
* (a CLI that exited non-zero mid-stream) must pass `prune: false`, or the
* missing tail reads as "these events were deleted upstream" and destroys real
* calendar data. `options.status` labels the resulting sync for the UI.
+ *
+ * `options.dateRange` ({ pastDate, futureDate }) declares that `rawEvents` only
+ * covers that window: pruning is then limited to cached events overlapping it,
+ * so history older than the window and events beyond it are never mistaken for
+ * upstream deletions.
*/
export async function pushSyncEvents(accountId, calendarId, calendarName, rawEvents, io, options = {}) {
- const { prune: shouldPrune = true, status = 'success' } = options;
+ const { prune: shouldPrune = true, status = 'success', dateRange = null } = options;
const account = await getAccount(accountId);
if (!account) throw new ServerError('Account not found', { status: 404 });
@@ -155,8 +160,17 @@ export async function pushSyncEvents(accountId, calendarId, calendarName, rawEve
let pruned = 0;
if (shouldPrune) {
const before = cache.events.length;
+ const winStart = dateRange ? dateRange.pastDate.getTime() : null;
+ const winEnd = dateRange ? dateRange.futureDate.getTime() : null;
+ const inSyncedWindow = (e) => {
+ if (!dateRange) return true;
+ const start = Date.parse(e.startTime || e.endTime);
+ const end = Date.parse(e.endTime || e.startTime);
+ // Unparseable/missing times can't be proven outside the window.
+ return (Number.isNaN(start) || start <= winEnd) && (Number.isNaN(end) || end >= winStart);
+ };
cache.events = cache.events.filter(e =>
- e.subcalendarId !== calendarId || incomingIds.has(e.externalId)
+ e.subcalendarId !== calendarId || incomingIds.has(e.externalId) || !inSyncedWindow(e)
);
pruned = before - cache.events.length;
}
@@ -285,7 +299,7 @@ Include the full events arrays as returned by gcal_list_events, with every field
cal.calendarName || cal.calendarId,
partial ? cal.events : cal.events.map(withExplicitConferenceFields),
null,
- { prune: !partial, status },
+ { prune: !partial, status, dateRange: { pastDate, futureDate } },
);
totalNew += syncResult.newEvents;
totalUpdated += syncResult.updated;
diff --git a/server/services/calendarGoogleSync.test.js b/server/services/calendarGoogleSync.test.js
index 5603ff2921..f1f1cd3b94 100644
--- a/server/services/calendarGoogleSync.test.js
+++ b/server/services/calendarGoogleSync.test.js
@@ -546,4 +546,17 @@ describe('Google participant snapshot reconciliation (#8838)', () => {
expect(savedCache().events[0]).toMatchObject({ organizer: null, attendees: [] });
expect(savedCache().events[0].myStatus).toBeUndefined();
});
+ it('with a dateRange, prunes only in-window deletions and retains events outside the window', async () => {
+ const day = 86400000;
+ const now = Date.now();
+ const iso = (offset) => new Date(now + offset * day).toISOString();
+ const mk = (id, off) => ({ id, externalId: id, title: id, subcalendarId: CAL_ID, startTime: iso(off), endTime: iso(off) });
+ await pushSyncEvents(ACCOUNT_ID, CAL_ID, 'Work', [rawEvent('kept-in-window', 'Kept')]);
+ const kept = { ...savedCache().events[0], id: 'kept-in-window', startTime: iso(5), endTime: iso(5) };
+ loadCache.mockResolvedValue({ events: [mk('old', -60), mk('far', 90), mk('deleted-in-window', 3), kept] });
+ const dateRange = { pastDate: new Date(now - 7 * day), futureDate: new Date(now + 30 * day) };
+ const result = await pushSyncEvents(ACCOUNT_ID, CAL_ID, 'Work', [rawEvent('kept-in-window', 'Kept')], null, { dateRange });
+ expect(result.pruned).toBe(1);
+ expect(savedCache().events.map(e => e.id).sort()).toEqual(['far', 'kept-in-window', 'old']);
+ });
});