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

fix: invalidate daily-log summary caches and restore the concurrency tests (#8032)

[._.]/ Adam Eivy09c3dcc62694
diff --git a/server/services/meatspaceAlcohol.js b/server/services/meatspaceAlcohol.js
index 56aa6f4ce4..99d20d0d2f 100644
--- a/server/services/meatspaceAlcohol.js
+++ b/server/services/meatspaceAlcohol.js
@@ -8,7 +8,7 @@
import { readFile } from 'fs/promises';
import { join } from 'path';
import { atomicWrite, PATHS, ensureDir, readJSONFile, getDateString } from '../lib/fileUtils.js';
-import { DAILY_LOG_FILE, loadMeatspaceDailyLog, mutateDailyLog } from './meatspaceDailyLog.js';
+import { loadMeatspaceDailyLog, mutateDailyLog } from './meatspaceDailyLog.js';
import {
isMortalLoomEnabled,
mlPush,
@@ -146,12 +146,6 @@ export function computeRollingAverages(entries, sex = 'male') {
*/
const loadDailyLog = (options) => loadMeatspaceDailyLog({ ...options, label: 'Alcohol' });
-async function saveDailyLog(log) {
- await ensureDir(MEATSPACE_DIR);
- await atomicWrite(DAILY_LOG_FILE, log);
- averageCache = null; // Invalidate cache
-}
-
// === Exported Service Functions ===
export async function getAlcoholSummary() {
@@ -223,7 +217,8 @@ export async function logDrink({ name, oz, abv, count = 1, date }) {
recalcAlcoholTotal(entry);
return { drink, standardDrinks, date: targetDate, dayTotal: entry.alcohol.standardDrinks };
}, { label: 'Alcohol' });
-
+
+ averageCache = null;
console.log(`🍺 Logged drink: ${name || 'unnamed'} ${oz}oz @ ${abv}% (${standardDrinks} std) on ${targetDate}`);
return result;
}
@@ -294,11 +289,17 @@ export async function updateDrink(date, index, updates) {
}
recalcAlcoholTotal(entry);
-
return { drink, dayTotal: entry.alcohol.standardDrinks };
}, { label: 'Alcohol' });
-
- console.log(`📝 Updated drink on ${date}[${index}]: ${result?.drink?.name || 'unnamed'} ${result?.drink?.oz}oz @ ${result?.drink?.abv}%`);
+
+ if (!result) return null;
+ averageCache = null;
+ const drinkLabel = `${result.drink?.name || 'unnamed'} ${result.drink?.oz}oz @ ${result.drink?.abv}%`;
+ if (result.date && result.date !== date) {
+ console.log(`📝 Moved drink from ${date}[${index}] to ${result.date}: ${drinkLabel}`);
+ } else {
+ console.log(`📝 Updated drink on ${date}[${index}]: ${drinkLabel}`);
+ }
return result;
}
@@ -318,11 +319,12 @@ export async function removeDrink(date, index) {
const removed = entry.alcohol.drinks.splice(index, 1)[0];
if (entry.alcohol.drinks.length === 0) delete entry.alcohol;
else recalcAlcoholTotal(entry);
-
return removed;
}, { label: 'Alcohol' });
-
- console.log(`🗑️ Removed drink from ${date}[${index}]: ${result?.name || 'unnamed'} ${result?.oz}oz @ ${result?.abv}%`);
+
+ if (!result) return null;
+ averageCache = null;
+ console.log(`🗑️ Removed drink from ${date}[${index}]: ${result.name || 'unnamed'} ${result.oz}oz @ ${result.abv}%`);
return result;
}
diff --git a/server/services/meatspaceDailyLog.js b/server/services/meatspaceDailyLog.js
index 52a170868d..ace1010d16 100644
--- a/server/services/meatspaceDailyLog.js
+++ b/server/services/meatspaceDailyLog.js
@@ -86,6 +86,8 @@ export async function loadMeatspaceDailyLog({ strict = false, label = 'MeatSpace
* 5. Atomically writing the updated log back to `DAILY_LOG_FILE`.
*
* @param {(log: { entries: object[], lastEntryDate: string|null }) => Promise<any>|any} mutatorFn
+ * Return `null` when the targeted record is absent. That skips the write so a
+ * miss cannot rewrite the file. Any other return, including `undefined`, persists.
* @param {{ label?: string }} [options]
* @returns {Promise<any>} The result of mutatorFn, or the updated log if mutatorFn returns undefined.
*/
@@ -93,9 +95,7 @@ export async function mutateDailyLog(mutatorFn, { label = 'MeatSpace' } = {}) {
return queueDailyLogWrite(async () => {
const log = await readLocalDailyLog({ strict: true, label });
const result = await mutatorFn(log);
- if (result === false || result === null) {
- return result;
- }
+ if (result === null) return result;
if (Array.isArray(log?.entries)) {
log.entries.sort((a, b) => (a.date || '').localeCompare(b.date || ''));
log.lastEntryDate = log.entries.length > 0 ? (log.entries[log.entries.length - 1]?.date || null) : null;
diff --git a/server/services/meatspaceDailyLog.test.js b/server/services/meatspaceDailyLog.test.js
index ee025d3ffc..79beeda799 100644
--- a/server/services/meatspaceDailyLog.test.js
+++ b/server/services/meatspaceDailyLog.test.js
@@ -1,18 +1,301 @@
-// This file tests the mutual exclusion and serialization capabilities that were added
-// to the meatspace daily logging system to fix race conditions between concurrent writes.
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { join } from 'path';
-// Actual test coverage for concurrent writing is done by integration tests in the
-// services that use this functionality (alcohol, nicotine, health).
+// The alcohol, nicotine, and body-composition services all read the same
+// daily-log.json. They used to each carry a byte-identical copy of the probe →
+// read → validate sequence; #4112 collapsed that into meatspaceDailyLog.js. These
+// tests pin the shared reader's behavior AND the delegation from the callers, so
+// the strict/#2726 semantics can't drift back apart.
-// The mutateDailyLog function provides:
-// 1. Serialization of write operations through a shared queue
-// 2. Strict read behavior to prevent silent data loss on transient I/O errors
-// 3. Atomic writes to prevent partial updates
+vi.mock('../lib/fileUtils.js', () => ({
+ tryReadFile: vi.fn().mockResolvedValue(null),
+ readJSONFile: vi.fn(),
+ getDateString: vi.fn(() => '2024-06-01'),
+ PATHS: {
+ root: '/mock',
+ data: '/mock/data',
+ meatspace: '/mock/data/meatspace'
+ },
+ ensureDir: vi.fn().mockResolvedValue(undefined),
+ atomicWrite: vi.fn().mockResolvedValue(undefined)
+}));
-// This is tested and demonstrated by the integration tests in:
-// - meatspaceAlcohol.test.js
-// - meatspaceNicotine.test.js
-// - meatspaceHealth.test.js
+vi.mock('./mortalLoomStore.js', () => ({
+ isMortalLoomEnabled: vi.fn().mockResolvedValue(false),
+ readDailyLogIfEnabled: vi.fn().mockResolvedValue(null),
+ mlArrayIfEnabled: vi.fn().mockResolvedValue(null),
+ mlPush: vi.fn(),
+ mlPatchById: vi.fn(),
+ mlRemoveById: vi.fn(),
+ mlIdAtDateIndex: vi.fn(),
+ mlUpsertHealthMetricByDate: vi.fn()
+}));
-// No direct unit test is needed here since this functionality is fully
-// exercised through the service-specific test suites that call the actual functions.
\ No newline at end of file
+import { atomicWrite, readJSONFile } from '../lib/fileUtils.js';
+import { readDailyLogIfEnabled } from './mortalLoomStore.js';
+import { DAILY_LOG_FILE, readLocalDailyLog, loadMeatspaceDailyLog } from './meatspaceDailyLog.js';
+import { getAlcoholSummary, getDailyAlcohol, logDrink, updateDrink } from './meatspaceAlcohol.js';
+import { getDailyNicotine, getNicotineSummary, logNicotine } from './meatspaceNicotine.js';
+import { addBodyEntry, getBodyHistory } from './meatspaceHealth.js';
+
+// Built with join() rather than a literal: the module builds it the same way, and
+// win32 separators would make a hardcoded POSIX path fail on the Windows runner.
+const EXPECTED_LOG_PATH = join('/mock/data/meatspace', 'daily-log.json');
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ readDailyLogIfEnabled.mockResolvedValue(null);
+});
+
+describe('DAILY_LOG_FILE', () => {
+ it('points at daily-log.json under the meatspace data dir', () => {
+ expect(DAILY_LOG_FILE).toBe(EXPECTED_LOG_PATH);
+ });
+});
+
+describe('readLocalDailyLog', () => {
+ it('reads the local mirror without consulting MortalLoom', async () => {
+ readJSONFile.mockResolvedValue({ entries: [{ date: '2024-03-02' }], lastEntryDate: '2024-03-02' });
+ const log = await readLocalDailyLog();
+ expect(log.entries).toEqual([{ date: '2024-03-02' }]);
+ expect(readDailyLogIfEnabled).not.toHaveBeenCalled();
+ });
+
+ it('passes the file, default, and allowArray:false through to readJSONFile', async () => {
+ readJSONFile.mockResolvedValue({ entries: [] });
+ await readLocalDailyLog({ strict: true, label: 'Alcohol' });
+ expect(readJSONFile).toHaveBeenCalledWith(
+ EXPECTED_LOG_PATH,
+ { entries: [], lastEntryDate: null },
+ { allowArray: false, strict: true }
+ );
+ });
+
+ it('returns the empty log when the file is absent', async () => {
+ readJSONFile.mockResolvedValue({ entries: [], lastEntryDate: null });
+ expect(await readLocalDailyLog()).toEqual({ entries: [], lastEntryDate: null });
+ });
+
+ it('hands back a fresh empty log each time so callers can mutate it', async () => {
+ readJSONFile.mockResolvedValue(null);
+ const first = await readLocalDailyLog();
+ first.entries.push({ date: '2024-01-01' });
+ const second = await readLocalDailyLog();
+ expect(second.entries).toEqual([]);
+ });
+
+ it('coerces a missing entries array to empty when not strict', async () => {
+ readJSONFile.mockResolvedValue({ lastEntryDate: '2024-01-01' });
+ expect(await readLocalDailyLog()).toEqual({ entries: [], lastEntryDate: '2024-01-01' });
+ });
+
+ it('substitutes the empty log for a non-object root when not strict', async () => {
+ readJSONFile.mockResolvedValue([{ date: '2024-01-01' }]);
+ expect(await readLocalDailyLog()).toEqual({ entries: [], lastEntryDate: null });
+ });
+
+ it('throws with the domain label for a non-object root under strict', async () => {
+ readJSONFile.mockResolvedValue([{ date: '2024-01-01' }]);
+ await expect(readLocalDailyLog({ strict: true, label: 'Health' }))
+ .rejects.toThrow(/Health daily log malformed/);
+ });
+
+ it('throws with the domain label for a non-array entries under strict', async () => {
+ readJSONFile.mockResolvedValue({ entries: 'nope' });
+ await expect(readLocalDailyLog({ strict: true, label: 'Nicotine' }))
+ .rejects.toThrow(/Nicotine daily log malformed/);
+ });
+
+ it('does not swallow a strict read failure raised by readJSONFile', async () => {
+ readJSONFile.mockRejectedValue(new Error('Unreadable JSON file: /mock/data/meatspace/daily-log.json'));
+ await expect(readLocalDailyLog({ strict: true })).rejects.toThrow(/Unreadable JSON file/);
+ });
+});
+
+describe('loadMeatspaceDailyLog', () => {
+ it('prefers the MortalLoom-composed log and skips the local read', async () => {
+ readDailyLogIfEnabled.mockResolvedValue({ entries: [{ date: '2024-05-05' }], lastEntryDate: '2024-05-05' });
+ readJSONFile.mockResolvedValue({ entries: [{ date: '1999-01-01' }] });
+ const log = await loadMeatspaceDailyLog();
+ expect(log.entries).toEqual([{ date: '2024-05-05' }]);
+ expect(readJSONFile).not.toHaveBeenCalled();
+ });
+
+ it('falls back to the local mirror when MortalLoom is off', async () => {
+ readDailyLogIfEnabled.mockResolvedValue(null);
+ readJSONFile.mockResolvedValue({ entries: [{ date: '2024-04-04' }], lastEntryDate: '2024-04-04' });
+ const log = await loadMeatspaceDailyLog();
+ expect(log.entries).toEqual([{ date: '2024-04-04' }]);
+ });
+
+ it('forwards strict to the MortalLoom probe', async () => {
+ readJSONFile.mockResolvedValue({ entries: [] });
+ await loadMeatspaceDailyLog({ strict: true });
+ expect(readDailyLogIfEnabled).toHaveBeenCalledWith({ strict: true });
+ });
+
+ it('propagates a strict MortalLoom failure instead of scoring a local empty', async () => {
+ readDailyLogIfEnabled.mockRejectedValue(new Error('MortalLoom store unreadable for daily log'));
+ readJSONFile.mockResolvedValue({ entries: [] });
+ await expect(loadMeatspaceDailyLog({ strict: true })).rejects.toThrow(/unreadable/i);
+ expect(readJSONFile).not.toHaveBeenCalled();
+ });
+});
+
+// The point of the extraction is that the three services actually go through it.
+// Each case asserts on a distinctive value or label that only the shared reader
+// (driven by the mocked readJSONFile) can produce, so a mock that stopped
+// intercepting would fail rather than pass against a default.
+describe('caller delegation (#4112)', () => {
+ it('routes alcohol reads through the shared reader', async () => {
+ readJSONFile.mockResolvedValue({
+ entries: [{ date: '2024-02-02', alcohol: { drinks: [{ name: 'Example Lager', oz: 12, abv: 5 }] } }]
+ });
+ const entries = await getDailyAlcohol();
+ expect(entries).toEqual([
+ { date: '2024-02-02', alcohol: { drinks: [{ name: 'Example Lager', oz: 12, abv: 5 }] } }
+ ]);
+ expect(readDailyLogIfEnabled).toHaveBeenCalled();
+ });
+
+ it('labels an alcohol strict failure as Alcohol', async () => {
+ readJSONFile.mockResolvedValue({ entries: 'nope' });
+ await expect(getDailyAlcohol(null, null, { strict: true }))
+ .rejects.toThrow(/Alcohol daily log malformed/);
+ });
+
+ it('routes nicotine reads through the shared reader', async () => {
+ readJSONFile.mockResolvedValue({
+ entries: [{ date: '2024-02-03', nicotine: { items: [{ product: 'Example Pouch', mgPerUnit: 3, count: 2 }], totalMg: 6 } }]
+ });
+ const entries = await getDailyNicotine();
+ expect(entries).toHaveLength(1);
+ expect(entries[0].nicotine.totalMg).toBe(6);
+ expect(readDailyLogIfEnabled).toHaveBeenCalled();
+ });
+
+ it('labels a nicotine strict failure as Nicotine', async () => {
+ readJSONFile.mockResolvedValue({ entries: 'nope' });
+ await expect(getDailyNicotine(null, null, { strict: true }))
+ .rejects.toThrow(/Nicotine daily log malformed/);
+ });
+
+ it('routes body history through the local reader, not the composed log', async () => {
+ readJSONFile.mockResolvedValue({
+ entries: [{ date: '2024-02-04', body: { weightLbs: 175 } }]
+ });
+ expect(await getBodyHistory()).toEqual([{ date: '2024-02-04', weightLbs: 175 }]);
+ // Body entries come from MortalLoom's own `bodyEntries` key, so the composed
+ // daily-log probe must stay out of this path (see meatspaceHealth.js).
+ expect(readDailyLogIfEnabled).not.toHaveBeenCalled();
+ });
+
+ it('labels a body-history strict failure as Health', async () => {
+ readJSONFile.mockResolvedValue({ entries: 'nope' });
+ await expect(getBodyHistory({ strict: true })).rejects.toThrow(/Health daily log malformed/);
+ });
+});
+
+// Concurrent alcohol, nicotine, and body writers share one file. The queue has to
+// hold the whole read-modify-write, and a miss or a bad read must not replace it.
+describe('serialized daily-log writes (#8032)', () => {
+ beforeEach(() => {
+ readJSONFile.mockReset();
+ atomicWrite.mockReset();
+ atomicWrite.mockResolvedValue(undefined);
+ });
+
+ function useLogStore(initial) {
+ let store = structuredClone(initial);
+ readJSONFile.mockImplementation(async () => structuredClone(store));
+ atomicWrite.mockImplementation(async (file, data) => {
+ if (String(file).endsWith('daily-log.json')) store = structuredClone(data);
+ });
+ return () => store;
+ }
+
+ it('keeps alcohol, nicotine, and body fields when the three writers overlap', async () => {
+ let store = {
+ entries: [{
+ date: '2024-01-01',
+ alcohol: { drinks: [{ name: 'Kept', oz: 12, abv: 5, count: 1 }], standardDrinks: 1 }
+ }],
+ lastEntryDate: '2024-01-01'
+ };
+ let activeReads = 0;
+ let maxActiveReads = 0;
+ readJSONFile.mockImplementation(async () => {
+ activeReads += 1;
+ maxActiveReads = Math.max(maxActiveReads, activeReads);
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const snapshot = structuredClone(store);
+ activeReads -= 1;
+ return snapshot;
+ });
+ atomicWrite.mockImplementation(async (file, data) => {
+ if (String(file).endsWith('daily-log.json')) store = structuredClone(data);
+ });
+
+ await Promise.all([
+ logDrink({ name: 'Example Lager', oz: 12, abv: 5, count: 1, date: '2024-06-01' }),
+ logNicotine({ product: 'Example Pouch', mgPerUnit: 3, count: 2, date: '2024-06-01' }),
+ addBodyEntry({ date: '2024-06-01', weightLbs: 175 })
+ ]);
+
+ expect(maxActiveReads).toBe(1);
+ const kept = store.entries.find((entry) => entry.date === '2024-01-01');
+ const day = store.entries.find((entry) => entry.date === '2024-06-01');
+ expect(kept.alcohol.drinks[0].name).toBe('Kept');
+ expect(day.alcohol.drinks).toEqual([
+ expect.objectContaining({ name: 'Example Lager', oz: 12, abv: 5, count: 1 })
+ ]);
+ expect(day.nicotine).toMatchObject({
+ items: [expect.objectContaining({ product: 'Example Pouch', mgPerUnit: 3, count: 2 })],
+ totalMg: 6
+ });
+ expect(day.body).toEqual({ weightLbs: 175 });
+ expect(store.lastEntryDate).toBe('2024-06-01');
+ });
+
+ it('does not rewrite the log when the drink index is missing', async () => {
+ readJSONFile.mockResolvedValue({
+ entries: [{
+ date: '2024-06-01',
+ alcohol: { drinks: [{ name: 'Example Lager', oz: 12, abv: 5, count: 1 }], standardDrinks: 1 }
+ }],
+ lastEntryDate: '2024-06-01'
+ });
+ await expect(updateDrink('2024-06-01', 3, { oz: 20 })).resolves.toBeNull();
+ expect(atomicWrite).not.toHaveBeenCalled();
+ });
+
+ it('does not write a body entry when the daily log is unreadable', async () => {
+ readJSONFile.mockRejectedValue(new Error('Unreadable JSON file: /mock/data/meatspace/daily-log.json'));
+ await expect(addBodyEntry({ date: '2024-06-01', weightLbs: 175 })).rejects.toThrow(/Unreadable JSON file/);
+ expect(atomicWrite).not.toHaveBeenCalled();
+ });
+
+ it('labels a body-write failure as Health and does not replace the log', async () => {
+ readJSONFile.mockResolvedValue({ entries: 'nope' });
+ await expect(addBodyEntry({ date: '2024-06-01', weightLbs: 175 }))
+ .rejects.toThrow(/Health daily log malformed/);
+ expect(atomicWrite).not.toHaveBeenCalled();
+ });
+
+ it('drops the alcohol summary cache after a local write', async () => {
+ const current = useLogStore({ entries: [], lastEntryDate: null });
+ await logDrink({ name: 'Example Lager', oz: 12, abv: 5, count: 1, date: '2024-06-01' });
+ expect((await getAlcoholSummary()).today).toBe(1);
+ await logDrink({ name: 'Example Wine', oz: 5, abv: 12, count: 1, date: '2024-06-01' });
+ expect((await getAlcoholSummary()).today).toBe(2);
+ expect(current().entries.find((entry) => entry.date === '2024-06-01').alcohol.drinks).toHaveLength(2);
+ });
+
+ it('drops the nicotine summary cache after a local write', async () => {
+ useLogStore({ entries: [], lastEntryDate: null });
+ await logNicotine({ product: 'Example Pouch', mgPerUnit: 3, count: 1, date: '2024-06-01' });
+ expect((await getNicotineSummary()).today).toBe(3);
+ await logNicotine({ product: 'Example Pouch', mgPerUnit: 4, count: 1, date: '2024-06-01' });
+ expect((await getNicotineSummary()).today).toBe(7);
+ });
+});
diff --git a/server/services/meatspaceHealth.js b/server/services/meatspaceHealth.js
index 8ac9ec007a..5b6a3664a3 100644
--- a/server/services/meatspaceHealth.js
+++ b/server/services/meatspaceHealth.js
@@ -8,7 +8,7 @@
import { join } from 'path';
import { atomicWrite, PATHS, ensureDir, readJSONFile, getDateString } from '../lib/fileUtils.js';
-import { DAILY_LOG_FILE, readLocalDailyLog, mutateDailyLog } from './meatspaceDailyLog.js';
+import { readLocalDailyLog, mutateDailyLog } from './meatspaceDailyLog.js';
import {
isMortalLoomEnabled,
mlArrayIfEnabled,
@@ -103,7 +103,7 @@ export async function addBodyEntry({ date, ...body }) {
entry.body = { ...(entry.body || {}), ...body };
return { date: targetDate, ...entry.body };
}, { label: 'Health' });
-
+
console.log(`⚖️ Body entry added for ${targetDate}`);
return result;
}
diff --git a/server/services/meatspaceNicotine.js b/server/services/meatspaceNicotine.js
index 850c78f4c6..45e3624466 100644
--- a/server/services/meatspaceNicotine.js
+++ b/server/services/meatspaceNicotine.js
@@ -7,7 +7,7 @@
import { join } from 'path';
import { atomicWrite, PATHS, ensureDir, readJSONFile, getDateString } from '../lib/fileUtils.js';
-import { DAILY_LOG_FILE, loadMeatspaceDailyLog, mutateDailyLog } from './meatspaceDailyLog.js';
+import { loadMeatspaceDailyLog, mutateDailyLog } from './meatspaceDailyLog.js';
import {
isMortalLoomEnabled,
mlPush,
@@ -105,12 +105,6 @@ function recalcDayTotal(entry) {
*/
const loadDailyLog = (options) => loadMeatspaceDailyLog({ ...options, label: 'Nicotine' });
-async function saveDailyLog(log) {
- await ensureDir(MEATSPACE_DIR);
- await atomicWrite(DAILY_LOG_FILE, log);
- averageCache = null;
-}
-
// === Exported Service Functions ===
export async function getNicotineSummary() {
@@ -177,7 +171,8 @@ export async function logNicotine({ product, mgPerUnit, count = 1, date }) {
return { item, totalMg, date: targetDate, dayTotal: entry.nicotine.totalMg };
}, { label: 'Nicotine' });
-
+
+ averageCache = null;
console.log(`🚬 Logged nicotine: ${product || 'unnamed'} ${mgPerUnit}mg x${count} (${totalMg}mg) on ${targetDate}`);
return result;
}
@@ -240,11 +235,17 @@ export async function updateNicotine(date, index, updates) {
}
recalcDayTotal(entry);
-
return { item, dayTotal: entry.nicotine.totalMg };
}, { label: 'Nicotine' });
-
- console.log(`📝 Updated nicotine on ${date}[${index}]: ${result?.item?.product || 'unnamed'} ${result?.item?.mgPerUnit}mg x${result?.item?.count}`);
+
+ if (!result) return null;
+ averageCache = null;
+ const itemLabel = `${result.item?.product || 'unnamed'} ${result.item?.mgPerUnit}mg x${result.item?.count}`;
+ if (result.date && result.date !== date) {
+ console.log(`📝 Moved nicotine from ${date}[${index}] to ${result.date}: ${itemLabel}`);
+ } else {
+ console.log(`📝 Updated nicotine on ${date}[${index}]: ${itemLabel}`);
+ }
return result;
}
@@ -264,11 +265,12 @@ export async function removeNicotine(date, index) {
const removed = entry.nicotine.items.splice(index, 1)[0];
if (entry.nicotine.items.length === 0) delete entry.nicotine;
else recalcDayTotal(entry);
-
return removed;
}, { label: 'Nicotine' });
-
- console.log(`🗑️ Removed nicotine from ${date}[${index}]: ${result?.product || 'unnamed'} ${result?.mgPerUnit}mg x${result?.count}`);
+
+ if (!result) return null;
+ averageCache = null;
+ console.log(`🗑️ Removed nicotine from ${date}[${index}]: ${result.product || 'unnamed'} ${result.mgPerUnit}mg x${result.count}`);
return result;
}