|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/lazypower/continuity/internal/hooks" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + extractForce bool |
| 16 | + extractTranscript string |
| 17 | + extractBackfillEmpty bool |
| 18 | +) |
| 19 | + |
| 20 | +var extractCmd = &cobra.Command{ |
| 21 | + Use: "extract [session-id]", |
| 22 | + Short: "Re-run extraction for a session", |
| 23 | + Long: `Trigger memory extraction for a completed session. |
| 24 | +
|
| 25 | +Typical uses: |
| 26 | + continuity extract <session-id> — re-extract if not already done |
| 27 | + continuity extract <session-id> --force — re-extract even if marked done |
| 28 | + continuity extract --backfill-empty — unmark every session that was |
| 29 | + flagged as extracted but has |
| 30 | + no memories attributed to it |
| 31 | +
|
| 32 | +When a session-id is given, continuity auto-discovers the transcript at |
| 33 | +~/.claude/projects/*/<session-id>.jsonl. Pass --transcript to override. |
| 34 | +
|
| 35 | +Requires a running server (continuity serve).`, |
| 36 | + Args: cobra.MaximumNArgs(1), |
| 37 | + RunE: runExtract, |
| 38 | +} |
| 39 | + |
| 40 | +func init() { |
| 41 | + extractCmd.Flags().BoolVar(&extractForce, "force", false, "Bypass the idempotency guard (re-extract already-extracted sessions)") |
| 42 | + extractCmd.Flags().StringVar(&extractTranscript, "transcript", "", "Path to transcript JSONL (overrides auto-discovery)") |
| 43 | + extractCmd.Flags().BoolVar(&extractBackfillEmpty, "backfill-empty", false, "Unmark every session marked extracted with zero attributed memories") |
| 44 | +} |
| 45 | + |
| 46 | +func runExtract(cmd *cobra.Command, args []string) error { |
| 47 | + client := hooks.NewClient() |
| 48 | + if !client.Healthy() { |
| 49 | + return fmt.Errorf("continuity server is not running — start it with: continuity serve") |
| 50 | + } |
| 51 | + |
| 52 | + if extractBackfillEmpty { |
| 53 | + if len(args) > 0 || extractForce || extractTranscript != "" { |
| 54 | + return fmt.Errorf("--backfill-empty cannot be combined with a session-id, --force, or --transcript") |
| 55 | + } |
| 56 | + return runBackfillEmpty(client) |
| 57 | + } |
| 58 | + |
| 59 | + if len(args) != 1 { |
| 60 | + return fmt.Errorf("session-id is required (or use --backfill-empty)") |
| 61 | + } |
| 62 | + sessionID := strings.TrimSpace(args[0]) |
| 63 | + if sessionID == "" { |
| 64 | + return fmt.Errorf("session-id is required") |
| 65 | + } |
| 66 | + |
| 67 | + transcriptPath := extractTranscript |
| 68 | + if transcriptPath == "" { |
| 69 | + found, err := findTranscript(sessionID) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + transcriptPath = found |
| 74 | + } |
| 75 | + if _, err := os.Stat(transcriptPath); err != nil { |
| 76 | + return fmt.Errorf("transcript not readable: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + body, _ := json.Marshal(map[string]any{ |
| 80 | + "transcript_path": transcriptPath, |
| 81 | + "force": extractForce, |
| 82 | + }) |
| 83 | + |
| 84 | + data, err := client.Post("/api/sessions/"+sessionID+"/extract", body) |
| 85 | + if err != nil { |
| 86 | + if len(data) > 0 { |
| 87 | + var resp struct { |
| 88 | + Error string `json:"error"` |
| 89 | + } |
| 90 | + if jsonErr := json.Unmarshal(data, &resp); jsonErr == nil && resp.Error != "" { |
| 91 | + return fmt.Errorf("%s", resp.Error) |
| 92 | + } |
| 93 | + } |
| 94 | + return fmt.Errorf("extract: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + fmt.Printf("extraction queued for %s (transcript: %s, force: %v)\n", sessionID, transcriptPath, extractForce) |
| 98 | + fmt.Println("check serve.log for progress — extraction runs asynchronously") |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func runBackfillEmpty(client *hooks.Client) error { |
| 103 | + data, err := client.Post("/api/sessions/unmark-empty-extractions", nil) |
| 104 | + if err != nil { |
| 105 | + if len(data) > 0 { |
| 106 | + var resp struct { |
| 107 | + Error string `json:"error"` |
| 108 | + } |
| 109 | + if jsonErr := json.Unmarshal(data, &resp); jsonErr == nil && resp.Error != "" { |
| 110 | + return fmt.Errorf("%s", resp.Error) |
| 111 | + } |
| 112 | + } |
| 113 | + return fmt.Errorf("backfill: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + var resp struct { |
| 117 | + Status string `json:"status"` |
| 118 | + Unmarked int64 `json:"unmarked"` |
| 119 | + Error string `json:"error"` |
| 120 | + } |
| 121 | + if err := json.Unmarshal(data, &resp); err != nil { |
| 122 | + return fmt.Errorf("parse response: %w", err) |
| 123 | + } |
| 124 | + if resp.Error != "" { |
| 125 | + return fmt.Errorf("%s", resp.Error) |
| 126 | + } |
| 127 | + |
| 128 | + fmt.Printf("unmarked %d session(s) that were extracted with no attributed memories\n", resp.Unmarked) |
| 129 | + if resp.Unmarked > 0 { |
| 130 | + fmt.Println("they will be re-extracted on their next Stop/SessionEnd hook,") |
| 131 | + fmt.Println("or force one now with: continuity extract <session-id> --force") |
| 132 | + } |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +// findTranscript searches ~/.claude/projects/*/<session-id>.jsonl for a |
| 137 | +// Claude Code transcript matching the given session id. The sessionID is |
| 138 | +// validated first — path separators or ".." would let a glob pattern escape |
| 139 | +// ~/.claude/projects, which is surprising for "auto-discovery". Callers who |
| 140 | +// genuinely need to point at a transcript outside that tree should pass |
| 141 | +// --transcript explicitly. |
| 142 | +func findTranscript(sessionID string) (string, error) { |
| 143 | + if err := validateSessionIDForGlob(sessionID); err != nil { |
| 144 | + return "", fmt.Errorf("%w — pass --transcript to point at a specific file", err) |
| 145 | + } |
| 146 | + home, err := os.UserHomeDir() |
| 147 | + if err != nil { |
| 148 | + return "", fmt.Errorf("resolve home dir: %w", err) |
| 149 | + } |
| 150 | + pattern := filepath.Join(home, ".claude", "projects", "*", sessionID+".jsonl") |
| 151 | + matches, err := filepath.Glob(pattern) |
| 152 | + if err != nil { |
| 153 | + return "", fmt.Errorf("glob transcripts: %w", err) |
| 154 | + } |
| 155 | + if len(matches) == 0 { |
| 156 | + return "", fmt.Errorf("no transcript found for session %s (looked in %s)", sessionID, pattern) |
| 157 | + } |
| 158 | + if len(matches) > 1 { |
| 159 | + return "", fmt.Errorf("multiple transcripts found for %s — pass --transcript to disambiguate:\n %s", sessionID, strings.Join(matches, "\n ")) |
| 160 | + } |
| 161 | + return matches[0], nil |
| 162 | +} |
| 163 | + |
| 164 | +// validateSessionIDForGlob rejects session IDs that would let the |
| 165 | +// auto-discovery glob escape ~/.claude/projects. Real Claude Code session |
| 166 | +// IDs are UUIDs, but continuity imports from other sources so we don't |
| 167 | +// require that — we just refuse anything that would traverse the filesystem. |
| 168 | +func validateSessionIDForGlob(sessionID string) error { |
| 169 | + if sessionID == "" { |
| 170 | + return fmt.Errorf("session-id is empty") |
| 171 | + } |
| 172 | + if strings.ContainsAny(sessionID, `/\`) { |
| 173 | + return fmt.Errorf("session-id %q contains a path separator", sessionID) |
| 174 | + } |
| 175 | + if sessionID == "." || sessionID == ".." || strings.Contains(sessionID, "..") { |
| 176 | + return fmt.Errorf("session-id %q contains path traversal", sessionID) |
| 177 | + } |
| 178 | + return nil |
| 179 | +} |
0 commit comments