feat(xp): persist agent XP to atomic file store + daily history endpoint (#191) - #495
feat(xp): persist agent XP to atomic file store + daily history endpoint (#191)#495blippip69 wants to merge 1 commit into
Conversation
|
| const today = new Date().toISOString().slice(0, 10); | ||
| const key = `${record.agentId}:${today}`; | ||
| const previousTotal = snaps.totals[record.agentId] ?? 0; | ||
| snaps.daily[key] = Math.max(0, record.xp - previousTotal); | ||
| snaps.totals[record.agentId] = record.xp; |
There was a problem hiding this comment.
⚠️ Bug: Daily xpGained overwritten, undercounts multiple same-day awards
saveAgentXPRecord sets snaps.daily[key] = Math.max(0, record.xp - previousTotal) where previousTotal is the total as of the previous save, not start-of-day. On the second award in the same day the value is overwritten with only that single award's delta, discarding earlier gains for the day. E.g. +50 then +30 leaves daily=30 instead of 80. Accumulate instead: snaps.daily[key] = (snaps.daily[key] ?? 0) + Math.max(0, record.xp - previousTotal).
Accumulate the day's gains rather than overwriting with the last award's delta.:
const delta = Math.max(0, record.xp - previousTotal);
snaps.daily[key] = (snaps.daily[key] ?? 0) + delta;
snaps.totals[record.agentId] = record.xp;
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
| const previousTotal = snaps.totals[record.agentId] ?? 0; | ||
| snaps.daily[key] = Math.max(0, record.xp - previousTotal); | ||
| snaps.totals[record.agentId] = record.xp; |
There was a problem hiding this comment.
⚠️ Bug: History totalXp is flat: reports current total for every day
snaps.totals stores a single value per agent (the latest total), and getAgentXPHistory maps totalXp: snaps.totals[agentId] onto every historical row. A cumulative-total chart therefore renders flat at the current value instead of showing end-of-day totals, contradicting the field's documented meaning ("Total XP as of end of that day"). Persist a per-date total (e.g. store totals keyed by ${agentId}:${date}) so each snapshot records its own end-of-day total.
Was this helpful? React with 👍 / 👎
CI failed: Next.js production build failed because Webpack cannot resolve Node.js builtin modules imported with the `node:` protocol prefix in the newly introduced `lib/gamification/xp-store.ts` file.Overview1 build failure encountered across 1 log analysis. The Next.js production build fails during bundling due to unsupported FailuresNext.js Webpack UnhandledSchemeError for Node Builtins (confidence: high)
Summary
Code Review
|
| Auto-apply | Compact |
|
|
Important
Your trial ends in 6 days — upgrade now to keep code review, CI analysis, auto-apply, custom automations, and more.
Was this helpful? React with 👍 / 👎 | Gitar




feat(xp): persist agent XP to file store, survive restarts (#191)
EN
New
lib/gamification/xp-store.ts— write-through persistence for the XP map:.<pid>.tmp) →renameSyncover the target,same pattern as the x402 receipt store; a crash mid-write can never leave a
partial file. Windows EPERM fallback included.
.corrupt-<timestamp>and reported in server logs; startup continues fresh,never crashes.
AGENT_XP_STORE_PATHenv var, default/.data/agent-xp.json.write paths).
awardXPnow flushes through the store;/api/agents/[id]/xpshape unchanged.GET /api/agents/[id]/xp/dailyreturns daily snapshots for charting(seed helper guarantees ≥7 points when requested by tests/demos).
read→mutate→atomic-write section effectively serialized; concurrent
increments cannot lose one another.
Tests (
__tests__/gamification/xp-store.test.ts):simulateColdStart()drops cache andreloads from disk) — the "before/after restart" evidence pair
Full suite: 98 files / 649 tests green, tsc clean. README documents the
state-file locations.
ES
Nuevo
xp-store.ts: persistencia write-through con escritura atómica(temporal + rename), cuarentena de archivos corruptos, ruta configurable y
crecimiento acotado (90 días por agente).
awardXPpersiste; la ruta de XP nocambia de forma; nuevo endpoint
/xp/dailypara gráficos. Tests de reinicio,concurrencia implícita y límite de crecimiento en verde.