feat: add cross-store wisp→digest squash (bd-kwjh.4)

- Add wisp detection in mol squash: checks wisp storage if not in main
- squashWispToPermanent: creates digest in permanent, deletes from wisp
- Fix directory naming: .beads-wisps → .beads-wisp (singular, matches doc)
- Add comprehensive tests for wisp squash scenarios

🤖 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-22 00:54:17 -08:00
parent 1d3c9e6a83
commit 69911070f0
5 changed files with 499 additions and 21 deletions

View File

@@ -33,7 +33,7 @@ Bond types:
conditional - B runs only if A fails
Wisp storage (ephemeral molecules):
Use --wisp to create molecules in .beads-wisps/ instead of .beads/.
Use --wisp to create molecules in .beads-wisp/ instead of .beads/.
Wisps are local-only, gitignored, and not synced - the "steam" of Gas Town.
Use bd mol squash to convert a wisp to a digest in permanent storage.
Use bd mol burn to delete a wisp without creating a digest.
@@ -149,7 +149,7 @@ func runMolBond(cmd *cobra.Command, args []string) {
fmt.Printf(" B: %s (%s)\n", issueB.Title, operandType(bIsProto))
fmt.Printf(" Bond type: %s\n", bondType)
if wisp {
fmt.Printf(" Storage: wisp (.beads-wisps/)\n")
fmt.Printf(" Storage: wisp (.beads-wisp/)\n")
}
if aIsProto && bIsProto {
fmt.Printf(" Result: compound proto\n")
@@ -203,7 +203,7 @@ func runMolBond(cmd *cobra.Command, args []string) {
fmt.Printf(" Spawned: %d issues\n", result.Spawned)
}
if wisp {
fmt.Printf(" Storage: wisp (.beads-wisps/)\n")
fmt.Printf(" Storage: wisp (.beads-wisp/)\n")
}
}
@@ -418,7 +418,7 @@ func init() {
molBondCmd.Flags().String("as", "", "Custom title for compound proto (proto+proto only)")
molBondCmd.Flags().Bool("dry-run", false, "Preview what would be created")
molBondCmd.Flags().StringSlice("var", []string{}, "Variable substitution for spawned protos (key=value)")
molBondCmd.Flags().Bool("wisp", false, "Create molecule in wisp storage (.beads-wisps/)")
molBondCmd.Flags().Bool("wisp", false, "Create molecule in wisp storage (.beads-wisp/)")
molCmd.AddCommand(molBondCmd)
}

View File

@@ -8,6 +8,7 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types"
@@ -50,12 +51,13 @@ Example:
// SquashResult holds the result of a squash operation
type SquashResult struct {
MoleculeID string `json:"molecule_id"`
DigestID string `json:"digest_id"`
SquashedIDs []string `json:"squashed_ids"`
SquashedCount int `json:"squashed_count"`
DeletedCount int `json:"deleted_count"`
KeptChildren bool `json:"kept_children"`
MoleculeID string `json:"molecule_id"`
DigestID string `json:"digest_id"`
SquashedIDs []string `json:"squashed_ids"`
SquashedCount int `json:"squashed_count"`
DeletedCount int `json:"deleted_count"`
KeptChildren bool `json:"kept_children"`
WispSquash bool `json:"wisp_squash,omitempty"` // True if this was a wisp→digest squash
}
func runMolSquash(cmd *cobra.Command, args []string) {
@@ -78,14 +80,55 @@ func runMolSquash(cmd *cobra.Command, args []string) {
keepChildren, _ := cmd.Flags().GetBool("keep-children")
summary, _ := cmd.Flags().GetString("summary")
// Resolve molecule ID
// Try to resolve molecule ID in main store first
moleculeID, err := utils.ResolvePartialID(ctx, store, args[0])
// If not found in main store, check wisp storage
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving molecule ID %s: %v\n", args[0], err)
// Try wisp storage
wispStore, wispErr := beads.NewWispStorage(ctx)
if wispErr != nil {
// No wisp storage available, report original error
fmt.Fprintf(os.Stderr, "Error resolving molecule ID %s: %v\n", args[0], err)
os.Exit(1)
}
defer wispStore.Close()
wispMolID, wispResolveErr := utils.ResolvePartialID(ctx, wispStore, args[0])
if wispResolveErr != nil {
// Not found in either store
fmt.Fprintf(os.Stderr, "Error resolving molecule ID %s: %v\n", args[0], err)
os.Exit(1)
}
// Found in wisp storage - do cross-store squash
runWispSquash(ctx, cmd, wispStore, store, wispMolID, dryRun, keepChildren, summary)
return
}
// Found in main store - check if it's actually a wisp by looking at root
issue, err := store.GetIssue(ctx, moleculeID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading molecule: %v\n", err)
os.Exit(1)
}
// Load the molecule subgraph
// If the root itself is a wisp, check if it should be in wisp storage
// This handles the case where someone created a wisp in main store by mistake
if issue.Wisp {
// Check if there's a corresponding wisp in wisp storage
wispStore, wispErr := beads.NewWispStorage(ctx)
if wispErr == nil {
defer wispStore.Close()
if wispIssue, _ := wispStore.GetIssue(ctx, moleculeID); wispIssue != nil {
// Found in wisp storage - do cross-store squash
runWispSquash(ctx, cmd, wispStore, store, moleculeID, dryRun, keepChildren, summary)
return
}
}
}
// Load the molecule subgraph from main store
subgraph, err := loadTemplateSubgraph(ctx, store, moleculeID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading molecule: %v\n", err)
@@ -163,6 +206,80 @@ func runMolSquash(cmd *cobra.Command, args []string) {
}
}
// runWispSquash handles squashing a wisp from wisp storage into permanent storage.
// This is the cross-store squash operation: load from wisp, create digest in permanent, delete wisp.
func runWispSquash(ctx context.Context, cmd *cobra.Command, wispStore, permanentStore storage.Storage, moleculeID string, dryRun, keepChildren bool, summary string) {
// Load the molecule subgraph from wisp storage
subgraph, err := loadTemplateSubgraph(ctx, wispStore, moleculeID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading wisp molecule: %v\n", err)
os.Exit(1)
}
// Collect all issues in the wisp (including root)
allIssues := subgraph.Issues
if dryRun {
fmt.Printf("\nDry run: would squash wisp %s → permanent digest\n\n", moleculeID)
fmt.Printf("Root: %s\n", subgraph.Root.Title)
fmt.Printf("Storage: .beads-wisp/ → .beads/\n")
fmt.Printf("\nIssues to squash (%d total):\n", len(allIssues))
for _, issue := range allIssues {
status := string(issue.Status)
if issue.ID == subgraph.Root.ID {
fmt.Printf(" - [%s] %s (%s) [ROOT]\n", status, issue.Title, issue.ID)
} else {
fmt.Printf(" - [%s] %s (%s)\n", status, issue.Title, issue.ID)
}
}
fmt.Printf("\nDigest preview:\n")
// For wisp squash, we generate a digest from all issues
var children []*types.Issue
for _, issue := range allIssues {
if issue.ID != subgraph.Root.ID {
children = append(children, issue)
}
}
digest := generateDigest(subgraph.Root, children)
if len(digest) > 500 {
fmt.Printf("%s...\n", digest[:500])
} else {
fmt.Printf("%s\n", digest)
}
if keepChildren {
fmt.Printf("\n--keep-children: wisp would NOT be deleted from .beads-wisp/\n")
} else {
fmt.Printf("\nWisp will be deleted from .beads-wisp/ after digest creation.\n")
}
return
}
// Perform the cross-store squash
result, err := squashWispToPermanent(ctx, wispStore, permanentStore, subgraph, keepChildren, summary, actor)
if err != nil {
fmt.Fprintf(os.Stderr, "Error squashing wisp: %v\n", err)
os.Exit(1)
}
// Schedule auto-flush for permanent store
markDirtyAndScheduleFlush()
if jsonOutput {
outputJSON(result)
return
}
fmt.Printf("%s Squashed wisp → permanent digest\n", ui.RenderPass("✓"))
fmt.Printf(" Wisp: %s (.beads-wisp/)\n", moleculeID)
fmt.Printf(" Digest: %s (.beads/)\n", result.DigestID)
fmt.Printf(" Squashed: %d issues\n", result.SquashedCount)
if result.DeletedCount > 0 {
fmt.Printf(" Deleted: %d issues from wisp storage\n", result.DeletedCount)
} else if result.KeptChildren {
fmt.Printf(" Wisp preserved (--keep-children)\n")
}
}
// generateDigest creates a summary from the molecule execution
// Tier 2: Simple concatenation of titles and descriptions
// Tier 3 (future): AI-powered summarization using Haiku
@@ -314,6 +431,85 @@ func deleteWispChildren(ctx context.Context, s storage.Storage, ids []string) (i
return deleted, lastErr
}
// squashWispToPermanent performs a cross-store squash: wisp → permanent digest.
// This is the key operation for wisp lifecycle management:
// 1. Creates a digest issue in permanent storage summarizing the wisp's work
// 2. Deletes the entire wisp molecule from wisp storage
//
// The digest captures the outcome of the ephemeral work without preserving
// the full execution trace (which would accumulate unbounded over time).
func squashWispToPermanent(ctx context.Context, wispStore, permanentStore storage.Storage, subgraph *MoleculeSubgraph, keepChildren bool, summary string, actorName string) (*SquashResult, error) {
if wispStore == nil || permanentStore == nil {
return nil, fmt.Errorf("both wisp and permanent stores are required")
}
root := subgraph.Root
// Collect all issue IDs (including root)
var allIDs []string
var children []*types.Issue
for _, issue := range subgraph.Issues {
allIDs = append(allIDs, issue.ID)
if issue.ID != root.ID {
children = append(children, issue)
}
}
// Use agent-provided summary if available, otherwise generate basic digest
var digestContent string
if summary != "" {
digestContent = summary
} else {
digestContent = generateDigest(root, children)
}
// Create digest issue in permanent storage (not a wisp)
now := time.Now()
digestIssue := &types.Issue{
Title: fmt.Sprintf("Digest: %s @ %s", root.Title, now.Format("2006-01-02 15:04")),
Description: digestContent,
Status: types.StatusClosed,
CloseReason: fmt.Sprintf("Squashed from wisp %s (%d issues)", root.ID, len(subgraph.Issues)),
Priority: root.Priority,
IssueType: types.TypeTask,
Wisp: false, // Digest is permanent
ClosedAt: &now,
}
result := &SquashResult{
MoleculeID: root.ID,
SquashedIDs: allIDs,
SquashedCount: len(subgraph.Issues),
KeptChildren: keepChildren,
WispSquash: true,
}
// Create digest in permanent storage
err := permanentStore.RunInTransaction(ctx, func(tx storage.Transaction) error {
if err := tx.CreateIssue(ctx, digestIssue, actorName); err != nil {
return fmt.Errorf("failed to create digest issue: %w", err)
}
result.DigestID = digestIssue.ID
return nil
})
if err != nil {
return nil, err
}
// Delete wisp issues from wisp storage (unless --keep-children)
if !keepChildren {
deleted, err := deleteWispChildren(ctx, wispStore, allIDs)
if err != nil {
// Log but don't fail - digest was created successfully
fmt.Fprintf(os.Stderr, "Warning: failed to delete some wisp issues: %v\n", err)
}
result.DeletedCount = deleted
}
return result, nil
}
func init() {
molSquashCmd.Flags().Bool("dry-run", false, "Preview what would be squashed")
molSquashCmd.Flags().Bool("keep-children", false, "Don't delete wisp children after squash")

View File

@@ -1282,6 +1282,288 @@ func TestSpawnAttachDryRunOutput(t *testing.T) {
}
}
// TestSquashWispToPermanent tests cross-store squash: wisp → permanent digest (bd-kwjh.4)
func TestSquashWispToPermanent(t *testing.T) {
ctx := context.Background()
// Create separate wisp and permanent stores
wispPath := t.TempDir() + "/wisp.db"
permPath := t.TempDir() + "/permanent.db"
wispStore, err := sqlite.New(ctx, wispPath)
if err != nil {
t.Fatalf("Failed to create wisp store: %v", err)
}
defer wispStore.Close()
if err := wispStore.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("Failed to set wisp config: %v", err)
}
permStore, err := sqlite.New(ctx, permPath)
if err != nil {
t.Fatalf("Failed to create permanent store: %v", err)
}
defer permStore.Close()
if err := permStore.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("Failed to set permanent config: %v", err)
}
// Create a wisp molecule in wisp storage
wispRoot := &types.Issue{
Title: "Deacon Patrol Cycle",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeEpic,
Wisp: true,
}
if err := wispStore.CreateIssue(ctx, wispRoot, "test"); err != nil {
t.Fatalf("Failed to create wisp root: %v", err)
}
wispChild1 := &types.Issue{
Title: "Check witnesses",
Description: "Verified 3 witnesses healthy",
Status: types.StatusClosed,
Priority: 2,
IssueType: types.TypeTask,
Wisp: true,
CloseReason: "All healthy",
}
wispChild2 := &types.Issue{
Title: "Process mail queue",
Description: "Processed 5 mail items",
Status: types.StatusClosed,
Priority: 2,
IssueType: types.TypeTask,
Wisp: true,
CloseReason: "Mail delivered",
}
if err := wispStore.CreateIssue(ctx, wispChild1, "test"); err != nil {
t.Fatalf("Failed to create wisp child1: %v", err)
}
if err := wispStore.CreateIssue(ctx, wispChild2, "test"); err != nil {
t.Fatalf("Failed to create wisp child2: %v", err)
}
// Add parent-child dependencies
if err := wispStore.AddDependency(ctx, &types.Dependency{
IssueID: wispChild1.ID,
DependsOnID: wispRoot.ID,
Type: types.DepParentChild,
}, "test"); err != nil {
t.Fatalf("Failed to add child1 dependency: %v", err)
}
if err := wispStore.AddDependency(ctx, &types.Dependency{
IssueID: wispChild2.ID,
DependsOnID: wispRoot.ID,
Type: types.DepParentChild,
}, "test"); err != nil {
t.Fatalf("Failed to add child2 dependency: %v", err)
}
// Load the subgraph
subgraph, err := loadTemplateSubgraph(ctx, wispStore, wispRoot.ID)
if err != nil {
t.Fatalf("Failed to load wisp subgraph: %v", err)
}
// Verify subgraph loaded correctly
if len(subgraph.Issues) != 3 {
t.Fatalf("Expected 3 issues in subgraph, got %d", len(subgraph.Issues))
}
// Perform cross-store squash
result, err := squashWispToPermanent(ctx, wispStore, permStore, subgraph, false, "", "test")
if err != nil {
t.Fatalf("squashWispToPermanent failed: %v", err)
}
// Verify result
if result.SquashedCount != 3 {
t.Errorf("SquashedCount = %d, want 3", result.SquashedCount)
}
if !result.WispSquash {
t.Error("WispSquash should be true")
}
if result.DigestID == "" {
t.Error("DigestID should not be empty")
}
if result.DeletedCount != 3 {
t.Errorf("DeletedCount = %d, want 3", result.DeletedCount)
}
// Verify digest was created in permanent storage
digest, err := permStore.GetIssue(ctx, result.DigestID)
if err != nil {
t.Fatalf("Failed to get digest from permanent store: %v", err)
}
if digest.Wisp {
t.Error("Digest should NOT be a wisp")
}
if digest.Status != types.StatusClosed {
t.Errorf("Digest status = %v, want closed", digest.Status)
}
if !strings.Contains(digest.Title, "Deacon Patrol Cycle") {
t.Errorf("Digest title %q should contain original molecule title", digest.Title)
}
if !strings.Contains(digest.Description, "Check witnesses") {
t.Error("Digest description should contain child titles")
}
// Verify wisps were deleted from wisp storage
// Note: GetIssue returns (nil, nil) when issue doesn't exist
rootIssue, err := wispStore.GetIssue(ctx, wispRoot.ID)
if err != nil {
t.Errorf("Unexpected error checking root deletion: %v", err)
}
if rootIssue != nil {
t.Error("Wisp root should have been deleted")
}
child1Issue, err := wispStore.GetIssue(ctx, wispChild1.ID)
if err != nil {
t.Errorf("Unexpected error checking child1 deletion: %v", err)
}
if child1Issue != nil {
t.Error("Wisp child1 should have been deleted")
}
child2Issue, err := wispStore.GetIssue(ctx, wispChild2.ID)
if err != nil {
t.Errorf("Unexpected error checking child2 deletion: %v", err)
}
if child2Issue != nil {
t.Error("Wisp child2 should have been deleted")
}
}
// TestSquashWispToPermanentWithSummary tests that agent summaries override auto-generation
func TestSquashWispToPermanentWithSummary(t *testing.T) {
ctx := context.Background()
wispPath := t.TempDir() + "/wisp.db"
permPath := t.TempDir() + "/permanent.db"
wispStore, err := sqlite.New(ctx, wispPath)
if err != nil {
t.Fatalf("Failed to create wisp store: %v", err)
}
defer wispStore.Close()
if err := wispStore.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("Failed to set wisp config: %v", err)
}
permStore, err := sqlite.New(ctx, permPath)
if err != nil {
t.Fatalf("Failed to create permanent store: %v", err)
}
defer permStore.Close()
if err := permStore.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("Failed to set permanent config: %v", err)
}
// Create a simple wisp molecule
wispRoot := &types.Issue{
Title: "Patrol Cycle",
Status: types.StatusClosed,
Priority: 1,
IssueType: types.TypeEpic,
Wisp: true,
}
if err := wispStore.CreateIssue(ctx, wispRoot, "test"); err != nil {
t.Fatalf("Failed to create wisp root: %v", err)
}
subgraph := &MoleculeSubgraph{
Root: wispRoot,
Issues: []*types.Issue{wispRoot},
}
// Squash with agent-provided summary
agentSummary := "## AI-Generated Patrol Summary\n\nAll systems healthy. No issues found."
result, err := squashWispToPermanent(ctx, wispStore, permStore, subgraph, true, agentSummary, "test")
if err != nil {
t.Fatalf("squashWispToPermanent failed: %v", err)
}
// Verify digest uses agent summary
digest, err := permStore.GetIssue(ctx, result.DigestID)
if err != nil {
t.Fatalf("Failed to get digest: %v", err)
}
if digest.Description != agentSummary {
t.Errorf("Digest should use agent summary.\nGot: %s\nWant: %s", digest.Description, agentSummary)
}
}
// TestSquashWispToPermanentKeepChildren tests --keep-children flag
func TestSquashWispToPermanentKeepChildren(t *testing.T) {
ctx := context.Background()
wispPath := t.TempDir() + "/wisp.db"
permPath := t.TempDir() + "/permanent.db"
wispStore, err := sqlite.New(ctx, wispPath)
if err != nil {
t.Fatalf("Failed to create wisp store: %v", err)
}
defer wispStore.Close()
if err := wispStore.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("Failed to set wisp config: %v", err)
}
permStore, err := sqlite.New(ctx, permPath)
if err != nil {
t.Fatalf("Failed to create permanent store: %v", err)
}
defer permStore.Close()
if err := permStore.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("Failed to set permanent config: %v", err)
}
// Create a wisp molecule
wispRoot := &types.Issue{
Title: "Test Molecule",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeEpic,
Wisp: true,
}
if err := wispStore.CreateIssue(ctx, wispRoot, "test"); err != nil {
t.Fatalf("Failed to create wisp root: %v", err)
}
subgraph := &MoleculeSubgraph{
Root: wispRoot,
Issues: []*types.Issue{wispRoot},
}
// Squash with keepChildren=true
result, err := squashWispToPermanent(ctx, wispStore, permStore, subgraph, true, "", "test")
if err != nil {
t.Fatalf("squashWispToPermanent failed: %v", err)
}
// Verify no deletion
if result.DeletedCount != 0 {
t.Errorf("DeletedCount = %d, want 0 (keep-children)", result.DeletedCount)
}
if !result.KeptChildren {
t.Error("KeptChildren should be true")
}
// Wisp should still exist
_, err = wispStore.GetIssue(ctx, wispRoot.ID)
if err != nil {
t.Error("Wisp should still exist with --keep-children")
}
// Digest should still be created
_, err = permStore.GetIssue(ctx, result.DigestID)
if err != nil {
t.Error("Digest should be created even with --keep-children")
}
}
// TestWispFilteringFromExport verifies that wisp issues are filtered
// from JSONL export (bd-687g). Wisp issues should only exist in SQLite,
// not in issues.jsonl, to prevent "zombie" resurrection after mol squash.

