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: keep Outlook calendar history older than the sync window (#12961)

[._.]/ Adam Eivye01ec8b55f16
diff --git a/server/lib/README.md b/server/lib/README.md
index e6207375b6..8654bb3ba9 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -335,6 +335,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `openAiModelsProbe.js` | `probeOpenAiModels(baseUrl, { timeoutMs, apiKey })` → `{ reachable, models, contextWindows, error }` — the one `GET {base}/models` probe for the local OpenAI-compatible daemons, shared by `services/observedContextWindows.js` (and through it `services/providerReadiness.js`) and `services/llamaServerManager.js`. Distinguishes unreachable from reachable-but-unlistable (`models: null`) from up-with-nothing-loaded (`[]`). `contextWindows` keys each served model to the window the listing declared (`max_model_len` / `n_ctx` / `loaded_context_length` / `context_length`, smallest wins), carrying an entry only where one was declared so `{}` reads as unknown — without it a prompt too large for the endpoint was dispatched anyway and stalled until the provider timeout (#7441). It also names the real transport failure via `describeFetchError` (undici reports every one as a bare `fetch failed`), and cancels an unread body on a non-OK response. `apiKey` attaches a Bearer header for a key-gated daemon (vLLM's compose stack), and a 401/403 answers `reachable: true` with `error: 'authentication required'` — a server that refused the request is definitively running, and calling it unreachable would send the user to start it again. Consolidated after the two copies drifted — one passed its timeout as a `timeout` key inside the fetch init object, where it is not an option, silently running a 500ms poll loop on the 15s default. |
| `openAiModelsProbeCache.js` | `probeOpenAiModelsCached(endpoint, apiKey)` and `resetOpenAiModelsProbeCache()` — the shared 15s TTL cache in front of `openAiModelsProbe.js`, keyed `endpoint\napiKey` (two providers on one authenticated endpoint must not inherit each other’s 401) and storing the PROMISE so N providers sharing a daemon share one socket. It lives BELOW its two readers — `services/providerReadiness.js`’s checklist and `services/observedContextWindows.js`’s pre-dispatch context gate — so a page poll and a run starting in the same window cost one listing and can never disagree about a daemon that restarted between them. An unexpected throw drops the entry rather than poisoning it for the TTL; expired entries are swept on every miss. `services/providerReadiness.js#resetProviderReadinessCache` clears it for the llama-server lifecycle routes. |
| `openAiChatStream.js` | `iterateOpenAiChat(...)` → normalized async content/reasoning chunks and `streamOpenAiChat(...)` → the streamed text — one streaming `POST {base}/chat/completions` against any OpenAI-compatible endpoint; both own timeout/abort composition, retries, parsing, backpressure, and reader cleanup. `streamOllamaChat(...)` is the assessment-only native `/api/chat` transport that preserves Ollama's exact eval counts and nanosecond timings. Also exports `buildMessages`, `parseStreamFrame`, `parseOllamaStreamFrame`, `normalizeUsage` (snake/camel/Ollama token-count keys → `{completionTokens, promptTokens}`, `null` = not reported), and `resolvePartialOutput`. Registering `onStats` on the OpenAI path asks for token counts; a daemon that rejects `stream_options` is retried once without it and remembered per endpoint. Sibling of `openAiModelsProbe.js`. Shared by Ask (`services/askService.js`), `services/localLlmPlayground.js` (provider-backed runs with a `/runs` record), and its `runEndpointLlmTest` (a bare loopback daemon PortOS holds no provider record for, which is how `services/localModelAssessments.js` measures llama.cpp / MTPLX / vLLM). An abort mid-stream throws with `.partialOutput` carrying what already streamed. |
+| `calendarSyncWindow.js` | `overlapsSyncWindow(event, dateRange)`: whether a cached calendar event falls in the window a sync covered, so reconcile-prune never deletes history or far-future events a windowed fetch could not return. Shared by the Google and Outlook paths. Pure. |
| `callerModePolicy.js` | The one caller EXECUTION-MODE policy: `CALLER_MODE_POLICIES` (`agent-harness`, `cli-harness`, `direct-api`, `any-text`) plus `resolveCallerModePolicy` / `allowedModesFor` / `callerModeRejection` / `isCallerModeEligible` / `filterCallerModeEligible`. Answers "may THIS caller run on THAT route mode?" for an explicit pin, `activeProvider` inheritance and every fallback candidate alike — `allowedModesFor()` is what rides on `requestCapabilities.allowedModes` into the toolkit's `getFallbackProvider`. Pure and dependency-light so the out-of-process autofixer can import it; `enabled`/benched state, prerequisites and text-transport consent stay with their existing owners. Unknown never becomes true: a record with no executable mode, and a required model capability with no positive evidence, are both refused. |
| `cliChildEnv.js` | The one place the AI-CLI child environment is composed, replacing the hand-rolled copy every spawn site carried — which made each env-level fix an N-file sweep (#3194). `buildCliChildEnv({ baseEnv, before, provider, model, cwd, extra, guard })` returns a COMPLETE env for `spawn`: filters the inherited base to runtime essentials/provider auth, then layers `baseEnv → before → Ollama-Claude defaults → provider.envVars → buildOpencodeEnvVars → extra`, pins `PWD` to `cwd`, strips `CLAUDECODE`, and (with `guard: true`) prepends the pm2 guard shim onto the final `PATH`. The Ollama-Claude layer raises Claude Code's default output ceiling to 65,536 tokens so a thinking-capable local model cannot finish its reasoning past the stock 32K ceiling and die before its final tool call; an explicit provider env value wins. `composeProviderEnv({ before, provider, model, extra })` returns just the ordered provider layers, for sites that build a DELTA someone else bases and spawns (the CoS runner payload, a shell-session overlay). The two slots are not interchangeable: `before` sits UNDER `provider.envVars` (forgeTokenEnv/claudeSettingsEnv, so a provider override still wins), `extra` sits OVER it (TERM/COLORTERM for a PTY). `cliChildEnv.test.js` asserts the composed order per call site and **discovers** any new site that hand-rolls the tuple instead of calling these — so the call-site list stays in the test, not in prose here. |
| `agentApiToken.js` | `AGENT_API_TOKEN_ENV` (`PORTOS_API_TOKEN`) + `AGENT_API_AUTH_CURL_ARG` — the name of the loopback session token PortOS injects into a spawned agent's environment (`services/agentApiAuth.js`) and the quoted `curl` argument that spends it, so the prompt builders writing agent-facing `curl` snippets and the spawn sites injecting the variable cannot drift. `agentApiCurl({ apiBase, path, payload })` and `agentApiAuthNote({ alsoCovering })` assemble the whole call and the paragraph that must travel with it — one shape for every agent-facing API call in the tree, after two hand-written copies had already drifted on whether the install "gates" or "may gate" `/api/*` (it is opt-in and off by default). Pure, zero-import. |
diff --git a/server/lib/calendarSyncWindow.js b/server/lib/calendarSyncWindow.js
new file mode 100644
index 0000000000..de134420e4
--- /dev/null
+++ b/server/lib/calendarSyncWindow.js
@@ -0,0 +1,14 @@
+/**
+ * True when a cached event could have been returned by a sync that covered
+ * `dateRange` ({ pastDate, futureDate } Dates), i.e. its absence from the batch
+ * means an upstream deletion. No range means the batch is the complete calendar.
+ * Bounds are exclusive like Google's timeMin/timeMax; unparseable/missing times
+ * can't be proven outside the window.
+ */
+export function overlapsSyncWindow(event, dateRange) {
+ if (!dateRange) return true;
+ const start = Date.parse(event.startTime || event.endTime);
+ const end = Date.parse(event.endTime || event.startTime);
+ return (Number.isNaN(start) || start < dateRange.futureDate.getTime())
+ && (Number.isNaN(end) || end > dateRange.pastDate.getTime());
+}
diff --git a/server/lib/index.js b/server/lib/index.js
index 2f36259f26..76f7fa9ce7 100644
--- a/server/lib/index.js
+++ b/server/lib/index.js
@@ -663,6 +663,7 @@ export * from './systemIdle.js';
export * from './browserConfig.js';
export * from './browserUnsafePorts.js';
export * from './buildId.js';
+export * from './calendarSyncWindow.js';
export * from './buildIdentity.js';
export * from './condaEnv.js';
export * from './cudaCapability.js';
diff --git a/server/services/calendarApiSync.js b/server/services/calendarApiSync.js
index 6a77f2c186..aadb1d0aa8 100644
--- a/server/services/calendarApiSync.js
+++ b/server/services/calendarApiSync.js
@@ -151,5 +151,5 @@ export async function syncOutlookCalendarApi(account, _cache, io, options = {})
// Declined/cancelled filtering handled universally by calendarSync.filterDeclinedAndCancelled at read time
console.log(`📅 Calendar API sync complete: ${events.length} events in ${page} page(s)`);
- return { events, status: 'success', syncMethod: 'api' };
+ return { events, status: 'success', syncMethod: 'api', dateRange: { pastDate: startRange, futureDate: endRange } };
}
diff --git a/server/services/calendarGoogleSync.js b/server/services/calendarGoogleSync.js
index 1a399cff37..9d76b2200f 100644
--- a/server/services/calendarGoogleSync.js
+++ b/server/services/calendarGoogleSync.js
@@ -7,6 +7,7 @@ import { getSettings } from './settings.js';
import { pickCliProvider, runCliProviderPrompt } from '../lib/cliProviderRun.js';
import { extractJson } from '../lib/jsonExtract.js';
import { selectMeetingUrl } from '../lib/meetingUrl.js';
+import { overlapsSyncWindow } from '../lib/calendarSyncWindow.js';
import { ServerError } from '../lib/errorHandler.js';
// Google Calendar sync is driven through an MCP-capable CLI provider — the
@@ -161,18 +162,8 @@ 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);
- // Bounds are exclusive like Google's timeMin/timeMax; 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) || !inSyncedWindow(e)
+ e.subcalendarId !== calendarId || incomingIds.has(e.externalId) || !overlapsSyncWindow(e, dateRange)
);
pruned = before - cache.events.length;
}
diff --git a/server/services/calendarSync.js b/server/services/calendarSync.js
index df41634a69..75501183bd 100644
--- a/server/services/calendarSync.js
+++ b/server/services/calendarSync.js
@@ -3,6 +3,7 @@ import { join } from 'path';
import { atomicWrite, ensureDir, filterBySearch as genericFilterBySearch, PATHS, readJSONFile, safeDate, UUID_RE } from '../lib/fileUtils.js';
import { createKeyedFileWriteQueue } from '../lib/fileWriteQueue.js';
import { ServerError } from '../lib/errorHandler.js';
+import { overlapsSyncWindow } from '../lib/calendarSyncWindow.js';
import { calendarTouchpointDedupeKey } from '../lib/tribeMatch.js';
import { getUserTimezone } from './userTimezone.js';
import { getAccount, updateSyncStatus } from './calendarAccounts.js';
@@ -247,7 +248,8 @@ async function runAccountSync(accountId, io, options) {
if (providerStatus === 'success') {
const fetchedIds = new Set(newEvents.filter(e => e.externalId).map(e => e.externalId));
const before = cache.events.length;
- cache.events = cache.events.filter(e => !e.externalId || fetchedIds.has(e.externalId));
+ // A windowed fetch only vouches for events overlapping its window.
+ cache.events = cache.events.filter(e => !e.externalId || fetchedIds.has(e.externalId) || !overlapsSyncWindow(e, providerResult?.dateRange));
pruned = before - cache.events.length;
if (pruned > 0) console.log(`🧹 Pruned ${pruned} stale calendar events from ${account.name}`);
}
diff --git a/server/services/calendarSync.test.js b/server/services/calendarSync.test.js
index 58c3ad00a6..3db88d33fc 100644
--- a/server/services/calendarSync.test.js
+++ b/server/services/calendarSync.test.js
@@ -195,6 +195,25 @@ describe('Outlook batch identity reconciliation (#8635)', () => {
});
});
+describe('Outlook windowed prune (#12961)', () => {
+ it('prunes only in-window events missing from the batch', async () => {
+ const day = 24 * 60 * 60 * 1000;
+ const at = d => new Date(Date.now() + d * day).toISOString();
+ getAccount.mockResolvedValue({ id: ACCOUNT_ID, name: 'Example Account', enabled: true, type: 'outlook-calendar' });
+ const old = { id: 'old', externalId: 'old', startTime: at(-20), endTime: at(-20) };
+ const far = { id: 'far', externalId: 'far', startTime: at(200), endTime: at(200) };
+ const gone = { id: 'gone', externalId: 'gone', startTime: at(2), endTime: at(2) };
+ const kept = { id: 'kept', externalId: 'kept', startTime: at(3), endTime: at(3) };
+ readJSONFile.mockResolvedValue({ events: [old, far, gone, kept] });
+ syncOutlookCalendarApi.mockResolvedValue({
+ status: 'success', events: [kept],
+ dateRange: { pastDate: new Date(Date.now() - 7 * day), futureDate: new Date(Date.now() + 90 * day) },
+ });
+ expect(await syncAccount(ACCOUNT_ID)).toMatchObject({ pruned: 1 });
+ expect(atomicWrite.mock.calls.at(-1)[1].events.map(e => e.id).sort()).toEqual(['far', 'kept', 'old']);
+ });
+});
+
// Real file I/O is essential: cloning a mock's ENOENT fallback hides the bug.
afterAll(cleanupTempDataRoots);