beads/internal/formula
Harry Miller d0743f89d7
fix(formula): substitute vars in step assignee, labels, and metadata (#5758)
* fix(formula): substitute vars in step assignee, labels, and metadata

Cook and pour substituted title/description/notes/gate but copied
assignee, labels, and metadata through raw, so a poured bead carried
literal `{{var}}` text in all three.

- labels: every label-based query, filter, and automation silently stops
  matching, and nothing errors (GH#5110).
- metadata: general keys came through literal; only a top-level "repo"
  key on a gh:* gate was substituted (GH#5110).
- assignee: worse than cosmetic - close refuses when the actor doesn't
  match the assignee, so every step of a poured molecule is unclosable
  and the molecule can never advance (GH#5754).

internal/formula/expand.go already substituted assignee and labels on
the inline-expansion path, so the two paths disagreed on the same
fields; Step.Assignee is documented as "supports substitution" and
ExtractVariables already scanned it, meaning pour hard-failed demanding
a variable it then refused to resolve.

Both call sites are fixed together: runtime-mode cook feeds the proto
that pour clones, so a field left literal in either path ships a
placeholder onto the bead. Fixing cloneSubgraphInto covers pour, wisp,
bond, and the proxied-server paths, which all funnel through it.

substituteMetadataRepo is generalized to substituteMetadataVars, which
resolves every string leaf of the metadata JSON at any depth. This drops
the earlier gh:*-gate-only restriction (SF2/SF4): reading a `repo` key
AS a GitHub selector is only correct on a gh:* gate, but resolving a
{{var}} in it interprets the key not at all. The walk still decodes into
json.RawMessage, so untouched values stay byte-identical, and object
keys are never rewritten (a rewritten key could collide with a sibling).

The variable scan is widened to match: extractAllVariables (pour) and
ExtractVariables (cook) now cover assignee, labels, metadata strings,
notes, and await ID, so pour demands exactly the variables it resolves.
Undeclared handlebars are still filtered out by VarDefs.

Regression coverage asserts on a poured bead, and checks the assignee
via validation.AssigneeMatches rather than a string compare - a
substitution-only assertion would pass while the unclosable symptom
persisted.

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

* chore(formula): regenerate schema_gen.go after the types.go doc edits

TestSchemaGenIsCurrent holds schema_gen.go to a byte-for-byte rerun of
the generator over types.go, and the field docs are part of the
generated output — so the Labels/Metadata comment updates made it stale.

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

* fix(mol bond): resolve vars like pour instead of demanding every handlebar

buildAttachCloneOpts demanded every variable extractAllVariables found in
the proto subgraph and applied no declared defaults, unlike pour
(cmd/bd/pour.go) and wisp (cmd/bd/wisp.go). Widening the scanned field set
to assignee, labels and metadata therefore widened bond's hard refusal
along with it: a proto whose label or metadata carried an undeclared
documentation handlebar, or a declared var carrying only a default, made
`bd mol bond` fail with "missing required variables" where it previously
succeeded.

Run applyVariableDefaults + extractRequiredVariables there, matching the
sibling call sites. extractRequiredVariables returns the unfiltered list
when VarDefs is nil, so a legacy template still requires every var it
mentions; the change is strictly a reduction in false failures. Applying
the defaults also puts them in opts.Vars, which is the map
cloneSubgraphInto actually substitutes from.

Also from review:

- docs/workflows/gates.md no longer enumerates a stale three-field list for
  where `{{var}}` substitution applies.
- maxMetadataSubstitutionDepth's doc names walkJSONStrings (not the
  long-gone substituteJSONVars) and says plainly that hitting the bound
  deliberately ships a literal placeholder rather than erroring.

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>

* build(bazel): add the validation dep to the bd_test go_test (make bazel-sync)

mol_embedded_test.go and template_test.go now import internal/validation
directly (validation.AssigneeMatches, to check the substituted step
assignee), so gazelle emits //internal/validation on bd_test. bd_lib
already carries the dep, so bd_test compiles through embed, but gazelle
lists every direct test import on the go_test itself. cmd/bd/BUILD.bazel
landed on main after this PR branched, so the PR could not carry the
line; without it, bazel.yml's "BUILD files in sync" step fails in the
required Bazel / test lane. Generated by `make bazel-sync`.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.local>
2026-09-30 18:08:44 -07:00
..
cmd/schemagen build(bazel): side-by-side Bazel scaffold + policy tests (#6798) 2026-09-27 03:41:02 -07:00
schemagen build(bazel): side-by-side Bazel scaffold + policy tests (#6798) 2026-09-27 03:41:02 -07:00
advice.go chore: remove issue ID references from comments and changelogs 2025-12-28 10:05:16 -08:00
advice_test.go fix: Prevent aspect self-matching infinite recursion (gt-8tmz.16) 2025-12-25 12:21:23 -08:00
BUILD.bazel fix(formula): reject a waits_for gate with no spawner, and stop reporting invalid formulas as not found (#5761) 2026-09-29 17:09:59 -07:00
condition.go fix: Address code review issues in condition evaluator 2025-12-25 14:00:56 -08:00
condition_test.go style: run gofmt across entire codebase (bd-nmk12) 2026-02-07 19:35:20 -08:00
controlflow.go chore: remove issue ID references from comments and changelogs 2025-12-28 10:05:16 -08:00
controlflow_test.go Implement computed range expansion for loops (gt-8tmz.27) 2025-12-25 17:48:35 -08:00
expand.go feat(cook): support standalone expansion formula execution in bd mol wisp (#1903) 2026-02-22 13:00:25 -08:00
expand_test.go feat(cook): support standalone expansion formula execution in bd mol wisp (#1903) 2026-02-22 13:00:25 -08:00
intent_test.go feat(formula): surface intent field in JSON output (maintainer cherry-pick from PR #4056) (#4081) 2026-07-06 22:21:56 -07:00
parser.go fix(formula): substitute vars in step assignee, labels, and metadata (#5758) 2026-09-30 18:08:44 -07:00
parser_test.go fix(formula): substitute vars in step assignee, labels, and metadata (#5758) 2026-09-30 18:08:44 -07:00
range.go Implement computed range expansion for loops (gt-8tmz.27) 2025-12-25 17:48:35 -08:00
range_test.go style: run gofmt across entire codebase (bd-nmk12) 2026-02-07 19:35:20 -08:00
schema.go feat(formula): make primitives discoverable to coding agents (GH#3621) (#3781) 2026-07-06 22:26:41 -07:00
schema_gen.go fix(formula): substitute vars in step assignee, labels, and metadata (#5758) 2026-09-30 18:08:44 -07:00
schema_gen_test.go test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
schema_test.go feat(formula): make primitives discoverable to coding agents (GH#3621) (#3781) 2026-07-06 22:26:41 -07:00
stepcondition.go feat: add negation support for step conditions (!{{var}}) 2025-12-31 00:37:59 -08:00
stepcondition_test.go style: run gofmt across entire codebase (bd-nmk12) 2026-02-07 19:35:20 -08:00
types.go fix(formula): substitute vars in step assignee, labels, and metadata (#5758) 2026-09-30 18:08:44 -07:00
validation_sentinel_test.go fix(formula): reject a waits_for gate with no spawner, and stop reporting invalid formulas as not found (#5761) 2026-09-29 17:09:59 -07:00