Skip to content

fix(pantry): refuse quantities and dates the file cannot read back - #753

Merged
LeMyst merged 2 commits into
cooklang:mainfrom
drawmeanelephant:fix/710-pantry-value-validation
Oct 22, 2026
Merged

LeMyst merged 2 commits into
cooklang:mainfrom
drawmeanelephant:fix/710-pantry-value-validation

Conversation

@drawmeanelephant

@drawmeanelephant drawmeanelephant commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #710

Summary

cook pantry add dairy milk --quantity garbage stored
milk = "garbage" and exited 0; --expire 32/13/2025 the same, through
update too. Nothing downstream can read those values: a quantity
PantryItem::parsed_quantity cannot parse is never subtracted from a
shopping list nor compared to low, and a date parse_date does not
accept never expires — so they sat in the file as junk every later read
quietly ignored.

Changes

Both in cookcli-core (pantry::add / pantry::update), so the
server's pantry writes — which go through core — get the same check:

  • New check_attribute_values runs before the document is opened.
    quantity and low are validated by the reader itself: the candidate
    value is handed to cooklang's PantryItem::parsed_quantity(), so
    what is accepted is exactly what the file reads back — 500%g,
    500 g, .5%kg, 2 — and unlim/unlimited are allowed as the
    documented "stocked, not measurable" spellings the seed pantry ships
    and the server treats specially. bought and expire must parse as
    a date in one of the spellings parse_date accepts.
  • update now normalizes its attributes the way add's request always
    has — trimmed, blank meaning unset — so --quantity " 500%g " stores
    500%g rather than a value unit_of could never match.
  • On refusal the error names the attribute and what to write, e.g.
    the expire date '32/13/2025' is not a date a pantry file can read back: write it like '2027-01-01'; nothing is written.
  • docs/api.md and the --quantity help now describe the validated
    contract (VALUE%UNIT, a plain count, or unlim/unlimited).

Test plan

  • add_refuses_an_attribute_nothing_could_read_back covers all four
    attributes and pins the file staying byte-identical.
  • update_refuses_an_attribute_nothing_could_read_back does the same
    for update.
  • add_accepts_the_quantities_and_dates_readers_use accepts unlim,
    unlimited, 2, 500%g, 500 g, .5%kg and every parse_date
    format.
  • add_accepts_unlimited_as_a_low_threshold covers low.
  • Live repro: cook pantry add dairy milk --quantity garbage → exit 1,
    file unchanged; --quantity "2%l" --expire 2025-12-01 still writes
    normally.
  • Full cargo test --all-features green.

Generated with Devin

@drawmeanelephant
drawmeanelephant force-pushed the fix/710-pantry-value-validation branch from 6fae444 to 4c9f938 Compare October 13, 2026 14:33

@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.

Thanks. Validating dates here is good: the check uses the same parse_date as expiring and days_until_expiry, so the accepted formats match exactly. The quantity check has two problems, though.

1. unlim / unlimited are now refused

These are documented, valid values:

  • they appear in seed/config/pantry.conf (lines 1, 11-13);
  • docs/api.md (around lines 873 and 913) describes the field as "any other string such as unlim";
  • cooklang's pantry subtraction and the server (src/server/handlers/shopping_list.rs:142) treat them specially.

With this PR, adding an item with quantity unlim returns "the quantity 'unlim' is not a quantity". CI stays green only because the server test that uses " unlim " (src/server/handlers/pantry.rs:587) goes through trimmed(), not through core.

2. The check doesn't match what reads the value

The PR validates against core's QUANTITY regex (crates/core/src/pantry/mod.rs:1331). Its unit group is (.*), so it accepts any string that starts with a digit. But the shopping list and is_low read quantities through cooklang's parse_quantity, which replaces % with a space and runs f64::parse on the first word.

Value Accepted here Readable by parse_quantity
500g yes no: never subtracted (pantry flour = "500g" leaves flour 600 g on the list)
1/2%cup yes no
1,5%kg yes no
.5%kg no yes

So the kind of value #710 is about can still be stored, and some readable values are refused.

Suggestion: validate with cooklang's own parsing. Build a PantryItem and call parsed_quantity(), as PantryItem::is_low does around mod.rs:129, and let unlim / unlimited through. Then update docs/api.md and the --quantity help in src/pantry.rs (lines 171, 183, 205) to match. If the free-form string promised by the docs should stay, the softer alternative is to store the value and return a warning in the outcome's diagnostics.

