Skip to content

fix(quick-add): retry Add Food dialog until details load; never overwrite existing entries - #41

Merged
milldr merged 2 commits into
mainfrom
tembo/quick-add-dialog-retry
Nov 4, 2026
Merged

milldr merged 2 commits into
mainfrom
tembo/quick-add-dialog-retry

Conversation

@milldr

@milldr milldr commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes intermittent crono quick-add failures:

Could not confirm completion: Quick add failed: Add Food dialog closed before details loaded after selecting "Quick Add, Fat"

It also fixes a likely cause of an earlier quick-add entry being overwritten or replaced.

Root cause

  • After pressing Enter on the search result, the flow waited a fixed 1s and checked for the dialog only once, with no retry. A slow or briefly re-rendering GWT dialog failed the whole run partway through, after earlier macros were already saved.
  • The serving-size input and the Add to Diary button were looked up across the entire page, not inside the Add Food dialog. When the dialog closed early, those lookups could hit controls belonging to the diary behind it. That could edit an existing entry, such as the earlier partial 5g fat entry, instead of adding a new one.

Changes

  • Waits for details, with retries (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).
  • Dialog-scoped controls: the serving-size fill and the Add to Diary click only act on elements inside the Add Food dialog. If the dialog is gone they fail instead of touching the diary.
  • New-entry verification (new verifyNewEntry option, 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.
  • Timeouts: the quick-add budget per macro is raised from 60s to 150s to cover the retries. The Kernel request timeout goes to 20 min and the browser session to 1200s, so both stay above the maximum (840s).
  • CLAUDE.md UI flow updated.

Tests

  • New tests/kernel/quick-add-dialog-retry.test.ts runs the generated quick-add script against a fake Playwright page with a simulated clock:
    • waits through loading and short closed states before entering the amount
    • re-opens and re-selects when the dialog closes early, and still succeeds
    • reports an error after 3 failed attempts without entering an amount
    • does not retry when an early close already changed the diary
    • adding 15g fat next to an existing fat entry creates a new entry
    • fails when Add to Diary doesn't create a new entry (the overwrite case)
    • the serving-size input and Add to Diary button are only looked up inside the dialog
  • Updated the timeout expectations in tests/automation/runner.test.ts.
  • tsc, eslint and prettier --check pass. vitest passes all 263 tests. In the sandbox, credentials.test.ts fails on unmodified main because the env vars KERNEL_API_KEY and CRONO_CRONOMETER_* are set; it passes with them unset.

Notes

  • Not tested against live Cronometer. The dialog lookup prefers GWT dialog/popup containers and falls back to the nearest ancestor of the dialog title that contains an input. Diary entries are counted by elements whose text exactly matches the food name, outside the dialog. Only the before/after difference matters.
  • New-entry verification is only enabled for quick-add. log / add --log get the retries and the dialog-scoped controls, but not the entry count check.

Want tembo to make any changes? Add a comment with @tembo and i'll get back to work!

View on Tembo  Review in Tembo  View Agent Settings

…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>
@tembo tembo Bot added the tembo Pull request created by Tembo label Nov 4, 2026
Comment thread src/kernel/food-dialog.ts Outdated
? `'${addToDiaryError}'`
: `'${addToDiaryError} for "' + ${itemNameVar} + '"'`;

const dialogClosedError = `'Add Food dialog closed before details loaded after selecting "' + ${foodNameVar} + '"'`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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} + '"'`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — the dialog-closed error now includes ${errorPrefix} like the other dialog errors (covered by a new test).

Comment thread src/kernel/food-dialog.ts
verifyNewEntry
? ` // If the early close logged the food anyway, re-adding would duplicate it
const entriesNow = await countDiaryEntries(${foodNameVar});
if (entriesNow !== entriesBefore) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/kernel/food-dialog.ts Outdated
${FIND_ADD_FOOD_DIALOG}
const dialog = findAddFoodDialog();
let count = 0;
for (const el of document.body.querySelectorAll('*')) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@milldr
milldr merged commit 531a9cb into main Nov 4, 2026
3 checks passed
@milldr
milldr deleted the tembo/quick-add-dialog-retry branch November 4, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tembo Pull request created by Tembo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant