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: guard NaN life expectancy and refresh the goals record's own timeHorizons copy (#4122)

[._.]/ Adam Eivy6888e03118c8
diff --git a/server/services/identity/goals.js b/server/services/identity/goals.js
index 219fd8ac92..3e0aa83f4f 100644
--- a/server/services/identity/goals.js
+++ b/server/services/identity/goals.js
@@ -232,6 +232,10 @@ export async function getGoals(options) {
// whatever it had rather than having its urgencies silently blanked.
const longevity = await loadLongevityFor(data);
if (longevity.timeHorizons) {
+ // The record carries its own copy of the horizons (stamped by setBirthDate, and
+ // merged across peers by dataSync) — refresh it too, or the payload would pair
+ // fresh urgencies with a stale `timeHorizons` and federate the stale one onward.
+ data.timeHorizons = longevity.timeHorizons;
for (const goal of data.goals) {
if (goal.status === 'active') goal.urgency = computeGoalUrgency(goal, longevity.timeHorizons);
}
@@ -269,8 +273,9 @@ export async function setBirthDate(birthDate) {
}
export async function createGoal({ title, description, horizon, category, goalType, parentId, tags, targetDate, timeBlockConfig, featureAreas }) {
+ // getGoals() already refreshed `goals.timeHorizons` against today, so read it from
+ // there rather than paying for a second longevity load.
const goals = await getGoals();
- const longevity = await loadLongevityFor(goals);
// Validate parentId references an existing goal
if (parentId && !goals.goals.find(g => g.id === parentId)) {
@@ -305,8 +310,8 @@ export async function createGoal({ title, description, horizon, category, goalTy
};
// Calculate urgency if time horizons available
- if (longevity.timeHorizons) {
- goal.urgency = computeGoalUrgency(goal, longevity.timeHorizons);
+ if (goals.timeHorizons) {
+ goal.urgency = computeGoalUrgency(goal, goals.timeHorizons);
}
goals.goals.push(goal);
@@ -344,10 +349,10 @@ export async function updateGoal(goalId, updates) {
}
goal.updatedAt = new Date().toISOString();
- // Recalculate urgency if horizon changed
- const longevity = await loadLongevityFor(goals);
- if (longevity.timeHorizons) {
- goal.urgency = computeGoalUrgency(goal, longevity.timeHorizons);
+ // Recalculate urgency if horizon changed — against the horizons getGoals() already
+ // refreshed, not a second longevity load.
+ if (goals.timeHorizons) {
+ goal.urgency = computeGoalUrgency(goal, goals.timeHorizons);
}
goals.updatedAt = new Date().toISOString();
diff --git a/server/services/identity/longevity.test.js b/server/services/identity/longevity.test.js
index c7340aea9c..ef988f8c6c 100644
--- a/server/services/identity/longevity.test.js
+++ b/server/services/identity/longevity.test.js
@@ -25,7 +25,7 @@ vi.mock('../meatspaceCalendar.js', () => ({ getActivities: vi.fn(async () => [])
const { computeTimeHorizons } = await import('./markers.js');
const { applyFreshTimeHorizons, readLongevity, getLongevity } = await import('./longevity.js');
-const { getGoals, getGoalsTree, computeGoalUrgency } = await import('./goals.js');
+const { createGoal, getGoals, getGoalsTree, computeGoalUrgency } = await import('./goals.js');
const { saveJSON } = await import('./store.js');
// A stored snapshot whose horizons were computed a year before "now": at age 44
@@ -96,6 +96,14 @@ describe('computeTimeHorizons', () => {
expect(computeTimeHorizons('not-a-date', 80)).toBeNull();
});
+ it('returns null for a NaN life expectancy (typeof NaN is "number")', () => {
+ // Without a finiteness check this slips through and computes
+ // yearsRemaining 0 / percentLifeComplete 100 — the exact false "no time
+ // left" reading the null contract exists to prevent.
+ expect(computeTimeHorizons(birthDateYearsAgo(45), NaN)).toBeNull();
+ expect(computeTimeHorizons(birthDateYearsAgo(45), Infinity)).toBeNull();
+ });
+
it('clamps a birth date already past the life expectancy', () => {
const horizons = computeTimeHorizons(birthDateYearsAgo(95), 80);
@@ -218,6 +226,26 @@ describe('goal urgency ranks on the re-derived horizons', () => {
expect(saveJSON).not.toHaveBeenCalled();
});
+ it('getGoals refreshes the record\'s own timeHorizons copy too', async () => {
+ // goals.json carries its own horizons (stamped by setBirthDate, merged across
+ // peers by dataSync) — a fresh urgency next to a stale copy would federate the
+ // stale one onward.
+ h.goalsData.timeHorizons = staleGenerousSnapshot().timeHorizons;
+
+ const data = await getGoals();
+
+ expect(data.timeHorizons.yearsRemaining).toBeCloseTo(10, 1);
+ expect(data.timeHorizons.yearsRemaining).not.toBe(40);
+ });
+
+ it('createGoal scores a new goal on the re-derived horizons', async () => {
+ h.goalsData.goals = [];
+
+ const goal = await createGoal({ title: 'Example Goal', horizon: '10-year' });
+
+ expect(goal.urgency).toBeCloseTo(0.7, 2);
+ });
+
it('falls back to the stored horizons when there is no birth date', async () => {
// Nothing to re-derive from, so this degrades to exactly the pre-fix
// behavior — the snapshot's own horizons — rather than fabricating any.
diff --git a/server/services/identity/markers.js b/server/services/identity/markers.js
index 394aea3cae..db350678e8 100644
--- a/server/services/identity/markers.js
+++ b/server/services/identity/markers.js
@@ -156,10 +156,13 @@ export function extractCardiovascularMarkers(savedMarkers) {
* Returns `null` — not a zero-filled shape — when the birth date is missing or
* unparseable, or when no adjusted life expectancy has been derived yet. "We can't
* compute horizons" must stay distinguishable from "you have no time left"; callers
- * gate on the null.
+ * gate on the null. `Number.isFinite` rather than a `typeof` check, because
+ * `typeof NaN === 'number'`: a NaN expectancy would otherwise sail past the guard
+ * and land on exactly the misleading `yearsRemaining: 0` / `percentLifeComplete: 100`
+ * this returns null to avoid.
*/
export function computeTimeHorizons(birthDate, adjustedLifeExpectancy) {
- if (!birthDate || typeof adjustedLifeExpectancy !== 'number') return null;
+ if (!birthDate || !Number.isFinite(adjustedLifeExpectancy)) return null;
const birthMs = new Date(birthDate).getTime();
if (Number.isNaN(birthMs)) return null;