bd mol run: add cross-database spawning support (gt-jsup)

Add --template-db flag and auto-discovery for wisp molecule spawning.
When --db contains .beads-wisp, auto-discover the main database to read
templates from while writing spawned instances to wisp storage.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-12-23 16:39:18 -08:00
parent c2d6b130d9
commit 2ac190f26d
2 changed files with 43 additions and 6 deletions

View File

@@ -6,6 +6,8 @@ import (
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/ui"
"github.com/steveyegge/beads/internal/utils"
@@ -25,9 +27,15 @@ This command:
After a crash or session reset, the pinned root issue ensures the agent
can resume from where it left off by checking 'bd ready'.
The --template-db flag enables cross-database spawning: read templates from
one database (e.g., main) while writing spawned instances to another (e.g., wisp).
This is essential for wisp molecule spawning where templates exist in the main
database but instances should be ephemeral.
Example:
bd mol run mol-version-bump --var version=1.2.0
bd mol run bd-qqc --var version=0.32.0 --var date=2025-01-01`,
bd mol run bd-qqc --var version=0.32.0 --var date=2025-01-01
bd --db .beads-wisp/beads.db mol run mol-patrol --template-db .beads/beads.db`,
Args: cobra.ExactArgs(1),
Run: runMolRun,
}
@@ -49,6 +57,7 @@ func runMolRun(cmd *cobra.Command, args []string) {
}
varFlags, _ := cmd.Flags().GetStringSlice("var")
templateDB, _ := cmd.Flags().GetString("template-db")
// Parse variables
vars := make(map[string]string)
@@ -61,15 +70,42 @@ func runMolRun(cmd *cobra.Command, args []string) {
vars[parts[0]] = parts[1]
}
// Resolve molecule ID
moleculeID, err := utils.ResolvePartialID(ctx, store, args[0])
// Determine which store to use for reading the template
// If --template-db is set, open a separate connection for reading the template
// This enables cross-database spawning (read from main, write to wisp)
//
// Auto-discovery: if --db contains ".beads-wisp" (wisp storage) but --template-db
// is not set, automatically use the main database for templates. This handles the
// common case of spawning patrol molecules from main DB into wisp storage.
templateStore := store
if templateDB == "" && strings.Contains(dbPath, ".beads-wisp") {
// Auto-discover main database for templates
templateDB = beads.FindDatabasePath()
if templateDB == "" {
fmt.Fprintf(os.Stderr, "Error: cannot find main database for templates\n")
fmt.Fprintf(os.Stderr, "Hint: specify --template-db explicitly\n")
os.Exit(1)
}
}
if templateDB != "" {
var err error
templateStore, err = sqlite.NewWithTimeout(ctx, templateDB, lockTimeout)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening template database %s: %v\n", templateDB, err)
os.Exit(1)
}
defer templateStore.Close()
}
// Resolve molecule ID from template store
moleculeID, err := utils.ResolvePartialID(ctx, templateStore, args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving molecule ID %s: %v\n", args[0], err)
os.Exit(1)
}
// Load the molecule subgraph
subgraph, err := loadTemplateSubgraph(ctx, store, moleculeID)
// Load the molecule subgraph from template store
subgraph, err := loadTemplateSubgraph(ctx, templateStore, moleculeID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading molecule: %v\n", err)
os.Exit(1)
@@ -132,6 +168,7 @@ func runMolRun(cmd *cobra.Command, args []string) {
func init() {
molRunCmd.Flags().StringSlice("var", []string{}, "Variable substitution (key=value)")
molRunCmd.Flags().String("template-db", "", "Database to read templates from (enables cross-database spawning)")
molCmd.AddCommand(molRunCmd)
}