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
stop one reachable peer pinning every wake to coordinate, and stop printing a rate nobody measured
[._.]/ Adam Eivy7fd55add1c1f
diff --git a/server/lib/persistentMindPlaybookPhase.js b/server/lib/persistentMindPlaybookPhase.jsindex c58d4b8d16..3c477ff10b 100644--- a/server/lib/persistentMindPlaybookPhase.js+++ b/server/lib/persistentMindPlaybookPhase.js@@ -64,6 +64,15 @@ const measuredFailureRate = (productivity) => {return null;};+/**+ * The coarse health enum is a STAND-IN for a failure rate, not a measurement+ * of one — `attention` is set by a stopped managed app, an open review alert,+ * or 85% disk, none of which is a failed unit of work. It still belongs in the+ * ladder (a mind whose install is unhealthy should maintain), but it must+ * never be rendered as "N% recent failure rate": a day on which nothing ran+ * would print a measurement that was never taken. The caller keeps the two+ * apart via the `failureRateMeasured` flag.+ */const healthFailureRate = (health) => {if (!health || typeof health.status !== 'string') return null;if (health.status === 'error') return 1;@@ -82,7 +91,7 @@ const healthFailureRate = (health) => {*/export function derivePersistentMindPlaybookPhaseSignals(worldSignals) {if (!worldSignals || typeof worldSignals !== 'object') {- return { districtCount: null, failureRate: null, peersWithActivity: null };+ return { districtCount: null, failureRate: null, failureRateMeasured: false, peersReachable: null };}// A failed source read arrives as `null`, not `[]`. Counting those as zero@@ -101,16 +110,21 @@ export function derivePersistentMindPlaybookPhaseSignals(worldSignals) {const productivity = Array.isArray(worldSignals.productivity) ? worldSignals.productivity[0] : null;// `??` (not `||`) so a measured zero-failure day stays 0 rather than// falling through to the coarse health signal.- const failureRate = measuredFailureRate(productivity) ?? healthFailureRate(worldSignals.health);+ const measured = measuredFailureRate(productivity);+ const failureRate = measured ?? healthFailureRate(worldSignals.health);- // "Unread peer contributions" approximated as federated peers currently- // reporting non-steady status (active/attention/error — something changed- // since the last look) that this mind can actually travel to.- const peersWithActivity = Array.isArray(worldSignals.peers)- ? worldSignals.peers.filter((peer) => peer?.travelAvailable === true && peer?.status && peer.status !== 'steady').length+ // Peers this mind can actually travel to. This is NOT "peers with unread+ // contributions": a peer is only a travel destination when `listEidoverse+ // Destinations()` saw it `status === 'online'`, which `coarseStatus` maps to+ // `active` — so filtering the projection for a non-`steady` status was true+ // for every travelable peer and measured nothing. Count reachability, say+ // reachability, and let it rank below construction so one permanently-online+ // peer cannot pin every wake to `coordinate`.+ const peersReachable = Array.isArray(worldSignals.peers)+ ? worldSignals.peers.filter((peer) => peer?.travelAvailable === true).length: null;- return { districtCount, failureRate, peersWithActivity };+ return { districtCount, failureRate, failureRateMeasured: measured !== null, peersReachable };}/**@@ -120,15 +134,20 @@ export function derivePersistentMindPlaybookPhaseSignals(worldSignals) {* construct/maintain/coordinate in.** Priority, highest first: a still-sparse Commons always explores first; a- * high recent failure rate outranks a peer visit (fix what is broken before- * going visiting); an active peer outranks routine construction; a Commons- * short of the maturity threshold keeps building; a mature, healthy,- * peer-quiet Commons falls back to steady-state maintenance.+ * high recent failure rate (or an unhealthy install) outranks everything else,+ * because fixing what is broken comes before building or visiting; a Commons+ * short of the maturity threshold keeps building; a mature Commons with a+ * reachable peer goes visiting; an otherwise mature, peer-less Commons falls+ * back to steady-state maintenance.+ *+ * `coordinate` deliberately ranks BELOW `construct`. The peer signal measures+ * reachability, not unread activity, so a single always-online peer would+ * otherwise satisfy it on every wake and `construct` would never run.*/export function selectPersistentMindPlaybookPhase(signals = {}) {const districtCount = nonNegativeIntOrNull(signals.districtCount);const failureRate = clampFractionOrNull(signals.failureRate);- const peersWithActivity = nonNegativeIntOrNull(signals.peersWithActivity);+ const peersReachable = nonNegativeIntOrNull(signals.peersReachable);if (districtCount === null || districtCount < THRESHOLDS.sparseDistrictCount) {return {@@ -137,13 +156,21 @@ export function selectPersistentMindPlaybookPhase(signals = {}) {};}if (failureRate !== null && failureRate >= THRESHOLDS.highFailureRate) {- return { phase: 'maintain', reason: `${Math.round(failureRate * 100)}% recent failure rate` };- }- if (peersWithActivity !== null && peersWithActivity > 0) {- return { phase: 'coordinate', reason: `${peersWithActivity} peer(s) with new activity to visit` };+ // Only a MEASURED rate is reported as one. The health-derived fallback is+ // a coarse enum standing in for a rate nobody took, and printing it as a+ // percentage claims a measurement that was never made.+ return {+ phase: 'maintain',+ reason: signals.failureRateMeasured === true+ ? `${Math.round(failureRate * 100)}% recent failure rate`+ : 'install health needs attention',+ };}if (districtCount < THRESHOLDS.matureDistrictCount) {return { phase: 'construct', reason: `${districtCount} live signal(s), room to densify before ${THRESHOLDS.matureDistrictCount}` };}+ if (peersReachable !== null && peersReachable > 0) {+ return { phase: 'coordinate', reason: `${peersReachable} reachable peer(s) to visit` };+ }return { phase: 'maintain', reason: `mature Commons (${districtCount} live signal(s)), steady-state upkeep` };}diff --git a/server/lib/persistentMindPlaybookPhase.test.js b/server/lib/persistentMindPlaybookPhase.test.jsindex 177cf35b7c..fcab378325 100644--- a/server/lib/persistentMindPlaybookPhase.test.js+++ b/server/lib/persistentMindPlaybookPhase.test.js@@ -18,36 +18,58 @@ describe('selectPersistentMindPlaybookPhase', () => {});it('constructs once the Commons clears the sparse threshold with no failures or peer activity', () => {- const result = selectPersistentMindPlaybookPhase({ districtCount: 4, failureRate: 0, peersWithActivity: 0 });+ const result = selectPersistentMindPlaybookPhase({ districtCount: 4, failureRate: 0, peersReachable: 0 });expect(result).toMatchObject({ phase: 'construct' });});- it('maintains when the recent failure rate crosses the threshold, even with peer activity waiting', () => {- const result = selectPersistentMindPlaybookPhase({ districtCount: 10, failureRate: 0.25, peersWithActivity: 3 });+ it('maintains when the recent failure rate crosses the threshold, even with peers reachable', () => {+ const result = selectPersistentMindPlaybookPhase({+ districtCount: 10, failureRate: 0.25, failureRateMeasured: true, peersReachable: 3,+ });expect(result).toMatchObject({ phase: 'maintain', reason: expect.stringContaining('25%') });});- it('coordinates when a peer has new activity and failures are below threshold', () => {- const result = selectPersistentMindPlaybookPhase({ districtCount: 10, failureRate: 0.1, peersWithActivity: 1 });- expect(result).toMatchObject({ phase: 'coordinate' });+ // The health-derived rate is a coarse enum standing in for a measurement+ // nobody took. It still routes to `maintain`, but printing it as "50% recent+ // failure rate" would claim a rate on a day when zero work ran.+ it('never reports an unmeasured, health-derived rate as a percentage', () => {+ const result = selectPersistentMindPlaybookPhase({+ districtCount: 10, failureRate: 0.5, failureRateMeasured: false, peersReachable: 0,+ });+ expect(result.phase).toBe('maintain');+ expect(result.reason).not.toMatch(/%/);+ expect(result.reason).toContain('health');+ });++ // A reachable peer must not outrank construction: the peer signal measures+ // reachability, so one always-online peer would otherwise pin every wake to+ // `coordinate` and `construct` would never run.+ it('keeps building below the mature threshold even with a reachable peer', () => {+ const result = selectPersistentMindPlaybookPhase({ districtCount: 10, failureRate: 0.1, peersReachable: 1 });+ expect(result).toMatchObject({ phase: 'construct' });+ });++ it('coordinates once the Commons is mature and a peer is reachable', () => {+ const result = selectPersistentMindPlaybookPhase({ districtCount: 16, failureRate: 0.1, peersReachable: 1 });+ expect(result).toMatchObject({ phase: 'coordinate', reason: expect.stringContaining('reachable peer') });});it('stays in construct just below the mature threshold once failures/peers are quiet', () => {- const result = selectPersistentMindPlaybookPhase({ districtCount: 15, failureRate: 0, peersWithActivity: 0 });+ const result = selectPersistentMindPlaybookPhase({ districtCount: 15, failureRate: 0, peersReachable: 0 });expect(result).toMatchObject({ phase: 'construct' });});it('falls back to maintain for a mature, healthy, peer-quiet Commons', () => {- const result = selectPersistentMindPlaybookPhase({ districtCount: 16, failureRate: 0, peersWithActivity: 0 });+ const result = selectPersistentMindPlaybookPhase({ districtCount: 16, failureRate: 0, peersReachable: 0 });expect(result).toMatchObject({ phase: 'maintain', reason: expect.stringContaining('mature') });});it('always resolves to a documented phase', () => {const matrix = [{},- { districtCount: -5, failureRate: -1, peersWithActivity: -2 },+ { districtCount: -5, failureRate: -1, peersReachable: -2 },{ districtCount: Number.NaN, failureRate: Number.NaN },- { districtCount: 100, failureRate: 0.9, peersWithActivity: 5 },+ { districtCount: 100, failureRate: 0.9, peersReachable: 5 },];for (const signals of matrix) {expect(PERSISTENT_MIND_PLAYBOOK_PHASES).toContain(selectPersistentMindPlaybookPhase(signals).phase);@@ -62,8 +84,9 @@ describe('selectPersistentMindPlaybookPhase', () => {describe('derivePersistentMindPlaybookPhaseSignals', () => {it('returns null signals for a missing or malformed world-signals projection', () => {- expect(derivePersistentMindPlaybookPhaseSignals(null)).toEqual({ districtCount: null, failureRate: null, peersWithActivity: null });- expect(derivePersistentMindPlaybookPhaseSignals('not-an-object')).toEqual({ districtCount: null, failureRate: null, peersWithActivity: null });+ const empty = { districtCount: null, failureRate: null, failureRateMeasured: false, peersReachable: null };+ expect(derivePersistentMindPlaybookPhaseSignals(null)).toEqual(empty);+ expect(derivePersistentMindPlaybookPhaseSignals('not-an-object')).toEqual(empty);});it('sums entities across every projected district array', () => {@@ -149,7 +172,11 @@ describe('derivePersistentMindPlaybookPhaseSignals', () => {expect(selectPersistentMindPlaybookPhase(measuredEmpty).reason).toContain('sparse');});- it('counts only travelable peers reporting non-steady status as active', () => {+ // Reachability is the whole signal. A travel destination is only ever built+ // from a peer `listEidoverseDestinations()` saw as `online`, which+ // `coarseStatus` maps to `active` — so a status filter on top of+ // `travelAvailable` matched every travelable peer and measured nothing.+ it('counts travelable peers regardless of their coarse status', () => {const signals = derivePersistentMindPlaybookPhaseSignals({peers: [{ travelAvailable: true, status: 'active' },@@ -158,7 +185,7 @@ describe('derivePersistentMindPlaybookPhaseSignals', () => {{ travelAvailable: true, status: 'error' },],});- expect(signals.peersWithActivity).toBe(2);+ expect(signals.peersReachable).toBe(3);});});@@ -197,7 +224,7 @@ describe('contract with buildEidoverseWorldSignals()', () => {const signals = derivePersistentMindPlaybookPhaseSignals(worldSignals);expect(signals.districtCount).toBeGreaterThan(0);expect(signals.failureRate).not.toBeNull();- expect(signals.peersWithActivity).toBe(1);+ expect(signals.peersReachable).toBe(1);expect(PERSISTENT_MIND_PLAYBOOK_PHASES).toContain(selectPersistentMindPlaybookPhase(signals).phase);});});diff --git a/server/services/persistentMindAdapter.test.js b/server/services/persistentMindAdapter.test.jsindex 176c99e94f..85033e9d86 100644--- a/server/services/persistentMindAdapter.test.js+++ b/server/services/persistentMindAdapter.test.js@@ -152,7 +152,7 @@ describe('persistent mind adapter', () => {it('resolves a maturity-aware phase and wires it into instructions only for continuous-play (#7458)', async () => {mock.root.config.persistentMindPlaybook = { mode: 'continuous-play' };- mock.resolvePlaybookPhase.mockResolvedValue({ phase: 'coordinate', reason: '2 peer(s) with new activity to visit', signals: { districtCount: 20, failureRate: 0, peersWithActivity: 2 } });+ mock.resolvePlaybookPhase.mockResolvedValue({ phase: 'coordinate', reason: '2 reachable peer(s) to visit', signals: { districtCount: 20, failureRate: 0, peersReachable: 2 } });const prepared = await createPersistentMindTurnAdapter().prepare({ profile });expect(mock.resolvePlaybookPhase).toHaveBeenCalledTimes(1);expect(prepared.playbookPhase).toMatchObject({ phase: 'coordinate' });diff --git a/server/services/persistentMindPlaybookSignals.test.js b/server/services/persistentMindPlaybookSignals.test.jsindex 262e9aacdd..23002ce9f3 100644--- a/server/services/persistentMindPlaybookSignals.test.js+++ b/server/services/persistentMindPlaybookSignals.test.js@@ -23,15 +23,17 @@ describe('resolvePersistentMindPlaybookPhase', () => {});const result = await resolvePersistentMindPlaybookPhase();expect(mock.collectEidoverseWorldSources).toHaveBeenCalledTimes(1);- expect(result.phase).toBe('coordinate');- expect(result.signals).toMatchObject({ districtCount: 5, failureRate: 0, peersWithActivity: 1 });+ // 5 live signals is below the maturity threshold, so a reachable peer does+ // not preempt construction — see selectPersistentMindPlaybookPhase.+ expect(result.phase).toBe('construct');+ expect(result.signals).toMatchObject({ districtCount: 5, failureRate: 0, peersReachable: 1 });});it('degrades to the safe explore default when the signal read fails', async () => {mock.collectEidoverseWorldSources.mockRejectedValue(new Error('world source unavailable'));const result = await resolvePersistentMindPlaybookPhase();expect(result.phase).toBe('explore');- expect(result.signals).toEqual({ districtCount: null, failureRate: null, peersWithActivity: null });+ expect(result.signals).toEqual({ districtCount: null, failureRate: null, failureRateMeasured: false, peersReachable: null });});it('propagates an abort rather than swallowing it as a signal failure', async () => {