Skip to content

fix: report non-numeric timer values in doctor - #614

Merged
LeMyst merged 4 commits into
cooklang:mainfrom
mvanhorn:fix/214-doctor-nonnumeric-timers
Oct 7, 2026
Merged

LeMyst merged 4 commits into
cooklang:mainfrom
mvanhorn:fix/214-doctor-nonnumeric-timers

Conversation

@mvanhorn

@mvanhorn mvanhorn commented Oct 2, 2026 •

Copy link
Copy Markdown

Fixes #214

cook doctor validate now warns when a timer's quantity is text, such as ~{a few%minutes} or ~{overnight}.

How it works:

  • Validation uses the shared PARSER, so nothing changes for other commands, and unit spellings such as ~{1%hr} or ~{10%Minutes} are not reported.
  • A timer whose quantity value is text (is_text()) is reported as a warning, not an error, so the recipe is still produced and its references are still checked.
  • The warning's span comes from cooklang::parser::PullParser (Event::Timer, quantity.value.span()), so it points at the timer's own value, including in recipes with YAML frontmatter.
  • Text that is still a number is skipped: ranges such as ~{10-20%minutes} and fractions such as ~{½%hour}. The bundled seed/Shared/Pizza Dough.cook no longer gets the warning.
  • The rendered warning has a Warning: label and the file and line, for example Warning: Timer value is text: a few (dish.cook:1).
  • docs/doctor.md describes the check.

Behavior change: --strict fails on warnings as well as errors, so a collection with a textual timer such as ~{overnight} that passed cook doctor validate --strict before now exits 1.

Tests cover textual, numeric, range and fraction timers, span placement (an ingredient with the same text before the timer, collapsed whitespace, a { inside frontmatter), the rendered label and line, and --strict.

Doctor validation now parses with its own parser that enables advanced
units and range values, backed by a converter that knows the standard
time units (second, minute, hour, day, and their symbols and aliases)
plus placeholder units for the other physical quantities the builder
requires. A textual timer quantity and an unknown timer unit are
reported; a number, decimal, or numeric range with a known time unit is
accepted, and a bare number with no unit is a warning. A named timer
with no quantity stays valid because the timer-requires-time extension
remains off. The shared parser is unchanged, so every other command
still accepts a textual timer, and the doctor docs and changelog
describe the check.

Fixes cooklang#214

@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 for picking up #214. I read the diff against cooklang 0.18.7 (the locked version); I did not build the branch, so please treat the scenarios below as read from the code rather than reproduced.

The two I'd address before merging are the timer unit table (valid units such as hr or Minutes become errors) and the loss of reference checking when a timer error is present. Most of the rest would go away with a lighter approach: parse with the shared PARSER and check each timer's quantity.value().is_text(), reported as a warning.

Comment thread crates/core/src/doctor.rs Outdated
PhysicalQuantity::Time,
&["s", "min", "h", "d"],
vec![
unit(&["second", "seconds"], &["s", "sec"], &["secs"], 1.0),

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.

This converter only knows eleven exact, case-sensitive English spellings, and unit lookup is an exact map match. Timers such as ~{10%Minutes}, ~{1%hr}, ~{2%hrs}, ~{5%m}, ~{2%weeks}, ~{10%Minuten} or ~{5%минут} validated cleanly before and now produce an Unknown timer unit error, so cook doctor validate --strict exits 1 on recipes every other command renders fine. Non-English collections would fail wholesale.

Comment thread crates/core/src/doctor.rs Outdated
};

let parsed = PARSER.parse(&content);
let parsed = VALIDATION_PARSER.parse(&content);

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.

Because the timer diagnostic is an error, a recipe that has one yields no parse output, so its references are never collected. A recipe with ~{a few%minutes} and @./missing{} used to report the broken reference; now it stays hidden until the timer is fixed. The new test asserts timer.references.is_empty(), which locks that behaviour in.

Comment thread crates/core/src/doctor.rs Outdated
/// is left exactly as the other commands use it.
static VALIDATION_PARSER: LazyLock<CooklangParser> = LazyLock::new(|| {
CooklangParser::new(
Extensions::ADVANCED_UNITS | Extensions::RANGE_VALUES,

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.

Two concerns with enabling these extensions for validation:

  1. ADVANCED_UNITS and RANGE_VALUES are parser-wide, so ingredient and cookware quantities also parse differently in doctor than everywhere else. For example @flour{1/0 cups} or @beans{1-1/0%cans} is a plain text quantity under the shared parser, but here it goes through numeric parsing and can raise an error on an ingredient that cook recipe and the server accept. That contradicts the doc claim that nothing else changes.
  2. Textual timers that are common in real recipes (~{overnight}, ~rest{until doubled}, ~{10 to 15%minutes}) become errors with no opt-out, so existing --strict CI pipelines go from exit 0 to exit 1 on upgrade. A warning severity, or a flag, would avoid that.

Comment thread crates/core/src/doctor.rs Outdated
/// finish unless each of them names a best unit. Those placeholders are not
/// time, and they are not words a recipe writes, so a timer unit is accepted
/// exactly when it is one of the time units above.
fn time_converter() -> Converter {

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.

This feels heavier than #214 needs: it enables a whole extension and builds a converter with placeholder mass/volume/length/temperature units. Parsing with the shared PARSER and then checking each timer's quantity.value().is_text() would report the issue without the unit false positives, the ingredient parsing drift, the hand-copied unit table or the expect below, and without roughly 90 lines of scaffolding that depend on cooklang's builder requirements staying as they are.

Comment thread crates/core/src/doctor.rs Outdated
extend: None,
quantity: vec![
group(
PhysicalQuantity::Time,

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.

This table is a hand copy of cooklang's bundled units.toml and already differs from it (best order is s/min/h/d here, s/h/min/d upstream). Nothing ties the two together, so when cooklang adds a time unit or alias, doctor will keep reporting Unknown timer unit for it.

Comment thread crates/core/src/doctor.rs Outdated

Converter::builder()
.with_units_file(file)
.expect("the doctor time units are a valid converter file")

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.

.expect() inside a LazyLock in a library crate that is also called from the NAPI addon: if a cooklang patch release tightens ConverterBuilder::finish (the manifest allows compatible upgrades and the lock is already at 0.18.7), the first validate call panics, the lock is poisoned, and every later validation in the process panics too. Returning a CoreError would be safer.

Comment thread CHANGELOG.md Outdated
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

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.

The changelog is generated by release-please from the commit messages, so this hand-written [Unreleased] section will end up next to a generated entry for the same fix: commit and need cleaning up by hand. I'd drop it from the PR.

Comment thread docs/doctor.md Outdated
Checks for: syntax errors, warnings, missing recipe references, invalid units or quantities.

A timer quantity is checked too. It has to be a number with a time unit —
`minutes`, `hours`, `seconds`, `days`, and the usual short forms — so

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.

"The usual short forms" promises more than the code accepts: only s, sec, secs, min, mins, h and d work, and matching is case sensitive. Someone writing ~{1%hr} or ~{90%m} after reading this gets an error. Either list the accepted spellings here or widen the check.

Comment thread tests/doctor_characterization_test.rs Outdated
/// `with_isolated_config` takes a [`std::process::Command`]. `assert_cmd`'s
/// wrapper does not deref to one, so this builds the process command, isolates
/// it, and only then wraps it for assertions.
fn cook_isolated(dir: &Path) -> Command {

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.

Rather than adding a second cook_isolated helper beside cook, could the existing cook(dir) helper route through common::with_isolated_config? CLAUDE.md asks that every test spawning cook be isolated, and that would cover the whole file with one function instead of only the four new tests.

Validate with the shared PARSER and report a timer whose quantity is text
as a warning, instead of enabling ADVANCED_UNITS with a hand-copied time
unit table. Unit spellings such as `hr` or `Minutes` are no longer
errors, and a recipe with a textual timer still has its references
checked.

Route the existing `cook` test helper through
`common::with_isolated_config` instead of adding a second helper, and drop
the hand-written CHANGELOG entry that release-please generates.
@mvanhorn

mvanhorn commented Oct 3, 2026

Copy link
Copy Markdown
Author

Thanks for the careful read, @LeMyst. I went with your lighter approach in 566c2cc:

  • Validation now uses the shared PARSER and reports a timer whose quantity.value().is_text() as a warning. The extensions, the copied unit table, and the LazyLock converter are gone, so hr, Minutes and friends are no longer errors and the .expect() panic path is gone too.
  • Since it's a warning, the recipe is still produced, so ~{a few%minutes} alongside @./missing{} reports both.
  • The existing cook(dir) helper now goes through common::with_isolated_config, and cook_isolated is removed.
  • The hand-written CHANGELOG section is dropped, and docs/doctor.md no longer promises short forms.

One behavior to flag: a range like ~{10-20%minutes} is now a warning too, since the shared parser reads it as text. cargo test, cargo fmt --check and cargo clippy -D warnings are clean here.

@LeMyst

LeMyst commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks @mvanhorn for picking up #214 in the first place, and for reworking it so quickly after the first round. 566c2cc covers everything from that review: the shared PARSER with an is_text() warning, no unit table or expect(), references still collected, the isolated cook() helper, and the CHANGELOG section dropped. It reads much lighter now.

I built 566c2cc and ran a few recipes through cook doctor validate. Four things seem worth looking at before merging:

1. The warning doesn't look like the rest of the report. render_findings appends a bare Timer value is text: a few line, with no Warning: label, no file:line and no source excerpt, while the parser's own findings in the same recipe get all of that. In a large collection it is easy to miss. cooklang's SourceDiag constructors are pub(crate), so it can't go through the same renderer, but a Warning: prefix and a line number would help.

2. The span sometimes points at the wrong text. quantity_text_span finds the first { followed by the value's text. I checked Diagnostic.location.span from doctor::validate for three recipes, and each one underlined something other than the timer:

Recipe Underlined
Add @salt{a few}, then cook ~{a few%minutes}. the ingredient @salt{a few}
Rest ~{a few%minutes}, add @x{a few}. the later @x{a few}, because cooklang's text_trimmed() collapses the double space, so a few is not in the timer's source
frontmatter with note: "{x", then Simmer ~{x%minutes}. the {x inside the YAML

The CLI doesn't print spans, but cookcli-core is a library, so other callers get the wrong location. cooklang::parser::PullParser is public, and its Event::Timer carries quantity.value.span(), the exact location of the value. Using it would remove the search and the tests built around it. One thing to check: how the offsets come out when the recipe has frontmatter.

3. Ranges are flagged, while the playground cited in #214 accepts them. ~{10-20%minutes} is now a warning, including in the bundled seed/Shared/Pizza Dough.cook. Since the issue uses the playground as its reference, warning on a range there looks like a false positive (~{½%hour} is warned about for the same reason). Skipping text that parses as a numeric range, or at least listing this in the docs as a known limitation, would avoid that.

4. --strict still changes outcome. I suggested a warning partly to keep --strict pipelines working, but --strict fails on warnings too, so that reasoning didn't hold up on my side, sorry. A collection with ~{overnight} or ~{10-20%minutes} that passed --strict before now exits 1. That may well be fine, since reporting these is the point of #214, but it is probably worth stating in the PR description. The description also still describes the first approach (separate parser, errors, 383 tests).

Thanks again for the work on this!


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

Take each timer's span from cooklang's PullParser instead of searching
the source for the first matching `{`, so the warning points at the
timer's own value even when an ingredient has the same text, the value
has extra whitespace, or frontmatter contains a `{`.

A range such as `~{10-20%minutes}` or a fraction such as `~{½%hour}` is
text to the shared parser but still a number, so it is no longer
reported. The rendered warning now starts with `Warning:` and names its
file and line.
@mvanhorn

mvanhorn commented Oct 3, 2026

Copy link
Copy Markdown
Author

Thanks @LeMyst, all four are in 505bdd3:

  1. The warning now renders as Warning: Timer value is text: a few (dish.cook:1), with the label coloured like the parser's own warnings.
  2. quantity_text_span is gone. The span comes from PullParser's Event::Timer (quantity.value.span()), so it lands on the timer's value. A test covers your three recipes, including the frontmatter one, where the offsets count from the start of the file.
  3. Text that is still a number is skipped: ranges like 10-20 and fractions like ½. seed/Shared/Pizza Dough.cook no longer warns, while a few and overnight still do.
  4. The PR description now describes the current approach and calls out that --strict exits 1 for a recipe with a textual timer such as ~{overnight}. docs/doctor.md says the same.

cargo fmt --check, cargo clippy --all-targets -- -D warnings and cargo test (959 passed) are clean.

Run the timer pass with RANGE_VALUES on top of the shared parser's
extensions, so a range such as `~{10-20%minutes}` comes back as a range
by cooklang's own rules, and drop the character-based is_numeric_text.

A Unicode fraction such as `~{½%hour}` is text to cooklang (0.18.7 and
0.19.0 alike), so it is reported again. Text that only looks numeric,
such as `~{1 2%minutes}` or `~{1.2.3%minutes}`, is reported instead of
being skipped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LeMyst

LeMyst commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks @mvanhorn, 505bdd3 handles all four points well. The span now comes from PullParser and lands on the timer's own value (including leading/trailing spaces, CRLF, a scaling lock, and frontmatter, >> metadata, notes or section titles, which produce no false warnings), the rendered line carries its label and line, and the description is up to date.

I pushed a small follow-up, 9c8f02c, to save another round trip on one thing, which partly came from my own mistake:

Unicode fractions are text to Cooklang. My last comment called ~{½%hour} a false positive. That was wrong: cooklang never parses Unicode fractions as numbers, neither the locked 0.18.7 nor the latest 0.19.0, so ½ doesn't scale or convert anywhere and the playground cited in #214 rejects it too. Sorry for sending you in that direction.

is_numeric_text accepted text that is not a number. It checks characters, so ~{1 2%minutes}, ~{1.2.3%minutes} and ~{1/2/3%minutes} were skipped silently.

The commit replaces the helper with cooklang's own rules: the PullParser pass now runs with PARSER.extensions() | Extensions::RANGE_VALUES, so a real range comes back as Value::Range and is skipped, and anything else stays text and is warned about. is_numeric_text is gone.

Timer Before After
~{10-20%minutes}, ~{1.5 - 2%hours}, ~{1 1/2-2%hours} not reported not reported
~{½%hour} not reported warning
~{1 2%minutes}, ~{1.2.3%minutes}, ~{1/2/3%minutes} not reported warning

The range test is renamed a_numeric_range_is_not_a_warning and covers all of these, and docs/doctor.md now says a Unicode fraction is reported and suggests ~{1/2%hour}.

One accepted gap: a malformed range such as ~{1/0-2%minutes} gets a recovery value (1.0) in this pass, so it isn't reported. The shared parser reads it as text without an error, so nothing flags it, but it seemed too rare to justify more code.

cargo fmt --check and cargo clippy --all-targets -- -D warnings are clean, and the cookcli-core suite (387 + 2) plus doctor_characterization_test, cli_integration_test and snapshot_test pass. The rest of cargo test didn't run locally (the machine ran out of disk space), so CI will have to confirm it. If you'd rather keep accepting ½ as cooking notation, that's a fair call. Feel free to revert that part of the commit; the junk-text fix doesn't depend on it.


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

@mvanhorn

mvanhorn commented Oct 6, 2026

Copy link
Copy Markdown
Author

Thanks @LeMyst, and no worries about the ½ detour. Letting cooklang's range parsing decide what counts as a number is better than my character check, and since cooklang never parses ½ as a number, warning on it and suggesting ~{1/2%hour} is the consistent call. I'd keep your follow-up as it is. Nothing else from my side.

@LeMyst
LeMyst merged commit 270c7e6 into cooklang:main Oct 7, 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.

cook doctor does not report non-numeric timer values

2 participants