caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

242 lines
8.0 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/ui"
)
var restoreApply bool
var restoreCmd = &cobra.Command{
Use: "restore <issue-id>",
GroupID: "sync",
Short: "Restore the pre-compaction content of a compacted issue",
Long: `Restore the pre-compaction content of a compacted issue.
When an issue is compacted, its description/design/notes/acceptance criteria
are summarized and the originals are archived to a compaction snapshot. This
command recovers that original content.
By default it is read-only: it displays the archived content without modifying
the database. Pass --apply to write the original content back into the issue
and step its compaction level back down.
If no archived snapshot exists (e.g. the issue was compacted by an older bd
before snapshot archiving), restore falls back to a best-effort reconstruction
from Dolt version history, which can only be displayed, not applied.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
issueID := args[0]
ctx := rootCtx
// Get the issue
issue, err := store.GetIssue(ctx, issueID)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
fmt.Fprintf(os.Stderr, "Error: issue '%s' not found\n", issueID)
} else {
fmt.Fprintf(os.Stderr, "Error: issue '%s' not found: %v\n", issueID, err)
}
os.Exit(1)
}
// Check if issue is compacted
if issue.CompactionLevel == 0 {
fmt.Fprintf(os.Stderr, "Error: issue %s is not compacted\n", issueID)
fmt.Fprintf(os.Stderr, "Hint: only compacted issues need restoration\n")
os.Exit(1)
}
// Prefer the archived snapshot: it is the authoritative pre-compaction
// copy and the only source that can be safely written back.
snap, err := store.GetCompactionSnapshot(ctx, issueID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to read compaction snapshot: %v\n", err)
os.Exit(1)
}
if restoreApply {
if snap == nil {
fmt.Fprintf(os.Stderr, "Error: no archived snapshot for %s; cannot safely restore content\n", issueID)
fmt.Fprintf(os.Stderr, "Hint: this issue was compacted before snapshot archiving existed.\n")
fmt.Fprintf(os.Stderr, " Run 'bd restore %s' (without --apply) to view the best-effort\n", issueID)
fmt.Fprintf(os.Stderr, " version reconstructed from Dolt history.\n")
os.Exit(1)
}
applied, err := store.RestoreFromSnapshot(ctx, issueID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to restore issue: %v\n", err)
os.Exit(1)
}
if applied == nil {
fmt.Fprintf(os.Stderr, "Error: no archived snapshot for %s\n", issueID)
os.Exit(1)
}
restored, err := store.GetIssue(ctx, issueID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: restored, but failed to re-read issue: %v\n", err)
os.Exit(1)
}
if jsonOutput {
outputJSON(restored)
return
}
fmt.Printf("%s Restored %s from archived snapshot (compaction level %d โ†’ %d)\n",
ui.RenderPass("โœ“"), issueID, issue.CompactionLevel, restored.CompactionLevel)
return
}
// Read-only display path. Prefer the archived snapshot; fall back to the
// Dolt-history heuristic when no snapshot exists.
if snap != nil {
view := snapshotView(issue, snap)
if jsonOutput {
outputJSON(view)
} else {
displayRestoredIssue(view, "archived snapshot")
fmt.Printf("%s\n", ui.RenderMuted("Run 'bd restore "+issueID+" --apply' to write this content back."))
}
return
}
// Query Dolt history for the pre-compaction version
history, err := store.History(ctx, issueID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to query history: %v\n", err)
os.Exit(1)
}
if len(history) == 0 {
fmt.Fprintf(os.Stderr, "Error: no history found for issue %s\n", issueID)
fmt.Fprintf(os.Stderr, "Hint: issue may have been compacted before Dolt history was available\n")
os.Exit(1)
}
// Find the pre-compaction version: the history entry with the most content.
// History is ordered by commit_date DESC, so we scan all entries.
var best *storage.HistoryEntry
bestSize := 0
for _, entry := range history {
size := issueContentSize(entry.Issue)
if size > bestSize {
bestSize = size
best = entry
}
}
if best == nil || bestSize <= issueContentSize(issue) {
fmt.Fprintf(os.Stderr, "Error: no pre-compaction version found in Dolt history\n")
fmt.Fprintf(os.Stderr, "Hint: issue may have been compacted before Dolt history was available\n")
os.Exit(1)
}
if jsonOutput {
outputJSON(best.Issue)
} else {
hashDisplay := best.CommitHash
if len(hashDisplay) > 8 {
hashDisplay = hashDisplay[:8]
}
displayRestoredIssue(best.Issue, "Dolt commit "+hashDisplay)
}
},
}
// snapshotView returns a copy of the current issue with its text content
// overlaid by the archived pre-compaction snapshot, for read-only display.
func snapshotView(issue *types.Issue, snap *types.IssueSnapshot) *types.Issue {
view := *issue
if snap.Title != "" {
view.Title = snap.Title
}
view.Description = snap.Description
view.Design = snap.Design
view.Notes = snap.Notes
view.AcceptanceCriteria = snap.AcceptanceCriteria
return &view
}
// issueContentSize returns the total text content size of an issue.
func issueContentSize(issue *types.Issue) int {
return len(issue.Description) + len(issue.Design) + len(issue.AcceptanceCriteria) + len(issue.Notes)
}
func init() {
restoreCmd.Flags().BoolVar(&jsonOutput, "json", false, "Output restore results in JSON format")
restoreCmd.Flags().BoolVar(&restoreApply, "apply", false, "Write the restored content back into the issue (default: display only)")
rootCmd.AddCommand(restoreCmd)
}
// displayRestoredIssue displays the restored issue in a readable format.
// provenance describes where the content came from (e.g. "archived snapshot"
// or "Dolt commit abc12345").
func displayRestoredIssue(issue *types.Issue, provenance string) {
fmt.Printf("\n%s %s (restored from %s)\n", ui.RenderAccent("๐Ÿ“œ"), ui.RenderBold(issue.ID), ui.RenderWarn(provenance))
fmt.Printf("%s\n\n", ui.RenderBold(issue.Title))
if issue.Description != "" {
fmt.Printf("%s\n%s\n\n", ui.RenderBold("Description:"), issue.Description)
}
if issue.Design != "" {
fmt.Printf("%s\n%s\n\n", ui.RenderBold("Design:"), issue.Design)
}
if issue.AcceptanceCriteria != "" {
fmt.Printf("%s\n%s\n\n", ui.RenderBold("Acceptance Criteria:"), issue.AcceptanceCriteria)
}
if issue.Notes != "" {
fmt.Printf("%s\n%s\n\n", ui.RenderBold("Notes:"), issue.Notes)
}
fmt.Printf("%s %s | %s %d | %s %s\n",
ui.RenderBold("Status:"), issue.Status,
ui.RenderBold("Priority:"), issue.Priority,
ui.RenderBold("Type:"), issue.IssueType,
)
if issue.Assignee != "" {
fmt.Printf("%s %s\n", ui.RenderBold("Assignee:"), issue.Assignee)
}
if len(issue.Labels) > 0 {
fmt.Printf("%s %s\n", ui.RenderBold("Labels:"), strings.Join(issue.Labels, ", "))
}
fmt.Printf("\n%s %s\n", ui.RenderBold("Created:"), issue.CreatedAt.Format("2006-01-02 15:04:05"))
fmt.Printf("%s %s\n", ui.RenderBold("Updated:"), issue.UpdatedAt.Format("2006-01-02 15:04:05"))
if issue.ClosedAt != nil {
fmt.Printf("%s %s\n", ui.RenderBold("Closed:"), issue.ClosedAt.Format("2006-01-02 15:04:05"))
}
if len(issue.Dependencies) > 0 {
fmt.Printf("\n%s\n", ui.RenderBold("Dependencies:"))
for _, dep := range issue.Dependencies {
fmt.Printf(" %s %s (%s)\n", ui.RenderPass("โ†’"), dep.DependsOnID, dep.Type)
}
}
if issue.CompactionLevel > 0 {
fmt.Printf("\n%s Level %d", ui.RenderWarn("โš ๏ธ This issue was compacted:"), issue.CompactionLevel)
if issue.CompactedAt != nil {
fmt.Printf(" at %s", issue.CompactedAt.Format("2006-01-02 15:04:05"))
}
if issue.OriginalSize > 0 {
currentSize := len(issue.Description) + len(issue.Design) + len(issue.AcceptanceCriteria) + len(issue.Notes)
reduction := 100 * (1 - float64(currentSize)/float64(issue.OriginalSize))
fmt.Printf(" (%.1f%% size reduction)", reduction)
}
fmt.Println()
}
fmt.Println()
}