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

address review (codex): xp-derived checksum-stable wire level; level 0 authoritative

[._.]/ Adam Eivy669cf40e7219
diff --git a/client/src/utils/cityArtifacts.js b/client/src/utils/cityArtifacts.js
index e0741d6ff3..1c79face2a 100644
--- a/client/src/utils/cityArtifacts.js
+++ b/client/src/utils/cityArtifacts.js
@@ -68,7 +68,9 @@ function finiteOrNull(value) {
// `level` field (a legacy payload that predates age-based level) falls back to XP.
export function effectiveLevel(character) {
const lvl = character?.level;
- if (Number.isFinite(lvl) && lvl >= 1) return Math.floor(lvl);
+ // Any finite nonnegative level is authoritative — including 0, a legitimate age level for a
+ // birthDate less than a year ago. (`>= 1` would reject it and let XP unlock level artifacts.)
+ if (Number.isFinite(lvl) && lvl >= 0) return Math.floor(lvl);
if (lvl === null) return null;
const xp = finiteOrNull(character?.xp);
if (xp === null) return null;
diff --git a/client/src/utils/cityArtifacts.test.js b/client/src/utils/cityArtifacts.test.js
index 5f141a0f2e..0467e9d201 100644
--- a/client/src/utils/cityArtifacts.test.js
+++ b/client/src/utils/cityArtifacts.test.js
@@ -33,10 +33,14 @@ describe('effectiveLevel', () => {
expect(effectiveLevel({ level: null, xp: 0 })).toBeNull();
});
+ it('treats age level 0 (birthDate < 1yr ago) as authoritative, not an XP fallback', () => {
+ expect(effectiveLevel({ level: 0, xp: 999999 })).toBe(0);
+ });
+
it('returns null when neither level nor xp is usable', () => {
expect(effectiveLevel(null)).toBeNull();
expect(effectiveLevel({})).toBeNull();
- expect(effectiveLevel({ level: 0, xp: 'nope' })).toBeNull();
+ expect(effectiveLevel({ level: null, xp: 'nope' })).toBeNull();
});
});
diff --git a/server/services/character.js b/server/services/character.js
index 91a5fd32e8..30beebfe9c 100644
--- a/server/services/character.js
+++ b/server/services/character.js
@@ -62,6 +62,21 @@ export function levelFromAge(ageYears) {
return Number.isFinite(ageYears) ? Math.floor(ageYears) : null;
}
+// Legacy pre-#2673 XP→level curve, retained ONLY for the federation wire projection
+// (getWireCharacter) so an older peer — which still levels off XP — receives a level
+// consistent with the shared `xp`. NOT used for the live age-based level.
+const LEGACY_XP_THRESHOLDS = [
+ 0, 300, 900, 2700, 6500, 14000, 23000, 34000, 48000, 64000,
+ 85000, 100000, 120000, 140000, 165000, 195000, 225000, 265000, 305000, 355000
+];
+function legacyLevelFromXp(xp) {
+ const safe = Number.isFinite(xp) ? xp : 0;
+ for (let i = LEGACY_XP_THRESHOLDS.length - 1; i >= 0; i--) {
+ if (safe >= LEGACY_XP_THRESHOLDS[i]) return i + 1;
+ }
+ return 1;
+}
+
function createEvent(type, description, overrides = {}) {
return {
id: crypto.randomUUID(),
@@ -118,15 +133,17 @@ export async function getCharacter() {
// Federation wire projection: the persisted record plus a backward-compatible integer `level`
// for pre-#2673 peers, whose CharacterSheet indexes XP thresholds by `character.level` and
-// NaNs without it. The level is age-derived (or the historical default 1 when birthDate is
-// unset) but NOT persisted; `ageYears` is deliberately excluded so the sync checksum stays
-// stable between birthdays. New peers ignore the remote level (applyCharacterRemote no longer
-// merges it), so this projection is invisible to same-version installs.
+// NaNs without it. The level is the LEGACY xp-derived value (what those peers compute from the
+// same shared `xp`), NOT the age level — deliberately so it stays a pure function of
+// `character.json`: the file-mtime checksum that fingerprints the `character` category then
+// still invalidates correctly, whereas an age/time-derived level would drift out of sync with
+// that checksum on a birthday or a birthDate edit (which touch a different file). It is not
+// persisted, and new peers ignore the remote level (applyCharacterRemote no longer merges it),
+// so this projection is invisible to same-version installs.
export async function getWireCharacter() {
const raw = await readJSONFile(CHARACTER_FILE, null);
if (!raw) return null;
- const { birthDate } = await getBirthDate().catch(() => ({ birthDate: null }));
- return { ...raw, level: levelFromAge(ageYearsFromBirthDate(birthDate)) ?? 1 };
+ return { ...raw, level: legacyLevelFromXp(raw.xp) };
}
export async function saveCharacter(data) {
diff --git a/server/services/character.test.js b/server/services/character.test.js
index 7abe95d21e..95c4dc3cf9 100644
--- a/server/services/character.test.js
+++ b/server/services/character.test.js
@@ -117,23 +117,27 @@ describe('getWireCharacter federation projection', () => {
store.birthDate = null;
});
- it('adds an age-derived level to the wire payload without persisting it', async () => {
+ it('adds a legacy xp-derived level to the wire payload without persisting it', async () => {
+ // 5000 xp → legacy level 4 (>= the 2700 threshold, < 6500). A valid integer for a
+ // pre-#2673 peer's XP-threshold UI, never null.
+ const wire = await characterService.getWireCharacter();
+ expect(wire.level).toBe(4);
+ expect(store.value.level).toBeUndefined(); // still not persisted
+ expect(wire.ageYears).toBeUndefined();
+ });
+
+ it('keeps the wire level a pure function of xp (checksum-stable), independent of birthDate', async () => {
+ // Setting a birthDate must NOT change the wire level — otherwise it would drift out of
+ // sync with the character.json-mtime checksum that fingerprints the sync category.
const birth = new Date();
birth.setFullYear(birth.getFullYear() - 33);
- birth.setDate(birth.getDate() - 30);
store.birthDate = birth.toISOString();
-
const wire = await characterService.getWireCharacter();
- expect(wire.level).toBe(33);
- // But the persisted record still carries no level.
- expect(store.value.level).toBeUndefined();
- });
+ expect(wire.level).toBe(4); // xp-derived, not age 33
- it('falls back to the historical default level 1 for backward-compat when birthDate is unset', async () => {
- store.birthDate = null;
- const wire = await characterService.getWireCharacter();
- expect(wire.level).toBe(1); // pre-#2673 peers index XP thresholds by level — never null
- expect(wire.ageYears).toBeUndefined(); // ageYears excluded so the sync checksum stays stable
+ store.value = { name: 'New', class: 'Dev', xp: 0, hp: 15, maxHp: 15 };
+ const fresh = await characterService.getWireCharacter();
+ expect(fresh.level).toBe(1); // 0 xp → level 1, never null
});
});