Skip to content

fix(pantry): refuse add collisions with a different kind of entry - #748

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/695-pantry-kind-clobber
Oct 17, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/695-pantry-kind-clobber

Conversation

@drawmeanelephant

Copy link
Copy Markdown
Contributor

Closes #695

Summary

pantry add only checked item_exists, which sees items — but general names share the TOML document root with sections, and section names share it with top-level items. Two collisions slipped through and insert replaced the existing entry wholesale:

  • pantry add general milk --quantity 3%l with a [milk] section present wiped the whole section, leaving milk = "3%l".
  • pantry add fridge milk with a top-level fridge = "2%l" opened [fridge] over the item (and likewise replaced [[fridge]] arrays).

remove, update and rename_section already refuse these collisions by not finding what they look for; add now refuses too via edit::name_taken_differently, which reports a root key held by the other kind of entry. A plain array of names still passes through to normalise_array_section as before.

Test plan

  • Repro: [milk] carton = "2%l" + cook pantry add general milk --quantity 3%l now errors a section named 'milk' already exists, exit 1, file unchanged.
  • Reverse: fridge = "2%l" + cook pantry add fridge milk errors a top-level item named 'fridge' already exists, file unchanged.
  • Three regression tests cover the general-vs-section, section-vs-item and [[array]] shapes; all 437 cookcli-core tests pass.

Generated with Devin

@LeMyst LeMyst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me. A [milk] table, an inline milk = { quantity = … } and [[fridge]] are each refused with the file left untouched, and a list-of-names section (including an empty fridge = []) is still converted and added to. The server returns 400, because the message doesn't contain " not found". The tests compare the file byte for byte, which is good.

  • Nit (pre-existing): pantry add general milk when the file has [[milk]] is refused by the existing check with the misleading "item 'milk' already exists in section 'general'". No data is lost.
  • Optional: a test for the inline-table collision in general.

🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

`add` only checked `item_exists`, which sees items — but `general` names
share the document root with sections and section names share it with
top-level items. `pantry add general milk` inserted over `[milk]`, and
`pantry add fridge milk` opened `[fridge]` over `fridge = "2%l"`,
destroying the entry wholesale. `remove` and `update` already refuse
these by not finding what they look for.

`name_taken_differently` now reports a root key held by the other kind:
a section (or `[[array]]` of tables) where a `general` item would go,
or a top-level item where a section would. A plain array of names still
passes through to `normalise_array_section` as before.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Fixed the nit properly rather than only testing it: [[milk]] + pantry add general milk now hits name_taken_differently first and reports "a section named 'milk' already exists" instead of the misleading item message — an array of tables isn't is_table_like, so the check order mattered. Covered by add_refuses_a_general_item_named_like_an_array_of_tables.

@drawmeanelephant
drawmeanelephant force-pushed the fix/695-pantry-kind-clobber branch from 2bb2cf2 to f64eb3a Compare October 14, 2026 13:32
@LeMyst

LeMyst commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

Thanks, fixing the check order is better than only testing it. [[milk]] plus pantry add general milk now reports "a section named 'milk' already exists", because name_taken_differently runs before item_exists and an array of tables counts as a section. add_refuses_a_general_item_named_like_an_array_of_tables (crates/core/src/pantry/tests.rs:1378) pins it. CI is green on f64eb3a, so this looks good to merge.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

@LeMyst
LeMyst merged commit b3d738d into cooklang:main Oct 17, 2026
5 checks passed
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.

pantry add silently destroys an existing same-named entry of a different kind

2 participants