* fix(create): let a remote backend mint the --parent child id
`bd create --parent` always called the raw GetNextChildID first, which the
http backend does not serve, so every child create on a remote workspace
refused before writing anything. The Lifecycle role already mints
<parent>.<n> for a create that carries a ParentID and no id of its own, on
every backend and inside the create's own transaction; conformance
RunIssueOperationsCreateUnderAParentMintsTheNextChildID pins it on every leg,
the served one included (and the bd-enterprise postgres leg the MC server
runs). So the create route needs no new capability token: on a registered
Remote backend the client skips the raw reservation and lets the server mint.
The local route keeps its reservation so its Dolt commit staging is
unchanged, and an explicit --id beside --parent is still refused up front.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(mol): molecule reads, auto-close and close --continue over issueops roles
Over the http backend three molecule paths failed, and two of them
silently:
- `bd mol current` / `bd mol progress` without an id answered "no molecules"
because the in_progress listing (a raw SearchIssues shape) and the parent
walk (raw GetDependencyRecordsForIssues) were refused and the refusal was
swallowed; with an id they refused on raw GetMoleculeProgress.
- The molecule auto-close after `bd close` never ran: findParentMolecules
swallowed the same refused edge read into "not part of a molecule".
- `bd close --continue` claimed the next step in a raw RunInTransaction.
The direct route now reads molecules through roleMolStore, which answers
through roles every backend serves: EdgeReader (batched at the wire's 100
anchors) for the parent walk, BatchGetter for the roots (Get per id where a
server does not publish issues.batchGet), Relations for the step rows (full
rows; the raw dependents read over http is getIssue's shallow projection,
which zeroed step assignees) and the progress counts, and Reader.List for the
in_progress/hooked listings. The auto-close closes the root through
Lifecycle.Close, and the --continue claim is one Lifecycle.Update guarded by
ExpectedStatus=open.
No refusal is swallowed any more: findParentMolecules, the molecule finders
and the hooked scan return their errors; `mol current/progress` report them;
the auto-close stays best effort but warns on stderr; AdvanceToNextStep only
moves on to the next ready step when the claim LOST a race (status mismatch,
not claimable, already claimed) and returns anything else. The one heuristic
still skipped is the id-pattern heal in loadDescendants, and only for an
ErrUnsupported backend that cannot express an id-prefix search; other
failures now surface. `mol current`/`mol progress` leave the
types.IssueFilter forbidigo hole, since they no longer build a filter.
gc does not depend on bd's molecule auto-close: it runs its own
(cmd/gc/molecule_autoclose.go, from the bd on_close hook and the
controller's bead-close events). Its worker prompts do run `bd mol current`.
Served e2e (TestE2E_MoleculesOverHTTP): create --parent mints <root>.1/.2,
mol current <id> is non-empty with the full in-progress step, mol progress
counts, close --continue auto-advances, closing the last step auto-closes
the root. `mol current` without an id now refuses out loud on this stack
(asserted: never an empty list); its non-empty assertion is skipped until
S6c (externaldeps reader passthrough) is underneath.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(mol): list molecule candidates with the four wire knobs, drop the S6c skip
On top of S6c the in_progress listing behind 'bd mol current' (no id)
reaches the v0 wire encoder, which refuses ListRequest.IncludeAllTypes.
Ask for templates, gates, infra and ephemeral rows explicitly, which the
wire publishes, and run the 'mol current without an id' served case.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* feat(issueops): one library entry each for molecule auto-close, advance and reads
The molecule rules lived only in cmd/bd, in copies that disagreed: two
auto-close implementations that closed the root through different
library entries outside the step close's transaction, a `close
--continue` claim that set no assignee and could take a step another
agent held on the direct and http routes, and three progress counts.
Every molecule rule now has one implementation in the public issueops
package, composed over three roles every backend serves (BatchGetter,
Relations, EdgeReader): MoleculeRoots, LoadMolecule, AnalyzeMolecule,
ViewMolecule, ReadMoleculeProgress, ActiveMoleculeIDs,
CompletedMolecule. Membership is the parent-child edge.
Auto-close: CloseRequest.AutoCloseMolecule and
CloseBatchRequest.AutoCloseMolecule (opt-in, so gc's own auto-close is
not doubled) close the molecule root a close completed IN THE SAME
TRANSACTION, unforced and guarded on the root revision read there:
storageissueops.CloseCompletedMoleculeInTx on the store backends (the
role bodies bound to the caller's tx, TxMoleculeReader) and
uow.CloseCompletedMoleculeInUOW on the unit of work. It replays on an
idempotent re-close, reports the root on CloseResult/CloseOutcome
.AutoClosedMolecule, and a root its own close policy refuses stays open
and is reported (MoleculeAutoCloseRefusal) without failing the step.
Advance: a new MoleculeStepper role (storage.DoltStorage accessor,
uow.MoleculeStepperSource) whose one implementation,
issueops.NewMoleculeStepper, claims the next ready step through the
Claimer (Lifecycle.Update's Claim for a wisp step): assignee set, a step
another actor holds skipped, never taken.
HTTP: closeIssue/batchCloseIssues carry auto_close_molecule behind the
issues.close.autoCloseMolecule behavior token, and POST
/v0/beads/issues/{id}:advanceMolecule (issues.advanceMolecule) serves
the stepper. Handlers only decode, call the role and encode. The client
sends the member and dials the operation only where advertised; against
an older server it DEGRADES rather than refuses — it runs the same
library rule over its own roles (issueops.CloseCompletedMolecule after
the close, NewMoleculeStepper for the advance), so the outcome is the
same in more round trips. The capability fallbacks that lived in
cmd/bd move into the client: BatchGetter answers one getIssue per id
without issues.batchGet, and EdgeReader splits past the operation's 100
anchors.
Conformance: RunMolecule* on dolt, embedded, uow and http (the http leg
twice: served, and against a server with the tokens struck out).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(cmd/bd): molecule commands and create --parent through the library entries
`bd close`, `bd close --continue`, `bd mol current`, `bd mol progress`,
`bd ready --mol` and `bd create --parent` now call the one library entry
for each rule on every route (direct, proxied server, remote):
- close (batch, --if-revision, proxied) sets CloseRequest/
CloseBatchRequest.AutoCloseMolecule and only reports what the library
closed in the step close's transaction. autoCloseCompletedMolecule,
autoCloseProxiedCompletedMolecule and closeCompletedMoleculeRoot are
gone, and so is the separate post-close write.
- --continue is the route's MoleculeStepper role: a real claim (the
assignee is set) that skips a step another actor holds. Before, the
direct and http routes flipped the status only and could take it.
AdvanceToNextStep, ClaimStepIfOpen and its two implementations are
gone.
- mol current / mol progress / ready --mol read through
issueops.ViewMolecule, ReadMoleculeProgress, ActiveMoleculeIDs and
LoadMolecule/AnalyzeMolecule over the route's roles. Over http this
fixes the missing step assignee on `ready --mol` (corpus mark
mc-ready-mol). The proxied route's SearchIssues fallback, the role
store, the third progress count and the cmd/bd copy of the readiness
analysis are deleted; only presentation stays here.
- create --parent asks the create role to mint the child id on every
route (the local CLI reservation is gone) and to inherit the parent's
labels (InheritLabelsFromParent) inside the create. The dry-run
preview still shows the labels, and a failed label read there is
reported, not swallowed.
TestProxiedServerMoleculeLifecycleAgreesOnEveryRoute runs one molecule
lifecycle through the real binary on the direct, proxied and remote
(`bd connect` to `bd serve`) routes and requires the same rows and the
same JSON on all three. The served e2e now also pins the held-step skip,
the claimed step's assignee and the full rows of `ready --mol`.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(httpclient): refuse molecule auto-close and advance before dialing an older server
The client used to DEGRADE against a server without
issues.close.autoCloseMolecule / issues.advanceMolecule: it closed the step
without the member and ran a second, client-side copy of the auto-close
(issueops.CloseCompletedMolecule) in its own transactions, and composed the
stepper over its own roles. That kept two executions of each rule alive.
Now the rule runs in ONE place, the server's library: a close or batch close
that sets AutoCloseMolecule, and an Advance, refuse before anything is dialed
with the typed capability error (storage.ErrUnsupported, Capability naming the
token). The composed single-item batch leg now carries the member too.
- issueops.CloseCompletedMolecule (the role-composed form) is gone.
MoleculeRootClose is the one root close request (unforced, the auto-close
reason, the session, guarded on the root revision read), and both
in-transaction bodies (storage issueops CloseCompletedMoleculeInTx, uow
CloseCompletedMoleculeInUOW) build from it.
- MoleculeView.Ready is `bd ready --mol`'s list (every analysis-ready node,
hydrated), so ready --mol and mol current derive from ViewMolecule alone.
Tests: unit pre-dial refusal and member pins (httpclient molecule_test.go),
TestServedMoleculeRefusesAgainstAnOlderServer (step stays open, nothing
claimed) replacing the degraded contract arm.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(cmd/bd): ask for the molecule auto-close only where the route serves it
The refuse-before-dialing client made every `bd close` over http fail
against a server that does not advertise issues.close.autoCloseMolecule
(an older bd serve, or the MC server shape, whose orchestrator owns the
molecule lifecycle): the CLI always set AutoCloseMolecule.
storage.ServesMoleculeAutoClose answers, through every decorator, whether a
store's closes may carry the flag (MoleculeAutoCloseProber, implemented by
httpclient.Store from its handshake; every local backend serves it). The
direct batch close and the --if-revision close set the flag from it; the
proxied route always serves it. Against a silent server the close goes out
without the member and the server's side owns the auto-close.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test(backend/http): scope the advertised-policy check to reads before the close
The close policy check is the client-side policy's last chance: it runs
before the close request, so only the reads up to that request count. A
read after the close (the old-server molecule notice reads the closed
step's edges) is not the external-dependency policy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test(backend/http/corpus): drop the six marks S16 and S19 retire
With the molecule library entries underneath, mol current (with and
without an id, json and text), mol progress and ready --mol read through
issueops.LoadMolecule over the roles, and create --parent lets the server
mint the child id. Those six shapes pass on the reference profile.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(cmd/bd): never silent against a server without the molecule tokens
Against a server that advertises neither issues.close.autoCloseMolecule
nor issues.advanceMolecule (an older bd serve, or one whose orchestrator
owns the molecule lifecycle):
- `bd close --continue` closed the step and only then found the advance
refused, leaving the step closed and the next step unclaimed. It now
asks storage.ServesMoleculeAdvance (a MoleculeAdvanceProber the http
store answers from its handshake; local backends always serve it)
BEFORE the close and refuses up front, saying nothing was closed.
- `bd close` went out without auto_close_molecule and said nothing. When
the closed step belongs to a molecule (issueops.MoleculeRoot), a notice
on stderr now names the molecule and the missing token: its auto-close
is the server's to run. The batch and --if-revision closes both say it.
The served e2e masks both tokens out of the handshake and checks the
up-front refusal (the step stays open) and the notice; the httpclient
unit test pins ServesMoleculeAdvance to the handshake.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(cmd/bd): mol show through issueops.LoadMolecule on every route
`bd mol show` still loaded the molecule through cmd/bd's
loadTemplateSubgraph (raw GetIssue, GetDependentsWithMetadata and
GetDependencyRecords, plus an id-pattern child probe), while mol current,
mol progress and ready --mol read through the library. It now loads
through issueops.LoadMolecule over the route's molecule roles (the direct
store's, or the proxied provider's), so membership is the parent-child
edge everywhere and the http route reads through roles the server serves.
The served e2e checks mol show's root and step count.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* docs: the molecule auto-close fails the step's close on a non-policy error; stale comments
CloseRequest.AutoCloseMolecule and CloseCompletedMoleculeInTx now say what
changed when the auto-close moved into the close's transaction: a root
close the close policy refuses still leaves the root open and is reported,
but any other failure (a storage error, a lost race on the root's revision)
fails the step's close with it, where the CLI used to close the root
afterwards and only warn.
Stale: the advanceMolecule vocabulary entry said the client composes the
stepper over its own roles when unserved (it refuses before dialing), and
the graph-create collision probe called itself best-effort by transport
(the http GetIssue miss is storage.ErrNotFound like every other store's).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* docs(changelog): the molecule tokens and the S16/S19 behaviours
Entries for issues.close.autoCloseMolecule and issues.advanceMolecule, the
issueops molecule entries, the auto-close inside the step's close (and a
non-policy failure of it failing the close), the real claim behind
close --continue, the molecule reads on every route, the notice and the
up-front --continue refusal against a server without the tokens, and
create --parent / mol reads over http.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(httpclient): regenerate unsupported_gen.go for MoleculeStepper
The PR adds MoleculeStepper() to storage.DoltStorage (200 -> 201
methods). httpclient implements it by hand, but the generated shell's
trailer still counted 200, so
//internal/storage/unsupportedgen:unsupportedgen_test
(TestGeneratedShellIsFresh) failed in Bazel / test and
Bazel / integration, turning CI Gate / Required red.
Regenerated with `go generate ./internal/httpclient`; the only change
is the trailer count (143 of 201 methods generated; 58 left).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(molecule): auto-close refusals, write skew, close --json, docs
Review fixes for #7513, numbered as in each iteration's review
synthesis.
Iteration 1:
F1: a close guard's refusal of the root (TemplateReadOnlyError,
PinnedError, CloseNotAssigneeError) is an auto-close refusal, like the
close policy's: the root stays open and is reported, and the step's
close stands. Pinned by the new conformance case
RunMoleculeGuardedRootKeepsTheStepClose on dolt, embedded, uow and the
served http client.
F2: two closes that finish a molecule's last two steps in concurrent
transactions each read the other's step as open, so neither closed the
root and both committed. Every auto-close now rewrites the root's
coordination cell before it decides the root (local_metadata
molecule-coordination/v1/<shard>, the dependency coordination idiom and
sharding with a FreshRowLock value), in both twins
(CloseCompletedMoleculesInTx and CloseCompletedMoleculesInUOW). Two
deciders of one root now conflict at commit (1213), and the retry reads
the winner's close and closes the root. This is the synthesis's option
(c), applied through a coordination cell instead of the root row; it
needs no post-commit second transaction and no engine lock. The
deterministic two-transaction regression fails without the touch and
passes with it on dolt and uow; the concurrent-close tests end with the
root closed in every round, reported by exactly one close.
F3: the auto-close checks the root's eligibility before walking any
step, MoleculeStepsClosed stops at the first open step, and a batch
decides each root once and reports it on the first item under it.
F4: ErrVersionMismatch on the root is not a refusal; it fails the
step's close with it. The classifier unit test pins this. A conformance
case cannot stage that race deterministically through the public roles.
F5: storage.ServesMoleculeAdvance answers (bool, error), and bd close
--continue refuses before closing anything when the handshake cannot be
read.
F6: the CHANGELOG and the stepper and batch-closer doc comments say
what the code does.
F7: the unserved-auto-close notice fires only when the close left an
auto-closing molecule with every step closed and its root open
(issueops.CompletedMolecule): again on a re-close, once per root in a
batch, never for a step under an ordinary epic or an unfinished
molecule. Proxied mol show, mol current and mol progress again report a
read unit of work that cannot be opened as itself, not as a molecule
that was not found.
F8: the write-ledger comment says the close is refused before dialing.
The dead FirstClosed/LastClosed guards in mol progress pre-exist on main
and are left as they are.
Iteration 2:
F1: on the proxied route, bd ready --mol reported a read unit
of work it could not open twice: once as itself, then as "molecule
'<id>' not found", which under --json is a second error document on
stdout. It now returns moleculeNotResolved, as mol show, mol current and
mol progress do. TestProxiedReadyMolReportsAnUnopenableReadOnce drives
runMoleculeReady with a provider whose NewUOW fails and wants one Error:
line, or one JSON document; both subtests fail with the old return.
F2: the coordination cell serializes every concurrent close of a step
under an open auto-closing root, not only the closes that finish the
molecule. That is the price of deciding the root in the step's
transaction, so it is documented (TouchMoleculeCoordinationInTx, the
UOW twin, CloseRequest.AutoCloseMolecule, the CHANGELOG) and pinned on
dolt and uow: two sibling closes with a third step still open conflict
(both commit when the touch is removed, on either twin), and a six-step
fan-out closes every step through the store's retry, with the root
closed and reported by exactly one close. The synthesis's alternative,
a post-commit recheck by any close that read a step open, is not taken:
it is a second, best-effort transaction after the step's commit, the
shape this PR exists to remove.
F3: MoleculeRootClose's doc no longer promises that a step reopened
between the completion read and the root close fails the step's close.
The guard is the root's RowVersion, which a step reopen never writes;
the doc now says what the guard covers and that this race goes
undetected (it is inherited from the CLI's old post-commit auto-close).
F4: the user docs describe the auto-close. docs/workflows/molecules.md
replaces "Closing the last child does not close the molecule root"
with which roots close and which stay open, the refusal warning, the
unserved-server note and the --json members, and documents the claiming
--continue and parent-child step membership;
docs/reference/json-schema.md gains a bd close --json section. These
are main-only behaviors on pages pinned to v1.2.2 (docs/cli-docs.pin):
a maintainer call against §1 of
engdocs/decisions/2026-07-17-docs-release-pin.md, as in #6508, #5855,
#5997 and #5092. The auto-close itself shipped in v1.2.2
(cmd/bd/close.go autoCloseCompletedMolecule), so the old sentence was
already wrong at the pin. docs/cli-reference/ is generated from the
pinned release and is not hand-edited; the bd close and bd mol show
help texts carry the same facts and reach it at the next pin bump.
F5: bd close --json reports the auto-close. The entry of a close that
completed a molecule gains auto_closed_molecule (the root it closed) or
molecule_auto_close_refusal (why the root stayed open), the HTTP API's
names, on the direct, proxied and --if-revision routes. The members sit
beside the issue's own rather than wrapping the output in an object the
way --suggest-next does: an object would change the output's shape
exactly when a close completes a molecule and break every consumer that
reads an array, while members leave each entry decoding as the closed
issue, compose with `closed` under the flag objects, and leave a plain
close's bytes unchanged. Pinned on the embedded and proxied tiers
(TestEmbeddedCloseJSONReportsMoleculeAutoClose, TestProxiedServerClose2B
json_reports_*, and both --if-revision replay tests), all red without
the change.
F6: cmd/bd's BeadsTemplateLabel is issueops.MoleculeTemplateLabel, so
the template commands and molecule membership cannot drift apart.
F7: the UOW twin's revision probe says that it writes nothing,
and the BatchCloseRequest.auto_close_molecule spec description has its
verb back (apigen regenerated, no drift).
Iteration 3:
F1: on the unit-of-work route (bd serve, the proxied CLI), a refused
root's molecule_auto_close_refusal and warning carried the use case's
and repository's wrapping ("close <id>: db:
IssueSQLRepository.CloseChecked <id>: ...") around the refusal, where
the tx twin and the docs give the refusal alone. Both twins now report
it through RefusedMoleculeAutoClose, which answers the refusing guard's
or policy's own sentence. RunMoleculeRefusedRootKeepsTheStepClose,
RunMoleculeGuardedRootKeepsTheStepClose and the embedded and proxied
close --json tests want that exact sentence; the uow conformance leg
and the proxied test fail without the unwrap.
F2: the docs no longer promise ErrVersionMismatch for a write to the
root that races its auto-close. The root's read and its revision check
share one transaction snapshot, so that write conflicts with the root
close at commit and the retry decides the root again; the close fails
only if it still conflicts when its retry budget runs out
(MoleculeRootClose, IsMoleculeAutoCloseRefusal,
CloseRequest.AutoCloseMolecule, the tx twin's comment, the CHANGELOG
and the OpenAPI description, apigen regenerated).
F3: TestEmbeddedCloseJSONReportsMoleculeAutoClose moves above
TestEmbeddedCloseConcurrent's doc comment, which documents its own test
again.
F4: bd mol progress counting direct steps while the auto-close counts
every descendant predates this PR; it is left for a follow-up.
F5: cmd/bd's molReader drops GetMoleculeProgress, which nothing called
once mol progress read through issueops.ReadMoleculeProgress.
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.5 <noreply@anthropic.com>
1036 lines
37 KiB
Go
1036 lines
37 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/config"
|
|
"github.com/steveyegge/beads/internal/formula"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/storage/issueops"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/internal/utils"
|
|
publicops "github.com/steveyegge/beads/issueops"
|
|
)
|
|
|
|
// configReader is the minimal slice of storage.Storage that config-reading
|
|
// helpers depend on, letting tests inject a fake without spinning up a Dolt
|
|
// server. Transaction-bound writers (storeMolWriter) satisfy it with reads
|
|
// that see in-transaction config writes.
|
|
type configReader interface {
|
|
GetConfig(ctx context.Context, key string) (string, error)
|
|
}
|
|
|
|
// BeadsTemplateLabel is the label used to identify Beads-based templates. It
|
|
// is the label molecule membership and auto-close read
|
|
// (issueops.MoleculeTemplateLabel), so the two cannot drift apart.
|
|
const BeadsTemplateLabel = publicops.MoleculeTemplateLabel
|
|
|
|
// variablePattern matches {{variable}} placeholders
|
|
var variablePattern = regexp.MustCompile(`\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}`)
|
|
|
|
// TemplateSubgraph holds a template epic and all its descendants
|
|
type TemplateSubgraph struct {
|
|
Root *types.Issue // The template epic
|
|
Issues []*types.Issue // All issues in the subgraph (including root)
|
|
Dependencies []*types.Dependency // All dependencies within the subgraph
|
|
IssueMap map[string]*types.Issue // ID -> Issue for quick lookup
|
|
VarDefs map[string]formula.VarDef // Variable definitions from formula (for defaults)
|
|
Phase string // Recommended phase: "liquid" (pour) or "vapor" (wisp)
|
|
Pour bool // If true, steps should be materialized as sub-issues (from formula pour=true)
|
|
|
|
// FormulaVarRefs names the variables referenced by the formula this
|
|
// subgraph was cooked from that may leave no trace in the subgraph itself,
|
|
// because they are consumed or erased BEFORE the cook:
|
|
//
|
|
// - a step condition is consumed by formula.FilterStepsByCondition and is
|
|
// never copied to an issue field, and nothing requires a condition var
|
|
// to be declared in [vars], so it would appear in neither the issues
|
|
// nor VarDefs;
|
|
// - the substitutable text of a step the filter DROPPED disappears along
|
|
// with the step;
|
|
// - a {name} placeholder in a standalone expansion formula's template is
|
|
// replaced by its --var value while formula.MaterializeExpansion builds
|
|
// the steps, so the name is gone before the cook sees them.
|
|
//
|
|
// All three are recorded ahead of the filter so the set of variables a
|
|
// proto can consume stays a property of the formula rather than of one
|
|
// pour's --var values: a var that changes which steps get poured, or that
|
|
// is referenced only inside a step the user just switched off, must not be
|
|
// reported as one the proto cannot consume.
|
|
//
|
|
// Empty for a persisted proto loaded from the database, which has no
|
|
// formula behind it - its conditions were already resolved at cook time.
|
|
FormulaVarRefs []string
|
|
|
|
// DeclaredVarsKnown reports whether VarDefs is the formula's complete
|
|
// declared-variable set. Only the cook can know that, so only the cook
|
|
// sets it (cookFormulaToSubgraphWithVars); a subgraph built any other way
|
|
// - loadTemplateSubgraph for a persisted or --attach proto, or a test
|
|
// fixture - leaves it false.
|
|
//
|
|
// It is NOT the same question as `VarDefs != nil`: a cooked formula that
|
|
// declares no [vars] at all also has a nil VarDefs, and that proto really
|
|
// does take no variables. Persistence, by contrast, drops the declarations
|
|
// entirely, so a var the formula declared but never wrote into a
|
|
// substituted field becomes indistinguishable from a typo - which is why
|
|
// checkUnknownVars stands down rather than guessing when this is false.
|
|
DeclaredVarsKnown bool
|
|
}
|
|
|
|
// InstantiateResult holds the result of template instantiation
|
|
type InstantiateResult struct {
|
|
NewEpicID string `json:"new_epic_id"`
|
|
IDMapping map[string]string `json:"id_mapping"` // old ID -> new ID
|
|
Created int `json:"created"` // number of issues created
|
|
}
|
|
|
|
// CloneOptions controls how the subgraph is cloned during spawn/bond
|
|
type CloneOptions struct {
|
|
Vars map[string]string // Variable substitutions for {{key}} placeholders
|
|
Assignee string // Assign the root epic to this agent/user
|
|
Actor string // Actor performing the operation
|
|
Ephemeral bool // If true, spawned issues are marked for bulk deletion
|
|
Prefix string // Override prefix for ID generation (bd-hobo: distinct prefixes)
|
|
|
|
// Dynamic bonding fields (for Christmas Ornament pattern)
|
|
ParentID string // Parent molecule ID to bond under (e.g., "patrol-x7k")
|
|
ChildRef string // Child reference with variables (e.g., "arm-{{polecat_name}}")
|
|
|
|
// Atomic attachment: if set, adds a dependency from the spawned root to
|
|
// AttachToID within the same transaction as the clone, preventing orphans.
|
|
AttachToID string // Molecule ID to attach spawned root to
|
|
AttachDepType types.DependencyType // Dependency type for the attachment
|
|
|
|
// RootOnly: if true, only create the root issue (no child step issues).
|
|
// Used by patrol wisps where steps are inlined at prime time, not tracked as beads.
|
|
RootOnly bool
|
|
}
|
|
|
|
// bondedIDPattern validates bonded IDs (alphanumeric, dash, underscore, dot)
|
|
var bondedIDPattern = regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)
|
|
|
|
// =============================================================================
|
|
// Beads Template Functions
|
|
// =============================================================================
|
|
|
|
// loadTemplateSubgraph loads a template epic and all its descendants
|
|
func loadTemplateSubgraph(ctx context.Context, s molReader, templateID string) (*TemplateSubgraph, error) {
|
|
if s == nil {
|
|
return nil, fmt.Errorf("no database connection")
|
|
}
|
|
|
|
// Get the root issue
|
|
root, err := s.GetIssue(ctx, templateID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get template: %w", err)
|
|
}
|
|
if root == nil {
|
|
return nil, fmt.Errorf("template %s not found", templateID)
|
|
}
|
|
|
|
subgraph := &TemplateSubgraph{
|
|
Root: root,
|
|
Issues: []*types.Issue{root},
|
|
IssueMap: map[string]*types.Issue{root.ID: root},
|
|
}
|
|
|
|
// Recursively load all children (with cycle detection, GH#2719)
|
|
visited := map[string]bool{root.ID: true}
|
|
if err := loadDescendants(ctx, s, subgraph, root.ID, visited); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Load all dependencies within the subgraph
|
|
for _, issue := range subgraph.Issues {
|
|
deps, err := s.GetDependencyRecords(ctx, issue.ID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get dependencies for %s: %w", issue.ID, err)
|
|
}
|
|
for _, dep := range deps {
|
|
// Only include dependencies where both ends are in the subgraph
|
|
if _, ok := subgraph.IssueMap[dep.DependsOnID]; ok {
|
|
subgraph.Dependencies = append(subgraph.Dependencies, dep)
|
|
}
|
|
}
|
|
}
|
|
|
|
return subgraph, nil
|
|
}
|
|
|
|
// loadDescendants recursively loads all child issues.
|
|
// It uses two strategies to find children:
|
|
// 1. Check dependency records for parent-child relationships
|
|
// 2. Check for hierarchical IDs (parent.N) to catch children with missing/wrong deps
|
|
//
|
|
// The visited set tracks IDs already expanded to detect cycles (GH#2719).
|
|
// Without this, cyclic parent-child dependencies cause unbounded recursion leading to OOM.
|
|
func loadDescendants(ctx context.Context, s molReader, subgraph *TemplateSubgraph, parentID string, visited map[string]bool) error {
|
|
// Track children we've already added to avoid duplicates
|
|
addedChildren := make(map[string]bool)
|
|
|
|
// Strategy 1: Get direct parent-child dependents with relationship metadata.
|
|
dependents, err := s.GetDependentsWithMetadata(ctx, parentID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get dependents of %s: %w", parentID, err)
|
|
}
|
|
|
|
// Only keep explicit parent-child relationships.
|
|
for _, dependent := range dependents {
|
|
if dependent.DependencyType != types.DepParentChild {
|
|
continue
|
|
}
|
|
|
|
if _, exists := subgraph.IssueMap[dependent.ID]; exists {
|
|
continue // Already in subgraph
|
|
}
|
|
|
|
// Cycle detection (GH#2719)
|
|
if visited[dependent.ID] {
|
|
continue
|
|
}
|
|
|
|
child := dependent.Issue
|
|
|
|
// Add to subgraph
|
|
subgraph.Issues = append(subgraph.Issues, &child)
|
|
subgraph.IssueMap[child.ID] = &child
|
|
addedChildren[child.ID] = true
|
|
|
|
// Mark visited before recursing
|
|
visited[child.ID] = true
|
|
if err := loadDescendants(ctx, s, subgraph, child.ID, visited); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Strategy 2: Find hierarchical children by ID pattern
|
|
// This catches children that have missing or incorrect dependency types.
|
|
// Hierarchical IDs follow the pattern: parentID.N (e.g., "gt-abc.1", "gt-abc.2")
|
|
hierarchicalChildren, err := findHierarchicalChildren(ctx, s, parentID)
|
|
if err != nil {
|
|
// The parent-child edges read above are the molecule's membership;
|
|
// this id-pattern scan only heals children whose edge is missing. A
|
|
// backend that cannot express an id-prefix search (a remote backend)
|
|
// therefore loads the edge-linked subgraph without the heal. Any other
|
|
// failure is a failed read and is returned, not treated as "no
|
|
// children".
|
|
var unsupported *storage.ErrUnsupported
|
|
if errors.As(err, &unsupported) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("failed to find hierarchical children of %s: %w", parentID, err)
|
|
}
|
|
|
|
for _, child := range hierarchicalChildren {
|
|
if addedChildren[child.ID] {
|
|
continue // Already added via dependency
|
|
}
|
|
if _, exists := subgraph.IssueMap[child.ID]; exists {
|
|
continue // Already in subgraph
|
|
}
|
|
|
|
// Cycle detection (GH#2719)
|
|
if visited[child.ID] {
|
|
continue
|
|
}
|
|
|
|
// Check if this hierarchical child has been reparented to a different parent (GH#2476).
|
|
// If it has an explicit parent-child dependency pointing elsewhere, skip it —
|
|
// the ID pattern match is stale and the child belongs to another molecule.
|
|
depRecs, err := s.GetDependencyRecords(ctx, child.ID)
|
|
if err == nil {
|
|
reparented := false
|
|
for _, dep := range depRecs {
|
|
if dep.Type == types.DepParentChild && dep.DependsOnID != parentID {
|
|
reparented = true
|
|
break
|
|
}
|
|
}
|
|
if reparented {
|
|
continue
|
|
}
|
|
}
|
|
|
|
// Add to subgraph
|
|
subgraph.Issues = append(subgraph.Issues, child)
|
|
subgraph.IssueMap[child.ID] = child
|
|
addedChildren[child.ID] = true
|
|
|
|
// Mark visited before recursing
|
|
visited[child.ID] = true
|
|
if err := loadDescendants(ctx, s, subgraph, child.ID, visited); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// findHierarchicalChildren finds issues with IDs that match the pattern parentID.N
|
|
// This catches hierarchical children that may be missing parent-child dependencies.
|
|
func findHierarchicalChildren(ctx context.Context, s molReader, parentID string) ([]*types.Issue, error) {
|
|
pattern := parentID + "."
|
|
candidates, err := s.SearchIssues(ctx, "", types.IssueFilter{IDPrefix: pattern})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var children []*types.Issue
|
|
for _, issue := range candidates {
|
|
_, directParentID, depth := types.ParseHierarchicalID(issue.ID)
|
|
if depth > 0 && directParentID == parentID {
|
|
children = append(children, issue)
|
|
}
|
|
}
|
|
|
|
return children, nil
|
|
}
|
|
|
|
// =============================================================================
|
|
// Proto Lookup Functions
|
|
// =============================================================================
|
|
|
|
// resolveProtoIDOrTitle resolves a proto by ID or title.
|
|
// It first tries to resolve as an ID (via ResolvePartialID).
|
|
// If that fails, it searches for protos with matching titles.
|
|
// Returns the proto ID if found, or an error if not found or ambiguous.
|
|
func resolveProtoIDOrTitle(ctx context.Context, s molReader, input string) (string, error) {
|
|
// Strategy 1: Try to resolve as an ID
|
|
protoID, err := utils.ResolvePartialID(ctx, s, input)
|
|
if err == nil {
|
|
// Verify it's a proto (has template label)
|
|
issue, getErr := s.GetIssue(ctx, protoID)
|
|
if getErr == nil && issue != nil {
|
|
labels, _ := s.GetLabels(ctx, protoID)
|
|
for _, label := range labels {
|
|
if label == BeadsTemplateLabel {
|
|
return protoID, nil // Found a valid proto by ID
|
|
}
|
|
}
|
|
}
|
|
// ID resolved but not a proto - continue to title search
|
|
}
|
|
|
|
// Strategy 2: Search for protos by title
|
|
protos, err := s.GetIssuesByLabel(ctx, BeadsTemplateLabel)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to search protos: %w", err)
|
|
}
|
|
|
|
var matches []*types.Issue
|
|
var exactMatch *types.Issue
|
|
|
|
for _, proto := range protos {
|
|
// Check for exact title match (case-insensitive)
|
|
if strings.EqualFold(proto.Title, input) {
|
|
exactMatch = proto
|
|
break
|
|
}
|
|
// Check for partial title match (case-insensitive)
|
|
if strings.Contains(strings.ToLower(proto.Title), strings.ToLower(input)) {
|
|
matches = append(matches, proto)
|
|
}
|
|
}
|
|
|
|
if exactMatch != nil {
|
|
return exactMatch.ID, nil
|
|
}
|
|
|
|
if len(matches) == 0 {
|
|
return "", fmt.Errorf("no proto found matching %q (by ID or title)", input)
|
|
}
|
|
|
|
if len(matches) == 1 {
|
|
return matches[0].ID, nil
|
|
}
|
|
|
|
// Multiple matches - show them all for disambiguation
|
|
var matchNames []string
|
|
for _, m := range matches {
|
|
matchNames = append(matchNames, fmt.Sprintf("%s: %s", m.ID, m.Title))
|
|
}
|
|
return "", fmt.Errorf("ambiguous: %q matches %d protos:\n %s\nUse the ID or a more specific title", input, len(matches), strings.Join(matchNames, "\n "))
|
|
}
|
|
|
|
// extractVariables finds all {{variable}} patterns in text.
|
|
// Handlebars control keywords like "else", "this" are excluded.
|
|
func extractVariables(text string) []string {
|
|
matches := variablePattern.FindAllStringSubmatch(text, -1)
|
|
seen := make(map[string]bool)
|
|
var vars []string
|
|
for _, match := range matches {
|
|
if len(match) >= 2 && !seen[match[1]] {
|
|
name := match[1]
|
|
// Skip Handlebars control keywords
|
|
if isHandlebarsKeyword(name) {
|
|
continue
|
|
}
|
|
vars = append(vars, name)
|
|
seen[name] = true
|
|
}
|
|
}
|
|
return vars
|
|
}
|
|
|
|
// isHandlebarsKeyword returns true for Handlebars control keywords
|
|
// that look like variables but aren't (e.g., "else", "this").
|
|
func isHandlebarsKeyword(name string) bool {
|
|
switch name {
|
|
case "else", "this", "root", "index", "key", "first", "last":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// extractAllVariables finds all variables across the entire subgraph.
|
|
//
|
|
// The fields scanned here must stay in sync with the fields cloneSubgraphInto
|
|
// substitutes - a var that pour resolves but never demands leaves a silent
|
|
// literal placeholder in the poured bead, and a var it demands but never
|
|
// resolves is a closed loop that fails the pour for nothing (GH#5110,
|
|
// GH#5754).
|
|
func extractAllVariables(subgraph *TemplateSubgraph) []string {
|
|
var sb strings.Builder
|
|
write := func(parts ...string) {
|
|
for _, p := range parts {
|
|
if p == "" {
|
|
continue
|
|
}
|
|
sb.WriteString(p)
|
|
sb.WriteByte(' ')
|
|
}
|
|
}
|
|
for _, issue := range subgraph.Issues {
|
|
write(issue.Title, issue.Description, issue.Design, issue.AcceptanceCriteria, issue.Notes)
|
|
write(issue.Assignee, issue.AwaitID)
|
|
write(issue.Labels...)
|
|
write(metadataVarStrings(issue.Metadata)...)
|
|
}
|
|
return extractVariables(sb.String())
|
|
}
|
|
|
|
// substitutedIssueFields returns every string on a proto issue that
|
|
// cloneSubgraphInto substitutes variables into when the proto is poured: the
|
|
// prose, the assignee, a gate's AwaitID, each label, and every string value in
|
|
// the metadata (GH#5110, GH#5754).
|
|
//
|
|
// KEEP IN SYNC with cloneSubgraphInto's newIssue literal: it is the write side
|
|
// of this read. Any field that gains a substituteVariables call there has to be
|
|
// added here in the same commit, or knownVarsAcross under-approximates what a
|
|
// pour consumes and checkUnknownVars refuses a --var the clone would have used.
|
|
// extractAllVariables reads the same fields for the variables a pour DEMANDS,
|
|
// so the two must agree as well: a name demanded there but missing here can
|
|
// never be poured, because supplying it is refused. Conversely, anything the
|
|
// clone copies verbatim must stay out - naming it would advertise a
|
|
// substitution that never happens: AwaitType and IssueType, and every metadata
|
|
// object KEY (substituteMetadataVars rewrites string values only, which is all
|
|
// metadataVarStrings returns).
|
|
func substitutedIssueFields(issue *types.Issue) []string {
|
|
if issue == nil {
|
|
return nil
|
|
}
|
|
fields := []string{
|
|
issue.Title,
|
|
issue.Description,
|
|
issue.Design,
|
|
issue.AcceptanceCriteria,
|
|
issue.Notes,
|
|
issue.Assignee,
|
|
issue.AwaitID,
|
|
}
|
|
fields = append(fields, issue.Labels...)
|
|
return append(fields, metadataVarStrings(issue.Metadata)...)
|
|
}
|
|
|
|
// extractConsumableVariables finds every variable name the subgraph's issues
|
|
// can consume at clone time - every field substitutedIssueFields names, not
|
|
// just the prose.
|
|
func extractConsumableVariables(subgraph *TemplateSubgraph) []string {
|
|
if subgraph == nil {
|
|
return nil
|
|
}
|
|
allText := ""
|
|
for _, issue := range subgraph.Issues {
|
|
for _, field := range substitutedIssueFields(issue) {
|
|
allText += field + " "
|
|
}
|
|
}
|
|
return extractVariables(allText)
|
|
}
|
|
|
|
// extractRequiredVariables returns only variables that don't have defaults.
|
|
// If VarDefs is available (from a cooked formula), uses it to filter out defaulted vars.
|
|
// Otherwise, falls back to returning all variables.
|
|
func extractRequiredVariables(subgraph *TemplateSubgraph) []string {
|
|
allVars := extractAllVariables(subgraph)
|
|
|
|
// If no VarDefs, assume all variables are required (legacy template behavior)
|
|
if subgraph.VarDefs == nil {
|
|
return allVars
|
|
}
|
|
|
|
// VarDefs exists (from a cooked formula) - only declared variables matter.
|
|
// Variables in text but NOT in VarDefs are ignored - they're documentation
|
|
// handlebars meant for LLM agents, not formula input variables (gt-ky9loa).
|
|
var required []string
|
|
for _, v := range allVars {
|
|
def, exists := subgraph.VarDefs[v]
|
|
if !exists {
|
|
// Not a declared formula variable - skip (documentation handlebars)
|
|
continue
|
|
}
|
|
// A declared variable is required if it has no default.
|
|
// nil Default = no default specified (must provide).
|
|
// Non-nil Default (including &"") = has explicit default (optional).
|
|
if def.Default == nil {
|
|
required = append(required, v)
|
|
}
|
|
}
|
|
return required
|
|
}
|
|
|
|
// applyVariableDefaults merges formula default values with provided variables.
|
|
// Returns a new map with defaults applied for any missing variables.
|
|
func applyVariableDefaults(vars map[string]string, subgraph *TemplateSubgraph) map[string]string {
|
|
if subgraph.VarDefs == nil {
|
|
return vars
|
|
}
|
|
|
|
result := make(map[string]string)
|
|
for k, v := range vars {
|
|
result[k] = v
|
|
}
|
|
|
|
// Apply defaults for missing variables (including empty-string defaults)
|
|
for name, def := range subgraph.VarDefs {
|
|
if _, exists := result[name]; !exists && def.Default != nil {
|
|
result[name] = *def.Default
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// substituteVariables replaces {{variable}} with values
|
|
func substituteVariables(text string, vars map[string]string) string {
|
|
return variablePattern.ReplaceAllStringFunc(text, func(match string) string {
|
|
// Extract variable name from {{name}}
|
|
name := match[2 : len(match)-2]
|
|
if val, ok := vars[name]; ok {
|
|
return val
|
|
}
|
|
return match // Leave unchanged if not found
|
|
})
|
|
}
|
|
|
|
// maxMetadataSubstitutionDepth bounds the recursion in walkJSONStrings and in
|
|
// cook.go's substituteMetadataValueDepth. Metadata is arbitrary JSON that can
|
|
// arrive from an untrusted proto, and a deeply nested value must not blow the
|
|
// stack. Reaching the bound deliberately stops substituting and returns the
|
|
// value as-is rather than erroring: a `{{var}}` nested deeper than this ships
|
|
// as a literal placeholder. That is the intended trade - a fence against a
|
|
// hostile proto, not a limit any real formula is expected to meet.
|
|
const maxMetadataSubstitutionDepth = 32
|
|
|
|
// substituteMetadataVars substitutes {{variable}} placeholders in every string
|
|
// value of an issue's metadata, at any nesting depth.
|
|
//
|
|
// Formula step metadata (`[steps.metadata]`) and a gate step's `repo` selector
|
|
// (repo = "{{gate_repo}}") are stored literally on the persisted proto by
|
|
// processStepToIssue/createGateIssue - `bd cook --persist` keeps the proto
|
|
// reusable across pours rather than substituting at compile time. Substitution
|
|
// instead happens at the same point as every other var-bearing issue field
|
|
// (Title, Description, Assignee, Labels, AwaitID, ...): here, in
|
|
// cloneSubgraphInto, when a proto is poured/spawned into real issues.
|
|
//
|
|
// This supersedes the earlier gh:*-gate-only, top-level-"repo"-only rule
|
|
// (SF2/SF4). That restriction existed because interpreting a `repo` key as a
|
|
// GitHub selector is only correct on a gh:* gate - but substituting a
|
|
// {{var}} placeholder interprets nothing about the key, and general metadata
|
|
// carrying literal placeholders was its own bug (GH#5110). A value with no
|
|
// placeholder is unaffected either way.
|
|
//
|
|
// The walk decodes into json.RawMessage rather than interface{} and rebuilds
|
|
// only the containers along a changed path, so every untouched value survives
|
|
// byte-identical - interface{} would mangle numbers to float64, and a full
|
|
// re-marshal of decoded values can HTML-escape strings that were never
|
|
// touched. Metadata with no substitutable placeholder is returned as-is.
|
|
func substituteMetadataVars(metadata json.RawMessage, vars map[string]string) json.RawMessage {
|
|
if len(metadata) == 0 {
|
|
return metadata
|
|
}
|
|
out, changed := walkJSONStrings(metadata, 0, func(s string) string {
|
|
return substituteVariables(s, vars)
|
|
})
|
|
if !changed {
|
|
return metadata
|
|
}
|
|
return out
|
|
}
|
|
|
|
// metadataVarStrings returns every string leaf in an issue's metadata, for
|
|
// variable extraction. Object keys are excluded because substitution does not
|
|
// touch them - scanning them would make pour demand a variable it then refuses
|
|
// to resolve.
|
|
func metadataVarStrings(metadata json.RawMessage) []string {
|
|
if len(metadata) == 0 {
|
|
return nil
|
|
}
|
|
var found []string
|
|
walkJSONStrings(metadata, 0, func(s string) string {
|
|
found = append(found, s)
|
|
return s
|
|
})
|
|
return found
|
|
}
|
|
|
|
// walkJSONStrings applies fn to every string leaf of a JSON value, at any
|
|
// nesting depth. It reports whether fn changed anything; when nothing did, the
|
|
// input bytes are returned untouched.
|
|
//
|
|
// Object keys are deliberately left alone: rewriting a key could collide with
|
|
// a sibling key and silently drop a value.
|
|
func walkJSONStrings(raw json.RawMessage, depth int, fn func(string) string) (json.RawMessage, bool) {
|
|
trimmed := bytes.TrimLeft(raw, " \t\r\n")
|
|
if len(trimmed) == 0 {
|
|
return raw, false
|
|
}
|
|
|
|
switch trimmed[0] {
|
|
case '"':
|
|
var s string
|
|
if err := json.Unmarshal(raw, &s); err != nil {
|
|
return raw, false
|
|
}
|
|
replaced := fn(s)
|
|
if replaced == s {
|
|
return raw, false
|
|
}
|
|
encoded, err := marshalNoHTMLEscape(replaced)
|
|
if err != nil {
|
|
return raw, false
|
|
}
|
|
return encoded, true
|
|
|
|
case '{':
|
|
if depth >= maxMetadataSubstitutionDepth {
|
|
return raw, false
|
|
}
|
|
var obj map[string]json.RawMessage
|
|
if err := json.Unmarshal(raw, &obj); err != nil {
|
|
return raw, false
|
|
}
|
|
changed := false
|
|
for k, v := range obj {
|
|
if newV, c := walkJSONStrings(v, depth+1, fn); c {
|
|
obj[k] = newV
|
|
changed = true
|
|
}
|
|
}
|
|
if !changed {
|
|
return raw, false
|
|
}
|
|
out, err := marshalNoHTMLEscape(obj)
|
|
if err != nil {
|
|
return raw, false
|
|
}
|
|
return out, true
|
|
|
|
case '[':
|
|
if depth >= maxMetadataSubstitutionDepth {
|
|
return raw, false
|
|
}
|
|
var arr []json.RawMessage
|
|
if err := json.Unmarshal(raw, &arr); err != nil {
|
|
return raw, false
|
|
}
|
|
changed := false
|
|
for i, v := range arr {
|
|
if newV, c := walkJSONStrings(v, depth+1, fn); c {
|
|
arr[i] = newV
|
|
changed = true
|
|
}
|
|
}
|
|
if !changed {
|
|
return raw, false
|
|
}
|
|
out, err := marshalNoHTMLEscape(arr)
|
|
if err != nil {
|
|
return raw, false
|
|
}
|
|
return out, true
|
|
}
|
|
|
|
// Number, bool, null: no string leaf here.
|
|
return raw, false
|
|
}
|
|
|
|
// substituteLabels returns labels with {{variable}} placeholders substituted.
|
|
// A formula step's labels are carried onto the proto literally by
|
|
// processStepToIssue, so - like Title and Description - they resolve here, at
|
|
// pour time (GH#5110). Returns nil for an empty input so an issue with no
|
|
// labels keeps a nil slice.
|
|
func substituteLabels(labels []string, vars map[string]string) []string {
|
|
if len(labels) == 0 {
|
|
return nil
|
|
}
|
|
out := make([]string, len(labels))
|
|
for i, l := range labels {
|
|
out[i] = substituteVariables(l, vars)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// marshalNoHTMLEscape is json.Marshal without HTML-escaping '<', '>', and
|
|
// '&' - the stdlib's json.Marshal escapes them by default (aimed at
|
|
// embedding JSON in HTML), which would silently corrupt an unrelated
|
|
// metadata value round-tripped through substituteMetadataVars.
|
|
func marshalNoHTMLEscape(v interface{}) (json.RawMessage, error) {
|
|
var buf bytes.Buffer
|
|
enc := json.NewEncoder(&buf)
|
|
enc.SetEscapeHTML(false)
|
|
if err := enc.Encode(v); err != nil {
|
|
return nil, err
|
|
}
|
|
// json.Encoder.Encode appends a trailing newline; callers embed this
|
|
// result as a json.RawMessage value, which must not carry one.
|
|
return bytes.TrimRight(buf.Bytes(), "\n"), nil
|
|
}
|
|
|
|
// generateBondedID creates a custom ID for dynamically bonded molecules.
|
|
// When bonding a proto to a parent molecule, this generates IDs like:
|
|
// - Root: parent.childref (e.g., "patrol-x7k.arm-ace")
|
|
// - Children: parent.childref.step (e.g., "patrol-x7k.arm-ace.capture")
|
|
//
|
|
// The childRef is variable-substituted before use.
|
|
// Returns empty string if not a bonded operation (opts.ParentID empty).
|
|
func generateBondedID(oldID string, rootID string, opts CloneOptions) (string, error) {
|
|
if opts.ParentID == "" {
|
|
return "", nil // Not a bonded operation
|
|
}
|
|
|
|
// Substitute variables in childRef
|
|
childRef := substituteVariables(opts.ChildRef, opts.Vars)
|
|
|
|
// Validate childRef after substitution
|
|
if childRef == "" {
|
|
return "", fmt.Errorf("childRef is empty after variable substitution")
|
|
}
|
|
if !bondedIDPattern.MatchString(childRef) {
|
|
return "", fmt.Errorf("invalid childRef '%s': must be alphanumeric, dash, underscore, or dot only", childRef)
|
|
}
|
|
|
|
if oldID == rootID {
|
|
// Root issue: parent.childref
|
|
newID := fmt.Sprintf("%s.%s", opts.ParentID, childRef)
|
|
return newID, nil
|
|
}
|
|
|
|
// Child issue: parent.childref.relative
|
|
// Extract the relative portion of the old ID (part after root)
|
|
relativeID := getRelativeID(oldID, rootID)
|
|
if relativeID == "" {
|
|
// No hierarchical relationship - use a suffix from the old ID to ensure uniqueness.
|
|
// Extract the last part of the old ID (after any prefix or dash)
|
|
suffix := extractIDSuffix(oldID)
|
|
newID := fmt.Sprintf("%s.%s.%s", opts.ParentID, childRef, suffix)
|
|
return newID, nil
|
|
}
|
|
|
|
newID := fmt.Sprintf("%s.%s.%s", opts.ParentID, childRef, relativeID)
|
|
return newID, nil
|
|
}
|
|
|
|
// extractIDSuffix extracts a suffix from an ID for use when IDs aren't hierarchical.
|
|
// For "patrol-abc123", returns "abc123".
|
|
// For "bd-xyz.1", returns "1".
|
|
// This ensures child IDs remain unique when bonding.
|
|
func extractIDSuffix(id string) string {
|
|
// First try to get the part after the last dot (for hierarchical IDs)
|
|
if lastDot := strings.LastIndex(id, "."); lastDot >= 0 {
|
|
return id[lastDot+1:]
|
|
}
|
|
// Otherwise, get the part after the last dash (for prefix-hash IDs)
|
|
if lastDash := strings.LastIndex(id, "-"); lastDash >= 0 {
|
|
return id[lastDash+1:]
|
|
}
|
|
// Fallback: use the whole ID
|
|
return id
|
|
}
|
|
|
|
// getRelativeID extracts the relative portion of a child ID from its parent.
|
|
// For example: getRelativeID("bd-abc.step1.sub", "bd-abc") returns "step1.sub"
|
|
// Returns empty string if oldID equals rootID or doesn't start with rootID.
|
|
func getRelativeID(oldID, rootID string) string {
|
|
if oldID == rootID {
|
|
return ""
|
|
}
|
|
// Check if oldID starts with rootID followed by a dot
|
|
prefix := rootID + "."
|
|
if strings.HasPrefix(oldID, prefix) {
|
|
return oldID[len(prefix):]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// flattenUnregisteredIssueTypes flattens issue types that are neither
|
|
// built-in nor already registered in types.custom, printing a warning
|
|
// naming each flattened type. Issues with children (the DependsOnID side
|
|
// of a parent-child dep) flatten to epic — matching the default for
|
|
// undeclared parent step types — and leaves flatten to task.
|
|
// Materializing a formula must not silently grow the type whitelist — a
|
|
// typo'd step type would become a permanently registered custom type — so
|
|
// unregistered types degrade instead; operators opt in with bd config set
|
|
// types.custom before pouring. Without the flatten, issue creation fails
|
|
// with "invalid issue type" on the first unregistered bead.
|
|
// (GH#3213, GH#5443)
|
|
func flattenUnregisteredIssueTypes(ctx context.Context, s configReader, issues []*types.Issue, deps []*types.Dependency) error {
|
|
// Seed with every non-built-in type used by the issues, then remove the
|
|
// registered ones below; what survives is unknown. IsBuiltIn (not
|
|
// IsValid) matches the validator this check exists to satisfy:
|
|
// IsValidWithCustom short-circuits on IsBuiltIn, so types like "event"
|
|
// need no types.custom entry.
|
|
unknown := make(map[types.IssueType]bool)
|
|
for _, issue := range issues {
|
|
t := issue.IssueType
|
|
if t == "" || t.IsBuiltIn() {
|
|
continue
|
|
}
|
|
unknown[t] = true
|
|
}
|
|
if len(unknown) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// Match insert validation's sources: the types.custom config value
|
|
// (kept in step with the custom_types table by SyncConfigTables)
|
|
// overlaid with config.yaml-declared types. Read through s so a
|
|
// transaction-bound caller sees in-transaction registration.
|
|
existing, err := s.GetConfig(ctx, "types.custom")
|
|
if err != nil {
|
|
// Don't degrade to "nothing registered": a transient read failure
|
|
// would silently flatten types the operator did register.
|
|
return fmt.Errorf("reading types.custom: %w", err)
|
|
}
|
|
for _, t := range issueops.ParseTypesConfigValue(existing) {
|
|
delete(unknown, types.IssueType(t))
|
|
}
|
|
for _, t := range config.GetCustomTypesFromYAML() {
|
|
delete(unknown, types.IssueType(t))
|
|
}
|
|
if len(unknown) == 0 {
|
|
return nil
|
|
}
|
|
|
|
names := make([]string, 0, len(unknown))
|
|
for t := range unknown {
|
|
names = append(names, string(t))
|
|
}
|
|
sort.Strings(names)
|
|
WarnError("flattening unregistered issue type(s) to task (epic for steps with children): %s (register with bd config set types.custom to keep them)", strings.Join(names, ", "))
|
|
|
|
hasChildren := make(map[string]bool)
|
|
for _, dep := range deps {
|
|
if dep.Type == types.DepParentChild {
|
|
hasChildren[dep.DependsOnID] = true
|
|
}
|
|
}
|
|
for _, issue := range issues {
|
|
if unknown[issue.IssueType] {
|
|
if hasChildren[issue.ID] {
|
|
issue.IssueType = types.TypeEpic
|
|
} else {
|
|
issue.IssueType = types.TypeTask
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// cloneSubgraph creates new issues from the template with variable substitution.
|
|
// Uses CloneOptions to control all spawn/bond behavior including dynamic bonding.
|
|
func cloneSubgraph(ctx context.Context, s storage.DoltStorage, subgraph *TemplateSubgraph, opts CloneOptions) (*InstantiateResult, error) {
|
|
if s == nil {
|
|
return nil, fmt.Errorf("no database connection")
|
|
}
|
|
|
|
var result *InstantiateResult
|
|
err := transact(ctx, s, "bd: clone template subgraph", func(tx storage.Transaction) error {
|
|
r, err := cloneSubgraphInto(ctx, storeMolWriter{DoltStorage: s, tx: tx}, subgraph, opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result = r
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func cloneSubgraphInto(ctx context.Context, w molWriter, subgraph *TemplateSubgraph, opts CloneOptions) (*InstantiateResult, error) {
|
|
if err := flattenUnregisteredIssueTypes(ctx, w, subgraph.Issues, subgraph.Dependencies); err != nil {
|
|
return nil, fmt.Errorf("checking custom types for subgraph: %w", err)
|
|
}
|
|
|
|
idMapping := make(map[string]string)
|
|
|
|
// First pass: create all issues with new IDs
|
|
for _, oldIssue := range subgraph.Issues {
|
|
// RootOnly: skip child issues, only create the root
|
|
if opts.RootOnly && oldIssue.ID != subgraph.Root.ID {
|
|
continue
|
|
}
|
|
// Determine assignee: use override for root epic, otherwise substitute
|
|
// the template's. Step.assignee is documented as supporting
|
|
// substitution, and an unsubstituted one is worse than cosmetic - it
|
|
// makes the poured bead unclosable, because close refuses when the
|
|
// actor doesn't match the assignee (GH#5754). The --assignee override
|
|
// is a literal value supplied on the command line, so it wins as-is.
|
|
issueAssignee := substituteVariables(oldIssue.Assignee, opts.Vars)
|
|
if oldIssue.ID == subgraph.Root.ID && opts.Assignee != "" {
|
|
issueAssignee = opts.Assignee
|
|
}
|
|
|
|
// Every field substituted below - and issueAssignee above - is one a
|
|
// --var can reach, so substitutedIssueFields must name it too - that
|
|
// read side is what checkUnknownVars uses to decide a supplied name is
|
|
// usable.
|
|
newIssue := &types.Issue{
|
|
// ID will be set below based on bonding options
|
|
Title: substituteVariables(oldIssue.Title, opts.Vars),
|
|
Description: substituteVariables(oldIssue.Description, opts.Vars),
|
|
Design: substituteVariables(oldIssue.Design, opts.Vars),
|
|
AcceptanceCriteria: substituteVariables(oldIssue.AcceptanceCriteria, opts.Vars),
|
|
Notes: substituteVariables(oldIssue.Notes, opts.Vars),
|
|
Status: types.StatusOpen, // Always start fresh
|
|
Priority: oldIssue.Priority,
|
|
IssueType: oldIssue.IssueType,
|
|
Assignee: issueAssignee,
|
|
EstimatedMinutes: oldIssue.EstimatedMinutes,
|
|
Ephemeral: opts.Ephemeral, // mark for cleanup when closed
|
|
IDPrefix: opts.Prefix, // distinct prefixes for mols/wisps
|
|
// Gate fields (for async coordination)
|
|
AwaitType: oldIssue.AwaitType,
|
|
AwaitID: substituteVariables(oldIssue.AwaitID, opts.Vars),
|
|
Timeout: oldIssue.Timeout,
|
|
Labels: substituteLabels(oldIssue.Labels, opts.Vars),
|
|
Metadata: substituteMetadataVars(oldIssue.Metadata, opts.Vars),
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
// Generate custom ID for dynamic bonding if ParentID is set
|
|
if opts.ParentID != "" {
|
|
bondedID, err := generateBondedID(oldIssue.ID, subgraph.Root.ID, opts)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to generate bonded ID for %s: %w", oldIssue.ID, err)
|
|
}
|
|
newIssue.ID = bondedID
|
|
}
|
|
|
|
if err := w.CreateIssue(ctx, newIssue, opts.Actor); err != nil {
|
|
return nil, fmt.Errorf("failed to create issue from %s: %w", oldIssue.ID, err)
|
|
}
|
|
|
|
idMapping[oldIssue.ID] = newIssue.ID
|
|
}
|
|
|
|
// Second pass: recreate dependencies with new IDs
|
|
for _, dep := range subgraph.Dependencies {
|
|
newFromID, ok1 := idMapping[dep.IssueID]
|
|
newToID, ok2 := idMapping[dep.DependsOnID]
|
|
if !ok1 || !ok2 {
|
|
continue // Skip if either end is outside the subgraph
|
|
}
|
|
|
|
newDep := &types.Dependency{
|
|
IssueID: newFromID,
|
|
DependsOnID: newToID,
|
|
Type: dep.Type,
|
|
Metadata: dep.Metadata,
|
|
}
|
|
if err := w.AddDependency(ctx, newDep, opts.Actor); err != nil {
|
|
return nil, fmt.Errorf("failed to create dependency: %w", err)
|
|
}
|
|
}
|
|
|
|
// Atomic attachment: link spawned root to target molecule within
|
|
// the same transaction (bd-wvplu: prevents orphaned spawns)
|
|
if opts.AttachToID != "" {
|
|
attachDep := &types.Dependency{
|
|
IssueID: idMapping[subgraph.Root.ID],
|
|
DependsOnID: opts.AttachToID,
|
|
Type: opts.AttachDepType,
|
|
}
|
|
if err := w.AddDependency(ctx, attachDep, opts.Actor); err != nil {
|
|
return nil, fmt.Errorf("attaching to molecule: %w", err)
|
|
}
|
|
}
|
|
|
|
return &InstantiateResult{
|
|
NewEpicID: idMapping[subgraph.Root.ID],
|
|
IDMapping: idMapping,
|
|
Created: len(idMapping),
|
|
}, nil
|
|
}
|
|
|
|
// printTemplateTree prints the template structure as a tree.
|
|
// Uses a visited set to detect cycles (GH#2719) and avoid infinite recursion.
|
|
func printTemplateTree(subgraph *TemplateSubgraph, parentID string, depth int, isRoot bool) {
|
|
visited := make(map[string]bool)
|
|
printTemplateTreeVisited(subgraph, parentID, depth, isRoot, visited)
|
|
}
|
|
|
|
// printTemplateTreeVisited is the internal recursive implementation with cycle tracking.
|
|
func printTemplateTreeVisited(subgraph *TemplateSubgraph, parentID string, depth int, isRoot bool, visited map[string]bool) {
|
|
indent := strings.Repeat(" ", depth)
|
|
|
|
// Print root
|
|
if isRoot {
|
|
fmt.Printf("%s %s (root)\n", indent, subgraph.Root.Title)
|
|
visited[parentID] = true
|
|
}
|
|
|
|
// Find children of this parent
|
|
var children []*types.Issue
|
|
for _, dep := range subgraph.Dependencies {
|
|
if dep.DependsOnID == parentID && dep.Type == types.DepParentChild {
|
|
if child, ok := subgraph.IssueMap[dep.IssueID]; ok {
|
|
children = append(children, child)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Print children
|
|
for i, child := range children {
|
|
connector := "├──"
|
|
if i == len(children)-1 {
|
|
connector = "└──"
|
|
}
|
|
vars := extractVariables(child.Title)
|
|
varStr := ""
|
|
if len(vars) > 0 {
|
|
varStr = fmt.Sprintf(" [%s]", strings.Join(vars, ", "))
|
|
}
|
|
|
|
// Cycle detection (GH#2719)
|
|
if visited[child.ID] {
|
|
fmt.Printf("%s %s %s%s (cycle detected, skipping)\n", indent, connector, child.Title, varStr)
|
|
continue
|
|
}
|
|
fmt.Printf("%s %s %s%s\n", indent, connector, child.Title, varStr)
|
|
visited[child.ID] = true
|
|
printTemplateTreeVisited(subgraph, child.ID, depth+1, false, visited)
|
|
}
|
|
}
|