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
Report calendar deletion failures and retry orphan cache cleanup (#11545)
[._.]/ff76d6b4022d
diff --git a/client/src/components/calendar/ConfigTab.test.jsx b/client/src/components/calendar/ConfigTab.test.jsxindex 7395e5c7e2..dc0c792bc0 100644--- a/client/src/components/calendar/ConfigTab.test.jsx+++ b/client/src/components/calendar/ConfigTab.test.jsx@@ -1,11 +1,14 @@-import { beforeEach, describe, expect, it, vi } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { useState } from 'react';import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';import { MemoryRouter } from 'react-router';import ConfigTab from './ConfigTab';+import { deleteCalendarAccount } from '../../services/apiCalendar.js';const api = vi.hoisted(() => ({getGoogleAuthStatus: vi.fn(),clearGoogleAuth: vi.fn(),+ deleteCalendarAccount: vi.fn(),}));const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn() }));vi.mock('../../services/api', () => api);@@ -17,6 +20,7 @@ beforeEach(() => {vi.resetAllMocks();api.getGoogleAuthStatus.mockResolvedValue({ hasCredentials: true, hasTokens: true });});+afterEach(() => vi.unstubAllGlobals());const openAuth = async () => {render(<MemoryRouter><ConfigTab accounts={[account]} setAccounts={vi.fn()} /></MemoryRouter>);@@ -60,3 +64,31 @@ describe('Google credential clearing', () => {expect(api.getGoogleAuthStatus).toHaveBeenCalledTimes(2);});});++describe('calendar account deletion', () => {+ // Catches false success/removal on cleanup failure, a missing visible API+ // error, or bodyless retry success failing to remove the retained account.+ it('shows the API failure, retains the account, and lets deletion succeed on retry', async () => {+ api.deleteCalendarAccount.mockImplementation(deleteCalendarAccount);+ const fetchMock = vi.fn()+ .mockResolvedValueOnce(new Response(JSON.stringify({ error: 'Filesystem operation failed', code: 'EACCES' }), { status: 500 }))+ .mockResolvedValueOnce(new Response(null, { status: 204 }));+ vi.stubGlobal('fetch', fetchMock);+ function Harness() {+ const [accounts, setAccounts] = useState([account]);+ return <MemoryRouter><ConfigTab accounts={accounts} setAccounts={setAccounts} /></MemoryRouter>;+ }+ render(<Harness />);+ fireEvent.click(screen.getByRole('button', { name: 'Delete account' }));+ fireEvent.click(screen.getByRole('button', { name: 'Remove' }));+ await waitFor(() => expect(toast.error).toHaveBeenCalledExactlyOnceWith('Filesystem operation failed'));+ expect(screen.getByText('Example Calendar')).toBeTruthy();+ expect(toast.success).not.toHaveBeenCalled();+ fireEvent.click(screen.getByRole('button', { name: 'Delete account' }));+ fireEvent.click(screen.getByRole('button', { name: 'Remove' }));+ await waitFor(() => expect(screen.queryByText('Example Calendar')).toBeNull());+ expect(toast.success).toHaveBeenCalledExactlyOnceWith('Account deleted');+ expect(fetchMock).toHaveBeenCalledTimes(2);+ expect(fetchMock).toHaveBeenLastCalledWith(`/api/calendar/accounts/${account.id}`, expect.objectContaining({ method: 'DELETE' }));+ });+});diff --git a/server/lib/README.md b/server/lib/README.mdindex 466567ce71..1b995f5a8a 100644--- a/server/lib/README.md+++ b/server/lib/README.md@@ -769,7 +769,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub|---|---|| `browserSuiteGate.js` | `browserSuiteCanRun(label, prerequisites)` is the `canRun` guard for the cross-workspace `*.browser.test.js` suites (Chrome + ffmpeg + the client bundler). Pass `{ name: discoveredValue }`; a falsy value is missing. Returns `false` with a visible skip log locally, but throws naming the missing prerequisites when `PORTOS_REQUIRE_BROWSER_SUITES` is set — the flag the CI `database` job sets for the browser suites the planner selected (#10312). || `dbTestGate.js` | `requireDbOrSkip(label, dbReady, reason)` keeps a missing local test database as a visible skipped suite, but throws when `PORTOS_REQUIRE_DB` is set so CI cannot pass after DB-backed suites disappear. |-| `gitTestRepo.js` | Shared real-git sandbox for integration tests (#4394): one initialized template (working tree + bare origin) per worker, then `fs.cp` into a fresh temp dir. `makeGitSandbox({ origin })`, `attachBareOrigin(scratch, repo)`, `materializeGitRepo(dest)`, `destroyGitSandbox`, plus `SKIP_HEAVY_INTEGRATION` (`VITEST_FAST=1`). `resetGitSandbox({ scratch, repo, initialHead })` / `resetGitWorktreeSandbox(repo, initialHead)` restore a sandbox in place (branches, worktrees, remote) so a `describe` can build one sandbox in `beforeAll` and reset between tests instead of paying the fs.cp/rm cycle per test (#5902). Every entry point runs `assertTempPath` first, so a path outside `os.tmpdir()` throws instead of `git init`-ing or `rm -rf`-ing a real checkout (#4554). Still real git — just not rebuilt from `init`+`commit`+`push` in every `beforeEach`. |+| `gitTestRepo.js` | Shared real-git sandbox for integration tests (#4394): one initialized template (working tree + bare origin) per worker, then `fs.cp` into a fresh temp dir. `makeGitSandbox({ origin })`, `attachBareOrigin(scratch, repo)`, `materializeGitRepo(dest)`, `destroyGitSandbox`, plus `SKIP_HEAVY_INTEGRATION` (`VITEST_FAST=1`). `resetGitSandbox({ scratch, repo, initialHead })` / `resetGitWorktreeSandbox(repo, initialHead)` restore a sandbox in place (branches, worktrees, remote) so a `describe` can build one sandbox in `beforeAll` and reset between tests instead of paying the fs.cp/rm cycle per test (#5902). Every entry point runs `assertTempPath` first, so a path outside `os.tmpdir()` throws instead of `git init`-ing or `rm -rf`-ing a real checkout (#4554). Fixture-local maintenance/GC detachment is disabled so awaited Git operations retain their writers through teardown. Still real git — just not rebuilt from `init`+`commit`+`push` in every `beforeEach`. || `mirrorParity.js` | Source-extraction and comparison primitives for declaration-level contract tests: the vendored `aiToolkit/` copy of `TOOL_USE_RE` (`localModelHeuristics.mirror.test.js`), and the bootstrap sequence's body assertions (`bootstrap*.test.js`). A pure module the browser needs is imported by the client rather than mirrored, so no client↔server copy test should need these: `stripCommentsAndNormalize` (so per-side commentary may diverge but logic may not), `extractDeclaration(src, name)` (balanced `{}`/`()`/`[]` walk over `function` / `async function` / `const`), and `compareDeclaration(serverSrc, clientSrc, name)`. Use these instead of hand-rolling a brace-walker per mirror. Pure — no `vitest` import — so callers own the assertions. || `mockPathsDataRoot.js` | Shared Vitest helpers for `PATHS.data → temp dir`, no-peer record creation guards, deterministic federation-identity doubles (`mockTestIdentity`), and `ownTestBodies(it)` → `{ it, drain }`, which owns each test body so teardown can `await drain()` before removing a fixture root — a body left running by Vitest's fail-fast cancellation otherwise recreates the removed root (`test temp leak:`). || `settingsTestUtil.js` | `bindSettingsFile(dataRoot)` → `writeSettingsFile`/`mergeSettingsFile`: direct settings.json disk writes that also drop the `getSettings()` read cache (dynamic-import reset) so a stale cache can't survive a bypass-`save()` write. |diff --git a/server/lib/gitTestRepo.js b/server/lib/gitTestRepo.jsindex 729c30391b..e70d1e5017 100644--- a/server/lib/gitTestRepo.js+++ b/server/lib/gitTestRepo.js@@ -46,6 +46,14 @@ async function configureIdentity(repo, identity = DEFAULT_IDENTITY) {await execGit(['config', 'commit.gpgsign', 'false'], repo);}+async function keepMaintenanceAttached(repo) {+ assertTempPath(repo, 'git maintenance configuration on a fixture repo');+ // An awaited Git command can otherwise leave detached maintenance writing+ // into .git/objects after the test starts removing its private sandbox.+ await execGit(['config', 'maintenance.autoDetach', 'false'], repo);+ await execGit(['config', 'gc.autoDetach', 'false'], repo);+}+async function stripOrigin(repo) {assertTempPath(repo, 'git remote surgery on a fixture repo');await execGit(['remote', 'remove', 'origin'], repo, { ignoreExitCode: true });@@ -80,11 +88,13 @@ async function buildTemplate() {const origin = join(scratch, 'origin.git');assertTempPath(scratch, 'git init for the fixture template');await execGit(['init', '-b', 'main', repo], scratch);+ await keepMaintenanceAttached(repo);await configureIdentity(repo);await writeFile(join(repo, 'initial.txt'), 'initial');await execGit(['add', '-A'], repo);await execGit(['commit', '-m', 'initial'], repo);await execGit(['init', '--bare', '-b', 'main', origin], scratch);+ await keepMaintenanceAttached(origin);await execGit(['remote', 'add', 'origin', origin], repo);await execGit(['push', '-u', 'origin', 'main'], repo);// Copied sandboxes are torn down per-test; the template itself lives fordiff --git a/server/lib/gitTestRepo.test.js b/server/lib/gitTestRepo.test.jsindex 0597d9d5e5..cec3e1adb0 100644--- a/server/lib/gitTestRepo.test.js+++ b/server/lib/gitTestRepo.test.js@@ -1,6 +1,7 @@-import { describe, it, expect, afterEach } from 'vitest';-import { mkdtemp, writeFile, readdir } from 'fs/promises';+import { describe, it, expect, afterEach, vi } from 'vitest';+import { chmod, mkdtemp, readFile, rm, writeFile, readdir } from 'fs/promises';import { existsSync } from 'fs';+import { createHash } from 'node:crypto';import { tmpdir } from 'os';import { join } from 'path';import { execGit } from './execGit.js';@@ -33,10 +34,78 @@ describe('template exit hook', () => {const sandboxes = [];afterEach(async () => {+ vi.unstubAllEnvs();await Promise.all(sandboxes.splice(0).map((s) => destroyGitSandbox(s)));});describe('makeGitSandbox', () => {+ it('keeps automatic maintenance inside the awaited Git lifetime before fixture removal', async () => {+ const box = await makeGitSandbox({ origin: true, prefix: 'portos-git-fx-maintenance-' });+ sandboxes.push(box.scratch);+ const entered = join(box.scratch, 'maintenance-entered');+ const release = join(box.scratch, 'maintenance-release');+ const hookScript = join(box.scratch, 'hold-maintenance.cjs');+ const trace = join(box.scratch, 'git-trace.jsonl');+ // Git samples loose objects in bucket 17. Two objects there and gc.auto=1+ // makes the real auto-maintenance path run, even in this tiny repository.+ for (let n = 0, written = 0; written < 2; n++) {+ const blob = `Example maintenance object ${n}\n`;+ if (!createHash('sha1').update(`blob ${Buffer.byteLength(blob)}\0${blob}`).digest('hex').startsWith('17')) continue;+ await writeFile(join(box.repo, 'maintenance-object.txt'), blob);+ await execGit(['hash-object', '-w', 'maintenance-object.txt'], box.repo);+ written++;+ }+ await execGit(['config', 'gc.auto', '1'], box.repo);+ // Select the task whose pre-auto-gc hook this regression holds.+ await execGit(['config', 'maintenance.gc.enabled', 'true'], box.repo);+ await writeFile(hookScript, `const fs = require('node:fs');+const [entered, release] = process.argv.slice(2);+fs.writeFileSync(entered, 'entered');+const deadline = Date.now() + 5000;+const timer = setInterval(() => {+ if (fs.existsSync(release) || Date.now() >= deadline) {+ clearInterval(timer);+ process.exitCode = fs.existsSync(release) ? 0 : 1;+ }+}, 10);+`);+ const quote = value => `'${value.replaceAll("'", "'\\''")}'`;+ const hook = join(box.repo, '.git', 'hooks', 'pre-auto-gc');+ await writeFile(hook, `#!/bin/sh\nexec ${quote(process.execPath.replaceAll('\\', '/'))} ${quote(hookScript)} ${quote(entered)} ${quote(release)}\n`);+ await chmod(hook, 0o755);+ vi.stubEnv('GIT_TRACE2_EVENT', trace);+ const readEvents = async () => (await readFile(trace, 'utf8')).trim().split('\n').map(line => JSON.parse(line));+ // Each fork adds an exit record without another start, under the same+ // Trace2 session identity. Drain even a deliberately broken baseline.+ const drainTrace = () => vi.waitFor(async () => {+ const events = await readEvents();+ const started = events.filter(event => event.event === 'start').length;+ const detached = events.filter(event => event.event === 'region_enter' && event.label === 'detach').length;+ expect(events.filter(event => event.event === 'exit').length).toBe(started + detached);+ }, { timeout: 3000 });+ let settled = false;+ const commit = execGit(['commit', '--allow-empty', '-m', 'Example maintained commit'], box.repo)+ .finally(() => { settled = true; });+ try {+ await vi.waitFor(() => expect(existsSync(entered)).toBe(true), { timeout: 3000 });+ // The held maintenance hook still owns the fixture. An awaited direct+ // Git child alone is insufficient when maintenance forks and detaches.+ const events = await readEvents();+ expect(events.some(event => event.category === 'maintenance' && event.label === 'detach')).toBe(false);+ expect(settled).toBe(false);+ } finally {+ await writeFile(release, 'release');+ await commit;+ await drainTrace();+ }+ // Receiving a push also runs maintenance in the copied bare origin.+ await execGit(['push', 'origin', 'main'], box.repo);+ await drainTrace();+ expect((await readEvents()).some(event => event.event === 'region_enter' && event.label === 'detach')).toBe(false);+ await rm(box.scratch, { recursive: true, force: true });+ expect(existsSync(box.scratch)).toBe(false);+ });+it('yields a real repo on main with the template commit and no origin by default', async () => {const box = await makeGitSandbox({ prefix: 'portos-git-fx-' });sandboxes.push(box.scratch);diff --git a/server/routes/calendar.js b/server/routes/calendar.jsindex 24554888d3..db519e323a 100644--- a/server/routes/calendar.js+++ b/server/routes/calendar.js@@ -177,9 +177,10 @@ router.delete('/accounts/:id', asyncHandler(async (req, res) => {if (!UUID_RE.test(req.params.id)) {throw new ServerError('Invalid account ID format', { status: 400, code: 'VALIDATION_ERROR' });}- const deleted = await calendarAccounts.deleteAccount(req.params.id);- if (!deleted) throw new ServerError('Account not found', { status: 404, code: 'NOT_FOUND' });- await calendarSync.deleteCache(req.params.id).catch(() => {});+ // Remove metadata first so queued/provider-delayed writes cannot recreate+ // the cache. Retry cleanup even if an earlier attempt removed the account.+ await calendarAccounts.deleteAccount(req.params.id);+ await calendarSync.deleteCache(req.params.id);req.app.get('io')?.emit('calendar:changed', {});res.status(204).send();}));diff --git a/server/routes/calendar.test.js b/server/routes/calendar.test.jsindex 4d52592993..ff976a68c8 100644--- a/server/routes/calendar.test.js+++ b/server/routes/calendar.test.js@@ -1,7 +1,7 @@-import { describe, it, expect, vi, beforeEach } from 'vitest';+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';import express from 'express';import { request } from '../lib/testHelper.js';-import { ServerError } from '../lib/errorHandler.js';+import { ServerError, errorEvents } from '../lib/errorHandler.js';vi.mock('../services/calendarAccounts.js', () => ({listAccounts: vi.fn(),@@ -73,6 +73,8 @@ import * as googleAuth from '../services/googleAuth.js';import * as googleOAuthAutoConfig from '../services/googleOAuthAutoConfig.js';const ACCOUNT_ID = '11111111-1111-1111-1111-111111111111';+const observeError = vi.fn();+afterEach(() => errorEvents.off('error', observeError));describe('Calendar Routes — normalized error handling', () => {let app;@@ -89,6 +91,36 @@ describe('Calendar Routes — normalized error handling', () => {});describe('thrown ServerErrors map to the standard JSON envelope', () => {+ // Catches a false 204 when cleanup fails or a retry rejected merely because+ // account metadata was already removed; cleanup must follow metadata removal.+ it('reports account cleanup failure and accepts retries after account removal', async () => {+ const io = { emit: vi.fn() };+ app.set('io', io);+ errorEvents.once('error', observeError);+ let present = true;+ calendarAccounts.deleteAccount.mockImplementation(async () => {+ const removed = present;+ present = false;+ return removed;+ });+ calendarSync.deleteCache.mockImplementationOnce(async () => {+ expect(present).toBe(false);+ throw Object.assign(new Error('Synthetic cache deletion denied'), { code: 'EACCES' });+ }).mockResolvedValue(undefined);+ const failed = await request(app).delete(`/api/calendar/accounts/${ACCOUNT_ID}`);+ expect(failed.status).toBe(500);+ expect(failed.body.error).toBe('Filesystem operation failed');+ expect(observeError).toHaveBeenCalledWith(expect.objectContaining({ status: 500 }), expect.any(Object));+ expect(io.emit).not.toHaveBeenCalledWith('calendar:changed', {});+ for (let attempt = 0; attempt < 2; attempt++) {+ const retried = await request(app).delete(`/api/calendar/accounts/${ACCOUNT_ID}`);+ expect(retried.status).toBe(204);+ }+ expect(calendarSync.deleteCache).toHaveBeenCalledTimes(3);+ expect(calendarSync.deleteCache).toHaveBeenLastCalledWith(ACCOUNT_ID);+ expect(io.emit.mock.calls.filter(([event]) => event === 'calendar:changed')).toHaveLength(2);+ });+it('GET /review/:date passes bounded page params to the service and defaults to the legacy first page', async () => {dailyReview.getDailyReview.mockResolvedValue({ events: [], total: 0 });await request(app).get('/api/calendar/review/2026-01-02');diff --git a/server/services/calendarCacheConcurrency.test.js b/server/services/calendarCacheConcurrency.test.jsindex c6ef31b879..1145b90348 100644--- a/server/services/calendarCacheConcurrency.test.js+++ b/server/services/calendarCacheConcurrency.test.js@@ -7,7 +7,7 @@ vi.mock('../lib/fileUtils.js', async importOriginal => ({}));vi.mock('fs/promises', () => ({ readdir: vi.fn(), unlink: vi.fn() }));vi.mock('./calendarAccounts.js', () => ({- getAccount: vi.fn(), updateSyncStatus: vi.fn(async () => {}),+ getAccount: vi.fn(), listAccounts: vi.fn(), updateSyncStatus: vi.fn(async () => {}),updateSubcalendars: vi.fn(), mergeDiscoveredSubcalendars: vi.fn(),}));vi.mock('./calendarApiSync.js', () => ({ syncOutlookCalendarApi: vi.fn() }));@@ -24,10 +24,10 @@ vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: vi.fn() }));vi.mock('@googleapis/calendar', () => ({ calendar: vi.fn() }));import { atomicWrite, readJSONFile } from '../lib/fileUtils.js';-import { unlink } from 'fs/promises';-import { getAccount } from './calendarAccounts.js';+import { readdir, unlink } from 'fs/promises';+import { getAccount, listAccounts } from './calendarAccounts.js';import { syncOutlookCalendarApi } from './calendarApiSync.js';-import { deleteCache, loadCache, purgeDisabledSubcalendars, syncAccount } from './calendarSync.js';+import { deleteCache, getEvent, getEvents, loadCache, purgeDisabledSubcalendars, syncAccount } from './calendarSync.js';import { mcpSyncAccount, pushSyncEvents } from './calendarGoogleSync.js';import { apiSyncAccount } from './calendarGoogleApiSync.js';@@ -65,6 +65,8 @@ beforeEach(() => {}]));persisted = new Map();getAccount.mockImplementation(async id => structuredClone(accounts.get(id) ?? null));+ listAccounts.mockImplementation(async () => structuredClone([...accounts.values()]));+ readdir.mockImplementation(async () => [...persisted.keys()].map(id => `${id}.json`));readJSONFile.mockImplementation(async (path, fallback) =>persisted.has(basename(path, '.json'))? structuredClone(persisted.get(basename(path, '.json')))@@ -123,6 +125,30 @@ describe('calendar cache mutation ownership', () => {expect(persisted.get(ACCOUNT).events.map(e => e.apiId)).toEqual(['fresh-event']);});+ // Catches swallowed unlink failures, orphan events escaping any read surface,+ // or a rejected deletion poisoning the queue so cleanup cannot be retried.+ it('reports failed deletion, hides its orphan cache, and retries without admitting delayed writes', async () => {+ await push('a', 'removed-event');+ await push('a', 'kept-event', OTHER);+ accounts.get(OTHER).enabled = false; // Existing disabled accounts remain visible.+ const expected = await getEvents({ accountId: OTHER });+ accounts.delete(ACCOUNT);+ const failure = Object.assign(new Error('Synthetic cache deletion denied'), { code: 'EACCES' });+ unlink.mockRejectedValueOnce(failure);+ await expect(deleteCache(ACCOUNT)).rejects.toBe(failure);+ expect(persisted.has(ACCOUNT)).toBe(true);+ expect(await getEvents()).toEqual(expected);+ expect(await getEvents({ accountId: ACCOUNT })).toEqual({ events: [], total: 0 });+ expect(await getEvent(ACCOUNT, persisted.get(ACCOUNT).events[0].id)).toBeNull();+ expect(await getEvent(OTHER, expected.events[0].id)).toEqual(expected.events[0]);+ await expect(push('b', 'late-event')).rejects.toMatchObject({ status: 404 });+ await deleteCache(ACCOUNT);+ expect(persisted.has(ACCOUNT)).toBe(false);+ // An absent cache is a successful repeat deletion, not an I/O failure.+ unlink.mockRejectedValueOnce(Object.assign(new Error('Synthetic missing cache'), { code: 'ENOENT' }));+ await expect(deleteCache(ACCOUNT)).resolves.toBeUndefined();+ });+it.each([false, true])('reconciles Outlook after clear (account removed: %s)', async removed => {accounts.get(ACCOUNT).type = 'outlook-calendar';persisted.set(ACCOUNT, { events: [{ externalId: 'old', title: 'Old event' }] });diff --git a/server/services/calendarSync.js b/server/services/calendarSync.jsindex a9bf723e83..df41634a69 100644--- a/server/services/calendarSync.js+++ b/server/services/calendarSync.js@@ -83,6 +83,7 @@ export async function getEvents(options = {}) {if (accountId) {const cache = await loadCache(accountId);const account = await getAccount(accountId);+ if (!account) return { events: [], total: 0 };let events = cache.events.map(e => ({ ...e, accountId: e.accountId || accountId }));events = filterDeclinedAndCancelled(events);events = filterByEnabledSubcalendars(events, account);@@ -104,7 +105,7 @@ export async function getEvents(options = {}) {const accountIds = files.filter(file => file.endsWith('.json')).map(file => file.replace('.json', ''))- .filter(id => UUID_RE.test(id));+ .filter(id => UUID_RE.test(id) && accountMap.has(id));// Load each account's cache in parallel rather than serializing one disk read// per account before aggregating the combined calendar view.const caches = await Promise.all(@@ -145,6 +146,7 @@ export async function purgeDisabledSubcalendars(accountId) {export async function getEvent(accountId, eventId) {const cache = await loadCache(accountId);+ if (!await getAccount(accountId)) return null;const event = cache.events.find(e => e.id === eventId);if (!event) return null;return { ...event, accountId: event.accountId || accountId };@@ -162,6 +164,7 @@ export async function deleteCache(accountId) {console.log(`🗑️ No calendar cache to delete for account ${accountId}`);} else {console.error(`❌ Failed to delete calendar cache for account ${accountId}: ${err.message}`);+ throw err;}}});