Repository navigation
fix(quick-add): retry Add Food dialog until details load; never overwrite existing entries - #41
Conversation
…erwrite existing entries - Poll for the selected food's details inside the Add Food dialog (10s, 1.5s closed grace) instead of a single 1s check; re-open and re-select up to 3 times when the dialog closes early or stalls. - Scope the serving-size input and Add to Diary button lookups to the dialog so an early close can no longer edit an existing diary entry. - Quick-add counts the macro's diary entries before/after and fails unless a new entry appeared; refuses to retry if an early close already changed the diary (avoids duplicates). - Raise the per-macro quick-add budget to 150s and the Kernel request/session timeouts to match. Co-authored-by: tembo[bot] <208362400+tembo-io[bot]@users.noreply.github.com>
| ? `'${addToDiaryError}'` | ||
| : `'${addToDiaryError} for "' + ${itemNameVar} + '"'`; | ||
|
|
||
| const dialogClosedError = `'Add Food dialog closed before details loaded after selecting "' + ${foodNameVar} + '"'`; |
There was a problem hiding this comment.
Every other dialog error here gets ${errorPrefix}, but this one doesn't. Since it's now built into dialogError and returned with the attempts suffix, quick-add and add-custom-food will report it without their prefix.
| const dialogClosedError = `'Add Food dialog closed before details loaded after selecting "' + ${foodNameVar} + '"'`; | |
| const dialogClosedError = `'${errorPrefix}Add Food dialog closed before details loaded after selecting "' + ${foodNameVar} + '"'`; |
There was a problem hiding this comment.
Fixed — the dialog-closed error now includes ${errorPrefix} like the other dialog errors (covered by a new test).
| verifyNewEntry | ||
| ? ` // If the early close logged the food anyway, re-adding would duplicate it | ||
| const entriesNow = await countDiaryEntries(${foodNameVar}); | ||
| if (entriesNow !== entriesBefore) { |
There was a problem hiding this comment.
The duplicate guard only exists when verifyNewEntry is on, but log and add --log now go through the same retry loop. If the dialog closes early and commits the food (the case this guard is for), those paths re-open the dialog and add it a second time without any warning. Before this PR they just failed. Could the before/after count be on whenever retries are possible, with verifyNewEntry controlling only the post-add check? Another option is to skip retries for the closed state when the count isn't available.
There was a problem hiding this comment.
Fixed — the pre-add diary entry count and the 'diary changed, not retrying' guard now always run, so log / add --log retries can't duplicate an early-committed entry; verifyNewEntry only controls the post-add check.
| ${FIND_ADD_FOOD_DIALOG} | ||
| const dialog = findAddFoodDialog(); | ||
| let count = 0; | ||
| for (const el of document.body.querySelectorAll('*')) { |
There was a problem hiding this comment.
This reads textContent on every element in the document, including body and large containers, so each call builds the full page text many times over. It runs every 500ms for up to 10s after Add to Diary, on a GWT diary DOM that can be big. A text-node TreeWalker (as in findAddFoodDialog) gives the deepest element directly and does less work. For example, walk the SHOW_TEXT nodes whose trimmed text is name, then count each distinct parentElement that is shown and not inside dialog.
There was a problem hiding this comment.
Fixed — countDiaryEntries now walks SHOW_TEXT nodes matching the name and counts distinct shown parent elements outside the dialog, instead of reading textContent on every element.
- Prefix the dialog-closed error with errorPrefix like other dialog errors. - Always snapshot diary entry count before opening the dialog so log/add retries never re-add a food an early close already committed; verifyNewEntry now only controls the post-add check. - Count diary entries with a text-node TreeWalker instead of reading textContent on every element. Co-authored-by: tembo[bot] <208362400+tembo-io[bot]@users.noreply.github.com>
Summary
Fixes intermittent
crono quick-addfailures:It also fixes a likely cause of an earlier quick-add entry being overwritten or replaced.
Root cause
Changes
src/kernel/food-dialog.ts): polls for up to 10s until the Serving Size input is present inside the dialog. The dialog only counts as closed after it has been gone for 1.5s, so a GWT re-render doesn't trigger a failure. If the dialog closes early, stalls, doesn't appear, or has no search bar, it presses Escape, re-opens the dialog and re-selects the food, up to 3 attempts. Errors now end with(after 3 attempts).verifyNewEntryoption, enabled for quick-add): counts the macro's diary entries before and after adding. The run fails unless the count went up, with an error that says an existing entry may have been modified. Before a retry, if the early close already changed the diary, it stops instead of re-adding, so no duplicate is created.CLAUDE.mdUI flow updated.Tests
tests/kernel/quick-add-dialog-retry.test.tsruns the generated quick-add script against a fake Playwright page with a simulated clock:loadingand shortclosedstates before entering the amounttests/automation/runner.test.ts.tsc,eslintandprettier --checkpass.vitestpasses all 263 tests. In the sandbox,credentials.test.tsfails on unmodifiedmainbecause the env varsKERNEL_API_KEYandCRONO_CRONOMETER_*are set; it passes with them unset.Notes
log/add --logget the retries and the dialog-scoped controls, but not the entry count check.