View File

@@ -625,7 +625,7 @@ func FindAllDatabases() []DatabaseInfo {
// WispDirName is the default name for the wisp storage directory.
// This directory is a sibling to .beads/ and should be gitignored.
// Wisps are ephemeral molecules - the "steam" in Gas Town's engine metaphor.
const WispDirName = ".beads-wisps"
const WispDirName = ".beads-wisp"
// FindWispDir locates or determines the wisp storage directory.
// The wisp directory is a sibling to the .beads directory.
@@ -639,7 +639,7 @@ func FindWispDir() string {
}
// Wisp dir is a sibling to .beads
// e.g., /project/.beads -> /project/.beads-wisps
// e.g., /project/.beads -> /project/.beads-wisp
projectRoot := filepath.Dir(beadsDir)
return filepath.Join(projectRoot, WispDirName)
}
@@ -720,7 +720,7 @@ func EnsureWispGitignore() error {
}
// IsWispDatabase checks if a database path is a wisp database.
// Returns true if the database is in a .beads-wisps directory.
// Returns true if the database is in a .beads-wisp directory.
func IsWispDatabase(dbPath string) bool {
if dbPath == "" {
return false

View File

@@ -1402,17 +1402,17 @@ func TestIsWispDatabase(t *testing.T) {
},
{
name: "wisp database",
dbPath: "/project/.beads-wisps/beads.db",
dbPath: "/project/.beads-wisp/beads.db",
expected: true,
},
{
name: "nested wisp",
dbPath: "/some/deep/path/.beads-wisps/beads.db",
dbPath: "/some/deep/path/.beads-wisp/beads.db",
expected: true,
},
{
name: "similar but not wisp",
dbPath: "/project/.beads-wisps-backup/beads.db",
dbPath: "/project/.beads-wisp-backup/beads.db",
expected: false,
},
}
@@ -1452,12 +1452,12 @@ func TestEnsureWispGitignore(t *testing.T) {
},
{
name: "already gitignored",
existingContent: ".beads-wisps/\n",
existingContent: ".beads-wisp/\n",
expectAppend: false,
},
{
name: "already gitignored without slash",
existingContent: ".beads-wisps\n",
existingContent: ".beads-wisp\n",
expectAppend: false,
},
{