Skip to content

Add update_food_entry tool to edit or move a logged entry - #76

Closed
paultristanwagner wants to merge 1 commit into
rwestergren:mainfrom
paultristanwagner:edit-diary-entry
Closed

paultristanwagner wants to merge 1 commit into
rwestergren:mainfrom
paultristanwagner:edit-diary-entry

Conversation

@paultristanwagner

Copy link
Copy Markdown
Contributor

All code in this PR was written by an AI agent (Claude Opus 5.5) working with me; I directed it and tested the results on my own account. Feel free to take, change or drop any part of it.

Part of #73.

Adds an update_food_entry tool: change a logged entry's amount or
measure, or move it to another meal. Until now the only way was to remove
the entry and log it again.

The app changes an amount with POST /api/v2/edit_serving, sending the whole serving back with the
new grams:

{"serving": {"servingId": …, "day": …, "order": 65537, "foodId": …,
             "measureId": …, "grams": 400.0, …},
 "auth": {…}, "config": {"call_version": 2}}

The meal group lives in the high bits of order, as add_serving already
builds it ((group << 16) | position). The app moves an entry with a
separate POST /api/v2/update_order {"diaryEntries": [<serving with new order>]}, but edit_serving accepts a new order too, so one call does
both. Verified against the live API on 1900-01-02: amount, meal and both at
once.

Changes:

  • client: update_serving(serving_id, day, *, grams, measure_id, diary_group) reads the serving from get_diary, changes the given
    fields and sends it back. A moved entry goes to the end of its new meal;
    staying in the same meal keeps its position. Arguments are validated
    before any request.
  • server: update_food_entry tool. The meal-name map that
    add_food_entry built inline is now a module constant both tools use.
  • README: tool table row.

https://claude.ai/code/session_01MCJFDo71qoSqTRtcGfkpXJ

The app changes a serving's amount with /api/v2/edit_serving, re-sending
the whole serving. The meal group lives in the high bits of `order`, and
edit_serving accepts a new order too, so one call changes the amount,
the measure and the meal.

Claude-Session: https://claude.ai/code/session_01MCJFDo71qoSqTRtcGfkpXJ
@paultristanwagner paultristanwagner mentioned this pull request Oct 16, 2026
8 of 9 tasks

serving = dict(serving)
if grams is not None:
serving["grams"] = grams

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Cronometer's API uses grams misleadingly - for regular foods and weight-based recipes, it holds actual grams.

For serving-based recipes, it holds a reference-serving count instead. Editing such an entry to grams=100 therefore sends 100 reference servings.

Take a look at enrich_diary_servings which already handles this. It may be useful to extract that shared measure logic to a small helper.

@jorgebaptista

Copy link
Copy Markdown
Contributor

This conflicts with current main, because #88 and #92 replaced the hard-coded group map with account-specific groups. Locally, I resolved it this way:

  • update_food_entry resolves diary_group with client.resolve_diary_group, so custom groups work (for example a "Night" group, number 5), and rejects "auto" for a move.
  • update_serving accepts any 16-bit group number instead of 1-4.
  • It echoes the target group like add_food_entry does.
  • I added server-level tests for a custom group and for "auto".

The full suite passes, with ruff clean. I'm happy to share the patch if it helps.

@rwestergren

Copy link
Copy Markdown
Owner

A clean patch / conflict resolution would be much appreciated

@jorgebaptista

Copy link
Copy Markdown
Contributor

Opened #96 with the conflict resolved against current main. It keeps this PR's commit and adds the diary-group resolution and tests.

rwestergren added a commit that referenced this pull request Nov 25, 2026
* Add update_food_entry tool to edit or move a logged entry

The app changes a serving's amount with /api/v2/edit_serving, re-sending
the whole serving. The meal group lives in the high bits of `order`, and
edit_serving accepts a new order too, so one call changes the amount,
the measure and the meal.

Claude-Session: https://claude.ai/code/session_01MCJFDo71qoSqTRtcGfkpXJ

* fix: accept any account diary group in update_serving

update_serving limited diary_group to 1-4, the old default layout. Groups now
come from resolve_diary_group, so any enabled group is valid (for example a
custom group 5). Add server-level tests for moving to a named custom group and
for rejecting "auto".

* Fix recipe amount validation and concurrent diary edits

Reject gram-weight edits for serving-based recipes, whose API amounts are reference-serving counts. Keep measure and group edits available and document the restriction.

Lock each serving's full fetch-modify-save cycle so concurrent partial edits preserve each other's changes. Cover both fixes with regression tests.

---------

Co-authored-by: Paul Tristan Wagner <paultristanwagner@gmail.com>
Co-authored-by: Randy Westergren <rawestergren@gmail.com>
@rwestergren

Copy link
Copy Markdown
Owner

Addressed by #96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants