Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

mcp: a state file that goes away is not served from the cache (#653)

Duarte Santose4441f7f893a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cd4c8ab42..3d69d9c23 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -44,6 +44,8 @@ asking for anything, promise.
in shared plan files (#640, thanks @rudycelekli for spotting the review side).
- A Coach review that adds a routine with an empty slot in it gets a second try instead of
failing outright (thanks @rudycelekli).
+- The MCP server stops showing a profile whose data was deleted, also where the system can't
+ watch files (thanks @rudycelekli).
## v1.4.2 (2026-10-18)
diff --git a/mcp/src/state.js b/mcp/src/state.js
index 9a786440b..bd45d7dea 100644
--- a/mcp/src/state.js
+++ b/mcp/src/state.js
@@ -12,6 +12,7 @@ let _db = undefined
let _uid = null
let _watcher = null
let _loadedMtime = 0 // mtimeMs we last read at — used to catch watcher omissions
+let _seeded = false // a test's in-memory state, never compared with the disk
// null means one thing only: the file is not there (an account that never synced from a device).
// Every other failure — no permission on the mount, a truncated or corrupt file — is a real fault,
@@ -119,8 +120,12 @@ export function getState() {
// Re-read if the file's mtime changed since our last load — covers watcher omissions and
// platforms with no fs.watch.
let mtime
- try { mtime = fs.statSync(file).mtimeMs } catch {
- return _state === undefined ? null : _state
+ try { mtime = fs.statSync(file).mtimeMs } catch (e) {
+ if (_seeded) return _state
+ // Gone (the account was deleted, say): without a watcher the cached profile used to be
+ // served on and on. Anything else gets the read below, which reports it as the fault it is.
+ if (e.code === 'ENOENT') { _state = null; _loadedMtime = 0; return null }
+ mtime = NaN
}
if (_state === undefined || mtime !== _loadedMtime) {
const fresh = readJsonOrNull(file)
@@ -129,8 +134,10 @@ export function getState() {
// field the app added since the snapshot was last saved shows up undefined-safe.
_state = Object.assign({}, defaultsShape(), fresh)
_loadedMtime = mtime
- } else if (_state === undefined) {
- _state = null // no state file at all — never signed in on a device
+ } else {
+ // No state file at all (never signed in on a device), or it went between stat and read.
+ _state = null
+ _loadedMtime = 0
}
}
return _state
@@ -151,6 +158,7 @@ export function _seedStateForTests(state) {
_db = { users: [{ id: _uid, name: 'Test', created: '2026-07-26T00:00:00.000Z' }], creds: [], subs: [], invites: [] }
_state = state
_loadedMtime = Number.MAX_SAFE_INTEGER // never re-read from disk in a test
+ _seeded = true
if (_watcher) { _watcher.close(); _watcher = null }
}
diff --git a/mcp/test/state.test.js b/mcp/test/state.test.js
index 8ec00ed6c..a6c4b3ffb 100644
--- a/mcp/test/state.test.js
+++ b/mcp/test/state.test.js
@@ -42,6 +42,19 @@ afterEach(() => { vi.unstubAllEnvs() })
afterAll(() => { dirs.forEach(d => fs.rmSync(d, { recursive: true, force: true })) })
describe('a read failure is not "you never signed in"', () => {
+ test('without a watcher, a state file that goes away stops being served from the cache (#653)', async () => {
+ const dir = mk({ 'db.json': DB, 'state-alex.json': JSON.stringify({ unit: 'lb' }) })
+ const watch = vi.spyOn(fs, 'watch').mockImplementation(() => { throw new Error('no fs.watch here') })
+ try {
+ const { getState } = await load(dir)
+ expect(getState().unit).toBe('lb')
+ fs.rmSync(path.join(dir, 'state-alex.json'))
+ expect(getState()).toBeNull()
+ fs.writeFileSync(path.join(dir, 'state-alex.json'), JSON.stringify({ unit: 'kg' }))
+ expect(getState().unit).toBe('kg')
+ } finally { watch.mockRestore() }
+ })
+
test('no state file at all is still null — that account really has never synced', async () => {
const { getState } = await load(mk({ 'db.json': DB }))
expect(getState()).toBeNull()