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: stop evicted iCloud reads from re-spawning brctl and flooding the log

Adam Eivy545dae915eec
diff --git a/server/lib/icloudFile.js b/server/lib/icloudFile.js
index d7b4d14753..f9f4d36aac 100644
--- a/server/lib/icloudFile.js
+++ b/server/lib/icloudFile.js
@@ -264,6 +264,14 @@ const pendingDownloads = new Map();
// later reads pick up where this one stopped.
const MAX_PENDING_DOWNLOADS = 4;
+// `brctl download` exits 0 within ~100ms of QUEUEING the download, so the in-flight
+// dedupe above clears almost immediately while the file stays evicted for seconds
+// to minutes. Without a cooldown every read (several per second from polling
+// callers) re-spawned brctl and logged a "download requested" line. Re-requesting
+// the same path inside this window cannot speed materialization up.
+const REQUEST_COOLDOWN_MS = 30_000;
+const lastRequestAt = new Map();
+
// Every background download gets a deadline. `brctl` is exactly what hangs when
// iCloud is wedged — the very condition this module exists for — and without a
// deadline four hung children would hold all MAX_PENDING_DOWNLOADS slots for the
@@ -315,6 +323,8 @@ export function requestMaterialization(path, options = {}) {
if (retryAfterExit) {
if (pendingDownloads.has(path)) return false;
if (pendingDownloads.size >= MAX_PENDING_DOWNLOADS) return false;
+ const last = lastRequestAt.get(path);
+ if (last !== undefined && Date.now() - last < REQUEST_COOLDOWN_MS) return false;
}
// try/catch at a child-process boundary (permitted by the repo's no-try/catch
@@ -334,7 +344,10 @@ export function requestMaterialization(path, options = {}) {
}
// `spawn` is synchronous, so no other read can interleave between the size
// check above and this reservation.
- if (retryAfterExit) pendingDownloads.set(path, child);
+ if (retryAfterExit) {
+ pendingDownloads.set(path, child);
+ lastRequestAt.set(path, Date.now());
+ }
// Release only the entry THIS child owns (see the map's comment above). A no-op
// for an untracked (pin) request, which was never added to the set.
const release = () => {
@@ -522,6 +535,7 @@ async function guardedRead(path, options) {
export function _resetICloudFileStateForTest() {
brctlMissingWarned = false;
pendingDownloads.clear();
+ lastRequestAt.clear();
readFlight = createSingleFlight();
dirCloudCache.clear();
}
diff --git a/server/lib/icloudFile.test.js b/server/lib/icloudFile.test.js
index c5abf2c6cb..60be084427 100644
--- a/server/lib/icloudFile.test.js
+++ b/server/lib/icloudFile.test.js
@@ -231,10 +231,21 @@ describe('requestMaterialization', () => {
expect(icloud.requestMaterialization(ICLOUD_PATH)).toBe(false);
expect(spawnMock).toHaveBeenCalledTimes(1);
- // Once the child exits the dedupe entry clears, so a later read can retry.
+ // Once the child exits the in-flight entry clears, but brctl exits 0 as soon
+ // as the download is QUEUED — re-requesting inside the cooldown would re-spawn
+ // (and re-log) on every poll while the file is still evicted.
child.emit('exit', 0, null);
- expect(icloud.requestMaterialization(ICLOUD_PATH)).toBe(true);
- expect(spawnMock).toHaveBeenCalledTimes(2);
+ expect(icloud.requestMaterialization(ICLOUD_PATH)).toBe(false);
+ expect(spawnMock).toHaveBeenCalledTimes(1);
+
+ // After the cooldown a later read can retry.
+ const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(Date.now() + 31_000);
+ try {
+ expect(icloud.requestMaterialization(ICLOUD_PATH)).toBe(true);
+ expect(spawnMock).toHaveBeenCalledTimes(2);
+ } finally {
+ nowSpy.mockRestore();
+ }
});
it('unrefs the child so a slow download cannot hold the process open', () => {
@@ -498,6 +509,9 @@ describe('background download slot ownership', () => {
const originalDeadline = icloud.DOWNLOAD_DEADLINE_MS;
icloud._setDownloadDeadlineForTest(10);
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true);
+ // Each read is a cooldown window apart so only slot ownership gates the spawn.
+ let clock = Date.now();
+ const nowSpy = vi.spyOn(Date, 'now').mockImplementation(() => clock);
try {
statMock.mockResolvedValue(datalessStats);
const children = [];
@@ -510,6 +524,7 @@ describe('background download slot ownership', () => {
// Deadline fires and frees the slot; a fresh read then starts a REPLACEMENT
// child for the same path.
await new Promise(r => setTimeout(r, 30));
+ clock += 31_000;
await icloud.readIfMaterialized(P).catch(() => {});
expect(spawnMock).toHaveBeenCalledTimes(2);
@@ -522,9 +537,11 @@ describe('background download slot ownership', () => {
// The replacement's own exit does release it.
children[1].emit('exit', 0, null);
+ clock += 31_000;
await icloud.readIfMaterialized(P).catch(() => {});
expect(spawnMock).toHaveBeenCalledTimes(3);
} finally {
+ nowSpy.mockRestore();
killSpy.mockRestore();
icloud._setDownloadDeadlineForTest(originalDeadline);
}
diff --git a/server/services/mortalLoomStore.js b/server/services/mortalLoomStore.js
index eeb65ef603..3ebec60430 100644
--- a/server/services/mortalLoomStore.js
+++ b/server/services/mortalLoomStore.js
@@ -380,6 +380,16 @@ async function readEnabledStore() {
* cycle, never truncates the user's data. Strict callers are the ones that turn
* `ok: false` into an explicit `unavailable`.
*/
+// Reads of an evicted store arrive several times a second; one warning per window.
+const EVICTED_WARN_INTERVAL_MS = 60_000;
+let lastEvictedWarnAt = 0;
+function warnEvictedOnce(detail) {
+ const now = Date.now();
+ if (now - lastEvictedWarnAt < EVICTED_WARN_INTERVAL_MS) return;
+ lastEvictedWarnAt = now;
+ console.warn(`⚠️ MortalLoom store evicted from local storage; ${detail}`);
+}
+
async function readStoreAtPathResult(path) {
// Attempt the read directly and classify from the error code rather than gating
// on `existsSync` first (codex #2742 review): `existsSync` swallows its errno —
@@ -401,7 +411,7 @@ async function readStoreAtPathResult(path) {
if (err.code === ICLOUD_NOT_MATERIALIZED) {
// Present but evicted — never a trustworthy empty, so a strict read reports
// `unavailable` and updateStore's guard refuses to seed over real data.
- console.warn(`⚠️ MortalLoom store evicted from local storage; skipping read until iCloud materializes it: ${path}`);
+ warnEvictedOnce(`skipping read until iCloud materializes it: ${path}`);
unreadable = true;
return null;
}
@@ -748,7 +758,7 @@ export async function getStatus() {
// the Settings page isn't waiting on some unrelated code path to trigger it.
const evicted = isEvictedStats(path, st);
if (evicted) {
- console.warn(`⚠️ MortalLoom store evicted from local storage; status summary unavailable: ${path}`);
+ warnEvictedOnce(`status summary unavailable: ${path}`);
requestMaterialization(path, { label: 'MortalLoom store' });
}
const raw = st && !evicted ? await withTransientRetry(() => readIfMaterialized(path, { label: 'MortalLoom store' })).catch((err) => {