Jelajahi Sumber

fix(qmd): prune orphaned vectors without vacuum

Session-Id: 019ed817
Push-Allowed: yes
Claude 3 minggu lalu
induk
melakukan
bff0e428a6
3 mengubah file dengan 28 tambahan dan 8 penghapusan
  1. 11 4
      dist/cli/qmd.js
  2. 10 4
      src/cli/qmd.ts
  3. 7 0
      test/cli.test.ts

+ 11 - 4
dist/cli/qmd.js

@@ -2276,6 +2276,7 @@ function parseCLI() {
             "embed-batch-size": { type: "string" }, // Batch size for HTTP provider
             "embed-timeout-ms": { type: "string" }, // Per-request timeout
             "embed-auto-fallback": { type: "boolean" }, // Wrap openai in AutoFallback (local fallback)
+            "no-vacuum": { type: "boolean" }, // cleanup: skip VACUUM for cron-safe pruning
             // Update options
             pull: { type: "boolean" }, // git pull before update
             refresh: { type: "boolean" },
@@ -2494,7 +2495,7 @@ function showHelp() {
     console.log("    --embed-batch-size <n>      - Batch size for HTTP provider (default: 64)");
     console.log("    --embed-timeout-ms <n>      - Per-request timeout in ms (default: 30000)");
     console.log("    --embed-auto-fallback       - Wrap openai provider in local fallback (or QMD_EMBED_AUTO_FALLBACK)");
-    console.log("  qmd cleanup                   - Clear caches, vacuum DB");
+    console.log("  qmd cleanup [--no-vacuum]     - Clear caches and orphaned data; VACUUM unless --no-vacuum");
     console.log("");
     console.log("Query syntax (qmd query):");
     console.log("  QMD queries are either a single expand query (no prefix) or a multi-line");
@@ -3089,6 +3090,7 @@ if (isMain) {
         }
         case "cleanup": {
             const db = getDb();
+            const skipVacuum = !!cli.values["no-vacuum"];
             // 1. Clear llm_cache
             const cacheCount = deleteLLMCache(db);
             console.log(`${c.green}✓${c.reset} Cleared ${cacheCount} cached API responses`);
@@ -3105,9 +3107,14 @@ if (isMain) {
             if (inactiveDocs > 0) {
                 console.log(`${c.green}✓${c.reset} Removed ${inactiveDocs} inactive document records`);
             }
-            // 4. Vacuum to reclaim space
-            vacuumDatabase(db);
-            console.log(`${c.green}✓${c.reset} Database vacuumed`);
+            // 4. Vacuum to reclaim space unless this is a lightweight cron prune.
+            if (skipVacuum) {
+                console.log(`${c.dim}Skipped VACUUM (--no-vacuum)${c.reset}`);
+            }
+            else {
+                vacuumDatabase(db);
+                console.log(`${c.green}✓${c.reset} Database vacuumed`);
+            }
             closeDb();
             break;
         }

+ 10 - 4
src/cli/qmd.ts

@@ -2675,6 +2675,7 @@ function parseCLI() {
       "embed-batch-size": { type: "string" },        // Batch size for HTTP provider
       "embed-timeout-ms": { type: "string" },        // Per-request timeout
       "embed-auto-fallback": { type: "boolean" },    // Wrap openai in AutoFallback (local fallback)
+      "no-vacuum": { type: "boolean" },              // cleanup: skip VACUUM for cron-safe pruning
       // Update options
       pull: { type: "boolean" },  // git pull before update
       refresh: { type: "boolean" },
@@ -2909,7 +2910,7 @@ function showHelp(): void {
   console.log("    --embed-batch-size <n>      - Batch size for HTTP provider (default: 64)");
   console.log("    --embed-timeout-ms <n>      - Per-request timeout in ms (default: 30000)");
   console.log("    --embed-auto-fallback       - Wrap openai provider in local fallback (or QMD_EMBED_AUTO_FALLBACK)");
-  console.log("  qmd cleanup                   - Clear caches, vacuum DB");
+  console.log("  qmd cleanup [--no-vacuum]     - Clear caches and orphaned data; VACUUM unless --no-vacuum");
   console.log("");
   console.log("Query syntax (qmd query):");
   console.log("  QMD queries are either a single expand query (no prefix) or a multi-line");
@@ -3548,6 +3549,7 @@ if (isMain) {
 
     case "cleanup": {
       const db = getDb();
+      const skipVacuum = !!cli.values["no-vacuum"];
 
       // 1. Clear llm_cache
       const cacheCount = deleteLLMCache(db);
@@ -3567,9 +3569,13 @@ if (isMain) {
         console.log(`${c.green}✓${c.reset} Removed ${inactiveDocs} inactive document records`);
       }
 
-      // 4. Vacuum to reclaim space
-      vacuumDatabase(db);
-      console.log(`${c.green}✓${c.reset} Database vacuumed`);
+      // 4. Vacuum to reclaim space unless this is a lightweight cron prune.
+      if (skipVacuum) {
+        console.log(`${c.dim}Skipped VACUUM (--no-vacuum)${c.reset}`);
+      } else {
+        vacuumDatabase(db);
+        console.log(`${c.green}✓${c.reset} Database vacuumed`);
+      }
 
       closeDb();
       break;

+ 7 - 0
test/cli.test.ts

@@ -742,6 +742,13 @@ describe("CLI Cleanup Command", () => {
     const { stdout, exitCode } = await runQmd(["cleanup"]);
     expect(exitCode).toBe(0);
   });
+
+  test("cleanup --no-vacuum prunes without vacuuming", async () => {
+    const { stdout, exitCode } = await runQmd(["cleanup", "--no-vacuum"]);
+    expect(exitCode).toBe(0);
+    expect(stdout).toContain("Skipped VACUUM");
+    expect(stdout).not.toContain("Database vacuumed");
+  });
 });
 
 describe("CLI Error Handling", () => {