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([issue-11677]): recover partial calendar schedules (#11723)
[._.]/0cefeea30046
diff --git a/server/services/goalCalendarScheduler.js b/server/services/goalCalendarScheduler.jsindex 035535cbce..f25c2f11ac 100644--- a/server/services/goalCalendarScheduler.js+++ b/server/services/goalCalendarScheduler.js@@ -24,6 +24,32 @@ function resolveTimeSlotMinute(timeSlot) {return parseInt(timeSlot.split(':')[1], 10) || 0;}+const eventKey = evt => `${evt.calendarId || 'primary'}:${evt.googleEventId}`;++async function deleteCalendarEvents(cal, events) {+ const results = await Promise.allSettled(events.map(async evt =>+ cal.events.delete({ calendarId: evt.calendarId || 'primary', eventId: evt.googleEventId })+ ));+ return events.filter((evt, index) => results[index].status === 'rejected'+ && results[index].reason?.code !== 404 && results[index].reason?.code !== 410);+}++async function retainScheduledEvents(goalId, events) {+ let found = false;+ await mutateGoals(freshGoals => {+ const freshGoal = freshGoals.goals.find(g => g.id === goalId);+ if (!freshGoal) return freshGoals;+ found = true;+ const existing = freshGoal.scheduledEvents || [];+ const existingIds = new Set(existing.map(eventKey));+ freshGoal.scheduledEvents = [...existing, ...events.filter(evt => !existingIds.has(eventKey(evt)))];+ freshGoal.updatedAt = new Date().toISOString();+ freshGoals.updatedAt = new Date().toISOString();+ return freshGoals;+ });+ return found;+}+export async function scheduleTimeBlocks(goalId) {const tokens = await getTokens();if (needsScopeUpgrade(tokens)) {@@ -89,53 +115,57 @@ export async function scheduleTimeBlocks(goalId) {}}- // Create events in parallel batches of 10 to avoid rate limiting+ // Settle the whole batch so a rejection cannot hide successful sibling inserts.const BATCH_SIZE = 10;const scheduledEvents = [];+ let failure;for (let i = 0; i < eventDefs.length; i += BATCH_SIZE) {const batch = eventDefs.slice(i, i + BATCH_SIZE);- const results = await Promise.all(batch.map(def =>+ const results = await Promise.allSettled(batch.map(async def =>cal.events.insert({ calendarId, requestBody: def.requestBody })));for (let j = 0; j < results.length; j++) {+ if (results[j].status === 'rejected') continue;scheduledEvents.push({id: `sched-${uuidv4()}`,- googleEventId: results[j].data.id,+ googleEventId: results[j].value.data.id,calendarId,milestoneId: batch[j].milestoneId,date: batch[j].date,createdAt: new Date().toISOString()});}+ const rejected = results.find(result => result.status === 'rejected');+ if (rejected) {+ failure = new ServerError(`Calendar event insertion failed: ${rejected.reason?.message || 'request rejected'}`, { code: 'CALENDAR_INSERT_FAILED' });+ break;+ }}- // Apply to a freshly re-read goal by id rather than the snapshot captured- // before the calendar batch above — see the note on the initial read.- let goalDeleted = false;- await mutateGoals(freshGoals => {- const freshGoal = freshGoals.goals.find(g => g.id === goalId);- if (!freshGoal) {- goalDeleted = true;- return freshGoals;- }- freshGoal.scheduledEvents = scheduledEvents;- freshGoal.updatedAt = new Date().toISOString();- freshGoals.updatedAt = new Date().toISOString();- return freshGoals;- });+ if (!failure) {+ // Re-read under the store lock, preserving concurrent edits and older events.+ await retainScheduledEvents(goalId, scheduledEvents).then(found => {+ if (!found) failure = new ServerError('Goal was deleted during scheduling', { status: 404, code: 'NOT_FOUND' });+ }, error => {+ failure = new ServerError(`Could not save scheduled calendar events: ${error.message}`, { code: 'CALENDAR_SAVE_FAILED' });+ });+ }- if (goalDeleted) {- // The deleted goal cannot retain event IDs for later removal. Compensate- // every event created by this batch before reporting the lost target.- const cleanup = await Promise.allSettled(scheduledEvents.map(evt =>- cal.events.delete({ calendarId: evt.calendarId, eventId: evt.googleEventId })- ));- const failed = cleanup.filter(result => result.status === 'rejected'- && result.reason?.code !== 404 && result.reason?.code !== 410);- if (failed.length) {- throw new ServerError(`Goal was deleted during scheduling; ${failed.length} calendar events could not be removed. Remove the remaining time blocks in Google Calendar.`, { code: 'CALENDAR_CLEANUP_FAILED' });+ if (failure) {+ const survivors = await deleteCalendarEvents(cal, scheduledEvents);+ if (!survivors.length) {+ throw new ServerError(`${failure.message}. All ${scheduledEvents.length} created calendar events were removed.`, { status: failure.status, code: failure.code });}- throw new ServerError('Goal was deleted during scheduling. Created calendar events were removed.', { status: 404, code: 'NOT_FOUND' });+ let retained = false;+ let persistenceError;+ await retainScheduledEvents(goalId, survivors).then(found => { retained = found; }, error => { persistenceError = error.message; });+ const recovery = retained+ ? 'Remaining time blocks were saved to the goal for later removal.'+ : 'Remaining time blocks could not be saved. Remove them in Google Calendar using the event IDs in the error details.';+ throw new ServerError(`${failure.message}; ${survivors.length} calendar events could not be removed. ${recovery}`, {+ code: 'CALENDAR_CLEANUP_FAILED',+ context: { remainingEvents: survivors, ...(persistenceError && { persistenceError }) }+ });}console.log(`📅 Scheduled ${scheduledEvents.length} time blocks for goal "${goal.title}"`);@@ -155,25 +185,39 @@ export async function removeScheduledEvents(goalId) {const cal = calendar({ version: 'v3', auth, timeout: GOOGLE_API_TIMEOUT_MS });const events = goal.scheduledEvents || [];- // Delete events in parallel- await Promise.all(events.map(evt =>- cal.events.delete({- calendarId: evt.calendarId || 'primary',- eventId: evt.googleEventId- }).catch(err => {- if (err.code !== 410 && err.code !== 404) console.log(`📅 Failed to delete event ${evt.googleEventId}: ${err.message}`);- })- ));-+ const survivors = await deleteCalendarEvents(cal, events);+ const removedIds = new Set(events.filter(evt => !survivors.includes(evt)).map(eventKey));+ let goalFound = false;+ let persistenceError;await mutateGoals(freshGoals => {const freshGoal = freshGoals.goals.find(g => g.id === goalId);if (freshGoal) {- freshGoal.scheduledEvents = [];+ goalFound = true;+ freshGoal.scheduledEvents = (freshGoal.scheduledEvents || []).filter(evt => !removedIds.has(eventKey(evt)));freshGoal.updatedAt = new Date().toISOString();}freshGoals.updatedAt = new Date().toISOString();return freshGoals;- });+ }).catch(error => { persistenceError = error; });+ if (survivors.length || persistenceError) {+ const recovery = persistenceError+ ? `Could not save the cleanup result: ${persistenceError.message}.`+ : goalFound+ ? 'Remaining time blocks were retained for later removal.'+ : 'The goal was deleted during cleanup; remaining time blocks could not be retained.';+ const manualCleanup = survivors.length && (!goalFound || persistenceError)+ ? ' Remove the remaining time blocks in Google Calendar using the event IDs in the error details.'+ : '';+ const outcome = survivors.length+ ? `${survivors.length} calendar events could not be removed.`+ : `All ${events.length} calendar events were removed.`;+ const error = new ServerError(`${outcome} ${recovery}${manualCleanup}`, {+ code: survivors.length ? 'CALENDAR_CLEANUP_FAILED' : 'CALENDAR_SAVE_FAILED',+ context: { remainingEvents: survivors, ...(persistenceError && { persistenceError: persistenceError.message }) }+ });+ if (persistenceError) error.cause = persistenceError;+ throw error;+ }console.log(`📅 Removed ${events.length} scheduled events for goal "${goal.title}"`);return { deleted: events.length };diff --git a/server/services/goalCalendarScheduler.test.js b/server/services/goalCalendarScheduler.test.jsindex 230eeb4f58..d0e89f0827 100644--- a/server/services/goalCalendarScheduler.test.js+++ b/server/services/goalCalendarScheduler.test.js@@ -30,12 +30,16 @@ vi.mock('./mortalLoomStore.js', () => ({const insertedEvents = [];let duringInsert;+let insertAttempts;+let rejectInsert;const deleteEvent = vi.fn(async () => ({}));vi.mock('@googleapis/calendar', () => ({calendar: () => ({events: {insert: vi.fn(async ({ requestBody }) => {- const id = `evt-${insertedEvents.length + 1}`;+ const attempt = ++insertAttempts;+ if (attempt === rejectInsert) throw new Error('Calendar quota exceeded');+ const id = `evt-${attempt}`;insertedEvents.push({ id, requestBody });await duringInsert?.();return { data: { id } };@@ -52,7 +56,8 @@ vi.mock('./googleAuth.js', () => ({getTokens: vi.fn(async () => ({ access_token: 'fake' })),}));-import { scheduleTimeBlocks, removeScheduledEvents } from './goalCalendarScheduler.js';+import * as goalStore from './identity/store.js';+import { scheduleTimeBlocks, removeScheduledEvents, rescheduleTimeBlocks } from './goalCalendarScheduler.js';import { addProgressEntry, deleteGoal } from './identity/goals.js';afterAll(() => { if (tempRoot) rmSync(tempRoot, { recursive: true, force: true }); });@@ -83,6 +88,8 @@ describe('goalCalendarScheduler write serialization (#8755)', () => {vi.spyOn(console, 'log').mockImplementation(() => {});insertedEvents.length = 0;duringInsert = undefined;+ insertAttempts = 0;+ rejectInsert = undefined;deleteEvent.mockReset().mockResolvedValue({});vi.useFakeTimers({ toFake: ['Date'] });vi.setSystemTime(new Date('2026-12-20T12:00:00Z'));@@ -135,6 +142,166 @@ describe('goalCalendarScheduler write serialization (#8755)', () => {expect(JSON.parse(readFileSync(goalsFile(), 'utf8')).goals).toEqual([]);});+ const dailyGoal = () => {+ const goal = baseGoal();+ goal.timeBlockConfig.preferredDays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];+ goal.timeBlockConfig.subcalendarId = 'example-calendar';+ return goal;+ };++ it('compensates all 11 successful inserts when the 12th fails, then retries without duplicate events', async () => {+ writeFileSync(goalsFile(), JSON.stringify({ goals: [dailyGoal()] }));+ rejectInsert = 12;+ await expect(scheduleTimeBlocks('g1')).rejects.toMatchObject({+ code: 'CALENDAR_INSERT_FAILED',+ message: expect.stringContaining('All 11 created calendar events were removed')+ });+ expect(insertedEvents).toHaveLength(11);+ expect(deleteEvent.mock.calls.map(([evt]) => evt.eventId)).toEqual(insertedEvents.map(evt => evt.id));+ expect(JSON.parse(readFileSync(goalsFile(), 'utf8')).goals[0].scheduledEvents).toEqual([]);++ rejectInsert = undefined;+ const result = await scheduleTimeBlocks('g1');+ expect(result.count).toBe(12);+ const retainedIds = JSON.parse(readFileSync(goalsFile(), 'utf8')).goals[0].scheduledEvents.map(evt => evt.googleEventId);+ expect(retainedIds).toEqual(result.events.map(evt => evt.googleEventId));+ expect(retainedIds.every(id => !deleteEvent.mock.calls.some(([evt]) => evt.eventId === id))).toBe(true);+ });++ it('stops after the failed batch and preserves survivors, existing events and concurrent edits for reschedule cleanup', async () => {+ const goal = dailyGoal();+ goal.targetDate = goal.milestones[0].targetDate = '2027-01-20';+ const existing = { id: 'existing', googleEventId: 'old-event', calendarId: 'primary' };+ goal.scheduledEvents = [existing];+ writeFileSync(goalsFile(), JSON.stringify({ goals: [goal] }));+ rejectInsert = 12;+ let progressWrite;+ duringInsert = () => (progressWrite ??= addProgressEntry('g1', { note: 'keep progress', date: '2026-12-20' }));+ deleteEvent.mockImplementation(async ({ eventId }) => {+ if (eventId === 'evt-11' || eventId === 'evt-20') throw new Error('Delete unavailable');+ if (eventId === 'evt-1') throw Object.assign(new Error('Already deleted'), { code: 410 });+ });+ await expect(scheduleTimeBlocks('g1')).rejects.toMatchObject({+ code: 'CALENDAR_CLEANUP_FAILED', message: expect.stringContaining('2 calendar events could not be removed')+ });+ expect(insertAttempts).toBe(20);+ expect(deleteEvent).toHaveBeenCalledTimes(19);+ let saved = JSON.parse(readFileSync(goalsFile(), 'utf8')).goals[0];+ expect(saved.scheduledEvents.map(evt => evt.googleEventId)).toEqual(['old-event', 'evt-11', 'evt-20']);+ expect(saved.progressLog[0].note).toBe('keep progress');+ expect(saved.title).toBe(goal.title);+ expect(deleteEvent).not.toHaveBeenCalledWith(expect.objectContaining({ eventId: 'old-event' }));++ await expect(rescheduleTimeBlocks('g1')).rejects.toMatchObject({ code: 'CALENDAR_CLEANUP_FAILED' });+ expect(insertAttempts).toBe(20);+ saved = JSON.parse(readFileSync(goalsFile(), 'utf8')).goals[0];+ expect(saved.scheduledEvents.map(evt => evt.googleEventId)).toEqual(['evt-11', 'evt-20']);+ deleteEvent.mockResolvedValue({});+ await removeScheduledEvents('g1');+ expect(JSON.parse(readFileSync(goalsFile(), 'utf8')).goals[0].scheduledEvents).toEqual([]);+ });++ it('preserves previously scheduled events when a new schedule succeeds', async () => {+ const goal = baseGoal();+ const existing = { id: 'existing', googleEventId: 'old-event', calendarId: 'primary' };+ goal.scheduledEvents = [existing];+ writeFileSync(goalsFile(), JSON.stringify({ goals: [goal] }));+ const result = await scheduleTimeBlocks('g1');+ expect(JSON.parse(readFileSync(goalsFile(), 'utf8')).goals[0].scheduledEvents).toEqual([existing, ...result.events]);+ });++ it('reports survivor IDs when the goal was deleted during a failed insert batch', async () => {+ writeFileSync(goalsFile(), JSON.stringify({ goals: [dailyGoal()] }));+ rejectInsert = 12;+ let deletion;+ duringInsert = () => (deletion ??= deleteGoal('g1'));+ deleteEvent.mockRejectedValueOnce(new Error('Delete unavailable'));+ await expect(scheduleTimeBlocks('g1')).rejects.toMatchObject({+ code: 'CALENDAR_CLEANUP_FAILED',+ message: expect.stringContaining('could not be saved'),+ context: { remainingEvents: [expect.objectContaining({ googleEventId: 'evt-1', calendarId: 'example-calendar' })] }+ });+ expect(JSON.parse(readFileSync(goalsFile(), 'utf8')).goals).toEqual([]);+ });++ it('compensates events when saving a completed schedule fails and retains failed deletions on the next write', async () => {+ writeFileSync(goalsFile(), JSON.stringify({ goals: [baseGoal()] }));+ vi.spyOn(goalStore, 'mutateGoals').mockRejectedValueOnce(new Error('Disk unavailable'));+ deleteEvent.mockRejectedValueOnce(new Error('Delete unavailable'));+ await expect(scheduleTimeBlocks('g1')).rejects.toMatchObject({+ code: 'CALENDAR_CLEANUP_FAILED', message: expect.stringContaining('Could not save scheduled calendar events')+ });+ expect(deleteEvent).toHaveBeenCalledTimes(insertedEvents.length);+ expect(JSON.parse(readFileSync(goalsFile(), 'utf8')).goals[0].scheduledEvents).toEqual([+ expect.objectContaining({ googleEventId: 'evt-1' })+ ]);+ });++ it('reports survivor IDs explicitly if the survivor write also fails', async () => {+ writeFileSync(goalsFile(), JSON.stringify({ goals: [dailyGoal()] }));+ rejectInsert = 12;+ vi.spyOn(goalStore, 'mutateGoals').mockRejectedValueOnce(new Error('Disk unavailable'));+ deleteEvent.mockRejectedValueOnce(new Error('Delete unavailable'));+ await expect(scheduleTimeBlocks('g1')).rejects.toMatchObject({+ code: 'CALENDAR_CLEANUP_FAILED', message: expect.stringContaining('could not be saved'),+ context: {+ remainingEvents: [expect.objectContaining({ googleEventId: 'evt-1', calendarId: 'example-calendar' })],+ persistenceError: 'Disk unavailable'+ }+ });+ expect(deleteEvent).toHaveBeenCalledTimes(11);+ expect(JSON.parse(readFileSync(goalsFile(), 'utf8')).goals[0].scheduledEvents).toEqual([]);+ });++ it('reports surviving event IDs when the goal is deleted during removal', async () => {+ const goal = baseGoal();+ goal.scheduledEvents = [{ id: 'sched-1', googleEventId: 'existing-event', calendarId: 'example-calendar' }];+ writeFileSync(goalsFile(), JSON.stringify({ goals: [goal] }));+ deleteEvent.mockImplementationOnce(async () => {+ await deleteGoal('g1');+ throw new Error('Delete unavailable');+ });++ await expect(removeScheduledEvents('g1')).rejects.toMatchObject({+ code: 'CALENDAR_CLEANUP_FAILED',+ message: expect.stringContaining('goal was deleted during cleanup'),+ context: { remainingEvents: goal.scheduledEvents }+ });+ expect(JSON.parse(readFileSync(goalsFile(), 'utf8')).goals).toEqual([]);+ });++ it('reports surviving event IDs and the write failure when removal cannot persist its result', async () => {+ const goal = baseGoal();+ goal.scheduledEvents = [{ id: 'sched-1', googleEventId: 'existing-event', calendarId: 'example-calendar' }];+ writeFileSync(goalsFile(), JSON.stringify({ goals: [goal] }));+ const writeError = new Error('Disk unavailable');+ vi.spyOn(goalStore, 'mutateGoals').mockRejectedValueOnce(writeError);+ deleteEvent.mockRejectedValueOnce(new Error('Delete unavailable'));++ await expect(removeScheduledEvents('g1')).rejects.toMatchObject({+ code: 'CALENDAR_CLEANUP_FAILED',+ message: expect.stringContaining('Could not save the cleanup result: Disk unavailable'),+ cause: writeError,+ context: { remainingEvents: goal.scheduledEvents, persistenceError: 'Disk unavailable' }+ });+ expect(JSON.parse(readFileSync(goalsFile(), 'utf8')).goals[0].scheduledEvents).toEqual(goal.scheduledEvents);+ });++ it('reports a cleanup write failure after all calendar events were removed', async () => {+ const goal = baseGoal();+ goal.scheduledEvents = [{ id: 'sched-1', googleEventId: 'existing-event', calendarId: 'primary' }];+ writeFileSync(goalsFile(), JSON.stringify({ goals: [goal] }));+ const writeError = new Error('Disk unavailable');+ vi.spyOn(goalStore, 'mutateGoals').mockRejectedValueOnce(writeError);++ await expect(removeScheduledEvents('g1')).rejects.toMatchObject({+ code: 'CALENDAR_SAVE_FAILED',+ message: expect.stringContaining('All 1 calendar events were removed'),+ cause: writeError,+ context: { remainingEvents: [], persistenceError: 'Disk unavailable' }+ });+ });+it('removeScheduledEvents clears events on a freshly re-read goal', async () => {const goal = baseGoal();goal.scheduledEvents = [{ id: 'sched-1', googleEventId: 'evt-1', calendarId: 'primary', milestoneId: 'ms1', date: '2026-06-01', createdAt: '2026-01-01T00:00:00.000Z' }];