feat(import): offer Claude history import at the end of enable · Entire

feat(import): offer Claude history import at the end of enable

0fecc24→main·

Sessions

996ef1f5522cView transcript

Changes

2

13 unmodified lines

14
15
16
17
18
19
20
1151 unmodified lines

1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
688 unmodified lines

1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937

13 unmodified lines

"github.com/entireio/cli/cmd/entire/cli/agent/external"
    "github.com/entireio/cli/cmd/entire/cli/agent/types"
    "github.com/entireio/cli/cmd/entire/cli/gitremote"
    "github.com/entireio/cli/cmd/entire/cli/importclaude"
    "github.com/entireio/cli/cmd/entire/cli/interactive"
    "github.com/entireio/cli/cmd/entire/cli/logging"
    "github.com/entireio/cli/cmd/entire/cli/paths"
1151 unmodified lines

return nil
    }

if err := promptImportClaudeContext(ctx, w, opts); err != nil {
        return err
    }

fmt.Fprintln(w, "\nReady.")

// Note about empty repos at the end, after setup is complete
688 unmodified lines

return nil
}

// promptImportClaudeContext offers, at enable time, to import the repo's
// existing Claude Code transcripts (past month) as local, read-only history.
// No-op when non-interactive, under --yes, or when nothing is found.
func promptImportClaudeContext(ctx context.Context, w io.Writer, opts EnableOptions) error {
    if opts.Yes || !interactive.CanPromptInteractively() {
        return nil
    }
    repoRoot, err := paths.WorktreeRoot(ctx)
    if err != nil {
        return nil //nolint:nilerr // not a git repo / nothing to import
    }
    now := time.Now()
    files, err := importclaude.DiscoverSessions(repoRoot, "", now, nil)
    if err != nil || len(files) == 0 {
        return nil //nolint:nilerr // best-effort discovery
    }
    turnCount := 0
    for _, f := range files {
        data, rerr := os.ReadFile(f)
        if rerr != nil {
            continue
        }
        urns, terr := importclaude.SplitTurns(data, "")
        if terr != nil {
            continue
        }
        turnCount += len(turns)
    }
    if turnCount == 0 {
        return nil
    }

doImport := false
    form := NewAccessibleForm(
        huh.NewGroup(
            huh.NewConfirm().
                Title(fmt.Sprintf("Found %d past Claude session turn(s) for this repo from the past month.", turnCount)).
                Description("Import them as local, read-only history? It stays on your machine and is never pushed.").
                Affirmative("Yes").
                Negative("No").
                Value(&doImport),
            ),
        )
    if err := form.Run(); err != nil {
        return fmt.Errorf("import prompt: %w", err)
    }
    if !doImport {
        return nil
    }
    repo, err := strategy.OpenRepository(ctx)
    if err != nil {
        return fmt.Errorf("open repository: %w", err)
    }
    defer repo.Close()
    res, err := importclaude.Run(ctx, repo, importclaude.Options{RepoRoot: repoRoot, Now: now})
    if err != nil {
        return err
    }
    fmt.Fprintf(w, "  ✓ Imported %d turn(s) of Claude history (local, read-only)\n", res.TurnsImported)
    return nil
}

// promptTelemetryConsent asks the user if they want to enable telemetry.
// It modifies settings.Telemetry based on the user's choice or flags.
// The caller is responsible for saving settings.

Mcmd/entire/cli/setup.go+67

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

package cli

import (
    "bytes"
    "context"
    "os"
    "path/filepath"
    "testing"

"github.com/entireio/cli/cmd/entire/cli/checkpoint"
    "github.com/entireio/cli/cmd/entire/cli/strategy"
    "github.com/entireio/cli/cmd/entire/cli/testutil"
)

func TestPromptImportClaudeContext_SkipsWhenNoTranscripts(t *testing.T) {
    // Not parallel: t.Chdir + t.Setenv.
    repoDir := t.TempDir()
    testutil.InitRepo(t, repoDir)
    t.Chdir(repoDir)
    t.Setenv("ENTIRE_TEST_CLAUDE_PROJECT_DIR", t.TempDir()) // empty dir
    t.Setenv("ENTIRE_TEST_TTY", "0")                        // non-interactive

var out bytes.Buffer
    if err := promptImportClaudeContext(context.Background(), &out, EnableOptions{}); err != nil {
        t.Fatal(err)
    }
    if out.Len() != 0 {
        t.Fatalf("expected silent no-op, got %q", out.String())
    }
}

func TestPromptImportClaudeContext_NonInteractiveDoesNotImport(t *testing.T) {
    // Not parallel: t.Chdir + t.Setenv.
    repoDir := t.TempDir()
    testutil.InitRepo(t, repoDir)
    testutil.WriteFile(t, repoDir, "f.txt", "x")
    testutil.GitAdd(t, repoDir, "f.txt")
    testutil.GitCommit(t, repoDir, "init")
    t.Chdir(repoDir)

cdir := t.TempDir()
    if err := os.WriteFile(filepath.Join(cdir, "s.jsonl"),
        []byte(`{"type":"user","uuid":"u1","message":{"role":"user","content":"hi"}}`+"\n"), 0o644); err != nil {
        t.Fatal(err)
    }
    t.Setenv("ENTIRE_TEST_CLAUDE_PROJECT_DIR", cdir)
    t.Setenv("ENTIRE_TEST_TTY", "0") // non-interactive => must not prompt or import

var out bytes.Buffer
    if err := promptImportClaudeContext(context.Background(), &out, EnableOptions{}); err != nil {
        t.Fatal(err)
    }

repo, err := strategy.OpenRepository(context.Background())
    if err != nil {
        t.Fatal(err)
    }
    defer repo.Close()
    importsRefs := checkpoint.ImportsRefs()
    stores, err := checkpoint.Open(context.Background(), repo, checkpoint.OpenOptions{Refs: &importsRefs})
    if err != nil {
        t.Fatal(err)
    }
    infos, err := stores.Persistent.List(context.Background())
    if err != nil {
        t.Fatal(err)
    }
    if len(infos) != 0 {
        t.Fatalf("non-interactive import should not have written: %+v", infos)
    }
}

Acmd/entire/cli/setup_import_prompt_test.go+71