Browse Source

fix: persist prompt field; apply 7-day retention to bg-tasks dir

Addresses greptile P2 review comments:
- PersistedTask now includes prompt, eliminating the empty-string stub
  in loadPersistedTask and keeping the BackgroundTask invariant intact
- cleanupOldLogs now sweeps bg-tasks/*.json with the same 7-day
  RETENTION_MS used for log files, preventing unbounded disk growth
Thomas Dyar 4 months ago
parent
commit
549813a719
2 changed files with 24 additions and 2 deletions
  1. 3 2
      src/background/background-manager.ts
  2. 21 0
      src/utils/logger.ts

+ 3 - 2
src/background/background-manager.ts

@@ -45,6 +45,7 @@ interface PersistedTask {
   parentSessionId: string;
   description: string;
   agent: string;
+  prompt: string;
   status: BackgroundTask['status'];
   result?: string;
   error?: string;
@@ -62,6 +63,7 @@ function persistTask(task: BackgroundTask): void {
       parentSessionId: task.parentSessionId,
       description: task.description,
       agent: task.agent,
+      prompt: task.prompt,
       status: task.status,
       result: task.result,
       error: task.error,
@@ -89,8 +91,7 @@ function loadPersistedTask(taskId: string): BackgroundTask | null {
       error: data.error,
       startedAt: new Date(data.startedAt),
       completedAt: data.completedAt ? new Date(data.completedAt) : undefined,
-      // Not persisted; callers use status/result only
-      prompt: '',
+      prompt: data.prompt,
       config: { maxConcurrentStarts: 10 },
     };
   } catch {

+ 21 - 0
src/utils/logger.ts

@@ -35,6 +35,27 @@ function cleanupOldLogs(logDir: string): void {
   } catch {
     // Directory may not exist yet — that's fine
   }
+
+  // Apply the same 7-day retention to persisted background task files
+  try {
+    const bgTaskDir = path.join(logDir, 'bg-tasks');
+    const taskFiles = fs.readdirSync(bgTaskDir);
+    const now = Date.now();
+    for (const entry of taskFiles) {
+      if (!entry.endsWith('.json')) continue;
+      const filePath = path.join(bgTaskDir, entry);
+      try {
+        const stat = fs.statSync(filePath);
+        if (now - stat.mtimeMs > RETENTION_MS) {
+          fs.unlinkSync(filePath);
+        }
+      } catch {
+        // Skip individual file errors
+      }
+    }
+  } catch {
+    // bg-tasks dir may not exist yet — that's fine
+  }
 }
 
 export function initLogger(sessionId: string): void {