Tests

The tests only cover refusals. Cases that must be accepted, such as unlim, 2, 500%g, 500 g and each date format, would catch over-strictness like point 1.

Minor: update now trims its inputs, so --quantity " " becomes "no attributes given". That seems fine.

This PR merges cleanly on top of #748.


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

@drawmeanelephant
drawmeanelephant force-pushed the fix/710-pantry-value-validation branch from 4c9f938 to 766b1e7 Compare October 14, 2026 12:26
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Reworked the validation to use the reader itself rather than a parallel regex: the candidate value now goes through cooklang's PantryItem::parsed_quantity(), so accepted == readable. unlim/unlimited are explicitly allowed (the seed pantry ships them and the server special-cases them), 500 g/.5%kg are accepted, and 500g/1/2%cup/1,5%kg are refused since nothing could parse them back. Added acceptance tests for the valid spellings and every parse_date format alongside the refusal tests, and updated docs/api.md + --quantity help to describe the contract.

@drawmeanelephant
drawmeanelephant force-pushed the fix/710-pantry-value-validation branch from 766b1e7 to 6e3042d Compare October 14, 2026 13:35
@LeMyst

LeMyst commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks, this is much better. unlim/unlimited go through again, 500g/1/2%cup/1,5%kg are refused with a clear 400 from the API, and the seed pantry still loads and edits through the CLI and the web page. A few things remain:

  1. Core still has a second quantity reader that disagrees with what is now accepted: the QUANTITY regex (crates/core/src/pantry/mod.rs:1345), used by is_out, unit_of and is_low_quantity, so by depleted and the stock dots on the pantry page. With .3%kg and 0.3%kg in the pantry, cook pantry depleted lists only 0.3%kg. .0 is never "out", and 1e2%g is read as 1 of unit e2%g. Routing those through parsed_quantity, as is_low already does, would leave one reader.
  2. f64::parse also accepts nan, inf and negatives. With flour = "nan%g", flour disappears from the shopping list entirely (600 - NaN > 0 is false, so it counts as covered). Checking value.is_finite() && value >= 0.0 in check_attribute_values would close that.
  3. Optional: --expire 27-01-02 and 1/2/27 are stored as year 27 ("EXPIRED 730405 days ago"). That matches the reader, but it is almost always a typo.
  4. Minor: low = "unlim" is accepted (tests.rs:1634), but a threshold that never compares is the kind of value pantry add and update persist unvalidated quantity/expire/bought/low values #710 is about. The refusal tests also don't include the pantry add and update persist unvalidated quantity/expire/bought/low values #710 values themselves (500g, 1/2%cup, 1,5%kg). The PUT table in docs/api.md (lines 948-951) doesn't mention the new 400 either.

The macOS CI failure (shopping_list_events_test::writing_announces_which_file_changed) looks like a file-watch timing flake unrelated to this change; a rerun should clear it.


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

@drawmeanelephant
drawmeanelephant force-pushed the fix/710-pantry-value-validation branch from 6e3042d to a2dbd51 Compare October 14, 2026 17:08
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Done on a2dbd51, all four points:

  • One reader: QUANTITY regex is gone. is_out, unit_of and is_low_quantity all go through parsed_quantity(), which delegates to cooklang::pantry::PantryItem::parsed_quantity() — .3%kg == 0.3%kg now, 1e2%g is 100 g, .0%g is out.
  • Finite, non-negative: quantity/low must parse through that reader to an amount.is_finite() && amount >= 0.0 — nan%g, inf%g, -5%g are refused.
  • Two-digit years: parse_date hits now require year() >= 100, so 27-01-02 and 1/2/27 are refused.
  • low: unlim/unlimited are accepted only under quantity; as a threshold they are refused.

Tests: refusal table now covers 500g, 1/2%cup, 1,5%kg, nan%g, inf%g, -5%g, low: unlim, expire: 27-01-02 (file untouched on each), a depleted parity test covers the reader spellings, and the old low: unlimited acceptance test is flipped to refusal. docs/api.md regenerated — the PUT table names the 400 and points at the accepted forms.

