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([issue-3705]): address codex review — generation-guard the pin's sticky-clear

Adam Eivyb0639b95fc1c
diff --git a/server/services/mortalLoomStore.js b/server/services/mortalLoomStore.js
index e04b1337ff..975662b866 100644
--- a/server/services/mortalLoomStore.js
+++ b/server/services/mortalLoomStore.js
@@ -100,6 +100,13 @@ export function defaultStorePath() { return DEFAULT_ICLOUD_PATH; }
// through to the retry path silently.
let lastPinnedPath = null;
+// Monotonic pin-attempt counter. Each pin captures its own generation so a stale
+// child's late failure can be told apart from the CURRENT pin even when both share
+// the same path (an A → B → A settings churn re-pins A while A's first child is
+// still in flight). A path-only guard can't distinguish those two A children, so
+// the first one's late failure would wrongly clear the second's sticky state and
+// let the next event spawn a duplicate pin.
+let pinGeneration = 0;
// The boot/settings-change pin. The spawn + detach/unref + error/exit handler
// plumbing (and the shared once-per-process "brctl missing" warning) all live in
@@ -108,23 +115,25 @@ let lastPinnedPath = null;
// after a *successful* pin (one configured path at a time; re-pin only when the
// path changes). `retryAfterExit: false` keeps the shared helper from tracking
// the pin in its in-flight read-heal map. `onFailure` clears the sticky path so a
-// failed / signal-killed pin can be retried on the next settings:updated — and
-// the captured `path` guard confines that clear to the pin's own path, so a stale
-// child's late failure can't null the cache for a path that has since moved on.
+// failed / signal-killed pin can be retried on the next settings:updated — gated
+// on the captured generation so only the CURRENT pin's own failure clears it (a
+// superseded pin's late failure is ignored).
function pinAgainstEviction(path) {
if (process.platform !== 'darwin') return;
if (!path || lastPinnedPath === path) return;
lastPinnedPath = path;
+ const myGeneration = ++pinGeneration;
+ const stillCurrent = () => pinGeneration === myGeneration;
const spawned = requestMaterialization(path, {
label: 'MortalLoom store',
retryAfterExit: false,
- onFailure: () => { if (lastPinnedPath === path) lastPinnedPath = null; },
+ onFailure: () => { if (stillCurrent()) lastPinnedPath = null; },
});
// The helper declined to spawn (a non-iCloud configured path, or a synchronous
// spawn failure — the non-darwin case is already handled above). Clear the
// sticky path so a corrected/healable path on the next settings:updated isn't
// wrongly deduped as "already pinned".
- if (!spawned && lastPinnedPath === path) lastPinnedPath = null;
+ if (!spawned && stillCurrent()) lastPinnedPath = null;
}
// === On-demand (blocking) materialization for the write path ===
@@ -198,6 +207,7 @@ export function _resetMortalLoomInitForTest() {
listenerAttached = false;
didInitialPin = false;
lastPinnedPath = null;
+ pinGeneration = 0;
// The once-per-process "brctl missing" flag now lives in icloudFile; tests that
// need it cleared reset it via icloudFile._resetICloudFileStateForTest().
}
diff --git a/server/services/mortalLoomStore.test.js b/server/services/mortalLoomStore.test.js
index e3bcc70561..f65b951f9f 100644
--- a/server/services/mortalLoomStore.test.js
+++ b/server/services/mortalLoomStore.test.js
@@ -930,6 +930,34 @@ describe('initMortalLoomStore — brctl pinning', () => {
expect(spawnMock).toHaveBeenCalledTimes(2);
});
+ it('a superseded pin for the SAME path does not clear the current pin (A → B → A)', async () => {
+ // Path alone can't tell two children for the same path apart: with an
+ // A → B → A settings churn, the FIRST A child is still in flight when the
+ // second A pin starts. A path-only guard would let the first A child's late
+ // failure clear the second A pin's sticky state, so the next settings:updated
+ // for A would wrongly spawn a duplicate. The per-attempt generation guard
+ // confines each failure's cache-clear to the pin that is still current.
+ Object.defineProperty(process, 'platform', { value: 'darwin', configurable: true });
+
+ const childA1 = makeFakeChild();
+ const childB = makeFakeChild();
+ const childA2 = makeFakeChild();
+ spawnMock.mockReturnValueOnce(childA1).mockReturnValueOnce(childB).mockReturnValueOnce(childA2);
+ settings = { mortalloom: { enabled: true, path: ubiq('A.json') } };
+ await store.initMortalLoomStore(); // childA1 (gen 1)
+ settingsEvents.emit('settings:updated', { mortalloom: { enabled: true, path: ubiq('B.json') } }); // childB (gen 2)
+ settingsEvents.emit('settings:updated', { mortalloom: { enabled: true, path: ubiq('A.json') } }); // childA2 (gen 3), sticky = A
+ expect(spawnMock).toHaveBeenCalledTimes(3);
+
+ // The FIRST A child now fails — it is superseded, so it must NOT clear the
+ // sticky path that belongs to the live second A pin.
+ childA1._emit('exit', 1, null);
+
+ // Re-emit A: still deduped by childA2's intact sticky, so no fourth spawn.
+ settingsEvents.emit('settings:updated', { mortalloom: { enabled: true, path: ubiq('A.json') } });
+ expect(spawnMock).toHaveBeenCalledTimes(3);
+ });
+
it('tolerates non-string path in settings without throwing in the listener', async () => {
// Regression: settings.json is shallow-merged and not schema-validated, so
// mortalloom.path can land as a number / array / object. Calling .trim()