fix(wdk): prevent resetting cron job lastRun timestamps to 0 in storage#1017
fix(wdk): prevent resetting cron job lastRun timestamps to 0 in storage#1017Ysh204 wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: eee01b0f4c
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (typeof atob !== 'undefined' && typeof TextDecoder !== 'undefined') { | ||
| const decoded = atob(value) | ||
| try { | ||
| return new TextDecoder('utf-8', { fatal: true }).decode(binaryStringToBytes(decoded)) |
There was a problem hiding this comment.
Preserve legacy Latin-1 payloads that are valid UTF-8
When a redirect response was produced by the previous Latin-1 btoa(JSON.stringify(...)) path, this line now treats it as UTF-8 whenever the raw bytes are well-formed UTF-8. For example, a legacy payload containing the literal text é decodes to é, so getRedirectResponse() silently returns different data instead of honoring the legacy fallback. This affects existing wallet redirects with those byte patterns; add an explicit encoding/version signal or otherwise disambiguate before preferring UTF-8.
Useful? React with 👍 / 👎.
Description
This PR resolves a critical bug in the WDK
Cronscheduler utility (packages/wallet/wdk/src/sequence/cron.ts) that caused persisted cron joblastRuntimestamps to be overwritten with0inlocalStoragewhen WDK reloaded or initialized.The Bug
lastRun = 0.runJobs()loads the persisted last run timestamps fromlocalStorageasynchronously.lastRunis less than the job's interval, the job's handler is not executed.job.lastRunremained at0.runJobs(),syncWithStorage()serialized the in-memorythis.jobs(where the job'slastRunwas0) and wrote it back tolocalStorage, erasing the actual last run timestamp.lastRun: 0and triggered the job prematurely.In practice, this meant every background cron job (such as checking queued recovery payloads) would run on every single app restart/tab refresh plus exactly 1 tick later, completely ignoring the configured execution interval.
The Fix
Update
runJobs()incron.tsto sync the in-memoryjob.lastRunstate with the loadedlastRunvalue from storage before evaluating the elapsed time. This ensures that non-triggered jobs preserve their actual persisted timestamps when writing back to storage.Testing & Verification
packages/wallet/wdk/test/cron.test.tsto verify last run persistence, check skip behavior, and ensure correct interval triggers.