@LeMyst

LeMyst commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

Thanks, this addresses all four points. Checked on a2dbd51:

  • With one reader, .3%kg, .0, 1e2%g and 50 g extra are listed by cook pantry depleted and get the right stock dots on the pantry page.
  • nan, inf, -5, two-digit years and low = "unlim" are refused by both the CLI and the API (400).
  • unlim/unlimited and all six date formats still go through, and the seed pantry still loads and edits.
  • The new refusal test fails when the fix is reverted.

A few small things are left:

  1. -0 still gets through. -0.0 >= 0.0 is true at crates/core/src/pantry/mod.rs:851, so these are all stored with the minus sign:

    • cook pantry add store x --quantity=-0
    • -0%g
    • --low=-0
    • PUT {"quantity":"-0"}

    It reads as 0, so nothing breaks, but adding !amount.is_sign_negative() would make "non-negative" true.

  2. The low refusal suggests what it just refused. --low unlim is refused with "…such as 500%g or 3, or unlim". That or \unlim` (mod.rs:859) should only appear for quantity`.

  3. Silent behaviour change for files written by hand. A quantity parsed_quantity can't read no longer counts for depleted or the stock dots. With butter = "0g", main's cook pantry depleted lists it, but this PR doesn't, and the pantry page shows it with a green "in stock" dot. That matches the shopping list, which never subtracted it, but it happens without any notice. A load warning for an unreadable quantity (other than unlim/unlimited), or a sentence in docs/pantry.md, would make it visible.

  4. Nit: add_accepts_the_quantities_and_dates_readers_use never reaches the %m/%d/%Y format, because 01/02/2026 is read as %d/%m/%Y first. A date like 01/13/2026 would cover it.

For the record, a pantry file edited by hand with flour = "nan%g" still drops flour from the shopping list. That's in cooklang's own subtraction, not here, so it's out of scope for this PR.

fmt, clippy and cargo test --workspace are clean, CI is green, and it merges cleanly with today's main, as do the core tests on a local merge.


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

@drawmeanelephant
drawmeanelephant force-pushed the fix/710-pantry-value-validation branch from a2dbd51 to b10f6e7 Compare October 17, 2026 16:48
`add` and `update` wrote whatever a request carried: `--quantity garbage`
landed as `milk = "garbage"`, and a `32/13/2025` expiry the same way.
Nothing can read them back — the quantity is never subtracted from a
shopping list nor judged against `low`, the date never expires — so they
were junk the next read quietly ignored.

The attributes a request sets are now checked before the document is
opened: `quantity` and `low` against the pattern the file parses them
with, `bought` and `expire` against the date spellings `parse_date`
accepts. `update` also trims its attributes and drops blanks, the way
`add`'s request always has. Nothing is written on a refusal, and the
server's pantry writes are covered by the same check since they go
through core.

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

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@drawmeanelephant
drawmeanelephant force-pushed the fix/710-pantry-value-validation branch from b10f6e7 to c464f31 Compare October 17, 2026 16:48
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

All four on c464f31:

  1. -0: check_attribute_values now also requires !amount.is_sign_negative(), so -0, -0%g and --low=-0 are all refused. Covered in add_refuses_quantities_and_dates_that_parse_to_nonsense.
  2. low wording: the or unlim`` suggestion is emitted only for quantity; `low` refusals no longer suggest what they just refused. New test `a_refused_low_does_not_suggest_it`.
  3. Unreadable values are visible: load() now emits a warning diagnostic per quantity/low that nothing can read back ('flour' has a quantity that cannot be read back: 0g), located at the file. unlim/unlimited are excluded as deliberate spellings. Plus a sentence in docs/pantry.md. New test load_warns_on_a_value_nothing_can_read_back.
  4. Date coverage: the expire fixture is now 01/13/2026 — month 13 can't be a day, so only %m/%d/%Y reads it.

On the nan%g hand-edit: agreed, that's cooklang's subtraction, out of scope here.

@LeMyst

LeMyst commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Thanks! Points 1, 2 and 4 are fixed. Checked on c464f31:

  • -0, -0%g, -0.0 and --low=-0 are refused by the CLI and the API (400), while 0, 0%g and 0.0 still go through.
  • The low refusal no longer offers unlim.
  • 01/13/2026 really goes through %m/%d/%Y.
  • Each new test fails when its fix is reverted.
  • Nothing from earlier rounds has regressed: nan, inf, -5, two-digit years and low = "unlim" are still refused, and unlim/unlimited, the six date formats and the seed pantry still work.

