11package main
22
33import (
4+ "bufio"
45 "context"
56 "encoding/json"
67 "flag"
@@ -142,20 +143,42 @@ func runWorkspaceQueryIndexClean(workspace string, args []string) error {
142143 fs := flag .NewFlagSet ("query index clean" , flag .ContinueOnError )
143144 fs .SetOutput (io .Discard )
144145 var jsonOut bool
146+ var yes bool
145147 fs .BoolVar (& jsonOut , "json" , false , "write JSON output" )
148+ fs .BoolVar (& yes , "yes" , false , "confirm removal of generated query index data" )
149+ fs .BoolVar (& yes , "y" , false , "confirm removal of generated query index data" )
146150 if err := fs .Parse (args ); err != nil {
147151 return fmt .Errorf ("%s" , workspaceQueryIndexUsageText (filepath .Base (os .Args [0 ])))
148152 }
149153 if fs .NArg () != 0 {
150154 return fmt .Errorf ("%s" , workspaceQueryIndexUsageText (filepath .Base (os .Args [0 ])))
151155 }
152- status , err := workspaceQueryIndexStatusForWorkspace (workspace , "/" )
156+ ctx := context .Background ()
157+ remote , err := openFSRemoteWorkspace (ctx , workspace )
153158 if err != nil {
154159 return err
155160 }
156- status .State = "clean"
157- status .Message = "No query index data was removed."
158- return writeWorkspaceQueryIndexStatus (status , jsonOut )
161+ defer remote .close ()
162+ if ! yes {
163+ ok , err := confirmWorkspaceQueryIndexClean (remote .selection .Name )
164+ if err != nil {
165+ return err
166+ }
167+ if ! ok {
168+ fmt .Println ()
169+ fmt .Println ("Query index clean cancelled." )
170+ fmt .Println ()
171+ return nil
172+ }
173+ }
174+ response , err := remote .controlPlane .CleanQueryIndex (ctx , remote .selection .ID , controlplane.WorkspaceQueryIndexCleanRequest {
175+ Workspace : remote .selection .Name ,
176+ Confirm : true ,
177+ })
178+ if err != nil {
179+ return err
180+ }
181+ return writeWorkspaceQueryIndexClean (response , jsonOut )
159182}
160183
161184func workspaceQueryIndexStatusForWorkspace (workspace , path string ) (controlplane.WorkspaceQueryIndexStatus , error ) {
@@ -271,6 +294,47 @@ func embeddingBackfillLabel(result controlplane.QueryEmbeddingBackfillResult) st
271294 return "off"
272295}
273296
297+ func writeWorkspaceQueryIndexClean (response controlplane.WorkspaceQueryIndexCleanResponse , jsonOut bool ) error {
298+ if jsonOut {
299+ enc := json .NewEncoder (os .Stdout )
300+ enc .SetIndent ("" , " " )
301+ return enc .Encode (response )
302+ }
303+ fmt .Fprintln (os .Stdout , "Query index clean" )
304+ fmt .Fprintln (os .Stdout )
305+ fmt .Fprintf (os .Stdout , "workspace %s\n " , response .Workspace )
306+ fmt .Fprintf (os .Stdout , "cleared %t\n " , response .Cleared )
307+ fmt .Fprintf (os .Stdout , "files %d\n " , response .RemovedFiles )
308+ fmt .Fprintf (os .Stdout , "chunks %d\n " , response .RemovedChunks )
309+ fmt .Fprintf (os .Stdout , "state %s\n " , response .Status .State )
310+ if response .Message != "" {
311+ fmt .Fprintln (os .Stdout )
312+ fmt .Fprintln (os .Stdout , response .Message )
313+ }
314+ return nil
315+ }
316+
317+ func confirmWorkspaceQueryIndexClean (workspace string ) (bool , error ) {
318+ return confirmWorkspaceQueryIndexCleanWithReader (workspace , bufio .NewReader (os .Stdin ))
319+ }
320+
321+ func confirmWorkspaceQueryIndexCleanWithReader (workspace string , reader * bufio.Reader ) (bool , error ) {
322+ workspace = strings .TrimSpace (workspace )
323+ if workspace == "" {
324+ return false , nil
325+ }
326+ fmt .Println ()
327+ fmt .Printf ("Are you sure you want to clear generated query index data for %s? Workspace files will not change. [y/N] " , workspace )
328+ raw , err := reader .ReadString ('\n' )
329+ if err != nil && strings .TrimSpace (raw ) == "" {
330+ fmt .Println ()
331+ return false , nil
332+ }
333+ fmt .Println ()
334+ answer := strings .ToLower (strings .TrimSpace (raw ))
335+ return answer == "y" || answer == "yes" , nil
336+ }
337+
274338func workspaceQueryIndexUsageText (bin string ) string {
275339 return brandHeaderString () + fmt .Sprintf (`Usage:
276340 %[1]s query index <status|create|rebuild|clean> [flags]
@@ -282,18 +346,20 @@ Subcommands:
282346 status Show keyword query projection and embedding state
283347 create Build keyword chunks and semantic embeddings
284348 rebuild Enqueue existing files for keyword query indexing
285- clean Remove stale query index data
349+ clean Clear generated query index data for the workspace
286350
287351Flags:
288352 --json Write JSON output
289353 --path <path> Scope status or rebuild to a workspace path
290354 --wait Wait for rebuild completion
291355 --force Rebuild existing chunks
292356 --embeddings Build semantic embeddings
357+ --yes, -y Confirm full-workspace query index cleanup
293358
294359Examples:
295360 %[1]s query index status
296361 %[1]s fs repo query index create --embeddings --wait
297362 %[1]s fs repo query index rebuild --path /cmd/afs --wait
363+ %[1]s query index clean --yes
298364` , bin )
299365}
0 commit comments