beads/cmd/bd/mol_squash.go
Julian Knutsen 5126de8c1a
perf(bd): no git subprocesses on read-only commands (in-process repo discovery, lazy actor, backup probe) (#7400)
* perf(git): locate the repository in-process instead of running git rev-parse

Every bd invocation resolved the process-wide git context (beads directory
discovery reads it) with `git rev-parse --git-dir --git-common-dir
--show-toplevel`: one git subprocess per bd call before any work. Tools that
call bd hundreds of times (gascity's doctor issues ~250 bd calls per run)
paid it on every call.

On Linux, initGitContext now first runs discoverGitInProcess, which mirrors
Git's discovery walk (physical cwd, .git as directory or gitfile, bare-dir
check, filesystem-boundary stop) and produces byte-for-byte what rev-parse
prints for ordinary repositories (top and subdirectories), linked worktrees
(absolute or relative gitfile, symlinked targets), submodules (core.worktree
pointing back at the gitfile's directory), nested repositories and no
repository. Both answers feed one shared normalization
(gitContextFromRevParse), so downstream values are unchanged.

It declines, and git runs exactly as before, whenever it cannot prove the
answer: GIT_DIR / GIT_WORK_TREE / GIT_COMMON_DIR / GIT_CEILING_DIRECTORIES /
GIT_DISCOVERY_ACROSS_FILESYSTEM / GIT_OBJECT_DIRECTORY / GIT_CONFIG_PARAMETERS
/ GIT_CONFIG_COUNT in the environment; a symlinked .git, an invalid gitfile
or git directory, a cwd inside a git directory; repository config with
core.worktree (other than the submodule shape), core.bare, extensions,
include directives, a format version above 1 or unmodeled syntax; any path
not owned by the effective uid (safe.directory is git's to evaluate), or
root under sudo. darwin and windows keep the subprocess (case folding and
ownership semantics).

Tests compare discoverGitFrom with real `git rev-parse` output across those
layouts, pin every decline case, and check the resulting cached context
equals the subprocess-derived one.

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

* perf(cli): resolve the git user.name actor fallback only when needed

PersistentPreRunE assigned `actor = getActorWithGit()` for every command,
which, with no --actor / BEADS_ACTOR / BD_ACTOR / config actor, runs
`git config user.name`: the second git subprocess of every bd call, paid
by read-only commands that never consult the actor.

The pre-run now calls deferActorGitFallback, which marks an empty actor as
pending; currentActor (and getActorWithGit, getActor) resolve it on first
use with the unchanged priority order and store it in the global and the
CommandContext copy, as the eager assignment did. Command code reads the
actor through currentActor() instead of the raw global (a mechanical
rewrite of every read site the compiler reported; writes, the flag binding
and the pre-run config binding keep the raw variable). setActor cancels a
pending fallback. The telemetry span's bd.actor attribute is still set, but
only for a recording span, so a noop span does not force the lookup.

With the in-process repository discovery from the previous commit, a
read-only `bd list` / `bd show` now starts no git process at startup, in
the main checkout and in a linked worktree; a write asks git for user.name
once (bd create previously asked once, now still once) and records the same
value. Measured on 50 sequential `bd list` calls: 100 -> 0 git execs
(embedded and proxied-server workspaces, main checkout and worktree).

TestEmbeddedReadOnlyCommandsSpawnNoGit counts git execs through a PATH shim;
TestDeferredActorGitFallback pins the deferral semantics.

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

* fix(git): decline in-process discovery on any config line git might read differently

Review follow-ups to the in-process repository discovery.

- The repository config scanner was looser than git: a config starting with
  a UTF-8 BOM followed by `[core] worktree = /other` made git report /other
  while discovery returned the cwd repository, and `[ core ]` or `!!! = x`
  (which git refuses) were accepted. scanGitConfig (gitconfig_linux.go) now
  skips a leading BOM exactly as git does and accepts only a narrow grammar:
  blank/comment lines, `[section]` / `[section "sub"]` headers at column 0,
  and `key` / `key = value` lines inside a section, with values free of
  backslashes and unbalanced quotes. Any other line declines to git.
- Walk from getcwd(2) (syscall.Getwd), as git does, instead of os.Getwd,
  which trusts $PWD when it names the same inode (bind-mount aliases).
- Resolve relative gitfile and commondir targets without filepath.Join's
  lexical Clean, so "link/.." resolves physically like git's realpath.
- A mount-boundary stop reports its own reason in the not-a-repository
  error.
- Tests: every decline case (BOM + core.worktree, malformed headers and
  keys, keys before a section, escapes, continuations, bare repositories,
  a commondir inside .git, ...) now also checks that what startup derives
  equals git's answer; BOM and CRLF/comment configs are equivalence cases;
  the no-repository case must be answered in-process instead of skipping;
  a $PWD alias test pins the getcwd walk.
- The exec-count test drops every GIT_* variable except the config-file
  selectors, so a runner exporting GIT_CONFIG_PARAMETERS & co. cannot make
  it fail spuriously.
- Comment the two places that touch the raw actor before the deferral.

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

* perf(backup): decide embedded auto-backup's git-remote probe without git

In an embedded workspace whose backup.enabled is unset, maybeAutoBackup
asks isBackupAutoEnabled -> primeHasGitRemote after every command, reads
included. That built a RepoContext (`git -C .beads rev-parse
--git-common-dir`) and ran `git remote`: two git processes per read even
after the startup calls were gone. The throttle cannot short-circuit it:
without a remote no backup ever runs, so there is no backup state to
throttle on.

Both probes are now answered in-process when provable, and the subprocess
remains the fallback, so the decision and every warning/notice downstream
of it are unchanged:

- internal/beads getGitCommonDirForPath uses git.CommonDirInProcess (the
  discovery walk from the given directory, git's spelling).
- primeHasGitRemote uses git.HasRemoteInProcess in the same directory
  primeGitCmd would run git in (primeGitDir, factored out). It answers only
  when every config file `git remote` reads -- system (GIT_CONFIG_SYSTEM, or
  /etc/gitconfig and <prefix>/etc/gitconfig of the git on PATH), global
  (GIT_CONFIG_GLOBAL, or XDG and ~/.gitconfig) and the repository's own --
  is absent or within scanGitConfig's grammar and include-free, no
  GIT_CONFIG_PARAMETERS/COUNT/GIT_CONFIG or discovery override is set, and
  no legacy .git/remotes or .git/branches entries exist. A remote seen only
  in a system-config candidate declines. Remote names outside
  [A-Za-z0-9._-] decline.

Tests compare HasRemoteInProcess with `git remote` (no remote, local,
worktree, submodule, remote.pushDefault only, global remote, no repo) and
pin the declines (global include, malformed global, command-line config);
CommonDirInProcess is compared with `git -C dir rev-parse
--git-common-dir`. TestEmbeddedReadOnlyCommandsSpawnNoGit now runs with
backup.enabled at its default, with and without a git remote, and still
expects zero git processes for list/show.

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

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-08 20:08:24 +00:00

344 lines
11 KiB
Go

package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/ui"
"github.com/steveyegge/beads/internal/utils"
)
var molSquashCmd = &cobra.Command{
Use: "squash <molecule-id>",
Short: "Compress molecule execution into a digest",
Long: `Squash a molecule's ephemeral children into a single digest issue.
This command collects all ephemeral child issues of a molecule (Ephemeral=true),
generates a summary digest, and promotes the wisps to persistent by
clearing their Wisp flag (or optionally deletes them).
The squash operation:
1. Loads the molecule and all its children
2. Filters to only wisps (ephemeral issues with Ephemeral=true)
3. Generates a digest (summary of work done)
4. Creates a permanent digest issue (Ephemeral=false)
5. Clears Wisp flag on children (promotes to persistent)
OR keeps them with --keep-children (default: delete)
AGENT INTEGRATION:
Use --summary to provide an AI-generated summary. This keeps bd as a pure
tool - the calling agent (orchestrator worker, Claude Code, etc.) is responsible
for generating intelligent summaries. Without --summary, a basic concatenation
of child issue content is used.
This is part of the wisp workflow: spawn creates wisps,
execution happens, squash compresses the trace into an outcome (digest).
Example:
bd mol squash bd-abc123 # Squash and promote children
bd mol squash bd-abc123 --dry-run # Preview what would be squashed
bd mol squash bd-abc123 --keep-children # Keep wisps after digest
bd mol squash bd-abc123 --summary "Agent-generated summary of work done"`,
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: runMolSquash,
}
// 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"`
WispSquash bool `json:"wisp_squash,omitempty"` // True if this was a wisp→digest squash
}
type molSquashInput struct {
moleculeArg string
dryRun bool
keepChildren bool
summary string
}
func gatherMolSquashInput(cmd *cobra.Command, args []string) molSquashInput {
in := molSquashInput{moleculeArg: args[0]}
in.dryRun, _ = cmd.Flags().GetBool("dry-run")
in.keepChildren, _ = cmd.Flags().GetBool("keep-children")
in.summary, _ = cmd.Flags().GetString("summary")
return in
}
func wispChildrenOf(subgraph *TemplateSubgraph) []*types.Issue {
var wispChildren []*types.Issue
for _, issue := range subgraph.Issues {
if issue.ID == subgraph.Root.ID {
continue
}
if issue.Ephemeral {
wispChildren = append(wispChildren, issue)
}
}
return wispChildren
}
func renderSquashDryRun(moleculeID string, subgraph *TemplateSubgraph, wispChildren []*types.Issue, keepChildren bool) {
fmt.Printf("\nDry run: would squash %d ephemeral children of %s\n\n", len(wispChildren), moleculeID)
fmt.Printf("Root: %s\n", subgraph.Root.Title)
fmt.Printf("\nWisp children to squash:\n")
for _, issue := range wispChildren {
status := string(issue.Status)
fmt.Printf(" - [%s] %s (%s)\n", status, issue.Title, issue.ID)
}
fmt.Printf("\nDigest preview:\n")
digest := generateDigest(subgraph.Root, wispChildren)
if len(digest) > 500 {
fmt.Printf("%s...\n", digest[:500])
} else {
fmt.Printf("%s\n", digest)
}
if keepChildren {
fmt.Printf("\n--keep-children: children would NOT be deleted\n")
} else {
fmt.Printf("\nChildren would be deleted after digest creation.\n")
}
}
func renderSquashResult(result *SquashResult) error {
if jsonOutput {
return outputJSON(result)
}
fmt.Printf("%s Squashed molecule: %d children → 1 digest\n", ui.RenderPass("✓"), result.SquashedCount)
fmt.Printf(" Digest ID: %s\n", result.DigestID)
if result.DeletedCount > 0 {
fmt.Printf(" Deleted: %d wisps\n", result.DeletedCount)
} else if result.KeptChildren {
fmt.Printf(" Children preserved (--keep-children)\n")
}
if result.WispSquash {
fmt.Printf(" Root auto-closed: %s\n", result.MoleculeID)
}
return nil
}
func runMolSquash(cmd *cobra.Command, args []string) error {
CheckReadonly("mol squash")
evt := metrics.NewCommandEvent("mol-squash")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
in := gatherMolSquashInput(cmd, args)
if usesProxiedServer() {
return runMolSquashProxiedServer(rootCtx, in)
}
ctx := rootCtx
if store == nil {
return HandleErrorWithHint("no database connection", diagHint())
}
moleculeID, err := utils.ResolvePartialID(ctx, store, in.moleculeArg)
if err != nil {
return HandleErrorRespectJSON("resolving molecule ID %s: %v", in.moleculeArg, err)
}
subgraph, err := loadTemplateSubgraph(ctx, store, moleculeID)
if err != nil {
return HandleErrorRespectJSON("loading molecule: %v", err)
}
wispChildren := wispChildrenOf(subgraph)
if len(wispChildren) == 0 {
if jsonOutput {
return outputJSON(SquashResult{
MoleculeID: moleculeID,
SquashedCount: 0,
})
}
fmt.Printf("No ephemeral children found for molecule %s\n", moleculeID)
return nil
}
if in.dryRun {
renderSquashDryRun(moleculeID, subgraph, wispChildren, in.keepChildren)
return nil
}
result, err := squashMolecule(ctx, store, subgraph.Root, wispChildren, in.keepChildren, in.summary, currentActor())
if err != nil {
return HandleErrorRespectJSON("squashing molecule: %v", err)
}
return renderSquashResult(result)
}
// generateDigest creates a summary from the molecule execution
// Tier 2: Simple concatenation of titles and descriptions
// Tier 3 (future): AI-powered summarization using Haiku
func generateDigest(root *types.Issue, children []*types.Issue) string {
var sb strings.Builder
sb.WriteString("## Molecule Execution Summary\n\n")
sb.WriteString(fmt.Sprintf("**Molecule**: %s\n", root.Title))
sb.WriteString(fmt.Sprintf("**Steps**: %d\n\n", len(children)))
// Count completed vs other statuses
completed := 0
inProgress := 0
for _, c := range children {
switch c.Status {
case types.StatusClosed:
completed++
case types.StatusInProgress:
inProgress++
}
}
sb.WriteString(fmt.Sprintf("**Completed**: %d/%d\n", completed, len(children)))
if inProgress > 0 {
sb.WriteString(fmt.Sprintf("**In Progress**: %d\n", inProgress))
}
sb.WriteString("\n---\n\n")
// List each step with its outcome
sb.WriteString("### Steps\n\n")
for i, child := range children {
status := string(child.Status)
sb.WriteString(fmt.Sprintf("%d. **[%s]** %s\n", i+1, status, child.Title))
if child.Description != "" {
// Include first 200 chars of description
desc := child.Description
if len(desc) > 200 {
desc = desc[:200] + "..."
}
sb.WriteString(fmt.Sprintf(" %s\n", desc))
}
if child.CloseReason != "" {
sb.WriteString(fmt.Sprintf(" *Outcome: %s*\n", child.CloseReason))
}
sb.WriteString("\n")
}
return sb.String()
}
// squashMolecule performs the squash operation
// If summary is provided (non-empty), it's used as the digest content.
// Otherwise, generateDigest() creates a basic concatenation.
// This enables agents to provide AI-generated summaries while keeping bd as a pure tool.
func squashMolecule(ctx context.Context, s storage.DoltStorage, root *types.Issue, children []*types.Issue, keepChildren bool, summary string, actorName string) (*SquashResult, error) {
if s == nil {
return nil, fmt.Errorf("no database connection")
}
var result *SquashResult
err := transact(ctx, s, fmt.Sprintf("bd: squash molecule %s", root.ID), func(tx storage.Transaction) error {
r, err := squashMoleculeInto(ctx, storeMolWriter{DoltStorage: s, tx: tx}, root, children, keepChildren, summary, actorName)
if err != nil {
return err
}
result = r
return nil
})
if err != nil {
return nil, err
}
return result, nil
}
func squashMoleculeInto(ctx context.Context, w molWriter, root *types.Issue, children []*types.Issue, keepChildren bool, summary string, actorName string) (*SquashResult, error) {
// Collect child IDs
childIDs := make([]string, len(children))
for i, c := range children {
childIDs[i] = c.ID
}
// 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 (permanent, not a wisp)
now := time.Now()
digestIssue := &types.Issue{
Title: fmt.Sprintf("Digest: %s", root.Title),
Description: digestContent,
Status: types.StatusClosed,
CloseReason: fmt.Sprintf("Squashed from %d wisps", len(children)),
Priority: root.Priority,
IssueType: types.TypeTask,
Ephemeral: false, // Digest is permanent, not a wisp
ClosedAt: &now,
}
result := &SquashResult{
MoleculeID: root.ID,
SquashedIDs: childIDs,
SquashedCount: len(children),
KeptChildren: keepChildren,
}
// Create digest issue
if err := w.CreateIssue(ctx, digestIssue, actorName); err != nil {
return nil, fmt.Errorf("failed to create digest issue: %w", err)
}
result.DigestID = digestIssue.ID
// Link digest to root as parent-child
dep := &types.Dependency{
IssueID: digestIssue.ID,
DependsOnID: root.ID,
Type: types.DepParentChild,
}
if err := w.AddDependency(ctx, dep, actorName); err != nil {
return nil, fmt.Errorf("failed to link digest to root: %w", err)
}
// Delete ephemeral children within the same transaction
if !keepChildren {
for _, id := range childIDs {
if err := w.DeleteIssue(ctx, id, actorName); err != nil {
return nil, fmt.Errorf("failed to delete child %s: %w", id, err)
}
result.DeletedCount++
}
}
// Auto-close the root if it's a wisp — squash completes the molecule lifecycle
if root.Ephemeral {
reason := fmt.Sprintf("Squashed: %d steps → digest %s", len(children), result.DigestID)
if err := w.CloseIssue(ctx, root.ID, reason, actorName); err != nil {
return nil, fmt.Errorf("failed to close wisp root %s: %w", root.ID, err)
}
// Clear ephemeral so the closed root stops being re-emitted by every wisp-table export cycle.
if err := w.UpdateIssue(ctx, root.ID, map[string]interface{}{"wisp": false}, actorName); err != nil {
return nil, fmt.Errorf("failed to clear ephemeral flag on root %s: %w", root.ID, err)
}
result.WispSquash = true
}
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 ephemeral children after squash")
molSquashCmd.Flags().String("summary", "", "Agent-provided summary (bypasses auto-generation)")
molCmd.AddCommand(molSquashCmd)
}