One thing is left, on point 3:

  1. The new warning never reaches anyone. load() adds it (crates/core/src/pantry/mod.rs:276-286), but every reader drops outcome.diagnostics:

    • cook pantry list, depleted, expiring, recipes and plan end in .into_value() (src/pantry.rs:328, :390, :464, :560, :709).
    • The pantry page uses only outcome.value (src/server/ui.rs:873-874).
    • cook doctor reads the file on its own.

    With butter = "0g", cook pantry list and depleted print nothing about it, and butter still gets a green dot on the pantry page. So the new sentence in docs/pantry.md, "cook pantry warns about such values when the file loads", isn't true yet. Either of these would close it:

    • Reword the sentence so it doesn't promise a warning. The rest of it holds on its own.
    • Show the diagnostics in the read commands. log_diagnostics (src/pantry.rs:14), which add/remove/update already use, would do it. Two things to watch if you go this way. The seed pantry ships quantity = "some" twice (seed/config/pantry.conf:32-33), so every seed user would get two warnings. Also, the value goes into the message raw, so a hand-edited "x\nWARN …" prints as two lines; {value:?} avoids that.

Nit: the comment at crates/core/src/pantry/tests.rs:1702 has month and day swapped. 13 can be a day, but it isn't a valid month.

fmt, clippy and cargo test --workspace are clean, CI is green, and it merges cleanly with today's main, where the core tests also pass.


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

`load()` warned about a `quantity`/`low` nothing can read back, but the
warning went nowhere: `list`, `depleted`, `expiring`, `recipes` and
`plan` all ended in `into_value()`, and the pantry page kept only
`outcome.value`. A `butter = "0g"` still looked silently stocked.

The read commands now run their outcome through `log_diagnostics`, as
`add`/`remove`/`update` already did, and the pantry page warns them to
the server log. The message quotes the value with `{value:?}` so a
hand-edited newline cannot forge a second warning line. The seed
pantry's `quantity = "some"` would warn every seed user twice, so the
fennel seeds and cinnamon get readable amounts.

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

Done on 50c316d:

  • The warning reaches the reads: list, depleted, expiring, recipes and plan now run their outcome through log_diagnostics, the same as add/remove/update, and the pantry page warns them to the server log. With butter = "0g", cook pantry list and depleted print WARN …/pantry.conf: 'butter' has a quantity that cannot be read back: "0g".
  • Forged lines: the value is quoted with {value:?}, so "x\nWARN …" stays one escaped line — covered by the load warning test.
  • Seed pantry: quantity = "some" on fennel seeds and cinnamon would warn every seed user twice; they now carry readable amounts (30%g, 50%g).
  • Nit: the comment now says 13 is a day but not a month.

@LeMyst

LeMyst commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks, point 3 is closed. Checked on 50c316d with butter = "0g":

  • cook pantry list, depleted, expiring and recipes print WARN …/pantry.conf: 'butter' has a quantity that cannot be read back: "0g" on stderr, so -f json output on stdout stays clean.
  • The pantry page logs it on the server.
  • The seed pantry no longer warns, and the docs/pantry.md sentence is now true.
  • A value with a newline stays one escaped line, and the month/day comment is fixed.

(plan doesn't print the warning, but that's right: it never reads the pantry.)

Two small things, neither blocking:

  1. The item name is still unquoted. Only the value got {value:?}; '{}' with item.name (crates/core/src/pantry/mod.rs:283-285) is still raw. The API refuses control characters in names, but PUT /api/pantry/raw (shopper and up) and hand edits don't. So "x\nWARN forged" = "0g" still prints a second line starting with WARN forged, in the CLI and in the server log. {:?} on the name too would close it.
  2. The pantry page logs on every view. Three loads of /pantry give three identical warnings. That's only noise, and the CLI does the same per run, so fine as is if you'd rather keep it simple.

fmt, clippy and cargo test --workspace are clean, CI is green, and it merges cleanly with today's main.


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

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 and update persist unvalidated quantity/expire/bought/low values

2 participants