Skip to content

fix: correct pet needs decay accumulation - #13

Merged
mozyonee merged 2 commits into
mainfrom
fix/pet-decay
Mar 9, 2026
Merged

fix: correct pet needs decay accumulation#13
mozyonee merged 2 commits into
mainfrom
fix/pet-decay

Conversation

@mozyonee

@mozyonee mozyonee commented Mar 8, 2026

Copy link
Copy Markdown
Owner

Issues fixed

1. Pet needs not decaying at all

pet.needs is a Mongoose subdocument — spreading it with { ...needs } copies internal Mongoose state instead of plain values. calculateDecayedNeeds was computing decay on garbage data and writing the original unchanged values back to the DB. Fixed by extracting a plain object via PET_NEED_KEYS before passing to the function.

2. Decay intervals being silently lost

needsUpdatedAt was saved as wall-clock new Date() after each decay write, discarding the sub-interval remainder. Under normal 5s polling with any timing jitter, up to 50% of decay intervals could be lost each cycle. Fixed by snapping needsUpdatedAt to lastUpdatedAt + intervals * PET_UPDATE_INTERVAL so the remainder carries over to the next run. calculateDecayedNeeds now returns intervals to support this.

3. Verbose urgent needs check

The startExpedition urgent needs guard used 5 individual if statements. Condensed to a single PET_NEED_KEYS.some(...) call.

Test plan

  • Verify pet needs decrease over time when polling /pets/:id or /pets/user
  • Verify needs decay correctly after a long gap (pet not fetched for several minutes)
  • Verify starting an expedition is blocked when any need is below the urgent threshold

… verbosity

- snap needsUpdatedAt to last interval boundary instead of wall clock so sub-interval remainder carries over to the next run
- return intervals from calculateDecayedNeeds to support snapping
- condense urgent needs check in startExpedition using PET_NEED_KEYS.some

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mozyonee mozyonee closed this Mar 8, 2026
@mozyonee mozyonee reopened this Mar 8, 2026
@railway-app
railway-app Bot temporarily deployed to widgetable / widgetable-pr-13 March 8, 2026 23:30 Destroyed
@railway-app

railway-app Bot commented Mar 8, 2026

Copy link
Copy Markdown

🚅 Deployed to the widgetable-pr-13 environment in widgetable

Service Status Web Updated (UTC)
Server ✅ Success (View Logs) Web Mar 9, 2026 at 12:04 am
Client ✅ Success (View Logs) Web Mar 8, 2026 at 11:34 pm

…goose subdoc serialization issue

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@railway-app
railway-app Bot temporarily deployed to widgetable / widgetable-pr-13 March 8, 2026 23:58 Destroyed
@mozyonee
mozyonee merged commit 07dfc9d into main Mar 9, 2026
3 checks passed
@mozyonee
mozyonee deleted the fix/pet-decay branch March 9, 2026 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant