beads/cmd
Harry Miller 35bac24ecc
fix(pour): reject --var names the proto cannot consume (#5762)
* fix(pour): reject --var names the proto cannot consume

An unrecognized --var name was dropped without comment. The failure that
surfaces is not a missing substitution but a missing step: a typo in an
optional var leaves its condition evaluating against an unset name, so
the conditional step is quietly never poured and the molecule looks like
it simply does not have that step.

checkPourVars now rejects names that no proto in the pour declares in
VarDefs or references as a {{handlebar}}, listing what is available.
Anything that could still affect the pour is accepted: a documentation
handlebar the formula never declared, and a var belonging to an --attach
proto rather than the root, both pass. Missing required vars are reported
first, unchanged, since the caller's hint is derived from them.

That hint is now only attached when a required var really is missing.
It interpolated missingVarHint unconditionally, so any other failure from
checkPourVars would have advised "--var =<value>".

* fix(wisp): reject --var names the proto cannot consume

bd mol wisp validates vars through checkRequiredVars rather than
checkPourVars, so the unknown-name rejection added for pour did not reach
it and a typo was still dropped without comment on the wisp path.

Route it through the same check. A wisp instantiates one proto with no
attachments, so the known set is that proto's declared vars plus the
handlebars it references.

The missing-var hint is now only attached when a var really is missing
here too, matching pour: otherwise any other failure from this check
advised "--var =<value>".

* fix(pour): count a var used only by a step condition as consumable

checkUnknownVars built its known set from the cooked subgraph, so it could
not see step conditions: formula.FilterStepsByCondition consumes them and
drops the steps carrying them BEFORE the cook, and nothing requires a
condition var to be declared in [vars]. An undeclared var that decides
which steps get poured therefore left no trace anywhere the check looked,
and a hard error turned a working pour into a failing one - exactly the
case the rule ("a name is rejected only when nothing in the pour could
consume it") means to accept.

TemplateSubgraph gains ConditionVars, recorded by
resolveAndCookFormulaWithVars immediately before the condition filter runs
- the last point at which those names are still visible. Collected
unconditionally, since they are a property of the formula rather than of
this pour's --var values, which is what makes the falsey case work: the
step is gone, and the var that removed it still counts. knownVarsAcross
adds them. A persisted proto loaded from the database leaves the field
empty, correctly: it has no formula behind it and its conditions were
resolved at cook time.

The reviewer's reproduction, against the pre-fix code:

    Error: unknown variables: has_spike (available: story)

Regression coverage answers the fifth review point as well - that every
existing test called checkPourVars directly with a hand-built subgraph,
which is precisely why this was invisible.
TestBdMolPour_AcceptsAVarUsedOnlyByAStepCondition runs the CLI through the
real cook pipeline with has_spike both true and false (the falsey case
being the one that regresses if the collection ever moves after the
filter), plus a typo case proving the widened set does not disable the
check. Two unit cases cover the same pair.

The check does not disable itself: a typo in a condition var is still
reported, now with the condition var among the available names.

Non-blocking review points 3 and 4: pour's and wisp's --var flag help says
an unusable name is an error, and the CHANGELOG entry states the new
behavior and says plainly that `bd mol bond --var` and `bd mol seed --var`
remain out of scope - both go through formula.ValidateProvidedVars, which
only iterates declared vars and so never examines a provided name with no
VarDef.

On review point 2 (the interaction with #5758): no action needed either
way round. #5758 widens extractAllVariables itself, and knownVarsAcross
calls that same function, so a var referenced only from a step's
assignee/labels/metadata joins this known set automatically when #5758
lands, in whichever order the two merge.

Reported-by: bee-ghosttrack
Agent-Signature: claude-code-opus-5-unknown-reasoning on behalf of Harry Miller

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test(pour): exercise the real cook pipeline without spawning a binary

The end-to-end case spawned an embedded bd and ran three CLI invocations,
the most expensive shape of test in cmd/bd - and cmd/bd already runs 477s
of its 600s budget on the macos runner, so the addition timed the package
out.

The process boundary was not the point. What the unit tests could not see
was the ORDERING inside the cook pipeline, and
resolveAndCookFormulaWithVars takes its search paths as an argument, so
the real pipeline - control flow, expansions, FilterStepsByCondition, the
cook - runs directly against a formula in a temp dir. Same coverage in
0.01s instead of 32s.

It is also a sharper test than the CLI version was. It now asserts the
PREMISE of the falsey case (that the conditional step really is absent
from the cooked subgraph, so the case is testing the ordering rather than
passing for an unrelated reason), and that the typo error offers the
condition var among the available names, which is the actual user-visible
point of carrying those names into the known set.

Both reds confirmed, including the near-miss: with the known-set change
reverted all three cases fail, and with the collection moved to AFTER the
condition filter - the plausible fix, and the one the reviewer's suggested
call site would have produced - has_spike=true passes while has_spike=false
still fails:

    --- FAIL: .../has_spike_false
        pour rejected a var its own step condition consumes (has_spike=false):
        unknown variables: has_spike (available: story)

Agent-Signature: claude-code-opus-5-unknown-reasoning on behalf of Harry Miller

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs(changelog): move this PR's entry under [Unreleased]

The 1.3.0 cut renamed the old [Unreleased] block, so the entry this branch
added there now merges cleanly into the released [1.3.0] section (#6643).

Agent-Signature: claude-code-opus-5-5-unknown-reasoning on behalf of Harry Miller
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* chore(bazel): enumerate the PR's new cmd/bd test files in go_test srcs

gazelle-generated. cmd/bd/BUILD.bazel lists go_test srcs explicitly, so the
two untagged test files this PR adds would not run under the advisory Bazel
lane added in #6881. Base parity at 6da2a4347 is exhaustive for untagged
tests (the only two unenumerated files are //go:build cgo && integration),
so the omission is PR-introduced rather than a pre-existing pattern.

Produced by make bazel-sync; no deps line was needed because go_test embeds
:bd_lib, which already depends on //internal/formula and //internal/types.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(pour): read every substituted field into the unknown-var known set

Maintainer review fixup for the unknown-`--var` refusal. The refusal failed
CLOSED on a known set narrower than what the pour actually substitutes, so it
rejected names the clone demonstrably consumes.

- knownVarsAcross read the five prose fields only, missing a gate's await_id
  and a gh:* gate's metadata.repo selector; a formula's `repo = "{{gate_repo}}"`
  is stored literally on the proto and filled in at clone time, and nothing
  mirrors that name into a scanned field. substitutedIssueFields now mirrors
  cloneSubgraphInto's substitution set, and gateRepoSelector is factored out of
  substituteMetadataRepo so the read and write sides of the selector cannot
  disagree about which issues carry one.
- a proto loaded from the database has no formula behind it, so its [vars]
  declarations are gone and a declared-but-unreferenced name cannot be told
  apart from a typo. `VarDefs == nil` does not identify that case - a formula
  declaring no [vars] is also nil, and that proto really does take none - so the
  cook stamps DeclaredVarsKnown and the refusal stands down without it. That
  covers every `bd cook --persist` proto and every --attach proto, which is
  always DB-loaded.
- a handlebar living only inside a step the condition filter dropped left the
  known set, so `--var deploy=false --var deploy_target=prod` failed while
  deploy=true succeeded. ConditionVars becomes FormulaVarRefs and is collected
  from the unfiltered steps' text and gate fields as well as their conditions,
  making a name's validity a property of the formula rather than of another
  var's value.
- a standalone `type = "expansion"` formula has no [[steps]]:
  MaterializeExpansion builds them from its [[template]] before the cook,
  substituting --var values into single-brace `{name}` placeholders, so a name
  used only there was consumed and then refused as unknown.
  formula.ExpansionVarRefs reports those names by running that same expansion
  - expandStep now wraps expandStepWith - with a substitution that records
  instead of replacing, so it covers exactly the fields the expansion fills in,
  including a name that `{target.description}` pulls in from the formula's own
  description. cook adds them to FormulaVarRefs ahead of the filter, and
  without MaterializeExpansion's has-Steps skip: the filter can remove every
  step, and then the template is materialized after all.

The hint-suppression branch was hand-copied to three sites and tested at none;
it is now handleVarErrorWithHint, pinned by a stderr assertion that an
unknown-var error prints no `Provide them with: --var =<value>`.

Tests: table cases for both gate fields and for `repo` on a non-gh gate staying
unsubstituted (the polarity that keeps the read side honest), for the DB-loaded
stand-down directly and via an --attach proto, and real-pipeline cases for the
dropped-step var, for vars only a dropped step's gate uses (await_id and a
gh:* repo), and for a template-only placeholder var, whose typo is still
refused with the real name offered. ExpansionVarRefs is pinned per substituted
field, per field the expansion drops (notes, condition), and for an expansion
formula that also has steps. Mutating each arm reds exactly the matching cases
and nothing else.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Julian Knutsen <agent@gascity.ai>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
2026-10-01 05:09:35 -07:00
..
bd fix(pour): reject --var names the proto cannot consume (#5762) 2026-10-01 05:09:35 -07:00