This repository was archived by the owner on May 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
342 lines (304 loc) · 9 KB
/
Copy pathmain.go
File metadata and controls
342 lines (304 loc) · 9 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
_ "modernc.org/sqlite"
)
const Version = "2.0.0"
// Resolved holds a fully resolved conversation entry
type Resolved struct {
CID, Title, Source string
Blob []byte
Workspace string
Mtime float64
}
func main() {
// Handle --clean-ghosts flag
if len(os.Args) > 1 && os.Args[1] == "--clean-ghosts" {
runCleanGhosts()
return
}
fmt.Println()
fmt.Println(strings.Repeat("=", 64))
fmt.Printf(" Antigravity Doctor v%s\n", Version)
fmt.Println(" Self-healing conversation index rebuilder")
fmt.Println(" NO REBOOT REQUIRED")
fmt.Println(strings.Repeat("=", 64))
fmt.Println()
if isAntigravityRunning() {
fmt.Println(" WARNING: Antigravity is still running!")
fmt.Println(" Close it first (File > Exit or Task Manager).")
fmt.Println()
fmt.Print(" Press Enter after closing (or Q to quit): ")
var input string
fmt.Scanln(&input)
if strings.TrimSpace(strings.ToLower(input)) == "q" {
return
}
}
paths := detectPaths()
if err := paths.Validate(); err != nil {
fmt.Printf(" ERROR: %v\n", err)
waitExit()
return
}
convs, err := discoverConversations(paths.ConversationsDir)
if err != nil || len(convs) == 0 {
fmt.Println(" No conversations found on disk.")
waitExit()
return
}
fmt.Printf(" Found %d conversations on disk\n", len(convs))
annStats := checkAnnotations(paths.AnnotationsDir, convs)
fmt.Printf(" Annotations: %d ghosts, %d orphans\n", annStats.Ghosts, annStats.Orphans)
cleaned := cleanTempFiles(paths.ConversationsDir)
if cleaned > 0 {
fmt.Printf(" Cleaned %d stale .tmp files\n", cleaned)
}
fmt.Println(" Reading existing metadata...")
titles, blobs := extractExistingMetadata(paths.DBPath)
fmt.Printf(" Preserved %d titles, %d metadata blobs\n", len(titles), len(blobs))
knownWS := loadWorkspaceURIs(paths.WorkspaceStorageDir)
fmt.Printf(" Loaded %d known workspaces\n", len(knownWS))
fmt.Println()
// Resolve all conversations
fmt.Println(" Scanning conversations (newest first):")
fmt.Println(" " + strings.Repeat("-", 60))
var entries []Resolved
stats := map[string]int{"brain": 0, "preserved": 0, "fallback": 0}
markers := map[string]string{"brain": "+", "preserved": "~", "fallback": "?"}
for i, c := range convs {
title, src := resolveTitle(c.ID, titles, paths.BrainDir, c.Mtime)
blob := blobs[c.ID]
ws := inferWorkspace(c.ID, paths.BrainDir, knownWS)
entries = append(entries, Resolved{c.ID, title, src, blob, ws, c.Mtime})
stats[src]++
wsFlag := ""
if ws != "" {
wsFlag = " [WS]"
}
dt := title
if len(dt) > 50 {
dt = dt[:50]
}
if i < 20 || i == len(convs)-1 {
fmt.Printf(" [%3d] %s %s%s\n", i+1, markers[src], dt, wsFlag)
} else if i == 20 {
fmt.Printf(" ... (%d more) ...\n", len(convs)-20)
}
}
fmt.Println(" " + strings.Repeat("-", 60))
fmt.Printf(" [+] brain: %d [~] preserved: %d [?] fallback: %d\n",
stats["brain"], stats["preserved"], stats["fallback"])
fmt.Println()
// Backup
backupDir := filepath.Join(filepath.Dir(exePath()), "antigravity_backup")
os.MkdirAll(backupDir, 0755)
ts := time.Now().Format("20060102_150405")
backupPath := filepath.Join(backupDir, fmt.Sprintf("state.vscdb.%s.bak", ts))
copyFile(paths.DBPath, backupPath)
fmt.Printf(" Backup saved: %s\n\n", backupPath)
// FIX 1: trajectorySummaries
fmt.Println(" [1/4] Rebuilding trajectorySummaries...")
var trajBytes []byte
for _, e := range entries {
entry := buildTrajectoryEntry(e.CID, e.Title, e.Blob, e.Workspace, e.Mtime)
trajBytes = append(trajBytes, encodeLengthDelimited(1, entry)...)
}
upsertDBKey(paths.DBPath, "antigravityUnifiedStateSync.trajectorySummaries",
base64.StdEncoding.EncodeToString(trajBytes))
fmt.Printf(" Done (%d entries)\n", len(entries))
// FIX 2: ChatSessionStore.index
fmt.Println(" [2/4] Rebuilding ChatSessionStore.index...")
upsertDBKey(paths.DBPath, "chat.ChatSessionStore.index", buildChatIndex(entries))
fmt.Printf(" Done (%d entries)\n", len(entries))
// FIX 3: Workspace indices
fmt.Println(" [3/4] Patching workspace indices...")
wsPatch := fixWorkspaceIndices(paths.WorkspaceStorageDir, entries)
fmt.Printf(" Patched %d workspace databases\n", wsPatch)
// FIX 4: Missing annotations
fmt.Println(" [4/4] Creating missing annotations...")
created := createMissingAnnotations(paths.AnnotationsDir, annStats.OrphanIDs, paths.ConversationsDir)
fmt.Printf(" Created %d annotations\n", created)
fmt.Println()
fmt.Println(" " + strings.Repeat("=", 60))
fmt.Printf(" SUCCESS! Index rebuilt with %d conversations.\n", len(entries))
fmt.Println(" " + strings.Repeat("=", 60))
fmt.Println()
fmt.Println(" NEXT: Close Antigravity if open, then reopen it.")
fmt.Println(" NO REBOOT NEEDED!")
fmt.Println()
waitExit()
}
// === Helpers ===
func exePath() string {
p, err := os.Executable()
if err != nil {
return "."
}
return p
}
func waitExit() {
fmt.Print(" Press Enter to close...")
fmt.Scanln()
}
func isAntigravityRunning() bool {
if runtime.GOOS == "windows" {
cmd := exec.Command("tasklist", "/FI", "IMAGENAME eq Antigravity.exe")
hideConsoleWindow(cmd)
out, err := cmd.Output()
if err == nil && strings.Contains(strings.ToLower(string(out)), "antigravity.exe") {
return true
}
} else {
out, _ := exec.Command("pgrep", "-f", "antigravity").Output()
if strings.TrimSpace(string(out)) != "" {
return true
}
}
return false
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}
func buildChatIndex(entries []Resolved) string {
type chatEntry struct {
IsActive bool `json:"isActive"`
SessionID string `json:"sessionId"`
}
type chatIndex struct {
Version int `json:"version"`
Entries map[string]chatEntry `json:"entries"`
}
idx := chatIndex{Version: 1, Entries: make(map[string]chatEntry)}
for _, e := range entries {
idx.Entries[e.CID] = chatEntry{IsActive: false, SessionID: e.CID}
}
b, _ := json.Marshal(idx)
return string(b)
}
func fixWorkspaceIndices(wsDir string, entries []Resolved) int {
if wsDir == "" {
return 0
}
dirs, err := os.ReadDir(wsDir)
if err != nil {
return 0
}
// Build URI->hash map and URI->conversations map
uriToHash := map[string]string{}
for _, d := range dirs {
if !d.IsDir() {
continue
}
data, err := os.ReadFile(filepath.Join(wsDir, d.Name(), "workspace.json"))
if err != nil {
continue
}
var ws map[string]interface{}
if json.Unmarshal(data, &ws) == nil {
if uri, ok := ws["folder"].(string); ok && uri != "" {
uriToHash[uri] = d.Name()
} else if uri, ok := ws["workspace"].(string); ok && uri != "" {
uriToHash[uri] = d.Name()
}
}
}
// Group conversations by workspace URI
wsConvs := map[string][]Resolved{}
for _, e := range entries {
if e.Workspace != "" {
uri := pathToURI(e.Workspace)
wsConvs[uri] = append(wsConvs[uri], e)
}
}
// Write per-workspace indices
type chatEntry struct {
IsActive bool `json:"isActive"`
SessionID string `json:"sessionId"`
}
type chatIndex struct {
Version int `json:"version"`
Entries map[string]chatEntry `json:"entries"`
}
fixed := 0
for wsURI, convList := range wsConvs {
hash, ok := uriToHash[wsURI]
if !ok {
continue
}
dbPath := filepath.Join(wsDir, hash, "state.vscdb")
if _, err := os.Stat(dbPath); err != nil {
continue
}
idx := chatIndex{Version: 1, Entries: make(map[string]chatEntry)}
for _, c := range convList {
idx.Entries[c.CID] = chatEntry{IsActive: false, SessionID: c.CID}
}
b, _ := json.Marshal(idx)
upsertDBKey(dbPath, "chat.ChatSessionStore.index", string(b))
fixed++
}
return fixed
}
func upsertDBKey(dbPath, key, value string) {
db, err := sql.Open("sqlite", dbPath)
if err != nil {
return
}
defer db.Close()
var existing string
err = db.QueryRow("SELECT value FROM ItemTable WHERE key=?", key).Scan(&existing)
if err == nil {
db.Exec("UPDATE ItemTable SET value=? WHERE key=?", value, key)
} else {
db.Exec("INSERT INTO ItemTable (key, value) VALUES (?, ?)", key, value)
}
}
func runCleanGhosts() {
paths := detectPaths()
fmt.Println("Cleaning ghost annotations...")
if _, err := os.Stat(paths.AnnotationsDir); err != nil {
fmt.Println("No annotations directory found.")
return
}
convIDs := map[string]bool{}
files, _ := os.ReadDir(paths.ConversationsDir)
for _, f := range files {
if strings.HasSuffix(f.Name(), ".pb") {
convIDs[strings.TrimSuffix(f.Name(), ".pb")] = true
}
}
cleaned := 0
annFiles, _ := os.ReadDir(paths.AnnotationsDir)
for _, f := range annFiles {
if strings.HasSuffix(f.Name(), ".pbtxt") {
id := strings.TrimSuffix(f.Name(), ".pbtxt")
if !convIDs[id] {
os.Remove(filepath.Join(paths.AnnotationsDir, f.Name()))
cleaned++
}
}
}
fmt.Printf("Cleaned %d ghost annotations.\n", cleaned)
}